]> Pileus Git - ~andy/linux/blob - drivers/staging/hv/storvsc.c
1976a34102b888ca5e53cabb2ab1f272a5e2da84
[~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  *   K. Y. Srinivasan <kys@microsoft.com>
21  *
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
31 #include "hyperv.h"
32 #include "hyperv_storage.h"
33
34
35 static inline struct storvsc_device *alloc_stor_device(struct hv_device *device)
36 {
37         struct storvsc_device *stor_device;
38
39         stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
40         if (!stor_device)
41                 return NULL;
42
43         /* Set to 2 to allow both inbound and outbound traffics */
44         /* (ie get_out_stor_device() and get_in_stor_device()) to proceed. */
45         atomic_set(&stor_device->ref_count, 2);
46         stor_device->destroy = false;
47         init_waitqueue_head(&stor_device->waiting_to_drain);
48         stor_device->device = device;
49         device->ext = stor_device;
50
51         return stor_device;
52 }
53
54
55 /* Get the stordevice object iff exists and its refcount > 0 */
56 static inline struct storvsc_device *get_in_stor_device(
57                                         struct hv_device *device)
58 {
59         struct storvsc_device *stor_device;
60
61         stor_device = (struct storvsc_device *)device->ext;
62         if (stor_device && atomic_read(&stor_device->ref_count))
63                 atomic_inc(&stor_device->ref_count);
64         else
65                 stor_device = NULL;
66
67         return stor_device;
68 }
69
70 /* Drop ref count to 0. No one can use stor_device object. */
71 static inline struct storvsc_device *final_release_stor_device(
72                         struct hv_device *device)
73 {
74         struct storvsc_device *stor_device;
75
76         stor_device = (struct storvsc_device *)device->ext;
77
78         /* Busy wait until the ref drop to 1, then set it to 0 */
79         while (atomic_cmpxchg(&stor_device->ref_count, 1, 0) != 1)
80                 udelay(100);
81
82         device->ext = NULL;
83         return stor_device;
84 }
85
86 static int storvsc_channel_init(struct hv_device *device)
87 {
88         struct storvsc_device *stor_device;
89         struct hv_storvsc_request *request;
90         struct vstor_packet *vstor_packet;
91         int ret, t;
92
93         stor_device = get_out_stor_device(device);
94         if (!stor_device)
95                 return -ENODEV;
96
97         request = &stor_device->init_request;
98         vstor_packet = &request->vstor_packet;
99
100         /*
101          * Now, initiate the vsc/vsp initialization protocol on the open
102          * channel
103          */
104         memset(request, 0, sizeof(struct hv_storvsc_request));
105         init_completion(&request->wait_event);
106         vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
107         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
108
109         ret = vmbus_sendpacket(device->channel, vstor_packet,
110                                sizeof(struct vstor_packet),
111                                (unsigned long)request,
112                                VM_PKT_DATA_INBAND,
113                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
114         if (ret != 0)
115                 goto cleanup;
116
117         t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
118         if (t == 0) {
119                 ret = -ETIMEDOUT;
120                 goto cleanup;
121         }
122
123         if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
124             vstor_packet->status != 0)
125                 goto cleanup;
126
127
128         /* reuse the packet for version range supported */
129         memset(vstor_packet, 0, sizeof(struct vstor_packet));
130         vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
131         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
132
133         vstor_packet->version.major_minor = VMSTOR_PROTOCOL_VERSION_CURRENT;
134         FILL_VMSTOR_REVISION(vstor_packet->version.revision);
135
136         ret = vmbus_sendpacket(device->channel, vstor_packet,
137                                sizeof(struct vstor_packet),
138                                (unsigned long)request,
139                                VM_PKT_DATA_INBAND,
140                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
141         if (ret != 0)
142                 goto cleanup;
143
144         t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
145         if (t == 0) {
146                 ret = -ETIMEDOUT;
147                 goto cleanup;
148         }
149
150         if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
151             vstor_packet->status != 0)
152                 goto cleanup;
153
154
155         memset(vstor_packet, 0, sizeof(struct vstor_packet));
156         vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
157         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
158         vstor_packet->storage_channel_properties.port_number =
159                                         stor_device->port_number;
160
161         ret = vmbus_sendpacket(device->channel, vstor_packet,
162                                sizeof(struct vstor_packet),
163                                (unsigned long)request,
164                                VM_PKT_DATA_INBAND,
165                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
166
167         if (ret != 0)
168                 goto cleanup;
169
170         t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
171         if (t == 0) {
172                 ret = -ETIMEDOUT;
173                 goto cleanup;
174         }
175
176         if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
177             vstor_packet->status != 0)
178                 goto cleanup;
179
180         stor_device->path_id = vstor_packet->storage_channel_properties.path_id;
181         stor_device->target_id
182                 = vstor_packet->storage_channel_properties.target_id;
183
184         memset(vstor_packet, 0, sizeof(struct vstor_packet));
185         vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
186         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
187
188         ret = vmbus_sendpacket(device->channel, vstor_packet,
189                                sizeof(struct vstor_packet),
190                                (unsigned long)request,
191                                VM_PKT_DATA_INBAND,
192                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
193
194         if (ret != 0)
195                 goto cleanup;
196
197         t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
198         if (t == 0) {
199                 ret = -ETIMEDOUT;
200                 goto cleanup;
201         }
202
203         if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
204             vstor_packet->status != 0)
205                 goto cleanup;
206
207
208 cleanup:
209         put_stor_device(device);
210         return ret;
211 }
212
213 static void storvsc_on_io_completion(struct hv_device *device,
214                                   struct vstor_packet *vstor_packet,
215                                   struct hv_storvsc_request *request)
216 {
217         struct storvsc_device *stor_device;
218         struct vstor_packet *stor_pkt;
219
220         stor_device = (struct storvsc_device *)device->ext;
221
222         stor_pkt = &request->vstor_packet;
223
224
225         /* Copy over the status...etc */
226         stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
227         stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
228         stor_pkt->vm_srb.sense_info_length =
229         vstor_packet->vm_srb.sense_info_length;
230
231         if (vstor_packet->vm_srb.scsi_status != 0 ||
232                 vstor_packet->vm_srb.srb_status != 1){
233                 DPRINT_WARN(STORVSC,
234                             "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
235                             stor_pkt->vm_srb.cdb[0],
236                             vstor_packet->vm_srb.scsi_status,
237                             vstor_packet->vm_srb.srb_status);
238         }
239
240         if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
241                 /* CHECK_CONDITION */
242                 if (vstor_packet->vm_srb.srb_status & 0x80) {
243                         /* autosense data available */
244                         DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data "
245                                     "valid - len %d\n", request,
246                                     vstor_packet->vm_srb.sense_info_length);
247
248                         memcpy(request->sense_buffer,
249                                vstor_packet->vm_srb.sense_data,
250                                vstor_packet->vm_srb.sense_info_length);
251
252                 }
253         }
254
255         stor_pkt->vm_srb.data_transfer_length =
256         vstor_packet->vm_srb.data_transfer_length;
257
258         request->on_io_completion(request);
259
260         if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
261                 stor_device->drain_notify)
262                 wake_up(&stor_device->waiting_to_drain);
263
264
265 }
266
267 static void storvsc_on_receive(struct hv_device *device,
268                              struct vstor_packet *vstor_packet,
269                              struct hv_storvsc_request *request)
270 {
271         switch (vstor_packet->operation) {
272         case VSTOR_OPERATION_COMPLETE_IO:
273                 storvsc_on_io_completion(device, vstor_packet, request);
274                 break;
275         case VSTOR_OPERATION_REMOVE_DEVICE:
276
277         default:
278                 break;
279         }
280 }
281
282 static void storvsc_on_channel_callback(void *context)
283 {
284         struct hv_device *device = (struct hv_device *)context;
285         struct storvsc_device *stor_device;
286         u32 bytes_recvd;
287         u64 request_id;
288         unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
289         struct hv_storvsc_request *request;
290         int ret;
291
292
293         stor_device = get_in_stor_device(device);
294         if (!stor_device)
295                 return;
296
297         do {
298                 ret = vmbus_recvpacket(device->channel, packet,
299                                        ALIGN(sizeof(struct vstor_packet), 8),
300                                        &bytes_recvd, &request_id);
301                 if (ret == 0 && bytes_recvd > 0) {
302
303                         request = (struct hv_storvsc_request *)
304                                         (unsigned long)request_id;
305
306                         if ((request == &stor_device->init_request) ||
307                             (request == &stor_device->reset_request)) {
308
309                                 memcpy(&request->vstor_packet, packet,
310                                        sizeof(struct vstor_packet));
311                                 complete(&request->wait_event);
312                         } else {
313                                 storvsc_on_receive(device,
314                                                 (struct vstor_packet *)packet,
315                                                 request);
316                         }
317                 } else {
318                         break;
319                 }
320         } while (1);
321
322         put_stor_device(device);
323         return;
324 }
325
326 static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
327 {
328         struct vmstorage_channel_properties props;
329         int ret;
330
331         memset(&props, 0, sizeof(struct vmstorage_channel_properties));
332
333         /* Open the channel */
334         ret = vmbus_open(device->channel,
335                          ring_size,
336                          ring_size,
337                          (void *)&props,
338                          sizeof(struct vmstorage_channel_properties),
339                          storvsc_on_channel_callback, device);
340
341         if (ret != 0)
342                 return ret;
343
344         ret = storvsc_channel_init(device);
345
346         return ret;
347 }
348
349 int storvsc_dev_add(struct hv_device *device,
350                                         void *additional_info)
351 {
352         struct storvsc_device *stor_device;
353         struct storvsc_device_info *device_info;
354         int ret = 0;
355
356         device_info = (struct storvsc_device_info *)additional_info;
357         stor_device = alloc_stor_device(device);
358         if (!stor_device)
359                 return -ENOMEM;
360
361         /* Save the channel properties to our storvsc channel */
362
363         /*
364          * If we support more than 1 scsi channel, we need to set the
365          * port number here to the scsi channel but how do we get the
366          * scsi channel prior to the bus scan.
367          *
368          * The host does not support this.
369          */
370
371         stor_device->port_number = device_info->port_number;
372         /* Send it back up */
373         ret = storvsc_connect_to_vsp(device, device_info->ring_buffer_size);
374         if (ret) {
375                 kfree(stor_device);
376                 return ret;
377         }
378         device_info->path_id = stor_device->path_id;
379         device_info->target_id = stor_device->target_id;
380
381         return ret;
382 }
383
384 int storvsc_dev_remove(struct hv_device *device)
385 {
386         struct storvsc_device *stor_device;
387         unsigned long flags;
388
389         stor_device = (struct storvsc_device *)device->ext;
390         atomic_dec(&stor_device->ref_count);
391
392         spin_lock_irqsave(&device->channel->inbound_lock, flags);
393         stor_device->destroy = true;
394         spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
395
396         /*
397          * At this point, all outbound traffic should be disable. We
398          * only allow inbound traffic (responses) to proceed so that
399          * outstanding requests can be completed.
400          */
401
402         storvsc_wait_to_drain(stor_device);
403
404         stor_device = final_release_stor_device(device);
405
406         /* Close the channel */
407         vmbus_close(device->channel);
408
409         kfree(stor_device);
410         return 0;
411 }
412
413 int storvsc_do_io(struct hv_device *device,
414                               struct hv_storvsc_request *request)
415 {
416         struct storvsc_device *stor_device;
417         struct vstor_packet *vstor_packet;
418         int ret = 0;
419
420         vstor_packet = &request->vstor_packet;
421         stor_device = get_out_stor_device(device);
422
423         if (!stor_device)
424                 return -ENODEV;
425
426
427         request->device  = device;
428
429
430         vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
431
432         vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
433
434
435         vstor_packet->vm_srb.sense_info_length = SENSE_BUFFER_SIZE;
436
437
438         vstor_packet->vm_srb.data_transfer_length =
439         request->data_buffer.len;
440
441         vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
442
443         if (request->data_buffer.len) {
444                 ret = vmbus_sendpacket_multipagebuffer(device->channel,
445                                 &request->data_buffer,
446                                 vstor_packet,
447                                 sizeof(struct vstor_packet),
448                                 (unsigned long)request);
449         } else {
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
457         if (ret != 0)
458                 return ret;
459
460         atomic_inc(&stor_device->num_outstanding_req);
461
462         put_stor_device(device);
463         return ret;
464 }
465
466 /*
467  * The channel properties uniquely specify how the device is to be
468  * presented to the guest. Map this information for use by the block
469  * driver. For Linux guests on Hyper-V, we emulate a scsi HBA in the guest
470  * (storvsc_drv) and so scsi devices in the guest  are handled by
471  * native upper level Linux drivers. Consequently, Hyper-V
472  * block driver, while being a generic block driver, presently does not
473  * deal with anything other than devices that would need to be presented
474  * to the guest as an IDE disk.
475  *
476  * This function maps the channel properties as embedded in the input
477  * parameter device_info onto information necessary to register the
478  * corresponding block device.
479  *
480  * Currently, there is no way to stop the emulation of the block device
481  * on the host side. And so, to prevent the native IDE drivers in Linux
482  * from taking over these devices (to be managedby Hyper-V block
483  * driver), we will take over if need be the major of the IDE controllers.
484  *
485  */
486
487 int storvsc_get_major_info(struct storvsc_device_info *device_info,
488                             struct storvsc_major_info *major_info)
489 {
490         static bool ide0_registered;
491         static bool ide1_registered;
492
493         /*
494          * For now we only support IDE disks.
495          */
496         major_info->devname = "ide";
497         major_info->diskname = "hd";
498
499         if (device_info->path_id) {
500                 major_info->major = 22;
501                 if (!ide1_registered) {
502                         major_info->do_register = true;
503                         ide1_registered = true;
504                 } else
505                         major_info->do_register = false;
506
507                 if (device_info->target_id)
508                         major_info->index = 3;
509                 else
510                         major_info->index = 2;
511
512                 return 0;
513         } else {
514                 major_info->major = 3;
515                 if (!ide0_registered) {
516                         major_info->do_register = true;
517                         ide0_registered = true;
518                 } else
519                         major_info->do_register = false;
520
521                 if (device_info->target_id)
522                         major_info->index = 1;
523                 else
524                         major_info->index = 0;
525
526                 return 0;
527         }
528
529         return -ENODEV;
530 }