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