Staging: hv: coding style cleanups for StorVsc.c
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / hv / StorVsc.c
blobc06ea8c2e963b018f298d333af024f425ce08cb3
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/string.h>
23 #include <linux/mm.h>
24 #include <linux/delay.h>
25 #include "osd.h"
26 #include "logging.h"
27 #include "StorVscApi.h"
28 #include "VmbusPacketFormat.h"
29 #include "vstorage.h"
32 struct storvsc_request_extension {
33 /* LIST_ENTRY ListEntry; */
35 struct hv_storvsc_request *Request;
36 struct hv_device *Device;
38 /* Synchronize the request/response if needed */
39 struct osd_waitevent *WaitEvent;
41 struct vstor_packet VStorPacket;
44 /* A storvsc device is a device object that contains a vmbus channel */
45 struct storvsc_device {
46 struct hv_device *Device;
48 /* 0 indicates the device is being destroyed */
49 atomic_t RefCount;
51 atomic_t NumOutstandingRequests;
54 * Each unique Port/Path/Target represents 1 channel ie scsi
55 * controller. In reality, the pathid, targetid is always 0
56 * and the port is set by us
58 unsigned int PortNumber;
59 unsigned char PathId;
60 unsigned char TargetId;
62 /* LIST_ENTRY OutstandingRequestList; */
63 /* HANDLE OutstandingRequestLock; */
65 /* Used for vsc/vsp channel reset process */
66 struct storvsc_request_extension InitRequest;
67 struct storvsc_request_extension ResetRequest;
71 static const char *gDriverName = "storvsc";
73 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
74 static const struct hv_guid gStorVscDeviceType = {
75 .data = {
76 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
77 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f
82 static int StorVscOnDeviceAdd(struct hv_device *Device, void *AdditionalInfo);
83 static int StorVscOnDeviceRemove(struct hv_device *Device);
84 static int StorVscOnIORequest(struct hv_device *Device,
85 struct hv_storvsc_request *Request);
86 static int StorVscOnHostReset(struct hv_device *Device);
87 static void StorVscOnCleanup(struct hv_driver *Device);
88 static void StorVscOnChannelCallback(void *Context);
89 static void StorVscOnIOCompletion(struct hv_device *Device,
90 struct vstor_packet *VStorPacket,
91 struct storvsc_request_extension *RequestExt);
92 static void StorVscOnReceive(struct hv_device *Device,
93 struct vstor_packet *VStorPacket,
94 struct storvsc_request_extension *RequestExt);
95 static int StorVscConnectToVsp(struct hv_device *Device);
97 static inline struct storvsc_device *AllocStorDevice(struct hv_device *Device)
99 struct storvsc_device *storDevice;
101 storDevice = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
102 if (!storDevice)
103 return NULL;
105 /* Set to 2 to allow both inbound and outbound traffics */
106 /* (ie GetStorDevice() and MustGetStorDevice()) to proceed. */
107 atomic_cmpxchg(&storDevice->RefCount, 0, 2);
109 storDevice->Device = Device;
110 Device->Extension = storDevice;
112 return storDevice;
115 static inline void FreeStorDevice(struct storvsc_device *Device)
117 ASSERT(atomic_read(&Device->RefCount) == 0);
118 kfree(Device);
121 /* Get the stordevice object iff exists and its refcount > 1 */
122 static inline struct storvsc_device *GetStorDevice(struct hv_device *Device)
124 struct storvsc_device *storDevice;
126 storDevice = (struct storvsc_device *)Device->Extension;
127 if (storDevice && atomic_read(&storDevice->RefCount) > 1)
128 atomic_inc(&storDevice->RefCount);
129 else
130 storDevice = NULL;
132 return storDevice;
135 /* Get the stordevice object iff exists and its refcount > 0 */
136 static inline struct storvsc_device *MustGetStorDevice(struct hv_device *Device)
138 struct storvsc_device *storDevice;
140 storDevice = (struct storvsc_device *)Device->Extension;
141 if (storDevice && atomic_read(&storDevice->RefCount))
142 atomic_inc(&storDevice->RefCount);
143 else
144 storDevice = NULL;
146 return storDevice;
149 static inline void PutStorDevice(struct hv_device *Device)
151 struct storvsc_device *storDevice;
153 storDevice = (struct storvsc_device *)Device->Extension;
154 ASSERT(storDevice);
156 atomic_dec(&storDevice->RefCount);
157 ASSERT(atomic_read(&storDevice->RefCount));
160 /* Drop ref count to 1 to effectively disable GetStorDevice() */
161 static inline struct storvsc_device *ReleaseStorDevice(struct hv_device *Device)
163 struct storvsc_device *storDevice;
165 storDevice = (struct storvsc_device *)Device->Extension;
166 ASSERT(storDevice);
168 /* Busy wait until the ref drop to 2, then set it to 1 */
169 while (atomic_cmpxchg(&storDevice->RefCount, 2, 1) != 2)
170 udelay(100);
172 return storDevice;
175 /* Drop ref count to 0. No one can use StorDevice object. */
176 static inline struct storvsc_device *FinalReleaseStorDevice(
177 struct hv_device *Device)
179 struct storvsc_device *storDevice;
181 storDevice = (struct storvsc_device *)Device->Extension;
182 ASSERT(storDevice);
184 /* Busy wait until the ref drop to 1, then set it to 0 */
185 while (atomic_cmpxchg(&storDevice->RefCount, 1, 0) != 1)
186 udelay(100);
188 Device->Extension = NULL;
189 return storDevice;
193 * StorVscInitialize - Main entry point
195 int StorVscInitialize(struct hv_driver *Driver)
197 struct storvsc_driver_object *storDriver;
199 DPRINT_ENTER(STORVSC);
201 storDriver = (struct storvsc_driver_object *)Driver;
203 DPRINT_DBG(STORVSC, "sizeof(STORVSC_REQUEST)=%zd "
204 "sizeof(struct storvsc_request_extension)=%zd "
205 "sizeof(struct vstor_packet)=%zd, "
206 "sizeof(struct vmscsi_request)=%zd",
207 sizeof(struct hv_storvsc_request),
208 sizeof(struct storvsc_request_extension),
209 sizeof(struct vstor_packet),
210 sizeof(struct vmscsi_request));
212 /* Make sure we are at least 2 pages since 1 page is used for control */
213 ASSERT(storDriver->RingBufferSize >= (PAGE_SIZE << 1));
215 Driver->name = gDriverName;
216 memcpy(&Driver->deviceType, &gStorVscDeviceType,
217 sizeof(struct hv_guid));
219 storDriver->RequestExtSize = sizeof(struct storvsc_request_extension);
222 * Divide the ring buffer data size (which is 1 page less
223 * than the ring buffer size since that page is reserved for
224 * the ring buffer indices) by the max request size (which is
225 * VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER + struct vstor_packet + u64)
227 storDriver->MaxOutstandingRequestsPerChannel =
228 ((storDriver->RingBufferSize - PAGE_SIZE) /
229 ALIGN_UP(MAX_MULTIPAGE_BUFFER_PACKET +
230 sizeof(struct vstor_packet) + sizeof(u64),
231 sizeof(u64)));
233 DPRINT_INFO(STORVSC, "max io %u, currently %u\n",
234 storDriver->MaxOutstandingRequestsPerChannel,
235 STORVSC_MAX_IO_REQUESTS);
237 /* Setup the dispatch table */
238 storDriver->Base.OnDeviceAdd = StorVscOnDeviceAdd;
239 storDriver->Base.OnDeviceRemove = StorVscOnDeviceRemove;
240 storDriver->Base.OnCleanup = StorVscOnCleanup;
242 storDriver->OnIORequest = StorVscOnIORequest;
243 storDriver->OnHostReset = StorVscOnHostReset;
245 DPRINT_EXIT(STORVSC);
247 return 0;
251 * StorVscOnDeviceAdd - Callback when the device belonging to this driver is added
253 static int StorVscOnDeviceAdd(struct hv_device *Device, void *AdditionalInfo)
255 struct storvsc_device *storDevice;
256 /* struct vmstorage_channel_properties *props; */
257 struct storvsc_device_info *deviceInfo;
258 int ret = 0;
260 DPRINT_ENTER(STORVSC);
262 deviceInfo = (struct storvsc_device_info *)AdditionalInfo;
263 storDevice = AllocStorDevice(Device);
264 if (!storDevice) {
265 ret = -1;
266 goto Cleanup;
269 /* Save the channel properties to our storvsc channel */
270 /* props = (struct vmstorage_channel_properties *)
271 * channel->offerMsg.Offer.u.Standard.UserDefined; */
273 /* FIXME: */
275 * If we support more than 1 scsi channel, we need to set the
276 * port number here to the scsi channel but how do we get the
277 * scsi channel prior to the bus scan
280 /* storChannel->PortNumber = 0;
281 storChannel->PathId = props->PathId;
282 storChannel->TargetId = props->TargetId; */
284 storDevice->PortNumber = deviceInfo->PortNumber;
285 /* Send it back up */
286 ret = StorVscConnectToVsp(Device);
288 /* deviceInfo->PortNumber = storDevice->PortNumber; */
289 deviceInfo->PathId = storDevice->PathId;
290 deviceInfo->TargetId = storDevice->TargetId;
292 DPRINT_DBG(STORVSC, "assigned port %u, path %u target %u\n",
293 storDevice->PortNumber, storDevice->PathId,
294 storDevice->TargetId);
296 Cleanup:
297 DPRINT_EXIT(STORVSC);
299 return ret;
302 static int StorVscChannelInit(struct hv_device *Device)
304 struct storvsc_device *storDevice;
305 struct storvsc_request_extension *request;
306 struct vstor_packet *vstorPacket;
307 int ret;
309 storDevice = GetStorDevice(Device);
310 if (!storDevice) {
311 DPRINT_ERR(STORVSC, "unable to get stor device..."
312 "device being destroyed?");
313 DPRINT_EXIT(STORVSC);
314 return -1;
317 request = &storDevice->InitRequest;
318 vstorPacket = &request->VStorPacket;
321 * Now, initiate the vsc/vsp initialization protocol on the open
322 * channel
324 memset(request, sizeof(struct storvsc_request_extension), 0);
325 request->WaitEvent = osd_WaitEventCreate();
327 vstorPacket->Operation = VStorOperationBeginInitialization;
328 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
330 /*SpinlockAcquire(gDriverExt.packetListLock);
331 INSERT_TAIL_LIST(&gDriverExt.packetList, &packet->listEntry.entry);
332 SpinlockRelease(gDriverExt.packetListLock);*/
334 DPRINT_INFO(STORVSC, "BEGIN_INITIALIZATION_OPERATION...");
336 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
337 vstorPacket,
338 sizeof(struct vstor_packet),
339 (unsigned long)request,
340 VmbusPacketTypeDataInBand,
341 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
342 if (ret != 0) {
343 DPRINT_ERR(STORVSC,
344 "unable to send BEGIN_INITIALIZATION_OPERATION");
345 goto Cleanup;
348 osd_WaitEventWait(request->WaitEvent);
350 if (vstorPacket->Operation != VStorOperationCompleteIo ||
351 vstorPacket->Status != 0) {
352 DPRINT_ERR(STORVSC, "BEGIN_INITIALIZATION_OPERATION failed "
353 "(op %d status 0x%x)",
354 vstorPacket->Operation, vstorPacket->Status);
355 goto Cleanup;
358 DPRINT_INFO(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION...");
360 /* reuse the packet for version range supported */
361 memset(vstorPacket, sizeof(struct vstor_packet), 0);
362 vstorPacket->Operation = VStorOperationQueryProtocolVersion;
363 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
365 vstorPacket->Version.MajorMinor = VMSTOR_PROTOCOL_VERSION_CURRENT;
366 FILL_VMSTOR_REVISION(vstorPacket->Version.Revision);
368 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
369 vstorPacket,
370 sizeof(struct vstor_packet),
371 (unsigned long)request,
372 VmbusPacketTypeDataInBand,
373 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
374 if (ret != 0) {
375 DPRINT_ERR(STORVSC,
376 "unable to send BEGIN_INITIALIZATION_OPERATION");
377 goto Cleanup;
380 osd_WaitEventWait(request->WaitEvent);
382 /* TODO: Check returned version */
383 if (vstorPacket->Operation != VStorOperationCompleteIo ||
384 vstorPacket->Status != 0) {
385 DPRINT_ERR(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION failed "
386 "(op %d status 0x%x)",
387 vstorPacket->Operation, vstorPacket->Status);
388 goto Cleanup;
391 /* Query channel properties */
392 DPRINT_INFO(STORVSC, "QUERY_PROPERTIES_OPERATION...");
394 memset(vstorPacket, sizeof(struct vstor_packet), 0);
395 vstorPacket->Operation = VStorOperationQueryProperties;
396 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
397 vstorPacket->StorageChannelProperties.PortNumber =
398 storDevice->PortNumber;
400 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
401 vstorPacket,
402 sizeof(struct vstor_packet),
403 (unsigned long)request,
404 VmbusPacketTypeDataInBand,
405 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
407 if (ret != 0) {
408 DPRINT_ERR(STORVSC,
409 "unable to send QUERY_PROPERTIES_OPERATION");
410 goto Cleanup;
413 osd_WaitEventWait(request->WaitEvent);
415 /* TODO: Check returned version */
416 if (vstorPacket->Operation != VStorOperationCompleteIo ||
417 vstorPacket->Status != 0) {
418 DPRINT_ERR(STORVSC, "QUERY_PROPERTIES_OPERATION failed "
419 "(op %d status 0x%x)",
420 vstorPacket->Operation, vstorPacket->Status);
421 goto Cleanup;
424 storDevice->PathId = vstorPacket->StorageChannelProperties.PathId;
425 storDevice->TargetId = vstorPacket->StorageChannelProperties.TargetId;
427 DPRINT_DBG(STORVSC, "channel flag 0x%x, max xfer len 0x%x",
428 vstorPacket->StorageChannelProperties.Flags,
429 vstorPacket->StorageChannelProperties.MaxTransferBytes);
431 DPRINT_INFO(STORVSC, "END_INITIALIZATION_OPERATION...");
433 memset(vstorPacket, sizeof(struct vstor_packet), 0);
434 vstorPacket->Operation = VStorOperationEndInitialization;
435 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
437 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
438 vstorPacket,
439 sizeof(struct vstor_packet),
440 (unsigned long)request,
441 VmbusPacketTypeDataInBand,
442 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
444 if (ret != 0) {
445 DPRINT_ERR(STORVSC,
446 "unable to send END_INITIALIZATION_OPERATION");
447 goto Cleanup;
450 osd_WaitEventWait(request->WaitEvent);
452 if (vstorPacket->Operation != VStorOperationCompleteIo ||
453 vstorPacket->Status != 0) {
454 DPRINT_ERR(STORVSC, "END_INITIALIZATION_OPERATION failed "
455 "(op %d status 0x%x)",
456 vstorPacket->Operation, vstorPacket->Status);
457 goto Cleanup;
460 DPRINT_INFO(STORVSC, "**** storage channel up and running!! ****");
462 Cleanup:
463 kfree(request->WaitEvent);
464 request->WaitEvent = NULL;
466 PutStorDevice(Device);
468 DPRINT_EXIT(STORVSC);
469 return ret;
472 static int StorVscConnectToVsp(struct hv_device *Device)
474 struct vmstorage_channel_properties props;
475 struct storvsc_driver_object *storDriver;
476 int ret;
478 storDriver = (struct storvsc_driver_object *)Device->Driver;
479 memset(&props, sizeof(struct vmstorage_channel_properties), 0);
481 /* Open the channel */
482 ret = Device->Driver->VmbusChannelInterface.Open(Device,
483 storDriver->RingBufferSize,
484 storDriver->RingBufferSize,
485 (void *)&props,
486 sizeof(struct vmstorage_channel_properties),
487 StorVscOnChannelCallback,
488 Device);
490 DPRINT_DBG(STORVSC, "storage props: path id %d, tgt id %d, max xfer %d",
491 props.PathId, props.TargetId, props.MaxTransferBytes);
493 if (ret != 0) {
494 DPRINT_ERR(STORVSC, "unable to open channel: %d", ret);
495 return -1;
498 ret = StorVscChannelInit(Device);
500 return ret;
505 * StorVscOnDeviceRemove - Callback when the our device is being removed
507 static int StorVscOnDeviceRemove(struct hv_device *Device)
509 struct storvsc_device *storDevice;
511 DPRINT_ENTER(STORVSC);
513 DPRINT_INFO(STORVSC, "disabling storage device (%p)...",
514 Device->Extension);
516 storDevice = ReleaseStorDevice(Device);
519 * At this point, all outbound traffic should be disable. We
520 * only allow inbound traffic (responses) to proceed so that
521 * outstanding requests can be completed.
523 while (atomic_read(&storDevice->NumOutstandingRequests)) {
524 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...",
525 atomic_read(&storDevice->NumOutstandingRequests));
526 udelay(100);
529 DPRINT_INFO(STORVSC, "removing storage device (%p)...",
530 Device->Extension);
532 storDevice = FinalReleaseStorDevice(Device);
534 DPRINT_INFO(STORVSC, "storage device (%p) safe to remove", storDevice);
536 /* Close the channel */
537 Device->Driver->VmbusChannelInterface.Close(Device);
539 FreeStorDevice(storDevice);
541 DPRINT_EXIT(STORVSC);
542 return 0;
545 static int StorVscOnHostReset(struct hv_device *Device)
547 struct storvsc_device *storDevice;
548 struct storvsc_request_extension *request;
549 struct vstor_packet *vstorPacket;
550 int ret;
552 DPRINT_ENTER(STORVSC);
554 DPRINT_INFO(STORVSC, "resetting host adapter...");
556 storDevice = GetStorDevice(Device);
557 if (!storDevice) {
558 DPRINT_ERR(STORVSC, "unable to get stor device..."
559 "device being destroyed?");
560 DPRINT_EXIT(STORVSC);
561 return -1;
564 request = &storDevice->ResetRequest;
565 vstorPacket = &request->VStorPacket;
567 request->WaitEvent = osd_WaitEventCreate();
569 vstorPacket->Operation = VStorOperationResetBus;
570 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
571 vstorPacket->VmSrb.PathId = storDevice->PathId;
573 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
574 vstorPacket,
575 sizeof(struct vstor_packet),
576 (unsigned long)&storDevice->ResetRequest,
577 VmbusPacketTypeDataInBand,
578 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
579 if (ret != 0) {
580 DPRINT_ERR(STORVSC, "Unable to send reset packet %p ret %d",
581 vstorPacket, ret);
582 goto Cleanup;
585 /* FIXME: Add a timeout */
586 osd_WaitEventWait(request->WaitEvent);
588 kfree(request->WaitEvent);
589 DPRINT_INFO(STORVSC, "host adapter reset completed");
592 * At this point, all outstanding requests in the adapter
593 * should have been flushed out and return to us
596 Cleanup:
597 PutStorDevice(Device);
598 DPRINT_EXIT(STORVSC);
599 return ret;
603 * StorVscOnIORequest - Callback to initiate an I/O request
605 static int StorVscOnIORequest(struct hv_device *Device,
606 struct hv_storvsc_request *Request)
608 struct storvsc_device *storDevice;
609 struct storvsc_request_extension *requestExtension;
610 struct vstor_packet *vstorPacket;
611 int ret = 0;
613 DPRINT_ENTER(STORVSC);
615 requestExtension =
616 (struct storvsc_request_extension *)Request->Extension;
617 vstorPacket = &requestExtension->VStorPacket;
618 storDevice = GetStorDevice(Device);
620 DPRINT_DBG(STORVSC, "enter - Device %p, DeviceExt %p, Request %p, "
621 "Extension %p", Device, storDevice, Request,
622 requestExtension);
624 DPRINT_DBG(STORVSC, "req %p len %d bus %d, target %d, lun %d cdblen %d",
625 Request, Request->DataBuffer.Length, Request->Bus,
626 Request->TargetId, Request->LunId, Request->CdbLen);
628 if (!storDevice) {
629 DPRINT_ERR(STORVSC, "unable to get stor device..."
630 "device being destroyed?");
631 DPRINT_EXIT(STORVSC);
632 return -2;
635 /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, Request->Cdb,
636 * Request->CdbLen); */
638 requestExtension->Request = Request;
639 requestExtension->Device = Device;
641 memset(vstorPacket, 0 , sizeof(struct vstor_packet));
643 vstorPacket->Flags |= REQUEST_COMPLETION_FLAG;
645 vstorPacket->VmSrb.Length = sizeof(struct vmscsi_request);
647 vstorPacket->VmSrb.PortNumber = Request->Host;
648 vstorPacket->VmSrb.PathId = Request->Bus;
649 vstorPacket->VmSrb.TargetId = Request->TargetId;
650 vstorPacket->VmSrb.Lun = Request->LunId;
652 vstorPacket->VmSrb.SenseInfoLength = SENSE_BUFFER_SIZE;
654 /* Copy over the scsi command descriptor block */
655 vstorPacket->VmSrb.CdbLength = Request->CdbLen;
656 memcpy(&vstorPacket->VmSrb.Cdb, Request->Cdb, Request->CdbLen);
658 vstorPacket->VmSrb.DataIn = Request->Type;
659 vstorPacket->VmSrb.DataTransferLength = Request->DataBuffer.Length;
661 vstorPacket->Operation = VStorOperationExecuteSRB;
663 DPRINT_DBG(STORVSC, "srb - len %d port %d, path %d, target %d, "
664 "lun %d senselen %d cdblen %d",
665 vstorPacket->VmSrb.Length,
666 vstorPacket->VmSrb.PortNumber,
667 vstorPacket->VmSrb.PathId,
668 vstorPacket->VmSrb.TargetId,
669 vstorPacket->VmSrb.Lun,
670 vstorPacket->VmSrb.SenseInfoLength,
671 vstorPacket->VmSrb.CdbLength);
673 if (requestExtension->Request->DataBuffer.Length) {
674 ret = Device->Driver->VmbusChannelInterface.
675 SendPacketMultiPageBuffer(Device,
676 &requestExtension->Request->DataBuffer,
677 vstorPacket,
678 sizeof(struct vstor_packet),
679 (unsigned long)requestExtension);
680 } else {
681 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
682 vstorPacket,
683 sizeof(struct vstor_packet),
684 (unsigned long)requestExtension,
685 VmbusPacketTypeDataInBand,
686 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
689 if (ret != 0) {
690 DPRINT_DBG(STORVSC, "Unable to send packet %p ret %d",
691 vstorPacket, ret);
694 atomic_inc(&storDevice->NumOutstandingRequests);
696 PutStorDevice(Device);
698 DPRINT_EXIT(STORVSC);
699 return ret;
703 * StorVscOnCleanup - Perform any cleanup when the driver is removed
705 static void StorVscOnCleanup(struct hv_driver *Driver)
707 DPRINT_ENTER(STORVSC);
708 DPRINT_EXIT(STORVSC);
711 static void StorVscOnIOCompletion(struct hv_device *Device,
712 struct vstor_packet *VStorPacket,
713 struct storvsc_request_extension *RequestExt)
715 struct hv_storvsc_request *request;
716 struct storvsc_device *storDevice;
718 DPRINT_ENTER(STORVSC);
720 storDevice = MustGetStorDevice(Device);
721 if (!storDevice) {
722 DPRINT_ERR(STORVSC, "unable to get stor device..."
723 "device being destroyed?");
724 DPRINT_EXIT(STORVSC);
725 return;
728 DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION - request extension %p "
729 "completed bytes xfer %u", RequestExt,
730 VStorPacket->VmSrb.DataTransferLength);
732 ASSERT(RequestExt != NULL);
733 ASSERT(RequestExt->Request != NULL);
735 request = RequestExt->Request;
737 ASSERT(request->OnIOCompletion != NULL);
739 /* Copy over the status...etc */
740 request->Status = VStorPacket->VmSrb.ScsiStatus;
742 if (request->Status != 0 || VStorPacket->VmSrb.SrbStatus != 1) {
743 DPRINT_WARN(STORVSC,
744 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
745 request->Cdb[0], VStorPacket->VmSrb.ScsiStatus,
746 VStorPacket->VmSrb.SrbStatus);
749 if ((request->Status & 0xFF) == 0x02) {
750 /* CHECK_CONDITION */
751 if (VStorPacket->VmSrb.SrbStatus & 0x80) {
752 /* autosense data available */
753 DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data "
754 "valid - len %d\n", RequestExt,
755 VStorPacket->VmSrb.SenseInfoLength);
757 ASSERT(VStorPacket->VmSrb.SenseInfoLength <=
758 request->SenseBufferSize);
759 memcpy(request->SenseBuffer,
760 VStorPacket->VmSrb.SenseData,
761 VStorPacket->VmSrb.SenseInfoLength);
763 request->SenseBufferSize =
764 VStorPacket->VmSrb.SenseInfoLength;
768 /* TODO: */
769 request->BytesXfer = VStorPacket->VmSrb.DataTransferLength;
771 request->OnIOCompletion(request);
773 atomic_dec(&storDevice->NumOutstandingRequests);
775 PutStorDevice(Device);
777 DPRINT_EXIT(STORVSC);
781 static void StorVscOnReceive(struct hv_device *Device,
782 struct vstor_packet *VStorPacket,
783 struct storvsc_request_extension *RequestExt)
785 switch (VStorPacket->Operation) {
786 case VStorOperationCompleteIo:
787 DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION");
788 StorVscOnIOCompletion(Device, VStorPacket, RequestExt);
789 break;
790 case VStorOperationRemoveDevice:
791 DPRINT_INFO(STORVSC, "REMOVE_DEVICE_OPERATION");
792 /* TODO: */
793 break;
795 default:
796 DPRINT_INFO(STORVSC, "Unknown operation received - %d",
797 VStorPacket->Operation);
798 break;
802 static void StorVscOnChannelCallback(void *context)
804 struct hv_device *device = (struct hv_device *)context;
805 struct storvsc_device *storDevice;
806 u32 bytesRecvd;
807 u64 requestId;
808 unsigned char packet[ALIGN_UP(sizeof(struct vstor_packet), 8)];
809 struct storvsc_request_extension *request;
810 int ret;
812 DPRINT_ENTER(STORVSC);
814 ASSERT(device);
816 storDevice = MustGetStorDevice(device);
817 if (!storDevice) {
818 DPRINT_ERR(STORVSC, "unable to get stor device..."
819 "device being destroyed?");
820 DPRINT_EXIT(STORVSC);
821 return;
824 do {
825 ret = device->Driver->VmbusChannelInterface.RecvPacket(device,
826 packet,
827 ALIGN_UP(sizeof(struct vstor_packet), 8),
828 &bytesRecvd, &requestId);
829 if (ret == 0 && bytesRecvd > 0) {
830 DPRINT_DBG(STORVSC, "receive %d bytes - tid %llx",
831 bytesRecvd, requestId);
833 /* ASSERT(bytesRecvd == sizeof(struct vstor_packet)); */
835 request = (struct storvsc_request_extension *)
836 (unsigned long)requestId;
837 ASSERT(request);
839 /* if (vstorPacket.Flags & SYNTHETIC_FLAG) */
840 if ((request == &storDevice->InitRequest) ||
841 (request == &storDevice->ResetRequest)) {
842 /* DPRINT_INFO(STORVSC,
843 * "reset completion - operation "
844 * "%u status %u",
845 * vstorPacket.Operation,
846 * vstorPacket.Status); */
848 memcpy(&request->VStorPacket, packet,
849 sizeof(struct vstor_packet));
851 osd_WaitEventSet(request->WaitEvent);
852 } else {
853 StorVscOnReceive(device,
854 (struct vstor_packet *)packet,
855 request);
857 } else {
858 /* DPRINT_DBG(STORVSC, "nothing else to read..."); */
859 break;
861 } while (1);
863 PutStorDevice(device);
865 DPRINT_EXIT(STORVSC);
866 return;