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