]> Pileus Git - ~andy/linux/blob - drivers/staging/hv/storvsc.c
Staging: hv: Get rid of IDE details from blkvsc_drv.c
[~andy/linux] / 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  * 4/3/2011: K. Y. Srinivasan - Significant restructuring and cleanup.
22  */
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/completion.h>
26 #include <linux/string.h>
27 #include <linux/slab.h>
28 #include <linux/mm.h>
29 #include <linux/delay.h>
30 #include "hv_api.h"
31 #include "logging.h"
32 #include "storvsc_api.h"
33 #include "vmbus_packet_format.h"
34 #include "vstorage.h"
35 #include "channel.h"
36
37
38 static inline struct storvsc_device *alloc_stor_device(struct hv_device *device)
39 {
40         struct storvsc_device *stor_device;
41
42         stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
43         if (!stor_device)
44                 return NULL;
45
46         /* Set to 2 to allow both inbound and outbound traffics */
47         /* (ie get_stor_device() and must_get_stor_device()) to proceed. */
48         atomic_cmpxchg(&stor_device->ref_count, 0, 2);
49
50         stor_device->device = device;
51         device->ext = stor_device;
52
53         return stor_device;
54 }
55
56 static inline void free_stor_device(struct storvsc_device *device)
57 {
58         kfree(device);
59 }
60
61 /* Get the stordevice object iff exists and its refcount > 0 */
62 static inline struct storvsc_device *must_get_stor_device(
63                                         struct hv_device *device)
64 {
65         struct storvsc_device *stor_device;
66
67         stor_device = (struct storvsc_device *)device->ext;
68         if (stor_device && atomic_read(&stor_device->ref_count))
69                 atomic_inc(&stor_device->ref_count);
70         else
71                 stor_device = NULL;
72
73         return stor_device;
74 }
75
76 /* Drop ref count to 1 to effectively disable get_stor_device() */
77 static inline struct storvsc_device *release_stor_device(
78                                         struct hv_device *device)
79 {
80         struct storvsc_device *stor_device;
81
82         stor_device = (struct storvsc_device *)device->ext;
83
84         /* Busy wait until the ref drop to 2, then set it to 1 */
85         while (atomic_cmpxchg(&stor_device->ref_count, 2, 1) != 2)
86                 udelay(100);
87
88         return stor_device;
89 }
90
91 /* Drop ref count to 0. No one can use stor_device object. */
92 static inline struct storvsc_device *final_release_stor_device(
93                         struct hv_device *device)
94 {
95         struct storvsc_device *stor_device;
96
97         stor_device = (struct storvsc_device *)device->ext;
98
99         /* Busy wait until the ref drop to 1, then set it to 0 */
100         while (atomic_cmpxchg(&stor_device->ref_count, 1, 0) != 1)
101                 udelay(100);
102
103         device->ext = NULL;
104         return stor_device;
105 }
106
107 static int stor_vsc_channel_init(struct hv_device *device)
108 {
109         struct storvsc_device *stor_device;
110         struct hv_storvsc_request *request;
111         struct vstor_packet *vstor_packet;
112         int ret, t;
113
114         stor_device = get_stor_device(device);
115         if (!stor_device) {
116                 DPRINT_ERR(STORVSC, "unable to get stor device..."
117                            "device being destroyed?");
118                 return -1;
119         }
120
121         request = &stor_device->init_request;
122         vstor_packet = &request->vstor_packet;
123
124         /*
125          * Now, initiate the vsc/vsp initialization protocol on the open
126          * channel
127          */
128         memset(request, 0, sizeof(struct hv_storvsc_request));
129         init_completion(&request->wait_event);
130         vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
131         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
132
133         DPRINT_INFO(STORVSC, "BEGIN_INITIALIZATION_OPERATION...");
134
135         ret = vmbus_sendpacket(device->channel, vstor_packet,
136                                sizeof(struct vstor_packet),
137                                (unsigned long)request,
138                                VM_PKT_DATA_INBAND,
139                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
140         if (ret != 0) {
141                 DPRINT_ERR(STORVSC,
142                            "unable to send BEGIN_INITIALIZATION_OPERATION");
143                 goto cleanup;
144         }
145
146         t = wait_for_completion_timeout(&request->wait_event, HZ);
147         if (t == 0) {
148                 ret = -ETIMEDOUT;
149                 goto cleanup;
150         }
151
152         if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
153             vstor_packet->status != 0) {
154                 DPRINT_ERR(STORVSC, "BEGIN_INITIALIZATION_OPERATION failed "
155                            "(op %d status 0x%x)",
156                            vstor_packet->operation, vstor_packet->status);
157                 goto cleanup;
158         }
159
160         DPRINT_INFO(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION...");
161
162         /* reuse the packet for version range supported */
163         memset(vstor_packet, 0, sizeof(struct vstor_packet));
164         vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
165         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
166
167         vstor_packet->version.major_minor = VMSTOR_PROTOCOL_VERSION_CURRENT;
168         FILL_VMSTOR_REVISION(vstor_packet->version.revision);
169
170         ret = vmbus_sendpacket(device->channel, vstor_packet,
171                                sizeof(struct vstor_packet),
172                                (unsigned long)request,
173                                VM_PKT_DATA_INBAND,
174                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
175         if (ret != 0) {
176                 DPRINT_ERR(STORVSC,
177                            "unable to send BEGIN_INITIALIZATION_OPERATION");
178                 goto cleanup;
179         }
180
181         t = wait_for_completion_timeout(&request->wait_event, HZ);
182         if (t == 0) {
183                 ret = -ETIMEDOUT;
184                 goto cleanup;
185         }
186
187         /* TODO: Check returned version */
188         if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
189             vstor_packet->status != 0) {
190                 DPRINT_ERR(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION failed "
191                            "(op %d status 0x%x)",
192                            vstor_packet->operation, vstor_packet->status);
193                 goto cleanup;
194         }
195
196         /* Query channel properties */
197         DPRINT_INFO(STORVSC, "QUERY_PROPERTIES_OPERATION...");
198
199         memset(vstor_packet, 0, sizeof(struct vstor_packet));
200         vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
201         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
202         vstor_packet->storage_channel_properties.port_number =
203                                         stor_device->port_number;
204
205         ret = vmbus_sendpacket(device->channel, vstor_packet,
206                                sizeof(struct vstor_packet),
207                                (unsigned long)request,
208                                VM_PKT_DATA_INBAND,
209                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
210
211         if (ret != 0) {
212                 DPRINT_ERR(STORVSC,
213                            "unable to send QUERY_PROPERTIES_OPERATION");
214                 goto cleanup;
215         }
216
217         t = wait_for_completion_timeout(&request->wait_event, HZ);
218         if (t == 0) {
219                 ret = -ETIMEDOUT;
220                 goto cleanup;
221         }
222
223         /* TODO: Check returned version */
224         if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
225             vstor_packet->status != 0) {
226                 DPRINT_ERR(STORVSC, "QUERY_PROPERTIES_OPERATION failed "
227                            "(op %d status 0x%x)",
228                            vstor_packet->operation, vstor_packet->status);
229                 goto cleanup;
230         }
231
232         stor_device->path_id = vstor_packet->storage_channel_properties.path_id;
233         stor_device->target_id
234                 = vstor_packet->storage_channel_properties.target_id;
235
236         DPRINT_DBG(STORVSC, "channel flag 0x%x, max xfer len 0x%x",
237                    vstor_packet->storage_channel_properties.flags,
238                    vstor_packet->storage_channel_properties.max_transfer_bytes);
239
240         DPRINT_INFO(STORVSC, "END_INITIALIZATION_OPERATION...");
241
242         memset(vstor_packet, 0, sizeof(struct vstor_packet));
243         vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
244         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
245
246         ret = vmbus_sendpacket(device->channel, vstor_packet,
247                                sizeof(struct vstor_packet),
248                                (unsigned long)request,
249                                VM_PKT_DATA_INBAND,
250                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
251
252         if (ret != 0) {
253                 DPRINT_ERR(STORVSC,
254                            "unable to send END_INITIALIZATION_OPERATION");
255                 goto cleanup;
256         }
257
258         t = wait_for_completion_timeout(&request->wait_event, HZ);
259         if (t == 0) {
260                 ret = -ETIMEDOUT;
261                 goto cleanup;
262         }
263
264         if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
265             vstor_packet->status != 0) {
266                 DPRINT_ERR(STORVSC, "END_INITIALIZATION_OPERATION failed "
267                            "(op %d status 0x%x)",
268                            vstor_packet->operation, vstor_packet->status);
269                 goto cleanup;
270         }
271
272         DPRINT_INFO(STORVSC, "**** storage channel up and running!! ****");
273
274 cleanup:
275         put_stor_device(device);
276         return ret;
277 }
278
279 static void stor_vsc_on_io_completion(struct hv_device *device,
280                                   struct vstor_packet *vstor_packet,
281                                   struct hv_storvsc_request *request)
282 {
283         struct storvsc_device *stor_device;
284         struct vstor_packet *stor_pkt;
285
286         stor_device = must_get_stor_device(device);
287         if (!stor_device) {
288                 DPRINT_ERR(STORVSC, "unable to get stor device..."
289                            "device being destroyed?");
290                 return;
291         }
292
293         DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION - request %p "
294                    "completed bytes xfer %u", request,
295                    vstor_packet->vm_srb.data_transfer_length);
296
297         stor_pkt = &request->vstor_packet;
298
299
300         /* Copy over the status...etc */
301         stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
302         stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
303         stor_pkt->vm_srb.sense_info_length =
304         vstor_packet->vm_srb.sense_info_length;
305
306         if (vstor_packet->vm_srb.scsi_status != 0 ||
307                 vstor_packet->vm_srb.srb_status != 1) {
308                 DPRINT_WARN(STORVSC,
309                             "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
310                             stor_pkt->vm_srb.cdb[0],
311                             vstor_packet->vm_srb.scsi_status,
312                             vstor_packet->vm_srb.srb_status);
313         }
314
315         if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
316                 /* CHECK_CONDITION */
317                 if (vstor_packet->vm_srb.srb_status & 0x80) {
318                         /* autosense data available */
319                         DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data "
320                                     "valid - len %d\n", request,
321                                     vstor_packet->vm_srb.sense_info_length);
322
323                         memcpy(request->sense_buffer,
324                                vstor_packet->vm_srb.sense_data,
325                                vstor_packet->vm_srb.sense_info_length);
326
327                 }
328         }
329
330         stor_pkt->vm_srb.data_transfer_length =
331         vstor_packet->vm_srb.data_transfer_length;
332
333         request->on_io_completion(request);
334
335         atomic_dec(&stor_device->num_outstanding_req);
336
337         put_stor_device(device);
338 }
339
340 static void stor_vsc_on_receive(struct hv_device *device,
341                              struct vstor_packet *vstor_packet,
342                              struct hv_storvsc_request *request)
343 {
344         switch (vstor_packet->operation) {
345         case VSTOR_OPERATION_COMPLETE_IO:
346                 DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION");
347                 stor_vsc_on_io_completion(device, vstor_packet, request);
348                 break;
349         case VSTOR_OPERATION_REMOVE_DEVICE:
350                 DPRINT_INFO(STORVSC, "REMOVE_DEVICE_OPERATION");
351                 /* TODO: */
352                 break;
353
354         default:
355                 DPRINT_INFO(STORVSC, "Unknown operation received - %d",
356                             vstor_packet->operation);
357                 break;
358         }
359 }
360
361 static void stor_vsc_on_channel_callback(void *context)
362 {
363         struct hv_device *device = (struct hv_device *)context;
364         struct storvsc_device *stor_device;
365         u32 bytes_recvd;
366         u64 request_id;
367         unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
368         struct hv_storvsc_request *request;
369         int ret;
370
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         do {
380                 ret = vmbus_recvpacket(device->channel, packet,
381                                        ALIGN(sizeof(struct vstor_packet), 8),
382                                        &bytes_recvd, &request_id);
383                 if (ret == 0 && bytes_recvd > 0) {
384                         DPRINT_DBG(STORVSC, "receive %d bytes - tid %llx",
385                                    bytes_recvd, request_id);
386
387
388                         request = (struct hv_storvsc_request *)
389                                         (unsigned long)request_id;
390
391                         if ((request == &stor_device->init_request) ||
392                             (request == &stor_device->reset_request)) {
393
394                                 memcpy(&request->vstor_packet, packet,
395                                        sizeof(struct vstor_packet));
396                                 complete(&request->wait_event);
397                         } else {
398                                 stor_vsc_on_receive(device,
399                                                 (struct vstor_packet *)packet,
400                                                 request);
401                         }
402                 } else {
403                         break;
404                 }
405         } while (1);
406
407         put_stor_device(device);
408         return;
409 }
410
411 static int stor_vsc_connect_to_vsp(struct hv_device *device)
412 {
413         struct vmstorage_channel_properties props;
414         struct storvsc_driver_object *stor_driver;
415         int ret;
416
417         stor_driver = (struct storvsc_driver_object *)device->drv;
418         memset(&props, 0, sizeof(struct vmstorage_channel_properties));
419
420         /* Open the channel */
421         ret = vmbus_open(device->channel,
422                          stor_driver->ring_buffer_size,
423                          stor_driver->ring_buffer_size,
424                          (void *)&props,
425                          sizeof(struct vmstorage_channel_properties),
426                          stor_vsc_on_channel_callback, device);
427
428         DPRINT_DBG(STORVSC, "storage props: path id %d, tgt id %d, max xfer %d",
429                    props.path_id, props.target_id, props.max_transfer_bytes);
430
431         if (ret != 0) {
432                 DPRINT_ERR(STORVSC, "unable to open channel: %d", ret);
433                 return -1;
434         }
435
436         ret = stor_vsc_channel_init(device);
437
438         return ret;
439 }
440
441 /*
442  * stor_vsc_on_device_add - Callback when the device belonging to this driver
443  * is added
444  */
445 int stor_vsc_on_device_add(struct hv_device *device,
446                                         void *additional_info)
447 {
448         struct storvsc_device *stor_device;
449         struct storvsc_device_info *device_info;
450         int ret = 0;
451
452         device_info = (struct storvsc_device_info *)additional_info;
453         stor_device = alloc_stor_device(device);
454         if (!stor_device) {
455                 ret = -1;
456                 goto cleanup;
457         }
458
459         /* Save the channel properties to our storvsc channel */
460
461         /* FIXME: */
462         /*
463          * If we support more than 1 scsi channel, we need to set the
464          * port number here to the scsi channel but how do we get the
465          * scsi channel prior to the bus scan
466          */
467
468         stor_device->port_number = device_info->port_number;
469         /* Send it back up */
470         ret = stor_vsc_connect_to_vsp(device);
471
472         device_info->path_id = stor_device->path_id;
473         device_info->target_id = stor_device->target_id;
474
475         DPRINT_DBG(STORVSC, "assigned port %u, path %u target %u\n",
476                    stor_device->port_number, stor_device->path_id,
477                    stor_device->target_id);
478
479 cleanup:
480         return ret;
481 }
482
483 /*
484  * stor_vsc_on_device_remove - Callback when the our device is being removed
485  */
486 int stor_vsc_on_device_remove(struct hv_device *device)
487 {
488         struct storvsc_device *stor_device;
489
490         DPRINT_INFO(STORVSC, "disabling storage device (%p)...",
491                     device->ext);
492
493         stor_device = release_stor_device(device);
494
495         /*
496          * At this point, all outbound traffic should be disable. We
497          * only allow inbound traffic (responses) to proceed so that
498          * outstanding requests can be completed.
499          */
500         while (atomic_read(&stor_device->num_outstanding_req)) {
501                 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...",
502                             atomic_read(&stor_device->num_outstanding_req));
503                 udelay(100);
504         }
505
506         DPRINT_INFO(STORVSC, "removing storage device (%p)...",
507                     device->ext);
508
509         stor_device = final_release_stor_device(device);
510
511         DPRINT_INFO(STORVSC, "storage device (%p) safe to remove", stor_device);
512
513         /* Close the channel */
514         vmbus_close(device->channel);
515
516         free_stor_device(stor_device);
517         return 0;
518 }
519
520 /*
521  * stor_vsc_on_io_request - Callback to initiate an I/O request
522  */
523 int stor_vsc_on_io_request(struct hv_device *device,
524                               struct hv_storvsc_request *request)
525 {
526         struct storvsc_device *stor_device;
527         struct vstor_packet *vstor_packet;
528         int ret = 0;
529
530         vstor_packet = &request->vstor_packet;
531         stor_device = get_stor_device(device);
532
533         DPRINT_DBG(STORVSC, "enter - Device %p, DeviceExt %p, Request %p, "
534                    , device, stor_device, request);
535
536         DPRINT_DBG(STORVSC, "req %p len %d",
537                    request, request->data_buffer.len);
538
539         if (!stor_device) {
540                 DPRINT_ERR(STORVSC, "unable to get stor device..."
541                            "device being destroyed?");
542                 return -2;
543         }
544
545
546         request->device  = device;
547
548
549         vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
550
551         vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
552
553
554         vstor_packet->vm_srb.sense_info_length = SENSE_BUFFER_SIZE;
555
556
557         vstor_packet->vm_srb.data_transfer_length =
558         request->data_buffer.len;
559
560         vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
561
562         DPRINT_DBG(STORVSC, "srb - len %d port %d, path %d, target %d, "
563                    "lun %d senselen %d cdblen %d",
564                    vstor_packet->vm_srb.length,
565                    vstor_packet->vm_srb.port_number,
566                    vstor_packet->vm_srb.path_id,
567                    vstor_packet->vm_srb.target_id,
568                    vstor_packet->vm_srb.lun,
569                    vstor_packet->vm_srb.sense_info_length,
570                    vstor_packet->vm_srb.cdb_length);
571
572         if (request->data_buffer.len) {
573                 ret = vmbus_sendpacket_multipagebuffer(device->channel,
574                                 &request->data_buffer,
575                                 vstor_packet,
576                                 sizeof(struct vstor_packet),
577                                 (unsigned long)request);
578         } else {
579                 ret = vmbus_sendpacket(device->channel, vstor_packet,
580                                        sizeof(struct vstor_packet),
581                                        (unsigned long)request,
582                                        VM_PKT_DATA_INBAND,
583                                        VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
584         }
585
586         if (ret != 0) {
587                 DPRINT_DBG(STORVSC, "Unable to send packet %p ret %d",
588                            vstor_packet, ret);
589         }
590
591         atomic_inc(&stor_device->num_outstanding_req);
592
593         put_stor_device(device);
594         return ret;
595 }
596
597 /*
598  * The channel properties uniquely specify how the device is to be
599  * presented to the guest. Map this information for use by the block
600  * driver. For Linux guests on Hyper-V, we emulate a scsi HBA in the guest
601  * (storvsc_drv) and so scsi devices in the guest  are handled by
602  * native upper level Linux drivers. Consequently, Hyper-V
603  * block driver, while being a generic block driver, presently does not
604  * deal with anything other than devices that would need to be presented
605  * to the guest as an IDE disk.
606  *
607  * This function maps the channel properties as embedded in the input
608  * parameter device_info onto information necessary to register the
609  * corresponding block device.
610  *
611  * Currently, there is no way to stop the emulation of the block device
612  * on the host side. And so, to prevent the native IDE drivers in Linux
613  * from taking over these devices (to be managedby Hyper-V block
614  * driver), we will take over if need be the major of the IDE controllers.
615  *
616  */
617
618 int stor_vsc_get_major_info(struct storvsc_device_info *device_info,
619                             struct storvsc_major_info *major_info)
620 {
621         static bool ide0_registered;
622         static bool ide1_registered;
623
624         /*
625          * For now we only support IDE disks.
626          */
627         major_info->devname = "ide";
628         major_info->diskname = "hd";
629
630         if (device_info->path_id) {
631                 major_info->major = 22;
632                 if (!ide1_registered) {
633                         major_info->do_register = true;
634                         ide1_registered = true;
635                 } else
636                         major_info->do_register = false;
637
638                 if (device_info->target_id)
639                         major_info->index = 3;
640                 else
641                         major_info->index = 2;
642
643                 return 0;
644         } else {
645                 major_info->major = 3;
646                 if (!ide0_registered) {
647                         major_info->do_register = true;
648                         ide0_registered = true;
649                 } else
650                         major_info->do_register = false;
651
652                 if (device_info->target_id)
653                         major_info->index = 1;
654                 else
655                         major_info->index = 0;
656
657                 return 0;
658         }
659
660         return -ENODEV;
661 }
662
663 /*
664  * stor_vsc_on_cleanup - Perform any cleanup when the driver is removed
665  */
666 void stor_vsc_on_cleanup(struct hv_driver *driver)
667 {
668 }
669