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