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