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