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