Staging: hv: storvsc: Cleanup storvsc_drv.c after adding the contents of hyperv_storage.h
[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
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
c1b3d067 639static int storvsc_dev_add(struct hv_device *device,
8dcf37d4
S
640 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
c1b3d067 674static int storvsc_dev_remove(struct hv_device *device)
8dcf37d4
S
675{
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
c1b3d067 711static int storvsc_do_io(struct hv_device *device,
8dcf37d4
S
712 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
bd1f5d6a
S
763static 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
bef4a34a 774
5b60acee
S
775static 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
a1ebfeae
S
785static 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
419f2d03
S
792static int storvsc_device_configure(struct scsi_device *sdevice)
793{
794 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
795 STORVSC_MAX_IO_REQUESTS);
796
419f2d03
S
797 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
798
419f2d03
S
799 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
800
801 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
419f2d03
S
802
803 return 0;
804}
805
49a3c7a9
S
806static 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
3862ef3e
S
821static 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
a9753cbd
S
848static 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
29fe2c9b
S
877
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
6a8ff44b
S
942
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
5c5c0234 1008
5c5c0234
S
1009static int storvsc_remove(struct hv_device *dev)
1010{
5c5c0234
S
1011 struct Scsi_Host *host = dev_get_drvdata(&dev->device);
1012 struct hv_host_device *host_dev =
1013 (struct hv_host_device *)host->hostdata;
1014
2935a407
S
1015 scsi_remove_host(host);
1016
2935a407 1017 scsi_host_put(host);
d36b0a03 1018
6cdc57c0 1019 storvsc_dev_remove(dev);
5c5c0234
S
1020 if (host_dev->request_pool) {
1021 kmem_cache_destroy(host_dev->request_pool);
1022 host_dev->request_pool = NULL;
1023 }
5c5c0234
S
1024 return 0;
1025}
1026
62838ce2
S
1027
1028static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
1029 sector_t capacity, int *info)
1030{
5326fd5c
S
1031 sector_t nsect = capacity;
1032 sector_t cylinders = nsect;
1033 int heads, sectors_pt;
62838ce2 1034
5326fd5c
S
1035 /*
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;
62838ce2
S
1043
1044 info[0] = heads;
5326fd5c
S
1045 info[1] = sectors_pt;
1046 info[2] = (int)cylinders;
62838ce2 1047
62838ce2
S
1048 return 0;
1049}
aa3d789e
S
1050
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
aa3d789e 1058
1eaaddf9 1059 stor_device = get_out_stor_device(device);
aa3d789e 1060 if (!stor_device)
f3b74165 1061 return -ENODEV;
aa3d789e
S
1062
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
46d2eb6d 1080 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
aa3d789e
S
1081 if (t == 0) {
1082 ret = -ETIMEDOUT;
1083 goto cleanup;
1084 }
1085
aa3d789e
S
1086
1087 /*
1088 * At this point, all outstanding requests in the adapter
1089 * should have been flushed out and return to us
1090 */
1091
1092cleanup:
aa3d789e
S
1093 return ret;
1094}
1095
1096
96e690be
S
1097/*
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
96e690be
S
1107 ret = storvsc_host_reset(dev);
1108 if (ret != 0)
1109 return ret;
1110
96e690be
S
1111 return ret;
1112}
1113
8a411bad
S
1114
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
3612ef99 1129 vm_srb = &request->vstor_packet.vm_srb;
8a411bad 1130 if (cmd_request->bounce_sgl_count) {
3612ef99
S
1131 if (vm_srb->data_in == READ_TYPE) {
1132 copy_from_bounce_buffer(scsi_sglist(scmnd),
8a411bad
S
1133 cmd_request->bounce_sgl,
1134 scsi_sg_count(scmnd));
3612ef99
S
1135 destroy_bounce_buffer(cmd_request->bounce_sgl,
1136 cmd_request->bounce_sgl_count);
1137 }
8a411bad
S
1138 }
1139
8a411bad
S
1140 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
8a411bad
S
1148 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
8a411bad
S
1157 scsi_done_fn(scmnd);
1158
1159 kmem_cache_free(host_dev->request_pool, cmd_request);
1160}
1161
c5b463ae
S
1162
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;
c5b463ae
S
1173 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) {
c5b463ae
S
1184
1185 cmd_request =
1186 (struct storvsc_cmd_request *)scmnd->host_scribble;
c5b463ae
S
1187
1188 goto retry_request;
1189 }
1190
c5b463ae
S
1191 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
c5b463ae
S
1229 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
c5b463ae
S
1234 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
fa23b8c7
S
1264 if (vm_srb->data_in == WRITE_TYPE)
1265 copy_to_bounce_buffer(sgl,
1266 cmd_request->bounce_sgl,
1267 scsi_sg_count(scmnd));
c5b463ae
S
1268
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)) {
c5b463ae
S
1280 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 */
636f0fd1
S
1288 ret = storvsc_do_io(dev, &cmd_request->request);
1289
d2598f01 1290 if (ret == -EAGAIN) {
c5b463ae
S
1291 /* 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
bef4a34a 1318
454f18a9 1319/* Scsi driver */
bef4a34a 1320static struct scsi_host_template scsi_driver = {
ff568d3a
GKH
1321 .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 */
0686e4f4 1330 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
ff568d3a 1331 .this_id = -1,
454f18a9 1332 /* no use setting to 0 since ll_blk_rw reset it to 1 */
ff568d3a
GKH
1333 /* 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 */
454f18a9 1341 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
ff568d3a 1342 .use_clustering = ENABLE_CLUSTERING,
454f18a9 1343 /* Make sure we dont get a sg segment crosses a page boundary */
ff568d3a 1344 .dma_boundary = PAGE_SIZE-1,
bef4a34a
HJ
1345};
1346
21e37742
S
1347/*
1348 * The storvsc_probe function assumes that the IDE guid
1349 * is the second entry.
1350 */
d847b5fe 1351static const struct hv_vmbus_device_id id_table[] = {
c45cf2d4
GKH
1352 /* SCSI guid */
1353 { VMBUS_DEVICE(0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
1354 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f) },
21e37742
S
1355 /* IDE guid */
1356 { VMBUS_DEVICE(0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
1357 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5) },
c45cf2d4 1358 { },
d847b5fe 1359};
bef4a34a 1360
d847b5fe 1361MODULE_DEVICE_TABLE(vmbus, id_table);
bd1f5d6a 1362
bd1f5d6a 1363
3e189519 1364/*
ff568d3a
GKH
1365 * storvsc_probe - Add a new device for this driver
1366 */
f5c78872 1367
9efd21e1 1368static int storvsc_probe(struct hv_device *device)
bef4a34a 1369{
ff568d3a 1370 int ret;
bef4a34a 1371 struct Scsi_Host *host;
795b613d 1372 struct hv_host_device *host_dev;
9f0c7d2c 1373 struct storvsc_device_info device_info;
bd1f5d6a
S
1374 bool dev_is_ide;
1375 int path = 0;
1376 int target = 0;
1377
21e37742 1378 if (!memcmp(&device->dev_type.b, id_table[1].guid, sizeof(uuid_le)))
bd1f5d6a
S
1379 dev_is_ide = true;
1380 else
1381 dev_is_ide = false;
bef4a34a 1382
ff568d3a 1383 host = scsi_host_alloc(&scsi_driver,
972621c9 1384 sizeof(struct hv_host_device));
f8feed06 1385 if (!host)
bef4a34a 1386 return -ENOMEM;
bef4a34a 1387
9efd21e1 1388 dev_set_drvdata(&device->device, host);
bef4a34a 1389
795b613d
S
1390 host_dev = (struct hv_host_device *)host->hostdata;
1391 memset(host_dev, 0, sizeof(struct hv_host_device));
bef4a34a 1392
795b613d 1393 host_dev->port = host->host_no;
97c15296 1394 host_dev->dev = device;
bef4a34a 1395
795b613d 1396 host_dev->request_pool =
9efd21e1 1397 kmem_cache_create(dev_name(&device->device),
1e05d88e 1398 sizeof(struct storvsc_cmd_request), 0,
ff568d3a
GKH
1399 SLAB_HWCACHE_ALIGN, NULL);
1400
795b613d 1401 if (!host_dev->request_pool) {
bef4a34a 1402 scsi_host_put(host);
bef4a34a
HJ
1403 return -ENOMEM;
1404 }
1405
8a046024 1406 device_info.port_number = host->host_no;
fa4d123a 1407 device_info.ring_buffer_size = storvsc_ringbuffer_size;
454f18a9 1408 /* Call to the vsc driver to add the device */
58f1f5cb 1409 ret = storvsc_dev_add(device, (void *)&device_info);
9efd21e1 1410
ff568d3a 1411 if (ret != 0) {
795b613d 1412 kmem_cache_destroy(host_dev->request_pool);
bef4a34a 1413 scsi_host_put(host);
8e854680 1414 return -ENODEV;
bef4a34a
HJ
1415 }
1416
bd1f5d6a
S
1417 if (dev_is_ide)
1418 storvsc_get_ide_info(device, &target, &path);
1419
795b613d
S
1420 host_dev->path = device_info.path_id;
1421 host_dev->target = device_info.target_id;
bef4a34a 1422
ff568d3a
GKH
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;
bef4a34a 1429
454f18a9 1430 /* Register the HBA and start the scsi bus scan */
9efd21e1 1431 ret = scsi_add_host(host, &device->device);
bd1f5d6a
S
1432 if (ret != 0)
1433 goto err_out;
bef4a34a 1434
bd1f5d6a
S
1435 if (!dev_is_ide) {
1436 scsi_scan_host(host);
1437 return 0;
1438 }
1439 ret = scsi_add_device(host, 0, target, 0);
1440 if (ret) {
1441 scsi_remove_host(host);
1442 goto err_out;
bef4a34a 1443 }
bd1f5d6a 1444 return 0;
bef4a34a 1445
bd1f5d6a
S
1446err_out:
1447 storvsc_dev_remove(device);
1448 kmem_cache_destroy(host_dev->request_pool);
1449 scsi_host_put(host);
1450 return -ENODEV;
bef4a34a
HJ
1451}
1452
7bd05b91
S
1453/* The one and only one */
1454
40bf63ed 1455static struct hv_driver storvsc_drv = {
768fa219 1456 .name = "storvsc",
d847b5fe 1457 .id_table = id_table,
40bf63ed
S
1458 .probe = storvsc_probe,
1459 .remove = storvsc_remove,
39ae6fae 1460};
7bd05b91 1461
d9bbae83 1462static int __init storvsc_drv_init(void)
f5c78872 1463{
01415ab3
S
1464 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 */
01415ab3 1472 max_outstanding_req_per_channel =
768fa219
GKH
1473 ((storvsc_ringbuffer_size - PAGE_SIZE) /
1474 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
1475 sizeof(struct vstor_packet) + sizeof(u64),
1476 sizeof(u64)));
f5c78872 1477
01415ab3 1478 if (max_outstanding_req_per_channel <
f5c78872 1479 STORVSC_MAX_IO_REQUESTS)
b06efc19 1480 return -EINVAL;
f5c78872 1481
768fa219 1482 return vmbus_driver_register(&storvsc_drv);
f5c78872
S
1483}
1484
c63ba9e1 1485static void __exit storvsc_drv_exit(void)
f5c78872 1486{
768fa219 1487 vmbus_driver_unregister(&storvsc_drv);
f5c78872
S
1488}
1489
ff568d3a 1490MODULE_LICENSE("GPL");
26c14cc1 1491MODULE_VERSION(HV_DRV_VERSION);
3afc7cc3 1492MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
d9bbae83 1493module_init(storvsc_drv_init);
c63ba9e1 1494module_exit(storvsc_drv_exit);