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