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