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