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