Staging: hv: storvsc_drv: Get rid of unnecessary comments/dead code
[deliverable/linux.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{
362 struct storvsc_driver *storvsc_drv_obj =
363 drv_to_stordrv(dev->device.driver);
364 struct Scsi_Host *host = dev_get_drvdata(&dev->device);
365 struct hv_host_device *host_dev =
366 (struct hv_host_device *)host->hostdata;
367
368 /*
369 * Call to the vsc driver to let it know that the device is being
370 * removed
371 */
372 storvsc_drv_obj->base.dev_rm(dev);
373
374 if (host_dev->request_pool) {
375 kmem_cache_destroy(host_dev->request_pool);
376 host_dev->request_pool = NULL;
377 }
378
379 DPRINT_INFO(STORVSC, "removing host adapter (%p)...", host);
380 scsi_remove_host(host);
381
382 DPRINT_INFO(STORVSC, "releasing host adapter (%p)...", host);
383 scsi_host_put(host);
384 return 0;
385}
386
62838ce2
S
387
388static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
389 sector_t capacity, int *info)
390{
391 sector_t total_sectors = capacity;
392 sector_t cylinder_times_heads = 0;
393 sector_t temp = 0;
394
395 int sectors_per_track = 0;
396 int heads = 0;
397 int cylinders = 0;
398 int rem = 0;
399
400 if (total_sectors > (65535 * 16 * 255))
401 total_sectors = (65535 * 16 * 255);
402
403 if (total_sectors >= (65535 * 16 * 63)) {
404 sectors_per_track = 255;
405 heads = 16;
406
407 cylinder_times_heads = total_sectors;
408 /* sector_div stores the quotient in cylinder_times_heads */
409 rem = sector_div(cylinder_times_heads, sectors_per_track);
410 } else {
411 sectors_per_track = 17;
412
413 cylinder_times_heads = total_sectors;
414 /* sector_div stores the quotient in cylinder_times_heads */
415 rem = sector_div(cylinder_times_heads, sectors_per_track);
416
417 temp = cylinder_times_heads + 1023;
418 /* sector_div stores the quotient in temp */
419 rem = sector_div(temp, 1024);
420
421 heads = temp;
422
423 if (heads < 4)
424 heads = 4;
425
426 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
427 sectors_per_track = 31;
428 heads = 16;
429
430 cylinder_times_heads = total_sectors;
431 /*
432 * sector_div stores the quotient in
433 * cylinder_times_heads
434 */
435 rem = sector_div(cylinder_times_heads,
436 sectors_per_track);
437 }
438
439 if (cylinder_times_heads >= (heads * 1024)) {
440 sectors_per_track = 63;
441 heads = 16;
442
443 cylinder_times_heads = total_sectors;
444 /*
445 * sector_div stores the quotient in
446 * cylinder_times_heads
447 */
448 rem = sector_div(cylinder_times_heads,
449 sectors_per_track);
450 }
451 }
452
453 temp = cylinder_times_heads;
454 /* sector_div stores the quotient in temp */
455 rem = sector_div(temp, heads);
456 cylinders = temp;
457
458 info[0] = heads;
459 info[1] = sectors_per_track;
460 info[2] = cylinders;
461
462 DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads,
463 sectors_per_track);
464
465 return 0;
466}
aa3d789e
S
467
468static int storvsc_host_reset(struct hv_device *device)
469{
470 struct storvsc_device *stor_device;
471 struct hv_storvsc_request *request;
472 struct vstor_packet *vstor_packet;
473 int ret, t;
474
475 DPRINT_INFO(STORVSC, "resetting host adapter...");
476
477 stor_device = get_stor_device(device);
478 if (!stor_device)
479 return -1;
480
481 request = &stor_device->reset_request;
482 vstor_packet = &request->vstor_packet;
483
484 init_completion(&request->wait_event);
485
486 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
487 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
488 vstor_packet->vm_srb.path_id = stor_device->path_id;
489
490 ret = vmbus_sendpacket(device->channel, vstor_packet,
491 sizeof(struct vstor_packet),
492 (unsigned long)&stor_device->reset_request,
493 VM_PKT_DATA_INBAND,
494 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
495 if (ret != 0)
496 goto cleanup;
497
498 t = wait_for_completion_timeout(&request->wait_event, HZ);
499 if (t == 0) {
500 ret = -ETIMEDOUT;
501 goto cleanup;
502 }
503
504 DPRINT_INFO(STORVSC, "host adapter reset completed");
505
506 /*
507 * At this point, all outstanding requests in the adapter
508 * should have been flushed out and return to us
509 */
510
511cleanup:
512 put_stor_device(device);
513 return ret;
514}
515
516
96e690be
S
517/*
518 * storvsc_host_reset_handler - Reset the scsi HBA
519 */
520static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
521{
522 int ret;
523 struct hv_host_device *host_dev =
524 (struct hv_host_device *)scmnd->device->host->hostdata;
525 struct hv_device *dev = host_dev->dev;
526
527 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...",
528 scmnd->device, dev);
529
530 /* Invokes the vsc to reset the host/bus */
531 ret = storvsc_host_reset(dev);
532 if (ret != 0)
533 return ret;
534
535 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted",
536 scmnd->device, dev);
537
538 return ret;
539}
540
8a411bad
S
541
542/*
543 * storvsc_commmand_completion - Command completion processing
544 */
545static void storvsc_commmand_completion(struct hv_storvsc_request *request)
546{
547 struct storvsc_cmd_request *cmd_request =
548 (struct storvsc_cmd_request *)request->context;
549 struct scsi_cmnd *scmnd = cmd_request->cmd;
550 struct hv_host_device *host_dev =
551 (struct hv_host_device *)scmnd->device->host->hostdata;
552 void (*scsi_done_fn)(struct scsi_cmnd *);
553 struct scsi_sense_hdr sense_hdr;
554 struct vmscsi_request *vm_srb;
555
8a411bad 556 if (cmd_request->bounce_sgl_count) {
8a411bad
S
557
558 /* FIXME: We can optimize on writes by just skipping this */
559 copy_from_bounce_buffer(scsi_sglist(scmnd),
560 cmd_request->bounce_sgl,
561 scsi_sg_count(scmnd));
562 destroy_bounce_buffer(cmd_request->bounce_sgl,
563 cmd_request->bounce_sgl_count);
564 }
565
566 vm_srb = &request->vstor_packet.vm_srb;
567 scmnd->result = vm_srb->scsi_status;
568
569 if (scmnd->result) {
570 if (scsi_normalize_sense(scmnd->sense_buffer,
571 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
572 scsi_print_sense_hdr("storvsc", &sense_hdr);
573 }
574
8a411bad
S
575 scsi_set_resid(scmnd,
576 request->data_buffer.len -
577 vm_srb->data_transfer_length);
578
579 scsi_done_fn = scmnd->scsi_done;
580
581 scmnd->host_scribble = NULL;
582 scmnd->scsi_done = NULL;
583
584 /* !!DO NOT MODIFY the scmnd after this call */
585 scsi_done_fn(scmnd);
586
587 kmem_cache_free(host_dev->request_pool, cmd_request);
588}
589
c5b463ae
S
590
591/*
592 * storvsc_queuecommand - Initiate command processing
593 */
594static int storvsc_queuecommand_lck(struct scsi_cmnd *scmnd,
595 void (*done)(struct scsi_cmnd *))
596{
597 int ret;
598 struct hv_host_device *host_dev =
599 (struct hv_host_device *)scmnd->device->host->hostdata;
600 struct hv_device *dev = host_dev->dev;
601 struct storvsc_driver *storvsc_drv_obj =
602 drv_to_stordrv(dev->device.driver);
603 struct hv_storvsc_request *request;
604 struct storvsc_cmd_request *cmd_request;
605 unsigned int request_size = 0;
606 int i;
607 struct scatterlist *sgl;
608 unsigned int sg_count = 0;
609 struct vmscsi_request *vm_srb;
610
611
612 /* If retrying, no need to prep the cmd */
613 if (scmnd->host_scribble) {
c5b463ae
S
614
615 cmd_request =
616 (struct storvsc_cmd_request *)scmnd->host_scribble;
617 DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p",
618 scmnd, cmd_request);
619
620 goto retry_request;
621 }
622
c5b463ae
S
623 scmnd->scsi_done = done;
624
625 request_size = sizeof(struct storvsc_cmd_request);
626
627 cmd_request = kmem_cache_zalloc(host_dev->request_pool,
628 GFP_ATOMIC);
629 if (!cmd_request) {
630 scmnd->scsi_done = NULL;
631 return SCSI_MLQUEUE_DEVICE_BUSY;
632 }
633
634 /* Setup the cmd request */
635 cmd_request->bounce_sgl_count = 0;
636 cmd_request->bounce_sgl = NULL;
637 cmd_request->cmd = scmnd;
638
639 scmnd->host_scribble = (unsigned char *)cmd_request;
640
641 request = &cmd_request->request;
642 vm_srb = &request->vstor_packet.vm_srb;
643
644
645 /* Build the SRB */
646 switch (scmnd->sc_data_direction) {
647 case DMA_TO_DEVICE:
648 vm_srb->data_in = WRITE_TYPE;
649 break;
650 case DMA_FROM_DEVICE:
651 vm_srb->data_in = READ_TYPE;
652 break;
653 default:
654 vm_srb->data_in = UNKNOWN_TYPE;
655 break;
656 }
657
658 request->on_io_completion = storvsc_commmand_completion;
659 request->context = cmd_request;/* scmnd; */
660
c5b463ae
S
661 vm_srb->port_number = host_dev->port;
662 vm_srb->path_id = scmnd->device->channel;
663 vm_srb->target_id = scmnd->device->id;
664 vm_srb->lun = scmnd->device->lun;
665
c5b463ae
S
666 vm_srb->cdb_length = scmnd->cmd_len;
667
668 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
669
670 request->sense_buffer = scmnd->sense_buffer;
671
672
673 request->data_buffer.len = scsi_bufflen(scmnd);
674 if (scsi_sg_count(scmnd)) {
675 sgl = (struct scatterlist *)scsi_sglist(scmnd);
676 sg_count = scsi_sg_count(scmnd);
677
678 /* check if we need to bounce the sgl */
679 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
680 cmd_request->bounce_sgl =
681 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
682 scsi_bufflen(scmnd));
683 if (!cmd_request->bounce_sgl) {
684 scmnd->scsi_done = NULL;
685 scmnd->host_scribble = NULL;
686 kmem_cache_free(host_dev->request_pool,
687 cmd_request);
688
689 return SCSI_MLQUEUE_HOST_BUSY;
690 }
691
692 cmd_request->bounce_sgl_count =
693 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
694 PAGE_SHIFT;
695
696 /*
697 * FIXME: We can optimize on reads by just skipping
698 * this
699 */
700 copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl,
701 scsi_sg_count(scmnd));
702
703 sgl = cmd_request->bounce_sgl;
704 sg_count = cmd_request->bounce_sgl_count;
705 }
706
707 request->data_buffer.offset = sgl[0].offset;
708
709 for (i = 0; i < sg_count; i++)
710 request->data_buffer.pfn_array[i] =
711 page_to_pfn(sg_page((&sgl[i])));
712
713 } else if (scsi_sglist(scmnd)) {
c5b463ae
S
714 request->data_buffer.offset =
715 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
716 request->data_buffer.pfn_array[0] =
717 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
718 }
719
720retry_request:
721 /* Invokes the vsc to start an IO */
722 ret = storvsc_drv_obj->on_io_request(dev,
723 &cmd_request->request);
724 if (ret == -1) {
725 /* no more space */
726
727 if (cmd_request->bounce_sgl_count) {
728 /*
729 * FIXME: We can optimize on writes by just skipping
730 * this
731 */
732 copy_from_bounce_buffer(scsi_sglist(scmnd),
733 cmd_request->bounce_sgl,
734 scsi_sg_count(scmnd));
735 destroy_bounce_buffer(cmd_request->bounce_sgl,
736 cmd_request->bounce_sgl_count);
737 }
738
739 kmem_cache_free(host_dev->request_pool, cmd_request);
740
741 scmnd->scsi_done = NULL;
742 scmnd->host_scribble = NULL;
743
744 ret = SCSI_MLQUEUE_DEVICE_BUSY;
745 }
746
747 return ret;
748}
749
750static DEF_SCSI_QCMD(storvsc_queuecommand)
751
bef4a34a 752
454f18a9 753/* Scsi driver */
bef4a34a 754static struct scsi_host_template scsi_driver = {
ff568d3a
GKH
755 .module = THIS_MODULE,
756 .name = "storvsc_host_t",
757 .bios_param = storvsc_get_chs,
758 .queuecommand = storvsc_queuecommand,
759 .eh_host_reset_handler = storvsc_host_reset_handler,
760 .slave_alloc = storvsc_device_alloc,
761 .slave_configure = storvsc_device_configure,
762 .cmd_per_lun = 1,
763 /* 64 max_queue * 1 target */
0686e4f4 764 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
ff568d3a 765 .this_id = -1,
454f18a9 766 /* no use setting to 0 since ll_blk_rw reset it to 1 */
ff568d3a
GKH
767 /* currently 32 */
768 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
769 /*
770 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
771 * into 1 sg element. If set, we must limit the max_segment_size to
772 * PAGE_SIZE, otherwise we may get 1 sg element that represents
773 * multiple
774 */
454f18a9 775 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
ff568d3a 776 .use_clustering = ENABLE_CLUSTERING,
454f18a9 777 /* Make sure we dont get a sg segment crosses a page boundary */
ff568d3a 778 .dma_boundary = PAGE_SIZE-1,
bef4a34a
HJ
779};
780
781
3e189519 782/*
ff568d3a
GKH
783 * storvsc_probe - Add a new device for this driver
784 */
f5c78872 785
9efd21e1 786static int storvsc_probe(struct hv_device *device)
bef4a34a 787{
ff568d3a 788 int ret;
2e79505d 789 struct storvsc_driver *storvsc_drv_obj =
9efd21e1 790 drv_to_stordrv(device->device.driver);
bef4a34a 791 struct Scsi_Host *host;
795b613d 792 struct hv_host_device *host_dev;
9f0c7d2c 793 struct storvsc_device_info device_info;
bef4a34a 794
ca623ad3 795 if (!storvsc_drv_obj->base.dev_add)
bef4a34a
HJ
796 return -1;
797
ff568d3a 798 host = scsi_host_alloc(&scsi_driver,
972621c9 799 sizeof(struct hv_host_device));
f8feed06 800 if (!host)
bef4a34a 801 return -ENOMEM;
bef4a34a 802
9efd21e1 803 dev_set_drvdata(&device->device, host);
bef4a34a 804
795b613d
S
805 host_dev = (struct hv_host_device *)host->hostdata;
806 memset(host_dev, 0, sizeof(struct hv_host_device));
bef4a34a 807
795b613d 808 host_dev->port = host->host_no;
97c15296 809 host_dev->dev = device;
bef4a34a 810
795b613d 811 host_dev->request_pool =
9efd21e1 812 kmem_cache_create(dev_name(&device->device),
1e05d88e 813 sizeof(struct storvsc_cmd_request), 0,
ff568d3a
GKH
814 SLAB_HWCACHE_ALIGN, NULL);
815
795b613d 816 if (!host_dev->request_pool) {
bef4a34a 817 scsi_host_put(host);
bef4a34a
HJ
818 return -ENOMEM;
819 }
820
8a046024 821 device_info.port_number = host->host_no;
454f18a9 822 /* Call to the vsc driver to add the device */
9efd21e1
S
823 ret = storvsc_drv_obj->base.dev_add(device, (void *)&device_info);
824
ff568d3a 825 if (ret != 0) {
795b613d 826 kmem_cache_destroy(host_dev->request_pool);
bef4a34a 827 scsi_host_put(host);
bef4a34a
HJ
828 return -1;
829 }
830
795b613d
S
831 host_dev->path = device_info.path_id;
832 host_dev->target = device_info.target_id;
bef4a34a 833
ff568d3a
GKH
834 /* max # of devices per target */
835 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
836 /* max # of targets per channel */
837 host->max_id = STORVSC_MAX_TARGETS;
838 /* max # of channels */
839 host->max_channel = STORVSC_MAX_CHANNELS - 1;
bef4a34a 840
454f18a9 841 /* Register the HBA and start the scsi bus scan */
9efd21e1 842 ret = scsi_add_host(host, &device->device);
ff568d3a 843 if (ret != 0) {
bef4a34a 844
9efd21e1 845 storvsc_drv_obj->base.dev_rm(device);
bef4a34a 846
795b613d 847 kmem_cache_destroy(host_dev->request_pool);
bef4a34a 848 scsi_host_put(host);
bef4a34a
HJ
849 return -1;
850 }
851
852 scsi_scan_host(host);
bef4a34a
HJ
853 return ret;
854}
855
7bd05b91
S
856/* The one and only one */
857
39ae6fae
S
858static struct storvsc_driver storvsc_drv = {
859 .base.probe = storvsc_probe,
860 .base.remove = storvsc_remove,
861};
7bd05b91
S
862
863
f5c78872
S
864/*
865 * storvsc_drv_init - StorVsc driver initialization.
866 */
867static int storvsc_drv_init(void)
868{
869 int ret;
565c38de
S
870 struct storvsc_driver *storvsc_drv_obj = &storvsc_drv;
871 struct hv_driver *drv = &storvsc_drv.base;
f5c78872
S
872
873 storvsc_drv_obj->ring_buffer_size = storvsc_ringbuffer_size;
874
875 /* Callback to client driver to complete the initialization */
876 storvsc_initialize(&storvsc_drv_obj->base);
877
878 DPRINT_INFO(STORVSC_DRV,
879 "max outstanding reqs %u",
880 storvsc_drv_obj->max_outstanding_req_per_channel);
881
882 if (storvsc_drv_obj->max_outstanding_req_per_channel <
883 STORVSC_MAX_IO_REQUESTS)
884 return -1;
885
886 drv->driver.name = storvsc_drv_obj->base.name;
887
f5c78872
S
888
889 /* The driver belongs to vmbus */
890 ret = vmbus_child_driver_register(&drv->driver);
891
892 return ret;
893}
894
895static int storvsc_drv_exit_cb(struct device *dev, void *data)
896{
897 struct device **curr = (struct device **)data;
898 *curr = dev;
899 return 1; /* stop iterating */
900}
901
902static void storvsc_drv_exit(void)
903{
565c38de
S
904 struct storvsc_driver *storvsc_drv_obj = &storvsc_drv;
905 struct hv_driver *drv = &storvsc_drv.base;
f5c78872
S
906 struct device *current_dev = NULL;
907 int ret;
908
909 while (1) {
910 current_dev = NULL;
911
912 /* Get the device */
913 ret = driver_for_each_device(&drv->driver, NULL,
914 (void *) &current_dev,
915 storvsc_drv_exit_cb);
916
917
918 if (current_dev == NULL)
919 break;
920
921 /* Initiate removal from the top-down */
922 device_unregister(current_dev);
923 }
924
925 if (storvsc_drv_obj->base.cleanup)
926 storvsc_drv_obj->base.cleanup(&storvsc_drv_obj->base);
927
928 vmbus_child_driver_unregister(&drv->driver);
929 return;
930}
931
bef4a34a
HJ
932static int __init storvsc_init(void)
933{
934 int ret;
935
bef4a34a 936 DPRINT_INFO(STORVSC_DRV, "Storvsc initializing....");
db085777 937 ret = storvsc_drv_init();
bef4a34a
HJ
938 return ret;
939}
940
941static void __exit storvsc_exit(void)
942{
bef4a34a 943 storvsc_drv_exit();
bef4a34a
HJ
944}
945
ff568d3a 946MODULE_LICENSE("GPL");
26c14cc1 947MODULE_VERSION(HV_DRV_VERSION);
3afc7cc3 948MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
bef4a34a
HJ
949module_init(storvsc_init);
950module_exit(storvsc_exit);
This page took 0.275023 seconds and 5 git commands to generate.