blob: 18f8771769be44cb1c8050394b0b6dca2d1732a0 [file] [log] [blame]
Hank Janssenbef4a342009-07-13 16:01:31 -07001/*
Hank Janssenbef4a342009-07-13 16:01:31 -07002 * Copyright (c) 2009, Microsoft Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
16 *
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
K. Y. Srinivasan972621c2011-05-10 07:54:19 -070020 * K. Y. Srinivasan <kys@microsoft.com>
Hank Janssenbef4a342009-07-13 16:01:31 -070021 */
K. Y. Srinivasana1be1702011-08-27 11:31:23 -070022
23#include <linux/kernel.h>
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070024#include <linux/wait.h>
K. Y. Srinivasana1be1702011-08-27 11:31:23 -070025#include <linux/sched.h>
26#include <linux/completion.h>
27#include <linux/string.h>
28#include <linux/mm.h>
29#include <linux/delay.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070030#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070032#include <linux/module.h>
33#include <linux/device.h>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070034#include <linux/hyperv.h>
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -080035#include <linux/mempool.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070036#include <scsi/scsi.h>
37#include <scsi/scsi_cmnd.h>
38#include <scsi/scsi_host.h>
39#include <scsi/scsi_device.h>
40#include <scsi/scsi_tcq.h>
41#include <scsi/scsi_eh.h>
42#include <scsi/scsi_devinfo.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070043#include <scsi/scsi_dbg.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070044
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070045
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -080046#define STORVSC_MIN_BUF_NR 64
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -070047#define STORVSC_RING_BUFFER_SIZE (20*PAGE_SIZE)
48static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
49
50module_param(storvsc_ringbuffer_size, int, S_IRUGO);
51MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
52
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070053/* to alert the user that structure sizes may be mismatched even though the */
54/* protocol versions match. */
55
56
57#define REVISION_STRING(REVISION_) #REVISION_
58#define FILL_VMSTOR_REVISION(RESULT_LVALUE_) \
59 do { \
60 char *revision_string \
61 = REVISION_STRING($Rev : 6 $) + 6; \
62 RESULT_LVALUE_ = 0; \
63 while (*revision_string >= '0' \
64 && *revision_string <= '9') { \
65 RESULT_LVALUE_ *= 10; \
66 RESULT_LVALUE_ += *revision_string - '0'; \
67 revision_string++; \
68 } \
69 } while (0)
70
71/* Major/minor macros. Minor version is in LSB, meaning that earlier flat */
72/* version numbers will be interpreted as "0.x" (i.e., 1 becomes 0.1). */
73#define VMSTOR_PROTOCOL_MAJOR(VERSION_) (((VERSION_) >> 8) & 0xff)
74#define VMSTOR_PROTOCOL_MINOR(VERSION_) (((VERSION_)) & 0xff)
75#define VMSTOR_PROTOCOL_VERSION(MAJOR_, MINOR_) ((((MAJOR_) & 0xff) << 8) | \
76 (((MINOR_) & 0xff)))
77#define VMSTOR_INVALID_PROTOCOL_VERSION (-1)
78
79/* Version history: */
80/* V1 Beta 0.1 */
81/* V1 RC < 2008/1/31 1.0 */
82/* V1 RC > 2008/1/31 2.0 */
K. Y. Srinivasan2b9525f2011-11-08 09:01:48 -080083#define VMSTOR_PROTOCOL_VERSION_CURRENT VMSTOR_PROTOCOL_VERSION(4, 2)
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070084
85
86
87
88/* This will get replaced with the max transfer length that is possible on */
89/* the host adapter. */
90/* The max transfer length will be published when we offer a vmbus channel. */
91#define MAX_TRANSFER_LENGTH 0x40000
92#define DEFAULT_PACKET_SIZE (sizeof(struct vmdata_gpa_direct) + \
93 sizeof(struct vstor_packet) + \
94 sizesizeof(u64) * (MAX_TRANSFER_LENGTH / PAGE_SIZE)))
95
96
97/* Packet structure describing virtual storage requests. */
98enum vstor_packet_operation {
99 VSTOR_OPERATION_COMPLETE_IO = 1,
100 VSTOR_OPERATION_REMOVE_DEVICE = 2,
101 VSTOR_OPERATION_EXECUTE_SRB = 3,
102 VSTOR_OPERATION_RESET_LUN = 4,
103 VSTOR_OPERATION_RESET_ADAPTER = 5,
104 VSTOR_OPERATION_RESET_BUS = 6,
105 VSTOR_OPERATION_BEGIN_INITIALIZATION = 7,
106 VSTOR_OPERATION_END_INITIALIZATION = 8,
107 VSTOR_OPERATION_QUERY_PROTOCOL_VERSION = 9,
108 VSTOR_OPERATION_QUERY_PROPERTIES = 10,
K. Y. Srinivasan2b9525f2011-11-08 09:01:48 -0800109 VSTOR_OPERATION_ENUMERATE_BUS = 11,
110 VSTOR_OPERATION_MAXIMUM = 11
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700111};
112
113/*
114 * Platform neutral description of a scsi request -
115 * this remains the same across the write regardless of 32/64 bit
116 * note: it's patterned off the SCSI_PASS_THROUGH structure
117 */
118#define CDB16GENERIC_LENGTH 0x10
119
120#ifndef SENSE_BUFFER_SIZE
121#define SENSE_BUFFER_SIZE 0x12
122#endif
123
124#define MAX_DATA_BUF_LEN_WITH_PADDING 0x14
125
126struct vmscsi_request {
127 unsigned short length;
128 unsigned char srb_status;
129 unsigned char scsi_status;
130
131 unsigned char port_number;
132 unsigned char path_id;
133 unsigned char target_id;
134 unsigned char lun;
135
136 unsigned char cdb_length;
137 unsigned char sense_info_length;
138 unsigned char data_in;
139 unsigned char reserved;
140
141 unsigned int data_transfer_length;
142
143 union {
144 unsigned char cdb[CDB16GENERIC_LENGTH];
145 unsigned char sense_data[SENSE_BUFFER_SIZE];
146 unsigned char reserved_array[MAX_DATA_BUF_LEN_WITH_PADDING];
147 };
148} __attribute((packed));
149
150
151/*
152 * This structure is sent during the intialization phase to get the different
153 * properties of the channel.
154 */
155struct vmstorage_channel_properties {
156 unsigned short protocol_version;
157 unsigned char path_id;
158 unsigned char target_id;
159
160 /* Note: port number is only really known on the client side */
161 unsigned int port_number;
162 unsigned int flags;
163 unsigned int max_transfer_bytes;
164
165 /* This id is unique for each channel and will correspond with */
166 /* vendor specific data in the inquirydata */
167 unsigned long long unique_id;
168} __packed;
169
170/* This structure is sent during the storage protocol negotiations. */
171struct vmstorage_protocol_version {
172 /* Major (MSW) and minor (LSW) version numbers. */
173 unsigned short major_minor;
174
175 /*
176 * Revision number is auto-incremented whenever this file is changed
177 * (See FILL_VMSTOR_REVISION macro above). Mismatch does not
178 * definitely indicate incompatibility--but it does indicate mismatched
179 * builds.
180 */
181 unsigned short revision;
182} __packed;
183
184/* Channel Property Flags */
185#define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
186#define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
187
188struct vstor_packet {
189 /* Requested operation type */
190 enum vstor_packet_operation operation;
191
192 /* Flags - see below for values */
193 unsigned int flags;
194
195 /* Status of the request returned from the server side. */
196 unsigned int status;
197
198 /* Data payload area */
199 union {
200 /*
201 * Structure used to forward SCSI commands from the
202 * client to the server.
203 */
204 struct vmscsi_request vm_srb;
205
206 /* Structure used to query channel properties. */
207 struct vmstorage_channel_properties storage_channel_properties;
208
209 /* Used during version negotiations. */
210 struct vmstorage_protocol_version version;
211 };
212} __packed;
213
214/* Packet flags */
215/*
216 * This flag indicates that the server should send back a completion for this
217 * packet.
218 */
219#define REQUEST_COMPLETION_FLAG 0x1
220
221/* This is the set of flags that the vsc can set in any packets it sends */
222#define VSC_LEGAL_FLAGS (REQUEST_COMPLETION_FLAG)
223
224
225/* Defines */
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700226
227#define STORVSC_MAX_IO_REQUESTS 128
228
229/*
230 * In Hyper-V, each port/path/target maps to 1 scsi host adapter. In
231 * reality, the path/target is not used (ie always set to 0) so our
232 * scsi host adapter essentially has 1 bus with 1 target that contains
233 * up to 256 luns.
234 */
235#define STORVSC_MAX_LUNS_PER_TARGET 64
236#define STORVSC_MAX_TARGETS 1
237#define STORVSC_MAX_CHANNELS 1
Mike Sterlingcf55f4a2011-09-06 16:10:55 -0700238#define STORVSC_MAX_CMD_LEN 16
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700239
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700240/* Matches Windows-end */
241enum storvsc_request_type {
242 WRITE_TYPE,
243 READ_TYPE,
244 UNKNOWN_TYPE,
245};
246
247
248struct hv_storvsc_request {
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700249 struct hv_device *device;
250
251 /* Synchronize the request/response if needed */
252 struct completion wait_event;
253
254 unsigned char *sense_buffer;
255 void *context;
256 void (*on_io_completion)(struct hv_storvsc_request *request);
257 struct hv_multipage_buffer data_buffer;
258
259 struct vstor_packet vstor_packet;
260};
261
262
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700263/* A storvsc device is a device object that contains a vmbus channel */
264struct storvsc_device {
265 struct hv_device *device;
266
267 bool destroy;
268 bool drain_notify;
269 atomic_t num_outstanding_req;
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700270 struct Scsi_Host *host;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700271
272 wait_queue_head_t waiting_to_drain;
273
274 /*
275 * Each unique Port/Path/Target represents 1 channel ie scsi
276 * controller. In reality, the pathid, targetid is always 0
277 * and the port is set by us
278 */
279 unsigned int port_number;
280 unsigned char path_id;
281 unsigned char target_id;
282
283 /* Used for vsc/vsp channel reset process */
284 struct hv_storvsc_request init_request;
285 struct hv_storvsc_request reset_request;
286};
287
K. Y. Srinivasance3e3012011-12-01 04:59:20 -0800288struct stor_mem_pools {
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700289 struct kmem_cache *request_pool;
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -0800290 mempool_t *request_mempool;
K. Y. Srinivasance3e3012011-12-01 04:59:20 -0800291};
292
293struct hv_host_device {
294 struct hv_device *dev;
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700295 unsigned int port;
296 unsigned char path;
297 unsigned char target;
298};
299
300struct storvsc_cmd_request {
301 struct list_head entry;
302 struct scsi_cmnd *cmd;
303
304 unsigned int bounce_sgl_count;
305 struct scatterlist *bounce_sgl;
306
307 struct hv_storvsc_request request;
308};
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700309
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800310struct storvsc_scan_work {
311 struct work_struct work;
312 struct Scsi_Host *host;
313 uint lun;
314};
315
316static void storvsc_bus_scan(struct work_struct *work)
317{
318 struct storvsc_scan_work *wrk;
319 int id, order_id;
320
321 wrk = container_of(work, struct storvsc_scan_work, work);
322 for (id = 0; id < wrk->host->max_id; ++id) {
323 if (wrk->host->reverse_ordering)
324 order_id = wrk->host->max_id - id - 1;
325 else
326 order_id = id;
327
328 scsi_scan_target(&wrk->host->shost_gendev, 0,
329 order_id, SCAN_WILD_CARD, 1);
330 }
331 kfree(wrk);
332}
333
K. Y. Srinivasanb4017312011-11-08 09:01:50 -0800334static void storvsc_remove_lun(struct work_struct *work)
335{
336 struct storvsc_scan_work *wrk;
337 struct scsi_device *sdev;
338
339 wrk = container_of(work, struct storvsc_scan_work, work);
340 if (!scsi_host_get(wrk->host))
341 goto done;
342
343 sdev = scsi_device_lookup(wrk->host, 0, 0, wrk->lun);
344
345 if (sdev) {
346 scsi_remove_device(sdev);
347 scsi_device_put(sdev);
348 }
349 scsi_host_put(wrk->host);
350
351done:
352 kfree(wrk);
353}
354
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700355static inline struct storvsc_device *get_out_stor_device(
356 struct hv_device *device)
357{
358 struct storvsc_device *stor_device;
359
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700360 stor_device = hv_get_drvdata(device);
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700361
362 if (stor_device && stor_device->destroy)
363 stor_device = NULL;
364
365 return stor_device;
366}
367
368
369static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
370{
371 dev->drain_notify = true;
372 wait_event(dev->waiting_to_drain,
373 atomic_read(&dev->num_outstanding_req) == 0);
374 dev->drain_notify = false;
375}
Hank Janssenbef4a342009-07-13 16:01:31 -0700376
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700377static inline struct storvsc_device *get_in_stor_device(
378 struct hv_device *device)
379{
380 struct storvsc_device *stor_device;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700381
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700382 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700383
384 if (!stor_device)
385 goto get_in_err;
386
387 /*
388 * If the device is being destroyed; allow incoming
389 * traffic only to cleanup outstanding requests.
390 */
391
392 if (stor_device->destroy &&
393 (atomic_read(&stor_device->num_outstanding_req) == 0))
394 stor_device = NULL;
395
396get_in_err:
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700397 return stor_device;
398
399}
400
401static int storvsc_channel_init(struct hv_device *device)
402{
403 struct storvsc_device *stor_device;
404 struct hv_storvsc_request *request;
405 struct vstor_packet *vstor_packet;
406 int ret, t;
407
408 stor_device = get_out_stor_device(device);
409 if (!stor_device)
410 return -ENODEV;
411
412 request = &stor_device->init_request;
413 vstor_packet = &request->vstor_packet;
414
415 /*
416 * Now, initiate the vsc/vsp initialization protocol on the open
417 * channel
418 */
419 memset(request, 0, sizeof(struct hv_storvsc_request));
420 init_completion(&request->wait_event);
421 vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
422 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
423
424 ret = vmbus_sendpacket(device->channel, vstor_packet,
425 sizeof(struct vstor_packet),
426 (unsigned long)request,
427 VM_PKT_DATA_INBAND,
428 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
429 if (ret != 0)
430 goto cleanup;
431
432 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
433 if (t == 0) {
434 ret = -ETIMEDOUT;
435 goto cleanup;
436 }
437
438 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
439 vstor_packet->status != 0)
440 goto cleanup;
441
442
443 /* reuse the packet for version range supported */
444 memset(vstor_packet, 0, sizeof(struct vstor_packet));
445 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
446 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
447
448 vstor_packet->version.major_minor = VMSTOR_PROTOCOL_VERSION_CURRENT;
449 FILL_VMSTOR_REVISION(vstor_packet->version.revision);
450
451 ret = vmbus_sendpacket(device->channel, vstor_packet,
452 sizeof(struct vstor_packet),
453 (unsigned long)request,
454 VM_PKT_DATA_INBAND,
455 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
456 if (ret != 0)
457 goto cleanup;
458
459 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
460 if (t == 0) {
461 ret = -ETIMEDOUT;
462 goto cleanup;
463 }
464
465 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
466 vstor_packet->status != 0)
467 goto cleanup;
468
469
470 memset(vstor_packet, 0, sizeof(struct vstor_packet));
471 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
472 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
473 vstor_packet->storage_channel_properties.port_number =
474 stor_device->port_number;
475
476 ret = vmbus_sendpacket(device->channel, vstor_packet,
477 sizeof(struct vstor_packet),
478 (unsigned long)request,
479 VM_PKT_DATA_INBAND,
480 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
481
482 if (ret != 0)
483 goto cleanup;
484
485 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
486 if (t == 0) {
487 ret = -ETIMEDOUT;
488 goto cleanup;
489 }
490
491 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
492 vstor_packet->status != 0)
493 goto cleanup;
494
495 stor_device->path_id = vstor_packet->storage_channel_properties.path_id;
496 stor_device->target_id
497 = vstor_packet->storage_channel_properties.target_id;
498
499 memset(vstor_packet, 0, sizeof(struct vstor_packet));
500 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
501 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
502
503 ret = vmbus_sendpacket(device->channel, vstor_packet,
504 sizeof(struct vstor_packet),
505 (unsigned long)request,
506 VM_PKT_DATA_INBAND,
507 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
508
509 if (ret != 0)
510 goto cleanup;
511
512 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
513 if (t == 0) {
514 ret = -ETIMEDOUT;
515 goto cleanup;
516 }
517
518 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
519 vstor_packet->status != 0)
520 goto cleanup;
521
522
523cleanup:
524 return ret;
525}
526
527static void storvsc_on_io_completion(struct hv_device *device,
528 struct vstor_packet *vstor_packet,
529 struct hv_storvsc_request *request)
530{
531 struct storvsc_device *stor_device;
532 struct vstor_packet *stor_pkt;
533
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700534 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700535 stor_pkt = &request->vstor_packet;
536
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -0700537 /*
538 * The current SCSI handling on the host side does
539 * not correctly handle:
540 * INQUIRY command with page code parameter set to 0x80
541 * MODE_SENSE command with cmd[2] == 0x1c
542 *
543 * Setup srb and scsi status so this won't be fatal.
544 * We do this so we can distinguish truly fatal failues
545 * (srb status == 0x4) and off-line the device in that case.
546 */
547
548 if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
549 (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
550 vstor_packet->vm_srb.scsi_status = 0;
551 vstor_packet->vm_srb.srb_status = 0x1;
552 }
553
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700554
555 /* Copy over the status...etc */
556 stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
557 stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
558 stor_pkt->vm_srb.sense_info_length =
559 vstor_packet->vm_srb.sense_info_length;
560
561 if (vstor_packet->vm_srb.scsi_status != 0 ||
562 vstor_packet->vm_srb.srb_status != 1){
Greg Kroah-Hartmand181daa2011-10-11 09:20:31 -0600563 dev_warn(&device->device,
564 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
565 stor_pkt->vm_srb.cdb[0],
566 vstor_packet->vm_srb.scsi_status,
567 vstor_packet->vm_srb.srb_status);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700568 }
569
570 if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
571 /* CHECK_CONDITION */
572 if (vstor_packet->vm_srb.srb_status & 0x80) {
573 /* autosense data available */
Greg Kroah-Hartmand181daa2011-10-11 09:20:31 -0600574 dev_warn(&device->device,
K. Y. Srinivasan41098f82011-10-14 21:31:57 -0700575 "stor pkt %p autosense data valid - len %d\n",
576 request,
577 vstor_packet->vm_srb.sense_info_length);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700578
579 memcpy(request->sense_buffer,
580 vstor_packet->vm_srb.sense_data,
581 vstor_packet->vm_srb.sense_info_length);
582
583 }
584 }
585
586 stor_pkt->vm_srb.data_transfer_length =
587 vstor_packet->vm_srb.data_transfer_length;
588
589 request->on_io_completion(request);
590
591 if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
592 stor_device->drain_notify)
593 wake_up(&stor_device->waiting_to_drain);
594
595
596}
597
598static void storvsc_on_receive(struct hv_device *device,
599 struct vstor_packet *vstor_packet,
600 struct hv_storvsc_request *request)
601{
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800602 struct storvsc_scan_work *work;
603 struct storvsc_device *stor_device;
604
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700605 switch (vstor_packet->operation) {
606 case VSTOR_OPERATION_COMPLETE_IO:
607 storvsc_on_io_completion(device, vstor_packet, request);
608 break;
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800609
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700610 case VSTOR_OPERATION_REMOVE_DEVICE:
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800611 case VSTOR_OPERATION_ENUMERATE_BUS:
612 stor_device = get_in_stor_device(device);
613 work = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
614 if (!work)
615 return;
616
617 INIT_WORK(&work->work, storvsc_bus_scan);
618 work->host = stor_device->host;
619 schedule_work(&work->work);
620 break;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700621
622 default:
623 break;
624 }
625}
626
627static void storvsc_on_channel_callback(void *context)
628{
629 struct hv_device *device = (struct hv_device *)context;
630 struct storvsc_device *stor_device;
631 u32 bytes_recvd;
632 u64 request_id;
633 unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
634 struct hv_storvsc_request *request;
635 int ret;
636
637
638 stor_device = get_in_stor_device(device);
639 if (!stor_device)
640 return;
641
642 do {
643 ret = vmbus_recvpacket(device->channel, packet,
644 ALIGN(sizeof(struct vstor_packet), 8),
645 &bytes_recvd, &request_id);
646 if (ret == 0 && bytes_recvd > 0) {
647
648 request = (struct hv_storvsc_request *)
649 (unsigned long)request_id;
650
651 if ((request == &stor_device->init_request) ||
652 (request == &stor_device->reset_request)) {
653
654 memcpy(&request->vstor_packet, packet,
655 sizeof(struct vstor_packet));
656 complete(&request->wait_event);
657 } else {
658 storvsc_on_receive(device,
659 (struct vstor_packet *)packet,
660 request);
661 }
662 } else {
663 break;
664 }
665 } while (1);
666
667 return;
668}
669
670static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
671{
672 struct vmstorage_channel_properties props;
673 int ret;
674
675 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
676
677 /* Open the channel */
678 ret = vmbus_open(device->channel,
679 ring_size,
680 ring_size,
681 (void *)&props,
682 sizeof(struct vmstorage_channel_properties),
683 storvsc_on_channel_callback, device);
684
685 if (ret != 0)
686 return ret;
687
688 ret = storvsc_channel_init(device);
689
690 return ret;
691}
692
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700693static int storvsc_dev_remove(struct hv_device *device)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700694{
695 struct storvsc_device *stor_device;
696 unsigned long flags;
697
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700698 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700699
700 spin_lock_irqsave(&device->channel->inbound_lock, flags);
701 stor_device->destroy = true;
702 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
703
704 /*
705 * At this point, all outbound traffic should be disable. We
706 * only allow inbound traffic (responses) to proceed so that
707 * outstanding requests can be completed.
708 */
709
710 storvsc_wait_to_drain(stor_device);
711
712 /*
713 * Since we have already drained, we don't need to busy wait
714 * as was done in final_release_stor_device()
715 * Note that we cannot set the ext pointer to NULL until
716 * we have drained - to drain the outgoing packets, we need to
717 * allow incoming packets.
718 */
719 spin_lock_irqsave(&device->channel->inbound_lock, flags);
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700720 hv_set_drvdata(device, NULL);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700721 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
722
723 /* Close the channel */
724 vmbus_close(device->channel);
725
726 kfree(stor_device);
727 return 0;
728}
729
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700730static int storvsc_do_io(struct hv_device *device,
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700731 struct hv_storvsc_request *request)
732{
733 struct storvsc_device *stor_device;
734 struct vstor_packet *vstor_packet;
735 int ret = 0;
736
737 vstor_packet = &request->vstor_packet;
738 stor_device = get_out_stor_device(device);
739
740 if (!stor_device)
741 return -ENODEV;
742
743
744 request->device = device;
745
746
747 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
748
749 vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
750
751
752 vstor_packet->vm_srb.sense_info_length = SENSE_BUFFER_SIZE;
753
754
755 vstor_packet->vm_srb.data_transfer_length =
756 request->data_buffer.len;
757
758 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
759
760 if (request->data_buffer.len) {
761 ret = vmbus_sendpacket_multipagebuffer(device->channel,
762 &request->data_buffer,
763 vstor_packet,
764 sizeof(struct vstor_packet),
765 (unsigned long)request);
766 } else {
767 ret = vmbus_sendpacket(device->channel, vstor_packet,
768 sizeof(struct vstor_packet),
769 (unsigned long)request,
770 VM_PKT_DATA_INBAND,
771 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
772 }
773
774 if (ret != 0)
775 return ret;
776
777 atomic_inc(&stor_device->num_outstanding_req);
778
779 return ret;
780}
781
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -0700782static void storvsc_get_ide_info(struct hv_device *dev, int *target, int *path)
783{
784 *target =
785 dev->dev_instance.b[5] << 8 | dev->dev_instance.b[4];
786
787 *path =
788 dev->dev_instance.b[3] << 24 |
789 dev->dev_instance.b[2] << 16 |
790 dev->dev_instance.b[1] << 8 | dev->dev_instance.b[0];
791}
792
Hank Janssenbef4a342009-07-13 16:01:31 -0700793
K. Y. Srinivasan5b60ace2011-05-10 07:54:25 -0700794static int storvsc_device_alloc(struct scsi_device *sdevice)
795{
K. Y. Srinivasance3e3012011-12-01 04:59:20 -0800796 struct stor_mem_pools *memp;
797 int number = STORVSC_MIN_BUF_NR;
798
799 memp = kzalloc(sizeof(struct stor_mem_pools), GFP_KERNEL);
800 if (!memp)
801 return -ENOMEM;
802
803 memp->request_pool =
804 kmem_cache_create(dev_name(&sdevice->sdev_dev),
805 sizeof(struct storvsc_cmd_request), 0,
806 SLAB_HWCACHE_ALIGN, NULL);
807
808 if (!memp->request_pool)
809 goto err0;
810
811 memp->request_mempool = mempool_create(number, mempool_alloc_slab,
812 mempool_free_slab,
813 memp->request_pool);
814
815 if (!memp->request_mempool)
816 goto err1;
817
818 sdevice->hostdata = memp;
819
K. Y. Srinivasan5b60ace2011-05-10 07:54:25 -0700820 return 0;
K. Y. Srinivasance3e3012011-12-01 04:59:20 -0800821
822err1:
823 kmem_cache_destroy(memp->request_pool);
824
825err0:
826 kfree(memp);
827 return -ENOMEM;
828}
829
830static void storvsc_device_destroy(struct scsi_device *sdevice)
831{
832 struct stor_mem_pools *memp = sdevice->hostdata;
833
834 mempool_destroy(memp->request_mempool);
835 kmem_cache_destroy(memp->request_pool);
836 kfree(memp);
837 sdevice->hostdata = NULL;
K. Y. Srinivasan5b60ace2011-05-10 07:54:25 -0700838}
839
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700840static int storvsc_device_configure(struct scsi_device *sdevice)
841{
842 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
843 STORVSC_MAX_IO_REQUESTS);
844
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700845 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
846
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700847 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700848
849 return 0;
850}
851
K. Y. Srinivasan49a3c7a2011-05-10 07:54:28 -0700852static void destroy_bounce_buffer(struct scatterlist *sgl,
853 unsigned int sg_count)
854{
855 int i;
856 struct page *page_buf;
857
858 for (i = 0; i < sg_count; i++) {
859 page_buf = sg_page((&sgl[i]));
860 if (page_buf != NULL)
861 __free_page(page_buf);
862 }
863
864 kfree(sgl);
865}
866
K. Y. Srinivasan3862ef32011-05-10 07:54:29 -0700867static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
868{
869 int i;
870
871 /* No need to check */
872 if (sg_count < 2)
873 return -1;
874
875 /* We have at least 2 sg entries */
876 for (i = 0; i < sg_count; i++) {
877 if (i == 0) {
878 /* make sure 1st one does not have hole */
879 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
880 return i;
881 } else if (i == sg_count - 1) {
882 /* make sure last one does not have hole */
883 if (sgl[i].offset != 0)
884 return i;
885 } else {
886 /* make sure no hole in the middle */
887 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
888 return i;
889 }
890 }
891 return -1;
892}
893
K. Y. Srinivasana9753cb2011-05-10 07:54:30 -0700894static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
895 unsigned int sg_count,
896 unsigned int len)
897{
898 int i;
899 int num_pages;
900 struct scatterlist *bounce_sgl;
901 struct page *page_buf;
902
903 num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
904
905 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
906 if (!bounce_sgl)
907 return NULL;
908
909 for (i = 0; i < num_pages; i++) {
910 page_buf = alloc_page(GFP_ATOMIC);
911 if (!page_buf)
912 goto cleanup;
913 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
914 }
915
916 return bounce_sgl;
917
918cleanup:
919 destroy_bounce_buffer(bounce_sgl, num_pages);
920 return NULL;
921}
922
K. Y. Srinivasan29fe2c92011-05-10 07:54:31 -0700923
924/* Assume the original sgl has enough room */
925static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
926 struct scatterlist *bounce_sgl,
K. Y. Srinivasan10c43dd42011-12-01 04:59:19 -0800927 unsigned int orig_sgl_count,
928 unsigned int bounce_sgl_count)
K. Y. Srinivasan29fe2c92011-05-10 07:54:31 -0700929{
930 int i;
931 int j = 0;
932 unsigned long src, dest;
933 unsigned int srclen, destlen, copylen;
934 unsigned int total_copied = 0;
935 unsigned long bounce_addr = 0;
936 unsigned long dest_addr = 0;
937 unsigned long flags;
938
939 local_irq_save(flags);
940
941 for (i = 0; i < orig_sgl_count; i++) {
942 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
943 KM_IRQ0) + orig_sgl[i].offset;
944 dest = dest_addr;
945 destlen = orig_sgl[i].length;
946
947 if (bounce_addr == 0)
948 bounce_addr =
949 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
950 KM_IRQ0);
951
952 while (destlen) {
953 src = bounce_addr + bounce_sgl[j].offset;
954 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
955
956 copylen = min(srclen, destlen);
957 memcpy((void *)dest, (void *)src, copylen);
958
959 total_copied += copylen;
960 bounce_sgl[j].offset += copylen;
961 destlen -= copylen;
962 dest += copylen;
963
964 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
965 /* full */
966 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
967 j++;
968
K. Y. Srinivasan10c43dd42011-12-01 04:59:19 -0800969 /*
970 * It is possible that the number of elements
971 * in the bounce buffer may not be equal to
972 * the number of elements in the original
973 * scatter list. Handle this correctly.
974 */
975
976 if (j == bounce_sgl_count) {
977 /*
978 * We are done; cleanup and return.
979 */
980 kunmap_atomic((void *)(dest_addr -
981 orig_sgl[i].offset),
982 KM_IRQ0);
983 local_irq_restore(flags);
984 return total_copied;
985 }
986
K. Y. Srinivasan29fe2c92011-05-10 07:54:31 -0700987 /* if we need to use another bounce buffer */
988 if (destlen || i != orig_sgl_count - 1)
989 bounce_addr =
990 (unsigned long)kmap_atomic(
991 sg_page((&bounce_sgl[j])), KM_IRQ0);
992 } else if (destlen == 0 && i == orig_sgl_count - 1) {
993 /* unmap the last bounce that is < PAGE_SIZE */
994 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
995 }
996 }
997
998 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
999 KM_IRQ0);
1000 }
1001
1002 local_irq_restore(flags);
1003
1004 return total_copied;
1005}
1006
K. Y. Srinivasan6a8ff442011-05-10 07:54:32 -07001007
1008/* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
1009static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
1010 struct scatterlist *bounce_sgl,
1011 unsigned int orig_sgl_count)
1012{
1013 int i;
1014 int j = 0;
1015 unsigned long src, dest;
1016 unsigned int srclen, destlen, copylen;
1017 unsigned int total_copied = 0;
1018 unsigned long bounce_addr = 0;
1019 unsigned long src_addr = 0;
1020 unsigned long flags;
1021
1022 local_irq_save(flags);
1023
1024 for (i = 0; i < orig_sgl_count; i++) {
1025 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
1026 KM_IRQ0) + orig_sgl[i].offset;
1027 src = src_addr;
1028 srclen = orig_sgl[i].length;
1029
1030 if (bounce_addr == 0)
1031 bounce_addr =
1032 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
1033 KM_IRQ0);
1034
1035 while (srclen) {
1036 /* assume bounce offset always == 0 */
1037 dest = bounce_addr + bounce_sgl[j].length;
1038 destlen = PAGE_SIZE - bounce_sgl[j].length;
1039
1040 copylen = min(srclen, destlen);
1041 memcpy((void *)dest, (void *)src, copylen);
1042
1043 total_copied += copylen;
1044 bounce_sgl[j].length += copylen;
1045 srclen -= copylen;
1046 src += copylen;
1047
1048 if (bounce_sgl[j].length == PAGE_SIZE) {
1049 /* full..move to next entry */
1050 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
1051 j++;
1052
1053 /* if we need to use another bounce buffer */
1054 if (srclen || i != orig_sgl_count - 1)
1055 bounce_addr =
1056 (unsigned long)kmap_atomic(
1057 sg_page((&bounce_sgl[j])), KM_IRQ0);
1058
1059 } else if (srclen == 0 && i == orig_sgl_count - 1) {
1060 /* unmap the last bounce that is < PAGE_SIZE */
1061 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
1062 }
1063 }
1064
1065 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
1066 }
1067
1068 local_irq_restore(flags);
1069
1070 return total_copied;
1071}
1072
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -07001073
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -07001074static int storvsc_remove(struct hv_device *dev)
1075{
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001076 struct storvsc_device *stor_device = hv_get_drvdata(dev);
1077 struct Scsi_Host *host = stor_device->host;
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -07001078
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -07001079 scsi_remove_host(host);
1080
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -07001081 scsi_host_put(host);
K. Y. Srinivasand36b0a02011-06-06 15:49:29 -07001082
K. Y. Srinivasan2935a402011-06-06 15:49:28 -07001083 storvsc_dev_remove(dev);
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001084
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -07001085 return 0;
1086}
1087
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001088
1089static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
1090 sector_t capacity, int *info)
1091{
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001092 sector_t nsect = capacity;
1093 sector_t cylinders = nsect;
1094 int heads, sectors_pt;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001095
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001096 /*
1097 * We are making up these values; let us keep it simple.
1098 */
1099 heads = 0xff;
1100 sectors_pt = 0x3f; /* Sectors per track */
1101 sector_div(cylinders, heads * sectors_pt);
1102 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
1103 cylinders = 0xffff;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001104
1105 info[0] = heads;
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001106 info[1] = sectors_pt;
1107 info[2] = (int)cylinders;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001108
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001109 return 0;
1110}
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001111
1112static int storvsc_host_reset(struct hv_device *device)
1113{
1114 struct storvsc_device *stor_device;
1115 struct hv_storvsc_request *request;
1116 struct vstor_packet *vstor_packet;
1117 int ret, t;
1118
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001119
K. Y. Srinivasan1eaaddf2011-08-27 11:31:03 -07001120 stor_device = get_out_stor_device(device);
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001121 if (!stor_device)
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001122 return FAILED;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001123
1124 request = &stor_device->reset_request;
1125 vstor_packet = &request->vstor_packet;
1126
1127 init_completion(&request->wait_event);
1128
1129 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
1130 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
1131 vstor_packet->vm_srb.path_id = stor_device->path_id;
1132
1133 ret = vmbus_sendpacket(device->channel, vstor_packet,
1134 sizeof(struct vstor_packet),
1135 (unsigned long)&stor_device->reset_request,
1136 VM_PKT_DATA_INBAND,
1137 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1138 if (ret != 0)
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001139 return FAILED;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001140
K. Y. Srinivasan46d2eb62011-06-16 13:16:36 -07001141 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001142 if (t == 0)
1143 return TIMEOUT_ERROR;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001144
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001145
1146 /*
1147 * At this point, all outstanding requests in the adapter
1148 * should have been flushed out and return to us
1149 */
1150
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001151 return SUCCESS;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001152}
1153
1154
K. Y. Srinivasan96e690be2011-05-10 07:54:38 -07001155/*
1156 * storvsc_host_reset_handler - Reset the scsi HBA
1157 */
1158static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
1159{
K. Y. Srinivasan7f33f302011-11-08 09:01:44 -08001160 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
K. Y. Srinivasan96e690be2011-05-10 07:54:38 -07001161 struct hv_device *dev = host_dev->dev;
1162
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001163 return storvsc_host_reset(dev);
K. Y. Srinivasan96e690be2011-05-10 07:54:38 -07001164}
1165
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001166
1167/*
K. Y. Srinivasanb8de73d2011-08-27 11:31:27 -07001168 * storvsc_command_completion - Command completion processing
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001169 */
K. Y. Srinivasanb8de73d2011-08-27 11:31:27 -07001170static void storvsc_command_completion(struct hv_storvsc_request *request)
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001171{
1172 struct storvsc_cmd_request *cmd_request =
1173 (struct storvsc_cmd_request *)request->context;
1174 struct scsi_cmnd *scmnd = cmd_request->cmd;
K. Y. Srinivasan7f33f302011-11-08 09:01:44 -08001175 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001176 void (*scsi_done_fn)(struct scsi_cmnd *);
1177 struct scsi_sense_hdr sense_hdr;
1178 struct vmscsi_request *vm_srb;
K. Y. Srinivasanb4017312011-11-08 09:01:50 -08001179 struct storvsc_scan_work *wrk;
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001180 struct stor_mem_pools *memp = scmnd->device->hostdata;
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001181
K. Y. Srinivasan3612ef92011-08-27 11:31:20 -07001182 vm_srb = &request->vstor_packet.vm_srb;
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001183 if (cmd_request->bounce_sgl_count) {
K. Y. Srinivasana768a762011-12-01 04:59:18 -08001184 if (vm_srb->data_in == READ_TYPE)
K. Y. Srinivasan3612ef92011-08-27 11:31:20 -07001185 copy_from_bounce_buffer(scsi_sglist(scmnd),
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001186 cmd_request->bounce_sgl,
K. Y. Srinivasan10c43dd42011-12-01 04:59:19 -08001187 scsi_sg_count(scmnd),
1188 cmd_request->bounce_sgl_count);
K. Y. Srinivasana768a762011-12-01 04:59:18 -08001189 destroy_bounce_buffer(cmd_request->bounce_sgl,
K. Y. Srinivasan3612ef92011-08-27 11:31:20 -07001190 cmd_request->bounce_sgl_count);
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001191 }
1192
K. Y. Srinivasan2544b792011-08-27 11:31:28 -07001193 /*
1194 * If there is an error; offline the device since all
1195 * error recovery strategies would have already been
1196 * deployed on the host side.
1197 */
1198 if (vm_srb->srb_status == 0x4)
1199 scmnd->result = DID_TARGET_FAILURE << 16;
1200 else
1201 scmnd->result = vm_srb->scsi_status;
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001202
K. Y. Srinivasanb4017312011-11-08 09:01:50 -08001203 /*
1204 * If the LUN is invalid; remove the device.
1205 */
1206 if (vm_srb->srb_status == 0x20) {
1207 struct storvsc_device *stor_dev;
1208 struct hv_device *dev = host_dev->dev;
1209 struct Scsi_Host *host;
1210
1211 stor_dev = get_in_stor_device(dev);
1212 host = stor_dev->host;
1213
1214 wrk = kmalloc(sizeof(struct storvsc_scan_work),
1215 GFP_ATOMIC);
1216 if (!wrk) {
1217 scmnd->result = DID_TARGET_FAILURE << 16;
1218 } else {
1219 wrk->host = host;
1220 wrk->lun = vm_srb->lun;
1221 INIT_WORK(&wrk->work, storvsc_remove_lun);
1222 schedule_work(&wrk->work);
1223 }
1224 }
1225
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001226 if (scmnd->result) {
1227 if (scsi_normalize_sense(scmnd->sense_buffer,
1228 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
1229 scsi_print_sense_hdr("storvsc", &sense_hdr);
1230 }
1231
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001232 scsi_set_resid(scmnd,
1233 request->data_buffer.len -
1234 vm_srb->data_transfer_length);
1235
1236 scsi_done_fn = scmnd->scsi_done;
1237
1238 scmnd->host_scribble = NULL;
1239 scmnd->scsi_done = NULL;
1240
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001241 scsi_done_fn(scmnd);
1242
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001243 mempool_free(cmd_request, memp->request_mempool);
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001244}
1245
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001246static bool storvsc_check_scsi_cmd(struct scsi_cmnd *scmnd)
1247{
1248 bool allowed = true;
1249 u8 scsi_op = scmnd->cmnd[0];
1250
1251 switch (scsi_op) {
K. Y. Srinivasan41098f82011-10-14 21:31:57 -07001252 /* smartd sends this command, which will offline the device */
1253 case SET_WINDOW:
K. Y. Srinivasan59e00e72011-11-08 09:01:42 -08001254 scmnd->result = ILLEGAL_REQUEST << 16;
K. Y. Srinivasan41098f82011-10-14 21:31:57 -07001255 allowed = false;
1256 break;
1257 default:
1258 break;
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001259 }
1260 return allowed;
1261}
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001262
1263/*
1264 * storvsc_queuecommand - Initiate command processing
1265 */
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001266static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001267{
1268 int ret;
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001269 struct hv_host_device *host_dev = shost_priv(host);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001270 struct hv_device *dev = host_dev->dev;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001271 struct hv_storvsc_request *request;
1272 struct storvsc_cmd_request *cmd_request;
1273 unsigned int request_size = 0;
1274 int i;
1275 struct scatterlist *sgl;
1276 unsigned int sg_count = 0;
1277 struct vmscsi_request *vm_srb;
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001278 struct stor_mem_pools *memp = scmnd->device->hostdata;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001279
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001280 if (storvsc_check_scsi_cmd(scmnd) == false) {
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001281 scmnd->scsi_done(scmnd);
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001282 return 0;
1283 }
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001284
1285 /* If retrying, no need to prep the cmd */
1286 if (scmnd->host_scribble) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001287
1288 cmd_request =
1289 (struct storvsc_cmd_request *)scmnd->host_scribble;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001290
1291 goto retry_request;
1292 }
1293
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001294 request_size = sizeof(struct storvsc_cmd_request);
1295
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001296 cmd_request = mempool_alloc(memp->request_mempool,
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001297 GFP_ATOMIC);
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001298 if (!cmd_request)
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001299 return SCSI_MLQUEUE_DEVICE_BUSY;
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001300
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -08001301 memset(cmd_request, 0, sizeof(struct storvsc_cmd_request));
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001302
1303 /* Setup the cmd request */
1304 cmd_request->bounce_sgl_count = 0;
1305 cmd_request->bounce_sgl = NULL;
1306 cmd_request->cmd = scmnd;
1307
1308 scmnd->host_scribble = (unsigned char *)cmd_request;
1309
1310 request = &cmd_request->request;
1311 vm_srb = &request->vstor_packet.vm_srb;
1312
1313
1314 /* Build the SRB */
1315 switch (scmnd->sc_data_direction) {
1316 case DMA_TO_DEVICE:
1317 vm_srb->data_in = WRITE_TYPE;
1318 break;
1319 case DMA_FROM_DEVICE:
1320 vm_srb->data_in = READ_TYPE;
1321 break;
1322 default:
1323 vm_srb->data_in = UNKNOWN_TYPE;
1324 break;
1325 }
1326
K. Y. Srinivasanb8de73d2011-08-27 11:31:27 -07001327 request->on_io_completion = storvsc_command_completion;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001328 request->context = cmd_request;/* scmnd; */
1329
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001330 vm_srb->port_number = host_dev->port;
1331 vm_srb->path_id = scmnd->device->channel;
1332 vm_srb->target_id = scmnd->device->id;
1333 vm_srb->lun = scmnd->device->lun;
1334
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001335 vm_srb->cdb_length = scmnd->cmd_len;
1336
1337 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
1338
1339 request->sense_buffer = scmnd->sense_buffer;
1340
1341
1342 request->data_buffer.len = scsi_bufflen(scmnd);
1343 if (scsi_sg_count(scmnd)) {
1344 sgl = (struct scatterlist *)scsi_sglist(scmnd);
1345 sg_count = scsi_sg_count(scmnd);
1346
1347 /* check if we need to bounce the sgl */
1348 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
1349 cmd_request->bounce_sgl =
1350 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
1351 scsi_bufflen(scmnd));
1352 if (!cmd_request->bounce_sgl) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001353 scmnd->host_scribble = NULL;
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -08001354 mempool_free(cmd_request,
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001355 memp->request_mempool);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001356
1357 return SCSI_MLQUEUE_HOST_BUSY;
1358 }
1359
1360 cmd_request->bounce_sgl_count =
1361 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
1362 PAGE_SHIFT;
1363
K. Y. Srinivasanfa23b8c2011-08-27 11:31:21 -07001364 if (vm_srb->data_in == WRITE_TYPE)
1365 copy_to_bounce_buffer(sgl,
1366 cmd_request->bounce_sgl,
1367 scsi_sg_count(scmnd));
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001368
1369 sgl = cmd_request->bounce_sgl;
1370 sg_count = cmd_request->bounce_sgl_count;
1371 }
1372
1373 request->data_buffer.offset = sgl[0].offset;
1374
1375 for (i = 0; i < sg_count; i++)
1376 request->data_buffer.pfn_array[i] =
1377 page_to_pfn(sg_page((&sgl[i])));
1378
1379 } else if (scsi_sglist(scmnd)) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001380 request->data_buffer.offset =
1381 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
1382 request->data_buffer.pfn_array[0] =
1383 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
1384 }
1385
1386retry_request:
1387 /* Invokes the vsc to start an IO */
K. Y. Srinivasan636f0fd2011-05-10 07:54:50 -07001388 ret = storvsc_do_io(dev, &cmd_request->request);
1389
K. Y. Srinivasand2598f02011-08-25 09:48:58 -07001390 if (ret == -EAGAIN) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001391 /* no more space */
1392
K. Y. Srinivasan70691ec2011-08-27 11:31:29 -07001393 if (cmd_request->bounce_sgl_count)
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001394 destroy_bounce_buffer(cmd_request->bounce_sgl,
K. Y. Srinivasan70691ec2011-08-27 11:31:29 -07001395 cmd_request->bounce_sgl_count);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001396
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001397 mempool_free(cmd_request, memp->request_mempool);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001398
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001399 scmnd->host_scribble = NULL;
1400
1401 ret = SCSI_MLQUEUE_DEVICE_BUSY;
1402 }
1403
1404 return ret;
1405}
1406
Bill Pemberton454f18a2009-07-27 16:47:24 -04001407/* Scsi driver */
Hank Janssenbef4a342009-07-13 16:01:31 -07001408static struct scsi_host_template scsi_driver = {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001409 .module = THIS_MODULE,
1410 .name = "storvsc_host_t",
1411 .bios_param = storvsc_get_chs,
1412 .queuecommand = storvsc_queuecommand,
1413 .eh_host_reset_handler = storvsc_host_reset_handler,
1414 .slave_alloc = storvsc_device_alloc,
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001415 .slave_destroy = storvsc_device_destroy,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001416 .slave_configure = storvsc_device_configure,
1417 .cmd_per_lun = 1,
1418 /* 64 max_queue * 1 target */
Lars Lindley0686e4f2010-03-11 23:51:23 +01001419 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001420 .this_id = -1,
Bill Pemberton454f18a2009-07-27 16:47:24 -04001421 /* no use setting to 0 since ll_blk_rw reset it to 1 */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001422 /* currently 32 */
1423 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
K. Y. Srinivasan039db522011-12-01 04:59:16 -08001424 .use_clustering = DISABLE_CLUSTERING,
Bill Pemberton454f18a2009-07-27 16:47:24 -04001425 /* Make sure we dont get a sg segment crosses a page boundary */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001426 .dma_boundary = PAGE_SIZE-1,
Hank Janssenbef4a342009-07-13 16:01:31 -07001427};
1428
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001429enum {
1430 SCSI_GUID,
1431 IDE_GUID,
1432};
1433
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001434static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001435 /* SCSI guid */
1436 { VMBUS_DEVICE(0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001437 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f)
1438 .driver_data = SCSI_GUID },
K. Y. Srinivasan21e37742011-08-27 11:31:18 -07001439 /* IDE guid */
1440 { VMBUS_DEVICE(0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001441 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5)
1442 .driver_data = IDE_GUID },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001443 { },
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001444};
Hank Janssenbef4a342009-07-13 16:01:31 -07001445
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001446MODULE_DEVICE_TABLE(vmbus, id_table);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001447
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001448
Hank Janssen3e189512010-03-04 22:11:00 +00001449/*
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001450 * storvsc_probe - Add a new device for this driver
1451 */
1452
K. Y. Srinivasan84946892011-09-13 10:59:38 -07001453static int storvsc_probe(struct hv_device *device,
1454 const struct hv_vmbus_device_id *dev_id)
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001455{
1456 int ret;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001457 struct Scsi_Host *host;
1458 struct hv_host_device *host_dev;
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001459 bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001460 int path = 0;
1461 int target = 0;
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001462 struct storvsc_device *stor_device;
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001463
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001464 host = scsi_host_alloc(&scsi_driver,
1465 sizeof(struct hv_host_device));
1466 if (!host)
1467 return -ENOMEM;
1468
K. Y. Srinivasan7f33f302011-11-08 09:01:44 -08001469 host_dev = shost_priv(host);
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001470 memset(host_dev, 0, sizeof(struct hv_host_device));
1471
1472 host_dev->port = host->host_no;
1473 host_dev->dev = device;
1474
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -08001475
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001476 stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001477 if (!stor_device) {
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001478 ret = -ENOMEM;
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001479 goto err_out0;
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001480 }
1481
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001482 stor_device->destroy = false;
1483 init_waitqueue_head(&stor_device->waiting_to_drain);
1484 stor_device->device = device;
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001485 stor_device->host = host;
1486 hv_set_drvdata(device, stor_device);
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001487
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001488 stor_device->port_number = host->host_no;
1489 ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001490 if (ret)
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001491 goto err_out1;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001492
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001493 if (dev_is_ide)
1494 storvsc_get_ide_info(device, &target, &path);
1495
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001496 host_dev->path = stor_device->path_id;
1497 host_dev->target = stor_device->target_id;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001498
1499 /* max # of devices per target */
1500 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
1501 /* max # of targets per channel */
1502 host->max_id = STORVSC_MAX_TARGETS;
1503 /* max # of channels */
1504 host->max_channel = STORVSC_MAX_CHANNELS - 1;
Mike Sterlingcf55f4a2011-09-06 16:10:55 -07001505 /* max cmd length */
1506 host->max_cmd_len = STORVSC_MAX_CMD_LEN;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001507
1508 /* Register the HBA and start the scsi bus scan */
1509 ret = scsi_add_host(host, &device->device);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001510 if (ret != 0)
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001511 goto err_out2;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001512
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001513 if (!dev_is_ide) {
1514 scsi_scan_host(host);
1515 return 0;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001516 }
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001517 ret = scsi_add_device(host, 0, target, 0);
1518 if (ret) {
1519 scsi_remove_host(host);
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001520 goto err_out2;
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001521 }
1522 return 0;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001523
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001524err_out2:
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001525 /*
1526 * Once we have connected with the host, we would need to
1527 * to invoke storvsc_dev_remove() to rollback this state and
1528 * this call also frees up the stor_device; hence the jump around
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001529 * err_out1 label.
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001530 */
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001531 storvsc_dev_remove(device);
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001532 goto err_out0;
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001533
1534err_out1:
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001535 kfree(stor_device);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001536
1537err_out0:
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001538 scsi_host_put(host);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001539 return ret;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001540}
1541
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -07001542/* The one and only one */
1543
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -07001544static struct hv_driver storvsc_drv = {
K. Y. Srinivasanfafb0ef2011-11-08 09:01:46 -08001545 .name = KBUILD_MODNAME,
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001546 .id_table = id_table,
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -07001547 .probe = storvsc_probe,
1548 .remove = storvsc_remove,
K. Y. Srinivasan39ae6fa2011-05-10 07:54:46 -07001549};
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -07001550
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -07001551static int __init storvsc_drv_init(void)
Hank Janssenbef4a342009-07-13 16:01:31 -07001552{
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001553 u32 max_outstanding_req_per_channel;
1554
1555 /*
1556 * Divide the ring buffer data size (which is 1 page less
1557 * than the ring buffer size since that page is reserved for
1558 * the ring buffer indices) by the max request size (which is
1559 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
1560 */
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001561 max_outstanding_req_per_channel =
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001562 ((storvsc_ringbuffer_size - PAGE_SIZE) /
1563 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
1564 sizeof(struct vstor_packet) + sizeof(u64),
1565 sizeof(u64)));
Hank Janssenbef4a342009-07-13 16:01:31 -07001566
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001567 if (max_outstanding_req_per_channel <
K. Y. Srinivasanf8feed02011-05-10 07:54:24 -07001568 STORVSC_MAX_IO_REQUESTS)
K. Y. Srinivasanb06efc12011-08-25 09:49:10 -07001569 return -EINVAL;
Hank Janssenbef4a342009-07-13 16:01:31 -07001570
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001571 return vmbus_driver_register(&storvsc_drv);
Hank Janssenbef4a342009-07-13 16:01:31 -07001572}
1573
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -07001574static void __exit storvsc_drv_exit(void)
Hank Janssenbef4a342009-07-13 16:01:31 -07001575{
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001576 vmbus_driver_unregister(&storvsc_drv);
Hank Janssenbef4a342009-07-13 16:01:31 -07001577}
1578
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001579MODULE_LICENSE("GPL");
Hank Janssen26c14cc2010-02-11 23:02:42 +00001580MODULE_VERSION(HV_DRV_VERSION);
Stephen Hemminger3afc7cc32010-05-06 21:44:45 -07001581MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -07001582module_init(storvsc_drv_init);
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -07001583module_exit(storvsc_drv_exit);