blob: 5624d26ba4ce68fe2748dad7eeea860cfd07bd98 [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
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -0700110
111 return 0;
112}
113
K. Y. Srinivasan5b60ace2011-05-10 07:54:25 -0700114static int storvsc_device_alloc(struct scsi_device *sdevice)
115{
116 /*
117 * This enables luns to be located sparsely. Otherwise, we may not
118 * discovered them.
119 */
120 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
121 return 0;
122}
123
K. Y. Srinivasana1ebfea2011-05-10 07:54:26 -0700124static int storvsc_merge_bvec(struct request_queue *q,
125 struct bvec_merge_data *bmd, struct bio_vec *bvec)
126{
127 /* checking done by caller. */
128 return bvec->bv_len;
129}
130
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700131static int storvsc_device_configure(struct scsi_device *sdevice)
132{
133 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
134 STORVSC_MAX_IO_REQUESTS);
135
136 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting max segment size to %ld",
137 sdevice, PAGE_SIZE);
138 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
139
140 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine",
141 sdevice);
142 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
143
144 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700145
146 return 0;
147}
148
K. Y. Srinivasan49a3c7a2011-05-10 07:54:28 -0700149static void destroy_bounce_buffer(struct scatterlist *sgl,
150 unsigned int sg_count)
151{
152 int i;
153 struct page *page_buf;
154
155 for (i = 0; i < sg_count; i++) {
156 page_buf = sg_page((&sgl[i]));
157 if (page_buf != NULL)
158 __free_page(page_buf);
159 }
160
161 kfree(sgl);
162}
163
K. Y. Srinivasan3862ef32011-05-10 07:54:29 -0700164static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
165{
166 int i;
167
168 /* No need to check */
169 if (sg_count < 2)
170 return -1;
171
172 /* We have at least 2 sg entries */
173 for (i = 0; i < sg_count; i++) {
174 if (i == 0) {
175 /* make sure 1st one does not have hole */
176 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
177 return i;
178 } else if (i == sg_count - 1) {
179 /* make sure last one does not have hole */
180 if (sgl[i].offset != 0)
181 return i;
182 } else {
183 /* make sure no hole in the middle */
184 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
185 return i;
186 }
187 }
188 return -1;
189}
190
K. Y. Srinivasana9753cb2011-05-10 07:54:30 -0700191static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
192 unsigned int sg_count,
193 unsigned int len)
194{
195 int i;
196 int num_pages;
197 struct scatterlist *bounce_sgl;
198 struct page *page_buf;
199
200 num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
201
202 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
203 if (!bounce_sgl)
204 return NULL;
205
206 for (i = 0; i < num_pages; i++) {
207 page_buf = alloc_page(GFP_ATOMIC);
208 if (!page_buf)
209 goto cleanup;
210 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
211 }
212
213 return bounce_sgl;
214
215cleanup:
216 destroy_bounce_buffer(bounce_sgl, num_pages);
217 return NULL;
218}
219
K. Y. Srinivasan29fe2c92011-05-10 07:54:31 -0700220
221/* Assume the original sgl has enough room */
222static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
223 struct scatterlist *bounce_sgl,
224 unsigned int orig_sgl_count)
225{
226 int i;
227 int j = 0;
228 unsigned long src, dest;
229 unsigned int srclen, destlen, copylen;
230 unsigned int total_copied = 0;
231 unsigned long bounce_addr = 0;
232 unsigned long dest_addr = 0;
233 unsigned long flags;
234
235 local_irq_save(flags);
236
237 for (i = 0; i < orig_sgl_count; i++) {
238 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
239 KM_IRQ0) + orig_sgl[i].offset;
240 dest = dest_addr;
241 destlen = orig_sgl[i].length;
242
243 if (bounce_addr == 0)
244 bounce_addr =
245 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
246 KM_IRQ0);
247
248 while (destlen) {
249 src = bounce_addr + bounce_sgl[j].offset;
250 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
251
252 copylen = min(srclen, destlen);
253 memcpy((void *)dest, (void *)src, copylen);
254
255 total_copied += copylen;
256 bounce_sgl[j].offset += copylen;
257 destlen -= copylen;
258 dest += copylen;
259
260 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
261 /* full */
262 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
263 j++;
264
265 /* if we need to use another bounce buffer */
266 if (destlen || i != orig_sgl_count - 1)
267 bounce_addr =
268 (unsigned long)kmap_atomic(
269 sg_page((&bounce_sgl[j])), KM_IRQ0);
270 } else if (destlen == 0 && i == orig_sgl_count - 1) {
271 /* unmap the last bounce that is < PAGE_SIZE */
272 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
273 }
274 }
275
276 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
277 KM_IRQ0);
278 }
279
280 local_irq_restore(flags);
281
282 return total_copied;
283}
284
K. Y. Srinivasan6a8ff442011-05-10 07:54:32 -0700285
286/* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
287static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
288 struct scatterlist *bounce_sgl,
289 unsigned int orig_sgl_count)
290{
291 int i;
292 int j = 0;
293 unsigned long src, dest;
294 unsigned int srclen, destlen, copylen;
295 unsigned int total_copied = 0;
296 unsigned long bounce_addr = 0;
297 unsigned long src_addr = 0;
298 unsigned long flags;
299
300 local_irq_save(flags);
301
302 for (i = 0; i < orig_sgl_count; i++) {
303 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
304 KM_IRQ0) + orig_sgl[i].offset;
305 src = src_addr;
306 srclen = orig_sgl[i].length;
307
308 if (bounce_addr == 0)
309 bounce_addr =
310 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
311 KM_IRQ0);
312
313 while (srclen) {
314 /* assume bounce offset always == 0 */
315 dest = bounce_addr + bounce_sgl[j].length;
316 destlen = PAGE_SIZE - bounce_sgl[j].length;
317
318 copylen = min(srclen, destlen);
319 memcpy((void *)dest, (void *)src, copylen);
320
321 total_copied += copylen;
322 bounce_sgl[j].length += copylen;
323 srclen -= copylen;
324 src += copylen;
325
326 if (bounce_sgl[j].length == PAGE_SIZE) {
327 /* full..move to next entry */
328 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
329 j++;
330
331 /* if we need to use another bounce buffer */
332 if (srclen || i != orig_sgl_count - 1)
333 bounce_addr =
334 (unsigned long)kmap_atomic(
335 sg_page((&bounce_sgl[j])), KM_IRQ0);
336
337 } else if (srclen == 0 && i == orig_sgl_count - 1) {
338 /* unmap the last bounce that is < PAGE_SIZE */
339 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
340 }
341 }
342
343 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
344 }
345
346 local_irq_restore(flags);
347
348 return total_copied;
349}
350
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -0700351
352/*
353 * storvsc_remove - Callback when our device is removed
354 */
355static int storvsc_remove(struct hv_device *dev)
356{
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -0700357 struct Scsi_Host *host = dev_get_drvdata(&dev->device);
358 struct hv_host_device *host_dev =
359 (struct hv_host_device *)host->hostdata;
360
361 /*
362 * Call to the vsc driver to let it know that the device is being
363 * removed
364 */
K. Y. Srinivasan6cdc57c2011-05-10 07:54:48 -0700365 storvsc_dev_remove(dev);
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -0700366
367 if (host_dev->request_pool) {
368 kmem_cache_destroy(host_dev->request_pool);
369 host_dev->request_pool = NULL;
370 }
371
372 DPRINT_INFO(STORVSC, "removing host adapter (%p)...", host);
373 scsi_remove_host(host);
374
375 DPRINT_INFO(STORVSC, "releasing host adapter (%p)...", host);
376 scsi_host_put(host);
377 return 0;
378}
379
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700380
381static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
382 sector_t capacity, int *info)
383{
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -0700384 sector_t nsect = capacity;
385 sector_t cylinders = nsect;
386 int heads, sectors_pt;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700387
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -0700388 /*
389 * We are making up these values; let us keep it simple.
390 */
391 heads = 0xff;
392 sectors_pt = 0x3f; /* Sectors per track */
393 sector_div(cylinders, heads * sectors_pt);
394 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
395 cylinders = 0xffff;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700396
397 info[0] = heads;
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -0700398 info[1] = sectors_pt;
399 info[2] = (int)cylinders;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700400
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -0700401 DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", (int)cylinders, heads,
402 sectors_pt);
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700403
404 return 0;
405}
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -0700406
407static int storvsc_host_reset(struct hv_device *device)
408{
409 struct storvsc_device *stor_device;
410 struct hv_storvsc_request *request;
411 struct vstor_packet *vstor_packet;
412 int ret, t;
413
414 DPRINT_INFO(STORVSC, "resetting host adapter...");
415
416 stor_device = get_stor_device(device);
417 if (!stor_device)
418 return -1;
419
420 request = &stor_device->reset_request;
421 vstor_packet = &request->vstor_packet;
422
423 init_completion(&request->wait_event);
424
425 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
426 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
427 vstor_packet->vm_srb.path_id = stor_device->path_id;
428
429 ret = vmbus_sendpacket(device->channel, vstor_packet,
430 sizeof(struct vstor_packet),
431 (unsigned long)&stor_device->reset_request,
432 VM_PKT_DATA_INBAND,
433 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
434 if (ret != 0)
435 goto cleanup;
436
437 t = wait_for_completion_timeout(&request->wait_event, HZ);
438 if (t == 0) {
439 ret = -ETIMEDOUT;
440 goto cleanup;
441 }
442
443 DPRINT_INFO(STORVSC, "host adapter reset completed");
444
445 /*
446 * At this point, all outstanding requests in the adapter
447 * should have been flushed out and return to us
448 */
449
450cleanup:
451 put_stor_device(device);
452 return ret;
453}
454
455
K. Y. Srinivasan96e690be2011-05-10 07:54:38 -0700456/*
457 * storvsc_host_reset_handler - Reset the scsi HBA
458 */
459static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
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;
465
466 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...",
467 scmnd->device, dev);
468
469 /* Invokes the vsc to reset the host/bus */
470 ret = storvsc_host_reset(dev);
471 if (ret != 0)
472 return ret;
473
474 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted",
475 scmnd->device, dev);
476
477 return ret;
478}
479
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700480
481/*
482 * storvsc_commmand_completion - Command completion processing
483 */
484static void storvsc_commmand_completion(struct hv_storvsc_request *request)
485{
486 struct storvsc_cmd_request *cmd_request =
487 (struct storvsc_cmd_request *)request->context;
488 struct scsi_cmnd *scmnd = cmd_request->cmd;
489 struct hv_host_device *host_dev =
490 (struct hv_host_device *)scmnd->device->host->hostdata;
491 void (*scsi_done_fn)(struct scsi_cmnd *);
492 struct scsi_sense_hdr sense_hdr;
493 struct vmscsi_request *vm_srb;
494
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700495 if (cmd_request->bounce_sgl_count) {
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700496
497 /* FIXME: We can optimize on writes by just skipping this */
498 copy_from_bounce_buffer(scsi_sglist(scmnd),
499 cmd_request->bounce_sgl,
500 scsi_sg_count(scmnd));
501 destroy_bounce_buffer(cmd_request->bounce_sgl,
502 cmd_request->bounce_sgl_count);
503 }
504
505 vm_srb = &request->vstor_packet.vm_srb;
506 scmnd->result = vm_srb->scsi_status;
507
508 if (scmnd->result) {
509 if (scsi_normalize_sense(scmnd->sense_buffer,
510 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
511 scsi_print_sense_hdr("storvsc", &sense_hdr);
512 }
513
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700514 scsi_set_resid(scmnd,
515 request->data_buffer.len -
516 vm_srb->data_transfer_length);
517
518 scsi_done_fn = scmnd->scsi_done;
519
520 scmnd->host_scribble = NULL;
521 scmnd->scsi_done = NULL;
522
523 /* !!DO NOT MODIFY the scmnd after this call */
524 scsi_done_fn(scmnd);
525
526 kmem_cache_free(host_dev->request_pool, cmd_request);
527}
528
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700529
530/*
531 * storvsc_queuecommand - Initiate command processing
532 */
533static int storvsc_queuecommand_lck(struct scsi_cmnd *scmnd,
534 void (*done)(struct scsi_cmnd *))
535{
536 int ret;
537 struct hv_host_device *host_dev =
538 (struct hv_host_device *)scmnd->device->host->hostdata;
539 struct hv_device *dev = host_dev->dev;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700540 struct hv_storvsc_request *request;
541 struct storvsc_cmd_request *cmd_request;
542 unsigned int request_size = 0;
543 int i;
544 struct scatterlist *sgl;
545 unsigned int sg_count = 0;
546 struct vmscsi_request *vm_srb;
547
548
549 /* If retrying, no need to prep the cmd */
550 if (scmnd->host_scribble) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700551
552 cmd_request =
553 (struct storvsc_cmd_request *)scmnd->host_scribble;
554 DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p",
555 scmnd, cmd_request);
556
557 goto retry_request;
558 }
559
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700560 scmnd->scsi_done = done;
561
562 request_size = sizeof(struct storvsc_cmd_request);
563
564 cmd_request = kmem_cache_zalloc(host_dev->request_pool,
565 GFP_ATOMIC);
566 if (!cmd_request) {
567 scmnd->scsi_done = NULL;
568 return SCSI_MLQUEUE_DEVICE_BUSY;
569 }
570
571 /* Setup the cmd request */
572 cmd_request->bounce_sgl_count = 0;
573 cmd_request->bounce_sgl = NULL;
574 cmd_request->cmd = scmnd;
575
576 scmnd->host_scribble = (unsigned char *)cmd_request;
577
578 request = &cmd_request->request;
579 vm_srb = &request->vstor_packet.vm_srb;
580
581
582 /* Build the SRB */
583 switch (scmnd->sc_data_direction) {
584 case DMA_TO_DEVICE:
585 vm_srb->data_in = WRITE_TYPE;
586 break;
587 case DMA_FROM_DEVICE:
588 vm_srb->data_in = READ_TYPE;
589 break;
590 default:
591 vm_srb->data_in = UNKNOWN_TYPE;
592 break;
593 }
594
595 request->on_io_completion = storvsc_commmand_completion;
596 request->context = cmd_request;/* scmnd; */
597
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700598 vm_srb->port_number = host_dev->port;
599 vm_srb->path_id = scmnd->device->channel;
600 vm_srb->target_id = scmnd->device->id;
601 vm_srb->lun = scmnd->device->lun;
602
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700603 vm_srb->cdb_length = scmnd->cmd_len;
604
605 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
606
607 request->sense_buffer = scmnd->sense_buffer;
608
609
610 request->data_buffer.len = scsi_bufflen(scmnd);
611 if (scsi_sg_count(scmnd)) {
612 sgl = (struct scatterlist *)scsi_sglist(scmnd);
613 sg_count = scsi_sg_count(scmnd);
614
615 /* check if we need to bounce the sgl */
616 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
617 cmd_request->bounce_sgl =
618 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
619 scsi_bufflen(scmnd));
620 if (!cmd_request->bounce_sgl) {
621 scmnd->scsi_done = NULL;
622 scmnd->host_scribble = NULL;
623 kmem_cache_free(host_dev->request_pool,
624 cmd_request);
625
626 return SCSI_MLQUEUE_HOST_BUSY;
627 }
628
629 cmd_request->bounce_sgl_count =
630 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
631 PAGE_SHIFT;
632
633 /*
634 * FIXME: We can optimize on reads by just skipping
635 * this
636 */
637 copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl,
638 scsi_sg_count(scmnd));
639
640 sgl = cmd_request->bounce_sgl;
641 sg_count = cmd_request->bounce_sgl_count;
642 }
643
644 request->data_buffer.offset = sgl[0].offset;
645
646 for (i = 0; i < sg_count; i++)
647 request->data_buffer.pfn_array[i] =
648 page_to_pfn(sg_page((&sgl[i])));
649
650 } else if (scsi_sglist(scmnd)) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700651 request->data_buffer.offset =
652 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
653 request->data_buffer.pfn_array[0] =
654 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
655 }
656
657retry_request:
658 /* Invokes the vsc to start an IO */
K. Y. Srinivasan636f0fd2011-05-10 07:54:50 -0700659 ret = storvsc_do_io(dev, &cmd_request->request);
660
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700661 if (ret == -1) {
662 /* no more space */
663
664 if (cmd_request->bounce_sgl_count) {
665 /*
666 * FIXME: We can optimize on writes by just skipping
667 * this
668 */
669 copy_from_bounce_buffer(scsi_sglist(scmnd),
670 cmd_request->bounce_sgl,
671 scsi_sg_count(scmnd));
672 destroy_bounce_buffer(cmd_request->bounce_sgl,
673 cmd_request->bounce_sgl_count);
674 }
675
676 kmem_cache_free(host_dev->request_pool, cmd_request);
677
678 scmnd->scsi_done = NULL;
679 scmnd->host_scribble = NULL;
680
681 ret = SCSI_MLQUEUE_DEVICE_BUSY;
682 }
683
684 return ret;
685}
686
687static DEF_SCSI_QCMD(storvsc_queuecommand)
688
Hank Janssenbef4a342009-07-13 16:01:31 -0700689
Bill Pemberton454f18a2009-07-27 16:47:24 -0400690/* Scsi driver */
Hank Janssenbef4a342009-07-13 16:01:31 -0700691static struct scsi_host_template scsi_driver = {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700692 .module = THIS_MODULE,
693 .name = "storvsc_host_t",
694 .bios_param = storvsc_get_chs,
695 .queuecommand = storvsc_queuecommand,
696 .eh_host_reset_handler = storvsc_host_reset_handler,
697 .slave_alloc = storvsc_device_alloc,
698 .slave_configure = storvsc_device_configure,
699 .cmd_per_lun = 1,
700 /* 64 max_queue * 1 target */
Lars Lindley0686e4f2010-03-11 23:51:23 +0100701 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700702 .this_id = -1,
Bill Pemberton454f18a2009-07-27 16:47:24 -0400703 /* no use setting to 0 since ll_blk_rw reset it to 1 */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700704 /* currently 32 */
705 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
706 /*
707 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
708 * into 1 sg element. If set, we must limit the max_segment_size to
709 * PAGE_SIZE, otherwise we may get 1 sg element that represents
710 * multiple
711 */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400712 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700713 .use_clustering = ENABLE_CLUSTERING,
Bill Pemberton454f18a2009-07-27 16:47:24 -0400714 /* Make sure we dont get a sg segment crosses a page boundary */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700715 .dma_boundary = PAGE_SIZE-1,
Hank Janssenbef4a342009-07-13 16:01:31 -0700716};
717
718
Hank Janssen3e189512010-03-04 22:11:00 +0000719/*
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700720 * storvsc_probe - Add a new device for this driver
721 */
722
723static int storvsc_probe(struct hv_device *device)
724{
725 int ret;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700726 struct Scsi_Host *host;
727 struct hv_host_device *host_dev;
728 struct storvsc_device_info device_info;
729
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700730 host = scsi_host_alloc(&scsi_driver,
731 sizeof(struct hv_host_device));
732 if (!host)
733 return -ENOMEM;
734
735 dev_set_drvdata(&device->device, host);
736
737 host_dev = (struct hv_host_device *)host->hostdata;
738 memset(host_dev, 0, sizeof(struct hv_host_device));
739
740 host_dev->port = host->host_no;
741 host_dev->dev = device;
742
743 host_dev->request_pool =
744 kmem_cache_create(dev_name(&device->device),
745 sizeof(struct storvsc_cmd_request), 0,
746 SLAB_HWCACHE_ALIGN, NULL);
747
748 if (!host_dev->request_pool) {
749 scsi_host_put(host);
750 return -ENOMEM;
751 }
752
753 device_info.port_number = host->host_no;
754 /* Call to the vsc driver to add the device */
K. Y. Srinivasan58f1f5c2011-05-10 07:54:49 -0700755 ret = storvsc_dev_add(device, (void *)&device_info);
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700756
757 if (ret != 0) {
758 kmem_cache_destroy(host_dev->request_pool);
759 scsi_host_put(host);
760 return -1;
761 }
762
763 host_dev->path = device_info.path_id;
764 host_dev->target = device_info.target_id;
765
766 /* max # of devices per target */
767 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
768 /* max # of targets per channel */
769 host->max_id = STORVSC_MAX_TARGETS;
770 /* max # of channels */
771 host->max_channel = STORVSC_MAX_CHANNELS - 1;
772
773 /* Register the HBA and start the scsi bus scan */
774 ret = scsi_add_host(host, &device->device);
775 if (ret != 0) {
776
K. Y. Srinivasan6cdc57c2011-05-10 07:54:48 -0700777 storvsc_dev_remove(device);
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700778
779 kmem_cache_destroy(host_dev->request_pool);
780 scsi_host_put(host);
781 return -1;
782 }
783
784 scsi_scan_host(host);
785 return ret;
786}
787
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -0700788/* The one and only one */
789
K. Y. Srinivasan39ae6fa2011-05-10 07:54:46 -0700790static struct storvsc_driver storvsc_drv = {
791 .base.probe = storvsc_probe,
792 .base.remove = storvsc_remove,
793};
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -0700794
795
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -0700796/*
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700797 * storvsc_drv_init - StorVsc driver initialization.
798 */
K. Y. Srinivasandb085772011-03-23 10:50:27 -0700799static int storvsc_drv_init(void)
Hank Janssenbef4a342009-07-13 16:01:31 -0700800{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700801 int ret;
K. Y. Srinivasan565c38d2011-05-10 07:54:44 -0700802 struct storvsc_driver *storvsc_drv_obj = &storvsc_drv;
803 struct hv_driver *drv = &storvsc_drv.base;
Hank Janssenbef4a342009-07-13 16:01:31 -0700804
Hank Janssen8a046022010-12-06 12:26:45 -0800805 storvsc_drv_obj->ring_buffer_size = storvsc_ringbuffer_size;
Hank Janssenbef4a342009-07-13 16:01:31 -0700806
Bill Pemberton454f18a2009-07-27 16:47:24 -0400807 /* Callback to client driver to complete the initialization */
K. Y. Srinivasancf6fdfb2011-05-10 07:54:36 -0700808 storvsc_initialize(&storvsc_drv_obj->base);
Hank Janssenbef4a342009-07-13 16:01:31 -0700809
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700810 DPRINT_INFO(STORVSC_DRV,
K. Y. Srinivasan1e05d882011-03-28 09:33:28 -0700811 "max outstanding reqs %u",
Hank Janssen8a046022010-12-06 12:26:45 -0800812 storvsc_drv_obj->max_outstanding_req_per_channel);
Hank Janssenbef4a342009-07-13 16:01:31 -0700813
Hank Janssen8a046022010-12-06 12:26:45 -0800814 if (storvsc_drv_obj->max_outstanding_req_per_channel <
K. Y. Srinivasanf8feed02011-05-10 07:54:24 -0700815 STORVSC_MAX_IO_REQUESTS)
Hank Janssenbef4a342009-07-13 16:01:31 -0700816 return -1;
Hank Janssenbef4a342009-07-13 16:01:31 -0700817
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800818 drv->driver.name = storvsc_drv_obj->base.name;
Hank Janssenbef4a342009-07-13 16:01:31 -0700819
Hank Janssenbef4a342009-07-13 16:01:31 -0700820
Bill Pemberton454f18a2009-07-27 16:47:24 -0400821 /* The driver belongs to vmbus */
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800822 ret = vmbus_child_driver_register(&drv->driver);
Hank Janssenbef4a342009-07-13 16:01:31 -0700823
Hank Janssenbef4a342009-07-13 16:01:31 -0700824 return ret;
825}
826
Hank Janssenbef4a342009-07-13 16:01:31 -0700827static int storvsc_drv_exit_cb(struct device *dev, void *data)
828{
829 struct device **curr = (struct device **)data;
830 *curr = dev;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400831 return 1; /* stop iterating */
Hank Janssenbef4a342009-07-13 16:01:31 -0700832}
833
Greg Kroah-Hartmanbd1de702009-07-29 09:04:51 -0700834static void storvsc_drv_exit(void)
Hank Janssenbef4a342009-07-13 16:01:31 -0700835{
K. Y. Srinivasan565c38d2011-05-10 07:54:44 -0700836 struct storvsc_driver *storvsc_drv_obj = &storvsc_drv;
837 struct hv_driver *drv = &storvsc_drv.base;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700838 struct device *current_dev = NULL;
Bill Pemberton2295ba22009-07-28 13:46:22 -0400839 int ret;
Hank Janssenbef4a342009-07-13 16:01:31 -0700840
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700841 while (1) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700842 current_dev = NULL;
843
Bill Pemberton454f18a2009-07-27 16:47:24 -0400844 /* Get the device */
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800845 ret = driver_for_each_device(&drv->driver, NULL,
Bill Pemberton2295ba22009-07-28 13:46:22 -0400846 (void *) &current_dev,
847 storvsc_drv_exit_cb);
848
Hank Janssenbef4a342009-07-13 16:01:31 -0700849
850 if (current_dev == NULL)
851 break;
852
Bill Pemberton454f18a2009-07-27 16:47:24 -0400853 /* Initiate removal from the top-down */
Hank Janssenbef4a342009-07-13 16:01:31 -0700854 device_unregister(current_dev);
855 }
856
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800857 if (storvsc_drv_obj->base.cleanup)
858 storvsc_drv_obj->base.cleanup(&storvsc_drv_obj->base);
Hank Janssenbef4a342009-07-13 16:01:31 -0700859
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800860 vmbus_child_driver_unregister(&drv->driver);
Hank Janssenbef4a342009-07-13 16:01:31 -0700861 return;
862}
863
Hank Janssenbef4a342009-07-13 16:01:31 -0700864static int __init storvsc_init(void)
865{
866 int ret;
867
Hank Janssenbef4a342009-07-13 16:01:31 -0700868 DPRINT_INFO(STORVSC_DRV, "Storvsc initializing....");
K. Y. Srinivasandb085772011-03-23 10:50:27 -0700869 ret = storvsc_drv_init();
Hank Janssenbef4a342009-07-13 16:01:31 -0700870 return ret;
871}
872
873static void __exit storvsc_exit(void)
874{
Hank Janssenbef4a342009-07-13 16:01:31 -0700875 storvsc_drv_exit();
Hank Janssenbef4a342009-07-13 16:01:31 -0700876}
877
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700878MODULE_LICENSE("GPL");
Hank Janssen26c14cc2010-02-11 23:02:42 +0000879MODULE_VERSION(HV_DRV_VERSION);
Stephen Hemminger3afc7cc32010-05-06 21:44:45 -0700880MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
Hank Janssenbef4a342009-07-13 16:01:31 -0700881module_init(storvsc_init);
882module_exit(storvsc_exit);