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