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