blob: 204b3ca5a196906adad091ea154eced2d58c0388 [file] [log] [blame]
Hank Janssenbef4a342009-07-13 16:01:31 -07001/*
Hank Janssenbef4a342009-07-13 16:01:31 -07002 * 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>
K. Y. Srinivasan972621c2011-05-10 07:54:19 -070020 * K. Y. Srinivasan <kys@microsoft.com>
Hank Janssenbef4a342009-07-13 16:01:31 -070021 */
K. Y. Srinivasana1be1702011-08-27 11:31:23 -070022
23#include <linux/kernel.h>
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070024#include <linux/wait.h>
K. Y. Srinivasana1be1702011-08-27 11:31:23 -070025#include <linux/sched.h>
26#include <linux/completion.h>
27#include <linux/string.h>
28#include <linux/mm.h>
29#include <linux/delay.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070030#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070032#include <linux/module.h>
33#include <linux/device.h>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070034#include <linux/hyperv.h>
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -080035#include <linux/mempool.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070036#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>
Hank Janssenbef4a342009-07-13 16:01:31 -070043#include <scsi/scsi_dbg.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070044
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070045
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -080046#define STORVSC_MIN_BUF_NR 64
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -070047#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
K. Y. Srinivasan09f03552012-01-12 12:37:54 -080053/*
54 * To alert the user that structure sizes may be mismatched even though the
55 * protocol versions match.
56 */
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070057
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
K. Y. Srinivasan09f03552012-01-12 12:37:54 -080073/*
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
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070078#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
K. Y. Srinivasan09f03552012-01-12 12:37:54 -080084/*
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
K. Y. Srinivasan2b9525f2011-11-08 09:01:48 -080092#define VMSTOR_PROTOCOL_VERSION_CURRENT VMSTOR_PROTOCOL_VERSION(4, 2)
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070093
94
95
96
K. Y. Srinivasan09f03552012-01-12 12:37:54 -080097/*
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
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700103#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,
K. Y. Srinivasan2b9525f2011-11-08 09:01:48 -0800121 VSTOR_OPERATION_ENUMERATE_BUS = 11,
122 VSTOR_OPERATION_MAXIMUM = 11
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700123};
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
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700226/*
K. Y. Srinivasan09f03552012-01-12 12:37:54 -0800227 * Packet Flags:
228 *
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700229 * This flag indicates that the server should send back a completion for this
230 * packet.
231 */
K. Y. Srinivasan09f03552012-01-12 12:37:54 -0800232
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700233#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
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700239
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
Mike Sterlingcf55f4a2011-09-06 16:10:55 -0700251#define STORVSC_MAX_CMD_LEN 16
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700252
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700253/* Matches Windows-end */
254enum storvsc_request_type {
255 WRITE_TYPE,
256 READ_TYPE,
257 UNKNOWN_TYPE,
258};
259
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800260/*
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
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700270
271struct hv_storvsc_request {
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700272 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
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700286/* 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;
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700293 struct Scsi_Host *host;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700294
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
K. Y. Srinivasance3e3012011-12-01 04:59:20 -0800311struct stor_mem_pools {
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700312 struct kmem_cache *request_pool;
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -0800313 mempool_t *request_mempool;
K. Y. Srinivasance3e3012011-12-01 04:59:20 -0800314};
315
316struct hv_host_device {
317 struct hv_device *dev;
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700318 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};
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700332
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800333struct 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
K. Y. Srinivasanb4017312011-11-08 09:01:50 -0800357static 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
K. Y. Srinivasana8c18c52012-01-12 12:38:00 -0800378/*
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
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700393static inline struct storvsc_device *get_out_stor_device(
394 struct hv_device *device)
395{
396 struct storvsc_device *stor_device;
397
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700398 stor_device = hv_get_drvdata(device);
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700399
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}
Hank Janssenbef4a342009-07-13 16:01:31 -0700414
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700415static inline struct storvsc_device *get_in_stor_device(
416 struct hv_device *device)
417{
418 struct storvsc_device *stor_device;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700419
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700420 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700421
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:
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700435 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
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700572 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700573 stor_pkt = &request->vstor_packet;
574
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -0700575 /*
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) ||
K. Y. Srinivasana8c18c52012-01-12 12:38:00 -0800587 (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -0700588 vstor_packet->vm_srb.scsi_status = 0;
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800589 vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -0700590 }
591
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700592
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 ||
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800600 vstor_packet->vm_srb.srb_status != SRB_STATUS_SUCCESS){
Greg Kroah-Hartmand181daa2011-10-11 09:20:31 -0600601 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);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700606 }
607
608 if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
609 /* CHECK_CONDITION */
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800610 if (vstor_packet->vm_srb.srb_status &
611 SRB_STATUS_AUTOSENSE_VALID) {
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700612 /* autosense data available */
Greg Kroah-Hartmand181daa2011-10-11 09:20:31 -0600613 dev_warn(&device->device,
K. Y. Srinivasan41098f82011-10-14 21:31:57 -0700614 "stor pkt %p autosense data valid - len %d\n",
615 request,
616 vstor_packet->vm_srb.sense_info_length);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700617
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{
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800641 struct storvsc_scan_work *work;
642 struct storvsc_device *stor_device;
643
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700644 switch (vstor_packet->operation) {
645 case VSTOR_OPERATION_COMPLETE_IO:
646 storvsc_on_io_completion(device, vstor_packet, request);
647 break;
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800648
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700649 case VSTOR_OPERATION_REMOVE_DEVICE:
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800650 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;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700660
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
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700716 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
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700731static int storvsc_dev_remove(struct hv_device *device)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700732{
733 struct storvsc_device *stor_device;
734 unsigned long flags;
735
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700736 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700737
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);
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700758 hv_set_drvdata(device, NULL);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700759 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
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700768static int storvsc_do_io(struct hv_device *device,
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700769 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
K. Y. Srinivasan5b60ace2011-05-10 07:54:25 -0700820static int storvsc_device_alloc(struct scsi_device *sdevice)
821{
K. Y. Srinivasance3e3012011-12-01 04:59:20 -0800822 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
K. Y. Srinivasan5b60ace2011-05-10 07:54:25 -0700846 return 0;
K. Y. Srinivasance3e3012011-12-01 04:59:20 -0800847
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;
K. Y. Srinivasan5b60ace2011-05-10 07:54:25 -0700864}
865
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700866static int storvsc_device_configure(struct scsi_device *sdevice)
867{
868 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
869 STORVSC_MAX_IO_REQUESTS);
870
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700871 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
872
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700873 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700874
875 return 0;
876}
877
K. Y. Srinivasan49a3c7a2011-05-10 07:54:28 -0700878static 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
K. Y. Srinivasan3862ef32011-05-10 07:54:29 -0700893static 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
K. Y. Srinivasana9753cb2011-05-10 07:54:30 -0700920static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
921 unsigned int sg_count,
K. Y. Srinivasan6e8087a2011-12-07 07:15:52 -0800922 unsigned int len,
923 int write)
K. Y. Srinivasana9753cb2011-05-10 07:54:30 -0700924{
925 int i;
926 int num_pages;
927 struct scatterlist *bounce_sgl;
928 struct page *page_buf;
K. Y. Srinivasan6e8087a2011-12-07 07:15:52 -0800929 unsigned int buf_len = ((write == WRITE_TYPE) ? 0 : PAGE_SIZE);
K. Y. Srinivasana9753cb2011-05-10 07:54:30 -0700930
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;
K. Y. Srinivasan6e8087a2011-12-07 07:15:52 -0800941 sg_set_page(&bounce_sgl[i], page_buf, buf_len, 0);
K. Y. Srinivasana9753cb2011-05-10 07:54:30 -0700942 }
943
944 return bounce_sgl;
945
946cleanup:
947 destroy_bounce_buffer(bounce_sgl, num_pages);
948 return NULL;
949}
950
K. Y. Srinivasan29fe2c92011-05-10 07:54:31 -0700951
952/* Assume the original sgl has enough room */
953static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
954 struct scatterlist *bounce_sgl,
K. Y. Srinivasan10c43dd42011-12-01 04:59:19 -0800955 unsigned int orig_sgl_count,
956 unsigned int bounce_sgl_count)
K. Y. Srinivasan29fe2c92011-05-10 07:54:31 -0700957{
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
K. Y. Srinivasan10c43dd42011-12-01 04:59:19 -0800997 /*
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
K. Y. Srinivasan29fe2c92011-05-10 07:54:31 -07001015 /* 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
K. Y. Srinivasan6a8ff442011-05-10 07:54:32 -07001035
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
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001101static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
1102 sector_t capacity, int *info)
1103{
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001104 sector_t nsect = capacity;
1105 sector_t cylinders = nsect;
1106 int heads, sectors_pt;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001107
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001108 /*
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;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001116
1117 info[0] = heads;
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001118 info[1] = sectors_pt;
1119 info[2] = (int)cylinders;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001120
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001121 return 0;
1122}
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001123
K. Y. Srinivasan4b270c82012-01-12 12:37:58 -08001124static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001125{
K. Y. Srinivasan4b270c82012-01-12 12:37:58 -08001126 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
1127 struct hv_device *device = host_dev->dev;
1128
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001129 struct storvsc_device *stor_device;
1130 struct hv_storvsc_request *request;
1131 struct vstor_packet *vstor_packet;
1132 int ret, t;
1133
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001134
K. Y. Srinivasan1eaaddf2011-08-27 11:31:03 -07001135 stor_device = get_out_stor_device(device);
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001136 if (!stor_device)
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001137 return FAILED;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001138
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)
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001154 return FAILED;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001155
K. Y. Srinivasan46d2eb62011-06-16 13:16:36 -07001156 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001157 if (t == 0)
1158 return TIMEOUT_ERROR;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001159
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001160
1161 /*
1162 * At this point, all outstanding requests in the adapter
1163 * should have been flushed out and return to us
1164 */
1165
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001166 return SUCCESS;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001167}
1168
1169
K. Y. Srinivasanb8de73d2011-08-27 11:31:27 -07001170static void storvsc_command_completion(struct hv_storvsc_request *request)
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001171{
1172 struct storvsc_cmd_request *cmd_request =
1173 (struct storvsc_cmd_request *)request->context;
1174 struct scsi_cmnd *scmnd = cmd_request->cmd;
K. Y. Srinivasan7f33f302011-11-08 09:01:44 -08001175 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001176 void (*scsi_done_fn)(struct scsi_cmnd *);
1177 struct scsi_sense_hdr sense_hdr;
1178 struct vmscsi_request *vm_srb;
K. Y. Srinivasanb4017312011-11-08 09:01:50 -08001179 struct storvsc_scan_work *wrk;
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001180 struct stor_mem_pools *memp = scmnd->device->hostdata;
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001181
K. Y. Srinivasan3612ef92011-08-27 11:31:20 -07001182 vm_srb = &request->vstor_packet.vm_srb;
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001183 if (cmd_request->bounce_sgl_count) {
K. Y. Srinivasana768a762011-12-01 04:59:18 -08001184 if (vm_srb->data_in == READ_TYPE)
K. Y. Srinivasan3612ef92011-08-27 11:31:20 -07001185 copy_from_bounce_buffer(scsi_sglist(scmnd),
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001186 cmd_request->bounce_sgl,
K. Y. Srinivasan10c43dd42011-12-01 04:59:19 -08001187 scsi_sg_count(scmnd),
1188 cmd_request->bounce_sgl_count);
K. Y. Srinivasana768a762011-12-01 04:59:18 -08001189 destroy_bounce_buffer(cmd_request->bounce_sgl,
K. Y. Srinivasan3612ef92011-08-27 11:31:20 -07001190 cmd_request->bounce_sgl_count);
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001191 }
1192
K. Y. Srinivasan2544b792011-08-27 11:31:28 -07001193 /*
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 */
K. Y. Srinivasan16046322012-01-12 12:37:57 -08001198 if (vm_srb->srb_status == SRB_STATUS_ERROR)
K. Y. Srinivasan2544b792011-08-27 11:31:28 -07001199 scmnd->result = DID_TARGET_FAILURE << 16;
1200 else
1201 scmnd->result = vm_srb->scsi_status;
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001202
K. Y. Srinivasanb4017312011-11-08 09:01:50 -08001203 /*
1204 * If the LUN is invalid; remove the device.
1205 */
K. Y. Srinivasan16046322012-01-12 12:37:57 -08001206 if (vm_srb->srb_status == SRB_STATUS_INVALID_LUN) {
K. Y. Srinivasanb4017312011-11-08 09:01:50 -08001207 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
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001226 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
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001232 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
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001241 scsi_done_fn(scmnd);
1242
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001243 mempool_free(cmd_request, memp->request_mempool);
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001244}
1245
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001246static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd)
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001247{
1248 bool allowed = true;
1249 u8 scsi_op = scmnd->cmnd[0];
1250
1251 switch (scsi_op) {
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001252 /*
1253 * smartd sends this command and the host does not handle
1254 * this. So, don't send it.
1255 */
K. Y. Srinivasan41098f82011-10-14 21:31:57 -07001256 case SET_WINDOW:
K. Y. Srinivasan59e00e72011-11-08 09:01:42 -08001257 scmnd->result = ILLEGAL_REQUEST << 16;
K. Y. Srinivasan41098f82011-10-14 21:31:57 -07001258 allowed = false;
1259 break;
1260 default:
1261 break;
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001262 }
1263 return allowed;
1264}
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001265
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001266static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001267{
1268 int ret;
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001269 struct hv_host_device *host_dev = shost_priv(host);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001270 struct hv_device *dev = host_dev->dev;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001271 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;
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001278 struct stor_mem_pools *memp = scmnd->device->hostdata;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001279
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001280 if (!storvsc_scsi_cmd_ok(scmnd)) {
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001281 scmnd->scsi_done(scmnd);
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001282 return 0;
1283 }
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001284
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001285 request_size = sizeof(struct storvsc_cmd_request);
1286
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001287 cmd_request = mempool_alloc(memp->request_mempool,
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001288 GFP_ATOMIC);
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001289
1290 /*
1291 * We might be invoked in an interrupt context; hence
1292 * mempool_alloc() can fail.
1293 */
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001294 if (!cmd_request)
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001295 return SCSI_MLQUEUE_DEVICE_BUSY;
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001296
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -08001297 memset(cmd_request, 0, sizeof(struct storvsc_cmd_request));
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001298
1299 /* Setup the cmd request */
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001300 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
K. Y. Srinivasanb8de73d2011-08-27 11:31:27 -07001321 request->on_io_completion = storvsc_command_completion;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001322 request->context = cmd_request;/* scmnd; */
1323
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001324 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
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001329 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),
K. Y. Srinivasan6e8087a2011-12-07 07:15:52 -08001345 scsi_bufflen(scmnd),
1346 vm_srb->data_in);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001347 if (!cmd_request->bounce_sgl) {
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001348 ret = SCSI_MLQUEUE_HOST_BUSY;
1349 goto queue_error;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001350 }
1351
1352 cmd_request->bounce_sgl_count =
1353 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
1354 PAGE_SHIFT;
1355
K. Y. Srinivasanfa23b8c2011-08-27 11:31:21 -07001356 if (vm_srb->data_in == WRITE_TYPE)
1357 copy_to_bounce_buffer(sgl,
1358 cmd_request->bounce_sgl,
1359 scsi_sg_count(scmnd));
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001360
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)) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001372 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
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001378 /* Invokes the vsc to start an IO */
K. Y. Srinivasan636f0fd2011-05-10 07:54:50 -07001379 ret = storvsc_do_io(dev, &cmd_request->request);
1380
K. Y. Srinivasand2598f02011-08-25 09:48:58 -07001381 if (ret == -EAGAIN) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001382 /* no more space */
1383
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001384 if (cmd_request->bounce_sgl_count) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001385 destroy_bounce_buffer(cmd_request->bounce_sgl,
K. Y. Srinivasan70691ec2011-08-27 11:31:29 -07001386 cmd_request->bounce_sgl_count);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001387
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001388 ret = SCSI_MLQUEUE_DEVICE_BUSY;
1389 goto queue_error;
1390 }
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001391 }
1392
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001393 return 0;
1394
1395queue_error:
1396 mempool_free(cmd_request, memp->request_mempool);
1397 scmnd->host_scribble = NULL;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001398 return ret;
1399}
1400
Hank Janssenbef4a342009-07-13 16:01:31 -07001401static struct scsi_host_template scsi_driver = {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001402 .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,
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001408 .slave_destroy = storvsc_device_destroy,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001409 .slave_configure = storvsc_device_configure,
1410 .cmd_per_lun = 1,
1411 /* 64 max_queue * 1 target */
Lars Lindley0686e4f2010-03-11 23:51:23 +01001412 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001413 .this_id = -1,
Bill Pemberton454f18a2009-07-27 16:47:24 -04001414 /* no use setting to 0 since ll_blk_rw reset it to 1 */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001415 /* currently 32 */
1416 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
K. Y. Srinivasan039db522011-12-01 04:59:16 -08001417 .use_clustering = DISABLE_CLUSTERING,
Bill Pemberton454f18a2009-07-27 16:47:24 -04001418 /* Make sure we dont get a sg segment crosses a page boundary */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001419 .dma_boundary = PAGE_SIZE-1,
Hank Janssenbef4a342009-07-13 16:01:31 -07001420};
1421
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001422enum {
1423 SCSI_GUID,
1424 IDE_GUID,
1425};
1426
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001427static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001428 /* SCSI guid */
1429 { VMBUS_DEVICE(0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001430 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f)
1431 .driver_data = SCSI_GUID },
K. Y. Srinivasan21e37742011-08-27 11:31:18 -07001432 /* IDE guid */
1433 { VMBUS_DEVICE(0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001434 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5)
1435 .driver_data = IDE_GUID },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001436 { },
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001437};
Hank Janssenbef4a342009-07-13 16:01:31 -07001438
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001439MODULE_DEVICE_TABLE(vmbus, id_table);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001440
K. Y. Srinivasan84946892011-09-13 10:59:38 -07001441static int storvsc_probe(struct hv_device *device,
1442 const struct hv_vmbus_device_id *dev_id)
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001443{
1444 int ret;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001445 struct Scsi_Host *host;
1446 struct hv_host_device *host_dev;
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001447 bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001448 int target = 0;
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001449 struct storvsc_device *stor_device;
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001450
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001451 host = scsi_host_alloc(&scsi_driver,
1452 sizeof(struct hv_host_device));
1453 if (!host)
1454 return -ENOMEM;
1455
K. Y. Srinivasan7f33f302011-11-08 09:01:44 -08001456 host_dev = shost_priv(host);
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001457 memset(host_dev, 0, sizeof(struct hv_host_device));
1458
1459 host_dev->port = host->host_no;
1460 host_dev->dev = device;
1461
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -08001462
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001463 stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001464 if (!stor_device) {
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001465 ret = -ENOMEM;
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001466 goto err_out0;
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001467 }
1468
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001469 stor_device->destroy = false;
1470 init_waitqueue_head(&stor_device->waiting_to_drain);
1471 stor_device->device = device;
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001472 stor_device->host = host;
1473 hv_set_drvdata(device, stor_device);
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001474
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001475 stor_device->port_number = host->host_no;
1476 ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001477 if (ret)
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001478 goto err_out1;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001479
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001480 host_dev->path = stor_device->path_id;
1481 host_dev->target = stor_device->target_id;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001482
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;
Mike Sterlingcf55f4a2011-09-06 16:10:55 -07001489 /* max cmd length */
1490 host->max_cmd_len = STORVSC_MAX_CMD_LEN;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001491
1492 /* Register the HBA and start the scsi bus scan */
1493 ret = scsi_add_host(host, &device->device);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001494 if (ret != 0)
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001495 goto err_out2;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001496
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001497 if (!dev_is_ide) {
1498 scsi_scan_host(host);
K. Y. Srinivasan59d22952012-01-12 12:37:55 -08001499 } 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 }
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001507 }
1508 return 0;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001509
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001510err_out2:
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001511 /*
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
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001515 * err_out1 label.
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001516 */
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001517 storvsc_dev_remove(device);
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001518 goto err_out0;
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001519
1520err_out1:
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001521 kfree(stor_device);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001522
1523err_out0:
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001524 scsi_host_put(host);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001525 return ret;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001526}
1527
K. Y. Srinivasanddcbf652012-01-12 12:37:59 -08001528static 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
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -07001540static struct hv_driver storvsc_drv = {
K. Y. Srinivasanfafb0ef2011-11-08 09:01:46 -08001541 .name = KBUILD_MODNAME,
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001542 .id_table = id_table,
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -07001543 .probe = storvsc_probe,
1544 .remove = storvsc_remove,
K. Y. Srinivasan39ae6fa2011-05-10 07:54:46 -07001545};
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -07001546
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -07001547static int __init storvsc_drv_init(void)
Hank Janssenbef4a342009-07-13 16:01:31 -07001548{
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001549 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 */
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001557 max_outstanding_req_per_channel =
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001558 ((storvsc_ringbuffer_size - PAGE_SIZE) /
1559 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
1560 sizeof(struct vstor_packet) + sizeof(u64),
1561 sizeof(u64)));
Hank Janssenbef4a342009-07-13 16:01:31 -07001562
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001563 if (max_outstanding_req_per_channel <
K. Y. Srinivasanf8feed02011-05-10 07:54:24 -07001564 STORVSC_MAX_IO_REQUESTS)
K. Y. Srinivasanb06efc12011-08-25 09:49:10 -07001565 return -EINVAL;
Hank Janssenbef4a342009-07-13 16:01:31 -07001566
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001567 return vmbus_driver_register(&storvsc_drv);
Hank Janssenbef4a342009-07-13 16:01:31 -07001568}
1569
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -07001570static void __exit storvsc_drv_exit(void)
Hank Janssenbef4a342009-07-13 16:01:31 -07001571{
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001572 vmbus_driver_unregister(&storvsc_drv);
Hank Janssenbef4a342009-07-13 16:01:31 -07001573}
1574
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001575MODULE_LICENSE("GPL");
Hank Janssen26c14cc2010-02-11 23:02:42 +00001576MODULE_VERSION(HV_DRV_VERSION);
Stephen Hemminger3afc7cc32010-05-06 21:44:45 -07001577MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -07001578module_init(storvsc_drv_init);
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -07001579module_exit(storvsc_drv_exit);