thinkpad-acpi: handle HKEY 0x4010, 0x4011 events
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / hv / storvsc.c
blob06cd3276813c55e9a6fc328fdee72fc109f1eee0
1 /*
2 * Copyright (c) 2009, Microsoft Corporation.
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.
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.
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.
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
20 * K. Y. Srinivasan <kys@microsoft.com>
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>
31 #include "hyperv.h"
32 #include "hyperv_storage.h"
35 static inline struct storvsc_device *alloc_stor_device(struct hv_device *device)
37 struct storvsc_device *stor_device;
39 stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
40 if (!stor_device)
41 return NULL;
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);
47 init_waitqueue_head(&stor_device->waiting_to_drain);
48 stor_device->device = device;
49 device->ext = stor_device;
51 return stor_device;
54 static inline void free_stor_device(struct storvsc_device *device)
56 kfree(device);
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)
63 struct storvsc_device *stor_device;
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;
71 return stor_device;
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)
78 struct storvsc_device *stor_device;
80 stor_device = (struct storvsc_device *)device->ext;
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);
86 return stor_device;
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)
93 struct storvsc_device *stor_device;
95 stor_device = (struct storvsc_device *)device->ext;
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);
101 device->ext = NULL;
102 return stor_device;
105 static int storvsc_channel_init(struct hv_device *device)
107 struct storvsc_device *stor_device;
108 struct hv_storvsc_request *request;
109 struct vstor_packet *vstor_packet;
110 int ret, t;
112 stor_device = get_stor_device(device);
113 if (!stor_device)
114 return -1;
116 request = &stor_device->init_request;
117 vstor_packet = &request->vstor_packet;
120 * Now, initiate the vsc/vsp initialization protocol on the open
121 * channel
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;
128 DPRINT_INFO(STORVSC, "BEGIN_INITIALIZATION_OPERATION...");
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;
138 t = wait_for_completion_timeout(&request->wait_event, HZ);
139 if (t == 0) {
140 ret = -ETIMEDOUT;
141 goto cleanup;
144 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
145 vstor_packet->status != 0)
146 goto cleanup;
148 DPRINT_INFO(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION...");
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;
155 vstor_packet->version.major_minor = VMSTOR_PROTOCOL_VERSION_CURRENT;
156 FILL_VMSTOR_REVISION(vstor_packet->version.revision);
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;
166 t = wait_for_completion_timeout(&request->wait_event, HZ);
167 if (t == 0) {
168 ret = -ETIMEDOUT;
169 goto cleanup;
172 /* TODO: Check returned version */
173 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
174 vstor_packet->status != 0)
175 goto cleanup;
177 /* Query channel properties */
178 DPRINT_INFO(STORVSC, "QUERY_PROPERTIES_OPERATION...");
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;
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);
192 if (ret != 0)
193 goto cleanup;
195 t = wait_for_completion_timeout(&request->wait_event, HZ);
196 if (t == 0) {
197 ret = -ETIMEDOUT;
198 goto cleanup;
201 /* TODO: Check returned version */
202 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
203 vstor_packet->status != 0)
204 goto cleanup;
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;
210 DPRINT_INFO(STORVSC, "END_INITIALIZATION_OPERATION...");
212 memset(vstor_packet, 0, sizeof(struct vstor_packet));
213 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
214 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
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);
222 if (ret != 0)
223 goto cleanup;
225 t = wait_for_completion_timeout(&request->wait_event, HZ);
226 if (t == 0) {
227 ret = -ETIMEDOUT;
228 goto cleanup;
231 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
232 vstor_packet->status != 0)
233 goto cleanup;
235 DPRINT_INFO(STORVSC, "**** storage channel up and running!! ****");
237 cleanup:
238 put_stor_device(device);
239 return ret;
242 static void storvsc_on_io_completion(struct hv_device *device,
243 struct vstor_packet *vstor_packet,
244 struct hv_storvsc_request *request)
246 struct storvsc_device *stor_device;
247 struct vstor_packet *stor_pkt;
249 stor_device = must_get_stor_device(device);
250 if (!stor_device)
251 return;
253 stor_pkt = &request->vstor_packet;
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;
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);
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);
279 memcpy(request->sense_buffer,
280 vstor_packet->vm_srb.sense_data,
281 vstor_packet->vm_srb.sense_info_length);
286 stor_pkt->vm_srb.data_transfer_length =
287 vstor_packet->vm_srb.data_transfer_length;
289 request->on_io_completion(request);
291 if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
292 stor_device->drain_notify)
293 wake_up(&stor_device->waiting_to_drain);
296 put_stor_device(device);
299 static void storvsc_on_receive(struct hv_device *device,
300 struct vstor_packet *vstor_packet,
301 struct hv_storvsc_request *request)
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;
312 default:
313 DPRINT_INFO(STORVSC, "Unknown operation received - %d",
314 vstor_packet->operation);
315 break;
319 static void storvsc_on_channel_callback(void *context)
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;
330 stor_device = must_get_stor_device(device);
331 if (!stor_device)
332 return;
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) {
340 request = (struct hv_storvsc_request *)
341 (unsigned long)request_id;
343 if ((request == &stor_device->init_request) ||
344 (request == &stor_device->reset_request)) {
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);
354 } else {
355 break;
357 } while (1);
359 put_stor_device(device);
360 return;
363 static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
365 struct vmstorage_channel_properties props;
366 int ret;
368 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
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);
378 if (ret != 0)
379 return -1;
381 ret = storvsc_channel_init(device);
383 return ret;
386 int storvsc_dev_add(struct hv_device *device,
387 void *additional_info)
389 struct storvsc_device *stor_device;
390 struct storvsc_device_info *device_info;
391 int ret = 0;
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;
400 /* Save the channel properties to our storvsc channel */
402 /* FIXME: */
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
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);
413 device_info->path_id = stor_device->path_id;
414 device_info->target_id = stor_device->target_id;
416 cleanup:
417 return ret;
420 int storvsc_dev_remove(struct hv_device *device)
422 struct storvsc_device *stor_device;
424 DPRINT_INFO(STORVSC, "disabling storage device (%p)...",
425 device->ext);
427 stor_device = release_stor_device(device);
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.
435 storvsc_wait_to_drain(stor_device);
437 stor_device = final_release_stor_device(device);
439 /* Close the channel */
440 vmbus_close(device->channel);
442 free_stor_device(stor_device);
443 return 0;
446 int storvsc_do_io(struct hv_device *device,
447 struct hv_storvsc_request *request)
449 struct storvsc_device *stor_device;
450 struct vstor_packet *vstor_packet;
451 int ret = 0;
453 vstor_packet = &request->vstor_packet;
454 stor_device = get_stor_device(device);
456 if (!stor_device)
457 return -2;
460 request->device = device;
463 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
465 vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
468 vstor_packet->vm_srb.sense_info_length = SENSE_BUFFER_SIZE;
471 vstor_packet->vm_srb.data_transfer_length =
472 request->data_buffer.len;
474 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
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);
490 if (ret != 0)
491 return ret;
493 atomic_inc(&stor_device->num_outstanding_req);
495 put_stor_device(device);
496 return ret;
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.
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.
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.
520 int storvsc_get_major_info(struct storvsc_device_info *device_info,
521 struct storvsc_major_info *major_info)
523 static bool ide0_registered;
524 static bool ide1_registered;
527 * For now we only support IDE disks.
529 major_info->devname = "ide";
530 major_info->diskname = "hd";
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;
540 if (device_info->target_id)
541 major_info->index = 3;
542 else
543 major_info->index = 2;
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;
554 if (device_info->target_id)
555 major_info->index = 1;
556 else
557 major_info->index = 0;
559 return 0;
562 return -ENODEV;