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_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;
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)
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 0. No one can use stor_device object. */
71 static inline struct storvsc_device *final_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 1, then set it to 0 */
79 while (atomic_cmpxchg(&stor_device->ref_count, 1, 0) != 1)
86 static int storvsc_channel_init(struct hv_device *device)
88 struct storvsc_device *stor_device;
89 struct hv_storvsc_request *request;
90 struct vstor_packet *vstor_packet;
93 stor_device = get_out_stor_device(device);
97 request = &stor_device->init_request;
98 vstor_packet = &request->vstor_packet;
101 * Now, initiate the vsc/vsp initialization protocol on the open
104 memset(request, 0, sizeof(struct hv_storvsc_request));
105 init_completion(&request->wait_event);
106 vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
107 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
109 ret = vmbus_sendpacket(device->channel, vstor_packet,
110 sizeof(struct vstor_packet),
111 (unsigned long)request,
113 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
117 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
123 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
124 vstor_packet->status != 0)
128 /* reuse the packet for version range supported */
129 memset(vstor_packet, 0, sizeof(struct vstor_packet));
130 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
131 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
133 vstor_packet->version.major_minor = VMSTOR_PROTOCOL_VERSION_CURRENT;
134 FILL_VMSTOR_REVISION(vstor_packet->version.revision);
136 ret = vmbus_sendpacket(device->channel, vstor_packet,
137 sizeof(struct vstor_packet),
138 (unsigned long)request,
140 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
144 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
150 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
151 vstor_packet->status != 0)
155 memset(vstor_packet, 0, sizeof(struct vstor_packet));
156 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
157 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
158 vstor_packet->storage_channel_properties.port_number =
159 stor_device->port_number;
161 ret = vmbus_sendpacket(device->channel, vstor_packet,
162 sizeof(struct vstor_packet),
163 (unsigned long)request,
165 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
170 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
176 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
177 vstor_packet->status != 0)
180 stor_device->path_id = vstor_packet->storage_channel_properties.path_id;
181 stor_device->target_id
182 = vstor_packet->storage_channel_properties.target_id;
184 memset(vstor_packet, 0, sizeof(struct vstor_packet));
185 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
186 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
188 ret = vmbus_sendpacket(device->channel, vstor_packet,
189 sizeof(struct vstor_packet),
190 (unsigned long)request,
192 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
197 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
203 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
204 vstor_packet->status != 0)
209 put_stor_device(device);
213 static void storvsc_on_io_completion(struct hv_device *device,
214 struct vstor_packet *vstor_packet,
215 struct hv_storvsc_request *request)
217 struct storvsc_device *stor_device;
218 struct vstor_packet *stor_pkt;
220 stor_device = (struct storvsc_device *)device->ext;
222 stor_pkt = &request->vstor_packet;
225 /* Copy over the status...etc */
226 stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
227 stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
228 stor_pkt->vm_srb.sense_info_length =
229 vstor_packet->vm_srb.sense_info_length;
231 if (vstor_packet->vm_srb.scsi_status != 0 ||
232 vstor_packet->vm_srb.srb_status != 1){
234 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
235 stor_pkt->vm_srb.cdb[0],
236 vstor_packet->vm_srb.scsi_status,
237 vstor_packet->vm_srb.srb_status);
240 if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
241 /* CHECK_CONDITION */
242 if (vstor_packet->vm_srb.srb_status & 0x80) {
243 /* autosense data available */
244 DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data "
245 "valid - len %d\n", request,
246 vstor_packet->vm_srb.sense_info_length);
248 memcpy(request->sense_buffer,
249 vstor_packet->vm_srb.sense_data,
250 vstor_packet->vm_srb.sense_info_length);
255 stor_pkt->vm_srb.data_transfer_length =
256 vstor_packet->vm_srb.data_transfer_length;
258 request->on_io_completion(request);
260 if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
261 stor_device->drain_notify)
262 wake_up(&stor_device->waiting_to_drain);
267 static void storvsc_on_receive(struct hv_device *device,
268 struct vstor_packet *vstor_packet,
269 struct hv_storvsc_request *request)
271 switch (vstor_packet->operation) {
272 case VSTOR_OPERATION_COMPLETE_IO:
273 storvsc_on_io_completion(device, vstor_packet, request);
275 case VSTOR_OPERATION_REMOVE_DEVICE:
282 static void storvsc_on_channel_callback(void *context)
284 struct hv_device *device = (struct hv_device *)context;
285 struct storvsc_device *stor_device;
288 unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
289 struct hv_storvsc_request *request;
293 stor_device = get_in_stor_device(device);
298 ret = vmbus_recvpacket(device->channel, packet,
299 ALIGN(sizeof(struct vstor_packet), 8),
300 &bytes_recvd, &request_id);
301 if (ret == 0 && bytes_recvd > 0) {
303 request = (struct hv_storvsc_request *)
304 (unsigned long)request_id;
306 if ((request == &stor_device->init_request) ||
307 (request == &stor_device->reset_request)) {
309 memcpy(&request->vstor_packet, packet,
310 sizeof(struct vstor_packet));
311 complete(&request->wait_event);
313 storvsc_on_receive(device,
314 (struct vstor_packet *)packet,
322 put_stor_device(device);
326 static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
328 struct vmstorage_channel_properties props;
331 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
333 /* Open the channel */
334 ret = vmbus_open(device->channel,
338 sizeof(struct vmstorage_channel_properties),
339 storvsc_on_channel_callback, device);
344 ret = storvsc_channel_init(device);
349 int storvsc_dev_add(struct hv_device *device,
350 void *additional_info)
352 struct storvsc_device *stor_device;
353 struct storvsc_device_info *device_info;
356 device_info = (struct storvsc_device_info *)additional_info;
357 stor_device = alloc_stor_device(device);
361 /* Save the channel properties to our storvsc channel */
364 * If we support more than 1 scsi channel, we need to set the
365 * port number here to the scsi channel but how do we get the
366 * scsi channel prior to the bus scan.
368 * The host does not support this.
371 stor_device->port_number = device_info->port_number;
372 /* Send it back up */
373 ret = storvsc_connect_to_vsp(device, device_info->ring_buffer_size);
378 device_info->path_id = stor_device->path_id;
379 device_info->target_id = stor_device->target_id;
384 int storvsc_dev_remove(struct hv_device *device)
386 struct storvsc_device *stor_device;
389 stor_device = (struct storvsc_device *)device->ext;
390 atomic_dec(&stor_device->ref_count);
392 spin_lock_irqsave(&device->channel->inbound_lock, flags);
393 stor_device->destroy = true;
394 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
397 * At this point, all outbound traffic should be disable. We
398 * only allow inbound traffic (responses) to proceed so that
399 * outstanding requests can be completed.
402 storvsc_wait_to_drain(stor_device);
404 stor_device = final_release_stor_device(device);
406 /* Close the channel */
407 vmbus_close(device->channel);
413 int storvsc_do_io(struct hv_device *device,
414 struct hv_storvsc_request *request)
416 struct storvsc_device *stor_device;
417 struct vstor_packet *vstor_packet;
420 vstor_packet = &request->vstor_packet;
421 stor_device = get_out_stor_device(device);
427 request->device = device;
430 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
432 vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
435 vstor_packet->vm_srb.sense_info_length = SENSE_BUFFER_SIZE;
438 vstor_packet->vm_srb.data_transfer_length =
439 request->data_buffer.len;
441 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
443 if (request->data_buffer.len) {
444 ret = vmbus_sendpacket_multipagebuffer(device->channel,
445 &request->data_buffer,
447 sizeof(struct vstor_packet),
448 (unsigned long)request);
450 ret = vmbus_sendpacket(device->channel, vstor_packet,
451 sizeof(struct vstor_packet),
452 (unsigned long)request,
454 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
460 atomic_inc(&stor_device->num_outstanding_req);
462 put_stor_device(device);
467 * The channel properties uniquely specify how the device is to be
468 * presented to the guest. Map this information for use by the block
469 * driver. For Linux guests on Hyper-V, we emulate a scsi HBA in the guest
470 * (storvsc_drv) and so scsi devices in the guest are handled by
471 * native upper level Linux drivers. Consequently, Hyper-V
472 * block driver, while being a generic block driver, presently does not
473 * deal with anything other than devices that would need to be presented
474 * to the guest as an IDE disk.
476 * This function maps the channel properties as embedded in the input
477 * parameter device_info onto information necessary to register the
478 * corresponding block device.
480 * Currently, there is no way to stop the emulation of the block device
481 * on the host side. And so, to prevent the native IDE drivers in Linux
482 * from taking over these devices (to be managedby Hyper-V block
483 * driver), we will take over if need be the major of the IDE controllers.
487 int storvsc_get_major_info(struct storvsc_device_info *device_info,
488 struct storvsc_major_info *major_info)
490 static bool ide0_registered;
491 static bool ide1_registered;
494 * For now we only support IDE disks.
496 major_info->devname = "ide";
497 major_info->diskname = "hd";
499 if (device_info->path_id) {
500 major_info->major = 22;
501 if (!ide1_registered) {
502 major_info->do_register = true;
503 ide1_registered = true;
505 major_info->do_register = false;
507 if (device_info->target_id)
508 major_info->index = 3;
510 major_info->index = 2;
514 major_info->major = 3;
515 if (!ide0_registered) {
516 major_info->do_register = true;
517 ide0_registered = true;
519 major_info->do_register = false;
521 if (device_info->target_id)
522 major_info->index = 1;
524 major_info->index = 0;