Staging: hv: storvsc: Miscellaneous cleanup of storvsc driver
[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>
f0d79fe9 24#include <linux/wait.h>
a1be1706
S
25#include <linux/sched.h>
26#include <linux/completion.h>
27#include <linux/string.h>
28#include <linux/mm.h>
29#include <linux/delay.h>
bef4a34a 30#include <linux/init.h>
5a0e3ad6 31#include <linux/slab.h>
bef4a34a
HJ
32#include <linux/module.h>
33#include <linux/device.h>
46a97191 34#include <linux/hyperv.h>
4e03e697 35#include <linux/mempool.h>
bef4a34a
HJ
36#include <scsi/scsi.h>
37#include <scsi/scsi_cmnd.h>
38#include <scsi/scsi_host.h>
39#include <scsi/scsi_device.h>
40#include <scsi/scsi_tcq.h>
41#include <scsi/scsi_eh.h>
42#include <scsi/scsi_devinfo.h>
bef4a34a 43#include <scsi/scsi_dbg.h>
3f335ea2 44
f0d79fe9 45
c649114a
S
46/*
47 * We setup a mempool to allocate request structures for this driver
48 * on a per-lun basis. The following define specifies the number of
49 * elements in the pool.
50 */
51
4e03e697 52#define STORVSC_MIN_BUF_NR 64
c649114a 53static int storvsc_ringbuffer_size = (20 * PAGE_SIZE);
c1b3d067
S
54
55module_param(storvsc_ringbuffer_size, int, S_IRUGO);
56MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
57
f0d79fe9 58
09f0355f
S
59/*
60 * Major/minor macros. Minor version is in LSB, meaning that earlier flat
61 * version numbers will be interpreted as "0.x" (i.e., 1 becomes 0.1).
62 */
63
f0d79fe9
S
64#define VMSTOR_PROTOCOL_MAJOR(VERSION_) (((VERSION_) >> 8) & 0xff)
65#define VMSTOR_PROTOCOL_MINOR(VERSION_) (((VERSION_)) & 0xff)
66#define VMSTOR_PROTOCOL_VERSION(MAJOR_, MINOR_) ((((MAJOR_) & 0xff) << 8) | \
67 (((MINOR_) & 0xff)))
f0d79fe9 68
09f0355f
S
69/*
70 * Version history:
71 * V1 Beta: 0.1
72 * V1 RC < 2008/1/31: 1.0
73 * V1 RC > 2008/1/31: 2.0
74 * Win7: 4.2
75 */
76
2b9525f5 77#define VMSTOR_PROTOCOL_VERSION_CURRENT VMSTOR_PROTOCOL_VERSION(4, 2)
f0d79fe9
S
78
79
80
81
09f0355f
S
82/*
83 * This will get replaced with the max transfer length that is possible on
84 * the host adapter.
85 * The max transfer length will be published when we offer a vmbus channel.
86 */
87
f0d79fe9
S
88#define MAX_TRANSFER_LENGTH 0x40000
89#define DEFAULT_PACKET_SIZE (sizeof(struct vmdata_gpa_direct) + \
90 sizeof(struct vstor_packet) + \
91 sizesizeof(u64) * (MAX_TRANSFER_LENGTH / PAGE_SIZE)))
92
93
94/* Packet structure describing virtual storage requests. */
95enum vstor_packet_operation {
96 VSTOR_OPERATION_COMPLETE_IO = 1,
97 VSTOR_OPERATION_REMOVE_DEVICE = 2,
98 VSTOR_OPERATION_EXECUTE_SRB = 3,
99 VSTOR_OPERATION_RESET_LUN = 4,
100 VSTOR_OPERATION_RESET_ADAPTER = 5,
101 VSTOR_OPERATION_RESET_BUS = 6,
102 VSTOR_OPERATION_BEGIN_INITIALIZATION = 7,
103 VSTOR_OPERATION_END_INITIALIZATION = 8,
104 VSTOR_OPERATION_QUERY_PROTOCOL_VERSION = 9,
105 VSTOR_OPERATION_QUERY_PROPERTIES = 10,
2b9525f5
S
106 VSTOR_OPERATION_ENUMERATE_BUS = 11,
107 VSTOR_OPERATION_MAXIMUM = 11
f0d79fe9
S
108};
109
110/*
111 * Platform neutral description of a scsi request -
112 * this remains the same across the write regardless of 32/64 bit
113 * note: it's patterned off the SCSI_PASS_THROUGH structure
114 */
115#define CDB16GENERIC_LENGTH 0x10
116
117#ifndef SENSE_BUFFER_SIZE
118#define SENSE_BUFFER_SIZE 0x12
119#endif
120
121#define MAX_DATA_BUF_LEN_WITH_PADDING 0x14
122
123struct vmscsi_request {
c649114a
S
124 u16 length;
125 u8 srb_status;
126 u8 scsi_status;
f0d79fe9 127
c649114a
S
128 u8 port_number;
129 u8 path_id;
130 u8 target_id;
131 u8 lun;
f0d79fe9 132
c649114a
S
133 u8 cdb_length;
134 u8 sense_info_length;
135 u8 data_in;
136 u8 reserved;
f0d79fe9 137
c649114a 138 u32 data_transfer_length;
f0d79fe9
S
139
140 union {
c649114a
S
141 u8 cdb[CDB16GENERIC_LENGTH];
142 u8 sense_data[SENSE_BUFFER_SIZE];
143 u8 reserved_array[MAX_DATA_BUF_LEN_WITH_PADDING];
f0d79fe9
S
144 };
145} __attribute((packed));
146
147
148/*
149 * This structure is sent during the intialization phase to get the different
150 * properties of the channel.
151 */
152struct vmstorage_channel_properties {
c649114a
S
153 u16 protocol_version;
154 u8 path_id;
155 u8 target_id;
f0d79fe9
S
156
157 /* Note: port number is only really known on the client side */
c649114a
S
158 u32 port_number;
159 u32 flags;
160 u32 max_transfer_bytes;
f0d79fe9 161
c649114a
S
162 /*
163 * This id is unique for each channel and will correspond with
164 * vendor specific data in the inquiry data.
165 */
166
167 u64 unique_id;
f0d79fe9
S
168} __packed;
169
170/* This structure is sent during the storage protocol negotiations. */
171struct vmstorage_protocol_version {
172 /* Major (MSW) and minor (LSW) version numbers. */
173 unsigned short major_minor;
174
175 /*
176 * Revision number is auto-incremented whenever this file is changed
177 * (See FILL_VMSTOR_REVISION macro above). Mismatch does not
178 * definitely indicate incompatibility--but it does indicate mismatched
179 * builds.
c649114a 180 * This is only used on the windows side. Just set it to 0.
f0d79fe9
S
181 */
182 unsigned short revision;
183} __packed;
184
185/* Channel Property Flags */
186#define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
187#define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
188
189struct vstor_packet {
190 /* Requested operation type */
191 enum vstor_packet_operation operation;
192
193 /* Flags - see below for values */
c649114a 194 u32 flags;
f0d79fe9
S
195
196 /* Status of the request returned from the server side. */
c649114a 197 u32 status;
f0d79fe9
S
198
199 /* Data payload area */
200 union {
201 /*
202 * Structure used to forward SCSI commands from the
203 * client to the server.
204 */
205 struct vmscsi_request vm_srb;
206
207 /* Structure used to query channel properties. */
208 struct vmstorage_channel_properties storage_channel_properties;
209
210 /* Used during version negotiations. */
211 struct vmstorage_protocol_version version;
212 };
213} __packed;
214
f0d79fe9 215/*
09f0355f
S
216 * Packet Flags:
217 *
f0d79fe9
S
218 * This flag indicates that the server should send back a completion for this
219 * packet.
220 */
09f0355f 221
f0d79fe9
S
222#define REQUEST_COMPLETION_FLAG 0x1
223
f0d79fe9
S
224#define STORVSC_MAX_IO_REQUESTS 128
225
226/*
227 * In Hyper-V, each port/path/target maps to 1 scsi host adapter. In
228 * reality, the path/target is not used (ie always set to 0) so our
229 * scsi host adapter essentially has 1 bus with 1 target that contains
230 * up to 256 luns.
231 */
232#define STORVSC_MAX_LUNS_PER_TARGET 64
233#define STORVSC_MAX_TARGETS 1
234#define STORVSC_MAX_CHANNELS 1
cf55f4a8 235#define STORVSC_MAX_CMD_LEN 16
f0d79fe9 236
f0d79fe9
S
237/* Matches Windows-end */
238enum storvsc_request_type {
c649114a 239 WRITE_TYPE = 0,
f0d79fe9
S
240 READ_TYPE,
241 UNKNOWN_TYPE,
242};
243
16046320
S
244/*
245 * SRB status codes and masks; a subset of the codes used here.
246 */
247
248#define SRB_STATUS_AUTOSENSE_VALID 0x80
249#define SRB_STATUS_INVALID_LUN 0x20
250#define SRB_STATUS_SUCCESS 0x01
251#define SRB_STATUS_ERROR 0x04
252
253
f0d79fe9
S
254
255struct hv_storvsc_request {
f0d79fe9
S
256 struct hv_device *device;
257
258 /* Synchronize the request/response if needed */
259 struct completion wait_event;
260
261 unsigned char *sense_buffer;
93a1bf4d 262 struct storvsc_cmd_request *cmd;
f0d79fe9
S
263 struct hv_multipage_buffer data_buffer;
264
265 struct vstor_packet vstor_packet;
266};
267
268
f0d79fe9
S
269/* A storvsc device is a device object that contains a vmbus channel */
270struct storvsc_device {
271 struct hv_device *device;
272
273 bool destroy;
274 bool drain_notify;
275 atomic_t num_outstanding_req;
cd654ea1 276 struct Scsi_Host *host;
f0d79fe9
S
277
278 wait_queue_head_t waiting_to_drain;
279
280 /*
281 * Each unique Port/Path/Target represents 1 channel ie scsi
282 * controller. In reality, the pathid, targetid is always 0
283 * and the port is set by us
284 */
285 unsigned int port_number;
286 unsigned char path_id;
287 unsigned char target_id;
288
289 /* Used for vsc/vsp channel reset process */
290 struct hv_storvsc_request init_request;
291 struct hv_storvsc_request reset_request;
292};
293
ce3e301c 294struct stor_mem_pools {
c1b3d067 295 struct kmem_cache *request_pool;
4e03e697 296 mempool_t *request_mempool;
ce3e301c
S
297};
298
299struct hv_host_device {
300 struct hv_device *dev;
c1b3d067
S
301 unsigned int port;
302 unsigned char path;
303 unsigned char target;
304};
305
306struct storvsc_cmd_request {
307 struct list_head entry;
308 struct scsi_cmnd *cmd;
309
310 unsigned int bounce_sgl_count;
311 struct scatterlist *bounce_sgl;
312
313 struct hv_storvsc_request request;
314};
f0d79fe9 315
12675799
S
316struct storvsc_scan_work {
317 struct work_struct work;
318 struct Scsi_Host *host;
319 uint lun;
320};
321
322static void storvsc_bus_scan(struct work_struct *work)
323{
324 struct storvsc_scan_work *wrk;
325 int id, order_id;
326
327 wrk = container_of(work, struct storvsc_scan_work, work);
328 for (id = 0; id < wrk->host->max_id; ++id) {
329 if (wrk->host->reverse_ordering)
330 order_id = wrk->host->max_id - id - 1;
331 else
332 order_id = id;
333
334 scsi_scan_target(&wrk->host->shost_gendev, 0,
335 order_id, SCAN_WILD_CARD, 1);
336 }
337 kfree(wrk);
338}
339
b4017319
S
340static void storvsc_remove_lun(struct work_struct *work)
341{
342 struct storvsc_scan_work *wrk;
343 struct scsi_device *sdev;
344
345 wrk = container_of(work, struct storvsc_scan_work, work);
346 if (!scsi_host_get(wrk->host))
347 goto done;
348
349 sdev = scsi_device_lookup(wrk->host, 0, 0, wrk->lun);
350
351 if (sdev) {
352 scsi_remove_device(sdev);
353 scsi_device_put(sdev);
354 }
355 scsi_host_put(wrk->host);
356
357done:
358 kfree(wrk);
359}
360
a8c18c57
S
361/*
362 * We can get incoming messages from the host that are not in response to
363 * messages that we have sent out. An example of this would be messages
364 * received by the guest to notify dynamic addition/removal of LUNs. To
365 * deal with potential race conditions where the driver may be in the
366 * midst of being unloaded when we might receive an unsolicited message
367 * from the host, we have implemented a mechanism to gurantee sequential
368 * consistency:
369 *
370 * 1) Once the device is marked as being destroyed, we will fail all
371 * outgoing messages.
372 * 2) We permit incoming messages when the device is being destroyed,
373 * only to properly account for messages already sent out.
374 */
375
f0d79fe9
S
376static inline struct storvsc_device *get_out_stor_device(
377 struct hv_device *device)
378{
379 struct storvsc_device *stor_device;
380
cd654ea1 381 stor_device = hv_get_drvdata(device);
f0d79fe9
S
382
383 if (stor_device && stor_device->destroy)
384 stor_device = NULL;
385
386 return stor_device;
387}
388
389
390static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
391{
392 dev->drain_notify = true;
393 wait_event(dev->waiting_to_drain,
394 atomic_read(&dev->num_outstanding_req) == 0);
395 dev->drain_notify = false;
396}
bef4a34a 397
8dcf37d4
S
398static inline struct storvsc_device *get_in_stor_device(
399 struct hv_device *device)
400{
401 struct storvsc_device *stor_device;
8dcf37d4 402
cd654ea1 403 stor_device = hv_get_drvdata(device);
8dcf37d4
S
404
405 if (!stor_device)
406 goto get_in_err;
407
408 /*
409 * If the device is being destroyed; allow incoming
410 * traffic only to cleanup outstanding requests.
411 */
412
413 if (stor_device->destroy &&
414 (atomic_read(&stor_device->num_outstanding_req) == 0))
415 stor_device = NULL;
416
417get_in_err:
8dcf37d4
S
418 return stor_device;
419
420}
421
2707388c
S
422static void destroy_bounce_buffer(struct scatterlist *sgl,
423 unsigned int sg_count)
424{
425 int i;
426 struct page *page_buf;
427
428 for (i = 0; i < sg_count; i++) {
429 page_buf = sg_page((&sgl[i]));
430 if (page_buf != NULL)
431 __free_page(page_buf);
432 }
433
434 kfree(sgl);
435}
436
437static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
438{
439 int i;
440
441 /* No need to check */
442 if (sg_count < 2)
443 return -1;
444
445 /* We have at least 2 sg entries */
446 for (i = 0; i < sg_count; i++) {
447 if (i == 0) {
448 /* make sure 1st one does not have hole */
449 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
450 return i;
451 } else if (i == sg_count - 1) {
452 /* make sure last one does not have hole */
453 if (sgl[i].offset != 0)
454 return i;
455 } else {
456 /* make sure no hole in the middle */
457 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
458 return i;
459 }
460 }
461 return -1;
462}
463
464static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
465 unsigned int sg_count,
466 unsigned int len,
467 int write)
468{
469 int i;
470 int num_pages;
471 struct scatterlist *bounce_sgl;
472 struct page *page_buf;
473 unsigned int buf_len = ((write == WRITE_TYPE) ? 0 : PAGE_SIZE);
474
475 num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
476
477 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
478 if (!bounce_sgl)
479 return NULL;
480
481 for (i = 0; i < num_pages; i++) {
482 page_buf = alloc_page(GFP_ATOMIC);
483 if (!page_buf)
484 goto cleanup;
485 sg_set_page(&bounce_sgl[i], page_buf, buf_len, 0);
486 }
487
488 return bounce_sgl;
489
490cleanup:
491 destroy_bounce_buffer(bounce_sgl, num_pages);
492 return NULL;
493}
494
495/* Assume the original sgl has enough room */
496static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
497 struct scatterlist *bounce_sgl,
498 unsigned int orig_sgl_count,
499 unsigned int bounce_sgl_count)
500{
501 int i;
502 int j = 0;
503 unsigned long src, dest;
504 unsigned int srclen, destlen, copylen;
505 unsigned int total_copied = 0;
506 unsigned long bounce_addr = 0;
507 unsigned long dest_addr = 0;
508 unsigned long flags;
509
510 local_irq_save(flags);
511
512 for (i = 0; i < orig_sgl_count; i++) {
513 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
514 KM_IRQ0) + orig_sgl[i].offset;
515 dest = dest_addr;
516 destlen = orig_sgl[i].length;
517
518 if (bounce_addr == 0)
519 bounce_addr =
520 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
521 KM_IRQ0);
522
523 while (destlen) {
524 src = bounce_addr + bounce_sgl[j].offset;
525 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
526
527 copylen = min(srclen, destlen);
528 memcpy((void *)dest, (void *)src, copylen);
529
530 total_copied += copylen;
531 bounce_sgl[j].offset += copylen;
532 destlen -= copylen;
533 dest += copylen;
534
535 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
536 /* full */
537 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
538 j++;
539
540 /*
541 * It is possible that the number of elements
542 * in the bounce buffer may not be equal to
543 * the number of elements in the original
544 * scatter list. Handle this correctly.
545 */
546
547 if (j == bounce_sgl_count) {
548 /*
549 * We are done; cleanup and return.
550 */
551 kunmap_atomic((void *)(dest_addr -
552 orig_sgl[i].offset),
553 KM_IRQ0);
554 local_irq_restore(flags);
555 return total_copied;
556 }
557
558 /* if we need to use another bounce buffer */
559 if (destlen || i != orig_sgl_count - 1)
560 bounce_addr =
561 (unsigned long)kmap_atomic(
562 sg_page((&bounce_sgl[j])), KM_IRQ0);
563 } else if (destlen == 0 && i == orig_sgl_count - 1) {
564 /* unmap the last bounce that is < PAGE_SIZE */
565 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
566 }
567 }
568
569 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
570 KM_IRQ0);
571 }
572
573 local_irq_restore(flags);
574
575 return total_copied;
576}
577
578/* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
579static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
580 struct scatterlist *bounce_sgl,
581 unsigned int orig_sgl_count)
582{
583 int i;
584 int j = 0;
585 unsigned long src, dest;
586 unsigned int srclen, destlen, copylen;
587 unsigned int total_copied = 0;
588 unsigned long bounce_addr = 0;
589 unsigned long src_addr = 0;
590 unsigned long flags;
591
592 local_irq_save(flags);
593
594 for (i = 0; i < orig_sgl_count; i++) {
595 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
596 KM_IRQ0) + orig_sgl[i].offset;
597 src = src_addr;
598 srclen = orig_sgl[i].length;
599
600 if (bounce_addr == 0)
601 bounce_addr =
602 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
603 KM_IRQ0);
604
605 while (srclen) {
606 /* assume bounce offset always == 0 */
607 dest = bounce_addr + bounce_sgl[j].length;
608 destlen = PAGE_SIZE - bounce_sgl[j].length;
609
610 copylen = min(srclen, destlen);
611 memcpy((void *)dest, (void *)src, copylen);
612
613 total_copied += copylen;
614 bounce_sgl[j].length += copylen;
615 srclen -= copylen;
616 src += copylen;
617
618 if (bounce_sgl[j].length == PAGE_SIZE) {
619 /* full..move to next entry */
620 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
621 j++;
622
623 /* if we need to use another bounce buffer */
624 if (srclen || i != orig_sgl_count - 1)
625 bounce_addr =
626 (unsigned long)kmap_atomic(
627 sg_page((&bounce_sgl[j])), KM_IRQ0);
628
629 } else if (srclen == 0 && i == orig_sgl_count - 1) {
630 /* unmap the last bounce that is < PAGE_SIZE */
631 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
632 }
633 }
634
635 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
636 }
637
638 local_irq_restore(flags);
639
640 return total_copied;
641}
642
8dcf37d4
S
643static int storvsc_channel_init(struct hv_device *device)
644{
645 struct storvsc_device *stor_device;
646 struct hv_storvsc_request *request;
647 struct vstor_packet *vstor_packet;
648 int ret, t;
649
650 stor_device = get_out_stor_device(device);
651 if (!stor_device)
652 return -ENODEV;
653
654 request = &stor_device->init_request;
655 vstor_packet = &request->vstor_packet;
656
657 /*
658 * Now, initiate the vsc/vsp initialization protocol on the open
659 * channel
660 */
661 memset(request, 0, sizeof(struct hv_storvsc_request));
662 init_completion(&request->wait_event);
663 vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
664 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
665
666 ret = vmbus_sendpacket(device->channel, vstor_packet,
667 sizeof(struct vstor_packet),
668 (unsigned long)request,
669 VM_PKT_DATA_INBAND,
670 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
671 if (ret != 0)
672 goto cleanup;
673
674 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
675 if (t == 0) {
676 ret = -ETIMEDOUT;
677 goto cleanup;
678 }
679
680 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
681 vstor_packet->status != 0)
682 goto cleanup;
683
684
685 /* reuse the packet for version range supported */
686 memset(vstor_packet, 0, sizeof(struct vstor_packet));
687 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
688 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
689
690 vstor_packet->version.major_minor = VMSTOR_PROTOCOL_VERSION_CURRENT;
c649114a 691 vstor_packet->version.revision = 0;
8dcf37d4
S
692
693 ret = vmbus_sendpacket(device->channel, vstor_packet,
694 sizeof(struct vstor_packet),
695 (unsigned long)request,
696 VM_PKT_DATA_INBAND,
697 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
698 if (ret != 0)
699 goto cleanup;
700
701 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
702 if (t == 0) {
703 ret = -ETIMEDOUT;
704 goto cleanup;
705 }
706
707 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
708 vstor_packet->status != 0)
709 goto cleanup;
710
711
712 memset(vstor_packet, 0, sizeof(struct vstor_packet));
713 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
714 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
715 vstor_packet->storage_channel_properties.port_number =
716 stor_device->port_number;
717
718 ret = vmbus_sendpacket(device->channel, vstor_packet,
719 sizeof(struct vstor_packet),
720 (unsigned long)request,
721 VM_PKT_DATA_INBAND,
722 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
723
724 if (ret != 0)
725 goto cleanup;
726
727 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
728 if (t == 0) {
729 ret = -ETIMEDOUT;
730 goto cleanup;
731 }
732
733 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
734 vstor_packet->status != 0)
735 goto cleanup;
736
737 stor_device->path_id = vstor_packet->storage_channel_properties.path_id;
738 stor_device->target_id
739 = vstor_packet->storage_channel_properties.target_id;
740
741 memset(vstor_packet, 0, sizeof(struct vstor_packet));
742 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
743 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
744
745 ret = vmbus_sendpacket(device->channel, vstor_packet,
746 sizeof(struct vstor_packet),
747 (unsigned long)request,
748 VM_PKT_DATA_INBAND,
749 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
750
751 if (ret != 0)
752 goto cleanup;
753
754 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
755 if (t == 0) {
756 ret = -ETIMEDOUT;
757 goto cleanup;
758 }
759
760 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
761 vstor_packet->status != 0)
762 goto cleanup;
763
764
765cleanup:
766 return ret;
767}
768
2707388c
S
769
770static void storvsc_command_completion(struct hv_storvsc_request *request)
8dcf37d4 771{
93a1bf4d 772 struct storvsc_cmd_request *cmd_request = request->cmd;
2707388c
S
773 struct scsi_cmnd *scmnd = cmd_request->cmd;
774 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
775 void (*scsi_done_fn)(struct scsi_cmnd *);
776 struct scsi_sense_hdr sense_hdr;
777 struct vmscsi_request *vm_srb;
778 struct storvsc_scan_work *wrk;
779 struct stor_mem_pools *memp = scmnd->device->hostdata;
8dcf37d4 780
2707388c
S
781 vm_srb = &request->vstor_packet.vm_srb;
782 if (cmd_request->bounce_sgl_count) {
783 if (vm_srb->data_in == READ_TYPE)
784 copy_from_bounce_buffer(scsi_sglist(scmnd),
785 cmd_request->bounce_sgl,
786 scsi_sg_count(scmnd),
787 cmd_request->bounce_sgl_count);
788 destroy_bounce_buffer(cmd_request->bounce_sgl,
789 cmd_request->bounce_sgl_count);
790 }
8dcf37d4 791
4ed51a21 792 /*
2707388c
S
793 * If there is an error; offline the device since all
794 * error recovery strategies would have already been
795 * deployed on the host side.
796 */
797 if (vm_srb->srb_status == SRB_STATUS_ERROR)
798 scmnd->result = DID_TARGET_FAILURE << 16;
799 else
800 scmnd->result = vm_srb->scsi_status;
801
802 /*
803 * If the LUN is invalid; remove the device.
804 */
805 if (vm_srb->srb_status == SRB_STATUS_INVALID_LUN) {
806 struct storvsc_device *stor_dev;
807 struct hv_device *dev = host_dev->dev;
808 struct Scsi_Host *host;
809
810 stor_dev = get_in_stor_device(dev);
811 host = stor_dev->host;
812
813 wrk = kmalloc(sizeof(struct storvsc_scan_work),
814 GFP_ATOMIC);
815 if (!wrk) {
816 scmnd->result = DID_TARGET_FAILURE << 16;
817 } else {
818 wrk->host = host;
819 wrk->lun = vm_srb->lun;
820 INIT_WORK(&wrk->work, storvsc_remove_lun);
821 schedule_work(&wrk->work);
822 }
823 }
824
825 if (scmnd->result) {
826 if (scsi_normalize_sense(scmnd->sense_buffer,
827 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
828 scsi_print_sense_hdr("storvsc", &sense_hdr);
829 }
830
831 scsi_set_resid(scmnd,
832 request->data_buffer.len -
833 vm_srb->data_transfer_length);
834
835 scsi_done_fn = scmnd->scsi_done;
836
837 scmnd->host_scribble = NULL;
838 scmnd->scsi_done = NULL;
839
840 scsi_done_fn(scmnd);
841
842 mempool_free(cmd_request, memp->request_mempool);
843}
844
845static void storvsc_on_io_completion(struct hv_device *device,
846 struct vstor_packet *vstor_packet,
847 struct hv_storvsc_request *request)
848{
849 struct storvsc_device *stor_device;
850 struct vstor_packet *stor_pkt;
851
852 stor_device = hv_get_drvdata(device);
853 stor_pkt = &request->vstor_packet;
854
855 /*
856 * The current SCSI handling on the host side does
857 * not correctly handle:
858 * INQUIRY command with page code parameter set to 0x80
859 * MODE_SENSE command with cmd[2] == 0x1c
860 *
861 * Setup srb and scsi status so this won't be fatal.
862 * We do this so we can distinguish truly fatal failues
4ed51a21
S
863 * (srb status == 0x4) and off-line the device in that case.
864 */
865
866 if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
a8c18c57 867 (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
4ed51a21 868 vstor_packet->vm_srb.scsi_status = 0;
16046320 869 vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
4ed51a21
S
870 }
871
8dcf37d4
S
872
873 /* Copy over the status...etc */
874 stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
875 stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
876 stor_pkt->vm_srb.sense_info_length =
877 vstor_packet->vm_srb.sense_info_length;
878
879 if (vstor_packet->vm_srb.scsi_status != 0 ||
16046320 880 vstor_packet->vm_srb.srb_status != SRB_STATUS_SUCCESS){
d181daa0
GKH
881 dev_warn(&device->device,
882 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
883 stor_pkt->vm_srb.cdb[0],
884 vstor_packet->vm_srb.scsi_status,
885 vstor_packet->vm_srb.srb_status);
8dcf37d4
S
886 }
887
888 if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
889 /* CHECK_CONDITION */
16046320
S
890 if (vstor_packet->vm_srb.srb_status &
891 SRB_STATUS_AUTOSENSE_VALID) {
8dcf37d4 892 /* autosense data available */
d181daa0 893 dev_warn(&device->device,
41098f8f
S
894 "stor pkt %p autosense data valid - len %d\n",
895 request,
896 vstor_packet->vm_srb.sense_info_length);
8dcf37d4
S
897
898 memcpy(request->sense_buffer,
899 vstor_packet->vm_srb.sense_data,
900 vstor_packet->vm_srb.sense_info_length);
901
902 }
903 }
904
905 stor_pkt->vm_srb.data_transfer_length =
906 vstor_packet->vm_srb.data_transfer_length;
907
2707388c 908 storvsc_command_completion(request);
8dcf37d4
S
909
910 if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
911 stor_device->drain_notify)
912 wake_up(&stor_device->waiting_to_drain);
913
914
915}
916
917static void storvsc_on_receive(struct hv_device *device,
918 struct vstor_packet *vstor_packet,
919 struct hv_storvsc_request *request)
920{
12675799
S
921 struct storvsc_scan_work *work;
922 struct storvsc_device *stor_device;
923
8dcf37d4
S
924 switch (vstor_packet->operation) {
925 case VSTOR_OPERATION_COMPLETE_IO:
926 storvsc_on_io_completion(device, vstor_packet, request);
927 break;
12675799 928
8dcf37d4 929 case VSTOR_OPERATION_REMOVE_DEVICE:
12675799
S
930 case VSTOR_OPERATION_ENUMERATE_BUS:
931 stor_device = get_in_stor_device(device);
932 work = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
933 if (!work)
934 return;
935
936 INIT_WORK(&work->work, storvsc_bus_scan);
937 work->host = stor_device->host;
938 schedule_work(&work->work);
939 break;
8dcf37d4
S
940
941 default:
942 break;
943 }
944}
945
946static void storvsc_on_channel_callback(void *context)
947{
948 struct hv_device *device = (struct hv_device *)context;
949 struct storvsc_device *stor_device;
950 u32 bytes_recvd;
951 u64 request_id;
952 unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
953 struct hv_storvsc_request *request;
954 int ret;
955
956
957 stor_device = get_in_stor_device(device);
958 if (!stor_device)
959 return;
960
961 do {
962 ret = vmbus_recvpacket(device->channel, packet,
963 ALIGN(sizeof(struct vstor_packet), 8),
964 &bytes_recvd, &request_id);
965 if (ret == 0 && bytes_recvd > 0) {
966
967 request = (struct hv_storvsc_request *)
968 (unsigned long)request_id;
969
970 if ((request == &stor_device->init_request) ||
971 (request == &stor_device->reset_request)) {
972
973 memcpy(&request->vstor_packet, packet,
974 sizeof(struct vstor_packet));
975 complete(&request->wait_event);
976 } else {
977 storvsc_on_receive(device,
978 (struct vstor_packet *)packet,
979 request);
980 }
981 } else {
982 break;
983 }
984 } while (1);
985
986 return;
987}
988
989static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
990{
991 struct vmstorage_channel_properties props;
992 int ret;
993
994 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
995
8dcf37d4
S
996 ret = vmbus_open(device->channel,
997 ring_size,
998 ring_size,
999 (void *)&props,
1000 sizeof(struct vmstorage_channel_properties),
1001 storvsc_on_channel_callback, device);
1002
1003 if (ret != 0)
1004 return ret;
1005
1006 ret = storvsc_channel_init(device);
1007
1008 return ret;
1009}
1010
c1b3d067 1011static int storvsc_dev_remove(struct hv_device *device)
8dcf37d4
S
1012{
1013 struct storvsc_device *stor_device;
1014 unsigned long flags;
1015
cd654ea1 1016 stor_device = hv_get_drvdata(device);
8dcf37d4
S
1017
1018 spin_lock_irqsave(&device->channel->inbound_lock, flags);
1019 stor_device->destroy = true;
1020 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
1021
1022 /*
1023 * At this point, all outbound traffic should be disable. We
1024 * only allow inbound traffic (responses) to proceed so that
1025 * outstanding requests can be completed.
1026 */
1027
1028 storvsc_wait_to_drain(stor_device);
1029
1030 /*
1031 * Since we have already drained, we don't need to busy wait
1032 * as was done in final_release_stor_device()
1033 * Note that we cannot set the ext pointer to NULL until
1034 * we have drained - to drain the outgoing packets, we need to
1035 * allow incoming packets.
1036 */
1037 spin_lock_irqsave(&device->channel->inbound_lock, flags);
cd654ea1 1038 hv_set_drvdata(device, NULL);
8dcf37d4
S
1039 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
1040
1041 /* Close the channel */
1042 vmbus_close(device->channel);
1043
1044 kfree(stor_device);
1045 return 0;
1046}
1047
c1b3d067 1048static int storvsc_do_io(struct hv_device *device,
8dcf37d4
S
1049 struct hv_storvsc_request *request)
1050{
1051 struct storvsc_device *stor_device;
1052 struct vstor_packet *vstor_packet;
1053 int ret = 0;
1054
1055 vstor_packet = &request->vstor_packet;
1056 stor_device = get_out_stor_device(device);
1057
1058 if (!stor_device)
1059 return -ENODEV;
1060
1061
1062 request->device = device;
1063
1064
1065 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
1066
1067 vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
1068
1069
1070 vstor_packet->vm_srb.sense_info_length = SENSE_BUFFER_SIZE;
1071
1072
1073 vstor_packet->vm_srb.data_transfer_length =
1074 request->data_buffer.len;
1075
1076 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
1077
1078 if (request->data_buffer.len) {
1079 ret = vmbus_sendpacket_multipagebuffer(device->channel,
1080 &request->data_buffer,
1081 vstor_packet,
1082 sizeof(struct vstor_packet),
1083 (unsigned long)request);
1084 } else {
1085 ret = vmbus_sendpacket(device->channel, vstor_packet,
1086 sizeof(struct vstor_packet),
1087 (unsigned long)request,
1088 VM_PKT_DATA_INBAND,
1089 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1090 }
1091
1092 if (ret != 0)
1093 return ret;
1094
1095 atomic_inc(&stor_device->num_outstanding_req);
1096
1097 return ret;
1098}
1099
5b60acee
S
1100static int storvsc_device_alloc(struct scsi_device *sdevice)
1101{
ce3e301c
S
1102 struct stor_mem_pools *memp;
1103 int number = STORVSC_MIN_BUF_NR;
1104
1105 memp = kzalloc(sizeof(struct stor_mem_pools), GFP_KERNEL);
1106 if (!memp)
1107 return -ENOMEM;
1108
1109 memp->request_pool =
1110 kmem_cache_create(dev_name(&sdevice->sdev_dev),
1111 sizeof(struct storvsc_cmd_request), 0,
1112 SLAB_HWCACHE_ALIGN, NULL);
1113
1114 if (!memp->request_pool)
1115 goto err0;
1116
1117 memp->request_mempool = mempool_create(number, mempool_alloc_slab,
1118 mempool_free_slab,
1119 memp->request_pool);
1120
1121 if (!memp->request_mempool)
1122 goto err1;
1123
1124 sdevice->hostdata = memp;
1125
5b60acee 1126 return 0;
ce3e301c
S
1127
1128err1:
1129 kmem_cache_destroy(memp->request_pool);
1130
1131err0:
1132 kfree(memp);
1133 return -ENOMEM;
1134}
1135
1136static void storvsc_device_destroy(struct scsi_device *sdevice)
1137{
1138 struct stor_mem_pools *memp = sdevice->hostdata;
1139
1140 mempool_destroy(memp->request_mempool);
1141 kmem_cache_destroy(memp->request_pool);
1142 kfree(memp);
1143 sdevice->hostdata = NULL;
5b60acee
S
1144}
1145
419f2d03
S
1146static int storvsc_device_configure(struct scsi_device *sdevice)
1147{
1148 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
1149 STORVSC_MAX_IO_REQUESTS);
1150
419f2d03
S
1151 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
1152
419f2d03 1153 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
419f2d03
S
1154
1155 return 0;
1156}
1157
62838ce2
S
1158static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
1159 sector_t capacity, int *info)
1160{
5326fd5c
S
1161 sector_t nsect = capacity;
1162 sector_t cylinders = nsect;
1163 int heads, sectors_pt;
62838ce2 1164
5326fd5c
S
1165 /*
1166 * We are making up these values; let us keep it simple.
1167 */
1168 heads = 0xff;
1169 sectors_pt = 0x3f; /* Sectors per track */
1170 sector_div(cylinders, heads * sectors_pt);
1171 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
1172 cylinders = 0xffff;
62838ce2
S
1173
1174 info[0] = heads;
5326fd5c
S
1175 info[1] = sectors_pt;
1176 info[2] = (int)cylinders;
62838ce2 1177
62838ce2
S
1178 return 0;
1179}
aa3d789e 1180
4b270c8b 1181static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
aa3d789e 1182{
4b270c8b
S
1183 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
1184 struct hv_device *device = host_dev->dev;
1185
aa3d789e
S
1186 struct storvsc_device *stor_device;
1187 struct hv_storvsc_request *request;
1188 struct vstor_packet *vstor_packet;
1189 int ret, t;
1190
aa3d789e 1191
1eaaddf9 1192 stor_device = get_out_stor_device(device);
aa3d789e 1193 if (!stor_device)
a00e8224 1194 return FAILED;
aa3d789e
S
1195
1196 request = &stor_device->reset_request;
1197 vstor_packet = &request->vstor_packet;
1198
1199 init_completion(&request->wait_event);
1200
1201 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
1202 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
1203 vstor_packet->vm_srb.path_id = stor_device->path_id;
1204
1205 ret = vmbus_sendpacket(device->channel, vstor_packet,
1206 sizeof(struct vstor_packet),
1207 (unsigned long)&stor_device->reset_request,
1208 VM_PKT_DATA_INBAND,
1209 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1210 if (ret != 0)
a00e8224 1211 return FAILED;
aa3d789e 1212
46d2eb6d 1213 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
a00e8224
S
1214 if (t == 0)
1215 return TIMEOUT_ERROR;
aa3d789e 1216
aa3d789e
S
1217
1218 /*
1219 * At this point, all outstanding requests in the adapter
1220 * should have been flushed out and return to us
1221 */
1222
a00e8224 1223 return SUCCESS;
aa3d789e
S
1224}
1225
c77b63b6 1226static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd)
92ae4ebd
OH
1227{
1228 bool allowed = true;
1229 u8 scsi_op = scmnd->cmnd[0];
1230
1231 switch (scsi_op) {
c77b63b6
S
1232 /*
1233 * smartd sends this command and the host does not handle
1234 * this. So, don't send it.
1235 */
41098f8f 1236 case SET_WINDOW:
59e00e74 1237 scmnd->result = ILLEGAL_REQUEST << 16;
41098f8f
S
1238 allowed = false;
1239 break;
1240 default:
1241 break;
92ae4ebd
OH
1242 }
1243 return allowed;
1244}
c5b463ae 1245
bab445e1 1246static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
c5b463ae
S
1247{
1248 int ret;
bab445e1 1249 struct hv_host_device *host_dev = shost_priv(host);
c5b463ae 1250 struct hv_device *dev = host_dev->dev;
c5b463ae
S
1251 struct hv_storvsc_request *request;
1252 struct storvsc_cmd_request *cmd_request;
1253 unsigned int request_size = 0;
1254 int i;
1255 struct scatterlist *sgl;
1256 unsigned int sg_count = 0;
1257 struct vmscsi_request *vm_srb;
ce3e301c 1258 struct stor_mem_pools *memp = scmnd->device->hostdata;
c5b463ae 1259
c77b63b6 1260 if (!storvsc_scsi_cmd_ok(scmnd)) {
bab445e1 1261 scmnd->scsi_done(scmnd);
92ae4ebd
OH
1262 return 0;
1263 }
c5b463ae 1264
c5b463ae
S
1265 request_size = sizeof(struct storvsc_cmd_request);
1266
ce3e301c 1267 cmd_request = mempool_alloc(memp->request_mempool,
c5b463ae 1268 GFP_ATOMIC);
c77b63b6
S
1269
1270 /*
1271 * We might be invoked in an interrupt context; hence
1272 * mempool_alloc() can fail.
1273 */
bab445e1 1274 if (!cmd_request)
c5b463ae 1275 return SCSI_MLQUEUE_DEVICE_BUSY;
bab445e1 1276
4e03e697 1277 memset(cmd_request, 0, sizeof(struct storvsc_cmd_request));
c5b463ae
S
1278
1279 /* Setup the cmd request */
c5b463ae
S
1280 cmd_request->cmd = scmnd;
1281
1282 scmnd->host_scribble = (unsigned char *)cmd_request;
1283
1284 request = &cmd_request->request;
1285 vm_srb = &request->vstor_packet.vm_srb;
1286
1287
1288 /* Build the SRB */
1289 switch (scmnd->sc_data_direction) {
1290 case DMA_TO_DEVICE:
1291 vm_srb->data_in = WRITE_TYPE;
1292 break;
1293 case DMA_FROM_DEVICE:
1294 vm_srb->data_in = READ_TYPE;
1295 break;
1296 default:
1297 vm_srb->data_in = UNKNOWN_TYPE;
1298 break;
1299 }
1300
93a1bf4d 1301 request->cmd = cmd_request;
c5b463ae 1302
c5b463ae
S
1303 vm_srb->port_number = host_dev->port;
1304 vm_srb->path_id = scmnd->device->channel;
1305 vm_srb->target_id = scmnd->device->id;
1306 vm_srb->lun = scmnd->device->lun;
1307
c5b463ae
S
1308 vm_srb->cdb_length = scmnd->cmd_len;
1309
1310 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
1311
1312 request->sense_buffer = scmnd->sense_buffer;
1313
1314
1315 request->data_buffer.len = scsi_bufflen(scmnd);
1316 if (scsi_sg_count(scmnd)) {
1317 sgl = (struct scatterlist *)scsi_sglist(scmnd);
1318 sg_count = scsi_sg_count(scmnd);
1319
1320 /* check if we need to bounce the sgl */
1321 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
1322 cmd_request->bounce_sgl =
1323 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
6e8087a4
S
1324 scsi_bufflen(scmnd),
1325 vm_srb->data_in);
c5b463ae 1326 if (!cmd_request->bounce_sgl) {
c77b63b6
S
1327 ret = SCSI_MLQUEUE_HOST_BUSY;
1328 goto queue_error;
c5b463ae
S
1329 }
1330
1331 cmd_request->bounce_sgl_count =
1332 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
1333 PAGE_SHIFT;
1334
fa23b8c7
S
1335 if (vm_srb->data_in == WRITE_TYPE)
1336 copy_to_bounce_buffer(sgl,
1337 cmd_request->bounce_sgl,
1338 scsi_sg_count(scmnd));
c5b463ae
S
1339
1340 sgl = cmd_request->bounce_sgl;
1341 sg_count = cmd_request->bounce_sgl_count;
1342 }
1343
1344 request->data_buffer.offset = sgl[0].offset;
1345
1346 for (i = 0; i < sg_count; i++)
1347 request->data_buffer.pfn_array[i] =
1348 page_to_pfn(sg_page((&sgl[i])));
1349
1350 } else if (scsi_sglist(scmnd)) {
c5b463ae
S
1351 request->data_buffer.offset =
1352 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
1353 request->data_buffer.pfn_array[0] =
1354 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
1355 }
1356
c5b463ae 1357 /* Invokes the vsc to start an IO */
636f0fd1
S
1358 ret = storvsc_do_io(dev, &cmd_request->request);
1359
d2598f01 1360 if (ret == -EAGAIN) {
c5b463ae
S
1361 /* no more space */
1362
c77b63b6 1363 if (cmd_request->bounce_sgl_count) {
c5b463ae 1364 destroy_bounce_buffer(cmd_request->bounce_sgl,
70691ec6 1365 cmd_request->bounce_sgl_count);
c5b463ae 1366
c77b63b6
S
1367 ret = SCSI_MLQUEUE_DEVICE_BUSY;
1368 goto queue_error;
1369 }
c5b463ae
S
1370 }
1371
c77b63b6
S
1372 return 0;
1373
1374queue_error:
1375 mempool_free(cmd_request, memp->request_mempool);
1376 scmnd->host_scribble = NULL;
c5b463ae
S
1377 return ret;
1378}
1379
bef4a34a 1380static struct scsi_host_template scsi_driver = {
ff568d3a
GKH
1381 .module = THIS_MODULE,
1382 .name = "storvsc_host_t",
1383 .bios_param = storvsc_get_chs,
1384 .queuecommand = storvsc_queuecommand,
1385 .eh_host_reset_handler = storvsc_host_reset_handler,
1386 .slave_alloc = storvsc_device_alloc,
ce3e301c 1387 .slave_destroy = storvsc_device_destroy,
ff568d3a
GKH
1388 .slave_configure = storvsc_device_configure,
1389 .cmd_per_lun = 1,
1390 /* 64 max_queue * 1 target */
0686e4f4 1391 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
ff568d3a 1392 .this_id = -1,
454f18a9 1393 /* no use setting to 0 since ll_blk_rw reset it to 1 */
ff568d3a
GKH
1394 /* currently 32 */
1395 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
039db52d 1396 .use_clustering = DISABLE_CLUSTERING,
454f18a9 1397 /* Make sure we dont get a sg segment crosses a page boundary */
ff568d3a 1398 .dma_boundary = PAGE_SIZE-1,
bef4a34a
HJ
1399};
1400
ef52a81b
S
1401enum {
1402 SCSI_GUID,
1403 IDE_GUID,
1404};
1405
d847b5fe 1406static const struct hv_vmbus_device_id id_table[] = {
c45cf2d4
GKH
1407 /* SCSI guid */
1408 { VMBUS_DEVICE(0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
ef52a81b
S
1409 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f)
1410 .driver_data = SCSI_GUID },
21e37742
S
1411 /* IDE guid */
1412 { VMBUS_DEVICE(0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
ef52a81b
S
1413 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5)
1414 .driver_data = IDE_GUID },
c45cf2d4 1415 { },
d847b5fe 1416};
bef4a34a 1417
d847b5fe 1418MODULE_DEVICE_TABLE(vmbus, id_table);
bd1f5d6a 1419
84946899
S
1420static int storvsc_probe(struct hv_device *device,
1421 const struct hv_vmbus_device_id *dev_id)
bef4a34a 1422{
ff568d3a 1423 int ret;
bef4a34a 1424 struct Scsi_Host *host;
795b613d 1425 struct hv_host_device *host_dev;
ef52a81b 1426 bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
bd1f5d6a 1427 int target = 0;
6e4198ce 1428 struct storvsc_device *stor_device;
bd1f5d6a 1429
ff568d3a 1430 host = scsi_host_alloc(&scsi_driver,
972621c9 1431 sizeof(struct hv_host_device));
f8feed06 1432 if (!host)
bef4a34a 1433 return -ENOMEM;
bef4a34a 1434
7f33f30a 1435 host_dev = shost_priv(host);
795b613d 1436 memset(host_dev, 0, sizeof(struct hv_host_device));
bef4a34a 1437
795b613d 1438 host_dev->port = host->host_no;
97c15296 1439 host_dev->dev = device;
bef4a34a 1440
4e03e697 1441
a13d35ab 1442 stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
6e4198ce 1443 if (!stor_device) {
225ce6ea 1444 ret = -ENOMEM;
ce3e301c 1445 goto err_out0;
6e4198ce 1446 }
9efd21e1 1447
a13d35ab
S
1448 stor_device->destroy = false;
1449 init_waitqueue_head(&stor_device->waiting_to_drain);
1450 stor_device->device = device;
cd654ea1
S
1451 stor_device->host = host;
1452 hv_set_drvdata(device, stor_device);
a13d35ab 1453
6e4198ce
S
1454 stor_device->port_number = host->host_no;
1455 ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size);
225ce6ea 1456 if (ret)
ce3e301c 1457 goto err_out1;
bef4a34a 1458
6e4198ce
S
1459 host_dev->path = stor_device->path_id;
1460 host_dev->target = stor_device->target_id;
bef4a34a 1461
ff568d3a
GKH
1462 /* max # of devices per target */
1463 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
1464 /* max # of targets per channel */
1465 host->max_id = STORVSC_MAX_TARGETS;
1466 /* max # of channels */
1467 host->max_channel = STORVSC_MAX_CHANNELS - 1;
cf55f4a8
MS
1468 /* max cmd length */
1469 host->max_cmd_len = STORVSC_MAX_CMD_LEN;
bef4a34a 1470
454f18a9 1471 /* Register the HBA and start the scsi bus scan */
9efd21e1 1472 ret = scsi_add_host(host, &device->device);
bd1f5d6a 1473 if (ret != 0)
ce3e301c 1474 goto err_out2;
bef4a34a 1475
bd1f5d6a
S
1476 if (!dev_is_ide) {
1477 scsi_scan_host(host);
59d22950
S
1478 } else {
1479 target = (device->dev_instance.b[5] << 8 |
1480 device->dev_instance.b[4]);
1481 ret = scsi_add_device(host, 0, target, 0);
1482 if (ret) {
1483 scsi_remove_host(host);
1484 goto err_out2;
1485 }
bef4a34a 1486 }
bd1f5d6a 1487 return 0;
bef4a34a 1488
ce3e301c 1489err_out2:
225ce6ea
S
1490 /*
1491 * Once we have connected with the host, we would need to
1492 * to invoke storvsc_dev_remove() to rollback this state and
1493 * this call also frees up the stor_device; hence the jump around
ce3e301c 1494 * err_out1 label.
225ce6ea 1495 */
bd1f5d6a 1496 storvsc_dev_remove(device);
ce3e301c 1497 goto err_out0;
225ce6ea
S
1498
1499err_out1:
ce3e301c 1500 kfree(stor_device);
225ce6ea
S
1501
1502err_out0:
bd1f5d6a 1503 scsi_host_put(host);
225ce6ea 1504 return ret;
bef4a34a
HJ
1505}
1506
ddcbf65e
S
1507static int storvsc_remove(struct hv_device *dev)
1508{
1509 struct storvsc_device *stor_device = hv_get_drvdata(dev);
1510 struct Scsi_Host *host = stor_device->host;
1511
1512 scsi_remove_host(host);
1513 storvsc_dev_remove(dev);
1514 scsi_host_put(host);
1515
1516 return 0;
1517}
1518
40bf63ed 1519static struct hv_driver storvsc_drv = {
fafb0efc 1520 .name = KBUILD_MODNAME,
d847b5fe 1521 .id_table = id_table,
40bf63ed
S
1522 .probe = storvsc_probe,
1523 .remove = storvsc_remove,
39ae6fae 1524};
7bd05b91 1525
d9bbae83 1526static int __init storvsc_drv_init(void)
f5c78872 1527{
01415ab3
S
1528 u32 max_outstanding_req_per_channel;
1529
1530 /*
1531 * Divide the ring buffer data size (which is 1 page less
1532 * than the ring buffer size since that page is reserved for
1533 * the ring buffer indices) by the max request size (which is
1534 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
1535 */
01415ab3 1536 max_outstanding_req_per_channel =
768fa219
GKH
1537 ((storvsc_ringbuffer_size - PAGE_SIZE) /
1538 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
1539 sizeof(struct vstor_packet) + sizeof(u64),
1540 sizeof(u64)));
f5c78872 1541
01415ab3 1542 if (max_outstanding_req_per_channel <
f5c78872 1543 STORVSC_MAX_IO_REQUESTS)
b06efc19 1544 return -EINVAL;
f5c78872 1545
768fa219 1546 return vmbus_driver_register(&storvsc_drv);
f5c78872
S
1547}
1548
c63ba9e1 1549static void __exit storvsc_drv_exit(void)
f5c78872 1550{
768fa219 1551 vmbus_driver_unregister(&storvsc_drv);
f5c78872
S
1552}
1553
ff568d3a 1554MODULE_LICENSE("GPL");
26c14cc1 1555MODULE_VERSION(HV_DRV_VERSION);
3afc7cc3 1556MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
d9bbae83 1557module_init(storvsc_drv_init);
c63ba9e1 1558module_exit(storvsc_drv_exit);