blob: 3b2917e8e490d05e6320f17c9dc208d52e99fdf0 [file] [log] [blame]
Hank Janssenbef4a342009-07-13 16:01:31 -07001/*
Hank Janssenbef4a342009-07-13 16:01:31 -07002 * 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>
K. Y. Srinivasan972621c2011-05-10 07:54:19 -070020 * K. Y. Srinivasan <kys@microsoft.com>
Hank Janssenbef4a342009-07-13 16:01:31 -070021 */
Hank Janssenbef4a342009-07-13 16:01:31 -070022#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070024#include <linux/module.h>
25#include <linux/device.h>
26#include <linux/blkdev.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070027#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>
Hank Janssenbef4a342009-07-13 16:01:31 -070034#include <scsi/scsi_dbg.h>
K. Y. Srinivasane3fe0bb2011-02-11 10:00:12 -080035#include "hv_api.h"
Greg Kroah-Hartman645954c2009-08-28 16:22:59 -070036#include "logging.h"
Greg Kroah-Hartman2d82f6c2010-05-05 22:52:28 -070037#include "version_info.h"
Greg Kroah-Hartman870cde82009-08-19 16:21:28 -070038#include "vmbus.h"
Greg Kroah-Hartmanbb969792010-05-05 22:40:43 -070039#include "storvsc_api.h"
K. Y. Srinivasanaf3043c2011-03-23 10:50:33 -070040#include "vstorage.h"
41#include "channel.h"
Hank Janssenbef4a342009-07-13 16:01:31 -070042
Hank Janssenbef4a342009-07-13 16:01:31 -070043
K. Y. Srinivasan0f0cdc62011-05-10 07:54:18 -070044static const char *driver_name = "storvsc";
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070045
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
K. Y. Srinivasan972621c2011-05-10 07:54:19 -070054struct hv_host_device {
K. Y. Srinivasan97c15292011-05-10 07:54:21 -070055 struct hv_device *dev;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070056 struct kmem_cache *request_pool;
57 unsigned int port;
58 unsigned char path;
59 unsigned char target;
Hank Janssenbef4a342009-07-13 16:01:31 -070060};
61
62struct storvsc_cmd_request {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070063 struct list_head entry;
64 struct scsi_cmnd *cmd;
Hank Janssenbef4a342009-07-13 16:01:31 -070065
66 unsigned int bounce_sgl_count;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070067 struct scatterlist *bounce_sgl;
Hank Janssenbef4a342009-07-13 16:01:31 -070068
Nicolas Palix0b3f6832009-07-29 14:10:19 +020069 struct hv_storvsc_request request;
Hank Janssenbef4a342009-07-13 16:01:31 -070070};
71
Hank Janssenbef4a342009-07-13 16:01:31 -070072
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070073/*
K. Y. Srinivasancf6fdfb2011-05-10 07:54:36 -070074 * storvsc_initialize - Main entry point
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070075 */
K. Y. Srinivasancf6fdfb2011-05-10 07:54:36 -070076static int storvsc_initialize(struct hv_driver *driver)
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070077{
K. Y. Srinivasan2e795052011-05-10 07:53:52 -070078 struct storvsc_driver *stor_driver;
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070079
K. Y. Srinivasanced01b02011-03-28 09:33:29 -070080 stor_driver = hvdr_to_stordr(driver);
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070081
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070082
83 /* Make sure we are at least 2 pages since 1 page is used for control */
84
K. Y. Srinivasan0f0cdc62011-05-10 07:54:18 -070085 driver->name = driver_name;
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070086 memcpy(&driver->dev_type, &gStorVscDeviceType,
87 sizeof(struct hv_guid));
88
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070089
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 */
K. Y. Srinivasan2ac5dad2011-05-10 07:53:57 -0700107 stor_driver->base.dev_add = storvsc_dev_add;
K. Y. Srinivasancb706b042011-05-10 07:53:58 -0700108 stor_driver->base.dev_rm = storvsc_dev_remove;
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -0700109
K. Y. Srinivasanbb465922011-05-10 07:54:02 -0700110 stor_driver->on_io_request = storvsc_do_io;
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -0700111
112 return 0;
113}
114
K. Y. Srinivasan5b60ace2011-05-10 07:54:25 -0700115static 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
K. Y. Srinivasana1ebfea2011-05-10 07:54:26 -0700125static 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
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700132static 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
K. Y. Srinivasan49a3c7a2011-05-10 07:54:28 -0700151static 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
K. Y. Srinivasan3862ef32011-05-10 07:54:29 -0700166static 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
K. Y. Srinivasana9753cb2011-05-10 07:54:30 -0700193static 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
K. Y. Srinivasan29fe2c92011-05-10 07:54:31 -0700222
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
K. Y. Srinivasan6a8ff442011-05-10 07:54:32 -0700287
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
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -0700353
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
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700384
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}
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -0700464
465static int storvsc_host_reset(struct hv_device *device)
466{
467 struct storvsc_device *stor_device;
468 struct hv_storvsc_request *request;
469 struct vstor_packet *vstor_packet;
470 int ret, t;
471
472 DPRINT_INFO(STORVSC, "resetting host adapter...");
473
474 stor_device = get_stor_device(device);
475 if (!stor_device)
476 return -1;
477
478 request = &stor_device->reset_request;
479 vstor_packet = &request->vstor_packet;
480
481 init_completion(&request->wait_event);
482
483 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
484 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
485 vstor_packet->vm_srb.path_id = stor_device->path_id;
486
487 ret = vmbus_sendpacket(device->channel, vstor_packet,
488 sizeof(struct vstor_packet),
489 (unsigned long)&stor_device->reset_request,
490 VM_PKT_DATA_INBAND,
491 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
492 if (ret != 0)
493 goto cleanup;
494
495 t = wait_for_completion_timeout(&request->wait_event, HZ);
496 if (t == 0) {
497 ret = -ETIMEDOUT;
498 goto cleanup;
499 }
500
501 DPRINT_INFO(STORVSC, "host adapter reset completed");
502
503 /*
504 * At this point, all outstanding requests in the adapter
505 * should have been flushed out and return to us
506 */
507
508cleanup:
509 put_stor_device(device);
510 return ret;
511}
512
513
Bill Pemberton454f18a2009-07-27 16:47:24 -0400514/* Static decl */
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700515static int storvsc_probe(struct hv_device *dev);
Jeff Garzikf2812332010-11-16 02:10:29 -0500516static int storvsc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd);
Hank Janssenbef4a342009-07-13 16:01:31 -0700517static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd);
Hank Janssenbef4a342009-07-13 16:01:31 -0700518
Hank Janssenbef4a342009-07-13 16:01:31 -0700519static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
Stephen Hemminger3afc7cc32010-05-06 21:44:45 -0700520module_param(storvsc_ringbuffer_size, int, S_IRUGO);
521MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
Hank Janssenbef4a342009-07-13 16:01:31 -0700522
Bill Pemberton454f18a2009-07-27 16:47:24 -0400523/* The one and only one */
K. Y. Srinivasan2e795052011-05-10 07:53:52 -0700524static struct storvsc_driver g_storvsc_drv;
Hank Janssenbef4a342009-07-13 16:01:31 -0700525
Bill Pemberton454f18a2009-07-27 16:47:24 -0400526/* Scsi driver */
Hank Janssenbef4a342009-07-13 16:01:31 -0700527static struct scsi_host_template scsi_driver = {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700528 .module = THIS_MODULE,
529 .name = "storvsc_host_t",
530 .bios_param = storvsc_get_chs,
531 .queuecommand = storvsc_queuecommand,
532 .eh_host_reset_handler = storvsc_host_reset_handler,
533 .slave_alloc = storvsc_device_alloc,
534 .slave_configure = storvsc_device_configure,
535 .cmd_per_lun = 1,
536 /* 64 max_queue * 1 target */
Lars Lindley0686e4f2010-03-11 23:51:23 +0100537 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700538 .this_id = -1,
Bill Pemberton454f18a2009-07-27 16:47:24 -0400539 /* no use setting to 0 since ll_blk_rw reset it to 1 */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700540 /* currently 32 */
541 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
542 /*
543 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
544 * into 1 sg element. If set, we must limit the max_segment_size to
545 * PAGE_SIZE, otherwise we may get 1 sg element that represents
546 * multiple
547 */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400548 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700549 .use_clustering = ENABLE_CLUSTERING,
Bill Pemberton454f18a2009-07-27 16:47:24 -0400550 /* Make sure we dont get a sg segment crosses a page boundary */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700551 .dma_boundary = PAGE_SIZE-1,
Hank Janssenbef4a342009-07-13 16:01:31 -0700552};
553
554
Hank Janssen3e189512010-03-04 22:11:00 +0000555/*
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700556 * storvsc_drv_init - StorVsc driver initialization.
557 */
K. Y. Srinivasandb085772011-03-23 10:50:27 -0700558static int storvsc_drv_init(void)
Hank Janssenbef4a342009-07-13 16:01:31 -0700559{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700560 int ret;
K. Y. Srinivasan2e795052011-05-10 07:53:52 -0700561 struct storvsc_driver *storvsc_drv_obj = &g_storvsc_drv;
K. Y. Srinivasan4af27d72011-03-07 13:34:01 -0800562 struct hv_driver *drv = &g_storvsc_drv.base;
Hank Janssenbef4a342009-07-13 16:01:31 -0700563
Hank Janssen8a046022010-12-06 12:26:45 -0800564 storvsc_drv_obj->ring_buffer_size = storvsc_ringbuffer_size;
Hank Janssenbef4a342009-07-13 16:01:31 -0700565
Bill Pemberton454f18a2009-07-27 16:47:24 -0400566 /* Callback to client driver to complete the initialization */
K. Y. Srinivasancf6fdfb2011-05-10 07:54:36 -0700567 storvsc_initialize(&storvsc_drv_obj->base);
Hank Janssenbef4a342009-07-13 16:01:31 -0700568
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700569 DPRINT_INFO(STORVSC_DRV,
K. Y. Srinivasan1e05d882011-03-28 09:33:28 -0700570 "max outstanding reqs %u",
Hank Janssen8a046022010-12-06 12:26:45 -0800571 storvsc_drv_obj->max_outstanding_req_per_channel);
Hank Janssenbef4a342009-07-13 16:01:31 -0700572
Hank Janssen8a046022010-12-06 12:26:45 -0800573 if (storvsc_drv_obj->max_outstanding_req_per_channel <
K. Y. Srinivasanf8feed02011-05-10 07:54:24 -0700574 STORVSC_MAX_IO_REQUESTS)
Hank Janssenbef4a342009-07-13 16:01:31 -0700575 return -1;
Hank Janssenbef4a342009-07-13 16:01:31 -0700576
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800577 drv->driver.name = storvsc_drv_obj->base.name;
Hank Janssenbef4a342009-07-13 16:01:31 -0700578
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700579 drv->probe = storvsc_probe;
K. Y. Srinivasan415b0232011-04-29 13:45:12 -0700580 drv->remove = storvsc_remove;
Hank Janssenbef4a342009-07-13 16:01:31 -0700581
Bill Pemberton454f18a2009-07-27 16:47:24 -0400582 /* The driver belongs to vmbus */
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800583 ret = vmbus_child_driver_register(&drv->driver);
Hank Janssenbef4a342009-07-13 16:01:31 -0700584
Hank Janssenbef4a342009-07-13 16:01:31 -0700585 return ret;
586}
587
Hank Janssenbef4a342009-07-13 16:01:31 -0700588static int storvsc_drv_exit_cb(struct device *dev, void *data)
589{
590 struct device **curr = (struct device **)data;
591 *curr = dev;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400592 return 1; /* stop iterating */
Hank Janssenbef4a342009-07-13 16:01:31 -0700593}
594
Greg Kroah-Hartmanbd1de702009-07-29 09:04:51 -0700595static void storvsc_drv_exit(void)
Hank Janssenbef4a342009-07-13 16:01:31 -0700596{
K. Y. Srinivasan2e795052011-05-10 07:53:52 -0700597 struct storvsc_driver *storvsc_drv_obj = &g_storvsc_drv;
K. Y. Srinivasan4af27d72011-03-07 13:34:01 -0800598 struct hv_driver *drv = &g_storvsc_drv.base;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700599 struct device *current_dev = NULL;
Bill Pemberton2295ba22009-07-28 13:46:22 -0400600 int ret;
Hank Janssenbef4a342009-07-13 16:01:31 -0700601
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700602 while (1) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700603 current_dev = NULL;
604
Bill Pemberton454f18a2009-07-27 16:47:24 -0400605 /* Get the device */
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800606 ret = driver_for_each_device(&drv->driver, NULL,
Bill Pemberton2295ba22009-07-28 13:46:22 -0400607 (void *) &current_dev,
608 storvsc_drv_exit_cb);
609
Hank Janssenbef4a342009-07-13 16:01:31 -0700610
611 if (current_dev == NULL)
612 break;
613
Bill Pemberton454f18a2009-07-27 16:47:24 -0400614 /* Initiate removal from the top-down */
Hank Janssenbef4a342009-07-13 16:01:31 -0700615 device_unregister(current_dev);
616 }
617
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800618 if (storvsc_drv_obj->base.cleanup)
619 storvsc_drv_obj->base.cleanup(&storvsc_drv_obj->base);
Hank Janssenbef4a342009-07-13 16:01:31 -0700620
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800621 vmbus_child_driver_unregister(&drv->driver);
Hank Janssenbef4a342009-07-13 16:01:31 -0700622 return;
623}
624
Hank Janssen3e189512010-03-04 22:11:00 +0000625/*
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700626 * storvsc_probe - Add a new device for this driver
627 */
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700628static int storvsc_probe(struct hv_device *device)
Hank Janssenbef4a342009-07-13 16:01:31 -0700629{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700630 int ret;
K. Y. Srinivasan2e795052011-05-10 07:53:52 -0700631 struct storvsc_driver *storvsc_drv_obj =
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700632 drv_to_stordrv(device->device.driver);
Hank Janssenbef4a342009-07-13 16:01:31 -0700633 struct Scsi_Host *host;
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700634 struct hv_host_device *host_dev;
Greg Kroah-Hartman9f0c7d22009-08-28 16:20:07 -0700635 struct storvsc_device_info device_info;
Hank Janssenbef4a342009-07-13 16:01:31 -0700636
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800637 if (!storvsc_drv_obj->base.dev_add)
Hank Janssenbef4a342009-07-13 16:01:31 -0700638 return -1;
639
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700640 host = scsi_host_alloc(&scsi_driver,
K. Y. Srinivasan972621c2011-05-10 07:54:19 -0700641 sizeof(struct hv_host_device));
K. Y. Srinivasanf8feed02011-05-10 07:54:24 -0700642 if (!host)
Hank Janssenbef4a342009-07-13 16:01:31 -0700643 return -ENOMEM;
Hank Janssenbef4a342009-07-13 16:01:31 -0700644
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700645 dev_set_drvdata(&device->device, host);
Hank Janssenbef4a342009-07-13 16:01:31 -0700646
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700647 host_dev = (struct hv_host_device *)host->hostdata;
648 memset(host_dev, 0, sizeof(struct hv_host_device));
Hank Janssenbef4a342009-07-13 16:01:31 -0700649
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700650 host_dev->port = host->host_no;
K. Y. Srinivasan97c15292011-05-10 07:54:21 -0700651 host_dev->dev = device;
Hank Janssenbef4a342009-07-13 16:01:31 -0700652
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700653 host_dev->request_pool =
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700654 kmem_cache_create(dev_name(&device->device),
K. Y. Srinivasan1e05d882011-03-28 09:33:28 -0700655 sizeof(struct storvsc_cmd_request), 0,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700656 SLAB_HWCACHE_ALIGN, NULL);
Hank Janssenbef4a342009-07-13 16:01:31 -0700657
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700658 if (!host_dev->request_pool) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700659 scsi_host_put(host);
Hank Janssenbef4a342009-07-13 16:01:31 -0700660 return -ENOMEM;
661 }
662
Hank Janssen8a046022010-12-06 12:26:45 -0800663 device_info.port_number = host->host_no;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400664 /* Call to the vsc driver to add the device */
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700665 ret = storvsc_drv_obj->base.dev_add(device, (void *)&device_info);
666
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700667 if (ret != 0) {
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700668 kmem_cache_destroy(host_dev->request_pool);
Hank Janssenbef4a342009-07-13 16:01:31 -0700669 scsi_host_put(host);
Hank Janssenbef4a342009-07-13 16:01:31 -0700670 return -1;
671 }
672
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700673 host_dev->path = device_info.path_id;
674 host_dev->target = device_info.target_id;
Hank Janssenbef4a342009-07-13 16:01:31 -0700675
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700676 /* max # of devices per target */
677 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
678 /* max # of targets per channel */
679 host->max_id = STORVSC_MAX_TARGETS;
680 /* max # of channels */
681 host->max_channel = STORVSC_MAX_CHANNELS - 1;
Hank Janssenbef4a342009-07-13 16:01:31 -0700682
Bill Pemberton454f18a2009-07-27 16:47:24 -0400683 /* Register the HBA and start the scsi bus scan */
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700684 ret = scsi_add_host(host, &device->device);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700685 if (ret != 0) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700686
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700687 storvsc_drv_obj->base.dev_rm(device);
Hank Janssenbef4a342009-07-13 16:01:31 -0700688
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700689 kmem_cache_destroy(host_dev->request_pool);
Hank Janssenbef4a342009-07-13 16:01:31 -0700690 scsi_host_put(host);
Hank Janssenbef4a342009-07-13 16:01:31 -0700691 return -1;
692 }
693
694 scsi_scan_host(host);
Hank Janssenbef4a342009-07-13 16:01:31 -0700695 return ret;
696}
697
Hank Janssen3e189512010-03-04 22:11:00 +0000698/*
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700699 * storvsc_commmand_completion - Command completion processing
700 */
Nicolas Palix0b3f6832009-07-29 14:10:19 +0200701static void storvsc_commmand_completion(struct hv_storvsc_request *request)
Hank Janssenbef4a342009-07-13 16:01:31 -0700702{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700703 struct storvsc_cmd_request *cmd_request =
K. Y. Srinivasand7a1bdb2011-03-28 09:33:44 -0700704 (struct storvsc_cmd_request *)request->context;
Hank Janssenbef4a342009-07-13 16:01:31 -0700705 struct scsi_cmnd *scmnd = cmd_request->cmd;
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700706 struct hv_host_device *host_dev =
K. Y. Srinivasan972621c2011-05-10 07:54:19 -0700707 (struct hv_host_device *)scmnd->device->host->hostdata;
Hank Janssenbef4a342009-07-13 16:01:31 -0700708 void (*scsi_done_fn)(struct scsi_cmnd *);
Hank Janssenbef4a342009-07-13 16:01:31 -0700709 struct scsi_sense_hdr sense_hdr;
K. Y. Srinivasan6dc3f0a2011-03-28 09:33:42 -0700710 struct vmscsi_request *vm_srb;
Hank Janssenbef4a342009-07-13 16:01:31 -0700711
Bill Pembertonb856e732010-05-05 15:27:41 -0400712 /* ASSERT(request == &cmd_request->request); */
713 /* ASSERT(scmnd); */
714 /* ASSERT((unsigned long)scmnd->host_scribble == */
715 /* (unsigned long)cmd_request); */
716 /* ASSERT(scmnd->scsi_done); */
Hank Janssenbef4a342009-07-13 16:01:31 -0700717
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700718 if (cmd_request->bounce_sgl_count) {
719 /* using bounce buffer */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400720 /* printk("copy_from_bounce_buffer\n"); */
Hank Janssenbef4a342009-07-13 16:01:31 -0700721
Bill Pemberton454f18a2009-07-27 16:47:24 -0400722 /* FIXME: We can optimize on writes by just skipping this */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700723 copy_from_bounce_buffer(scsi_sglist(scmnd),
724 cmd_request->bounce_sgl,
725 scsi_sg_count(scmnd));
726 destroy_bounce_buffer(cmd_request->bounce_sgl,
727 cmd_request->bounce_sgl_count);
Hank Janssenbef4a342009-07-13 16:01:31 -0700728 }
729
K. Y. Srinivasand7a1bdb2011-03-28 09:33:44 -0700730 vm_srb = &request->vstor_packet.vm_srb;
K. Y. Srinivasan6dc3f0a2011-03-28 09:33:42 -0700731 scmnd->result = vm_srb->scsi_status;
Hank Janssenbef4a342009-07-13 16:01:31 -0700732
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700733 if (scmnd->result) {
734 if (scsi_normalize_sense(scmnd->sense_buffer,
K. Y. Srinivasan6f461cc2011-03-28 09:33:38 -0700735 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
Hank Janssenbef4a342009-07-13 16:01:31 -0700736 scsi_print_sense_hdr("storvsc", &sense_hdr);
Hank Janssenbef4a342009-07-13 16:01:31 -0700737 }
738
Hank Janssen8a046022010-12-06 12:26:45 -0800739 /* ASSERT(request->BytesXfer <= request->data_buffer.Length); */
740 scsi_set_resid(scmnd,
K. Y. Srinivasand7a1bdb2011-03-28 09:33:44 -0700741 request->data_buffer.len -
K. Y. Srinivasane9e936c2011-03-28 09:33:43 -0700742 vm_srb->data_transfer_length);
Hank Janssenbef4a342009-07-13 16:01:31 -0700743
744 scsi_done_fn = scmnd->scsi_done;
745
746 scmnd->host_scribble = NULL;
747 scmnd->scsi_done = NULL;
748
Bill Pemberton454f18a2009-07-27 16:47:24 -0400749 /* !!DO NOT MODIFY the scmnd after this call */
Hank Janssenbef4a342009-07-13 16:01:31 -0700750 scsi_done_fn(scmnd);
751
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700752 kmem_cache_free(host_dev->request_pool, cmd_request);
Hank Janssenbef4a342009-07-13 16:01:31 -0700753}
754
Hank Janssen3e189512010-03-04 22:11:00 +0000755/*
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700756 * storvsc_queuecommand - Initiate command processing
757 */
Jeff Garzikf2812332010-11-16 02:10:29 -0500758static int storvsc_queuecommand_lck(struct scsi_cmnd *scmnd,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700759 void (*done)(struct scsi_cmnd *))
Hank Janssenbef4a342009-07-13 16:01:31 -0700760{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700761 int ret;
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700762 struct hv_host_device *host_dev =
K. Y. Srinivasan972621c2011-05-10 07:54:19 -0700763 (struct hv_host_device *)scmnd->device->host->hostdata;
K. Y. Srinivasanc2ad4832011-05-10 07:54:22 -0700764 struct hv_device *dev = host_dev->dev;
K. Y. Srinivasan2e795052011-05-10 07:53:52 -0700765 struct storvsc_driver *storvsc_drv_obj =
K. Y. Srinivasanc2ad4832011-05-10 07:54:22 -0700766 drv_to_stordrv(dev->device.driver);
Nicolas Palix0b3f6832009-07-29 14:10:19 +0200767 struct hv_storvsc_request *request;
Hank Janssenbef4a342009-07-13 16:01:31 -0700768 struct storvsc_cmd_request *cmd_request;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700769 unsigned int request_size = 0;
Hank Janssenbef4a342009-07-13 16:01:31 -0700770 int i;
771 struct scatterlist *sgl;
Hank Janssen77c5cea2010-09-01 11:10:41 -0700772 unsigned int sg_count = 0;
K. Y. Srinivasan12fbd412011-03-28 09:33:31 -0700773 struct vmscsi_request *vm_srb;
Hank Janssenbef4a342009-07-13 16:01:31 -0700774
Hank Janssenbef4a342009-07-13 16:01:31 -0700775
Bill Pemberton454f18a2009-07-27 16:47:24 -0400776 /* If retrying, no need to prep the cmd */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700777 if (scmnd->host_scribble) {
Bill Pembertonb856e732010-05-05 15:27:41 -0400778 /* ASSERT(scmnd->scsi_done != NULL); */
Hank Janssenbef4a342009-07-13 16:01:31 -0700779
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700780 cmd_request =
781 (struct storvsc_cmd_request *)scmnd->host_scribble;
782 DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p",
783 scmnd, cmd_request);
Hank Janssenbef4a342009-07-13 16:01:31 -0700784
785 goto retry_request;
786 }
787
Bill Pembertonb856e732010-05-05 15:27:41 -0400788 /* ASSERT(scmnd->scsi_done == NULL); */
789 /* ASSERT(scmnd->host_scribble == NULL); */
Hank Janssenbef4a342009-07-13 16:01:31 -0700790
791 scmnd->scsi_done = done;
792
793 request_size = sizeof(struct storvsc_cmd_request);
794
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700795 cmd_request = kmem_cache_zalloc(host_dev->request_pool,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700796 GFP_ATOMIC);
797 if (!cmd_request) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700798 scmnd->scsi_done = NULL;
799 return SCSI_MLQUEUE_DEVICE_BUSY;
800 }
801
Bill Pemberton454f18a2009-07-27 16:47:24 -0400802 /* Setup the cmd request */
Hank Janssenbef4a342009-07-13 16:01:31 -0700803 cmd_request->bounce_sgl_count = 0;
804 cmd_request->bounce_sgl = NULL;
805 cmd_request->cmd = scmnd;
806
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700807 scmnd->host_scribble = (unsigned char *)cmd_request;
Hank Janssenbef4a342009-07-13 16:01:31 -0700808
809 request = &cmd_request->request;
K. Y. Srinivasand7a1bdb2011-03-28 09:33:44 -0700810 vm_srb = &request->vstor_packet.vm_srb;
Hank Janssenbef4a342009-07-13 16:01:31 -0700811
Hank Janssenbef4a342009-07-13 16:01:31 -0700812
Bill Pemberton454f18a2009-07-27 16:47:24 -0400813 /* Build the SRB */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700814 switch (scmnd->sc_data_direction) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700815 case DMA_TO_DEVICE:
K. Y. Srinivasan12fbd412011-03-28 09:33:31 -0700816 vm_srb->data_in = WRITE_TYPE;
Hank Janssenbef4a342009-07-13 16:01:31 -0700817 break;
818 case DMA_FROM_DEVICE:
K. Y. Srinivasan12fbd412011-03-28 09:33:31 -0700819 vm_srb->data_in = READ_TYPE;
Hank Janssenbef4a342009-07-13 16:01:31 -0700820 break;
821 default:
K. Y. Srinivasan12fbd412011-03-28 09:33:31 -0700822 vm_srb->data_in = UNKNOWN_TYPE;
Hank Janssenbef4a342009-07-13 16:01:31 -0700823 break;
824 }
825
K. Y. Srinivasand7a1bdb2011-03-28 09:33:44 -0700826 request->on_io_completion = storvsc_commmand_completion;
827 request->context = cmd_request;/* scmnd; */
Hank Janssenbef4a342009-07-13 16:01:31 -0700828
Bill Pemberton454f18a2009-07-27 16:47:24 -0400829 /* request->PortId = scmnd->device->channel; */
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700830 vm_srb->port_number = host_dev->port;
K. Y. Srinivasan735625f2011-03-28 09:33:33 -0700831 vm_srb->path_id = scmnd->device->channel;
K. Y. Srinivasanb4dba0a2011-03-28 09:33:34 -0700832 vm_srb->target_id = scmnd->device->id;
K. Y. Srinivasanfc3967b2011-03-28 09:33:35 -0700833 vm_srb->lun = scmnd->device->lun;
Hank Janssenbef4a342009-07-13 16:01:31 -0700834
Bill Pembertonb856e732010-05-05 15:27:41 -0400835 /* ASSERT(scmnd->cmd_len <= 16); */
K. Y. Srinivasan473f9402011-03-28 09:33:36 -0700836 vm_srb->cdb_length = scmnd->cmd_len;
K. Y. Srinivasan373dd8a2011-03-28 09:33:37 -0700837
838 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
Hank Janssenbef4a342009-07-13 16:01:31 -0700839
K. Y. Srinivasand7a1bdb2011-03-28 09:33:44 -0700840 request->sense_buffer = scmnd->sense_buffer;
Hank Janssenbef4a342009-07-13 16:01:31 -0700841
842
K. Y. Srinivasand7a1bdb2011-03-28 09:33:44 -0700843 request->data_buffer.len = scsi_bufflen(scmnd);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700844 if (scsi_sg_count(scmnd)) {
845 sgl = (struct scatterlist *)scsi_sglist(scmnd);
Hank Janssen77c5cea2010-09-01 11:10:41 -0700846 sg_count = scsi_sg_count(scmnd);
Hank Janssenbef4a342009-07-13 16:01:31 -0700847
Bill Pemberton454f18a2009-07-27 16:47:24 -0400848 /* check if we need to bounce the sgl */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700849 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700850 cmd_request->bounce_sgl =
851 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
852 scsi_bufflen(scmnd));
853 if (!cmd_request->bounce_sgl) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700854 scmnd->scsi_done = NULL;
855 scmnd->host_scribble = NULL;
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700856 kmem_cache_free(host_dev->request_pool,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700857 cmd_request);
Hank Janssenbef4a342009-07-13 16:01:31 -0700858
859 return SCSI_MLQUEUE_HOST_BUSY;
860 }
861
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700862 cmd_request->bounce_sgl_count =
Uwe Kleine-König73509682011-01-20 09:32:01 +0100863 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700864 PAGE_SHIFT;
Hank Janssenbef4a342009-07-13 16:01:31 -0700865
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700866 /*
867 * FIXME: We can optimize on reads by just skipping
868 * this
869 */
870 copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl,
871 scsi_sg_count(scmnd));
Hank Janssenbef4a342009-07-13 16:01:31 -0700872
873 sgl = cmd_request->bounce_sgl;
Hank Janssen77c5cea2010-09-01 11:10:41 -0700874 sg_count = cmd_request->bounce_sgl_count;
Hank Janssenbef4a342009-07-13 16:01:31 -0700875 }
876
K. Y. Srinivasand7a1bdb2011-03-28 09:33:44 -0700877 request->data_buffer.offset = sgl[0].offset;
Hank Janssenbef4a342009-07-13 16:01:31 -0700878
K. Y. Srinivasanf8feed02011-05-10 07:54:24 -0700879 for (i = 0; i < sg_count; i++)
K. Y. Srinivasand7a1bdb2011-03-28 09:33:44 -0700880 request->data_buffer.pfn_array[i] =
Hank Janssen77c5cea2010-09-01 11:10:41 -0700881 page_to_pfn(sg_page((&sgl[i])));
K. Y. Srinivasanf8feed02011-05-10 07:54:24 -0700882
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700883 } else if (scsi_sglist(scmnd)) {
Bill Pembertonb856e732010-05-05 15:27:41 -0400884 /* ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE); */
K. Y. Srinivasand7a1bdb2011-03-28 09:33:44 -0700885 request->data_buffer.offset =
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700886 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
K. Y. Srinivasand7a1bdb2011-03-28 09:33:44 -0700887 request->data_buffer.pfn_array[0] =
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700888 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
Hank Janssenbef4a342009-07-13 16:01:31 -0700889 }
Hank Janssenbef4a342009-07-13 16:01:31 -0700890
891retry_request:
Bill Pemberton454f18a2009-07-27 16:47:24 -0400892 /* Invokes the vsc to start an IO */
K. Y. Srinivasanc2ad4832011-05-10 07:54:22 -0700893 ret = storvsc_drv_obj->on_io_request(dev,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700894 &cmd_request->request);
895 if (ret == -1) {
896 /* no more space */
Hank Janssenbef4a342009-07-13 16:01:31 -0700897
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700898 if (cmd_request->bounce_sgl_count) {
899 /*
900 * FIXME: We can optimize on writes by just skipping
901 * this
902 */
903 copy_from_bounce_buffer(scsi_sglist(scmnd),
904 cmd_request->bounce_sgl,
905 scsi_sg_count(scmnd));
906 destroy_bounce_buffer(cmd_request->bounce_sgl,
907 cmd_request->bounce_sgl_count);
Hank Janssenbef4a342009-07-13 16:01:31 -0700908 }
909
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700910 kmem_cache_free(host_dev->request_pool, cmd_request);
Hank Janssenbef4a342009-07-13 16:01:31 -0700911
912 scmnd->scsi_done = NULL;
913 scmnd->host_scribble = NULL;
914
915 ret = SCSI_MLQUEUE_DEVICE_BUSY;
916 }
917
Hank Janssenbef4a342009-07-13 16:01:31 -0700918 return ret;
919}
920
Jeff Garzikf2812332010-11-16 02:10:29 -0500921static DEF_SCSI_QCMD(storvsc_queuecommand)
922
Hank Janssen3e189512010-03-04 22:11:00 +0000923/*
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700924 * storvsc_host_reset_handler - Reset the scsi HBA
925 */
Hank Janssenbef4a342009-07-13 16:01:31 -0700926static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
927{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700928 int ret;
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700929 struct hv_host_device *host_dev =
K. Y. Srinivasan972621c2011-05-10 07:54:19 -0700930 (struct hv_host_device *)scmnd->device->host->hostdata;
K. Y. Srinivasanc2ad4832011-05-10 07:54:22 -0700931 struct hv_device *dev = host_dev->dev;
Hank Janssenbef4a342009-07-13 16:01:31 -0700932
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700933 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...",
K. Y. Srinivasanc2ad4832011-05-10 07:54:22 -0700934 scmnd->device, dev);
Hank Janssenbef4a342009-07-13 16:01:31 -0700935
Bill Pemberton454f18a2009-07-27 16:47:24 -0400936 /* Invokes the vsc to reset the host/bus */
K. Y. Srinivasan3d5f03f2011-05-10 07:54:35 -0700937 ret = storvsc_host_reset(dev);
Greg Kroah-Hartman83c720e2010-07-22 15:14:04 -0700938 if (ret != 0)
Hank Janssenbef4a342009-07-13 16:01:31 -0700939 return ret;
Hank Janssenbef4a342009-07-13 16:01:31 -0700940
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700941 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted",
K. Y. Srinivasanc2ad4832011-05-10 07:54:22 -0700942 scmnd->device, dev);
Hank Janssenbef4a342009-07-13 16:01:31 -0700943
Hank Janssenbef4a342009-07-13 16:01:31 -0700944 return ret;
945}
946
Hank Janssenbef4a342009-07-13 16:01:31 -0700947static int __init storvsc_init(void)
948{
949 int ret;
950
Hank Janssenbef4a342009-07-13 16:01:31 -0700951 DPRINT_INFO(STORVSC_DRV, "Storvsc initializing....");
K. Y. Srinivasandb085772011-03-23 10:50:27 -0700952 ret = storvsc_drv_init();
Hank Janssenbef4a342009-07-13 16:01:31 -0700953 return ret;
954}
955
956static void __exit storvsc_exit(void)
957{
Hank Janssenbef4a342009-07-13 16:01:31 -0700958 storvsc_drv_exit();
Hank Janssenbef4a342009-07-13 16:01:31 -0700959}
960
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700961MODULE_LICENSE("GPL");
Hank Janssen26c14cc2010-02-11 23:02:42 +0000962MODULE_VERSION(HV_DRV_VERSION);
Stephen Hemminger3afc7cc32010-05-06 21:44:45 -0700963MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
Hank Janssenbef4a342009-07-13 16:01:31 -0700964module_init(storvsc_init);
965module_exit(storvsc_exit);