]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/hv/storvsc.c
8c62829f599362a6349064a6971daf18762cf084
[karo-tx-linux.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
55 /* Get the stordevice object iff exists and its refcount > 0 */
56 static inline struct storvsc_device *must_get_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_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_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 = must_get_stor_device(device);
236         if (!stor_device)
237                 return;
238
239         stor_pkt = &request->vstor_packet;
240
241
242         /* Copy over the status...etc */
243         stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
244         stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
245         stor_pkt->vm_srb.sense_info_length =
246         vstor_packet->vm_srb.sense_info_length;
247
248         if (vstor_packet->vm_srb.scsi_status != 0 ||
249                 vstor_packet->vm_srb.srb_status != 1){
250                 DPRINT_WARN(STORVSC,
251                             "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
252                             stor_pkt->vm_srb.cdb[0],
253                             vstor_packet->vm_srb.scsi_status,
254                             vstor_packet->vm_srb.srb_status);
255         }
256
257         if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
258                 /* CHECK_CONDITION */
259                 if (vstor_packet->vm_srb.srb_status & 0x80) {
260                         /* autosense data available */
261                         DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data "
262                                     "valid - len %d\n", request,
263                                     vstor_packet->vm_srb.sense_info_length);
264
265                         memcpy(request->sense_buffer,
266                                vstor_packet->vm_srb.sense_data,
267                                vstor_packet->vm_srb.sense_info_length);
268
269                 }
270         }
271
272         stor_pkt->vm_srb.data_transfer_length =
273         vstor_packet->vm_srb.data_transfer_length;
274
275         request->on_io_completion(request);
276
277         if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
278                 stor_device->drain_notify)
279                 wake_up(&stor_device->waiting_to_drain);
280
281
282         put_stor_device(device);
283 }
284
285 static void storvsc_on_receive(struct hv_device *device,
286                              struct vstor_packet *vstor_packet,
287                              struct hv_storvsc_request *request)
288 {
289         switch (vstor_packet->operation) {
290         case VSTOR_OPERATION_COMPLETE_IO:
291                 storvsc_on_io_completion(device, vstor_packet, request);
292                 break;
293         case VSTOR_OPERATION_REMOVE_DEVICE:
294
295         default:
296                 break;
297         }
298 }
299
300 static void storvsc_on_channel_callback(void *context)
301 {
302         struct hv_device *device = (struct hv_device *)context;
303         struct storvsc_device *stor_device;
304         u32 bytes_recvd;
305         u64 request_id;
306         unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
307         struct hv_storvsc_request *request;
308         int ret;
309
310
311         stor_device = must_get_stor_device(device);
312         if (!stor_device)
313                 return;
314
315         do {
316                 ret = vmbus_recvpacket(device->channel, packet,
317                                        ALIGN(sizeof(struct vstor_packet), 8),
318                                        &bytes_recvd, &request_id);
319                 if (ret == 0 && bytes_recvd > 0) {
320
321                         request = (struct hv_storvsc_request *)
322                                         (unsigned long)request_id;
323
324                         if ((request == &stor_device->init_request) ||
325                             (request == &stor_device->reset_request)) {
326
327                                 memcpy(&request->vstor_packet, packet,
328                                        sizeof(struct vstor_packet));
329                                 complete(&request->wait_event);
330                         } else {
331                                 storvsc_on_receive(device,
332                                                 (struct vstor_packet *)packet,
333                                                 request);
334                         }
335                 } else {
336                         break;
337                 }
338         } while (1);
339
340         put_stor_device(device);
341         return;
342 }
343
344 static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
345 {
346         struct vmstorage_channel_properties props;
347         int ret;
348
349         memset(&props, 0, sizeof(struct vmstorage_channel_properties));
350
351         /* Open the channel */
352         ret = vmbus_open(device->channel,
353                          ring_size,
354                          ring_size,
355                          (void *)&props,
356                          sizeof(struct vmstorage_channel_properties),
357                          storvsc_on_channel_callback, device);
358
359         if (ret != 0)
360                 return ret;
361
362         ret = storvsc_channel_init(device);
363
364         return ret;
365 }
366
367 int storvsc_dev_add(struct hv_device *device,
368                                         void *additional_info)
369 {
370         struct storvsc_device *stor_device;
371         struct storvsc_device_info *device_info;
372         int ret = 0;
373
374         device_info = (struct storvsc_device_info *)additional_info;
375         stor_device = alloc_stor_device(device);
376         if (!stor_device)
377                 return -ENOMEM;
378
379         /* Save the channel properties to our storvsc channel */
380
381         /*
382          * If we support more than 1 scsi channel, we need to set the
383          * port number here to the scsi channel but how do we get the
384          * scsi channel prior to the bus scan.
385          *
386          * The host does not support this.
387          */
388
389         stor_device->port_number = device_info->port_number;
390         /* Send it back up */
391         ret = storvsc_connect_to_vsp(device, device_info->ring_buffer_size);
392         if (ret) {
393                 kfree(stor_device);
394                 return ret;
395         }
396         device_info->path_id = stor_device->path_id;
397         device_info->target_id = stor_device->target_id;
398
399         return ret;
400 }
401
402 int storvsc_dev_remove(struct hv_device *device)
403 {
404         struct storvsc_device *stor_device;
405
406         stor_device = release_stor_device(device);
407
408         /*
409          * At this point, all outbound traffic should be disable. We
410          * only allow inbound traffic (responses) to proceed so that
411          * outstanding requests can be completed.
412          */
413
414         storvsc_wait_to_drain(stor_device);
415
416         stor_device = final_release_stor_device(device);
417
418         /* Close the channel */
419         vmbus_close(device->channel);
420
421         kfree(stor_device);
422         return 0;
423 }
424
425 int storvsc_do_io(struct hv_device *device,
426                               struct hv_storvsc_request *request)
427 {
428         struct storvsc_device *stor_device;
429         struct vstor_packet *vstor_packet;
430         int ret = 0;
431
432         vstor_packet = &request->vstor_packet;
433         stor_device = get_stor_device(device);
434
435         if (!stor_device)
436                 return -ENODEV;
437
438
439         request->device  = device;
440
441
442         vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
443
444         vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
445
446
447         vstor_packet->vm_srb.sense_info_length = SENSE_BUFFER_SIZE;
448
449
450         vstor_packet->vm_srb.data_transfer_length =
451         request->data_buffer.len;
452
453         vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
454
455         if (request->data_buffer.len) {
456                 ret = vmbus_sendpacket_multipagebuffer(device->channel,
457                                 &request->data_buffer,
458                                 vstor_packet,
459                                 sizeof(struct vstor_packet),
460                                 (unsigned long)request);
461         } else {
462                 ret = vmbus_sendpacket(device->channel, vstor_packet,
463                                        sizeof(struct vstor_packet),
464                                        (unsigned long)request,
465                                        VM_PKT_DATA_INBAND,
466                                        VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
467         }
468
469         if (ret != 0)
470                 return ret;
471
472         atomic_inc(&stor_device->num_outstanding_req);
473
474         put_stor_device(device);
475         return ret;
476 }
477
478 /*
479  * The channel properties uniquely specify how the device is to be
480  * presented to the guest. Map this information for use by the block
481  * driver. For Linux guests on Hyper-V, we emulate a scsi HBA in the guest
482  * (storvsc_drv) and so scsi devices in the guest  are handled by
483  * native upper level Linux drivers. Consequently, Hyper-V
484  * block driver, while being a generic block driver, presently does not
485  * deal with anything other than devices that would need to be presented
486  * to the guest as an IDE disk.
487  *
488  * This function maps the channel properties as embedded in the input
489  * parameter device_info onto information necessary to register the
490  * corresponding block device.
491  *
492  * Currently, there is no way to stop the emulation of the block device
493  * on the host side. And so, to prevent the native IDE drivers in Linux
494  * from taking over these devices (to be managedby Hyper-V block
495  * driver), we will take over if need be the major of the IDE controllers.
496  *
497  */
498
499 int storvsc_get_major_info(struct storvsc_device_info *device_info,
500                             struct storvsc_major_info *major_info)
501 {
502         static bool ide0_registered;
503         static bool ide1_registered;
504
505         /*
506          * For now we only support IDE disks.
507          */
508         major_info->devname = "ide";
509         major_info->diskname = "hd";
510
511         if (device_info->path_id) {
512                 major_info->major = 22;
513                 if (!ide1_registered) {
514                         major_info->do_register = true;
515                         ide1_registered = true;
516                 } else
517                         major_info->do_register = false;
518
519                 if (device_info->target_id)
520                         major_info->index = 3;
521                 else
522                         major_info->index = 2;
523
524                 return 0;
525         } else {
526                 major_info->major = 3;
527                 if (!ide0_registered) {
528                         major_info->do_register = true;
529                         ide0_registered = true;
530                 } else
531                         major_info->do_register = false;
532
533                 if (device_info->target_id)
534                         major_info->index = 1;
535                 else
536                         major_info->index = 0;
537
538                 return 0;
539         }
540
541         return -ENODEV;
542 }