blob: 907d85568cc17ead38d81ef63130187670c1b1ff [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. Srinivasan5b60ace2011-05-10 07:54:25 -070077static int storvsc_device_alloc(struct scsi_device *sdevice)
78{
79 /*
80 * This enables luns to be located sparsely. Otherwise, we may not
81 * discovered them.
82 */
83 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
84 return 0;
85}
86
K. Y. Srinivasana1ebfea2011-05-10 07:54:26 -070087static int storvsc_merge_bvec(struct request_queue *q,
88 struct bvec_merge_data *bmd, struct bio_vec *bvec)
89{
90 /* checking done by caller. */
91 return bvec->bv_len;
92}
93
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -070094static int storvsc_device_configure(struct scsi_device *sdevice)
95{
96 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
97 STORVSC_MAX_IO_REQUESTS);
98
99 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting max segment size to %ld",
100 sdevice, PAGE_SIZE);
101 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
102
103 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine",
104 sdevice);
105 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
106
107 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700108
109 return 0;
110}
111
K. Y. Srinivasan49a3c7a2011-05-10 07:54:28 -0700112static void destroy_bounce_buffer(struct scatterlist *sgl,
113 unsigned int sg_count)
114{
115 int i;
116 struct page *page_buf;
117
118 for (i = 0; i < sg_count; i++) {
119 page_buf = sg_page((&sgl[i]));
120 if (page_buf != NULL)
121 __free_page(page_buf);
122 }
123
124 kfree(sgl);
125}
126
K. Y. Srinivasan3862ef32011-05-10 07:54:29 -0700127static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
128{
129 int i;
130
131 /* No need to check */
132 if (sg_count < 2)
133 return -1;
134
135 /* We have at least 2 sg entries */
136 for (i = 0; i < sg_count; i++) {
137 if (i == 0) {
138 /* make sure 1st one does not have hole */
139 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
140 return i;
141 } else if (i == sg_count - 1) {
142 /* make sure last one does not have hole */
143 if (sgl[i].offset != 0)
144 return i;
145 } else {
146 /* make sure no hole in the middle */
147 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
148 return i;
149 }
150 }
151 return -1;
152}
153
K. Y. Srinivasana9753cb2011-05-10 07:54:30 -0700154static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
155 unsigned int sg_count,
156 unsigned int len)
157{
158 int i;
159 int num_pages;
160 struct scatterlist *bounce_sgl;
161 struct page *page_buf;
162
163 num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
164
165 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
166 if (!bounce_sgl)
167 return NULL;
168
169 for (i = 0; i < num_pages; i++) {
170 page_buf = alloc_page(GFP_ATOMIC);
171 if (!page_buf)
172 goto cleanup;
173 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
174 }
175
176 return bounce_sgl;
177
178cleanup:
179 destroy_bounce_buffer(bounce_sgl, num_pages);
180 return NULL;
181}
182
K. Y. Srinivasan29fe2c92011-05-10 07:54:31 -0700183
184/* Assume the original sgl has enough room */
185static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
186 struct scatterlist *bounce_sgl,
187 unsigned int orig_sgl_count)
188{
189 int i;
190 int j = 0;
191 unsigned long src, dest;
192 unsigned int srclen, destlen, copylen;
193 unsigned int total_copied = 0;
194 unsigned long bounce_addr = 0;
195 unsigned long dest_addr = 0;
196 unsigned long flags;
197
198 local_irq_save(flags);
199
200 for (i = 0; i < orig_sgl_count; i++) {
201 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
202 KM_IRQ0) + orig_sgl[i].offset;
203 dest = dest_addr;
204 destlen = orig_sgl[i].length;
205
206 if (bounce_addr == 0)
207 bounce_addr =
208 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
209 KM_IRQ0);
210
211 while (destlen) {
212 src = bounce_addr + bounce_sgl[j].offset;
213 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
214
215 copylen = min(srclen, destlen);
216 memcpy((void *)dest, (void *)src, copylen);
217
218 total_copied += copylen;
219 bounce_sgl[j].offset += copylen;
220 destlen -= copylen;
221 dest += copylen;
222
223 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
224 /* full */
225 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
226 j++;
227
228 /* if we need to use another bounce buffer */
229 if (destlen || i != orig_sgl_count - 1)
230 bounce_addr =
231 (unsigned long)kmap_atomic(
232 sg_page((&bounce_sgl[j])), KM_IRQ0);
233 } else if (destlen == 0 && i == orig_sgl_count - 1) {
234 /* unmap the last bounce that is < PAGE_SIZE */
235 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
236 }
237 }
238
239 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
240 KM_IRQ0);
241 }
242
243 local_irq_restore(flags);
244
245 return total_copied;
246}
247
K. Y. Srinivasan6a8ff442011-05-10 07:54:32 -0700248
249/* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
250static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
251 struct scatterlist *bounce_sgl,
252 unsigned int orig_sgl_count)
253{
254 int i;
255 int j = 0;
256 unsigned long src, dest;
257 unsigned int srclen, destlen, copylen;
258 unsigned int total_copied = 0;
259 unsigned long bounce_addr = 0;
260 unsigned long src_addr = 0;
261 unsigned long flags;
262
263 local_irq_save(flags);
264
265 for (i = 0; i < orig_sgl_count; i++) {
266 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
267 KM_IRQ0) + orig_sgl[i].offset;
268 src = src_addr;
269 srclen = orig_sgl[i].length;
270
271 if (bounce_addr == 0)
272 bounce_addr =
273 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
274 KM_IRQ0);
275
276 while (srclen) {
277 /* assume bounce offset always == 0 */
278 dest = bounce_addr + bounce_sgl[j].length;
279 destlen = PAGE_SIZE - bounce_sgl[j].length;
280
281 copylen = min(srclen, destlen);
282 memcpy((void *)dest, (void *)src, copylen);
283
284 total_copied += copylen;
285 bounce_sgl[j].length += copylen;
286 srclen -= copylen;
287 src += copylen;
288
289 if (bounce_sgl[j].length == PAGE_SIZE) {
290 /* full..move to next entry */
291 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
292 j++;
293
294 /* if we need to use another bounce buffer */
295 if (srclen || i != orig_sgl_count - 1)
296 bounce_addr =
297 (unsigned long)kmap_atomic(
298 sg_page((&bounce_sgl[j])), KM_IRQ0);
299
300 } else if (srclen == 0 && i == orig_sgl_count - 1) {
301 /* unmap the last bounce that is < PAGE_SIZE */
302 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
303 }
304 }
305
306 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
307 }
308
309 local_irq_restore(flags);
310
311 return total_copied;
312}
313
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -0700314
315/*
316 * storvsc_remove - Callback when our device is removed
317 */
318static int storvsc_remove(struct hv_device *dev)
319{
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -0700320 struct Scsi_Host *host = dev_get_drvdata(&dev->device);
321 struct hv_host_device *host_dev =
322 (struct hv_host_device *)host->hostdata;
323
324 /*
325 * Call to the vsc driver to let it know that the device is being
326 * removed
327 */
K. Y. Srinivasan6cdc57c2011-05-10 07:54:48 -0700328 storvsc_dev_remove(dev);
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -0700329
330 if (host_dev->request_pool) {
331 kmem_cache_destroy(host_dev->request_pool);
332 host_dev->request_pool = NULL;
333 }
334
335 DPRINT_INFO(STORVSC, "removing host adapter (%p)...", host);
336 scsi_remove_host(host);
337
338 DPRINT_INFO(STORVSC, "releasing host adapter (%p)...", host);
339 scsi_host_put(host);
340 return 0;
341}
342
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700343
344static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
345 sector_t capacity, int *info)
346{
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -0700347 sector_t nsect = capacity;
348 sector_t cylinders = nsect;
349 int heads, sectors_pt;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700350
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -0700351 /*
352 * We are making up these values; let us keep it simple.
353 */
354 heads = 0xff;
355 sectors_pt = 0x3f; /* Sectors per track */
356 sector_div(cylinders, heads * sectors_pt);
357 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
358 cylinders = 0xffff;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700359
360 info[0] = heads;
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -0700361 info[1] = sectors_pt;
362 info[2] = (int)cylinders;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700363
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -0700364 DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", (int)cylinders, heads,
365 sectors_pt);
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700366
367 return 0;
368}
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -0700369
370static int storvsc_host_reset(struct hv_device *device)
371{
372 struct storvsc_device *stor_device;
373 struct hv_storvsc_request *request;
374 struct vstor_packet *vstor_packet;
375 int ret, t;
376
377 DPRINT_INFO(STORVSC, "resetting host adapter...");
378
379 stor_device = get_stor_device(device);
380 if (!stor_device)
381 return -1;
382
383 request = &stor_device->reset_request;
384 vstor_packet = &request->vstor_packet;
385
386 init_completion(&request->wait_event);
387
388 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
389 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
390 vstor_packet->vm_srb.path_id = stor_device->path_id;
391
392 ret = vmbus_sendpacket(device->channel, vstor_packet,
393 sizeof(struct vstor_packet),
394 (unsigned long)&stor_device->reset_request,
395 VM_PKT_DATA_INBAND,
396 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
397 if (ret != 0)
398 goto cleanup;
399
400 t = wait_for_completion_timeout(&request->wait_event, HZ);
401 if (t == 0) {
402 ret = -ETIMEDOUT;
403 goto cleanup;
404 }
405
406 DPRINT_INFO(STORVSC, "host adapter reset completed");
407
408 /*
409 * At this point, all outstanding requests in the adapter
410 * should have been flushed out and return to us
411 */
412
413cleanup:
414 put_stor_device(device);
415 return ret;
416}
417
418
K. Y. Srinivasan96e690be2011-05-10 07:54:38 -0700419/*
420 * storvsc_host_reset_handler - Reset the scsi HBA
421 */
422static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
423{
424 int ret;
425 struct hv_host_device *host_dev =
426 (struct hv_host_device *)scmnd->device->host->hostdata;
427 struct hv_device *dev = host_dev->dev;
428
429 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...",
430 scmnd->device, dev);
431
432 /* Invokes the vsc to reset the host/bus */
433 ret = storvsc_host_reset(dev);
434 if (ret != 0)
435 return ret;
436
437 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted",
438 scmnd->device, dev);
439
440 return ret;
441}
442
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700443
444/*
445 * storvsc_commmand_completion - Command completion processing
446 */
447static void storvsc_commmand_completion(struct hv_storvsc_request *request)
448{
449 struct storvsc_cmd_request *cmd_request =
450 (struct storvsc_cmd_request *)request->context;
451 struct scsi_cmnd *scmnd = cmd_request->cmd;
452 struct hv_host_device *host_dev =
453 (struct hv_host_device *)scmnd->device->host->hostdata;
454 void (*scsi_done_fn)(struct scsi_cmnd *);
455 struct scsi_sense_hdr sense_hdr;
456 struct vmscsi_request *vm_srb;
457
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700458 if (cmd_request->bounce_sgl_count) {
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700459
460 /* FIXME: We can optimize on writes by just skipping this */
461 copy_from_bounce_buffer(scsi_sglist(scmnd),
462 cmd_request->bounce_sgl,
463 scsi_sg_count(scmnd));
464 destroy_bounce_buffer(cmd_request->bounce_sgl,
465 cmd_request->bounce_sgl_count);
466 }
467
468 vm_srb = &request->vstor_packet.vm_srb;
469 scmnd->result = vm_srb->scsi_status;
470
471 if (scmnd->result) {
472 if (scsi_normalize_sense(scmnd->sense_buffer,
473 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
474 scsi_print_sense_hdr("storvsc", &sense_hdr);
475 }
476
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700477 scsi_set_resid(scmnd,
478 request->data_buffer.len -
479 vm_srb->data_transfer_length);
480
481 scsi_done_fn = scmnd->scsi_done;
482
483 scmnd->host_scribble = NULL;
484 scmnd->scsi_done = NULL;
485
486 /* !!DO NOT MODIFY the scmnd after this call */
487 scsi_done_fn(scmnd);
488
489 kmem_cache_free(host_dev->request_pool, cmd_request);
490}
491
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700492
493/*
494 * storvsc_queuecommand - Initiate command processing
495 */
496static int storvsc_queuecommand_lck(struct scsi_cmnd *scmnd,
497 void (*done)(struct scsi_cmnd *))
498{
499 int ret;
500 struct hv_host_device *host_dev =
501 (struct hv_host_device *)scmnd->device->host->hostdata;
502 struct hv_device *dev = host_dev->dev;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700503 struct hv_storvsc_request *request;
504 struct storvsc_cmd_request *cmd_request;
505 unsigned int request_size = 0;
506 int i;
507 struct scatterlist *sgl;
508 unsigned int sg_count = 0;
509 struct vmscsi_request *vm_srb;
510
511
512 /* If retrying, no need to prep the cmd */
513 if (scmnd->host_scribble) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700514
515 cmd_request =
516 (struct storvsc_cmd_request *)scmnd->host_scribble;
517 DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p",
518 scmnd, cmd_request);
519
520 goto retry_request;
521 }
522
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700523 scmnd->scsi_done = done;
524
525 request_size = sizeof(struct storvsc_cmd_request);
526
527 cmd_request = kmem_cache_zalloc(host_dev->request_pool,
528 GFP_ATOMIC);
529 if (!cmd_request) {
530 scmnd->scsi_done = NULL;
531 return SCSI_MLQUEUE_DEVICE_BUSY;
532 }
533
534 /* Setup the cmd request */
535 cmd_request->bounce_sgl_count = 0;
536 cmd_request->bounce_sgl = NULL;
537 cmd_request->cmd = scmnd;
538
539 scmnd->host_scribble = (unsigned char *)cmd_request;
540
541 request = &cmd_request->request;
542 vm_srb = &request->vstor_packet.vm_srb;
543
544
545 /* Build the SRB */
546 switch (scmnd->sc_data_direction) {
547 case DMA_TO_DEVICE:
548 vm_srb->data_in = WRITE_TYPE;
549 break;
550 case DMA_FROM_DEVICE:
551 vm_srb->data_in = READ_TYPE;
552 break;
553 default:
554 vm_srb->data_in = UNKNOWN_TYPE;
555 break;
556 }
557
558 request->on_io_completion = storvsc_commmand_completion;
559 request->context = cmd_request;/* scmnd; */
560
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700561 vm_srb->port_number = host_dev->port;
562 vm_srb->path_id = scmnd->device->channel;
563 vm_srb->target_id = scmnd->device->id;
564 vm_srb->lun = scmnd->device->lun;
565
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700566 vm_srb->cdb_length = scmnd->cmd_len;
567
568 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
569
570 request->sense_buffer = scmnd->sense_buffer;
571
572
573 request->data_buffer.len = scsi_bufflen(scmnd);
574 if (scsi_sg_count(scmnd)) {
575 sgl = (struct scatterlist *)scsi_sglist(scmnd);
576 sg_count = scsi_sg_count(scmnd);
577
578 /* check if we need to bounce the sgl */
579 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
580 cmd_request->bounce_sgl =
581 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
582 scsi_bufflen(scmnd));
583 if (!cmd_request->bounce_sgl) {
584 scmnd->scsi_done = NULL;
585 scmnd->host_scribble = NULL;
586 kmem_cache_free(host_dev->request_pool,
587 cmd_request);
588
589 return SCSI_MLQUEUE_HOST_BUSY;
590 }
591
592 cmd_request->bounce_sgl_count =
593 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
594 PAGE_SHIFT;
595
596 /*
597 * FIXME: We can optimize on reads by just skipping
598 * this
599 */
600 copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl,
601 scsi_sg_count(scmnd));
602
603 sgl = cmd_request->bounce_sgl;
604 sg_count = cmd_request->bounce_sgl_count;
605 }
606
607 request->data_buffer.offset = sgl[0].offset;
608
609 for (i = 0; i < sg_count; i++)
610 request->data_buffer.pfn_array[i] =
611 page_to_pfn(sg_page((&sgl[i])));
612
613 } else if (scsi_sglist(scmnd)) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700614 request->data_buffer.offset =
615 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
616 request->data_buffer.pfn_array[0] =
617 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
618 }
619
620retry_request:
621 /* Invokes the vsc to start an IO */
K. Y. Srinivasan636f0fd2011-05-10 07:54:50 -0700622 ret = storvsc_do_io(dev, &cmd_request->request);
623
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700624 if (ret == -1) {
625 /* no more space */
626
627 if (cmd_request->bounce_sgl_count) {
628 /*
629 * FIXME: We can optimize on writes by just skipping
630 * this
631 */
632 copy_from_bounce_buffer(scsi_sglist(scmnd),
633 cmd_request->bounce_sgl,
634 scsi_sg_count(scmnd));
635 destroy_bounce_buffer(cmd_request->bounce_sgl,
636 cmd_request->bounce_sgl_count);
637 }
638
639 kmem_cache_free(host_dev->request_pool, cmd_request);
640
641 scmnd->scsi_done = NULL;
642 scmnd->host_scribble = NULL;
643
644 ret = SCSI_MLQUEUE_DEVICE_BUSY;
645 }
646
647 return ret;
648}
649
650static DEF_SCSI_QCMD(storvsc_queuecommand)
651
Hank Janssenbef4a342009-07-13 16:01:31 -0700652
Bill Pemberton454f18a2009-07-27 16:47:24 -0400653/* Scsi driver */
Hank Janssenbef4a342009-07-13 16:01:31 -0700654static struct scsi_host_template scsi_driver = {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700655 .module = THIS_MODULE,
656 .name = "storvsc_host_t",
657 .bios_param = storvsc_get_chs,
658 .queuecommand = storvsc_queuecommand,
659 .eh_host_reset_handler = storvsc_host_reset_handler,
660 .slave_alloc = storvsc_device_alloc,
661 .slave_configure = storvsc_device_configure,
662 .cmd_per_lun = 1,
663 /* 64 max_queue * 1 target */
Lars Lindley0686e4f2010-03-11 23:51:23 +0100664 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700665 .this_id = -1,
Bill Pemberton454f18a2009-07-27 16:47:24 -0400666 /* no use setting to 0 since ll_blk_rw reset it to 1 */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700667 /* currently 32 */
668 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
669 /*
670 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
671 * into 1 sg element. If set, we must limit the max_segment_size to
672 * PAGE_SIZE, otherwise we may get 1 sg element that represents
673 * multiple
674 */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400675 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700676 .use_clustering = ENABLE_CLUSTERING,
Bill Pemberton454f18a2009-07-27 16:47:24 -0400677 /* Make sure we dont get a sg segment crosses a page boundary */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700678 .dma_boundary = PAGE_SIZE-1,
Hank Janssenbef4a342009-07-13 16:01:31 -0700679};
680
681
Hank Janssen3e189512010-03-04 22:11:00 +0000682/*
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700683 * storvsc_probe - Add a new device for this driver
684 */
685
686static int storvsc_probe(struct hv_device *device)
687{
688 int ret;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700689 struct Scsi_Host *host;
690 struct hv_host_device *host_dev;
691 struct storvsc_device_info device_info;
692
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700693 host = scsi_host_alloc(&scsi_driver,
694 sizeof(struct hv_host_device));
695 if (!host)
696 return -ENOMEM;
697
698 dev_set_drvdata(&device->device, host);
699
700 host_dev = (struct hv_host_device *)host->hostdata;
701 memset(host_dev, 0, sizeof(struct hv_host_device));
702
703 host_dev->port = host->host_no;
704 host_dev->dev = device;
705
706 host_dev->request_pool =
707 kmem_cache_create(dev_name(&device->device),
708 sizeof(struct storvsc_cmd_request), 0,
709 SLAB_HWCACHE_ALIGN, NULL);
710
711 if (!host_dev->request_pool) {
712 scsi_host_put(host);
713 return -ENOMEM;
714 }
715
716 device_info.port_number = host->host_no;
K. Y. Srinivasanfa4d1232011-05-10 07:56:01 -0700717 device_info.ring_buffer_size = storvsc_ringbuffer_size;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700718 /* Call to the vsc driver to add the device */
K. Y. Srinivasan58f1f5c2011-05-10 07:54:49 -0700719 ret = storvsc_dev_add(device, (void *)&device_info);
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700720
721 if (ret != 0) {
722 kmem_cache_destroy(host_dev->request_pool);
723 scsi_host_put(host);
724 return -1;
725 }
726
727 host_dev->path = device_info.path_id;
728 host_dev->target = device_info.target_id;
729
730 /* max # of devices per target */
731 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
732 /* max # of targets per channel */
733 host->max_id = STORVSC_MAX_TARGETS;
734 /* max # of channels */
735 host->max_channel = STORVSC_MAX_CHANNELS - 1;
736
737 /* Register the HBA and start the scsi bus scan */
738 ret = scsi_add_host(host, &device->device);
739 if (ret != 0) {
740
K. Y. Srinivasan6cdc57c2011-05-10 07:54:48 -0700741 storvsc_dev_remove(device);
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700742
743 kmem_cache_destroy(host_dev->request_pool);
744 scsi_host_put(host);
745 return -1;
746 }
747
748 scsi_scan_host(host);
749 return ret;
750}
751
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -0700752/* The one and only one */
753
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -0700754static struct hv_driver storvsc_drv = {
755 .probe = storvsc_probe,
756 .remove = storvsc_remove,
K. Y. Srinivasan39ae6fa2011-05-10 07:54:46 -0700757};
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -0700758
759
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700760/*
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700761 * storvsc_drv_init - StorVsc driver initialization.
762 */
K. Y. Srinivasandb085772011-03-23 10:50:27 -0700763static int storvsc_drv_init(void)
Hank Janssenbef4a342009-07-13 16:01:31 -0700764{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700765 int ret;
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -0700766 struct hv_driver *drv = &storvsc_drv;
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -0700767 u32 max_outstanding_req_per_channel;
768
769 /*
770 * Divide the ring buffer data size (which is 1 page less
771 * than the ring buffer size since that page is reserved for
772 * the ring buffer indices) by the max request size (which is
773 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
774 */
775
776 max_outstanding_req_per_channel =
777 ((storvsc_ringbuffer_size - PAGE_SIZE) /
778 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
779 sizeof(struct vstor_packet) + sizeof(u64),
780 sizeof(u64)));
Hank Janssenbef4a342009-07-13 16:01:31 -0700781
K. Y. Srinivasan1a96d9a2011-05-10 07:56:06 -0700782 memcpy(&drv->dev_type, &gStorVscDeviceType,
783 sizeof(struct hv_guid));
784
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -0700785 if (max_outstanding_req_per_channel <
K. Y. Srinivasanf8feed02011-05-10 07:54:24 -0700786 STORVSC_MAX_IO_REQUESTS)
Hank Janssenbef4a342009-07-13 16:01:31 -0700787 return -1;
Hank Janssenbef4a342009-07-13 16:01:31 -0700788
K. Y. Srinivasan4742e822011-05-10 07:56:05 -0700789 drv->name = driver_name;
K. Y. Srinivasan206cf172011-05-10 07:56:04 -0700790 drv->driver.name = driver_name;
Hank Janssenbef4a342009-07-13 16:01:31 -0700791
Hank Janssenbef4a342009-07-13 16:01:31 -0700792
Bill Pemberton454f18a2009-07-27 16:47:24 -0400793 /* The driver belongs to vmbus */
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800794 ret = vmbus_child_driver_register(&drv->driver);
Hank Janssenbef4a342009-07-13 16:01:31 -0700795
Hank Janssenbef4a342009-07-13 16:01:31 -0700796 return ret;
797}
798
Hank Janssenbef4a342009-07-13 16:01:31 -0700799static int storvsc_drv_exit_cb(struct device *dev, void *data)
800{
801 struct device **curr = (struct device **)data;
802 *curr = dev;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400803 return 1; /* stop iterating */
Hank Janssenbef4a342009-07-13 16:01:31 -0700804}
805
Greg Kroah-Hartmanbd1de702009-07-29 09:04:51 -0700806static void storvsc_drv_exit(void)
Hank Janssenbef4a342009-07-13 16:01:31 -0700807{
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -0700808 struct hv_driver *drv = &storvsc_drv;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700809 struct device *current_dev = NULL;
Bill Pemberton2295ba22009-07-28 13:46:22 -0400810 int ret;
Hank Janssenbef4a342009-07-13 16:01:31 -0700811
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700812 while (1) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700813 current_dev = NULL;
814
Bill Pemberton454f18a2009-07-27 16:47:24 -0400815 /* Get the device */
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800816 ret = driver_for_each_device(&drv->driver, NULL,
Bill Pemberton2295ba22009-07-28 13:46:22 -0400817 (void *) &current_dev,
818 storvsc_drv_exit_cb);
819
Hank Janssenbef4a342009-07-13 16:01:31 -0700820
821 if (current_dev == NULL)
822 break;
823
Bill Pemberton454f18a2009-07-27 16:47:24 -0400824 /* Initiate removal from the top-down */
Hank Janssenbef4a342009-07-13 16:01:31 -0700825 device_unregister(current_dev);
826 }
827
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800828 vmbus_child_driver_unregister(&drv->driver);
Hank Janssenbef4a342009-07-13 16:01:31 -0700829 return;
830}
831
Hank Janssenbef4a342009-07-13 16:01:31 -0700832static int __init storvsc_init(void)
833{
834 int ret;
835
Hank Janssenbef4a342009-07-13 16:01:31 -0700836 DPRINT_INFO(STORVSC_DRV, "Storvsc initializing....");
K. Y. Srinivasandb085772011-03-23 10:50:27 -0700837 ret = storvsc_drv_init();
Hank Janssenbef4a342009-07-13 16:01:31 -0700838 return ret;
839}
840
841static void __exit storvsc_exit(void)
842{
Hank Janssenbef4a342009-07-13 16:01:31 -0700843 storvsc_drv_exit();
Hank Janssenbef4a342009-07-13 16:01:31 -0700844}
845
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700846MODULE_LICENSE("GPL");
Hank Janssen26c14cc2010-02-11 23:02:42 +0000847MODULE_VERSION(HV_DRV_VERSION);
Stephen Hemminger3afc7cc32010-05-06 21:44:45 -0700848MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
Hank Janssenbef4a342009-07-13 16:01:31 -0700849module_init(storvsc_init);
850module_exit(storvsc_exit);