Staging: hv: storvsc: Add the contents of hyperv_storage.h to storvsc_drv.c
[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
S
44
45/* to alert the user that structure sizes may be mismatched even though the */
46/* protocol versions match. */
47
48
49#define REVISION_STRING(REVISION_) #REVISION_
50#define FILL_VMSTOR_REVISION(RESULT_LVALUE_) \
51 do { \
52 char *revision_string \
53 = REVISION_STRING($Rev : 6 $) + 6; \
54 RESULT_LVALUE_ = 0; \
55 while (*revision_string >= '0' \
56 && *revision_string <= '9') { \
57 RESULT_LVALUE_ *= 10; \
58 RESULT_LVALUE_ += *revision_string - '0'; \
59 revision_string++; \
60 } \
61 } while (0)
62
63/* Major/minor macros. Minor version is in LSB, meaning that earlier flat */
64/* version numbers will be interpreted as "0.x" (i.e., 1 becomes 0.1). */
65#define VMSTOR_PROTOCOL_MAJOR(VERSION_) (((VERSION_) >> 8) & 0xff)
66#define VMSTOR_PROTOCOL_MINOR(VERSION_) (((VERSION_)) & 0xff)
67#define VMSTOR_PROTOCOL_VERSION(MAJOR_, MINOR_) ((((MAJOR_) & 0xff) << 8) | \
68 (((MINOR_) & 0xff)))
69#define VMSTOR_INVALID_PROTOCOL_VERSION (-1)
70
71/* Version history: */
72/* V1 Beta 0.1 */
73/* V1 RC < 2008/1/31 1.0 */
74/* V1 RC > 2008/1/31 2.0 */
75#define VMSTOR_PROTOCOL_VERSION_CURRENT VMSTOR_PROTOCOL_VERSION(2, 0)
76
77
78
79
80/* This will get replaced with the max transfer length that is possible on */
81/* the host adapter. */
82/* The max transfer length will be published when we offer a vmbus channel. */
83#define MAX_TRANSFER_LENGTH 0x40000
84#define DEFAULT_PACKET_SIZE (sizeof(struct vmdata_gpa_direct) + \
85 sizeof(struct vstor_packet) + \
86 sizesizeof(u64) * (MAX_TRANSFER_LENGTH / PAGE_SIZE)))
87
88
89/* Packet structure describing virtual storage requests. */
90enum vstor_packet_operation {
91 VSTOR_OPERATION_COMPLETE_IO = 1,
92 VSTOR_OPERATION_REMOVE_DEVICE = 2,
93 VSTOR_OPERATION_EXECUTE_SRB = 3,
94 VSTOR_OPERATION_RESET_LUN = 4,
95 VSTOR_OPERATION_RESET_ADAPTER = 5,
96 VSTOR_OPERATION_RESET_BUS = 6,
97 VSTOR_OPERATION_BEGIN_INITIALIZATION = 7,
98 VSTOR_OPERATION_END_INITIALIZATION = 8,
99 VSTOR_OPERATION_QUERY_PROTOCOL_VERSION = 9,
100 VSTOR_OPERATION_QUERY_PROPERTIES = 10,
101 VSTOR_OPERATION_MAXIMUM = 10
102};
103
104/*
105 * Platform neutral description of a scsi request -
106 * this remains the same across the write regardless of 32/64 bit
107 * note: it's patterned off the SCSI_PASS_THROUGH structure
108 */
109#define CDB16GENERIC_LENGTH 0x10
110
111#ifndef SENSE_BUFFER_SIZE
112#define SENSE_BUFFER_SIZE 0x12
113#endif
114
115#define MAX_DATA_BUF_LEN_WITH_PADDING 0x14
116
117struct vmscsi_request {
118 unsigned short length;
119 unsigned char srb_status;
120 unsigned char scsi_status;
121
122 unsigned char port_number;
123 unsigned char path_id;
124 unsigned char target_id;
125 unsigned char lun;
126
127 unsigned char cdb_length;
128 unsigned char sense_info_length;
129 unsigned char data_in;
130 unsigned char reserved;
131
132 unsigned int data_transfer_length;
133
134 union {
135 unsigned char cdb[CDB16GENERIC_LENGTH];
136 unsigned char sense_data[SENSE_BUFFER_SIZE];
137 unsigned char reserved_array[MAX_DATA_BUF_LEN_WITH_PADDING];
138 };
139} __attribute((packed));
140
141
142/*
143 * This structure is sent during the intialization phase to get the different
144 * properties of the channel.
145 */
146struct vmstorage_channel_properties {
147 unsigned short protocol_version;
148 unsigned char path_id;
149 unsigned char target_id;
150
151 /* Note: port number is only really known on the client side */
152 unsigned int port_number;
153 unsigned int flags;
154 unsigned int max_transfer_bytes;
155
156 /* This id is unique for each channel and will correspond with */
157 /* vendor specific data in the inquirydata */
158 unsigned long long unique_id;
159} __packed;
160
161/* This structure is sent during the storage protocol negotiations. */
162struct vmstorage_protocol_version {
163 /* Major (MSW) and minor (LSW) version numbers. */
164 unsigned short major_minor;
165
166 /*
167 * Revision number is auto-incremented whenever this file is changed
168 * (See FILL_VMSTOR_REVISION macro above). Mismatch does not
169 * definitely indicate incompatibility--but it does indicate mismatched
170 * builds.
171 */
172 unsigned short revision;
173} __packed;
174
175/* Channel Property Flags */
176#define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
177#define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
178
179struct vstor_packet {
180 /* Requested operation type */
181 enum vstor_packet_operation operation;
182
183 /* Flags - see below for values */
184 unsigned int flags;
185
186 /* Status of the request returned from the server side. */
187 unsigned int status;
188
189 /* Data payload area */
190 union {
191 /*
192 * Structure used to forward SCSI commands from the
193 * client to the server.
194 */
195 struct vmscsi_request vm_srb;
196
197 /* Structure used to query channel properties. */
198 struct vmstorage_channel_properties storage_channel_properties;
199
200 /* Used during version negotiations. */
201 struct vmstorage_protocol_version version;
202 };
203} __packed;
204
205/* Packet flags */
206/*
207 * This flag indicates that the server should send back a completion for this
208 * packet.
209 */
210#define REQUEST_COMPLETION_FLAG 0x1
211
212/* This is the set of flags that the vsc can set in any packets it sends */
213#define VSC_LEGAL_FLAGS (REQUEST_COMPLETION_FLAG)
214
215
216/* Defines */
217#define STORVSC_RING_BUFFER_SIZE (20*PAGE_SIZE)
218#define BLKVSC_RING_BUFFER_SIZE (20*PAGE_SIZE)
219
220#define STORVSC_MAX_IO_REQUESTS 128
221
222/*
223 * In Hyper-V, each port/path/target maps to 1 scsi host adapter. In
224 * reality, the path/target is not used (ie always set to 0) so our
225 * scsi host adapter essentially has 1 bus with 1 target that contains
226 * up to 256 luns.
227 */
228#define STORVSC_MAX_LUNS_PER_TARGET 64
229#define STORVSC_MAX_TARGETS 1
230#define STORVSC_MAX_CHANNELS 1
231
232struct hv_storvsc_request;
233
234/* Matches Windows-end */
235enum storvsc_request_type {
236 WRITE_TYPE,
237 READ_TYPE,
238 UNKNOWN_TYPE,
239};
240
241
242struct hv_storvsc_request {
243 struct hv_storvsc_request *request;
244 struct hv_device *device;
245
246 /* Synchronize the request/response if needed */
247 struct completion wait_event;
248
249 unsigned char *sense_buffer;
250 void *context;
251 void (*on_io_completion)(struct hv_storvsc_request *request);
252 struct hv_multipage_buffer data_buffer;
253
254 struct vstor_packet vstor_packet;
255};
256
257
258struct storvsc_device_info {
259 u32 ring_buffer_size;
260 unsigned int port_number;
261 unsigned char path_id;
262 unsigned char target_id;
263};
264
265struct storvsc_major_info {
266 int major;
267 int index;
268 bool do_register;
269 char *devname;
270 char *diskname;
271};
272
273/* A storvsc device is a device object that contains a vmbus channel */
274struct storvsc_device {
275 struct hv_device *device;
276
277 bool destroy;
278 bool drain_notify;
279 atomic_t num_outstanding_req;
280
281 wait_queue_head_t waiting_to_drain;
282
283 /*
284 * Each unique Port/Path/Target represents 1 channel ie scsi
285 * controller. In reality, the pathid, targetid is always 0
286 * and the port is set by us
287 */
288 unsigned int port_number;
289 unsigned char path_id;
290 unsigned char target_id;
291
292 /* Used for vsc/vsp channel reset process */
293 struct hv_storvsc_request init_request;
294 struct hv_storvsc_request reset_request;
295};
296
297
298static inline struct storvsc_device *get_out_stor_device(
299 struct hv_device *device)
300{
301 struct storvsc_device *stor_device;
302
303 stor_device = (struct storvsc_device *)device->ext;
304
305 if (stor_device && stor_device->destroy)
306 stor_device = NULL;
307
308 return stor_device;
309}
310
311
312static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
313{
314 dev->drain_notify = true;
315 wait_event(dev->waiting_to_drain,
316 atomic_read(&dev->num_outstanding_req) == 0);
317 dev->drain_notify = false;
318}
bef4a34a 319
8dcf37d4 320
a1be1706 321static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
8dcf37d4 322
a1be1706
S
323module_param(storvsc_ringbuffer_size, int, S_IRUGO);
324MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
8dcf37d4 325
a1be1706
S
326struct hv_host_device {
327 struct hv_device *dev;
328 struct kmem_cache *request_pool;
329 unsigned int port;
330 unsigned char path;
331 unsigned char target;
332};
333
334struct storvsc_cmd_request {
335 struct list_head entry;
336 struct scsi_cmnd *cmd;
337
338 unsigned int bounce_sgl_count;
339 struct scatterlist *bounce_sgl;
340
341 struct hv_storvsc_request request;
342};
8dcf37d4
S
343
344static inline struct storvsc_device *alloc_stor_device(struct hv_device *device)
345{
346 struct storvsc_device *stor_device;
347
348 stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
349 if (!stor_device)
350 return NULL;
351
352 stor_device->destroy = false;
353 init_waitqueue_head(&stor_device->waiting_to_drain);
354 stor_device->device = device;
355 device->ext = stor_device;
356
357 return stor_device;
358}
359
360
361static inline struct storvsc_device *get_in_stor_device(
362 struct hv_device *device)
363{
364 struct storvsc_device *stor_device;
365 unsigned long flags;
366
367 spin_lock_irqsave(&device->channel->inbound_lock, flags);
368 stor_device = (struct storvsc_device *)device->ext;
369
370 if (!stor_device)
371 goto get_in_err;
372
373 /*
374 * If the device is being destroyed; allow incoming
375 * traffic only to cleanup outstanding requests.
376 */
377
378 if (stor_device->destroy &&
379 (atomic_read(&stor_device->num_outstanding_req) == 0))
380 stor_device = NULL;
381
382get_in_err:
383 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
384 return stor_device;
385
386}
387
388static int storvsc_channel_init(struct hv_device *device)
389{
390 struct storvsc_device *stor_device;
391 struct hv_storvsc_request *request;
392 struct vstor_packet *vstor_packet;
393 int ret, t;
394
395 stor_device = get_out_stor_device(device);
396 if (!stor_device)
397 return -ENODEV;
398
399 request = &stor_device->init_request;
400 vstor_packet = &request->vstor_packet;
401
402 /*
403 * Now, initiate the vsc/vsp initialization protocol on the open
404 * channel
405 */
406 memset(request, 0, sizeof(struct hv_storvsc_request));
407 init_completion(&request->wait_event);
408 vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
409 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
410
411 ret = vmbus_sendpacket(device->channel, vstor_packet,
412 sizeof(struct vstor_packet),
413 (unsigned long)request,
414 VM_PKT_DATA_INBAND,
415 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
416 if (ret != 0)
417 goto cleanup;
418
419 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
420 if (t == 0) {
421 ret = -ETIMEDOUT;
422 goto cleanup;
423 }
424
425 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
426 vstor_packet->status != 0)
427 goto cleanup;
428
429
430 /* reuse the packet for version range supported */
431 memset(vstor_packet, 0, sizeof(struct vstor_packet));
432 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
433 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
434
435 vstor_packet->version.major_minor = VMSTOR_PROTOCOL_VERSION_CURRENT;
436 FILL_VMSTOR_REVISION(vstor_packet->version.revision);
437
438 ret = vmbus_sendpacket(device->channel, vstor_packet,
439 sizeof(struct vstor_packet),
440 (unsigned long)request,
441 VM_PKT_DATA_INBAND,
442 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
443 if (ret != 0)
444 goto cleanup;
445
446 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
447 if (t == 0) {
448 ret = -ETIMEDOUT;
449 goto cleanup;
450 }
451
452 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
453 vstor_packet->status != 0)
454 goto cleanup;
455
456
457 memset(vstor_packet, 0, sizeof(struct vstor_packet));
458 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
459 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
460 vstor_packet->storage_channel_properties.port_number =
461 stor_device->port_number;
462
463 ret = vmbus_sendpacket(device->channel, vstor_packet,
464 sizeof(struct vstor_packet),
465 (unsigned long)request,
466 VM_PKT_DATA_INBAND,
467 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
468
469 if (ret != 0)
470 goto cleanup;
471
472 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
473 if (t == 0) {
474 ret = -ETIMEDOUT;
475 goto cleanup;
476 }
477
478 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
479 vstor_packet->status != 0)
480 goto cleanup;
481
482 stor_device->path_id = vstor_packet->storage_channel_properties.path_id;
483 stor_device->target_id
484 = vstor_packet->storage_channel_properties.target_id;
485
486 memset(vstor_packet, 0, sizeof(struct vstor_packet));
487 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
488 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
489
490 ret = vmbus_sendpacket(device->channel, vstor_packet,
491 sizeof(struct vstor_packet),
492 (unsigned long)request,
493 VM_PKT_DATA_INBAND,
494 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
495
496 if (ret != 0)
497 goto cleanup;
498
499 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
500 if (t == 0) {
501 ret = -ETIMEDOUT;
502 goto cleanup;
503 }
504
505 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
506 vstor_packet->status != 0)
507 goto cleanup;
508
509
510cleanup:
511 return ret;
512}
513
514static void storvsc_on_io_completion(struct hv_device *device,
515 struct vstor_packet *vstor_packet,
516 struct hv_storvsc_request *request)
517{
518 struct storvsc_device *stor_device;
519 struct vstor_packet *stor_pkt;
520
521 stor_device = (struct storvsc_device *)device->ext;
522
523 stor_pkt = &request->vstor_packet;
524
525
526 /* Copy over the status...etc */
527 stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
528 stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
529 stor_pkt->vm_srb.sense_info_length =
530 vstor_packet->vm_srb.sense_info_length;
531
532 if (vstor_packet->vm_srb.scsi_status != 0 ||
533 vstor_packet->vm_srb.srb_status != 1){
534 DPRINT_WARN(STORVSC,
535 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
536 stor_pkt->vm_srb.cdb[0],
537 vstor_packet->vm_srb.scsi_status,
538 vstor_packet->vm_srb.srb_status);
539 }
540
541 if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
542 /* CHECK_CONDITION */
543 if (vstor_packet->vm_srb.srb_status & 0x80) {
544 /* autosense data available */
545 DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data "
546 "valid - len %d\n", request,
547 vstor_packet->vm_srb.sense_info_length);
548
549 memcpy(request->sense_buffer,
550 vstor_packet->vm_srb.sense_data,
551 vstor_packet->vm_srb.sense_info_length);
552
553 }
554 }
555
556 stor_pkt->vm_srb.data_transfer_length =
557 vstor_packet->vm_srb.data_transfer_length;
558
559 request->on_io_completion(request);
560
561 if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
562 stor_device->drain_notify)
563 wake_up(&stor_device->waiting_to_drain);
564
565
566}
567
568static void storvsc_on_receive(struct hv_device *device,
569 struct vstor_packet *vstor_packet,
570 struct hv_storvsc_request *request)
571{
572 switch (vstor_packet->operation) {
573 case VSTOR_OPERATION_COMPLETE_IO:
574 storvsc_on_io_completion(device, vstor_packet, request);
575 break;
576 case VSTOR_OPERATION_REMOVE_DEVICE:
577
578 default:
579 break;
580 }
581}
582
583static void storvsc_on_channel_callback(void *context)
584{
585 struct hv_device *device = (struct hv_device *)context;
586 struct storvsc_device *stor_device;
587 u32 bytes_recvd;
588 u64 request_id;
589 unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
590 struct hv_storvsc_request *request;
591 int ret;
592
593
594 stor_device = get_in_stor_device(device);
595 if (!stor_device)
596 return;
597
598 do {
599 ret = vmbus_recvpacket(device->channel, packet,
600 ALIGN(sizeof(struct vstor_packet), 8),
601 &bytes_recvd, &request_id);
602 if (ret == 0 && bytes_recvd > 0) {
603
604 request = (struct hv_storvsc_request *)
605 (unsigned long)request_id;
606
607 if ((request == &stor_device->init_request) ||
608 (request == &stor_device->reset_request)) {
609
610 memcpy(&request->vstor_packet, packet,
611 sizeof(struct vstor_packet));
612 complete(&request->wait_event);
613 } else {
614 storvsc_on_receive(device,
615 (struct vstor_packet *)packet,
616 request);
617 }
618 } else {
619 break;
620 }
621 } while (1);
622
623 return;
624}
625
626static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
627{
628 struct vmstorage_channel_properties props;
629 int ret;
630
631 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
632
633 /* Open the channel */
634 ret = vmbus_open(device->channel,
635 ring_size,
636 ring_size,
637 (void *)&props,
638 sizeof(struct vmstorage_channel_properties),
639 storvsc_on_channel_callback, device);
640
641 if (ret != 0)
642 return ret;
643
644 ret = storvsc_channel_init(device);
645
646 return ret;
647}
648
649int storvsc_dev_add(struct hv_device *device,
650 void *additional_info)
651{
652 struct storvsc_device *stor_device;
653 struct storvsc_device_info *device_info;
654 int ret = 0;
655
656 device_info = (struct storvsc_device_info *)additional_info;
657 stor_device = alloc_stor_device(device);
658 if (!stor_device)
659 return -ENOMEM;
660
661 /* Save the channel properties to our storvsc channel */
662
663 /*
664 * If we support more than 1 scsi channel, we need to set the
665 * port number here to the scsi channel but how do we get the
666 * scsi channel prior to the bus scan.
667 *
668 * The host does not support this.
669 */
670
671 stor_device->port_number = device_info->port_number;
672 /* Send it back up */
673 ret = storvsc_connect_to_vsp(device, device_info->ring_buffer_size);
674 if (ret) {
675 kfree(stor_device);
676 return ret;
677 }
678 device_info->path_id = stor_device->path_id;
679 device_info->target_id = stor_device->target_id;
680
681 return ret;
682}
683
684int storvsc_dev_remove(struct hv_device *device)
685{
686 struct storvsc_device *stor_device;
687 unsigned long flags;
688
689 stor_device = (struct storvsc_device *)device->ext;
690
691 spin_lock_irqsave(&device->channel->inbound_lock, flags);
692 stor_device->destroy = true;
693 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
694
695 /*
696 * At this point, all outbound traffic should be disable. We
697 * only allow inbound traffic (responses) to proceed so that
698 * outstanding requests can be completed.
699 */
700
701 storvsc_wait_to_drain(stor_device);
702
703 /*
704 * Since we have already drained, we don't need to busy wait
705 * as was done in final_release_stor_device()
706 * Note that we cannot set the ext pointer to NULL until
707 * we have drained - to drain the outgoing packets, we need to
708 * allow incoming packets.
709 */
710 spin_lock_irqsave(&device->channel->inbound_lock, flags);
711 device->ext = NULL;
712 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
713
714 /* Close the channel */
715 vmbus_close(device->channel);
716
717 kfree(stor_device);
718 return 0;
719}
720
721int storvsc_do_io(struct hv_device *device,
722 struct hv_storvsc_request *request)
723{
724 struct storvsc_device *stor_device;
725 struct vstor_packet *vstor_packet;
726 int ret = 0;
727
728 vstor_packet = &request->vstor_packet;
729 stor_device = get_out_stor_device(device);
730
731 if (!stor_device)
732 return -ENODEV;
733
734
735 request->device = device;
736
737
738 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
739
740 vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
741
742
743 vstor_packet->vm_srb.sense_info_length = SENSE_BUFFER_SIZE;
744
745
746 vstor_packet->vm_srb.data_transfer_length =
747 request->data_buffer.len;
748
749 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
750
751 if (request->data_buffer.len) {
752 ret = vmbus_sendpacket_multipagebuffer(device->channel,
753 &request->data_buffer,
754 vstor_packet,
755 sizeof(struct vstor_packet),
756 (unsigned long)request);
757 } else {
758 ret = vmbus_sendpacket(device->channel, vstor_packet,
759 sizeof(struct vstor_packet),
760 (unsigned long)request,
761 VM_PKT_DATA_INBAND,
762 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
763 }
764
765 if (ret != 0)
766 return ret;
767
768 atomic_inc(&stor_device->num_outstanding_req);
769
770 return ret;
771}
772
bd1f5d6a
S
773static void storvsc_get_ide_info(struct hv_device *dev, int *target, int *path)
774{
775 *target =
776 dev->dev_instance.b[5] << 8 | dev->dev_instance.b[4];
777
778 *path =
779 dev->dev_instance.b[3] << 24 |
780 dev->dev_instance.b[2] << 16 |
781 dev->dev_instance.b[1] << 8 | dev->dev_instance.b[0];
782}
783
bef4a34a 784
5b60acee
S
785static int storvsc_device_alloc(struct scsi_device *sdevice)
786{
787 /*
788 * This enables luns to be located sparsely. Otherwise, we may not
789 * discovered them.
790 */
791 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
792 return 0;
793}
794
a1ebfeae
S
795static int storvsc_merge_bvec(struct request_queue *q,
796 struct bvec_merge_data *bmd, struct bio_vec *bvec)
797{
798 /* checking done by caller. */
799 return bvec->bv_len;
800}
801
419f2d03
S
802static int storvsc_device_configure(struct scsi_device *sdevice)
803{
804 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
805 STORVSC_MAX_IO_REQUESTS);
806
419f2d03
S
807 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
808
419f2d03
S
809 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
810
811 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
419f2d03
S
812
813 return 0;
814}
815
49a3c7a9
S
816static void destroy_bounce_buffer(struct scatterlist *sgl,
817 unsigned int sg_count)
818{
819 int i;
820 struct page *page_buf;
821
822 for (i = 0; i < sg_count; i++) {
823 page_buf = sg_page((&sgl[i]));
824 if (page_buf != NULL)
825 __free_page(page_buf);
826 }
827
828 kfree(sgl);
829}
830
3862ef3e
S
831static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
832{
833 int i;
834
835 /* No need to check */
836 if (sg_count < 2)
837 return -1;
838
839 /* We have at least 2 sg entries */
840 for (i = 0; i < sg_count; i++) {
841 if (i == 0) {
842 /* make sure 1st one does not have hole */
843 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
844 return i;
845 } else if (i == sg_count - 1) {
846 /* make sure last one does not have hole */
847 if (sgl[i].offset != 0)
848 return i;
849 } else {
850 /* make sure no hole in the middle */
851 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
852 return i;
853 }
854 }
855 return -1;
856}
857
a9753cbd
S
858static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
859 unsigned int sg_count,
860 unsigned int len)
861{
862 int i;
863 int num_pages;
864 struct scatterlist *bounce_sgl;
865 struct page *page_buf;
866
867 num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
868
869 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
870 if (!bounce_sgl)
871 return NULL;
872
873 for (i = 0; i < num_pages; i++) {
874 page_buf = alloc_page(GFP_ATOMIC);
875 if (!page_buf)
876 goto cleanup;
877 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
878 }
879
880 return bounce_sgl;
881
882cleanup:
883 destroy_bounce_buffer(bounce_sgl, num_pages);
884 return NULL;
885}
886
29fe2c9b
S
887
888/* Assume the original sgl has enough room */
889static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
890 struct scatterlist *bounce_sgl,
891 unsigned int orig_sgl_count)
892{
893 int i;
894 int j = 0;
895 unsigned long src, dest;
896 unsigned int srclen, destlen, copylen;
897 unsigned int total_copied = 0;
898 unsigned long bounce_addr = 0;
899 unsigned long dest_addr = 0;
900 unsigned long flags;
901
902 local_irq_save(flags);
903
904 for (i = 0; i < orig_sgl_count; i++) {
905 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
906 KM_IRQ0) + orig_sgl[i].offset;
907 dest = dest_addr;
908 destlen = orig_sgl[i].length;
909
910 if (bounce_addr == 0)
911 bounce_addr =
912 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
913 KM_IRQ0);
914
915 while (destlen) {
916 src = bounce_addr + bounce_sgl[j].offset;
917 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
918
919 copylen = min(srclen, destlen);
920 memcpy((void *)dest, (void *)src, copylen);
921
922 total_copied += copylen;
923 bounce_sgl[j].offset += copylen;
924 destlen -= copylen;
925 dest += copylen;
926
927 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
928 /* full */
929 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
930 j++;
931
932 /* if we need to use another bounce buffer */
933 if (destlen || i != orig_sgl_count - 1)
934 bounce_addr =
935 (unsigned long)kmap_atomic(
936 sg_page((&bounce_sgl[j])), KM_IRQ0);
937 } else if (destlen == 0 && i == orig_sgl_count - 1) {
938 /* unmap the last bounce that is < PAGE_SIZE */
939 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
940 }
941 }
942
943 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
944 KM_IRQ0);
945 }
946
947 local_irq_restore(flags);
948
949 return total_copied;
950}
951
6a8ff44b
S
952
953/* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
954static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
955 struct scatterlist *bounce_sgl,
956 unsigned int orig_sgl_count)
957{
958 int i;
959 int j = 0;
960 unsigned long src, dest;
961 unsigned int srclen, destlen, copylen;
962 unsigned int total_copied = 0;
963 unsigned long bounce_addr = 0;
964 unsigned long src_addr = 0;
965 unsigned long flags;
966
967 local_irq_save(flags);
968
969 for (i = 0; i < orig_sgl_count; i++) {
970 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
971 KM_IRQ0) + orig_sgl[i].offset;
972 src = src_addr;
973 srclen = orig_sgl[i].length;
974
975 if (bounce_addr == 0)
976 bounce_addr =
977 (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
978 KM_IRQ0);
979
980 while (srclen) {
981 /* assume bounce offset always == 0 */
982 dest = bounce_addr + bounce_sgl[j].length;
983 destlen = PAGE_SIZE - bounce_sgl[j].length;
984
985 copylen = min(srclen, destlen);
986 memcpy((void *)dest, (void *)src, copylen);
987
988 total_copied += copylen;
989 bounce_sgl[j].length += copylen;
990 srclen -= copylen;
991 src += copylen;
992
993 if (bounce_sgl[j].length == PAGE_SIZE) {
994 /* full..move to next entry */
995 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
996 j++;
997
998 /* if we need to use another bounce buffer */
999 if (srclen || i != orig_sgl_count - 1)
1000 bounce_addr =
1001 (unsigned long)kmap_atomic(
1002 sg_page((&bounce_sgl[j])), KM_IRQ0);
1003
1004 } else if (srclen == 0 && i == orig_sgl_count - 1) {
1005 /* unmap the last bounce that is < PAGE_SIZE */
1006 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
1007 }
1008 }
1009
1010 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
1011 }
1012
1013 local_irq_restore(flags);
1014
1015 return total_copied;
1016}
1017
5c5c0234 1018
5c5c0234
S
1019static int storvsc_remove(struct hv_device *dev)
1020{
5c5c0234
S
1021 struct Scsi_Host *host = dev_get_drvdata(&dev->device);
1022 struct hv_host_device *host_dev =
1023 (struct hv_host_device *)host->hostdata;
1024
2935a407
S
1025 scsi_remove_host(host);
1026
2935a407 1027 scsi_host_put(host);
d36b0a03 1028
6cdc57c0 1029 storvsc_dev_remove(dev);
5c5c0234
S
1030 if (host_dev->request_pool) {
1031 kmem_cache_destroy(host_dev->request_pool);
1032 host_dev->request_pool = NULL;
1033 }
5c5c0234
S
1034 return 0;
1035}
1036
62838ce2
S
1037
1038static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
1039 sector_t capacity, int *info)
1040{
5326fd5c
S
1041 sector_t nsect = capacity;
1042 sector_t cylinders = nsect;
1043 int heads, sectors_pt;
62838ce2 1044
5326fd5c
S
1045 /*
1046 * We are making up these values; let us keep it simple.
1047 */
1048 heads = 0xff;
1049 sectors_pt = 0x3f; /* Sectors per track */
1050 sector_div(cylinders, heads * sectors_pt);
1051 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
1052 cylinders = 0xffff;
62838ce2
S
1053
1054 info[0] = heads;
5326fd5c
S
1055 info[1] = sectors_pt;
1056 info[2] = (int)cylinders;
62838ce2 1057
62838ce2
S
1058 return 0;
1059}
aa3d789e
S
1060
1061static int storvsc_host_reset(struct hv_device *device)
1062{
1063 struct storvsc_device *stor_device;
1064 struct hv_storvsc_request *request;
1065 struct vstor_packet *vstor_packet;
1066 int ret, t;
1067
aa3d789e 1068
1eaaddf9 1069 stor_device = get_out_stor_device(device);
aa3d789e 1070 if (!stor_device)
f3b74165 1071 return -ENODEV;
aa3d789e
S
1072
1073 request = &stor_device->reset_request;
1074 vstor_packet = &request->vstor_packet;
1075
1076 init_completion(&request->wait_event);
1077
1078 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
1079 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
1080 vstor_packet->vm_srb.path_id = stor_device->path_id;
1081
1082 ret = vmbus_sendpacket(device->channel, vstor_packet,
1083 sizeof(struct vstor_packet),
1084 (unsigned long)&stor_device->reset_request,
1085 VM_PKT_DATA_INBAND,
1086 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1087 if (ret != 0)
1088 goto cleanup;
1089
46d2eb6d 1090 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
aa3d789e
S
1091 if (t == 0) {
1092 ret = -ETIMEDOUT;
1093 goto cleanup;
1094 }
1095
aa3d789e
S
1096
1097 /*
1098 * At this point, all outstanding requests in the adapter
1099 * should have been flushed out and return to us
1100 */
1101
1102cleanup:
aa3d789e
S
1103 return ret;
1104}
1105
1106
96e690be
S
1107/*
1108 * storvsc_host_reset_handler - Reset the scsi HBA
1109 */
1110static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
1111{
1112 int ret;
1113 struct hv_host_device *host_dev =
1114 (struct hv_host_device *)scmnd->device->host->hostdata;
1115 struct hv_device *dev = host_dev->dev;
1116
96e690be
S
1117 ret = storvsc_host_reset(dev);
1118 if (ret != 0)
1119 return ret;
1120
96e690be
S
1121 return ret;
1122}
1123
8a411bad
S
1124
1125/*
1126 * storvsc_commmand_completion - Command completion processing
1127 */
1128static void storvsc_commmand_completion(struct hv_storvsc_request *request)
1129{
1130 struct storvsc_cmd_request *cmd_request =
1131 (struct storvsc_cmd_request *)request->context;
1132 struct scsi_cmnd *scmnd = cmd_request->cmd;
1133 struct hv_host_device *host_dev =
1134 (struct hv_host_device *)scmnd->device->host->hostdata;
1135 void (*scsi_done_fn)(struct scsi_cmnd *);
1136 struct scsi_sense_hdr sense_hdr;
1137 struct vmscsi_request *vm_srb;
1138
3612ef99 1139 vm_srb = &request->vstor_packet.vm_srb;
8a411bad 1140 if (cmd_request->bounce_sgl_count) {
3612ef99
S
1141 if (vm_srb->data_in == READ_TYPE) {
1142 copy_from_bounce_buffer(scsi_sglist(scmnd),
8a411bad
S
1143 cmd_request->bounce_sgl,
1144 scsi_sg_count(scmnd));
3612ef99
S
1145 destroy_bounce_buffer(cmd_request->bounce_sgl,
1146 cmd_request->bounce_sgl_count);
1147 }
8a411bad
S
1148 }
1149
8a411bad
S
1150 scmnd->result = vm_srb->scsi_status;
1151
1152 if (scmnd->result) {
1153 if (scsi_normalize_sense(scmnd->sense_buffer,
1154 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
1155 scsi_print_sense_hdr("storvsc", &sense_hdr);
1156 }
1157
8a411bad
S
1158 scsi_set_resid(scmnd,
1159 request->data_buffer.len -
1160 vm_srb->data_transfer_length);
1161
1162 scsi_done_fn = scmnd->scsi_done;
1163
1164 scmnd->host_scribble = NULL;
1165 scmnd->scsi_done = NULL;
1166
8a411bad
S
1167 scsi_done_fn(scmnd);
1168
1169 kmem_cache_free(host_dev->request_pool, cmd_request);
1170}
1171
c5b463ae
S
1172
1173/*
1174 * storvsc_queuecommand - Initiate command processing
1175 */
1176static int storvsc_queuecommand_lck(struct scsi_cmnd *scmnd,
1177 void (*done)(struct scsi_cmnd *))
1178{
1179 int ret;
1180 struct hv_host_device *host_dev =
1181 (struct hv_host_device *)scmnd->device->host->hostdata;
1182 struct hv_device *dev = host_dev->dev;
c5b463ae
S
1183 struct hv_storvsc_request *request;
1184 struct storvsc_cmd_request *cmd_request;
1185 unsigned int request_size = 0;
1186 int i;
1187 struct scatterlist *sgl;
1188 unsigned int sg_count = 0;
1189 struct vmscsi_request *vm_srb;
1190
1191
1192 /* If retrying, no need to prep the cmd */
1193 if (scmnd->host_scribble) {
c5b463ae
S
1194
1195 cmd_request =
1196 (struct storvsc_cmd_request *)scmnd->host_scribble;
c5b463ae
S
1197
1198 goto retry_request;
1199 }
1200
c5b463ae
S
1201 scmnd->scsi_done = done;
1202
1203 request_size = sizeof(struct storvsc_cmd_request);
1204
1205 cmd_request = kmem_cache_zalloc(host_dev->request_pool,
1206 GFP_ATOMIC);
1207 if (!cmd_request) {
1208 scmnd->scsi_done = NULL;
1209 return SCSI_MLQUEUE_DEVICE_BUSY;
1210 }
1211
1212 /* Setup the cmd request */
1213 cmd_request->bounce_sgl_count = 0;
1214 cmd_request->bounce_sgl = NULL;
1215 cmd_request->cmd = scmnd;
1216
1217 scmnd->host_scribble = (unsigned char *)cmd_request;
1218
1219 request = &cmd_request->request;
1220 vm_srb = &request->vstor_packet.vm_srb;
1221
1222
1223 /* Build the SRB */
1224 switch (scmnd->sc_data_direction) {
1225 case DMA_TO_DEVICE:
1226 vm_srb->data_in = WRITE_TYPE;
1227 break;
1228 case DMA_FROM_DEVICE:
1229 vm_srb->data_in = READ_TYPE;
1230 break;
1231 default:
1232 vm_srb->data_in = UNKNOWN_TYPE;
1233 break;
1234 }
1235
1236 request->on_io_completion = storvsc_commmand_completion;
1237 request->context = cmd_request;/* scmnd; */
1238
c5b463ae
S
1239 vm_srb->port_number = host_dev->port;
1240 vm_srb->path_id = scmnd->device->channel;
1241 vm_srb->target_id = scmnd->device->id;
1242 vm_srb->lun = scmnd->device->lun;
1243
c5b463ae
S
1244 vm_srb->cdb_length = scmnd->cmd_len;
1245
1246 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
1247
1248 request->sense_buffer = scmnd->sense_buffer;
1249
1250
1251 request->data_buffer.len = scsi_bufflen(scmnd);
1252 if (scsi_sg_count(scmnd)) {
1253 sgl = (struct scatterlist *)scsi_sglist(scmnd);
1254 sg_count = scsi_sg_count(scmnd);
1255
1256 /* check if we need to bounce the sgl */
1257 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
1258 cmd_request->bounce_sgl =
1259 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
1260 scsi_bufflen(scmnd));
1261 if (!cmd_request->bounce_sgl) {
1262 scmnd->scsi_done = NULL;
1263 scmnd->host_scribble = NULL;
1264 kmem_cache_free(host_dev->request_pool,
1265 cmd_request);
1266
1267 return SCSI_MLQUEUE_HOST_BUSY;
1268 }
1269
1270 cmd_request->bounce_sgl_count =
1271 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
1272 PAGE_SHIFT;
1273
fa23b8c7
S
1274 if (vm_srb->data_in == WRITE_TYPE)
1275 copy_to_bounce_buffer(sgl,
1276 cmd_request->bounce_sgl,
1277 scsi_sg_count(scmnd));
c5b463ae
S
1278
1279 sgl = cmd_request->bounce_sgl;
1280 sg_count = cmd_request->bounce_sgl_count;
1281 }
1282
1283 request->data_buffer.offset = sgl[0].offset;
1284
1285 for (i = 0; i < sg_count; i++)
1286 request->data_buffer.pfn_array[i] =
1287 page_to_pfn(sg_page((&sgl[i])));
1288
1289 } else if (scsi_sglist(scmnd)) {
c5b463ae
S
1290 request->data_buffer.offset =
1291 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
1292 request->data_buffer.pfn_array[0] =
1293 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
1294 }
1295
1296retry_request:
1297 /* Invokes the vsc to start an IO */
636f0fd1
S
1298 ret = storvsc_do_io(dev, &cmd_request->request);
1299
d2598f01 1300 if (ret == -EAGAIN) {
c5b463ae
S
1301 /* no more space */
1302
1303 if (cmd_request->bounce_sgl_count) {
1304 /*
1305 * FIXME: We can optimize on writes by just skipping
1306 * this
1307 */
1308 copy_from_bounce_buffer(scsi_sglist(scmnd),
1309 cmd_request->bounce_sgl,
1310 scsi_sg_count(scmnd));
1311 destroy_bounce_buffer(cmd_request->bounce_sgl,
1312 cmd_request->bounce_sgl_count);
1313 }
1314
1315 kmem_cache_free(host_dev->request_pool, cmd_request);
1316
1317 scmnd->scsi_done = NULL;
1318 scmnd->host_scribble = NULL;
1319
1320 ret = SCSI_MLQUEUE_DEVICE_BUSY;
1321 }
1322
1323 return ret;
1324}
1325
1326static DEF_SCSI_QCMD(storvsc_queuecommand)
1327
bef4a34a 1328
454f18a9 1329/* Scsi driver */
bef4a34a 1330static struct scsi_host_template scsi_driver = {
ff568d3a
GKH
1331 .module = THIS_MODULE,
1332 .name = "storvsc_host_t",
1333 .bios_param = storvsc_get_chs,
1334 .queuecommand = storvsc_queuecommand,
1335 .eh_host_reset_handler = storvsc_host_reset_handler,
1336 .slave_alloc = storvsc_device_alloc,
1337 .slave_configure = storvsc_device_configure,
1338 .cmd_per_lun = 1,
1339 /* 64 max_queue * 1 target */
0686e4f4 1340 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
ff568d3a 1341 .this_id = -1,
454f18a9 1342 /* no use setting to 0 since ll_blk_rw reset it to 1 */
ff568d3a
GKH
1343 /* currently 32 */
1344 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
1345 /*
1346 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
1347 * into 1 sg element. If set, we must limit the max_segment_size to
1348 * PAGE_SIZE, otherwise we may get 1 sg element that represents
1349 * multiple
1350 */
454f18a9 1351 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
ff568d3a 1352 .use_clustering = ENABLE_CLUSTERING,
454f18a9 1353 /* Make sure we dont get a sg segment crosses a page boundary */
ff568d3a 1354 .dma_boundary = PAGE_SIZE-1,
bef4a34a
HJ
1355};
1356
21e37742
S
1357/*
1358 * The storvsc_probe function assumes that the IDE guid
1359 * is the second entry.
1360 */
d847b5fe 1361static const struct hv_vmbus_device_id id_table[] = {
c45cf2d4
GKH
1362 /* SCSI guid */
1363 { VMBUS_DEVICE(0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
1364 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f) },
21e37742
S
1365 /* IDE guid */
1366 { VMBUS_DEVICE(0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
1367 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5) },
c45cf2d4 1368 { },
d847b5fe 1369};
bef4a34a 1370
d847b5fe 1371MODULE_DEVICE_TABLE(vmbus, id_table);
bd1f5d6a 1372
bd1f5d6a 1373
3e189519 1374/*
ff568d3a
GKH
1375 * storvsc_probe - Add a new device for this driver
1376 */
f5c78872 1377
9efd21e1 1378static int storvsc_probe(struct hv_device *device)
bef4a34a 1379{
ff568d3a 1380 int ret;
bef4a34a 1381 struct Scsi_Host *host;
795b613d 1382 struct hv_host_device *host_dev;
9f0c7d2c 1383 struct storvsc_device_info device_info;
bd1f5d6a
S
1384 bool dev_is_ide;
1385 int path = 0;
1386 int target = 0;
1387
21e37742 1388 if (!memcmp(&device->dev_type.b, id_table[1].guid, sizeof(uuid_le)))
bd1f5d6a
S
1389 dev_is_ide = true;
1390 else
1391 dev_is_ide = false;
bef4a34a 1392
ff568d3a 1393 host = scsi_host_alloc(&scsi_driver,
972621c9 1394 sizeof(struct hv_host_device));
f8feed06 1395 if (!host)
bef4a34a 1396 return -ENOMEM;
bef4a34a 1397
9efd21e1 1398 dev_set_drvdata(&device->device, host);
bef4a34a 1399
795b613d
S
1400 host_dev = (struct hv_host_device *)host->hostdata;
1401 memset(host_dev, 0, sizeof(struct hv_host_device));
bef4a34a 1402
795b613d 1403 host_dev->port = host->host_no;
97c15296 1404 host_dev->dev = device;
bef4a34a 1405
795b613d 1406 host_dev->request_pool =
9efd21e1 1407 kmem_cache_create(dev_name(&device->device),
1e05d88e 1408 sizeof(struct storvsc_cmd_request), 0,
ff568d3a
GKH
1409 SLAB_HWCACHE_ALIGN, NULL);
1410
795b613d 1411 if (!host_dev->request_pool) {
bef4a34a 1412 scsi_host_put(host);
bef4a34a
HJ
1413 return -ENOMEM;
1414 }
1415
8a046024 1416 device_info.port_number = host->host_no;
fa4d123a 1417 device_info.ring_buffer_size = storvsc_ringbuffer_size;
454f18a9 1418 /* Call to the vsc driver to add the device */
58f1f5cb 1419 ret = storvsc_dev_add(device, (void *)&device_info);
9efd21e1 1420
ff568d3a 1421 if (ret != 0) {
795b613d 1422 kmem_cache_destroy(host_dev->request_pool);
bef4a34a 1423 scsi_host_put(host);
8e854680 1424 return -ENODEV;
bef4a34a
HJ
1425 }
1426
bd1f5d6a
S
1427 if (dev_is_ide)
1428 storvsc_get_ide_info(device, &target, &path);
1429
795b613d
S
1430 host_dev->path = device_info.path_id;
1431 host_dev->target = device_info.target_id;
bef4a34a 1432
ff568d3a
GKH
1433 /* max # of devices per target */
1434 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
1435 /* max # of targets per channel */
1436 host->max_id = STORVSC_MAX_TARGETS;
1437 /* max # of channels */
1438 host->max_channel = STORVSC_MAX_CHANNELS - 1;
bef4a34a 1439
454f18a9 1440 /* Register the HBA and start the scsi bus scan */
9efd21e1 1441 ret = scsi_add_host(host, &device->device);
bd1f5d6a
S
1442 if (ret != 0)
1443 goto err_out;
bef4a34a 1444
bd1f5d6a
S
1445 if (!dev_is_ide) {
1446 scsi_scan_host(host);
1447 return 0;
1448 }
1449 ret = scsi_add_device(host, 0, target, 0);
1450 if (ret) {
1451 scsi_remove_host(host);
1452 goto err_out;
bef4a34a 1453 }
bd1f5d6a 1454 return 0;
bef4a34a 1455
bd1f5d6a
S
1456err_out:
1457 storvsc_dev_remove(device);
1458 kmem_cache_destroy(host_dev->request_pool);
1459 scsi_host_put(host);
1460 return -ENODEV;
bef4a34a
HJ
1461}
1462
7bd05b91
S
1463/* The one and only one */
1464
40bf63ed 1465static struct hv_driver storvsc_drv = {
768fa219 1466 .name = "storvsc",
d847b5fe 1467 .id_table = id_table,
40bf63ed
S
1468 .probe = storvsc_probe,
1469 .remove = storvsc_remove,
39ae6fae 1470};
7bd05b91 1471
d9bbae83 1472static int __init storvsc_drv_init(void)
f5c78872 1473{
01415ab3
S
1474 u32 max_outstanding_req_per_channel;
1475
1476 /*
1477 * Divide the ring buffer data size (which is 1 page less
1478 * than the ring buffer size since that page is reserved for
1479 * the ring buffer indices) by the max request size (which is
1480 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
1481 */
01415ab3 1482 max_outstanding_req_per_channel =
768fa219
GKH
1483 ((storvsc_ringbuffer_size - PAGE_SIZE) /
1484 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
1485 sizeof(struct vstor_packet) + sizeof(u64),
1486 sizeof(u64)));
f5c78872 1487
01415ab3 1488 if (max_outstanding_req_per_channel <
f5c78872 1489 STORVSC_MAX_IO_REQUESTS)
b06efc19 1490 return -EINVAL;
f5c78872 1491
768fa219 1492 return vmbus_driver_register(&storvsc_drv);
f5c78872
S
1493}
1494
c63ba9e1 1495static void __exit storvsc_drv_exit(void)
f5c78872 1496{
768fa219 1497 vmbus_driver_unregister(&storvsc_drv);
f5c78872
S
1498}
1499
ff568d3a 1500MODULE_LICENSE("GPL");
26c14cc1 1501MODULE_VERSION(HV_DRV_VERSION);
3afc7cc3 1502MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
d9bbae83 1503module_init(storvsc_drv_init);
c63ba9e1 1504module_exit(storvsc_drv_exit);