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