ReactOS  0.4.15-dev-439-g292f67a
scsi.c File Reference
#include "usbstor.h"
#include <debug.h>
Include dependency graph for scsi.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

static NTSTATUS USBSTOR_SrbStatusToNtStatus (IN PSCSI_REQUEST_BLOCK Srb)
 
static NTSTATUS USBSTOR_IssueBulkOrInterruptRequest (IN PFDO_DEVICE_EXTENSION FDODeviceExtension, IN PIRP Irp, IN USBD_PIPE_HANDLE PipeHandle, IN ULONG TransferFlags, IN ULONG TransferBufferLength, IN PVOID TransferBuffer, IN PMDL TransferBufferMDL, IN PIO_COMPLETION_ROUTINE CompletionRoutine)
 
static BOOLEAN USBSTOR_IsCSWValid (PIRP_CONTEXT Context)
 
static NTSTATUS USBSTOR_IssueRequestSense (IN PFDO_DEVICE_EXTENSION FDODeviceExtension, IN PIRP Irp)
 
NTSTATUS NTAPI USBSTOR_CSWCompletionRoutine (PDEVICE_OBJECT DeviceObject, PIRP Irp, PVOID Ctx)
 
NTSTATUS USBSTOR_SendCSWRequest (PFDO_DEVICE_EXTENSION FDODeviceExtension, PIRP Irp)
 
NTSTATUS NTAPI USBSTOR_DataCompletionRoutine (PDEVICE_OBJECT DeviceObject, PIRP Irp, PVOID Ctx)
 
NTSTATUS NTAPI USBSTOR_CBWCompletionRoutine (PDEVICE_OBJECT DeviceObject, PIRP Irp, PVOID Ctx)
 
VOID DumpCBW (PUCHAR Block)
 
static NTSTATUS USBSTOR_SendCBWRequest (IN PFDO_DEVICE_EXTENSION FDODeviceExtension, IN PIRP Irp)
 
