]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/staging/hv/StorVsc.c
Staging: hv: osd: remove MemAllocZeroed wrapper
[mirror_ubuntu-bionic-kernel.git] / drivers / staging / hv / StorVsc.c
1 /*
2 *
3 * Copyright (c) 2009, Microsoft Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307 USA.
17 *
18 * Authors:
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
21 *
22 */
23
24 #define KERNEL_2_6_27
25
26 #include <linux/kernel.h>
27 #include <linux/string.h>
28 #include <linux/mm.h>
29 #include "include/logging.h"
30
31 #include "include/StorVscApi.h"
32 #include "include/VmbusPacketFormat.h"
33 #include "include/vstorage.h"
34
35
36 //
37 // #defines
38 //
39
40 //
41 // Data types
42 //
43
44 typedef struct _STORVSC_REQUEST_EXTENSION {
45 //LIST_ENTRY ListEntry;
46
47 STORVSC_REQUEST *Request;
48 DEVICE_OBJECT *Device;
49
50 // Synchronize the request/response if needed
51 HANDLE WaitEvent;
52
53 VSTOR_PACKET VStorPacket;
54 } STORVSC_REQUEST_EXTENSION;
55
56
57 // A storvsc device is a device object that contains a vmbus channel
58 typedef struct _STORVSC_DEVICE{
59 DEVICE_OBJECT *Device;
60
61 int RefCount; // 0 indicates the device is being destroyed
62
63 int NumOutstandingRequests;
64
65 // Each unique Port/Path/Target represents 1 channel ie scsi controller. In reality, the pathid, targetid is always 0
66 // and the port is set by us
67 unsigned int PortNumber;
68 unsigned char PathId;
69 unsigned char TargetId;
70
71 //LIST_ENTRY OutstandingRequestList;
72 //HANDLE OutstandingRequestLock;
73
74 // Used for vsc/vsp channel reset process
75 STORVSC_REQUEST_EXTENSION InitRequest;
76
77 STORVSC_REQUEST_EXTENSION ResetRequest;
78
79 } STORVSC_DEVICE;
80
81
82 //
83 // Globals
84 //
85 static const char* gDriverName="storvsc";
86
87 //{ba6163d9-04a1-4d29-b605-72e2ffb1dc7f}
88 static const GUID gStorVscDeviceType={
89 .Data = {0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d, 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f}
90 };
91
92 //
93 // Internal routines
94 //
95 static int
96 StorVscOnDeviceAdd(
97 DEVICE_OBJECT *Device,
98 void *AdditionalInfo
99 );
100
101 static int
102 StorVscOnDeviceRemove(
103 DEVICE_OBJECT *Device
104 );
105
106 static int
107 StorVscOnIORequest(
108 DEVICE_OBJECT *Device,
109 STORVSC_REQUEST *Request
110 );
111
112 static int
113 StorVscOnHostReset(
114 DEVICE_OBJECT *Device
115 );
116
117 static void
118 StorVscOnCleanup(
119 DRIVER_OBJECT *Device
120 );
121
122 static void
123 StorVscOnChannelCallback(
124 void * Context
125 );
126
127 static void
128 StorVscOnIOCompletion(
129 DEVICE_OBJECT *Device,
130 VSTOR_PACKET *VStorPacket,
131 STORVSC_REQUEST_EXTENSION *RequestExt
132 );
133
134 static void
135 StorVscOnReceive(
136 DEVICE_OBJECT *Device,
137 VSTOR_PACKET *VStorPacket,
138 STORVSC_REQUEST_EXTENSION *RequestExt
139 );
140
141 static int
142 StorVscConnectToVsp(
143 DEVICE_OBJECT *Device
144 );
145
146 static inline STORVSC_DEVICE* AllocStorDevice(DEVICE_OBJECT *Device)
147 {
148 STORVSC_DEVICE *storDevice;
149
150 storDevice = kzalloc(sizeof(STORVSC_DEVICE), GFP_KERNEL);
151 if (!storDevice)
152 return NULL;
153
154 // Set to 2 to allow both inbound and outbound traffics
155 // (ie GetStorDevice() and MustGetStorDevice()) to proceed.
156 InterlockedCompareExchange(&storDevice->RefCount, 2, 0);
157
158 storDevice->Device = Device;
159 Device->Extension = storDevice;
160
161 return storDevice;
162 }
163
164 static inline void FreeStorDevice(STORVSC_DEVICE *Device)
165 {
166 ASSERT(Device->RefCount == 0);
167 MemFree(Device);
168 }
169
170 // Get the stordevice object iff exists and its refcount > 1
171 static inline STORVSC_DEVICE* GetStorDevice(DEVICE_OBJECT *Device)
172 {
173 STORVSC_DEVICE *storDevice;
174
175 storDevice = (STORVSC_DEVICE*)Device->Extension;
176 if (storDevice && storDevice->RefCount > 1)
177 {
178 InterlockedIncrement(&storDevice->RefCount);
179 }
180 else
181 {
182 storDevice = NULL;
183 }
184
185 return storDevice;
186 }
187
188 // Get the stordevice object iff exists and its refcount > 0
189 static inline STORVSC_DEVICE* MustGetStorDevice(DEVICE_OBJECT *Device)
190 {
191 STORVSC_DEVICE *storDevice;
192
193 storDevice = (STORVSC_DEVICE*)Device->Extension;
194 if (storDevice && storDevice->RefCount)
195 {
196 InterlockedIncrement(&storDevice->RefCount);
197 }
198 else
199 {
200 storDevice = NULL;
201 }
202
203 return storDevice;
204 }
205
206 static inline void PutStorDevice(DEVICE_OBJECT *Device)
207 {
208 STORVSC_DEVICE *storDevice;
209
210 storDevice = (STORVSC_DEVICE*)Device->Extension;
211 ASSERT(storDevice);
212
213 InterlockedDecrement(&storDevice->RefCount);
214 ASSERT(storDevice->RefCount);
215 }
216
217 // Drop ref count to 1 to effectively disable GetStorDevice()
218 static inline STORVSC_DEVICE* ReleaseStorDevice(DEVICE_OBJECT *Device)
219 {
220 STORVSC_DEVICE *storDevice;
221
222 storDevice = (STORVSC_DEVICE*)Device->Extension;
223 ASSERT(storDevice);
224
225 // Busy wait until the ref drop to 2, then set it to 1
226 while (InterlockedCompareExchange(&storDevice->RefCount, 1, 2) != 2)
227 {
228 Sleep(100);
229 }
230
231 return storDevice;
232 }
233
234 // Drop ref count to 0. No one can use StorDevice object.
235 static inline STORVSC_DEVICE* FinalReleaseStorDevice(DEVICE_OBJECT *Device)
236 {
237 STORVSC_DEVICE *storDevice;
238
239 storDevice = (STORVSC_DEVICE*)Device->Extension;
240 ASSERT(storDevice);
241
242 // Busy wait until the ref drop to 1, then set it to 0
243 while (InterlockedCompareExchange(&storDevice->RefCount, 0, 1) != 1)
244 {
245 Sleep(100);
246 }
247
248 Device->Extension = NULL;
249 return storDevice;
250 }
251
252 /*++;
253
254
255 Name:
256 StorVscInitialize()
257
258 Description:
259 Main entry point
260
261 --*/
262 int
263 StorVscInitialize(
264 DRIVER_OBJECT *Driver
265 )
266 {
267 STORVSC_DRIVER_OBJECT* storDriver = (STORVSC_DRIVER_OBJECT*)Driver;
268 int ret=0;
269
270 DPRINT_ENTER(STORVSC);
271
272 DPRINT_DBG(STORVSC, "sizeof(STORVSC_REQUEST)=%d sizeof(STORVSC_REQUEST_EXTENSION)=%d sizeof(VSTOR_PACKET)=%d, sizeof(VMSCSI_REQUEST)=%d",
273 sizeof(STORVSC_REQUEST), sizeof(STORVSC_REQUEST_EXTENSION), sizeof(VSTOR_PACKET), sizeof(VMSCSI_REQUEST));
274
275 // Make sure we are at least 2 pages since 1 page is used for control
276 ASSERT(storDriver->RingBufferSize >= (PAGE_SIZE << 1));
277
278 Driver->name = gDriverName;
279 memcpy(&Driver->deviceType, &gStorVscDeviceType, sizeof(GUID));
280
281 storDriver->RequestExtSize = sizeof(STORVSC_REQUEST_EXTENSION);
282
283 // Divide the ring buffer data size (which is 1 page less than the ring buffer size since that page is reserved for the ring buffer indices)
284 // by the max request size (which is VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER + VSTOR_PACKET + u64)
285 storDriver->MaxOutstandingRequestsPerChannel =
286 ((storDriver->RingBufferSize - PAGE_SIZE) / ALIGN_UP(MAX_MULTIPAGE_BUFFER_PACKET + sizeof(VSTOR_PACKET) + sizeof(u64),sizeof(u64)));
287
288 DPRINT_INFO(STORVSC, "max io %u, currently %u\n", storDriver->MaxOutstandingRequestsPerChannel, STORVSC_MAX_IO_REQUESTS);
289
290 // Setup the dispatch table
291 storDriver->Base.OnDeviceAdd = StorVscOnDeviceAdd;
292 storDriver->Base.OnDeviceRemove = StorVscOnDeviceRemove;
293 storDriver->Base.OnCleanup = StorVscOnCleanup;
294
295 storDriver->OnIORequest = StorVscOnIORequest;
296 storDriver->OnHostReset = StorVscOnHostReset;
297
298 DPRINT_EXIT(STORVSC);
299
300 return ret;
301 }
302
303 /*++
304
305 Name:
306 StorVscOnDeviceAdd()
307
308 Description:
309 Callback when the device belonging to this driver is added
310
311 --*/
312 int
313 StorVscOnDeviceAdd(
314 DEVICE_OBJECT *Device,
315 void *AdditionalInfo
316 )
317 {
318 int ret=0;
319 STORVSC_DEVICE *storDevice;
320 //VMSTORAGE_CHANNEL_PROPERTIES *props;
321 STORVSC_DEVICE_INFO *deviceInfo = (STORVSC_DEVICE_INFO*)AdditionalInfo;
322
323 DPRINT_ENTER(STORVSC);
324
325 storDevice = AllocStorDevice(Device);
326 if (!storDevice)
327 {
328 ret = -1;
329 goto Cleanup;
330 }
331
332 // Save the channel properties to our storvsc channel
333 //props = (VMSTORAGE_CHANNEL_PROPERTIES*) channel->offerMsg.Offer.u.Standard.UserDefined;
334
335 // FIXME:
336 // If we support more than 1 scsi channel, we need to set the port number here
337 // to the scsi channel but how do we get the scsi channel prior to the bus scan
338 /*storChannel->PortNumber = 0;
339 storChannel->PathId = props->PathId;
340 storChannel->TargetId = props->TargetId;*/
341
342 storDevice->PortNumber = deviceInfo->PortNumber;
343 // Send it back up
344 ret = StorVscConnectToVsp(Device);
345
346 //deviceInfo->PortNumber = storDevice->PortNumber;
347 deviceInfo->PathId = storDevice->PathId;
348 deviceInfo->TargetId = storDevice->TargetId;
349
350 DPRINT_DBG(STORVSC, "assigned port %u, path %u target %u\n", storDevice->PortNumber, storDevice->PathId, storDevice->TargetId);
351
352 Cleanup:
353 DPRINT_EXIT(STORVSC);
354
355 return ret;
356 }
357
358 static int StorVscChannelInit(DEVICE_OBJECT *Device)
359 {
360 int ret=0;
361 STORVSC_DEVICE *storDevice;
362 STORVSC_REQUEST_EXTENSION *request;
363 VSTOR_PACKET *vstorPacket;
364
365 storDevice = GetStorDevice(Device);
366 if (!storDevice)
367 {
368 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
369 DPRINT_EXIT(STORVSC);
370 return -1;
371 }
372
373 request = &storDevice->InitRequest;
374 vstorPacket = &request->VStorPacket;
375
376 // Now, initiate the vsc/vsp initialization protocol on the open channel
377
378 memset(request, sizeof(STORVSC_REQUEST_EXTENSION), 0);
379 request->WaitEvent = WaitEventCreate();
380
381 vstorPacket->Operation = VStorOperationBeginInitialization;
382 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
383
384 /*SpinlockAcquire(gDriverExt.packetListLock);
385 INSERT_TAIL_LIST(&gDriverExt.packetList, &packet->listEntry.entry);
386 SpinlockRelease(gDriverExt.packetListLock);*/
387
388 DPRINT_INFO(STORVSC, "BEGIN_INITIALIZATION_OPERATION...");
389
390 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
391 vstorPacket,
392 sizeof(VSTOR_PACKET),
393 (unsigned long)request,
394 VmbusPacketTypeDataInBand,
395 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
396 if ( ret != 0)
397 {
398 DPRINT_ERR(STORVSC, "unable to send BEGIN_INITIALIZATION_OPERATION");
399 goto Cleanup;
400 }
401
402 WaitEventWait(request->WaitEvent);
403
404 if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
405 {
406 DPRINT_ERR(STORVSC, "BEGIN_INITIALIZATION_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
407 goto Cleanup;
408 }
409
410 DPRINT_INFO(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION...");
411
412 // reuse the packet for version range supported
413 memset(vstorPacket, sizeof(VSTOR_PACKET), 0);
414 vstorPacket->Operation = VStorOperationQueryProtocolVersion;
415 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
416
417 vstorPacket->Version.MajorMinor = VMSTOR_PROTOCOL_VERSION_CURRENT;
418 FILL_VMSTOR_REVISION(vstorPacket->Version.Revision);
419
420 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
421 vstorPacket,
422 sizeof(VSTOR_PACKET),
423 (unsigned long)request,
424 VmbusPacketTypeDataInBand,
425 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
426 if ( ret != 0)
427 {
428 DPRINT_ERR(STORVSC, "unable to send BEGIN_INITIALIZATION_OPERATION");
429 goto Cleanup;
430 }
431
432 WaitEventWait(request->WaitEvent);
433
434 // TODO: Check returned version
435 if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
436 {
437 DPRINT_ERR(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
438 goto Cleanup;
439 }
440
441 // Query channel properties
442 DPRINT_INFO(STORVSC, "QUERY_PROPERTIES_OPERATION...");
443
444 memset(vstorPacket, sizeof(VSTOR_PACKET), 0);
445 vstorPacket->Operation = VStorOperationQueryProperties;
446 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
447 vstorPacket->StorageChannelProperties.PortNumber = storDevice->PortNumber;
448
449 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
450 vstorPacket,
451 sizeof(VSTOR_PACKET),
452 (unsigned long)request,
453 VmbusPacketTypeDataInBand,
454 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
455
456 if ( ret != 0)
457 {
458 DPRINT_ERR(STORVSC, "unable to send QUERY_PROPERTIES_OPERATION");
459 goto Cleanup;
460 }
461
462 WaitEventWait(request->WaitEvent);
463
464 // TODO: Check returned version
465 if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
466 {
467 DPRINT_ERR(STORVSC, "QUERY_PROPERTIES_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
468 goto Cleanup;
469 }
470
471 //storDevice->PortNumber = vstorPacket->StorageChannelProperties.PortNumber;
472 storDevice->PathId = vstorPacket->StorageChannelProperties.PathId;
473 storDevice->TargetId = vstorPacket->StorageChannelProperties.TargetId;
474
475 DPRINT_DBG(STORVSC, "channel flag 0x%x, max xfer len 0x%x", vstorPacket->StorageChannelProperties.Flags, vstorPacket->StorageChannelProperties.MaxTransferBytes);
476
477 DPRINT_INFO(STORVSC, "END_INITIALIZATION_OPERATION...");
478
479 memset(vstorPacket, sizeof(VSTOR_PACKET), 0);
480 vstorPacket->Operation = VStorOperationEndInitialization;
481 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
482
483 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
484 vstorPacket,
485 sizeof(VSTOR_PACKET),
486 (unsigned long)request,
487 VmbusPacketTypeDataInBand,
488 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
489
490 if ( ret != 0)
491 {
492 DPRINT_ERR(STORVSC, "unable to send END_INITIALIZATION_OPERATION");
493 goto Cleanup;
494 }
495
496 WaitEventWait(request->WaitEvent);
497
498 if (vstorPacket->Operation != VStorOperationCompleteIo || vstorPacket->Status != 0)
499 {
500 DPRINT_ERR(STORVSC, "END_INITIALIZATION_OPERATION failed (op %d status 0x%x)", vstorPacket->Operation, vstorPacket->Status);
501 goto Cleanup;
502 }
503
504 DPRINT_INFO(STORVSC, "**** storage channel up and running!! ****");
505
506 Cleanup:
507 if (request->WaitEvent)
508 {
509 WaitEventClose(request->WaitEvent);
510 request->WaitEvent = NULL;
511 }
512
513 PutStorDevice(Device);
514
515 DPRINT_EXIT(STORVSC);
516 return ret;
517 }
518
519
520 int
521 StorVscConnectToVsp(
522 DEVICE_OBJECT *Device
523 )
524 {
525 int ret=0;
526 VMSTORAGE_CHANNEL_PROPERTIES props;
527
528 STORVSC_DRIVER_OBJECT *storDriver = (STORVSC_DRIVER_OBJECT*) Device->Driver;;
529
530 memset(&props, sizeof(VMSTORAGE_CHANNEL_PROPERTIES), 0);
531
532 // Open the channel
533 ret = Device->Driver->VmbusChannelInterface.Open(Device,
534 storDriver->RingBufferSize,
535 storDriver->RingBufferSize,
536 (void *)&props,
537 sizeof(VMSTORAGE_CHANNEL_PROPERTIES),
538 StorVscOnChannelCallback,
539 Device
540 );
541
542 DPRINT_DBG(STORVSC, "storage props: path id %d, tgt id %d, max xfer %d", props.PathId, props.TargetId, props.MaxTransferBytes);
543
544 if (ret != 0)
545 {
546 DPRINT_ERR(STORVSC, "unable to open channel: %d", ret);
547 return -1;
548 }
549
550 ret = StorVscChannelInit(Device);
551
552 return ret;
553 }
554
555
556 /*++
557
558 Name:
559 StorVscOnDeviceRemove()
560
561 Description:
562 Callback when the our device is being removed
563
564 --*/
565 int
566 StorVscOnDeviceRemove(
567 DEVICE_OBJECT *Device
568 )
569 {
570 STORVSC_DEVICE *storDevice;
571 int ret=0;
572
573 DPRINT_ENTER(STORVSC);
574
575 DPRINT_INFO(STORVSC, "disabling storage device (%p)...", Device->Extension);
576
577 storDevice = ReleaseStorDevice(Device);
578
579 // At this point, all outbound traffic should be disable. We only allow inbound traffic (responses) to proceed
580 // so that outstanding requests can be completed.
581 while (storDevice->NumOutstandingRequests)
582 {
583 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...", storDevice->NumOutstandingRequests);
584
585 Sleep(100);
586 }
587
588 DPRINT_INFO(STORVSC, "removing storage device (%p)...", Device->Extension);
589
590 storDevice = FinalReleaseStorDevice(Device);
591
592 DPRINT_INFO(STORVSC, "storage device (%p) safe to remove", storDevice);
593
594 // Close the channel
595 Device->Driver->VmbusChannelInterface.Close(Device);
596
597 FreeStorDevice(storDevice);
598
599 DPRINT_EXIT(STORVSC);
600 return ret;
601 }
602
603
604 //static void
605 //StorVscOnTargetRescan(
606 // void *Context
607 // )
608 //{
609 // DEVICE_OBJECT *device=(DEVICE_OBJECT*)Context;
610 // STORVSC_DRIVER_OBJECT *storDriver;
611 //
612 // DPRINT_ENTER(STORVSC);
613 //
614 // storDriver = (STORVSC_DRIVER_OBJECT*) device->Driver;
615 // storDriver->OnHostRescan(device);
616 //
617 // DPRINT_EXIT(STORVSC);
618 //}
619
620 int
621 StorVscOnHostReset(
622 DEVICE_OBJECT *Device
623 )
624 {
625 int ret=0;
626
627 STORVSC_DEVICE *storDevice;
628 STORVSC_REQUEST_EXTENSION *request;
629 VSTOR_PACKET *vstorPacket;
630
631 DPRINT_ENTER(STORVSC);
632
633 DPRINT_INFO(STORVSC, "resetting host adapter...");
634
635 storDevice = GetStorDevice(Device);
636 if (!storDevice)
637 {
638 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
639 DPRINT_EXIT(STORVSC);
640 return -1;
641 }
642
643 request = &storDevice->ResetRequest;
644 vstorPacket = &request->VStorPacket;
645
646 request->WaitEvent = WaitEventCreate();
647
648 vstorPacket->Operation = VStorOperationResetBus;
649 vstorPacket->Flags = REQUEST_COMPLETION_FLAG;
650 vstorPacket->VmSrb.PathId = storDevice->PathId;
651
652 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
653 vstorPacket,
654 sizeof(VSTOR_PACKET),
655 (unsigned long)&storDevice->ResetRequest,
656 VmbusPacketTypeDataInBand,
657 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
658 if (ret != 0)
659 {
660 DPRINT_ERR(STORVSC, "Unable to send reset packet %p ret %d", vstorPacket, ret);
661 goto Cleanup;
662 }
663
664 // FIXME: Add a timeout
665 WaitEventWait(request->WaitEvent);
666
667 WaitEventClose(request->WaitEvent);
668 DPRINT_INFO(STORVSC, "host adapter reset completed");
669
670 // At this point, all outstanding requests in the adapter should have been flushed out and return to us
671
672 Cleanup:
673 PutStorDevice(Device);
674 DPRINT_EXIT(STORVSC);
675 return ret;
676 }
677
678 /*++
679
680 Name:
681 StorVscOnIORequest()
682
683 Description:
684 Callback to initiate an I/O request
685
686 --*/
687 int
688 StorVscOnIORequest(
689 DEVICE_OBJECT *Device,
690 STORVSC_REQUEST *Request
691 )
692 {
693 STORVSC_DEVICE *storDevice;
694 STORVSC_REQUEST_EXTENSION* requestExtension = (STORVSC_REQUEST_EXTENSION*) Request->Extension;
695 VSTOR_PACKET* vstorPacket =&requestExtension->VStorPacket;
696 int ret=0;
697
698 DPRINT_ENTER(STORVSC);
699
700 storDevice = GetStorDevice(Device);
701
702 DPRINT_DBG(STORVSC, "enter - Device %p, DeviceExt %p, Request %p, Extension %p",
703 Device, storDevice, Request, requestExtension);
704
705 DPRINT_DBG(STORVSC, "req %p len %d bus %d, target %d, lun %d cdblen %d",
706 Request, Request->DataBuffer.Length, Request->Bus, Request->TargetId, Request->LunId, Request->CdbLen);
707
708 if (!storDevice)
709 {
710 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
711 DPRINT_EXIT(STORVSC);
712 return -2;
713 }
714
715 //PrintBytes(Request->Cdb, Request->CdbLen);
716
717 requestExtension->Request = Request;
718 requestExtension->Device = Device;
719
720 memset(vstorPacket, 0 , sizeof(VSTOR_PACKET));
721
722 vstorPacket->Flags |= REQUEST_COMPLETION_FLAG;
723
724 vstorPacket->VmSrb.Length = sizeof(VMSCSI_REQUEST);
725
726 vstorPacket->VmSrb.PortNumber = Request->Host;
727 vstorPacket->VmSrb.PathId = Request->Bus;
728 vstorPacket->VmSrb.TargetId = Request->TargetId;
729 vstorPacket->VmSrb.Lun = Request->LunId;
730
731 vstorPacket->VmSrb.SenseInfoLength = SENSE_BUFFER_SIZE;
732
733 // Copy over the scsi command descriptor block
734 vstorPacket->VmSrb.CdbLength = Request->CdbLen;
735 memcpy(&vstorPacket->VmSrb.Cdb, Request->Cdb, Request->CdbLen);
736
737 vstorPacket->VmSrb.DataIn = Request->Type;
738 vstorPacket->VmSrb.DataTransferLength = Request->DataBuffer.Length;
739
740 vstorPacket->Operation = VStorOperationExecuteSRB;
741
742 DPRINT_DBG(STORVSC, "srb - len %d port %d, path %d, target %d, lun %d senselen %d cdblen %d",
743 vstorPacket->VmSrb.Length,
744 vstorPacket->VmSrb.PortNumber,
745 vstorPacket->VmSrb.PathId,
746 vstorPacket->VmSrb.TargetId,
747 vstorPacket->VmSrb.Lun,
748 vstorPacket->VmSrb.SenseInfoLength,
749 vstorPacket->VmSrb.CdbLength);
750
751 if (requestExtension->Request->DataBuffer.Length)
752 {
753 ret = Device->Driver->VmbusChannelInterface.SendPacketMultiPageBuffer(Device,
754 &requestExtension->Request->DataBuffer,
755 vstorPacket,
756 sizeof(VSTOR_PACKET),
757 (unsigned long)requestExtension);
758 }
759 else
760 {
761 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
762 vstorPacket,
763 sizeof(VSTOR_PACKET),
764 (unsigned long)requestExtension,
765 VmbusPacketTypeDataInBand,
766 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
767 }
768
769 if (ret != 0)
770 {
771 DPRINT_DBG(STORVSC, "Unable to send packet %p ret %d", vstorPacket, ret);
772 }
773
774 InterlockedIncrement(&storDevice->NumOutstandingRequests);
775
776 PutStorDevice(Device);
777
778 DPRINT_EXIT(STORVSC);
779 return ret;
780 }
781
782 /*++
783
784 Name:
785 StorVscOnCleanup()
786
787 Description:
788 Perform any cleanup when the driver is removed
789
790 --*/
791 void
792 StorVscOnCleanup(
793 DRIVER_OBJECT *Driver
794 )
795 {
796 DPRINT_ENTER(STORVSC);
797 DPRINT_EXIT(STORVSC);
798 }
799
800
801 static void
802 StorVscOnIOCompletion(
803 DEVICE_OBJECT *Device,
804 VSTOR_PACKET *VStorPacket,
805 STORVSC_REQUEST_EXTENSION *RequestExt
806 )
807 {
808 STORVSC_REQUEST *request;
809 STORVSC_DEVICE *storDevice;
810
811 DPRINT_ENTER(STORVSC);
812
813 storDevice = MustGetStorDevice(Device);
814 if (!storDevice)
815 {
816 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
817 DPRINT_EXIT(STORVSC);
818 return;
819 }
820
821 DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION - request extension %p completed bytes xfer %u",
822 RequestExt, VStorPacket->VmSrb.DataTransferLength);
823
824 ASSERT(RequestExt != NULL);
825 ASSERT(RequestExt->Request != NULL);
826
827 request = RequestExt->Request;
828
829 ASSERT(request->OnIOCompletion != NULL);
830
831 // Copy over the status...etc
832 request->Status = VStorPacket->VmSrb.ScsiStatus;
833
834 if (request->Status != 0 || VStorPacket->VmSrb.SrbStatus != 1)
835 {
836 DPRINT_WARN(STORVSC, "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
837 request->Cdb[0],
838 VStorPacket->VmSrb.ScsiStatus,
839 VStorPacket->VmSrb.SrbStatus);
840 }
841
842 if ((request->Status & 0xFF) == 0x02) // CHECK_CONDITION
843 {
844 if (VStorPacket->VmSrb.SrbStatus & 0x80) // autosense data available
845 {
846 DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data valid - len %d\n",
847 RequestExt, VStorPacket->VmSrb.SenseInfoLength);
848
849 ASSERT(VStorPacket->VmSrb.SenseInfoLength <= request->SenseBufferSize);
850 memcpy(request->SenseBuffer,
851 VStorPacket->VmSrb.SenseData,
852 VStorPacket->VmSrb.SenseInfoLength);
853
854 request->SenseBufferSize = VStorPacket->VmSrb.SenseInfoLength;
855 }
856 }
857
858 // TODO:
859 request->BytesXfer = VStorPacket->VmSrb.DataTransferLength;
860
861 request->OnIOCompletion(request);
862
863 InterlockedDecrement(&storDevice->NumOutstandingRequests);
864
865 PutStorDevice(Device);
866
867 DPRINT_EXIT(STORVSC);
868 }
869
870
871 static void
872 StorVscOnReceive(
873 DEVICE_OBJECT *Device,
874 VSTOR_PACKET *VStorPacket,
875 STORVSC_REQUEST_EXTENSION *RequestExt
876 )
877 {
878 switch(VStorPacket->Operation)
879 {
880 case VStorOperationCompleteIo:
881
882 DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION");
883 StorVscOnIOCompletion(Device, VStorPacket, RequestExt);
884 break;
885
886 //case ENUMERATE_DEVICE_OPERATION:
887
888 // DPRINT_INFO(STORVSC, "ENUMERATE_DEVICE_OPERATION");
889
890 // StorVscOnTargetRescan(Device);
891 // break;
892
893 case VStorOperationRemoveDevice:
894
895 DPRINT_INFO(STORVSC, "REMOVE_DEVICE_OPERATION");
896 // TODO:
897 break;
898
899 default:
900 DPRINT_INFO(STORVSC, "Unknown operation received - %d", VStorPacket->Operation);
901 break;
902 }
903 }
904
905 void
906 StorVscOnChannelCallback(
907 void * Context
908 )
909 {
910 int ret=0;
911 DEVICE_OBJECT *device = (DEVICE_OBJECT*)Context;
912 STORVSC_DEVICE *storDevice;
913 u32 bytesRecvd;
914 u64 requestId;
915 unsigned char packet[ALIGN_UP(sizeof(VSTOR_PACKET),8)];
916 STORVSC_REQUEST_EXTENSION *request;
917
918 DPRINT_ENTER(STORVSC);
919
920 ASSERT(device);
921
922 storDevice = MustGetStorDevice(device);
923 if (!storDevice)
924 {
925 DPRINT_ERR(STORVSC, "unable to get stor device...device being destroyed?");
926 DPRINT_EXIT(STORVSC);
927 return;
928 }
929
930 do
931 {
932 ret = device->Driver->VmbusChannelInterface.RecvPacket(device,
933 packet,
934 ALIGN_UP(sizeof(VSTOR_PACKET),8),
935 &bytesRecvd,
936 &requestId);
937 if (ret == 0 && bytesRecvd > 0)
938 {
939 DPRINT_DBG(STORVSC, "receive %d bytes - tid %llx", bytesRecvd, requestId);
940
941 //ASSERT(bytesRecvd == sizeof(VSTOR_PACKET));
942
943 request = (STORVSC_REQUEST_EXTENSION*)(unsigned long)requestId;
944 ASSERT(request);
945
946 //if (vstorPacket.Flags & SYNTHETIC_FLAG)
947 if ((request == &storDevice->InitRequest) || (request == &storDevice->ResetRequest))
948 {
949 //DPRINT_INFO(STORVSC, "reset completion - operation %u status %u", vstorPacket.Operation, vstorPacket.Status);
950
951 memcpy(&request->VStorPacket, packet, sizeof(VSTOR_PACKET));
952
953 WaitEventSet(request->WaitEvent);
954 }
955 else
956 {
957 StorVscOnReceive(device, (VSTOR_PACKET*)packet, request);
958 }
959 }
960 else
961 {
962 //DPRINT_DBG(STORVSC, "nothing else to read...");
963 break;
964 }
965 } while (1);
966
967 PutStorDevice(device);
968
969 DPRINT_EXIT(STORVSC);
970 return;
971 }