blob: ed2140c01543e40aac51eb00a5f4af587489ac9f [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>
K. Y. Srinivasan3e1edf62011-06-06 15:49:36 -070027#include <linux/dmi.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070028#include <scsi/scsi.h>
29#include <scsi/scsi_cmnd.h>
30#include <scsi/scsi_host.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_tcq.h>
33#include <scsi/scsi_eh.h>
34#include <scsi/scsi_devinfo.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070035#include <scsi/scsi_dbg.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070036
37#include "hyperv.h"
K. Y. Srinivasancdee1502011-05-12 19:34:34 -070038#include "hyperv_storage.h"
Hank Janssenbef4a342009-07-13 16:01:31 -070039
K. Y. Srinivasan2db2cab2011-05-10 07:54:39 -070040static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
Hank Janssenbef4a342009-07-13 16:01:31 -070041
K. Y. Srinivasan3d598ce2011-05-10 07:54:40 -070042module_param(storvsc_ringbuffer_size, int, S_IRUGO);
43MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
44
K. Y. Srinivasan0f0cdc62011-05-10 07:54:18 -070045static const char *driver_name = "storvsc";
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070046
K. Y. Srinivasan972621c2011-05-10 07:54:19 -070047struct hv_host_device {
K. Y. Srinivasan97c15292011-05-10 07:54:21 -070048 struct hv_device *dev;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070049 struct kmem_cache *request_pool;
50 unsigned int port;
51 unsigned char path;
52 unsigned char target;
Hank Janssenbef4a342009-07-13 16:01:31 -070053};
54
55struct storvsc_cmd_request {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070056 struct list_head entry;
57 struct scsi_cmnd *cmd;
Hank Janssenbef4a342009-07-13 16:01:31 -070058
59 unsigned int bounce_sgl_count;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070060 struct scatterlist *bounce_sgl;
Hank Janssenbef4a342009-07-13 16:01:31 -070061
Nicolas Palix0b3f6832009-07-29 14:10:19 +020062 struct hv_storvsc_request request;
Hank Janssenbef4a342009-07-13 16:01:31 -070063};
64
Hank Janssenbef4a342009-07-13 16:01:31 -070065
K. Y. Srinivasan5b60ace2011-05-10 07:54:25 -070066static int storvsc_device_alloc(struct scsi_device *sdevice)
67{
68 /*
69 * This enables luns to be located sparsely. Otherwise, we may not
70 * discovered them.
71 */
72 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
73 return 0;
74}
75
K. Y. Srinivasana1ebfea2011-05-10 07:54:26 -070076static int storvsc_merge_bvec(struct request_queue *q,
77 struct bvec_merge_data *bmd, struct bio_vec *bvec)
78{
79 /* checking done by caller. */
80 return bvec->bv_len;
81}
82
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -070083static int storvsc_device_configure(struct scsi_device *sdevice)
84{
85 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
86 STORVSC_MAX_IO_REQUESTS);
87
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -070088 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
89
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -070090 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
91
92 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -070093
94 return 0;
95}
96
K. Y. Srinivasan49a3c7a2011-05-10 07:54:28 -070097static void destroy_bounce_buffer(struct scatterlist *sgl,
98 unsigned int sg_count)
99{
100 int i;
101 struct page *page_buf;
102
103 for (i = 0; i < sg_count; i++) {
104 page_buf = sg_page((&sgl[i]));
105 if (page_buf != NULL)
106 __free_page(page_buf);
107 }
108
109 kfree(sgl);
110}
111
K. Y. Srinivasan3862ef32011-05-10 07:54:29 -0700112static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
113{
114 int i;
115
116 /* No need to check */
117 if (sg_count < 2)
118 return -1;
119
120 /* We have at least 2 sg entries */
121 for (i = 0; i < sg_count; i++) {
122 if (i == 0) {
123 /* make sure 1st one does not have hole */
124 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
125 return i;
126 } else if (i == sg_count - 1) {
127 /* make sure last one does not have hole */
128 if (sgl[i].offset != 0)
129 return i;
130 } else {
131 /* make sure no hole in the middle */
132 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
133 return i;
134 }
135 }
136 return -1;
137}
138
K. Y. Srinivasana9753cb2011-05-10 07:54:30 -0700139static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
140 unsigned int sg_count,
141 unsigned int len)
142{
143 int i;
144 int num_pages;
145 struct scatterlist *bounce_sgl;
146 struct page *page_buf;
147
148 num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
149
150 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
151 if (!bounce_sgl)
152 return NULL;
153
154 for (i = 0; i < num_pages; i++) {
155 page_buf = alloc_page(GFP_ATOMIC);
156 if (!page_buf)
157 goto cleanup;
158 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
159 }
160
161 return bounce_sgl;
162
163cleanup:
164 destroy_bounce_buffer(bounce_sgl, num_pages);
165 return NULL;
166}
167
K. Y. Srinivasan29fe2c92011-05-10 07:54:31 -0700168
169/* Assume the original sgl has enough room */
170static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
171 struct scatterlist *bounce_sgl,
172 unsigned int orig_sgl_count)
173{
174 int i;
175 int j = 0;
176 unsigned long src, dest;
177 unsigned int srclen, destlen, copylen;
178 unsigned int total_copied = 0;
179 unsigned long bounce_addr = 0;
180 unsigned long dest_addr = 0;
181 unsigned long flags;
182
183 local_irq_save(flags);
184
185 for (i = 0; i < orig_sgl_count; i++) {
186 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
187 KM_IRQ0) + orig_sgl[i].offset;
188 dest = dest_addr;
189 destlen = orig_sgl[i].length;
190
191 if (bounce_addr == 0)
192 bounce_addr =
193 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
194 KM_IRQ0);
195
196 while (destlen) {
197 src = bounce_addr + bounce_sgl[j].offset;
198 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
199
200 copylen = min(srclen, destlen);
201 memcpy((void *)dest, (void *)src, copylen);
202
203 total_copied += copylen;
204 bounce_sgl[j].offset += copylen;
205 destlen -= copylen;
206 dest += copylen;
207
208 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
209 /* full */
210 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
211 j++;
212
213 /* if we need to use another bounce buffer */
214 if (destlen || i != orig_sgl_count - 1)
215 bounce_addr =
216 (unsigned long)kmap_atomic(
217 sg_page((&bounce_sgl[j])), KM_IRQ0);
218 } else if (destlen == 0 && i == orig_sgl_count - 1) {
219 /* unmap the last bounce that is < PAGE_SIZE */
220 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
221 }
222 }
223
224 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
225 KM_IRQ0);
226 }
227
228 local_irq_restore(flags);
229
230 return total_copied;
231}
232
K. Y. Srinivasan6a8ff442011-05-10 07:54:32 -0700233
234/* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
235static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
236 struct scatterlist *bounce_sgl,
237 unsigned int orig_sgl_count)
238{
239 int i;
240 int j = 0;
241 unsigned long src, dest;
242 unsigned int srclen, destlen, copylen;
243 unsigned int total_copied = 0;
244 unsigned long bounce_addr = 0;
245 unsigned long src_addr = 0;
246 unsigned long flags;
247
248 local_irq_save(flags);
249
250 for (i = 0; i < orig_sgl_count; i++) {
251 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
252 KM_IRQ0) + orig_sgl[i].offset;
253 src = src_addr;
254 srclen = orig_sgl[i].length;
255
256 if (bounce_addr == 0)
257 bounce_addr =
258 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
259 KM_IRQ0);
260
261 while (srclen) {
262 /* assume bounce offset always == 0 */
263 dest = bounce_addr + bounce_sgl[j].length;
264 destlen = PAGE_SIZE - bounce_sgl[j].length;
265
266 copylen = min(srclen, destlen);
267 memcpy((void *)dest, (void *)src, copylen);
268
269 total_copied += copylen;
270 bounce_sgl[j].length += copylen;
271 srclen -= copylen;
272 src += copylen;
273
274 if (bounce_sgl[j].length == PAGE_SIZE) {
275 /* full..move to next entry */
276 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
277 j++;
278
279 /* if we need to use another bounce buffer */
280 if (srclen || i != orig_sgl_count - 1)
281 bounce_addr =
282 (unsigned long)kmap_atomic(
283 sg_page((&bounce_sgl[j])), KM_IRQ0);
284
285 } else if (srclen == 0 && i == orig_sgl_count - 1) {
286 /* unmap the last bounce that is < PAGE_SIZE */
287 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
288 }
289 }
290
291 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
292 }
293
294 local_irq_restore(flags);
295
296 return total_copied;
297}
298
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -0700299
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -0700300static int storvsc_remove(struct hv_device *dev)
301{
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -0700302 struct Scsi_Host *host = dev_get_drvdata(&dev->device);
303 struct hv_host_device *host_dev =
304 (struct hv_host_device *)host->hostdata;
305
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -0700306 scsi_remove_host(host);
307
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -0700308 scsi_host_put(host);
K. Y. Srinivasand36b0a02011-06-06 15:49:29 -0700309
K. Y. Srinivasan2935a402011-06-06 15:49:28 -0700310 storvsc_dev_remove(dev);
311 if (host_dev->request_pool) {
312 kmem_cache_destroy(host_dev->request_pool);
313 host_dev->request_pool = NULL;
314 }
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -0700315 return 0;
316}
317
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700318
319static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
320 sector_t capacity, int *info)
321{
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -0700322 sector_t nsect = capacity;
323 sector_t cylinders = nsect;
324 int heads, sectors_pt;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700325
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -0700326 /*
327 * We are making up these values; let us keep it simple.
328 */
329 heads = 0xff;
330 sectors_pt = 0x3f; /* Sectors per track */
331 sector_div(cylinders, heads * sectors_pt);
332 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
333 cylinders = 0xffff;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700334
335 info[0] = heads;
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -0700336 info[1] = sectors_pt;
337 info[2] = (int)cylinders;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700338
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700339 return 0;
340}
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -0700341
342static int storvsc_host_reset(struct hv_device *device)
343{
344 struct storvsc_device *stor_device;
345 struct hv_storvsc_request *request;
346 struct vstor_packet *vstor_packet;
347 int ret, t;
348
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -0700349
350 stor_device = get_stor_device(device);
351 if (!stor_device)
352 return -1;
353
354 request = &stor_device->reset_request;
355 vstor_packet = &request->vstor_packet;
356
357 init_completion(&request->wait_event);
358
359 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
360 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
361 vstor_packet->vm_srb.path_id = stor_device->path_id;
362
363 ret = vmbus_sendpacket(device->channel, vstor_packet,
364 sizeof(struct vstor_packet),
365 (unsigned long)&stor_device->reset_request,
366 VM_PKT_DATA_INBAND,
367 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
368 if (ret != 0)
369 goto cleanup;
370
K. Y. Srinivasan46d2eb62011-06-16 13:16:36 -0700371 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -0700372 if (t == 0) {
373 ret = -ETIMEDOUT;
374 goto cleanup;
375 }
376
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -0700377
378 /*
379 * At this point, all outstanding requests in the adapter
380 * should have been flushed out and return to us
381 */
382
383cleanup:
384 put_stor_device(device);
385 return ret;
386}
387
388
K. Y. Srinivasan96e690be2011-05-10 07:54:38 -0700389/*
390 * storvsc_host_reset_handler - Reset the scsi HBA
391 */
392static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
393{
394 int ret;
395 struct hv_host_device *host_dev =
396 (struct hv_host_device *)scmnd->device->host->hostdata;
397 struct hv_device *dev = host_dev->dev;
398
K. Y. Srinivasan96e690be2011-05-10 07:54:38 -0700399 ret = storvsc_host_reset(dev);
400 if (ret != 0)
401 return ret;
402
K. Y. Srinivasan96e690be2011-05-10 07:54:38 -0700403 return ret;
404}
405
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700406
407/*
408 * storvsc_commmand_completion - Command completion processing
409 */
410static void storvsc_commmand_completion(struct hv_storvsc_request *request)
411{
412 struct storvsc_cmd_request *cmd_request =
413 (struct storvsc_cmd_request *)request->context;
414 struct scsi_cmnd *scmnd = cmd_request->cmd;
415 struct hv_host_device *host_dev =
416 (struct hv_host_device *)scmnd->device->host->hostdata;
417 void (*scsi_done_fn)(struct scsi_cmnd *);
418 struct scsi_sense_hdr sense_hdr;
419 struct vmscsi_request *vm_srb;
420
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700421 if (cmd_request->bounce_sgl_count) {
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700422
423 /* FIXME: We can optimize on writes by just skipping this */
424 copy_from_bounce_buffer(scsi_sglist(scmnd),
425 cmd_request->bounce_sgl,
426 scsi_sg_count(scmnd));
427 destroy_bounce_buffer(cmd_request->bounce_sgl,
428 cmd_request->bounce_sgl_count);
429 }
430
431 vm_srb = &request->vstor_packet.vm_srb;
432 scmnd->result = vm_srb->scsi_status;
433
434 if (scmnd->result) {
435 if (scsi_normalize_sense(scmnd->sense_buffer,
436 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
437 scsi_print_sense_hdr("storvsc", &sense_hdr);
438 }
439
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700440 scsi_set_resid(scmnd,
441 request->data_buffer.len -
442 vm_srb->data_transfer_length);
443
444 scsi_done_fn = scmnd->scsi_done;
445
446 scmnd->host_scribble = NULL;
447 scmnd->scsi_done = NULL;
448
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700449 scsi_done_fn(scmnd);
450
451 kmem_cache_free(host_dev->request_pool, cmd_request);
452}
453
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700454
455/*
456 * storvsc_queuecommand - Initiate command processing
457 */
458static int storvsc_queuecommand_lck(struct scsi_cmnd *scmnd,
459 void (*done)(struct scsi_cmnd *))
460{
461 int ret;
462 struct hv_host_device *host_dev =
463 (struct hv_host_device *)scmnd->device->host->hostdata;
464 struct hv_device *dev = host_dev->dev;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700465 struct hv_storvsc_request *request;
466 struct storvsc_cmd_request *cmd_request;
467 unsigned int request_size = 0;
468 int i;
469 struct scatterlist *sgl;
470 unsigned int sg_count = 0;
471 struct vmscsi_request *vm_srb;
472
473
474 /* If retrying, no need to prep the cmd */
475 if (scmnd->host_scribble) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700476
477 cmd_request =
478 (struct storvsc_cmd_request *)scmnd->host_scribble;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700479
480 goto retry_request;
481 }
482
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700483 scmnd->scsi_done = done;
484
485 request_size = sizeof(struct storvsc_cmd_request);
486
487 cmd_request = kmem_cache_zalloc(host_dev->request_pool,
488 GFP_ATOMIC);
489 if (!cmd_request) {
490 scmnd->scsi_done = NULL;
491 return SCSI_MLQUEUE_DEVICE_BUSY;
492 }
493
494 /* Setup the cmd request */
495 cmd_request->bounce_sgl_count = 0;
496 cmd_request->bounce_sgl = NULL;
497 cmd_request->cmd = scmnd;
498
499 scmnd->host_scribble = (unsigned char *)cmd_request;
500
501 request = &cmd_request->request;
502 vm_srb = &request->vstor_packet.vm_srb;
503
504
505 /* Build the SRB */
506 switch (scmnd->sc_data_direction) {
507 case DMA_TO_DEVICE:
508 vm_srb->data_in = WRITE_TYPE;
509 break;
510 case DMA_FROM_DEVICE:
511 vm_srb->data_in = READ_TYPE;
512 break;
513 default:
514 vm_srb->data_in = UNKNOWN_TYPE;
515 break;
516 }
517
518 request->on_io_completion = storvsc_commmand_completion;
519 request->context = cmd_request;/* scmnd; */
520
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700521 vm_srb->port_number = host_dev->port;
522 vm_srb->path_id = scmnd->device->channel;
523 vm_srb->target_id = scmnd->device->id;
524 vm_srb->lun = scmnd->device->lun;
525
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700526 vm_srb->cdb_length = scmnd->cmd_len;
527
528 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
529
530 request->sense_buffer = scmnd->sense_buffer;
531
532
533 request->data_buffer.len = scsi_bufflen(scmnd);
534 if (scsi_sg_count(scmnd)) {
535 sgl = (struct scatterlist *)scsi_sglist(scmnd);
536 sg_count = scsi_sg_count(scmnd);
537
538 /* check if we need to bounce the sgl */
539 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
540 cmd_request->bounce_sgl =
541 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
542 scsi_bufflen(scmnd));
543 if (!cmd_request->bounce_sgl) {
544 scmnd->scsi_done = NULL;
545 scmnd->host_scribble = NULL;
546 kmem_cache_free(host_dev->request_pool,
547 cmd_request);
548
549 return SCSI_MLQUEUE_HOST_BUSY;
550 }
551
552 cmd_request->bounce_sgl_count =
553 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
554 PAGE_SHIFT;
555
556 /*
557 * FIXME: We can optimize on reads by just skipping
558 * this
559 */
560 copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl,
561 scsi_sg_count(scmnd));
562
563 sgl = cmd_request->bounce_sgl;
564 sg_count = cmd_request->bounce_sgl_count;
565 }
566
567 request->data_buffer.offset = sgl[0].offset;
568
569 for (i = 0; i < sg_count; i++)
570 request->data_buffer.pfn_array[i] =
571 page_to_pfn(sg_page((&sgl[i])));
572
573 } else if (scsi_sglist(scmnd)) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700574 request->data_buffer.offset =
575 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
576 request->data_buffer.pfn_array[0] =
577 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
578 }
579
580retry_request:
581 /* Invokes the vsc to start an IO */
K. Y. Srinivasan636f0fd2011-05-10 07:54:50 -0700582 ret = storvsc_do_io(dev, &cmd_request->request);
583
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700584 if (ret == -1) {
585 /* no more space */
586
587 if (cmd_request->bounce_sgl_count) {
588 /*
589 * FIXME: We can optimize on writes by just skipping
590 * this
591 */
592 copy_from_bounce_buffer(scsi_sglist(scmnd),
593 cmd_request->bounce_sgl,
594 scsi_sg_count(scmnd));
595 destroy_bounce_buffer(cmd_request->bounce_sgl,
596 cmd_request->bounce_sgl_count);
597 }
598
599 kmem_cache_free(host_dev->request_pool, cmd_request);
600
601 scmnd->scsi_done = NULL;
602 scmnd->host_scribble = NULL;
603
604 ret = SCSI_MLQUEUE_DEVICE_BUSY;
605 }
606
607 return ret;
608}
609
610static DEF_SCSI_QCMD(storvsc_queuecommand)
611
Hank Janssenbef4a342009-07-13 16:01:31 -0700612
Bill Pemberton454f18a2009-07-27 16:47:24 -0400613/* Scsi driver */
Hank Janssenbef4a342009-07-13 16:01:31 -0700614static struct scsi_host_template scsi_driver = {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700615 .module = THIS_MODULE,
616 .name = "storvsc_host_t",
617 .bios_param = storvsc_get_chs,
618 .queuecommand = storvsc_queuecommand,
619 .eh_host_reset_handler = storvsc_host_reset_handler,
620 .slave_alloc = storvsc_device_alloc,
621 .slave_configure = storvsc_device_configure,
622 .cmd_per_lun = 1,
623 /* 64 max_queue * 1 target */
Lars Lindley0686e4f2010-03-11 23:51:23 +0100624 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700625 .this_id = -1,
Bill Pemberton454f18a2009-07-27 16:47:24 -0400626 /* no use setting to 0 since ll_blk_rw reset it to 1 */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700627 /* currently 32 */
628 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
629 /*
630 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
631 * into 1 sg element. If set, we must limit the max_segment_size to
632 * PAGE_SIZE, otherwise we may get 1 sg element that represents
633 * multiple
634 */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400635 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700636 .use_clustering = ENABLE_CLUSTERING,
Bill Pemberton454f18a2009-07-27 16:47:24 -0400637 /* Make sure we dont get a sg segment crosses a page boundary */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700638 .dma_boundary = PAGE_SIZE-1,
Hank Janssenbef4a342009-07-13 16:01:31 -0700639};
640
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -0700641static const struct hv_vmbus_device_id id_table[] = {
642 {
643 /* SCSI guid */
644 .guid = {
645 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
646 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f
647 }
648 },
649 {
650 .guid = { }
651 },
652};
Hank Janssenbef4a342009-07-13 16:01:31 -0700653
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -0700654MODULE_DEVICE_TABLE(vmbus, id_table);
Hank Janssen3e189512010-03-04 22:11:00 +0000655/*
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700656 * storvsc_probe - Add a new device for this driver
657 */
658
659static int storvsc_probe(struct hv_device *device)
660{
661 int ret;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700662 struct Scsi_Host *host;
663 struct hv_host_device *host_dev;
664 struct storvsc_device_info device_info;
665
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700666 host = scsi_host_alloc(&scsi_driver,
667 sizeof(struct hv_host_device));
668 if (!host)
669 return -ENOMEM;
670
671 dev_set_drvdata(&device->device, host);
672
673 host_dev = (struct hv_host_device *)host->hostdata;
674 memset(host_dev, 0, sizeof(struct hv_host_device));
675
676 host_dev->port = host->host_no;
677 host_dev->dev = device;
678
679 host_dev->request_pool =
680 kmem_cache_create(dev_name(&device->device),
681 sizeof(struct storvsc_cmd_request), 0,
682 SLAB_HWCACHE_ALIGN, NULL);
683
684 if (!host_dev->request_pool) {
685 scsi_host_put(host);
686 return -ENOMEM;
687 }
688
689 device_info.port_number = host->host_no;
K. Y. Srinivasanfa4d1232011-05-10 07:56:01 -0700690 device_info.ring_buffer_size = storvsc_ringbuffer_size;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700691 /* Call to the vsc driver to add the device */
K. Y. Srinivasan58f1f5c2011-05-10 07:54:49 -0700692 ret = storvsc_dev_add(device, (void *)&device_info);
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700693
694 if (ret != 0) {
695 kmem_cache_destroy(host_dev->request_pool);
696 scsi_host_put(host);
697 return -1;
698 }
699
700 host_dev->path = device_info.path_id;
701 host_dev->target = device_info.target_id;
702
703 /* max # of devices per target */
704 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
705 /* max # of targets per channel */
706 host->max_id = STORVSC_MAX_TARGETS;
707 /* max # of channels */
708 host->max_channel = STORVSC_MAX_CHANNELS - 1;
709
710 /* Register the HBA and start the scsi bus scan */
711 ret = scsi_add_host(host, &device->device);
712 if (ret != 0) {
713
K. Y. Srinivasan6cdc57c2011-05-10 07:54:48 -0700714 storvsc_dev_remove(device);
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700715
716 kmem_cache_destroy(host_dev->request_pool);
717 scsi_host_put(host);
718 return -1;
719 }
720
721 scsi_scan_host(host);
722 return ret;
723}
724
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -0700725/* The one and only one */
726
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -0700727static struct hv_driver storvsc_drv = {
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -0700728 .id_table = id_table,
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -0700729 .probe = storvsc_probe,
730 .remove = storvsc_remove,
K. Y. Srinivasan39ae6fa2011-05-10 07:54:46 -0700731};
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -0700732
K. Y. Srinivasan3e1edf62011-06-06 15:49:36 -0700733/*
734 * We use a DMI table to determine if we should autoload this driver This is
735 * needed by distro tools to determine if the hyperv drivers should be
736 * installed and/or configured. We don't do anything else with the table, but
737 * it needs to be present.
738 */
739
740static const struct dmi_system_id __initconst
741hv_stor_dmi_table[] __maybe_unused = {
742 {
743 .ident = "Hyper-V",
744 .matches = {
745 DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
746 DMI_MATCH(DMI_PRODUCT_NAME, "Virtual Machine"),
747 DMI_MATCH(DMI_BOARD_NAME, "Virtual Machine"),
748 },
749 },
750 { },
751};
752MODULE_DEVICE_TABLE(dmi, hv_stor_dmi_table);
753
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -0700754static int __init storvsc_drv_init(void)
Hank Janssenbef4a342009-07-13 16:01:31 -0700755{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700756 int ret;
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -0700757 struct hv_driver *drv = &storvsc_drv;
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -0700758 u32 max_outstanding_req_per_channel;
759
760 /*
761 * Divide the ring buffer data size (which is 1 page less
762 * than the ring buffer size since that page is reserved for
763 * the ring buffer indices) by the max request size (which is
764 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
765 */
766
767 max_outstanding_req_per_channel =
768 ((storvsc_ringbuffer_size - PAGE_SIZE) /
769 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
770 sizeof(struct vstor_packet) + sizeof(u64),
771 sizeof(u64)));
Hank Janssenbef4a342009-07-13 16:01:31 -0700772
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -0700773 if (max_outstanding_req_per_channel <
K. Y. Srinivasanf8feed02011-05-10 07:54:24 -0700774 STORVSC_MAX_IO_REQUESTS)
Hank Janssenbef4a342009-07-13 16:01:31 -0700775 return -1;
Hank Janssenbef4a342009-07-13 16:01:31 -0700776
K. Y. Srinivasan206cf172011-05-10 07:56:04 -0700777 drv->driver.name = driver_name;
Hank Janssenbef4a342009-07-13 16:01:31 -0700778
Hank Janssenbef4a342009-07-13 16:01:31 -0700779
Bill Pemberton454f18a2009-07-27 16:47:24 -0400780 /* The driver belongs to vmbus */
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800781 ret = vmbus_child_driver_register(&drv->driver);
Hank Janssenbef4a342009-07-13 16:01:31 -0700782
Hank Janssenbef4a342009-07-13 16:01:31 -0700783 return ret;
784}
785
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -0700786static void __exit storvsc_drv_exit(void)
Hank Janssenbef4a342009-07-13 16:01:31 -0700787{
K. Y. Srinivasan2bda87c2011-05-12 19:34:45 -0700788 vmbus_child_driver_unregister(&storvsc_drv.driver);
Hank Janssenbef4a342009-07-13 16:01:31 -0700789}
790
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700791MODULE_LICENSE("GPL");
Hank Janssen26c14cc2010-02-11 23:02:42 +0000792MODULE_VERSION(HV_DRV_VERSION);
Stephen Hemminger3afc7cc32010-05-06 21:44:45 -0700793MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -0700794module_init(storvsc_drv_init);
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -0700795module_exit(storvsc_drv_exit);