Merge git://git.kernel.org/pub/scm/linux/kernel/git/lwfinger/linux-staging into stagi...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / hv / storvsc_drv.c
1 /*
2 * Copyright (c) 2009, Microsoft Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
16 *
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
20 */
21 #include <linux/init.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <linux/device.h>
25 #include <linux/blkdev.h>
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_cmnd.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_tcq.h>
31 #include <scsi/scsi_eh.h>
32 #include <scsi/scsi_devinfo.h>
33 #include <scsi/scsi_dbg.h>
34 #include "hv_api.h"
35 #include "logging.h"
36 #include "version_info.h"
37 #include "vmbus.h"
38 #include "storvsc_api.h"
39 #include "vstorage.h"
40 #include "channel.h"
41
42
43 static const char *g_driver_name = "storvsc";
44
45 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
46 static const struct hv_guid gStorVscDeviceType = {
47 .data = {
48 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
49 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f
50 }
51 };
52
53 struct host_device_context {
54 /* must be 1st field
55 * FIXME this is a bug */
56 /* point back to our device context */
57 struct hv_device *device_ctx;
58 struct kmem_cache *request_pool;
59 unsigned int port;
60 unsigned char path;
61 unsigned char target;
62 };
63
64 struct storvsc_cmd_request {
65 struct list_head entry;
66 struct scsi_cmnd *cmd;
67
68 unsigned int bounce_sgl_count;
69 struct scatterlist *bounce_sgl;
70
71 struct hv_storvsc_request request;
72 };
73
74
75 /*
76 * stor_vsc_initialize - Main entry point
77 */
78 static int stor_vsc_initialize(struct hv_driver *driver)
79 {
80 struct storvsc_driver_object *stor_driver;
81
82 stor_driver = hvdr_to_stordr(driver);
83
84 DPRINT_DBG(STORVSC,
85 "sizeof(struct hv_storvsc_request)=%zd "
86 "sizeof(struct vstor_packet)=%zd, "
87 "sizeof(struct vmscsi_request)=%zd",
88 sizeof(struct hv_storvsc_request),
89 sizeof(struct vstor_packet),
90 sizeof(struct vmscsi_request));
91
92 /* Make sure we are at least 2 pages since 1 page is used for control */
93
94 driver->name = g_driver_name;
95 memcpy(&driver->dev_type, &gStorVscDeviceType,
96 sizeof(struct hv_guid));
97
98
99 /*
100 * Divide the ring buffer data size (which is 1 page less
101 * than the ring buffer size since that page is reserved for
102 * the ring buffer indices) by the max request size (which is
103 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
104 */
105 stor_driver->max_outstanding_req_per_channel =
106 ((stor_driver->ring_buffer_size - PAGE_SIZE) /
107 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
108 sizeof(struct vstor_packet) + sizeof(u64),
109 sizeof(u64)));
110
111 DPRINT_INFO(STORVSC, "max io %u, currently %u\n",
112 stor_driver->max_outstanding_req_per_channel,
113 STORVSC_MAX_IO_REQUESTS);
114
115 /* Setup the dispatch table */
116 stor_driver->base.dev_add = stor_vsc_on_device_add;
117 stor_driver->base.dev_rm = stor_vsc_on_device_remove;
118 stor_driver->base.cleanup = stor_vsc_on_cleanup;
119
120 stor_driver->on_io_request = stor_vsc_on_io_request;
121
122 return 0;
123 }
124
125 /* Static decl */
126 static int storvsc_probe(struct device *dev);
127 static int storvsc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd);
128 static int storvsc_device_alloc(struct scsi_device *);
129 static int storvsc_device_configure(struct scsi_device *);
130 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd);
131 static int storvsc_remove(struct device *dev);
132
133 static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
134 unsigned int sg_count,
135 unsigned int len);
136 static void destroy_bounce_buffer(struct scatterlist *sgl,
137 unsigned int sg_count);
138 static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count);
139 static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
140 struct scatterlist *bounce_sgl,
141 unsigned int orig_sgl_count);
142 static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
143 struct scatterlist *bounce_sgl,
144 unsigned int orig_sgl_count);
145
146 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device *bdev,
147 sector_t capacity, int *info);
148
149
150 static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
151 module_param(storvsc_ringbuffer_size, int, S_IRUGO);
152 MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
153
154 /* The one and only one */
155 static struct storvsc_driver_object g_storvsc_drv;
156
157 /* Scsi driver */
158 static struct scsi_host_template scsi_driver = {
159 .module = THIS_MODULE,
160 .name = "storvsc_host_t",
161 .bios_param = storvsc_get_chs,
162 .queuecommand = storvsc_queuecommand,
163 .eh_host_reset_handler = storvsc_host_reset_handler,
164 .slave_alloc = storvsc_device_alloc,
165 .slave_configure = storvsc_device_configure,
166 .cmd_per_lun = 1,
167 /* 64 max_queue * 1 target */
168 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
169 .this_id = -1,
170 /* no use setting to 0 since ll_blk_rw reset it to 1 */
171 /* currently 32 */
172 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
173 /*
174 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
175 * into 1 sg element. If set, we must limit the max_segment_size to
176 * PAGE_SIZE, otherwise we may get 1 sg element that represents
177 * multiple
178 */
179 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
180 .use_clustering = ENABLE_CLUSTERING,
181 /* Make sure we dont get a sg segment crosses a page boundary */
182 .dma_boundary = PAGE_SIZE-1,
183 };
184
185
186 /*
187 * storvsc_drv_init - StorVsc driver initialization.
188 */
189 static int storvsc_drv_init(void)
190 {
191 int ret;
192 struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv;
193 struct hv_driver *drv = &g_storvsc_drv.base;
194
195 storvsc_drv_obj->ring_buffer_size = storvsc_ringbuffer_size;
196
197 /* Callback to client driver to complete the initialization */
198 stor_vsc_initialize(&storvsc_drv_obj->base);
199
200 drv->priv = storvsc_drv_obj;
201
202 DPRINT_INFO(STORVSC_DRV,
203 "max outstanding reqs %u",
204 storvsc_drv_obj->max_outstanding_req_per_channel);
205
206 if (storvsc_drv_obj->max_outstanding_req_per_channel <
207 STORVSC_MAX_IO_REQUESTS) {
208 DPRINT_ERR(STORVSC_DRV,
209 "The number of outstanding io requests (%d) "
210 "is larger than that supported (%d) internally.",
211 STORVSC_MAX_IO_REQUESTS,
212 storvsc_drv_obj->max_outstanding_req_per_channel);
213 return -1;
214 }
215
216 drv->driver.name = storvsc_drv_obj->base.name;
217
218 drv->driver.probe = storvsc_probe;
219 drv->driver.remove = storvsc_remove;
220
221 /* The driver belongs to vmbus */
222 ret = vmbus_child_driver_register(&drv->driver);
223
224 return ret;
225 }
226
227
228 static int stor_vsc_on_host_reset(struct hv_device *device)
229 {
230 struct storvsc_device *stor_device;
231 struct hv_storvsc_request *request;
232 struct vstor_packet *vstor_packet;
233 int ret, t;
234
235 DPRINT_INFO(STORVSC, "resetting host adapter...");
236
237 stor_device = get_stor_device(device);
238 if (!stor_device) {
239 DPRINT_ERR(STORVSC, "unable to get stor device..."
240 "device being destroyed?");
241 return -1;
242 }
243
244 request = &stor_device->reset_request;
245 vstor_packet = &request->vstor_packet;
246
247 init_completion(&request->wait_event);
248
249 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
250 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
251 vstor_packet->vm_srb.path_id = stor_device->path_id;
252
253 ret = vmbus_sendpacket(device->channel, vstor_packet,
254 sizeof(struct vstor_packet),
255 (unsigned long)&stor_device->reset_request,
256 VM_PKT_DATA_INBAND,
257 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
258 if (ret != 0) {
259 DPRINT_ERR(STORVSC, "Unable to send reset packet %p ret %d",
260 vstor_packet, ret);
261 goto cleanup;
262 }
263
264 t = wait_for_completion_timeout(&request->wait_event, HZ);
265 if (t == 0) {
266 ret = -ETIMEDOUT;
267 goto cleanup;
268 }
269
270 DPRINT_INFO(STORVSC, "host adapter reset completed");
271
272 /*
273 * At this point, all outstanding requests in the adapter
274 * should have been flushed out and return to us
275 */
276
277 cleanup:
278 put_stor_device(device);
279 return ret;
280 }
281
282 static int storvsc_drv_exit_cb(struct device *dev, void *data)
283 {
284 struct device **curr = (struct device **)data;
285 *curr = dev;
286 return 1; /* stop iterating */
287 }
288
289 static void storvsc_drv_exit(void)
290 {
291 struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv;
292 struct hv_driver *drv = &g_storvsc_drv.base;
293 struct device *current_dev = NULL;
294 int ret;
295
296 while (1) {
297 current_dev = NULL;
298
299 /* Get the device */
300 ret = driver_for_each_device(&drv->driver, NULL,
301 (void *) &current_dev,
302 storvsc_drv_exit_cb);
303
304 if (ret)
305 DPRINT_WARN(STORVSC_DRV,
306 "driver_for_each_device returned %d", ret);
307
308 if (current_dev == NULL)
309 break;
310
311 /* Initiate removal from the top-down */
312 device_unregister(current_dev);
313 }
314
315 if (storvsc_drv_obj->base.cleanup)
316 storvsc_drv_obj->base.cleanup(&storvsc_drv_obj->base);
317
318 vmbus_child_driver_unregister(&drv->driver);
319 return;
320 }
321
322 /*
323 * storvsc_probe - Add a new device for this driver
324 */
325 static int storvsc_probe(struct device *device)
326 {
327 int ret;
328 struct hv_driver *drv =
329 drv_to_hv_drv(device->driver);
330 struct storvsc_driver_object *storvsc_drv_obj = drv->priv;
331 struct hv_device *device_obj = device_to_hv_device(device);
332 struct Scsi_Host *host;
333 struct host_device_context *host_device_ctx;
334 struct storvsc_device_info device_info;
335
336 if (!storvsc_drv_obj->base.dev_add)
337 return -1;
338
339 host = scsi_host_alloc(&scsi_driver,
340 sizeof(struct host_device_context));
341 if (!host) {
342 DPRINT_ERR(STORVSC_DRV, "unable to allocate scsi host object");
343 return -ENOMEM;
344 }
345
346 dev_set_drvdata(device, host);
347
348 host_device_ctx = (struct host_device_context *)host->hostdata;
349 memset(host_device_ctx, 0, sizeof(struct host_device_context));
350
351 host_device_ctx->port = host->host_no;
352 host_device_ctx->device_ctx = device_obj;
353
354 host_device_ctx->request_pool =
355 kmem_cache_create(dev_name(&device_obj->device),
356 sizeof(struct storvsc_cmd_request), 0,
357 SLAB_HWCACHE_ALIGN, NULL);
358
359 if (!host_device_ctx->request_pool) {
360 scsi_host_put(host);
361 return -ENOMEM;
362 }
363
364 device_info.port_number = host->host_no;
365 /* Call to the vsc driver to add the device */
366 ret = storvsc_drv_obj->base.dev_add(device_obj,
367 (void *)&device_info);
368 if (ret != 0) {
369 DPRINT_ERR(STORVSC_DRV, "unable to add scsi vsc device");
370 kmem_cache_destroy(host_device_ctx->request_pool);
371 scsi_host_put(host);
372 return -1;
373 }
374
375 /* host_device_ctx->port = device_info.PortNumber; */
376 host_device_ctx->path = device_info.path_id;
377 host_device_ctx->target = device_info.target_id;
378
379 /* max # of devices per target */
380 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
381 /* max # of targets per channel */
382 host->max_id = STORVSC_MAX_TARGETS;
383 /* max # of channels */
384 host->max_channel = STORVSC_MAX_CHANNELS - 1;
385
386 /* Register the HBA and start the scsi bus scan */
387 ret = scsi_add_host(host, device);
388 if (ret != 0) {
389 DPRINT_ERR(STORVSC_DRV, "unable to add scsi host device");
390
391 storvsc_drv_obj->base.dev_rm(device_obj);
392
393 kmem_cache_destroy(host_device_ctx->request_pool);
394 scsi_host_put(host);
395 return -1;
396 }
397
398 scsi_scan_host(host);
399 return ret;
400 }
401
402 /*
403 * storvsc_remove - Callback when our device is removed
404 */
405 static int storvsc_remove(struct device *device)
406 {
407 struct hv_driver *drv =
408 drv_to_hv_drv(device->driver);
409 struct storvsc_driver_object *storvsc_drv_obj = drv->priv;
410 struct hv_device *device_obj = device_to_hv_device(device);
411 struct Scsi_Host *host = dev_get_drvdata(device);
412 struct host_device_context *host_device_ctx =
413 (struct host_device_context *)host->hostdata;
414
415 /*
416 * Call to the vsc driver to let it know that the device is being
417 * removed
418 */
419 storvsc_drv_obj->base.dev_rm(device_obj);
420
421 if (host_device_ctx->request_pool) {
422 kmem_cache_destroy(host_device_ctx->request_pool);
423 host_device_ctx->request_pool = NULL;
424 }
425
426 DPRINT_INFO(STORVSC, "removing host adapter (%p)...", host);
427 scsi_remove_host(host);
428
429 DPRINT_INFO(STORVSC, "releasing host adapter (%p)...", host);
430 scsi_host_put(host);
431 return 0;
432 }
433
434 /*
435 * storvsc_commmand_completion - Command completion processing
436 */
437 static void storvsc_commmand_completion(struct hv_storvsc_request *request)
438 {
439 struct storvsc_cmd_request *cmd_request =
440 (struct storvsc_cmd_request *)request->context;
441 struct scsi_cmnd *scmnd = cmd_request->cmd;
442 struct host_device_context *host_device_ctx =
443 (struct host_device_context *)scmnd->device->host->hostdata;
444 void (*scsi_done_fn)(struct scsi_cmnd *);
445 struct scsi_sense_hdr sense_hdr;
446 struct vmscsi_request *vm_srb;
447
448 /* ASSERT(request == &cmd_request->request); */
449 /* ASSERT(scmnd); */
450 /* ASSERT((unsigned long)scmnd->host_scribble == */
451 /* (unsigned long)cmd_request); */
452 /* ASSERT(scmnd->scsi_done); */
453
454 if (cmd_request->bounce_sgl_count) {
455 /* using bounce buffer */
456 /* printk("copy_from_bounce_buffer\n"); */
457
458 /* FIXME: We can optimize on writes by just skipping this */
459 copy_from_bounce_buffer(scsi_sglist(scmnd),
460 cmd_request->bounce_sgl,
461 scsi_sg_count(scmnd));
462 destroy_bounce_buffer(cmd_request->bounce_sgl,
463 cmd_request->bounce_sgl_count);
464 }
465
466 vm_srb = &request->vstor_packet.vm_srb;
467 scmnd->result = vm_srb->scsi_status;
468
469 if (scmnd->result) {
470 if (scsi_normalize_sense(scmnd->sense_buffer,
471 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
472 scsi_print_sense_hdr("storvsc", &sense_hdr);
473 }
474
475 /* ASSERT(request->BytesXfer <= request->data_buffer.Length); */
476 scsi_set_resid(scmnd,
477 request->data_buffer.len -
478 vm_srb->data_transfer_length);
479
480 scsi_done_fn = scmnd->scsi_done;
481
482 scmnd->host_scribble = NULL;
483 scmnd->scsi_done = NULL;
484
485 /* !!DO NOT MODIFY the scmnd after this call */
486 scsi_done_fn(scmnd);
487
488 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
489 }
490
491 static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
492 {
493 int i;
494
495 /* No need to check */
496 if (sg_count < 2)
497 return -1;
498
499 /* We have at least 2 sg entries */
500 for (i = 0; i < sg_count; i++) {
501 if (i == 0) {
502 /* make sure 1st one does not have hole */
503 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
504 return i;
505 } else if (i == sg_count - 1) {
506 /* make sure last one does not have hole */
507 if (sgl[i].offset != 0)
508 return i;
509 } else {
510 /* make sure no hole in the middle */
511 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
512 return i;
513 }
514 }
515 return -1;
516 }
517
518 static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
519 unsigned int sg_count,
520 unsigned int len)
521 {
522 int i;
523 int num_pages;
524 struct scatterlist *bounce_sgl;
525 struct page *page_buf;
526
527 num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
528
529 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
530 if (!bounce_sgl)
531 return NULL;
532
533 for (i = 0; i < num_pages; i++) {
534 page_buf = alloc_page(GFP_ATOMIC);
535 if (!page_buf)
536 goto cleanup;
537 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
538 }
539
540 return bounce_sgl;
541
542 cleanup:
543 destroy_bounce_buffer(bounce_sgl, num_pages);
544 return NULL;
545 }
546
547 static void destroy_bounce_buffer(struct scatterlist *sgl,
548 unsigned int sg_count)
549 {
550 int i;
551 struct page *page_buf;
552
553 for (i = 0; i < sg_count; i++) {
554 page_buf = sg_page((&sgl[i]));
555 if (page_buf != NULL)
556 __free_page(page_buf);
557 }
558
559 kfree(sgl);
560 }
561
562 /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
563 static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
564 struct scatterlist *bounce_sgl,
565 unsigned int orig_sgl_count)
566 {
567 int i;
568 int j = 0;
569 unsigned long src, dest;
570 unsigned int srclen, destlen, copylen;
571 unsigned int total_copied = 0;
572 unsigned long bounce_addr = 0;
573 unsigned long src_addr = 0;
574 unsigned long flags;
575
576 local_irq_save(flags);
577
578 for (i = 0; i < orig_sgl_count; i++) {
579 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
580 KM_IRQ0) + orig_sgl[i].offset;
581 src = src_addr;
582 srclen = orig_sgl[i].length;
583
584 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
585
586 if (bounce_addr == 0)
587 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
588
589 while (srclen) {
590 /* assume bounce offset always == 0 */
591 dest = bounce_addr + bounce_sgl[j].length;
592 destlen = PAGE_SIZE - bounce_sgl[j].length;
593
594 copylen = min(srclen, destlen);
595 memcpy((void *)dest, (void *)src, copylen);
596
597 total_copied += copylen;
598 bounce_sgl[j].length += copylen;
599 srclen -= copylen;
600 src += copylen;
601
602 if (bounce_sgl[j].length == PAGE_SIZE) {
603 /* full..move to next entry */
604 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
605 j++;
606
607 /* if we need to use another bounce buffer */
608 if (srclen || i != orig_sgl_count - 1)
609 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
610 } else if (srclen == 0 && i == orig_sgl_count - 1) {
611 /* unmap the last bounce that is < PAGE_SIZE */
612 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
613 }
614 }
615
616 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
617 }
618
619 local_irq_restore(flags);
620
621 return total_copied;
622 }
623
624 /* Assume the original sgl has enough room */
625 static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
626 struct scatterlist *bounce_sgl,
627 unsigned int orig_sgl_count)
628 {
629 int i;
630 int j = 0;
631 unsigned long src, dest;
632 unsigned int srclen, destlen, copylen;
633 unsigned int total_copied = 0;
634 unsigned long bounce_addr = 0;
635 unsigned long dest_addr = 0;
636 unsigned long flags;
637
638 local_irq_save(flags);
639
640 for (i = 0; i < orig_sgl_count; i++) {
641 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
642 KM_IRQ0) + orig_sgl[i].offset;
643 dest = dest_addr;
644 destlen = orig_sgl[i].length;
645 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
646
647 if (bounce_addr == 0)
648 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
649
650 while (destlen) {
651 src = bounce_addr + bounce_sgl[j].offset;
652 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
653
654 copylen = min(srclen, destlen);
655 memcpy((void *)dest, (void *)src, copylen);
656
657 total_copied += copylen;
658 bounce_sgl[j].offset += copylen;
659 destlen -= copylen;
660 dest += copylen;
661
662 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
663 /* full */
664 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
665 j++;
666
667 /* if we need to use another bounce buffer */
668 if (destlen || i != orig_sgl_count - 1)
669 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
670 } else if (destlen == 0 && i == orig_sgl_count - 1) {
671 /* unmap the last bounce that is < PAGE_SIZE */
672 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
673 }
674 }
675
676 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
677 KM_IRQ0);
678 }
679
680 local_irq_restore(flags);
681
682 return total_copied;
683 }
684
685 /*
686 * storvsc_queuecommand - Initiate command processing
687 */
688 static int storvsc_queuecommand_lck(struct scsi_cmnd *scmnd,
689 void (*done)(struct scsi_cmnd *))
690 {
691 int ret;
692 struct host_device_context *host_device_ctx =
693 (struct host_device_context *)scmnd->device->host->hostdata;
694 struct hv_device *device_ctx = host_device_ctx->device_ctx;
695 struct hv_driver *drv =
696 drv_to_hv_drv(device_ctx->device.driver);
697 struct storvsc_driver_object *storvsc_drv_obj = drv->priv;
698 struct hv_storvsc_request *request;
699 struct storvsc_cmd_request *cmd_request;
700 unsigned int request_size = 0;
701 int i;
702 struct scatterlist *sgl;
703 unsigned int sg_count = 0;
704 struct vmscsi_request *vm_srb;
705
706 DPRINT_DBG(STORVSC_DRV, "scmnd %p dir %d, use_sg %d buf %p len %d "
707 "queue depth %d tagged %d", scmnd, scmnd->sc_data_direction,
708 scsi_sg_count(scmnd), scsi_sglist(scmnd),
709 scsi_bufflen(scmnd), scmnd->device->queue_depth,
710 scmnd->device->tagged_supported);
711
712 /* If retrying, no need to prep the cmd */
713 if (scmnd->host_scribble) {
714 /* ASSERT(scmnd->scsi_done != NULL); */
715
716 cmd_request =
717 (struct storvsc_cmd_request *)scmnd->host_scribble;
718 DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p",
719 scmnd, cmd_request);
720
721 goto retry_request;
722 }
723
724 /* ASSERT(scmnd->scsi_done == NULL); */
725 /* ASSERT(scmnd->host_scribble == NULL); */
726
727 scmnd->scsi_done = done;
728
729 request_size = sizeof(struct storvsc_cmd_request);
730
731 cmd_request = kmem_cache_zalloc(host_device_ctx->request_pool,
732 GFP_ATOMIC);
733 if (!cmd_request) {
734 DPRINT_ERR(STORVSC_DRV, "scmnd (%p) - unable to allocate "
735 "storvsc_cmd_request...marking queue busy", scmnd);
736 scmnd->scsi_done = NULL;
737 return SCSI_MLQUEUE_DEVICE_BUSY;
738 }
739
740 /* Setup the cmd request */
741 cmd_request->bounce_sgl_count = 0;
742 cmd_request->bounce_sgl = NULL;
743 cmd_request->cmd = scmnd;
744
745 scmnd->host_scribble = (unsigned char *)cmd_request;
746
747 request = &cmd_request->request;
748 vm_srb = &request->vstor_packet.vm_srb;
749
750 DPRINT_DBG(STORVSC_DRV, "req %p size %d", request, request_size);
751
752 /* Build the SRB */
753 switch (scmnd->sc_data_direction) {
754 case DMA_TO_DEVICE:
755 vm_srb->data_in = WRITE_TYPE;
756 break;
757 case DMA_FROM_DEVICE:
758 vm_srb->data_in = READ_TYPE;
759 break;
760 default:
761 vm_srb->data_in = UNKNOWN_TYPE;
762 break;
763 }
764
765 request->on_io_completion = storvsc_commmand_completion;
766 request->context = cmd_request;/* scmnd; */
767
768 /* request->PortId = scmnd->device->channel; */
769 vm_srb->port_number = host_device_ctx->port;
770 vm_srb->path_id = scmnd->device->channel;
771 vm_srb->target_id = scmnd->device->id;
772 vm_srb->lun = scmnd->device->lun;
773
774 /* ASSERT(scmnd->cmd_len <= 16); */
775 vm_srb->cdb_length = scmnd->cmd_len;
776
777 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
778
779 request->sense_buffer = scmnd->sense_buffer;
780
781
782 request->data_buffer.len = scsi_bufflen(scmnd);
783 if (scsi_sg_count(scmnd)) {
784 sgl = (struct scatterlist *)scsi_sglist(scmnd);
785 sg_count = scsi_sg_count(scmnd);
786
787 /* check if we need to bounce the sgl */
788 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
789 DPRINT_INFO(STORVSC_DRV,
790 "need to bounce buffer for this scmnd %p",
791 scmnd);
792 cmd_request->bounce_sgl =
793 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
794 scsi_bufflen(scmnd));
795 if (!cmd_request->bounce_sgl) {
796 DPRINT_ERR(STORVSC_DRV,
797 "unable to create bounce buffer for "
798 "this scmnd %p", scmnd);
799
800 scmnd->scsi_done = NULL;
801 scmnd->host_scribble = NULL;
802 kmem_cache_free(host_device_ctx->request_pool,
803 cmd_request);
804
805 return SCSI_MLQUEUE_HOST_BUSY;
806 }
807
808 cmd_request->bounce_sgl_count =
809 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
810 PAGE_SHIFT;
811
812 /*
813 * FIXME: We can optimize on reads by just skipping
814 * this
815 */
816 copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl,
817 scsi_sg_count(scmnd));
818
819 sgl = cmd_request->bounce_sgl;
820 sg_count = cmd_request->bounce_sgl_count;
821 }
822
823 request->data_buffer.offset = sgl[0].offset;
824
825 for (i = 0; i < sg_count; i++) {
826 DPRINT_DBG(STORVSC_DRV, "sgl[%d] len %d offset %d\n",
827 i, sgl[i].length, sgl[i].offset);
828 request->data_buffer.pfn_array[i] =
829 page_to_pfn(sg_page((&sgl[i])));
830 }
831 } else if (scsi_sglist(scmnd)) {
832 /* ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE); */
833 request->data_buffer.offset =
834 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
835 request->data_buffer.pfn_array[0] =
836 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
837 }
838
839 retry_request:
840 /* Invokes the vsc to start an IO */
841 ret = storvsc_drv_obj->on_io_request(device_ctx,
842 &cmd_request->request);
843 if (ret == -1) {
844 /* no more space */
845 DPRINT_ERR(STORVSC_DRV,
846 "scmnd (%p) - queue FULL...marking queue busy",
847 scmnd);
848
849 if (cmd_request->bounce_sgl_count) {
850 /*
851 * FIXME: We can optimize on writes by just skipping
852 * this
853 */
854 copy_from_bounce_buffer(scsi_sglist(scmnd),
855 cmd_request->bounce_sgl,
856 scsi_sg_count(scmnd));
857 destroy_bounce_buffer(cmd_request->bounce_sgl,
858 cmd_request->bounce_sgl_count);
859 }
860
861 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
862
863 scmnd->scsi_done = NULL;
864 scmnd->host_scribble = NULL;
865
866 ret = SCSI_MLQUEUE_DEVICE_BUSY;
867 }
868
869 return ret;
870 }
871
872 static DEF_SCSI_QCMD(storvsc_queuecommand)
873
874 static int storvsc_merge_bvec(struct request_queue *q,
875 struct bvec_merge_data *bmd, struct bio_vec *bvec)
876 {
877 /* checking done by caller. */
878 return bvec->bv_len;
879 }
880
881 /*
882 * storvsc_device_configure - Configure the specified scsi device
883 */
884 static int storvsc_device_alloc(struct scsi_device *sdevice)
885 {
886 DPRINT_DBG(STORVSC_DRV, "sdev (%p) - setting device flag to %d",
887 sdevice, BLIST_SPARSELUN);
888 /*
889 * This enables luns to be located sparsely. Otherwise, we may not
890 * discovered them.
891 */
892 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
893 return 0;
894 }
895
896 static int storvsc_device_configure(struct scsi_device *sdevice)
897 {
898 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - curr queue depth %d", sdevice,
899 sdevice->queue_depth);
900
901 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting queue depth to %d",
902 sdevice, STORVSC_MAX_IO_REQUESTS);
903 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
904 STORVSC_MAX_IO_REQUESTS);
905
906 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting max segment size to %ld",
907 sdevice, PAGE_SIZE);
908 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
909
910 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine",
911 sdevice);
912 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
913
914 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
915 /* sdevice->timeout = (2000 * HZ);//(75 * HZ); */
916
917 return 0;
918 }
919
920 /*
921 * storvsc_host_reset_handler - Reset the scsi HBA
922 */
923 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
924 {
925 int ret;
926 struct host_device_context *host_device_ctx =
927 (struct host_device_context *)scmnd->device->host->hostdata;
928 struct hv_device *device_ctx = host_device_ctx->device_ctx;
929
930 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...",
931 scmnd->device, device_ctx);
932
933 /* Invokes the vsc to reset the host/bus */
934 ret = stor_vsc_on_host_reset(device_ctx);
935 if (ret != 0)
936 return ret;
937
938 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted",
939 scmnd->device, device_ctx);
940
941 return ret;
942 }
943
944 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
945 sector_t capacity, int *info)
946 {
947 sector_t total_sectors = capacity;
948 sector_t cylinder_times_heads = 0;
949 sector_t temp = 0;
950
951 int sectors_per_track = 0;
952 int heads = 0;
953 int cylinders = 0;
954 int rem = 0;
955
956 if (total_sectors > (65535 * 16 * 255))
957 total_sectors = (65535 * 16 * 255);
958
959 if (total_sectors >= (65535 * 16 * 63)) {
960 sectors_per_track = 255;
961 heads = 16;
962
963 cylinder_times_heads = total_sectors;
964 /* sector_div stores the quotient in cylinder_times_heads */
965 rem = sector_div(cylinder_times_heads, sectors_per_track);
966 } else {
967 sectors_per_track = 17;
968
969 cylinder_times_heads = total_sectors;
970 /* sector_div stores the quotient in cylinder_times_heads */
971 rem = sector_div(cylinder_times_heads, sectors_per_track);
972
973 temp = cylinder_times_heads + 1023;
974 /* sector_div stores the quotient in temp */
975 rem = sector_div(temp, 1024);
976
977 heads = temp;
978
979 if (heads < 4)
980 heads = 4;
981
982 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
983 sectors_per_track = 31;
984 heads = 16;
985
986 cylinder_times_heads = total_sectors;
987 /*
988 * sector_div stores the quotient in
989 * cylinder_times_heads
990 */
991 rem = sector_div(cylinder_times_heads,
992 sectors_per_track);
993 }
994
995 if (cylinder_times_heads >= (heads * 1024)) {
996 sectors_per_track = 63;
997 heads = 16;
998
999 cylinder_times_heads = total_sectors;
1000 /*
1001 * sector_div stores the quotient in
1002 * cylinder_times_heads
1003 */
1004 rem = sector_div(cylinder_times_heads,
1005 sectors_per_track);
1006 }
1007 }
1008
1009 temp = cylinder_times_heads;
1010 /* sector_div stores the quotient in temp */
1011 rem = sector_div(temp, heads);
1012 cylinders = temp;
1013
1014 info[0] = heads;
1015 info[1] = sectors_per_track;
1016 info[2] = cylinders;
1017
1018 DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads,
1019 sectors_per_track);
1020
1021 return 0;
1022 }
1023
1024 static int __init storvsc_init(void)
1025 {
1026 int ret;
1027
1028 DPRINT_INFO(STORVSC_DRV, "Storvsc initializing....");
1029 ret = storvsc_drv_init();
1030 return ret;
1031 }
1032
1033 static void __exit storvsc_exit(void)
1034 {
1035 storvsc_drv_exit();
1036 }
1037
1038 MODULE_LICENSE("GPL");
1039 MODULE_VERSION(HV_DRV_VERSION);
1040 MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
1041 module_init(storvsc_init);
1042 module_exit(storvsc_exit);