Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[firefly-linux-kernel-4.4.55.git] / drivers / staging / hv / storvsc.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  */
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/wait.h>
24 #include <linux/string.h>
25 #include <linux/slab.h>
26 #include <linux/mm.h>
27 #include <linux/delay.h>
28 #include "hv_api.h"
29 #include "logging.h"
30 #include "storvsc_api.h"
31 #include "vmbus_packet_format.h"
32 #include "vstorage.h"
33 #include "channel.h"
34
35
36 struct storvsc_request_extension {
37         /* LIST_ENTRY ListEntry; */
38
39         struct hv_storvsc_request *request;
40         struct hv_device *device;
41
42         /* Synchronize the request/response if needed */
43         int wait_condition;
44         wait_queue_head_t wait_event;
45
46         struct vstor_packet vstor_packet;
47 };
48
49 /* A storvsc device is a device object that contains a vmbus channel */
50 struct storvsc_device {
51         struct hv_device *device;
52
53         /* 0 indicates the device is being destroyed */
54         atomic_t ref_count;
55
56         atomic_t num_outstanding_req;
57
58         /*
59          * Each unique Port/Path/Target represents 1 channel ie scsi
60          * controller. In reality, the pathid, targetid is always 0
61          * and the port is set by us
62          */
63         unsigned int port_number;
64         unsigned char path_id;
65         unsigned char target_id;
66
67         /* LIST_ENTRY OutstandingRequestList; */
68         /* HANDLE OutstandingRequestLock; */
69
70         /* Used for vsc/vsp channel reset process */
71         struct storvsc_request_extension init_request;
72         struct storvsc_request_extension reset_request;
73 };
74
75
76 static const char *g_driver_name = "storvsc";
77
78 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
79 static const struct hv_guid gStorVscDeviceType = {
80         .data = {
81                 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
82                 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f
83         }
84 };
85
86
87 static inline struct storvsc_device *alloc_stor_device(struct hv_device *device)
88 {
89         struct storvsc_device *stor_device;
90
91         stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
92         if (!stor_device)
93                 return NULL;
94
95         /* Set to 2 to allow both inbound and outbound traffics */
96         /* (ie get_stor_device() and must_get_stor_device()) to proceed. */
97         atomic_cmpxchg(&stor_device->ref_count, 0, 2);
98
99         stor_device->device = device;
100         device->ext = stor_device;
101
102         return stor_device;
103 }
104
105 static inline void free_stor_device(struct storvsc_device *device)
106 {
107         /* ASSERT(atomic_read(&device->ref_count) == 0); */
108         kfree(device);
109 }
110
111 /* Get the stordevice object iff exists and its refcount > 1 */
112 static inline struct storvsc_device *get_stor_device(struct hv_device *device)
113 {
114         struct storvsc_device *stor_device;
115
116         stor_device = (struct storvsc_device *)device->ext;
117         if (stor_device && atomic_read(&stor_device->ref_count) > 1)
118                 atomic_inc(&stor_device->ref_count);
119         else
120                 stor_device = NULL;
121
122         return stor_device;
123 }
124
125 /* Get the stordevice object iff exists and its refcount > 0 */
126 static inline struct storvsc_device *must_get_stor_device(
127                                         struct hv_device *device)
128 {
129         struct storvsc_device *stor_device;
130
131         stor_device = (struct storvsc_device *)device->ext;
132         if (stor_device && atomic_read(&stor_device->ref_count))
133                 atomic_inc(&stor_device->ref_count);
134         else
135                 stor_device = NULL;
136
137         return stor_device;
138 }
139
140 static inline void put_stor_device(struct hv_device *device)
141 {
142         struct storvsc_device *stor_device;
143
144         stor_device = (struct storvsc_device *)device->ext;
145         /* ASSERT(stor_device); */
146
147         atomic_dec(&stor_device->ref_count);
148         /* ASSERT(atomic_read(&stor_device->ref_count)); */
149 }
150
151 /* Drop ref count to 1 to effectively disable get_stor_device() */
152 static inline struct storvsc_device *release_stor_device(
153                                         struct hv_device *device)
154 {
155         struct storvsc_device *stor_device;
156
157         stor_device = (struct storvsc_device *)device->ext;
158         /* ASSERT(stor_device); */
159
160         /* Busy wait until the ref drop to 2, then set it to 1 */
161         while (atomic_cmpxchg(&stor_device->ref_count, 2, 1) != 2)
162                 udelay(100);
163
164         return stor_device;
165 }
166
167 /* Drop ref count to 0. No one can use stor_device object. */
168 static inline struct storvsc_device *final_release_stor_device(
169                         struct hv_device *device)
170 {
171         struct storvsc_device *stor_device;
172
173         stor_device = (struct storvsc_device *)device->ext;
174         /* ASSERT(stor_device); */
175
176         /* Busy wait until the ref drop to 1, then set it to 0 */
177         while (atomic_cmpxchg(&stor_device->ref_count, 1, 0) != 1)
178                 udelay(100);
179
180         device->ext = NULL;
181         return stor_device;
182 }
183
184 static int stor_vsc_channel_init(struct hv_device *device)
185 {
186         struct storvsc_device *stor_device;
187         struct storvsc_request_extension *request;
188         struct vstor_packet *vstor_packet;
189         int ret;
190
191         stor_device = get_stor_device(device);
192         if (!stor_device) {
193                 DPRINT_ERR(STORVSC, "unable to get stor device..."
194                            "device being destroyed?");
195                 return -1;
196         }
197
198         request = &stor_device->init_request;
199         vstor_packet = &request->vstor_packet;
200
201         /*
202          * Now, initiate the vsc/vsp initialization protocol on the open
203          * channel
204          */
205         memset(request, 0, sizeof(struct storvsc_request_extension));
206         init_waitqueue_head(&request->wait_event);
207         vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
208         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
209
210         DPRINT_INFO(STORVSC, "BEGIN_INITIALIZATION_OPERATION...");
211
212         request->wait_condition = 0;
213         ret = vmbus_sendpacket(device->channel, vstor_packet,
214                                sizeof(struct vstor_packet),
215                                (unsigned long)request,
216                                VM_PKT_DATA_INBAND,
217                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
218         if (ret != 0) {
219                 DPRINT_ERR(STORVSC,
220                            "unable to send BEGIN_INITIALIZATION_OPERATION");
221                 goto cleanup;
222         }
223
224         wait_event_timeout(request->wait_event, request->wait_condition,
225                         msecs_to_jiffies(1000));
226         if (request->wait_condition == 0) {
227                 ret = -ETIMEDOUT;
228                 goto cleanup;
229         }
230
231
232         if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
233             vstor_packet->status != 0) {
234                 DPRINT_ERR(STORVSC, "BEGIN_INITIALIZATION_OPERATION failed "
235                            "(op %d status 0x%x)",
236                            vstor_packet->operation, vstor_packet->status);
237                 goto cleanup;
238         }
239
240         DPRINT_INFO(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION...");
241
242         /* reuse the packet for version range supported */
243         memset(vstor_packet, 0, sizeof(struct vstor_packet));
244         vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
245         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
246
247         vstor_packet->version.major_minor = VMSTOR_PROTOCOL_VERSION_CURRENT;
248         FILL_VMSTOR_REVISION(vstor_packet->version.revision);
249
250         request->wait_condition = 0;
251         ret = vmbus_sendpacket(device->channel, vstor_packet,
252                                sizeof(struct vstor_packet),
253                                (unsigned long)request,
254                                VM_PKT_DATA_INBAND,
255                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
256         if (ret != 0) {
257                 DPRINT_ERR(STORVSC,
258                            "unable to send BEGIN_INITIALIZATION_OPERATION");
259                 goto cleanup;
260         }
261
262         wait_event_timeout(request->wait_event, request->wait_condition,
263                         msecs_to_jiffies(1000));
264         if (request->wait_condition == 0) {
265                 ret = -ETIMEDOUT;
266                 goto cleanup;
267         }
268
269         /* TODO: Check returned version */
270         if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
271             vstor_packet->status != 0) {
272                 DPRINT_ERR(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION failed "
273                            "(op %d status 0x%x)",
274                            vstor_packet->operation, vstor_packet->status);
275                 goto cleanup;
276         }
277
278         /* Query channel properties */
279         DPRINT_INFO(STORVSC, "QUERY_PROPERTIES_OPERATION...");
280
281         memset(vstor_packet, 0, sizeof(struct vstor_packet));
282         vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
283         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
284         vstor_packet->storage_channel_properties.port_number =
285                                         stor_device->port_number;
286
287         request->wait_condition = 0;
288         ret = vmbus_sendpacket(device->channel, vstor_packet,
289                                sizeof(struct vstor_packet),
290                                (unsigned long)request,
291                                VM_PKT_DATA_INBAND,
292                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
293
294         if (ret != 0) {
295                 DPRINT_ERR(STORVSC,
296                            "unable to send QUERY_PROPERTIES_OPERATION");
297                 goto cleanup;
298         }
299
300         wait_event_timeout(request->wait_event, request->wait_condition,
301                         msecs_to_jiffies(1000));
302         if (request->wait_condition == 0) {
303                 ret = -ETIMEDOUT;
304                 goto cleanup;
305         }
306
307         /* TODO: Check returned version */
308         if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
309             vstor_packet->status != 0) {
310                 DPRINT_ERR(STORVSC, "QUERY_PROPERTIES_OPERATION failed "
311                            "(op %d status 0x%x)",
312                            vstor_packet->operation, vstor_packet->status);
313                 goto cleanup;
314         }
315
316         stor_device->path_id = vstor_packet->storage_channel_properties.path_id;
317         stor_device->target_id
318                 = vstor_packet->storage_channel_properties.target_id;
319
320         DPRINT_DBG(STORVSC, "channel flag 0x%x, max xfer len 0x%x",
321                    vstor_packet->storage_channel_properties.flags,
322                    vstor_packet->storage_channel_properties.max_transfer_bytes);
323
324         DPRINT_INFO(STORVSC, "END_INITIALIZATION_OPERATION...");
325
326         memset(vstor_packet, 0, sizeof(struct vstor_packet));
327         vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
328         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
329
330         request->wait_condition = 0;
331         ret = vmbus_sendpacket(device->channel, vstor_packet,
332                                sizeof(struct vstor_packet),
333                                (unsigned long)request,
334                                VM_PKT_DATA_INBAND,
335                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
336
337         if (ret != 0) {
338                 DPRINT_ERR(STORVSC,
339                            "unable to send END_INITIALIZATION_OPERATION");
340                 goto cleanup;
341         }
342
343         wait_event_timeout(request->wait_event, request->wait_condition,
344                         msecs_to_jiffies(1000));
345         if (request->wait_condition == 0) {
346                 ret = -ETIMEDOUT;
347                 goto cleanup;
348         }
349
350         if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
351             vstor_packet->status != 0) {
352                 DPRINT_ERR(STORVSC, "END_INITIALIZATION_OPERATION failed "
353                            "(op %d status 0x%x)",
354                            vstor_packet->operation, vstor_packet->status);
355                 goto cleanup;
356         }
357
358         DPRINT_INFO(STORVSC, "**** storage channel up and running!! ****");
359
360 cleanup:
361         put_stor_device(device);
362         return ret;
363 }
364
365 static void stor_vsc_on_io_completion(struct hv_device *device,
366                                   struct vstor_packet *vstor_packet,
367                                   struct storvsc_request_extension *request_ext)
368 {
369         struct hv_storvsc_request *request;
370         struct storvsc_device *stor_device;
371
372         stor_device = must_get_stor_device(device);
373         if (!stor_device) {
374                 DPRINT_ERR(STORVSC, "unable to get stor device..."
375                            "device being destroyed?");
376                 return;
377         }
378
379         DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION - request extension %p "
380                    "completed bytes xfer %u", request_ext,
381                    vstor_packet->vm_srb.data_transfer_length);
382
383         /* ASSERT(request_ext != NULL); */
384         /* ASSERT(request_ext->request != NULL); */
385
386         request = request_ext->request;
387
388         /* ASSERT(request->OnIOCompletion != NULL); */
389
390         /* Copy over the status...etc */
391         request->status = vstor_packet->vm_srb.scsi_status;
392
393         if (request->status != 0 || vstor_packet->vm_srb.srb_status != 1) {
394                 DPRINT_WARN(STORVSC,
395                             "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
396                             request->cdb[0], vstor_packet->vm_srb.scsi_status,
397                             vstor_packet->vm_srb.srb_status);
398         }
399
400         if ((request->status & 0xFF) == 0x02) {
401                 /* CHECK_CONDITION */
402                 if (vstor_packet->vm_srb.srb_status & 0x80) {
403                         /* autosense data available */
404                         DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data "
405                                     "valid - len %d\n", request_ext,
406                                     vstor_packet->vm_srb.sense_info_length);
407
408                         /* ASSERT(vstor_packet->vm_srb.sense_info_length <= */
409                         /*      request->SenseBufferSize); */
410                         memcpy(request->sense_buffer,
411                                vstor_packet->vm_srb.sense_data,
412                                vstor_packet->vm_srb.sense_info_length);
413
414                         request->sense_buffer_size =
415                                         vstor_packet->vm_srb.sense_info_length;
416                 }
417         }
418
419         /* TODO: */
420         request->bytes_xfer = vstor_packet->vm_srb.data_transfer_length;
421
422         request->on_io_completion(request);
423
424         atomic_dec(&stor_device->num_outstanding_req);
425
426         put_stor_device(device);
427 }
428
429 static void stor_vsc_on_receive(struct hv_device *device,
430                              struct vstor_packet *vstor_packet,
431                              struct storvsc_request_extension *request_ext)
432 {
433         switch (vstor_packet->operation) {
434         case VSTOR_OPERATION_COMPLETE_IO:
435                 DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION");
436                 stor_vsc_on_io_completion(device, vstor_packet, request_ext);
437                 break;
438         case VSTOR_OPERATION_REMOVE_DEVICE:
439                 DPRINT_INFO(STORVSC, "REMOVE_DEVICE_OPERATION");
440                 /* TODO: */
441                 break;
442
443         default:
444                 DPRINT_INFO(STORVSC, "Unknown operation received - %d",
445                             vstor_packet->operation);
446                 break;
447         }
448 }
449
450 static void stor_vsc_on_channel_callback(void *context)
451 {
452         struct hv_device *device = (struct hv_device *)context;
453         struct storvsc_device *stor_device;
454         u32 bytes_recvd;
455         u64 request_id;
456         unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
457         struct storvsc_request_extension *request;
458         int ret;
459
460         /* ASSERT(device); */
461
462         stor_device = must_get_stor_device(device);
463         if (!stor_device) {
464                 DPRINT_ERR(STORVSC, "unable to get stor device..."
465                            "device being destroyed?");
466                 return;
467         }
468
469         do {
470                 ret = vmbus_recvpacket(device->channel, packet,
471                                        ALIGN(sizeof(struct vstor_packet), 8),
472                                        &bytes_recvd, &request_id);
473                 if (ret == 0 && bytes_recvd > 0) {
474                         DPRINT_DBG(STORVSC, "receive %d bytes - tid %llx",
475                                    bytes_recvd, request_id);
476
477                         /* ASSERT(bytes_recvd ==
478                                         sizeof(struct vstor_packet)); */
479
480                         request = (struct storvsc_request_extension *)
481                                         (unsigned long)request_id;
482                         /* ASSERT(request);c */
483
484                         /* if (vstor_packet.Flags & SYNTHETIC_FLAG) */
485                         if ((request == &stor_device->init_request) ||
486                             (request == &stor_device->reset_request)) {
487                                 /* DPRINT_INFO(STORVSC,
488                                  *             "reset completion - operation "
489                                  *             "%u status %u",
490                                  *             vstor_packet.Operation,
491                                  *             vstor_packet.Status); */
492
493                                 memcpy(&request->vstor_packet, packet,
494                                        sizeof(struct vstor_packet));
495                                 request->wait_condition = 1;
496                                 wake_up(&request->wait_event);
497                         } else {
498                                 stor_vsc_on_receive(device,
499                                                 (struct vstor_packet *)packet,
500                                                 request);
501                         }
502                 } else {
503                         /* DPRINT_DBG(STORVSC, "nothing else to read..."); */
504                         break;
505                 }
506         } while (1);
507
508         put_stor_device(device);
509         return;
510 }
511
512 static int stor_vsc_connect_to_vsp(struct hv_device *device)
513 {
514         struct vmstorage_channel_properties props;
515         struct storvsc_driver_object *stor_driver;
516         int ret;
517
518         stor_driver = (struct storvsc_driver_object *)device->drv;
519         memset(&props, 0, sizeof(struct vmstorage_channel_properties));
520
521         /* Open the channel */
522         ret = vmbus_open(device->channel,
523                          stor_driver->ring_buffer_size,
524                          stor_driver->ring_buffer_size,
525                          (void *)&props,
526                          sizeof(struct vmstorage_channel_properties),
527                          stor_vsc_on_channel_callback, device);
528
529         DPRINT_DBG(STORVSC, "storage props: path id %d, tgt id %d, max xfer %d",
530                    props.path_id, props.target_id, props.max_transfer_bytes);
531
532         if (ret != 0) {
533                 DPRINT_ERR(STORVSC, "unable to open channel: %d", ret);
534                 return -1;
535         }
536
537         ret = stor_vsc_channel_init(device);
538
539         return ret;
540 }
541
542 /*
543  * stor_vsc_on_device_add - Callback when the device belonging to this driver
544  * is added
545  */
546 static int stor_vsc_on_device_add(struct hv_device *device,
547                                         void *additional_info)
548 {
549         struct storvsc_device *stor_device;
550         /* struct vmstorage_channel_properties *props; */
551         struct storvsc_device_info *device_info;
552         int ret = 0;
553
554         device_info = (struct storvsc_device_info *)additional_info;
555         stor_device = alloc_stor_device(device);
556         if (!stor_device) {
557                 ret = -1;
558                 goto cleanup;
559         }
560
561         /* Save the channel properties to our storvsc channel */
562         /* props = (struct vmstorage_channel_properties *)
563          *              channel->offerMsg.Offer.u.Standard.UserDefined; */
564
565         /* FIXME: */
566         /*
567          * If we support more than 1 scsi channel, we need to set the
568          * port number here to the scsi channel but how do we get the
569          * scsi channel prior to the bus scan
570          */
571
572         /* storChannel->PortNumber = 0;
573         storChannel->PathId = props->PathId;
574         storChannel->TargetId = props->TargetId; */
575
576         stor_device->port_number = device_info->port_number;
577         /* Send it back up */
578         ret = stor_vsc_connect_to_vsp(device);
579
580         /* device_info->PortNumber = stor_device->PortNumber; */
581         device_info->path_id = stor_device->path_id;
582         device_info->target_id = stor_device->target_id;
583
584         DPRINT_DBG(STORVSC, "assigned port %u, path %u target %u\n",
585                    stor_device->port_number, stor_device->path_id,
586                    stor_device->target_id);
587
588 cleanup:
589         return ret;
590 }
591
592 /*
593  * stor_vsc_on_device_remove - Callback when the our device is being removed
594  */
595 static int stor_vsc_on_device_remove(struct hv_device *device)
596 {
597         struct storvsc_device *stor_device;
598
599         DPRINT_INFO(STORVSC, "disabling storage device (%p)...",
600                     device->ext);
601
602         stor_device = release_stor_device(device);
603
604         /*
605          * At this point, all outbound traffic should be disable. We
606          * only allow inbound traffic (responses) to proceed so that
607          * outstanding requests can be completed.
608          */
609         while (atomic_read(&stor_device->num_outstanding_req)) {
610                 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...",
611                             atomic_read(&stor_device->num_outstanding_req));
612                 udelay(100);
613         }
614
615         DPRINT_INFO(STORVSC, "removing storage device (%p)...",
616                     device->ext);
617
618         stor_device = final_release_stor_device(device);
619
620         DPRINT_INFO(STORVSC, "storage device (%p) safe to remove", stor_device);
621
622         /* Close the channel */
623         vmbus_close(device->channel);
624
625         free_stor_device(stor_device);
626         return 0;
627 }
628
629 int stor_vsc_on_host_reset(struct hv_device *device)
630 {
631         struct storvsc_device *stor_device;
632         struct storvsc_request_extension *request;
633         struct vstor_packet *vstor_packet;
634         int ret;
635
636         DPRINT_INFO(STORVSC, "resetting host adapter...");
637
638         stor_device = get_stor_device(device);
639         if (!stor_device) {
640                 DPRINT_ERR(STORVSC, "unable to get stor device..."
641                            "device being destroyed?");
642                 return -1;
643         }
644
645         request = &stor_device->reset_request;
646         vstor_packet = &request->vstor_packet;
647
648         init_waitqueue_head(&request->wait_event);
649
650         vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
651         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
652         vstor_packet->vm_srb.path_id = stor_device->path_id;
653
654         request->wait_condition = 0;
655         ret = vmbus_sendpacket(device->channel, vstor_packet,
656                                sizeof(struct vstor_packet),
657                                (unsigned long)&stor_device->reset_request,
658                                VM_PKT_DATA_INBAND,
659                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
660         if (ret != 0) {
661                 DPRINT_ERR(STORVSC, "Unable to send reset packet %p ret %d",
662                            vstor_packet, ret);
663                 goto cleanup;
664         }
665
666         wait_event_timeout(request->wait_event, request->wait_condition,
667                         msecs_to_jiffies(1000));
668         if (request->wait_condition == 0) {
669                 ret = -ETIMEDOUT;
670                 goto cleanup;
671         }
672
673         DPRINT_INFO(STORVSC, "host adapter reset completed");
674
675         /*
676          * At this point, all outstanding requests in the adapter
677          * should have been flushed out and return to us
678          */
679
680 cleanup:
681         put_stor_device(device);
682         return ret;
683 }
684
685 /*
686  * stor_vsc_on_io_request - Callback to initiate an I/O request
687  */
688 static int stor_vsc_on_io_request(struct hv_device *device,
689                               struct hv_storvsc_request *request)
690 {
691         struct storvsc_device *stor_device;
692         struct storvsc_request_extension *request_extension;
693         struct vstor_packet *vstor_packet;
694         int ret = 0;
695
696         request_extension =
697                 (struct storvsc_request_extension *)request->extension;
698         vstor_packet = &request_extension->vstor_packet;
699         stor_device = get_stor_device(device);
700
701         DPRINT_DBG(STORVSC, "enter - Device %p, DeviceExt %p, Request %p, "
702                    "Extension %p", device, stor_device, request,
703                    request_extension);
704
705         DPRINT_DBG(STORVSC, "req %p len %d bus %d, target %d, lun %d cdblen %d",
706                    request, request->data_buffer.len, request->bus,
707                    request->target_id, request->lun_id, request->cdb_len);
708
709         if (!stor_device) {
710                 DPRINT_ERR(STORVSC, "unable to get stor device..."
711                            "device being destroyed?");
712                 return -2;
713         }
714
715         /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, request->Cdb,
716          *                      request->CdbLen); */
717
718         request_extension->request = request;
719         request_extension->device  = device;
720
721         memset(vstor_packet, 0 , sizeof(struct vstor_packet));
722
723         vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
724
725         vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
726
727         vstor_packet->vm_srb.port_number = request->host;
728         vstor_packet->vm_srb.path_id = request->bus;
729         vstor_packet->vm_srb.target_id = request->target_id;
730         vstor_packet->vm_srb.lun = request->lun_id;
731
732         vstor_packet->vm_srb.sense_info_length = SENSE_BUFFER_SIZE;
733
734         /* Copy over the scsi command descriptor block */
735         vstor_packet->vm_srb.cdb_length = request->cdb_len;
736         memcpy(&vstor_packet->vm_srb.cdb, request->cdb, request->cdb_len);
737
738         vstor_packet->vm_srb.data_in = request->type;
739         vstor_packet->vm_srb.data_transfer_length = request->data_buffer.len;
740
741         vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
742
743         DPRINT_DBG(STORVSC, "srb - len %d port %d, path %d, target %d, "
744                    "lun %d senselen %d cdblen %d",
745                    vstor_packet->vm_srb.length,
746                    vstor_packet->vm_srb.port_number,
747                    vstor_packet->vm_srb.path_id,
748                    vstor_packet->vm_srb.target_id,
749                    vstor_packet->vm_srb.lun,
750                    vstor_packet->vm_srb.sense_info_length,
751                    vstor_packet->vm_srb.cdb_length);
752
753         if (request_extension->request->data_buffer.len) {
754                 ret = vmbus_sendpacket_multipagebuffer(device->channel,
755                                 &request_extension->request->data_buffer,
756                                 vstor_packet,
757                                 sizeof(struct vstor_packet),
758                                 (unsigned long)request_extension);
759         } else {
760                 ret = vmbus_sendpacket(device->channel, vstor_packet,
761                                        sizeof(struct vstor_packet),
762                                        (unsigned long)request_extension,
763                                        VM_PKT_DATA_INBAND,
764                                        VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
765         }
766
767         if (ret != 0) {
768                 DPRINT_DBG(STORVSC, "Unable to send packet %p ret %d",
769                            vstor_packet, ret);
770         }
771
772         atomic_inc(&stor_device->num_outstanding_req);
773
774         put_stor_device(device);
775         return ret;
776 }
777
778 /*
779  * stor_vsc_on_cleanup - Perform any cleanup when the driver is removed
780  */
781 static void stor_vsc_on_cleanup(struct hv_driver *driver)
782 {
783 }
784
785 /*
786  * stor_vsc_initialize - Main entry point
787  */
788 int stor_vsc_initialize(struct hv_driver *driver)
789 {
790         struct storvsc_driver_object *stor_driver;
791
792         stor_driver = (struct storvsc_driver_object *)driver;
793
794         DPRINT_DBG(STORVSC, "sizeof(STORVSC_REQUEST)=%zd "
795                    "sizeof(struct storvsc_request_extension)=%zd "
796                    "sizeof(struct vstor_packet)=%zd, "
797                    "sizeof(struct vmscsi_request)=%zd",
798                    sizeof(struct hv_storvsc_request),
799                    sizeof(struct storvsc_request_extension),
800                    sizeof(struct vstor_packet),
801                    sizeof(struct vmscsi_request));
802
803         /* Make sure we are at least 2 pages since 1 page is used for control */
804         /* ASSERT(stor_driver->RingBufferSize >= (PAGE_SIZE << 1)); */
805
806         driver->name = g_driver_name;
807         memcpy(&driver->dev_type, &gStorVscDeviceType,
808                sizeof(struct hv_guid));
809
810         stor_driver->request_ext_size =
811                         sizeof(struct storvsc_request_extension);
812
813         /*
814          * Divide the ring buffer data size (which is 1 page less
815          * than the ring buffer size since that page is reserved for
816          * the ring buffer indices) by the max request size (which is
817          * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
818          */
819         stor_driver->max_outstanding_req_per_channel =
820                 ((stor_driver->ring_buffer_size - PAGE_SIZE) /
821                   ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
822                            sizeof(struct vstor_packet) + sizeof(u64),
823                            sizeof(u64)));
824
825         DPRINT_INFO(STORVSC, "max io %u, currently %u\n",
826                     stor_driver->max_outstanding_req_per_channel,
827                     STORVSC_MAX_IO_REQUESTS);
828
829         /* Setup the dispatch table */
830         stor_driver->base.dev_add       = stor_vsc_on_device_add;
831         stor_driver->base.dev_rm        = stor_vsc_on_device_remove;
832         stor_driver->base.cleanup       = stor_vsc_on_cleanup;
833
834         stor_driver->on_io_request      = stor_vsc_on_io_request;
835
836         return 0;
837 }