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