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
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.
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>
29 #include <linux/delay.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);
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;
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)
59 struct storvsc_device *stor_device;
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);
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)
74 struct storvsc_device *stor_device;
76 stor_device = (struct storvsc_device *)device->ext;
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)
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)
89 struct storvsc_device *stor_device;
91 stor_device = (struct storvsc_device *)device->ext;
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)
101 static int storvsc_channel_init(struct hv_device *device)
103 struct storvsc_device *stor_device;
104 struct hv_storvsc_request *request;
105 struct vstor_packet *vstor_packet;
108 stor_device = get_stor_device(device);
112 request = &stor_device->init_request;
113 vstor_packet = &request->vstor_packet;
116 * Now, initiate the vsc/vsp initialization protocol on the open
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;
124 ret = vmbus_sendpacket(device->channel, vstor_packet,
125 sizeof(struct vstor_packet),
126 (unsigned long)request,
128 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
132 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
138 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
139 vstor_packet->status != 0)
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;
148 vstor_packet->version.major_minor = VMSTOR_PROTOCOL_VERSION_CURRENT;
149 FILL_VMSTOR_REVISION(vstor_packet->version.revision);
151 ret = vmbus_sendpacket(device->channel, vstor_packet,
152 sizeof(struct vstor_packet),
153 (unsigned long)request,
155 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
159 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
165 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
166 vstor_packet->status != 0)
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;
176 ret = vmbus_sendpacket(device->channel, vstor_packet,
177 sizeof(struct vstor_packet),
178 (unsigned long)request,
180 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
185 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
191 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
192 vstor_packet->status != 0)
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;
199 memset(vstor_packet, 0, sizeof(struct vstor_packet));
200 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
201 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
203 ret = vmbus_sendpacket(device->channel, vstor_packet,
204 sizeof(struct vstor_packet),
205 (unsigned long)request,
207 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
212 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
218 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
219 vstor_packet->status != 0)
224 put_stor_device(device);
228 static void storvsc_on_io_completion(struct hv_device *device,
229 struct vstor_packet *vstor_packet,
230 struct hv_storvsc_request *request)
232 struct storvsc_device *stor_device;
233 struct vstor_packet *stor_pkt;
235 stor_device = must_get_stor_device(device);
239 stor_pkt = &request->vstor_packet;
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;
248 if (vstor_packet->vm_srb.scsi_status != 0 ||
249 vstor_packet->vm_srb.srb_status != 1){
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);
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);
265 memcpy(request->sense_buffer,
266 vstor_packet->vm_srb.sense_data,
267 vstor_packet->vm_srb.sense_info_length);
272 stor_pkt->vm_srb.data_transfer_length =
273 vstor_packet->vm_srb.data_transfer_length;
275 request->on_io_completion(request);
277 if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
278 stor_device->drain_notify)
279 wake_up(&stor_device->waiting_to_drain);
282 put_stor_device(device);
285 static void storvsc_on_receive(struct hv_device *device,
286 struct vstor_packet *vstor_packet,
287 struct hv_storvsc_request *request)
289 switch (vstor_packet->operation) {
290 case VSTOR_OPERATION_COMPLETE_IO:
291 storvsc_on_io_completion(device, vstor_packet, request);
293 case VSTOR_OPERATION_REMOVE_DEVICE:
300 static void storvsc_on_channel_callback(void *context)
302 struct hv_device *device = (struct hv_device *)context;
303 struct storvsc_device *stor_device;
306 unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
307 struct hv_storvsc_request *request;
311 stor_device = must_get_stor_device(device);
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) {
321 request = (struct hv_storvsc_request *)
322 (unsigned long)request_id;
324 if ((request == &stor_device->init_request) ||
325 (request == &stor_device->reset_request)) {
327 memcpy(&request->vstor_packet, packet,
328 sizeof(struct vstor_packet));
329 complete(&request->wait_event);
331 storvsc_on_receive(device,
332 (struct vstor_packet *)packet,
340 put_stor_device(device);
344 static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
346 struct vmstorage_channel_properties props;
349 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
351 /* Open the channel */
352 ret = vmbus_open(device->channel,
356 sizeof(struct vmstorage_channel_properties),
357 storvsc_on_channel_callback, device);
362 ret = storvsc_channel_init(device);
367 int storvsc_dev_add(struct hv_device *device,
368 void *additional_info)
370 struct storvsc_device *stor_device;
371 struct storvsc_device_info *device_info;
374 device_info = (struct storvsc_device_info *)additional_info;
375 stor_device = alloc_stor_device(device);
379 /* Save the channel properties to our storvsc channel */
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.
386 * The host does not support this.
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);
396 device_info->path_id = stor_device->path_id;
397 device_info->target_id = stor_device->target_id;
402 int storvsc_dev_remove(struct hv_device *device)
404 struct storvsc_device *stor_device;
406 stor_device = release_stor_device(device);
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.
414 storvsc_wait_to_drain(stor_device);
416 stor_device = final_release_stor_device(device);
418 /* Close the channel */
419 vmbus_close(device->channel);
425 int storvsc_do_io(struct hv_device *device,
426 struct hv_storvsc_request *request)
428 struct storvsc_device *stor_device;
429 struct vstor_packet *vstor_packet;
432 vstor_packet = &request->vstor_packet;
433 stor_device = get_stor_device(device);
439 request->device = device;
442 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
444 vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
447 vstor_packet->vm_srb.sense_info_length = SENSE_BUFFER_SIZE;
450 vstor_packet->vm_srb.data_transfer_length =
451 request->data_buffer.len;
453 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
455 if (request->data_buffer.len) {
456 ret = vmbus_sendpacket_multipagebuffer(device->channel,
457 &request->data_buffer,
459 sizeof(struct vstor_packet),
460 (unsigned long)request);
462 ret = vmbus_sendpacket(device->channel, vstor_packet,
463 sizeof(struct vstor_packet),
464 (unsigned long)request,
466 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
472 atomic_inc(&stor_device->num_outstanding_req);
474 put_stor_device(device);
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.
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.
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.
499 int storvsc_get_major_info(struct storvsc_device_info *device_info,
500 struct storvsc_major_info *major_info)
502 static bool ide0_registered;
503 static bool ide1_registered;
506 * For now we only support IDE disks.
508 major_info->devname = "ide";
509 major_info->diskname = "hd";
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;
517 major_info->do_register = false;
519 if (device_info->target_id)
520 major_info->index = 3;
522 major_info->index = 2;
526 major_info->major = 3;
527 if (!ide0_registered) {
528 major_info->do_register = true;
529 ide0_registered = true;
531 major_info->do_register = false;
533 if (device_info->target_id)
534 major_info->index = 1;
536 major_info->index = 0;