abc5ac568ab2342d61331a93d8734a255a3212d1
[pandora-kernel.git] / drivers / staging / hv / storvsc_drv.c
1 /*
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>
20  *   K. Y. Srinivasan <kys@microsoft.com>
21  */
22
23 #include <linux/kernel.h>
24 #include <linux/wait.h>
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>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/module.h>
33 #include <linux/device.h>
34 #include <linux/hyperv.h>
35 #include <scsi/scsi.h>
36 #include <scsi/scsi_cmnd.h>
37 #include <scsi/scsi_host.h>
38 #include <scsi/scsi_device.h>
39 #include <scsi/scsi_tcq.h>
40 #include <scsi/scsi_eh.h>
41 #include <scsi/scsi_devinfo.h>
42 #include <scsi/scsi_dbg.h>
43
44
45 #define STORVSC_RING_BUFFER_SIZE                        (20*PAGE_SIZE)
46 static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
47
48 module_param(storvsc_ringbuffer_size, int, S_IRUGO);
49 MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
50
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. */
96 enum 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
123 struct 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  */
152 struct 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. */
168 struct 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
185 struct 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 */
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 #define STORVSC_MAX_CMD_LEN                             16
236
237 struct hv_storvsc_request;
238
239 /* Matches Windows-end */
240 enum storvsc_request_type {
241         WRITE_TYPE,
242         READ_TYPE,
243         UNKNOWN_TYPE,
244 };
245
246
247 struct hv_storvsc_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
262 /* A storvsc device is a device object that contains a vmbus channel */
263 struct storvsc_device {
264         struct hv_device *device;
265
266         bool     destroy;
267         bool     drain_notify;
268         atomic_t num_outstanding_req;
269         struct Scsi_Host *host;
270
271         wait_queue_head_t waiting_to_drain;
272
273         /*
274          * Each unique Port/Path/Target represents 1 channel ie scsi
275          * controller. In reality, the pathid, targetid is always 0
276          * and the port is set by us
277          */
278         unsigned int port_number;
279         unsigned char path_id;
280         unsigned char target_id;
281
282         /* Used for vsc/vsp channel reset process */
283         struct hv_storvsc_request init_request;
284         struct hv_storvsc_request reset_request;
285 };
286
287 struct hv_host_device {
288         struct hv_device *dev;
289         struct kmem_cache *request_pool;
290         unsigned int port;
291         unsigned char path;
292         unsigned char target;
293 };
294
295 struct storvsc_cmd_request {
296         struct list_head entry;
297         struct scsi_cmnd *cmd;
298
299         unsigned int bounce_sgl_count;
300         struct scatterlist *bounce_sgl;
301
302         struct hv_storvsc_request request;
303 };
304
305 static inline struct storvsc_device *get_out_stor_device(
306                                         struct hv_device *device)
307 {
308         struct storvsc_device *stor_device;
309
310         stor_device = hv_get_drvdata(device);
311
312         if (stor_device && stor_device->destroy)
313                 stor_device = NULL;
314
315         return stor_device;
316 }
317
318
319 static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
320 {
321         dev->drain_notify = true;
322         wait_event(dev->waiting_to_drain,
323                    atomic_read(&dev->num_outstanding_req) == 0);
324         dev->drain_notify = false;
325 }
326
327 static inline struct storvsc_device *get_in_stor_device(
328                                         struct hv_device *device)
329 {
330         struct storvsc_device *stor_device;
331
332         stor_device = hv_get_drvdata(device);
333
334         if (!stor_device)
335                 goto get_in_err;
336
337         /*
338          * If the device is being destroyed; allow incoming
339          * traffic only to cleanup outstanding requests.
340          */
341
342         if (stor_device->destroy  &&
343                 (atomic_read(&stor_device->num_outstanding_req) == 0))
344                 stor_device = NULL;
345
346 get_in_err:
347         return stor_device;
348
349 }
350
351 static int storvsc_channel_init(struct hv_device *device)
352 {
353         struct storvsc_device *stor_device;
354         struct hv_storvsc_request *request;
355         struct vstor_packet *vstor_packet;
356         int ret, t;
357
358         stor_device = get_out_stor_device(device);
359         if (!stor_device)
360                 return -ENODEV;
361
362         request = &stor_device->init_request;
363         vstor_packet = &request->vstor_packet;
364
365         /*
366          * Now, initiate the vsc/vsp initialization protocol on the open
367          * channel
368          */
369         memset(request, 0, sizeof(struct hv_storvsc_request));
370         init_completion(&request->wait_event);
371         vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
372         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
373
374         ret = vmbus_sendpacket(device->channel, vstor_packet,
375                                sizeof(struct vstor_packet),
376                                (unsigned long)request,
377                                VM_PKT_DATA_INBAND,
378                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
379         if (ret != 0)
380                 goto cleanup;
381
382         t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
383         if (t == 0) {
384                 ret = -ETIMEDOUT;
385                 goto cleanup;
386         }
387
388         if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
389             vstor_packet->status != 0)
390                 goto cleanup;
391
392
393         /* reuse the packet for version range supported */
394         memset(vstor_packet, 0, sizeof(struct vstor_packet));
395         vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
396         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
397
398         vstor_packet->version.major_minor = VMSTOR_PROTOCOL_VERSION_CURRENT;
399         FILL_VMSTOR_REVISION(vstor_packet->version.revision);
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         memset(vstor_packet, 0, sizeof(struct vstor_packet));
421         vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
422         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
423         vstor_packet->storage_channel_properties.port_number =
424                                         stor_device->port_number;
425
426         ret = vmbus_sendpacket(device->channel, vstor_packet,
427                                sizeof(struct vstor_packet),
428                                (unsigned long)request,
429                                VM_PKT_DATA_INBAND,
430                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
431
432         if (ret != 0)
433                 goto cleanup;
434
435         t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
436         if (t == 0) {
437                 ret = -ETIMEDOUT;
438                 goto cleanup;
439         }
440
441         if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
442             vstor_packet->status != 0)
443                 goto cleanup;
444
445         stor_device->path_id = vstor_packet->storage_channel_properties.path_id;
446         stor_device->target_id
447                 = vstor_packet->storage_channel_properties.target_id;
448
449         memset(vstor_packet, 0, sizeof(struct vstor_packet));
450         vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
451         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
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
473 cleanup:
474         return ret;
475 }
476
477 static void storvsc_on_io_completion(struct hv_device *device,
478                                   struct vstor_packet *vstor_packet,
479                                   struct hv_storvsc_request *request)
480 {
481         struct storvsc_device *stor_device;
482         struct vstor_packet *stor_pkt;
483
484         stor_device = hv_get_drvdata(device);
485         stor_pkt = &request->vstor_packet;
486
487         /*
488          * The current SCSI handling on the host side does
489          * not correctly handle:
490          * INQUIRY command with page code parameter set to 0x80
491          * MODE_SENSE command with cmd[2] == 0x1c
492          *
493          * Setup srb and scsi status so this won't be fatal.
494          * We do this so we can distinguish truly fatal failues
495          * (srb status == 0x4) and off-line the device in that case.
496          */
497
498         if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
499                 (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
500                 vstor_packet->vm_srb.scsi_status = 0;
501                 vstor_packet->vm_srb.srb_status = 0x1;
502         }
503
504
505         /* Copy over the status...etc */
506         stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
507         stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
508         stor_pkt->vm_srb.sense_info_length =
509         vstor_packet->vm_srb.sense_info_length;
510
511         if (vstor_packet->vm_srb.scsi_status != 0 ||
512                 vstor_packet->vm_srb.srb_status != 1){
513                 dev_warn(&device->device,
514                          "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
515                          stor_pkt->vm_srb.cdb[0],
516                          vstor_packet->vm_srb.scsi_status,
517                          vstor_packet->vm_srb.srb_status);
518         }
519
520         if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
521                 /* CHECK_CONDITION */
522                 if (vstor_packet->vm_srb.srb_status & 0x80) {
523                         /* autosense data available */
524                         dev_warn(&device->device,
525                                  "stor pkt %p autosense data valid - len %d\n",
526                                  request,
527                                  vstor_packet->vm_srb.sense_info_length);
528
529                         memcpy(request->sense_buffer,
530                                vstor_packet->vm_srb.sense_data,
531                                vstor_packet->vm_srb.sense_info_length);
532
533                 }
534         }
535
536         stor_pkt->vm_srb.data_transfer_length =
537         vstor_packet->vm_srb.data_transfer_length;
538
539         request->on_io_completion(request);
540
541         if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
542                 stor_device->drain_notify)
543                 wake_up(&stor_device->waiting_to_drain);
544
545
546 }
547
548 static void storvsc_on_receive(struct hv_device *device,
549                              struct vstor_packet *vstor_packet,
550                              struct hv_storvsc_request *request)
551 {
552         switch (vstor_packet->operation) {
553         case VSTOR_OPERATION_COMPLETE_IO:
554                 storvsc_on_io_completion(device, vstor_packet, request);
555                 break;
556         case VSTOR_OPERATION_REMOVE_DEVICE:
557
558         default:
559                 break;
560         }
561 }
562
563 static void storvsc_on_channel_callback(void *context)
564 {
565         struct hv_device *device = (struct hv_device *)context;
566         struct storvsc_device *stor_device;
567         u32 bytes_recvd;
568         u64 request_id;
569         unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
570         struct hv_storvsc_request *request;
571         int ret;
572
573
574         stor_device = get_in_stor_device(device);
575         if (!stor_device)
576                 return;
577
578         do {
579                 ret = vmbus_recvpacket(device->channel, packet,
580                                        ALIGN(sizeof(struct vstor_packet), 8),
581                                        &bytes_recvd, &request_id);
582                 if (ret == 0 && bytes_recvd > 0) {
583
584                         request = (struct hv_storvsc_request *)
585                                         (unsigned long)request_id;
586
587                         if ((request == &stor_device->init_request) ||
588                             (request == &stor_device->reset_request)) {
589
590                                 memcpy(&request->vstor_packet, packet,
591                                        sizeof(struct vstor_packet));
592                                 complete(&request->wait_event);
593                         } else {
594                                 storvsc_on_receive(device,
595                                                 (struct vstor_packet *)packet,
596                                                 request);
597                         }
598                 } else {
599                         break;
600                 }
601         } while (1);
602
603         return;
604 }
605
606 static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
607 {
608         struct vmstorage_channel_properties props;
609         int ret;
610
611         memset(&props, 0, sizeof(struct vmstorage_channel_properties));
612
613         /* Open the channel */
614         ret = vmbus_open(device->channel,
615                          ring_size,
616                          ring_size,
617                          (void *)&props,
618                          sizeof(struct vmstorage_channel_properties),
619                          storvsc_on_channel_callback, device);
620
621         if (ret != 0)
622                 return ret;
623
624         ret = storvsc_channel_init(device);
625
626         return ret;
627 }
628
629 static int storvsc_dev_remove(struct hv_device *device)
630 {
631         struct storvsc_device *stor_device;
632         unsigned long flags;
633
634         stor_device = hv_get_drvdata(device);
635
636         spin_lock_irqsave(&device->channel->inbound_lock, flags);
637         stor_device->destroy = true;
638         spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
639
640         /*
641          * At this point, all outbound traffic should be disable. We
642          * only allow inbound traffic (responses) to proceed so that
643          * outstanding requests can be completed.
644          */
645
646         storvsc_wait_to_drain(stor_device);
647
648         /*
649          * Since we have already drained, we don't need to busy wait
650          * as was done in final_release_stor_device()
651          * Note that we cannot set the ext pointer to NULL until
652          * we have drained - to drain the outgoing packets, we need to
653          * allow incoming packets.
654          */
655         spin_lock_irqsave(&device->channel->inbound_lock, flags);
656         hv_set_drvdata(device, NULL);
657         spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
658
659         /* Close the channel */
660         vmbus_close(device->channel);
661
662         kfree(stor_device);
663         return 0;
664 }
665
666 static int storvsc_do_io(struct hv_device *device,
667                               struct hv_storvsc_request *request)
668 {
669         struct storvsc_device *stor_device;
670         struct vstor_packet *vstor_packet;
671         int ret = 0;
672
673         vstor_packet = &request->vstor_packet;
674         stor_device = get_out_stor_device(device);
675
676         if (!stor_device)
677                 return -ENODEV;
678
679
680         request->device  = device;
681
682
683         vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
684
685         vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
686
687
688         vstor_packet->vm_srb.sense_info_length = SENSE_BUFFER_SIZE;
689
690
691         vstor_packet->vm_srb.data_transfer_length =
692         request->data_buffer.len;
693
694         vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
695
696         if (request->data_buffer.len) {
697                 ret = vmbus_sendpacket_multipagebuffer(device->channel,
698                                 &request->data_buffer,
699                                 vstor_packet,
700                                 sizeof(struct vstor_packet),
701                                 (unsigned long)request);
702         } else {
703                 ret = vmbus_sendpacket(device->channel, vstor_packet,
704                                sizeof(struct vstor_packet),
705                                (unsigned long)request,
706                                VM_PKT_DATA_INBAND,
707                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
708         }
709
710         if (ret != 0)
711                 return ret;
712
713         atomic_inc(&stor_device->num_outstanding_req);
714
715         return ret;
716 }
717
718 static void storvsc_get_ide_info(struct hv_device *dev, int *target, int *path)
719 {
720         *target =
721                 dev->dev_instance.b[5] << 8 | dev->dev_instance.b[4];
722
723         *path =
724                 dev->dev_instance.b[3] << 24 |
725                 dev->dev_instance.b[2] << 16 |
726                 dev->dev_instance.b[1] << 8  | dev->dev_instance.b[0];
727 }
728
729
730 static int storvsc_device_alloc(struct scsi_device *sdevice)
731 {
732         /*
733          * This enables luns to be located sparsely. Otherwise, we may not
734          * discovered them.
735          */
736         sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
737         return 0;
738 }
739
740 static int storvsc_merge_bvec(struct request_queue *q,
741                               struct bvec_merge_data *bmd, struct bio_vec *bvec)
742 {
743         /* checking done by caller. */
744         return bvec->bv_len;
745 }
746
747 static int storvsc_device_configure(struct scsi_device *sdevice)
748 {
749         scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
750                                 STORVSC_MAX_IO_REQUESTS);
751
752         blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
753
754         blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
755
756         blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
757
758         return 0;
759 }
760
761 static void destroy_bounce_buffer(struct scatterlist *sgl,
762                                   unsigned int sg_count)
763 {
764         int i;
765         struct page *page_buf;
766
767         for (i = 0; i < sg_count; i++) {
768                 page_buf = sg_page((&sgl[i]));
769                 if (page_buf != NULL)
770                         __free_page(page_buf);
771         }
772
773         kfree(sgl);
774 }
775
776 static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
777 {
778         int i;
779
780         /* No need to check */
781         if (sg_count < 2)
782                 return -1;
783
784         /* We have at least 2 sg entries */
785         for (i = 0; i < sg_count; i++) {
786                 if (i == 0) {
787                         /* make sure 1st one does not have hole */
788                         if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
789                                 return i;
790                 } else if (i == sg_count - 1) {
791                         /* make sure last one does not have hole */
792                         if (sgl[i].offset != 0)
793                                 return i;
794                 } else {
795                         /* make sure no hole in the middle */
796                         if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
797                                 return i;
798                 }
799         }
800         return -1;
801 }
802
803 static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
804                                                 unsigned int sg_count,
805                                                 unsigned int len)
806 {
807         int i;
808         int num_pages;
809         struct scatterlist *bounce_sgl;
810         struct page *page_buf;
811
812         num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
813
814         bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
815         if (!bounce_sgl)
816                 return NULL;
817
818         for (i = 0; i < num_pages; i++) {
819                 page_buf = alloc_page(GFP_ATOMIC);
820                 if (!page_buf)
821                         goto cleanup;
822                 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
823         }
824
825         return bounce_sgl;
826
827 cleanup:
828         destroy_bounce_buffer(bounce_sgl, num_pages);
829         return NULL;
830 }
831
832
833 /* Assume the original sgl has enough room */
834 static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
835                                             struct scatterlist *bounce_sgl,
836                                             unsigned int orig_sgl_count)
837 {
838         int i;
839         int j = 0;
840         unsigned long src, dest;
841         unsigned int srclen, destlen, copylen;
842         unsigned int total_copied = 0;
843         unsigned long bounce_addr = 0;
844         unsigned long dest_addr = 0;
845         unsigned long flags;
846
847         local_irq_save(flags);
848
849         for (i = 0; i < orig_sgl_count; i++) {
850                 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
851                                         KM_IRQ0) + orig_sgl[i].offset;
852                 dest = dest_addr;
853                 destlen = orig_sgl[i].length;
854
855                 if (bounce_addr == 0)
856                         bounce_addr =
857                         (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
858                                                         KM_IRQ0);
859
860                 while (destlen) {
861                         src = bounce_addr + bounce_sgl[j].offset;
862                         srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
863
864                         copylen = min(srclen, destlen);
865                         memcpy((void *)dest, (void *)src, copylen);
866
867                         total_copied += copylen;
868                         bounce_sgl[j].offset += copylen;
869                         destlen -= copylen;
870                         dest += copylen;
871
872                         if (bounce_sgl[j].offset == bounce_sgl[j].length) {
873                                 /* full */
874                                 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
875                                 j++;
876
877                                 /* if we need to use another bounce buffer */
878                                 if (destlen || i != orig_sgl_count - 1)
879                                         bounce_addr =
880                                         (unsigned long)kmap_atomic(
881                                         sg_page((&bounce_sgl[j])), KM_IRQ0);
882                         } else if (destlen == 0 && i == orig_sgl_count - 1) {
883                                 /* unmap the last bounce that is < PAGE_SIZE */
884                                 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
885                         }
886                 }
887
888                 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
889                               KM_IRQ0);
890         }
891
892         local_irq_restore(flags);
893
894         return total_copied;
895 }
896
897
898 /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
899 static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
900                                           struct scatterlist *bounce_sgl,
901                                           unsigned int orig_sgl_count)
902 {
903         int i;
904         int j = 0;
905         unsigned long src, dest;
906         unsigned int srclen, destlen, copylen;
907         unsigned int total_copied = 0;
908         unsigned long bounce_addr = 0;
909         unsigned long src_addr = 0;
910         unsigned long flags;
911
912         local_irq_save(flags);
913
914         for (i = 0; i < orig_sgl_count; i++) {
915                 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
916                                 KM_IRQ0) + orig_sgl[i].offset;
917                 src = src_addr;
918                 srclen = orig_sgl[i].length;
919
920                 if (bounce_addr == 0)
921                         bounce_addr =
922                         (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])),
923                                                 KM_IRQ0);
924
925                 while (srclen) {
926                         /* assume bounce offset always == 0 */
927                         dest = bounce_addr + bounce_sgl[j].length;
928                         destlen = PAGE_SIZE - bounce_sgl[j].length;
929
930                         copylen = min(srclen, destlen);
931                         memcpy((void *)dest, (void *)src, copylen);
932
933                         total_copied += copylen;
934                         bounce_sgl[j].length += copylen;
935                         srclen -= copylen;
936                         src += copylen;
937
938                         if (bounce_sgl[j].length == PAGE_SIZE) {
939                                 /* full..move to next entry */
940                                 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
941                                 j++;
942
943                                 /* if we need to use another bounce buffer */
944                                 if (srclen || i != orig_sgl_count - 1)
945                                         bounce_addr =
946                                         (unsigned long)kmap_atomic(
947                                         sg_page((&bounce_sgl[j])), KM_IRQ0);
948
949                         } else if (srclen == 0 && i == orig_sgl_count - 1) {
950                                 /* unmap the last bounce that is < PAGE_SIZE */
951                                 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
952                         }
953                 }
954
955                 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
956         }
957
958         local_irq_restore(flags);
959
960         return total_copied;
961 }
962
963
964 static int storvsc_remove(struct hv_device *dev)
965 {
966         struct storvsc_device *stor_device = hv_get_drvdata(dev);
967         struct Scsi_Host *host = stor_device->host;
968         struct hv_host_device *host_dev =
969                         (struct hv_host_device *)host->hostdata;
970
971         scsi_remove_host(host);
972
973         scsi_host_put(host);
974
975         storvsc_dev_remove(dev);
976         if (host_dev->request_pool) {
977                 kmem_cache_destroy(host_dev->request_pool);
978                 host_dev->request_pool = NULL;
979         }
980         return 0;
981 }
982
983
984 static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
985                            sector_t capacity, int *info)
986 {
987         sector_t nsect = capacity;
988         sector_t cylinders = nsect;
989         int heads, sectors_pt;
990
991         /*
992          * We are making up these values; let us keep it simple.
993          */
994         heads = 0xff;
995         sectors_pt = 0x3f;      /* Sectors per track */
996         sector_div(cylinders, heads * sectors_pt);
997         if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
998                 cylinders = 0xffff;
999
1000         info[0] = heads;
1001         info[1] = sectors_pt;
1002         info[2] = (int)cylinders;
1003
1004         return 0;
1005 }
1006
1007 static int storvsc_host_reset(struct hv_device *device)
1008 {
1009         struct storvsc_device *stor_device;
1010         struct hv_storvsc_request *request;
1011         struct vstor_packet *vstor_packet;
1012         int ret, t;
1013
1014
1015         stor_device = get_out_stor_device(device);
1016         if (!stor_device)
1017                 return -ENODEV;
1018
1019         request = &stor_device->reset_request;
1020         vstor_packet = &request->vstor_packet;
1021
1022         init_completion(&request->wait_event);
1023
1024         vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
1025         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
1026         vstor_packet->vm_srb.path_id = stor_device->path_id;
1027
1028         ret = vmbus_sendpacket(device->channel, vstor_packet,
1029                                sizeof(struct vstor_packet),
1030                                (unsigned long)&stor_device->reset_request,
1031                                VM_PKT_DATA_INBAND,
1032                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1033         if (ret != 0)
1034                 goto cleanup;
1035
1036         t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
1037         if (t == 0) {
1038                 ret = -ETIMEDOUT;
1039                 goto cleanup;
1040         }
1041
1042
1043         /*
1044          * At this point, all outstanding requests in the adapter
1045          * should have been flushed out and return to us
1046          * There is a potential race here where the host may be in
1047          * the process of responding when we return from here.
1048          * Just wait for all in-transit packets to be accounted for
1049          * before we return from here.
1050          */
1051         storvsc_wait_to_drain(stor_device);
1052
1053 cleanup:
1054         return ret;
1055 }
1056
1057
1058 /*
1059  * storvsc_host_reset_handler - Reset the scsi HBA
1060  */
1061 static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
1062 {
1063         int ret;
1064         struct hv_host_device *host_dev =
1065                 (struct hv_host_device *)scmnd->device->host->hostdata;
1066         struct hv_device *dev = host_dev->dev;
1067
1068         ret = storvsc_host_reset(dev);
1069         if (ret != 0)
1070                 return ret;
1071
1072         return ret;
1073 }
1074
1075
1076 /*
1077  * storvsc_command_completion - Command completion processing
1078  */
1079 static void storvsc_command_completion(struct hv_storvsc_request *request)
1080 {
1081         struct storvsc_cmd_request *cmd_request =
1082                 (struct storvsc_cmd_request *)request->context;
1083         struct scsi_cmnd *scmnd = cmd_request->cmd;
1084         struct hv_host_device *host_dev =
1085                 (struct hv_host_device *)scmnd->device->host->hostdata;
1086         void (*scsi_done_fn)(struct scsi_cmnd *);
1087         struct scsi_sense_hdr sense_hdr;
1088         struct vmscsi_request *vm_srb;
1089
1090         vm_srb = &request->vstor_packet.vm_srb;
1091         if (cmd_request->bounce_sgl_count) {
1092                 if (vm_srb->data_in == READ_TYPE) {
1093                         copy_from_bounce_buffer(scsi_sglist(scmnd),
1094                                         cmd_request->bounce_sgl,
1095                                         scsi_sg_count(scmnd));
1096                         destroy_bounce_buffer(cmd_request->bounce_sgl,
1097                                         cmd_request->bounce_sgl_count);
1098                 }
1099         }
1100
1101         /*
1102          * If there is an error; offline the device since all
1103          * error recovery strategies would have already been
1104          * deployed on the host side.
1105          */
1106         if (vm_srb->srb_status == 0x4)
1107                 scmnd->result = DID_TARGET_FAILURE << 16;
1108         else
1109                 scmnd->result = vm_srb->scsi_status;
1110
1111         if (scmnd->result) {
1112                 if (scsi_normalize_sense(scmnd->sense_buffer,
1113                                 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
1114                         scsi_print_sense_hdr("storvsc", &sense_hdr);
1115         }
1116
1117         scsi_set_resid(scmnd,
1118                 request->data_buffer.len -
1119                 vm_srb->data_transfer_length);
1120
1121         scsi_done_fn = scmnd->scsi_done;
1122
1123         scmnd->host_scribble = NULL;
1124         scmnd->scsi_done = NULL;
1125
1126         scsi_done_fn(scmnd);
1127
1128         kmem_cache_free(host_dev->request_pool, cmd_request);
1129 }
1130
1131 static bool storvsc_check_scsi_cmd(struct scsi_cmnd *scmnd)
1132 {
1133         bool allowed = true;
1134         u8 scsi_op = scmnd->cmnd[0];
1135
1136         switch (scsi_op) {
1137         /* smartd sends this command, which will offline the device */
1138         case SET_WINDOW:
1139                 scmnd->result = DID_ERROR << 16;
1140                 allowed = false;
1141                 break;
1142         default:
1143                 break;
1144         }
1145         return allowed;
1146 }
1147
1148 /*
1149  * storvsc_queuecommand - Initiate command processing
1150  */
1151 static int storvsc_queuecommand_lck(struct scsi_cmnd *scmnd,
1152                                 void (*done)(struct scsi_cmnd *))
1153 {
1154         int ret;
1155         struct hv_host_device *host_dev =
1156                 (struct hv_host_device *)scmnd->device->host->hostdata;
1157         struct hv_device *dev = host_dev->dev;
1158         struct hv_storvsc_request *request;
1159         struct storvsc_cmd_request *cmd_request;
1160         unsigned int request_size = 0;
1161         int i;
1162         struct scatterlist *sgl;
1163         unsigned int sg_count = 0;
1164         struct vmscsi_request *vm_srb;
1165
1166         if (storvsc_check_scsi_cmd(scmnd) == false) {
1167                 done(scmnd);
1168                 return 0;
1169         }
1170
1171         /* If retrying, no need to prep the cmd */
1172         if (scmnd->host_scribble) {
1173
1174                 cmd_request =
1175                         (struct storvsc_cmd_request *)scmnd->host_scribble;
1176
1177                 goto retry_request;
1178         }
1179
1180         scmnd->scsi_done = done;
1181
1182         request_size = sizeof(struct storvsc_cmd_request);
1183
1184         cmd_request = kmem_cache_zalloc(host_dev->request_pool,
1185                                        GFP_ATOMIC);
1186         if (!cmd_request) {
1187                 scmnd->scsi_done = NULL;
1188                 return SCSI_MLQUEUE_DEVICE_BUSY;
1189         }
1190
1191         /* Setup the cmd request */
1192         cmd_request->bounce_sgl_count = 0;
1193         cmd_request->bounce_sgl = NULL;
1194         cmd_request->cmd = scmnd;
1195
1196         scmnd->host_scribble = (unsigned char *)cmd_request;
1197
1198         request = &cmd_request->request;
1199         vm_srb = &request->vstor_packet.vm_srb;
1200
1201
1202         /* Build the SRB */
1203         switch (scmnd->sc_data_direction) {
1204         case DMA_TO_DEVICE:
1205                 vm_srb->data_in = WRITE_TYPE;
1206                 break;
1207         case DMA_FROM_DEVICE:
1208                 vm_srb->data_in = READ_TYPE;
1209                 break;
1210         default:
1211                 vm_srb->data_in = UNKNOWN_TYPE;
1212                 break;
1213         }
1214
1215         request->on_io_completion = storvsc_command_completion;
1216         request->context = cmd_request;/* scmnd; */
1217
1218         vm_srb->port_number = host_dev->port;
1219         vm_srb->path_id = scmnd->device->channel;
1220         vm_srb->target_id = scmnd->device->id;
1221         vm_srb->lun = scmnd->device->lun;
1222
1223         vm_srb->cdb_length = scmnd->cmd_len;
1224
1225         memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
1226
1227         request->sense_buffer = scmnd->sense_buffer;
1228
1229
1230         request->data_buffer.len = scsi_bufflen(scmnd);
1231         if (scsi_sg_count(scmnd)) {
1232                 sgl = (struct scatterlist *)scsi_sglist(scmnd);
1233                 sg_count = scsi_sg_count(scmnd);
1234
1235                 /* check if we need to bounce the sgl */
1236                 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
1237                         cmd_request->bounce_sgl =
1238                                 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
1239                                                      scsi_bufflen(scmnd));
1240                         if (!cmd_request->bounce_sgl) {
1241                                 scmnd->scsi_done = NULL;
1242                                 scmnd->host_scribble = NULL;
1243                                 kmem_cache_free(host_dev->request_pool,
1244                                                 cmd_request);
1245
1246                                 return SCSI_MLQUEUE_HOST_BUSY;
1247                         }
1248
1249                         cmd_request->bounce_sgl_count =
1250                                 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
1251                                         PAGE_SHIFT;
1252
1253                         if (vm_srb->data_in == WRITE_TYPE)
1254                                 copy_to_bounce_buffer(sgl,
1255                                         cmd_request->bounce_sgl,
1256                                         scsi_sg_count(scmnd));
1257
1258                         sgl = cmd_request->bounce_sgl;
1259                         sg_count = cmd_request->bounce_sgl_count;
1260                 }
1261
1262                 request->data_buffer.offset = sgl[0].offset;
1263
1264                 for (i = 0; i < sg_count; i++)
1265                         request->data_buffer.pfn_array[i] =
1266                                 page_to_pfn(sg_page((&sgl[i])));
1267
1268         } else if (scsi_sglist(scmnd)) {
1269                 request->data_buffer.offset =
1270                         virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
1271                 request->data_buffer.pfn_array[0] =
1272                         virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
1273         }
1274
1275 retry_request:
1276         /* Invokes the vsc to start an IO */
1277         ret = storvsc_do_io(dev, &cmd_request->request);
1278
1279         if (ret == -EAGAIN) {
1280                 /* no more space */
1281
1282                 if (cmd_request->bounce_sgl_count)
1283                         destroy_bounce_buffer(cmd_request->bounce_sgl,
1284                                         cmd_request->bounce_sgl_count);
1285
1286                 kmem_cache_free(host_dev->request_pool, cmd_request);
1287
1288                 scmnd->scsi_done = NULL;
1289                 scmnd->host_scribble = NULL;
1290
1291                 ret = SCSI_MLQUEUE_DEVICE_BUSY;
1292         }
1293
1294         return ret;
1295 }
1296
1297 static DEF_SCSI_QCMD(storvsc_queuecommand)
1298
1299
1300 /* Scsi driver */
1301 static struct scsi_host_template scsi_driver = {
1302         .module =               THIS_MODULE,
1303         .name =                 "storvsc_host_t",
1304         .bios_param =           storvsc_get_chs,
1305         .queuecommand =         storvsc_queuecommand,
1306         .eh_host_reset_handler =        storvsc_host_reset_handler,
1307         .slave_alloc =          storvsc_device_alloc,
1308         .slave_configure =      storvsc_device_configure,
1309         .cmd_per_lun =          1,
1310         /* 64 max_queue * 1 target */
1311         .can_queue =            STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
1312         .this_id =              -1,
1313         /* no use setting to 0 since ll_blk_rw reset it to 1 */
1314         /* currently 32 */
1315         .sg_tablesize =         MAX_MULTIPAGE_BUFFER_COUNT,
1316         /*
1317          * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
1318          * into 1 sg element. If set, we must limit the max_segment_size to
1319          * PAGE_SIZE, otherwise we may get 1 sg element that represents
1320          * multiple
1321          */
1322         /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
1323         .use_clustering =       ENABLE_CLUSTERING,
1324         /* Make sure we dont get a sg segment crosses a page boundary */
1325         .dma_boundary =         PAGE_SIZE-1,
1326 };
1327
1328 enum {
1329         SCSI_GUID,
1330         IDE_GUID,
1331 };
1332
1333 static const struct hv_vmbus_device_id id_table[] = {
1334         /* SCSI guid */
1335         { VMBUS_DEVICE(0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
1336                        0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f)
1337           .driver_data = SCSI_GUID },
1338         /* IDE guid */
1339         { VMBUS_DEVICE(0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
1340                        0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5)
1341           .driver_data = IDE_GUID },
1342         { },
1343 };
1344
1345 MODULE_DEVICE_TABLE(vmbus, id_table);
1346
1347
1348 /*
1349  * storvsc_probe - Add a new device for this driver
1350  */
1351
1352 static int storvsc_probe(struct hv_device *device,
1353                         const struct hv_vmbus_device_id *dev_id)
1354 {
1355         int ret;
1356         struct Scsi_Host *host;
1357         struct hv_host_device *host_dev;
1358         bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
1359         int path = 0;
1360         int target = 0;
1361         struct storvsc_device *stor_device;
1362
1363         host = scsi_host_alloc(&scsi_driver,
1364                                sizeof(struct hv_host_device));
1365         if (!host)
1366                 return -ENOMEM;
1367
1368         host_dev = (struct hv_host_device *)host->hostdata;
1369         memset(host_dev, 0, sizeof(struct hv_host_device));
1370
1371         host_dev->port = host->host_no;
1372         host_dev->dev = device;
1373
1374         host_dev->request_pool =
1375                                 kmem_cache_create(dev_name(&device->device),
1376                                         sizeof(struct storvsc_cmd_request), 0,
1377                                         SLAB_HWCACHE_ALIGN, NULL);
1378
1379         if (!host_dev->request_pool) {
1380                 scsi_host_put(host);
1381                 return -ENOMEM;
1382         }
1383
1384         stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
1385         if (!stor_device) {
1386                 kmem_cache_destroy(host_dev->request_pool);
1387                 scsi_host_put(host);
1388                 return -ENOMEM;
1389         }
1390
1391         stor_device->destroy = false;
1392         init_waitqueue_head(&stor_device->waiting_to_drain);
1393         stor_device->device = device;
1394         stor_device->host = host;
1395         hv_set_drvdata(device, stor_device);
1396
1397         stor_device->port_number = host->host_no;
1398         ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size);
1399         if (ret) {
1400                 kmem_cache_destroy(host_dev->request_pool);
1401                 scsi_host_put(host);
1402                 kfree(stor_device);
1403                 return ret;
1404         }
1405
1406         if (dev_is_ide)
1407                 storvsc_get_ide_info(device, &target, &path);
1408
1409         host_dev->path = stor_device->path_id;
1410         host_dev->target = stor_device->target_id;
1411
1412         /* max # of devices per target */
1413         host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
1414         /* max # of targets per channel */
1415         host->max_id = STORVSC_MAX_TARGETS;
1416         /* max # of channels */
1417         host->max_channel = STORVSC_MAX_CHANNELS - 1;
1418         /* max cmd length */
1419         host->max_cmd_len = STORVSC_MAX_CMD_LEN;
1420
1421         /* Register the HBA and start the scsi bus scan */
1422         ret = scsi_add_host(host, &device->device);
1423         if (ret != 0)
1424                 goto err_out;
1425
1426         if (!dev_is_ide) {
1427                 scsi_scan_host(host);
1428                 return 0;
1429         }
1430         ret = scsi_add_device(host, 0, target, 0);
1431         if (ret) {
1432                 scsi_remove_host(host);
1433                 goto err_out;
1434         }
1435         return 0;
1436
1437 err_out:
1438         storvsc_dev_remove(device);
1439         kmem_cache_destroy(host_dev->request_pool);
1440         scsi_host_put(host);
1441         return -ENODEV;
1442 }
1443
1444 /* The one and only one */
1445
1446 static struct hv_driver storvsc_drv = {
1447         .name = "storvsc",
1448         .id_table = id_table,
1449         .probe = storvsc_probe,
1450         .remove = storvsc_remove,
1451 };
1452
1453 static int __init storvsc_drv_init(void)
1454 {
1455         u32 max_outstanding_req_per_channel;
1456
1457         /*
1458          * Divide the ring buffer data size (which is 1 page less
1459          * than the ring buffer size since that page is reserved for
1460          * the ring buffer indices) by the max request size (which is
1461          * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
1462          */
1463         max_outstanding_req_per_channel =
1464                 ((storvsc_ringbuffer_size - PAGE_SIZE) /
1465                 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
1466                 sizeof(struct vstor_packet) + sizeof(u64),
1467                 sizeof(u64)));
1468
1469         if (max_outstanding_req_per_channel <
1470             STORVSC_MAX_IO_REQUESTS)
1471                 return -EINVAL;
1472
1473         return vmbus_driver_register(&storvsc_drv);
1474 }
1475
1476 static void __exit storvsc_drv_exit(void)
1477 {
1478         vmbus_driver_unregister(&storvsc_drv);
1479 }
1480
1481 MODULE_LICENSE("GPL");
1482 MODULE_VERSION(HV_DRV_VERSION);
1483 MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
1484 module_init(storvsc_drv_init);
1485 module_exit(storvsc_drv_exit);