NTSTATUS USBSTOR_HandleExecuteSCSI (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
 

Variables

IO_COMPLETION_ROUTINE USBSTOR_CSWCompletionRoutine
 
IO_COMPLETION_ROUTINE USBSTOR_DataCompletionRoutine
 
IO_COMPLETION_ROUTINE USBSTOR_CBWCompletionRoutine
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 14 of file scsi.c.

Function Documentation

◆ DumpCBW()

VOID DumpCBW ( PUCHAR  Block)

Definition at line 444 of file scsi.c.

446 {
447  DPRINT("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
448  Block[0] & 0xFF, Block[1] & 0xFF, Block[2] & 0xFF, Block[3] & 0xFF, Block[4] & 0xFF, Block[5] & 0xFF, Block[6] & 0xFF, Block[7] & 0xFF, Block[8] & 0xFF, Block[9] & 0xFF,
449  Block[10] & 0xFF, Block[11] & 0xFF, Block[12] & 0xFF, Block[13] & 0xFF, Block[14] & 0xFF, Block[15] & 0xFF, Block[16] & 0xFF, Block[17] & 0xFF, Block[18] & 0xFF, Block[19] & 0xFF,
450  Block[20] & 0xFF, Block[21] & 0xFF, Block[22] & 0xFF, Block[23] & 0xFF, Block[24] & 0xFF, Block[25] & 0xFF, Block[26] & 0xFF, Block[27] & 0xFF, Block[28] & 0xFF, Block[29] & 0xFF,
451  Block[30] & 0xFF);
452 }
void DPRINT(...)
Definition: polytest.cpp:61

Referenced by USBSTOR_SendCBWRequest(), and USBSTOR_SendRequest().

◆ USBSTOR_CBWCompletionRoutine()

NTSTATUS NTAPI USBSTOR_CBWCompletionRoutine ( PDEVICE_OBJECT  DeviceObject,
PIRP  Irp,
PVOID  Ctx 
)

Definition at line 329 of file scsi.c.

333 {
334  PIO_STACK_LOCATION IoStack;
336  PPDO_DEVICE_EXTENSION PDODeviceExtension;
337  PFDO_DEVICE_EXTENSION FDODeviceExtension;
340  PMDL Mdl = NULL;
342 
343  DPRINT("USBSTOR_CBWCompletionRoutine Irp %p Ctx %p Status %x\n", Irp, Ctx, Irp->IoStatus.Status);
344 
345  FDODeviceExtension = (PFDO_DEVICE_EXTENSION)Ctx;
347  Request = IoStack->Parameters.Scsi.Srb;
348  PDODeviceExtension = (PPDO_DEVICE_EXTENSION)IoStack->DeviceObject->DeviceExtension;
349 
350  if (!NT_SUCCESS(Irp->IoStatus.Status))
351  {
352  goto ResetRecovery;
353  }
354 
355  // a request without the buffer AND not a sense request
356  // for a sense request we provide just a TransferBuffer, an Mdl will be allocated by usbport (see below)
357  if (!Irp->MdlAddress && Request == FDODeviceExtension->ActiveSrb)
358  {
359  Request->SrbStatus = SRB_STATUS_SUCCESS;
360  USBSTOR_SendCSWRequest(FDODeviceExtension, Irp);
362  }
363 
364  // a request with the data buffer
365 
367  {
368  PipeHandle = FDODeviceExtension->InterfaceInformation->Pipes[FDODeviceExtension->BulkInPipeIndex].PipeHandle;
370  }
372  {
373  PipeHandle = FDODeviceExtension->InterfaceInformation->Pipes[FDODeviceExtension->BulkOutPipeIndex].PipeHandle;
375  }
376  else
377  {
378  // we check the validity of a request in disk.c so we should never be here
379  DPRINT1("Warning: shouldn't be here\n");
380  goto ResetRecovery;
381  }
382 
383  // if it is not a Sense Request
384  if (Request == FDODeviceExtension->ActiveSrb)
385  {
386  if (MmGetMdlVirtualAddress(Irp->MdlAddress) == Request->DataBuffer)
387  {
388  Mdl = Irp->MdlAddress;
389  }
390  else
391  {
392  Mdl = IoAllocateMdl(Request->DataBuffer,
393  Request->DataTransferLength,
394  FALSE,
395  FALSE,
396  NULL);
397 
398  if (Mdl)
399  {
400  IoBuildPartialMdl(Irp->MdlAddress,
401  Mdl,
402  Request->DataBuffer,
403  Request->DataTransferLength);
404  }
405  }
406 
407  if (!Mdl)
408  {
409  DPRINT1("USBSTOR_CBWCompletionRoutine: Mdl - %p\n", Mdl);
410  goto ResetRecovery;
411  }
412  }
413  else
414  {
415  ASSERT(Request->DataBuffer);
416  TransferBuffer = Request->DataBuffer;
417  }
418 
419  USBSTOR_IssueBulkOrInterruptRequest(FDODeviceExtension,
420  Irp,
421  PipeHandle,
423  Request->DataTransferLength,
425  Mdl,
427 
429 
430 ResetRecovery:
431  Request = FDODeviceExtension->ActiveSrb;
432  IoStack->Parameters.Scsi.Srb = Request;
433  Irp->IoStatus.Information = 0;
434  Irp->IoStatus.Status = STATUS_IO_DEVICE_ERROR;
435  Request->SrbStatus = SRB_STATUS_BUS_RESET;
436 
437  USBSTOR_QueueTerminateRequest(PDODeviceExtension->LowerDeviceObject, Irp);
438  USBSTOR_QueueResetDevice(FDODeviceExtension);
439 
441 }
#define SRB_STATUS_BUS_RESET
Definition: srb.h:345
#define SRB_FLAGS_UNSPECIFIED_DIRECTION
Definition: srb.h:395
#define STATUS_MORE_PROCESSING_REQUIRED
Definition: shellext.h:68
#define MmGetMdlVirtualAddress(_Mdl)
_In_ PIRP Irp
Definition: csq.h:116
#define USBD_TRANSFER_DIRECTION_IN
Definition: usb.h:160
struct _PDO_DEVICE_EXTENSION * PPDO_DEVICE_EXTENSION
#define STATUS_CONTINUE_COMPLETION
#define SRB_FLAGS_DATA_IN
Definition: srb.h:392
struct _FDO_DEVICE_EXTENSION * PFDO_DEVICE_EXTENSION
#define STATUS_IO_DEVICE_ERROR
Definition: udferr_usr.h:179
_In_ NDIS_HANDLE _In_ PNDIS_REQUEST Request
Definition: ndis.h:5173
VOID NTAPI IoBuildPartialMdl(IN PMDL SourceMdl, IN PMDL TargetMdl, IN PVOID VirtualAddress, IN ULONG Length)
Definition: iomdl.c:96
IO_COMPLETION_ROUTINE USBSTOR_DataCompletionRoutine
Definition: scsi.c:256
#define USBD_TRANSFER_DIRECTION_OUT
Definition: usb.h:159
PVOID DeviceExtension
Definition: env_spec_w32.h:418
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
VOID USBSTOR_QueueTerminateRequest(IN PDEVICE_OBJECT FDODeviceObject, IN PIRP Irp)
Definition: queue.c:186
NTSTATUS USBSTOR_SendCSWRequest(PFDO_DEVICE_EXTENSION FDODeviceExtension, PIRP Irp)
Definition: scsi.c:242
if(!(yy_init))
Definition: macro.lex.yy.c:714
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
PDEVICE_OBJECT DeviceObject
Definition: iotypes.h:2867
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
PMDL NTAPI IoAllocateMdl(IN PVOID VirtualAddress, IN ULONG Length, IN BOOLEAN SecondaryBuffer, IN BOOLEAN ChargeQuota, IN PIRP Irp)
Definition: iomdl.c:22
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2745
#define USBD_SHORT_TRANSFER_OK
Definition: usb.h:154
static HANDLE PipeHandle
Definition: dhcpcsvc.c:21
#define SRB_FLAGS_DATA_OUT
Definition: srb.h:393
static NTSTATUS USBSTOR_IssueBulkOrInterruptRequest(IN PFDO_DEVICE_EXTENSION FDODeviceExtension, IN PIRP Irp, IN USBD_PIPE_HANDLE PipeHandle, IN ULONG TransferFlags, IN ULONG TransferBufferLength, IN PVOID TransferBuffer, IN PMDL TransferBufferMDL, IN PIO_COMPLETION_ROUTINE CompletionRoutine)
Definition: scsi.c:60
#define DPRINT1
Definition: precomp.h:8
ULONG TransferFlags
Definition: usb.h:469
unsigned int ULONG
Definition: retypes.h:1
#define SRB_STATUS_SUCCESS
Definition: srb.h:333
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2772
VOID NTAPI USBSTOR_QueueResetDevice(IN PFDO_DEVICE_EXTENSION FDODeviceExtension)
Definition: error.c:148
PVOID TransferBuffer
Definition: usb.h:471

◆ USBSTOR_CSWCompletionRoutine()

NTSTATUS NTAPI USBSTOR_CSWCompletionRoutine ( PDEVICE_OBJECT  DeviceObject,
PIRP  Irp,
PVOID  Ctx 
)

Definition at line 129 of file scsi.c.

133 {
135  PIO_STACK_LOCATION IoStack;
136  PPDO_DEVICE_EXTENSION PDODeviceExtension;
137  PFDO_DEVICE_EXTENSION FDODeviceExtension;
139 
140  DPRINT("USBSTOR_CSWCompletionRoutine Irp %p Ctx %p Status %x\n", Irp, Ctx, Irp->IoStatus.Status);
141 
143  PDODeviceExtension = (PPDO_DEVICE_EXTENSION)IoStack->DeviceObject->DeviceExtension;
144  FDODeviceExtension = (PFDO_DEVICE_EXTENSION)Ctx;
145  Context = &FDODeviceExtension->CurrentIrpContext;
146  Request = IoStack->Parameters.Scsi.Srb;
147  ASSERT(Request);
148 
149  // first check for Irp errors
150  if (!NT_SUCCESS(Irp->IoStatus.Status))
151  {
152  if (USBD_STATUS(Context->Urb.UrbHeader.Status) == USBD_STATUS(USBD_STATUS_STALL_PID))
153  {
154  if (Context->StallRetryCount < 2)
155  {
156  ++Context->StallRetryCount;
157 
158  // clear stall and resend cbw
159  USBSTOR_QueueResetPipe(FDODeviceExtension);
160 
162  }
163  }
164  else
165  {
166  DPRINT1("USBSTOR_CSWCompletionRoutine: Urb.Hdr.Status - %x\n", Context->Urb.UrbHeader.Status);
167  }
168 
169  goto ResetRecovery;
170  }
171 
172  // now check the CSW packet validity
174  {
175  goto ResetRecovery;
176  }
177 
178  // finally check for CSW errors
179  if (Context->csw.Status == CSW_STATUS_COMMAND_PASSED)
180  {
181  // should happen only when a sense request was sent
182  if (Request != FDODeviceExtension->ActiveSrb)
183  {
184  ASSERT(IoStack->Parameters.Scsi.Srb == &Context->SenseSrb);
185  FDODeviceExtension->ActiveSrb->SenseInfoBufferLength = Request->DataTransferLength;
186  Request = FDODeviceExtension->ActiveSrb;
187  IoStack->Parameters.Scsi.Srb = Request;
188  Request->SrbStatus |= SRB_STATUS_AUTOSENSE_VALID;
189  }
190 
191  Irp->IoStatus.Status = USBSTOR_SrbStatusToNtStatus(Request);
192  }
193  else if (Context->csw.Status == CSW_STATUS_COMMAND_FAILED)
194  {
195  // the command is correct but with failed status - issue request sense
196  DPRINT("USBSTOR_CSWCompletionRoutine: CSW_STATUS_COMMAND_FAILED\n");
197 
198  ASSERT(FDODeviceExtension->ActiveSrb == Request);
199 
200  // setting a generic error status, additional information
201  // should be read by higher-level driver from SenseInfoBuffer
202  Request->SrbStatus = SRB_STATUS_ERROR;
203  Request->ScsiStatus = 2;
204  Request->DataTransferLength = 0;
205 
206  DPRINT("Flags: %x SBL: %x, buf: %p\n", Request->SrbFlags, Request->SenseInfoBufferLength, Request->SenseInfoBuffer);
207 
208  if (!(Request->SrbFlags & SRB_FLAGS_DISABLE_AUTOSENSE) &&
209  Request->SenseInfoBufferLength &&
210  Request->SenseInfoBuffer)
211  {
212  USBSTOR_IssueRequestSense(FDODeviceExtension, Irp);
214  }
215 
216  Irp->IoStatus.Status = STATUS_IO_DEVICE_ERROR;
217  }
218 
219  Irp->IoStatus.Information = Request->DataTransferLength;
220 
221  // terminate current request
222  USBSTOR_QueueTerminateRequest(PDODeviceExtension->LowerDeviceObject, Irp);
223  USBSTOR_QueueNextRequest(PDODeviceExtension->LowerDeviceObject);
224 
226 
227 ResetRecovery:
228 
229  Request = FDODeviceExtension->ActiveSrb;
230  IoStack->Parameters.Scsi.Srb = Request;
231  Irp->IoStatus.Information = 0;
232  Irp->IoStatus.Status = STATUS_IO_DEVICE_ERROR;
233  Request->SrbStatus = SRB_STATUS_BUS_RESET;
234 
235  USBSTOR_QueueTerminateRequest(PDODeviceExtension->LowerDeviceObject, Irp);
236  USBSTOR_QueueResetDevice(FDODeviceExtension);
237 
239 }
#define SRB_FLAGS_DISABLE_AUTOSENSE
Definition: srb.h:391
#define SRB_STATUS_BUS_RESET
Definition: srb.h:345
static NTSTATUS USBSTOR_SrbStatusToNtStatus(IN PSCSI_REQUEST_BLOCK Srb)
Definition: scsi.c:20
#define STATUS_MORE_PROCESSING_REQUIRED
Definition: shellext.h:68
#define SRB_STATUS_AUTOSENSE_VALID
Definition: srb.h:379
_In_ PIRP Irp
Definition: csq.h:116
#define CSW_STATUS_COMMAND_PASSED
Definition: usbstor.h:82
struct _PDO_DEVICE_EXTENSION * PPDO_DEVICE_EXTENSION
static NTSTATUS USBSTOR_IssueRequestSense(IN PFDO_DEVICE_EXTENSION FDODeviceExtension, IN PIRP Irp)
Definition: scsi.c:502
#define STATUS_CONTINUE_COMPLETION
#define STATUS_IO_DEVICE_ERROR
Definition: udferr_usr.h:179
#define SRB_STATUS_ERROR
Definition: srb.h:336
_In_ NDIS_HANDLE _In_ PNDIS_REQUEST Request
Definition: ndis.h:5173
VOID USBSTOR_QueueNextRequest(IN PDEVICE_OBJECT DeviceObject)
Definition: queue.c:220
PVOID DeviceExtension
Definition: env_spec_w32.h:418
void DPRINT(...)
Definition: polytest.cpp:61
#define CSW_STATUS_COMMAND_FAILED
Definition: usbstor.h:83
VOID USBSTOR_QueueTerminateRequest(IN PDEVICE_OBJECT FDODeviceObject, IN PIRP Irp)
Definition: queue.c:186
static BOOLEAN USBSTOR_IsCSWValid(PIRP_CONTEXT Context)
Definition: scsi.c:101
#define CSW_STATUS_PHASE_ERROR
Definition: usbstor.h:84
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
PDEVICE_OBJECT DeviceObject
Definition: iotypes.h:2867
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
LONG USBD_STATUS
Definition: usb.h:165
#define USBD_STATUS_STALL_PID
Definition: usb.h:175
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2745
#define DPRINT1
Definition: precomp.h:8
VOID NTAPI USBSTOR_QueueResetPipe(IN PFDO_DEVICE_EXTENSION FDODeviceExtension)
Definition: error.c:135
struct tagContext Context
Definition: acpixf.h:1034
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2772
VOID NTAPI USBSTOR_QueueResetDevice(IN PFDO_DEVICE_EXTENSION FDODeviceExtension)
Definition: error.c:148

◆ USBSTOR_DataCompletionRoutine()

NTSTATUS NTAPI USBSTOR_DataCompletionRoutine ( PDEVICE_OBJECT  DeviceObject,
PIRP  Irp,
PVOID  Ctx 
)

Definition at line 260 of file scsi.c.

264 {
266  PIO_STACK_LOCATION IoStack;
268  PFDO_DEVICE_EXTENSION FDODeviceExtension;
269  PPDO_DEVICE_EXTENSION PDODeviceExtension;
270 
271  DPRINT("USBSTOR_DataCompletionRoutine Irp %p Ctx %p Status %x\n", Irp, Ctx, Irp->IoStatus.Status);
272 
273  FDODeviceExtension = (PFDO_DEVICE_EXTENSION)Ctx;
274  Context = &FDODeviceExtension->CurrentIrpContext;
276  Request = IoStack->Parameters.Scsi.Srb;
277  PDODeviceExtension = (PPDO_DEVICE_EXTENSION)IoStack->DeviceObject->DeviceExtension;
278 
279  // for Sense Request a partial MDL was already freed (if existed)
280  if (Request == FDODeviceExtension->ActiveSrb &&
281  Context->Urb.UrbBulkOrInterruptTransfer.TransferBufferMDL != Irp->MdlAddress)
282  {
283  IoFreeMdl(Context->Urb.UrbBulkOrInterruptTransfer.TransferBufferMDL);
284  }
285 
286  if (NT_SUCCESS(Irp->IoStatus.Status))
287  {
288  if (Context->Urb.UrbBulkOrInterruptTransfer.TransferBufferLength < Request->DataTransferLength)
289  {
290  Request->SrbStatus = SRB_STATUS_DATA_OVERRUN;
291  }
292  else
293  {
294  Request->SrbStatus = SRB_STATUS_SUCCESS;
295  }
296 
297  Request->DataTransferLength = Context->Urb.UrbBulkOrInterruptTransfer.TransferBufferLength;
298  USBSTOR_SendCSWRequest(FDODeviceExtension, Irp);
299  }
300  else if (USBD_STATUS(Context->Urb.UrbHeader.Status) == USBD_STATUS(USBD_STATUS_STALL_PID))
301  {
302  ++Context->StallRetryCount;
303 
304  Request->SrbStatus = SRB_STATUS_DATA_OVERRUN;
305  Request->DataTransferLength = Context->Urb.UrbBulkOrInterruptTransfer.TransferBufferLength;
306 
307  // clear stall and resend cbw
308  USBSTOR_QueueResetPipe(FDODeviceExtension);
309  }
310  else
311  {
312  Irp->IoStatus.Information = 0;
313  Irp->IoStatus.Status = STATUS_IO_DEVICE_ERROR;
314  Request->SrbStatus = SRB_STATUS_BUS_RESET;
315 
316  USBSTOR_QueueTerminateRequest(PDODeviceExtension->LowerDeviceObject, Irp);
317  USBSTOR_QueueResetDevice(FDODeviceExtension);
318 
320  }
321 
323 }
#define SRB_STATUS_BUS_RESET
Definition: srb.h:345
#define STATUS_MORE_PROCESSING_REQUIRED
Definition: shellext.h:68
_In_ PIRP Irp
Definition: csq.h:116
struct _PDO_DEVICE_EXTENSION * PPDO_DEVICE_EXTENSION
#define STATUS_CONTINUE_COMPLETION
struct _FDO_DEVICE_EXTENSION * PFDO_DEVICE_EXTENSION
#define STATUS_IO_DEVICE_ERROR
Definition: udferr_usr.h:179
_In_ NDIS_HANDLE _In_ PNDIS_REQUEST Request
Definition: ndis.h:5173
PVOID DeviceExtension
Definition: env_spec_w32.h:418
void DPRINT(...)
Definition: polytest.cpp:61
VOID USBSTOR_QueueTerminateRequest(IN PDEVICE_OBJECT FDODeviceObject, IN PIRP Irp)
Definition: queue.c:186
NTSTATUS USBSTOR_SendCSWRequest(PFDO_DEVICE_EXTENSION FDODeviceExtension, PIRP Irp)
Definition: scsi.c:242
if(!(yy_init))
Definition: macro.lex.yy.c:714
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
PDEVICE_OBJECT DeviceObject
Definition: iotypes.h:2867
#define SRB_STATUS_DATA_OVERRUN
Definition: srb.h:349
VOID NTAPI IoFreeMdl(PMDL Mdl)
Definition: iomdl.c:146
LONG USBD_STATUS
Definition: usb.h:165
#define USBD_STATUS_STALL_PID
Definition: usb.h:175
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2745
VOID NTAPI USBSTOR_QueueResetPipe(IN PFDO_DEVICE_EXTENSION FDODeviceExtension)
Definition: error.c:135
struct tagContext Context
Definition: acpixf.h:1034
#define SRB_STATUS_SUCCESS
Definition: srb.h:333
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2772
VOID NTAPI USBSTOR_QueueResetDevice(IN PFDO_DEVICE_EXTENSION FDODeviceExtension)
Definition: error.c:148

◆ USBSTOR_HandleExecuteSCSI()

NTSTATUS USBSTOR_HandleExecuteSCSI ( IN PDEVICE_OBJECT  DeviceObject,
IN PIRP  Irp 
)

Definition at line 540 of file scsi.c.

543 {
544  PIO_STACK_LOCATION IoStack;
547 
548  ASSERT(PDODeviceExtension->Common.IsFDO == FALSE);
549 
551  Request = IoStack->Parameters.Scsi.Srb;
552 
553  DPRINT("USBSTOR_HandleExecuteSCSI Operation Code %x, Length %lu\n", SrbGetCdb(Request)->CDB10.OperationCode, Request->DataTransferLength);
554 
555  // check that we're sending to the right LUN
556  ASSERT(SrbGetCdb(Request)->CDB10.LogicalUnitNumber == PDODeviceExtension->LUN);
557 
558  return USBSTOR_SendCBWRequest(PDODeviceExtension->LowerDeviceObject->DeviceExtension, Irp);
559 }
_In_ PIRP Irp
Definition: csq.h:116
struct _PDO_DEVICE_EXTENSION * PPDO_DEVICE_EXTENSION
_In_ NDIS_HANDLE _In_ PNDIS_REQUEST Request
Definition: ndis.h:5173
PVOID DeviceExtension
Definition: env_spec_w32.h:418
COMMON_DEVICE_EXTENSION Common
Definition: pci.h:56
void DPRINT(...)
Definition: polytest.cpp:61
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1569
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2745
#define SrbGetCdb(srb)
Definition: usbstor.h:17
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2772
static NTSTATUS USBSTOR_SendCBWRequest(IN PFDO_DEVICE_EXTENSION FDODeviceExtension, IN PIRP Irp)
Definition: scsi.c:456

Referenced by USBSTOR_HandleTransferError(), and USBSTOR_StartIo().

◆ USBSTOR_IsCSWValid()

static BOOLEAN USBSTOR_IsCSWValid ( PIRP_CONTEXT  Context)
static

Definition at line 101 of file scsi.c.

103 {
104  if (Context->csw.Signature != CSW_SIGNATURE)
105  {
106  DPRINT1("[USBSTOR] Expected Signature %x but got %x\n", CSW_SIGNATURE, Context->csw.Signature);
107  return FALSE;
108  }
109 
110  if (Context->csw.Tag != PtrToUlong(Context->Irp))
111  {
112  DPRINT1("[USBSTOR] Expected Tag %Ix but got %x\n", PtrToUlong(Context->Irp), Context->csw.Tag);
113  return FALSE;
114  }
115 
116  return TRUE;
117 }
#define TRUE
Definition: types.h:120
#define PtrToUlong(u)
Definition: config.h:107
#define CSW_SIGNATURE
Definition: usbstor.h:65
#define DPRINT1
Definition: precomp.h:8

Referenced by USBSTOR_CSWCompletionRoutine().

◆ USBSTOR_IssueBulkOrInterruptRequest()

static NTSTATUS USBSTOR_IssueBulkOrInterruptRequest ( IN PFDO_DEVICE_EXTENSION  FDODeviceExtension,
IN PIRP  Irp,
IN USBD_PIPE_HANDLE  PipeHandle,
IN ULONG  TransferFlags,
IN ULONG  TransferBufferLength,
IN PVOID  TransferBuffer,
IN PMDL  TransferBufferMDL,
IN PIO_COMPLETION_ROUTINE  CompletionRoutine 
)
static

Definition at line 60 of file scsi.c.

69 {
70  PIO_STACK_LOCATION NextStack;
71  PIRP_CONTEXT Context = &FDODeviceExtension->CurrentIrpContext;
72 
74 
75  Context->Urb.UrbHeader.Length = sizeof(struct _URB_BULK_OR_INTERRUPT_TRANSFER);
76  Context->Urb.UrbHeader.Function = URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER;
77 
78  Context->Urb.UrbBulkOrInterruptTransfer.PipeHandle = PipeHandle;
79  Context->Urb.UrbBulkOrInterruptTransfer.TransferFlags = TransferFlags;
80  Context->Urb.UrbBulkOrInterruptTransfer.TransferBufferLength = TransferBufferLength;
81  Context->Urb.UrbBulkOrInterruptTransfer.TransferBuffer = TransferBuffer;
82  Context->Urb.UrbBulkOrInterruptTransfer.TransferBufferMDL = TransferBufferMDL;
83 
84  NextStack = IoGetNextIrpStackLocation(Irp);
86  NextStack->Parameters.DeviceIoControl.IoControlCode = IOCTL_INTERNAL_USB_SUBMIT_URB;
87  NextStack->Parameters.Others.Argument1 = &Context->Urb;
88 
91  FDODeviceExtension,
92  TRUE,
93  TRUE,
94  TRUE);
95 
96  return IoCallDriver(FDODeviceExtension->LowerDeviceObject, Irp);
97 }
#define TRUE
Definition: types.h:120
#define IOCTL_INTERNAL_USB_SUBMIT_URB
Definition: usbioctl.h:32
_In_ PIRP Irp
Definition: csq.h:116
#define IoSetCompletionRoutine(_Irp, _CompletionRoutine, _Context, _InvokeOnSuccess, _InvokeOnError, _InvokeOnCancel)
Definition: irp.cpp:498
#define IRP_MJ_INTERNAL_DEVICE_CONTROL
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetNextIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2647
static HANDLE PipeHandle
Definition: dhcpcsvc.c:21
NTSTATUS NTAPI IoCallDriver(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: irp.c:1218
_In_ PIRP _In_opt_ PVOID _In_opt_ POPLOCK_WAIT_COMPLETE_ROUTINE CompletionRoutine
Definition: fsrtlfuncs.h:673
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2772
#define URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER
Definition: usb.h:95

Referenced by USBSTOR_CBWCompletionRoutine(), USBSTOR_SendCBWRequest(), and USBSTOR_SendCSWRequest().

◆ USBSTOR_IssueRequestSense()

static NTSTATUS USBSTOR_IssueRequestSense ( IN PFDO_DEVICE_EXTENSION  FDODeviceExtension,
IN PIRP  Irp 
)
static

Definition at line 502 of file scsi.c.

505 {
506  PIO_STACK_LOCATION IoStack;
507  PSCSI_REQUEST_BLOCK CurrentSrb;
508  PSCSI_REQUEST_BLOCK SenseSrb;
509 
510  DPRINT("USBSTOR_IssueRequestSense: \n");
511 
512  CurrentSrb = FDODeviceExtension->ActiveSrb;
513  SenseSrb = &FDODeviceExtension->CurrentIrpContext.SenseSrb;
515  IoStack->Parameters.Scsi.Srb = SenseSrb;
516 
517  RtlZeroMemory(SenseSrb, sizeof(*SenseSrb));
518 
520  SenseSrb->Length = sizeof(*SenseSrb);
521  SenseSrb->CdbLength = CDB6GENERIC_LENGTH;
522  SenseSrb->SrbFlags = SRB_FLAGS_DATA_IN |
525 
526  ASSERT(CurrentSrb->SenseInfoBufferLength);
527  ASSERT(CurrentSrb->SenseInfoBuffer);
528  DPRINT("SenseInfoBuffer %x, SenseInfoBufferLength %x\n", CurrentSrb->SenseInfoBuffer, CurrentSrb->SenseInfoBufferLength);
529 
530  SenseSrb->DataTransferLength = CurrentSrb->SenseInfoBufferLength;
531  SenseSrb->DataBuffer = CurrentSrb->SenseInfoBuffer;
532 
533  SrbGetCdb(SenseSrb)->CDB6GENERIC.OperationCode = SCSIOP_REQUEST_SENSE;
534  SrbGetCdb(SenseSrb)->AsByte[4] = CurrentSrb->SenseInfoBufferLength;
535 
536  return USBSTOR_SendCBWRequest(FDODeviceExtension, Irp);
537 }
#define SRB_FLAGS_DISABLE_AUTOSENSE
Definition: srb.h:391
ULONG SrbFlags
Definition: srb.h:252
_In_ PIRP Irp
Definition: csq.h:116
#define SRB_FLAGS_NO_QUEUE_FREEZE
Definition: srb.h:396
PVOID DataBuffer
Definition: srb.h:255
ULONG DataTransferLength
Definition: srb.h:253
UCHAR CdbLength
Definition: srb.h:250
#define SRB_FLAGS_DATA_IN
Definition: srb.h:392
void DPRINT(...)
Definition: polytest.cpp:61
#define CDB6GENERIC_LENGTH
Definition: cdrw_hw.h:830
UCHAR Function
Definition: srb.h:242
USHORT Length
Definition: srb.h:241
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2745
UCHAR SenseInfoBufferLength
Definition: srb.h:251
PVOID SenseInfoBuffer
Definition: srb.h:256
#define SrbGetCdb(srb)
Definition: usbstor.h:17
#define SRB_FUNCTION_EXECUTE_SCSI
Definition: srb.h:307
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
#define SCSIOP_REQUEST_SENSE
Definition: cdrw_hw.h:870
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2772
static NTSTATUS USBSTOR_SendCBWRequest(IN PFDO_DEVICE_EXTENSION FDODeviceExtension, IN PIRP Irp)
Definition: scsi.c:456

Referenced by USBSTOR_CSWCompletionRoutine().

◆ USBSTOR_SendCBWRequest()

static NTSTATUS USBSTOR_SendCBWRequest ( IN PFDO_DEVICE_EXTENSION  FDODeviceExtension,
IN PIRP  Irp 
)
static

Definition at line 456 of file scsi.c.

459 {
460  PPDO_DEVICE_EXTENSION PDODeviceExtension;
461  PIO_STACK_LOCATION IoStack;
463  PIRP_CONTEXT Context = &FDODeviceExtension->CurrentIrpContext;
464 
465  RtlZeroMemory(&Context->cbw, sizeof(CBW));
466  RtlZeroMemory(&Context->Urb, sizeof(URB));
467 
469  PDODeviceExtension = IoStack->DeviceObject->DeviceExtension;
470  Request = IoStack->Parameters.Scsi.Srb;
471 
472  // Make a CBW structure from SCSI request block
473  Context->cbw.Signature = CBW_SIGNATURE;
474  Context->cbw.Tag = PtrToUlong(Irp);
475  Context->cbw.DataTransferLength = Request->DataTransferLength;
476  Context->cbw.Flags = ((UCHAR)Request->SrbFlags & SRB_FLAGS_UNSPECIFIED_DIRECTION) << 1;
477  Context->cbw.LUN = PDODeviceExtension->LUN;
478  Context->cbw.CommandBlockLength = Request->CdbLength;
479 
480  RtlCopyMemory(&Context->cbw.CommandBlock, Request->Cdb, Request->CdbLength);
481 
482  DPRINT("CBW for IRP %p\n", Irp);
483  DumpCBW((PUCHAR)&Context->cbw);
484 
485  // initialize rest of context
486  Context->Irp = Irp;
487  Context->StallRetryCount = 0;
488 
490  FDODeviceExtension,
491  Irp,
492  FDODeviceExtension->InterfaceInformation->Pipes[FDODeviceExtension->BulkOutPipeIndex].PipeHandle,
494  sizeof(CBW),
495  &Context->cbw,
496  NULL,
498 }
#define SRB_FLAGS_UNSPECIFIED_DIRECTION
Definition: srb.h:395
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
_In_ PIRP Irp
Definition: csq.h:116
unsigned char * PUCHAR
Definition: retypes.h:3
Definition: usbstor.h:69
_In_ NDIS_HANDLE _In_ PNDIS_REQUEST Request
Definition: ndis.h:5173
#define USBD_TRANSFER_DIRECTION_OUT
Definition: usb.h:159
PVOID DeviceExtension
Definition: env_spec_w32.h:418
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
#define PtrToUlong(u)
Definition: config.h:107
PDEVICE_OBJECT DeviceObject
Definition: iotypes.h:2867
unsigned char UCHAR
Definition: xmlstorage.h:181
VOID DumpCBW(PUCHAR Block)
Definition: scsi.c:444
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2745
Definition: usb.h:529
static NTSTATUS USBSTOR_IssueBulkOrInterruptRequest(IN PFDO_DEVICE_EXTENSION FDODeviceExtension, IN PIRP Irp, IN USBD_PIPE_HANDLE PipeHandle, IN ULONG TransferFlags, IN ULONG TransferBufferLength, IN PVOID TransferBuffer, IN PMDL TransferBufferMDL, IN PIO_COMPLETION_ROUTINE CompletionRoutine)
Definition: scsi.c:60
IO_COMPLETION_ROUTINE USBSTOR_CBWCompletionRoutine
Definition: scsi.c:325
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2772
#define CBW_SIGNATURE
Definition: usbstor.h:64

Referenced by USBSTOR_HandleExecuteSCSI(), and USBSTOR_IssueRequestSense().

◆ USBSTOR_SendCSWRequest()

NTSTATUS USBSTOR_SendCSWRequest ( PFDO_DEVICE_EXTENSION  FDODeviceExtension,
PIRP  Irp 
)

Definition at line 242 of file scsi.c.

245 {
246  return USBSTOR_IssueBulkOrInterruptRequest(FDODeviceExtension,
247  Irp,
248  FDODeviceExtension->InterfaceInformation->Pipes[FDODeviceExtension->BulkInPipeIndex].PipeHandle,
250  sizeof(CSW),
251  &FDODeviceExtension->CurrentIrpContext.csw,
252  NULL,
254 }
_In_ PIRP Irp
Definition: csq.h:116
#define USBD_TRANSFER_DIRECTION_IN
Definition: usb.h:160
Definition: usbstor.h:86
IO_COMPLETION_ROUTINE USBSTOR_CSWCompletionRoutine
Definition: scsi.c:125
smooth NULL
Definition: ftsmooth.c:416
static NTSTATUS USBSTOR_IssueBulkOrInterruptRequest(IN PFDO_DEVICE_EXTENSION FDODeviceExtension, IN PIRP Irp, IN USBD_PIPE_HANDLE PipeHandle, IN ULONG TransferFlags, IN ULONG TransferBufferLength, IN PVOID TransferBuffer, IN PMDL TransferBufferMDL, IN PIO_COMPLETION_ROUTINE CompletionRoutine)
Definition: scsi.c:60

Referenced by USBSTOR_CBWCompletionRoutine(), USBSTOR_DataCompletionRoutine(), and USBSTOR_ResetPipeWorkItemRoutine().

◆ USBSTOR_SrbStatusToNtStatus()

static NTSTATUS USBSTOR_SrbStatusToNtStatus ( IN PSCSI_REQUEST_BLOCK  Srb)
static

Definition at line 20 of file scsi.c.

22 {
23  UCHAR SrbStatus;
24 
25  SrbStatus = SRB_STATUS(Srb->SrbStatus);
26 
27  switch (SrbStatus)
28  {
29  case SRB_STATUS_SUCCESS:
30  return STATUS_SUCCESS;
31 
34 
38 
41  case SRB_STATUS_NO_HBA:
44 
45  case SRB_STATUS_TIMEOUT:
46  return STATUS_IO_TIMEOUT;
47 
52 
53  default:
55  }
56 }
#define STATUS_DEVICE_DOES_NOT_EXIST
Definition: ntstatus.h:414
#define SRB_STATUS_BUS_RESET
Definition: srb.h:345
#define SRB_STATUS_COMMAND_TIMEOUT
Definition: srb.h:343
#define STATUS_INVALID_DEVICE_REQUEST
Definition: udferr_usr.h:138
#define SRB_STATUS_NO_DEVICE
Definition: srb.h:340
#define SRB_STATUS_BAD_SRB_BLOCK_LENGTH
Definition: srb.h:352
#define SRB_STATUS_INVALID_TARGET_ID
Definition: srb.h:355
#define SRB_STATUS(Status)
Definition: srb.h:381
#define STATUS_IO_DEVICE_ERROR
Definition: udferr_usr.h:179
#define SRB_STATUS_NO_HBA
Definition: srb.h:348
#define STATUS_DEVICE_NOT_CONNECTED
Definition: udferr_usr.h:160
#define SRB_STATUS_BAD_FUNCTION
Definition: srb.h:356
#define SRB_STATUS_DATA_OVERRUN
Definition: srb.h:349
unsigned char UCHAR
Definition: xmlstorage.h:181
#define SRB_STATUS_SELECTION_TIMEOUT
Definition: srb.h:342
#define SRB_STATUS_INVALID_LUN
Definition: srb.h:354
#define STATUS_BUFFER_OVERFLOW
Definition: shellext.h:66
#define SRB_STATUS_TIMEOUT
Definition: srb.h:341
#define STATUS_IO_TIMEOUT
Definition: udferr_usr.h:163
#define SRB_STATUS_SUCCESS
Definition: srb.h:333
IN PSCSI_REQUEST_BLOCK Srb
Definition: class2.h:49
return STATUS_SUCCESS
Definition: btrfs.c:3014

Referenced by USBSTOR_CSWCompletionRoutine().

Variable Documentation

◆ USBSTOR_CBWCompletionRoutine

IO_COMPLETION_ROUTINE USBSTOR_CBWCompletionRoutine

Definition at line 325 of file scsi.c.

Referenced by USBSTOR_SendCBW(), and USBSTOR_SendCBWRequest().

◆ USBSTOR_CSWCompletionRoutine

IO_COMPLETION_ROUTINE USBSTOR_CSWCompletionRoutine

Definition at line 125 of file scsi.c.

Referenced by USBSTOR_CBWCompletionRoutine(), USBSTOR_SendCSW(), and USBSTOR_SendCSWRequest().

◆ USBSTOR_DataCompletionRoutine

IO_COMPLETION_ROUTINE USBSTOR_DataCompletionRoutine

Definition at line 256 of file scsi.c.

Referenced by USBSTOR_CBWCompletionRoutine().