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