Staging: hv: storvsc: In case of scsi errors offline the device
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / hv / storvsc_drv.c
CommitLineData
bef4a34a 1/*
bef4a34a
HJ
2 * 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>
972621c9 20 * K. Y. Srinivasan <kys@microsoft.com>
bef4a34a 21 */
a1be1706
S
22
23#include <linux/kernel.h>
f0d79fe9 24#include <linux/wait.h>
a1be1706
S
25#include <linux/sched.h>
26#include <linux/completion.h>
27#include <linux/string.h>
28#include <linux/mm.h>
29#include <linux/delay.h>
bef4a34a 30#include <linux/init.h>
5a0e3ad6 31#include <linux/slab.h>
bef4a34a
HJ
32#include <linux/module.h>
33#include <linux/device.h>
bef4a34a
HJ
34#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>
bef4a34a 41#include <scsi/scsi_dbg.h>
3f335ea2
S
42
43#include "hyperv.h"
f0d79fe9 44
c1b3d067
S
45#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
f0d79fe9
S
51/* 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 */
f0d79fe9
S
223
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
f0d79fe9
S
269
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
c1b3d067
S
294struct 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};
f0d79fe9
S
311
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}
bef4a34a 333
8dcf37d4
S
334static 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
4ed51a21
S
515 /*
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
8dcf37d4
S
532
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
c1b3d067 656static int storvsc_dev_add(struct hv_device *device,
8dcf37d4
S
657 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
c1b3d067 691static int storvsc_dev_remove(struct hv_device *device)
8dcf37d4
S
692{
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
c1b3d067 728static int storvsc_do_io(struct hv_device *device,
8dcf37d4
S
729 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
bd1f5d6a
S
780static 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
bef4a34a 791
5b60acee
S
792static 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
a1ebfeae
S
802static 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
419f2d03
S
809static int storvsc_device_configure(struct scsi_device *sdevice)
810{
811 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
812 STORVSC_MAX_IO_REQUESTS);
813
419f2d03
S
814 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
815
419f2d03
S
816 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
817
818 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
419f2d03
S
819
820 return 0;
821}
822
49a3c7a9
S
823static 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
3862ef3e
S
838static 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
a9753cbd
S
865static 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
29fe2c9b
S
894
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
6a8ff44b
S
959
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
5c5c0234 1025
5c5c0234
S
1026static int storvsc_remove(struct hv_device *dev)
1027{
5c5c0234
S
1028 struct Scsi_Host *host = dev_get_drvdata(&dev->device);
1029 struct hv_host_device *host_dev =
1030 (struct hv_host_device *)host->hostdata;
1031
2935a407
S
1032 scsi_remove_host(host);
1033
2935a407 1034 scsi_host_put(host);
d36b0a03 1035
6cdc57c0 1036 storvsc_dev_remove(dev);
5c5c0234
S
1037 if (host_dev->request_pool) {
1038 kmem_cache_destroy(host_dev->request_pool);
1039 host_dev->request_pool = NULL;
1040 }
5c5c0234
S
1041 return 0;
1042}
1043
62838ce2
S
1044
1045static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
1046 sector_t capacity, int *info)
1047{
5326fd5c
S
1048 sector_t nsect = capacity;
1049 sector_t cylinders = nsect;
1050 int heads, sectors_pt;
62838ce2 1051
5326fd5c
S
1052 /*
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;
62838ce2
S
1060
1061 info[0] = heads;
5326fd5c
S
1062 info[1] = sectors_pt;
1063 info[2] = (int)cylinders;
62838ce2 1064
62838ce2
S
1065 return 0;
1066}
aa3d789e
S
1067
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
aa3d789e 1075
1eaaddf9 1076 stor_device = get_out_stor_device(device);
aa3d789e 1077 if (!stor_device)
f3b74165 1078 return -ENODEV;
aa3d789e
S
1079
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
46d2eb6d 1097 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
aa3d789e
S
1098 if (t == 0) {
1099 ret = -ETIMEDOUT;
1100 goto cleanup;
1101 }
1102
aa3d789e
S
1103
1104 /*
1105 * At this point, all outstanding requests in the adapter
1106 * should have been flushed out and return to us
1107 */
1108
1109cleanup:
aa3d789e
S
1110 return ret;
1111}
1112
1113
96e690be
S
1114/*
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
96e690be
S
1124 ret = storvsc_host_reset(dev);
1125 if (ret != 0)
1126 return ret;
1127
96e690be
S
1128 return ret;
1129}
1130
8a411bad
S
1131
1132/*
b8de73df 1133 * storvsc_command_completion - Command completion processing
8a411bad 1134 */
b8de73df 1135static void storvsc_command_completion(struct hv_storvsc_request *request)
8a411bad
S
1136{
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
3612ef99 1146 vm_srb = &request->vstor_packet.vm_srb;
8a411bad 1147 if (cmd_request->bounce_sgl_count) {
3612ef99
S
1148 if (vm_srb->data_in == READ_TYPE) {
1149 copy_from_bounce_buffer(scsi_sglist(scmnd),
8a411bad
S
1150 cmd_request->bounce_sgl,
1151 scsi_sg_count(scmnd));
3612ef99
S
1152 destroy_bounce_buffer(cmd_request->bounce_sgl,
1153 cmd_request->bounce_sgl_count);
1154 }
8a411bad
S
1155 }
1156
2544b794
S
1157 /*
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;
8a411bad
S
1166
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
8a411bad
S
1173 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
8a411bad
S
1182 scsi_done_fn(scmnd);
1183
1184 kmem_cache_free(host_dev->request_pool, cmd_request);
1185}
1186
c5b463ae
S
1187
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;
c5b463ae
S
1198 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) {
c5b463ae
S
1209
1210 cmd_request =
1211 (struct storvsc_cmd_request *)scmnd->host_scribble;
c5b463ae
S
1212
1213 goto retry_request;
1214 }
1215
c5b463ae
S
1216 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
b8de73df 1251 request->on_io_completion = storvsc_command_completion;
c5b463ae
S
1252 request->context = cmd_request;/* scmnd; */
1253
c5b463ae
S
1254 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
c5b463ae
S
1259 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
fa23b8c7
S
1289 if (vm_srb->data_in == WRITE_TYPE)
1290 copy_to_bounce_buffer(sgl,
1291 cmd_request->bounce_sgl,
1292 scsi_sg_count(scmnd));
c5b463ae
S
1293
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)) {
c5b463ae
S
1305 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 */
636f0fd1
S
1313 ret = storvsc_do_io(dev, &cmd_request->request);
1314
d2598f01 1315 if (ret == -EAGAIN) {
c5b463ae
S
1316 /* no more space */
1317
1318 if (cmd_request->bounce_sgl_count) {
1319 /*
1320 * FIXME: We can optimize on writes by just skipping
1321 * this
1322 */
1323 copy_from_bounce_buffer(scsi_sglist(scmnd),
1324 cmd_request->bounce_sgl,
1325 scsi_sg_count(scmnd));
1326 destroy_bounce_buffer(cmd_request->bounce_sgl,
1327 cmd_request->bounce_sgl_count);
1328 }
1329
1330 kmem_cache_free(host_dev->request_pool, cmd_request);
1331
1332 scmnd->scsi_done = NULL;
1333 scmnd->host_scribble = NULL;
1334
1335 ret = SCSI_MLQUEUE_DEVICE_BUSY;
1336 }
1337
1338 return ret;
1339}
1340
1341static DEF_SCSI_QCMD(storvsc_queuecommand)
1342
bef4a34a 1343
454f18a9 1344/* Scsi driver */
bef4a34a 1345static struct scsi_host_template scsi_driver = {
ff568d3a
GKH
1346 .module = THIS_MODULE,
1347 .name = "storvsc_host_t",
1348 .bios_param = storvsc_get_chs,
1349 .queuecommand = storvsc_queuecommand,
1350 .eh_host_reset_handler = storvsc_host_reset_handler,
1351 .slave_alloc = storvsc_device_alloc,
1352 .slave_configure = storvsc_device_configure,
1353 .cmd_per_lun = 1,
1354 /* 64 max_queue * 1 target */
0686e4f4 1355 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
ff568d3a 1356 .this_id = -1,
454f18a9 1357 /* no use setting to 0 since ll_blk_rw reset it to 1 */
ff568d3a
GKH
1358 /* currently 32 */
1359 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
1360 /*
1361 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
1362 * into 1 sg element. If set, we must limit the max_segment_size to
1363 * PAGE_SIZE, otherwise we may get 1 sg element that represents
1364 * multiple
1365 */
454f18a9 1366 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
ff568d3a 1367 .use_clustering = ENABLE_CLUSTERING,
454f18a9 1368 /* Make sure we dont get a sg segment crosses a page boundary */
ff568d3a 1369 .dma_boundary = PAGE_SIZE-1,
bef4a34a
HJ
1370};
1371
21e37742
S
1372/*
1373 * The storvsc_probe function assumes that the IDE guid
1374 * is the second entry.
1375 */
d847b5fe 1376static const struct hv_vmbus_device_id id_table[] = {
c45cf2d4
GKH
1377 /* SCSI guid */
1378 { VMBUS_DEVICE(0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
1379 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f) },
21e37742
S
1380 /* IDE guid */
1381 { VMBUS_DEVICE(0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
1382 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5) },
c45cf2d4 1383 { },
d847b5fe 1384};
bef4a34a 1385
d847b5fe 1386MODULE_DEVICE_TABLE(vmbus, id_table);
bd1f5d6a 1387
bd1f5d6a 1388
3e189519 1389/*
ff568d3a
GKH
1390 * storvsc_probe - Add a new device for this driver
1391 */
f5c78872 1392
9efd21e1 1393static int storvsc_probe(struct hv_device *device)
bef4a34a 1394{
ff568d3a 1395 int ret;
bef4a34a 1396 struct Scsi_Host *host;
795b613d 1397 struct hv_host_device *host_dev;
9f0c7d2c 1398 struct storvsc_device_info device_info;
bd1f5d6a
S
1399 bool dev_is_ide;
1400 int path = 0;
1401 int target = 0;
1402
21e37742 1403 if (!memcmp(&device->dev_type.b, id_table[1].guid, sizeof(uuid_le)))
bd1f5d6a
S
1404 dev_is_ide = true;
1405 else
1406 dev_is_ide = false;
bef4a34a 1407
ff568d3a 1408 host = scsi_host_alloc(&scsi_driver,
972621c9 1409 sizeof(struct hv_host_device));
f8feed06 1410 if (!host)
bef4a34a 1411 return -ENOMEM;
bef4a34a 1412
9efd21e1 1413 dev_set_drvdata(&device->device, host);
bef4a34a 1414
795b613d
S
1415 host_dev = (struct hv_host_device *)host->hostdata;
1416 memset(host_dev, 0, sizeof(struct hv_host_device));
bef4a34a 1417
795b613d 1418 host_dev->port = host->host_no;
97c15296 1419 host_dev->dev = device;
bef4a34a 1420
795b613d 1421 host_dev->request_pool =
9efd21e1 1422 kmem_cache_create(dev_name(&device->device),
1e05d88e 1423 sizeof(struct storvsc_cmd_request), 0,
ff568d3a
GKH
1424 SLAB_HWCACHE_ALIGN, NULL);
1425
795b613d 1426 if (!host_dev->request_pool) {
bef4a34a 1427 scsi_host_put(host);
bef4a34a
HJ
1428 return -ENOMEM;
1429 }
1430
8a046024 1431 device_info.port_number = host->host_no;
fa4d123a 1432 device_info.ring_buffer_size = storvsc_ringbuffer_size;
454f18a9 1433 /* Call to the vsc driver to add the device */
58f1f5cb 1434 ret = storvsc_dev_add(device, (void *)&device_info);
9efd21e1 1435
ff568d3a 1436 if (ret != 0) {
795b613d 1437 kmem_cache_destroy(host_dev->request_pool);
bef4a34a 1438 scsi_host_put(host);
8e854680 1439 return -ENODEV;
bef4a34a
HJ
1440 }
1441
bd1f5d6a
S
1442 if (dev_is_ide)
1443 storvsc_get_ide_info(device, &target, &path);
1444
795b613d
S
1445 host_dev->path = device_info.path_id;
1446 host_dev->target = device_info.target_id;
bef4a34a 1447
ff568d3a
GKH
1448 /* max # of devices per target */
1449 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
1450 /* max # of targets per channel */
1451 host->max_id = STORVSC_MAX_TARGETS;
1452 /* max # of channels */
1453 host->max_channel = STORVSC_MAX_CHANNELS - 1;
bef4a34a 1454
454f18a9 1455 /* Register the HBA and start the scsi bus scan */
9efd21e1 1456 ret = scsi_add_host(host, &device->device);
bd1f5d6a
S
1457 if (ret != 0)
1458 goto err_out;
bef4a34a 1459
bd1f5d6a
S
1460 if (!dev_is_ide) {
1461 scsi_scan_host(host);
1462 return 0;
1463 }
1464 ret = scsi_add_device(host, 0, target, 0);
1465 if (ret) {
1466 scsi_remove_host(host);
1467 goto err_out;
bef4a34a 1468 }
bd1f5d6a 1469 return 0;
bef4a34a 1470
bd1f5d6a
S
1471err_out:
1472 storvsc_dev_remove(device);
1473 kmem_cache_destroy(host_dev->request_pool);
1474 scsi_host_put(host);
1475 return -ENODEV;
bef4a34a
HJ
1476}
1477
7bd05b91
S
1478/* The one and only one */
1479
40bf63ed 1480static struct hv_driver storvsc_drv = {
768fa219 1481 .name = "storvsc",
d847b5fe 1482 .id_table = id_table,
40bf63ed
S
1483 .probe = storvsc_probe,
1484 .remove = storvsc_remove,
39ae6fae 1485};
7bd05b91 1486
d9bbae83 1487static int __init storvsc_drv_init(void)
f5c78872 1488{
01415ab3
S
1489 u32 max_outstanding_req_per_channel;
1490
1491 /*
1492 * Divide the ring buffer data size (which is 1 page less
1493 * than the ring buffer size since that page is reserved for
1494 * the ring buffer indices) by the max request size (which is
1495 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
1496 */
01415ab3 1497 max_outstanding_req_per_channel =
768fa219
GKH
1498 ((storvsc_ringbuffer_size - PAGE_SIZE) /
1499 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
1500 sizeof(struct vstor_packet) + sizeof(u64),
1501 sizeof(u64)));
f5c78872 1502
01415ab3 1503 if (max_outstanding_req_per_channel <
f5c78872 1504 STORVSC_MAX_IO_REQUESTS)
b06efc19 1505 return -EINVAL;
f5c78872 1506
768fa219 1507 return vmbus_driver_register(&storvsc_drv);
f5c78872
S
1508}
1509
c63ba9e1 1510static void __exit storvsc_drv_exit(void)
f5c78872 1511{
768fa219 1512 vmbus_driver_unregister(&storvsc_drv);
f5c78872
S
1513}
1514
ff568d3a 1515MODULE_LICENSE("GPL");
26c14cc1 1516MODULE_VERSION(HV_DRV_VERSION);
3afc7cc3 1517MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
d9bbae83 1518module_init(storvsc_drv_init);
c63ba9e1 1519module_exit(storvsc_drv_exit);