blob: d575bc92ffc4606caa494d2babc434e0eef73b12 [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>
Hank Janssenbef4a342009-07-13 16:01:31 -070034#include <scsi/scsi.h>
35#include <scsi/scsi_cmnd.h>
36#include <scsi/scsi_host.h>
37#include <scsi/scsi_device.h>
38#include <scsi/scsi_tcq.h>
39#include <scsi/scsi_eh.h>
40#include <scsi/scsi_devinfo.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070041#include <scsi/scsi_dbg.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070042
43#include "hyperv.h"
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070044
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -070045#define STORVSC_RING_BUFFER_SIZE (20*PAGE_SIZE)
46static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
47
48module_param(storvsc_ringbuffer_size, int, S_IRUGO);
49MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
50
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070051/* to alert the user that structure sizes may be mismatched even though the */
52/* protocol versions match. */
53
54
55#define REVISION_STRING(REVISION_) #REVISION_
56#define FILL_VMSTOR_REVISION(RESULT_LVALUE_) \
57 do { \
58 char *revision_string \
59 = REVISION_STRING($Rev : 6 $) + 6; \
60 RESULT_LVALUE_ = 0; \
61 while (*revision_string >= '0' \
62 && *revision_string <= '9') { \
63 RESULT_LVALUE_ *= 10; \
64 RESULT_LVALUE_ += *revision_string - '0'; \
65 revision_string++; \
66 } \
67 } while (0)
68
69/* Major/minor macros. Minor version is in LSB, meaning that earlier flat */
70/* version numbers will be interpreted as "0.x" (i.e., 1 becomes 0.1). */
71#define VMSTOR_PROTOCOL_MAJOR(VERSION_) (((VERSION_) >> 8) & 0xff)
72#define VMSTOR_PROTOCOL_MINOR(VERSION_) (((VERSION_)) & 0xff)
73#define VMSTOR_PROTOCOL_VERSION(MAJOR_, MINOR_) ((((MAJOR_) & 0xff) << 8) | \
74 (((MINOR_) & 0xff)))
75#define VMSTOR_INVALID_PROTOCOL_VERSION (-1)
76
77/* Version history: */
78/* V1 Beta 0.1 */
79/* V1 RC < 2008/1/31 1.0 */
80/* V1 RC > 2008/1/31 2.0 */
81#define VMSTOR_PROTOCOL_VERSION_CURRENT VMSTOR_PROTOCOL_VERSION(2, 0)
82
83
84
85
86/* This will get replaced with the max transfer length that is possible on */
87/* the host adapter. */
88/* The max transfer length will be published when we offer a vmbus channel. */
89#define MAX_TRANSFER_LENGTH 0x40000
90#define DEFAULT_PACKET_SIZE (sizeof(struct vmdata_gpa_direct) + \
91 sizeof(struct vstor_packet) + \
92 sizesizeof(u64) * (MAX_TRANSFER_LENGTH / PAGE_SIZE)))
93
94
95/* Packet structure describing virtual storage requests. */
96enum vstor_packet_operation {
97 VSTOR_OPERATION_COMPLETE_IO = 1,
98 VSTOR_OPERATION_REMOVE_DEVICE = 2,
99 VSTOR_OPERATION_EXECUTE_SRB = 3,
100 VSTOR_OPERATION_RESET_LUN = 4,
101 VSTOR_OPERATION_RESET_ADAPTER = 5,
102 VSTOR_OPERATION_RESET_BUS = 6,
103 VSTOR_OPERATION_BEGIN_INITIALIZATION = 7,
104 VSTOR_OPERATION_END_INITIALIZATION = 8,
105 VSTOR_OPERATION_QUERY_PROTOCOL_VERSION = 9,
106 VSTOR_OPERATION_QUERY_PROPERTIES = 10,
107 VSTOR_OPERATION_MAXIMUM = 10
108};
109
110/*
111 * Platform neutral description of a scsi request -
112 * this remains the same across the write regardless of 32/64 bit
113 * note: it's patterned off the SCSI_PASS_THROUGH structure
114 */
115#define CDB16GENERIC_LENGTH 0x10
116
117#ifndef SENSE_BUFFER_SIZE
118#define SENSE_BUFFER_SIZE 0x12
119#endif
120
121#define MAX_DATA_BUF_LEN_WITH_PADDING 0x14
122
123struct vmscsi_request {
124 unsigned short length;
125 unsigned char srb_status;
126 unsigned char scsi_status;
127
128 unsigned char port_number;
129 unsigned char path_id;
130 unsigned char target_id;
131 unsigned char lun;
132
133 unsigned char cdb_length;
134 unsigned char sense_info_length;
135 unsigned char data_in;
136 unsigned char reserved;
137
138 unsigned int data_transfer_length;
139
140 union {
141 unsigned char cdb[CDB16GENERIC_LENGTH];
142 unsigned char sense_data[SENSE_BUFFER_SIZE];
143 unsigned char reserved_array[MAX_DATA_BUF_LEN_WITH_PADDING];
144 };
145} __attribute((packed));
146
147
148/*
149 * This structure is sent during the intialization phase to get the different
150 * properties of the channel.
151 */
152struct vmstorage_channel_properties {
153 unsigned short protocol_version;
154 unsigned char path_id;
155 unsigned char target_id;
156
157 /* Note: port number is only really known on the client side */
158 unsigned int port_number;
159 unsigned int flags;
160 unsigned int max_transfer_bytes;
161
162 /* This id is unique for each channel and will correspond with */
163 /* vendor specific data in the inquirydata */
164 unsigned long long unique_id;
165} __packed;
166
167/* This structure is sent during the storage protocol negotiations. */
168struct vmstorage_protocol_version {
169 /* Major (MSW) and minor (LSW) version numbers. */
170 unsigned short major_minor;
171
172 /*
173 * Revision number is auto-incremented whenever this file is changed
174 * (See FILL_VMSTOR_REVISION macro above). Mismatch does not
175 * definitely indicate incompatibility--but it does indicate mismatched
176 * builds.
177 */
178 unsigned short revision;
179} __packed;
180
181/* Channel Property Flags */
182#define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
183#define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
184
185struct vstor_packet {
186 /* Requested operation type */
187 enum vstor_packet_operation operation;
188
189 /* Flags - see below for values */
190 unsigned int flags;
191
192 /* Status of the request returned from the server side. */
193 unsigned int status;
194
195 /* Data payload area */
196 union {
197 /*
198 * Structure used to forward SCSI commands from the
199 * client to the server.
200 */
201 struct vmscsi_request vm_srb;
202
203 /* Structure used to query channel properties. */
204 struct vmstorage_channel_properties storage_channel_properties;
205
206 /* Used during version negotiations. */
207 struct vmstorage_protocol_version version;
208 };
209} __packed;
210
211/* Packet flags */
212/*
213 * This flag indicates that the server should send back a completion for this
214 * packet.
215 */
216#define REQUEST_COMPLETION_FLAG 0x1
217
218/* This is the set of flags that the vsc can set in any packets it sends */
219#define VSC_LEGAL_FLAGS (REQUEST_COMPLETION_FLAG)
220
221
222/* Defines */
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700223
224#define STORVSC_MAX_IO_REQUESTS 128
225
226/*
227 * In Hyper-V, each port/path/target maps to 1 scsi host adapter. In
228 * reality, the path/target is not used (ie always set to 0) so our
229 * scsi host adapter essentially has 1 bus with 1 target that contains
230 * up to 256 luns.
231 */
232#define STORVSC_MAX_LUNS_PER_TARGET 64
233#define STORVSC_MAX_TARGETS 1
234#define STORVSC_MAX_CHANNELS 1
235
236struct hv_storvsc_request;
237
238/* Matches Windows-end */
239enum storvsc_request_type {
240 WRITE_TYPE,
241 READ_TYPE,
242 UNKNOWN_TYPE,
243};
244
245
246struct hv_storvsc_request {
247 struct hv_storvsc_request *request;
248 struct hv_device *device;
249
250 /* Synchronize the request/response if needed */
251 struct completion wait_event;
252
253 unsigned char *sense_buffer;
254 void *context;
255 void (*on_io_completion)(struct hv_storvsc_request *request);
256 struct hv_multipage_buffer data_buffer;
257
258 struct vstor_packet vstor_packet;
259};
260
261
262struct storvsc_device_info {
263 u32 ring_buffer_size;
264 unsigned int port_number;
265 unsigned char path_id;
266 unsigned char target_id;
267};
268
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700269
270/* A storvsc device is a device object that contains a vmbus channel */
271struct storvsc_device {
272 struct hv_device *device;
273
274 bool destroy;
275 bool drain_notify;
276 atomic_t num_outstanding_req;
277
278 wait_queue_head_t waiting_to_drain;
279
280 /*
281 * Each unique Port/Path/Target represents 1 channel ie scsi
282 * controller. In reality, the pathid, targetid is always 0
283 * and the port is set by us
284 */
285 unsigned int port_number;
286 unsigned char path_id;
287 unsigned char target_id;
288
289 /* Used for vsc/vsp channel reset process */
290 struct hv_storvsc_request init_request;
291 struct hv_storvsc_request reset_request;
292};
293
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700294struct hv_host_device {
295 struct hv_device *dev;
296 struct kmem_cache *request_pool;
297 unsigned int port;
298 unsigned char path;
299 unsigned char target;
300};
301
302struct storvsc_cmd_request {
303 struct list_head entry;
304 struct scsi_cmnd *cmd;
305
306 unsigned int bounce_sgl_count;
307 struct scatterlist *bounce_sgl;
308
309 struct hv_storvsc_request request;
310};
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700311
312static inline struct storvsc_device *get_out_stor_device(
313 struct hv_device *device)
314{
315 struct storvsc_device *stor_device;
316
317 stor_device = (struct storvsc_device *)device->ext;
318
319 if (stor_device && stor_device->destroy)
320 stor_device = NULL;
321
322 return stor_device;
323}
324
325
326static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
327{
328 dev->drain_notify = true;
329 wait_event(dev->waiting_to_drain,
330 atomic_read(&dev->num_outstanding_req) == 0);
331 dev->drain_notify = false;
332}
Hank Janssenbef4a342009-07-13 16:01:31 -0700333
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700334static inline struct storvsc_device *alloc_stor_device(struct hv_device *device)
335{
336 struct storvsc_device *stor_device;
337
338 stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
339 if (!stor_device)
340 return NULL;
341
342 stor_device->destroy = false;
343 init_waitqueue_head(&stor_device->waiting_to_drain);
344 stor_device->device = device;
345 device->ext = stor_device;
346
347 return stor_device;
348}
349
350
351static inline struct storvsc_device *get_in_stor_device(
352 struct hv_device *device)
353{
354 struct storvsc_device *stor_device;
355 unsigned long flags;
356
357 spin_lock_irqsave(&device->channel->inbound_lock, flags);
358 stor_device = (struct storvsc_device *)device->ext;
359
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:
373 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
374 return stor_device;
375
376}
377
378static int storvsc_channel_init(struct hv_device *device)
379{
380 struct storvsc_device *stor_device;
381 struct hv_storvsc_request *request;
382 struct vstor_packet *vstor_packet;
383 int ret, t;
384
385 stor_device = get_out_stor_device(device);
386 if (!stor_device)
387 return -ENODEV;
388
389 request = &stor_device->init_request;
390 vstor_packet = &request->vstor_packet;
391
392 /*
393 * Now, initiate the vsc/vsp initialization protocol on the open
394 * channel
395 */
396 memset(request, 0, sizeof(struct hv_storvsc_request));
397 init_completion(&request->wait_event);
398 vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
399 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
400
401 ret = vmbus_sendpacket(device->channel, vstor_packet,
402 sizeof(struct vstor_packet),
403 (unsigned long)request,
404 VM_PKT_DATA_INBAND,
405 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
406 if (ret != 0)
407 goto cleanup;
408
409 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
410 if (t == 0) {
411 ret = -ETIMEDOUT;
412 goto cleanup;
413 }
414
415 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
416 vstor_packet->status != 0)
417 goto cleanup;
418
419
420 /* reuse the packet for version range supported */
421 memset(vstor_packet, 0, sizeof(struct vstor_packet));
422 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
423 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
424
425 vstor_packet->version.major_minor = VMSTOR_PROTOCOL_VERSION_CURRENT;
426 FILL_VMSTOR_REVISION(vstor_packet->version.revision);
427
428 ret = vmbus_sendpacket(device->channel, vstor_packet,
429 sizeof(struct vstor_packet),
430 (unsigned long)request,
431 VM_PKT_DATA_INBAND,
432 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
433 if (ret != 0)
434 goto cleanup;
435
436 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
437 if (t == 0) {
438 ret = -ETIMEDOUT;
439 goto cleanup;
440 }
441
442 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
443 vstor_packet->status != 0)
444 goto cleanup;
445
446
447 memset(vstor_packet, 0, sizeof(struct vstor_packet));
448 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
449 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
450 vstor_packet->storage_channel_properties.port_number =
451 stor_device->port_number;
452
453 ret = vmbus_sendpacket(device->channel, vstor_packet,
454 sizeof(struct vstor_packet),
455 (unsigned long)request,
456 VM_PKT_DATA_INBAND,
457 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
458
459 if (ret != 0)
460 goto cleanup;
461
462 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
463 if (t == 0) {
464 ret = -ETIMEDOUT;
465 goto cleanup;
466 }
467
468 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
469 vstor_packet->status != 0)
470 goto cleanup;
471
472 stor_device->path_id = vstor_packet->storage_channel_properties.path_id;
473 stor_device->target_id
474 = vstor_packet->storage_channel_properties.target_id;
475
476 memset(vstor_packet, 0, sizeof(struct vstor_packet));
477 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
478 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
479
480 ret = vmbus_sendpacket(device->channel, vstor_packet,
481 sizeof(struct vstor_packet),
482 (unsigned long)request,
483 VM_PKT_DATA_INBAND,
484 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
485
486 if (ret != 0)
487 goto cleanup;
488
489 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
490 if (t == 0) {
491 ret = -ETIMEDOUT;
492 goto cleanup;
493 }
494
495 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
496 vstor_packet->status != 0)
497 goto cleanup;
498
499
500cleanup:
501 return ret;
502}
503
504static void storvsc_on_io_completion(struct hv_device *device,
505 struct vstor_packet *vstor_packet,
506 struct hv_storvsc_request *request)
507{
508 struct storvsc_device *stor_device;
509 struct vstor_packet *stor_pkt;
510
511 stor_device = (struct storvsc_device *)device->ext;
512
513 stor_pkt = &request->vstor_packet;
514
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -0700515 /*
516 * The current SCSI handling on the host side does
517 * not correctly handle:
518 * INQUIRY command with page code parameter set to 0x80
519 * MODE_SENSE command with cmd[2] == 0x1c
520 *
521 * Setup srb and scsi status so this won't be fatal.
522 * We do this so we can distinguish truly fatal failues
523 * (srb status == 0x4) and off-line the device in that case.
524 */
525
526 if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
527 (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
528 vstor_packet->vm_srb.scsi_status = 0;
529 vstor_packet->vm_srb.srb_status = 0x1;
530 }
531
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700532
533 /* Copy over the status...etc */
534 stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
535 stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
536 stor_pkt->vm_srb.sense_info_length =
537 vstor_packet->vm_srb.sense_info_length;
538
539 if (vstor_packet->vm_srb.scsi_status != 0 ||
540 vstor_packet->vm_srb.srb_status != 1){
541 DPRINT_WARN(STORVSC,
542 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
543 stor_pkt->vm_srb.cdb[0],
544 vstor_packet->vm_srb.scsi_status,
545 vstor_packet->vm_srb.srb_status);
546 }
547
548 if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
549 /* CHECK_CONDITION */
550 if (vstor_packet->vm_srb.srb_status & 0x80) {
551 /* autosense data available */
552 DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data "
553 "valid - len %d\n", request,
554 vstor_packet->vm_srb.sense_info_length);
555
556 memcpy(request->sense_buffer,
557 vstor_packet->vm_srb.sense_data,
558 vstor_packet->vm_srb.sense_info_length);
559
560 }
561 }
562
563 stor_pkt->vm_srb.data_transfer_length =
564 vstor_packet->vm_srb.data_transfer_length;
565
566 request->on_io_completion(request);
567
568 if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
569 stor_device->drain_notify)
570 wake_up(&stor_device->waiting_to_drain);
571
572
573}
574
575static void storvsc_on_receive(struct hv_device *device,
576 struct vstor_packet *vstor_packet,
577 struct hv_storvsc_request *request)
578{
579 switch (vstor_packet->operation) {
580 case VSTOR_OPERATION_COMPLETE_IO:
581 storvsc_on_io_completion(device, vstor_packet, request);
582 break;
583 case VSTOR_OPERATION_REMOVE_DEVICE:
584
585 default:
586 break;
587 }
588}
589
590static void storvsc_on_channel_callback(void *context)
591{
592 struct hv_device *device = (struct hv_device *)context;
593 struct storvsc_device *stor_device;
594 u32 bytes_recvd;
595 u64 request_id;
596 unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
597 struct hv_storvsc_request *request;
598 int ret;
599
600
601 stor_device = get_in_stor_device(device);
602 if (!stor_device)
603 return;
604
605 do {
606 ret = vmbus_recvpacket(device->channel, packet,
607 ALIGN(sizeof(struct vstor_packet), 8),
608 &bytes_recvd, &request_id);
609 if (ret == 0 && bytes_recvd > 0) {
610
611 request = (struct hv_storvsc_request *)
612 (unsigned long)request_id;
613
614 if ((request == &stor_device->init_request) ||
615 (request == &stor_device->reset_request)) {
616
617 memcpy(&request->vstor_packet, packet,
618 sizeof(struct vstor_packet));
619 complete(&request->wait_event);
620 } else {
621 storvsc_on_receive(device,
622 (struct vstor_packet *)packet,
623 request);
624 }
625 } else {
626 break;
627 }
628 } while (1);
629
630 return;
631}
632
633static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
634{
635 struct vmstorage_channel_properties props;
636 int ret;
637
638 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
639
640 /* Open the channel */
641 ret = vmbus_open(device->channel,
642 ring_size,
643 ring_size,
644 (void *)&props,
645 sizeof(struct vmstorage_channel_properties),
646 storvsc_on_channel_callback, device);
647
648 if (ret != 0)
649 return ret;
650
651 ret = storvsc_channel_init(device);
652
653 return ret;
654}
655
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700656static int storvsc_dev_add(struct hv_device *device,
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700657 void *additional_info)
658{
659 struct storvsc_device *stor_device;
660 struct storvsc_device_info *device_info;
661 int ret = 0;
662
663 device_info = (struct storvsc_device_info *)additional_info;
664 stor_device = alloc_stor_device(device);
665 if (!stor_device)
666 return -ENOMEM;
667
668 /* Save the channel properties to our storvsc channel */
669
670 /*
671 * If we support more than 1 scsi channel, we need to set the
672 * port number here to the scsi channel but how do we get the
673 * scsi channel prior to the bus scan.
674 *
675 * The host does not support this.
676 */
677
678 stor_device->port_number = device_info->port_number;
679 /* Send it back up */
680 ret = storvsc_connect_to_vsp(device, device_info->ring_buffer_size);
681 if (ret) {
682 kfree(stor_device);
683 return ret;
684 }
685 device_info->path_id = stor_device->path_id;
686 device_info->target_id = stor_device->target_id;
687
688 return ret;
689}
690
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700691static int storvsc_dev_remove(struct hv_device *device)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700692{
693 struct storvsc_device *stor_device;
694 unsigned long flags;
695
696 stor_device = (struct storvsc_device *)device->ext;
697
698 spin_lock_irqsave(&device->channel->inbound_lock, flags);
699 stor_device->destroy = true;
700 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
701
702 /*
703 * At this point, all outbound traffic should be disable. We
704 * only allow inbound traffic (responses) to proceed so that
705 * outstanding requests can be completed.
706 */
707
708 storvsc_wait_to_drain(stor_device);
709
710 /*
711 * Since we have already drained, we don't need to busy wait
712 * as was done in final_release_stor_device()
713 * Note that we cannot set the ext pointer to NULL until
714 * we have drained - to drain the outgoing packets, we need to
715 * allow incoming packets.
716 */
717 spin_lock_irqsave(&device->channel->inbound_lock, flags);
718 device->ext = NULL;
719 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
720
721 /* Close the channel */
722 vmbus_close(device->channel);
723
724 kfree(stor_device);
725 return 0;
726}
727
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700728static int storvsc_do_io(struct hv_device *device,
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700729 struct hv_storvsc_request *request)
730{
731 struct storvsc_device *stor_device;
732 struct vstor_packet *vstor_packet;
733 int ret = 0;
734
735 vstor_packet = &request->vstor_packet;
736 stor_device = get_out_stor_device(device);
737
738 if (!stor_device)
739 return -ENODEV;
740
741
742 request->device = device;
743
744
745 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
746
747 vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
748
749
750 vstor_packet->vm_srb.sense_info_length = SENSE_BUFFER_SIZE;
751
752
753 vstor_packet->vm_srb.data_transfer_length =
754 request->data_buffer.len;
755
756 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
757
758 if (request->data_buffer.len) {
759 ret = vmbus_sendpacket_multipagebuffer(device->channel,
760 &request->data_buffer,
761 vstor_packet,
762 sizeof(struct vstor_packet),
763 (unsigned long)request);
764 } else {
765 ret = vmbus_sendpacket(device->channel, vstor_packet,
766 sizeof(struct vstor_packet),
767 (unsigned long)request,
768 VM_PKT_DATA_INBAND,
769 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
770 }
771
772 if (ret != 0)
773 return ret;
774
775 atomic_inc(&stor_device->num_outstanding_req);
776
777 return ret;
778}
779
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -0700780static void storvsc_get_ide_info(struct hv_device *dev, int *target, int *path)
781{
782 *target =
783 dev->dev_instance.b[5] << 8 | dev->dev_instance.b[4];
784
785 *path =
786 dev->dev_instance.b[3] << 24 |
787 dev->dev_instance.b[2] << 16 |
788 dev->dev_instance.b[1] << 8 | dev->dev_instance.b[0];
789}
790
Hank Janssenbef4a342009-07-13 16:01:31 -0700791
K. Y. Srinivasan5b60ace2011-05-10 07:54:25 -0700792static int storvsc_device_alloc(struct scsi_device *sdevice)
793{
794 /*
795 * This enables luns to be located sparsely. Otherwise, we may not
796 * discovered them.
797 */
798 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
799 return 0;
800}
801
K. Y. Srinivasana1ebfea2011-05-10 07:54:26 -0700802static int storvsc_merge_bvec(struct request_queue *q,
803 struct bvec_merge_data *bmd, struct bio_vec *bvec)
804{
805 /* checking done by caller. */
806 return bvec->bv_len;
807}
808
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700809static int storvsc_device_configure(struct scsi_device *sdevice)
810{
811 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
812 STORVSC_MAX_IO_REQUESTS);
813
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700814 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
815
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700816 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
817
818 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700819
820 return 0;
821}
822
K. Y. Srinivasan49a3c7a2011-05-10 07:54:28 -0700823static void destroy_bounce_buffer(struct scatterlist *sgl,
824 unsigned int sg_count)
825{
826 int i;
827 struct page *page_buf;
828
829 for (i = 0; i < sg_count; i++) {
830 page_buf = sg_page((&sgl[i]));
831 if (page_buf != NULL)
832 __free_page(page_buf);
833 }
834
835 kfree(sgl);
836}
837
K. Y. Srinivasan3862ef32011-05-10 07:54:29 -0700838static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
839{
840 int i;
841
842 /* No need to check */
843 if (sg_count < 2)
844 return -1;
845
846 /* We have at least 2 sg entries */
847 for (i = 0; i < sg_count; i++) {
848 if (i == 0) {
849 /* make sure 1st one does not have hole */
850 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
851 return i;
852 } else if (i == sg_count - 1) {
853 /* make sure last one does not have hole */
854 if (sgl[i].offset != 0)
855 return i;
856 } else {
857 /* make sure no hole in the middle */
858 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
859 return i;
860 }
861 }
862 return -1;
863}
864
K. Y. Srinivasana9753cb2011-05-10 07:54:30 -0700865static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
866 unsigned int sg_count,
867 unsigned int len)
868{
869 int i;
870 int num_pages;
871 struct scatterlist *bounce_sgl;
872 struct page *page_buf;
873
874 num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
875
876 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
877 if (!bounce_sgl)
878 return NULL;
879
880 for (i = 0; i < num_pages; i++) {
881 page_buf = alloc_page(GFP_ATOMIC);
882 if (!page_buf)
883 goto cleanup;
884 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
885 }
886
887 return bounce_sgl;
888
889cleanup:
890 destroy_bounce_buffer(bounce_sgl, num_pages);
891 return NULL;
892}
893
K. Y. Srinivasan29fe2c92011-05-10 07:54:31 -0700894
895/* Assume the original sgl has enough room */
896static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
897 struct scatterlist *bounce_sgl,
898 unsigned int orig_sgl_count)
899{
900 int i;
901 int j = 0;
902 unsigned long src, dest;
903 unsigned int srclen, destlen, copylen;
904 unsigned int total_copied = 0;
905 unsigned long bounce_addr = 0;
906 unsigned long dest_addr = 0;
907 unsigned long flags;
908
909 local_irq_save(flags);
910
911 for (i = 0; i < orig_sgl_count; i++) {
912 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
913 KM_IRQ0) + orig_sgl[i].offset;
914 dest = dest_addr;
915 destlen = orig_sgl[i].length;
916
917 if (bounce_addr == 0)
918 bounce_addr =
919 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
920 KM_IRQ0);
921
922 while (destlen) {
923 src = bounce_addr + bounce_sgl[j].offset;
924 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
925
926 copylen = min(srclen, destlen);
927 memcpy((void *)dest, (void *)src, copylen);
928
929 total_copied += copylen;
930 bounce_sgl[j].offset += copylen;
931 destlen -= copylen;
932 dest += copylen;
933
934 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
935 /* full */
936 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
937 j++;
938
939 /* if we need to use another bounce buffer */
940 if (destlen || i != orig_sgl_count - 1)
941 bounce_addr =
942 (unsigned long)kmap_atomic(
943 sg_page((&bounce_sgl[j])), KM_IRQ0);
944 } else if (destlen == 0 && i == orig_sgl_count - 1) {
945 /* unmap the last bounce that is < PAGE_SIZE */
946 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
947 }
948 }
949
950 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
951 KM_IRQ0);
952 }
953
954 local_irq_restore(flags);
955
956 return total_copied;
957}
958
K. Y. Srinivasan6a8ff442011-05-10 07:54:32 -0700959
960/* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
961static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
962 struct scatterlist *bounce_sgl,
963 unsigned int orig_sgl_count)
964{
965 int i;
966 int j = 0;
967 unsigned long src, dest;
968 unsigned int srclen, destlen, copylen;
969 unsigned int total_copied = 0;
970 unsigned long bounce_addr = 0;
971 unsigned long src_addr = 0;
972 unsigned long flags;
973
974 local_irq_save(flags);
975
976 for (i = 0; i < orig_sgl_count; i++) {
977 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
978 KM_IRQ0) + orig_sgl[i].offset;
979 src = src_addr;
980 srclen = orig_sgl[i].length;
981
982 if (bounce_addr == 0)
983 bounce_addr =
984 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
985 KM_IRQ0);
986
987 while (srclen) {
988 /* assume bounce offset always == 0 */
989 dest = bounce_addr + bounce_sgl[j].length;
990 destlen = PAGE_SIZE - bounce_sgl[j].length;
991
992 copylen = min(srclen, destlen);
993 memcpy((void *)dest, (void *)src, copylen);
994
995 total_copied += copylen;
996 bounce_sgl[j].length += copylen;
997 srclen -= copylen;
998 src += copylen;
999
1000 if (bounce_sgl[j].length == PAGE_SIZE) {
1001 /* full..move to next entry */
1002 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
1003 j++;
1004
1005 /* if we need to use another bounce buffer */
1006 if (srclen || i != orig_sgl_count - 1)
1007 bounce_addr =
1008 (unsigned long)kmap_atomic(
1009 sg_page((&bounce_sgl[j])), KM_IRQ0);
1010
1011 } else if (srclen == 0 && i == orig_sgl_count - 1) {
1012 /* unmap the last bounce that is < PAGE_SIZE */
1013 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
1014 }
1015 }
1016
1017 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
1018 }
1019
1020 local_irq_restore(flags);
1021
1022 return total_copied;
1023}
1024
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -07001025
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -07001026static int storvsc_remove(struct hv_device *dev)
1027{
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -07001028 struct Scsi_Host *host = dev_get_drvdata(&dev->device);
1029 struct hv_host_device *host_dev =
1030 (struct hv_host_device *)host->hostdata;
1031
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -07001032 scsi_remove_host(host);
1033
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -07001034 scsi_host_put(host);
K. Y. Srinivasand36b0a02011-06-06 15:49:29 -07001035
K. Y. Srinivasan2935a402011-06-06 15:49:28 -07001036 storvsc_dev_remove(dev);
1037 if (host_dev->request_pool) {
1038 kmem_cache_destroy(host_dev->request_pool);
1039 host_dev->request_pool = NULL;
1040 }
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -07001041 return 0;
1042}
1043
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001044
1045static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
1046 sector_t capacity, int *info)
1047{
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001048 sector_t nsect = capacity;
1049 sector_t cylinders = nsect;
1050 int heads, sectors_pt;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001051
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001052 /*
1053 * We are making up these values; let us keep it simple.
1054 */
1055 heads = 0xff;
1056 sectors_pt = 0x3f; /* Sectors per track */
1057 sector_div(cylinders, heads * sectors_pt);
1058 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
1059 cylinders = 0xffff;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001060
1061 info[0] = heads;
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001062 info[1] = sectors_pt;
1063 info[2] = (int)cylinders;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001064
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001065 return 0;
1066}
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001067
1068static int storvsc_host_reset(struct hv_device *device)
1069{
1070 struct storvsc_device *stor_device;
1071 struct hv_storvsc_request *request;
1072 struct vstor_packet *vstor_packet;
1073 int ret, t;
1074
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001075
K. Y. Srinivasan1eaaddf2011-08-27 11:31:03 -07001076 stor_device = get_out_stor_device(device);
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001077 if (!stor_device)
K. Y. Srinivasanf3b74162011-08-25 09:49:08 -07001078 return -ENODEV;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001079
1080 request = &stor_device->reset_request;
1081 vstor_packet = &request->vstor_packet;
1082
1083 init_completion(&request->wait_event);
1084
1085 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
1086 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
1087 vstor_packet->vm_srb.path_id = stor_device->path_id;
1088
1089 ret = vmbus_sendpacket(device->channel, vstor_packet,
1090 sizeof(struct vstor_packet),
1091 (unsigned long)&stor_device->reset_request,
1092 VM_PKT_DATA_INBAND,
1093 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1094 if (ret != 0)
1095 goto cleanup;
1096
K. Y. Srinivasan46d2eb62011-06-16 13:16:36 -07001097 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001098 if (t == 0) {
1099 ret = -ETIMEDOUT;
1100 goto cleanup;
1101 }
1102
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001103
1104 /*
1105 * At this point, all outstanding requests in the adapter
1106 * should have been flushed out and return to us
1107 */
1108
1109cleanup:
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001110 return ret;
1111}
1112
1113
K. Y. Srinivasan96e690be2011-05-10 07:54:38 -07001114/*
1115 * storvsc_host_reset_handler - Reset the scsi HBA
1116 */
1117static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
1118{
1119 int ret;
1120 struct hv_host_device *host_dev =
1121 (struct hv_host_device *)scmnd->device->host->hostdata;
1122 struct hv_device *dev = host_dev->dev;
1123
K. Y. Srinivasan96e690be2011-05-10 07:54:38 -07001124 ret = storvsc_host_reset(dev);
1125 if (ret != 0)
1126 return ret;
1127
K. Y. Srinivasan96e690be2011-05-10 07:54:38 -07001128 return ret;
1129}
1130
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001131
1132/*
K. Y. Srinivasanb8de73d2011-08-27 11:31:27 -07001133 * storvsc_command_completion - Command completion processing
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001134 */
K. Y. Srinivasanb8de73d2011-08-27 11:31:27 -07001135static void storvsc_command_completion(struct hv_storvsc_request *request)
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001136{
1137 struct storvsc_cmd_request *cmd_request =
1138 (struct storvsc_cmd_request *)request->context;
1139 struct scsi_cmnd *scmnd = cmd_request->cmd;
1140 struct hv_host_device *host_dev =
1141 (struct hv_host_device *)scmnd->device->host->hostdata;
1142 void (*scsi_done_fn)(struct scsi_cmnd *);
1143 struct scsi_sense_hdr sense_hdr;
1144 struct vmscsi_request *vm_srb;
1145
K. Y. Srinivasan3612ef92011-08-27 11:31:20 -07001146 vm_srb = &request->vstor_packet.vm_srb;
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001147 if (cmd_request->bounce_sgl_count) {
K. Y. Srinivasan3612ef92011-08-27 11:31:20 -07001148 if (vm_srb->data_in == READ_TYPE) {
1149 copy_from_bounce_buffer(scsi_sglist(scmnd),
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001150 cmd_request->bounce_sgl,
1151 scsi_sg_count(scmnd));
K. Y. Srinivasan3612ef92011-08-27 11:31:20 -07001152 destroy_bounce_buffer(cmd_request->bounce_sgl,
1153 cmd_request->bounce_sgl_count);
1154 }
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001155 }
1156
K. Y. Srinivasan2544b792011-08-27 11:31:28 -07001157 /*
1158 * If there is an error; offline the device since all
1159 * error recovery strategies would have already been
1160 * deployed on the host side.
1161 */
1162 if (vm_srb->srb_status == 0x4)
1163 scmnd->result = DID_TARGET_FAILURE << 16;
1164 else
1165 scmnd->result = vm_srb->scsi_status;
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001166
1167 if (scmnd->result) {
1168 if (scsi_normalize_sense(scmnd->sense_buffer,
1169 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
1170 scsi_print_sense_hdr("storvsc", &sense_hdr);
1171 }
1172
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001173 scsi_set_resid(scmnd,
1174 request->data_buffer.len -
1175 vm_srb->data_transfer_length);
1176
1177 scsi_done_fn = scmnd->scsi_done;
1178
1179 scmnd->host_scribble = NULL;
1180 scmnd->scsi_done = NULL;
1181
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001182 scsi_done_fn(scmnd);
1183
1184 kmem_cache_free(host_dev->request_pool, cmd_request);
1185}
1186
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001187
1188/*
1189 * storvsc_queuecommand - Initiate command processing
1190 */
1191static int storvsc_queuecommand_lck(struct scsi_cmnd *scmnd,
1192 void (*done)(struct scsi_cmnd *))
1193{
1194 int ret;
1195 struct hv_host_device *host_dev =
1196 (struct hv_host_device *)scmnd->device->host->hostdata;
1197 struct hv_device *dev = host_dev->dev;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001198 struct hv_storvsc_request *request;
1199 struct storvsc_cmd_request *cmd_request;
1200 unsigned int request_size = 0;
1201 int i;
1202 struct scatterlist *sgl;
1203 unsigned int sg_count = 0;
1204 struct vmscsi_request *vm_srb;
1205
1206
1207 /* If retrying, no need to prep the cmd */
1208 if (scmnd->host_scribble) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001209
1210 cmd_request =
1211 (struct storvsc_cmd_request *)scmnd->host_scribble;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001212
1213 goto retry_request;
1214 }
1215
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001216 scmnd->scsi_done = done;
1217
1218 request_size = sizeof(struct storvsc_cmd_request);
1219
1220 cmd_request = kmem_cache_zalloc(host_dev->request_pool,
1221 GFP_ATOMIC);
1222 if (!cmd_request) {
1223 scmnd->scsi_done = NULL;
1224 return SCSI_MLQUEUE_DEVICE_BUSY;
1225 }
1226
1227 /* Setup the cmd request */
1228 cmd_request->bounce_sgl_count = 0;
1229 cmd_request->bounce_sgl = NULL;
1230 cmd_request->cmd = scmnd;
1231
1232 scmnd->host_scribble = (unsigned char *)cmd_request;
1233
1234 request = &cmd_request->request;
1235 vm_srb = &request->vstor_packet.vm_srb;
1236
1237
1238 /* Build the SRB */
1239 switch (scmnd->sc_data_direction) {
1240 case DMA_TO_DEVICE:
1241 vm_srb->data_in = WRITE_TYPE;
1242 break;
1243 case DMA_FROM_DEVICE:
1244 vm_srb->data_in = READ_TYPE;
1245 break;
1246 default:
1247 vm_srb->data_in = UNKNOWN_TYPE;
1248 break;
1249 }
1250
K. Y. Srinivasanb8de73d2011-08-27 11:31:27 -07001251 request->on_io_completion = storvsc_command_completion;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001252 request->context = cmd_request;/* scmnd; */
1253
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001254 vm_srb->port_number = host_dev->port;
1255 vm_srb->path_id = scmnd->device->channel;
1256 vm_srb->target_id = scmnd->device->id;
1257 vm_srb->lun = scmnd->device->lun;
1258
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001259 vm_srb->cdb_length = scmnd->cmd_len;
1260
1261 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
1262
1263 request->sense_buffer = scmnd->sense_buffer;
1264
1265
1266 request->data_buffer.len = scsi_bufflen(scmnd);
1267 if (scsi_sg_count(scmnd)) {
1268 sgl = (struct scatterlist *)scsi_sglist(scmnd);
1269 sg_count = scsi_sg_count(scmnd);
1270
1271 /* check if we need to bounce the sgl */
1272 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
1273 cmd_request->bounce_sgl =
1274 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
1275 scsi_bufflen(scmnd));
1276 if (!cmd_request->bounce_sgl) {
1277 scmnd->scsi_done = NULL;
1278 scmnd->host_scribble = NULL;
1279 kmem_cache_free(host_dev->request_pool,
1280 cmd_request);
1281
1282 return SCSI_MLQUEUE_HOST_BUSY;
1283 }
1284
1285 cmd_request->bounce_sgl_count =
1286 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
1287 PAGE_SHIFT;
1288
K. Y. Srinivasanfa23b8c2011-08-27 11:31:21 -07001289 if (vm_srb->data_in == WRITE_TYPE)
1290 copy_to_bounce_buffer(sgl,
1291 cmd_request->bounce_sgl,
1292 scsi_sg_count(scmnd));
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001293
1294 sgl = cmd_request->bounce_sgl;
1295 sg_count = cmd_request->bounce_sgl_count;
1296 }
1297
1298 request->data_buffer.offset = sgl[0].offset;
1299
1300 for (i = 0; i < sg_count; i++)
1301 request->data_buffer.pfn_array[i] =
1302 page_to_pfn(sg_page((&sgl[i])));
1303
1304 } else if (scsi_sglist(scmnd)) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001305 request->data_buffer.offset =
1306 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
1307 request->data_buffer.pfn_array[0] =
1308 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
1309 }
1310
1311retry_request:
1312 /* Invokes the vsc to start an IO */
K. Y. Srinivasan636f0fd2011-05-10 07:54:50 -07001313 ret = storvsc_do_io(dev, &cmd_request->request);
1314
K. Y. Srinivasand2598f02011-08-25 09:48:58 -07001315 if (ret == -EAGAIN) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001316 /* no more space */
1317
K. Y. Srinivasan70691ec2011-08-27 11:31:29 -07001318 if (cmd_request->bounce_sgl_count)
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001319 destroy_bounce_buffer(cmd_request->bounce_sgl,
K. Y. Srinivasan70691ec2011-08-27 11:31:29 -07001320 cmd_request->bounce_sgl_count);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001321
1322 kmem_cache_free(host_dev->request_pool, cmd_request);
1323
1324 scmnd->scsi_done = NULL;
1325 scmnd->host_scribble = NULL;
1326
1327 ret = SCSI_MLQUEUE_DEVICE_BUSY;
1328 }
1329
1330 return ret;
1331}
1332
1333static DEF_SCSI_QCMD(storvsc_queuecommand)
1334
Hank Janssenbef4a342009-07-13 16:01:31 -07001335
Bill Pemberton454f18a2009-07-27 16:47:24 -04001336/* Scsi driver */
Hank Janssenbef4a342009-07-13 16:01:31 -07001337static struct scsi_host_template scsi_driver = {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001338 .module = THIS_MODULE,
1339 .name = "storvsc_host_t",
1340 .bios_param = storvsc_get_chs,
1341 .queuecommand = storvsc_queuecommand,
1342 .eh_host_reset_handler = storvsc_host_reset_handler,
1343 .slave_alloc = storvsc_device_alloc,
1344 .slave_configure = storvsc_device_configure,
1345 .cmd_per_lun = 1,
1346 /* 64 max_queue * 1 target */
Lars Lindley0686e4f2010-03-11 23:51:23 +01001347 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001348 .this_id = -1,
Bill Pemberton454f18a2009-07-27 16:47:24 -04001349 /* no use setting to 0 since ll_blk_rw reset it to 1 */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001350 /* currently 32 */
1351 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
1352 /*
1353 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
1354 * into 1 sg element. If set, we must limit the max_segment_size to
1355 * PAGE_SIZE, otherwise we may get 1 sg element that represents
1356 * multiple
1357 */
Bill Pemberton454f18a2009-07-27 16:47:24 -04001358 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001359 .use_clustering = ENABLE_CLUSTERING,
Bill Pemberton454f18a2009-07-27 16:47:24 -04001360 /* Make sure we dont get a sg segment crosses a page boundary */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001361 .dma_boundary = PAGE_SIZE-1,
Hank Janssenbef4a342009-07-13 16:01:31 -07001362};
1363
K. Y. Srinivasan21e37742011-08-27 11:31:18 -07001364/*
1365 * The storvsc_probe function assumes that the IDE guid
1366 * is the second entry.
1367 */
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001368static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001369 /* SCSI guid */
1370 { VMBUS_DEVICE(0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
1371 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f) },
K. Y. Srinivasan21e37742011-08-27 11:31:18 -07001372 /* IDE guid */
1373 { VMBUS_DEVICE(0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
1374 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5) },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001375 { },
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001376};
Hank Janssenbef4a342009-07-13 16:01:31 -07001377
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001378MODULE_DEVICE_TABLE(vmbus, id_table);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001379
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001380
Hank Janssen3e189512010-03-04 22:11:00 +00001381/*
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001382 * storvsc_probe - Add a new device for this driver
1383 */
1384
1385static int storvsc_probe(struct hv_device *device)
1386{
1387 int ret;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001388 struct Scsi_Host *host;
1389 struct hv_host_device *host_dev;
1390 struct storvsc_device_info device_info;
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001391 bool dev_is_ide;
1392 int path = 0;
1393 int target = 0;
1394
K. Y. Srinivasan21e37742011-08-27 11:31:18 -07001395 if (!memcmp(&device->dev_type.b, id_table[1].guid, sizeof(uuid_le)))
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001396 dev_is_ide = true;
1397 else
1398 dev_is_ide = false;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001399
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001400 host = scsi_host_alloc(&scsi_driver,
1401 sizeof(struct hv_host_device));
1402 if (!host)
1403 return -ENOMEM;
1404
1405 dev_set_drvdata(&device->device, host);
1406
1407 host_dev = (struct hv_host_device *)host->hostdata;
1408 memset(host_dev, 0, sizeof(struct hv_host_device));
1409
1410 host_dev->port = host->host_no;
1411 host_dev->dev = device;
1412
1413 host_dev->request_pool =
1414 kmem_cache_create(dev_name(&device->device),
1415 sizeof(struct storvsc_cmd_request), 0,
1416 SLAB_HWCACHE_ALIGN, NULL);
1417
1418 if (!host_dev->request_pool) {
1419 scsi_host_put(host);
1420 return -ENOMEM;
1421 }
1422
1423 device_info.port_number = host->host_no;
K. Y. Srinivasanfa4d1232011-05-10 07:56:01 -07001424 device_info.ring_buffer_size = storvsc_ringbuffer_size;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001425 /* Call to the vsc driver to add the device */
K. Y. Srinivasan58f1f5c2011-05-10 07:54:49 -07001426 ret = storvsc_dev_add(device, (void *)&device_info);
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001427
1428 if (ret != 0) {
1429 kmem_cache_destroy(host_dev->request_pool);
1430 scsi_host_put(host);
K. Y. Srinivasan8e854682011-08-25 09:49:09 -07001431 return -ENODEV;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001432 }
1433
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001434 if (dev_is_ide)
1435 storvsc_get_ide_info(device, &target, &path);
1436
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001437 host_dev->path = device_info.path_id;
1438 host_dev->target = device_info.target_id;
1439
1440 /* max # of devices per target */
1441 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
1442 /* max # of targets per channel */
1443 host->max_id = STORVSC_MAX_TARGETS;
1444 /* max # of channels */
1445 host->max_channel = STORVSC_MAX_CHANNELS - 1;
1446
1447 /* Register the HBA and start the scsi bus scan */
1448 ret = scsi_add_host(host, &device->device);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001449 if (ret != 0)
1450 goto err_out;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001451
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001452 if (!dev_is_ide) {
1453 scsi_scan_host(host);
1454 return 0;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001455 }
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001456 ret = scsi_add_device(host, 0, target, 0);
1457 if (ret) {
1458 scsi_remove_host(host);
1459 goto err_out;
1460 }
1461 return 0;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001462
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001463err_out:
1464 storvsc_dev_remove(device);
1465 kmem_cache_destroy(host_dev->request_pool);
1466 scsi_host_put(host);
1467 return -ENODEV;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001468}
1469
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -07001470/* The one and only one */
1471
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -07001472static struct hv_driver storvsc_drv = {
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001473 .name = "storvsc",
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001474 .id_table = id_table,
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -07001475 .probe = storvsc_probe,
1476 .remove = storvsc_remove,
K. Y. Srinivasan39ae6fa2011-05-10 07:54:46 -07001477};
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -07001478
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -07001479static int __init storvsc_drv_init(void)
Hank Janssenbef4a342009-07-13 16:01:31 -07001480{
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001481 u32 max_outstanding_req_per_channel;
1482
1483 /*
1484 * Divide the ring buffer data size (which is 1 page less
1485 * than the ring buffer size since that page is reserved for
1486 * the ring buffer indices) by the max request size (which is
1487 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
1488 */
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001489 max_outstanding_req_per_channel =
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001490 ((storvsc_ringbuffer_size - PAGE_SIZE) /
1491 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
1492 sizeof(struct vstor_packet) + sizeof(u64),
1493 sizeof(u64)));
Hank Janssenbef4a342009-07-13 16:01:31 -07001494
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001495 if (max_outstanding_req_per_channel <
K. Y. Srinivasanf8feed02011-05-10 07:54:24 -07001496 STORVSC_MAX_IO_REQUESTS)
K. Y. Srinivasanb06efc12011-08-25 09:49:10 -07001497 return -EINVAL;
Hank Janssenbef4a342009-07-13 16:01:31 -07001498
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001499 return vmbus_driver_register(&storvsc_drv);
Hank Janssenbef4a342009-07-13 16:01:31 -07001500}
1501
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -07001502static void __exit storvsc_drv_exit(void)
Hank Janssenbef4a342009-07-13 16:01:31 -07001503{
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001504 vmbus_driver_unregister(&storvsc_drv);
Hank Janssenbef4a342009-07-13 16:01:31 -07001505}
1506
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001507MODULE_LICENSE("GPL");
Hank Janssen26c14cc2010-02-11 23:02:42 +00001508MODULE_VERSION(HV_DRV_VERSION);
Stephen Hemminger3afc7cc32010-05-06 21:44:45 -07001509MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -07001510module_init(storvsc_drv_init);
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -07001511module_exit(storvsc_drv_exit);