Staging: hv: Move the definition of stor_vsc_initialize()
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / hv / storvsc_drv.c
1 /*
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>
20 */
21 #include <linux/init.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <linux/device.h>
25 #include <linux/blkdev.h>
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_cmnd.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_tcq.h>
31 #include <scsi/scsi_eh.h>
32 #include <scsi/scsi_devinfo.h>
33 #include <scsi/scsi_dbg.h>
34 #include "hv_api.h"
35 #include "logging.h"
36 #include "version_info.h"
37 #include "vmbus.h"
38 #include "storvsc_api.h"
39
40
41 static const char *g_driver_name = "storvsc";
42
43 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
44 static const struct hv_guid gStorVscDeviceType = {
45 .data = {
46 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
47 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f
48 }
49 };
50
51 struct host_device_context {
52 /* must be 1st field
53 * FIXME this is a bug */
54 /* point back to our device context */
55 struct hv_device *device_ctx;
56 struct kmem_cache *request_pool;
57 unsigned int port;
58 unsigned char path;
59 unsigned char target;
60 };
61
62 struct storvsc_cmd_request {
63 struct list_head entry;
64 struct scsi_cmnd *cmd;
65
66 unsigned int bounce_sgl_count;
67 struct scatterlist *bounce_sgl;
68
69 struct hv_storvsc_request request;
70 /* !!!DO NOT ADD ANYTHING BELOW HERE!!! */
71 /* The extension buffer falls right here and is pointed to by
72 * request.Extension;
73 * Which sounds like a very bad design... */
74 };
75
76
77 /*
78 * stor_vsc_initialize - Main entry point
79 */
80 int stor_vsc_initialize(struct hv_driver *driver)
81 {
82 struct storvsc_driver_object *stor_driver;
83
84 stor_driver = (struct storvsc_driver_object *)driver;
85
86 DPRINT_DBG(STORVSC, "sizeof(STORVSC_REQUEST)=%zd "
87 "sizeof(struct storvsc_request_extension)=%zd "
88 "sizeof(struct vstor_packet)=%zd, "
89 "sizeof(struct vmscsi_request)=%zd",
90 sizeof(struct hv_storvsc_request),
91 sizeof(struct storvsc_request_extension),
92 sizeof(struct vstor_packet),
93 sizeof(struct vmscsi_request));
94
95 /* Make sure we are at least 2 pages since 1 page is used for control */
96
97 driver->name = g_driver_name;
98 memcpy(&driver->dev_type, &gStorVscDeviceType,
99 sizeof(struct hv_guid));
100
101 stor_driver->request_ext_size =
102 sizeof(struct storvsc_request_extension);
103
104 /*
105 * Divide the ring buffer data size (which is 1 page less
106 * than the ring buffer size since that page is reserved for
107 * the ring buffer indices) by the max request size (which is
108 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
109 */
110 stor_driver->max_outstanding_req_per_channel =
111 ((stor_driver->ring_buffer_size - PAGE_SIZE) /
112 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
113 sizeof(struct vstor_packet) + sizeof(u64),
114 sizeof(u64)));
115
116 DPRINT_INFO(STORVSC, "max io %u, currently %u\n",
117 stor_driver->max_outstanding_req_per_channel,
118 STORVSC_MAX_IO_REQUESTS);
119
120 /* Setup the dispatch table */
121 stor_driver->base.dev_add = stor_vsc_on_device_add;
122 stor_driver->base.dev_rm = stor_vsc_on_device_remove;
123 stor_driver->base.cleanup = stor_vsc_on_cleanup;
124
125 stor_driver->on_io_request = stor_vsc_on_io_request;
126
127 return 0;
128 }
129
130 /* Static decl */
131 static int storvsc_probe(struct device *dev);
132 static int storvsc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd);
133 static int storvsc_device_alloc(struct scsi_device *);
134 static int storvsc_device_configure(struct scsi_device *);
135 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd);
136 static int storvsc_remove(struct device *dev);
137
138 static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
139 unsigned int sg_count,
140 unsigned int len);
141 static void destroy_bounce_buffer(struct scatterlist *sgl,
142 unsigned int sg_count);
143 static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count);
144 static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
145 struct scatterlist *bounce_sgl,
146 unsigned int orig_sgl_count);
147 static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
148 struct scatterlist *bounce_sgl,
149 unsigned int orig_sgl_count);
150
151 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device *bdev,
152 sector_t capacity, int *info);
153
154
155 static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
156 module_param(storvsc_ringbuffer_size, int, S_IRUGO);
157 MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
158
159 /* The one and only one */
160 static struct storvsc_driver_object g_storvsc_drv;
161
162 /* Scsi driver */
163 static struct scsi_host_template scsi_driver = {
164 .module = THIS_MODULE,
165 .name = "storvsc_host_t",
166 .bios_param = storvsc_get_chs,
167 .queuecommand = storvsc_queuecommand,
168 .eh_host_reset_handler = storvsc_host_reset_handler,
169 .slave_alloc = storvsc_device_alloc,
170 .slave_configure = storvsc_device_configure,
171 .cmd_per_lun = 1,
172 /* 64 max_queue * 1 target */
173 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
174 .this_id = -1,
175 /* no use setting to 0 since ll_blk_rw reset it to 1 */
176 /* currently 32 */
177 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
178 /*
179 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
180 * into 1 sg element. If set, we must limit the max_segment_size to
181 * PAGE_SIZE, otherwise we may get 1 sg element that represents
182 * multiple
183 */
184 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
185 .use_clustering = ENABLE_CLUSTERING,
186 /* Make sure we dont get a sg segment crosses a page boundary */
187 .dma_boundary = PAGE_SIZE-1,
188 };
189
190
191 /*
192 * storvsc_drv_init - StorVsc driver initialization.
193 */
194 static int storvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
195 {
196 int ret;
197 struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv;
198 struct hv_driver *drv = &g_storvsc_drv.base;
199
200 storvsc_drv_obj->ring_buffer_size = storvsc_ringbuffer_size;
201
202 /* Callback to client driver to complete the initialization */
203 drv_init(&storvsc_drv_obj->base);
204
205 drv->priv = storvsc_drv_obj;
206
207 DPRINT_INFO(STORVSC_DRV,
208 "request extension size %u, max outstanding reqs %u",
209 storvsc_drv_obj->request_ext_size,
210 storvsc_drv_obj->max_outstanding_req_per_channel);
211
212 if (storvsc_drv_obj->max_outstanding_req_per_channel <
213 STORVSC_MAX_IO_REQUESTS) {
214 DPRINT_ERR(STORVSC_DRV,
215 "The number of outstanding io requests (%d) "
216 "is larger than that supported (%d) internally.",
217 STORVSC_MAX_IO_REQUESTS,
218 storvsc_drv_obj->max_outstanding_req_per_channel);
219 return -1;
220 }
221
222 drv->driver.name = storvsc_drv_obj->base.name;
223
224 drv->driver.probe = storvsc_probe;
225 drv->driver.remove = storvsc_remove;
226
227 /* The driver belongs to vmbus */
228 ret = vmbus_child_driver_register(&drv->driver);
229
230 return ret;
231 }
232
233 static int storvsc_drv_exit_cb(struct device *dev, void *data)
234 {
235 struct device **curr = (struct device **)data;
236 *curr = dev;
237 return 1; /* stop iterating */
238 }
239
240 static void storvsc_drv_exit(void)
241 {
242 struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv;
243 struct hv_driver *drv = &g_storvsc_drv.base;
244 struct device *current_dev = NULL;
245 int ret;
246
247 while (1) {
248 current_dev = NULL;
249
250 /* Get the device */
251 ret = driver_for_each_device(&drv->driver, NULL,
252 (void *) &current_dev,
253 storvsc_drv_exit_cb);
254
255 if (ret)
256 DPRINT_WARN(STORVSC_DRV,
257 "driver_for_each_device returned %d", ret);
258
259 if (current_dev == NULL)
260 break;
261
262 /* Initiate removal from the top-down */
263 device_unregister(current_dev);
264 }
265
266 if (storvsc_drv_obj->base.cleanup)
267 storvsc_drv_obj->base.cleanup(&storvsc_drv_obj->base);
268
269 vmbus_child_driver_unregister(&drv->driver);
270 return;
271 }
272
273 /*
274 * storvsc_probe - Add a new device for this driver
275 */
276 static int storvsc_probe(struct device *device)
277 {
278 int ret;
279 struct hv_driver *drv =
280 drv_to_hv_drv(device->driver);
281 struct storvsc_driver_object *storvsc_drv_obj = drv->priv;
282 struct hv_device *device_obj = device_to_hv_device(device);
283 struct Scsi_Host *host;
284 struct host_device_context *host_device_ctx;
285 struct storvsc_device_info device_info;
286
287 if (!storvsc_drv_obj->base.dev_add)
288 return -1;
289
290 host = scsi_host_alloc(&scsi_driver,
291 sizeof(struct host_device_context));
292 if (!host) {
293 DPRINT_ERR(STORVSC_DRV, "unable to allocate scsi host object");
294 return -ENOMEM;
295 }
296
297 dev_set_drvdata(device, host);
298
299 host_device_ctx = (struct host_device_context *)host->hostdata;
300 memset(host_device_ctx, 0, sizeof(struct host_device_context));
301
302 host_device_ctx->port = host->host_no;
303 host_device_ctx->device_ctx = device_obj;
304
305 host_device_ctx->request_pool =
306 kmem_cache_create(dev_name(&device_obj->device),
307 sizeof(struct storvsc_cmd_request) +
308 storvsc_drv_obj->request_ext_size, 0,
309 SLAB_HWCACHE_ALIGN, NULL);
310
311 if (!host_device_ctx->request_pool) {
312 scsi_host_put(host);
313 return -ENOMEM;
314 }
315
316 device_info.port_number = host->host_no;
317 /* Call to the vsc driver to add the device */
318 ret = storvsc_drv_obj->base.dev_add(device_obj,
319 (void *)&device_info);
320 if (ret != 0) {
321 DPRINT_ERR(STORVSC_DRV, "unable to add scsi vsc device");
322 kmem_cache_destroy(host_device_ctx->request_pool);
323 scsi_host_put(host);
324 return -1;
325 }
326
327 /* host_device_ctx->port = device_info.PortNumber; */
328 host_device_ctx->path = device_info.path_id;
329 host_device_ctx->target = device_info.target_id;
330
331 /* max # of devices per target */
332 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
333 /* max # of targets per channel */
334 host->max_id = STORVSC_MAX_TARGETS;
335 /* max # of channels */
336 host->max_channel = STORVSC_MAX_CHANNELS - 1;
337
338 /* Register the HBA and start the scsi bus scan */
339 ret = scsi_add_host(host, device);
340 if (ret != 0) {
341 DPRINT_ERR(STORVSC_DRV, "unable to add scsi host device");
342
343 storvsc_drv_obj->base.dev_rm(device_obj);
344
345 kmem_cache_destroy(host_device_ctx->request_pool);
346 scsi_host_put(host);
347 return -1;
348 }
349
350 scsi_scan_host(host);
351 return ret;
352 }
353
354 /*
355 * storvsc_remove - Callback when our device is removed
356 */
357 static int storvsc_remove(struct device *device)
358 {
359 int ret;
360 struct hv_driver *drv =
361 drv_to_hv_drv(device->driver);
362 struct storvsc_driver_object *storvsc_drv_obj = drv->priv;
363 struct hv_device *device_obj = device_to_hv_device(device);
364 struct Scsi_Host *host = dev_get_drvdata(device);
365 struct host_device_context *host_device_ctx =
366 (struct host_device_context *)host->hostdata;
367
368
369 if (!storvsc_drv_obj->base.dev_rm)
370 return -1;
371
372 /*
373 * Call to the vsc driver to let it know that the device is being
374 * removed
375 */
376 ret = storvsc_drv_obj->base.dev_rm(device_obj);
377 if (ret != 0) {
378 /* TODO: */
379 DPRINT_ERR(STORVSC, "unable to remove vsc device (ret %d)",
380 ret);
381 }
382
383 if (host_device_ctx->request_pool) {
384 kmem_cache_destroy(host_device_ctx->request_pool);
385 host_device_ctx->request_pool = NULL;
386 }
387
388 DPRINT_INFO(STORVSC, "removing host adapter (%p)...", host);
389 scsi_remove_host(host);
390
391 DPRINT_INFO(STORVSC, "releasing host adapter (%p)...", host);
392 scsi_host_put(host);
393 return ret;
394 }
395
396 /*
397 * storvsc_commmand_completion - Command completion processing
398 */
399 static void storvsc_commmand_completion(struct hv_storvsc_request *request)
400 {
401 struct storvsc_cmd_request *cmd_request =
402 (struct storvsc_cmd_request *)request->context;
403 struct scsi_cmnd *scmnd = cmd_request->cmd;
404 struct host_device_context *host_device_ctx =
405 (struct host_device_context *)scmnd->device->host->hostdata;
406 void (*scsi_done_fn)(struct scsi_cmnd *);
407 struct scsi_sense_hdr sense_hdr;
408
409 /* ASSERT(request == &cmd_request->request); */
410 /* ASSERT(scmnd); */
411 /* ASSERT((unsigned long)scmnd->host_scribble == */
412 /* (unsigned long)cmd_request); */
413 /* ASSERT(scmnd->scsi_done); */
414
415 if (cmd_request->bounce_sgl_count) {
416 /* using bounce buffer */
417 /* printk("copy_from_bounce_buffer\n"); */
418
419 /* FIXME: We can optimize on writes by just skipping this */
420 copy_from_bounce_buffer(scsi_sglist(scmnd),
421 cmd_request->bounce_sgl,
422 scsi_sg_count(scmnd));
423 destroy_bounce_buffer(cmd_request->bounce_sgl,
424 cmd_request->bounce_sgl_count);
425 }
426
427 scmnd->result = request->status;
428
429 if (scmnd->result) {
430 if (scsi_normalize_sense(scmnd->sense_buffer,
431 request->sense_buffer_size, &sense_hdr))
432 scsi_print_sense_hdr("storvsc", &sense_hdr);
433 }
434
435 /* ASSERT(request->BytesXfer <= request->data_buffer.Length); */
436 scsi_set_resid(scmnd,
437 request->data_buffer.len - request->bytes_xfer);
438
439 scsi_done_fn = scmnd->scsi_done;
440
441 scmnd->host_scribble = NULL;
442 scmnd->scsi_done = NULL;
443
444 /* !!DO NOT MODIFY the scmnd after this call */
445 scsi_done_fn(scmnd);
446
447 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
448 }
449
450 static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
451 {
452 int i;
453
454 /* No need to check */
455 if (sg_count < 2)
456 return -1;
457
458 /* We have at least 2 sg entries */
459 for (i = 0; i < sg_count; i++) {
460 if (i == 0) {
461 /* make sure 1st one does not have hole */
462 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
463 return i;
464 } else if (i == sg_count - 1) {
465 /* make sure last one does not have hole */
466 if (sgl[i].offset != 0)
467 return i;
468 } else {
469 /* make sure no hole in the middle */
470 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
471 return i;
472 }
473 }
474 return -1;
475 }
476
477 static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
478 unsigned int sg_count,
479 unsigned int len)
480 {
481 int i;
482 int num_pages;
483 struct scatterlist *bounce_sgl;
484 struct page *page_buf;
485
486 num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
487
488 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
489 if (!bounce_sgl)
490 return NULL;
491
492 for (i = 0; i < num_pages; i++) {
493 page_buf = alloc_page(GFP_ATOMIC);
494 if (!page_buf)
495 goto cleanup;
496 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
497 }
498
499 return bounce_sgl;
500
501 cleanup:
502 destroy_bounce_buffer(bounce_sgl, num_pages);
503 return NULL;
504 }
505
506 static void destroy_bounce_buffer(struct scatterlist *sgl,
507 unsigned int sg_count)
508 {
509 int i;
510 struct page *page_buf;
511
512 for (i = 0; i < sg_count; i++) {
513 page_buf = sg_page((&sgl[i]));
514 if (page_buf != NULL)
515 __free_page(page_buf);
516 }
517
518 kfree(sgl);
519 }
520
521 /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
522 static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
523 struct scatterlist *bounce_sgl,
524 unsigned int orig_sgl_count)
525 {
526 int i;
527 int j = 0;
528 unsigned long src, dest;
529 unsigned int srclen, destlen, copylen;
530 unsigned int total_copied = 0;
531 unsigned long bounce_addr = 0;
532 unsigned long src_addr = 0;
533 unsigned long flags;
534
535 local_irq_save(flags);
536
537 for (i = 0; i < orig_sgl_count; i++) {
538 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
539 KM_IRQ0) + orig_sgl[i].offset;
540 src = src_addr;
541 srclen = orig_sgl[i].length;
542
543 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
544
545 if (bounce_addr == 0)
546 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
547
548 while (srclen) {
549 /* assume bounce offset always == 0 */
550 dest = bounce_addr + bounce_sgl[j].length;
551 destlen = PAGE_SIZE - bounce_sgl[j].length;
552
553 copylen = min(srclen, destlen);
554 memcpy((void *)dest, (void *)src, copylen);
555
556 total_copied += copylen;
557 bounce_sgl[j].length += copylen;
558 srclen -= copylen;
559 src += copylen;
560
561 if (bounce_sgl[j].length == PAGE_SIZE) {
562 /* full..move to next entry */
563 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
564 j++;
565
566 /* if we need to use another bounce buffer */
567 if (srclen || i != orig_sgl_count - 1)
568 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
569 } else if (srclen == 0 && i == orig_sgl_count - 1) {
570 /* unmap the last bounce that is < PAGE_SIZE */
571 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
572 }
573 }
574
575 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
576 }
577
578 local_irq_restore(flags);
579
580 return total_copied;
581 }
582
583 /* Assume the original sgl has enough room */
584 static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
585 struct scatterlist *bounce_sgl,
586 unsigned int orig_sgl_count)
587 {
588 int i;
589 int j = 0;
590 unsigned long src, dest;
591 unsigned int srclen, destlen, copylen;
592 unsigned int total_copied = 0;
593 unsigned long bounce_addr = 0;
594 unsigned long dest_addr = 0;
595 unsigned long flags;
596
597 local_irq_save(flags);
598
599 for (i = 0; i < orig_sgl_count; i++) {
600 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
601 KM_IRQ0) + orig_sgl[i].offset;
602 dest = dest_addr;
603 destlen = orig_sgl[i].length;
604 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
605
606 if (bounce_addr == 0)
607 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
608
609 while (destlen) {
610 src = bounce_addr + bounce_sgl[j].offset;
611 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
612
613 copylen = min(srclen, destlen);
614 memcpy((void *)dest, (void *)src, copylen);
615
616 total_copied += copylen;
617 bounce_sgl[j].offset += copylen;
618 destlen -= copylen;
619 dest += copylen;
620
621 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
622 /* full */
623 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
624 j++;
625
626 /* if we need to use another bounce buffer */
627 if (destlen || i != orig_sgl_count - 1)
628 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
629 } else if (destlen == 0 && i == orig_sgl_count - 1) {
630 /* unmap the last bounce that is < PAGE_SIZE */
631 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
632 }
633 }
634
635 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
636 KM_IRQ0);
637 }
638
639 local_irq_restore(flags);
640
641 return total_copied;
642 }
643
644 /*
645 * storvsc_queuecommand - Initiate command processing
646 */
647 static int storvsc_queuecommand_lck(struct scsi_cmnd *scmnd,
648 void (*done)(struct scsi_cmnd *))
649 {
650 int ret;
651 struct host_device_context *host_device_ctx =
652 (struct host_device_context *)scmnd->device->host->hostdata;
653 struct hv_device *device_ctx = host_device_ctx->device_ctx;
654 struct hv_driver *drv =
655 drv_to_hv_drv(device_ctx->device.driver);
656 struct storvsc_driver_object *storvsc_drv_obj = drv->priv;
657 struct hv_storvsc_request *request;
658 struct storvsc_cmd_request *cmd_request;
659 unsigned int request_size = 0;
660 int i;
661 struct scatterlist *sgl;
662 unsigned int sg_count = 0;
663
664 DPRINT_DBG(STORVSC_DRV, "scmnd %p dir %d, use_sg %d buf %p len %d "
665 "queue depth %d tagged %d", scmnd, scmnd->sc_data_direction,
666 scsi_sg_count(scmnd), scsi_sglist(scmnd),
667 scsi_bufflen(scmnd), scmnd->device->queue_depth,
668 scmnd->device->tagged_supported);
669
670 /* If retrying, no need to prep the cmd */
671 if (scmnd->host_scribble) {
672 /* ASSERT(scmnd->scsi_done != NULL); */
673
674 cmd_request =
675 (struct storvsc_cmd_request *)scmnd->host_scribble;
676 DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p",
677 scmnd, cmd_request);
678
679 goto retry_request;
680 }
681
682 /* ASSERT(scmnd->scsi_done == NULL); */
683 /* ASSERT(scmnd->host_scribble == NULL); */
684
685 scmnd->scsi_done = done;
686
687 request_size = sizeof(struct storvsc_cmd_request);
688
689 cmd_request = kmem_cache_alloc(host_device_ctx->request_pool,
690 GFP_ATOMIC);
691 if (!cmd_request) {
692 DPRINT_ERR(STORVSC_DRV, "scmnd (%p) - unable to allocate "
693 "storvsc_cmd_request...marking queue busy", scmnd);
694 scmnd->scsi_done = NULL;
695 return SCSI_MLQUEUE_DEVICE_BUSY;
696 }
697
698 /* Setup the cmd request */
699 cmd_request->bounce_sgl_count = 0;
700 cmd_request->bounce_sgl = NULL;
701 cmd_request->cmd = scmnd;
702
703 scmnd->host_scribble = (unsigned char *)cmd_request;
704
705 request = &cmd_request->request;
706
707 request->extension =
708 (void *)((unsigned long)cmd_request + request_size);
709 DPRINT_DBG(STORVSC_DRV, "req %p size %d ext %d", request, request_size,
710 storvsc_drv_obj->request_ext_size);
711
712 /* Build the SRB */
713 switch (scmnd->sc_data_direction) {
714 case DMA_TO_DEVICE:
715 request->type = WRITE_TYPE;
716 break;
717 case DMA_FROM_DEVICE:
718 request->type = READ_TYPE;
719 break;
720 default:
721 request->type = UNKNOWN_TYPE;
722 break;
723 }
724
725 request->on_io_completion = storvsc_commmand_completion;
726 request->context = cmd_request;/* scmnd; */
727
728 /* request->PortId = scmnd->device->channel; */
729 request->host = host_device_ctx->port;
730 request->bus = scmnd->device->channel;
731 request->target_id = scmnd->device->id;
732 request->lun_id = scmnd->device->lun;
733
734 /* ASSERT(scmnd->cmd_len <= 16); */
735 request->cdb_len = scmnd->cmd_len;
736 request->cdb = scmnd->cmnd;
737
738 request->sense_buffer = scmnd->sense_buffer;
739 request->sense_buffer_size = SCSI_SENSE_BUFFERSIZE;
740
741
742 request->data_buffer.len = scsi_bufflen(scmnd);
743 if (scsi_sg_count(scmnd)) {
744 sgl = (struct scatterlist *)scsi_sglist(scmnd);
745 sg_count = scsi_sg_count(scmnd);
746
747 /* check if we need to bounce the sgl */
748 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
749 DPRINT_INFO(STORVSC_DRV,
750 "need to bounce buffer for this scmnd %p",
751 scmnd);
752 cmd_request->bounce_sgl =
753 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
754 scsi_bufflen(scmnd));
755 if (!cmd_request->bounce_sgl) {
756 DPRINT_ERR(STORVSC_DRV,
757 "unable to create bounce buffer for "
758 "this scmnd %p", scmnd);
759
760 scmnd->scsi_done = NULL;
761 scmnd->host_scribble = NULL;
762 kmem_cache_free(host_device_ctx->request_pool,
763 cmd_request);
764
765 return SCSI_MLQUEUE_HOST_BUSY;
766 }
767
768 cmd_request->bounce_sgl_count =
769 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
770 PAGE_SHIFT;
771
772 /*
773 * FIXME: We can optimize on reads by just skipping
774 * this
775 */
776 copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl,
777 scsi_sg_count(scmnd));
778
779 sgl = cmd_request->bounce_sgl;
780 sg_count = cmd_request->bounce_sgl_count;
781 }
782
783 request->data_buffer.offset = sgl[0].offset;
784
785 for (i = 0; i < sg_count; i++) {
786 DPRINT_DBG(STORVSC_DRV, "sgl[%d] len %d offset %d\n",
787 i, sgl[i].length, sgl[i].offset);
788 request->data_buffer.pfn_array[i] =
789 page_to_pfn(sg_page((&sgl[i])));
790 }
791 } else if (scsi_sglist(scmnd)) {
792 /* ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE); */
793 request->data_buffer.offset =
794 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
795 request->data_buffer.pfn_array[0] =
796 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
797 }
798
799 retry_request:
800 /* Invokes the vsc to start an IO */
801 ret = storvsc_drv_obj->on_io_request(device_ctx,
802 &cmd_request->request);
803 if (ret == -1) {
804 /* no more space */
805 DPRINT_ERR(STORVSC_DRV,
806 "scmnd (%p) - queue FULL...marking queue busy",
807 scmnd);
808
809 if (cmd_request->bounce_sgl_count) {
810 /*
811 * FIXME: We can optimize on writes by just skipping
812 * this
813 */
814 copy_from_bounce_buffer(scsi_sglist(scmnd),
815 cmd_request->bounce_sgl,
816 scsi_sg_count(scmnd));
817 destroy_bounce_buffer(cmd_request->bounce_sgl,
818 cmd_request->bounce_sgl_count);
819 }
820
821 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
822
823 scmnd->scsi_done = NULL;
824 scmnd->host_scribble = NULL;
825
826 ret = SCSI_MLQUEUE_DEVICE_BUSY;
827 }
828
829 return ret;
830 }
831
832 static DEF_SCSI_QCMD(storvsc_queuecommand)
833
834 static int storvsc_merge_bvec(struct request_queue *q,
835 struct bvec_merge_data *bmd, struct bio_vec *bvec)
836 {
837 /* checking done by caller. */
838 return bvec->bv_len;
839 }
840
841 /*
842 * storvsc_device_configure - Configure the specified scsi device
843 */
844 static int storvsc_device_alloc(struct scsi_device *sdevice)
845 {
846 DPRINT_DBG(STORVSC_DRV, "sdev (%p) - setting device flag to %d",
847 sdevice, BLIST_SPARSELUN);
848 /*
849 * This enables luns to be located sparsely. Otherwise, we may not
850 * discovered them.
851 */
852 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
853 return 0;
854 }
855
856 static int storvsc_device_configure(struct scsi_device *sdevice)
857 {
858 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - curr queue depth %d", sdevice,
859 sdevice->queue_depth);
860
861 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting queue depth to %d",
862 sdevice, STORVSC_MAX_IO_REQUESTS);
863 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
864 STORVSC_MAX_IO_REQUESTS);
865
866 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting max segment size to %ld",
867 sdevice, PAGE_SIZE);
868 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
869
870 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine",
871 sdevice);
872 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
873
874 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
875 /* sdevice->timeout = (2000 * HZ);//(75 * HZ); */
876
877 return 0;
878 }
879
880 /*
881 * storvsc_host_reset_handler - Reset the scsi HBA
882 */
883 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
884 {
885 int ret;
886 struct host_device_context *host_device_ctx =
887 (struct host_device_context *)scmnd->device->host->hostdata;
888 struct hv_device *device_ctx = host_device_ctx->device_ctx;
889
890 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...",
891 scmnd->device, device_ctx);
892
893 /* Invokes the vsc to reset the host/bus */
894 ret = stor_vsc_on_host_reset(device_ctx);
895 if (ret != 0)
896 return ret;
897
898 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted",
899 scmnd->device, device_ctx);
900
901 return ret;
902 }
903
904 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
905 sector_t capacity, int *info)
906 {
907 sector_t total_sectors = capacity;
908 sector_t cylinder_times_heads = 0;
909 sector_t temp = 0;
910
911 int sectors_per_track = 0;
912 int heads = 0;
913 int cylinders = 0;
914 int rem = 0;
915
916 if (total_sectors > (65535 * 16 * 255))
917 total_sectors = (65535 * 16 * 255);
918
919 if (total_sectors >= (65535 * 16 * 63)) {
920 sectors_per_track = 255;
921 heads = 16;
922
923 cylinder_times_heads = total_sectors;
924 /* sector_div stores the quotient in cylinder_times_heads */
925 rem = sector_div(cylinder_times_heads, sectors_per_track);
926 } else {
927 sectors_per_track = 17;
928
929 cylinder_times_heads = total_sectors;
930 /* sector_div stores the quotient in cylinder_times_heads */
931 rem = sector_div(cylinder_times_heads, sectors_per_track);
932
933 temp = cylinder_times_heads + 1023;
934 /* sector_div stores the quotient in temp */
935 rem = sector_div(temp, 1024);
936
937 heads = temp;
938
939 if (heads < 4)
940 heads = 4;
941
942 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
943 sectors_per_track = 31;
944 heads = 16;
945
946 cylinder_times_heads = total_sectors;
947 /*
948 * sector_div stores the quotient in
949 * cylinder_times_heads
950 */
951 rem = sector_div(cylinder_times_heads,
952 sectors_per_track);
953 }
954
955 if (cylinder_times_heads >= (heads * 1024)) {
956 sectors_per_track = 63;
957 heads = 16;
958
959 cylinder_times_heads = total_sectors;
960 /*
961 * sector_div stores the quotient in
962 * cylinder_times_heads
963 */
964 rem = sector_div(cylinder_times_heads,
965 sectors_per_track);
966 }
967 }
968
969 temp = cylinder_times_heads;
970 /* sector_div stores the quotient in temp */
971 rem = sector_div(temp, heads);
972 cylinders = temp;
973
974 info[0] = heads;
975 info[1] = sectors_per_track;
976 info[2] = cylinders;
977
978 DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads,
979 sectors_per_track);
980
981 return 0;
982 }
983
984 static int __init storvsc_init(void)
985 {
986 int ret;
987
988 DPRINT_INFO(STORVSC_DRV, "Storvsc initializing....");
989 ret = storvsc_drv_init(stor_vsc_initialize);
990 return ret;
991 }
992
993 static void __exit storvsc_exit(void)
994 {
995 storvsc_drv_exit();
996 }
997
998 MODULE_LICENSE("GPL");
999 MODULE_VERSION(HV_DRV_VERSION);
1000 MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
1001 module_init(storvsc_init);
1002 module_exit(storvsc_exit);