blob: 0bb4e0e489836c47f8941f12afbc15ecf1d86341 [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
515
516 /* Copy over the status...etc */
517 stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
518 stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
519 stor_pkt->vm_srb.sense_info_length =
520 vstor_packet->vm_srb.sense_info_length;
521
522 if (vstor_packet->vm_srb.scsi_status != 0 ||
523 vstor_packet->vm_srb.srb_status != 1){
524 DPRINT_WARN(STORVSC,
525 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
526 stor_pkt->vm_srb.cdb[0],
527 vstor_packet->vm_srb.scsi_status,
528 vstor_packet->vm_srb.srb_status);
529 }
530
531 if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
532 /* CHECK_CONDITION */
533 if (vstor_packet->vm_srb.srb_status & 0x80) {
534 /* autosense data available */
535 DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data "
536 "valid - len %d\n", request,
537 vstor_packet->vm_srb.sense_info_length);
538
539 memcpy(request->sense_buffer,
540 vstor_packet->vm_srb.sense_data,
541 vstor_packet->vm_srb.sense_info_length);
542
543 }
544 }
545
546 stor_pkt->vm_srb.data_transfer_length =
547 vstor_packet->vm_srb.data_transfer_length;
548
549 request->on_io_completion(request);
550
551 if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
552 stor_device->drain_notify)
553 wake_up(&stor_device->waiting_to_drain);
554
555
556}
557
558static void storvsc_on_receive(struct hv_device *device,
559 struct vstor_packet *vstor_packet,
560 struct hv_storvsc_request *request)
561{
562 switch (vstor_packet->operation) {
563 case VSTOR_OPERATION_COMPLETE_IO:
564 storvsc_on_io_completion(device, vstor_packet, request);
565 break;
566 case VSTOR_OPERATION_REMOVE_DEVICE:
567
568 default:
569 break;
570 }
571}
572
573static void storvsc_on_channel_callback(void *context)
574{
575 struct hv_device *device = (struct hv_device *)context;
576 struct storvsc_device *stor_device;
577 u32 bytes_recvd;
578 u64 request_id;
579 unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
580 struct hv_storvsc_request *request;
581 int ret;
582
583
584 stor_device = get_in_stor_device(device);
585 if (!stor_device)
586 return;
587
588 do {
589 ret = vmbus_recvpacket(device->channel, packet,
590 ALIGN(sizeof(struct vstor_packet), 8),
591 &bytes_recvd, &request_id);
592 if (ret == 0 && bytes_recvd > 0) {
593
594 request = (struct hv_storvsc_request *)
595 (unsigned long)request_id;
596
597 if ((request == &stor_device->init_request) ||
598 (request == &stor_device->reset_request)) {
599
600 memcpy(&request->vstor_packet, packet,
601 sizeof(struct vstor_packet));
602 complete(&request->wait_event);
603 } else {
604 storvsc_on_receive(device,
605 (struct vstor_packet *)packet,
606 request);
607 }
608 } else {
609 break;
610 }
611 } while (1);
612
613 return;
614}
615
616static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
617{
618 struct vmstorage_channel_properties props;
619 int ret;
620
621 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
622
623 /* Open the channel */
624 ret = vmbus_open(device->channel,
625 ring_size,
626 ring_size,
627 (void *)&props,
628 sizeof(struct vmstorage_channel_properties),
629 storvsc_on_channel_callback, device);
630
631 if (ret != 0)
632 return ret;
633
634 ret = storvsc_channel_init(device);
635
636 return ret;
637}
638
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700639static int storvsc_dev_add(struct hv_device *device,
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700640 void *additional_info)
641{
642 struct storvsc_device *stor_device;
643 struct storvsc_device_info *device_info;
644 int ret = 0;
645
646 device_info = (struct storvsc_device_info *)additional_info;
647 stor_device = alloc_stor_device(device);
648 if (!stor_device)
649 return -ENOMEM;
650
651 /* Save the channel properties to our storvsc channel */
652
653 /*
654 * If we support more than 1 scsi channel, we need to set the
655 * port number here to the scsi channel but how do we get the
656 * scsi channel prior to the bus scan.
657 *
658 * The host does not support this.
659 */
660
661 stor_device->port_number = device_info->port_number;
662 /* Send it back up */
663 ret = storvsc_connect_to_vsp(device, device_info->ring_buffer_size);
664 if (ret) {
665 kfree(stor_device);
666 return ret;
667 }
668 device_info->path_id = stor_device->path_id;
669 device_info->target_id = stor_device->target_id;
670
671 return ret;
672}
673
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700674static int storvsc_dev_remove(struct hv_device *device)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700675{
676 struct storvsc_device *stor_device;
677 unsigned long flags;
678
679 stor_device = (struct storvsc_device *)device->ext;
680
681 spin_lock_irqsave(&device->channel->inbound_lock, flags);
682 stor_device->destroy = true;
683 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
684
685 /*
686 * At this point, all outbound traffic should be disable. We
687 * only allow inbound traffic (responses) to proceed so that
688 * outstanding requests can be completed.
689 */
690
691 storvsc_wait_to_drain(stor_device);
692
693 /*
694 * Since we have already drained, we don't need to busy wait
695 * as was done in final_release_stor_device()
696 * Note that we cannot set the ext pointer to NULL until
697 * we have drained - to drain the outgoing packets, we need to
698 * allow incoming packets.
699 */
700 spin_lock_irqsave(&device->channel->inbound_lock, flags);
701 device->ext = NULL;
702 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
703
704 /* Close the channel */
705 vmbus_close(device->channel);
706
707 kfree(stor_device);
708 return 0;
709}
710
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700711static int storvsc_do_io(struct hv_device *device,
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700712 struct hv_storvsc_request *request)
713{
714 struct storvsc_device *stor_device;
715 struct vstor_packet *vstor_packet;
716 int ret = 0;
717
718 vstor_packet = &request->vstor_packet;
719 stor_device = get_out_stor_device(device);
720
721 if (!stor_device)
722 return -ENODEV;
723
724
725 request->device = device;
726
727
728 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
729
730 vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
731
732
733 vstor_packet->vm_srb.sense_info_length = SENSE_BUFFER_SIZE;
734
735
736 vstor_packet->vm_srb.data_transfer_length =
737 request->data_buffer.len;
738
739 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
740
741 if (request->data_buffer.len) {
742 ret = vmbus_sendpacket_multipagebuffer(device->channel,
743 &request->data_buffer,
744 vstor_packet,
745 sizeof(struct vstor_packet),
746 (unsigned long)request);
747 } else {
748 ret = vmbus_sendpacket(device->channel, vstor_packet,
749 sizeof(struct vstor_packet),
750 (unsigned long)request,
751 VM_PKT_DATA_INBAND,
752 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
753 }
754
755 if (ret != 0)
756 return ret;
757
758 atomic_inc(&stor_device->num_outstanding_req);
759
760 return ret;
761}
762
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -0700763static void storvsc_get_ide_info(struct hv_device *dev, int *target, int *path)
764{
765 *target =
766 dev->dev_instance.b[5] << 8 | dev->dev_instance.b[4];
767
768 *path =
769 dev->dev_instance.b[3] << 24 |
770 dev->dev_instance.b[2] << 16 |
771 dev->dev_instance.b[1] << 8 | dev->dev_instance.b[0];
772}
773
Hank Janssenbef4a342009-07-13 16:01:31 -0700774
K. Y. Srinivasan5b60ace2011-05-10 07:54:25 -0700775static int storvsc_device_alloc(struct scsi_device *sdevice)
776{
777 /*
778 * This enables luns to be located sparsely. Otherwise, we may not
779 * discovered them.
780 */
781 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
782 return 0;
783}
784
K. Y. Srinivasana1ebfea2011-05-10 07:54:26 -0700785static int storvsc_merge_bvec(struct request_queue *q,
786 struct bvec_merge_data *bmd, struct bio_vec *bvec)
787{
788 /* checking done by caller. */
789 return bvec->bv_len;
790}
791
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700792static int storvsc_device_configure(struct scsi_device *sdevice)
793{
794 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
795 STORVSC_MAX_IO_REQUESTS);
796
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700797 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
798
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700799 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
800
801 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700802
803 return 0;
804}
805
K. Y. Srinivasan49a3c7a2011-05-10 07:54:28 -0700806static void destroy_bounce_buffer(struct scatterlist *sgl,
807 unsigned int sg_count)
808{
809 int i;
810 struct page *page_buf;
811
812 for (i = 0; i < sg_count; i++) {
813 page_buf = sg_page((&sgl[i]));
814 if (page_buf != NULL)
815 __free_page(page_buf);
816 }
817
818 kfree(sgl);
819}
820
K. Y. Srinivasan3862ef32011-05-10 07:54:29 -0700821static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
822{
823 int i;
824
825 /* No need to check */
826 if (sg_count < 2)
827 return -1;
828
829 /* We have at least 2 sg entries */
830 for (i = 0; i < sg_count; i++) {
831 if (i == 0) {
832 /* make sure 1st one does not have hole */
833 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
834 return i;
835 } else if (i == sg_count - 1) {
836 /* make sure last one does not have hole */
837 if (sgl[i].offset != 0)
838 return i;
839 } else {
840 /* make sure no hole in the middle */
841 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
842 return i;
843 }
844 }
845 return -1;
846}
847
K. Y. Srinivasana9753cb2011-05-10 07:54:30 -0700848static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
849 unsigned int sg_count,
850 unsigned int len)
851{
852 int i;
853 int num_pages;
854 struct scatterlist *bounce_sgl;
855 struct page *page_buf;
856
857 num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
858
859 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
860 if (!bounce_sgl)
861 return NULL;
862
863 for (i = 0; i < num_pages; i++) {
864 page_buf = alloc_page(GFP_ATOMIC);
865 if (!page_buf)
866 goto cleanup;
867 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
868 }
869
870 return bounce_sgl;
871
872cleanup:
873 destroy_bounce_buffer(bounce_sgl, num_pages);
874 return NULL;
875}
876
K. Y. Srinivasan29fe2c92011-05-10 07:54:31 -0700877
878/* Assume the original sgl has enough room */
879static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
880 struct scatterlist *bounce_sgl,
881 unsigned int orig_sgl_count)
882{
883 int i;
884 int j = 0;
885 unsigned long src, dest;
886 unsigned int srclen, destlen, copylen;
887 unsigned int total_copied = 0;
888 unsigned long bounce_addr = 0;
889 unsigned long dest_addr = 0;
890 unsigned long flags;
891
892 local_irq_save(flags);
893
894 for (i = 0; i < orig_sgl_count; i++) {
895 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
896 KM_IRQ0) + orig_sgl[i].offset;
897 dest = dest_addr;
898 destlen = orig_sgl[i].length;
899
900 if (bounce_addr == 0)
901 bounce_addr =
902 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
903 KM_IRQ0);
904
905 while (destlen) {
906 src = bounce_addr + bounce_sgl[j].offset;
907 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
908
909 copylen = min(srclen, destlen);
910 memcpy((void *)dest, (void *)src, copylen);
911
912 total_copied += copylen;
913 bounce_sgl[j].offset += copylen;
914 destlen -= copylen;
915 dest += copylen;
916
917 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
918 /* full */
919 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
920 j++;
921
922 /* if we need to use another bounce buffer */
923 if (destlen || i != orig_sgl_count - 1)
924 bounce_addr =
925 (unsigned long)kmap_atomic(
926 sg_page((&bounce_sgl[j])), KM_IRQ0);
927 } else if (destlen == 0 && i == orig_sgl_count - 1) {
928 /* unmap the last bounce that is < PAGE_SIZE */
929 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
930 }
931 }
932
933 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
934 KM_IRQ0);
935 }
936
937 local_irq_restore(flags);
938
939 return total_copied;
940}
941
K. Y. Srinivasan6a8ff442011-05-10 07:54:32 -0700942
943/* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
944static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
945 struct scatterlist *bounce_sgl,
946 unsigned int orig_sgl_count)
947{
948 int i;
949 int j = 0;
950 unsigned long src, dest;
951 unsigned int srclen, destlen, copylen;
952 unsigned int total_copied = 0;
953 unsigned long bounce_addr = 0;
954 unsigned long src_addr = 0;
955 unsigned long flags;
956
957 local_irq_save(flags);
958
959 for (i = 0; i < orig_sgl_count; i++) {
960 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
961 KM_IRQ0) + orig_sgl[i].offset;
962 src = src_addr;
963 srclen = orig_sgl[i].length;
964
965 if (bounce_addr == 0)
966 bounce_addr =
967 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
968 KM_IRQ0);
969
970 while (srclen) {
971 /* assume bounce offset always == 0 */
972 dest = bounce_addr + bounce_sgl[j].length;
973 destlen = PAGE_SIZE - bounce_sgl[j].length;
974
975 copylen = min(srclen, destlen);
976 memcpy((void *)dest, (void *)src, copylen);
977
978 total_copied += copylen;
979 bounce_sgl[j].length += copylen;
980 srclen -= copylen;
981 src += copylen;
982
983 if (bounce_sgl[j].length == PAGE_SIZE) {
984 /* full..move to next entry */
985 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
986 j++;
987
988 /* if we need to use another bounce buffer */
989 if (srclen || i != orig_sgl_count - 1)
990 bounce_addr =
991 (unsigned long)kmap_atomic(
992 sg_page((&bounce_sgl[j])), KM_IRQ0);
993
994 } else if (srclen == 0 && i == orig_sgl_count - 1) {
995 /* unmap the last bounce that is < PAGE_SIZE */
996 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
997 }
998 }
999
1000 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
1001 }
1002
1003 local_irq_restore(flags);
1004
1005 return total_copied;
1006}
1007
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -07001008
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -07001009static int storvsc_remove(struct hv_device *dev)
1010{
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -07001011 struct Scsi_Host *host = dev_get_drvdata(&dev->device);
1012 struct hv_host_device *host_dev =
1013 (struct hv_host_device *)host->hostdata;
1014
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -07001015 scsi_remove_host(host);
1016
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -07001017 scsi_host_put(host);
K. Y. Srinivasand36b0a02011-06-06 15:49:29 -07001018
K. Y. Srinivasan2935a402011-06-06 15:49:28 -07001019 storvsc_dev_remove(dev);
1020 if (host_dev->request_pool) {
1021 kmem_cache_destroy(host_dev->request_pool);
1022 host_dev->request_pool = NULL;
1023 }
K. Y. Srinivasan5c5c0232011-05-10 07:54:33 -07001024 return 0;
1025}
1026
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001027
1028static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
1029 sector_t capacity, int *info)
1030{
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001031 sector_t nsect = capacity;
1032 sector_t cylinders = nsect;
1033 int heads, sectors_pt;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001034
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001035 /*
1036 * We are making up these values; let us keep it simple.
1037 */
1038 heads = 0xff;
1039 sectors_pt = 0x3f; /* Sectors per track */
1040 sector_div(cylinders, heads * sectors_pt);
1041 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
1042 cylinders = 0xffff;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001043
1044 info[0] = heads;
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001045 info[1] = sectors_pt;
1046 info[2] = (int)cylinders;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001047
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001048 return 0;
1049}
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001050
1051static int storvsc_host_reset(struct hv_device *device)
1052{
1053 struct storvsc_device *stor_device;
1054 struct hv_storvsc_request *request;
1055 struct vstor_packet *vstor_packet;
1056 int ret, t;
1057
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001058
K. Y. Srinivasan1eaaddf2011-08-27 11:31:03 -07001059 stor_device = get_out_stor_device(device);
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001060 if (!stor_device)
K. Y. Srinivasanf3b74162011-08-25 09:49:08 -07001061 return -ENODEV;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001062
1063 request = &stor_device->reset_request;
1064 vstor_packet = &request->vstor_packet;
1065
1066 init_completion(&request->wait_event);
1067
1068 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
1069 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
1070 vstor_packet->vm_srb.path_id = stor_device->path_id;
1071
1072 ret = vmbus_sendpacket(device->channel, vstor_packet,
1073 sizeof(struct vstor_packet),
1074 (unsigned long)&stor_device->reset_request,
1075 VM_PKT_DATA_INBAND,
1076 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1077 if (ret != 0)
1078 goto cleanup;
1079
K. Y. Srinivasan46d2eb62011-06-16 13:16:36 -07001080 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001081 if (t == 0) {
1082 ret = -ETIMEDOUT;
1083 goto cleanup;
1084 }
1085
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001086
1087 /*
1088 * At this point, all outstanding requests in the adapter
1089 * should have been flushed out and return to us
1090 */
1091
1092cleanup:
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001093 return ret;
1094}
1095
1096
K. Y. Srinivasan96e690be2011-05-10 07:54:38 -07001097/*
1098 * storvsc_host_reset_handler - Reset the scsi HBA
1099 */
1100static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
1101{
1102 int ret;
1103 struct hv_host_device *host_dev =
1104 (struct hv_host_device *)scmnd->device->host->hostdata;
1105 struct hv_device *dev = host_dev->dev;
1106
K. Y. Srinivasan96e690be2011-05-10 07:54:38 -07001107 ret = storvsc_host_reset(dev);
1108 if (ret != 0)
1109 return ret;
1110
K. Y. Srinivasan96e690be2011-05-10 07:54:38 -07001111 return ret;
1112}
1113
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001114
1115/*
1116 * storvsc_commmand_completion - Command completion processing
1117 */
1118static void storvsc_commmand_completion(struct hv_storvsc_request *request)
1119{
1120 struct storvsc_cmd_request *cmd_request =
1121 (struct storvsc_cmd_request *)request->context;
1122 struct scsi_cmnd *scmnd = cmd_request->cmd;
1123 struct hv_host_device *host_dev =
1124 (struct hv_host_device *)scmnd->device->host->hostdata;
1125 void (*scsi_done_fn)(struct scsi_cmnd *);
1126 struct scsi_sense_hdr sense_hdr;
1127 struct vmscsi_request *vm_srb;
1128
K. Y. Srinivasan3612ef92011-08-27 11:31:20 -07001129 vm_srb = &request->vstor_packet.vm_srb;
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001130 if (cmd_request->bounce_sgl_count) {
K. Y. Srinivasan3612ef92011-08-27 11:31:20 -07001131 if (vm_srb->data_in == READ_TYPE) {
1132 copy_from_bounce_buffer(scsi_sglist(scmnd),
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001133 cmd_request->bounce_sgl,
1134 scsi_sg_count(scmnd));
K. Y. Srinivasan3612ef92011-08-27 11:31:20 -07001135 destroy_bounce_buffer(cmd_request->bounce_sgl,
1136 cmd_request->bounce_sgl_count);
1137 }
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001138 }
1139
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001140 scmnd->result = vm_srb->scsi_status;
1141
1142 if (scmnd->result) {
1143 if (scsi_normalize_sense(scmnd->sense_buffer,
1144 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
1145 scsi_print_sense_hdr("storvsc", &sense_hdr);
1146 }
1147
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001148 scsi_set_resid(scmnd,
1149 request->data_buffer.len -
1150 vm_srb->data_transfer_length);
1151
1152 scsi_done_fn = scmnd->scsi_done;
1153
1154 scmnd->host_scribble = NULL;
1155 scmnd->scsi_done = NULL;
1156
K. Y. Srinivasan8a411ba2011-05-10 07:54:41 -07001157 scsi_done_fn(scmnd);
1158
1159 kmem_cache_free(host_dev->request_pool, cmd_request);
1160}
1161
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001162
1163/*
1164 * storvsc_queuecommand - Initiate command processing
1165 */
1166static int storvsc_queuecommand_lck(struct scsi_cmnd *scmnd,
1167 void (*done)(struct scsi_cmnd *))
1168{
1169 int ret;
1170 struct hv_host_device *host_dev =
1171 (struct hv_host_device *)scmnd->device->host->hostdata;
1172 struct hv_device *dev = host_dev->dev;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001173 struct hv_storvsc_request *request;
1174 struct storvsc_cmd_request *cmd_request;
1175 unsigned int request_size = 0;
1176 int i;
1177 struct scatterlist *sgl;
1178 unsigned int sg_count = 0;
1179 struct vmscsi_request *vm_srb;
1180
1181
1182 /* If retrying, no need to prep the cmd */
1183 if (scmnd->host_scribble) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001184
1185 cmd_request =
1186 (struct storvsc_cmd_request *)scmnd->host_scribble;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001187
1188 goto retry_request;
1189 }
1190
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001191 scmnd->scsi_done = done;
1192
1193 request_size = sizeof(struct storvsc_cmd_request);
1194
1195 cmd_request = kmem_cache_zalloc(host_dev->request_pool,
1196 GFP_ATOMIC);
1197 if (!cmd_request) {
1198 scmnd->scsi_done = NULL;
1199 return SCSI_MLQUEUE_DEVICE_BUSY;
1200 }
1201
1202 /* Setup the cmd request */
1203 cmd_request->bounce_sgl_count = 0;
1204 cmd_request->bounce_sgl = NULL;
1205 cmd_request->cmd = scmnd;
1206
1207 scmnd->host_scribble = (unsigned char *)cmd_request;
1208
1209 request = &cmd_request->request;
1210 vm_srb = &request->vstor_packet.vm_srb;
1211
1212
1213 /* Build the SRB */
1214 switch (scmnd->sc_data_direction) {
1215 case DMA_TO_DEVICE:
1216 vm_srb->data_in = WRITE_TYPE;
1217 break;
1218 case DMA_FROM_DEVICE:
1219 vm_srb->data_in = READ_TYPE;
1220 break;
1221 default:
1222 vm_srb->data_in = UNKNOWN_TYPE;
1223 break;
1224 }
1225
1226 request->on_io_completion = storvsc_commmand_completion;
1227 request->context = cmd_request;/* scmnd; */
1228
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001229 vm_srb->port_number = host_dev->port;
1230 vm_srb->path_id = scmnd->device->channel;
1231 vm_srb->target_id = scmnd->device->id;
1232 vm_srb->lun = scmnd->device->lun;
1233
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001234 vm_srb->cdb_length = scmnd->cmd_len;
1235
1236 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
1237
1238 request->sense_buffer = scmnd->sense_buffer;
1239
1240
1241 request->data_buffer.len = scsi_bufflen(scmnd);
1242 if (scsi_sg_count(scmnd)) {
1243 sgl = (struct scatterlist *)scsi_sglist(scmnd);
1244 sg_count = scsi_sg_count(scmnd);
1245
1246 /* check if we need to bounce the sgl */
1247 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
1248 cmd_request->bounce_sgl =
1249 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
1250 scsi_bufflen(scmnd));
1251 if (!cmd_request->bounce_sgl) {
1252 scmnd->scsi_done = NULL;
1253 scmnd->host_scribble = NULL;
1254 kmem_cache_free(host_dev->request_pool,
1255 cmd_request);
1256
1257 return SCSI_MLQUEUE_HOST_BUSY;
1258 }
1259
1260 cmd_request->bounce_sgl_count =
1261 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
1262 PAGE_SHIFT;
1263
K. Y. Srinivasanfa23b8c2011-08-27 11:31:21 -07001264 if (vm_srb->data_in == WRITE_TYPE)
1265 copy_to_bounce_buffer(sgl,
1266 cmd_request->bounce_sgl,
1267 scsi_sg_count(scmnd));
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001268
1269 sgl = cmd_request->bounce_sgl;
1270 sg_count = cmd_request->bounce_sgl_count;
1271 }
1272
1273 request->data_buffer.offset = sgl[0].offset;
1274
1275 for (i = 0; i < sg_count; i++)
1276 request->data_buffer.pfn_array[i] =
1277 page_to_pfn(sg_page((&sgl[i])));
1278
1279 } else if (scsi_sglist(scmnd)) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001280 request->data_buffer.offset =
1281 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
1282 request->data_buffer.pfn_array[0] =
1283 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
1284 }
1285
1286retry_request:
1287 /* Invokes the vsc to start an IO */
K. Y. Srinivasan636f0fd2011-05-10 07:54:50 -07001288 ret = storvsc_do_io(dev, &cmd_request->request);
1289
K. Y. Srinivasand2598f02011-08-25 09:48:58 -07001290 if (ret == -EAGAIN) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001291 /* no more space */
1292
1293 if (cmd_request->bounce_sgl_count) {
1294 /*
1295 * FIXME: We can optimize on writes by just skipping
1296 * this
1297 */
1298 copy_from_bounce_buffer(scsi_sglist(scmnd),
1299 cmd_request->bounce_sgl,
1300 scsi_sg_count(scmnd));
1301 destroy_bounce_buffer(cmd_request->bounce_sgl,
1302 cmd_request->bounce_sgl_count);
1303 }
1304
1305 kmem_cache_free(host_dev->request_pool, cmd_request);
1306
1307 scmnd->scsi_done = NULL;
1308 scmnd->host_scribble = NULL;
1309
1310 ret = SCSI_MLQUEUE_DEVICE_BUSY;
1311 }
1312
1313 return ret;
1314}
1315
1316static DEF_SCSI_QCMD(storvsc_queuecommand)
1317
Hank Janssenbef4a342009-07-13 16:01:31 -07001318
Bill Pemberton454f18a2009-07-27 16:47:24 -04001319/* Scsi driver */
Hank Janssenbef4a342009-07-13 16:01:31 -07001320static struct scsi_host_template scsi_driver = {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001321 .module = THIS_MODULE,
1322 .name = "storvsc_host_t",
1323 .bios_param = storvsc_get_chs,
1324 .queuecommand = storvsc_queuecommand,
1325 .eh_host_reset_handler = storvsc_host_reset_handler,
1326 .slave_alloc = storvsc_device_alloc,
1327 .slave_configure = storvsc_device_configure,
1328 .cmd_per_lun = 1,
1329 /* 64 max_queue * 1 target */
Lars Lindley0686e4f2010-03-11 23:51:23 +01001330 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001331 .this_id = -1,
Bill Pemberton454f18a2009-07-27 16:47:24 -04001332 /* no use setting to 0 since ll_blk_rw reset it to 1 */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001333 /* currently 32 */
1334 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
1335 /*
1336 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
1337 * into 1 sg element. If set, we must limit the max_segment_size to
1338 * PAGE_SIZE, otherwise we may get 1 sg element that represents
1339 * multiple
1340 */
Bill Pemberton454f18a2009-07-27 16:47:24 -04001341 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001342 .use_clustering = ENABLE_CLUSTERING,
Bill Pemberton454f18a2009-07-27 16:47:24 -04001343 /* Make sure we dont get a sg segment crosses a page boundary */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001344 .dma_boundary = PAGE_SIZE-1,
Hank Janssenbef4a342009-07-13 16:01:31 -07001345};
1346
K. Y. Srinivasan21e37742011-08-27 11:31:18 -07001347/*
1348 * The storvsc_probe function assumes that the IDE guid
1349 * is the second entry.
1350 */
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001351static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001352 /* SCSI guid */
1353 { VMBUS_DEVICE(0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
1354 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f) },
K. Y. Srinivasan21e37742011-08-27 11:31:18 -07001355 /* IDE guid */
1356 { VMBUS_DEVICE(0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
1357 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5) },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001358 { },
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001359};
Hank Janssenbef4a342009-07-13 16:01:31 -07001360
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001361MODULE_DEVICE_TABLE(vmbus, id_table);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001362
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001363
Hank Janssen3e189512010-03-04 22:11:00 +00001364/*
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001365 * storvsc_probe - Add a new device for this driver
1366 */
1367
1368static int storvsc_probe(struct hv_device *device)
1369{
1370 int ret;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001371 struct Scsi_Host *host;
1372 struct hv_host_device *host_dev;
1373 struct storvsc_device_info device_info;
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001374 bool dev_is_ide;
1375 int path = 0;
1376 int target = 0;
1377
K. Y. Srinivasan21e37742011-08-27 11:31:18 -07001378 if (!memcmp(&device->dev_type.b, id_table[1].guid, sizeof(uuid_le)))
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001379 dev_is_ide = true;
1380 else
1381 dev_is_ide = false;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001382
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001383 host = scsi_host_alloc(&scsi_driver,
1384 sizeof(struct hv_host_device));
1385 if (!host)
1386 return -ENOMEM;
1387
1388 dev_set_drvdata(&device->device, host);
1389
1390 host_dev = (struct hv_host_device *)host->hostdata;
1391 memset(host_dev, 0, sizeof(struct hv_host_device));
1392
1393 host_dev->port = host->host_no;
1394 host_dev->dev = device;
1395
1396 host_dev->request_pool =
1397 kmem_cache_create(dev_name(&device->device),
1398 sizeof(struct storvsc_cmd_request), 0,
1399 SLAB_HWCACHE_ALIGN, NULL);
1400
1401 if (!host_dev->request_pool) {
1402 scsi_host_put(host);
1403 return -ENOMEM;
1404 }
1405
1406 device_info.port_number = host->host_no;
K. Y. Srinivasanfa4d1232011-05-10 07:56:01 -07001407 device_info.ring_buffer_size = storvsc_ringbuffer_size;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001408 /* Call to the vsc driver to add the device */
K. Y. Srinivasan58f1f5c2011-05-10 07:54:49 -07001409 ret = storvsc_dev_add(device, (void *)&device_info);
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001410
1411 if (ret != 0) {
1412 kmem_cache_destroy(host_dev->request_pool);
1413 scsi_host_put(host);
K. Y. Srinivasan8e854682011-08-25 09:49:09 -07001414 return -ENODEV;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001415 }
1416
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001417 if (dev_is_ide)
1418 storvsc_get_ide_info(device, &target, &path);
1419
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001420 host_dev->path = device_info.path_id;
1421 host_dev->target = device_info.target_id;
1422
1423 /* max # of devices per target */
1424 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
1425 /* max # of targets per channel */
1426 host->max_id = STORVSC_MAX_TARGETS;
1427 /* max # of channels */
1428 host->max_channel = STORVSC_MAX_CHANNELS - 1;
1429
1430 /* Register the HBA and start the scsi bus scan */
1431 ret = scsi_add_host(host, &device->device);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001432 if (ret != 0)
1433 goto err_out;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001434
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001435 if (!dev_is_ide) {
1436 scsi_scan_host(host);
1437 return 0;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001438 }
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001439 ret = scsi_add_device(host, 0, target, 0);
1440 if (ret) {
1441 scsi_remove_host(host);
1442 goto err_out;
1443 }
1444 return 0;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001445
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001446err_out:
1447 storvsc_dev_remove(device);
1448 kmem_cache_destroy(host_dev->request_pool);
1449 scsi_host_put(host);
1450 return -ENODEV;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001451}
1452
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -07001453/* The one and only one */
1454
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -07001455static struct hv_driver storvsc_drv = {
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001456 .name = "storvsc",
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001457 .id_table = id_table,
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -07001458 .probe = storvsc_probe,
1459 .remove = storvsc_remove,
K. Y. Srinivasan39ae6fa2011-05-10 07:54:46 -07001460};
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -07001461
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -07001462static int __init storvsc_drv_init(void)
Hank Janssenbef4a342009-07-13 16:01:31 -07001463{
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001464 u32 max_outstanding_req_per_channel;
1465
1466 /*
1467 * Divide the ring buffer data size (which is 1 page less
1468 * than the ring buffer size since that page is reserved for
1469 * the ring buffer indices) by the max request size (which is
1470 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
1471 */
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001472 max_outstanding_req_per_channel =
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001473 ((storvsc_ringbuffer_size - PAGE_SIZE) /
1474 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
1475 sizeof(struct vstor_packet) + sizeof(u64),
1476 sizeof(u64)));
Hank Janssenbef4a342009-07-13 16:01:31 -07001477
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001478 if (max_outstanding_req_per_channel <
K. Y. Srinivasanf8feed02011-05-10 07:54:24 -07001479 STORVSC_MAX_IO_REQUESTS)
K. Y. Srinivasanb06efc12011-08-25 09:49:10 -07001480 return -EINVAL;
Hank Janssenbef4a342009-07-13 16:01:31 -07001481
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001482 return vmbus_driver_register(&storvsc_drv);
Hank Janssenbef4a342009-07-13 16:01:31 -07001483}
1484
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -07001485static void __exit storvsc_drv_exit(void)
Hank Janssenbef4a342009-07-13 16:01:31 -07001486{
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001487 vmbus_driver_unregister(&storvsc_drv);
Hank Janssenbef4a342009-07-13 16:01:31 -07001488}
1489
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001490MODULE_LICENSE("GPL");
Hank Janssen26c14cc2010-02-11 23:02:42 +00001491MODULE_VERSION(HV_DRV_VERSION);
Stephen Hemminger3afc7cc32010-05-06 21:44:45 -07001492MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -07001493module_init(storvsc_drv_init);
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -07001494module_exit(storvsc_drv_exit);