blob: 6300b7915bc78056497a2a026b8f2d752f637128 [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
K. Y. Srinivasan2db2cab2011-05-10 07:54:39 -070043static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
Hank Janssenbef4a342009-07-13 16:01:31 -070044
K. Y. Srinivasan3d598ce2011-05-10 07:54:40 -070045module_param(storvsc_ringbuffer_size, int, S_IRUGO);
46MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
47
K. Y. Srinivasan0f0cdc62011-05-10 07:54:18 -070048static const char *driver_name = "storvsc";
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070049
50/* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
51static const struct hv_guid gStorVscDeviceType = {
52 .data = {
53 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
54 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f
55 }
56};
57
K. Y. Srinivasan972621c2011-05-10 07:54:19 -070058struct hv_host_device {
K. Y. Srinivasan97c15292011-05-10 07:54:21 -070059 struct hv_device *dev;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070060 struct kmem_cache *request_pool;
61 unsigned int port;
62 unsigned char path;
63 unsigned char target;
Hank Janssenbef4a342009-07-13 16:01:31 -070064};
65
66struct storvsc_cmd_request {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070067 struct list_head entry;
68 struct scsi_cmnd *cmd;
Hank Janssenbef4a342009-07-13 16:01:31 -070069
70 unsigned int bounce_sgl_count;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070071 struct scatterlist *bounce_sgl;
Hank Janssenbef4a342009-07-13 16:01:31 -070072
Nicolas Palix0b3f6832009-07-29 14:10:19 +020073 struct hv_storvsc_request request;
Hank Janssenbef4a342009-07-13 16:01:31 -070074};
75
Hank Janssenbef4a342009-07-13 16:01:31 -070076
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070077/*
K. Y. Srinivasancf6fdfb2011-05-10 07:54:36 -070078 * storvsc_initialize - Main entry point
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070079 */
K. Y. Srinivasancf6fdfb2011-05-10 07:54:36 -070080static int storvsc_initialize(struct hv_driver *driver)
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070081{
K. Y. Srinivasan2e795052011-05-10 07:53:52 -070082 struct storvsc_driver *stor_driver;
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070083
K. Y. Srinivasanced01b02011-03-28 09:33:29 -070084 stor_driver = hvdr_to_stordr(driver);
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070085
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070086
87 /* Make sure we are at least 2 pages since 1 page is used for control */
88
K. Y. Srinivasan0f0cdc62011-05-10 07:54:18 -070089 driver->name = driver_name;
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070090 memcpy(&driver->dev_type, &gStorVscDeviceType,
91 sizeof(struct hv_guid));
92
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070093
94 /*
95 * Divide the ring buffer data size (which is 1 page less
96 * than the ring buffer size since that page is reserved for
97 * the ring buffer indices) by the max request size (which is
98 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
99 */
100 stor_driver->max_outstanding_req_per_channel =
101 ((stor_driver->ring_buffer_size - PAGE_SIZE) /
102 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
103 sizeof(struct vstor_packet) + sizeof(u64),
104 sizeof(u64)));
105
106 DPRINT_INFO(STORVSC, "max io %u, currently %u\n",
107 stor_driver->max_outstanding_req_per_channel,
108 STORVSC_MAX_IO_REQUESTS);
109
110 /* Setup the dispatch table */
K. Y. Srinivasan2ac5dad2011-05-10 07:53:57 -0700111 stor_driver->base.dev_add = storvsc_dev_add;
K. Y. Srinivasancb706b042011-05-10 07:53:58 -0700112 stor_driver->base.dev_rm = storvsc_dev_remove;
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -0700113
K. Y. Srinivasanbb465922011-05-10 07:54:02 -0700114 stor_driver->on_io_request = storvsc_do_io;
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -0700115
116 return 0;
117}
118
K. Y. Srinivasan5b60ace2011-05-10 07:54:25 -0700119static int storvsc_device_alloc(struct scsi_device *sdevice)
120{
121 /*
122 * This enables luns to be located sparsely. Otherwise, we may not
123 * discovered them.
124 */
125 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
126 return 0;
127}
128
K. Y. Srinivasana1ebfea2011-05-10 07:54:26 -0700129static int storvsc_merge_bvec(struct request_queue *q,
130 struct bvec_merge_data *bmd, struct bio_vec *bvec)
131{
132 /* checking done by caller. */
133 return bvec->bv_len;
134}
135
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700136static int storvsc_device_configure(struct scsi_device *sdevice)
137{
138 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
139 STORVSC_MAX_IO_REQUESTS);
140
141 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting max segment size to %ld",
142 sdevice, PAGE_SIZE);
143 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
144
145 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine",
146 sdevice);
147 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
148
149 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700150
151 return 0;
152}
153
K. Y. Srinivasan49a3c7a2011-05-10 07:54:28 -0700154static void destroy_bounce_buffer(struct scatterlist *sgl,
155 unsigned int sg_count)
156{
157 int i;
158 struct page *page_buf;
159
160 for (i = 0; i < sg_count; i++) {
161 page_buf = sg_page((&sgl[i]));
162 if (page_buf != NULL)
163 __free_page(page_buf);
164 }
165
166 kfree(sgl);
167}
168
K. Y. Srinivasan3862ef32011-05-10 07:54:29 -0700169static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
170{
171 int i;
172
173 /* No need to check */
174 if (sg_count < 2)
175 return -1;
176
177 /* We have at least 2 sg entries */
178 for (i = 0; i < sg_count; i++) {
179 if (i == 0) {
180 /* make sure 1st one does not have hole */
181 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
182 return i;
183 } else if (i == sg_count - 1) {
184 /* make sure last one does not have hole */
185 if (sgl[i].offset != 0)
186 return i;
187 } else {
188 /* make sure no hole in the middle */
189 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
190 return i;
191 }
192 }
193 return -1;
194}
195
K. Y. Srinivasana9753cb2011-05-10 07:54:30 -0700196static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
197 unsigned int sg_count,
198 unsigned int len)
199{
200 int i;
201 int num_pages;
202 struct scatterlist *bounce_sgl;
203 struct page *page_buf;
204
205 num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
206
207 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
208 if (!bounce_sgl)
209 return NULL;
210
211 for (i = 0; i < num_pages; i++) {
212 page_buf = alloc_page(GFP_ATOMIC);
213 if (!page_buf)
214 goto cleanup;
215 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
216 }
217
218 return bounce_sgl;
219
220cleanup:
221 destroy_bounce_buffer(bounce_sgl, num_pages);
222 return NULL;
223}
224
K. Y. Srinivasan29fe2c92011-05-10 07:54:31 -0700225
226/* Assume the original sgl has enough room */
227static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
228 struct scatterlist *bounce_sgl,
229 unsigned int orig_sgl_count)
230{
231 int i;
232 int j = 0;
233 unsigned long src, dest;
234 unsigned int srclen, destlen, copylen;
235 unsigned int total_copied = 0;
236 unsigned long bounce_addr = 0;
237 unsigned long dest_addr = 0;
238 unsigned long flags;
239
240 local_irq_save(flags);
241
242 for (i = 0; i < orig_sgl_count; i++) {
243 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
244 KM_IRQ0) + orig_sgl[i].offset;
245 dest = dest_addr;
246 destlen = orig_sgl[i].length;
247
248 if (bounce_addr == 0)
249 bounce_addr =
250 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
251 KM_IRQ0);
252
253 while (destlen) {
254 src = bounce_addr + bounce_sgl[j].offset;
255 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
256
257 copylen = min(srclen, destlen);
258 memcpy((void *)dest, (void *)src, copylen);
259
260 total_copied += copylen;
261 bounce_sgl[j].offset += copylen;
262 destlen -= copylen;
263 dest += copylen;
264
265 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
266 /* full */
267 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
268 j++;
269
270 /* if we need to use another bounce buffer */
271 if (destlen || i != orig_sgl_count - 1)
272 bounce_addr =
273 (unsigned long)kmap_atomic(
274 sg_page((&bounce_sgl[j])), KM_IRQ0);
275 } else if (destlen == 0 && i == orig_sgl_count - 1) {
276 /* unmap the last bounce that is < PAGE_SIZE */
277 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
278 }
279 }
280
281 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
282 KM_IRQ0);
283 }
284
285 local_irq_restore(flags);
286
287 return total_copied;
288}
289
K. Y. Srinivasan6a8ff442011-05-10 07:54:32 -0700290
291/* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
292static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
293 struct scatterlist *bounce_sgl,
294 unsigned int orig_sgl_count)
295{
296 int i;
297 int j = 0;
298 unsigned long src, dest;
299 unsigned int srclen, destlen, copylen;
300 unsigned int total_copied = 0;
301 unsigned long bounce_addr = 0;
302 unsigned long src_addr = 0;
303 unsigned long flags;
304
305 local_irq_save(flags);
306
307 for (i = 0; i < orig_sgl_count; i++) {
308 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
309 KM_IRQ0) + orig_sgl[i].offset;
310 src = src_addr;
311 srclen = orig_sgl[i].length;
312
313 if (bounce_addr == 0)
314 bounce_addr =
315 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
316 KM_IRQ0);
317
318 while (srclen) {
319 /* assume bounce offset always == 0 */
320 dest = bounce_addr + bounce_sgl[j].length;
321 destlen = PAGE_SIZE - bounce_sgl[j].length;
322
323 copylen = min(srclen, destlen);
324 memcpy((void *)dest, (void *)src, copylen);
325
326 total_copied += copylen;
327 bounce_sgl[j].length += copylen;
328 srclen -= copylen;
329 src += copylen;
330
331 if (bounce_sgl[j].length == PAGE_SIZE) {
332 /* full..move to next entry */
333 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
334 j++;
335
336 /* if we need to use another bounce buffer */
337 if (srclen || i != orig_sgl_count - 1)
338 bounce_addr =
339 (unsigned long)kmap_atomic(
340 sg_page((&bounce_sgl[j])), KM_IRQ0);
341
342 } else if (srclen == 0 && i == orig_sgl_count - 1) {
343 /* unmap the last bounce that is < PAGE_SIZE */
344 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
345 }
346 }
347
348 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
349 }
350
351 local_irq_restore(flags);
352
353 return total_copied;
354}
355
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -0700356
357/*
358 * storvsc_remove - Callback when our device is removed
359 */
360static int storvsc_remove(struct hv_device *dev)
361{
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -0700362 struct Scsi_Host *host = dev_get_drvdata(&dev->device);
363 struct hv_host_device *host_dev =
364 (struct hv_host_device *)host->hostdata;
365
366 /*
367 * Call to the vsc driver to let it know that the device is being
368 * removed
369 */
K. Y. Srinivasan6cdc57c2011-05-10 07:54:48 -0700370 storvsc_dev_remove(dev);
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -0700371
372 if (host_dev->request_pool) {
373 kmem_cache_destroy(host_dev->request_pool);
374 host_dev->request_pool = NULL;
375 }
376
377 DPRINT_INFO(STORVSC, "removing host adapter (%p)...", host);
378 scsi_remove_host(host);
379
380 DPRINT_INFO(STORVSC, "releasing host adapter (%p)...", host);
381 scsi_host_put(host);
382 return 0;
383}
384
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700385
386static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
387 sector_t capacity, int *info)
388{
389 sector_t total_sectors = capacity;
390 sector_t cylinder_times_heads = 0;
391 sector_t temp = 0;
392
393 int sectors_per_track = 0;
394 int heads = 0;
395 int cylinders = 0;
396 int rem = 0;
397
398 if (total_sectors > (65535 * 16 * 255))
399 total_sectors = (65535 * 16 * 255);
400
401 if (total_sectors >= (65535 * 16 * 63)) {
402 sectors_per_track = 255;
403 heads = 16;
404
405 cylinder_times_heads = total_sectors;
406 /* sector_div stores the quotient in cylinder_times_heads */
407 rem = sector_div(cylinder_times_heads, sectors_per_track);
408 } else {
409 sectors_per_track = 17;
410
411 cylinder_times_heads = total_sectors;
412 /* sector_div stores the quotient in cylinder_times_heads */
413 rem = sector_div(cylinder_times_heads, sectors_per_track);
414
415 temp = cylinder_times_heads + 1023;
416 /* sector_div stores the quotient in temp */
417 rem = sector_div(temp, 1024);
418
419 heads = temp;
420
421 if (heads < 4)
422 heads = 4;
423
424 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
425 sectors_per_track = 31;
426 heads = 16;
427
428 cylinder_times_heads = total_sectors;
429 /*
430 * sector_div stores the quotient in
431 * cylinder_times_heads
432 */
433 rem = sector_div(cylinder_times_heads,
434 sectors_per_track);
435 }
436
437 if (cylinder_times_heads >= (heads * 1024)) {
438 sectors_per_track = 63;
439 heads = 16;
440
441 cylinder_times_heads = total_sectors;
442 /*
443 * sector_div stores the quotient in
444 * cylinder_times_heads
445 */
446 rem = sector_div(cylinder_times_heads,
447 sectors_per_track);
448 }
449 }
450
451 temp = cylinder_times_heads;
452 /* sector_div stores the quotient in temp */
453 rem = sector_div(temp, heads);
454 cylinders = temp;
455
456 info[0] = heads;
457 info[1] = sectors_per_track;
458 info[2] = cylinders;
459
460 DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads,
461 sectors_per_track);
462
463 return 0;
464}
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -0700465
466static int storvsc_host_reset(struct hv_device *device)
467{
468 struct storvsc_device *stor_device;
469 struct hv_storvsc_request *request;
470 struct vstor_packet *vstor_packet;
471 int ret, t;
472
473 DPRINT_INFO(STORVSC, "resetting host adapter...");
474
475 stor_device = get_stor_device(device);
476 if (!stor_device)
477 return -1;
478
479 request = &stor_device->reset_request;
480 vstor_packet = &request->vstor_packet;
481
482 init_completion(&request->wait_event);
483
484 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
485 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
486 vstor_packet->vm_srb.path_id = stor_device->path_id;
487
488 ret = vmbus_sendpacket(device->channel, vstor_packet,
489 sizeof(struct vstor_packet),
490 (unsigned long)&stor_device->reset_request,
491 VM_PKT_DATA_INBAND,
492 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
493 if (ret != 0)
494 goto cleanup;
495
496 t = wait_for_completion_timeout(&request->wait_event, HZ);
497 if (t == 0) {
498 ret = -ETIMEDOUT;
499 goto cleanup;
500 }
501
502 DPRINT_INFO(STORVSC, "host adapter reset completed");
503
504 /*
505 * At this point, all outstanding requests in the adapter
506 * should have been flushed out and return to us
507 */
508
509cleanup:
510 put_stor_device(device);
511 return ret;
512}
513
514
K. Y. Srinivasan96e690be2011-05-10 07:54:38 -0700515/*
516 * storvsc_host_reset_handler - Reset the scsi HBA
517 */
518static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
519{
520 int ret;
521 struct hv_host_device *host_dev =
522 (struct hv_host_device *)scmnd->device->host->hostdata;
523 struct hv_device *dev = host_dev->dev;
524
525 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...",
526 scmnd->device, dev);
527
528 /* Invokes the vsc to reset the host/bus */
529 ret = storvsc_host_reset(dev);
530 if (ret != 0)
531 return ret;
532
533 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted",
534 scmnd->device, dev);
535
536 return ret;
537}
538
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700539
540/*
541 * storvsc_commmand_completion - Command completion processing
542 */
543static void storvsc_commmand_completion(struct hv_storvsc_request *request)
544{
545 struct storvsc_cmd_request *cmd_request =
546 (struct storvsc_cmd_request *)request->context;
547 struct scsi_cmnd *scmnd = cmd_request->cmd;
548 struct hv_host_device *host_dev =
549 (struct hv_host_device *)scmnd->device->host->hostdata;
550 void (*scsi_done_fn)(struct scsi_cmnd *);
551 struct scsi_sense_hdr sense_hdr;
552 struct vmscsi_request *vm_srb;
553
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700554 if (cmd_request->bounce_sgl_count) {
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700555
556 /* FIXME: We can optimize on writes by just skipping this */
557 copy_from_bounce_buffer(scsi_sglist(scmnd),
558 cmd_request->bounce_sgl,
559 scsi_sg_count(scmnd));
560 destroy_bounce_buffer(cmd_request->bounce_sgl,
561 cmd_request->bounce_sgl_count);
562 }
563
564 vm_srb = &request->vstor_packet.vm_srb;
565 scmnd->result = vm_srb->scsi_status;
566
567 if (scmnd->result) {
568 if (scsi_normalize_sense(scmnd->sense_buffer,
569 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
570 scsi_print_sense_hdr("storvsc", &sense_hdr);
571 }
572
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700573 scsi_set_resid(scmnd,
574 request->data_buffer.len -
575 vm_srb->data_transfer_length);
576
577 scsi_done_fn = scmnd->scsi_done;
578
579 scmnd->host_scribble = NULL;
580 scmnd->scsi_done = NULL;
581
582 /* !!DO NOT MODIFY the scmnd after this call */
583 scsi_done_fn(scmnd);
584
585 kmem_cache_free(host_dev->request_pool, cmd_request);
586}
587
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700588
589/*
590 * storvsc_queuecommand - Initiate command processing
591 */
592static int storvsc_queuecommand_lck(struct scsi_cmnd *scmnd,
593 void (*done)(struct scsi_cmnd *))
594{
595 int ret;
596 struct hv_host_device *host_dev =
597 (struct hv_host_device *)scmnd->device->host->hostdata;
598 struct hv_device *dev = host_dev->dev;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700599 struct hv_storvsc_request *request;
600 struct storvsc_cmd_request *cmd_request;
601 unsigned int request_size = 0;
602 int i;
603 struct scatterlist *sgl;
604 unsigned int sg_count = 0;
605 struct vmscsi_request *vm_srb;
606
607
608 /* If retrying, no need to prep the cmd */
609 if (scmnd->host_scribble) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700610
611 cmd_request =
612 (struct storvsc_cmd_request *)scmnd->host_scribble;
613 DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p",
614 scmnd, cmd_request);
615
616 goto retry_request;
617 }
618
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700619 scmnd->scsi_done = done;
620
621 request_size = sizeof(struct storvsc_cmd_request);
622
623 cmd_request = kmem_cache_zalloc(host_dev->request_pool,
624 GFP_ATOMIC);
625 if (!cmd_request) {
626 scmnd->scsi_done = NULL;
627 return SCSI_MLQUEUE_DEVICE_BUSY;
628 }
629
630 /* Setup the cmd request */
631 cmd_request->bounce_sgl_count = 0;
632 cmd_request->bounce_sgl = NULL;
633 cmd_request->cmd = scmnd;
634
635 scmnd->host_scribble = (unsigned char *)cmd_request;
636
637 request = &cmd_request->request;
638 vm_srb = &request->vstor_packet.vm_srb;
639
640
641 /* Build the SRB */
642 switch (scmnd->sc_data_direction) {
643 case DMA_TO_DEVICE:
644 vm_srb->data_in = WRITE_TYPE;
645 break;
646 case DMA_FROM_DEVICE:
647 vm_srb->data_in = READ_TYPE;
648 break;
649 default:
650 vm_srb->data_in = UNKNOWN_TYPE;
651 break;
652 }
653
654 request->on_io_completion = storvsc_commmand_completion;
655 request->context = cmd_request;/* scmnd; */
656
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700657 vm_srb->port_number = host_dev->port;
658 vm_srb->path_id = scmnd->device->channel;
659 vm_srb->target_id = scmnd->device->id;
660 vm_srb->lun = scmnd->device->lun;
661
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700662 vm_srb->cdb_length = scmnd->cmd_len;
663
664 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
665
666 request->sense_buffer = scmnd->sense_buffer;
667
668
669 request->data_buffer.len = scsi_bufflen(scmnd);
670 if (scsi_sg_count(scmnd)) {
671 sgl = (struct scatterlist *)scsi_sglist(scmnd);
672 sg_count = scsi_sg_count(scmnd);
673
674 /* check if we need to bounce the sgl */
675 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
676 cmd_request->bounce_sgl =
677 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
678 scsi_bufflen(scmnd));
679 if (!cmd_request->bounce_sgl) {
680 scmnd->scsi_done = NULL;
681 scmnd->host_scribble = NULL;
682 kmem_cache_free(host_dev->request_pool,
683 cmd_request);
684
685 return SCSI_MLQUEUE_HOST_BUSY;
686 }
687
688 cmd_request->bounce_sgl_count =
689 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
690 PAGE_SHIFT;
691
692 /*
693 * FIXME: We can optimize on reads by just skipping
694 * this
695 */
696 copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl,
697 scsi_sg_count(scmnd));
698
699 sgl = cmd_request->bounce_sgl;
700 sg_count = cmd_request->bounce_sgl_count;
701 }
702
703 request->data_buffer.offset = sgl[0].offset;
704
705 for (i = 0; i < sg_count; i++)
706 request->data_buffer.pfn_array[i] =
707 page_to_pfn(sg_page((&sgl[i])));
708
709 } else if (scsi_sglist(scmnd)) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700710 request->data_buffer.offset =
711 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
712 request->data_buffer.pfn_array[0] =
713 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
714 }
715
716retry_request:
717 /* Invokes the vsc to start an IO */
K. Y. Srinivasan636f0fd2011-05-10 07:54:50 -0700718 ret = storvsc_do_io(dev, &cmd_request->request);
719
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700720 if (ret == -1) {
721 /* no more space */
722
723 if (cmd_request->bounce_sgl_count) {
724 /*
725 * FIXME: We can optimize on writes by just skipping
726 * this
727 */
728 copy_from_bounce_buffer(scsi_sglist(scmnd),
729 cmd_request->bounce_sgl,
730 scsi_sg_count(scmnd));
731 destroy_bounce_buffer(cmd_request->bounce_sgl,
732 cmd_request->bounce_sgl_count);
733 }
734
735 kmem_cache_free(host_dev->request_pool, cmd_request);
736
737 scmnd->scsi_done = NULL;
738 scmnd->host_scribble = NULL;
739
740 ret = SCSI_MLQUEUE_DEVICE_BUSY;
741 }
742
743 return ret;
744}
745
746static DEF_SCSI_QCMD(storvsc_queuecommand)
747
Hank Janssenbef4a342009-07-13 16:01:31 -0700748
Bill Pemberton454f18a2009-07-27 16:47:24 -0400749/* Scsi driver */
Hank Janssenbef4a342009-07-13 16:01:31 -0700750static struct scsi_host_template scsi_driver = {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700751 .module = THIS_MODULE,
752 .name = "storvsc_host_t",
753 .bios_param = storvsc_get_chs,
754 .queuecommand = storvsc_queuecommand,
755 .eh_host_reset_handler = storvsc_host_reset_handler,
756 .slave_alloc = storvsc_device_alloc,
757 .slave_configure = storvsc_device_configure,
758 .cmd_per_lun = 1,
759 /* 64 max_queue * 1 target */
Lars Lindley0686e4f2010-03-11 23:51:23 +0100760 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700761 .this_id = -1,
Bill Pemberton454f18a2009-07-27 16:47:24 -0400762 /* no use setting to 0 since ll_blk_rw reset it to 1 */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700763 /* currently 32 */
764 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
765 /*
766 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
767 * into 1 sg element. If set, we must limit the max_segment_size to
768 * PAGE_SIZE, otherwise we may get 1 sg element that represents
769 * multiple
770 */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400771 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700772 .use_clustering = ENABLE_CLUSTERING,
Bill Pemberton454f18a2009-07-27 16:47:24 -0400773 /* Make sure we dont get a sg segment crosses a page boundary */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700774 .dma_boundary = PAGE_SIZE-1,
Hank Janssenbef4a342009-07-13 16:01:31 -0700775};
776
777
Hank Janssen3e189512010-03-04 22:11:00 +0000778/*
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700779 * storvsc_probe - Add a new device for this driver
780 */
781
782static int storvsc_probe(struct hv_device *device)
783{
784 int ret;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700785 struct Scsi_Host *host;
786 struct hv_host_device *host_dev;
787 struct storvsc_device_info device_info;
788
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700789 host = scsi_host_alloc(&scsi_driver,
790 sizeof(struct hv_host_device));
791 if (!host)
792 return -ENOMEM;
793
794 dev_set_drvdata(&device->device, host);
795
796 host_dev = (struct hv_host_device *)host->hostdata;
797 memset(host_dev, 0, sizeof(struct hv_host_device));
798
799 host_dev->port = host->host_no;
800 host_dev->dev = device;
801
802 host_dev->request_pool =
803 kmem_cache_create(dev_name(&device->device),
804 sizeof(struct storvsc_cmd_request), 0,
805 SLAB_HWCACHE_ALIGN, NULL);
806
807 if (!host_dev->request_pool) {
808 scsi_host_put(host);
809 return -ENOMEM;
810 }
811
812 device_info.port_number = host->host_no;
813 /* Call to the vsc driver to add the device */
K. Y. Srinivasan58f1f5c2011-05-10 07:54:49 -0700814 ret = storvsc_dev_add(device, (void *)&device_info);
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700815
816 if (ret != 0) {
817 kmem_cache_destroy(host_dev->request_pool);
818 scsi_host_put(host);
819 return -1;
820 }
821
822 host_dev->path = device_info.path_id;
823 host_dev->target = device_info.target_id;
824
825 /* max # of devices per target */
826 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
827 /* max # of targets per channel */
828 host->max_id = STORVSC_MAX_TARGETS;
829 /* max # of channels */
830 host->max_channel = STORVSC_MAX_CHANNELS - 1;
831
832 /* Register the HBA and start the scsi bus scan */
833 ret = scsi_add_host(host, &device->device);
834 if (ret != 0) {
835
K. Y. Srinivasan6cdc57c2011-05-10 07:54:48 -0700836 storvsc_dev_remove(device);
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700837
838 kmem_cache_destroy(host_dev->request_pool);
839 scsi_host_put(host);
840 return -1;
841 }
842
843 scsi_scan_host(host);
844 return ret;
845}
846
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -0700847/* The one and only one */
848
K. Y. Srinivasan39ae6fa2011-05-10 07:54:46 -0700849static struct storvsc_driver storvsc_drv = {
850 .base.probe = storvsc_probe,
851 .base.remove = storvsc_remove,
852};
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -0700853
854
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700855/*
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700856 * storvsc_drv_init - StorVsc driver initialization.
857 */
K. Y. Srinivasandb085772011-03-23 10:50:27 -0700858static int storvsc_drv_init(void)
Hank Janssenbef4a342009-07-13 16:01:31 -0700859{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700860 int ret;
K. Y. Srinivasan565c38d2011-05-10 07:54:44 -0700861 struct storvsc_driver *storvsc_drv_obj = &storvsc_drv;
862 struct hv_driver *drv = &storvsc_drv.base;
Hank Janssenbef4a342009-07-13 16:01:31 -0700863
Hank Janssen8a046022010-12-06 12:26:45 -0800864 storvsc_drv_obj->ring_buffer_size = storvsc_ringbuffer_size;
Hank Janssenbef4a342009-07-13 16:01:31 -0700865
Bill Pemberton454f18a2009-07-27 16:47:24 -0400866 /* Callback to client driver to complete the initialization */
K. Y. Srinivasancf6fdfb2011-05-10 07:54:36 -0700867 storvsc_initialize(&storvsc_drv_obj->base);
Hank Janssenbef4a342009-07-13 16:01:31 -0700868
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700869 DPRINT_INFO(STORVSC_DRV,
K. Y. Srinivasan1e05d882011-03-28 09:33:28 -0700870 "max outstanding reqs %u",
Hank Janssen8a046022010-12-06 12:26:45 -0800871 storvsc_drv_obj->max_outstanding_req_per_channel);
Hank Janssenbef4a342009-07-13 16:01:31 -0700872
Hank Janssen8a046022010-12-06 12:26:45 -0800873 if (storvsc_drv_obj->max_outstanding_req_per_channel <
K. Y. Srinivasanf8feed02011-05-10 07:54:24 -0700874 STORVSC_MAX_IO_REQUESTS)
Hank Janssenbef4a342009-07-13 16:01:31 -0700875 return -1;
Hank Janssenbef4a342009-07-13 16:01:31 -0700876
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800877 drv->driver.name = storvsc_drv_obj->base.name;
Hank Janssenbef4a342009-07-13 16:01:31 -0700878
Hank Janssenbef4a342009-07-13 16:01:31 -0700879
Bill Pemberton454f18a2009-07-27 16:47:24 -0400880 /* The driver belongs to vmbus */
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800881 ret = vmbus_child_driver_register(&drv->driver);
Hank Janssenbef4a342009-07-13 16:01:31 -0700882
Hank Janssenbef4a342009-07-13 16:01:31 -0700883 return ret;
884}
885
Hank Janssenbef4a342009-07-13 16:01:31 -0700886static int storvsc_drv_exit_cb(struct device *dev, void *data)
887{
888 struct device **curr = (struct device **)data;
889 *curr = dev;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400890 return 1; /* stop iterating */
Hank Janssenbef4a342009-07-13 16:01:31 -0700891}
892
Greg Kroah-Hartmanbd1de702009-07-29 09:04:51 -0700893static void storvsc_drv_exit(void)
Hank Janssenbef4a342009-07-13 16:01:31 -0700894{
K. Y. Srinivasan565c38d2011-05-10 07:54:44 -0700895 struct storvsc_driver *storvsc_drv_obj = &storvsc_drv;
896 struct hv_driver *drv = &storvsc_drv.base;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700897 struct device *current_dev = NULL;
Bill Pemberton2295ba22009-07-28 13:46:22 -0400898 int ret;
Hank Janssenbef4a342009-07-13 16:01:31 -0700899
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700900 while (1) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700901 current_dev = NULL;
902
Bill Pemberton454f18a2009-07-27 16:47:24 -0400903 /* Get the device */
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800904 ret = driver_for_each_device(&drv->driver, NULL,
Bill Pemberton2295ba22009-07-28 13:46:22 -0400905 (void *) &current_dev,
906 storvsc_drv_exit_cb);
907
Hank Janssenbef4a342009-07-13 16:01:31 -0700908
909 if (current_dev == NULL)
910 break;
911
Bill Pemberton454f18a2009-07-27 16:47:24 -0400912 /* Initiate removal from the top-down */
Hank Janssenbef4a342009-07-13 16:01:31 -0700913 device_unregister(current_dev);
914 }
915
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800916 if (storvsc_drv_obj->base.cleanup)
917 storvsc_drv_obj->base.cleanup(&storvsc_drv_obj->base);
Hank Janssenbef4a342009-07-13 16:01:31 -0700918
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800919 vmbus_child_driver_unregister(&drv->driver);
Hank Janssenbef4a342009-07-13 16:01:31 -0700920 return;
921}
922
Hank Janssenbef4a342009-07-13 16:01:31 -0700923static int __init storvsc_init(void)
924{
925 int ret;
926
Hank Janssenbef4a342009-07-13 16:01:31 -0700927 DPRINT_INFO(STORVSC_DRV, "Storvsc initializing....");
K. Y. Srinivasandb085772011-03-23 10:50:27 -0700928 ret = storvsc_drv_init();
Hank Janssenbef4a342009-07-13 16:01:31 -0700929 return ret;
930}
931
932static void __exit storvsc_exit(void)
933{
Hank Janssenbef4a342009-07-13 16:01:31 -0700934 storvsc_drv_exit();
Hank Janssenbef4a342009-07-13 16:01:31 -0700935}
936
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700937MODULE_LICENSE("GPL");
Hank Janssen26c14cc2010-02-11 23:02:42 +0000938MODULE_VERSION(HV_DRV_VERSION);
Stephen Hemminger3afc7cc32010-05-06 21:44:45 -0700939MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
Hank Janssenbef4a342009-07-13 16:01:31 -0700940module_init(storvsc_init);
941module_exit(storvsc_exit);