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