blob: 9f07458000419cec7acafb1dc3dc592643780e4d [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. Srinivasanc6491142012-01-12 12:38:03 -080046/*
47 * We setup a mempool to allocate request structures for this driver
48 * on a per-lun basis. The following define specifies the number of
49 * elements in the pool.
50 */
51
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -080052#define STORVSC_MIN_BUF_NR 64
K. Y. Srinivasanc6491142012-01-12 12:38:03 -080053static int storvsc_ringbuffer_size = (20 * PAGE_SIZE);
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -070054
55module_param(storvsc_ringbuffer_size, int, S_IRUGO);
56MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
57
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070058
K. Y. Srinivasan09f03552012-01-12 12:37:54 -080059/*
60 * Major/minor macros. Minor version is in LSB, meaning that earlier flat
61 * version numbers will be interpreted as "0.x" (i.e., 1 becomes 0.1).
62 */
63
K. Y. Srinivasan85904a52012-01-12 12:38:04 -080064static inline u16 storvsc_get_version(u8 major, u8 minor)
65{
66 u16 version;
67
68 version = ((major << 8) | minor);
69 return version;
70}
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070071
K. Y. Srinivasan09f03552012-01-12 12:37:54 -080072/*
73 * Version history:
74 * V1 Beta: 0.1
75 * V1 RC < 2008/1/31: 1.0
76 * V1 RC > 2008/1/31: 2.0
77 * Win7: 4.2
78 */
79
K. Y. Srinivasan85904a52012-01-12 12:38:04 -080080#define VMSTOR_CURRENT_MAJOR 4
81#define VMSTOR_CURRENT_MINOR 2
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070082
83
K. Y. Srinivasan09f03552012-01-12 12:37:54 -080084/*
85 * This will get replaced with the max transfer length that is possible on
86 * the host adapter.
87 * The max transfer length will be published when we offer a vmbus channel.
88 */
89
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070090#define MAX_TRANSFER_LENGTH 0x40000
91#define DEFAULT_PACKET_SIZE (sizeof(struct vmdata_gpa_direct) + \
92 sizeof(struct vstor_packet) + \
93 sizesizeof(u64) * (MAX_TRANSFER_LENGTH / PAGE_SIZE)))
94
95
96/* Packet structure describing virtual storage requests. */
97enum vstor_packet_operation {
98 VSTOR_OPERATION_COMPLETE_IO = 1,
99 VSTOR_OPERATION_REMOVE_DEVICE = 2,
100 VSTOR_OPERATION_EXECUTE_SRB = 3,
101 VSTOR_OPERATION_RESET_LUN = 4,
102 VSTOR_OPERATION_RESET_ADAPTER = 5,
103 VSTOR_OPERATION_RESET_BUS = 6,
104 VSTOR_OPERATION_BEGIN_INITIALIZATION = 7,
105 VSTOR_OPERATION_END_INITIALIZATION = 8,
106 VSTOR_OPERATION_QUERY_PROTOCOL_VERSION = 9,
107 VSTOR_OPERATION_QUERY_PROPERTIES = 10,
K. Y. Srinivasan2b9525f2011-11-08 09:01:48 -0800108 VSTOR_OPERATION_ENUMERATE_BUS = 11,
109 VSTOR_OPERATION_MAXIMUM = 11
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700110};
111
112/*
113 * Platform neutral description of a scsi request -
114 * this remains the same across the write regardless of 32/64 bit
115 * note: it's patterned off the SCSI_PASS_THROUGH structure
116 */
K. Y. Srinivasan6b2f9492012-01-12 12:38:05 -0800117#define STORVSC_MAX_CMD_LEN 0x10
118#define STORVSC_SENSE_BUFFER_SIZE 0x12
119#define STORVSC_MAX_BUF_LEN_WITH_PADDING 0x14
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700120
121struct vmscsi_request {
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800122 u16 length;
123 u8 srb_status;
124 u8 scsi_status;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700125
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800126 u8 port_number;
127 u8 path_id;
128 u8 target_id;
129 u8 lun;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700130
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800131 u8 cdb_length;
132 u8 sense_info_length;
133 u8 data_in;
134 u8 reserved;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700135
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800136 u32 data_transfer_length;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700137
138 union {
K. Y. Srinivasan6b2f9492012-01-12 12:38:05 -0800139 u8 cdb[STORVSC_MAX_CMD_LEN];
140 u8 sense_data[STORVSC_SENSE_BUFFER_SIZE];
141 u8 reserved_array[STORVSC_MAX_BUF_LEN_WITH_PADDING];
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700142 };
143} __attribute((packed));
144
145
146/*
147 * This structure is sent during the intialization phase to get the different
148 * properties of the channel.
149 */
150struct vmstorage_channel_properties {
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800151 u16 protocol_version;
152 u8 path_id;
153 u8 target_id;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700154
155 /* Note: port number is only really known on the client side */
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800156 u32 port_number;
157 u32 flags;
158 u32 max_transfer_bytes;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700159
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800160 /*
161 * This id is unique for each channel and will correspond with
162 * vendor specific data in the inquiry data.
163 */
164
165 u64 unique_id;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700166} __packed;
167
168/* This structure is sent during the storage protocol negotiations. */
169struct vmstorage_protocol_version {
170 /* Major (MSW) and minor (LSW) version numbers. */
K. Y. Srinivasan85904a52012-01-12 12:38:04 -0800171 u16 major_minor;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700172
173 /*
174 * Revision number is auto-incremented whenever this file is changed
175 * (See FILL_VMSTOR_REVISION macro above). Mismatch does not
176 * definitely indicate incompatibility--but it does indicate mismatched
177 * builds.
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800178 * This is only used on the windows side. Just set it to 0.
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700179 */
K. Y. Srinivasan85904a52012-01-12 12:38:04 -0800180 u16 revision;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700181} __packed;
182
183/* Channel Property Flags */
184#define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
185#define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
186
187struct vstor_packet {
188 /* Requested operation type */
189 enum vstor_packet_operation operation;
190
191 /* Flags - see below for values */
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800192 u32 flags;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700193
194 /* Status of the request returned from the server side. */
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800195 u32 status;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700196
197 /* Data payload area */
198 union {
199 /*
200 * Structure used to forward SCSI commands from the
201 * client to the server.
202 */
203 struct vmscsi_request vm_srb;
204
205 /* Structure used to query channel properties. */
206 struct vmstorage_channel_properties storage_channel_properties;
207
208 /* Used during version negotiations. */
209 struct vmstorage_protocol_version version;
210 };
211} __packed;
212
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700213/*
K. Y. Srinivasan09f03552012-01-12 12:37:54 -0800214 * Packet Flags:
215 *
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700216 * This flag indicates that the server should send back a completion for this
217 * packet.
218 */
K. Y. Srinivasan09f03552012-01-12 12:37:54 -0800219
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700220#define REQUEST_COMPLETION_FLAG 0x1
221
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700222#define STORVSC_MAX_IO_REQUESTS 128
223
224/*
225 * In Hyper-V, each port/path/target maps to 1 scsi host adapter. In
226 * reality, the path/target is not used (ie always set to 0) so our
227 * scsi host adapter essentially has 1 bus with 1 target that contains
228 * up to 256 luns.
229 */
230#define STORVSC_MAX_LUNS_PER_TARGET 64
231#define STORVSC_MAX_TARGETS 1
232#define STORVSC_MAX_CHANNELS 1
233
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700234/* Matches Windows-end */
235enum storvsc_request_type {
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800236 WRITE_TYPE = 0,
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700237 READ_TYPE,
238 UNKNOWN_TYPE,
239};
240
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800241/*
242 * SRB status codes and masks; a subset of the codes used here.
243 */
244
245#define SRB_STATUS_AUTOSENSE_VALID 0x80
246#define SRB_STATUS_INVALID_LUN 0x20
247#define SRB_STATUS_SUCCESS 0x01
248#define SRB_STATUS_ERROR 0x04
249
250
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700251
252struct hv_storvsc_request {
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700253 struct hv_device *device;
254
255 /* Synchronize the request/response if needed */
256 struct completion wait_event;
257
258 unsigned char *sense_buffer;
K. Y. Srinivasan93a1bf42012-01-12 12:38:02 -0800259 struct storvsc_cmd_request *cmd;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700260 struct hv_multipage_buffer data_buffer;
261
262 struct vstor_packet vstor_packet;
263};
264
265
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700266/* A storvsc device is a device object that contains a vmbus channel */
267struct storvsc_device {
268 struct hv_device *device;
269
270 bool destroy;
271 bool drain_notify;
272 atomic_t num_outstanding_req;
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700273 struct Scsi_Host *host;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700274
275 wait_queue_head_t waiting_to_drain;
276
277 /*
278 * Each unique Port/Path/Target represents 1 channel ie scsi
279 * controller. In reality, the pathid, targetid is always 0
280 * and the port is set by us
281 */
282 unsigned int port_number;
283 unsigned char path_id;
284 unsigned char target_id;
285
286 /* Used for vsc/vsp channel reset process */
287 struct hv_storvsc_request init_request;
288 struct hv_storvsc_request reset_request;
289};
290
K. Y. Srinivasance3e3012011-12-01 04:59:20 -0800291struct stor_mem_pools {
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700292 struct kmem_cache *request_pool;
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -0800293 mempool_t *request_mempool;
K. Y. Srinivasance3e3012011-12-01 04:59:20 -0800294};
295
296struct hv_host_device {
297 struct hv_device *dev;
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700298 unsigned int port;
299 unsigned char path;
300 unsigned char target;
301};
302
303struct storvsc_cmd_request {
304 struct list_head entry;
305 struct scsi_cmnd *cmd;
306
307 unsigned int bounce_sgl_count;
308 struct scatterlist *bounce_sgl;
309
310 struct hv_storvsc_request request;
311};
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700312
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800313struct storvsc_scan_work {
314 struct work_struct work;
315 struct Scsi_Host *host;
316 uint lun;
317};
318
319static void storvsc_bus_scan(struct work_struct *work)
320{
321 struct storvsc_scan_work *wrk;
322 int id, order_id;
323
324 wrk = container_of(work, struct storvsc_scan_work, work);
325 for (id = 0; id < wrk->host->max_id; ++id) {
326 if (wrk->host->reverse_ordering)
327 order_id = wrk->host->max_id - id - 1;
328 else
329 order_id = id;
330
331 scsi_scan_target(&wrk->host->shost_gendev, 0,
332 order_id, SCAN_WILD_CARD, 1);
333 }
334 kfree(wrk);
335}
336
K. Y. Srinivasanb4017312011-11-08 09:01:50 -0800337static void storvsc_remove_lun(struct work_struct *work)
338{
339 struct storvsc_scan_work *wrk;
340 struct scsi_device *sdev;
341
342 wrk = container_of(work, struct storvsc_scan_work, work);
343 if (!scsi_host_get(wrk->host))
344 goto done;
345
346 sdev = scsi_device_lookup(wrk->host, 0, 0, wrk->lun);
347
348 if (sdev) {
349 scsi_remove_device(sdev);
350 scsi_device_put(sdev);
351 }
352 scsi_host_put(wrk->host);
353
354done:
355 kfree(wrk);
356}
357
K. Y. Srinivasana8c18c52012-01-12 12:38:00 -0800358/*
359 * We can get incoming messages from the host that are not in response to
360 * messages that we have sent out. An example of this would be messages
361 * received by the guest to notify dynamic addition/removal of LUNs. To
362 * deal with potential race conditions where the driver may be in the
363 * midst of being unloaded when we might receive an unsolicited message
364 * from the host, we have implemented a mechanism to gurantee sequential
365 * consistency:
366 *
367 * 1) Once the device is marked as being destroyed, we will fail all
368 * outgoing messages.
369 * 2) We permit incoming messages when the device is being destroyed,
370 * only to properly account for messages already sent out.
371 */
372
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700373static inline struct storvsc_device *get_out_stor_device(
374 struct hv_device *device)
375{
376 struct storvsc_device *stor_device;
377
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700378 stor_device = hv_get_drvdata(device);
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700379
380 if (stor_device && stor_device->destroy)
381 stor_device = NULL;
382
383 return stor_device;
384}
385
386
387static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
388{
389 dev->drain_notify = true;
390 wait_event(dev->waiting_to_drain,
391 atomic_read(&dev->num_outstanding_req) == 0);
392 dev->drain_notify = false;
393}
Hank Janssenbef4a342009-07-13 16:01:31 -0700394
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700395static inline struct storvsc_device *get_in_stor_device(
396 struct hv_device *device)
397{
398 struct storvsc_device *stor_device;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700399
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700400 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700401
402 if (!stor_device)
403 goto get_in_err;
404
405 /*
406 * If the device is being destroyed; allow incoming
407 * traffic only to cleanup outstanding requests.
408 */
409
410 if (stor_device->destroy &&
411 (atomic_read(&stor_device->num_outstanding_req) == 0))
412 stor_device = NULL;
413
414get_in_err:
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700415 return stor_device;
416
417}
418
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800419static void destroy_bounce_buffer(struct scatterlist *sgl,
420 unsigned int sg_count)
421{
422 int i;
423 struct page *page_buf;
424
425 for (i = 0; i < sg_count; i++) {
426 page_buf = sg_page((&sgl[i]));
427 if (page_buf != NULL)
428 __free_page(page_buf);
429 }
430
431 kfree(sgl);
432}
433
434static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
435{
436 int i;
437
438 /* No need to check */
439 if (sg_count < 2)
440 return -1;
441
442 /* We have at least 2 sg entries */
443 for (i = 0; i < sg_count; i++) {
444 if (i == 0) {
445 /* make sure 1st one does not have hole */
446 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
447 return i;
448 } else if (i == sg_count - 1) {
449 /* make sure last one does not have hole */
450 if (sgl[i].offset != 0)
451 return i;
452 } else {
453 /* make sure no hole in the middle */
454 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
455 return i;
456 }
457 }
458 return -1;
459}
460
461static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
462 unsigned int sg_count,
463 unsigned int len,
464 int write)
465{
466 int i;
467 int num_pages;
468 struct scatterlist *bounce_sgl;
469 struct page *page_buf;
470 unsigned int buf_len = ((write == WRITE_TYPE) ? 0 : PAGE_SIZE);
471
472 num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
473
474 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
475 if (!bounce_sgl)
476 return NULL;
477
478 for (i = 0; i < num_pages; i++) {
479 page_buf = alloc_page(GFP_ATOMIC);
480 if (!page_buf)
481 goto cleanup;
482 sg_set_page(&bounce_sgl[i], page_buf, buf_len, 0);
483 }
484
485 return bounce_sgl;
486
487cleanup:
488 destroy_bounce_buffer(bounce_sgl, num_pages);
489 return NULL;
490}
491
492/* Assume the original sgl has enough room */
493static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
494 struct scatterlist *bounce_sgl,
495 unsigned int orig_sgl_count,
496 unsigned int bounce_sgl_count)
497{
498 int i;
499 int j = 0;
500 unsigned long src, dest;
501 unsigned int srclen, destlen, copylen;
502 unsigned int total_copied = 0;
503 unsigned long bounce_addr = 0;
504 unsigned long dest_addr = 0;
505 unsigned long flags;
506
507 local_irq_save(flags);
508
509 for (i = 0; i < orig_sgl_count; i++) {
510 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
511 KM_IRQ0) + orig_sgl[i].offset;
512 dest = dest_addr;
513 destlen = orig_sgl[i].length;
514
515 if (bounce_addr == 0)
516 bounce_addr =
517 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
518 KM_IRQ0);
519
520 while (destlen) {
521 src = bounce_addr + bounce_sgl[j].offset;
522 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
523
524 copylen = min(srclen, destlen);
525 memcpy((void *)dest, (void *)src, copylen);
526
527 total_copied += copylen;
528 bounce_sgl[j].offset += copylen;
529 destlen -= copylen;
530 dest += copylen;
531
532 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
533 /* full */
534 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
535 j++;
536
537 /*
538 * It is possible that the number of elements
539 * in the bounce buffer may not be equal to
540 * the number of elements in the original
541 * scatter list. Handle this correctly.
542 */
543
544 if (j == bounce_sgl_count) {
545 /*
546 * We are done; cleanup and return.
547 */
548 kunmap_atomic((void *)(dest_addr -
549 orig_sgl[i].offset),
550 KM_IRQ0);
551 local_irq_restore(flags);
552 return total_copied;
553 }
554
555 /* if we need to use another bounce buffer */
556 if (destlen || i != orig_sgl_count - 1)
557 bounce_addr =
558 (unsigned long)kmap_atomic(
559 sg_page((&bounce_sgl[j])), KM_IRQ0);
560 } else if (destlen == 0 && i == orig_sgl_count - 1) {
561 /* unmap the last bounce that is < PAGE_SIZE */
562 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
563 }
564 }
565
566 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
567 KM_IRQ0);
568 }
569
570 local_irq_restore(flags);
571
572 return total_copied;
573}
574
575/* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
576static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
577 struct scatterlist *bounce_sgl,
578 unsigned int orig_sgl_count)
579{
580 int i;
581 int j = 0;
582 unsigned long src, dest;
583 unsigned int srclen, destlen, copylen;
584 unsigned int total_copied = 0;
585 unsigned long bounce_addr = 0;
586 unsigned long src_addr = 0;
587 unsigned long flags;
588
589 local_irq_save(flags);
590
591 for (i = 0; i < orig_sgl_count; i++) {
592 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
593 KM_IRQ0) + orig_sgl[i].offset;
594 src = src_addr;
595 srclen = orig_sgl[i].length;
596
597 if (bounce_addr == 0)
598 bounce_addr =
599 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
600 KM_IRQ0);
601
602 while (srclen) {
603 /* assume bounce offset always == 0 */
604 dest = bounce_addr + bounce_sgl[j].length;
605 destlen = PAGE_SIZE - bounce_sgl[j].length;
606
607 copylen = min(srclen, destlen);
608 memcpy((void *)dest, (void *)src, copylen);
609
610 total_copied += copylen;
611 bounce_sgl[j].length += copylen;
612 srclen -= copylen;
613 src += copylen;
614
615 if (bounce_sgl[j].length == PAGE_SIZE) {
616 /* full..move to next entry */
617 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
618 j++;
619
620 /* if we need to use another bounce buffer */
621 if (srclen || i != orig_sgl_count - 1)
622 bounce_addr =
623 (unsigned long)kmap_atomic(
624 sg_page((&bounce_sgl[j])), KM_IRQ0);
625
626 } else if (srclen == 0 && i == orig_sgl_count - 1) {
627 /* unmap the last bounce that is < PAGE_SIZE */
628 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
629 }
630 }
631
632 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
633 }
634
635 local_irq_restore(flags);
636
637 return total_copied;
638}
639
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700640static int storvsc_channel_init(struct hv_device *device)
641{
642 struct storvsc_device *stor_device;
643 struct hv_storvsc_request *request;
644 struct vstor_packet *vstor_packet;
645 int ret, t;
646
647 stor_device = get_out_stor_device(device);
648 if (!stor_device)
649 return -ENODEV;
650
651 request = &stor_device->init_request;
652 vstor_packet = &request->vstor_packet;
653
654 /*
655 * Now, initiate the vsc/vsp initialization protocol on the open
656 * channel
657 */
658 memset(request, 0, sizeof(struct hv_storvsc_request));
659 init_completion(&request->wait_event);
660 vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
661 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
662
663 ret = vmbus_sendpacket(device->channel, vstor_packet,
664 sizeof(struct vstor_packet),
665 (unsigned long)request,
666 VM_PKT_DATA_INBAND,
667 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
668 if (ret != 0)
669 goto cleanup;
670
671 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
672 if (t == 0) {
673 ret = -ETIMEDOUT;
674 goto cleanup;
675 }
676
677 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
678 vstor_packet->status != 0)
679 goto cleanup;
680
681
682 /* reuse the packet for version range supported */
683 memset(vstor_packet, 0, sizeof(struct vstor_packet));
684 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
685 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
686
K. Y. Srinivasan85904a52012-01-12 12:38:04 -0800687 vstor_packet->version.major_minor =
688 storvsc_get_version(VMSTOR_CURRENT_MAJOR, VMSTOR_CURRENT_MINOR);
689
690 /*
691 * The revision number is only used in Windows; set it to 0.
692 */
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800693 vstor_packet->version.revision = 0;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700694
695 ret = vmbus_sendpacket(device->channel, vstor_packet,
696 sizeof(struct vstor_packet),
697 (unsigned long)request,
698 VM_PKT_DATA_INBAND,
699 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
700 if (ret != 0)
701 goto cleanup;
702
703 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
704 if (t == 0) {
705 ret = -ETIMEDOUT;
706 goto cleanup;
707 }
708
709 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
710 vstor_packet->status != 0)
711 goto cleanup;
712
713
714 memset(vstor_packet, 0, sizeof(struct vstor_packet));
715 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
716 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
717 vstor_packet->storage_channel_properties.port_number =
718 stor_device->port_number;
719
720 ret = vmbus_sendpacket(device->channel, vstor_packet,
721 sizeof(struct vstor_packet),
722 (unsigned long)request,
723 VM_PKT_DATA_INBAND,
724 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
725
726 if (ret != 0)
727 goto cleanup;
728
729 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
730 if (t == 0) {
731 ret = -ETIMEDOUT;
732 goto cleanup;
733 }
734
735 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
736 vstor_packet->status != 0)
737 goto cleanup;
738
739 stor_device->path_id = vstor_packet->storage_channel_properties.path_id;
740 stor_device->target_id
741 = vstor_packet->storage_channel_properties.target_id;
742
743 memset(vstor_packet, 0, sizeof(struct vstor_packet));
744 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
745 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
746
747 ret = vmbus_sendpacket(device->channel, vstor_packet,
748 sizeof(struct vstor_packet),
749 (unsigned long)request,
750 VM_PKT_DATA_INBAND,
751 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
752
753 if (ret != 0)
754 goto cleanup;
755
756 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
757 if (t == 0) {
758 ret = -ETIMEDOUT;
759 goto cleanup;
760 }
761
762 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
763 vstor_packet->status != 0)
764 goto cleanup;
765
766
767cleanup:
768 return ret;
769}
770
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800771
772static void storvsc_command_completion(struct hv_storvsc_request *request)
773{
K. Y. Srinivasan93a1bf42012-01-12 12:38:02 -0800774 struct storvsc_cmd_request *cmd_request = request->cmd;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800775 struct scsi_cmnd *scmnd = cmd_request->cmd;
776 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
777 void (*scsi_done_fn)(struct scsi_cmnd *);
778 struct scsi_sense_hdr sense_hdr;
779 struct vmscsi_request *vm_srb;
780 struct storvsc_scan_work *wrk;
781 struct stor_mem_pools *memp = scmnd->device->hostdata;
782
783 vm_srb = &request->vstor_packet.vm_srb;
784 if (cmd_request->bounce_sgl_count) {
785 if (vm_srb->data_in == READ_TYPE)
786 copy_from_bounce_buffer(scsi_sglist(scmnd),
787 cmd_request->bounce_sgl,
788 scsi_sg_count(scmnd),
789 cmd_request->bounce_sgl_count);
790 destroy_bounce_buffer(cmd_request->bounce_sgl,
791 cmd_request->bounce_sgl_count);
792 }
793
794 /*
795 * If there is an error; offline the device since all
796 * error recovery strategies would have already been
797 * deployed on the host side.
798 */
799 if (vm_srb->srb_status == SRB_STATUS_ERROR)
800 scmnd->result = DID_TARGET_FAILURE << 16;
801 else
802 scmnd->result = vm_srb->scsi_status;
803
804 /*
805 * If the LUN is invalid; remove the device.
806 */
807 if (vm_srb->srb_status == SRB_STATUS_INVALID_LUN) {
808 struct storvsc_device *stor_dev;
809 struct hv_device *dev = host_dev->dev;
810 struct Scsi_Host *host;
811
812 stor_dev = get_in_stor_device(dev);
813 host = stor_dev->host;
814
815 wrk = kmalloc(sizeof(struct storvsc_scan_work),
816 GFP_ATOMIC);
817 if (!wrk) {
818 scmnd->result = DID_TARGET_FAILURE << 16;
819 } else {
820 wrk->host = host;
821 wrk->lun = vm_srb->lun;
822 INIT_WORK(&wrk->work, storvsc_remove_lun);
823 schedule_work(&wrk->work);
824 }
825 }
826
827 if (scmnd->result) {
828 if (scsi_normalize_sense(scmnd->sense_buffer,
829 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
830 scsi_print_sense_hdr("storvsc", &sense_hdr);
831 }
832
833 scsi_set_resid(scmnd,
834 request->data_buffer.len -
835 vm_srb->data_transfer_length);
836
837 scsi_done_fn = scmnd->scsi_done;
838
839 scmnd->host_scribble = NULL;
840 scmnd->scsi_done = NULL;
841
842 scsi_done_fn(scmnd);
843
844 mempool_free(cmd_request, memp->request_mempool);
845}
846
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700847static void storvsc_on_io_completion(struct hv_device *device,
848 struct vstor_packet *vstor_packet,
849 struct hv_storvsc_request *request)
850{
851 struct storvsc_device *stor_device;
852 struct vstor_packet *stor_pkt;
853
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700854 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700855 stor_pkt = &request->vstor_packet;
856
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -0700857 /*
858 * The current SCSI handling on the host side does
859 * not correctly handle:
860 * INQUIRY command with page code parameter set to 0x80
861 * MODE_SENSE command with cmd[2] == 0x1c
862 *
863 * Setup srb and scsi status so this won't be fatal.
864 * We do this so we can distinguish truly fatal failues
865 * (srb status == 0x4) and off-line the device in that case.
866 */
867
868 if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
K. Y. Srinivasana8c18c52012-01-12 12:38:00 -0800869 (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -0700870 vstor_packet->vm_srb.scsi_status = 0;
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800871 vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -0700872 }
873
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700874
875 /* Copy over the status...etc */
876 stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
877 stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
878 stor_pkt->vm_srb.sense_info_length =
879 vstor_packet->vm_srb.sense_info_length;
880
881 if (vstor_packet->vm_srb.scsi_status != 0 ||
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800882 vstor_packet->vm_srb.srb_status != SRB_STATUS_SUCCESS){
Greg Kroah-Hartmand181daa2011-10-11 09:20:31 -0600883 dev_warn(&device->device,
884 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
885 stor_pkt->vm_srb.cdb[0],
886 vstor_packet->vm_srb.scsi_status,
887 vstor_packet->vm_srb.srb_status);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700888 }
889
890 if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
891 /* CHECK_CONDITION */
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800892 if (vstor_packet->vm_srb.srb_status &
893 SRB_STATUS_AUTOSENSE_VALID) {
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700894 /* autosense data available */
Greg Kroah-Hartmand181daa2011-10-11 09:20:31 -0600895 dev_warn(&device->device,
K. Y. Srinivasan41098f82011-10-14 21:31:57 -0700896 "stor pkt %p autosense data valid - len %d\n",
897 request,
898 vstor_packet->vm_srb.sense_info_length);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700899
900 memcpy(request->sense_buffer,
901 vstor_packet->vm_srb.sense_data,
902 vstor_packet->vm_srb.sense_info_length);
903
904 }
905 }
906
907 stor_pkt->vm_srb.data_transfer_length =
908 vstor_packet->vm_srb.data_transfer_length;
909
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800910 storvsc_command_completion(request);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700911
912 if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
913 stor_device->drain_notify)
914 wake_up(&stor_device->waiting_to_drain);
915
916
917}
918
919static void storvsc_on_receive(struct hv_device *device,
920 struct vstor_packet *vstor_packet,
921 struct hv_storvsc_request *request)
922{
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800923 struct storvsc_scan_work *work;
924 struct storvsc_device *stor_device;
925
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700926 switch (vstor_packet->operation) {
927 case VSTOR_OPERATION_COMPLETE_IO:
928 storvsc_on_io_completion(device, vstor_packet, request);
929 break;
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800930
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700931 case VSTOR_OPERATION_REMOVE_DEVICE:
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800932 case VSTOR_OPERATION_ENUMERATE_BUS:
933 stor_device = get_in_stor_device(device);
934 work = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
935 if (!work)
936 return;
937
938 INIT_WORK(&work->work, storvsc_bus_scan);
939 work->host = stor_device->host;
940 schedule_work(&work->work);
941 break;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700942
943 default:
944 break;
945 }
946}
947
948static void storvsc_on_channel_callback(void *context)
949{
950 struct hv_device *device = (struct hv_device *)context;
951 struct storvsc_device *stor_device;
952 u32 bytes_recvd;
953 u64 request_id;
954 unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
955 struct hv_storvsc_request *request;
956 int ret;
957
958
959 stor_device = get_in_stor_device(device);
960 if (!stor_device)
961 return;
962
963 do {
964 ret = vmbus_recvpacket(device->channel, packet,
965 ALIGN(sizeof(struct vstor_packet), 8),
966 &bytes_recvd, &request_id);
967 if (ret == 0 && bytes_recvd > 0) {
968
969 request = (struct hv_storvsc_request *)
970 (unsigned long)request_id;
971
972 if ((request == &stor_device->init_request) ||
973 (request == &stor_device->reset_request)) {
974
975 memcpy(&request->vstor_packet, packet,
976 sizeof(struct vstor_packet));
977 complete(&request->wait_event);
978 } else {
979 storvsc_on_receive(device,
980 (struct vstor_packet *)packet,
981 request);
982 }
983 } else {
984 break;
985 }
986 } while (1);
987
988 return;
989}
990
991static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
992{
993 struct vmstorage_channel_properties props;
994 int ret;
995
996 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
997
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700998 ret = vmbus_open(device->channel,
999 ring_size,
1000 ring_size,
1001 (void *)&props,
1002 sizeof(struct vmstorage_channel_properties),
1003 storvsc_on_channel_callback, device);
1004
1005 if (ret != 0)
1006 return ret;
1007
1008 ret = storvsc_channel_init(device);
1009
1010 return ret;
1011}
1012
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -07001013static int storvsc_dev_remove(struct hv_device *device)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001014{
1015 struct storvsc_device *stor_device;
1016 unsigned long flags;
1017
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001018 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001019
1020 spin_lock_irqsave(&device->channel->inbound_lock, flags);
1021 stor_device->destroy = true;
1022 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
1023
1024 /*
1025 * At this point, all outbound traffic should be disable. We
1026 * only allow inbound traffic (responses) to proceed so that
1027 * outstanding requests can be completed.
1028 */
1029
1030 storvsc_wait_to_drain(stor_device);
1031
1032 /*
1033 * Since we have already drained, we don't need to busy wait
1034 * as was done in final_release_stor_device()
1035 * Note that we cannot set the ext pointer to NULL until
1036 * we have drained - to drain the outgoing packets, we need to
1037 * allow incoming packets.
1038 */
1039 spin_lock_irqsave(&device->channel->inbound_lock, flags);
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001040 hv_set_drvdata(device, NULL);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001041 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
1042
1043 /* Close the channel */
1044 vmbus_close(device->channel);
1045
1046 kfree(stor_device);
1047 return 0;
1048}
1049
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -07001050static int storvsc_do_io(struct hv_device *device,
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001051 struct hv_storvsc_request *request)
1052{
1053 struct storvsc_device *stor_device;
1054 struct vstor_packet *vstor_packet;
1055 int ret = 0;
1056
1057 vstor_packet = &request->vstor_packet;
1058 stor_device = get_out_stor_device(device);
1059
1060 if (!stor_device)
1061 return -ENODEV;
1062
1063
1064 request->device = device;
1065
1066
1067 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
1068
1069 vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
1070
1071
K. Y. Srinivasan6b2f9492012-01-12 12:38:05 -08001072 vstor_packet->vm_srb.sense_info_length = STORVSC_SENSE_BUFFER_SIZE;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001073
1074
1075 vstor_packet->vm_srb.data_transfer_length =
1076 request->data_buffer.len;
1077
1078 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
1079
1080 if (request->data_buffer.len) {
1081 ret = vmbus_sendpacket_multipagebuffer(device->channel,
1082 &request->data_buffer,
1083 vstor_packet,
1084 sizeof(struct vstor_packet),
1085 (unsigned long)request);
1086 } else {
1087 ret = vmbus_sendpacket(device->channel, vstor_packet,
1088 sizeof(struct vstor_packet),
1089 (unsigned long)request,
1090 VM_PKT_DATA_INBAND,
1091 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1092 }
1093
1094 if (ret != 0)
1095 return ret;
1096
1097 atomic_inc(&stor_device->num_outstanding_req);
1098
1099 return ret;
1100}
1101
K. Y. Srinivasan5b60ace2011-05-10 07:54:25 -07001102static int storvsc_device_alloc(struct scsi_device *sdevice)
1103{
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001104 struct stor_mem_pools *memp;
1105 int number = STORVSC_MIN_BUF_NR;
1106
1107 memp = kzalloc(sizeof(struct stor_mem_pools), GFP_KERNEL);
1108 if (!memp)
1109 return -ENOMEM;
1110
1111 memp->request_pool =
1112 kmem_cache_create(dev_name(&sdevice->sdev_dev),
1113 sizeof(struct storvsc_cmd_request), 0,
1114 SLAB_HWCACHE_ALIGN, NULL);
1115
1116 if (!memp->request_pool)
1117 goto err0;
1118
1119 memp->request_mempool = mempool_create(number, mempool_alloc_slab,
1120 mempool_free_slab,
1121 memp->request_pool);
1122
1123 if (!memp->request_mempool)
1124 goto err1;
1125
1126 sdevice->hostdata = memp;
1127
K. Y. Srinivasan5b60ace2011-05-10 07:54:25 -07001128 return 0;
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001129
1130err1:
1131 kmem_cache_destroy(memp->request_pool);
1132
1133err0:
1134 kfree(memp);
1135 return -ENOMEM;
1136}
1137
1138static void storvsc_device_destroy(struct scsi_device *sdevice)
1139{
1140 struct stor_mem_pools *memp = sdevice->hostdata;
1141
1142 mempool_destroy(memp->request_mempool);
1143 kmem_cache_destroy(memp->request_pool);
1144 kfree(memp);
1145 sdevice->hostdata = NULL;
K. Y. Srinivasan5b60ace2011-05-10 07:54:25 -07001146}
1147
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001148static int storvsc_device_configure(struct scsi_device *sdevice)
1149{
1150 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
1151 STORVSC_MAX_IO_REQUESTS);
1152
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001153 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
1154
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001155 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001156
1157 return 0;
1158}
1159
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001160static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
1161 sector_t capacity, int *info)
1162{
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001163 sector_t nsect = capacity;
1164 sector_t cylinders = nsect;
1165 int heads, sectors_pt;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001166
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001167 /*
1168 * We are making up these values; let us keep it simple.
1169 */
1170 heads = 0xff;
1171 sectors_pt = 0x3f; /* Sectors per track */
1172 sector_div(cylinders, heads * sectors_pt);
1173 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
1174 cylinders = 0xffff;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001175
1176 info[0] = heads;
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001177 info[1] = sectors_pt;
1178 info[2] = (int)cylinders;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001179
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001180 return 0;
1181}
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001182
K. Y. Srinivasan4b270c82012-01-12 12:37:58 -08001183static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001184{
K. Y. Srinivasan4b270c82012-01-12 12:37:58 -08001185 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
1186 struct hv_device *device = host_dev->dev;
1187
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001188 struct storvsc_device *stor_device;
1189 struct hv_storvsc_request *request;
1190 struct vstor_packet *vstor_packet;
1191 int ret, t;
1192
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001193
K. Y. Srinivasan1eaaddf2011-08-27 11:31:03 -07001194 stor_device = get_out_stor_device(device);
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001195 if (!stor_device)
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001196 return FAILED;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001197
1198 request = &stor_device->reset_request;
1199 vstor_packet = &request->vstor_packet;
1200
1201 init_completion(&request->wait_event);
1202
1203 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
1204 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
1205 vstor_packet->vm_srb.path_id = stor_device->path_id;
1206
1207 ret = vmbus_sendpacket(device->channel, vstor_packet,
1208 sizeof(struct vstor_packet),
1209 (unsigned long)&stor_device->reset_request,
1210 VM_PKT_DATA_INBAND,
1211 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1212 if (ret != 0)
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001213 return FAILED;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001214
K. Y. Srinivasan46d2eb62011-06-16 13:16:36 -07001215 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001216 if (t == 0)
1217 return TIMEOUT_ERROR;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001218
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001219
1220 /*
1221 * At this point, all outstanding requests in the adapter
1222 * should have been flushed out and return to us
1223 */
1224
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001225 return SUCCESS;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001226}
1227
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001228static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd)
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001229{
1230 bool allowed = true;
1231 u8 scsi_op = scmnd->cmnd[0];
1232
1233 switch (scsi_op) {
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001234 /*
1235 * smartd sends this command and the host does not handle
1236 * this. So, don't send it.
1237 */
K. Y. Srinivasan41098f82011-10-14 21:31:57 -07001238 case SET_WINDOW:
K. Y. Srinivasan59e00e72011-11-08 09:01:42 -08001239 scmnd->result = ILLEGAL_REQUEST << 16;
K. Y. Srinivasan41098f82011-10-14 21:31:57 -07001240 allowed = false;
1241 break;
1242 default:
1243 break;
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001244 }
1245 return allowed;
1246}
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001247
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001248static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001249{
1250 int ret;
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001251 struct hv_host_device *host_dev = shost_priv(host);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001252 struct hv_device *dev = host_dev->dev;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001253 struct hv_storvsc_request *request;
1254 struct storvsc_cmd_request *cmd_request;
1255 unsigned int request_size = 0;
1256 int i;
1257 struct scatterlist *sgl;
1258 unsigned int sg_count = 0;
1259 struct vmscsi_request *vm_srb;
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001260 struct stor_mem_pools *memp = scmnd->device->hostdata;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001261
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001262 if (!storvsc_scsi_cmd_ok(scmnd)) {
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001263 scmnd->scsi_done(scmnd);
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001264 return 0;
1265 }
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001266
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001267 request_size = sizeof(struct storvsc_cmd_request);
1268
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001269 cmd_request = mempool_alloc(memp->request_mempool,
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001270 GFP_ATOMIC);
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001271
1272 /*
1273 * We might be invoked in an interrupt context; hence
1274 * mempool_alloc() can fail.
1275 */
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001276 if (!cmd_request)
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001277 return SCSI_MLQUEUE_DEVICE_BUSY;
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001278
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -08001279 memset(cmd_request, 0, sizeof(struct storvsc_cmd_request));
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001280
1281 /* Setup the cmd request */
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001282 cmd_request->cmd = scmnd;
1283
1284 scmnd->host_scribble = (unsigned char *)cmd_request;
1285
1286 request = &cmd_request->request;
1287 vm_srb = &request->vstor_packet.vm_srb;
1288
1289
1290 /* Build the SRB */
1291 switch (scmnd->sc_data_direction) {
1292 case DMA_TO_DEVICE:
1293 vm_srb->data_in = WRITE_TYPE;
1294 break;
1295 case DMA_FROM_DEVICE:
1296 vm_srb->data_in = READ_TYPE;
1297 break;
1298 default:
1299 vm_srb->data_in = UNKNOWN_TYPE;
1300 break;
1301 }
1302
K. Y. Srinivasan93a1bf42012-01-12 12:38:02 -08001303 request->cmd = cmd_request;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001304
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001305 vm_srb->port_number = host_dev->port;
1306 vm_srb->path_id = scmnd->device->channel;
1307 vm_srb->target_id = scmnd->device->id;
1308 vm_srb->lun = scmnd->device->lun;
1309
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001310 vm_srb->cdb_length = scmnd->cmd_len;
1311
1312 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
1313
1314 request->sense_buffer = scmnd->sense_buffer;
1315
1316
1317 request->data_buffer.len = scsi_bufflen(scmnd);
1318 if (scsi_sg_count(scmnd)) {
1319 sgl = (struct scatterlist *)scsi_sglist(scmnd);
1320 sg_count = scsi_sg_count(scmnd);
1321
1322 /* check if we need to bounce the sgl */
1323 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
1324 cmd_request->bounce_sgl =
1325 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
K. Y. Srinivasan6e8087a2011-12-07 07:15:52 -08001326 scsi_bufflen(scmnd),
1327 vm_srb->data_in);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001328 if (!cmd_request->bounce_sgl) {
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001329 ret = SCSI_MLQUEUE_HOST_BUSY;
1330 goto queue_error;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001331 }
1332
1333 cmd_request->bounce_sgl_count =
1334 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
1335 PAGE_SHIFT;
1336
K. Y. Srinivasanfa23b8c2011-08-27 11:31:21 -07001337 if (vm_srb->data_in == WRITE_TYPE)
1338 copy_to_bounce_buffer(sgl,
1339 cmd_request->bounce_sgl,
1340 scsi_sg_count(scmnd));
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001341
1342 sgl = cmd_request->bounce_sgl;
1343 sg_count = cmd_request->bounce_sgl_count;
1344 }
1345
1346 request->data_buffer.offset = sgl[0].offset;
1347
1348 for (i = 0; i < sg_count; i++)
1349 request->data_buffer.pfn_array[i] =
1350 page_to_pfn(sg_page((&sgl[i])));
1351
1352 } else if (scsi_sglist(scmnd)) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001353 request->data_buffer.offset =
1354 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
1355 request->data_buffer.pfn_array[0] =
1356 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
1357 }
1358
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001359 /* Invokes the vsc to start an IO */
K. Y. Srinivasan636f0fd2011-05-10 07:54:50 -07001360 ret = storvsc_do_io(dev, &cmd_request->request);
1361
K. Y. Srinivasand2598f02011-08-25 09:48:58 -07001362 if (ret == -EAGAIN) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001363 /* no more space */
1364
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001365 if (cmd_request->bounce_sgl_count) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001366 destroy_bounce_buffer(cmd_request->bounce_sgl,
K. Y. Srinivasan70691ec2011-08-27 11:31:29 -07001367 cmd_request->bounce_sgl_count);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001368
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001369 ret = SCSI_MLQUEUE_DEVICE_BUSY;
1370 goto queue_error;
1371 }
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001372 }
1373
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001374 return 0;
1375
1376queue_error:
1377 mempool_free(cmd_request, memp->request_mempool);
1378 scmnd->host_scribble = NULL;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001379 return ret;
1380}
1381
Hank Janssenbef4a342009-07-13 16:01:31 -07001382static struct scsi_host_template scsi_driver = {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001383 .module = THIS_MODULE,
1384 .name = "storvsc_host_t",
1385 .bios_param = storvsc_get_chs,
1386 .queuecommand = storvsc_queuecommand,
1387 .eh_host_reset_handler = storvsc_host_reset_handler,
1388 .slave_alloc = storvsc_device_alloc,
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001389 .slave_destroy = storvsc_device_destroy,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001390 .slave_configure = storvsc_device_configure,
1391 .cmd_per_lun = 1,
1392 /* 64 max_queue * 1 target */
Lars Lindley0686e4f2010-03-11 23:51:23 +01001393 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001394 .this_id = -1,
Bill Pemberton454f18a2009-07-27 16:47:24 -04001395 /* no use setting to 0 since ll_blk_rw reset it to 1 */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001396 /* currently 32 */
1397 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
K. Y. Srinivasan039db522011-12-01 04:59:16 -08001398 .use_clustering = DISABLE_CLUSTERING,
Bill Pemberton454f18a2009-07-27 16:47:24 -04001399 /* Make sure we dont get a sg segment crosses a page boundary */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001400 .dma_boundary = PAGE_SIZE-1,
Hank Janssenbef4a342009-07-13 16:01:31 -07001401};
1402
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001403enum {
1404 SCSI_GUID,
1405 IDE_GUID,
1406};
1407
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001408static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001409 /* SCSI guid */
1410 { VMBUS_DEVICE(0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001411 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f)
1412 .driver_data = SCSI_GUID },
K. Y. Srinivasan21e37742011-08-27 11:31:18 -07001413 /* IDE guid */
1414 { VMBUS_DEVICE(0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001415 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5)
1416 .driver_data = IDE_GUID },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001417 { },
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001418};
Hank Janssenbef4a342009-07-13 16:01:31 -07001419
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001420MODULE_DEVICE_TABLE(vmbus, id_table);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001421
K. Y. Srinivasan84946892011-09-13 10:59:38 -07001422static int storvsc_probe(struct hv_device *device,
1423 const struct hv_vmbus_device_id *dev_id)
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001424{
1425 int ret;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001426 struct Scsi_Host *host;
1427 struct hv_host_device *host_dev;
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001428 bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001429 int target = 0;
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001430 struct storvsc_device *stor_device;
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001431
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001432 host = scsi_host_alloc(&scsi_driver,
1433 sizeof(struct hv_host_device));
1434 if (!host)
1435 return -ENOMEM;
1436
K. Y. Srinivasan7f33f302011-11-08 09:01:44 -08001437 host_dev = shost_priv(host);
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001438 memset(host_dev, 0, sizeof(struct hv_host_device));
1439
1440 host_dev->port = host->host_no;
1441 host_dev->dev = device;
1442
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -08001443
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001444 stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001445 if (!stor_device) {
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001446 ret = -ENOMEM;
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001447 goto err_out0;
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001448 }
1449
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001450 stor_device->destroy = false;
1451 init_waitqueue_head(&stor_device->waiting_to_drain);
1452 stor_device->device = device;
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001453 stor_device->host = host;
1454 hv_set_drvdata(device, stor_device);
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001455
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001456 stor_device->port_number = host->host_no;
1457 ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001458 if (ret)
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001459 goto err_out1;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001460
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001461 host_dev->path = stor_device->path_id;
1462 host_dev->target = stor_device->target_id;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001463
1464 /* max # of devices per target */
1465 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
1466 /* max # of targets per channel */
1467 host->max_id = STORVSC_MAX_TARGETS;
1468 /* max # of channels */
1469 host->max_channel = STORVSC_MAX_CHANNELS - 1;
Mike Sterlingcf55f4a2011-09-06 16:10:55 -07001470 /* max cmd length */
1471 host->max_cmd_len = STORVSC_MAX_CMD_LEN;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001472
1473 /* Register the HBA and start the scsi bus scan */
1474 ret = scsi_add_host(host, &device->device);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001475 if (ret != 0)
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001476 goto err_out2;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001477
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001478 if (!dev_is_ide) {
1479 scsi_scan_host(host);
K. Y. Srinivasan59d22952012-01-12 12:37:55 -08001480 } else {
1481 target = (device->dev_instance.b[5] << 8 |
1482 device->dev_instance.b[4]);
1483 ret = scsi_add_device(host, 0, target, 0);
1484 if (ret) {
1485 scsi_remove_host(host);
1486 goto err_out2;
1487 }
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001488 }
1489 return 0;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001490
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001491err_out2:
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001492 /*
1493 * Once we have connected with the host, we would need to
1494 * to invoke storvsc_dev_remove() to rollback this state and
1495 * this call also frees up the stor_device; hence the jump around
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001496 * err_out1 label.
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001497 */
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001498 storvsc_dev_remove(device);
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001499 goto err_out0;
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001500
1501err_out1:
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001502 kfree(stor_device);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001503
1504err_out0:
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001505 scsi_host_put(host);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001506 return ret;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001507}
1508
K. Y. Srinivasanddcbf652012-01-12 12:37:59 -08001509static int storvsc_remove(struct hv_device *dev)
1510{
1511 struct storvsc_device *stor_device = hv_get_drvdata(dev);
1512 struct Scsi_Host *host = stor_device->host;
1513
1514 scsi_remove_host(host);
1515 storvsc_dev_remove(dev);
1516 scsi_host_put(host);
1517
1518 return 0;
1519}
1520
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -07001521static struct hv_driver storvsc_drv = {
K. Y. Srinivasanfafb0ef2011-11-08 09:01:46 -08001522 .name = KBUILD_MODNAME,
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001523 .id_table = id_table,
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -07001524 .probe = storvsc_probe,
1525 .remove = storvsc_remove,
K. Y. Srinivasan39ae6fa2011-05-10 07:54:46 -07001526};
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -07001527
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -07001528static int __init storvsc_drv_init(void)
Hank Janssenbef4a342009-07-13 16:01:31 -07001529{
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001530 u32 max_outstanding_req_per_channel;
1531
1532 /*
1533 * Divide the ring buffer data size (which is 1 page less
1534 * than the ring buffer size since that page is reserved for
1535 * the ring buffer indices) by the max request size (which is
1536 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
1537 */
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001538 max_outstanding_req_per_channel =
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001539 ((storvsc_ringbuffer_size - PAGE_SIZE) /
1540 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
1541 sizeof(struct vstor_packet) + sizeof(u64),
1542 sizeof(u64)));
Hank Janssenbef4a342009-07-13 16:01:31 -07001543
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001544 if (max_outstanding_req_per_channel <
K. Y. Srinivasanf8feed02011-05-10 07:54:24 -07001545 STORVSC_MAX_IO_REQUESTS)
K. Y. Srinivasanb06efc12011-08-25 09:49:10 -07001546 return -EINVAL;
Hank Janssenbef4a342009-07-13 16:01:31 -07001547
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001548 return vmbus_driver_register(&storvsc_drv);
Hank Janssenbef4a342009-07-13 16:01:31 -07001549}
1550
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -07001551static void __exit storvsc_drv_exit(void)
Hank Janssenbef4a342009-07-13 16:01:31 -07001552{
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001553 vmbus_driver_unregister(&storvsc_drv);
Hank Janssenbef4a342009-07-13 16:01:31 -07001554}
1555
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001556MODULE_LICENSE("GPL");
Hank Janssen26c14cc2010-02-11 23:02:42 +00001557MODULE_VERSION(HV_DRV_VERSION);
Stephen Hemminger3afc7cc32010-05-06 21:44:45 -07001558MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -07001559module_init(storvsc_drv_init);
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -07001560module_exit(storvsc_drv_exit);