Staging: hv: Use native wait primitives
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / hv / storvsc.c
blob2560342a0b9c6d57be436d0e66850c0dcd3a2a47
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>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/wait.h>
24 #include <linux/string.h>
25 #include <linux/slab.h>
26 #include <linux/mm.h>
27 #include <linux/delay.h>
28 #include "osd.h"
29 #include "logging.h"
30 #include "storvsc_api.h"
31 #include "vmbus_packet_format.h"
32 #include "vstorage.h"
33 #include "channel.h"
36 struct storvsc_request_extension {
37 /* LIST_ENTRY ListEntry; */
39 struct hv_storvsc_request *request;
40 struct hv_device *device;
42 /* Synchronize the request/response if needed */
43 int wait_condition;
44 wait_queue_head_t wait_event;
46 struct vstor_packet vstor_packet;
49 /* A storvsc device is a device object that contains a vmbus channel */
50 struct storvsc_device {
51 struct hv_device *device;
53 /* 0 indicates the device is being destroyed */
54 atomic_t ref_count;
56 atomic_t num_outstanding_req;
59 * Each unique Port/Path/Target represents 1 channel ie scsi
60 * controller. In reality, the pathid, targetid is always 0
61 * and the port is set by us
63 unsigned int port_number;
64 unsigned char path_id;
65 unsigned char target_id;
67 /* LIST_ENTRY OutstandingRequestList; */
68 /* HANDLE OutstandingRequestLock; */
70 /* Used for vsc/vsp channel reset process */
71 struct storvsc_request_extension init_request;
72 struct storvsc_request_extension reset_request;
76 static const char *g_driver_name = "storvsc";
78 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
79 static const struct hv_guid gStorVscDeviceType = {
80 .data = {
81 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
82 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f
87 static inline struct storvsc_device *alloc_stor_device(struct hv_device *device)
89 struct storvsc_device *stor_device;
91 stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
92 if (!stor_device)
93 return NULL;
95 /* Set to 2 to allow both inbound and outbound traffics */
96 /* (ie get_stor_device() and must_get_stor_device()) to proceed. */
97 atomic_cmpxchg(&stor_device->ref_count, 0, 2);
99 stor_device->device = device;
100 device->ext = stor_device;
102 return stor_device;
105 static inline void free_stor_device(struct storvsc_device *device)
107 /* ASSERT(atomic_read(&device->ref_count) == 0); */
108 kfree(device);
111 /* Get the stordevice object iff exists and its refcount > 1 */
112 static inline struct storvsc_device *get_stor_device(struct hv_device *device)
114 struct storvsc_device *stor_device;
116 stor_device = (struct storvsc_device *)device->ext;
117 if (stor_device && atomic_read(&stor_device->ref_count) > 1)
118 atomic_inc(&stor_device->ref_count);
119 else
120 stor_device = NULL;
122 return stor_device;
125 /* Get the stordevice object iff exists and its refcount > 0 */
126 static inline struct storvsc_device *must_get_stor_device(
127 struct hv_device *device)
129 struct storvsc_device *stor_device;
131 stor_device = (struct storvsc_device *)device->ext;
132 if (stor_device && atomic_read(&stor_device->ref_count))
133 atomic_inc(&stor_device->ref_count);
134 else
135 stor_device = NULL;
137 return stor_device;
140 static inline void put_stor_device(struct hv_device *device)
142 struct storvsc_device *stor_device;
144 stor_device = (struct storvsc_device *)device->ext;
145 /* ASSERT(stor_device); */
147 atomic_dec(&stor_device->ref_count);
148 /* ASSERT(atomic_read(&stor_device->ref_count)); */
151 /* Drop ref count to 1 to effectively disable get_stor_device() */
152 static inline struct storvsc_device *release_stor_device(
153 struct hv_device *device)
155 struct storvsc_device *stor_device;
157 stor_device = (struct storvsc_device *)device->ext;
158 /* ASSERT(stor_device); */
160 /* Busy wait until the ref drop to 2, then set it to 1 */
161 while (atomic_cmpxchg(&stor_device->ref_count, 2, 1) != 2)
162 udelay(100);
164 return stor_device;
167 /* Drop ref count to 0. No one can use stor_device object. */
168 static inline struct storvsc_device *final_release_stor_device(
169 struct hv_device *device)
171 struct storvsc_device *stor_device;
173 stor_device = (struct storvsc_device *)device->ext;
174 /* ASSERT(stor_device); */
176 /* Busy wait until the ref drop to 1, then set it to 0 */
177 while (atomic_cmpxchg(&stor_device->ref_count, 1, 0) != 1)
178 udelay(100);
180 device->ext = NULL;
181 return stor_device;
184 static int stor_vsc_channel_init(struct hv_device *device)
186 struct storvsc_device *stor_device;
187 struct storvsc_request_extension *request;
188 struct vstor_packet *vstor_packet;
189 int ret;
191 stor_device = get_stor_device(device);
192 if (!stor_device) {
193 DPRINT_ERR(STORVSC, "unable to get stor device..."
194 "device being destroyed?");
195 return -1;
198 request = &stor_device->init_request;
199 vstor_packet = &request->vstor_packet;
202 * Now, initiate the vsc/vsp initialization protocol on the open
203 * channel
205 memset(request, 0, sizeof(struct storvsc_request_extension));
206 init_waitqueue_head(&request->wait_event);
207 vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
208 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
210 DPRINT_INFO(STORVSC, "BEGIN_INITIALIZATION_OPERATION...");
212 request->wait_condition = 0;
213 ret = vmbus_sendpacket(device->channel, vstor_packet,
214 sizeof(struct vstor_packet),
215 (unsigned long)request,
216 VM_PKT_DATA_INBAND,
217 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
218 if (ret != 0) {
219 DPRINT_ERR(STORVSC,
220 "unable to send BEGIN_INITIALIZATION_OPERATION");
221 goto cleanup;
224 wait_event_timeout(request->wait_event, request->wait_condition,
225 msecs_to_jiffies(1000));
226 if (request->wait_condition == 0) {
227 ret = -ETIMEDOUT;
228 goto cleanup;
232 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
233 vstor_packet->status != 0) {
234 DPRINT_ERR(STORVSC, "BEGIN_INITIALIZATION_OPERATION failed "
235 "(op %d status 0x%x)",
236 vstor_packet->operation, vstor_packet->status);
237 goto cleanup;
240 DPRINT_INFO(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION...");
242 /* reuse the packet for version range supported */
243 memset(vstor_packet, 0, sizeof(struct vstor_packet));
244 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
245 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
247 vstor_packet->version.major_minor = VMSTOR_PROTOCOL_VERSION_CURRENT;
248 FILL_VMSTOR_REVISION(vstor_packet->version.revision);
250 request->wait_condition = 0;
251 ret = vmbus_sendpacket(device->channel, vstor_packet,
252 sizeof(struct vstor_packet),
253 (unsigned long)request,
254 VM_PKT_DATA_INBAND,
255 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
256 if (ret != 0) {
257 DPRINT_ERR(STORVSC,
258 "unable to send BEGIN_INITIALIZATION_OPERATION");
259 goto cleanup;
262 wait_event_timeout(request->wait_event, request->wait_condition,
263 msecs_to_jiffies(1000));
264 if (request->wait_condition == 0) {
265 ret = -ETIMEDOUT;
266 goto cleanup;
269 /* TODO: Check returned version */
270 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
271 vstor_packet->status != 0) {
272 DPRINT_ERR(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION failed "
273 "(op %d status 0x%x)",
274 vstor_packet->operation, vstor_packet->status);
275 goto cleanup;
278 /* Query channel properties */
279 DPRINT_INFO(STORVSC, "QUERY_PROPERTIES_OPERATION...");
281 memset(vstor_packet, 0, sizeof(struct vstor_packet));
282 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
283 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
284 vstor_packet->storage_channel_properties.port_number =
285 stor_device->port_number;
287 request->wait_condition = 0;
288 ret = vmbus_sendpacket(device->channel, vstor_packet,
289 sizeof(struct vstor_packet),
290 (unsigned long)request,
291 VM_PKT_DATA_INBAND,
292 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
294 if (ret != 0) {
295 DPRINT_ERR(STORVSC,
296 "unable to send QUERY_PROPERTIES_OPERATION");
297 goto cleanup;
300 wait_event_timeout(request->wait_event, request->wait_condition,
301 msecs_to_jiffies(1000));
302 if (request->wait_condition == 0) {
303 ret = -ETIMEDOUT;
304 goto cleanup;
307 /* TODO: Check returned version */
308 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
309 vstor_packet->status != 0) {
310 DPRINT_ERR(STORVSC, "QUERY_PROPERTIES_OPERATION failed "
311 "(op %d status 0x%x)",
312 vstor_packet->operation, vstor_packet->status);
313 goto cleanup;
316 stor_device->path_id = vstor_packet->storage_channel_properties.path_id;
317 stor_device->target_id
318 = vstor_packet->storage_channel_properties.target_id;
320 DPRINT_DBG(STORVSC, "channel flag 0x%x, max xfer len 0x%x",
321 vstor_packet->storage_channel_properties.flags,
322 vstor_packet->storage_channel_properties.max_transfer_bytes);
324 DPRINT_INFO(STORVSC, "END_INITIALIZATION_OPERATION...");
326 memset(vstor_packet, 0, sizeof(struct vstor_packet));
327 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
328 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
330 request->wait_condition = 0;
331 ret = vmbus_sendpacket(device->channel, vstor_packet,
332 sizeof(struct vstor_packet),
333 (unsigned long)request,
334 VM_PKT_DATA_INBAND,
335 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
337 if (ret != 0) {
338 DPRINT_ERR(STORVSC,
339 "unable to send END_INITIALIZATION_OPERATION");
340 goto cleanup;
343 wait_event_timeout(request->wait_event, request->wait_condition,
344 msecs_to_jiffies(1000));
345 if (request->wait_condition == 0) {
346 ret = -ETIMEDOUT;
347 goto cleanup;
350 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
351 vstor_packet->status != 0) {
352 DPRINT_ERR(STORVSC, "END_INITIALIZATION_OPERATION failed "
353 "(op %d status 0x%x)",
354 vstor_packet->operation, vstor_packet->status);
355 goto cleanup;
358 DPRINT_INFO(STORVSC, "**** storage channel up and running!! ****");
360 cleanup:
361 put_stor_device(device);
362 return ret;
365 static void stor_vsc_on_io_completion(struct hv_device *device,
366 struct vstor_packet *vstor_packet,
367 struct storvsc_request_extension *request_ext)
369 struct hv_storvsc_request *request;
370 struct storvsc_device *stor_device;
372 stor_device = must_get_stor_device(device);
373 if (!stor_device) {
374 DPRINT_ERR(STORVSC, "unable to get stor device..."
375 "device being destroyed?");
376 return;
379 DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION - request extension %p "
380 "completed bytes xfer %u", request_ext,
381 vstor_packet->vm_srb.data_transfer_length);
383 /* ASSERT(request_ext != NULL); */
384 /* ASSERT(request_ext->request != NULL); */
386 request = request_ext->request;
388 /* ASSERT(request->OnIOCompletion != NULL); */
390 /* Copy over the status...etc */
391 request->status = vstor_packet->vm_srb.scsi_status;
393 if (request->status != 0 || vstor_packet->vm_srb.srb_status != 1) {
394 DPRINT_WARN(STORVSC,
395 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
396 request->cdb[0], vstor_packet->vm_srb.scsi_status,
397 vstor_packet->vm_srb.srb_status);
400 if ((request->status & 0xFF) == 0x02) {
401 /* CHECK_CONDITION */
402 if (vstor_packet->vm_srb.srb_status & 0x80) {
403 /* autosense data available */
404 DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data "
405 "valid - len %d\n", request_ext,
406 vstor_packet->vm_srb.sense_info_length);
408 /* ASSERT(vstor_packet->vm_srb.sense_info_length <= */
409 /* request->SenseBufferSize); */
410 memcpy(request->sense_buffer,
411 vstor_packet->vm_srb.sense_data,
412 vstor_packet->vm_srb.sense_info_length);
414 request->sense_buffer_size =
415 vstor_packet->vm_srb.sense_info_length;
419 /* TODO: */
420 request->bytes_xfer = vstor_packet->vm_srb.data_transfer_length;
422 request->on_io_completion(request);
424 atomic_dec(&stor_device->num_outstanding_req);
426 put_stor_device(device);
429 static void stor_vsc_on_receive(struct hv_device *device,
430 struct vstor_packet *vstor_packet,
431 struct storvsc_request_extension *request_ext)
433 switch (vstor_packet->operation) {
434 case VSTOR_OPERATION_COMPLETE_IO:
435 DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION");
436 stor_vsc_on_io_completion(device, vstor_packet, request_ext);
437 break;
438 case VSTOR_OPERATION_REMOVE_DEVICE:
439 DPRINT_INFO(STORVSC, "REMOVE_DEVICE_OPERATION");
440 /* TODO: */
441 break;
443 default:
444 DPRINT_INFO(STORVSC, "Unknown operation received - %d",
445 vstor_packet->operation);
446 break;
450 static void stor_vsc_on_channel_callback(void *context)
452 struct hv_device *device = (struct hv_device *)context;
453 struct storvsc_device *stor_device;
454 u32 bytes_recvd;
455 u64 request_id;
456 unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
457 struct storvsc_request_extension *request;
458 int ret;
460 /* ASSERT(device); */
462 stor_device = must_get_stor_device(device);
463 if (!stor_device) {
464 DPRINT_ERR(STORVSC, "unable to get stor device..."
465 "device being destroyed?");
466 return;
469 do {
470 ret = vmbus_recvpacket(device->channel, packet,
471 ALIGN(sizeof(struct vstor_packet), 8),
472 &bytes_recvd, &request_id);
473 if (ret == 0 && bytes_recvd > 0) {
474 DPRINT_DBG(STORVSC, "receive %d bytes - tid %llx",
475 bytes_recvd, request_id);
477 /* ASSERT(bytes_recvd ==
478 sizeof(struct vstor_packet)); */
480 request = (struct storvsc_request_extension *)
481 (unsigned long)request_id;
482 /* ASSERT(request);c */
484 /* if (vstor_packet.Flags & SYNTHETIC_FLAG) */
485 if ((request == &stor_device->init_request) ||
486 (request == &stor_device->reset_request)) {
487 /* DPRINT_INFO(STORVSC,
488 * "reset completion - operation "
489 * "%u status %u",
490 * vstor_packet.Operation,
491 * vstor_packet.Status); */
493 memcpy(&request->vstor_packet, packet,
494 sizeof(struct vstor_packet));
495 request->wait_condition = 1;
496 wake_up(&request->wait_event);
497 } else {
498 stor_vsc_on_receive(device,
499 (struct vstor_packet *)packet,
500 request);
502 } else {
503 /* DPRINT_DBG(STORVSC, "nothing else to read..."); */
504 break;
506 } while (1);
508 put_stor_device(device);
509 return;
512 static int stor_vsc_connect_to_vsp(struct hv_device *device)
514 struct vmstorage_channel_properties props;
515 struct storvsc_driver_object *stor_driver;
516 int ret;
518 stor_driver = (struct storvsc_driver_object *)device->drv;
519 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
521 /* Open the channel */
522 ret = vmbus_open(device->channel,
523 stor_driver->ring_buffer_size,
524 stor_driver->ring_buffer_size,
525 (void *)&props,
526 sizeof(struct vmstorage_channel_properties),
527 stor_vsc_on_channel_callback, device);
529 DPRINT_DBG(STORVSC, "storage props: path id %d, tgt id %d, max xfer %d",
530 props.path_id, props.target_id, props.max_transfer_bytes);
532 if (ret != 0) {
533 DPRINT_ERR(STORVSC, "unable to open channel: %d", ret);
534 return -1;
537 ret = stor_vsc_channel_init(device);
539 return ret;
543 * stor_vsc_on_device_add - Callback when the device belonging to this driver
544 * is added
546 static int stor_vsc_on_device_add(struct hv_device *device,
547 void *additional_info)
549 struct storvsc_device *stor_device;
550 /* struct vmstorage_channel_properties *props; */
551 struct storvsc_device_info *device_info;
552 int ret = 0;
554 device_info = (struct storvsc_device_info *)additional_info;
555 stor_device = alloc_stor_device(device);
556 if (!stor_device) {
557 ret = -1;
558 goto cleanup;
561 /* Save the channel properties to our storvsc channel */
562 /* props = (struct vmstorage_channel_properties *)
563 * channel->offerMsg.Offer.u.Standard.UserDefined; */
565 /* FIXME: */
567 * If we support more than 1 scsi channel, we need to set the
568 * port number here to the scsi channel but how do we get the
569 * scsi channel prior to the bus scan
572 /* storChannel->PortNumber = 0;
573 storChannel->PathId = props->PathId;
574 storChannel->TargetId = props->TargetId; */
576 stor_device->port_number = device_info->port_number;
577 /* Send it back up */
578 ret = stor_vsc_connect_to_vsp(device);
580 /* device_info->PortNumber = stor_device->PortNumber; */
581 device_info->path_id = stor_device->path_id;
582 device_info->target_id = stor_device->target_id;
584 DPRINT_DBG(STORVSC, "assigned port %u, path %u target %u\n",
585 stor_device->port_number, stor_device->path_id,
586 stor_device->target_id);
588 cleanup:
589 return ret;
593 * stor_vsc_on_device_remove - Callback when the our device is being removed
595 static int stor_vsc_on_device_remove(struct hv_device *device)
597 struct storvsc_device *stor_device;
599 DPRINT_INFO(STORVSC, "disabling storage device (%p)...",
600 device->ext);
602 stor_device = release_stor_device(device);
605 * At this point, all outbound traffic should be disable. We
606 * only allow inbound traffic (responses) to proceed so that
607 * outstanding requests can be completed.
609 while (atomic_read(&stor_device->num_outstanding_req)) {
610 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...",
611 atomic_read(&stor_device->num_outstanding_req));
612 udelay(100);
615 DPRINT_INFO(STORVSC, "removing storage device (%p)...",
616 device->ext);
618 stor_device = final_release_stor_device(device);
620 DPRINT_INFO(STORVSC, "storage device (%p) safe to remove", stor_device);
622 /* Close the channel */
623 vmbus_close(device->channel);
625 free_stor_device(stor_device);
626 return 0;
629 int stor_vsc_on_host_reset(struct hv_device *device)
631 struct storvsc_device *stor_device;
632 struct storvsc_request_extension *request;
633 struct vstor_packet *vstor_packet;
634 int ret;
636 DPRINT_INFO(STORVSC, "resetting host adapter...");
638 stor_device = get_stor_device(device);
639 if (!stor_device) {
640 DPRINT_ERR(STORVSC, "unable to get stor device..."
641 "device being destroyed?");
642 return -1;
645 request = &stor_device->reset_request;
646 vstor_packet = &request->vstor_packet;
648 init_waitqueue_head(&request->wait_event);
650 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
651 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
652 vstor_packet->vm_srb.path_id = stor_device->path_id;
654 request->wait_condition = 0;
655 ret = vmbus_sendpacket(device->channel, vstor_packet,
656 sizeof(struct vstor_packet),
657 (unsigned long)&stor_device->reset_request,
658 VM_PKT_DATA_INBAND,
659 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
660 if (ret != 0) {
661 DPRINT_ERR(STORVSC, "Unable to send reset packet %p ret %d",
662 vstor_packet, ret);
663 goto cleanup;
666 wait_event_timeout(request->wait_event, request->wait_condition,
667 msecs_to_jiffies(1000));
668 if (request->wait_condition == 0) {
669 ret = -ETIMEDOUT;
670 goto cleanup;
673 DPRINT_INFO(STORVSC, "host adapter reset completed");
676 * At this point, all outstanding requests in the adapter
677 * should have been flushed out and return to us
680 cleanup:
681 put_stor_device(device);
682 return ret;
686 * stor_vsc_on_io_request - Callback to initiate an I/O request
688 static int stor_vsc_on_io_request(struct hv_device *device,
689 struct hv_storvsc_request *request)
691 struct storvsc_device *stor_device;
692 struct storvsc_request_extension *request_extension;
693 struct vstor_packet *vstor_packet;
694 int ret = 0;
696 request_extension =
697 (struct storvsc_request_extension *)request->extension;
698 vstor_packet = &request_extension->vstor_packet;
699 stor_device = get_stor_device(device);
701 DPRINT_DBG(STORVSC, "enter - Device %p, DeviceExt %p, Request %p, "
702 "Extension %p", device, stor_device, request,
703 request_extension);
705 DPRINT_DBG(STORVSC, "req %p len %d bus %d, target %d, lun %d cdblen %d",
706 request, request->data_buffer.len, request->bus,
707 request->target_id, request->lun_id, request->cdb_len);
709 if (!stor_device) {
710 DPRINT_ERR(STORVSC, "unable to get stor device..."
711 "device being destroyed?");
712 return -2;
715 /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, request->Cdb,
716 * request->CdbLen); */
718 request_extension->request = request;
719 request_extension->device = device;
721 memset(vstor_packet, 0 , sizeof(struct vstor_packet));
723 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
725 vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
727 vstor_packet->vm_srb.port_number = request->host;
728 vstor_packet->vm_srb.path_id = request->bus;
729 vstor_packet->vm_srb.target_id = request->target_id;
730 vstor_packet->vm_srb.lun = request->lun_id;
732 vstor_packet->vm_srb.sense_info_length = SENSE_BUFFER_SIZE;
734 /* Copy over the scsi command descriptor block */
735 vstor_packet->vm_srb.cdb_length = request->cdb_len;
736 memcpy(&vstor_packet->vm_srb.cdb, request->cdb, request->cdb_len);
738 vstor_packet->vm_srb.data_in = request->type;
739 vstor_packet->vm_srb.data_transfer_length = request->data_buffer.len;
741 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
743 DPRINT_DBG(STORVSC, "srb - len %d port %d, path %d, target %d, "
744 "lun %d senselen %d cdblen %d",
745 vstor_packet->vm_srb.length,
746 vstor_packet->vm_srb.port_number,
747 vstor_packet->vm_srb.path_id,
748 vstor_packet->vm_srb.target_id,
749 vstor_packet->vm_srb.lun,
750 vstor_packet->vm_srb.sense_info_length,
751 vstor_packet->vm_srb.cdb_length);
753 if (request_extension->request->data_buffer.len) {
754 ret = vmbus_sendpacket_multipagebuffer(device->channel,
755 &request_extension->request->data_buffer,
756 vstor_packet,
757 sizeof(struct vstor_packet),
758 (unsigned long)request_extension);
759 } else {
760 ret = vmbus_sendpacket(device->channel, vstor_packet,
761 sizeof(struct vstor_packet),
762 (unsigned long)request_extension,
763 VM_PKT_DATA_INBAND,
764 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
767 if (ret != 0) {
768 DPRINT_DBG(STORVSC, "Unable to send packet %p ret %d",
769 vstor_packet, ret);
772 atomic_inc(&stor_device->num_outstanding_req);
774 put_stor_device(device);
775 return ret;
779 * stor_vsc_on_cleanup - Perform any cleanup when the driver is removed
781 static void stor_vsc_on_cleanup(struct hv_driver *driver)
786 * stor_vsc_initialize - Main entry point
788 int stor_vsc_initialize(struct hv_driver *driver)
790 struct storvsc_driver_object *stor_driver;
792 stor_driver = (struct storvsc_driver_object *)driver;
794 DPRINT_DBG(STORVSC, "sizeof(STORVSC_REQUEST)=%zd "
795 "sizeof(struct storvsc_request_extension)=%zd "
796 "sizeof(struct vstor_packet)=%zd, "
797 "sizeof(struct vmscsi_request)=%zd",
798 sizeof(struct hv_storvsc_request),
799 sizeof(struct storvsc_request_extension),
800 sizeof(struct vstor_packet),
801 sizeof(struct vmscsi_request));
803 /* Make sure we are at least 2 pages since 1 page is used for control */
804 /* ASSERT(stor_driver->RingBufferSize >= (PAGE_SIZE << 1)); */
806 driver->name = g_driver_name;
807 memcpy(&driver->dev_type, &gStorVscDeviceType,
808 sizeof(struct hv_guid));
810 stor_driver->request_ext_size =
811 sizeof(struct storvsc_request_extension);
814 * Divide the ring buffer data size (which is 1 page less
815 * than the ring buffer size since that page is reserved for
816 * the ring buffer indices) by the max request size (which is
817 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
819 stor_driver->max_outstanding_req_per_channel =
820 ((stor_driver->ring_buffer_size - PAGE_SIZE) /
821 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
822 sizeof(struct vstor_packet) + sizeof(u64),
823 sizeof(u64)));
825 DPRINT_INFO(STORVSC, "max io %u, currently %u\n",
826 stor_driver->max_outstanding_req_per_channel,
827 STORVSC_MAX_IO_REQUESTS);
829 /* Setup the dispatch table */
830 stor_driver->base.dev_add = stor_vsc_on_device_add;
831 stor_driver->base.dev_rm = stor_vsc_on_device_remove;
832 stor_driver->base.cleanup = stor_vsc_on_cleanup;
834 stor_driver->on_io_request = stor_vsc_on_io_request;
836 return 0;