1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/device.h>
24#include <linux/blkdev.h>
25#include <scsi/scsi.h>
26#include <scsi/scsi_cmnd.h>
27#include <scsi/scsi_host.h>
28#include <scsi/scsi_device.h>
29#include <scsi/scsi_tcq.h>
30#include <scsi/scsi_eh.h>
31#include <scsi/scsi_devinfo.h>
32#include <scsi/scsi_dbg.h>
33#include "osd.h"
34#include "logging.h"
35#include "vmbus.h"
36#include "StorVscApi.h"
37
38
39struct host_device_context {
40
41
42 struct work_struct host_rescan_work;
43
44
45 struct device_context *device_ctx;
46 struct kmem_cache *request_pool;
47 unsigned int port;
48 unsigned char path;
49 unsigned char target;
50};
51
52struct storvsc_cmd_request {
53 struct list_head entry;
54 struct scsi_cmnd *cmd;
55
56 unsigned int bounce_sgl_count;
57 struct scatterlist *bounce_sgl;
58
59 struct hv_storvsc_request request;
60
61
62
63
64};
65
66struct storvsc_driver_context {
67
68
69 struct driver_context drv_ctx;
70 struct storvsc_driver_object drv_obj;
71};
72
73
74static int storvsc_probe(struct device *dev);
75static int storvsc_queuecommand(struct scsi_cmnd *scmnd,
76 void (*done)(struct scsi_cmnd *));
77static int storvsc_device_alloc(struct scsi_device *);
78static int storvsc_device_configure(struct scsi_device *);
79static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd);
80static void storvsc_host_rescan_callback(struct work_struct *work);
81static void storvsc_host_rescan(struct hv_device *device_obj);
82static int storvsc_remove(struct device *dev);
83
84static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
85 unsigned int sg_count,
86 unsigned int len);
87static void destroy_bounce_buffer(struct scatterlist *sgl,
88 unsigned int sg_count);
89static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count);
90static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
91 struct scatterlist *bounce_sgl,
92 unsigned int orig_sgl_count);
93static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
94 struct scatterlist *bounce_sgl,
95 unsigned int orig_sgl_count);
96
97static int storvsc_report_luns(struct scsi_device *sdev, unsigned int luns[],
98 unsigned int *lun_count);
99static int storvsc_get_chs(struct scsi_device *sdev, struct block_device *bdev,
100 sector_t capacity, int *info);
101
102
103static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
104
105
106static struct storvsc_driver_context g_storvsc_drv;
107
108
109static struct scsi_host_template scsi_driver = {
110 .module = THIS_MODULE,
111 .name = "storvsc_host_t",
112 .bios_param = storvsc_get_chs,
113 .queuecommand = storvsc_queuecommand,
114 .eh_host_reset_handler = storvsc_host_reset_handler,
115 .slave_alloc = storvsc_device_alloc,
116 .slave_configure = storvsc_device_configure,
117 .cmd_per_lun = 1,
118
119 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
120 .this_id = -1,
121
122
123 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
124
125
126
127
128
129
130
131 .use_clustering = ENABLE_CLUSTERING,
132
133 .dma_boundary = PAGE_SIZE-1,
134};
135
136
137
138
139
140static int storvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
141{
142 int ret;
143 struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv.drv_obj;
144 struct driver_context *drv_ctx = &g_storvsc_drv.drv_ctx;
145
146 DPRINT_ENTER(STORVSC_DRV);
147
148 vmbus_get_interface(&storvsc_drv_obj->Base.VmbusChannelInterface);
149
150 storvsc_drv_obj->RingBufferSize = storvsc_ringbuffer_size;
151 storvsc_drv_obj->OnHostRescan = storvsc_host_rescan;
152
153
154 drv_init(&storvsc_drv_obj->Base);
155
156 DPRINT_INFO(STORVSC_DRV,
157 "request extension size %u, max outstanding reqs %u",
158 storvsc_drv_obj->RequestExtSize,
159 storvsc_drv_obj->MaxOutstandingRequestsPerChannel);
160
161 if (storvsc_drv_obj->MaxOutstandingRequestsPerChannel <
162 STORVSC_MAX_IO_REQUESTS) {
163 DPRINT_ERR(STORVSC_DRV,
164 "The number of outstanding io requests (%d) "
165 "is larger than that supported (%d) internally.",
166 STORVSC_MAX_IO_REQUESTS,
167 storvsc_drv_obj->MaxOutstandingRequestsPerChannel);
168 return -1;
169 }
170
171 drv_ctx->driver.name = storvsc_drv_obj->Base.name;
172 memcpy(&drv_ctx->class_id, &storvsc_drv_obj->Base.deviceType,
173 sizeof(struct hv_guid));
174
175 drv_ctx->probe = storvsc_probe;
176 drv_ctx->remove = storvsc_remove;
177
178
179 ret = vmbus_child_driver_register(drv_ctx);
180
181 DPRINT_EXIT(STORVSC_DRV);
182
183 return ret;
184}
185
186static int storvsc_drv_exit_cb(struct device *dev, void *data)
187{
188 struct device **curr = (struct device **)data;
189 *curr = dev;
190 return 1;
191}
192
193static void storvsc_drv_exit(void)
194{
195 struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv.drv_obj;
196 struct driver_context *drv_ctx = &g_storvsc_drv.drv_ctx;
197 struct device *current_dev = NULL;
198 int ret;
199
200 DPRINT_ENTER(STORVSC_DRV);
201
202 while (1) {
203 current_dev = NULL;
204
205
206 ret = driver_for_each_device(&drv_ctx->driver, NULL,
207 (void *) ¤t_dev,
208 storvsc_drv_exit_cb);
209
210 if (ret)
211 DPRINT_WARN(STORVSC_DRV,
212 "driver_for_each_device returned %d", ret);
213
214 if (current_dev == NULL)
215 break;
216
217
218 device_unregister(current_dev);
219 }
220
221 if (storvsc_drv_obj->Base.OnCleanup)
222 storvsc_drv_obj->Base.OnCleanup(&storvsc_drv_obj->Base);
223
224 vmbus_child_driver_unregister(drv_ctx);
225
226 DPRINT_EXIT(STORVSC_DRV);
227
228 return;
229}
230
231
232
233
234static int storvsc_probe(struct device *device)
235{
236 int ret;
237 struct driver_context *driver_ctx =
238 driver_to_driver_context(device->driver);
239 struct storvsc_driver_context *storvsc_drv_ctx =
240 (struct storvsc_driver_context *)driver_ctx;
241 struct storvsc_driver_object *storvsc_drv_obj =
242 &storvsc_drv_ctx->drv_obj;
243 struct device_context *device_ctx = device_to_device_context(device);
244 struct hv_device *device_obj = &device_ctx->device_obj;
245 struct Scsi_Host *host;
246 struct host_device_context *host_device_ctx;
247 struct storvsc_device_info device_info;
248
249 DPRINT_ENTER(STORVSC_DRV);
250
251 if (!storvsc_drv_obj->Base.OnDeviceAdd)
252 return -1;
253
254 host = scsi_host_alloc(&scsi_driver,
255 sizeof(struct host_device_context));
256 if (!host) {
257 DPRINT_ERR(STORVSC_DRV, "unable to allocate scsi host object");
258 return -ENOMEM;
259 }
260
261 dev_set_drvdata(device, host);
262
263 host_device_ctx = (struct host_device_context *)host->hostdata;
264 memset(host_device_ctx, 0, sizeof(struct host_device_context));
265
266 host_device_ctx->port = host->host_no;
267 host_device_ctx->device_ctx = device_ctx;
268
269 INIT_WORK(&host_device_ctx->host_rescan_work,
270 storvsc_host_rescan_callback);
271
272 host_device_ctx->request_pool =
273 kmem_cache_create(dev_name(&device_ctx->device),
274 sizeof(struct storvsc_cmd_request) +
275 storvsc_drv_obj->RequestExtSize, 0,
276 SLAB_HWCACHE_ALIGN, NULL);
277
278 if (!host_device_ctx->request_pool) {
279 scsi_host_put(host);
280 DPRINT_EXIT(STORVSC_DRV);
281
282 return -ENOMEM;
283 }
284
285 device_info.PortNumber = host->host_no;
286
287 ret = storvsc_drv_obj->Base.OnDeviceAdd(device_obj,
288 (void *)&device_info);
289 if (ret != 0) {
290 DPRINT_ERR(STORVSC_DRV, "unable to add scsi vsc device");
291 kmem_cache_destroy(host_device_ctx->request_pool);
292 scsi_host_put(host);
293 DPRINT_EXIT(STORVSC_DRV);
294
295 return -1;
296 }
297
298
299 host_device_ctx->path = device_info.PathId;
300 host_device_ctx->target = device_info.TargetId;
301
302
303 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
304
305 host->max_id = STORVSC_MAX_TARGETS;
306
307 host->max_channel = STORVSC_MAX_CHANNELS - 1;
308
309
310 ret = scsi_add_host(host, device);
311 if (ret != 0) {
312 DPRINT_ERR(STORVSC_DRV, "unable to add scsi host device");
313
314 storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
315
316 kmem_cache_destroy(host_device_ctx->request_pool);
317 scsi_host_put(host);
318 DPRINT_EXIT(STORVSC_DRV);
319
320 return -1;
321 }
322
323 scsi_scan_host(host);
324
325 DPRINT_EXIT(STORVSC_DRV);
326
327 return ret;
328}
329
330
331
332
333static int storvsc_remove(struct device *device)
334{
335 int ret;
336 struct driver_context *driver_ctx =
337 driver_to_driver_context(device->driver);
338 struct storvsc_driver_context *storvsc_drv_ctx =
339 (struct storvsc_driver_context *)driver_ctx;
340 struct storvsc_driver_object *storvsc_drv_obj =
341 &storvsc_drv_ctx->drv_obj;
342 struct device_context *device_ctx = device_to_device_context(device);
343 struct hv_device *device_obj = &device_ctx->device_obj;
344 struct Scsi_Host *host = dev_get_drvdata(device);
345 struct host_device_context *host_device_ctx =
346 (struct host_device_context *)host->hostdata;
347
348
349 DPRINT_ENTER(STORVSC_DRV);
350
351 if (!storvsc_drv_obj->Base.OnDeviceRemove) {
352 DPRINT_EXIT(STORVSC_DRV);
353 return -1;
354 }
355
356
357
358
359
360 ret = storvsc_drv_obj->Base.OnDeviceRemove(device_obj);
361 if (ret != 0) {
362
363 DPRINT_ERR(STORVSC, "unable to remove vsc device (ret %d)",
364 ret);
365 }
366
367 if (host_device_ctx->request_pool) {
368 kmem_cache_destroy(host_device_ctx->request_pool);
369 host_device_ctx->request_pool = NULL;
370 }
371
372 DPRINT_INFO(STORVSC, "removing host adapter (%p)...", host);
373 scsi_remove_host(host);
374
375 DPRINT_INFO(STORVSC, "releasing host adapter (%p)...", host);
376 scsi_host_put(host);
377
378 DPRINT_EXIT(STORVSC_DRV);
379
380 return ret;
381}
382
383
384
385
386static void storvsc_commmand_completion(struct hv_storvsc_request *request)
387{
388 struct storvsc_cmd_request *cmd_request =
389 (struct storvsc_cmd_request *)request->Context;
390 struct scsi_cmnd *scmnd = cmd_request->cmd;
391 struct host_device_context *host_device_ctx =
392 (struct host_device_context *)scmnd->device->host->hostdata;
393 void (*scsi_done_fn)(struct scsi_cmnd *);
394 struct scsi_sense_hdr sense_hdr;
395
396 ASSERT(request == &cmd_request->request);
397 ASSERT((unsigned long)scmnd->host_scribble ==
398 (unsigned long)cmd_request);
399 ASSERT(scmnd);
400 ASSERT(scmnd->scsi_done);
401
402 DPRINT_ENTER(STORVSC_DRV);
403
404 if (cmd_request->bounce_sgl_count) {
405
406
407
408
409 copy_from_bounce_buffer(scsi_sglist(scmnd),
410 cmd_request->bounce_sgl,
411 scsi_sg_count(scmnd));
412 destroy_bounce_buffer(cmd_request->bounce_sgl,
413 cmd_request->bounce_sgl_count);
414 }
415
416 scmnd->result = request->Status;
417
418 if (scmnd->result) {
419 if (scsi_normalize_sense(scmnd->sense_buffer,
420 request->SenseBufferSize, &sense_hdr))
421 scsi_print_sense_hdr("storvsc", &sense_hdr);
422 }
423
424 ASSERT(request->BytesXfer <= request->DataBuffer.Length);
425 scsi_set_resid(scmnd, request->DataBuffer.Length - request->BytesXfer);
426
427 scsi_done_fn = scmnd->scsi_done;
428
429 scmnd->host_scribble = NULL;
430 scmnd->scsi_done = NULL;
431
432
433 scsi_done_fn(scmnd);
434
435 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
436
437 DPRINT_EXIT(STORVSC_DRV);
438}
439
440static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
441{
442 int i;
443
444
445 if (sg_count < 2)
446 return -1;
447
448
449 for (i = 0; i < sg_count; i++) {
450 if (i == 0) {
451
452 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
453 return i;
454 } else if (i == sg_count - 1) {
455
456 if (sgl[i].offset != 0)
457 return i;
458 } else {
459
460 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
461 return i;
462 }
463 }
464 return -1;
465}
466
467static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
468 unsigned int sg_count,
469 unsigned int len)
470{
471 int i;
472 int num_pages;
473 struct scatterlist *bounce_sgl;
474 struct page *page_buf;
475
476 num_pages = ALIGN_UP(len, PAGE_SIZE) >> PAGE_SHIFT;
477
478 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
479 if (!bounce_sgl)
480 return NULL;
481
482 for (i = 0; i < num_pages; i++) {
483 page_buf = alloc_page(GFP_ATOMIC);
484 if (!page_buf)
485 goto cleanup;
486 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
487 }
488
489 return bounce_sgl;
490
491cleanup:
492 destroy_bounce_buffer(bounce_sgl, num_pages);
493 return NULL;
494}
495
496static void destroy_bounce_buffer(struct scatterlist *sgl,
497 unsigned int sg_count)
498{
499 int i;
500 struct page *page_buf;
501
502 for (i = 0; i < sg_count; i++) {
503 page_buf = sg_page((&sgl[i]));
504 if (page_buf != NULL)
505 __free_page(page_buf);
506 }
507
508 kfree(sgl);
509}
510
511
512static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
513 struct scatterlist *bounce_sgl,
514 unsigned int orig_sgl_count)
515{
516 int i;
517 int j = 0;
518 unsigned long src, dest;
519 unsigned int srclen, destlen, copylen;
520 unsigned int total_copied = 0;
521 unsigned long bounce_addr = 0;
522 unsigned long src_addr = 0;
523 unsigned long flags;
524
525 local_irq_save(flags);
526
527 for (i = 0; i < orig_sgl_count; i++) {
528 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
529 KM_IRQ0) + orig_sgl[i].offset;
530 src = src_addr;
531 srclen = orig_sgl[i].length;
532
533 ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE);
534
535 if (j == 0)
536 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
537
538 while (srclen) {
539
540 dest = bounce_addr + bounce_sgl[j].length;
541 destlen = PAGE_SIZE - bounce_sgl[j].length;
542
543 copylen = min(srclen, destlen);
544 memcpy((void *)dest, (void *)src, copylen);
545
546 total_copied += copylen;
547 bounce_sgl[j].length += copylen;
548 srclen -= copylen;
549 src += copylen;
550
551 if (bounce_sgl[j].length == PAGE_SIZE) {
552
553 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
554 j++;
555
556
557 if (srclen || i != orig_sgl_count - 1)
558 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
559 } else if (srclen == 0 && i == orig_sgl_count - 1) {
560
561 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
562 }
563 }
564
565 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
566 }
567
568 local_irq_restore(flags);
569
570 return total_copied;
571}
572
573
574static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
575 struct scatterlist *bounce_sgl,
576 unsigned int orig_sgl_count)
577{
578 int i;
579 int j = 0;
580 unsigned long src, dest;
581 unsigned int srclen, destlen, copylen;
582 unsigned int total_copied = 0;
583 unsigned long bounce_addr = 0;
584 unsigned long dest_addr = 0;
585 unsigned long flags;
586
587 local_irq_save(flags);
588
589 for (i = 0; i < orig_sgl_count; i++) {
590 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
591 KM_IRQ0) + orig_sgl[i].offset;
592 dest = dest_addr;
593 destlen = orig_sgl[i].length;
594 ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE);
595
596 if (j == 0)
597 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
598
599 while (destlen) {
600 src = bounce_addr + bounce_sgl[j].offset;
601 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
602
603 copylen = min(srclen, destlen);
604 memcpy((void *)dest, (void *)src, copylen);
605
606 total_copied += copylen;
607 bounce_sgl[j].offset += copylen;
608 destlen -= copylen;
609 dest += copylen;
610
611 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
612
613 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
614 j++;
615
616
617 if (destlen || i != orig_sgl_count - 1)
618 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
619 } else if (destlen == 0 && i == orig_sgl_count - 1) {
620
621 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
622 }
623 }
624
625 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
626 KM_IRQ0);
627 }
628
629 local_irq_restore(flags);
630
631 return total_copied;
632}
633
634
635
636
637static int storvsc_queuecommand(struct scsi_cmnd *scmnd,
638 void (*done)(struct scsi_cmnd *))
639{
640 int ret;
641 struct host_device_context *host_device_ctx =
642 (struct host_device_context *)scmnd->device->host->hostdata;
643 struct device_context *device_ctx = host_device_ctx->device_ctx;
644 struct driver_context *driver_ctx =
645 driver_to_driver_context(device_ctx->device.driver);
646 struct storvsc_driver_context *storvsc_drv_ctx =
647 (struct storvsc_driver_context *)driver_ctx;
648 struct storvsc_driver_object *storvsc_drv_obj =
649 &storvsc_drv_ctx->drv_obj;
650 struct hv_storvsc_request *request;
651 struct storvsc_cmd_request *cmd_request;
652 unsigned int request_size = 0;
653 int i;
654 struct scatterlist *sgl;
655
656 DPRINT_ENTER(STORVSC_DRV);
657
658 DPRINT_DBG(STORVSC_DRV, "scmnd %p dir %d, use_sg %d buf %p len %d "
659 "queue depth %d tagged %d", scmnd, scmnd->sc_data_direction,
660 scsi_sg_count(scmnd), scsi_sglist(scmnd),
661 scsi_bufflen(scmnd), scmnd->device->queue_depth,
662 scmnd->device->tagged_supported);
663
664
665 if (scmnd->host_scribble) {
666 ASSERT(scmnd->scsi_done != NULL);
667
668 cmd_request =
669 (struct storvsc_cmd_request *)scmnd->host_scribble;
670 DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p",
671 scmnd, cmd_request);
672
673 goto retry_request;
674 }
675
676 ASSERT(scmnd->scsi_done == NULL);
677 ASSERT(scmnd->host_scribble == NULL);
678
679 scmnd->scsi_done = done;
680
681 request_size = sizeof(struct storvsc_cmd_request);
682
683 cmd_request = kmem_cache_alloc(host_device_ctx->request_pool,
684 GFP_ATOMIC);
685 if (!cmd_request) {
686 DPRINT_ERR(STORVSC_DRV, "scmnd (%p) - unable to allocate "
687 "storvsc_cmd_request...marking queue busy", scmnd);
688 scmnd->scsi_done = NULL;
689 return SCSI_MLQUEUE_DEVICE_BUSY;
690 }
691
692
693 cmd_request->bounce_sgl_count = 0;
694 cmd_request->bounce_sgl = NULL;
695 cmd_request->cmd = scmnd;
696
697 scmnd->host_scribble = (unsigned char *)cmd_request;
698
699 request = &cmd_request->request;
700
701 request->Extension =
702 (void *)((unsigned long)cmd_request + request_size);
703 DPRINT_DBG(STORVSC_DRV, "req %p size %d ext %d", request, request_size,
704 storvsc_drv_obj->RequestExtSize);
705
706
707 switch (scmnd->sc_data_direction) {
708 case DMA_TO_DEVICE:
709 request->Type = WRITE_TYPE;
710 break;
711 case DMA_FROM_DEVICE:
712 request->Type = READ_TYPE;
713 break;
714 default:
715 request->Type = UNKNOWN_TYPE;
716 break;
717 }
718
719 request->OnIOCompletion = storvsc_commmand_completion;
720 request->Context = cmd_request;
721
722
723 request->Host = host_device_ctx->port;
724 request->Bus = scmnd->device->channel;
725 request->TargetId = scmnd->device->id;
726 request->LunId = scmnd->device->lun;
727
728 ASSERT(scmnd->cmd_len <= 16);
729 request->CdbLen = scmnd->cmd_len;
730 request->Cdb = scmnd->cmnd;
731
732 request->SenseBuffer = scmnd->sense_buffer;
733 request->SenseBufferSize = SCSI_SENSE_BUFFERSIZE;
734
735
736 request->DataBuffer.Length = scsi_bufflen(scmnd);
737 if (scsi_sg_count(scmnd)) {
738 sgl = (struct scatterlist *)scsi_sglist(scmnd);
739
740
741 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
742 DPRINT_INFO(STORVSC_DRV,
743 "need to bounce buffer for this scmnd %p",
744 scmnd);
745 cmd_request->bounce_sgl =
746 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
747 scsi_bufflen(scmnd));
748 if (!cmd_request->bounce_sgl) {
749 DPRINT_ERR(STORVSC_DRV,
750 "unable to create bounce buffer for "
751 "this scmnd %p", scmnd);
752
753 scmnd->scsi_done = NULL;
754 scmnd->host_scribble = NULL;
755 kmem_cache_free(host_device_ctx->request_pool,
756 cmd_request);
757
758 return SCSI_MLQUEUE_HOST_BUSY;
759 }
760
761 cmd_request->bounce_sgl_count =
762 ALIGN_UP(scsi_bufflen(scmnd), PAGE_SIZE) >>
763 PAGE_SHIFT;
764
765
766
767
768
769 copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl,
770 scsi_sg_count(scmnd));
771
772 sgl = cmd_request->bounce_sgl;
773 }
774
775 request->DataBuffer.Offset = sgl[0].offset;
776
777 for (i = 0; i < scsi_sg_count(scmnd); i++) {
778 DPRINT_DBG(STORVSC_DRV, "sgl[%d] len %d offset %d \n",
779 i, sgl[i].length, sgl[i].offset);
780 request->DataBuffer.PfnArray[i] =
781 page_to_pfn(sg_page((&sgl[i])));
782 }
783 } else if (scsi_sglist(scmnd)) {
784 ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE);
785 request->DataBuffer.Offset =
786 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
787 request->DataBuffer.PfnArray[0] =
788 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
789 } else {
790 ASSERT(scsi_bufflen(scmnd) == 0);
791 }
792
793retry_request:
794
795 ret = storvsc_drv_obj->OnIORequest(&device_ctx->device_obj,
796 &cmd_request->request);
797 if (ret == -1) {
798
799 DPRINT_ERR(STORVSC_DRV,
800 "scmnd (%p) - queue FULL...marking queue busy",
801 scmnd);
802
803 if (cmd_request->bounce_sgl_count) {
804
805
806
807
808 copy_from_bounce_buffer(scsi_sglist(scmnd),
809 cmd_request->bounce_sgl,
810 scsi_sg_count(scmnd));
811 destroy_bounce_buffer(cmd_request->bounce_sgl,
812 cmd_request->bounce_sgl_count);
813 }
814
815 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
816
817 scmnd->scsi_done = NULL;
818 scmnd->host_scribble = NULL;
819
820 ret = SCSI_MLQUEUE_DEVICE_BUSY;
821 }
822
823 DPRINT_EXIT(STORVSC_DRV);
824
825 return ret;
826}
827
828static int storvsc_merge_bvec(struct request_queue *q,
829 struct bvec_merge_data *bmd, struct bio_vec *bvec)
830{
831
832 return bvec->bv_len;
833}
834
835
836
837
838static int storvsc_device_alloc(struct scsi_device *sdevice)
839{
840 DPRINT_DBG(STORVSC_DRV, "sdev (%p) - setting device flag to %d",
841 sdevice, BLIST_SPARSELUN);
842
843
844
845
846 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
847 return 0;
848}
849
850static int storvsc_device_configure(struct scsi_device *sdevice)
851{
852 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - curr queue depth %d", sdevice,
853 sdevice->queue_depth);
854
855 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting queue depth to %d",
856 sdevice, STORVSC_MAX_IO_REQUESTS);
857 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
858 STORVSC_MAX_IO_REQUESTS);
859
860 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting max segment size to %ld",
861 sdevice, PAGE_SIZE);
862 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
863
864 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine",
865 sdevice);
866 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
867
868 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
869
870
871 return 0;
872}
873
874
875
876
877static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
878{
879 int ret;
880 struct host_device_context *host_device_ctx =
881 (struct host_device_context *)scmnd->device->host->hostdata;
882 struct device_context *device_ctx = host_device_ctx->device_ctx;
883 struct driver_context *driver_ctx =
884 driver_to_driver_context(device_ctx->device.driver);
885 struct storvsc_driver_context *storvsc_drv_ctx =
886 (struct storvsc_driver_context *)driver_ctx;
887
888 struct storvsc_driver_object *storvsc_drv_obj =
889 &storvsc_drv_ctx->drv_obj;
890
891 DPRINT_ENTER(STORVSC_DRV);
892
893 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...",
894 scmnd->device, &device_ctx->device_obj);
895
896
897 ASSERT(storvsc_drv_obj->OnHostReset);
898 ret = storvsc_drv_obj->OnHostReset(&device_ctx->device_obj);
899 if (ret != 0) {
900 DPRINT_EXIT(STORVSC_DRV);
901 return ret;
902 }
903
904 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted",
905 scmnd->device, &device_ctx->device_obj);
906
907 DPRINT_EXIT(STORVSC_DRV);
908
909 return ret;
910}
911
912
913
914
915static void storvsc_host_rescan_callback(struct work_struct *work)
916{
917 struct hv_device *device_obj =
918 &((struct host_device_context *)work)->device_ctx->device_obj;
919 struct device_context *device_ctx = to_device_context(device_obj);
920 struct Scsi_Host *host = dev_get_drvdata(&device_ctx->device);
921 struct scsi_device *sdev;
922 struct host_device_context *host_device_ctx;
923 struct scsi_device **sdevs_remove_list;
924 unsigned int sdevs_count = 0;
925 unsigned int found;
926 unsigned int i;
927 unsigned int lun_count = 0;
928 unsigned int *lun_list;
929
930 DPRINT_ENTER(STORVSC_DRV);
931
932 host_device_ctx = (struct host_device_context *)host->hostdata;
933 lun_list = kcalloc(STORVSC_MAX_LUNS_PER_TARGET, sizeof(unsigned int),
934 GFP_ATOMIC);
935 if (!lun_list) {
936 DPRINT_ERR(STORVSC_DRV, "unable to allocate lun list");
937 return;
938 }
939
940 sdevs_remove_list = kcalloc(STORVSC_MAX_LUNS_PER_TARGET,
941 sizeof(void *), GFP_ATOMIC);
942 if (!sdevs_remove_list) {
943 kfree(lun_list);
944 DPRINT_ERR(STORVSC_DRV, "unable to allocate lun remove list");
945 return;
946 }
947
948 DPRINT_INFO(STORVSC_DRV, "rescanning host for new scsi devices...");
949
950
951 scsi_scan_target(&host->shost_gendev, host_device_ctx->path,
952 host_device_ctx->target, SCAN_WILD_CARD, 1);
953
954 DPRINT_INFO(STORVSC_DRV, "rescanning host for removed scsi device...");
955
956
957 shost_for_each_device(sdev, host) {
958 lun_count = STORVSC_MAX_LUNS_PER_TARGET;
959 storvsc_report_luns(sdev, lun_list, &lun_count);
960
961 DPRINT_INFO(STORVSC_DRV,
962 "report luns on scsi device (%p) found %u luns ",
963 sdev, lun_count);
964 DPRINT_INFO(STORVSC_DRV,
965 "existing luns on scsi device (%p) host (%d)",
966 sdev, host->host_no);
967
968 scsi_device_put(sdev);
969 break;
970 }
971
972 for (i = 0; i < lun_count; i++)
973 DPRINT_INFO(STORVSC_DRV, "%d) lun %u", i, lun_list[i]);
974
975
976
977
978
979
980 shost_for_each_device(sdev, host) {
981
982 found = 0;
983 for (i = 0; i < lun_count; i++) {
984 if (sdev->lun == lun_list[i]) {
985 found = 1;
986 break;
987 }
988 }
989 if (!found) {
990 DPRINT_INFO(STORVSC_DRV, "lun (%u) does not exists",
991 sdev->lun);
992 sdevs_remove_list[sdevs_count++] = sdev;
993 }
994 }
995
996
997 for (i = 0; i < sdevs_count; i++) {
998 DPRINT_INFO(STORVSC_DRV,
999 "removing scsi device (%p) lun (%u)...",
1000 sdevs_remove_list[i], sdevs_remove_list[i]->lun);
1001
1002
1003 if (!scsi_device_get(sdevs_remove_list[i])) {
1004 scsi_remove_device(sdevs_remove_list[i]);
1005 scsi_device_put(sdevs_remove_list[i]);
1006 }
1007 }
1008
1009 DPRINT_INFO(STORVSC_DRV, "rescan completed on dev obj (%p) "
1010 "target (%u) bus (%u)", device_obj,
1011 host_device_ctx->target, host_device_ctx->path);
1012
1013 kfree(lun_list);
1014 kfree(sdevs_remove_list);
1015
1016 DPRINT_EXIT(STORVSC_DRV);
1017}
1018
1019static int storvsc_report_luns(struct scsi_device *sdev, unsigned int luns[],
1020 unsigned int *lun_count)
1021{
1022 int i, j;
1023 unsigned int lun = 0;
1024 unsigned int num_luns;
1025 int result;
1026 unsigned char *data;
1027 struct scsi_sense_hdr sshdr;
1028 unsigned char cmd[16] = {0};
1029
1030 unsigned int report_len = 8 * (STORVSC_MAX_LUNS_PER_TARGET+1);
1031 unsigned long long *report_luns;
1032 const unsigned int in_lun_count = *lun_count;
1033
1034 *lun_count = 0;
1035
1036 report_luns = kzalloc(report_len, GFP_ATOMIC);
1037 if (!report_luns)
1038 return -ENOMEM;
1039
1040 cmd[0] = REPORT_LUNS;
1041
1042
1043 *(unsigned int *)&cmd[6] = cpu_to_be32(report_len);
1044
1045 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE,
1046 (unsigned char *)report_luns, report_len,
1047 &sshdr, 30 * HZ, 3, NULL);
1048 if (result != 0) {
1049 kfree(report_luns);
1050 return -EBUSY;
1051 }
1052
1053
1054 report_len = be32_to_cpu(*(unsigned int *)&report_luns[0]);
1055
1056 num_luns = (report_len / sizeof(unsigned long long));
1057 if (num_luns > in_lun_count) {
1058 kfree(report_luns);
1059 return -EINVAL;
1060 }
1061
1062 *lun_count = num_luns;
1063
1064 DPRINT_DBG(STORVSC_DRV,
1065 "report luns on scsi device (%p) found %u luns ",
1066 sdev, num_luns);
1067
1068
1069 for (i = 1; i < num_luns + 1; i++) {
1070 lun = 0;
1071 data = (unsigned char *)&report_luns[i];
1072 for (j = 0; j < sizeof(lun); j += 2) {
1073 lun = lun | (((data[j] << 8) | data[j + 1]) <<
1074 (j * 8));
1075 }
1076
1077 luns[i-1] = lun;
1078 }
1079
1080 kfree(report_luns);
1081 return 0;
1082}
1083
1084static void storvsc_host_rescan(struct hv_device *device_obj)
1085{
1086 struct device_context *device_ctx = to_device_context(device_obj);
1087 struct Scsi_Host *host = dev_get_drvdata(&device_ctx->device);
1088 struct host_device_context *host_device_ctx;
1089
1090 DPRINT_ENTER(STORVSC_DRV);
1091
1092 host_device_ctx = (struct host_device_context *)host->hostdata;
1093
1094 DPRINT_INFO(STORVSC_DRV, "initiating rescan on dev obj (%p) "
1095 "target (%u) bus (%u)...", device_obj,
1096 host_device_ctx->target, host_device_ctx->path);
1097
1098
1099
1100
1101
1102
1103 schedule_work(&host_device_ctx->host_rescan_work);
1104 DPRINT_EXIT(STORVSC_DRV);
1105}
1106
1107static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
1108 sector_t capacity, int *info)
1109{
1110 sector_t total_sectors = capacity;
1111 sector_t cylinder_times_heads = 0;
1112 sector_t temp = 0;
1113
1114 int sectors_per_track = 0;
1115 int heads = 0;
1116 int cylinders = 0;
1117 int rem = 0;
1118
1119 if (total_sectors > (65535 * 16 * 255))
1120 total_sectors = (65535 * 16 * 255);
1121
1122 if (total_sectors >= (65535 * 16 * 63)) {
1123 sectors_per_track = 255;
1124 heads = 16;
1125
1126 cylinder_times_heads = total_sectors;
1127
1128 rem = sector_div(cylinder_times_heads, sectors_per_track);
1129 } else {
1130 sectors_per_track = 17;
1131
1132 cylinder_times_heads = total_sectors;
1133
1134 rem = sector_div(cylinder_times_heads, sectors_per_track);
1135
1136 temp = cylinder_times_heads + 1023;
1137
1138 rem = sector_div(temp, 1024);
1139
1140 heads = temp;
1141
1142 if (heads < 4)
1143 heads = 4;
1144
1145 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
1146 sectors_per_track = 31;
1147 heads = 16;
1148
1149 cylinder_times_heads = total_sectors;
1150
1151
1152
1153
1154 rem = sector_div(cylinder_times_heads,
1155 sectors_per_track);
1156 }
1157
1158 if (cylinder_times_heads >= (heads * 1024)) {
1159 sectors_per_track = 63;
1160 heads = 16;
1161
1162 cylinder_times_heads = total_sectors;
1163
1164
1165
1166
1167 rem = sector_div(cylinder_times_heads,
1168 sectors_per_track);
1169 }
1170 }
1171
1172 temp = cylinder_times_heads;
1173
1174 rem = sector_div(temp, heads);
1175 cylinders = temp;
1176
1177 info[0] = heads;
1178 info[1] = sectors_per_track;
1179 info[2] = cylinders;
1180
1181 DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads,
1182 sectors_per_track);
1183
1184 return 0;
1185}
1186
1187static int __init storvsc_init(void)
1188{
1189 int ret;
1190
1191 DPRINT_ENTER(STORVSC_DRV);
1192 DPRINT_INFO(STORVSC_DRV, "Storvsc initializing....");
1193 ret = storvsc_drv_init(StorVscInitialize);
1194 DPRINT_EXIT(STORVSC_DRV);
1195 return ret;
1196}
1197
1198static void __exit storvsc_exit(void)
1199{
1200 DPRINT_ENTER(STORVSC_DRV);
1201 storvsc_drv_exit();
1202 DPRINT_ENTER(STORVSC_DRV);
1203}
1204
1205MODULE_LICENSE("GPL");
1206module_param(storvsc_ringbuffer_size, int, S_IRUGO);
1207module_init(storvsc_init);
1208module_exit(storvsc_exit);
1209