blob: 096f615a92ecd58b67d25921ca87b9d3eafa2cd8 [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 */
K. Y. Srinivasana1be1702011-08-27 11:31:23 -070022
23#include <linux/kernel.h>
24#include <linux/sched.h>
25#include <linux/completion.h>
26#include <linux/string.h>
27#include <linux/mm.h>
28#include <linux/delay.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070029#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070031#include <linux/module.h>
32#include <linux/device.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070033#include <scsi/scsi.h>
34#include <scsi/scsi_cmnd.h>
35#include <scsi/scsi_host.h>
36#include <scsi/scsi_device.h>
37#include <scsi/scsi_tcq.h>
38#include <scsi/scsi_eh.h>
39#include <scsi/scsi_devinfo.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070040#include <scsi/scsi_dbg.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070041
42#include "hyperv.h"
K. Y. Srinivasancdee1502011-05-12 19:34:34 -070043#include "hyperv_storage.h"
Hank Janssenbef4a342009-07-13 16:01:31 -070044
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -070045
K. Y. Srinivasana1be1702011-08-27 11:31:23 -070046static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -070047
K. Y. Srinivasana1be1702011-08-27 11:31:23 -070048module_param(storvsc_ringbuffer_size, int, S_IRUGO);
49MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -070050
K. Y. Srinivasana1be1702011-08-27 11:31:23 -070051struct hv_host_device {
52 struct hv_device *dev;
53 struct kmem_cache *request_pool;
54 unsigned int port;
55 unsigned char path;
56 unsigned char target;
57};
58
59struct storvsc_cmd_request {
60 struct list_head entry;
61 struct scsi_cmnd *cmd;
62
63 unsigned int bounce_sgl_count;
64 struct scatterlist *bounce_sgl;
65
66 struct hv_storvsc_request request;
67};
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -070068
69static inline struct storvsc_device *alloc_stor_device(struct hv_device *device)
70{
71 struct storvsc_device *stor_device;
72
73 stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
74 if (!stor_device)
75 return NULL;
76
77 stor_device->destroy = false;
78 init_waitqueue_head(&stor_device->waiting_to_drain);
79 stor_device->device = device;
80 device->ext = stor_device;
81
82 return stor_device;
83}
84
85
86static inline struct storvsc_device *get_in_stor_device(
87 struct hv_device *device)
88{
89 struct storvsc_device *stor_device;
90 unsigned long flags;
91
92 spin_lock_irqsave(&device->channel->inbound_lock, flags);
93 stor_device = (struct storvsc_device *)device->ext;
94
95 if (!stor_device)
96 goto get_in_err;
97
98 /*
99 * If the device is being destroyed; allow incoming
100 * traffic only to cleanup outstanding requests.
101 */
102
103 if (stor_device->destroy &&
104 (atomic_read(&stor_device->num_outstanding_req) == 0))
105 stor_device = NULL;
106
107get_in_err:
108 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
109 return stor_device;
110
111}
112
113static int storvsc_channel_init(struct hv_device *device)
114{
115 struct storvsc_device *stor_device;
116 struct hv_storvsc_request *request;
117 struct vstor_packet *vstor_packet;
118 int ret, t;
119
120 stor_device = get_out_stor_device(device);
121 if (!stor_device)
122 return -ENODEV;
123
124 request = &stor_device->init_request;
125 vstor_packet = &request->vstor_packet;
126
127 /*
128 * Now, initiate the vsc/vsp initialization protocol on the open
129 * channel
130 */
131 memset(request, 0, sizeof(struct hv_storvsc_request));
132 init_completion(&request->wait_event);
133 vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
134 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
135
136 ret = vmbus_sendpacket(device->channel, vstor_packet,
137 sizeof(struct vstor_packet),
138 (unsigned long)request,
139 VM_PKT_DATA_INBAND,
140 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
141 if (ret != 0)
142 goto cleanup;
143
144 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
145 if (t == 0) {
146 ret = -ETIMEDOUT;
147 goto cleanup;
148 }
149
150 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
151 vstor_packet->status != 0)
152 goto cleanup;
153
154
155 /* reuse the packet for version range supported */
156 memset(vstor_packet, 0, sizeof(struct vstor_packet));
157 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
158 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
159
160 vstor_packet->version.major_minor = VMSTOR_PROTOCOL_VERSION_CURRENT;
161 FILL_VMSTOR_REVISION(vstor_packet->version.revision);
162
163 ret = vmbus_sendpacket(device->channel, vstor_packet,
164 sizeof(struct vstor_packet),
165 (unsigned long)request,
166 VM_PKT_DATA_INBAND,
167 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
168 if (ret != 0)
169 goto cleanup;
170
171 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
172 if (t == 0) {
173 ret = -ETIMEDOUT;
174 goto cleanup;
175 }
176
177 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
178 vstor_packet->status != 0)
179 goto cleanup;
180
181
182 memset(vstor_packet, 0, sizeof(struct vstor_packet));
183 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
184 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
185 vstor_packet->storage_channel_properties.port_number =
186 stor_device->port_number;
187
188 ret = vmbus_sendpacket(device->channel, vstor_packet,
189 sizeof(struct vstor_packet),
190 (unsigned long)request,
191 VM_PKT_DATA_INBAND,
192 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
193
194 if (ret != 0)
195 goto cleanup;
196
197 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
198 if (t == 0) {
199 ret = -ETIMEDOUT;
200 goto cleanup;
201 }
202
203 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
204 vstor_packet->status != 0)
205 goto cleanup;
206
207 stor_device->path_id = vstor_packet->storage_channel_properties.path_id;
208 stor_device->target_id
209 = vstor_packet->storage_channel_properties.target_id;
210
211 memset(vstor_packet, 0, sizeof(struct vstor_packet));
212 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
213 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
214
215 ret = vmbus_sendpacket(device->channel, vstor_packet,
216 sizeof(struct vstor_packet),
217 (unsigned long)request,
218 VM_PKT_DATA_INBAND,
219 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
220
221 if (ret != 0)
222 goto cleanup;
223
224 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
225 if (t == 0) {
226 ret = -ETIMEDOUT;
227 goto cleanup;
228 }
229
230 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
231 vstor_packet->status != 0)
232 goto cleanup;
233
234
235cleanup:
236 return ret;
237}
238
239static void storvsc_on_io_completion(struct hv_device *device,
240 struct vstor_packet *vstor_packet,
241 struct hv_storvsc_request *request)
242{
243 struct storvsc_device *stor_device;
244 struct vstor_packet *stor_pkt;
245
246 stor_device = (struct storvsc_device *)device->ext;
247
248 stor_pkt = &request->vstor_packet;
249
250
251 /* Copy over the status...etc */
252 stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
253 stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
254 stor_pkt->vm_srb.sense_info_length =
255 vstor_packet->vm_srb.sense_info_length;
256
257 if (vstor_packet->vm_srb.scsi_status != 0 ||
258 vstor_packet->vm_srb.srb_status != 1){
259 DPRINT_WARN(STORVSC,
260 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
261 stor_pkt->vm_srb.cdb[0],
262 vstor_packet->vm_srb.scsi_status,
263 vstor_packet->vm_srb.srb_status);
264 }
265
266 if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
267 /* CHECK_CONDITION */
268 if (vstor_packet->vm_srb.srb_status & 0x80) {
269 /* autosense data available */
270 DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data "
271 "valid - len %d\n", request,
272 vstor_packet->vm_srb.sense_info_length);
273
274 memcpy(request->sense_buffer,
275 vstor_packet->vm_srb.sense_data,
276 vstor_packet->vm_srb.sense_info_length);
277
278 }
279 }
280
281 stor_pkt->vm_srb.data_transfer_length =
282 vstor_packet->vm_srb.data_transfer_length;
283
284 request->on_io_completion(request);
285
286 if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
287 stor_device->drain_notify)
288 wake_up(&stor_device->waiting_to_drain);
289
290
291}
292
293static void storvsc_on_receive(struct hv_device *device,
294 struct vstor_packet *vstor_packet,
295 struct hv_storvsc_request *request)
296{
297 switch (vstor_packet->operation) {
298 case VSTOR_OPERATION_COMPLETE_IO:
299 storvsc_on_io_completion(device, vstor_packet, request);
300 break;
301 case VSTOR_OPERATION_REMOVE_DEVICE:
302
303 default:
304 break;
305 }
306}
307
308static void storvsc_on_channel_callback(void *context)
309{
310 struct hv_device *device = (struct hv_device *)context;
311 struct storvsc_device *stor_device;
312 u32 bytes_recvd;
313 u64 request_id;
314 unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
315 struct hv_storvsc_request *request;
316 int ret;
317
318
319 stor_device = get_in_stor_device(device);
320 if (!stor_device)
321 return;
322
323 do {
324 ret = vmbus_recvpacket(device->channel, packet,
325 ALIGN(sizeof(struct vstor_packet), 8),
326 &bytes_recvd, &request_id);
327 if (ret == 0 && bytes_recvd > 0) {
328
329 request = (struct hv_storvsc_request *)
330 (unsigned long)request_id;
331
332 if ((request == &stor_device->init_request) ||
333 (request == &stor_device->reset_request)) {
334
335 memcpy(&request->vstor_packet, packet,
336 sizeof(struct vstor_packet));
337 complete(&request->wait_event);
338 } else {
339 storvsc_on_receive(device,
340 (struct vstor_packet *)packet,
341 request);
342 }
343 } else {
344 break;
345 }
346 } while (1);
347
348 return;
349}
350
351static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
352{
353 struct vmstorage_channel_properties props;
354 int ret;
355
356 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
357
358 /* Open the channel */
359 ret = vmbus_open(device->channel,
360 ring_size,
361 ring_size,
362 (void *)&props,
363 sizeof(struct vmstorage_channel_properties),
364 storvsc_on_channel_callback, device);
365
366 if (ret != 0)
367 return ret;
368
369 ret = storvsc_channel_init(device);
370
371 return ret;
372}
373
374int storvsc_dev_add(struct hv_device *device,
375 void *additional_info)
376{
377 struct storvsc_device *stor_device;
378 struct storvsc_device_info *device_info;
379 int ret = 0;
380
381 device_info = (struct storvsc_device_info *)additional_info;
382 stor_device = alloc_stor_device(device);
383 if (!stor_device)
384 return -ENOMEM;
385
386 /* Save the channel properties to our storvsc channel */
387
388 /*
389 * If we support more than 1 scsi channel, we need to set the
390 * port number here to the scsi channel but how do we get the
391 * scsi channel prior to the bus scan.
392 *
393 * The host does not support this.
394 */
395
396 stor_device->port_number = device_info->port_number;
397 /* Send it back up */
398 ret = storvsc_connect_to_vsp(device, device_info->ring_buffer_size);
399 if (ret) {
400 kfree(stor_device);
401 return ret;
402 }
403 device_info->path_id = stor_device->path_id;
404 device_info->target_id = stor_device->target_id;
405
406 return ret;
407}
408
409int storvsc_dev_remove(struct hv_device *device)
410{
411 struct storvsc_device *stor_device;
412 unsigned long flags;
413
414 stor_device = (struct storvsc_device *)device->ext;
415
416 spin_lock_irqsave(&device->channel->inbound_lock, flags);
417 stor_device->destroy = true;
418 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
419
420 /*
421 * At this point, all outbound traffic should be disable. We
422 * only allow inbound traffic (responses) to proceed so that
423 * outstanding requests can be completed.
424 */
425
426 storvsc_wait_to_drain(stor_device);
427
428 /*
429 * Since we have already drained, we don't need to busy wait
430 * as was done in final_release_stor_device()
431 * Note that we cannot set the ext pointer to NULL until
432 * we have drained - to drain the outgoing packets, we need to
433 * allow incoming packets.
434 */
435 spin_lock_irqsave(&device->channel->inbound_lock, flags);
436 device->ext = NULL;
437 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
438
439 /* Close the channel */
440 vmbus_close(device->channel);
441
442 kfree(stor_device);
443 return 0;
444}
445
446int storvsc_do_io(struct hv_device *device,
447 struct hv_storvsc_request *request)
448{
449 struct storvsc_device *stor_device;
450 struct vstor_packet *vstor_packet;
451 int ret = 0;
452
453 vstor_packet = &request->vstor_packet;
454 stor_device = get_out_stor_device(device);
455
456 if (!stor_device)
457 return -ENODEV;
458
459
460 request->device = device;
461
462
463 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
464
465 vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
466
467
468 vstor_packet->vm_srb.sense_info_length = SENSE_BUFFER_SIZE;
469
470
471 vstor_packet->vm_srb.data_transfer_length =
472 request->data_buffer.len;
473
474 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
475
476 if (request->data_buffer.len) {
477 ret = vmbus_sendpacket_multipagebuffer(device->channel,
478 &request->data_buffer,
479 vstor_packet,
480 sizeof(struct vstor_packet),
481 (unsigned long)request);
482 } else {
483 ret = vmbus_sendpacket(device->channel, vstor_packet,
484 sizeof(struct vstor_packet),
485 (unsigned long)request,
486 VM_PKT_DATA_INBAND,
487 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
488 }
489
490 if (ret != 0)
491 return ret;
492
493 atomic_inc(&stor_device->num_outstanding_req);
494
495 return ret;
496}
497
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -0700498static void storvsc_get_ide_info(struct hv_device *dev, int *target, int *path)
499{
500 *target =
501 dev->dev_instance.b[5] << 8 | dev->dev_instance.b[4];
502
503 *path =
504 dev->dev_instance.b[3] << 24 |
505 dev->dev_instance.b[2] << 16 |
506 dev->dev_instance.b[1] << 8 | dev->dev_instance.b[0];
507}
508
Hank Janssenbef4a342009-07-13 16:01:31 -0700509
K. Y. Srinivasan5b60ace2011-05-10 07:54:25 -0700510static int storvsc_device_alloc(struct scsi_device *sdevice)
511{
512 /*
513 * This enables luns to be located sparsely. Otherwise, we may not
514 * discovered them.
515 */
516 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
517 return 0;
518}
519
K. Y. Srinivasana1ebfea2011-05-10 07:54:26 -0700520static int storvsc_merge_bvec(struct request_queue *q,
521 struct bvec_merge_data *bmd, struct bio_vec *bvec)
522{
523 /* checking done by caller. */
524 return bvec->bv_len;
525}
526
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700527static int storvsc_device_configure(struct scsi_device *sdevice)
528{
529 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
530 STORVSC_MAX_IO_REQUESTS);
531
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700532 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
533
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700534 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
535
536 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700537
538 return 0;
539}
540
K. Y. Srinivasan49a3c7a2011-05-10 07:54:28 -0700541static void destroy_bounce_buffer(struct scatterlist *sgl,
542 unsigned int sg_count)
543{
544 int i;
545 struct page *page_buf;
546
547 for (i = 0; i < sg_count; i++) {
548 page_buf = sg_page((&sgl[i]));
549 if (page_buf != NULL)
550 __free_page(page_buf);
551 }
552
553 kfree(sgl);
554}
555
K. Y. Srinivasan3862ef32011-05-10 07:54:29 -0700556static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
557{
558 int i;
559
560 /* No need to check */
561 if (sg_count < 2)
562 return -1;
563
564 /* We have at least 2 sg entries */
565 for (i = 0; i < sg_count; i++) {
566 if (i == 0) {
567 /* make sure 1st one does not have hole */
568 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
569 return i;
570 } else if (i == sg_count - 1) {
571 /* make sure last one does not have hole */
572 if (sgl[i].offset != 0)
573 return i;
574 } else {
575 /* make sure no hole in the middle */
576 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
577 return i;
578 }
579 }
580 return -1;
581}
582
K. Y. Srinivasana9753cb2011-05-10 07:54:30 -0700583static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
584 unsigned int sg_count,
585 unsigned int len)
586{
587 int i;
588 int num_pages;
589 struct scatterlist *bounce_sgl;
590 struct page *page_buf;
591
592 num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
593
594 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
595 if (!bounce_sgl)
596 return NULL;
597
598 for (i = 0; i < num_pages; i++) {
599 page_buf = alloc_page(GFP_ATOMIC);
600 if (!page_buf)
601 goto cleanup;
602 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
603 }
604
605 return bounce_sgl;
606
607cleanup:
608 destroy_bounce_buffer(bounce_sgl, num_pages);
609 return NULL;
610}
611
K. Y. Srinivasan29fe2c92011-05-10 07:54:31 -0700612
613/* Assume the original sgl has enough room */
614static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
615 struct scatterlist *bounce_sgl,
616 unsigned int orig_sgl_count)
617{
618 int i;
619 int j = 0;
620 unsigned long src, dest;
621 unsigned int srclen, destlen, copylen;
622 unsigned int total_copied = 0;
623 unsigned long bounce_addr = 0;
624 unsigned long dest_addr = 0;
625 unsigned long flags;
626
627 local_irq_save(flags);
628
629 for (i = 0; i < orig_sgl_count; i++) {
630 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
631 KM_IRQ0) + orig_sgl[i].offset;
632 dest = dest_addr;
633 destlen = orig_sgl[i].length;
634
635 if (bounce_addr == 0)
636 bounce_addr =
637 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
638 KM_IRQ0);
639
640 while (destlen) {
641 src = bounce_addr + bounce_sgl[j].offset;
642 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
643
644 copylen = min(srclen, destlen);
645 memcpy((void *)dest, (void *)src, copylen);
646
647 total_copied += copylen;
648 bounce_sgl[j].offset += copylen;
649 destlen -= copylen;
650 dest += copylen;
651
652 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
653 /* full */
654 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
655 j++;
656
657 /* if we need to use another bounce buffer */
658 if (destlen || i != orig_sgl_count - 1)
659 bounce_addr =
660 (unsigned long)kmap_atomic(
661 sg_page((&bounce_sgl[j])), KM_IRQ0);
662 } else if (destlen == 0 && i == orig_sgl_count - 1) {
663 /* unmap the last bounce that is < PAGE_SIZE */
664 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
665 }
666 }
667
668 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
669 KM_IRQ0);
670 }
671
672 local_irq_restore(flags);
673
674 return total_copied;
675}
676
K. Y. Srinivasan6a8ff442011-05-10 07:54:32 -0700677
678/* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
679static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
680 struct scatterlist *bounce_sgl,
681 unsigned int orig_sgl_count)
682{
683 int i;
684 int j = 0;
685 unsigned long src, dest;
686 unsigned int srclen, destlen, copylen;
687 unsigned int total_copied = 0;
688 unsigned long bounce_addr = 0;
689 unsigned long src_addr = 0;
690 unsigned long flags;
691
692 local_irq_save(flags);
693
694 for (i = 0; i < orig_sgl_count; i++) {
695 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
696 KM_IRQ0) + orig_sgl[i].offset;
697 src = src_addr;
698 srclen = orig_sgl[i].length;
699
700 if (bounce_addr == 0)
701 bounce_addr =
702 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
703 KM_IRQ0);
704
705 while (srclen) {
706 /* assume bounce offset always == 0 */
707 dest = bounce_addr + bounce_sgl[j].length;
708 destlen = PAGE_SIZE - bounce_sgl[j].length;
709
710 copylen = min(srclen, destlen);
711 memcpy((void *)dest, (void *)src, copylen);
712
713 total_copied += copylen;
714 bounce_sgl[j].length += copylen;
715 srclen -= copylen;
716 src += copylen;
717
718 if (bounce_sgl[j].length == PAGE_SIZE) {
719 /* full..move to next entry */
720 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
721 j++;
722
723 /* if we need to use another bounce buffer */
724 if (srclen || i != orig_sgl_count - 1)
725 bounce_addr =
726 (unsigned long)kmap_atomic(
727 sg_page((&bounce_sgl[j])), KM_IRQ0);
728
729 } else if (srclen == 0 && i == orig_sgl_count - 1) {
730 /* unmap the last bounce that is < PAGE_SIZE */
731 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
732 }
733 }
734
735 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
736 }
737
738 local_irq_restore(flags);
739
740 return total_copied;
741}
742
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -0700743
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -0700744static int storvsc_remove(struct hv_device *dev)
745{
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -0700746 struct Scsi_Host *host = dev_get_drvdata(&dev->device);
747 struct hv_host_device *host_dev =
748 (struct hv_host_device *)host->hostdata;
749
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -0700750 scsi_remove_host(host);
751
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -0700752 scsi_host_put(host);
K. Y. Srinivasand36b0a02011-06-06 15:49:29 -0700753
K. Y. Srinivasan2935a402011-06-06 15:49:28 -0700754 storvsc_dev_remove(dev);
755 if (host_dev->request_pool) {
756 kmem_cache_destroy(host_dev->request_pool);
757 host_dev->request_pool = NULL;
758 }
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -0700759 return 0;
760}
761
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700762
763static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
764 sector_t capacity, int *info)
765{
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -0700766 sector_t nsect = capacity;
767 sector_t cylinders = nsect;
768 int heads, sectors_pt;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700769
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -0700770 /*
771 * We are making up these values; let us keep it simple.
772 */
773 heads = 0xff;
774 sectors_pt = 0x3f; /* Sectors per track */
775 sector_div(cylinders, heads * sectors_pt);
776 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
777 cylinders = 0xffff;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700778
779 info[0] = heads;
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -0700780 info[1] = sectors_pt;
781 info[2] = (int)cylinders;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700782
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -0700783 return 0;
784}
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -0700785
786static int storvsc_host_reset(struct hv_device *device)
787{
788 struct storvsc_device *stor_device;
789 struct hv_storvsc_request *request;
790 struct vstor_packet *vstor_packet;
791 int ret, t;
792
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -0700793
K. Y. Srinivasan1eaaddf2011-08-27 11:31:03 -0700794 stor_device = get_out_stor_device(device);
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -0700795 if (!stor_device)
K. Y. Srinivasanf3b74162011-08-25 09:49:08 -0700796 return -ENODEV;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -0700797
798 request = &stor_device->reset_request;
799 vstor_packet = &request->vstor_packet;
800
801 init_completion(&request->wait_event);
802
803 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
804 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
805 vstor_packet->vm_srb.path_id = stor_device->path_id;
806
807 ret = vmbus_sendpacket(device->channel, vstor_packet,
808 sizeof(struct vstor_packet),
809 (unsigned long)&stor_device->reset_request,
810 VM_PKT_DATA_INBAND,
811 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
812 if (ret != 0)
813 goto cleanup;
814
K. Y. Srinivasan46d2eb62011-06-16 13:16:36 -0700815 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -0700816 if (t == 0) {
817 ret = -ETIMEDOUT;
818 goto cleanup;
819 }
820
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -0700821
822 /*
823 * At this point, all outstanding requests in the adapter
824 * should have been flushed out and return to us
825 */
826
827cleanup:
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -0700828 return ret;
829}
830
831
K. Y. Srinivasan96e690be2011-05-10 07:54:38 -0700832/*
833 * storvsc_host_reset_handler - Reset the scsi HBA
834 */
835static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
836{
837 int ret;
838 struct hv_host_device *host_dev =
839 (struct hv_host_device *)scmnd->device->host->hostdata;
840 struct hv_device *dev = host_dev->dev;
841
K. Y. Srinivasan96e690be2011-05-10 07:54:38 -0700842 ret = storvsc_host_reset(dev);
843 if (ret != 0)
844 return ret;
845
K. Y. Srinivasan96e690be2011-05-10 07:54:38 -0700846 return ret;
847}
848
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700849
850/*
851 * storvsc_commmand_completion - Command completion processing
852 */
853static void storvsc_commmand_completion(struct hv_storvsc_request *request)
854{
855 struct storvsc_cmd_request *cmd_request =
856 (struct storvsc_cmd_request *)request->context;
857 struct scsi_cmnd *scmnd = cmd_request->cmd;
858 struct hv_host_device *host_dev =
859 (struct hv_host_device *)scmnd->device->host->hostdata;
860 void (*scsi_done_fn)(struct scsi_cmnd *);
861 struct scsi_sense_hdr sense_hdr;
862 struct vmscsi_request *vm_srb;
863
K. Y. Srinivasan3612ef92011-08-27 11:31:20 -0700864 vm_srb = &request->vstor_packet.vm_srb;
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700865 if (cmd_request->bounce_sgl_count) {
K. Y. Srinivasan3612ef92011-08-27 11:31:20 -0700866 if (vm_srb->data_in == READ_TYPE) {
867 copy_from_bounce_buffer(scsi_sglist(scmnd),
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700868 cmd_request->bounce_sgl,
869 scsi_sg_count(scmnd));
K. Y. Srinivasan3612ef92011-08-27 11:31:20 -0700870 destroy_bounce_buffer(cmd_request->bounce_sgl,
871 cmd_request->bounce_sgl_count);
872 }
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700873 }
874
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700875 scmnd->result = vm_srb->scsi_status;
876
877 if (scmnd->result) {
878 if (scsi_normalize_sense(scmnd->sense_buffer,
879 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
880 scsi_print_sense_hdr("storvsc", &sense_hdr);
881 }
882
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700883 scsi_set_resid(scmnd,
884 request->data_buffer.len -
885 vm_srb->data_transfer_length);
886
887 scsi_done_fn = scmnd->scsi_done;
888
889 scmnd->host_scribble = NULL;
890 scmnd->scsi_done = NULL;
891
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -0700892 scsi_done_fn(scmnd);
893
894 kmem_cache_free(host_dev->request_pool, cmd_request);
895}
896
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700897
898/*
899 * storvsc_queuecommand - Initiate command processing
900 */
901static int storvsc_queuecommand_lck(struct scsi_cmnd *scmnd,
902 void (*done)(struct scsi_cmnd *))
903{
904 int ret;
905 struct hv_host_device *host_dev =
906 (struct hv_host_device *)scmnd->device->host->hostdata;
907 struct hv_device *dev = host_dev->dev;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700908 struct hv_storvsc_request *request;
909 struct storvsc_cmd_request *cmd_request;
910 unsigned int request_size = 0;
911 int i;
912 struct scatterlist *sgl;
913 unsigned int sg_count = 0;
914 struct vmscsi_request *vm_srb;
915
916
917 /* If retrying, no need to prep the cmd */
918 if (scmnd->host_scribble) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700919
920 cmd_request =
921 (struct storvsc_cmd_request *)scmnd->host_scribble;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700922
923 goto retry_request;
924 }
925
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700926 scmnd->scsi_done = done;
927
928 request_size = sizeof(struct storvsc_cmd_request);
929
930 cmd_request = kmem_cache_zalloc(host_dev->request_pool,
931 GFP_ATOMIC);
932 if (!cmd_request) {
933 scmnd->scsi_done = NULL;
934 return SCSI_MLQUEUE_DEVICE_BUSY;
935 }
936
937 /* Setup the cmd request */
938 cmd_request->bounce_sgl_count = 0;
939 cmd_request->bounce_sgl = NULL;
940 cmd_request->cmd = scmnd;
941
942 scmnd->host_scribble = (unsigned char *)cmd_request;
943
944 request = &cmd_request->request;
945 vm_srb = &request->vstor_packet.vm_srb;
946
947
948 /* Build the SRB */
949 switch (scmnd->sc_data_direction) {
950 case DMA_TO_DEVICE:
951 vm_srb->data_in = WRITE_TYPE;
952 break;
953 case DMA_FROM_DEVICE:
954 vm_srb->data_in = READ_TYPE;
955 break;
956 default:
957 vm_srb->data_in = UNKNOWN_TYPE;
958 break;
959 }
960
961 request->on_io_completion = storvsc_commmand_completion;
962 request->context = cmd_request;/* scmnd; */
963
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700964 vm_srb->port_number = host_dev->port;
965 vm_srb->path_id = scmnd->device->channel;
966 vm_srb->target_id = scmnd->device->id;
967 vm_srb->lun = scmnd->device->lun;
968
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -0700969 vm_srb->cdb_length = scmnd->cmd_len;
970
971 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
972
973 request->sense_buffer = scmnd->sense_buffer;
974
975
976 request->data_buffer.len = scsi_bufflen(scmnd);
977 if (scsi_sg_count(scmnd)) {
978 sgl = (struct scatterlist *)scsi_sglist(scmnd);
979 sg_count = scsi_sg_count(scmnd);
980
981 /* check if we need to bounce the sgl */
982 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
983 cmd_request->bounce_sgl =
984 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
985 scsi_bufflen(scmnd));
986 if (!cmd_request->bounce_sgl) {
987 scmnd->scsi_done = NULL;
988 scmnd->host_scribble = NULL;
989 kmem_cache_free(host_dev->request_pool,
990 cmd_request);
991
992 return SCSI_MLQUEUE_HOST_BUSY;
993 }
994
995 cmd_request->bounce_sgl_count =
996 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
997 PAGE_SHIFT;
998
K. Y. Srinivasanfa23b8c2011-08-27 11:31:21 -0700999 if (vm_srb->data_in == WRITE_TYPE)
1000 copy_to_bounce_buffer(sgl,
1001 cmd_request->bounce_sgl,
1002 scsi_sg_count(scmnd));
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001003
1004 sgl = cmd_request->bounce_sgl;
1005 sg_count = cmd_request->bounce_sgl_count;
1006 }
1007
1008 request->data_buffer.offset = sgl[0].offset;
1009
1010 for (i = 0; i < sg_count; i++)
1011 request->data_buffer.pfn_array[i] =
1012 page_to_pfn(sg_page((&sgl[i])));
1013
1014 } else if (scsi_sglist(scmnd)) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001015 request->data_buffer.offset =
1016 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
1017 request->data_buffer.pfn_array[0] =
1018 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
1019 }
1020
1021retry_request:
1022 /* Invokes the vsc to start an IO */
K. Y. Srinivasan636f0fd2011-05-10 07:54:50 -07001023 ret = storvsc_do_io(dev, &cmd_request->request);
1024
K. Y. Srinivasand2598f02011-08-25 09:48:58 -07001025 if (ret == -EAGAIN) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001026 /* no more space */
1027
1028 if (cmd_request->bounce_sgl_count) {
1029 /*
1030 * FIXME: We can optimize on writes by just skipping
1031 * this
1032 */
1033 copy_from_bounce_buffer(scsi_sglist(scmnd),
1034 cmd_request->bounce_sgl,
1035 scsi_sg_count(scmnd));
1036 destroy_bounce_buffer(cmd_request->bounce_sgl,
1037 cmd_request->bounce_sgl_count);
1038 }
1039
1040 kmem_cache_free(host_dev->request_pool, cmd_request);
1041
1042 scmnd->scsi_done = NULL;
1043 scmnd->host_scribble = NULL;
1044
1045 ret = SCSI_MLQUEUE_DEVICE_BUSY;
1046 }
1047
1048 return ret;
1049}
1050
1051static DEF_SCSI_QCMD(storvsc_queuecommand)
1052
Hank Janssenbef4a342009-07-13 16:01:31 -07001053
Bill Pemberton454f18a2009-07-27 16:47:24 -04001054/* Scsi driver */
Hank Janssenbef4a342009-07-13 16:01:31 -07001055static struct scsi_host_template scsi_driver = {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001056 .module = THIS_MODULE,
1057 .name = "storvsc_host_t",
1058 .bios_param = storvsc_get_chs,
1059 .queuecommand = storvsc_queuecommand,
1060 .eh_host_reset_handler = storvsc_host_reset_handler,
1061 .slave_alloc = storvsc_device_alloc,
1062 .slave_configure = storvsc_device_configure,
1063 .cmd_per_lun = 1,
1064 /* 64 max_queue * 1 target */
Lars Lindley0686e4f2010-03-11 23:51:23 +01001065 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001066 .this_id = -1,
Bill Pemberton454f18a2009-07-27 16:47:24 -04001067 /* no use setting to 0 since ll_blk_rw reset it to 1 */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001068 /* currently 32 */
1069 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
1070 /*
1071 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
1072 * into 1 sg element. If set, we must limit the max_segment_size to
1073 * PAGE_SIZE, otherwise we may get 1 sg element that represents
1074 * multiple
1075 */
Bill Pemberton454f18a2009-07-27 16:47:24 -04001076 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001077 .use_clustering = ENABLE_CLUSTERING,
Bill Pemberton454f18a2009-07-27 16:47:24 -04001078 /* Make sure we dont get a sg segment crosses a page boundary */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001079 .dma_boundary = PAGE_SIZE-1,
Hank Janssenbef4a342009-07-13 16:01:31 -07001080};
1081
K. Y. Srinivasan21e37742011-08-27 11:31:18 -07001082/*
1083 * The storvsc_probe function assumes that the IDE guid
1084 * is the second entry.
1085 */
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001086static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001087 /* SCSI guid */
1088 { VMBUS_DEVICE(0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
1089 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f) },
K. Y. Srinivasan21e37742011-08-27 11:31:18 -07001090 /* IDE guid */
1091 { VMBUS_DEVICE(0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
1092 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5) },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001093 { },
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001094};
Hank Janssenbef4a342009-07-13 16:01:31 -07001095
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001096MODULE_DEVICE_TABLE(vmbus, id_table);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001097
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001098
Hank Janssen3e189512010-03-04 22:11:00 +00001099/*
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001100 * storvsc_probe - Add a new device for this driver
1101 */
1102
1103static int storvsc_probe(struct hv_device *device)
1104{
1105 int ret;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001106 struct Scsi_Host *host;
1107 struct hv_host_device *host_dev;
1108 struct storvsc_device_info device_info;
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001109 bool dev_is_ide;
1110 int path = 0;
1111 int target = 0;
1112
K. Y. Srinivasan21e37742011-08-27 11:31:18 -07001113 if (!memcmp(&device->dev_type.b, id_table[1].guid, sizeof(uuid_le)))
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001114 dev_is_ide = true;
1115 else
1116 dev_is_ide = false;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001117
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001118 host = scsi_host_alloc(&scsi_driver,
1119 sizeof(struct hv_host_device));
1120 if (!host)
1121 return -ENOMEM;
1122
1123 dev_set_drvdata(&device->device, host);
1124
1125 host_dev = (struct hv_host_device *)host->hostdata;
1126 memset(host_dev, 0, sizeof(struct hv_host_device));
1127
1128 host_dev->port = host->host_no;
1129 host_dev->dev = device;
1130
1131 host_dev->request_pool =
1132 kmem_cache_create(dev_name(&device->device),
1133 sizeof(struct storvsc_cmd_request), 0,
1134 SLAB_HWCACHE_ALIGN, NULL);
1135
1136 if (!host_dev->request_pool) {
1137 scsi_host_put(host);
1138 return -ENOMEM;
1139 }
1140
1141 device_info.port_number = host->host_no;
K. Y. Srinivasanfa4d1232011-05-10 07:56:01 -07001142 device_info.ring_buffer_size = storvsc_ringbuffer_size;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001143 /* Call to the vsc driver to add the device */
K. Y. Srinivasan58f1f5c2011-05-10 07:54:49 -07001144 ret = storvsc_dev_add(device, (void *)&device_info);
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001145
1146 if (ret != 0) {
1147 kmem_cache_destroy(host_dev->request_pool);
1148 scsi_host_put(host);
K. Y. Srinivasan8e854682011-08-25 09:49:09 -07001149 return -ENODEV;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001150 }
1151
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001152 if (dev_is_ide)
1153 storvsc_get_ide_info(device, &target, &path);
1154
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001155 host_dev->path = device_info.path_id;
1156 host_dev->target = device_info.target_id;
1157
1158 /* max # of devices per target */
1159 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
1160 /* max # of targets per channel */
1161 host->max_id = STORVSC_MAX_TARGETS;
1162 /* max # of channels */
1163 host->max_channel = STORVSC_MAX_CHANNELS - 1;
1164
1165 /* Register the HBA and start the scsi bus scan */
1166 ret = scsi_add_host(host, &device->device);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001167 if (ret != 0)
1168 goto err_out;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001169
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001170 if (!dev_is_ide) {
1171 scsi_scan_host(host);
1172 return 0;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001173 }
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001174 ret = scsi_add_device(host, 0, target, 0);
1175 if (ret) {
1176 scsi_remove_host(host);
1177 goto err_out;
1178 }
1179 return 0;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001180
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001181err_out:
1182 storvsc_dev_remove(device);
1183 kmem_cache_destroy(host_dev->request_pool);
1184 scsi_host_put(host);
1185 return -ENODEV;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001186}
1187
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -07001188/* The one and only one */
1189
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -07001190static struct hv_driver storvsc_drv = {
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001191 .name = "storvsc",
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001192 .id_table = id_table,
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -07001193 .probe = storvsc_probe,
1194 .remove = storvsc_remove,
K. Y. Srinivasan39ae6fa2011-05-10 07:54:46 -07001195};
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -07001196
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -07001197static int __init storvsc_drv_init(void)
Hank Janssenbef4a342009-07-13 16:01:31 -07001198{
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001199 u32 max_outstanding_req_per_channel;
1200
1201 /*
1202 * Divide the ring buffer data size (which is 1 page less
1203 * than the ring buffer size since that page is reserved for
1204 * the ring buffer indices) by the max request size (which is
1205 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
1206 */
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001207 max_outstanding_req_per_channel =
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001208 ((storvsc_ringbuffer_size - PAGE_SIZE) /
1209 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
1210 sizeof(struct vstor_packet) + sizeof(u64),
1211 sizeof(u64)));
Hank Janssenbef4a342009-07-13 16:01:31 -07001212
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001213 if (max_outstanding_req_per_channel <
K. Y. Srinivasanf8feed02011-05-10 07:54:24 -07001214 STORVSC_MAX_IO_REQUESTS)
K. Y. Srinivasanb06efc12011-08-25 09:49:10 -07001215 return -EINVAL;
Hank Janssenbef4a342009-07-13 16:01:31 -07001216
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001217 return vmbus_driver_register(&storvsc_drv);
Hank Janssenbef4a342009-07-13 16:01:31 -07001218}
1219
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -07001220static void __exit storvsc_drv_exit(void)
Hank Janssenbef4a342009-07-13 16:01:31 -07001221{
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001222 vmbus_driver_unregister(&storvsc_drv);
Hank Janssenbef4a342009-07-13 16:01:31 -07001223}
1224
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001225MODULE_LICENSE("GPL");
Hank Janssen26c14cc2010-02-11 23:02:42 +00001226MODULE_VERSION(HV_DRV_VERSION);
Stephen Hemminger3afc7cc32010-05-06 21:44:45 -07001227MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -07001228module_init(storvsc_drv_init);
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -07001229module_exit(storvsc_drv_exit);