xref: /reactos/sdk/include/ddk/srbhelper.h (revision f0ffda34)
1 #ifndef _SRBHELPER_H_
2 #define _SRBHELPER_H_
3 
4 #if !defined(_NTSTORPORT_) && !defined(_NTSTORPORTP_) && !defined(_NTSRB_)
5 #include <scsi.h>
6 #include <srb.h>
7 #endif
8 
9 #if (NTDDI_VERSION >= NTDDI_WIN8)
10 
11 #if !defined(SRBHELPER_ASSERT)
12 #define SRBHELPER_ASSERT NT_ASSERT
13 #endif
14 
15 #if !defined(SRB_ALIGN_SIZEOF)
16 #define SRB_ALIGN_SIZEOF(x) (((ULONG_PTR)(sizeof(x) + sizeof(PVOID) - 1)) & ~(sizeof(PVOID) - 1))
17 #endif
18 
19 #if defined(_NTSTORPORT_) || defined(_NTSTORPORTP_)
20 #define SrbMoveMemory(Destination, Source, Length) StorPortMoveMemory(Destination, Source, Length)
21 #elif defined(_NTDDK_)
22 #define SrbMoveMemory(Destination, Source, Length) RtlMoveMemory(Destination, Source, Length)
23 #else
24 #define SrbMoveMemory(Destination, Source, Length) memmove(Destination, Source, Length)
25 #endif
26 
27 #if defined(_NTDDK_)
28 #define SrbCopyMemory(Destination, Source, Length) RtlCopyMemory(Destination, Source, Length)
29 #else
30 #define SrbCopyMemory(Destination, Source, Length) memcpy(Destination, Source, Length)
31 #endif
32 
33 #if defined(_NTDDK_)
34 #define SrbZeroMemory(Destination, Length) RtlZeroMemory(Destination, Length)
35 #else
36 #define SrbZeroMemory(Destination, Length) memset(Destination, 0, Length)
37 #endif
38 
39 #if defined(_NTDDK_)
40 #define SrbEqualMemory(Source1, Source2, Length) RtlEqualMemory(Source1, Source2, Length)
41 #else
42 #define SrbEqualMemory(Source1, Source2, Length) (memcmp(Source1, Source2, Length) == 0)
43 #endif
44 
45 FORCEINLINE
46 PSRBEX_DATA
SrbGetSrbExDataByIndex(_In_ PSTORAGE_REQUEST_BLOCK Srb,_In_ ULONG SrbExDataIndex)47 SrbGetSrbExDataByIndex(
48   _In_ PSTORAGE_REQUEST_BLOCK Srb,
49   _In_ ULONG SrbExDataIndex)
50 {
51   PSRBEX_DATA srbExData = NULL;
52 
53   if ((Srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK) &&
54       (SrbExDataIndex < Srb->NumSrbExData) && (Srb->SrbExDataOffset[SrbExDataIndex]) &&
55       (Srb->SrbExDataOffset[SrbExDataIndex] >= sizeof(STORAGE_REQUEST_BLOCK)) &&
56       (Srb->SrbExDataOffset[SrbExDataIndex] < Srb->SrbLength))
57   {
58     srbExData = (PSRBEX_DATA)((PUCHAR)Srb + Srb->SrbExDataOffset[SrbExDataIndex]);
59   }
60 
61   return srbExData;
62 }
63 
64 FORCEINLINE
65 PSRBEX_DATA
SrbGetSrbExDataByType(_In_ PSTORAGE_REQUEST_BLOCK Srb,_In_ SRBEXDATATYPE Type)66 SrbGetSrbExDataByType(
67   _In_ PSTORAGE_REQUEST_BLOCK Srb,
68   _In_ SRBEXDATATYPE Type)
69 {
70   if ((Srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK) && (Srb->NumSrbExData > 0))
71   {
72     PSRBEX_DATA srbExData = NULL;
73     UCHAR i = 0;
74 
75     for (i = 0; i < Srb->NumSrbExData; i++)
76     {
77       if (Srb->SrbExDataOffset[i] >= sizeof(STORAGE_REQUEST_BLOCK) &&
78           Srb->SrbExDataOffset[i] < Srb->SrbLength)
79       {
80         srbExData = (PSRBEX_DATA)((PUCHAR)Srb + Srb->SrbExDataOffset[i]);
81         if (srbExData->Type == Type)
82         {
83           return srbExData;
84         }
85       }
86     }
87   }
88 
89   return NULL;
90 }
91 
92 FORCEINLINE
93 PSRBEX_DATA
SrbGetPrimarySrbExData(_In_ PSTORAGE_REQUEST_BLOCK Srb)94 SrbGetPrimarySrbExData(
95   _In_ PSTORAGE_REQUEST_BLOCK Srb)
96 {
97   if (Srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
98   {
99     switch (Srb->SrbFunction)
100     {
101     case SRB_FUNCTION_POWER:
102       return SrbGetSrbExDataByType(Srb, SrbExDataTypePower);
103 
104     case SRB_FUNCTION_PNP:
105       return SrbGetSrbExDataByType(Srb, SrbExDataTypePnP);
106 
107     case SRB_FUNCTION_WMI:
108       return SrbGetSrbExDataByType(Srb, SrbExDataTypeWmi);
109 
110     case SRB_FUNCTION_EXECUTE_SCSI: {
111       PSRBEX_DATA srbExData = NULL;
112       UCHAR i = 0;
113 
114       for (i = 0; i < Srb->NumSrbExData; i++)
115       {
116         if (Srb->SrbExDataOffset[i] >= sizeof(STORAGE_REQUEST_BLOCK) &&
117             Srb->SrbExDataOffset[i] < Srb->SrbLength)
118         {
119           srbExData = (PSRBEX_DATA)((PUCHAR)Srb + Srb->SrbExDataOffset[i]);
120           if (srbExData->Type == SrbExDataTypeScsiCdb16 ||
121               srbExData->Type == SrbExDataTypeScsiCdb32 ||
122               srbExData->Type == SrbExDataTypeScsiCdbVar)
123           {
124             return srbExData;
125           }
126         }
127       }
128       return NULL;
129     }
130 
131     default:
132       return NULL;
133     }
134   }
135 
136   return NULL;
137 }
138 
SrbGetAddress(_In_ PSTORAGE_REQUEST_BLOCK Srb)139 FORCEINLINE PSTOR_ADDRESS SrbGetAddress(_In_ PSTORAGE_REQUEST_BLOCK Srb)
140 {
141   PSTOR_ADDRESS storAddr = NULL;
142 
143   if (Srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
144   {
145     SRBHELPER_ASSERT(Srb->AddressOffset);
146 
147     if (Srb->AddressOffset)
148     {
149       storAddr = (PSTOR_ADDRESS)((PUCHAR)Srb + Srb->AddressOffset);
150       SRBHELPER_ASSERT(storAddr->Type == STOR_ADDRESS_TYPE_BTL8);
151     }
152   }
153 
154   return storAddr;
155 }
156 
157 FORCEINLINE
158 BOOLEAN
SrbCopySrb(_In_ PVOID DestinationSrb,_In_ ULONG DestinationSrbLength,_In_ PVOID SourceSrb)159 SrbCopySrb(
160   _In_ PVOID DestinationSrb,
161   _In_ ULONG DestinationSrbLength,
162   _In_ PVOID SourceSrb)
163 {
164   PSTORAGE_REQUEST_BLOCK sourceSrb = (PSTORAGE_REQUEST_BLOCK)SourceSrb;
165   BOOLEAN status = FALSE;
166 
167   if (sourceSrb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
168   {
169     if (DestinationSrbLength >= sourceSrb->SrbLength)
170     {
171       SrbCopyMemory(DestinationSrb, SourceSrb, sourceSrb->SrbLength);
172       status = TRUE;
173     }
174   }
175   else
176   {
177     if (DestinationSrbLength >= SCSI_REQUEST_BLOCK_SIZE)
178     {
179       SrbCopyMemory(DestinationSrb, SourceSrb, SCSI_REQUEST_BLOCK_SIZE);
180       status = TRUE;
181     }
182   }
183 
184   return status;
185 }
186 
187 FORCEINLINE
188 VOID
SrbZeroSrb(_In_ PVOID Srb)189 SrbZeroSrb(
190   _In_ PVOID Srb)
191 {
192   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
193   UCHAR function = srb->Function;
194   USHORT length = srb->Length;
195 
196   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
197   {
198     ULONG srbLength = srb->SrbLength;
199 
200     SrbZeroMemory(Srb, srb->SrbLength);
201 
202     srb->SrbLength = srbLength;
203   }
204   else
205   {
206     SrbZeroMemory(Srb, sizeof(SCSI_REQUEST_BLOCK));
207   }
208 
209   srb->Function = function;
210   srb->Length = length;
211 }
212 
213 FORCEINLINE
214 ULONG
SrbGetSrbLength(_In_ PVOID Srb)215 SrbGetSrbLength(
216   _In_ PVOID Srb)
217 {
218   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
219 
220   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
221   {
222     return srb->SrbLength;
223   }
224   else
225   {
226     return sizeof(SCSI_REQUEST_BLOCK);
227   }
228 }
229 
230 FORCEINLINE
231 VOID
SrbSetSrbLength(_In_ PVOID Srb,_In_ ULONG Length)232 SrbSetSrbLength(
233   _In_ PVOID Srb,
234   _In_ ULONG Length)
235 {
236   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
237 
238   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
239   {
240     srb->SrbLength = Length;
241   }
242 }
243 
244 FORCEINLINE
245 ULONG
SrbGetDefaultSrbLengthFromFunction(_In_ ULONG SrbFunction)246 SrbGetDefaultSrbLengthFromFunction(
247   _In_ ULONG SrbFunction)
248 {
249   switch (SrbFunction)
250   {
251   case SRB_FUNCTION_PNP:
252     return SRB_ALIGN_SIZEOF(STORAGE_REQUEST_BLOCK) + SRB_ALIGN_SIZEOF(STOR_ADDR_BTL8) +
253            SRB_ALIGN_SIZEOF(SRBEX_DATA_PNP);
254   case SRB_FUNCTION_POWER:
255     return SRB_ALIGN_SIZEOF(STORAGE_REQUEST_BLOCK) + SRB_ALIGN_SIZEOF(STOR_ADDR_BTL8) +
256            SRB_ALIGN_SIZEOF(SRBEX_DATA_POWER);
257   case SRB_FUNCTION_WMI:
258     return SRB_ALIGN_SIZEOF(STORAGE_REQUEST_BLOCK) + SRB_ALIGN_SIZEOF(STOR_ADDR_BTL8) +
259            SRB_ALIGN_SIZEOF(SRBEX_DATA_WMI);
260   case SRB_FUNCTION_EXECUTE_SCSI:
261     return SRB_ALIGN_SIZEOF(STORAGE_REQUEST_BLOCK) + SRB_ALIGN_SIZEOF(STOR_ADDR_BTL8) +
262            SRB_ALIGN_SIZEOF(SRBEX_DATA_SCSI_CDB16);
263   case SRB_FUNCTION_IO_CONTROL:
264     return SRB_ALIGN_SIZEOF(STORAGE_REQUEST_BLOCK) + SRB_ALIGN_SIZEOF(STOR_ADDR_BTL8);
265   default:
266     return SRB_ALIGN_SIZEOF(STORAGE_REQUEST_BLOCK) + SRB_ALIGN_SIZEOF(STOR_ADDR_BTL8);
267   }
268 }
269 
270 FORCEINLINE
271 PCDB
SrbGetScsiData(_In_ PSTORAGE_REQUEST_BLOCK SrbEx,_In_opt_ PUCHAR CdbLength8,_In_opt_ PULONG CdbLength32,_In_opt_ PUCHAR ScsiStatus,_In_opt_ PVOID * SenseInfoBuffer,_In_opt_ PUCHAR SenseInfoBufferLength)272 SrbGetScsiData(
273   _In_ PSTORAGE_REQUEST_BLOCK SrbEx,
274   _In_opt_ PUCHAR CdbLength8,
275   _In_opt_ PULONG CdbLength32,
276   _In_opt_ PUCHAR ScsiStatus,
277   _In_opt_ PVOID *SenseInfoBuffer,
278   _In_opt_ PUCHAR SenseInfoBufferLength)
279 {
280   PCDB Cdb = NULL;
281   ULONG i;
282   PSRBEX_DATA SrbExData = NULL;
283   BOOLEAN FoundEntry = FALSE;
284 
285   if ((SrbEx->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK) &&
286       (SrbEx->SrbFunction == SRB_FUNCTION_EXECUTE_SCSI))
287   {
288     SRBHELPER_ASSERT(SrbEx->NumSrbExData > 0);
289 
290     for (i = 0; i < SrbEx->NumSrbExData; i++)
291     {
292       if ((SrbEx->SrbExDataOffset[i] < sizeof(STORAGE_REQUEST_BLOCK)) ||
293           (SrbEx->SrbExDataOffset[i] >= SrbEx->SrbLength))
294       {
295         SRBHELPER_ASSERT(FALSE);
296         continue;
297       }
298 
299       SrbExData = (PSRBEX_DATA)((PUCHAR)SrbEx + SrbEx->SrbExDataOffset[i]);
300 
301       switch (SrbExData->Type)
302       {
303           case SrbExDataTypeScsiCdb16:
304             if (SrbEx->SrbExDataOffset[i] + sizeof(SRBEX_DATA_SCSI_CDB16) <= SrbEx->SrbLength)
305             {
306               FoundEntry = TRUE;
307               if (CdbLength8)
308               {
309                 *CdbLength8 = ((PSRBEX_DATA_SCSI_CDB16)SrbExData)->CdbLength;
310               }
311 
312               if (((PSRBEX_DATA_SCSI_CDB16)SrbExData)->CdbLength > 0)
313               {
314                 Cdb = (PCDB)((PSRBEX_DATA_SCSI_CDB16)SrbExData)->Cdb;
315               }
316 
317               if (ScsiStatus)
318               {
319                 *ScsiStatus = ((PSRBEX_DATA_SCSI_CDB16)SrbExData)->ScsiStatus;
320               }
321 
322               if (SenseInfoBuffer)
323               {
324                 *SenseInfoBuffer = ((PSRBEX_DATA_SCSI_CDB16)SrbExData)->SenseInfoBuffer;
325               }
326 
327               if (SenseInfoBufferLength)
328               {
329                 *SenseInfoBufferLength = ((PSRBEX_DATA_SCSI_CDB16)SrbExData)->SenseInfoBufferLength;
330               }
331             }
332             else
333             {
334               // Catch invalid offset
335               SRBHELPER_ASSERT(FALSE);
336             }
337             break;
338 
339           case SrbExDataTypeScsiCdb32:
340             if (SrbEx->SrbExDataOffset[i] + sizeof(SRBEX_DATA_SCSI_CDB32) <= SrbEx->SrbLength)
341             {
342               FoundEntry = TRUE;
343               if (CdbLength8)
344               {
345                 *CdbLength8 = ((PSRBEX_DATA_SCSI_CDB32)SrbExData)->CdbLength;
346               }
347 
348               if (((PSRBEX_DATA_SCSI_CDB32)SrbExData)->CdbLength > 0)
349               {
350                 Cdb = (PCDB)((PSRBEX_DATA_SCSI_CDB32)SrbExData)->Cdb;
351               }
352 
353               if (ScsiStatus)
354               {
355                 *ScsiStatus = ((PSRBEX_DATA_SCSI_CDB32)SrbExData)->ScsiStatus;
356               }
357 
358               if (SenseInfoBuffer)
359               {
360                 *SenseInfoBuffer = ((PSRBEX_DATA_SCSI_CDB32)SrbExData)->SenseInfoBuffer;
361               }
362 
363               if (SenseInfoBufferLength)
364               {
365                 *SenseInfoBufferLength = ((PSRBEX_DATA_SCSI_CDB32)SrbExData)->SenseInfoBufferLength;
366               }
367             }
368             else
369             {
370               // Catch invalid offset
371               SRBHELPER_ASSERT(FALSE);
372             }
373             break;
374 
375           case SrbExDataTypeScsiCdbVar:
376             if (SrbEx->SrbExDataOffset[i] + sizeof(SRBEX_DATA_SCSI_CDB_VAR) <= SrbEx->SrbLength)
377             {
378               FoundEntry = TRUE;
379               if (CdbLength32)
380               {
381                 *CdbLength32 = ((PSRBEX_DATA_SCSI_CDB_VAR)SrbExData)->CdbLength;
382               }
383 
384               if (((PSRBEX_DATA_SCSI_CDB_VAR)SrbExData)->CdbLength > 0)
385               {
386                 Cdb = (PCDB)((PSRBEX_DATA_SCSI_CDB_VAR)SrbExData)->Cdb;
387               }
388 
389               if (ScsiStatus)
390               {
391                 *ScsiStatus = ((PSRBEX_DATA_SCSI_CDB_VAR)SrbExData)->ScsiStatus;
392               }
393 
394               if (SenseInfoBuffer)
395               {
396                 *SenseInfoBuffer = ((PSRBEX_DATA_SCSI_CDB_VAR)SrbExData)->SenseInfoBuffer;
397               }
398 
399               if (SenseInfoBufferLength)
400               {
401                 *SenseInfoBufferLength = ((PSRBEX_DATA_SCSI_CDB_VAR)SrbExData)->SenseInfoBufferLength;
402               }
403             }
404             else
405             {
406               SRBHELPER_ASSERT(FALSE);
407             }
408             break;
409       }
410 
411       if (FoundEntry)
412       {
413         break;
414       }
415     }
416   }
417   else
418   {
419 
420     if (CdbLength8)
421     {
422       *CdbLength8 = 0;
423     }
424 
425     if (CdbLength32)
426     {
427       *CdbLength32 = 0;
428     }
429 
430     if (ScsiStatus)
431     {
432       *ScsiStatus = SCSISTAT_GOOD;
433     }
434 
435     if (SenseInfoBuffer)
436     {
437       *SenseInfoBuffer = NULL;
438     }
439 
440     if (SenseInfoBufferLength)
441     {
442       *SenseInfoBufferLength = 0;
443     }
444   }
445 
446   return Cdb;
447 }
448 
449 FORCEINLINE
450 VOID
SrbSetScsiData(_In_ PSTORAGE_REQUEST_BLOCK SrbEx,_In_opt_ PUCHAR CdbLength8,_In_opt_ PULONG CdbLength32,_In_opt_ PUCHAR ScsiStatus,_In_opt_ PVOID * SenseInfoBuffer,_In_opt_ PUCHAR SenseInfoBufferLength)451 SrbSetScsiData(
452   _In_ PSTORAGE_REQUEST_BLOCK SrbEx,
453   _In_opt_ PUCHAR CdbLength8,
454   _In_opt_ PULONG CdbLength32,
455   _In_opt_ PUCHAR ScsiStatus,
456   _In_opt_ PVOID *SenseInfoBuffer,
457   _In_opt_ PUCHAR SenseInfoBufferLength)
458 {
459   ULONG i;
460   PSRBEX_DATA SrbExData = NULL;
461   BOOLEAN FoundEntry = FALSE;
462 
463   if ((SrbEx->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK) &&
464       (SrbEx->SrbFunction == SRB_FUNCTION_EXECUTE_SCSI))
465   {
466     SRBHELPER_ASSERT(SrbEx->NumSrbExData > 0);
467 
468     for (i = 0; i < SrbEx->NumSrbExData; i++)
469     {
470       if ((SrbEx->SrbExDataOffset[i] < sizeof(STORAGE_REQUEST_BLOCK)) ||
471           (SrbEx->SrbExDataOffset[i] >= SrbEx->SrbLength))
472       {
473         SRBHELPER_ASSERT(FALSE);
474         continue;
475       }
476 
477       SrbExData = (PSRBEX_DATA)((PUCHAR)SrbEx + SrbEx->SrbExDataOffset[i]);
478 
479       switch (SrbExData->Type)
480       {
481           case SrbExDataTypeScsiCdb16:
482             if (SrbEx->SrbExDataOffset[i] + sizeof(SRBEX_DATA_SCSI_CDB16) <= SrbEx->SrbLength)
483             {
484               FoundEntry = TRUE;
485               if (CdbLength8)
486               {
487                 ((PSRBEX_DATA_SCSI_CDB16)SrbExData)->CdbLength = *CdbLength8;
488               }
489 
490               if (ScsiStatus)
491               {
492                 ((PSRBEX_DATA_SCSI_CDB16)SrbExData)->ScsiStatus = *ScsiStatus;
493               }
494 
495               if (SenseInfoBuffer)
496               {
497                 ((PSRBEX_DATA_SCSI_CDB16)SrbExData)->SenseInfoBuffer = *SenseInfoBuffer;
498               }
499 
500               if (SenseInfoBufferLength)
501               {
502                 ((PSRBEX_DATA_SCSI_CDB16)SrbExData)->SenseInfoBufferLength = *SenseInfoBufferLength;
503               }
504             }
505             else
506             {
507               // Catch invalid offset
508               SRBHELPER_ASSERT(FALSE);
509             }
510             break;
511 
512           case SrbExDataTypeScsiCdb32:
513             if (SrbEx->SrbExDataOffset[i] + sizeof(SRBEX_DATA_SCSI_CDB32) <= SrbEx->SrbLength)
514             {
515               FoundEntry = TRUE;
516               if (CdbLength8)
517               {
518                 ((PSRBEX_DATA_SCSI_CDB32)SrbExData)->CdbLength = *CdbLength8;
519               }
520 
521               if (ScsiStatus)
522               {
523                 ((PSRBEX_DATA_SCSI_CDB32)SrbExData)->ScsiStatus = *ScsiStatus;
524               }
525 
526               if (SenseInfoBuffer)
527               {
528                 ((PSRBEX_DATA_SCSI_CDB32)SrbExData)->SenseInfoBuffer = *SenseInfoBuffer;
529               }
530 
531               if (SenseInfoBufferLength)
532               {
533                 ((PSRBEX_DATA_SCSI_CDB32)SrbExData)->SenseInfoBufferLength = *SenseInfoBufferLength;
534               }
535             }
536             else
537             {
538               SRBHELPER_ASSERT(FALSE);
539             }
540             break;
541 
542           case SrbExDataTypeScsiCdbVar:
543             if (SrbEx->SrbExDataOffset[i] + sizeof(SRBEX_DATA_SCSI_CDB_VAR) <= SrbEx->SrbLength)
544             {
545               FoundEntry = TRUE;
546               if (CdbLength32)
547               {
548                 ((PSRBEX_DATA_SCSI_CDB_VAR)SrbExData)->CdbLength = *CdbLength32;
549               }
550 
551               if (ScsiStatus)
552               {
553                 ((PSRBEX_DATA_SCSI_CDB_VAR)SrbExData)->ScsiStatus = *ScsiStatus;
554               }
555 
556               if (SenseInfoBuffer)
557               {
558                 ((PSRBEX_DATA_SCSI_CDB_VAR)SrbExData)->SenseInfoBuffer = *SenseInfoBuffer;
559               }
560 
561               if (SenseInfoBufferLength)
562               {
563                 ((PSRBEX_DATA_SCSI_CDB_VAR)SrbExData)->SenseInfoBufferLength = *SenseInfoBufferLength;
564               }
565             }
566             else
567             {
568               SRBHELPER_ASSERT(FALSE);
569             }
570             break;
571       }
572 
573       if (FoundEntry)
574       {
575         break;
576       }
577     }
578   }
579 }
580 
581 FORCEINLINE
582 PCDB
SrbGetCdb(_In_ PVOID Srb)583 SrbGetCdb(
584   _In_ PVOID Srb)
585 {
586   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
587   PCDB pCdb = NULL;
588 
589   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
590   {
591     return SrbGetScsiData(srb, NULL, NULL, NULL, NULL, NULL);
592   }
593   else
594   {
595     pCdb = (PCDB)((PSCSI_REQUEST_BLOCK)srb)->Cdb;
596   }
597   return pCdb;
598 }
599 
600 FORCEINLINE
601 ULONG
SrbGetSrbFunction(_In_ PVOID Srb)602 SrbGetSrbFunction(
603   _In_ PVOID Srb)
604 {
605   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
606 
607   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
608   {
609     return srb->SrbFunction;
610   }
611   else
612   {
613     return (ULONG)((PSCSI_REQUEST_BLOCK)srb)->Function;
614   }
615 }
616 
617 FORCEINLINE
618 PVOID
SrbGetSenseInfoBuffer(_In_ PVOID Srb)619 SrbGetSenseInfoBuffer(
620   _In_ PVOID Srb)
621 {
622   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
623   PVOID pSenseInfoBuffer = NULL;
624 
625   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
626   {
627     SrbGetScsiData(srb, NULL, NULL, NULL, &pSenseInfoBuffer, NULL);
628   }
629   else
630   {
631     pSenseInfoBuffer = ((PSCSI_REQUEST_BLOCK)srb)->SenseInfoBuffer;
632   }
633   return pSenseInfoBuffer;
634 }
635 
636 FORCEINLINE
637 UCHAR
SrbGetSenseInfoBufferLength(_In_ PVOID Srb)638 SrbGetSenseInfoBufferLength(
639   _In_ PVOID Srb)
640 {
641   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
642   UCHAR SenseInfoBufferLength = 0;
643 
644   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
645   {
646     SrbGetScsiData(srb, NULL, NULL, NULL, NULL, &SenseInfoBufferLength);
647   }
648   else
649   {
650     SenseInfoBufferLength = ((PSCSI_REQUEST_BLOCK)srb)->SenseInfoBufferLength;
651   }
652   return SenseInfoBufferLength;
653 }
654 
655 FORCEINLINE
656 VOID
SrbSetSenseInfoBuffer(_In_ PVOID Srb,_In_opt_ PVOID SenseInfoBuffer)657 SrbSetSenseInfoBuffer(
658   _In_ PVOID Srb,
659   _In_opt_ PVOID SenseInfoBuffer)
660 {
661   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
662 
663   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
664   {
665     SrbSetScsiData(srb, NULL, NULL, NULL, &SenseInfoBuffer, NULL);
666   }
667   else
668   {
669     ((PSCSI_REQUEST_BLOCK)srb)->SenseInfoBuffer = SenseInfoBuffer;
670   }
671 }
672 
673 FORCEINLINE
674 VOID
SrbSetSenseInfoBufferLength(_In_ PVOID Srb,_In_ UCHAR SenseInfoBufferLength)675 SrbSetSenseInfoBufferLength(
676   _In_ PVOID Srb,
677   _In_ UCHAR SenseInfoBufferLength)
678 {
679   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
680 
681   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
682   {
683     SrbSetScsiData(srb, NULL, NULL, NULL, NULL, &SenseInfoBufferLength);
684   }
685   else
686   {
687     ((PSCSI_REQUEST_BLOCK)srb)->SenseInfoBufferLength = SenseInfoBufferLength;
688   }
689 }
690 
691 FORCEINLINE
692 PVOID
SrbGetOriginalRequest(_In_ PVOID Srb)693 SrbGetOriginalRequest(
694   _In_ PVOID Srb)
695 {
696   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
697 
698   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
699   {
700     return srb->OriginalRequest;
701   }
702   else
703   {
704     return ((PSCSI_REQUEST_BLOCK)srb)->OriginalRequest;
705   }
706 }
707 
708 FORCEINLINE
709 VOID
SrbSetOriginalRequest(_In_ PVOID Srb,_In_opt_ PVOID OriginalRequest)710 SrbSetOriginalRequest(
711   _In_ PVOID Srb,
712   _In_opt_ PVOID OriginalRequest)
713 {
714   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
715 
716   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
717   {
718     srb->OriginalRequest = OriginalRequest;
719   }
720   else
721   {
722     ((PSCSI_REQUEST_BLOCK)srb)->OriginalRequest = OriginalRequest;
723   }
724 }
725 
726 FORCEINLINE
727 PVOID
SrbGetDataBuffer(_In_ PVOID Srb)728 SrbGetDataBuffer(
729   _In_ PVOID Srb)
730 {
731   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
732   PVOID DataBuffer;
733 
734   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
735   {
736     DataBuffer = srb->DataBuffer;
737   }
738   else
739   {
740     DataBuffer = ((PSCSI_REQUEST_BLOCK)srb)->DataBuffer;
741   }
742   return DataBuffer;
743 }
744 
745 FORCEINLINE
746 VOID
SrbSetDataBuffer(_In_ PVOID Srb,_In_opt_ __drv_aliasesMem PVOID DataBuffer)747 SrbSetDataBuffer(
748   _In_ PVOID Srb,
749   _In_opt_ __drv_aliasesMem PVOID DataBuffer)
750 {
751   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
752 
753   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
754   {
755     srb->DataBuffer = DataBuffer;
756   }
757   else
758   {
759     ((PSCSI_REQUEST_BLOCK)srb)->DataBuffer = DataBuffer;
760   }
761 }
762 
763 FORCEINLINE
764 ULONG
SrbGetDataTransferLength(_In_ PVOID Srb)765 SrbGetDataTransferLength(
766   _In_ PVOID Srb)
767 {
768   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
769   ULONG DataTransferLength;
770 
771   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
772   {
773     DataTransferLength = srb->DataTransferLength;
774   }
775   else
776   {
777     DataTransferLength = ((PSCSI_REQUEST_BLOCK)srb)->DataTransferLength;
778   }
779   return DataTransferLength;
780 }
781 
782 FORCEINLINE
783 VOID
SrbSetDataTransferLength(_In_ PVOID Srb,_In_ ULONG DataTransferLength)784 SrbSetDataTransferLength(
785   _In_ PVOID Srb,
786   _In_ ULONG DataTransferLength)
787 {
788   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
789 
790   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
791   {
792     srb->DataTransferLength = DataTransferLength;
793   }
794   else
795   {
796     ((PSCSI_REQUEST_BLOCK)srb)->DataTransferLength = DataTransferLength;
797   }
798 }
799 
800 FORCEINLINE
801 ULONG
SrbGetTimeOutValue(_In_ PVOID Srb)802 SrbGetTimeOutValue(
803   _In_ PVOID Srb)
804 {
805   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
806   ULONG timeOutValue;
807 
808   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
809   {
810     timeOutValue = srb->TimeOutValue;
811   }
812   else
813   {
814     timeOutValue = ((PSCSI_REQUEST_BLOCK)srb)->TimeOutValue;
815   }
816   return timeOutValue;
817 }
818 
819 FORCEINLINE
820 VOID
SrbSetTimeOutValue(_In_ PVOID Srb,_In_ ULONG TimeOutValue)821 SrbSetTimeOutValue(
822   _In_ PVOID Srb,
823   _In_ ULONG TimeOutValue)
824 {
825   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
826 
827   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
828   {
829     srb->TimeOutValue = TimeOutValue;
830   }
831   else
832   {
833     ((PSCSI_REQUEST_BLOCK)srb)->TimeOutValue = TimeOutValue;
834   }
835 }
836 
837 FORCEINLINE
838 VOID
SrbSetQueueSortKey(_In_ PVOID Srb,_In_ ULONG QueueSortKey)839 SrbSetQueueSortKey(
840   _In_ PVOID Srb,
841   _In_ ULONG QueueSortKey)
842 {
843   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
844 
845   if (srb->Function != SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
846   {
847     ((PSCSI_REQUEST_BLOCK)srb)->QueueSortKey = QueueSortKey;
848   }
849 }
850 
851 FORCEINLINE
852 VOID
SrbSetQueueTag(_In_ PVOID Srb,_In_ ULONG QueueTag)853 SrbSetQueueTag(
854   _In_ PVOID Srb,
855   _In_ ULONG QueueTag)
856 {
857   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
858 
859   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
860   {
861     srb->RequestTag = QueueTag;
862   }
863   else
864   {
865     ((PSCSI_REQUEST_BLOCK)srb)->QueueTag = (UCHAR)QueueTag;
866   }
867 }
868 
869 #define SrbSetRequestTag SrbSetQueueTag
870 
871 FORCEINLINE
872 ULONG
SrbGetQueueTag(_In_ PVOID Srb)873 SrbGetQueueTag(
874   _In_ PVOID Srb)
875 {
876   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
877 
878   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
879   {
880     return srb->RequestTag;
881   }
882   else
883   {
884     return ((PSCSI_REQUEST_BLOCK)srb)->QueueTag;
885   }
886 }
887 
888 #define SrbGetRequestTag SrbGetQueueTag
889 
890 FORCEINLINE
891 PVOID
SrbGetNextSrb(_In_ PVOID Srb)892 SrbGetNextSrb(
893   _In_ PVOID Srb)
894 {
895   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
896 
897   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
898   {
899     return (PVOID)srb->NextSrb;
900   }
901   else
902   {
903     return (PVOID)((PSCSI_REQUEST_BLOCK)srb)->NextSrb;
904   }
905 }
906 
907 FORCEINLINE
908 VOID
SrbSetNextSrb(_In_ PVOID Srb,_In_opt_ PVOID NextSrb)909 SrbSetNextSrb(
910   _In_ PVOID Srb,
911   _In_opt_ PVOID NextSrb)
912 {
913   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
914 
915   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
916   {
917     srb->NextSrb = (PSTORAGE_REQUEST_BLOCK)NextSrb;
918   }
919   else
920   {
921     ((PSCSI_REQUEST_BLOCK)srb)->NextSrb = (PSCSI_REQUEST_BLOCK)NextSrb;
922   }
923 }
924 
925 FORCEINLINE
926 ULONG
SrbGetSrbFlags(_In_ PVOID Srb)927 SrbGetSrbFlags(
928   _In_ PVOID Srb)
929 {
930   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
931   ULONG srbFlags;
932 
933   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
934   {
935     srbFlags = srb->SrbFlags;
936   }
937   else
938   {
939     srbFlags = ((PSCSI_REQUEST_BLOCK)srb)->SrbFlags;
940   }
941   return srbFlags;
942 }
943 
944 FORCEINLINE
945 VOID
SrbAssignSrbFlags(_In_ PVOID Srb,_In_ ULONG Flags)946 SrbAssignSrbFlags(
947   _In_ PVOID Srb,
948   _In_ ULONG Flags)
949 {
950   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
951 
952   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
953   {
954     srb->SrbFlags = Flags;
955   }
956   else
957   {
958     ((PSCSI_REQUEST_BLOCK)srb)->SrbFlags = Flags;
959   }
960 }
961 
962 FORCEINLINE
963 VOID
SrbSetSrbFlags(_In_ PVOID Srb,_In_ ULONG Flags)964 SrbSetSrbFlags(
965   _In_ PVOID Srb,
966   _In_ ULONG Flags)
967 {
968   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
969 
970   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
971   {
972     srb->SrbFlags |= Flags;
973   }
974   else
975   {
976     ((PSCSI_REQUEST_BLOCK)srb)->SrbFlags |= Flags;
977   }
978 }
979 
980 FORCEINLINE
981 VOID
SrbClearSrbFlags(_In_ PVOID Srb,_In_ ULONG Flags)982 SrbClearSrbFlags(
983   _In_ PVOID Srb,
984   _In_ ULONG Flags)
985 {
986   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
987 
988   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
989   {
990     srb->SrbFlags &= ~Flags;
991   }
992   else
993   {
994     ((PSCSI_REQUEST_BLOCK)srb)->SrbFlags &= ~Flags;
995   }
996 }
997 
998 FORCEINLINE
999 ULONG
SrbGetSystemStatus(_In_ PVOID Srb)1000 SrbGetSystemStatus(
1001   _In_ PVOID Srb)
1002 {
1003   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
1004   ULONG systemStatus;
1005 
1006   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
1007   {
1008     systemStatus = srb->SystemStatus;
1009   }
1010   else
1011   {
1012     systemStatus = ((PSCSI_REQUEST_BLOCK)srb)->InternalStatus;
1013   }
1014   return systemStatus;
1015 }
1016 
1017 FORCEINLINE
1018 VOID
SrbSetSystemStatus(_In_ PVOID Srb,_In_ ULONG Status)1019 SrbSetSystemStatus(
1020   _In_ PVOID Srb,
1021   _In_ ULONG Status)
1022 {
1023   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
1024 
1025   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
1026   {
1027     srb->SystemStatus = Status;
1028   }
1029   else
1030   {
1031     ((PSCSI_REQUEST_BLOCK)srb)->InternalStatus = Status;
1032   }
1033 }
1034 
1035 FORCEINLINE
1036 UCHAR
SrbGetScsiStatus(_In_ PVOID Srb)1037 SrbGetScsiStatus(
1038   _In_ PVOID Srb)
1039 {
1040   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
1041   UCHAR scsiStatus = 0;
1042 
1043   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
1044   {
1045     SrbGetScsiData(srb, NULL, NULL, &scsiStatus, NULL, NULL);
1046   }
1047   else
1048   {
1049     scsiStatus = ((PSCSI_REQUEST_BLOCK)srb)->ScsiStatus;
1050   }
1051   return scsiStatus;
1052 }
1053 
1054 FORCEINLINE
1055 VOID
SrbSetScsiStatus(_In_ PVOID Srb,_In_ UCHAR ScsiStatus)1056 SrbSetScsiStatus(
1057   _In_ PVOID Srb,
1058   _In_ UCHAR ScsiStatus)
1059 {
1060   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
1061 
1062   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
1063   {
1064     SrbSetScsiData(srb, NULL, NULL, &ScsiStatus, NULL, NULL);
1065   }
1066   else
1067   {
1068     ((PSCSI_REQUEST_BLOCK)srb)->ScsiStatus = ScsiStatus;
1069   }
1070 }
1071 
1072 FORCEINLINE
1073 UCHAR
SrbGetCdbLength(_In_ PVOID Srb)1074 SrbGetCdbLength(
1075   _In_ PVOID Srb)
1076 {
1077   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
1078   UCHAR CdbLength = 0;
1079 
1080   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
1081   {
1082     SrbGetScsiData(srb, &CdbLength, NULL, NULL, NULL, NULL);
1083   }
1084   else
1085   {
1086     CdbLength = ((PSCSI_REQUEST_BLOCK)srb)->CdbLength;
1087   }
1088   return CdbLength;
1089 }
1090 
1091 FORCEINLINE
1092 VOID
SrbSetCdbLength(_In_ PVOID Srb,_In_ UCHAR CdbLength)1093 SrbSetCdbLength(
1094   _In_ PVOID Srb,
1095   _In_ UCHAR CdbLength)
1096 {
1097   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
1098 
1099   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
1100   {
1101     SrbSetScsiData(srb, &CdbLength, NULL, NULL, NULL, NULL);
1102   }
1103   else
1104   {
1105     ((PSCSI_REQUEST_BLOCK)srb)->CdbLength = CdbLength;
1106   }
1107 }
1108 
1109 FORCEINLINE
1110 ULONG
SrbGetRequestAttribute(_In_ PVOID Srb)1111 SrbGetRequestAttribute(
1112   _In_ PVOID Srb)
1113 {
1114   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
1115   ULONG RequestAttribute;
1116   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
1117   {
1118     RequestAttribute = srb->RequestAttribute;
1119   }
1120   else
1121   {
1122     RequestAttribute = ((PSCSI_REQUEST_BLOCK)srb)->QueueAction;
1123   }
1124   return RequestAttribute;
1125 }
1126 
1127 #define SrbGetQueueAction SrbGetRequestAttribute
1128 
1129 FORCEINLINE
1130 VOID
SrbSetRequestAttribute(_In_ PVOID Srb,_In_ UCHAR RequestAttribute)1131 SrbSetRequestAttribute(
1132   _In_ PVOID Srb,
1133   _In_ UCHAR RequestAttribute)
1134 {
1135   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
1136 
1137   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
1138   {
1139     srb->RequestAttribute = RequestAttribute;
1140   }
1141   else
1142   {
1143     ((PSCSI_REQUEST_BLOCK)srb)->QueueAction = RequestAttribute;
1144   }
1145 }
1146 
1147 #define SrbSetQueueAction SrbSetRequestAttribute
1148 
1149 FORCEINLINE
1150 UCHAR
SrbGetPathId(_In_ PVOID Srb)1151 SrbGetPathId(
1152   _In_ PVOID Srb)
1153 {
1154   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
1155   UCHAR PathId = 0;
1156   PSTOR_ADDRESS storAddr = NULL;
1157 
1158   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
1159   {
1160     storAddr = (PSTOR_ADDRESS)SrbGetAddress(srb);
1161     if (storAddr)
1162     {
1163       switch (storAddr->Type)
1164       {
1165         case STOR_ADDRESS_TYPE_BTL8:
1166           PathId = ((PSTOR_ADDR_BTL8)storAddr)->Path;
1167           break;
1168 
1169         default:
1170           SRBHELPER_ASSERT(FALSE);
1171           break;
1172       }
1173     }
1174   }
1175   else
1176   {
1177     PathId = ((PSCSI_REQUEST_BLOCK)srb)->PathId;
1178   }
1179   return PathId;
1180 }
1181 
1182 FORCEINLINE
1183 UCHAR
SrbGetTargetId(_In_ PVOID Srb)1184 SrbGetTargetId(
1185   _In_ PVOID Srb)
1186 {
1187   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
1188   UCHAR TargetId = 0;
1189   PSTOR_ADDRESS storAddr = NULL;
1190 
1191   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
1192   {
1193     storAddr = (PSTOR_ADDRESS)SrbGetAddress(srb);
1194     if (storAddr)
1195     {
1196       switch (storAddr->Type)
1197       {
1198         case STOR_ADDRESS_TYPE_BTL8:
1199           TargetId = ((PSTOR_ADDR_BTL8)storAddr)->Target;
1200           break;
1201 
1202         default:
1203           SRBHELPER_ASSERT(FALSE);
1204           break;
1205       }
1206     }
1207   }
1208   else
1209   {
1210     TargetId = ((PSCSI_REQUEST_BLOCK)srb)->TargetId;
1211   }
1212   return TargetId;
1213 }
1214 
1215 FORCEINLINE
1216 UCHAR
SrbGetLun(_In_ PVOID Srb)1217 SrbGetLun(
1218   _In_ PVOID Srb)
1219 {
1220   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
1221   UCHAR Lun = 0;
1222   PSTOR_ADDRESS storAddr = NULL;
1223 
1224   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
1225   {
1226     storAddr = (PSTOR_ADDRESS)SrbGetAddress(srb);
1227     if (storAddr)
1228     {
1229       switch (storAddr->Type)
1230       {
1231         case STOR_ADDRESS_TYPE_BTL8:
1232           Lun = ((PSTOR_ADDR_BTL8)storAddr)->Lun;
1233           break;
1234 
1235         default:
1236           SRBHELPER_ASSERT(FALSE);
1237           break;
1238       }
1239     }
1240   }
1241   else
1242   {
1243     Lun = ((PSCSI_REQUEST_BLOCK)srb)->Lun;
1244   }
1245   return Lun;
1246 }
1247 
1248 FORCEINLINE
1249 VOID
SrbGetPathTargetLun(_In_ PVOID Srb,_In_opt_ PUCHAR PathId,_In_opt_ PUCHAR TargetId,_In_opt_ PUCHAR Lun)1250 SrbGetPathTargetLun(
1251   _In_ PVOID Srb,
1252   _In_opt_ PUCHAR PathId,
1253   _In_opt_ PUCHAR TargetId,
1254   _In_opt_ PUCHAR Lun)
1255 {
1256   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
1257   PSTOR_ADDRESS storAddr = NULL;
1258 
1259   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
1260   {
1261     storAddr = (PSTOR_ADDRESS)SrbGetAddress(srb);
1262     if (storAddr)
1263     {
1264       switch (storAddr->Type)
1265       {
1266         case STOR_ADDRESS_TYPE_BTL8:
1267           if (PathId != NULL)
1268           {
1269             *PathId = ((PSTOR_ADDR_BTL8)storAddr)->Path;
1270           }
1271 
1272           if (TargetId != NULL)
1273           {
1274             *TargetId = ((PSTOR_ADDR_BTL8)storAddr)->Target;
1275           }
1276 
1277           if (Lun != NULL)
1278           {
1279             *Lun = ((PSTOR_ADDR_BTL8)storAddr)->Lun;
1280           }
1281 
1282           break;
1283 
1284         default:
1285           SRBHELPER_ASSERT(FALSE);
1286           break;
1287       }
1288     }
1289   }
1290   else
1291   {
1292     if (PathId != NULL)
1293     {
1294       *PathId = ((PSCSI_REQUEST_BLOCK)srb)->PathId;
1295     }
1296 
1297     if (TargetId != NULL)
1298     {
1299       *TargetId = ((PSCSI_REQUEST_BLOCK)srb)->TargetId;
1300     }
1301 
1302     if (Lun != NULL)
1303     {
1304       *Lun = ((PSCSI_REQUEST_BLOCK)srb)->Lun;
1305     }
1306   }
1307 
1308   return;
1309 }
1310 
1311 FORCEINLINE
1312 PVOID
SrbGetMiniportContext(_In_ PVOID Srb)1313 SrbGetMiniportContext(
1314   _In_ PVOID Srb)
1315 {
1316   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
1317 
1318   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
1319   {
1320     return srb->MiniportContext;
1321   }
1322   else
1323   {
1324     return ((PSCSI_REQUEST_BLOCK)srb)->SrbExtension;
1325   }
1326 }
1327 
1328 FORCEINLINE
1329 UCHAR
SrbGetSrbStatus(_In_ PVOID Srb)1330 SrbGetSrbStatus(
1331   _In_ PVOID Srb)
1332 {
1333   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
1334 
1335   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
1336   {
1337     return srb->SrbStatus;
1338   }
1339   else
1340   {
1341     return ((PSCSI_REQUEST_BLOCK)srb)->SrbStatus;
1342   }
1343 }
1344 
1345 FORCEINLINE
1346 VOID
SrbSetSrbStatus(_In_ PVOID Srb,_In_ UCHAR status)1347 SrbSetSrbStatus(
1348   _In_ PVOID Srb,
1349   _In_ UCHAR status)
1350 {
1351   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
1352 
1353   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
1354   {
1355     if (srb->SrbStatus & SRB_STATUS_AUTOSENSE_VALID)
1356     {
1357       srb->SrbStatus = status | SRB_STATUS_AUTOSENSE_VALID;
1358     }
1359     else
1360     {
1361       srb->SrbStatus = status;
1362     }
1363   }
1364   else
1365   {
1366     if (((PSCSI_REQUEST_BLOCK)srb)->SrbStatus & SRB_STATUS_AUTOSENSE_VALID)
1367     {
1368       ((PSCSI_REQUEST_BLOCK)srb)->SrbStatus = status | SRB_STATUS_AUTOSENSE_VALID;
1369     }
1370     else
1371     {
1372       ((PSCSI_REQUEST_BLOCK)srb)->SrbStatus = status;
1373     }
1374   }
1375 }
1376 
1377 FORCEINLINE
1378 PVOID
SrbGetPortContext(_In_ PVOID Srb)1379 SrbGetPortContext(
1380   _In_ PVOID Srb)
1381 {
1382   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
1383 
1384   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
1385   {
1386     return srb->PortContext;
1387   }
1388   else
1389   {
1390     SRBHELPER_ASSERT(FALSE);
1391     return NULL;
1392   }
1393 }
1394 
1395 FORCEINLINE
1396 VOID
SrbSetPortContext(_In_ PVOID Srb,_In_ PVOID PortContext)1397 SrbSetPortContext(
1398   _In_ PVOID Srb,
1399   _In_ PVOID PortContext)
1400 {
1401   PSTORAGE_REQUEST_BLOCK srb = (PSTORAGE_REQUEST_BLOCK)Srb;
1402 
1403   if (srb->Function == SRB_FUNCTION_STORAGE_REQUEST_BLOCK)
1404   {
1405     srb->PortContext = PortContext;
1406   }
1407   else
1408   {
1409     SRBHELPER_ASSERT(FALSE);
1410   }
1411 }
1412 
1413 #endif /* (NTDDI_VERSION >= NTDDI_WIN8) */
1414 #endif /* _SRBHELPER_H_ */
1415