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