blob: 629edd17fd9b682fecc123f810ae4ec380a92eb0 [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 */
117#define CDB16GENERIC_LENGTH 0x10
118
119#ifndef SENSE_BUFFER_SIZE
120#define SENSE_BUFFER_SIZE 0x12
121#endif
122
123#define MAX_DATA_BUF_LEN_WITH_PADDING 0x14
124
125struct vmscsi_request {
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800126 u16 length;
127 u8 srb_status;
128 u8 scsi_status;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700129
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800130 u8 port_number;
131 u8 path_id;
132 u8 target_id;
133 u8 lun;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700134
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800135 u8 cdb_length;
136 u8 sense_info_length;
137 u8 data_in;
138 u8 reserved;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700139
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800140 u32 data_transfer_length;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700141
142 union {
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800143 u8 cdb[CDB16GENERIC_LENGTH];
144 u8 sense_data[SENSE_BUFFER_SIZE];
145 u8 reserved_array[MAX_DATA_BUF_LEN_WITH_PADDING];
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700146 };
147} __attribute((packed));
148
149
150/*
151 * This structure is sent during the intialization phase to get the different
152 * properties of the channel.
153 */
154struct vmstorage_channel_properties {
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800155 u16 protocol_version;
156 u8 path_id;
157 u8 target_id;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700158
159 /* Note: port number is only really known on the client side */
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800160 u32 port_number;
161 u32 flags;
162 u32 max_transfer_bytes;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700163
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800164 /*
165 * This id is unique for each channel and will correspond with
166 * vendor specific data in the inquiry data.
167 */
168
169 u64 unique_id;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700170} __packed;
171
172/* This structure is sent during the storage protocol negotiations. */
173struct vmstorage_protocol_version {
174 /* Major (MSW) and minor (LSW) version numbers. */
K. Y. Srinivasan85904a52012-01-12 12:38:04 -0800175 u16 major_minor;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700176
177 /*
178 * Revision number is auto-incremented whenever this file is changed
179 * (See FILL_VMSTOR_REVISION macro above). Mismatch does not
180 * definitely indicate incompatibility--but it does indicate mismatched
181 * builds.
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800182 * This is only used on the windows side. Just set it to 0.
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700183 */
K. Y. Srinivasan85904a52012-01-12 12:38:04 -0800184 u16 revision;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700185} __packed;
186
187/* Channel Property Flags */
188#define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
189#define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
190
191struct vstor_packet {
192 /* Requested operation type */
193 enum vstor_packet_operation operation;
194
195 /* Flags - see below for values */
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800196 u32 flags;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700197
198 /* Status of the request returned from the server side. */
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800199 u32 status;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700200
201 /* Data payload area */
202 union {
203 /*
204 * Structure used to forward SCSI commands from the
205 * client to the server.
206 */
207 struct vmscsi_request vm_srb;
208
209 /* Structure used to query channel properties. */
210 struct vmstorage_channel_properties storage_channel_properties;
211
212 /* Used during version negotiations. */
213 struct vmstorage_protocol_version version;
214 };
215} __packed;
216
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700217/*
K. Y. Srinivasan09f03552012-01-12 12:37:54 -0800218 * Packet Flags:
219 *
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700220 * This flag indicates that the server should send back a completion for this
221 * packet.
222 */
K. Y. Srinivasan09f03552012-01-12 12:37:54 -0800223
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700224#define REQUEST_COMPLETION_FLAG 0x1
225
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700226#define STORVSC_MAX_IO_REQUESTS 128
227
228/*
229 * In Hyper-V, each port/path/target maps to 1 scsi host adapter. In
230 * reality, the path/target is not used (ie always set to 0) so our
231 * scsi host adapter essentially has 1 bus with 1 target that contains
232 * up to 256 luns.
233 */
234#define STORVSC_MAX_LUNS_PER_TARGET 64
235#define STORVSC_MAX_TARGETS 1
236#define STORVSC_MAX_CHANNELS 1
Mike Sterlingcf55f4a2011-09-06 16:10:55 -0700237#define STORVSC_MAX_CMD_LEN 16
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700238
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700239/* Matches Windows-end */
240enum storvsc_request_type {
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800241 WRITE_TYPE = 0,
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700242 READ_TYPE,
243 UNKNOWN_TYPE,
244};
245
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800246/*
247 * SRB status codes and masks; a subset of the codes used here.
248 */
249
250#define SRB_STATUS_AUTOSENSE_VALID 0x80
251#define SRB_STATUS_INVALID_LUN 0x20
252#define SRB_STATUS_SUCCESS 0x01
253#define SRB_STATUS_ERROR 0x04
254
255
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700256
257struct hv_storvsc_request {
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700258 struct hv_device *device;
259
260 /* Synchronize the request/response if needed */
261 struct completion wait_event;
262
263 unsigned char *sense_buffer;
K. Y. Srinivasan93a1bf42012-01-12 12:38:02 -0800264 struct storvsc_cmd_request *cmd;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700265 struct hv_multipage_buffer data_buffer;
266
267 struct vstor_packet vstor_packet;
268};
269
270
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700271/* A storvsc device is a device object that contains a vmbus channel */
272struct storvsc_device {
273 struct hv_device *device;
274
275 bool destroy;
276 bool drain_notify;
277 atomic_t num_outstanding_req;
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700278 struct Scsi_Host *host;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700279
280 wait_queue_head_t waiting_to_drain;
281
282 /*
283 * Each unique Port/Path/Target represents 1 channel ie scsi
284 * controller. In reality, the pathid, targetid is always 0
285 * and the port is set by us
286 */
287 unsigned int port_number;
288 unsigned char path_id;
289 unsigned char target_id;
290
291 /* Used for vsc/vsp channel reset process */
292 struct hv_storvsc_request init_request;
293 struct hv_storvsc_request reset_request;
294};
295
K. Y. Srinivasance3e3012011-12-01 04:59:20 -0800296struct stor_mem_pools {
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700297 struct kmem_cache *request_pool;
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -0800298 mempool_t *request_mempool;
K. Y. Srinivasance3e3012011-12-01 04:59:20 -0800299};
300
301struct hv_host_device {
302 struct hv_device *dev;
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700303 unsigned int port;
304 unsigned char path;
305 unsigned char target;
306};
307
308struct storvsc_cmd_request {
309 struct list_head entry;
310 struct scsi_cmnd *cmd;
311
312 unsigned int bounce_sgl_count;
313 struct scatterlist *bounce_sgl;
314
315 struct hv_storvsc_request request;
316};
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700317
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800318struct storvsc_scan_work {
319 struct work_struct work;
320 struct Scsi_Host *host;
321 uint lun;
322};
323
324static void storvsc_bus_scan(struct work_struct *work)
325{
326 struct storvsc_scan_work *wrk;
327 int id, order_id;
328
329 wrk = container_of(work, struct storvsc_scan_work, work);
330 for (id = 0; id < wrk->host->max_id; ++id) {
331 if (wrk->host->reverse_ordering)
332 order_id = wrk->host->max_id - id - 1;
333 else
334 order_id = id;
335
336 scsi_scan_target(&wrk->host->shost_gendev, 0,
337 order_id, SCAN_WILD_CARD, 1);
338 }
339 kfree(wrk);
340}
341
K. Y. Srinivasanb4017312011-11-08 09:01:50 -0800342static void storvsc_remove_lun(struct work_struct *work)
343{
344 struct storvsc_scan_work *wrk;
345 struct scsi_device *sdev;
346
347 wrk = container_of(work, struct storvsc_scan_work, work);
348 if (!scsi_host_get(wrk->host))
349 goto done;
350
351 sdev = scsi_device_lookup(wrk->host, 0, 0, wrk->lun);
352
353 if (sdev) {
354 scsi_remove_device(sdev);
355 scsi_device_put(sdev);
356 }
357 scsi_host_put(wrk->host);
358
359done:
360 kfree(wrk);
361}
362
K. Y. Srinivasana8c18c52012-01-12 12:38:00 -0800363/*
364 * We can get incoming messages from the host that are not in response to
365 * messages that we have sent out. An example of this would be messages
366 * received by the guest to notify dynamic addition/removal of LUNs. To
367 * deal with potential race conditions where the driver may be in the
368 * midst of being unloaded when we might receive an unsolicited message
369 * from the host, we have implemented a mechanism to gurantee sequential
370 * consistency:
371 *
372 * 1) Once the device is marked as being destroyed, we will fail all
373 * outgoing messages.
374 * 2) We permit incoming messages when the device is being destroyed,
375 * only to properly account for messages already sent out.
376 */
377
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700378static inline struct storvsc_device *get_out_stor_device(
379 struct hv_device *device)
380{
381 struct storvsc_device *stor_device;
382
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700383 stor_device = hv_get_drvdata(device);
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700384
385 if (stor_device && stor_device->destroy)
386 stor_device = NULL;
387
388 return stor_device;
389}
390
391
392static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
393{
394 dev->drain_notify = true;
395 wait_event(dev->waiting_to_drain,
396 atomic_read(&dev->num_outstanding_req) == 0);
397 dev->drain_notify = false;
398}
Hank Janssenbef4a342009-07-13 16:01:31 -0700399
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700400static inline struct storvsc_device *get_in_stor_device(
401 struct hv_device *device)
402{
403 struct storvsc_device *stor_device;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700404
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700405 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700406
407 if (!stor_device)
408 goto get_in_err;
409
410 /*
411 * If the device is being destroyed; allow incoming
412 * traffic only to cleanup outstanding requests.
413 */
414
415 if (stor_device->destroy &&
416 (atomic_read(&stor_device->num_outstanding_req) == 0))
417 stor_device = NULL;
418
419get_in_err:
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700420 return stor_device;
421
422}
423
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800424static void destroy_bounce_buffer(struct scatterlist *sgl,
425 unsigned int sg_count)
426{
427 int i;
428 struct page *page_buf;
429
430 for (i = 0; i < sg_count; i++) {
431 page_buf = sg_page((&sgl[i]));
432 if (page_buf != NULL)
433 __free_page(page_buf);
434 }
435
436 kfree(sgl);
437}
438
439static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
440{
441 int i;
442
443 /* No need to check */
444 if (sg_count < 2)
445 return -1;
446
447 /* We have at least 2 sg entries */
448 for (i = 0; i < sg_count; i++) {
449 if (i == 0) {
450 /* make sure 1st one does not have hole */
451 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
452 return i;
453 } else if (i == sg_count - 1) {
454 /* make sure last one does not have hole */
455 if (sgl[i].offset != 0)
456 return i;
457 } else {
458 /* make sure no hole in the middle */
459 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
460 return i;
461 }
462 }
463 return -1;
464}
465
466static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
467 unsigned int sg_count,
468 unsigned int len,
469 int write)
470{
471 int i;
472 int num_pages;
473 struct scatterlist *bounce_sgl;
474 struct page *page_buf;
475 unsigned int buf_len = ((write == WRITE_TYPE) ? 0 : PAGE_SIZE);
476
477 num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
478
479 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
480 if (!bounce_sgl)
481 return NULL;
482
483 for (i = 0; i < num_pages; i++) {
484 page_buf = alloc_page(GFP_ATOMIC);
485 if (!page_buf)
486 goto cleanup;
487 sg_set_page(&bounce_sgl[i], page_buf, buf_len, 0);
488 }
489
490 return bounce_sgl;
491
492cleanup:
493 destroy_bounce_buffer(bounce_sgl, num_pages);
494 return NULL;
495}
496
497/* Assume the original sgl has enough room */
498static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
499 struct scatterlist *bounce_sgl,
500 unsigned int orig_sgl_count,
501 unsigned int bounce_sgl_count)
502{
503 int i;
504 int j = 0;
505 unsigned long src, dest;
506 unsigned int srclen, destlen, copylen;
507 unsigned int total_copied = 0;
508 unsigned long bounce_addr = 0;
509 unsigned long dest_addr = 0;
510 unsigned long flags;
511
512 local_irq_save(flags);
513
514 for (i = 0; i < orig_sgl_count; i++) {
515 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
516 KM_IRQ0) + orig_sgl[i].offset;
517 dest = dest_addr;
518 destlen = orig_sgl[i].length;
519
520 if (bounce_addr == 0)
521 bounce_addr =
522 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
523 KM_IRQ0);
524
525 while (destlen) {
526 src = bounce_addr + bounce_sgl[j].offset;
527 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
528
529 copylen = min(srclen, destlen);
530 memcpy((void *)dest, (void *)src, copylen);
531
532 total_copied += copylen;
533 bounce_sgl[j].offset += copylen;
534 destlen -= copylen;
535 dest += copylen;
536
537 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
538 /* full */
539 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
540 j++;
541
542 /*
543 * It is possible that the number of elements
544 * in the bounce buffer may not be equal to
545 * the number of elements in the original
546 * scatter list. Handle this correctly.
547 */
548
549 if (j == bounce_sgl_count) {
550 /*
551 * We are done; cleanup and return.
552 */
553 kunmap_atomic((void *)(dest_addr -
554 orig_sgl[i].offset),
555 KM_IRQ0);
556 local_irq_restore(flags);
557 return total_copied;
558 }
559
560 /* if we need to use another bounce buffer */
561 if (destlen || i != orig_sgl_count - 1)
562 bounce_addr =
563 (unsigned long)kmap_atomic(
564 sg_page((&bounce_sgl[j])), KM_IRQ0);
565 } else if (destlen == 0 && i == orig_sgl_count - 1) {
566 /* unmap the last bounce that is < PAGE_SIZE */
567 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
568 }
569 }
570
571 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
572 KM_IRQ0);
573 }
574
575 local_irq_restore(flags);
576
577 return total_copied;
578}
579
580/* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
581static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
582 struct scatterlist *bounce_sgl,
583 unsigned int orig_sgl_count)
584{
585 int i;
586 int j = 0;
587 unsigned long src, dest;
588 unsigned int srclen, destlen, copylen;
589 unsigned int total_copied = 0;
590 unsigned long bounce_addr = 0;
591 unsigned long src_addr = 0;
592 unsigned long flags;
593
594 local_irq_save(flags);
595
596 for (i = 0; i < orig_sgl_count; i++) {
597 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
598 KM_IRQ0) + orig_sgl[i].offset;
599 src = src_addr;
600 srclen = orig_sgl[i].length;
601
602 if (bounce_addr == 0)
603 bounce_addr =
604 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
605 KM_IRQ0);
606
607 while (srclen) {
608 /* assume bounce offset always == 0 */
609 dest = bounce_addr + bounce_sgl[j].length;
610 destlen = PAGE_SIZE - bounce_sgl[j].length;
611
612 copylen = min(srclen, destlen);
613 memcpy((void *)dest, (void *)src, copylen);
614
615 total_copied += copylen;
616 bounce_sgl[j].length += copylen;
617 srclen -= copylen;
618 src += copylen;
619
620 if (bounce_sgl[j].length == PAGE_SIZE) {
621 /* full..move to next entry */
622 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
623 j++;
624
625 /* if we need to use another bounce buffer */
626 if (srclen || i != orig_sgl_count - 1)
627 bounce_addr =
628 (unsigned long)kmap_atomic(
629 sg_page((&bounce_sgl[j])), KM_IRQ0);
630
631 } else if (srclen == 0 && i == orig_sgl_count - 1) {
632 /* unmap the last bounce that is < PAGE_SIZE */
633 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
634 }
635 }
636
637 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
638 }
639
640 local_irq_restore(flags);
641
642 return total_copied;
643}
644
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700645static int storvsc_channel_init(struct hv_device *device)
646{
647 struct storvsc_device *stor_device;
648 struct hv_storvsc_request *request;
649 struct vstor_packet *vstor_packet;
650 int ret, t;
651
652 stor_device = get_out_stor_device(device);
653 if (!stor_device)
654 return -ENODEV;
655
656 request = &stor_device->init_request;
657 vstor_packet = &request->vstor_packet;
658
659 /*
660 * Now, initiate the vsc/vsp initialization protocol on the open
661 * channel
662 */
663 memset(request, 0, sizeof(struct hv_storvsc_request));
664 init_completion(&request->wait_event);
665 vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
666 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
667
668 ret = vmbus_sendpacket(device->channel, vstor_packet,
669 sizeof(struct vstor_packet),
670 (unsigned long)request,
671 VM_PKT_DATA_INBAND,
672 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
673 if (ret != 0)
674 goto cleanup;
675
676 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
677 if (t == 0) {
678 ret = -ETIMEDOUT;
679 goto cleanup;
680 }
681
682 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
683 vstor_packet->status != 0)
684 goto cleanup;
685
686
687 /* reuse the packet for version range supported */
688 memset(vstor_packet, 0, sizeof(struct vstor_packet));
689 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
690 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
691
K. Y. Srinivasan85904a52012-01-12 12:38:04 -0800692 vstor_packet->version.major_minor =
693 storvsc_get_version(VMSTOR_CURRENT_MAJOR, VMSTOR_CURRENT_MINOR);
694
695 /*
696 * The revision number is only used in Windows; set it to 0.
697 */
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800698 vstor_packet->version.revision = 0;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700699
700 ret = vmbus_sendpacket(device->channel, vstor_packet,
701 sizeof(struct vstor_packet),
702 (unsigned long)request,
703 VM_PKT_DATA_INBAND,
704 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
705 if (ret != 0)
706 goto cleanup;
707
708 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
709 if (t == 0) {
710 ret = -ETIMEDOUT;
711 goto cleanup;
712 }
713
714 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
715 vstor_packet->status != 0)
716 goto cleanup;
717
718
719 memset(vstor_packet, 0, sizeof(struct vstor_packet));
720 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
721 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
722 vstor_packet->storage_channel_properties.port_number =
723 stor_device->port_number;
724
725 ret = vmbus_sendpacket(device->channel, vstor_packet,
726 sizeof(struct vstor_packet),
727 (unsigned long)request,
728 VM_PKT_DATA_INBAND,
729 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
730
731 if (ret != 0)
732 goto cleanup;
733
734 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
735 if (t == 0) {
736 ret = -ETIMEDOUT;
737 goto cleanup;
738 }
739
740 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
741 vstor_packet->status != 0)
742 goto cleanup;
743
744 stor_device->path_id = vstor_packet->storage_channel_properties.path_id;
745 stor_device->target_id
746 = vstor_packet->storage_channel_properties.target_id;
747
748 memset(vstor_packet, 0, sizeof(struct vstor_packet));
749 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
750 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
751
752 ret = vmbus_sendpacket(device->channel, vstor_packet,
753 sizeof(struct vstor_packet),
754 (unsigned long)request,
755 VM_PKT_DATA_INBAND,
756 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
757
758 if (ret != 0)
759 goto cleanup;
760
761 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
762 if (t == 0) {
763 ret = -ETIMEDOUT;
764 goto cleanup;
765 }
766
767 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
768 vstor_packet->status != 0)
769 goto cleanup;
770
771
772cleanup:
773 return ret;
774}
775
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800776
777static void storvsc_command_completion(struct hv_storvsc_request *request)
778{
K. Y. Srinivasan93a1bf42012-01-12 12:38:02 -0800779 struct storvsc_cmd_request *cmd_request = request->cmd;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800780 struct scsi_cmnd *scmnd = cmd_request->cmd;
781 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
782 void (*scsi_done_fn)(struct scsi_cmnd *);
783 struct scsi_sense_hdr sense_hdr;
784 struct vmscsi_request *vm_srb;
785 struct storvsc_scan_work *wrk;
786 struct stor_mem_pools *memp = scmnd->device->hostdata;
787
788 vm_srb = &request->vstor_packet.vm_srb;
789 if (cmd_request->bounce_sgl_count) {
790 if (vm_srb->data_in == READ_TYPE)
791 copy_from_bounce_buffer(scsi_sglist(scmnd),
792 cmd_request->bounce_sgl,
793 scsi_sg_count(scmnd),
794 cmd_request->bounce_sgl_count);
795 destroy_bounce_buffer(cmd_request->bounce_sgl,
796 cmd_request->bounce_sgl_count);
797 }
798
799 /*
800 * If there is an error; offline the device since all
801 * error recovery strategies would have already been
802 * deployed on the host side.
803 */
804 if (vm_srb->srb_status == SRB_STATUS_ERROR)
805 scmnd->result = DID_TARGET_FAILURE << 16;
806 else
807 scmnd->result = vm_srb->scsi_status;
808
809 /*
810 * If the LUN is invalid; remove the device.
811 */
812 if (vm_srb->srb_status == SRB_STATUS_INVALID_LUN) {
813 struct storvsc_device *stor_dev;
814 struct hv_device *dev = host_dev->dev;
815 struct Scsi_Host *host;
816
817 stor_dev = get_in_stor_device(dev);
818 host = stor_dev->host;
819
820 wrk = kmalloc(sizeof(struct storvsc_scan_work),
821 GFP_ATOMIC);
822 if (!wrk) {
823 scmnd->result = DID_TARGET_FAILURE << 16;
824 } else {
825 wrk->host = host;
826 wrk->lun = vm_srb->lun;
827 INIT_WORK(&wrk->work, storvsc_remove_lun);
828 schedule_work(&wrk->work);
829 }
830 }
831
832 if (scmnd->result) {
833 if (scsi_normalize_sense(scmnd->sense_buffer,
834 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
835 scsi_print_sense_hdr("storvsc", &sense_hdr);
836 }
837
838 scsi_set_resid(scmnd,
839 request->data_buffer.len -
840 vm_srb->data_transfer_length);
841
842 scsi_done_fn = scmnd->scsi_done;
843
844 scmnd->host_scribble = NULL;
845 scmnd->scsi_done = NULL;
846
847 scsi_done_fn(scmnd);
848
849 mempool_free(cmd_request, memp->request_mempool);
850}
851
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700852static void storvsc_on_io_completion(struct hv_device *device,
853 struct vstor_packet *vstor_packet,
854 struct hv_storvsc_request *request)
855{
856 struct storvsc_device *stor_device;
857 struct vstor_packet *stor_pkt;
858
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700859 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700860 stor_pkt = &request->vstor_packet;
861
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -0700862 /*
863 * The current SCSI handling on the host side does
864 * not correctly handle:
865 * INQUIRY command with page code parameter set to 0x80
866 * MODE_SENSE command with cmd[2] == 0x1c
867 *
868 * Setup srb and scsi status so this won't be fatal.
869 * We do this so we can distinguish truly fatal failues
870 * (srb status == 0x4) and off-line the device in that case.
871 */
872
873 if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
K. Y. Srinivasana8c18c52012-01-12 12:38:00 -0800874 (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -0700875 vstor_packet->vm_srb.scsi_status = 0;
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800876 vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -0700877 }
878
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700879
880 /* Copy over the status...etc */
881 stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
882 stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
883 stor_pkt->vm_srb.sense_info_length =
884 vstor_packet->vm_srb.sense_info_length;
885
886 if (vstor_packet->vm_srb.scsi_status != 0 ||
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800887 vstor_packet->vm_srb.srb_status != SRB_STATUS_SUCCESS){
Greg Kroah-Hartmand181daa2011-10-11 09:20:31 -0600888 dev_warn(&device->device,
889 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
890 stor_pkt->vm_srb.cdb[0],
891 vstor_packet->vm_srb.scsi_status,
892 vstor_packet->vm_srb.srb_status);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700893 }
894
895 if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
896 /* CHECK_CONDITION */
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800897 if (vstor_packet->vm_srb.srb_status &
898 SRB_STATUS_AUTOSENSE_VALID) {
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700899 /* autosense data available */
Greg Kroah-Hartmand181daa2011-10-11 09:20:31 -0600900 dev_warn(&device->device,
K. Y. Srinivasan41098f82011-10-14 21:31:57 -0700901 "stor pkt %p autosense data valid - len %d\n",
902 request,
903 vstor_packet->vm_srb.sense_info_length);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700904
905 memcpy(request->sense_buffer,
906 vstor_packet->vm_srb.sense_data,
907 vstor_packet->vm_srb.sense_info_length);
908
909 }
910 }
911
912 stor_pkt->vm_srb.data_transfer_length =
913 vstor_packet->vm_srb.data_transfer_length;
914
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800915 storvsc_command_completion(request);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700916
917 if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
918 stor_device->drain_notify)
919 wake_up(&stor_device->waiting_to_drain);
920
921
922}
923
924static void storvsc_on_receive(struct hv_device *device,
925 struct vstor_packet *vstor_packet,
926 struct hv_storvsc_request *request)
927{
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800928 struct storvsc_scan_work *work;
929 struct storvsc_device *stor_device;
930
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700931 switch (vstor_packet->operation) {
932 case VSTOR_OPERATION_COMPLETE_IO:
933 storvsc_on_io_completion(device, vstor_packet, request);
934 break;
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800935
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700936 case VSTOR_OPERATION_REMOVE_DEVICE:
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800937 case VSTOR_OPERATION_ENUMERATE_BUS:
938 stor_device = get_in_stor_device(device);
939 work = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
940 if (!work)
941 return;
942
943 INIT_WORK(&work->work, storvsc_bus_scan);
944 work->host = stor_device->host;
945 schedule_work(&work->work);
946 break;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700947
948 default:
949 break;
950 }
951}
952
953static void storvsc_on_channel_callback(void *context)
954{
955 struct hv_device *device = (struct hv_device *)context;
956 struct storvsc_device *stor_device;
957 u32 bytes_recvd;
958 u64 request_id;
959 unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
960 struct hv_storvsc_request *request;
961 int ret;
962
963
964 stor_device = get_in_stor_device(device);
965 if (!stor_device)
966 return;
967
968 do {
969 ret = vmbus_recvpacket(device->channel, packet,
970 ALIGN(sizeof(struct vstor_packet), 8),
971 &bytes_recvd, &request_id);
972 if (ret == 0 && bytes_recvd > 0) {
973
974 request = (struct hv_storvsc_request *)
975 (unsigned long)request_id;
976
977 if ((request == &stor_device->init_request) ||
978 (request == &stor_device->reset_request)) {
979
980 memcpy(&request->vstor_packet, packet,
981 sizeof(struct vstor_packet));
982 complete(&request->wait_event);
983 } else {
984 storvsc_on_receive(device,
985 (struct vstor_packet *)packet,
986 request);
987 }
988 } else {
989 break;
990 }
991 } while (1);
992
993 return;
994}
995
996static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
997{
998 struct vmstorage_channel_properties props;
999 int ret;
1000
1001 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
1002
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001003 ret = vmbus_open(device->channel,
1004 ring_size,
1005 ring_size,
1006 (void *)&props,
1007 sizeof(struct vmstorage_channel_properties),
1008 storvsc_on_channel_callback, device);
1009
1010 if (ret != 0)
1011 return ret;
1012
1013 ret = storvsc_channel_init(device);
1014
1015 return ret;
1016}
1017
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -07001018static int storvsc_dev_remove(struct hv_device *device)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001019{
1020 struct storvsc_device *stor_device;
1021 unsigned long flags;
1022
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001023 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001024
1025 spin_lock_irqsave(&device->channel->inbound_lock, flags);
1026 stor_device->destroy = true;
1027 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
1028
1029 /*
1030 * At this point, all outbound traffic should be disable. We
1031 * only allow inbound traffic (responses) to proceed so that
1032 * outstanding requests can be completed.
1033 */
1034
1035 storvsc_wait_to_drain(stor_device);
1036
1037 /*
1038 * Since we have already drained, we don't need to busy wait
1039 * as was done in final_release_stor_device()
1040 * Note that we cannot set the ext pointer to NULL until
1041 * we have drained - to drain the outgoing packets, we need to
1042 * allow incoming packets.
1043 */
1044 spin_lock_irqsave(&device->channel->inbound_lock, flags);
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001045 hv_set_drvdata(device, NULL);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001046 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
1047
1048 /* Close the channel */
1049 vmbus_close(device->channel);
1050
1051 kfree(stor_device);
1052 return 0;
1053}
1054
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -07001055static int storvsc_do_io(struct hv_device *device,
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001056 struct hv_storvsc_request *request)
1057{
1058 struct storvsc_device *stor_device;
1059 struct vstor_packet *vstor_packet;
1060 int ret = 0;
1061
1062 vstor_packet = &request->vstor_packet;
1063 stor_device = get_out_stor_device(device);
1064
1065 if (!stor_device)
1066 return -ENODEV;
1067
1068
1069 request->device = device;
1070
1071
1072 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
1073
1074 vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
1075
1076
1077 vstor_packet->vm_srb.sense_info_length = SENSE_BUFFER_SIZE;
1078
1079
1080 vstor_packet->vm_srb.data_transfer_length =
1081 request->data_buffer.len;
1082
1083 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
1084
1085 if (request->data_buffer.len) {
1086 ret = vmbus_sendpacket_multipagebuffer(device->channel,
1087 &request->data_buffer,
1088 vstor_packet,
1089 sizeof(struct vstor_packet),
1090 (unsigned long)request);
1091 } else {
1092 ret = vmbus_sendpacket(device->channel, vstor_packet,
1093 sizeof(struct vstor_packet),
1094 (unsigned long)request,
1095 VM_PKT_DATA_INBAND,
1096 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1097 }
1098
1099 if (ret != 0)
1100 return ret;
1101
1102 atomic_inc(&stor_device->num_outstanding_req);
1103
1104 return ret;
1105}
1106
K. Y. Srinivasan5b60ace2011-05-10 07:54:25 -07001107static int storvsc_device_alloc(struct scsi_device *sdevice)
1108{
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001109 struct stor_mem_pools *memp;
1110 int number = STORVSC_MIN_BUF_NR;
1111
1112 memp = kzalloc(sizeof(struct stor_mem_pools), GFP_KERNEL);
1113 if (!memp)
1114 return -ENOMEM;
1115
1116 memp->request_pool =
1117 kmem_cache_create(dev_name(&sdevice->sdev_dev),
1118 sizeof(struct storvsc_cmd_request), 0,
1119 SLAB_HWCACHE_ALIGN, NULL);
1120
1121 if (!memp->request_pool)
1122 goto err0;
1123
1124 memp->request_mempool = mempool_create(number, mempool_alloc_slab,
1125 mempool_free_slab,
1126 memp->request_pool);
1127
1128 if (!memp->request_mempool)
1129 goto err1;
1130
1131 sdevice->hostdata = memp;
1132
K. Y. Srinivasan5b60ace2011-05-10 07:54:25 -07001133 return 0;
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001134
1135err1:
1136 kmem_cache_destroy(memp->request_pool);
1137
1138err0:
1139 kfree(memp);
1140 return -ENOMEM;
1141}
1142
1143static void storvsc_device_destroy(struct scsi_device *sdevice)
1144{
1145 struct stor_mem_pools *memp = sdevice->hostdata;
1146
1147 mempool_destroy(memp->request_mempool);
1148 kmem_cache_destroy(memp->request_pool);
1149 kfree(memp);
1150 sdevice->hostdata = NULL;
K. Y. Srinivasan5b60ace2011-05-10 07:54:25 -07001151}
1152
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001153static int storvsc_device_configure(struct scsi_device *sdevice)
1154{
1155 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
1156 STORVSC_MAX_IO_REQUESTS);
1157
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001158 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
1159
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001160 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001161
1162 return 0;
1163}
1164
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001165static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
1166 sector_t capacity, int *info)
1167{
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001168 sector_t nsect = capacity;
1169 sector_t cylinders = nsect;
1170 int heads, sectors_pt;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001171
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001172 /*
1173 * We are making up these values; let us keep it simple.
1174 */
1175 heads = 0xff;
1176 sectors_pt = 0x3f; /* Sectors per track */
1177 sector_div(cylinders, heads * sectors_pt);
1178 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
1179 cylinders = 0xffff;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001180
1181 info[0] = heads;
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001182 info[1] = sectors_pt;
1183 info[2] = (int)cylinders;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001184
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001185 return 0;
1186}
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001187
K. Y. Srinivasan4b270c82012-01-12 12:37:58 -08001188static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001189{
K. Y. Srinivasan4b270c82012-01-12 12:37:58 -08001190 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
1191 struct hv_device *device = host_dev->dev;
1192
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001193 struct storvsc_device *stor_device;
1194 struct hv_storvsc_request *request;
1195 struct vstor_packet *vstor_packet;
1196 int ret, t;
1197
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001198
K. Y. Srinivasan1eaaddf2011-08-27 11:31:03 -07001199 stor_device = get_out_stor_device(device);
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001200 if (!stor_device)
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001201 return FAILED;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001202
1203 request = &stor_device->reset_request;
1204 vstor_packet = &request->vstor_packet;
1205
1206 init_completion(&request->wait_event);
1207
1208 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
1209 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
1210 vstor_packet->vm_srb.path_id = stor_device->path_id;
1211
1212 ret = vmbus_sendpacket(device->channel, vstor_packet,
1213 sizeof(struct vstor_packet),
1214 (unsigned long)&stor_device->reset_request,
1215 VM_PKT_DATA_INBAND,
1216 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1217 if (ret != 0)
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001218 return FAILED;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001219
K. Y. Srinivasan46d2eb62011-06-16 13:16:36 -07001220 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001221 if (t == 0)
1222 return TIMEOUT_ERROR;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001223
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001224
1225 /*
1226 * At this point, all outstanding requests in the adapter
1227 * should have been flushed out and return to us
1228 */
1229
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001230 return SUCCESS;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001231}
1232
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001233static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd)
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001234{
1235 bool allowed = true;
1236 u8 scsi_op = scmnd->cmnd[0];
1237
1238 switch (scsi_op) {
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001239 /*
1240 * smartd sends this command and the host does not handle
1241 * this. So, don't send it.
1242 */
K. Y. Srinivasan41098f82011-10-14 21:31:57 -07001243 case SET_WINDOW:
K. Y. Srinivasan59e00e72011-11-08 09:01:42 -08001244 scmnd->result = ILLEGAL_REQUEST << 16;
K. Y. Srinivasan41098f82011-10-14 21:31:57 -07001245 allowed = false;
1246 break;
1247 default:
1248 break;
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001249 }
1250 return allowed;
1251}
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001252
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001253static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001254{
1255 int ret;
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001256 struct hv_host_device *host_dev = shost_priv(host);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001257 struct hv_device *dev = host_dev->dev;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001258 struct hv_storvsc_request *request;
1259 struct storvsc_cmd_request *cmd_request;
1260 unsigned int request_size = 0;
1261 int i;
1262 struct scatterlist *sgl;
1263 unsigned int sg_count = 0;
1264 struct vmscsi_request *vm_srb;
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001265 struct stor_mem_pools *memp = scmnd->device->hostdata;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001266
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001267 if (!storvsc_scsi_cmd_ok(scmnd)) {
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001268 scmnd->scsi_done(scmnd);
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001269 return 0;
1270 }
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001271
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001272 request_size = sizeof(struct storvsc_cmd_request);
1273
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001274 cmd_request = mempool_alloc(memp->request_mempool,
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001275 GFP_ATOMIC);
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001276
1277 /*
1278 * We might be invoked in an interrupt context; hence
1279 * mempool_alloc() can fail.
1280 */
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001281 if (!cmd_request)
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001282 return SCSI_MLQUEUE_DEVICE_BUSY;
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001283
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -08001284 memset(cmd_request, 0, sizeof(struct storvsc_cmd_request));
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001285
1286 /* Setup the cmd request */
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001287 cmd_request->cmd = scmnd;
1288
1289 scmnd->host_scribble = (unsigned char *)cmd_request;
1290
1291 request = &cmd_request->request;
1292 vm_srb = &request->vstor_packet.vm_srb;
1293
1294
1295 /* Build the SRB */
1296 switch (scmnd->sc_data_direction) {
1297 case DMA_TO_DEVICE:
1298 vm_srb->data_in = WRITE_TYPE;
1299 break;
1300 case DMA_FROM_DEVICE:
1301 vm_srb->data_in = READ_TYPE;
1302 break;
1303 default:
1304 vm_srb->data_in = UNKNOWN_TYPE;
1305 break;
1306 }
1307
K. Y. Srinivasan93a1bf42012-01-12 12:38:02 -08001308 request->cmd = cmd_request;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001309
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001310 vm_srb->port_number = host_dev->port;
1311 vm_srb->path_id = scmnd->device->channel;
1312 vm_srb->target_id = scmnd->device->id;
1313 vm_srb->lun = scmnd->device->lun;
1314
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001315 vm_srb->cdb_length = scmnd->cmd_len;
1316
1317 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
1318
1319 request->sense_buffer = scmnd->sense_buffer;
1320
1321
1322 request->data_buffer.len = scsi_bufflen(scmnd);
1323 if (scsi_sg_count(scmnd)) {
1324 sgl = (struct scatterlist *)scsi_sglist(scmnd);
1325 sg_count = scsi_sg_count(scmnd);
1326
1327 /* check if we need to bounce the sgl */
1328 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
1329 cmd_request->bounce_sgl =
1330 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
K. Y. Srinivasan6e8087a2011-12-07 07:15:52 -08001331 scsi_bufflen(scmnd),
1332 vm_srb->data_in);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001333 if (!cmd_request->bounce_sgl) {
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001334 ret = SCSI_MLQUEUE_HOST_BUSY;
1335 goto queue_error;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001336 }
1337
1338 cmd_request->bounce_sgl_count =
1339 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
1340 PAGE_SHIFT;
1341
K. Y. Srinivasanfa23b8c2011-08-27 11:31:21 -07001342 if (vm_srb->data_in == WRITE_TYPE)
1343 copy_to_bounce_buffer(sgl,
1344 cmd_request->bounce_sgl,
1345 scsi_sg_count(scmnd));
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001346
1347 sgl = cmd_request->bounce_sgl;
1348 sg_count = cmd_request->bounce_sgl_count;
1349 }
1350
1351 request->data_buffer.offset = sgl[0].offset;
1352
1353 for (i = 0; i < sg_count; i++)
1354 request->data_buffer.pfn_array[i] =
1355 page_to_pfn(sg_page((&sgl[i])));
1356
1357 } else if (scsi_sglist(scmnd)) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001358 request->data_buffer.offset =
1359 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
1360 request->data_buffer.pfn_array[0] =
1361 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
1362 }
1363
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001364 /* Invokes the vsc to start an IO */
K. Y. Srinivasan636f0fd2011-05-10 07:54:50 -07001365 ret = storvsc_do_io(dev, &cmd_request->request);
1366
K. Y. Srinivasand2598f02011-08-25 09:48:58 -07001367 if (ret == -EAGAIN) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001368 /* no more space */
1369
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001370 if (cmd_request->bounce_sgl_count) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001371 destroy_bounce_buffer(cmd_request->bounce_sgl,
K. Y. Srinivasan70691ec2011-08-27 11:31:29 -07001372 cmd_request->bounce_sgl_count);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001373
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001374 ret = SCSI_MLQUEUE_DEVICE_BUSY;
1375 goto queue_error;
1376 }
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001377 }
1378
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001379 return 0;
1380
1381queue_error:
1382 mempool_free(cmd_request, memp->request_mempool);
1383 scmnd->host_scribble = NULL;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001384 return ret;
1385}
1386
Hank Janssenbef4a342009-07-13 16:01:31 -07001387static struct scsi_host_template scsi_driver = {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001388 .module = THIS_MODULE,
1389 .name = "storvsc_host_t",
1390 .bios_param = storvsc_get_chs,
1391 .queuecommand = storvsc_queuecommand,
1392 .eh_host_reset_handler = storvsc_host_reset_handler,
1393 .slave_alloc = storvsc_device_alloc,
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001394 .slave_destroy = storvsc_device_destroy,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001395 .slave_configure = storvsc_device_configure,
1396 .cmd_per_lun = 1,
1397 /* 64 max_queue * 1 target */
Lars Lindley0686e4f2010-03-11 23:51:23 +01001398 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001399 .this_id = -1,
Bill Pemberton454f18a2009-07-27 16:47:24 -04001400 /* no use setting to 0 since ll_blk_rw reset it to 1 */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001401 /* currently 32 */
1402 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
K. Y. Srinivasan039db522011-12-01 04:59:16 -08001403 .use_clustering = DISABLE_CLUSTERING,
Bill Pemberton454f18a2009-07-27 16:47:24 -04001404 /* Make sure we dont get a sg segment crosses a page boundary */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001405 .dma_boundary = PAGE_SIZE-1,
Hank Janssenbef4a342009-07-13 16:01:31 -07001406};
1407
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001408enum {
1409 SCSI_GUID,
1410 IDE_GUID,
1411};
1412
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001413static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001414 /* SCSI guid */
1415 { VMBUS_DEVICE(0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001416 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f)
1417 .driver_data = SCSI_GUID },
K. Y. Srinivasan21e37742011-08-27 11:31:18 -07001418 /* IDE guid */
1419 { VMBUS_DEVICE(0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001420 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5)
1421 .driver_data = IDE_GUID },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001422 { },
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001423};
Hank Janssenbef4a342009-07-13 16:01:31 -07001424
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001425MODULE_DEVICE_TABLE(vmbus, id_table);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001426
K. Y. Srinivasan84946892011-09-13 10:59:38 -07001427static int storvsc_probe(struct hv_device *device,
1428 const struct hv_vmbus_device_id *dev_id)
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001429{
1430 int ret;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001431 struct Scsi_Host *host;
1432 struct hv_host_device *host_dev;
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001433 bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001434 int target = 0;
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001435 struct storvsc_device *stor_device;
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001436
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001437 host = scsi_host_alloc(&scsi_driver,
1438 sizeof(struct hv_host_device));
1439 if (!host)
1440 return -ENOMEM;
1441
K. Y. Srinivasan7f33f302011-11-08 09:01:44 -08001442 host_dev = shost_priv(host);
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001443 memset(host_dev, 0, sizeof(struct hv_host_device));
1444
1445 host_dev->port = host->host_no;
1446 host_dev->dev = device;
1447
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -08001448
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001449 stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001450 if (!stor_device) {
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001451 ret = -ENOMEM;
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001452 goto err_out0;
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001453 }
1454
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001455 stor_device->destroy = false;
1456 init_waitqueue_head(&stor_device->waiting_to_drain);
1457 stor_device->device = device;
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001458 stor_device->host = host;
1459 hv_set_drvdata(device, stor_device);
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001460
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001461 stor_device->port_number = host->host_no;
1462 ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001463 if (ret)
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001464 goto err_out1;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001465
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001466 host_dev->path = stor_device->path_id;
1467 host_dev->target = stor_device->target_id;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001468
1469 /* max # of devices per target */
1470 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
1471 /* max # of targets per channel */
1472 host->max_id = STORVSC_MAX_TARGETS;
1473 /* max # of channels */
1474 host->max_channel = STORVSC_MAX_CHANNELS - 1;
Mike Sterlingcf55f4a2011-09-06 16:10:55 -07001475 /* max cmd length */
1476 host->max_cmd_len = STORVSC_MAX_CMD_LEN;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001477
1478 /* Register the HBA and start the scsi bus scan */
1479 ret = scsi_add_host(host, &device->device);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001480 if (ret != 0)
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001481 goto err_out2;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001482
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001483 if (!dev_is_ide) {
1484 scsi_scan_host(host);
K. Y. Srinivasan59d22952012-01-12 12:37:55 -08001485 } else {
1486 target = (device->dev_instance.b[5] << 8 |
1487 device->dev_instance.b[4]);
1488 ret = scsi_add_device(host, 0, target, 0);
1489 if (ret) {
1490 scsi_remove_host(host);
1491 goto err_out2;
1492 }
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001493 }
1494 return 0;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001495
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001496err_out2:
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001497 /*
1498 * Once we have connected with the host, we would need to
1499 * to invoke storvsc_dev_remove() to rollback this state and
1500 * this call also frees up the stor_device; hence the jump around
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001501 * err_out1 label.
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001502 */
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001503 storvsc_dev_remove(device);
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001504 goto err_out0;
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001505
1506err_out1:
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001507 kfree(stor_device);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001508
1509err_out0:
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001510 scsi_host_put(host);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001511 return ret;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001512}
1513
K. Y. Srinivasanddcbf652012-01-12 12:37:59 -08001514static int storvsc_remove(struct hv_device *dev)
1515{
1516 struct storvsc_device *stor_device = hv_get_drvdata(dev);
1517 struct Scsi_Host *host = stor_device->host;
1518
1519 scsi_remove_host(host);
1520 storvsc_dev_remove(dev);
1521 scsi_host_put(host);
1522
1523 return 0;
1524}
1525
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -07001526static struct hv_driver storvsc_drv = {
K. Y. Srinivasanfafb0ef2011-11-08 09:01:46 -08001527 .name = KBUILD_MODNAME,
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001528 .id_table = id_table,
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -07001529 .probe = storvsc_probe,
1530 .remove = storvsc_remove,
K. Y. Srinivasan39ae6fa2011-05-10 07:54:46 -07001531};
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -07001532
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -07001533static int __init storvsc_drv_init(void)
Hank Janssenbef4a342009-07-13 16:01:31 -07001534{
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001535 u32 max_outstanding_req_per_channel;
1536
1537 /*
1538 * Divide the ring buffer data size (which is 1 page less
1539 * than the ring buffer size since that page is reserved for
1540 * the ring buffer indices) by the max request size (which is
1541 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
1542 */
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001543 max_outstanding_req_per_channel =
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001544 ((storvsc_ringbuffer_size - PAGE_SIZE) /
1545 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
1546 sizeof(struct vstor_packet) + sizeof(u64),
1547 sizeof(u64)));
Hank Janssenbef4a342009-07-13 16:01:31 -07001548
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001549 if (max_outstanding_req_per_channel <
K. Y. Srinivasanf8feed02011-05-10 07:54:24 -07001550 STORVSC_MAX_IO_REQUESTS)
K. Y. Srinivasanb06efc12011-08-25 09:49:10 -07001551 return -EINVAL;
Hank Janssenbef4a342009-07-13 16:01:31 -07001552
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001553 return vmbus_driver_register(&storvsc_drv);
Hank Janssenbef4a342009-07-13 16:01:31 -07001554}
1555
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -07001556static void __exit storvsc_drv_exit(void)
Hank Janssenbef4a342009-07-13 16:01:31 -07001557{
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001558 vmbus_driver_unregister(&storvsc_drv);
Hank Janssenbef4a342009-07-13 16:01:31 -07001559}
1560
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001561MODULE_LICENSE("GPL");
Hank Janssen26c14cc2010-02-11 23:02:42 +00001562MODULE_VERSION(HV_DRV_VERSION);
Stephen Hemminger3afc7cc32010-05-06 21:44:45 -07001563MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -07001564module_init(storvsc_drv_init);
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -07001565module_exit(storvsc_drv_exit);