Staging: hv: storvsc: Cleanup storvsc_drv.c after adding the contents of storvsc.c
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / hv / storvsc_drv.c
CommitLineData
bef4a34a 1/*
bef4a34a
HJ
2 * 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>
972621c9 20 * K. Y. Srinivasan <kys@microsoft.com>
bef4a34a 21 */
a1be1706
S
22
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>
bef4a34a 29#include <linux/init.h>
5a0e3ad6 30#include <linux/slab.h>
bef4a34a
HJ
31#include <linux/module.h>
32#include <linux/device.h>
bef4a34a
HJ
33#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>
bef4a34a 40#include <scsi/scsi_dbg.h>
3f335ea2
S
41
42#include "hyperv.h"
cdee1504 43#include "hyperv_storage.h"
bef4a34a 44
8dcf37d4 45
a1be1706 46static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
8dcf37d4 47
a1be1706
S
48module_param(storvsc_ringbuffer_size, int, S_IRUGO);
49MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
8dcf37d4 50
a1be1706
S
51struct 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};
8dcf37d4
S
68
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
bd1f5d6a
S
498static 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
bef4a34a 509
5b60acee
S
510static 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
a1ebfeae
S
520static 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
419f2d03
S
527static int storvsc_device_configure(struct scsi_device *sdevice)
528{
529 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
530 STORVSC_MAX_IO_REQUESTS);
531
419f2d03
S
532 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
533
419f2d03
S
534 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
535
536 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
419f2d03
S
537
538 return 0;
539}
540
49a3c7a9
S
541static 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
3862ef3e
S
556static 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
a9753cbd
S
583static 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
29fe2c9b
S
612
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
6a8ff44b
S
677
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
5c5c0234 743
5c5c0234
S
744static int storvsc_remove(struct hv_device *dev)
745{
5c5c0234
S
746 struct Scsi_Host *host = dev_get_drvdata(&dev->device);
747 struct hv_host_device *host_dev =
748 (struct hv_host_device *)host->hostdata;
749
2935a407
S
750 scsi_remove_host(host);
751
2935a407 752 scsi_host_put(host);
d36b0a03 753
6cdc57c0 754 storvsc_dev_remove(dev);
5c5c0234
S
755 if (host_dev->request_pool) {
756 kmem_cache_destroy(host_dev->request_pool);
757 host_dev->request_pool = NULL;
758 }
5c5c0234
S
759 return 0;
760}
761
62838ce2
S
762
763static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
764 sector_t capacity, int *info)
765{
5326fd5c
S
766 sector_t nsect = capacity;
767 sector_t cylinders = nsect;
768 int heads, sectors_pt;
62838ce2 769
5326fd5c
S
770 /*
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;
62838ce2
S
778
779 info[0] = heads;
5326fd5c
S
780 info[1] = sectors_pt;
781 info[2] = (int)cylinders;
62838ce2 782
62838ce2
S
783 return 0;
784}
aa3d789e
S
785
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
aa3d789e 793
1eaaddf9 794 stor_device = get_out_stor_device(device);
aa3d789e 795 if (!stor_device)
f3b74165 796 return -ENODEV;
aa3d789e
S
797
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
46d2eb6d 815 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
aa3d789e
S
816 if (t == 0) {
817 ret = -ETIMEDOUT;
818 goto cleanup;
819 }
820
aa3d789e
S
821
822 /*
823 * At this point, all outstanding requests in the adapter
824 * should have been flushed out and return to us
825 */
826
827cleanup:
aa3d789e
S
828 return ret;
829}
830
831
96e690be
S
832/*
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
96e690be
S
842 ret = storvsc_host_reset(dev);
843 if (ret != 0)
844 return ret;
845
96e690be
S
846 return ret;
847}
848
8a411bad
S
849
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
3612ef99 864 vm_srb = &request->vstor_packet.vm_srb;
8a411bad 865 if (cmd_request->bounce_sgl_count) {
3612ef99
S
866 if (vm_srb->data_in == READ_TYPE) {
867 copy_from_bounce_buffer(scsi_sglist(scmnd),
8a411bad
S
868 cmd_request->bounce_sgl,
869 scsi_sg_count(scmnd));
3612ef99
S
870 destroy_bounce_buffer(cmd_request->bounce_sgl,
871 cmd_request->bounce_sgl_count);
872 }
8a411bad
S
873 }
874
8a411bad
S
875 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
8a411bad
S
883 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
8a411bad
S
892 scsi_done_fn(scmnd);
893
894 kmem_cache_free(host_dev->request_pool, cmd_request);
895}
896
c5b463ae
S
897
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;
c5b463ae
S
908 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) {
c5b463ae
S
919
920 cmd_request =
921 (struct storvsc_cmd_request *)scmnd->host_scribble;
c5b463ae
S
922
923 goto retry_request;
924 }
925
c5b463ae
S
926 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
c5b463ae
S
964 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
c5b463ae
S
969 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
fa23b8c7
S
999 if (vm_srb->data_in == WRITE_TYPE)
1000 copy_to_bounce_buffer(sgl,
1001 cmd_request->bounce_sgl,
1002 scsi_sg_count(scmnd));
c5b463ae
S
1003
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)) {
c5b463ae
S
1015 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 */
636f0fd1
S
1023 ret = storvsc_do_io(dev, &cmd_request->request);
1024
d2598f01 1025 if (ret == -EAGAIN) {
c5b463ae
S
1026 /* 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
bef4a34a 1053
454f18a9 1054/* Scsi driver */
bef4a34a 1055static struct scsi_host_template scsi_driver = {
ff568d3a
GKH
1056 .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 */
0686e4f4 1065 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
ff568d3a 1066 .this_id = -1,
454f18a9 1067 /* no use setting to 0 since ll_blk_rw reset it to 1 */
ff568d3a
GKH
1068 /* 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 */
454f18a9 1076 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
ff568d3a 1077 .use_clustering = ENABLE_CLUSTERING,
454f18a9 1078 /* Make sure we dont get a sg segment crosses a page boundary */
ff568d3a 1079 .dma_boundary = PAGE_SIZE-1,
bef4a34a
HJ
1080};
1081
21e37742
S
1082/*
1083 * The storvsc_probe function assumes that the IDE guid
1084 * is the second entry.
1085 */
d847b5fe 1086static const struct hv_vmbus_device_id id_table[] = {
c45cf2d4
GKH
1087 /* SCSI guid */
1088 { VMBUS_DEVICE(0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
1089 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f) },
21e37742
S
1090 /* IDE guid */
1091 { VMBUS_DEVICE(0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
1092 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5) },
c45cf2d4 1093 { },
d847b5fe 1094};
bef4a34a 1095
d847b5fe 1096MODULE_DEVICE_TABLE(vmbus, id_table);
bd1f5d6a 1097
bd1f5d6a 1098
3e189519 1099/*
ff568d3a
GKH
1100 * storvsc_probe - Add a new device for this driver
1101 */
f5c78872 1102
9efd21e1 1103static int storvsc_probe(struct hv_device *device)
bef4a34a 1104{
ff568d3a 1105 int ret;
bef4a34a 1106 struct Scsi_Host *host;
795b613d 1107 struct hv_host_device *host_dev;
9f0c7d2c 1108 struct storvsc_device_info device_info;
bd1f5d6a
S
1109 bool dev_is_ide;
1110 int path = 0;
1111 int target = 0;
1112
21e37742 1113 if (!memcmp(&device->dev_type.b, id_table[1].guid, sizeof(uuid_le)))
bd1f5d6a
S
1114 dev_is_ide = true;
1115 else
1116 dev_is_ide = false;
bef4a34a 1117
ff568d3a 1118 host = scsi_host_alloc(&scsi_driver,
972621c9 1119 sizeof(struct hv_host_device));
f8feed06 1120 if (!host)
bef4a34a 1121 return -ENOMEM;
bef4a34a 1122
9efd21e1 1123 dev_set_drvdata(&device->device, host);
bef4a34a 1124
795b613d
S
1125 host_dev = (struct hv_host_device *)host->hostdata;
1126 memset(host_dev, 0, sizeof(struct hv_host_device));
bef4a34a 1127
795b613d 1128 host_dev->port = host->host_no;
97c15296 1129 host_dev->dev = device;
bef4a34a 1130
795b613d 1131 host_dev->request_pool =
9efd21e1 1132 kmem_cache_create(dev_name(&device->device),
1e05d88e 1133 sizeof(struct storvsc_cmd_request), 0,
ff568d3a
GKH
1134 SLAB_HWCACHE_ALIGN, NULL);
1135
795b613d 1136 if (!host_dev->request_pool) {
bef4a34a 1137 scsi_host_put(host);
bef4a34a
HJ
1138 return -ENOMEM;
1139 }
1140
8a046024 1141 device_info.port_number = host->host_no;
fa4d123a 1142 device_info.ring_buffer_size = storvsc_ringbuffer_size;
454f18a9 1143 /* Call to the vsc driver to add the device */
58f1f5cb 1144 ret = storvsc_dev_add(device, (void *)&device_info);
9efd21e1 1145
ff568d3a 1146 if (ret != 0) {
795b613d 1147 kmem_cache_destroy(host_dev->request_pool);
bef4a34a 1148 scsi_host_put(host);
8e854680 1149 return -ENODEV;
bef4a34a
HJ
1150 }
1151
bd1f5d6a
S
1152 if (dev_is_ide)
1153 storvsc_get_ide_info(device, &target, &path);
1154
795b613d
S
1155 host_dev->path = device_info.path_id;
1156 host_dev->target = device_info.target_id;
bef4a34a 1157
ff568d3a
GKH
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;
bef4a34a 1164
454f18a9 1165 /* Register the HBA and start the scsi bus scan */
9efd21e1 1166 ret = scsi_add_host(host, &device->device);
bd1f5d6a
S
1167 if (ret != 0)
1168 goto err_out;
bef4a34a 1169
bd1f5d6a
S
1170 if (!dev_is_ide) {
1171 scsi_scan_host(host);
1172 return 0;
1173 }
1174 ret = scsi_add_device(host, 0, target, 0);
1175 if (ret) {
1176 scsi_remove_host(host);
1177 goto err_out;
bef4a34a 1178 }
bd1f5d6a 1179 return 0;
bef4a34a 1180
bd1f5d6a
S
1181err_out:
1182 storvsc_dev_remove(device);
1183 kmem_cache_destroy(host_dev->request_pool);
1184 scsi_host_put(host);
1185 return -ENODEV;
bef4a34a
HJ
1186}
1187
7bd05b91
S
1188/* The one and only one */
1189
40bf63ed 1190static struct hv_driver storvsc_drv = {
768fa219 1191 .name = "storvsc",
d847b5fe 1192 .id_table = id_table,
40bf63ed
S
1193 .probe = storvsc_probe,
1194 .remove = storvsc_remove,
39ae6fae 1195};
7bd05b91 1196
d9bbae83 1197static int __init storvsc_drv_init(void)
f5c78872 1198{
01415ab3
S
1199 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 */
01415ab3 1207 max_outstanding_req_per_channel =
768fa219
GKH
1208 ((storvsc_ringbuffer_size - PAGE_SIZE) /
1209 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
1210 sizeof(struct vstor_packet) + sizeof(u64),
1211 sizeof(u64)));
f5c78872 1212
01415ab3 1213 if (max_outstanding_req_per_channel <
f5c78872 1214 STORVSC_MAX_IO_REQUESTS)
b06efc19 1215 return -EINVAL;
f5c78872 1216
768fa219 1217 return vmbus_driver_register(&storvsc_drv);
f5c78872
S
1218}
1219
c63ba9e1 1220static void __exit storvsc_drv_exit(void)
f5c78872 1221{
768fa219 1222 vmbus_driver_unregister(&storvsc_drv);
f5c78872
S
1223}
1224
ff568d3a 1225MODULE_LICENSE("GPL");
26c14cc1 1226MODULE_VERSION(HV_DRV_VERSION);
3afc7cc3 1227MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
d9bbae83 1228module_init(storvsc_drv_init);
c63ba9e1 1229module_exit(storvsc_drv_exit);