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