blob: c0633e9eceac637392e8cd9918b6746de7af169d [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. Srinivasan3f335ea2011-05-12 19:34:15 -070035
36#include "hyperv.h"
K. Y. Srinivasane3fe0bb2011-02-11 10:00:12 -080037#include "hv_api.h"
Greg Kroah-Hartman645954c2009-08-28 16:22:59 -070038#include "logging.h"
Greg Kroah-Hartman2d82f6c2010-05-05 22:52:28 -070039#include "version_info.h"
Greg Kroah-Hartman870cde82009-08-19 16:21:28 -070040#include "vmbus.h"
Greg Kroah-Hartmanbb969792010-05-05 22:40:43 -070041#include "storvsc_api.h"
K. Y. Srinivasanaf3043c2011-03-23 10:50:33 -070042#include "vstorage.h"
43#include "channel.h"
Hank Janssenbef4a342009-07-13 16:01:31 -070044
K. Y. Srinivasan2db2cab2011-05-10 07:54:39 -070045static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
Hank Janssenbef4a342009-07-13 16:01:31 -070046
K. Y. Srinivasan3d598ce2011-05-10 07:54:40 -070047module_param(storvsc_ringbuffer_size, int, S_IRUGO);
48MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
49
K. Y. Srinivasan0f0cdc62011-05-10 07:54:18 -070050static const char *driver_name = "storvsc";
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070051
52/* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
53static const struct hv_guid gStorVscDeviceType = {
54 .data = {
55 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
56 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f
57 }
58};
59
K. Y. Srinivasan972621c2011-05-10 07:54:19 -070060struct hv_host_device {
K. Y. Srinivasan97c15292011-05-10 07:54:21 -070061 struct hv_device *dev;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070062 struct kmem_cache *request_pool;
63 unsigned int port;
64 unsigned char path;
65 unsigned char target;
Hank Janssenbef4a342009-07-13 16:01:31 -070066};
67
68struct storvsc_cmd_request {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070069 struct list_head entry;
70 struct scsi_cmnd *cmd;
Hank Janssenbef4a342009-07-13 16:01:31 -070071
72 unsigned int bounce_sgl_count;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070073 struct scatterlist *bounce_sgl;
Hank Janssenbef4a342009-07-13 16:01:31 -070074
Nicolas Palix0b3f6832009-07-29 14:10:19 +020075 struct hv_storvsc_request request;
Hank Janssenbef4a342009-07-13 16:01:31 -070076};
77
Hank Janssenbef4a342009-07-13 16:01:31 -070078
K. Y. Srinivasan5b60ace2011-05-10 07:54:25 -070079static int storvsc_device_alloc(struct scsi_device *sdevice)
80{
81 /*
82 * This enables luns to be located sparsely. Otherwise, we may not
83 * discovered them.
84 */
85 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
86 return 0;
87}
88
K. Y. Srinivasana1ebfea2011-05-10 07:54:26 -070089static int storvsc_merge_bvec(struct request_queue *q,
90 struct bvec_merge_data *bmd, struct bio_vec *bvec)
91{
92 /* checking done by caller. */
93 return bvec->bv_len;
94}
95
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -070096static int storvsc_device_configure(struct scsi_device *sdevice)
97{
98 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
99 STORVSC_MAX_IO_REQUESTS);
100
101 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting max segment size to %ld",
102 sdevice, PAGE_SIZE);
103 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
104
105 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine",
106 sdevice);
107 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
108
109 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700110
111 return 0;
112}
113
K. Y. Srinivasan49a3c7a2011-05-10 07:54:28 -0700114static void destroy_bounce_buffer(struct scatterlist *sgl,
115 unsigned int sg_count)
116{
117 int i;
118 struct page *page_buf;
119
120 for (i = 0; i < sg_count; i++) {
121 page_buf = sg_page((&sgl[i]));
122 if (page_buf != NULL)
123 __free_page(page_buf);
124 }
125
126 kfree(sgl);
127}
128
K. Y. Srinivasan3862ef32011-05-10 07:54:29 -0700129static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
130{
131 int i;
132
133 /* No need to check */
134 if (sg_count < 2)
135 return -1;
136
137 /* We have at least 2 sg entries */
138 for (i = 0; i < sg_count; i++) {
139 if (i == 0) {
140 /* make sure 1st one does not have hole */
141 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
142 return i;
143 } else if (i == sg_count - 1) {
144 /* make sure last one does not have hole */
145 if (sgl[i].offset != 0)
146 return i;
147 } else {
148 /* make sure no hole in the middle */
149 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
150 return i;
151 }
152 }
153 return -1;
154}
155
K. Y. Srinivasana9753cb2011-05-10 07:54:30 -0700156static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
157 unsigned int sg_count,
158 unsigned int len)
159{
160 int i;
161 int num_pages;
162 struct scatterlist *bounce_sgl;
163 struct page *page_buf;
164
165 num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
166
167 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
168 if (!bounce_sgl)
169 return NULL;
170
171 for (i = 0; i < num_pages; i++) {
172 page_buf = alloc_page(GFP_ATOMIC);
173 if (!page_buf)
174 goto cleanup;
175 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
176 }
177
178 return bounce_sgl;
179
180cleanup:
181 destroy_bounce_buffer(bounce_sgl, num_pages);
182 return NULL;
183}
184
K. Y. Srinivasan29fe2c92011-05-10 07:54:31 -0700185
186/* Assume the original sgl has enough room */
187static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
188 struct scatterlist *bounce_sgl,
189 unsigned int orig_sgl_count)
190{
191 int i;
192 int j = 0;
193 unsigned long src, dest;
194 unsigned int srclen, destlen, copylen;
195 unsigned int total_copied = 0;
196 unsigned long bounce_addr = 0;
197 unsigned long dest_addr = 0;
198 unsigned long flags;
199
200 local_irq_save(flags);
201
202 for (i = 0; i < orig_sgl_count; i++) {
203 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
204 KM_IRQ0) + orig_sgl[i].offset;
205 dest = dest_addr;
206 destlen = orig_sgl[i].length;
207
208 if (bounce_addr == 0)
209 bounce_addr =
210 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
211 KM_IRQ0);
212
213 while (destlen) {
214 src = bounce_addr + bounce_sgl[j].offset;
215 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
216
217 copylen = min(srclen, destlen);
218 memcpy((void *)dest, (void *)src, copylen);
219
220 total_copied += copylen;
221 bounce_sgl[j].offset += copylen;
222 destlen -= copylen;
223 dest += copylen;
224
225 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
226 /* full */
227 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
228 j++;
229
230 /* if we need to use another bounce buffer */
231 if (destlen || i != orig_sgl_count - 1)
232 bounce_addr =
233 (unsigned long)kmap_atomic(
234 sg_page((&bounce_sgl[j])), KM_IRQ0);
235 } else if (destlen == 0 && i == orig_sgl_count - 1) {
236 /* unmap the last bounce that is < PAGE_SIZE */
237 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
238 }
239 }
240
241 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
242 KM_IRQ0);
243 }
244
245 local_irq_restore(flags);
246
247 return total_copied;
248}
249
K. Y. Srinivasan6a8ff442011-05-10 07:54:32 -0700250
251/* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
252static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
253 struct scatterlist *bounce_sgl,
254 unsigned int orig_sgl_count)
255{
256 int i;
257 int j = 0;
258 unsigned long src, dest;
259 unsigned int srclen, destlen, copylen;
260 unsigned int total_copied = 0;
261 unsigned long bounce_addr = 0;
262 unsigned long src_addr = 0;
263 unsigned long flags;
264
265 local_irq_save(flags);
266
267 for (i = 0; i < orig_sgl_count; i++) {
268 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
269 KM_IRQ0) + orig_sgl[i].offset;
270 src = src_addr;
271 srclen = orig_sgl[i].length;
272
273 if (bounce_addr == 0)
274 bounce_addr =
275 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
276 KM_IRQ0);
277
278 while (srclen) {
279 /* assume bounce offset always == 0 */
280 dest = bounce_addr + bounce_sgl[j].length;
281 destlen = PAGE_SIZE - bounce_sgl[j].length;
282
283 copylen = min(srclen, destlen);
284 memcpy((void *)dest, (void *)src, copylen);
285
286 total_copied += copylen;
287 bounce_sgl[j].length += copylen;
288 srclen -= copylen;
289 src += copylen;
290
291 if (bounce_sgl[j].length == PAGE_SIZE) {
292 /* full..move to next entry */
293 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
294 j++;
295
296 /* if we need to use another bounce buffer */
297 if (srclen || i != orig_sgl_count - 1)
298 bounce_addr =
299 (unsigned long)kmap_atomic(
300 sg_page((&bounce_sgl[j])), KM_IRQ0);
301
302 } else if (srclen == 0 && i == orig_sgl_count - 1) {
303 /* unmap the last bounce that is < PAGE_SIZE */
304 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
305 }
306 }
307
308 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
309 }
310
311 local_irq_restore(flags);
312
313 return total_copied;
314}
315
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -0700316
317/*
318 * storvsc_remove - Callback when our device is removed
319 */
320static int storvsc_remove(struct hv_device *dev)
321{
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -0700322 struct Scsi_Host *host = dev_get_drvdata(&dev->device);
323 struct hv_host_device *host_dev =
324 (struct hv_host_device *)host->hostdata;
325
326 /*
327 * Call to the vsc driver to let it know that the device is being
328 * removed
329 */
K. Y. Srinivasan6cdc57c2011-05-10 07:54:48 -0700330 storvsc_dev_remove(dev);
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -0700331
332 if (host_dev->request_pool) {
333 kmem_cache_destroy(host_dev->request_pool);
334 host_dev->request_pool = NULL;
335 }
336
337 DPRINT_INFO(STORVSC, "removing host adapter (%p)...", host);
338 scsi_remove_host(host);
339
340 DPRINT_INFO(STORVSC, "releasing host adapter (%p)...", host);
341 scsi_host_put(host);
342 return 0;
343}
344
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700345
346static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
347 sector_t capacity, int *info)
348{
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -0700349 sector_t nsect = capacity;
350 sector_t cylinders = nsect;
351 int heads, sectors_pt;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700352
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -0700353 /*
354 * We are making up these values; let us keep it simple.
355 */
356 heads = 0xff;
357 sectors_pt = 0x3f; /* Sectors per track */
358 sector_div(cylinders, heads * sectors_pt);
359 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
360 cylinders = 0xffff;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700361
362 info[0] = heads;
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -0700363 info[1] = sectors_pt;
364 info[2] = (int)cylinders;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700365
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -0700366 DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", (int)cylinders, heads,
367 sectors_pt);
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700368
369 return 0;
370}
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -0700371
372static int storvsc_host_reset(struct hv_device *device)
373{
374 struct storvsc_device *stor_device;
375 struct hv_storvsc_request *request;
376 struct vstor_packet *vstor_packet;
377 int ret, t;
378
379 DPRINT_INFO(STORVSC, "resetting host adapter...");
380
381 stor_device = get_stor_device(device);
382 if (!stor_device)
383 return -1;
384
385 request = &stor_device->reset_request;
386 vstor_packet = &request->vstor_packet;
387
388 init_completion(&request->wait_event);
389
390 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
391 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
392 vstor_packet->vm_srb.path_id = stor_device->path_id;
393
394 ret = vmbus_sendpacket(device->channel, vstor_packet,
395 sizeof(struct vstor_packet),
396 (unsigned long)&stor_device->reset_request,
397 VM_PKT_DATA_INBAND,
398 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
399 if (ret != 0)
400 goto cleanup;
401
402 t = wait_for_completion_timeout(&request->wait_event, HZ);
403 if (t == 0) {
404 ret = -ETIMEDOUT;
405 goto cleanup;
406 }
407
408 DPRINT_INFO(STORVSC, "host adapter reset completed");
409
410 /*
411 * At this point, all outstanding requests in the adapter
412 * should have been flushed out and return to us
413 */
414
415cleanup:
416 put_stor_device(device);
417 return ret;
418}
419
420
K. Y. Srinivasan96e690be2011-05-10 07:54:38 -0700421/*
422 * storvsc_host_reset_handler - Reset the scsi HBA
423 */
424static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
425{
426 int ret;
427 struct hv_host_device *host_dev =
428 (struct hv_host_device *)scmnd->device->host->hostdata;
429 struct hv_device *dev = host_dev->dev;
430
431 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...",
432 scmnd->device, dev);
433
434 /* Invokes the vsc to reset the host/bus */
435 ret = storvsc_host_reset(dev);
436 if (ret != 0)
437 return ret;
438
439 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted",
440 scmnd->device, dev);
441
442 return ret;
443}
444
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700445
446/*
447 * storvsc_commmand_completion - Command completion processing
448 */
449static void storvsc_commmand_completion(struct hv_storvsc_request *request)
450{
451 struct storvsc_cmd_request *cmd_request =
452 (struct storvsc_cmd_request *)request->context;
453 struct scsi_cmnd *scmnd = cmd_request->cmd;
454 struct hv_host_device *host_dev =
455 (struct hv_host_device *)scmnd->device->host->hostdata;
456 void (*scsi_done_fn)(struct scsi_cmnd *);
457 struct scsi_sense_hdr sense_hdr;
458 struct vmscsi_request *vm_srb;
459
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700460 if (cmd_request->bounce_sgl_count) {
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700461
462 /* FIXME: We can optimize on writes by just skipping this */
463 copy_from_bounce_buffer(scsi_sglist(scmnd),
464 cmd_request->bounce_sgl,
465 scsi_sg_count(scmnd));
466 destroy_bounce_buffer(cmd_request->bounce_sgl,
467 cmd_request->bounce_sgl_count);
468 }
469
470 vm_srb = &request->vstor_packet.vm_srb;
471 scmnd->result = vm_srb->scsi_status;
472
473 if (scmnd->result) {
474 if (scsi_normalize_sense(scmnd->sense_buffer,
475 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
476 scsi_print_sense_hdr("storvsc", &sense_hdr);
477 }
478
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700479 scsi_set_resid(scmnd,
480 request->data_buffer.len -
481 vm_srb->data_transfer_length);
482
483 scsi_done_fn = scmnd->scsi_done;
484
485 scmnd->host_scribble = NULL;
486 scmnd->scsi_done = NULL;
487
488 /* !!DO NOT MODIFY the scmnd after this call */
489 scsi_done_fn(scmnd);
490
491 kmem_cache_free(host_dev->request_pool, cmd_request);
492}
493
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700494
495/*
496 * storvsc_queuecommand - Initiate command processing
497 */
498static int storvsc_queuecommand_lck(struct scsi_cmnd *scmnd,
499 void (*done)(struct scsi_cmnd *))
500{
501 int ret;
502 struct hv_host_device *host_dev =
503 (struct hv_host_device *)scmnd->device->host->hostdata;
504 struct hv_device *dev = host_dev->dev;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700505 struct hv_storvsc_request *request;
506 struct storvsc_cmd_request *cmd_request;
507 unsigned int request_size = 0;
508 int i;
509 struct scatterlist *sgl;
510 unsigned int sg_count = 0;
511 struct vmscsi_request *vm_srb;
512
513
514 /* If retrying, no need to prep the cmd */
515 if (scmnd->host_scribble) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700516
517 cmd_request =
518 (struct storvsc_cmd_request *)scmnd->host_scribble;
519 DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p",
520 scmnd, cmd_request);
521
522 goto retry_request;
523 }
524
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700525 scmnd->scsi_done = done;
526
527 request_size = sizeof(struct storvsc_cmd_request);
528
529 cmd_request = kmem_cache_zalloc(host_dev->request_pool,
530 GFP_ATOMIC);
531 if (!cmd_request) {
532 scmnd->scsi_done = NULL;
533 return SCSI_MLQUEUE_DEVICE_BUSY;
534 }
535
536 /* Setup the cmd request */
537 cmd_request->bounce_sgl_count = 0;
538 cmd_request->bounce_sgl = NULL;
539 cmd_request->cmd = scmnd;
540
541 scmnd->host_scribble = (unsigned char *)cmd_request;
542
543 request = &cmd_request->request;
544 vm_srb = &request->vstor_packet.vm_srb;
545
546
547 /* Build the SRB */
548 switch (scmnd->sc_data_direction) {
549 case DMA_TO_DEVICE:
550 vm_srb->data_in = WRITE_TYPE;
551 break;
552 case DMA_FROM_DEVICE:
553 vm_srb->data_in = READ_TYPE;
554 break;
555 default:
556 vm_srb->data_in = UNKNOWN_TYPE;
557 break;
558 }
559
560 request->on_io_completion = storvsc_commmand_completion;
561 request->context = cmd_request;/* scmnd; */
562
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700563 vm_srb->port_number = host_dev->port;
564 vm_srb->path_id = scmnd->device->channel;
565 vm_srb->target_id = scmnd->device->id;
566 vm_srb->lun = scmnd->device->lun;
567
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700568 vm_srb->cdb_length = scmnd->cmd_len;
569
570 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
571
572 request->sense_buffer = scmnd->sense_buffer;
573
574
575 request->data_buffer.len = scsi_bufflen(scmnd);
576 if (scsi_sg_count(scmnd)) {
577 sgl = (struct scatterlist *)scsi_sglist(scmnd);
578 sg_count = scsi_sg_count(scmnd);
579
580 /* check if we need to bounce the sgl */
581 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
582 cmd_request->bounce_sgl =
583 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
584 scsi_bufflen(scmnd));
585 if (!cmd_request->bounce_sgl) {
586 scmnd->scsi_done = NULL;
587 scmnd->host_scribble = NULL;
588 kmem_cache_free(host_dev->request_pool,
589 cmd_request);
590
591 return SCSI_MLQUEUE_HOST_BUSY;
592 }
593
594 cmd_request->bounce_sgl_count =
595 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
596 PAGE_SHIFT;
597
598 /*
599 * FIXME: We can optimize on reads by just skipping
600 * this
601 */
602 copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl,
603 scsi_sg_count(scmnd));
604
605 sgl = cmd_request->bounce_sgl;
606 sg_count = cmd_request->bounce_sgl_count;
607 }
608
609 request->data_buffer.offset = sgl[0].offset;
610
611 for (i = 0; i < sg_count; i++)
612 request->data_buffer.pfn_array[i] =
613 page_to_pfn(sg_page((&sgl[i])));
614
615 } else if (scsi_sglist(scmnd)) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700616 request->data_buffer.offset =
617 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
618 request->data_buffer.pfn_array[0] =
619 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
620 }
621
622retry_request:
623 /* Invokes the vsc to start an IO */
K. Y. Srinivasan636f0fd2011-05-10 07:54:50 -0700624 ret = storvsc_do_io(dev, &cmd_request->request);
625
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700626 if (ret == -1) {
627 /* no more space */
628
629 if (cmd_request->bounce_sgl_count) {
630 /*
631 * FIXME: We can optimize on writes by just skipping
632 * this
633 */
634 copy_from_bounce_buffer(scsi_sglist(scmnd),
635 cmd_request->bounce_sgl,
636 scsi_sg_count(scmnd));
637 destroy_bounce_buffer(cmd_request->bounce_sgl,
638 cmd_request->bounce_sgl_count);
639 }
640
641 kmem_cache_free(host_dev->request_pool, cmd_request);
642
643 scmnd->scsi_done = NULL;
644 scmnd->host_scribble = NULL;
645
646 ret = SCSI_MLQUEUE_DEVICE_BUSY;
647 }
648
649 return ret;
650}
651
652static DEF_SCSI_QCMD(storvsc_queuecommand)
653
Hank Janssenbef4a342009-07-13 16:01:31 -0700654
Bill Pemberton454f18a2009-07-27 16:47:24 -0400655/* Scsi driver */
Hank Janssenbef4a342009-07-13 16:01:31 -0700656static struct scsi_host_template scsi_driver = {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700657 .module = THIS_MODULE,
658 .name = "storvsc_host_t",
659 .bios_param = storvsc_get_chs,
660 .queuecommand = storvsc_queuecommand,
661 .eh_host_reset_handler = storvsc_host_reset_handler,
662 .slave_alloc = storvsc_device_alloc,
663 .slave_configure = storvsc_device_configure,
664 .cmd_per_lun = 1,
665 /* 64 max_queue * 1 target */
Lars Lindley0686e4f2010-03-11 23:51:23 +0100666 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700667 .this_id = -1,
Bill Pemberton454f18a2009-07-27 16:47:24 -0400668 /* no use setting to 0 since ll_blk_rw reset it to 1 */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700669 /* currently 32 */
670 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
671 /*
672 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
673 * into 1 sg element. If set, we must limit the max_segment_size to
674 * PAGE_SIZE, otherwise we may get 1 sg element that represents
675 * multiple
676 */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400677 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700678 .use_clustering = ENABLE_CLUSTERING,
Bill Pemberton454f18a2009-07-27 16:47:24 -0400679 /* Make sure we dont get a sg segment crosses a page boundary */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700680 .dma_boundary = PAGE_SIZE-1,
Hank Janssenbef4a342009-07-13 16:01:31 -0700681};
682
683
Hank Janssen3e189512010-03-04 22:11:00 +0000684/*
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700685 * storvsc_probe - Add a new device for this driver
686 */
687
688static int storvsc_probe(struct hv_device *device)
689{
690 int ret;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700691 struct Scsi_Host *host;
692 struct hv_host_device *host_dev;
693 struct storvsc_device_info device_info;
694
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700695 host = scsi_host_alloc(&scsi_driver,
696 sizeof(struct hv_host_device));
697 if (!host)
698 return -ENOMEM;
699
700 dev_set_drvdata(&device->device, host);
701
702 host_dev = (struct hv_host_device *)host->hostdata;
703 memset(host_dev, 0, sizeof(struct hv_host_device));
704
705 host_dev->port = host->host_no;
706 host_dev->dev = device;
707
708 host_dev->request_pool =
709 kmem_cache_create(dev_name(&device->device),
710 sizeof(struct storvsc_cmd_request), 0,
711 SLAB_HWCACHE_ALIGN, NULL);
712
713 if (!host_dev->request_pool) {
714 scsi_host_put(host);
715 return -ENOMEM;
716 }
717
718 device_info.port_number = host->host_no;
K. Y. Srinivasanfa4d1232011-05-10 07:56:01 -0700719 device_info.ring_buffer_size = storvsc_ringbuffer_size;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700720 /* Call to the vsc driver to add the device */
K. Y. Srinivasan58f1f5c2011-05-10 07:54:49 -0700721 ret = storvsc_dev_add(device, (void *)&device_info);
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700722
723 if (ret != 0) {
724 kmem_cache_destroy(host_dev->request_pool);
725 scsi_host_put(host);
726 return -1;
727 }
728
729 host_dev->path = device_info.path_id;
730 host_dev->target = device_info.target_id;
731
732 /* max # of devices per target */
733 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
734 /* max # of targets per channel */
735 host->max_id = STORVSC_MAX_TARGETS;
736 /* max # of channels */
737 host->max_channel = STORVSC_MAX_CHANNELS - 1;
738
739 /* Register the HBA and start the scsi bus scan */
740 ret = scsi_add_host(host, &device->device);
741 if (ret != 0) {
742
K. Y. Srinivasan6cdc57c2011-05-10 07:54:48 -0700743 storvsc_dev_remove(device);
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700744
745 kmem_cache_destroy(host_dev->request_pool);
746 scsi_host_put(host);
747 return -1;
748 }
749
750 scsi_scan_host(host);
751 return ret;
752}
753
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -0700754/* The one and only one */
755
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -0700756static struct hv_driver storvsc_drv = {
757 .probe = storvsc_probe,
758 .remove = storvsc_remove,
K. Y. Srinivasan39ae6fa2011-05-10 07:54:46 -0700759};
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -0700760
761
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700762/*
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700763 * storvsc_drv_init - StorVsc driver initialization.
764 */
K. Y. Srinivasandb085772011-03-23 10:50:27 -0700765static int storvsc_drv_init(void)
Hank Janssenbef4a342009-07-13 16:01:31 -0700766{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700767 int ret;
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -0700768 struct hv_driver *drv = &storvsc_drv;
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -0700769 u32 max_outstanding_req_per_channel;
770
771 /*
772 * Divide the ring buffer data size (which is 1 page less
773 * than the ring buffer size since that page is reserved for
774 * the ring buffer indices) by the max request size (which is
775 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
776 */
777
778 max_outstanding_req_per_channel =
779 ((storvsc_ringbuffer_size - PAGE_SIZE) /
780 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
781 sizeof(struct vstor_packet) + sizeof(u64),
782 sizeof(u64)));
Hank Janssenbef4a342009-07-13 16:01:31 -0700783
K. Y. Srinivasan1a96d9a2011-05-10 07:56:06 -0700784 memcpy(&drv->dev_type, &gStorVscDeviceType,
785 sizeof(struct hv_guid));
786
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -0700787 if (max_outstanding_req_per_channel <
K. Y. Srinivasanf8feed02011-05-10 07:54:24 -0700788 STORVSC_MAX_IO_REQUESTS)
Hank Janssenbef4a342009-07-13 16:01:31 -0700789 return -1;
Hank Janssenbef4a342009-07-13 16:01:31 -0700790
K. Y. Srinivasan4742e822011-05-10 07:56:05 -0700791 drv->name = driver_name;
K. Y. Srinivasan206cf172011-05-10 07:56:04 -0700792 drv->driver.name = driver_name;
Hank Janssenbef4a342009-07-13 16:01:31 -0700793
Hank Janssenbef4a342009-07-13 16:01:31 -0700794
Bill Pemberton454f18a2009-07-27 16:47:24 -0400795 /* The driver belongs to vmbus */
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800796 ret = vmbus_child_driver_register(&drv->driver);
Hank Janssenbef4a342009-07-13 16:01:31 -0700797
Hank Janssenbef4a342009-07-13 16:01:31 -0700798 return ret;
799}
800
Hank Janssenbef4a342009-07-13 16:01:31 -0700801static int storvsc_drv_exit_cb(struct device *dev, void *data)
802{
803 struct device **curr = (struct device **)data;
804 *curr = dev;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400805 return 1; /* stop iterating */
Hank Janssenbef4a342009-07-13 16:01:31 -0700806}
807
Greg Kroah-Hartmanbd1de702009-07-29 09:04:51 -0700808static void storvsc_drv_exit(void)
Hank Janssenbef4a342009-07-13 16:01:31 -0700809{
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -0700810 struct hv_driver *drv = &storvsc_drv;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700811 struct device *current_dev = NULL;
Bill Pemberton2295ba22009-07-28 13:46:22 -0400812 int ret;
Hank Janssenbef4a342009-07-13 16:01:31 -0700813
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700814 while (1) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700815 current_dev = NULL;
816
Bill Pemberton454f18a2009-07-27 16:47:24 -0400817 /* Get the device */
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800818 ret = driver_for_each_device(&drv->driver, NULL,
Bill Pemberton2295ba22009-07-28 13:46:22 -0400819 (void *) &current_dev,
820 storvsc_drv_exit_cb);
821
Hank Janssenbef4a342009-07-13 16:01:31 -0700822
823 if (current_dev == NULL)
824 break;
825
Bill Pemberton454f18a2009-07-27 16:47:24 -0400826 /* Initiate removal from the top-down */
Hank Janssenbef4a342009-07-13 16:01:31 -0700827 device_unregister(current_dev);
828 }
829
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800830 vmbus_child_driver_unregister(&drv->driver);
Hank Janssenbef4a342009-07-13 16:01:31 -0700831 return;
832}
833
Hank Janssenbef4a342009-07-13 16:01:31 -0700834static int __init storvsc_init(void)
835{
836 int ret;
837
Hank Janssenbef4a342009-07-13 16:01:31 -0700838 DPRINT_INFO(STORVSC_DRV, "Storvsc initializing....");
K. Y. Srinivasandb085772011-03-23 10:50:27 -0700839 ret = storvsc_drv_init();
Hank Janssenbef4a342009-07-13 16:01:31 -0700840 return ret;
841}
842
843static void __exit storvsc_exit(void)
844{
Hank Janssenbef4a342009-07-13 16:01:31 -0700845 storvsc_drv_exit();
Hank Janssenbef4a342009-07-13 16:01:31 -0700846}
847
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700848MODULE_LICENSE("GPL");
Hank Janssen26c14cc2010-02-11 23:02:42 +0000849MODULE_VERSION(HV_DRV_VERSION);
Stephen Hemminger3afc7cc32010-05-06 21:44:45 -0700850MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
Hank Janssenbef4a342009-07-13 16:01:31 -0700851module_init(storvsc_init);
852module_exit(storvsc_exit);