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