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>
21 * 4/3/2011: K. Y. Srinivasan - Significant restructuring and cleanup.
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 "storvsc_api.h"
33 #include "vmbus_packet_format.h"
38 static inline struct storvsc_device *alloc_stor_device(struct hv_device *device)
40 struct storvsc_device *stor_device;
42 stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
46 /* Set to 2 to allow both inbound and outbound traffics */
47 /* (ie get_stor_device() and must_get_stor_device()) to proceed. */
48 atomic_cmpxchg(&stor_device->ref_count, 0, 2);
50 stor_device->device = device;
51 device->ext = stor_device;
56 static inline void free_stor_device(struct storvsc_device *device)
61 /* Get the stordevice object iff exists and its refcount > 0 */
62 static inline struct storvsc_device *must_get_stor_device(
63 struct hv_device *device)
65 struct storvsc_device *stor_device;
67 stor_device = (struct storvsc_device *)device->ext;
68 if (stor_device && atomic_read(&stor_device->ref_count))
69 atomic_inc(&stor_device->ref_count);
76 /* Drop ref count to 1 to effectively disable get_stor_device() */
77 static inline struct storvsc_device *release_stor_device(
78 struct hv_device *device)
80 struct storvsc_device *stor_device;
82 stor_device = (struct storvsc_device *)device->ext;
84 /* Busy wait until the ref drop to 2, then set it to 1 */
85 while (atomic_cmpxchg(&stor_device->ref_count, 2, 1) != 2)
91 /* Drop ref count to 0. No one can use stor_device object. */
92 static inline struct storvsc_device *final_release_stor_device(
93 struct hv_device *device)
95 struct storvsc_device *stor_device;
97 stor_device = (struct storvsc_device *)device->ext;
99 /* Busy wait until the ref drop to 1, then set it to 0 */
100 while (atomic_cmpxchg(&stor_device->ref_count, 1, 0) != 1)
107 static int stor_vsc_channel_init(struct hv_device *device)
109 struct storvsc_device *stor_device;
110 struct hv_storvsc_request *request;
111 struct vstor_packet *vstor_packet;
114 stor_device = get_stor_device(device);
116 DPRINT_ERR(STORVSC, "unable to get stor device..."
117 "device being destroyed?");
121 request = &stor_device->init_request;
122 vstor_packet = &request->vstor_packet;
125 * Now, initiate the vsc/vsp initialization protocol on the open
128 memset(request, 0, sizeof(struct hv_storvsc_request));
129 init_completion(&request->wait_event);
130 vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
131 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
133 DPRINT_INFO(STORVSC, "BEGIN_INITIALIZATION_OPERATION...");
135 ret = vmbus_sendpacket(device->channel, vstor_packet,
136 sizeof(struct vstor_packet),
137 (unsigned long)request,
139 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
142 "unable to send BEGIN_INITIALIZATION_OPERATION");
146 t = wait_for_completion_timeout(&request->wait_event, HZ);
152 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
153 vstor_packet->status != 0) {
154 DPRINT_ERR(STORVSC, "BEGIN_INITIALIZATION_OPERATION failed "
155 "(op %d status 0x%x)",
156 vstor_packet->operation, vstor_packet->status);
160 DPRINT_INFO(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION...");
162 /* reuse the packet for version range supported */
163 memset(vstor_packet, 0, sizeof(struct vstor_packet));
164 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
165 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
167 vstor_packet->version.major_minor = VMSTOR_PROTOCOL_VERSION_CURRENT;
168 FILL_VMSTOR_REVISION(vstor_packet->version.revision);
170 ret = vmbus_sendpacket(device->channel, vstor_packet,
171 sizeof(struct vstor_packet),
172 (unsigned long)request,
174 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
177 "unable to send BEGIN_INITIALIZATION_OPERATION");
181 t = wait_for_completion_timeout(&request->wait_event, HZ);
187 /* TODO: Check returned version */
188 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
189 vstor_packet->status != 0) {
190 DPRINT_ERR(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION failed "
191 "(op %d status 0x%x)",
192 vstor_packet->operation, vstor_packet->status);
196 /* Query channel properties */
197 DPRINT_INFO(STORVSC, "QUERY_PROPERTIES_OPERATION...");
199 memset(vstor_packet, 0, sizeof(struct vstor_packet));
200 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
201 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
202 vstor_packet->storage_channel_properties.port_number =
203 stor_device->port_number;
205 ret = vmbus_sendpacket(device->channel, vstor_packet,
206 sizeof(struct vstor_packet),
207 (unsigned long)request,
209 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
213 "unable to send QUERY_PROPERTIES_OPERATION");
217 t = wait_for_completion_timeout(&request->wait_event, HZ);
223 /* TODO: Check returned version */
224 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
225 vstor_packet->status != 0) {
226 DPRINT_ERR(STORVSC, "QUERY_PROPERTIES_OPERATION failed "
227 "(op %d status 0x%x)",
228 vstor_packet->operation, vstor_packet->status);
232 stor_device->path_id = vstor_packet->storage_channel_properties.path_id;
233 stor_device->target_id
234 = vstor_packet->storage_channel_properties.target_id;
236 DPRINT_DBG(STORVSC, "channel flag 0x%x, max xfer len 0x%x",
237 vstor_packet->storage_channel_properties.flags,
238 vstor_packet->storage_channel_properties.max_transfer_bytes);
240 DPRINT_INFO(STORVSC, "END_INITIALIZATION_OPERATION...");
242 memset(vstor_packet, 0, sizeof(struct vstor_packet));
243 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
244 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
246 ret = vmbus_sendpacket(device->channel, vstor_packet,
247 sizeof(struct vstor_packet),
248 (unsigned long)request,
250 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
254 "unable to send END_INITIALIZATION_OPERATION");
258 t = wait_for_completion_timeout(&request->wait_event, HZ);
264 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
265 vstor_packet->status != 0) {
266 DPRINT_ERR(STORVSC, "END_INITIALIZATION_OPERATION failed "
267 "(op %d status 0x%x)",
268 vstor_packet->operation, vstor_packet->status);
272 DPRINT_INFO(STORVSC, "**** storage channel up and running!! ****");
275 put_stor_device(device);
279 static void stor_vsc_on_io_completion(struct hv_device *device,
280 struct vstor_packet *vstor_packet,
281 struct hv_storvsc_request *request)
283 struct storvsc_device *stor_device;
284 struct vstor_packet *stor_pkt;
286 stor_device = must_get_stor_device(device);
288 DPRINT_ERR(STORVSC, "unable to get stor device..."
289 "device being destroyed?");
293 DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION - request %p "
294 "completed bytes xfer %u", request,
295 vstor_packet->vm_srb.data_transfer_length);
297 stor_pkt = &request->vstor_packet;
300 /* Copy over the status...etc */
301 stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
302 stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
303 stor_pkt->vm_srb.sense_info_length =
304 vstor_packet->vm_srb.sense_info_length;
306 if (vstor_packet->vm_srb.scsi_status != 0 ||
307 vstor_packet->vm_srb.srb_status != 1) {
309 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
310 stor_pkt->vm_srb.cdb[0],
311 vstor_packet->vm_srb.scsi_status,
312 vstor_packet->vm_srb.srb_status);
315 if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
316 /* CHECK_CONDITION */
317 if (vstor_packet->vm_srb.srb_status & 0x80) {
318 /* autosense data available */
319 DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data "
320 "valid - len %d\n", request,
321 vstor_packet->vm_srb.sense_info_length);
323 memcpy(request->sense_buffer,
324 vstor_packet->vm_srb.sense_data,
325 vstor_packet->vm_srb.sense_info_length);
330 stor_pkt->vm_srb.data_transfer_length =
331 vstor_packet->vm_srb.data_transfer_length;
333 request->on_io_completion(request);
335 atomic_dec(&stor_device->num_outstanding_req);
337 put_stor_device(device);
340 static void stor_vsc_on_receive(struct hv_device *device,
341 struct vstor_packet *vstor_packet,
342 struct hv_storvsc_request *request)
344 switch (vstor_packet->operation) {
345 case VSTOR_OPERATION_COMPLETE_IO:
346 DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION");
347 stor_vsc_on_io_completion(device, vstor_packet, request);
349 case VSTOR_OPERATION_REMOVE_DEVICE:
350 DPRINT_INFO(STORVSC, "REMOVE_DEVICE_OPERATION");
355 DPRINT_INFO(STORVSC, "Unknown operation received - %d",
356 vstor_packet->operation);
361 static void stor_vsc_on_channel_callback(void *context)
363 struct hv_device *device = (struct hv_device *)context;
364 struct storvsc_device *stor_device;
367 unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
368 struct hv_storvsc_request *request;
372 stor_device = must_get_stor_device(device);
374 DPRINT_ERR(STORVSC, "unable to get stor device..."
375 "device being destroyed?");
380 ret = vmbus_recvpacket(device->channel, packet,
381 ALIGN(sizeof(struct vstor_packet), 8),
382 &bytes_recvd, &request_id);
383 if (ret == 0 && bytes_recvd > 0) {
384 DPRINT_DBG(STORVSC, "receive %d bytes - tid %llx",
385 bytes_recvd, request_id);
388 request = (struct hv_storvsc_request *)
389 (unsigned long)request_id;
391 if ((request == &stor_device->init_request) ||
392 (request == &stor_device->reset_request)) {
394 memcpy(&request->vstor_packet, packet,
395 sizeof(struct vstor_packet));
396 complete(&request->wait_event);
398 stor_vsc_on_receive(device,
399 (struct vstor_packet *)packet,
407 put_stor_device(device);
411 static int stor_vsc_connect_to_vsp(struct hv_device *device)
413 struct vmstorage_channel_properties props;
414 struct storvsc_driver_object *stor_driver;
417 stor_driver = (struct storvsc_driver_object *)device->drv;
418 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
420 /* Open the channel */
421 ret = vmbus_open(device->channel,
422 stor_driver->ring_buffer_size,
423 stor_driver->ring_buffer_size,
425 sizeof(struct vmstorage_channel_properties),
426 stor_vsc_on_channel_callback, device);
428 DPRINT_DBG(STORVSC, "storage props: path id %d, tgt id %d, max xfer %d",
429 props.path_id, props.target_id, props.max_transfer_bytes);
432 DPRINT_ERR(STORVSC, "unable to open channel: %d", ret);
436 ret = stor_vsc_channel_init(device);
442 * stor_vsc_on_device_add - Callback when the device belonging to this driver
445 int stor_vsc_on_device_add(struct hv_device *device,
446 void *additional_info)
448 struct storvsc_device *stor_device;
449 struct storvsc_device_info *device_info;
452 device_info = (struct storvsc_device_info *)additional_info;
453 stor_device = alloc_stor_device(device);
459 /* Save the channel properties to our storvsc channel */
463 * If we support more than 1 scsi channel, we need to set the
464 * port number here to the scsi channel but how do we get the
465 * scsi channel prior to the bus scan
468 stor_device->port_number = device_info->port_number;
469 /* Send it back up */
470 ret = stor_vsc_connect_to_vsp(device);
472 device_info->path_id = stor_device->path_id;
473 device_info->target_id = stor_device->target_id;
475 DPRINT_DBG(STORVSC, "assigned port %u, path %u target %u\n",
476 stor_device->port_number, stor_device->path_id,
477 stor_device->target_id);
484 * stor_vsc_on_device_remove - Callback when the our device is being removed
486 int stor_vsc_on_device_remove(struct hv_device *device)
488 struct storvsc_device *stor_device;
490 DPRINT_INFO(STORVSC, "disabling storage device (%p)...",
493 stor_device = release_stor_device(device);
496 * At this point, all outbound traffic should be disable. We
497 * only allow inbound traffic (responses) to proceed so that
498 * outstanding requests can be completed.
500 while (atomic_read(&stor_device->num_outstanding_req)) {
501 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...",
502 atomic_read(&stor_device->num_outstanding_req));
506 DPRINT_INFO(STORVSC, "removing storage device (%p)...",
509 stor_device = final_release_stor_device(device);
511 DPRINT_INFO(STORVSC, "storage device (%p) safe to remove", stor_device);
513 /* Close the channel */
514 vmbus_close(device->channel);
516 free_stor_device(stor_device);
521 * stor_vsc_on_io_request - Callback to initiate an I/O request
523 int stor_vsc_on_io_request(struct hv_device *device,
524 struct hv_storvsc_request *request)
526 struct storvsc_device *stor_device;
527 struct vstor_packet *vstor_packet;
530 vstor_packet = &request->vstor_packet;
531 stor_device = get_stor_device(device);
533 DPRINT_DBG(STORVSC, "enter - Device %p, DeviceExt %p, Request %p, "
534 , device, stor_device, request);
536 DPRINT_DBG(STORVSC, "req %p len %d",
537 request, request->data_buffer.len);
540 DPRINT_ERR(STORVSC, "unable to get stor device..."
541 "device being destroyed?");
546 request->device = device;
549 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
551 vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
554 vstor_packet->vm_srb.sense_info_length = SENSE_BUFFER_SIZE;
557 vstor_packet->vm_srb.data_transfer_length =
558 request->data_buffer.len;
560 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
562 DPRINT_DBG(STORVSC, "srb - len %d port %d, path %d, target %d, "
563 "lun %d senselen %d cdblen %d",
564 vstor_packet->vm_srb.length,
565 vstor_packet->vm_srb.port_number,
566 vstor_packet->vm_srb.path_id,
567 vstor_packet->vm_srb.target_id,
568 vstor_packet->vm_srb.lun,
569 vstor_packet->vm_srb.sense_info_length,
570 vstor_packet->vm_srb.cdb_length);
572 if (request->data_buffer.len) {
573 ret = vmbus_sendpacket_multipagebuffer(device->channel,
574 &request->data_buffer,
576 sizeof(struct vstor_packet),
577 (unsigned long)request);
579 ret = vmbus_sendpacket(device->channel, vstor_packet,
580 sizeof(struct vstor_packet),
581 (unsigned long)request,
583 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
587 DPRINT_DBG(STORVSC, "Unable to send packet %p ret %d",
591 atomic_inc(&stor_device->num_outstanding_req);
593 put_stor_device(device);
598 * The channel properties uniquely specify how the device is to be
599 * presented to the guest. Map this information for use by the block
600 * driver. For Linux guests on Hyper-V, we emulate a scsi HBA in the guest
601 * (storvsc_drv) and so scsi devices in the guest are handled by
602 * native upper level Linux drivers. Consequently, Hyper-V
603 * block driver, while being a generic block driver, presently does not
604 * deal with anything other than devices that would need to be presented
605 * to the guest as an IDE disk.
607 * This function maps the channel properties as embedded in the input
608 * parameter device_info onto information necessary to register the
609 * corresponding block device.
611 * Currently, there is no way to stop the emulation of the block device
612 * on the host side. And so, to prevent the native IDE drivers in Linux
613 * from taking over these devices (to be managedby Hyper-V block
614 * driver), we will take over if need be the major of the IDE controllers.
618 int stor_vsc_get_major_info(struct storvsc_device_info *device_info,
619 struct storvsc_major_info *major_info)
621 static bool ide0_registered;
622 static bool ide1_registered;
625 * For now we only support IDE disks.
627 major_info->devname = "ide";
628 major_info->diskname = "hd";
630 if (device_info->path_id) {
631 major_info->major = 22;
632 if (!ide1_registered)
633 major_info->do_register = true;
635 major_info->do_register = false;
636 ide1_registered = true;
638 if (device_info->target_id)
639 major_info->index = 3;
641 major_info->index = 2;
645 major_info->major = 3;
646 if (!ide0_registered)
647 major_info->do_register = true;
649 major_info->do_register = false;
650 ide0_registered = true;
652 if (device_info->target_id)
653 major_info->index = 1;
655 major_info->index = 0;
664 * stor_vsc_on_cleanup - Perform any cleanup when the driver is removed
666 void stor_vsc_on_cleanup(struct hv_driver *driver)