blob: 7c82d148f32e5d3b9c607b16faba0bdee61ef8ed [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. Srinivasanc1b3d062011-08-27 11:31:25 -0700288struct hv_host_device {
289 struct hv_device *dev;
290 struct kmem_cache *request_pool;
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -0800291 mempool_t *request_mempool;
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700292 unsigned int port;
293 unsigned char path;
294 unsigned char target;
295};
296
297struct storvsc_cmd_request {
298 struct list_head entry;
299 struct scsi_cmnd *cmd;
300
301 unsigned int bounce_sgl_count;
302 struct scatterlist *bounce_sgl;
303
304 struct hv_storvsc_request request;
305};
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700306
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800307struct storvsc_scan_work {
308 struct work_struct work;
309 struct Scsi_Host *host;
310 uint lun;
311};
312
313static void storvsc_bus_scan(struct work_struct *work)
314{
315 struct storvsc_scan_work *wrk;
316 int id, order_id;
317
318 wrk = container_of(work, struct storvsc_scan_work, work);
319 for (id = 0; id < wrk->host->max_id; ++id) {
320 if (wrk->host->reverse_ordering)
321 order_id = wrk->host->max_id - id - 1;
322 else
323 order_id = id;
324
325 scsi_scan_target(&wrk->host->shost_gendev, 0,
326 order_id, SCAN_WILD_CARD, 1);
327 }
328 kfree(wrk);
329}
330
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700331static inline struct storvsc_device *get_out_stor_device(
332 struct hv_device *device)
333{
334 struct storvsc_device *stor_device;
335
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700336 stor_device = hv_get_drvdata(device);
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700337
338 if (stor_device && stor_device->destroy)
339 stor_device = NULL;
340
341 return stor_device;
342}
343
344
345static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
346{
347 dev->drain_notify = true;
348 wait_event(dev->waiting_to_drain,
349 atomic_read(&dev->num_outstanding_req) == 0);
350 dev->drain_notify = false;
351}
Hank Janssenbef4a342009-07-13 16:01:31 -0700352
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700353static inline struct storvsc_device *get_in_stor_device(
354 struct hv_device *device)
355{
356 struct storvsc_device *stor_device;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700357
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700358 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700359
360 if (!stor_device)
361 goto get_in_err;
362
363 /*
364 * If the device is being destroyed; allow incoming
365 * traffic only to cleanup outstanding requests.
366 */
367
368 if (stor_device->destroy &&
369 (atomic_read(&stor_device->num_outstanding_req) == 0))
370 stor_device = NULL;
371
372get_in_err:
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700373 return stor_device;
374
375}
376
377static int storvsc_channel_init(struct hv_device *device)
378{
379 struct storvsc_device *stor_device;
380 struct hv_storvsc_request *request;
381 struct vstor_packet *vstor_packet;
382 int ret, t;
383
384 stor_device = get_out_stor_device(device);
385 if (!stor_device)
386 return -ENODEV;
387
388 request = &stor_device->init_request;
389 vstor_packet = &request->vstor_packet;
390
391 /*
392 * Now, initiate the vsc/vsp initialization protocol on the open
393 * channel
394 */
395 memset(request, 0, sizeof(struct hv_storvsc_request));
396 init_completion(&request->wait_event);
397 vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
398 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
399
400 ret = vmbus_sendpacket(device->channel, vstor_packet,
401 sizeof(struct vstor_packet),
402 (unsigned long)request,
403 VM_PKT_DATA_INBAND,
404 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
405 if (ret != 0)
406 goto cleanup;
407
408 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
409 if (t == 0) {
410 ret = -ETIMEDOUT;
411 goto cleanup;
412 }
413
414 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
415 vstor_packet->status != 0)
416 goto cleanup;
417
418
419 /* reuse the packet for version range supported */
420 memset(vstor_packet, 0, sizeof(struct vstor_packet));
421 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
422 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
423
424 vstor_packet->version.major_minor = VMSTOR_PROTOCOL_VERSION_CURRENT;
425 FILL_VMSTOR_REVISION(vstor_packet->version.revision);
426
427 ret = vmbus_sendpacket(device->channel, vstor_packet,
428 sizeof(struct vstor_packet),
429 (unsigned long)request,
430 VM_PKT_DATA_INBAND,
431 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
432 if (ret != 0)
433 goto cleanup;
434
435 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
436 if (t == 0) {
437 ret = -ETIMEDOUT;
438 goto cleanup;
439 }
440
441 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
442 vstor_packet->status != 0)
443 goto cleanup;
444
445
446 memset(vstor_packet, 0, sizeof(struct vstor_packet));
447 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
448 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
449 vstor_packet->storage_channel_properties.port_number =
450 stor_device->port_number;
451
452 ret = vmbus_sendpacket(device->channel, vstor_packet,
453 sizeof(struct vstor_packet),
454 (unsigned long)request,
455 VM_PKT_DATA_INBAND,
456 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
457
458 if (ret != 0)
459 goto cleanup;
460
461 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
462 if (t == 0) {
463 ret = -ETIMEDOUT;
464 goto cleanup;
465 }
466
467 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
468 vstor_packet->status != 0)
469 goto cleanup;
470
471 stor_device->path_id = vstor_packet->storage_channel_properties.path_id;
472 stor_device->target_id
473 = vstor_packet->storage_channel_properties.target_id;
474
475 memset(vstor_packet, 0, sizeof(struct vstor_packet));
476 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
477 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
478
479 ret = vmbus_sendpacket(device->channel, vstor_packet,
480 sizeof(struct vstor_packet),
481 (unsigned long)request,
482 VM_PKT_DATA_INBAND,
483 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
484
485 if (ret != 0)
486 goto cleanup;
487
488 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
489 if (t == 0) {
490 ret = -ETIMEDOUT;
491 goto cleanup;
492 }
493
494 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
495 vstor_packet->status != 0)
496 goto cleanup;
497
498
499cleanup:
500 return ret;
501}
502
503static void storvsc_on_io_completion(struct hv_device *device,
504 struct vstor_packet *vstor_packet,
505 struct hv_storvsc_request *request)
506{
507 struct storvsc_device *stor_device;
508 struct vstor_packet *stor_pkt;
509
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700510 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700511 stor_pkt = &request->vstor_packet;
512
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -0700513 /*
514 * The current SCSI handling on the host side does
515 * not correctly handle:
516 * INQUIRY command with page code parameter set to 0x80
517 * MODE_SENSE command with cmd[2] == 0x1c
518 *
519 * Setup srb and scsi status so this won't be fatal.
520 * We do this so we can distinguish truly fatal failues
521 * (srb status == 0x4) and off-line the device in that case.
522 */
523
524 if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
525 (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
526 vstor_packet->vm_srb.scsi_status = 0;
527 vstor_packet->vm_srb.srb_status = 0x1;
528 }
529
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700530
531 /* Copy over the status...etc */
532 stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
533 stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
534 stor_pkt->vm_srb.sense_info_length =
535 vstor_packet->vm_srb.sense_info_length;
536
537 if (vstor_packet->vm_srb.scsi_status != 0 ||
538 vstor_packet->vm_srb.srb_status != 1){
Greg Kroah-Hartmand181daa2011-10-11 09:20:31 -0600539 dev_warn(&device->device,
540 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
541 stor_pkt->vm_srb.cdb[0],
542 vstor_packet->vm_srb.scsi_status,
543 vstor_packet->vm_srb.srb_status);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700544 }
545
546 if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
547 /* CHECK_CONDITION */
548 if (vstor_packet->vm_srb.srb_status & 0x80) {
549 /* autosense data available */
Greg Kroah-Hartmand181daa2011-10-11 09:20:31 -0600550 dev_warn(&device->device,
K. Y. Srinivasan41098f82011-10-14 21:31:57 -0700551 "stor pkt %p autosense data valid - len %d\n",
552 request,
553 vstor_packet->vm_srb.sense_info_length);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700554
555 memcpy(request->sense_buffer,
556 vstor_packet->vm_srb.sense_data,
557 vstor_packet->vm_srb.sense_info_length);
558
559 }
560 }
561
562 stor_pkt->vm_srb.data_transfer_length =
563 vstor_packet->vm_srb.data_transfer_length;
564
565 request->on_io_completion(request);
566
567 if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
568 stor_device->drain_notify)
569 wake_up(&stor_device->waiting_to_drain);
570
571
572}
573
574static void storvsc_on_receive(struct hv_device *device,
575 struct vstor_packet *vstor_packet,
576 struct hv_storvsc_request *request)
577{
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800578 struct storvsc_scan_work *work;
579 struct storvsc_device *stor_device;
580
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700581 switch (vstor_packet->operation) {
582 case VSTOR_OPERATION_COMPLETE_IO:
583 storvsc_on_io_completion(device, vstor_packet, request);
584 break;
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800585
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700586 case VSTOR_OPERATION_REMOVE_DEVICE:
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800587 case VSTOR_OPERATION_ENUMERATE_BUS:
588 stor_device = get_in_stor_device(device);
589 work = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
590 if (!work)
591 return;
592
593 INIT_WORK(&work->work, storvsc_bus_scan);
594 work->host = stor_device->host;
595 schedule_work(&work->work);
596 break;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700597
598 default:
599 break;
600 }
601}
602
603static void storvsc_on_channel_callback(void *context)
604{
605 struct hv_device *device = (struct hv_device *)context;
606 struct storvsc_device *stor_device;
607 u32 bytes_recvd;
608 u64 request_id;
609 unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
610 struct hv_storvsc_request *request;
611 int ret;
612
613
614 stor_device = get_in_stor_device(device);
615 if (!stor_device)
616 return;
617
618 do {
619 ret = vmbus_recvpacket(device->channel, packet,
620 ALIGN(sizeof(struct vstor_packet), 8),
621 &bytes_recvd, &request_id);
622 if (ret == 0 && bytes_recvd > 0) {
623
624 request = (struct hv_storvsc_request *)
625 (unsigned long)request_id;
626
627 if ((request == &stor_device->init_request) ||
628 (request == &stor_device->reset_request)) {
629
630 memcpy(&request->vstor_packet, packet,
631 sizeof(struct vstor_packet));
632 complete(&request->wait_event);
633 } else {
634 storvsc_on_receive(device,
635 (struct vstor_packet *)packet,
636 request);
637 }
638 } else {
639 break;
640 }
641 } while (1);
642
643 return;
644}
645
646static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
647{
648 struct vmstorage_channel_properties props;
649 int ret;
650
651 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
652
653 /* Open the channel */
654 ret = vmbus_open(device->channel,
655 ring_size,
656 ring_size,
657 (void *)&props,
658 sizeof(struct vmstorage_channel_properties),
659 storvsc_on_channel_callback, device);
660
661 if (ret != 0)
662 return ret;
663
664 ret = storvsc_channel_init(device);
665
666 return ret;
667}
668
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700669static int storvsc_dev_remove(struct hv_device *device)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700670{
671 struct storvsc_device *stor_device;
672 unsigned long flags;
673
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700674 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700675
676 spin_lock_irqsave(&device->channel->inbound_lock, flags);
677 stor_device->destroy = true;
678 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
679
680 /*
681 * At this point, all outbound traffic should be disable. We
682 * only allow inbound traffic (responses) to proceed so that
683 * outstanding requests can be completed.
684 */
685
686 storvsc_wait_to_drain(stor_device);
687
688 /*
689 * Since we have already drained, we don't need to busy wait
690 * as was done in final_release_stor_device()
691 * Note that we cannot set the ext pointer to NULL until
692 * we have drained - to drain the outgoing packets, we need to
693 * allow incoming packets.
694 */
695 spin_lock_irqsave(&device->channel->inbound_lock, flags);
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700696 hv_set_drvdata(device, NULL);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700697 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
698
699 /* Close the channel */
700 vmbus_close(device->channel);
701
702 kfree(stor_device);
703 return 0;
704}
705
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700706static int storvsc_do_io(struct hv_device *device,
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700707 struct hv_storvsc_request *request)
708{
709 struct storvsc_device *stor_device;
710 struct vstor_packet *vstor_packet;
711 int ret = 0;
712
713 vstor_packet = &request->vstor_packet;
714 stor_device = get_out_stor_device(device);
715
716 if (!stor_device)
717 return -ENODEV;
718
719
720 request->device = device;
721
722
723 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
724
725 vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
726
727
728 vstor_packet->vm_srb.sense_info_length = SENSE_BUFFER_SIZE;
729
730
731 vstor_packet->vm_srb.data_transfer_length =
732 request->data_buffer.len;
733
734 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
735
736 if (request->data_buffer.len) {
737 ret = vmbus_sendpacket_multipagebuffer(device->channel,
738 &request->data_buffer,
739 vstor_packet,
740 sizeof(struct vstor_packet),
741 (unsigned long)request);
742 } else {
743 ret = vmbus_sendpacket(device->channel, vstor_packet,
744 sizeof(struct vstor_packet),
745 (unsigned long)request,
746 VM_PKT_DATA_INBAND,
747 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
748 }
749
750 if (ret != 0)
751 return ret;
752
753 atomic_inc(&stor_device->num_outstanding_req);
754
755 return ret;
756}
757
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -0700758static void storvsc_get_ide_info(struct hv_device *dev, int *target, int *path)
759{
760 *target =
761 dev->dev_instance.b[5] << 8 | dev->dev_instance.b[4];
762
763 *path =
764 dev->dev_instance.b[3] << 24 |
765 dev->dev_instance.b[2] << 16 |
766 dev->dev_instance.b[1] << 8 | dev->dev_instance.b[0];
767}
768
Hank Janssenbef4a342009-07-13 16:01:31 -0700769
K. Y. Srinivasan5b60ace2011-05-10 07:54:25 -0700770static int storvsc_device_alloc(struct scsi_device *sdevice)
771{
772 /*
773 * This enables luns to be located sparsely. Otherwise, we may not
774 * discovered them.
775 */
776 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
777 return 0;
778}
779
K. Y. Srinivasana1ebfea2011-05-10 07:54:26 -0700780static int storvsc_merge_bvec(struct request_queue *q,
781 struct bvec_merge_data *bmd, struct bio_vec *bvec)
782{
783 /* checking done by caller. */
784 return bvec->bv_len;
785}
786
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700787static int storvsc_device_configure(struct scsi_device *sdevice)
788{
789 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
790 STORVSC_MAX_IO_REQUESTS);
791
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700792 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
793
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700794 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
795
796 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700797
798 return 0;
799}
800
K. Y. Srinivasan49a3c7a2011-05-10 07:54:28 -0700801static void destroy_bounce_buffer(struct scatterlist *sgl,
802 unsigned int sg_count)
803{
804 int i;
805 struct page *page_buf;
806
807 for (i = 0; i < sg_count; i++) {
808 page_buf = sg_page((&sgl[i]));
809 if (page_buf != NULL)
810 __free_page(page_buf);
811 }
812
813 kfree(sgl);
814}
815
K. Y. Srinivasan3862ef32011-05-10 07:54:29 -0700816static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
817{
818 int i;
819
820 /* No need to check */
821 if (sg_count < 2)
822 return -1;
823
824 /* We have at least 2 sg entries */
825 for (i = 0; i < sg_count; i++) {
826 if (i == 0) {
827 /* make sure 1st one does not have hole */
828 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
829 return i;
830 } else if (i == sg_count - 1) {
831 /* make sure last one does not have hole */
832 if (sgl[i].offset != 0)
833 return i;
834 } else {
835 /* make sure no hole in the middle */
836 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
837 return i;
838 }
839 }
840 return -1;
841}
842
K. Y. Srinivasana9753cb2011-05-10 07:54:30 -0700843static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
844 unsigned int sg_count,
845 unsigned int len)
846{
847 int i;
848 int num_pages;
849 struct scatterlist *bounce_sgl;
850 struct page *page_buf;
851
852 num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
853
854 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
855 if (!bounce_sgl)
856 return NULL;
857
858 for (i = 0; i < num_pages; i++) {
859 page_buf = alloc_page(GFP_ATOMIC);
860 if (!page_buf)
861 goto cleanup;
862 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
863 }
864
865 return bounce_sgl;
866
867cleanup:
868 destroy_bounce_buffer(bounce_sgl, num_pages);
869 return NULL;
870}
871
K. Y. Srinivasan29fe2c92011-05-10 07:54:31 -0700872
873/* Assume the original sgl has enough room */
874static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
875 struct scatterlist *bounce_sgl,
876 unsigned int orig_sgl_count)
877{
878 int i;
879 int j = 0;
880 unsigned long src, dest;
881 unsigned int srclen, destlen, copylen;
882 unsigned int total_copied = 0;
883 unsigned long bounce_addr = 0;
884 unsigned long dest_addr = 0;
885 unsigned long flags;
886
887 local_irq_save(flags);
888
889 for (i = 0; i < orig_sgl_count; i++) {
890 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
891 KM_IRQ0) + orig_sgl[i].offset;
892 dest = dest_addr;
893 destlen = orig_sgl[i].length;
894
895 if (bounce_addr == 0)
896 bounce_addr =
897 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
898 KM_IRQ0);
899
900 while (destlen) {
901 src = bounce_addr + bounce_sgl[j].offset;
902 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
903
904 copylen = min(srclen, destlen);
905 memcpy((void *)dest, (void *)src, copylen);
906
907 total_copied += copylen;
908 bounce_sgl[j].offset += copylen;
909 destlen -= copylen;
910 dest += copylen;
911
912 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
913 /* full */
914 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
915 j++;
916
917 /* if we need to use another bounce buffer */
918 if (destlen || i != orig_sgl_count - 1)
919 bounce_addr =
920 (unsigned long)kmap_atomic(
921 sg_page((&bounce_sgl[j])), KM_IRQ0);
922 } else if (destlen == 0 && i == orig_sgl_count - 1) {
923 /* unmap the last bounce that is < PAGE_SIZE */
924 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
925 }
926 }
927
928 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
929 KM_IRQ0);
930 }
931
932 local_irq_restore(flags);
933
934 return total_copied;
935}
936
K. Y. Srinivasan6a8ff442011-05-10 07:54:32 -0700937
938/* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
939static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
940 struct scatterlist *bounce_sgl,
941 unsigned int orig_sgl_count)
942{
943 int i;
944 int j = 0;
945 unsigned long src, dest;
946 unsigned int srclen, destlen, copylen;
947 unsigned int total_copied = 0;
948 unsigned long bounce_addr = 0;
949 unsigned long src_addr = 0;
950 unsigned long flags;
951
952 local_irq_save(flags);
953
954 for (i = 0; i < orig_sgl_count; i++) {
955 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
956 KM_IRQ0) + orig_sgl[i].offset;
957 src = src_addr;
958 srclen = orig_sgl[i].length;
959
960 if (bounce_addr == 0)
961 bounce_addr =
962 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
963 KM_IRQ0);
964
965 while (srclen) {
966 /* assume bounce offset always == 0 */
967 dest = bounce_addr + bounce_sgl[j].length;
968 destlen = PAGE_SIZE - bounce_sgl[j].length;
969
970 copylen = min(srclen, destlen);
971 memcpy((void *)dest, (void *)src, copylen);
972
973 total_copied += copylen;
974 bounce_sgl[j].length += copylen;
975 srclen -= copylen;
976 src += copylen;
977
978 if (bounce_sgl[j].length == PAGE_SIZE) {
979 /* full..move to next entry */
980 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
981 j++;
982
983 /* if we need to use another bounce buffer */
984 if (srclen || i != orig_sgl_count - 1)
985 bounce_addr =
986 (unsigned long)kmap_atomic(
987 sg_page((&bounce_sgl[j])), KM_IRQ0);
988
989 } else if (srclen == 0 && i == orig_sgl_count - 1) {
990 /* unmap the last bounce that is < PAGE_SIZE */
991 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
992 }
993 }
994
995 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
996 }
997
998 local_irq_restore(flags);
999
1000 return total_copied;
1001}
1002
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -07001003
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -07001004static int storvsc_remove(struct hv_device *dev)
1005{
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001006 struct storvsc_device *stor_device = hv_get_drvdata(dev);
1007 struct Scsi_Host *host = stor_device->host;
K. Y. Srinivasan7f33f302011-11-08 09:01:44 -08001008 struct hv_host_device *host_dev = shost_priv(host);
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -07001009
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -07001010 scsi_remove_host(host);
1011
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -07001012 scsi_host_put(host);
K. Y. Srinivasand36b0a02011-06-06 15:49:29 -07001013
K. Y. Srinivasan2935a402011-06-06 15:49:28 -07001014 storvsc_dev_remove(dev);
1015 if (host_dev->request_pool) {
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -08001016 mempool_destroy(host_dev->request_mempool);
K. Y. Srinivasan2935a402011-06-06 15:49:28 -07001017 kmem_cache_destroy(host_dev->request_pool);
1018 host_dev->request_pool = NULL;
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -08001019 host_dev->request_mempool = NULL;
K. Y. Srinivasan2935a402011-06-06 15:49:28 -07001020 }
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -07001021 return 0;
1022}
1023
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001024
1025static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
1026 sector_t capacity, int *info)
1027{
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001028 sector_t nsect = capacity;
1029 sector_t cylinders = nsect;
1030 int heads, sectors_pt;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001031
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001032 /*
1033 * We are making up these values; let us keep it simple.
1034 */
1035 heads = 0xff;
1036 sectors_pt = 0x3f; /* Sectors per track */
1037 sector_div(cylinders, heads * sectors_pt);
1038 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
1039 cylinders = 0xffff;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001040
1041 info[0] = heads;
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001042 info[1] = sectors_pt;
1043 info[2] = (int)cylinders;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001044
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001045 return 0;
1046}
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001047
1048static int storvsc_host_reset(struct hv_device *device)
1049{
1050 struct storvsc_device *stor_device;
1051 struct hv_storvsc_request *request;
1052 struct vstor_packet *vstor_packet;
1053 int ret, t;
1054
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001055
K. Y. Srinivasan1eaaddf2011-08-27 11:31:03 -07001056 stor_device = get_out_stor_device(device);
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001057 if (!stor_device)
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001058 return FAILED;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001059
1060 request = &stor_device->reset_request;
1061 vstor_packet = &request->vstor_packet;
1062
1063 init_completion(&request->wait_event);
1064
1065 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
1066 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
1067 vstor_packet->vm_srb.path_id = stor_device->path_id;
1068
1069 ret = vmbus_sendpacket(device->channel, vstor_packet,
1070 sizeof(struct vstor_packet),
1071 (unsigned long)&stor_device->reset_request,
1072 VM_PKT_DATA_INBAND,
1073 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1074 if (ret != 0)
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001075 return FAILED;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001076
K. Y. Srinivasan46d2eb62011-06-16 13:16:36 -07001077 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001078 if (t == 0)
1079 return TIMEOUT_ERROR;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001080
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001081
1082 /*
1083 * At this point, all outstanding requests in the adapter
1084 * should have been flushed out and return to us
1085 */
1086
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001087 return SUCCESS;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001088}
1089
1090
K. Y. Srinivasan96e690be2011-05-10 07:54:38 -07001091/*
1092 * storvsc_host_reset_handler - Reset the scsi HBA
1093 */
1094static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
1095{
K. Y. Srinivasan7f33f302011-11-08 09:01:44 -08001096 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
K. Y. Srinivasan96e690be2011-05-10 07:54:38 -07001097 struct hv_device *dev = host_dev->dev;
1098
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001099 return storvsc_host_reset(dev);
K. Y. Srinivasan96e690be2011-05-10 07:54:38 -07001100}
1101
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001102
1103/*
K. Y. Srinivasanb8de73d2011-08-27 11:31:27 -07001104 * storvsc_command_completion - Command completion processing
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001105 */
K. Y. Srinivasanb8de73d2011-08-27 11:31:27 -07001106static void storvsc_command_completion(struct hv_storvsc_request *request)
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001107{
1108 struct storvsc_cmd_request *cmd_request =
1109 (struct storvsc_cmd_request *)request->context;
1110 struct scsi_cmnd *scmnd = cmd_request->cmd;
K. Y. Srinivasan7f33f302011-11-08 09:01:44 -08001111 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001112 void (*scsi_done_fn)(struct scsi_cmnd *);
1113 struct scsi_sense_hdr sense_hdr;
1114 struct vmscsi_request *vm_srb;
1115
K. Y. Srinivasan3612ef92011-08-27 11:31:20 -07001116 vm_srb = &request->vstor_packet.vm_srb;
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001117 if (cmd_request->bounce_sgl_count) {
K. Y. Srinivasan3612ef92011-08-27 11:31:20 -07001118 if (vm_srb->data_in == READ_TYPE) {
1119 copy_from_bounce_buffer(scsi_sglist(scmnd),
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001120 cmd_request->bounce_sgl,
1121 scsi_sg_count(scmnd));
K. Y. Srinivasan3612ef92011-08-27 11:31:20 -07001122 destroy_bounce_buffer(cmd_request->bounce_sgl,
1123 cmd_request->bounce_sgl_count);
1124 }
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001125 }
1126
K. Y. Srinivasan2544b792011-08-27 11:31:28 -07001127 /*
1128 * If there is an error; offline the device since all
1129 * error recovery strategies would have already been
1130 * deployed on the host side.
1131 */
1132 if (vm_srb->srb_status == 0x4)
1133 scmnd->result = DID_TARGET_FAILURE << 16;
1134 else
1135 scmnd->result = vm_srb->scsi_status;
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001136
1137 if (scmnd->result) {
1138 if (scsi_normalize_sense(scmnd->sense_buffer,
1139 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
1140 scsi_print_sense_hdr("storvsc", &sense_hdr);
1141 }
1142
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001143 scsi_set_resid(scmnd,
1144 request->data_buffer.len -
1145 vm_srb->data_transfer_length);
1146
1147 scsi_done_fn = scmnd->scsi_done;
1148
1149 scmnd->host_scribble = NULL;
1150 scmnd->scsi_done = NULL;
1151
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001152 scsi_done_fn(scmnd);
1153
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -08001154 mempool_free(cmd_request, host_dev->request_mempool);
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001155}
1156
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001157static bool storvsc_check_scsi_cmd(struct scsi_cmnd *scmnd)
1158{
1159 bool allowed = true;
1160 u8 scsi_op = scmnd->cmnd[0];
1161
1162 switch (scsi_op) {
K. Y. Srinivasan41098f82011-10-14 21:31:57 -07001163 /* smartd sends this command, which will offline the device */
1164 case SET_WINDOW:
K. Y. Srinivasan59e00e72011-11-08 09:01:42 -08001165 scmnd->result = ILLEGAL_REQUEST << 16;
K. Y. Srinivasan41098f82011-10-14 21:31:57 -07001166 allowed = false;
1167 break;
1168 default:
1169 break;
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001170 }
1171 return allowed;
1172}
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001173
1174/*
1175 * storvsc_queuecommand - Initiate command processing
1176 */
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001177static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001178{
1179 int ret;
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001180 struct hv_host_device *host_dev = shost_priv(host);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001181 struct hv_device *dev = host_dev->dev;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001182 struct hv_storvsc_request *request;
1183 struct storvsc_cmd_request *cmd_request;
1184 unsigned int request_size = 0;
1185 int i;
1186 struct scatterlist *sgl;
1187 unsigned int sg_count = 0;
1188 struct vmscsi_request *vm_srb;
1189
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001190 if (storvsc_check_scsi_cmd(scmnd) == false) {
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001191 scmnd->scsi_done(scmnd);
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001192 return 0;
1193 }
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001194
1195 /* If retrying, no need to prep the cmd */
1196 if (scmnd->host_scribble) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001197
1198 cmd_request =
1199 (struct storvsc_cmd_request *)scmnd->host_scribble;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001200
1201 goto retry_request;
1202 }
1203
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001204 request_size = sizeof(struct storvsc_cmd_request);
1205
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -08001206 cmd_request = mempool_alloc(host_dev->request_mempool,
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001207 GFP_ATOMIC);
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001208 if (!cmd_request)
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001209 return SCSI_MLQUEUE_DEVICE_BUSY;
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001210
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -08001211 memset(cmd_request, 0, sizeof(struct storvsc_cmd_request));
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001212
1213 /* Setup the cmd request */
1214 cmd_request->bounce_sgl_count = 0;
1215 cmd_request->bounce_sgl = NULL;
1216 cmd_request->cmd = scmnd;
1217
1218 scmnd->host_scribble = (unsigned char *)cmd_request;
1219
1220 request = &cmd_request->request;
1221 vm_srb = &request->vstor_packet.vm_srb;
1222
1223
1224 /* Build the SRB */
1225 switch (scmnd->sc_data_direction) {
1226 case DMA_TO_DEVICE:
1227 vm_srb->data_in = WRITE_TYPE;
1228 break;
1229 case DMA_FROM_DEVICE:
1230 vm_srb->data_in = READ_TYPE;
1231 break;
1232 default:
1233 vm_srb->data_in = UNKNOWN_TYPE;
1234 break;
1235 }
1236
K. Y. Srinivasanb8de73d2011-08-27 11:31:27 -07001237 request->on_io_completion = storvsc_command_completion;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001238 request->context = cmd_request;/* scmnd; */
1239
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001240 vm_srb->port_number = host_dev->port;
1241 vm_srb->path_id = scmnd->device->channel;
1242 vm_srb->target_id = scmnd->device->id;
1243 vm_srb->lun = scmnd->device->lun;
1244
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001245 vm_srb->cdb_length = scmnd->cmd_len;
1246
1247 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
1248
1249 request->sense_buffer = scmnd->sense_buffer;
1250
1251
1252 request->data_buffer.len = scsi_bufflen(scmnd);
1253 if (scsi_sg_count(scmnd)) {
1254 sgl = (struct scatterlist *)scsi_sglist(scmnd);
1255 sg_count = scsi_sg_count(scmnd);
1256
1257 /* check if we need to bounce the sgl */
1258 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
1259 cmd_request->bounce_sgl =
1260 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
1261 scsi_bufflen(scmnd));
1262 if (!cmd_request->bounce_sgl) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001263 scmnd->host_scribble = NULL;
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -08001264 mempool_free(cmd_request,
1265 host_dev->request_mempool);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001266
1267 return SCSI_MLQUEUE_HOST_BUSY;
1268 }
1269
1270 cmd_request->bounce_sgl_count =
1271 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
1272 PAGE_SHIFT;
1273
K. Y. Srinivasanfa23b8c2011-08-27 11:31:21 -07001274 if (vm_srb->data_in == WRITE_TYPE)
1275 copy_to_bounce_buffer(sgl,
1276 cmd_request->bounce_sgl,
1277 scsi_sg_count(scmnd));
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001278
1279 sgl = cmd_request->bounce_sgl;
1280 sg_count = cmd_request->bounce_sgl_count;
1281 }
1282
1283 request->data_buffer.offset = sgl[0].offset;
1284
1285 for (i = 0; i < sg_count; i++)
1286 request->data_buffer.pfn_array[i] =
1287 page_to_pfn(sg_page((&sgl[i])));
1288
1289 } else if (scsi_sglist(scmnd)) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001290 request->data_buffer.offset =
1291 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
1292 request->data_buffer.pfn_array[0] =
1293 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
1294 }
1295
1296retry_request:
1297 /* Invokes the vsc to start an IO */
K. Y. Srinivasan636f0fd2011-05-10 07:54:50 -07001298 ret = storvsc_do_io(dev, &cmd_request->request);
1299
K. Y. Srinivasand2598f02011-08-25 09:48:58 -07001300 if (ret == -EAGAIN) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001301 /* no more space */
1302
K. Y. Srinivasan70691ec2011-08-27 11:31:29 -07001303 if (cmd_request->bounce_sgl_count)
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001304 destroy_bounce_buffer(cmd_request->bounce_sgl,
K. Y. Srinivasan70691ec2011-08-27 11:31:29 -07001305 cmd_request->bounce_sgl_count);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001306
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -08001307 mempool_free(cmd_request, host_dev->request_mempool);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001308
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001309 scmnd->host_scribble = NULL;
1310
1311 ret = SCSI_MLQUEUE_DEVICE_BUSY;
1312 }
1313
1314 return ret;
1315}
1316
Bill Pemberton454f18a2009-07-27 16:47:24 -04001317/* Scsi driver */
Hank Janssenbef4a342009-07-13 16:01:31 -07001318static struct scsi_host_template scsi_driver = {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001319 .module = THIS_MODULE,
1320 .name = "storvsc_host_t",
1321 .bios_param = storvsc_get_chs,
1322 .queuecommand = storvsc_queuecommand,
1323 .eh_host_reset_handler = storvsc_host_reset_handler,
1324 .slave_alloc = storvsc_device_alloc,
1325 .slave_configure = storvsc_device_configure,
1326 .cmd_per_lun = 1,
1327 /* 64 max_queue * 1 target */
Lars Lindley0686e4f2010-03-11 23:51:23 +01001328 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001329 .this_id = -1,
Bill Pemberton454f18a2009-07-27 16:47:24 -04001330 /* no use setting to 0 since ll_blk_rw reset it to 1 */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001331 /* currently 32 */
1332 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
1333 /*
1334 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
1335 * into 1 sg element. If set, we must limit the max_segment_size to
1336 * PAGE_SIZE, otherwise we may get 1 sg element that represents
1337 * multiple
1338 */
Bill Pemberton454f18a2009-07-27 16:47:24 -04001339 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001340 .use_clustering = ENABLE_CLUSTERING,
Bill Pemberton454f18a2009-07-27 16:47:24 -04001341 /* Make sure we dont get a sg segment crosses a page boundary */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001342 .dma_boundary = PAGE_SIZE-1,
Hank Janssenbef4a342009-07-13 16:01:31 -07001343};
1344
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001345enum {
1346 SCSI_GUID,
1347 IDE_GUID,
1348};
1349
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001350static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001351 /* SCSI guid */
1352 { VMBUS_DEVICE(0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001353 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f)
1354 .driver_data = SCSI_GUID },
K. Y. Srinivasan21e37742011-08-27 11:31:18 -07001355 /* IDE guid */
1356 { VMBUS_DEVICE(0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001357 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5)
1358 .driver_data = IDE_GUID },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001359 { },
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001360};
Hank Janssenbef4a342009-07-13 16:01:31 -07001361
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001362MODULE_DEVICE_TABLE(vmbus, id_table);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001363
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001364
Hank Janssen3e189512010-03-04 22:11:00 +00001365/*
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001366 * storvsc_probe - Add a new device for this driver
1367 */
1368
K. Y. Srinivasan84946892011-09-13 10:59:38 -07001369static int storvsc_probe(struct hv_device *device,
1370 const struct hv_vmbus_device_id *dev_id)
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001371{
1372 int ret;
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -08001373 int number = STORVSC_MIN_BUF_NR;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001374 struct Scsi_Host *host;
1375 struct hv_host_device *host_dev;
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001376 bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001377 int path = 0;
1378 int target = 0;
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001379 struct storvsc_device *stor_device;
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001380
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001381 host = scsi_host_alloc(&scsi_driver,
1382 sizeof(struct hv_host_device));
1383 if (!host)
1384 return -ENOMEM;
1385
K. Y. Srinivasan7f33f302011-11-08 09:01:44 -08001386 host_dev = shost_priv(host);
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001387 memset(host_dev, 0, sizeof(struct hv_host_device));
1388
1389 host_dev->port = host->host_no;
1390 host_dev->dev = device;
1391
1392 host_dev->request_pool =
1393 kmem_cache_create(dev_name(&device->device),
1394 sizeof(struct storvsc_cmd_request), 0,
1395 SLAB_HWCACHE_ALIGN, NULL);
1396
1397 if (!host_dev->request_pool) {
1398 scsi_host_put(host);
1399 return -ENOMEM;
1400 }
1401
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -08001402 host_dev->request_mempool = mempool_create(number, mempool_alloc_slab,
1403 mempool_free_slab,
1404 host_dev->request_pool);
1405
1406 if (!host_dev->request_mempool) {
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001407 ret = -ENOMEM;
1408 goto err_out0;
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -08001409 }
1410
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001411 stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001412 if (!stor_device) {
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001413 ret = -ENOMEM;
1414 goto err_out1;
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001415 }
1416
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001417 stor_device->destroy = false;
1418 init_waitqueue_head(&stor_device->waiting_to_drain);
1419 stor_device->device = device;
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001420 stor_device->host = host;
1421 hv_set_drvdata(device, stor_device);
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001422
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001423 stor_device->port_number = host->host_no;
1424 ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001425 if (ret)
1426 goto err_out2;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001427
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001428 if (dev_is_ide)
1429 storvsc_get_ide_info(device, &target, &path);
1430
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001431 host_dev->path = stor_device->path_id;
1432 host_dev->target = stor_device->target_id;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001433
1434 /* max # of devices per target */
1435 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
1436 /* max # of targets per channel */
1437 host->max_id = STORVSC_MAX_TARGETS;
1438 /* max # of channels */
1439 host->max_channel = STORVSC_MAX_CHANNELS - 1;
Mike Sterlingcf55f4a2011-09-06 16:10:55 -07001440 /* max cmd length */
1441 host->max_cmd_len = STORVSC_MAX_CMD_LEN;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001442
1443 /* Register the HBA and start the scsi bus scan */
1444 ret = scsi_add_host(host, &device->device);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001445 if (ret != 0)
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001446 goto err_out3;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001447
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001448 if (!dev_is_ide) {
1449 scsi_scan_host(host);
1450 return 0;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001451 }
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001452 ret = scsi_add_device(host, 0, target, 0);
1453 if (ret) {
1454 scsi_remove_host(host);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001455 goto err_out3;
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001456 }
1457 return 0;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001458
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001459err_out3:
1460 /*
1461 * Once we have connected with the host, we would need to
1462 * to invoke storvsc_dev_remove() to rollback this state and
1463 * this call also frees up the stor_device; hence the jump around
1464 * err_out2 label.
1465 */
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001466 storvsc_dev_remove(device);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001467 goto err_out1;
1468
1469err_out2:
1470 kfree(stor_device);
1471
1472err_out1:
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -08001473 mempool_destroy(host_dev->request_mempool);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001474
1475err_out0:
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001476 kmem_cache_destroy(host_dev->request_pool);
1477 scsi_host_put(host);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001478 return ret;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001479}
1480
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -07001481/* The one and only one */
1482
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -07001483static struct hv_driver storvsc_drv = {
K. Y. Srinivasanfafb0ef2011-11-08 09:01:46 -08001484 .name = KBUILD_MODNAME,
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001485 .id_table = id_table,
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -07001486 .probe = storvsc_probe,
1487 .remove = storvsc_remove,
K. Y. Srinivasan39ae6fa2011-05-10 07:54:46 -07001488};
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -07001489
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -07001490static int __init storvsc_drv_init(void)
Hank Janssenbef4a342009-07-13 16:01:31 -07001491{
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001492 u32 max_outstanding_req_per_channel;
1493
1494 /*
1495 * Divide the ring buffer data size (which is 1 page less
1496 * than the ring buffer size since that page is reserved for
1497 * the ring buffer indices) by the max request size (which is
1498 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
1499 */
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001500 max_outstanding_req_per_channel =
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001501 ((storvsc_ringbuffer_size - PAGE_SIZE) /
1502 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
1503 sizeof(struct vstor_packet) + sizeof(u64),
1504 sizeof(u64)));
Hank Janssenbef4a342009-07-13 16:01:31 -07001505
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001506 if (max_outstanding_req_per_channel <
K. Y. Srinivasanf8feed02011-05-10 07:54:24 -07001507 STORVSC_MAX_IO_REQUESTS)
K. Y. Srinivasanb06efc12011-08-25 09:49:10 -07001508 return -EINVAL;
Hank Janssenbef4a342009-07-13 16:01:31 -07001509
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001510 return vmbus_driver_register(&storvsc_drv);
Hank Janssenbef4a342009-07-13 16:01:31 -07001511}
1512
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -07001513static void __exit storvsc_drv_exit(void)
Hank Janssenbef4a342009-07-13 16:01:31 -07001514{
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001515 vmbus_driver_unregister(&storvsc_drv);
Hank Janssenbef4a342009-07-13 16:01:31 -07001516}
1517
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001518MODULE_LICENSE("GPL");
Hank Janssen26c14cc2010-02-11 23:02:42 +00001519MODULE_VERSION(HV_DRV_VERSION);
Stephen Hemminger3afc7cc32010-05-06 21:44:45 -07001520MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -07001521module_init(storvsc_drv_init);
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -07001522module_exit(storvsc_drv_exit);