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