Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

7093 lines
191 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1990 - 1999
  3. Module Name:
  4. internal.c
  5. Abstract:
  6. This is the NT SCSI port driver. This file contains the internal
  7. code.
  8. Authors:
  9. Mike Glass
  10. Jeff Havens
  11. Environment:
  12. kernel mode only
  13. Notes:
  14. This module is a driver dll for scsi miniports.
  15. Revision History:
  16. --*/
  17. #include "ideport.h"
  18. NTSTATUS
  19. IdeSendMiniPortIoctl(
  20. IN PFDO_EXTENSION DeviceExtension,
  21. IN PIRP RequestIrp
  22. );
  23. NTSTATUS
  24. IdeSendScsiPassThrough (
  25. IN PFDO_EXTENSION DeviceExtension,
  26. IN PIRP RequestIrp,
  27. IN BOOLEAN Direct
  28. );
  29. NTSTATUS
  30. IdeGetInquiryData(
  31. IN PFDO_EXTENSION DeviceExtension,
  32. IN PIRP Irp
  33. );
  34. NTSTATUS
  35. IdeClaimLogicalUnit(
  36. IN PFDO_EXTENSION DeviceExtension,
  37. IN PIRP Irp
  38. );
  39. NTSTATUS
  40. IdeRemoveDevice(
  41. IN PFDO_EXTENSION DeviceExtension,
  42. IN PIRP Irp
  43. );
  44. VOID
  45. IdeLogResetError(
  46. IN PFDO_EXTENSION DeviceExtension,
  47. IN PSCSI_REQUEST_BLOCK Srb,
  48. IN ULONG UniqueId
  49. );
  50. #ifdef LOG_GET_NEXT_CALLER
  51. VOID
  52. IdeLogCompletedCommand(
  53. PFDO_EXTENSION FdoExtension,
  54. PSCSI_REQUEST_BLOCK Srb
  55. );
  56. VOID
  57. IdeLogGetNextLuCaller (
  58. PFDO_EXTENSION FdoExtension,
  59. PPDO_EXTENSION PdoExtension,
  60. PUCHAR FileName,
  61. ULONG LineNumber
  62. );
  63. #endif
  64. #ifdef ALLOC_PRAGMA
  65. #pragma alloc_text(NONPAGE, IdePortDeviceControl)
  66. #pragma alloc_text(PAGE, IdeSendMiniPortIoctl)
  67. #pragma alloc_text(PAGE, IdeGetInquiryData)
  68. #pragma alloc_text(PAGE, IdeSendScsiPassThrough)
  69. #pragma alloc_text(PAGE, IdeClaimLogicalUnit)
  70. #pragma alloc_text(PAGE, IdeRemoveDevice)
  71. #endif
  72. #if DBG
  73. #define CheckIrql() {\
  74. if (saveIrql != KeGetCurrentIrql()){\
  75. DebugPrint((1, "saveIrql=%x, current=%x\n", saveIrql, KeGetCurrentIrql()));\
  76. ASSERT(FALSE);}\
  77. }
  78. #else
  79. #define CheckIrql()
  80. #endif
  81. //
  82. // Routines start
  83. //
  84. NTSTATUS
  85. IdePortDispatch(
  86. IN PDEVICE_OBJECT DeviceObject,
  87. IN PIRP Irp
  88. )
  89. /*++
  90. Routine Description:
  91. Arguments:
  92. DeviceObject - Address of device object.
  93. Irp - Address of I/O request packet.
  94. Return Value:
  95. Status.
  96. --*/
  97. {
  98. PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(Irp);
  99. PDEVICE_EXTENSION_HEADER doExtension;
  100. PFDO_EXTENSION deviceExtension;
  101. PSCSI_REQUEST_BLOCK srb = irpStack->Parameters.Scsi.Srb;
  102. PLOGICAL_UNIT_EXTENSION logicalUnit;
  103. NTSTATUS status;
  104. RESET_CONTEXT resetContext;
  105. KIRQL currentIrql;
  106. KIRQL saveIrql=KeGetCurrentIrql();
  107. #if DBG
  108. UCHAR savedCdb[16];
  109. ULONG ki;
  110. #endif
  111. doExtension = DeviceObject->DeviceExtension;
  112. if (doExtension->AttacheeDeviceObject == NULL) {
  113. //
  114. // This is a PDO
  115. //
  116. PPDO_EXTENSION pdoExtension = (PPDO_EXTENSION) doExtension;
  117. srb->PathId = (UCHAR) pdoExtension->PathId;
  118. srb->TargetId = (UCHAR) pdoExtension->TargetId;
  119. srb->Lun = (UCHAR) pdoExtension->Lun;
  120. ((PCDB) (srb->Cdb))->CDB6GENERIC.LogicalUnitNumber = srb->Lun;
  121. CheckIrql();
  122. return IdePortDispatch(
  123. pdoExtension->ParentDeviceExtension->DeviceObject,
  124. Irp
  125. );
  126. } else {
  127. //
  128. // This is a FDO;
  129. //
  130. deviceExtension = (PFDO_EXTENSION) doExtension;
  131. }
  132. //
  133. // Init SRB Flags for IDE
  134. //
  135. INIT_IDE_SRB_FLAGS (srb);
  136. //
  137. // get the target device object extension
  138. //
  139. logicalUnit = RefLogicalUnitExtensionWithTag(
  140. deviceExtension,
  141. srb->PathId,
  142. srb->TargetId,
  143. srb->Lun,
  144. TRUE,
  145. Irp
  146. );
  147. if (logicalUnit == NULL) {
  148. DebugPrint((1, "IdePortDispatch: Bad logical unit address.\n"));
  149. //
  150. // Fail the request. Set status in Irp and complete it.
  151. //
  152. srb->SrbStatus = SRB_STATUS_NO_DEVICE;
  153. Irp->IoStatus.Status = STATUS_NO_SUCH_DEVICE;
  154. CheckIrql();
  155. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  156. CheckIrql();
  157. return STATUS_NO_SUCH_DEVICE;
  158. }
  159. //
  160. // special flag for tape device
  161. //
  162. TEST_AND_SET_SRB_FOR_RDP(logicalUnit->ScsiDeviceType, srb);
  163. //
  164. // hang the logUnitExtension off the Irp
  165. //
  166. IDEPORT_PUT_LUNEXT_IN_IRP (irpStack, logicalUnit);
  167. //
  168. // check for DMA candidate
  169. // default (0) is DMA candidate
  170. //
  171. if (SRB_IS_DMA_CANDIDATE(srb)) {
  172. if (srb->SrbFlags & SRB_FLAGS_UNSPECIFIED_DIRECTION) {
  173. ULONG deviceFlags = deviceExtension->HwDeviceExtension->DeviceFlags[srb->TargetId];
  174. if (deviceFlags & DFLAGS_ATAPI_DEVICE) {
  175. if (srb->Cdb[0] == SCSIOP_MODE_SENSE) {
  176. if (!(deviceFlags & DFLAGS_TAPE_DEVICE)) {
  177. CheckIrql();
  178. return DeviceAtapiModeSense(logicalUnit, Irp);
  179. }
  180. //
  181. // we should do PIO for mode sense/select
  182. //
  183. MARK_SRB_AS_PIO_CANDIDATE(srb);
  184. } else if (srb->Cdb[0] == SCSIOP_MODE_SELECT) {
  185. if (!(deviceFlags & DFLAGS_TAPE_DEVICE)) {
  186. CheckIrql();
  187. return DeviceAtapiModeSelect(logicalUnit, Irp);
  188. }
  189. MARK_SRB_AS_PIO_CANDIDATE(srb);
  190. } else if (srb->Cdb[0] == SCSIOP_REQUEST_SENSE) {
  191. //
  192. // SCSIOP_REQUEST_SENSE
  193. // ALi can't handle odd word udma xfer
  194. // safest thing to do is do pio
  195. //
  196. MARK_SRB_AS_PIO_CANDIDATE(srb);
  197. } else if ((srb->Function == SRB_FUNCTION_ATA_POWER_PASS_THROUGH) ||
  198. (srb->Function == SRB_FUNCTION_ATA_PASS_THROUGH_EX) ||
  199. (srb->Function == SRB_FUNCTION_ATA_PASS_THROUGH)) {
  200. MARK_SRB_AS_PIO_CANDIDATE(srb);
  201. } else if ((srb->Cdb[0] == ATAPI_MODE_SENSE) ||
  202. (srb->Cdb[0] == ATAPI_MODE_SELECT) ||
  203. (srb->Cdb[0] == SCSIOP_INQUIRY) ||
  204. (srb->Cdb[0] == SCSIOP_GET_EVENT_STATUS) ||
  205. (srb->Cdb[0] == SCSIOP_GET_CONFIGURATION)) {
  206. //
  207. // ISSUE: use a pio only commands table
  208. //
  209. MARK_SRB_AS_PIO_CANDIDATE(srb);
  210. }
  211. } else { // ATA deivce
  212. if ((srb->Cdb[0] != SCSIOP_READ) && (srb->Cdb[0] != SCSIOP_WRITE)) {
  213. //
  214. // for ATA device, we can only DMA with SCSIOP_READ and SCSIOP_WRITE
  215. //
  216. MARK_SRB_AS_PIO_CANDIDATE(srb);
  217. if (srb->Cdb[0] == SCSIOP_READ_CAPACITY) {
  218. CheckIrql();
  219. return DeviceIdeReadCapacity (logicalUnit, Irp);
  220. } else if (srb->Cdb[0] == SCSIOP_MODE_SENSE) {
  221. CheckIrql();
  222. return DeviceIdeModeSense (logicalUnit, Irp);
  223. } else if (srb->Cdb[0] == SCSIOP_MODE_SELECT) {
  224. CheckIrql();
  225. return DeviceIdeModeSelect (logicalUnit, Irp);
  226. }
  227. }
  228. }
  229. //
  230. //Check with the miniport (Special cases)
  231. //
  232. ASSERT (doExtension->AttacheeDeviceObject);
  233. ASSERT (srb->TargetId >=0);
  234. #if DBG
  235. for (ki=0;ki<srb->CdbLength;ki++) {
  236. savedCdb[ki]=srb->Cdb[ki];
  237. }
  238. #endif
  239. //Check for NULL.
  240. if (deviceExtension->TransferModeInterface.UseDma){
  241. if (!((deviceExtension->TransferModeInterface.UseDma)
  242. (deviceExtension->TransferModeInterface.VendorSpecificDeviceExtension,
  243. (PVOID)(srb->Cdb), srb->TargetId))) {
  244. MARK_SRB_AS_PIO_CANDIDATE(srb);
  245. }
  246. }
  247. #if DBG
  248. for (ki=0;ki<srb->CdbLength;ki++) {
  249. if (savedCdb[ki] != srb->Cdb[ki]) {
  250. DebugPrint((DBG_ALWAYS,
  251. "Miniport modified the Cdb\n"));
  252. ASSERT(FALSE);
  253. }
  254. }
  255. #endif
  256. if ((logicalUnit->DmaTransferTimeoutCount >= PDO_DMA_TIMEOUT_LIMIT) ||
  257. (logicalUnit->CrcErrorCount >= PDO_UDMA_CRC_ERROR_LIMIT)) {
  258. //
  259. // broken hardware
  260. //
  261. MARK_SRB_AS_PIO_CANDIDATE(srb);
  262. }
  263. } else {
  264. MARK_SRB_AS_PIO_CANDIDATE(srb);
  265. }
  266. }
  267. switch (srb->Function) {
  268. case SRB_FUNCTION_SHUTDOWN:
  269. DebugPrint((1, "IdePortDispatch: SRB_FUNCTION_SHUTDOWN...\n"));
  270. // ISSUE: 08/30/2000: disable/restore MSN settings
  271. case SRB_FUNCTION_FLUSH:
  272. {
  273. ULONG dFlags = deviceExtension->HwDeviceExtension->DeviceFlags[srb->TargetId];
  274. //
  275. // for IDE devices, complete the request with status success if
  276. // the device doesn't support the flush cache command
  277. //
  278. if (!(dFlags & DFLAGS_ATAPI_DEVICE) &&
  279. ((logicalUnit->FlushCacheTimeoutCount >= PDO_FLUSH_TIMEOUT_LIMIT) ||
  280. (logicalUnit->
  281. ParentDeviceExtension->
  282. HwDeviceExtension->
  283. DeviceParameters[logicalUnit->TargetId].IdeFlushCommand
  284. == IDE_COMMAND_NO_FLUSH))) {
  285. srb->SrbStatus = SRB_STATUS_SUCCESS;
  286. status = STATUS_SUCCESS;
  287. CheckIrql();
  288. break;
  289. }
  290. DebugPrint((1,
  291. "IdePortDispatch: SRB_FUNCTION_%x to target %x\n",
  292. srb->Function,
  293. srb->TargetId
  294. ));
  295. #if 1
  296. //
  297. // we don't really handle these srb functions (shutdown
  298. // and flush). They just get translated to a flush cache
  299. // command on ata drives (since we don't know how to flush
  300. // the adapter cache). We will ignore them, since the
  301. // upper drivers should use SCSIOP_SYNCHRONIZE_CACHE to
  302. // flush the device cache.
  303. //
  304. if (!(dFlags & DFLAGS_ATAPI_DEVICE)) {
  305. srb->SrbStatus = SRB_STATUS_SUCCESS;
  306. status = STATUS_SUCCESS;
  307. CheckIrql();
  308. break;
  309. }
  310. #endif
  311. //
  312. // Fall thru to execute_scsi
  313. //
  314. }
  315. case SRB_FUNCTION_ATA_POWER_PASS_THROUGH:
  316. case SRB_FUNCTION_ATA_PASS_THROUGH:
  317. case SRB_FUNCTION_ATA_PASS_THROUGH_EX:
  318. case SRB_FUNCTION_IO_CONTROL:
  319. case SRB_FUNCTION_EXECUTE_SCSI:
  320. if (logicalUnit->PdoState & PDOS_DEADMEAT) {
  321. //
  322. // Fail the request. Set status in Irp and complete it.
  323. //
  324. srb->SrbStatus = SRB_STATUS_NO_DEVICE;
  325. status = STATUS_NO_SUCH_DEVICE;
  326. CheckIrql();
  327. break;
  328. }
  329. if (srb->SrbFlags & SRB_FLAGS_NO_KEEP_AWAKE) {
  330. if (logicalUnit->DevicePowerState != PowerDeviceD0) {
  331. DebugPrint ((DBG_POWER, "0x%x powered down. failing SRB_FLAGS_NO_KEEP_AWAKE srb 0x%x\n", logicalUnit, srb));
  332. srb->SrbStatus = SRB_STATUS_NOT_POWERED;
  333. status = STATUS_NO_SUCH_DEVICE;
  334. CheckIrql();
  335. break;
  336. }
  337. }
  338. //
  339. // Mark Irp status pending.
  340. //
  341. IoMarkIrpPending(Irp);
  342. if (srb->SrbFlags & SRB_FLAGS_BYPASS_FROZEN_QUEUE) {
  343. //
  344. // Call start io directly. This will by-pass the
  345. // frozen queue.
  346. //
  347. DebugPrint((DBG_READ_WRITE,
  348. "IdePortDispatch: Bypass frozen queue, IRP %lx\n",
  349. Irp));
  350. IoStartPacket(DeviceObject, Irp, (PULONG)NULL, NULL);
  351. CheckIrql();
  352. return STATUS_PENDING;
  353. } else {
  354. BOOLEAN inserted;
  355. //
  356. // Queue the packet normally.
  357. //
  358. status = IdePortInsertByKeyDeviceQueue (
  359. logicalUnit,
  360. Irp,
  361. srb->QueueSortKey,
  362. &inserted
  363. );
  364. if (NT_SUCCESS(status) && inserted) {
  365. //
  366. // irp is queued
  367. //
  368. } else {
  369. //
  370. // irp is ready to go
  371. //
  372. //
  373. // Clear the active flag. If there is another request, the flag will be
  374. // set again when the request is passed to the miniport.
  375. //
  376. CLRMASK (logicalUnit->LuFlags, PD_LOGICAL_UNIT_IS_ACTIVE);
  377. //
  378. // Clear the retry count.
  379. //
  380. logicalUnit->RetryCount = 0;
  381. //
  382. // Queue is empty; start request.
  383. //
  384. IoStartPacket(DeviceObject, Irp, (PULONG)NULL, NULL);
  385. }
  386. CheckIrql();
  387. return STATUS_PENDING;
  388. }
  389. case SRB_FUNCTION_RELEASE_QUEUE:
  390. DebugPrint((2,"IdePortDispatch: SCSI unfreeze queue TID %d\n",
  391. srb->TargetId));
  392. //
  393. // Acquire the spinlock to protect the flags structure and the saved
  394. // interrupt context.
  395. //
  396. KeRaiseIrql(DISPATCH_LEVEL, &currentIrql);
  397. KeAcquireSpinLockAtDpcLevel(&deviceExtension->SpinLock);
  398. //
  399. // Make sure the queue is frozen.
  400. //
  401. if (!(logicalUnit->LuFlags & PD_QUEUE_FROZEN)) {
  402. DebugPrint((DBG_WARNING,
  403. "IdePortDispatch: Request to unfreeze an unfrozen queue!\n"
  404. ));
  405. KeReleaseSpinLock(&deviceExtension->SpinLock, currentIrql);
  406. srb->SrbStatus = SRB_STATUS_SUCCESS;
  407. status = STATUS_SUCCESS;
  408. CheckIrql();
  409. break;
  410. }
  411. CLRMASK (logicalUnit->LuFlags, PD_QUEUE_FROZEN);
  412. //
  413. // If there is not an untagged request running then start the
  414. // next request for this logical unit. Otherwise free the
  415. // spin lock.
  416. //
  417. if (logicalUnit->SrbData.CurrentSrb == NULL) {
  418. //
  419. // GetNextLuRequest frees the spinlock.
  420. //
  421. GetNextLuRequest(deviceExtension, logicalUnit);
  422. KeLowerIrql(currentIrql);
  423. } else {
  424. DebugPrint((DBG_WARNING,
  425. "IdePortDispatch: Request to unfreeze queue with active request\n"
  426. ));
  427. KeReleaseSpinLock(&deviceExtension->SpinLock, currentIrql);
  428. }
  429. srb->SrbStatus = SRB_STATUS_SUCCESS;
  430. status = STATUS_SUCCESS;
  431. CheckIrql();
  432. break;
  433. case SRB_FUNCTION_RESET_BUS: {
  434. PATA_PASS_THROUGH ataPassThroughData;
  435. ataPassThroughData = ExAllocatePool(NonPagedPool, sizeof(ATA_PASS_THROUGH));
  436. if (ataPassThroughData == NULL) {
  437. srb->SrbStatus = SRB_STATUS_INTERNAL_ERROR;
  438. srb->InternalStatus=STATUS_INSUFFICIENT_RESOURCES;
  439. status=STATUS_INSUFFICIENT_RESOURCES;
  440. IdeLogNoMemoryError(deviceExtension,
  441. logicalUnit->TargetId,
  442. NonPagedPool,
  443. sizeof(ATA_PASS_THROUGH),
  444. IDEPORT_TAG_DISPATCH_RESET
  445. );
  446. CheckIrql();
  447. break;
  448. }
  449. RtlZeroMemory (ataPassThroughData, sizeof (*ataPassThroughData));
  450. ataPassThroughData->IdeReg.bReserved = ATA_PTFLAGS_BUS_RESET;
  451. status = IssueSyncAtaPassThroughSafe (
  452. logicalUnit->ParentDeviceExtension,
  453. logicalUnit,
  454. ataPassThroughData,
  455. FALSE,
  456. FALSE,
  457. 30,
  458. FALSE
  459. );
  460. if (NT_SUCCESS(status)) {
  461. IdeLogResetError(deviceExtension,
  462. srb,
  463. ('R'<<24) | 256);
  464. srb->SrbStatus = SRB_STATUS_SUCCESS;
  465. } else {
  466. //
  467. // fail to send ata pass through
  468. //
  469. srb->SrbStatus = SRB_STATUS_ERROR;
  470. if (status==STATUS_INSUFFICIENT_RESOURCES) {
  471. srb->SrbStatus = SRB_STATUS_INTERNAL_ERROR;
  472. srb->InternalStatus=STATUS_INSUFFICIENT_RESOURCES;
  473. }
  474. }
  475. CheckIrql();
  476. break;
  477. }
  478. //
  479. // Acquire the spinlock to protect the flags structure and the saved
  480. // interrupt context.
  481. //
  482. /*++
  483. KeAcquireSpinLock(&deviceExtension->SpinLock, &currentIrql);
  484. resetContext.DeviceExtension = deviceExtension;
  485. resetContext.PathId = srb->PathId;
  486. resetContext.NewResetSequence = TRUE;
  487. resetContext.ResetSrb = NULL;
  488. if (!KeSynchronizeExecution(deviceExtension->InterruptObject,
  489. IdeResetBusSynchronized,
  490. &resetContext)) {
  491. DebugPrint((1,"IdePortDispatch: Reset failed\n"));
  492. srb->SrbStatus = SRB_STATUS_PHASE_SEQUENCE_FAILURE;
  493. status = STATUS_IO_DEVICE_ERROR;
  494. } else {
  495. IdeLogResetError(deviceExtension,
  496. srb,
  497. ('R'<<24) | 256);
  498. srb->SrbStatus = SRB_STATUS_SUCCESS;
  499. status = STATUS_SUCCESS;
  500. }
  501. KeReleaseSpinLock(&deviceExtension->SpinLock, currentIrql);
  502. CheckIrql();
  503. break;
  504. --*/
  505. case SRB_FUNCTION_ABORT_COMMAND:
  506. DebugPrint((1, "IdePortDispatch: SCSI Abort or Reset command\n"));
  507. //
  508. // Mark Irp status pending.
  509. //
  510. IoMarkIrpPending(Irp);
  511. //
  512. // Don't queue these requests in the logical unit
  513. // queue, rather queue them to the adapter queue.
  514. //
  515. KeRaiseIrql(DISPATCH_LEVEL, &currentIrql);
  516. IoStartPacket(DeviceObject, Irp, (PULONG)NULL, NULL);
  517. KeLowerIrql(currentIrql);
  518. CheckIrql();
  519. return STATUS_PENDING;
  520. break;
  521. case SRB_FUNCTION_FLUSH_QUEUE:
  522. DebugPrint((1, "IdePortDispatch: SCSI flush queue command\n"));
  523. status = IdePortFlushLogicalUnit (
  524. deviceExtension,
  525. logicalUnit,
  526. FALSE
  527. );
  528. if (NT_SUCCESS(status)) {
  529. srb->SrbStatus = SRB_STATUS_SUCCESS;
  530. } else {
  531. srb->SrbStatus = SRB_STATUS_ERROR;
  532. }
  533. CheckIrql();
  534. break;
  535. case SRB_FUNCTION_ATTACH_DEVICE:
  536. case SRB_FUNCTION_CLAIM_DEVICE:
  537. case SRB_FUNCTION_RELEASE_DEVICE:
  538. status = IdeClaimLogicalUnit(deviceExtension, Irp);
  539. CheckIrql();
  540. break;
  541. case SRB_FUNCTION_REMOVE_DEVICE:
  542. //
  543. // decrement the refcount before remove the device
  544. //
  545. UnrefLogicalUnitExtensionWithTag(
  546. deviceExtension,
  547. logicalUnit,
  548. Irp
  549. );
  550. status = IdeRemoveDevice(deviceExtension, Irp);
  551. Irp->IoStatus.Status = status;
  552. CheckIrql();
  553. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  554. CheckIrql();
  555. return status;
  556. default:
  557. //
  558. // Found unsupported SRB function.
  559. //
  560. DebugPrint((1,"IdePortDispatch: Unsupported function, SRB %lx\n",
  561. srb));
  562. srb->SrbStatus = SRB_STATUS_INVALID_REQUEST;
  563. status = STATUS_INVALID_DEVICE_REQUEST;
  564. CheckIrql();
  565. break;
  566. }
  567. //
  568. // Set status in Irp.
  569. //
  570. Irp->IoStatus.Status = status;
  571. //
  572. // Decrement the logUnitExtension reference count
  573. //
  574. CheckIrql();
  575. UnrefLogicalUnitExtensionWithTag(
  576. deviceExtension,
  577. logicalUnit,
  578. Irp
  579. );
  580. IDEPORT_PUT_LUNEXT_IN_IRP (irpStack, NULL);
  581. //
  582. // Complete request at raised IRQ.
  583. //
  584. CheckIrql();
  585. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  586. CheckIrql();
  587. return status;
  588. } // end IdePortDispatch()
  589. VOID
  590. IdePortStartIo (
  591. IN PDEVICE_OBJECT DeviceObject,
  592. IN PIRP Irp
  593. )
  594. /*++
  595. Routine Description:
  596. Arguments:
  597. DeviceObject - Supplies pointer to Adapter device object.
  598. Irp - Supplies a pointer to an IRP.
  599. Return Value:
  600. Nothing.
  601. --*/
  602. {
  603. PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(Irp);
  604. PSCSI_REQUEST_BLOCK srb = irpStack->Parameters.Scsi.Srb;
  605. PFDO_EXTENSION deviceExtension = DeviceObject->DeviceExtension;
  606. PSRB_DATA srbData;
  607. PLOGICAL_UNIT_EXTENSION logicalUnit;
  608. LONG interlockResult;
  609. NTSTATUS status;
  610. ULONG deviceFlags = deviceExtension->HwDeviceExtension->DeviceFlags[srb->TargetId];
  611. PCDB cdb;
  612. LARGE_INTEGER timer;
  613. LogStartTime(TimeStartIo, &timer);
  614. DebugPrint((3,"IdePortStartIo: Enter routine\n"));
  615. //
  616. // Set the default flags in the SRB.
  617. //
  618. srb->SrbFlags |= deviceExtension->SrbFlags;
  619. //
  620. // Get logical unit extension.
  621. //
  622. logicalUnit = IDEPORT_GET_LUNEXT_IN_IRP (irpStack);
  623. if (!(srb->SrbFlags & SRB_FLAGS_NO_KEEP_AWAKE) &&
  624. (srb->Function != SRB_FUNCTION_ATA_POWER_PASS_THROUGH) &&
  625. (logicalUnit->IdleCounter)) {
  626. //
  627. // Tell Po that we are busy
  628. //
  629. PoSetDeviceBusy (logicalUnit->IdleCounter);
  630. }
  631. DebugPrint((2,"IdePortStartIo: Irp 0x%8x Srb 0x%8x DataBuf 0x%8x Len 0x%8x\n", Irp, srb, srb->DataBuffer, srb->DataTransferLength));
  632. //
  633. // No special resources are required. Set the SRB data to the
  634. // structure in the logical unit extension, set the queue tag value
  635. // to the untagged value, and clear the SRB extension.
  636. //
  637. srbData = &logicalUnit->SrbData;
  638. //
  639. // Update the sequence number for this request if there is not already one
  640. // assigned.
  641. //
  642. if (!srbData->SequenceNumber) {
  643. //
  644. // Assign a sequence number to the request and store it in the logical
  645. // unit.
  646. //
  647. srbData->SequenceNumber = deviceExtension->SequenceNumber++;
  648. }
  649. //
  650. // If this is not an ABORT request the set the current srb.
  651. // NOTE: Lock should be held here!
  652. //
  653. if (srb->Function != SRB_FUNCTION_ABORT_COMMAND) {
  654. ASSERT(srbData->CurrentSrb == NULL);
  655. srbData->CurrentSrb = srb;
  656. ASSERT(srbData->CurrentSrb);
  657. if ((deviceExtension->HwDeviceExtension->DeviceFlags[srb->TargetId] & DFLAGS_USE_DMA) &&
  658. SRB_IS_DMA_CANDIDATE(srb)) {
  659. MARK_SRB_FOR_DMA(srb);
  660. } else {
  661. MARK_SRB_FOR_PIO(srb);
  662. }
  663. } else {
  664. //
  665. // Only abort requests can be started when there is a current request
  666. // active.
  667. //
  668. ASSERT(logicalUnit->AbortSrb == NULL);
  669. logicalUnit->AbortSrb = srb;
  670. }
  671. //
  672. // Log the command
  673. //
  674. IdeLogStartCommandLog(srbData);
  675. //
  676. // Flush the data buffer if necessary.
  677. //
  678. if (srb->SrbFlags & SRB_FLAGS_UNSPECIFIED_DIRECTION) {
  679. //
  680. // Save the MDL virtual address.
  681. //
  682. srbData->SrbDataOffset = MmGetMdlVirtualAddress(Irp->MdlAddress);
  683. do {
  684. //
  685. // Determine if the adapter needs mapped memory.
  686. //
  687. if (!SRB_USES_DMA(srb)) { // PIO
  688. if (Irp->MdlAddress) {
  689. //
  690. // Get the mapped system address and
  691. // calculate offset into MDL.
  692. //
  693. srbData->SrbDataOffset = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, HighPagePriority);
  694. if ((srbData->SrbDataOffset == NULL) &&
  695. (deviceExtension->ReservedPages != NULL)) {
  696. KeAcquireSpinLockAtDpcLevel(&deviceExtension->SpinLock);
  697. //
  698. // this would set the appropriate flags in the device extension
  699. // and srbData only when the call succeeds.
  700. //
  701. srbData->SrbDataOffset = IdeMapLockedPagesWithReservedMapping(deviceExtension,
  702. srbData,
  703. Irp->MdlAddress
  704. );
  705. //
  706. // if there is another active request using the reserved pages
  707. // mark this one pending. When the active request completes this
  708. // one will be picked up
  709. //
  710. if (srbData->SrbDataOffset == (PVOID)-1) {
  711. DebugPrint ((1,
  712. "Irp 0x%x marked pending\n",
  713. Irp
  714. ));
  715. //
  716. // remove the current Srb
  717. //
  718. srbData->CurrentSrb = NULL;
  719. ASSERT(DeviceObject->CurrentIrp == Irp);
  720. SETMASK(deviceExtension->Flags, PD_PENDING_DEVICE_REQUEST);
  721. KeReleaseSpinLockFromDpcLevel(&deviceExtension->SpinLock);
  722. return;
  723. }
  724. KeReleaseSpinLockFromDpcLevel(&deviceExtension->SpinLock);
  725. }
  726. if (srbData->SrbDataOffset == NULL) {
  727. deviceExtension->LastMemoryFailure += IDEPORT_TAG_STARTIO_MDL;
  728. srbData->CurrentSrb = NULL;
  729. //
  730. // This is the correct status for insufficient resources
  731. //
  732. srb->SrbStatus=SRB_STATUS_INTERNAL_ERROR;
  733. srb->InternalStatus=STATUS_INSUFFICIENT_RESOURCES;
  734. Irp->IoStatus.Status=STATUS_INSUFFICIENT_RESOURCES;
  735. IdeLogNoMemoryError(deviceExtension,
  736. logicalUnit->TargetId,
  737. NonPagedPool,
  738. sizeof(MDL),
  739. IDEPORT_TAG_STARTIO_MDL
  740. );
  741. //
  742. // Clear the device busy flag
  743. //
  744. IoStartNextPacket(DeviceObject, FALSE);
  745. //
  746. // Acquire spin lock to protect the flags
  747. //
  748. KeAcquireSpinLockAtDpcLevel(&deviceExtension->SpinLock);
  749. //
  750. // Get the next request, if this request does not
  751. // bypass frozen queue. We don't want to start the
  752. // next request, if the queue is frozen.
  753. //
  754. if (!(srb->SrbFlags & SRB_FLAGS_BYPASS_FROZEN_QUEUE)) {
  755. //
  756. // This flag needs to be set for getnextlu to work
  757. //
  758. logicalUnit->LuFlags |= PD_LOGICAL_UNIT_IS_ACTIVE;
  759. //
  760. // Retrieve the next request and give it to the fdo
  761. // This releases the spinlock
  762. //
  763. GetNextLuRequest(deviceExtension, logicalUnit);
  764. }
  765. else {
  766. KeReleaseSpinLockFromDpcLevel(&deviceExtension->SpinLock);
  767. }
  768. //
  769. // Decrement the logUnitExtension reference count
  770. //
  771. UnrefLogicalUnitExtensionWithTag(
  772. deviceExtension,
  773. logicalUnit,
  774. Irp
  775. );
  776. //
  777. // Complete the request
  778. //
  779. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  780. return;
  781. }
  782. srb->DataBuffer = srbData->SrbDataOffset +
  783. (ULONG)((PUCHAR)srb->DataBuffer -
  784. (PCCHAR)MmGetMdlVirtualAddress(Irp->MdlAddress));
  785. }
  786. IdePortAllocateAccessToken (DeviceObject);
  787. status = STATUS_SUCCESS;
  788. } else { // DMA
  789. //
  790. // If the buffer is not mapped then the I/O buffer must be flushed.
  791. //
  792. KeFlushIoBuffers(Irp->MdlAddress,
  793. (BOOLEAN) (srb->SrbFlags & SRB_FLAGS_DATA_IN ? TRUE : FALSE),
  794. TRUE);
  795. #if defined (FAKE_BMSETUP_FAILURE)
  796. if (!(FailBmSetupCount++ % FAKE_BMSETUP_FAILURE)) {
  797. status = STATUS_UNSUCCESSFUL;
  798. } else {
  799. #endif // FAKE_BMSETUP_FAILURE
  800. status = deviceExtension->HwDeviceExtension->BusMasterInterface.BmSetup (
  801. deviceExtension->HwDeviceExtension->BusMasterInterface.Context,
  802. srb->DataBuffer,
  803. srb->DataTransferLength,
  804. Irp->MdlAddress,
  805. (BOOLEAN) (srb->SrbFlags & SRB_FLAGS_DATA_IN),
  806. IdePortAllocateAccessToken,
  807. DeviceObject
  808. );
  809. #if defined (FAKE_BMSETUP_FAILURE)
  810. }
  811. #endif // FAKE_BMSETUP_FAILURE
  812. if (!NT_SUCCESS(status)) {
  813. DebugPrint((1,
  814. "IdePortStartIo: IoAllocateAdapterChannel failed(%x). try pio for srb %x\n",
  815. status, srb));
  816. //
  817. // out of resource for DMA, try PIO
  818. //
  819. MARK_SRB_FOR_PIO(srb);
  820. }
  821. }
  822. } while (!NT_SUCCESS(status));
  823. } else {
  824. IdePortAllocateAccessToken (DeviceObject);
  825. }
  826. LogStopTime(TimeStartIo, &timer, 0);
  827. return;
  828. } // end IdePortStartIO()
  829. BOOLEAN
  830. IdePortInterrupt(
  831. IN PKINTERRUPT Interrupt,
  832. IN PDEVICE_OBJECT DeviceObject
  833. )
  834. /*++
  835. Routine Description:
  836. Arguments:
  837. Interrupt
  838. Device Object
  839. Return Value:
  840. Returns TRUE if interrupt expected.
  841. --*/
  842. {
  843. PFDO_EXTENSION deviceExtension = DeviceObject->DeviceExtension;
  844. BOOLEAN returnValue;
  845. LARGE_INTEGER timer;
  846. UNREFERENCED_PARAMETER(Interrupt);
  847. #ifdef ENABLE_ATAPI_VERIFIER
  848. ViAtapiInterrupt(deviceExtension);
  849. #endif
  850. LogStartTime(TimeIsr, &timer);
  851. returnValue = AtapiInterrupt(deviceExtension->HwDeviceExtension);
  852. LogStopTime(TimeIsr, &timer, 100);
  853. //
  854. // Check to see if a DPC needs to be queued.
  855. //
  856. if (deviceExtension->InterruptData.InterruptFlags & PD_NOTIFICATION_REQUIRED) {
  857. IoRequestDpc(deviceExtension->DeviceObject, NULL, NULL);
  858. }
  859. return(returnValue);
  860. } // end IdePortInterrupt()
  861. VOID
  862. IdePortCompletionDpc(
  863. IN PKDPC Dpc,
  864. IN PDEVICE_OBJECT DeviceObject,
  865. IN PIRP Irp,
  866. IN PVOID Context
  867. )
  868. /*++
  869. Routine Description:
  870. Arguments:
  871. Dpc
  872. DeviceObject
  873. Irp - not used
  874. // Context - not used
  875. Return Value:
  876. None.
  877. --*/
  878. {
  879. PFDO_EXTENSION deviceExtension = DeviceObject->DeviceExtension;
  880. INTERRUPT_CONTEXT interruptContext;
  881. INTERRUPT_DATA savedInterruptData;
  882. BOOLEAN callStartIo;
  883. PLOGICAL_UNIT_EXTENSION logicalUnit;
  884. PSRB_DATA srbData;
  885. LONG interlockResult;
  886. LARGE_INTEGER timeValue;
  887. PMDL mdl;
  888. LARGE_INTEGER timer;
  889. LogStartTime(TimeDpc, &timer);
  890. UNREFERENCED_PARAMETER(Dpc);
  891. //
  892. // Acquire the spinlock to protect flush adapter buffers information.
  893. //
  894. KeAcquireSpinLockAtDpcLevel(&deviceExtension->SpinLock);
  895. //
  896. // Get the interrupt state. This copies the interrupt state to the
  897. // saved state where it can be processed. It also clears the interrupt
  898. // flags.
  899. //
  900. interruptContext.DeviceExtension = deviceExtension;
  901. interruptContext.SavedInterruptData = &savedInterruptData;
  902. if (!KeSynchronizeExecution(deviceExtension->InterruptObject,
  903. IdeGetInterruptState,
  904. &interruptContext)) {
  905. //
  906. // There is no work to do so just return.
  907. //
  908. KeReleaseSpinLockFromDpcLevel(&deviceExtension->SpinLock);
  909. LogStopTime(TimeDpc, &timer, 0);
  910. return;
  911. }
  912. //
  913. // We only support one request at a time, so we can just check
  914. // the first completed request to determine whether we use DMA
  915. // and whether we need to flush DMA
  916. //
  917. if (savedInterruptData.CompletedRequests != NULL) {
  918. PSCSI_REQUEST_BLOCK srb;
  919. srbData = savedInterruptData.CompletedRequests;
  920. ASSERT(srbData->CurrentSrb);
  921. srb = srbData->CurrentSrb;
  922. if (srb->SrbFlags & SRB_FLAGS_UNSPECIFIED_DIRECTION) {
  923. if (SRB_USES_DMA(srb)) {
  924. deviceExtension->HwDeviceExtension->BusMasterInterface.BmFlush (
  925. deviceExtension->HwDeviceExtension->BusMasterInterface.Context
  926. );
  927. }
  928. }
  929. }
  930. //
  931. // check for empty channels
  932. //
  933. if (savedInterruptData.InterruptFlags & PD_ALL_DEVICE_MISSING) {
  934. PPDO_EXTENSION pdoExtension;
  935. IDE_PATH_ID pathId;
  936. ULONG errorCount;
  937. BOOLEAN rescanActive = FALSE;
  938. pathId.l = 0;
  939. while (pdoExtension = NextLogUnitExtensionWithTag (
  940. deviceExtension,
  941. &pathId,
  942. TRUE,
  943. IdePortCompletionDpc
  944. )) {
  945. KeAcquireSpinLockAtDpcLevel(&pdoExtension->PdoSpinLock);
  946. SETMASK (pdoExtension->PdoState, PDOS_DEADMEAT);
  947. IdeLogDeadMeatReason( pdoExtension->DeadmeatRecord.Reason,
  948. reportedMissing
  949. );
  950. if (pdoExtension->LuFlags & PD_RESCAN_ACTIVE) {
  951. rescanActive = TRUE;
  952. }
  953. KeReleaseSpinLockFromDpcLevel(&pdoExtension->PdoSpinLock);
  954. UnrefPdoWithTag(
  955. pdoExtension,
  956. IdePortCompletionDpc
  957. );
  958. }
  959. //
  960. // Don't ask for a rescan if you are in the middle of one.
  961. //
  962. if (!rescanActive) {
  963. IoInvalidateDeviceRelations (
  964. deviceExtension->AttacheePdo,
  965. BusRelations
  966. );
  967. } else {
  968. DebugPrint((1,
  969. "The device marked deadmeat during enumeration\n"
  970. ));
  971. }
  972. }
  973. //
  974. // Check for timer requests.
  975. //
  976. if (savedInterruptData.InterruptFlags & PD_TIMER_CALL_REQUEST) {
  977. //
  978. // The miniport wants a timer request. Save the timer parameters.
  979. //
  980. deviceExtension->HwTimerRequest = savedInterruptData.HwTimerRequest;
  981. //
  982. // If the requested timer value is zero, then cancel the timer.
  983. //
  984. if (savedInterruptData.MiniportTimerValue == 0) {
  985. KeCancelTimer(&deviceExtension->MiniPortTimer);
  986. } else {
  987. //
  988. // Convert the timer value from mircoseconds to a negative 100
  989. // nanoseconds.
  990. //
  991. timeValue.QuadPart = Int32x32To64(
  992. savedInterruptData.MiniportTimerValue,
  993. -10);
  994. //
  995. // Set the timer.
  996. //
  997. KeSetTimer(&deviceExtension->MiniPortTimer,
  998. timeValue,
  999. &deviceExtension->MiniPortTimerDpc);
  1000. }
  1001. }
  1002. if (savedInterruptData.InterruptFlags & PD_RESET_REQUEST) {
  1003. RESET_CONTEXT resetContext;
  1004. //
  1005. // clear the reset request
  1006. //
  1007. CLRMASK (savedInterruptData.InterruptFlags, PD_RESET_REQUEST);
  1008. //
  1009. // Request timed out.
  1010. //
  1011. resetContext.DeviceExtension = deviceExtension;
  1012. resetContext.PathId = 0;
  1013. resetContext.NewResetSequence = TRUE;
  1014. resetContext.ResetSrb = NULL;
  1015. if (!KeSynchronizeExecution(deviceExtension->InterruptObject,
  1016. IdeResetBusSynchronized,
  1017. &resetContext)) {
  1018. DebugPrint((DBG_WARNING,"IdePortCompletionDpc: Reset failed\n"));
  1019. }
  1020. }
  1021. //
  1022. // Verify that the ready for next request is ok.
  1023. //
  1024. if (savedInterruptData.InterruptFlags & PD_READY_FOR_NEXT_REQUEST) {
  1025. //
  1026. // If the device busy bit is not set, then this is a duplicate request.
  1027. // If a no disconnect request is executing, then don't call start I/O.
  1028. // This can occur when the miniport does a NextRequest followed by
  1029. // a NextLuRequest.
  1030. //
  1031. if ((deviceExtension->Flags & (PD_DEVICE_IS_BUSY | PD_DISCONNECT_RUNNING))
  1032. == (PD_DEVICE_IS_BUSY | PD_DISCONNECT_RUNNING)) {
  1033. //
  1034. // Clear the device busy flag. This flag is set by
  1035. // IdeStartIoSynchonized.
  1036. //
  1037. CLRMASK (deviceExtension->Flags, PD_DEVICE_IS_BUSY);
  1038. if (!(savedInterruptData.InterruptFlags & PD_RESET_HOLD)) {
  1039. //
  1040. // The miniport is ready for the next request and there is
  1041. // not a pending reset hold, so clear the port timer.
  1042. //
  1043. deviceExtension->PortTimeoutCounter = PD_TIMER_STOPPED;
  1044. }
  1045. } else {
  1046. //
  1047. // If a no disconnect request is executing, then clear the
  1048. // busy flag. When the disconnect request completes an
  1049. // IoStartNextPacket will be done.
  1050. //
  1051. CLRMASK (deviceExtension->Flags, PD_DEVICE_IS_BUSY);
  1052. //
  1053. // Clear the ready for next request flag.
  1054. //
  1055. CLRMASK (savedInterruptData.InterruptFlags, PD_READY_FOR_NEXT_REQUEST);
  1056. }
  1057. }
  1058. KeReleaseSpinLockFromDpcLevel(&deviceExtension->SpinLock);
  1059. //
  1060. // Free Access Token
  1061. //
  1062. if ((savedInterruptData.CompletedRequests != NULL) &&
  1063. (deviceExtension->SyncAccessInterface.FreeAccessToken)) {
  1064. (*deviceExtension->SyncAccessInterface.FreeAccessToken) (
  1065. deviceExtension->SyncAccessInterface.Token
  1066. );
  1067. }
  1068. //
  1069. // Check for a ready for next packet.
  1070. //
  1071. if (savedInterruptData.InterruptFlags & PD_READY_FOR_NEXT_REQUEST) {
  1072. //
  1073. // Start the next request.
  1074. //
  1075. IoStartNextPacket(deviceExtension->DeviceObject, FALSE);
  1076. }
  1077. //
  1078. // Check for an error log requests.
  1079. //
  1080. if (savedInterruptData.InterruptFlags & PD_LOG_ERROR) {
  1081. //
  1082. // Process the request.
  1083. //
  1084. LogErrorEntry(deviceExtension,
  1085. &savedInterruptData.LogEntry);
  1086. }
  1087. //
  1088. // Process any completed requests.
  1089. //
  1090. callStartIo = FALSE;
  1091. while (savedInterruptData.CompletedRequests != NULL) {
  1092. //
  1093. // Remove the request from the linked-list.
  1094. //
  1095. srbData = savedInterruptData.CompletedRequests;
  1096. savedInterruptData.CompletedRequests = srbData->CompletedRequests;
  1097. srbData->CompletedRequests = NULL;
  1098. //
  1099. // We only supports one request at a time
  1100. //
  1101. ASSERT (savedInterruptData.CompletedRequests == NULL);
  1102. //
  1103. // Stop the command log. The request sense will be logged as the next request.
  1104. //
  1105. IdeLogStopCommandLog(srbData);
  1106. IdeProcessCompletedRequest(deviceExtension,
  1107. srbData,
  1108. &callStartIo);
  1109. }
  1110. //
  1111. // Process any completed abort requests.
  1112. //
  1113. while (savedInterruptData.CompletedAbort != NULL) {
  1114. logicalUnit = savedInterruptData.CompletedAbort;
  1115. //
  1116. // Remove request from the completed abort list.
  1117. //
  1118. savedInterruptData.CompletedAbort = logicalUnit->CompletedAbort;
  1119. //
  1120. // Acquire the spinlock to protect the flags structure,
  1121. // and the free of the srb extension.
  1122. //
  1123. KeAcquireSpinLockAtDpcLevel(&deviceExtension->SpinLock);
  1124. //
  1125. // Note the timer which was started for the abort request is not
  1126. // stopped by the get interrupt routine. Rather the timer is stopped.
  1127. // when the aborted request completes.
  1128. //
  1129. Irp = logicalUnit->AbortSrb->OriginalRequest;
  1130. //
  1131. // Set IRP status. Class drivers will reset IRP status based
  1132. // on request sense if error.
  1133. //
  1134. if (SRB_STATUS(logicalUnit->AbortSrb->SrbStatus) == SRB_STATUS_SUCCESS) {
  1135. Irp->IoStatus.Status = STATUS_SUCCESS;
  1136. } else {
  1137. Irp->IoStatus.Status = IdeTranslateSrbStatus(logicalUnit->AbortSrb);
  1138. }
  1139. Irp->IoStatus.Information = 0;
  1140. //
  1141. // Clear the abort request pointer.
  1142. //
  1143. logicalUnit->AbortSrb = NULL;
  1144. KeReleaseSpinLockFromDpcLevel(&deviceExtension->SpinLock);
  1145. UnrefLogicalUnitExtensionWithTag(
  1146. deviceExtension,
  1147. IDEPORT_GET_LUNEXT_IN_IRP(IoGetCurrentIrpStackLocation(Irp)),
  1148. Irp
  1149. );
  1150. IoCompleteRequest(Irp, IO_DISK_INCREMENT);
  1151. }
  1152. //
  1153. // Call the start I/O routine if necessary.
  1154. //
  1155. if (callStartIo) {
  1156. ASSERT(DeviceObject->CurrentIrp != NULL);
  1157. IdePortStartIo(DeviceObject, DeviceObject->CurrentIrp);
  1158. }
  1159. //
  1160. // Check for reset
  1161. //
  1162. if (savedInterruptData.InterruptFlags & PD_RESET_REPORTED) {
  1163. //
  1164. // we had a bus reset. everyone on the bus should be in PowerDeviceD0
  1165. //
  1166. IDE_PATH_ID pathId;
  1167. PPDO_EXTENSION pdoExtension;
  1168. POWER_STATE powerState;
  1169. pathId.l = 0;
  1170. powerState.DeviceState = PowerDeviceD0;
  1171. while (pdoExtension = NextLogUnitExtensionWithTag (
  1172. deviceExtension,
  1173. &pathId,
  1174. FALSE,
  1175. IdePortCompletionDpc
  1176. )) {
  1177. //
  1178. // If rescan is active, the pdo might go away
  1179. //
  1180. if (pdoExtension != savedInterruptData.PdoExtensionResetBus &&
  1181. !(pdoExtension->LuFlags & PD_RESCAN_ACTIVE)) {
  1182. PoRequestPowerIrp (
  1183. pdoExtension->DeviceObject,
  1184. IRP_MN_SET_POWER,
  1185. powerState,
  1186. NULL,
  1187. NULL,
  1188. NULL
  1189. );
  1190. }
  1191. UnrefLogicalUnitExtensionWithTag (
  1192. deviceExtension,
  1193. pdoExtension,
  1194. IdePortCompletionDpc
  1195. );
  1196. }
  1197. }
  1198. LogStopTime(TimeDpc, &timer, 0);
  1199. return;
  1200. } // end IdePortCompletionDpc()
  1201. #ifdef IDEDEBUG_TEST_START_STOP_DEVICE
  1202. typedef enum {
  1203. IdeDebugStartStop_Idle=0,
  1204. IdeDebugStartStop_StopPending,
  1205. IdeDebugStartStop_Stopped,
  1206. IdeDebugStartStop_StartPending,
  1207. IdeDebugStartStop_Started,
  1208. IdeDebugStartStop_LastState
  1209. } IDEDEBUG_STARTSTOP_STATE;
  1210. PDEVICE_OBJECT IdeDebugStartStopDeviceObject=NULL;
  1211. IDEDEBUG_STARTSTOP_STATE IdeDebugStartStopState = IdeDebugStartStop_Idle;
  1212. IDEDEBUG_STARTSTOP_STATE IdeDebugStartStopTimer = 0;
  1213. PDEVICE_OBJECT
  1214. IoGetAttachedDevice(
  1215. IN PDEVICE_OBJECT DeviceObject
  1216. );
  1217. NTSTATUS
  1218. IdeDebugSynchronousCallCompletionRoutine(
  1219. IN PDEVICE_OBJECT DeviceObject,
  1220. IN OUT PIRP Irp,
  1221. IN OUT PVOID Context
  1222. )
  1223. {
  1224. PKEVENT event = Context;
  1225. *(Irp->UserIosb) = Irp->IoStatus;
  1226. KeSetEvent( event, IO_NO_INCREMENT, FALSE );
  1227. IoFreeIrp (Irp);
  1228. return STATUS_MORE_PROCESSING_REQUIRED;
  1229. }
  1230. NTSTATUS
  1231. IdeDebugSynchronousCall(
  1232. IN PDEVICE_OBJECT DeviceObject,
  1233. IN PIO_STACK_LOCATION TopStackLocation
  1234. )
  1235. /*++
  1236. Routine Description:
  1237. This function sends a synchronous irp to the top level device
  1238. object which roots on DeviceObject.
  1239. Parameters:
  1240. DeviceObject - Supplies the device object of the device being removed.
  1241. TopStackLocation - Supplies a pointer to the parameter block for the irp.
  1242. Return Value:
  1243. NTSTATUS code.
  1244. --*/
  1245. {
  1246. PIRP irp;
  1247. PIO_STACK_LOCATION irpSp;
  1248. IO_STATUS_BLOCK statusBlock;
  1249. KEVENT event;
  1250. NTSTATUS status;
  1251. PDEVICE_OBJECT deviceObject;
  1252. PAGED_CODE();
  1253. //
  1254. // Get a pointer to the topmost device object in the stack of devices,
  1255. // beginning with the deviceObject.
  1256. //
  1257. deviceObject = IoGetAttachedDevice(DeviceObject);
  1258. //
  1259. // Begin by allocating the IRP for this request. Do not charge quota to
  1260. // the current process for this IRP.
  1261. //
  1262. irp = IoAllocateIrp(deviceObject->StackSize, FALSE);
  1263. if (irp == NULL){
  1264. return STATUS_INSUFFICIENT_RESOURCES;
  1265. }
  1266. //
  1267. // Initialize it to failure.
  1268. //
  1269. irp->IoStatus.Status = statusBlock.Status = STATUS_NOT_SUPPORTED;
  1270. irp->IoStatus.Information = statusBlock.Information = 0;
  1271. irp->UserIosb = &statusBlock;
  1272. //
  1273. // Set the pointer to the status block and initialized event.
  1274. //
  1275. KeInitializeEvent( &event,
  1276. SynchronizationEvent,
  1277. FALSE );
  1278. //
  1279. // Set the address of the current thread
  1280. //
  1281. irp->Tail.Overlay.Thread = PsGetCurrentThread();
  1282. //
  1283. // Get a pointer to the stack location of the first driver which will be
  1284. // invoked. This is where the function codes and parameters are set.
  1285. //
  1286. irpSp = IoGetNextIrpStackLocation(irp);
  1287. //
  1288. // Copy in the caller-supplied stack location contents
  1289. //
  1290. *irpSp = *TopStackLocation;
  1291. IoSetCompletionRoutine(
  1292. irp,
  1293. IdeDebugSynchronousCallCompletionRoutine,
  1294. &event,
  1295. TRUE,
  1296. TRUE,
  1297. TRUE
  1298. );
  1299. //
  1300. // Call the driver
  1301. //
  1302. status = IoCallDriver(DeviceObject, irp);
  1303. //
  1304. // If a driver returns STATUS_PENDING, we will wait for it to complete
  1305. //
  1306. if (status == STATUS_PENDING) {
  1307. (VOID) KeWaitForSingleObject( &event,
  1308. Executive,
  1309. KernelMode,
  1310. FALSE,
  1311. (PLARGE_INTEGER) NULL );
  1312. status = statusBlock.Status;
  1313. }
  1314. return status;
  1315. }
  1316. NTSTATUS
  1317. IdeDebugStartStopWorkRoutine (
  1318. IN PDEVICE_OBJECT DeviceObject,
  1319. IN PIO_WORKITEM WorkItem
  1320. )
  1321. {
  1322. NTSTATUS status;
  1323. IO_STACK_LOCATION irpSp;
  1324. PVOID dummy;
  1325. RtlZeroMemory(&irpSp, sizeof(IO_STACK_LOCATION));
  1326. irpSp.MajorFunction = IRP_MJ_PNP;
  1327. //
  1328. // release resource for this worker item
  1329. //
  1330. IoFreeWorkItem(WorkItem);
  1331. if (IdeDebugStartStopDeviceObject) {
  1332. if (IdeDebugStartStopState == IdeDebugStartStop_StopPending) {
  1333. irpSp.MinorFunction = IRP_MN_STOP_DEVICE;
  1334. status = IdeDebugSynchronousCall(DeviceObject, &irpSp);
  1335. if (!NT_SUCCESS(status)) {
  1336. DbgBreakPoint();
  1337. }
  1338. IdeDebugStartStopTimer = 0;
  1339. IdeDebugStartStopState = IdeDebugStartStop_Stopped;
  1340. } else if (IdeDebugStartStopState == IdeDebugStartStop_StartPending) {
  1341. // this will only work with legacy ide channels enmerated by pciidex.sys
  1342. irpSp.MinorFunction = IRP_MN_START_DEVICE;
  1343. status =IdeDebugSynchronousCall(DeviceObject, &irpSp);
  1344. if (!NT_SUCCESS(status)) {
  1345. DbgBreakPoint();
  1346. }
  1347. IdeDebugStartStopTimer = 0;
  1348. IdeDebugStartStopState = IdeDebugStartStop_Started;
  1349. } else {
  1350. DbgBreakPoint();
  1351. }
  1352. }
  1353. return STATUS_SUCCESS;
  1354. }
  1355. #endif //IDEDEBUG_TEST_START_STOP_DEVICE
  1356. #ifdef DPC_FOR_EMPTY_CHANNEL
  1357. BOOLEAN
  1358. IdeCheckEmptyChannel(
  1359. IN PVOID ServiceContext
  1360. )
  1361. {
  1362. ULONG status;
  1363. PSCSI_REQUEST_BLOCK Srb;
  1364. PDEVICE_OBJECT deviceObject = ServiceContext;
  1365. PFDO_EXTENSION deviceExtension = deviceObject->DeviceExtension;
  1366. PHW_DEVICE_EXTENSION hwDeviceExtension = deviceExtension->HwDeviceExtension;
  1367. if ((status=IdePortChannelEmptyQuick(&hwDeviceExtension->BaseIoAddress1, &hwDeviceExtension->BaseIoAddress2,
  1368. hwDeviceExtension->MaxIdeDevice, &hwDeviceExtension->CurrentIdeDevice,
  1369. &hwDeviceExtension->MoreWait, &hwDeviceExtension->NoRetry))!= STATUS_RETRY) {
  1370. //
  1371. // Clear current SRB.
  1372. //
  1373. Srb=hwDeviceExtension->CurrentSrb;
  1374. hwDeviceExtension->CurrentSrb = NULL;
  1375. //
  1376. // Set status in SRB.
  1377. //
  1378. if (status == 1) {
  1379. Srb->SrbStatus = (UCHAR) SRB_STATUS_SUCCESS;
  1380. } else {
  1381. Srb->SrbStatus = (UCHAR) SRB_STATUS_ERROR;
  1382. }
  1383. //
  1384. // Clear all the variables
  1385. //
  1386. hwDeviceExtension->MoreWait=0;
  1387. hwDeviceExtension->CurrentIdeDevice=0;
  1388. hwDeviceExtension->NoRetry=0;
  1389. //
  1390. // Indicate command complete.
  1391. //
  1392. IdePortNotification(IdeRequestComplete,
  1393. hwDeviceExtension,
  1394. Srb);
  1395. //
  1396. // Indicate ready for next request.
  1397. //
  1398. IdePortNotification(IdeNextRequest,
  1399. hwDeviceExtension,
  1400. NULL);
  1401. IoRequestDpc(deviceObject, NULL, NULL);
  1402. return TRUE;
  1403. }
  1404. return FALSE;
  1405. }
  1406. #endif
  1407. VOID
  1408. IdePortTickHandler(
  1409. IN PDEVICE_OBJECT DeviceObject,
  1410. IN PVOID Context
  1411. )
  1412. /*++
  1413. Routine Description:
  1414. Arguments:
  1415. Return Value:
  1416. None.
  1417. --*/
  1418. {
  1419. PFDO_EXTENSION deviceExtension =
  1420. (PFDO_EXTENSION) DeviceObject->DeviceExtension;
  1421. PLOGICAL_UNIT_EXTENSION logicalUnit;
  1422. ULONG target;
  1423. IDE_PATH_ID pathId;
  1424. UNREFERENCED_PARAMETER(Context);
  1425. #if DBG
  1426. if (IdeDebugRescanBusFreq) {
  1427. IdeDebugRescanBusCounter++;
  1428. if (IdeDebugRescanBusCounter == IdeDebugRescanBusFreq) {
  1429. IoInvalidateDeviceRelations (
  1430. deviceExtension->AttacheePdo,
  1431. BusRelations
  1432. );
  1433. IdeDebugRescanBusCounter = 0;
  1434. }
  1435. }
  1436. #endif //DBG
  1437. #ifdef IDEDEBUG_TEST_START_STOP_DEVICE
  1438. if (deviceExtension->LogicalUnitList[0] &&
  1439. (IdeDebugStartStopDeviceObject == deviceExtension->LogicalUnitList[0]->DeviceObject)) {
  1440. PIO_WORKITEM workItem;
  1441. if (IdeDebugStartStopState == IdeDebugStartStop_Idle) {
  1442. IdeDebugStartStopState = IdeDebugStartStop_StopPending;
  1443. workItem = IoAllocateWorkItem(IdeDebugStartStopDeviceObject);
  1444. IoQueueWorkItem(
  1445. workItem,
  1446. IdeDebugStartStopWorkRoutine,
  1447. DelayedWorkQueue,
  1448. workItem
  1449. );
  1450. } else if (IdeDebugStartStopState == IdeDebugStartStop_Stopped) {
  1451. if (IdeDebugStartStopTimer > 5) {
  1452. IdeDebugStartStopState = IdeDebugStartStop_StartPending;
  1453. workItem = IoAllocateWorkItem(IdeDebugStartStopDeviceObject);
  1454. IoQueueWorkItem(
  1455. workItem,
  1456. IdeDebugStartStopWorkRoutine,
  1457. HyperCriticalWorkQueue,
  1458. workItem
  1459. );
  1460. } else {
  1461. IdeDebugStartStopTimer++;
  1462. }
  1463. } else if (IdeDebugStartStopState == IdeDebugStartStop_Started) {
  1464. if (IdeDebugStartStopTimer > 10) {
  1465. IdeDebugStartStopState = IdeDebugStartStop_Idle;
  1466. } else {
  1467. IdeDebugStartStopTimer++;
  1468. }
  1469. }
  1470. }
  1471. #endif // IDEDEBUG_TEST_START_STOP_DEVICE
  1472. //
  1473. // Acquire the spinlock to protect the flags structure.
  1474. //
  1475. KeAcquireSpinLockAtDpcLevel(&deviceExtension->SpinLock);
  1476. #ifdef DPC_FOR_EMPTY_CHANNEL
  1477. //
  1478. //Holding the lock is OK.
  1479. //The empty channel check is quick
  1480. //
  1481. if (deviceExtension->HwDeviceExtension->MoreWait) {
  1482. if (!KeSynchronizeExecution (
  1483. deviceExtension->InterruptObject,
  1484. IdeCheckEmptyChannel,
  1485. DeviceObject
  1486. )) {
  1487. DebugPrint((0,"ATAPI: ChannelEmpty check- device busy after 1sec\n"));
  1488. }
  1489. KeReleaseSpinLockFromDpcLevel(&deviceExtension->SpinLock);
  1490. return;
  1491. }
  1492. #endif
  1493. //
  1494. // Check for port timeouts.
  1495. //
  1496. if (deviceExtension->ResetCallAgain) {
  1497. RESET_CONTEXT resetContext;
  1498. //
  1499. // Request timed out.
  1500. //
  1501. resetContext.DeviceExtension = deviceExtension;
  1502. resetContext.PathId = 0;
  1503. resetContext.NewResetSequence = FALSE;
  1504. resetContext.ResetSrb = NULL;
  1505. if (!KeSynchronizeExecution(deviceExtension->InterruptObject,
  1506. IdeResetBusSynchronized,
  1507. &resetContext)) {
  1508. DebugPrint((0,"IdePortTickHanlder: Reset failed\n"));
  1509. }
  1510. KeReleaseSpinLockFromDpcLevel(&deviceExtension->SpinLock);
  1511. return;
  1512. }
  1513. if (deviceExtension->PortTimeoutCounter > 0) {
  1514. if (--deviceExtension->PortTimeoutCounter == 0) {
  1515. //
  1516. // Process the port timeout.
  1517. //
  1518. if (deviceExtension->InterruptObject) {
  1519. if (KeSynchronizeExecution(deviceExtension->InterruptObject,
  1520. IdeTimeoutSynchronized,
  1521. deviceExtension->DeviceObject)){
  1522. //
  1523. // Log error if IdeTimeoutSynchonized indicates this was an error
  1524. // timeout.
  1525. //
  1526. if (deviceExtension->DeviceObject->CurrentIrp) {
  1527. IdeLogTimeoutError(deviceExtension,
  1528. deviceExtension->DeviceObject->CurrentIrp,
  1529. 256);
  1530. }
  1531. }
  1532. } else {
  1533. PIRP irp = deviceExtension->DeviceObject->CurrentIrp;
  1534. DebugPrint((0,
  1535. "The device was suprise removed with an active request\n"
  1536. ));
  1537. //
  1538. // the device was probably surprise removed. Complete
  1539. // the request with status_no_such_device
  1540. //
  1541. if (irp) {
  1542. PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(irp);
  1543. PSCSI_REQUEST_BLOCK srb = irpStack->Parameters.Scsi.Srb;
  1544. srb->SrbStatus = SRB_STATUS_NO_DEVICE;
  1545. irp->IoStatus.Status = STATUS_NO_SUCH_DEVICE;
  1546. UnrefLogicalUnitExtensionWithTag (
  1547. deviceExtension,
  1548. IDEPORT_GET_LUNEXT_IN_IRP(irpStack),
  1549. irp
  1550. );
  1551. IoCompleteRequest(irp, IO_NO_INCREMENT);
  1552. }
  1553. }
  1554. }
  1555. //
  1556. // check for busy Luns and restart its request
  1557. //
  1558. pathId.l = 0;
  1559. while (logicalUnit = NextLogUnitExtensionWithTag(
  1560. deviceExtension,
  1561. &pathId,
  1562. TRUE,
  1563. IdePortTickHandler
  1564. )) {
  1565. AtapiRestartBusyRequest(deviceExtension, logicalUnit);
  1566. UnrefLogicalUnitExtensionWithTag (
  1567. deviceExtension,
  1568. logicalUnit,
  1569. IdePortTickHandler
  1570. );
  1571. }
  1572. KeReleaseSpinLockFromDpcLevel(&deviceExtension->SpinLock);
  1573. //
  1574. // Since a port timeout has been done. Skip the rest of the
  1575. // processing.
  1576. //
  1577. return;
  1578. }
  1579. //
  1580. // Scan each of the logical units. If it has an active request then
  1581. // decrement the timeout value and process a timeout if it is zero.
  1582. //
  1583. pathId.l = 0;
  1584. while (logicalUnit = NextLogUnitExtensionWithTag(
  1585. deviceExtension,
  1586. &pathId,
  1587. TRUE,
  1588. IdePortTickHandler
  1589. )) {
  1590. //
  1591. // Check for busy requests.
  1592. //
  1593. if (AtapiRestartBusyRequest (deviceExtension, logicalUnit)) {
  1594. //
  1595. // this lun was marked busy
  1596. // skip all other checks
  1597. //
  1598. } else if (logicalUnit->RequestTimeoutCounter == 0) {
  1599. RESET_CONTEXT resetContext;
  1600. //
  1601. // Request timed out.
  1602. //
  1603. logicalUnit->RequestTimeoutCounter = PD_TIMER_STOPPED;
  1604. DebugPrint((1,"IdePortTickHandler: Request timed out\n"));
  1605. resetContext.DeviceExtension = deviceExtension;
  1606. resetContext.PathId = logicalUnit->PathId;
  1607. resetContext.NewResetSequence = TRUE;
  1608. resetContext.ResetSrb = NULL;
  1609. if (deviceExtension->InterruptObject) {
  1610. if (!KeSynchronizeExecution(deviceExtension->InterruptObject,
  1611. IdeResetBusSynchronized,
  1612. &resetContext)) {
  1613. DebugPrint((1,"IdePortTickHanlder: Reset failed\n"));
  1614. } else {
  1615. //
  1616. // Log the reset.
  1617. //
  1618. IdeLogResetError( deviceExtension,
  1619. logicalUnit->SrbData.CurrentSrb,
  1620. ('P'<<24) | 257);
  1621. }
  1622. }
  1623. } else if (logicalUnit->RequestTimeoutCounter > 0) {
  1624. //
  1625. // Decrement timeout count.
  1626. //
  1627. logicalUnit->RequestTimeoutCounter--;
  1628. }
  1629. UnrefLogicalUnitExtensionWithTag (
  1630. deviceExtension,
  1631. logicalUnit,
  1632. IdePortTickHandler
  1633. );
  1634. }
  1635. KeReleaseSpinLockFromDpcLevel(&deviceExtension->SpinLock);
  1636. return;
  1637. } // end IdePortTickHandler()
  1638. BOOLEAN
  1639. AtapiRestartBusyRequest (
  1640. PFDO_EXTENSION DeviceExtension,
  1641. PPDO_EXTENSION LogicalUnit
  1642. )
  1643. {
  1644. PIRP irp;
  1645. PIO_STACK_LOCATION irpStack;
  1646. PSCSI_REQUEST_BLOCK srb;
  1647. //
  1648. // Check for busy requests.
  1649. //
  1650. if (LogicalUnit->LuFlags & PD_LOGICAL_UNIT_IS_BUSY) {
  1651. //
  1652. // If a request sense is needed or the queue is
  1653. // frozen, defer processing this busy request until
  1654. // that special processing has completed. This prevents
  1655. // a random busy request from being started when a REQUEST
  1656. // SENSE needs to be sent.
  1657. //
  1658. if (!(LogicalUnit->LuFlags &
  1659. (PD_NEED_REQUEST_SENSE | PD_QUEUE_FROZEN))) {
  1660. DebugPrint((1,"IdePortTickHandler: Retrying busy status request\n"));
  1661. //
  1662. // Clear the busy flag and retry the request. Release the
  1663. // spinlock while the call to IoStartPacket is made.
  1664. //
  1665. CLRMASK (LogicalUnit->LuFlags, PD_LOGICAL_UNIT_IS_BUSY | PD_QUEUE_IS_FULL);
  1666. irp = LogicalUnit->BusyRequest;
  1667. //
  1668. // Clear the busy request.
  1669. //
  1670. LogicalUnit->BusyRequest = NULL;
  1671. //
  1672. // check if the device is gone
  1673. //
  1674. if (LogicalUnit->PdoState & (PDOS_SURPRISE_REMOVED | PDOS_REMOVED)) {
  1675. irpStack = IoGetCurrentIrpStackLocation(irp);
  1676. srb = irpStack->Parameters.Scsi.Srb;
  1677. srb->SrbStatus = SRB_STATUS_NO_DEVICE;
  1678. irp->IoStatus.Status = STATUS_NO_SUCH_DEVICE;
  1679. //
  1680. // Decrement the logUnitExtension reference count
  1681. //
  1682. UnrefLogicalUnitExtensionWithTag(
  1683. DeviceExtension,
  1684. LogicalUnit,
  1685. irp
  1686. );
  1687. IoCompleteRequest(irp, IO_NO_INCREMENT);
  1688. return TRUE;
  1689. }
  1690. KeReleaseSpinLockFromDpcLevel(&DeviceExtension->SpinLock);
  1691. IoStartPacket(DeviceExtension->DeviceObject, irp, (PULONG)NULL, NULL);
  1692. KeAcquireSpinLockAtDpcLevel(&DeviceExtension->SpinLock);
  1693. }
  1694. return TRUE;
  1695. } else {
  1696. return FALSE;
  1697. }
  1698. }
  1699. NTSTATUS
  1700. IdePortDeviceControl(
  1701. IN PDEVICE_OBJECT DeviceObject,
  1702. IN PIRP Irp
  1703. )
  1704. /*++
  1705. Routine Description:
  1706. This routine is the device control dispatcher.
  1707. Arguments:
  1708. DeviceObject
  1709. Irp
  1710. Return Value:
  1711. NTSTATUS
  1712. --*/
  1713. {
  1714. PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(Irp);
  1715. PFDO_EXTENSION deviceExtension = DeviceObject->DeviceExtension;
  1716. UCHAR scsiBus;
  1717. NTSTATUS status;
  1718. ULONG j;
  1719. //
  1720. // Initialize the information field.
  1721. //
  1722. Irp->IoStatus.Information = 0;
  1723. switch (irpStack->Parameters.DeviceIoControl.IoControlCode) {
  1724. //
  1725. // Get adapter capabilities.
  1726. //
  1727. case IOCTL_SCSI_GET_CAPABILITIES:
  1728. if (irpStack->Parameters.DeviceIoControl.OutputBufferLength
  1729. < sizeof(IO_SCSI_CAPABILITIES)) {
  1730. status = STATUS_BUFFER_TOO_SMALL;
  1731. break;
  1732. }
  1733. //
  1734. // this is dynamic
  1735. //
  1736. deviceExtension->Capabilities.AdapterUsesPio = FALSE;
  1737. for (j=0; j<deviceExtension->HwDeviceExtension->MaxIdeDevice; j++) {
  1738. deviceExtension->Capabilities.AdapterUsesPio |=
  1739. !(deviceExtension->HwDeviceExtension->DeviceFlags[j] & DFLAGS_USE_DMA);
  1740. }
  1741. RtlCopyMemory(Irp->AssociatedIrp.SystemBuffer,
  1742. &deviceExtension->Capabilities,
  1743. sizeof(IO_SCSI_CAPABILITIES));
  1744. Irp->IoStatus.Information = sizeof(IO_SCSI_CAPABILITIES);
  1745. status = STATUS_SUCCESS;
  1746. break;
  1747. case IOCTL_SCSI_PASS_THROUGH:
  1748. status = IdeSendScsiPassThrough(deviceExtension, Irp, FALSE);
  1749. break;
  1750. case IOCTL_SCSI_PASS_THROUGH_DIRECT:
  1751. status = IdeSendScsiPassThrough(deviceExtension, Irp, TRUE);
  1752. break;
  1753. case IOCTL_SCSI_MINIPORT:
  1754. status = IdeSendMiniPortIoctl( deviceExtension, Irp);
  1755. break;
  1756. case IOCTL_SCSI_GET_INQUIRY_DATA:
  1757. status = IdeGetInquiryData(deviceExtension, Irp);
  1758. break;
  1759. case IOCTL_SCSI_RESCAN_BUS:
  1760. //
  1761. // should return only after we get the device relation irp
  1762. // this will be fixed if needed.
  1763. //
  1764. IoInvalidateDeviceRelations (
  1765. deviceExtension->AttacheePdo,
  1766. BusRelations
  1767. );
  1768. status = STATUS_SUCCESS;
  1769. break;
  1770. default:
  1771. return ChannelDeviceIoControl (DeviceObject, Irp);
  1772. break;
  1773. } // end switch
  1774. //
  1775. // Set status in Irp.
  1776. //
  1777. Irp->IoStatus.Status = status;
  1778. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  1779. return status;
  1780. } // end IdePortDeviceControl()
  1781. BOOLEAN
  1782. IdeStartIoSynchronized (
  1783. PVOID ServiceContext
  1784. )
  1785. /*++
  1786. Routine Description:
  1787. This routine calls the dependent driver start io routine.
  1788. It also starts the request timer for the logical unit if necesary and
  1789. inserts the SRB data structure in to the requset list.
  1790. Arguments:
  1791. ServiceContext - Supplies the pointer to the device object.
  1792. Return Value:
  1793. Returns the value returned by the dependent start I/O routine.
  1794. Notes:
  1795. The port driver spinlock must be held when this routine is called.
  1796. --*/
  1797. {
  1798. PDEVICE_OBJECT deviceObject = ServiceContext;
  1799. PFDO_EXTENSION deviceExtension = deviceObject->DeviceExtension;
  1800. PIO_STACK_LOCATION irpStack;
  1801. PLOGICAL_UNIT_EXTENSION logicalUnit;
  1802. PSCSI_REQUEST_BLOCK srb;
  1803. PSRB_DATA srbData;
  1804. BOOLEAN timerStarted;
  1805. BOOLEAN returnValue;
  1806. BOOLEAN resetRequest;
  1807. DebugPrint((3, "IdePortStartIoSynchronized: Enter routine\n"));
  1808. irpStack = IoGetCurrentIrpStackLocation(deviceObject->CurrentIrp);
  1809. srb = irpStack->Parameters.Scsi.Srb;
  1810. //
  1811. // Get the logical unit extension.
  1812. //
  1813. logicalUnit = IDEPORT_GET_LUNEXT_IN_IRP (irpStack);
  1814. //
  1815. // Check for a reset hold. If one is in progress then flag it and return.
  1816. // The timer will reset the current request. This check should be made
  1817. // before anything else is done.
  1818. //
  1819. if (deviceExtension->InterruptData.InterruptFlags & PD_RESET_HOLD) {
  1820. DebugPrint ((1, "IdeStartIoSynchronized: PD_RESET_HOLD set...request is held for later..\n"));
  1821. deviceExtension->InterruptData.InterruptFlags |= PD_HELD_REQUEST;
  1822. return(TRUE);
  1823. }
  1824. if ((((srb->Function == SRB_FUNCTION_ATA_PASS_THROUGH) ||
  1825. (srb->Function == SRB_FUNCTION_ATA_POWER_PASS_THROUGH)) &&
  1826. (((PATA_PASS_THROUGH) (srb->DataBuffer))->IdeReg.bReserved & ATA_PTFLAGS_BUS_RESET))) {
  1827. resetRequest = TRUE;
  1828. } else {
  1829. resetRequest = FALSE;
  1830. }
  1831. //
  1832. // Start the port timer. This ensures that the miniport asks for
  1833. // the next request in a resonable amount of time. Set the device
  1834. // busy flag to indicate it is ok to start the next request.
  1835. //
  1836. deviceExtension->PortTimeoutCounter = srb->TimeOutValue;
  1837. deviceExtension->Flags |= PD_DEVICE_IS_BUSY;
  1838. //
  1839. // Start the logical unit timer if it is not currently running.
  1840. //
  1841. if (logicalUnit->RequestTimeoutCounter == PD_TIMER_STOPPED) {
  1842. //
  1843. // Set request timeout value from Srb SCSI extension in Irp.
  1844. //
  1845. logicalUnit->RequestTimeoutCounter = srb->TimeOutValue;
  1846. timerStarted = TRUE;
  1847. } else {
  1848. timerStarted = FALSE;
  1849. }
  1850. //
  1851. // Indicate that there maybe more requests queued, if this is not a bypass
  1852. // request.
  1853. //
  1854. if (!(srb->SrbFlags & SRB_FLAGS_BYPASS_FROZEN_QUEUE)) {
  1855. if (srb->SrbFlags & SRB_FLAGS_DISABLE_DISCONNECT) {
  1856. //
  1857. // This request does not allow disconnects. Remember that so
  1858. // no more requests are started until this one completes.
  1859. //
  1860. CLRMASK (deviceExtension->Flags, PD_DISCONNECT_RUNNING);
  1861. }
  1862. logicalUnit->LuFlags |= PD_LOGICAL_UNIT_IS_ACTIVE;
  1863. } else {
  1864. //
  1865. // If this is an abort request make sure that it still looks valid.
  1866. //
  1867. if (srb->Function == SRB_FUNCTION_ABORT_COMMAND) {
  1868. srbData = IdeGetSrbData(deviceExtension, srb);
  1869. //
  1870. // Make sure the srb request is still active.
  1871. //
  1872. if (srbData == NULL || srbData->CurrentSrb == NULL
  1873. || !(srbData->CurrentSrb->SrbFlags & SRB_FLAGS_IS_ACTIVE)) {
  1874. //
  1875. // Mark the Srb as active.
  1876. //
  1877. srb->SrbFlags |= SRB_FLAGS_IS_ACTIVE;
  1878. if (timerStarted) {
  1879. logicalUnit->RequestTimeoutCounter = PD_TIMER_STOPPED;
  1880. }
  1881. //
  1882. // The request is gone.
  1883. //
  1884. DebugPrint((1, "IdePortStartIO: Request completed be for it was aborted.\n"));
  1885. srb->SrbStatus = SRB_STATUS_ABORT_FAILED;
  1886. IdePortNotification(IdeRequestComplete,
  1887. deviceExtension + 1,
  1888. srb);
  1889. IdePortNotification(IdeNextRequest,
  1890. deviceExtension + 1);
  1891. //
  1892. // Queue a DPC to process the work that was just indicated.
  1893. //
  1894. IoRequestDpc(deviceExtension->DeviceObject, NULL, NULL);
  1895. return(TRUE);
  1896. }
  1897. }
  1898. //
  1899. // Any untagged request that bypasses the queue
  1900. // clears the need request sense flag.
  1901. //
  1902. CLRMASK (logicalUnit->LuFlags, PD_NEED_REQUEST_SENSE);
  1903. if (srb->SrbFlags & SRB_FLAGS_DISABLE_DISCONNECT) {
  1904. //
  1905. // This request does not allow disconnects. Remember that so
  1906. // no more requests are started until this one completes.
  1907. //
  1908. CLRMASK (deviceExtension->Flags, PD_DISCONNECT_RUNNING);
  1909. }
  1910. //
  1911. // Set the timeout value in the logical unit.
  1912. //
  1913. logicalUnit->RequestTimeoutCounter = srb->TimeOutValue;
  1914. }
  1915. //
  1916. // Mark the Srb as active.
  1917. //
  1918. srb->SrbFlags |= SRB_FLAGS_IS_ACTIVE;
  1919. #if 0
  1920. //joedai
  1921. {
  1922. ULONG c;
  1923. PUCHAR s;
  1924. PUCHAR d;
  1925. s = (PUCHAR) deviceObject->CurrentIrp;
  1926. d = (PUCHAR) &deviceExtension->debugData[deviceExtension->nextEntry].irp;
  1927. deviceExtension->debugDataPtr[deviceExtension->nextEntry].irp = (PIRP) d;
  1928. for (c=0; c<sizeof(IRP); c++) {
  1929. d[c] = s[c];
  1930. }
  1931. if (deviceObject->CurrentIrp->MdlAddress) {
  1932. s = (PUCHAR) deviceObject->CurrentIrp->MdlAddress;
  1933. d = (PUCHAR) &deviceExtension->debugData[deviceExtension->nextEntry].mdl;
  1934. deviceExtension->debugDataPtr[deviceExtension->nextEntry].mdl = (PMDL) d;
  1935. for (c=0; c<sizeof(MDL); c++) {
  1936. d[c] = s[c];
  1937. }
  1938. } else {
  1939. d = (PUCHAR) &deviceExtension->debugData[deviceExtension->nextEntry].mdl;
  1940. deviceExtension->debugDataPtr[deviceExtension->nextEntry].mdl = (PMDL) d;
  1941. for (c=0; c<sizeof(MDL); c++) {
  1942. d[c] = 0;
  1943. }
  1944. }
  1945. s = (PUCHAR) srb;
  1946. d = (PUCHAR) &deviceExtension->debugData[deviceExtension->nextEntry].srb;
  1947. deviceExtension->debugDataPtr[deviceExtension->nextEntry].srb = (PSCSI_REQUEST_BLOCK) d;
  1948. for (c=0; c<sizeof(SCSI_REQUEST_BLOCK); c++) {
  1949. d[c] = s[c];
  1950. }
  1951. ASSERT((((ULONG)srb->DataBuffer) & 0x80000000));
  1952. deviceExtension->nextEntry = (deviceExtension->nextEntry + 1) % NUM_DEBUG_ENTRY;
  1953. }
  1954. #endif
  1955. //
  1956. // maybe the device is gone
  1957. //
  1958. if (logicalUnit->PdoState & PDOS_DEADMEAT) {
  1959. srb->SrbStatus = SRB_STATUS_NO_DEVICE;
  1960. IdePortNotification(IdeRequestComplete,
  1961. deviceExtension + 1,
  1962. srb);
  1963. IdePortNotification(IdeNextRequest,
  1964. deviceExtension + 1);
  1965. IoRequestDpc(deviceExtension->DeviceObject, NULL, NULL);
  1966. return TRUE;
  1967. }
  1968. if (resetRequest) {
  1969. RESET_CONTEXT resetContext;
  1970. resetContext.DeviceExtension = deviceExtension;
  1971. resetContext.PathId = 0;
  1972. resetContext.NewResetSequence = TRUE;
  1973. resetContext.ResetSrb = srb;
  1974. srb->SrbStatus = SRB_STATUS_PENDING;
  1975. returnValue = IdeResetBusSynchronized (&resetContext);
  1976. } else {
  1977. returnValue = AtapiStartIo (deviceExtension->HwDeviceExtension,
  1978. srb);
  1979. }
  1980. //
  1981. // Check for miniport work requests.
  1982. //
  1983. if (deviceExtension->InterruptData.InterruptFlags & PD_NOTIFICATION_REQUIRED) {
  1984. IoRequestDpc(deviceExtension->DeviceObject, NULL, NULL);
  1985. }
  1986. return returnValue;
  1987. } // end IdeStartIoSynchronized()
  1988. BOOLEAN
  1989. IdeTimeoutSynchronized (
  1990. PVOID ServiceContext
  1991. )
  1992. /*++
  1993. Routine Description:
  1994. This routine handles a port timeout. There are two reason these can occur
  1995. either because of a reset hold or a time out waiting for a read for next
  1996. request notification. If a reset hold completes, then any held request
  1997. must be started. If a timeout occurs, then the bus must be reset.
  1998. Arguments:
  1999. ServiceContext - Supplies the pointer to the device object.
  2000. Return Value:
  2001. TRUE - If a timeout error should be logged.
  2002. Notes:
  2003. The port driver spinlock must be held when this routine is called.
  2004. --*/
  2005. {
  2006. PDEVICE_OBJECT deviceObject = ServiceContext;
  2007. PFDO_EXTENSION deviceExtension = deviceObject->DeviceExtension;
  2008. ULONG i;
  2009. BOOLEAN enumProbing = FALSE;
  2010. BOOLEAN noErrorLog = FALSE;
  2011. DebugPrint((3, "IdeTimeoutSynchronized: Enter routine\n"));
  2012. //
  2013. // Make sure the timer is stopped.
  2014. //
  2015. deviceExtension->PortTimeoutCounter = PD_TIMER_STOPPED;
  2016. //
  2017. // Check for a reset hold. If one is in progress then clear it and check
  2018. // for a pending held request
  2019. //
  2020. if (deviceExtension->InterruptData.InterruptFlags & PD_RESET_HOLD) {
  2021. CLRMASK (deviceExtension->InterruptData.InterruptFlags, PD_RESET_HOLD);
  2022. if (deviceExtension->InterruptData.InterruptFlags & PD_HELD_REQUEST) {
  2023. //
  2024. // Clear the held request flag and restart the request.
  2025. //
  2026. CLRMASK (deviceExtension->InterruptData.InterruptFlags, PD_HELD_REQUEST);
  2027. IdeStartIoSynchronized(ServiceContext);
  2028. }
  2029. return(FALSE);
  2030. } else {
  2031. //
  2032. // Miniport is hung and not accepting new requests. So reset the
  2033. // bus to clear things up.
  2034. //
  2035. if (deviceExtension->HwDeviceExtension->CurrentSrb) {
  2036. deviceExtension->HwDeviceExtension->TimeoutCount[
  2037. deviceExtension->HwDeviceExtension->CurrentSrb->TargetId
  2038. ]++;
  2039. //
  2040. // Many harddrives fail to respond to the first DMA operation
  2041. // We then reset the device and subsequently everything works fine
  2042. // The hack is to mask this error from being logged in the system logs
  2043. //
  2044. if (deviceExtension->HwDeviceExtension->TimeoutCount[
  2045. deviceExtension->HwDeviceExtension->CurrentSrb->TargetId
  2046. ] == 1) {
  2047. noErrorLog=TRUE;
  2048. }
  2049. enumProbing = TestForEnumProbing (deviceExtension->HwDeviceExtension->CurrentSrb);
  2050. }
  2051. if (!enumProbing) {
  2052. DebugPrint((0,
  2053. "IdeTimeoutSynchronized: DevObj 0x%x Next request timed out. Resetting bus..currentSrb=0x%x\n",
  2054. deviceObject,
  2055. deviceExtension->HwDeviceExtension->CurrentSrb));
  2056. }
  2057. ASSERT (deviceExtension->ResetSrb == 0);
  2058. deviceExtension->ResetSrb = NULL;
  2059. deviceExtension->ResetCallAgain = 0;
  2060. AtapiResetController (deviceExtension->HwDeviceExtension,
  2061. 0,
  2062. &deviceExtension->ResetCallAgain);
  2063. //
  2064. // Set the reset hold flag and start the counter.
  2065. // if we are doing enumertion, don't set the flag
  2066. // we shouldn't set the flag if ResetCallAgain is not set
  2067. //
  2068. if (!enumProbing &&
  2069. (deviceExtension->ResetCallAgain)) {
  2070. ASSERT(deviceExtension->ResetCallAgain);
  2071. deviceExtension->InterruptData.InterruptFlags |= PD_RESET_HOLD;
  2072. deviceExtension->PortTimeoutCounter = PD_TIMER_RESET_HOLD_TIME;
  2073. } else {
  2074. ASSERT(deviceExtension->ResetCallAgain == 0);
  2075. }
  2076. //
  2077. // Check for miniport work requests.
  2078. //
  2079. if (deviceExtension->InterruptData.InterruptFlags & PD_NOTIFICATION_REQUIRED) {
  2080. IoRequestDpc(deviceExtension->DeviceObject, NULL, NULL);
  2081. }
  2082. }
  2083. if (enumProbing || noErrorLog) {
  2084. return(FALSE);
  2085. } else {
  2086. return(TRUE);
  2087. }
  2088. } // end IdeTimeoutSynchronized()
  2089. NTSTATUS
  2090. FASTCALL
  2091. IdeBuildAndSendIrp (
  2092. IN PPDO_EXTENSION PdoExtension,
  2093. IN PSCSI_REQUEST_BLOCK Srb,
  2094. IN PIO_COMPLETION_ROUTINE CompletionRoutine,
  2095. IN PVOID CompletionContext
  2096. )
  2097. {
  2098. LARGE_INTEGER largeInt;
  2099. NTSTATUS status = STATUS_PENDING;
  2100. PIRP irp;
  2101. PIO_STACK_LOCATION irpStack;
  2102. //
  2103. // why?
  2104. //
  2105. largeInt.QuadPart = (LONGLONG) 1;
  2106. //
  2107. // Build IRP for this request.
  2108. //
  2109. irp = IoBuildAsynchronousFsdRequest(IRP_MJ_READ,
  2110. PdoExtension->DeviceObject,
  2111. Srb->DataBuffer,
  2112. Srb->DataTransferLength,
  2113. &largeInt,
  2114. NULL);
  2115. if (irp == NULL) {
  2116. IdeLogNoMemoryError(PdoExtension->ParentDeviceExtension,
  2117. PdoExtension->TargetId,
  2118. NonPagedPool,
  2119. IoSizeOfIrp(PdoExtension->DeviceObject->StackSize),
  2120. IDEPORT_TAG_SEND_IRP
  2121. );
  2122. status = STATUS_INSUFFICIENT_RESOURCES;
  2123. goto GetOut;
  2124. }
  2125. IoSetCompletionRoutine(irp,
  2126. (PIO_COMPLETION_ROUTINE)CompletionRoutine,
  2127. CompletionContext,
  2128. TRUE,
  2129. TRUE,
  2130. TRUE);
  2131. irpStack = IoGetNextIrpStackLocation(irp);
  2132. irpStack->MajorFunction = IRP_MJ_SCSI;
  2133. //
  2134. // Save SRB address in next stack for port driver.
  2135. //
  2136. irpStack->Parameters.Scsi.Srb = Srb;
  2137. //
  2138. // put the irp in the original request field
  2139. //
  2140. Srb->OriginalRequest = irp;
  2141. (VOID)IoCallDriver(PdoExtension->DeviceObject, irp);
  2142. status = STATUS_PENDING;
  2143. GetOut:
  2144. return status;
  2145. }
  2146. VOID
  2147. FASTCALL
  2148. IdeFreeIrpAndMdl(
  2149. IN PIRP Irp
  2150. )
  2151. {
  2152. ASSERT(Irp);
  2153. if (Irp->MdlAddress != NULL) {
  2154. MmUnlockPages(Irp->MdlAddress);
  2155. IoFreeMdl(Irp->MdlAddress);
  2156. Irp->MdlAddress = NULL;
  2157. }
  2158. IoFreeIrp(Irp);
  2159. return;
  2160. }
  2161. VOID
  2162. IssueRequestSense(
  2163. IN PPDO_EXTENSION PdoExtension,
  2164. IN PSCSI_REQUEST_BLOCK FailingSrb
  2165. )
  2166. /*++
  2167. Routine Description:
  2168. This routine creates a REQUEST SENSE request and uses IoCallDriver to
  2169. renter the driver. The completion routine cleans up the data structures
  2170. and processes the logical unit queue according to the flags.
  2171. A pointer to failing SRB is stored at the end of the request sense
  2172. Srb, so that the completion routine can find it.
  2173. Arguments:
  2174. DeviceExension - Supplies a pointer to the pdo device extension
  2175. FailingSrb - Supplies a pointer to the request that the request sense
  2176. is being done for.
  2177. Return Value:
  2178. None.
  2179. --*/
  2180. {
  2181. PIO_STACK_LOCATION irpStack;
  2182. PIRP irp;
  2183. PSCSI_REQUEST_BLOCK srb;
  2184. PCDB cdb;
  2185. PVOID *pointer;
  2186. PLOGICAL_UNIT_EXTENSION logicalUnit;
  2187. KIRQL currentIrql;
  2188. NTSTATUS status;
  2189. #if DBG
  2190. PIO_STACK_LOCATION failingIrpStack;
  2191. PIRP failingIrp;
  2192. PLOGICAL_UNIT_EXTENSION failingLogicalUnit;
  2193. #endif
  2194. DebugPrint((3,"IssueRequestSense: Enter routine\n"));
  2195. //
  2196. // Build the asynchronous request
  2197. // to be sent to the port driver.
  2198. //
  2199. // Allocate Srb from non-paged pool
  2200. // plus room for a pointer to the failing IRP.
  2201. // Note this routine is in an error-handling
  2202. // path and is a shortterm allocation.
  2203. //
  2204. srb = ExAllocatePool(NonPagedPool,
  2205. sizeof(SCSI_REQUEST_BLOCK) + sizeof(PVOID));
  2206. if (srb == NULL) {
  2207. DebugPrint((1, "IssueRequest sense - pool allocation failed\n"));
  2208. goto Getout;
  2209. }
  2210. RtlZeroMemory(srb, sizeof(SCSI_REQUEST_BLOCK));
  2211. //
  2212. // Save the Failing SRB after the request sense Srb.
  2213. //
  2214. pointer = (PVOID *) (srb+1);
  2215. *pointer = FailingSrb;
  2216. //
  2217. // Build the REQUEST SENSE CDB.
  2218. //
  2219. srb->CdbLength = 6;
  2220. cdb = (PCDB)srb->Cdb;
  2221. cdb->CDB6INQUIRY.OperationCode = SCSIOP_REQUEST_SENSE;
  2222. cdb->CDB6INQUIRY.LogicalUnitNumber = 0;
  2223. cdb->CDB6INQUIRY.Reserved1 = 0;
  2224. cdb->CDB6INQUIRY.PageCode = 0;
  2225. cdb->CDB6INQUIRY.IReserved = 0;
  2226. cdb->CDB6INQUIRY.AllocationLength =
  2227. (UCHAR)FailingSrb->SenseInfoBufferLength;
  2228. cdb->CDB6INQUIRY.Control = 0;
  2229. //
  2230. // Set up SCSI bus address.
  2231. //
  2232. srb->TargetId = FailingSrb->TargetId;
  2233. srb->Lun = FailingSrb->Lun;
  2234. srb->PathId = FailingSrb->PathId;
  2235. srb->Function = SRB_FUNCTION_EXECUTE_SCSI;
  2236. srb->Length = sizeof(SCSI_REQUEST_BLOCK);
  2237. //
  2238. // Set timeout value to 16 seconds.
  2239. //
  2240. srb->TimeOutValue = 0x10;
  2241. //
  2242. // Disable auto request sense.
  2243. //
  2244. srb->SenseInfoBufferLength = 0;
  2245. //
  2246. // Sense buffer is in stack.
  2247. //
  2248. srb->SenseInfoBuffer = NULL;
  2249. //
  2250. // Set read and bypass frozen queue bits in flags.
  2251. //
  2252. //
  2253. // Set SRB flags to indicate the logical unit queue should be by
  2254. // passed and that no queue processing should be done when the request
  2255. // completes. Also disable disconnect and synchronous data
  2256. // transfer if necessary.
  2257. //
  2258. srb->SrbFlags = SRB_FLAGS_DATA_IN | SRB_FLAGS_BYPASS_FROZEN_QUEUE |
  2259. SRB_FLAGS_DISABLE_DISCONNECT;
  2260. if (FailingSrb->SrbFlags & SRB_FLAGS_DISABLE_SYNCH_TRANSFER) {
  2261. srb->SrbFlags |= SRB_FLAGS_DISABLE_SYNCH_TRANSFER;
  2262. }
  2263. srb->DataBuffer = FailingSrb->SenseInfoBuffer;
  2264. //
  2265. // Set the transfer length.
  2266. //
  2267. srb->DataTransferLength = FailingSrb->SenseInfoBufferLength;
  2268. //
  2269. // Zero out status.
  2270. //
  2271. srb->ScsiStatus = srb->SrbStatus = 0;
  2272. srb->NextSrb = 0;
  2273. #if DBG
  2274. //
  2275. // This was added to catch a bug where the original request
  2276. // was pointing to a pnp irp
  2277. //
  2278. ASSERT(FailingSrb->OriginalRequest);
  2279. failingIrp = FailingSrb->OriginalRequest;
  2280. failingIrpStack = IoGetCurrentIrpStackLocation(failingIrp);
  2281. failingLogicalUnit = IDEPORT_GET_LUNEXT_IN_IRP (failingIrpStack);
  2282. ASSERT(failingLogicalUnit);
  2283. #endif
  2284. status = IdeBuildAndSendIrp(PdoExtension,
  2285. srb,
  2286. IdePortInternalCompletion,
  2287. srb
  2288. );
  2289. if (NT_SUCCESS(status)) {
  2290. return;
  2291. }
  2292. ASSERT(status == STATUS_INSUFFICIENT_RESOURCES);
  2293. Getout:
  2294. if (srb) {
  2295. ExFreePool(srb);
  2296. }
  2297. irp = FailingSrb->OriginalRequest;
  2298. irpStack = IoGetCurrentIrpStackLocation(irp);
  2299. logicalUnit = IDEPORT_GET_LUNEXT_IN_IRP (irpStack);
  2300. //
  2301. // Clear the request sense flag. Since IdeStartIoSync will never get called, this
  2302. // flag won't be cleared.
  2303. //
  2304. KeAcquireSpinLock(&logicalUnit->ParentDeviceExtension->SpinLock, &currentIrql);
  2305. CLRMASK (logicalUnit->LuFlags, PD_NEED_REQUEST_SENSE);
  2306. KeReleaseSpinLock(&logicalUnit->ParentDeviceExtension->SpinLock, currentIrql);
  2307. //
  2308. // unfreeze the queue if necessary
  2309. //
  2310. ASSERT(FailingSrb->SrbStatus & SRB_STATUS_QUEUE_FROZEN);
  2311. if ((FailingSrb->SrbFlags & SRB_FLAGS_NO_QUEUE_FREEZE) &&
  2312. (FailingSrb->SrbStatus & SRB_STATUS_QUEUE_FROZEN)) {
  2313. CLRMASK (logicalUnit->LuFlags, PD_QUEUE_FROZEN);
  2314. KeAcquireSpinLock(&logicalUnit->ParentDeviceExtension->SpinLock, &currentIrql);
  2315. GetNextLuRequest(logicalUnit->ParentDeviceExtension, logicalUnit);
  2316. KeLowerIrql(currentIrql);
  2317. CLRMASK (FailingSrb->SrbStatus, SRB_STATUS_QUEUE_FROZEN);
  2318. }
  2319. //
  2320. // Decrement the logUnitExtension reference count
  2321. //
  2322. UnrefLogicalUnitExtensionWithTag(
  2323. IDEPORT_GET_LUNEXT_IN_IRP(irpStack)->ParentDeviceExtension,
  2324. IDEPORT_GET_LUNEXT_IN_IRP(irpStack),
  2325. irp
  2326. );
  2327. //
  2328. // Complete the original request
  2329. //
  2330. IoCompleteRequest(irp, IO_DISK_INCREMENT);
  2331. return;
  2332. } // end IssueRequestSense()
  2333. NTSTATUS
  2334. IdePortInternalCompletion(
  2335. PDEVICE_OBJECT DeviceObject,
  2336. PIRP Irp,
  2337. PVOID Context
  2338. )
  2339. /*++
  2340. Routine Description:
  2341. Arguments:
  2342. Device object
  2343. IRP
  2344. Context - pointer to SRB
  2345. Return Value:
  2346. NTSTATUS
  2347. --*/
  2348. {
  2349. PIO_STACK_LOCATION irpStack;
  2350. PSCSI_REQUEST_BLOCK srb = Context;
  2351. PSCSI_REQUEST_BLOCK failingSrb;
  2352. PIRP failingIrp;
  2353. PLOGICAL_UNIT_EXTENSION logicalUnit;
  2354. PSENSE_DATA senseBuffer;
  2355. PHW_DEVICE_EXTENSION hwDeviceExtension;
  2356. KIRQL currentIrql;
  2357. UNREFERENCED_PARAMETER(DeviceObject);
  2358. DebugPrint((3,"IdePortInternalCompletion: Enter routine\n"));
  2359. //
  2360. // If RESET_BUS or ABORT_COMMAND request
  2361. // then free pool and return.
  2362. //
  2363. if ((srb->Function == SRB_FUNCTION_ABORT_COMMAND) ||
  2364. (srb->Function == SRB_FUNCTION_RESET_BUS)) {
  2365. //
  2366. // Deallocate internal SRB and IRP.
  2367. //
  2368. ExFreePool(srb);
  2369. IoFreeIrp(Irp);
  2370. return STATUS_MORE_PROCESSING_REQUIRED;
  2371. }
  2372. irpStack = IoGetCurrentIrpStackLocation(Irp);
  2373. //
  2374. // Request sense completed. If successful or data over/underrun
  2375. // get the failing SRB and indicate that the sense information
  2376. // is valid. The class driver will check for underrun and determine
  2377. // if there is enough sense information to be useful.
  2378. //
  2379. //
  2380. // Get a pointer to failing Irp and Srb.
  2381. //
  2382. failingSrb = *((PVOID *) (srb+1));
  2383. failingIrp = failingSrb->OriginalRequest;
  2384. irpStack = IoGetCurrentIrpStackLocation(failingIrp);
  2385. logicalUnit = IDEPORT_GET_LUNEXT_IN_IRP (irpStack);
  2386. if ((SRB_STATUS(srb->SrbStatus) == SRB_STATUS_SUCCESS) ||
  2387. (SRB_STATUS(srb->SrbStatus) == SRB_STATUS_DATA_OVERRUN)) {
  2388. //
  2389. // Report sense buffer is valid.
  2390. //
  2391. failingSrb->SrbStatus |= SRB_STATUS_AUTOSENSE_VALID;
  2392. //
  2393. // Copy bytes transferred to failing SRB
  2394. // request sense length field to communicate
  2395. // to the class drivers the number of valid
  2396. // sense bytes.
  2397. //
  2398. failingSrb->SenseInfoBufferLength = (UCHAR) srb->DataTransferLength;
  2399. #if 0
  2400. //
  2401. // enable for debugging only
  2402. // if sense buffer is smaller than 13 bytes, then the debugprint
  2403. // below could bugchecl the system
  2404. //
  2405. //
  2406. // Print the sense buffer for debugging purposes.
  2407. //
  2408. senseBuffer = failingSrb->SenseInfoBuffer;
  2409. DebugPrint((DBG_ATAPI_DEVICES, "CDB=%x, SenseKey=%x, ASC=%x, ASQ=%x\n",
  2410. failingSrb->Cdb[0],
  2411. senseBuffer->SenseKey, senseBuffer->AdditionalSenseCode,
  2412. senseBuffer->AdditionalSenseCodeQualifier));
  2413. #endif
  2414. }
  2415. //
  2416. // Clear the request sense flag. If we fail due to fault injection
  2417. // IdeStartIo won't get called and this flag never gets cleared.
  2418. //
  2419. KeAcquireSpinLock(&logicalUnit->ParentDeviceExtension->SpinLock, &currentIrql);
  2420. CLRMASK (logicalUnit->LuFlags, PD_NEED_REQUEST_SENSE);
  2421. KeReleaseSpinLock(&logicalUnit->ParentDeviceExtension->SpinLock, currentIrql);
  2422. //
  2423. // unfreeze the queue if necessary
  2424. //
  2425. ASSERT(failingSrb->SrbStatus & SRB_STATUS_QUEUE_FROZEN);
  2426. if ((failingSrb->SrbFlags & SRB_FLAGS_NO_QUEUE_FREEZE) &&
  2427. (failingSrb->SrbStatus & SRB_STATUS_QUEUE_FROZEN)) {
  2428. CLRMASK (logicalUnit->LuFlags, PD_QUEUE_FROZEN);
  2429. KeAcquireSpinLock(&logicalUnit->ParentDeviceExtension->SpinLock, &currentIrql);
  2430. GetNextLuRequest(logicalUnit->ParentDeviceExtension, logicalUnit);
  2431. KeLowerIrql(currentIrql);
  2432. CLRMASK (failingSrb->SrbStatus, SRB_STATUS_QUEUE_FROZEN);
  2433. }
  2434. //
  2435. // Decrement the logUnitExtension reference count
  2436. //
  2437. UnrefLogicalUnitExtensionWithTag(
  2438. IDEPORT_GET_LUNEXT_IN_IRP(irpStack)->ParentDeviceExtension,
  2439. IDEPORT_GET_LUNEXT_IN_IRP(irpStack),
  2440. failingIrp
  2441. );
  2442. //
  2443. // Complete the failing request.
  2444. //
  2445. IoCompleteRequest(failingIrp, IO_DISK_INCREMENT);
  2446. //
  2447. // Deallocate internal SRB, MDL and IRP.
  2448. //
  2449. ExFreePool(srb);
  2450. IdeFreeIrpAndMdl(Irp);
  2451. return STATUS_MORE_PROCESSING_REQUIRED;
  2452. } // IdePortInternalCompletion()
  2453. BOOLEAN
  2454. IdeGetInterruptState(
  2455. IN PVOID ServiceContext
  2456. )
  2457. /*++
  2458. Routine Description:
  2459. This routine saves the InterruptFlags, MapTransferParameters and
  2460. CompletedRequests fields and clears the InterruptFlags.
  2461. This routine also removes the request from the logical unit queue if it is
  2462. tag. Finally the request time is updated.
  2463. Arguments:
  2464. ServiceContext - Supplies a pointer to the interrupt context which contains
  2465. pointers to the interrupt data and where to save it.
  2466. Return Value:
  2467. Returns TURE if there is new work and FALSE otherwise.
  2468. Notes:
  2469. Called via KeSynchronizeExecution with the port device extension spinlock
  2470. held.
  2471. --*/
  2472. {
  2473. PINTERRUPT_CONTEXT interruptContext = ServiceContext;
  2474. ULONG limit = 0;
  2475. PFDO_EXTENSION deviceExtension;
  2476. PLOGICAL_UNIT_EXTENSION logicalUnit;
  2477. PSCSI_REQUEST_BLOCK srb;
  2478. PSRB_DATA srbData;
  2479. PSRB_DATA nextSrbData;
  2480. BOOLEAN isTimed;
  2481. deviceExtension = interruptContext->DeviceExtension;
  2482. //
  2483. // Check for pending work.
  2484. //
  2485. if (!(deviceExtension->InterruptData.InterruptFlags & PD_NOTIFICATION_REQUIRED)) {
  2486. return(FALSE);
  2487. }
  2488. //
  2489. // Move the interrupt state to save area.
  2490. //
  2491. *interruptContext->SavedInterruptData = deviceExtension->InterruptData;
  2492. //
  2493. // Clear the interrupt state.
  2494. //
  2495. deviceExtension->InterruptData.InterruptFlags &= PD_INTERRUPT_FLAG_MASK;
  2496. deviceExtension->InterruptData.CompletedRequests = NULL;
  2497. deviceExtension->InterruptData.ReadyLogicalUnit = NULL;
  2498. deviceExtension->InterruptData.CompletedAbort = NULL;
  2499. deviceExtension->InterruptData.PdoExtensionResetBus = NULL;
  2500. srbData = interruptContext->SavedInterruptData->CompletedRequests;
  2501. while (srbData != NULL) {
  2502. PIRP irp;
  2503. PIO_STACK_LOCATION irpStack;
  2504. ASSERT(limit++ < 100);
  2505. //
  2506. // Get a pointer to the SRB and the logical unit extension.
  2507. //
  2508. ASSERT(srbData->CurrentSrb != NULL);
  2509. srb = srbData->CurrentSrb;
  2510. irp = srb->OriginalRequest;
  2511. irpStack = IoGetCurrentIrpStackLocation(irp);
  2512. logicalUnit = IDEPORT_GET_LUNEXT_IN_IRP (irpStack);
  2513. //
  2514. // If the request did not succeed, then check for the special cases.
  2515. //
  2516. if (srb->SrbStatus != SRB_STATUS_SUCCESS) {
  2517. //
  2518. // If this request failed and a REQUEST SENSE command needs to
  2519. // be done, then set a flag to indicate this and prevent other
  2520. // commands from being started.
  2521. //
  2522. if (NEED_REQUEST_SENSE(srb)) {
  2523. if (logicalUnit->LuFlags & PD_NEED_REQUEST_SENSE) {
  2524. //
  2525. // This implies that requests have completed with a
  2526. // status of check condition before a REQUEST SENSE
  2527. // command could be preformed. This should never occur.
  2528. // Convert the request to another code so that only one
  2529. // auto request sense is issued.
  2530. //
  2531. srb->ScsiStatus = 0;
  2532. srb->SrbStatus = SRB_STATUS_REQUEST_SENSE_FAILED;
  2533. } else {
  2534. //
  2535. // Indicate that an auto request sense needs to be done.
  2536. //
  2537. logicalUnit->LuFlags |= PD_NEED_REQUEST_SENSE;
  2538. }
  2539. }
  2540. }
  2541. logicalUnit->RequestTimeoutCounter = PD_TIMER_STOPPED;
  2542. srbData = srbData->CompletedRequests;
  2543. }
  2544. return(TRUE);
  2545. }
  2546. VOID
  2547. IdePortAllocateAccessToken (
  2548. IN PDEVICE_OBJECT DeviceObject
  2549. )
  2550. {
  2551. PFDO_EXTENSION fdoExtension = DeviceObject->DeviceExtension;
  2552. if (!fdoExtension->SyncAccessInterface.AllocateAccessToken) {
  2553. CallIdeStartIoSynchronized (
  2554. NULL,
  2555. NULL,
  2556. NULL,
  2557. DeviceObject
  2558. );
  2559. } else {
  2560. (*fdoExtension->SyncAccessInterface.AllocateAccessToken) (
  2561. fdoExtension->SyncAccessInterface.Token,
  2562. CallIdeStartIoSynchronized,
  2563. DeviceObject
  2564. );
  2565. }
  2566. }
  2567. IO_ALLOCATION_ACTION
  2568. CallIdeStartIoSynchronized (
  2569. IN PVOID Reserved1,
  2570. IN PVOID Reserved2,
  2571. IN PVOID Reserved3,
  2572. IN PVOID DeviceObject
  2573. )
  2574. {
  2575. PFDO_EXTENSION deviceExtension = ((PDEVICE_OBJECT) DeviceObject)->DeviceExtension;
  2576. KIRQL currentIrql;
  2577. KeAcquireSpinLock(&deviceExtension->SpinLock, &currentIrql);
  2578. KeSynchronizeExecution (
  2579. deviceExtension->InterruptObject,
  2580. IdeStartIoSynchronized,
  2581. DeviceObject
  2582. );
  2583. KeReleaseSpinLock(&deviceExtension->SpinLock, currentIrql);
  2584. return KeepObject;
  2585. }
  2586. VOID
  2587. LogErrorEntry(
  2588. IN PFDO_EXTENSION DeviceExtension,
  2589. IN PERROR_LOG_ENTRY LogEntry
  2590. )
  2591. /*++
  2592. Routine Description:
  2593. This function allocates an I/O error log record, fills it in and writes it
  2594. to the I/O error log.
  2595. Arguments:
  2596. DeviceExtension - Supplies a pointer to the port device extension.
  2597. LogEntry - Supplies a pointer to the scsi port log entry.
  2598. Return Value:
  2599. None.
  2600. --*/
  2601. {
  2602. PIO_ERROR_LOG_PACKET errorLogEntry;
  2603. errorLogEntry = (PIO_ERROR_LOG_PACKET)IoAllocateErrorLogEntry(
  2604. DeviceExtension->DeviceObject,
  2605. sizeof(IO_ERROR_LOG_PACKET) + 4 * sizeof(ULONG)
  2606. );
  2607. if (errorLogEntry != NULL) {
  2608. //
  2609. // Translate the miniport error code into the NT I\O driver.
  2610. //
  2611. switch (LogEntry->ErrorCode) {
  2612. case SP_BUS_PARITY_ERROR:
  2613. errorLogEntry->ErrorCode = IO_ERR_PARITY;
  2614. break;
  2615. case SP_UNEXPECTED_DISCONNECT:
  2616. errorLogEntry->ErrorCode = IO_ERR_CONTROLLER_ERROR;
  2617. break;
  2618. case SP_INVALID_RESELECTION:
  2619. errorLogEntry->ErrorCode = IO_ERR_CONTROLLER_ERROR;
  2620. break;
  2621. case SP_BUS_TIME_OUT:
  2622. errorLogEntry->ErrorCode = IO_ERR_TIMEOUT;
  2623. break;
  2624. case SP_PROTOCOL_ERROR:
  2625. errorLogEntry->ErrorCode = IO_ERR_CONTROLLER_ERROR;
  2626. break;
  2627. case SP_INTERNAL_ADAPTER_ERROR:
  2628. errorLogEntry->ErrorCode = IO_ERR_CONTROLLER_ERROR;
  2629. break;
  2630. case SP_IRQ_NOT_RESPONDING:
  2631. errorLogEntry->ErrorCode = IO_ERR_INCORRECT_IRQL;
  2632. break;
  2633. case SP_BAD_FW_ERROR:
  2634. errorLogEntry->ErrorCode = IO_ERR_BAD_FIRMWARE;
  2635. break;
  2636. case SP_BAD_FW_WARNING:
  2637. errorLogEntry->ErrorCode = IO_WRN_BAD_FIRMWARE;
  2638. break;
  2639. default:
  2640. errorLogEntry->ErrorCode = IO_ERR_CONTROLLER_ERROR;
  2641. break;
  2642. }
  2643. errorLogEntry->SequenceNumber = LogEntry->SequenceNumber;
  2644. errorLogEntry->MajorFunctionCode = IRP_MJ_SCSI;
  2645. errorLogEntry->RetryCount = (UCHAR) LogEntry->ErrorLogRetryCount;
  2646. errorLogEntry->UniqueErrorValue = LogEntry->UniqueId;
  2647. errorLogEntry->FinalStatus = STATUS_SUCCESS;
  2648. errorLogEntry->DumpDataSize = 4 * sizeof(ULONG);
  2649. errorLogEntry->DumpData[0] = LogEntry->PathId;
  2650. errorLogEntry->DumpData[1] = LogEntry->TargetId;
  2651. errorLogEntry->DumpData[2] = LogEntry->Lun;
  2652. errorLogEntry->DumpData[3] = LogEntry->ErrorCode;
  2653. IoWriteErrorLogEntry(errorLogEntry);
  2654. }
  2655. #if DBG
  2656. {
  2657. PCHAR errorCodeString;
  2658. switch (LogEntry->ErrorCode) {
  2659. case SP_BUS_PARITY_ERROR:
  2660. errorCodeString = "SCSI bus partity error";
  2661. break;
  2662. case SP_UNEXPECTED_DISCONNECT:
  2663. errorCodeString = "Unexpected disconnect";
  2664. break;
  2665. case SP_INVALID_RESELECTION:
  2666. errorCodeString = "Invalid reselection";
  2667. break;
  2668. case SP_BUS_TIME_OUT:
  2669. errorCodeString = "SCSI bus time out";
  2670. break;
  2671. case SP_PROTOCOL_ERROR:
  2672. errorCodeString = "SCSI protocol error";
  2673. break;
  2674. case SP_INTERNAL_ADAPTER_ERROR:
  2675. errorCodeString = "Internal adapter error";
  2676. break;
  2677. default:
  2678. errorCodeString = "Unknown error code";
  2679. break;
  2680. }
  2681. DebugPrint((DBG_ALWAYS,"LogErrorEntry: Logging SCSI error packet. ErrorCode = %s.\n",
  2682. errorCodeString
  2683. ));
  2684. DebugPrint((DBG_ALWAYS,
  2685. "PathId = %2x, TargetId = %2x, Lun = %2x, UniqueId = %x.\n",
  2686. LogEntry->PathId,
  2687. LogEntry->TargetId,
  2688. LogEntry->Lun,
  2689. LogEntry->UniqueId
  2690. ));
  2691. }
  2692. #endif
  2693. }
  2694. VOID
  2695. GetNextLuPendingRequest(
  2696. IN PFDO_EXTENSION DeviceExtension,
  2697. IN PLOGICAL_UNIT_EXTENSION LogicalUnit
  2698. )
  2699. {
  2700. if (LogicalUnit->PendingRequest) {
  2701. GetNextLuRequest(
  2702. DeviceExtension,
  2703. LogicalUnit
  2704. );
  2705. } else {
  2706. KeReleaseSpinLockFromDpcLevel(&DeviceExtension->SpinLock);
  2707. }
  2708. return;
  2709. }
  2710. #ifdef LOG_GET_NEXT_CALLER
  2711. VOID
  2712. GetNextLuRequest2(
  2713. IN PFDO_EXTENSION DeviceExtension,
  2714. IN PLOGICAL_UNIT_EXTENSION LogicalUnit,
  2715. IN PUCHAR FileName,
  2716. IN ULONG LineNumber
  2717. )
  2718. #else
  2719. VOID
  2720. GetNextLuRequest(
  2721. IN PFDO_EXTENSION DeviceExtension,
  2722. IN PLOGICAL_UNIT_EXTENSION LogicalUnit
  2723. )
  2724. #endif
  2725. /*++
  2726. Routine Description:
  2727. This routine get the next request for the specified logical unit. It does
  2728. the necessary initialization to the logical unit structure and submitts the
  2729. request to the device queue. The DeviceExtension SpinLock must be held
  2730. when this function called. It is released by this function.
  2731. Arguments:
  2732. DeviceExtension - Supplies a pointer to the port device extension.
  2733. LogicalUnit - Supplies a pointer to the logical unit extension to get the
  2734. next request from.
  2735. Return Value:
  2736. None.
  2737. --*/
  2738. {
  2739. PKDEVICE_QUEUE_ENTRY packet;
  2740. PIO_STACK_LOCATION irpStack;
  2741. PSCSI_REQUEST_BLOCK srb;
  2742. POWER_STATE powerState;
  2743. PIRP nextIrp;
  2744. BOOLEAN powerUpDevice = FALSE;
  2745. #ifdef LOG_GET_NEXT_CALLER
  2746. IdeLogGetNextLuCaller(DeviceExtension,
  2747. LogicalUnit,
  2748. FileName,
  2749. LineNumber
  2750. );
  2751. #endif
  2752. //
  2753. // If the active flag is not set, then the queue is not busy or there is
  2754. // a request being processed and the next request should not be started..
  2755. //
  2756. if ((!(LogicalUnit->LuFlags & PD_LOGICAL_UNIT_IS_ACTIVE) &&
  2757. (LogicalUnit->PendingRequest == NULL))
  2758. || (LogicalUnit->SrbData.CurrentSrb)) {
  2759. DebugPrint ((2, "IdePort GetNextLuRequest: 0x%x 0x%x NOT PD_LOGICAL_UNIT_IS_ACTIVE\n",
  2760. DeviceExtension->IdeResource.TranslatedCommandBaseAddress,
  2761. LogicalUnit->TargetId
  2762. ));
  2763. //
  2764. // Release the spinlock.
  2765. //
  2766. KeReleaseSpinLockFromDpcLevel(&DeviceExtension->SpinLock);
  2767. return;
  2768. }
  2769. //
  2770. // Check for pending requests, queue full or busy requests. Pending
  2771. // requests occur when untagged request is started and there are active
  2772. // queued requests. Busy requests occur when the target returns a BUSY
  2773. // or QUEUE FULL status. Busy requests are started by the timer code.
  2774. // Also if the need request sense flag is set, it indicates that
  2775. // an error status was detected on the logical unit. No new requests
  2776. // should be started until this flag is cleared. This flag is cleared
  2777. // by an untagged command that by-passes the LU queue i.e.
  2778. //
  2779. // The busy flag and the need request sense flag have the effect of
  2780. // forcing the queue of outstanding requests to drain after an error or
  2781. // until a busy request gets started.
  2782. //
  2783. if (LogicalUnit->LuFlags & (PD_LOGICAL_UNIT_IS_BUSY
  2784. | PD_QUEUE_IS_FULL | PD_NEED_REQUEST_SENSE | PD_QUEUE_FROZEN) ||
  2785. (LogicalUnit->PdoState & (PDOS_REMOVED | PDOS_SURPRISE_REMOVED))) {
  2786. //
  2787. // If the request queue is now empty, then the pending request can
  2788. // be started.
  2789. //
  2790. DebugPrint((2, "IdePort: GetNextLuRequest: 0x%x 0x%x Ignoring a get next lu call.\n",
  2791. DeviceExtension->IdeResource.TranslatedCommandBaseAddress,
  2792. LogicalUnit->TargetId
  2793. ));
  2794. //
  2795. // Note the active flag is not cleared. So the next request
  2796. // will be processed when the other requests have completed.
  2797. // Release the spinlock.
  2798. //
  2799. KeReleaseSpinLockFromDpcLevel(&DeviceExtension->SpinLock);
  2800. return;
  2801. }
  2802. //
  2803. // Clear the active flag. If there is another request, the flag will be
  2804. // set again when the request is passed to the miniport.
  2805. //
  2806. CLRMASK (LogicalUnit->LuFlags, PD_LOGICAL_UNIT_IS_ACTIVE);
  2807. LogicalUnit->RetryCount = 0;
  2808. nextIrp = NULL;
  2809. if (LogicalUnit->PendingRequest) {
  2810. nextIrp = LogicalUnit->PendingRequest;
  2811. LogicalUnit->PendingRequest = NULL;
  2812. } else {
  2813. //
  2814. // Remove the packet from the logical unit device queue.
  2815. //
  2816. packet = KeRemoveByKeyDeviceQueue(&LogicalUnit->DeviceObject->DeviceQueue,
  2817. LogicalUnit->CurrentKey);
  2818. if (packet != NULL) {
  2819. nextIrp = CONTAINING_RECORD(packet, IRP, Tail.Overlay.DeviceQueueEntry);
  2820. #if DBG
  2821. InterlockedDecrement (
  2822. &LogicalUnit->NumberOfIrpQueued
  2823. );
  2824. #endif // DBG
  2825. }
  2826. }
  2827. if (!nextIrp) {
  2828. DebugPrint ((2, "IdePort GetNextLuRequest: 0x%x 0x%x no irp to processing\n",
  2829. DeviceExtension->IdeResource.TranslatedCommandBaseAddress,
  2830. LogicalUnit->TargetId
  2831. ));
  2832. }
  2833. if (nextIrp) {
  2834. BOOLEAN pendingRequest;
  2835. irpStack = IoGetCurrentIrpStackLocation(nextIrp);
  2836. srb = (PSCSI_REQUEST_BLOCK)irpStack->Parameters.Others.Argument1;
  2837. if (LogicalUnit->PdoState & PDOS_QUEUE_BLOCKED) {
  2838. DebugPrint ((2, "IdePort GetNextLuRequest: 0x%x 0x%x Lu must queue\n",
  2839. DeviceExtension->IdeResource.TranslatedCommandBaseAddress,
  2840. LogicalUnit->TargetId
  2841. ));
  2842. pendingRequest = TRUE;
  2843. if (!(LogicalUnit->PdoState & PDOS_MUST_QUEUE)) {
  2844. //
  2845. // device is powered down
  2846. // use a large time in case it spins up slowly
  2847. //
  2848. if (srb->TimeOutValue < DEFAULT_SPINUP_TIME) {
  2849. srb->TimeOutValue = DEFAULT_SPINUP_TIME;
  2850. }
  2851. //
  2852. // We are not powered up.
  2853. // issue an power up
  2854. //
  2855. powerUpDevice = TRUE;
  2856. DebugPrint ((2, "IdePort GetNextLuRequest: 0x%x 0x%x need to spin up device, requeue irp 0x%x\n",
  2857. DeviceExtension->IdeResource.TranslatedCommandBaseAddress,
  2858. LogicalUnit->TargetId,
  2859. nextIrp));
  2860. }
  2861. } else {
  2862. pendingRequest = FALSE;
  2863. }
  2864. if (pendingRequest) {
  2865. ASSERT (LogicalUnit->PendingRequest == NULL);
  2866. LogicalUnit->PendingRequest = nextIrp;
  2867. nextIrp = NULL;
  2868. }
  2869. }
  2870. if (nextIrp) {
  2871. //
  2872. // Set the new current key.
  2873. //
  2874. LogicalUnit->CurrentKey = srb->QueueSortKey;
  2875. //
  2876. // Hack to work-around the starvation led to by numerous requests touching the same sector.
  2877. //
  2878. LogicalUnit->CurrentKey++;
  2879. //
  2880. // Release the spinlock.
  2881. //
  2882. KeReleaseSpinLockFromDpcLevel(&DeviceExtension->SpinLock);
  2883. DebugPrint ((2, "GetNextLuRequest: IoStartPacket 0x%x\n", nextIrp));
  2884. IoStartPacket(DeviceExtension->DeviceObject, nextIrp, (PULONG)NULL, NULL);
  2885. } else {
  2886. NTSTATUS status;
  2887. KeReleaseSpinLockFromDpcLevel(&DeviceExtension->SpinLock);
  2888. if (powerUpDevice) {
  2889. powerState.DeviceState = PowerDeviceD0;
  2890. status = PoRequestPowerIrp (
  2891. LogicalUnit->DeviceObject,
  2892. IRP_MN_SET_POWER,
  2893. powerState,
  2894. NULL,
  2895. NULL,
  2896. NULL
  2897. );
  2898. ASSERT (NT_SUCCESS(status));
  2899. }
  2900. }
  2901. } // end GetNextLuRequest()
  2902. VOID
  2903. IdeLogTimeoutError(
  2904. IN PFDO_EXTENSION DeviceExtension,
  2905. IN PIRP Irp,
  2906. IN ULONG UniqueId
  2907. )
  2908. /*++
  2909. Routine Description:
  2910. This function logs an error when a request times out.
  2911. Arguments:
  2912. DeviceExtension - Supplies a pointer to the port device extension.
  2913. Irp - Supplies a pointer to the request which timedout.
  2914. UniqueId - Supplies the UniqueId for this error.
  2915. Return Value:
  2916. None.
  2917. Notes:
  2918. The port device extension spinlock should be held when this routine is
  2919. called.
  2920. --*/
  2921. {
  2922. PIO_ERROR_LOG_PACKET errorLogEntry;
  2923. PIO_STACK_LOCATION irpStack;
  2924. PSRB_DATA srbData;
  2925. PSCSI_REQUEST_BLOCK srb;
  2926. irpStack = IoGetCurrentIrpStackLocation(Irp);
  2927. srb = (PSCSI_REQUEST_BLOCK)irpStack->Parameters.Others.Argument1;
  2928. srbData = IdeGetSrbData(DeviceExtension, srb);
  2929. if (!srbData) {
  2930. return;
  2931. }
  2932. errorLogEntry = (PIO_ERROR_LOG_PACKET) IoAllocateErrorLogEntry(DeviceExtension->DeviceObject,
  2933. sizeof(IO_ERROR_LOG_PACKET) + 4 * sizeof(ULONG));
  2934. if (errorLogEntry != NULL) {
  2935. errorLogEntry->ErrorCode = IO_ERR_TIMEOUT;
  2936. errorLogEntry->SequenceNumber = srbData->SequenceNumber;
  2937. errorLogEntry->MajorFunctionCode = irpStack->MajorFunction;
  2938. errorLogEntry->RetryCount = (UCHAR) srbData->ErrorLogRetryCount;
  2939. errorLogEntry->UniqueErrorValue = UniqueId;
  2940. errorLogEntry->FinalStatus = STATUS_SUCCESS;
  2941. errorLogEntry->DumpDataSize = 4 * sizeof(ULONG);
  2942. errorLogEntry->DumpData[0] = srb->PathId;
  2943. errorLogEntry->DumpData[1] = srb->TargetId;
  2944. errorLogEntry->DumpData[2] = srb->Lun;
  2945. errorLogEntry->DumpData[3] = SP_REQUEST_TIMEOUT;
  2946. IoWriteErrorLogEntry(errorLogEntry);
  2947. }
  2948. }
  2949. VOID
  2950. IdeLogResetError(
  2951. IN PFDO_EXTENSION DeviceExtension,
  2952. IN PSCSI_REQUEST_BLOCK Srb,
  2953. IN ULONG UniqueId
  2954. )
  2955. /*++
  2956. Routine Description:
  2957. This function logs an error when the bus is reset.
  2958. Arguments:
  2959. DeviceExtension - Supplies a pointer to the port device extension.
  2960. Srb - Supplies a pointer to the request which timed-out.
  2961. UniqueId - Supplies the UniqueId for this error.
  2962. Return Value:
  2963. None.
  2964. Notes:
  2965. The port device extension spinlock should be held when this routine is
  2966. called.
  2967. --*/
  2968. {
  2969. PIO_ERROR_LOG_PACKET errorLogEntry;
  2970. PIO_STACK_LOCATION irpStack;
  2971. PIRP irp;
  2972. PSRB_DATA srbData;
  2973. ULONG sequenceNumber = 0;
  2974. UCHAR function = 0,
  2975. pathId = 0,
  2976. targetId = 0,
  2977. lun = 0,
  2978. retryCount = 0;
  2979. if (Srb) {
  2980. irp = Srb->OriginalRequest;
  2981. if (irp) {
  2982. irpStack = IoGetCurrentIrpStackLocation(irp);
  2983. function = irpStack->MajorFunction;
  2984. }
  2985. srbData = IdeGetSrbData(DeviceExtension, Srb);
  2986. if (!srbData) {
  2987. return;
  2988. }
  2989. pathId = Srb->PathId;
  2990. targetId = Srb->TargetId;
  2991. lun = Srb->Lun;
  2992. retryCount = (UCHAR) srbData->ErrorLogRetryCount;
  2993. sequenceNumber = srbData->SequenceNumber;
  2994. }
  2995. errorLogEntry = (PIO_ERROR_LOG_PACKET) IoAllocateErrorLogEntry( DeviceExtension->DeviceObject,
  2996. sizeof(IO_ERROR_LOG_PACKET)
  2997. + 4 * sizeof(ULONG) );
  2998. if (errorLogEntry != NULL) {
  2999. errorLogEntry->ErrorCode = IO_ERR_TIMEOUT;
  3000. errorLogEntry->SequenceNumber = sequenceNumber;
  3001. errorLogEntry->MajorFunctionCode = function;
  3002. errorLogEntry->RetryCount = retryCount;
  3003. errorLogEntry->UniqueErrorValue = UniqueId;
  3004. errorLogEntry->FinalStatus = STATUS_SUCCESS;
  3005. errorLogEntry->DumpDataSize = 4 * sizeof(ULONG);
  3006. errorLogEntry->DumpData[0] = pathId;
  3007. errorLogEntry->DumpData[1] = targetId;
  3008. errorLogEntry->DumpData[2] = lun;
  3009. errorLogEntry->DumpData[3] = SP_REQUEST_TIMEOUT;
  3010. IoWriteErrorLogEntry(errorLogEntry);
  3011. }
  3012. }
  3013. NTSTATUS
  3014. IdeTranslateSrbStatus(
  3015. IN PSCSI_REQUEST_BLOCK Srb
  3016. )
  3017. /*++
  3018. Routine Description:
  3019. This routine translates an srb status into an ntstatus.
  3020. Arguments:
  3021. Srb - Supplies a pointer to the failing Srb.
  3022. Return Value:
  3023. An nt status approprate for the error.
  3024. --*/
  3025. {
  3026. switch (SRB_STATUS(Srb->SrbStatus)) {
  3027. case SRB_STATUS_INVALID_LUN:
  3028. case SRB_STATUS_INVALID_TARGET_ID:
  3029. case SRB_STATUS_NO_DEVICE:
  3030. case SRB_STATUS_NO_HBA:
  3031. return(STATUS_DEVICE_DOES_NOT_EXIST);
  3032. case SRB_STATUS_COMMAND_TIMEOUT:
  3033. case SRB_STATUS_BUS_RESET:
  3034. case SRB_STATUS_TIMEOUT:
  3035. return(STATUS_IO_TIMEOUT);
  3036. case SRB_STATUS_SELECTION_TIMEOUT:
  3037. return(STATUS_DEVICE_NOT_CONNECTED);
  3038. case SRB_STATUS_BAD_FUNCTION:
  3039. case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
  3040. return(STATUS_INVALID_DEVICE_REQUEST);
  3041. case SRB_STATUS_DATA_OVERRUN:
  3042. return(STATUS_BUFFER_OVERFLOW);
  3043. default:
  3044. return(STATUS_IO_DEVICE_ERROR);
  3045. }
  3046. return(STATUS_IO_DEVICE_ERROR);
  3047. }
  3048. BOOLEAN
  3049. IdeResetBusSynchronized (
  3050. PVOID ServiceContext
  3051. )
  3052. /*++
  3053. Routine Description:
  3054. This function resets the bus and sets up the port timer so the reset hold
  3055. flag is clean when necessary.
  3056. Arguments:
  3057. ServiceContext - Supplies a pointer to the reset context which includes a
  3058. pointer to the device extension and the pathid to be reset.
  3059. Return Value:
  3060. TRUE - if the reset succeeds.
  3061. --*/
  3062. {
  3063. PRESET_CONTEXT resetContext = ServiceContext;
  3064. PFDO_EXTENSION deviceExtension;
  3065. PSCSI_REQUEST_BLOCK resetSrbToComplete;
  3066. BOOLEAN goodReset;
  3067. resetSrbToComplete = NULL;
  3068. deviceExtension = resetContext->DeviceExtension;
  3069. //
  3070. // Should never get a reset srb while one is in progress
  3071. //
  3072. if (resetContext->ResetSrb && deviceExtension->ResetSrb) {
  3073. ASSERT (resetContext->ResetSrb == deviceExtension->ResetSrb);
  3074. }
  3075. if (resetContext->NewResetSequence) {
  3076. //
  3077. // a new reset sequence to kill the reset in progress if any
  3078. //
  3079. if (deviceExtension->ResetCallAgain) {
  3080. DebugPrint ((0, "ATAPI: WARNING: Resetting a reset\n"));
  3081. deviceExtension->ResetCallAgain = 0;
  3082. if (deviceExtension->ResetSrb) {
  3083. resetSrbToComplete = deviceExtension->ResetSrb;
  3084. resetSrbToComplete->SrbStatus = SRB_STATUS_ERROR;
  3085. deviceExtension->ResetSrb = NULL;
  3086. }
  3087. }
  3088. deviceExtension->ResetSrb = resetContext->ResetSrb;
  3089. }
  3090. goodReset = AtapiResetController (
  3091. deviceExtension->HwDeviceExtension,
  3092. resetContext->PathId,
  3093. &deviceExtension->ResetCallAgain);
  3094. //
  3095. // Set the reset hold flag and start the counter if the reset is not done
  3096. //
  3097. if ((goodReset) && (deviceExtension->ResetCallAgain)) {
  3098. deviceExtension->InterruptData.InterruptFlags |= PD_RESET_HOLD;
  3099. deviceExtension->PortTimeoutCounter = PD_TIMER_RESET_HOLD_TIME;
  3100. } else {
  3101. CLRMASK (deviceExtension->InterruptData.InterruptFlags, PD_RESET_HOLD);
  3102. deviceExtension->PortTimeoutCounter = PD_TIMER_STOPPED;
  3103. if (deviceExtension->ResetSrb) {
  3104. resetSrbToComplete = deviceExtension->ResetSrb;
  3105. deviceExtension->ResetSrb = NULL;
  3106. }
  3107. if (resetSrbToComplete) {
  3108. if (goodReset) {
  3109. resetSrbToComplete->SrbStatus = SRB_STATUS_SUCCESS;
  3110. } else {
  3111. resetSrbToComplete->SrbStatus = SRB_STATUS_ERROR;
  3112. }
  3113. }
  3114. if (goodReset) {
  3115. IdePortNotification(IdeResetDetected,
  3116. deviceExtension->HwDeviceExtension,
  3117. resetSrbToComplete);
  3118. }
  3119. if (deviceExtension->InterruptData.InterruptFlags & PD_HELD_REQUEST) {
  3120. //
  3121. // Clear the held request flag and restart the request.
  3122. //
  3123. CLRMASK (deviceExtension->InterruptData.InterruptFlags, PD_HELD_REQUEST);
  3124. IdeStartIoSynchronized(deviceExtension->DeviceObject);
  3125. }
  3126. }
  3127. if (resetSrbToComplete) {
  3128. IdePortNotification(IdeRequestComplete,
  3129. deviceExtension->HwDeviceExtension,
  3130. resetSrbToComplete);
  3131. IdePortNotification(IdeNextRequest,
  3132. deviceExtension->HwDeviceExtension,
  3133. NULL);
  3134. }
  3135. //
  3136. // Check for miniport work requests.
  3137. //
  3138. if (deviceExtension->InterruptData.InterruptFlags & PD_NOTIFICATION_REQUIRED) {
  3139. //
  3140. // Queue a DPC.
  3141. //
  3142. IoRequestDpc(deviceExtension->DeviceObject, NULL, NULL);
  3143. }
  3144. return(TRUE);
  3145. }
  3146. VOID
  3147. IdeProcessCompletedRequest(
  3148. IN PFDO_EXTENSION DeviceExtension,
  3149. IN PSRB_DATA SrbData,
  3150. OUT PBOOLEAN CallStartIo
  3151. )
  3152. /*++
  3153. Routine Description:
  3154. This routine processes a request which has completed. It completes any
  3155. pending transfers, releases the adapter objects and map registers when
  3156. necessary. It deallocates any resources allocated for the request.
  3157. It processes the return status, by requeueing busy request, requesting
  3158. sense information or logging an error.
  3159. Arguments:
  3160. DeviceExtension - Supplies a pointer to the device extension for the
  3161. adapter data.
  3162. SrbData - Supplies a pointer to the SRB data block to be completed.
  3163. CallStartIo - This value is set if the start I/O routine needs to be
  3164. called.
  3165. Return Value:
  3166. None.
  3167. --*/
  3168. {
  3169. PLOGICAL_UNIT_EXTENSION logicalUnit;
  3170. PSCSI_REQUEST_BLOCK srb;
  3171. PIO_ERROR_LOG_PACKET errorLogPacket;
  3172. ULONG sequenceNumber;
  3173. LONG interlockResult;
  3174. PIRP irp;
  3175. PIO_STACK_LOCATION irpStack;
  3176. PHW_DEVICE_EXTENSION hwDeviceExtension = DeviceExtension->HwDeviceExtension;
  3177. ASSERT(SrbData->CurrentSrb);
  3178. srb = SrbData->CurrentSrb;
  3179. irp = srb->OriginalRequest;
  3180. DebugPrint((2,"CompletedRequest: Irp 0x%8x Srb 0x%8x DataBuf 0x%8x Len 0x%8x\n", irp, srb, srb->DataBuffer, srb->DataTransferLength));
  3181. #ifdef IDE_MULTIPLE_IRP_COMPLETE_REQUESTS_CHECK
  3182. if (irp->CurrentLocation > (CCHAR) (irp->StackCount + 1)) {
  3183. KeBugCheckEx( MULTIPLE_IRP_COMPLETE_REQUESTS, (ULONG_PTR) irp, (ULONG_PTR) srb, 0, 0 );
  3184. }
  3185. #endif // IDE_MULTIPLE_IRP_COMPLETE_REQUESTS_CHECK
  3186. irpStack = IoGetCurrentIrpStackLocation(irp);
  3187. //
  3188. // Get logical unit extension for this request.
  3189. //
  3190. logicalUnit = IDEPORT_GET_LUNEXT_IN_IRP (irpStack);
  3191. //
  3192. // If miniport needs mapped system addresses, the the
  3193. // data buffer address in the SRB must be restored to
  3194. // original unmapped virtual address. Ensure that this request requires
  3195. // a data transfer.
  3196. //
  3197. if (srb->SrbFlags & SRB_FLAGS_UNSPECIFIED_DIRECTION) {
  3198. if (!SRB_USES_DMA(srb)) {
  3199. if (irp->MdlAddress) {
  3200. //
  3201. // If an IRP is for a transfer larger than a miniport driver
  3202. // can handle, the request is broken up into multiple smaller
  3203. // requests. Each request uses the same MDL and the data
  3204. // buffer address field in the SRB may not be at the
  3205. // beginning of the memory described by the MDL.
  3206. //
  3207. srb->DataBuffer = (PCCHAR)MmGetMdlVirtualAddress(irp->MdlAddress) +
  3208. ((PCCHAR)srb->DataBuffer - SrbData->SrbDataOffset);
  3209. //
  3210. // Since this driver driver did programmaged I/O then the buffer
  3211. // needs to flushed if this an data-in transfer.
  3212. //
  3213. if (srb->SrbFlags & SRB_FLAGS_DATA_IN) {
  3214. KeFlushIoBuffers(irp->MdlAddress,
  3215. TRUE,
  3216. FALSE);
  3217. }
  3218. if (SrbData->Flags & SRB_DATA_RESERVED_PAGES) {
  3219. KeAcquireSpinLockAtDpcLevel(&DeviceExtension->SpinLock);
  3220. IdeUnmapReservedMapping(DeviceExtension, SrbData, irp->MdlAddress);
  3221. KeReleaseSpinLockFromDpcLevel(&DeviceExtension->SpinLock);
  3222. }
  3223. }
  3224. }
  3225. }
  3226. #ifdef LOG_GET_NEXT_CALLER
  3227. IdeLogCompletedCommand(DeviceExtension, srb);
  3228. #endif
  3229. //
  3230. // Clear the current request.
  3231. //
  3232. SrbData->CurrentSrb = NULL;
  3233. //
  3234. // If the no diconnect flag was set for this SRB, then check to see
  3235. // if IoStartNextPacket must be called.
  3236. //
  3237. if (srb->SrbFlags & SRB_FLAGS_DISABLE_DISCONNECT) {
  3238. //
  3239. // Acquire the spinlock to protect the flags strcuture.
  3240. //
  3241. KeAcquireSpinLockAtDpcLevel(&DeviceExtension->SpinLock);
  3242. //
  3243. // Set the disconnect running flag and check the busy flag.
  3244. //
  3245. DeviceExtension->Flags |= PD_DISCONNECT_RUNNING;
  3246. //
  3247. // The interrupt flags are checked unsynchonized. This works because
  3248. // the RESET_HOLD flag is cleared with the spinlock held and the
  3249. // counter is only set with the spinlock held. So the only case where
  3250. // there is a problem is is a reset occurs before this code get run,
  3251. // but this code runs before the timer is set for a reset hold;
  3252. // the timer will soon set for the new value.
  3253. //
  3254. if (!(DeviceExtension->InterruptData.InterruptFlags & PD_RESET_HOLD)) {
  3255. //
  3256. // The miniport is ready for the next request and there is not a
  3257. // pending reset hold, so clear the port timer.
  3258. //
  3259. DeviceExtension->PortTimeoutCounter = PD_TIMER_STOPPED;
  3260. }
  3261. //
  3262. // Release the spinlock.
  3263. //
  3264. KeReleaseSpinLockFromDpcLevel(&DeviceExtension->SpinLock);
  3265. if (!(DeviceExtension->Flags & PD_DEVICE_IS_BUSY) &&
  3266. !*CallStartIo &&
  3267. !(DeviceExtension->Flags & PD_PENDING_DEVICE_REQUEST)) {
  3268. //
  3269. // The busy flag is clear so the miniport has requested the
  3270. // next request. Call IoStartNextPacket.
  3271. //
  3272. IoStartNextPacket(DeviceExtension->DeviceObject, FALSE);
  3273. }
  3274. }
  3275. //
  3276. // Check if scatter/gather list came from pool.
  3277. //
  3278. if (srb->SrbFlags & SRB_FLAGS_SGLIST_FROM_POOL) {
  3279. CLRMASK (srb->SrbFlags, SRB_FLAGS_SGLIST_FROM_POOL);
  3280. }
  3281. //
  3282. // Acquire the spinlock to protect the flags structure,
  3283. // and the free of the srb extension.
  3284. //
  3285. KeAcquireSpinLockAtDpcLevel(&DeviceExtension->SpinLock);
  3286. //
  3287. // Move bytes transfered to IRP.
  3288. //
  3289. irp->IoStatus.Information = srb->DataTransferLength;
  3290. //
  3291. // Save the sequence number in case an error needs to be logged later.
  3292. //
  3293. sequenceNumber = SrbData->SequenceNumber;
  3294. SrbData->SequenceNumber = 0;
  3295. SrbData->ErrorLogRetryCount = 0;
  3296. #if DBG
  3297. SrbData = NULL;
  3298. #endif
  3299. if (DeviceExtension->Flags & PD_PENDING_DEVICE_REQUEST) {
  3300. //
  3301. // The start I/O routine needs to be called because it could not
  3302. // allocate an srb extension. Clear the pending flag and note
  3303. // that it needs to be called later.
  3304. //
  3305. CLRMASK (DeviceExtension->Flags, PD_PENDING_DEVICE_REQUEST);
  3306. *CallStartIo = TRUE;
  3307. }
  3308. //
  3309. // If success then start next packet.
  3310. // Not starting packet effectively
  3311. // freezes the queue.
  3312. //
  3313. if (SRB_STATUS(srb->SrbStatus) == SRB_STATUS_SUCCESS) {
  3314. ULONG srbFlags;
  3315. #if DBG
  3316. PVOID tag = irp;
  3317. #endif
  3318. irp->IoStatus.Status = STATUS_SUCCESS;
  3319. //
  3320. // save the srbFlags for later user
  3321. //
  3322. srbFlags = srb->SrbFlags;
  3323. if (srb->Function == SRB_FUNCTION_ATA_POWER_PASS_THROUGH) {
  3324. //
  3325. // must complete power irp before starting a new request
  3326. //
  3327. KeReleaseSpinLockFromDpcLevel(&DeviceExtension->SpinLock);
  3328. //
  3329. // Decrement the logUnitExtension reference count
  3330. //
  3331. UnrefLogicalUnitExtensionWithTag(
  3332. DeviceExtension,
  3333. logicalUnit,
  3334. tag
  3335. );
  3336. IoCompleteRequest(irp, IO_DISK_INCREMENT);
  3337. irp = NULL;
  3338. //
  3339. // we had a device state transition...restart the lu queue
  3340. //
  3341. KeAcquireSpinLockAtDpcLevel(&DeviceExtension->SpinLock);
  3342. GetNextLuRequest(DeviceExtension, logicalUnit);
  3343. } else {
  3344. //
  3345. // If the queue is being bypassed then keep the queue frozen.
  3346. // If there are outstanding requests as indicated by the timer
  3347. // being active then don't start the then next request.
  3348. //
  3349. if (!(srbFlags & SRB_FLAGS_BYPASS_FROZEN_QUEUE) &&
  3350. logicalUnit->RequestTimeoutCounter == PD_TIMER_STOPPED) {
  3351. //
  3352. // This is a normal request start the next packet.
  3353. //
  3354. GetNextLuRequest(DeviceExtension, logicalUnit);
  3355. } else {
  3356. //
  3357. // Release the spinlock.
  3358. //
  3359. KeReleaseSpinLockFromDpcLevel(&DeviceExtension->SpinLock);
  3360. }
  3361. }
  3362. DebugPrint((2,
  3363. "IdeProcessCompletedRequests: Iocompletion IRP %lx\n",
  3364. irp));
  3365. //
  3366. // Note that the retry count and sequence number are not cleared
  3367. // for completed packets which were generated by the port driver.
  3368. //
  3369. if (irp) {
  3370. //
  3371. // Decrement the logUnitExtension reference count
  3372. //
  3373. UnrefLogicalUnitExtensionWithTag(
  3374. DeviceExtension,
  3375. logicalUnit,
  3376. tag
  3377. );
  3378. IoCompleteRequest(irp, IO_DISK_INCREMENT);
  3379. }
  3380. return;
  3381. }
  3382. //
  3383. // Set IRP status. Class drivers will reset IRP status based
  3384. // on request sense if error.
  3385. //
  3386. irp->IoStatus.Status = IdeTranslateSrbStatus(srb);
  3387. DebugPrint((2, "IdeProcessCompletedRequests: Queue frozen TID %d\n",
  3388. srb->TargetId));
  3389. if ((srb->SrbStatus == SRB_STATUS_TIMEOUT) ||
  3390. (srb->SrbStatus == SRB_STATUS_BUS_RESET)) {
  3391. if (SRB_USES_DMA(srb)) {
  3392. ULONG errorCount;
  3393. //
  3394. // retry with PIO
  3395. //
  3396. DebugPrint ((DBG_ALWAYS, "ATAPI: retrying dma srb 0x%x with pio\n", srb));
  3397. MARK_SRB_AS_PIO_CANDIDATE(srb);
  3398. srb->SrbStatus = SRB_STATUS_PENDING;
  3399. srb->ScsiStatus = 0;
  3400. if (srb->SrbFlags & SRB_FLAGS_BYPASS_FROZEN_QUEUE) {
  3401. KeReleaseSpinLockFromDpcLevel(&DeviceExtension->SpinLock);
  3402. //
  3403. // iostart the fdo
  3404. //
  3405. IoStartPacket(DeviceExtension->DeviceObject, irp, (PULONG)NULL, NULL);
  3406. } else {
  3407. KeInsertByKeyDeviceQueue(&logicalUnit->DeviceObject->DeviceQueue,
  3408. &irp->Tail.Overlay.DeviceQueueEntry,
  3409. srb->QueueSortKey);
  3410. GetNextLuRequest(DeviceExtension, logicalUnit);
  3411. }
  3412. //
  3413. // spinlock is released.
  3414. //
  3415. //
  3416. // we got an error using DMA
  3417. //
  3418. errorCount = InterlockedIncrement(&logicalUnit->DmaTransferTimeoutCount);
  3419. if (errorCount == PDO_DMA_TIMEOUT_LIMIT) {
  3420. ERROR_LOG_ENTRY errorLogEntry;
  3421. ULONG i;
  3422. //
  3423. // Timeout errors need not be device specific. So no need to
  3424. // update the hall of shame
  3425. //
  3426. errorLogEntry.ErrorCode = SP_PROTOCOL_ERROR;
  3427. errorLogEntry.MajorFunctionCode = IRP_MJ_SCSI;
  3428. errorLogEntry.PathId = srb->PathId;
  3429. errorLogEntry.TargetId = srb->TargetId;
  3430. errorLogEntry.Lun = srb->Lun;
  3431. errorLogEntry.UniqueId = ERRLOGID_TOO_MANY_DMA_TIMEOUT;
  3432. errorLogEntry.ErrorLogRetryCount = errorCount;
  3433. errorLogEntry.SequenceNumber = 0;
  3434. LogErrorEntry(
  3435. DeviceExtension,
  3436. &errorLogEntry
  3437. );
  3438. //
  3439. // disable DMA
  3440. //
  3441. hwDeviceExtension->DeviceParameters[srb->TargetId].TransferModeMask |= DMA_SUPPORT;
  3442. DebugPrint ((DBG_ALWAYS,
  3443. "ATAPI ERROR: 0x%x target %d has too many DMA timeout, falling back to PIO\n",
  3444. DeviceExtension->IdeResource.TranslatedCommandBaseAddress,
  3445. srb->TargetId
  3446. ));
  3447. //
  3448. // rescan the bus to update transfer mode
  3449. //
  3450. #if defined (BUS_CHECK_ON_DMA_ERROR)
  3451. if (!(logicalUnit->LuFlags & PD_RESCAN_ACTIVE)) {
  3452. IoInvalidateDeviceRelations (
  3453. DeviceExtension->AttacheePdo,
  3454. BusRelations
  3455. );
  3456. }
  3457. #endif // BUS_CHECK_ON_DMA_ERROR
  3458. }
  3459. return;
  3460. } else {
  3461. if ((!TestForEnumProbing(srb)) &&
  3462. (srb->Function != SRB_FUNCTION_ATA_POWER_PASS_THROUGH) &&
  3463. (srb->Function != SRB_FUNCTION_ATA_PASS_THROUGH_EX) &&
  3464. (srb->Function != SRB_FUNCTION_ATA_PASS_THROUGH)) {
  3465. ULONG errorCount;
  3466. ULONG errorCountLimit;
  3467. //
  3468. // Check if were trying the flush the device cache
  3469. //
  3470. if ((srb->Function == SRB_FUNCTION_FLUSH) ||
  3471. (srb->Function == SRB_FUNCTION_SHUTDOWN) ||
  3472. (srb->Cdb[0] == SCSIOP_SYNCHRONIZE_CACHE)) {
  3473. errorCount = InterlockedIncrement(&logicalUnit->FlushCacheTimeoutCount);
  3474. DebugPrint((1,
  3475. "FlushCacheTimeout incremented to 0x%x\n",
  3476. errorCount
  3477. ));
  3478. //
  3479. // Disable flush on IDE devices
  3480. //
  3481. if (errorCount >= PDO_FLUSH_TIMEOUT_LIMIT ) {
  3482. hwDeviceExtension->
  3483. DeviceParameters[srb->TargetId].IdeFlushCommand = IDE_COMMAND_NO_FLUSH;
  3484. #ifdef ENABLE_48BIT_LBA
  3485. hwDeviceExtension->
  3486. DeviceParameters[srb->TargetId].IdeFlushCommandExt = IDE_COMMAND_NO_FLUSH;
  3487. #endif
  3488. }
  3489. ASSERT (errorCount <= PDO_FLUSH_TIMEOUT_LIMIT);
  3490. //
  3491. // looks like the device doesn't support flush cache
  3492. //
  3493. srb->SrbStatus = SRB_STATUS_SUCCESS;
  3494. irp->IoStatus.Status = STATUS_SUCCESS;
  3495. } else {
  3496. errorCount = InterlockedIncrement(&logicalUnit->ConsecutiveTimeoutCount);
  3497. DebugPrint ((DBG_ALWAYS, "0x%x target %d has 0x%x timeout errors so far\n",
  3498. logicalUnit->ParentDeviceExtension->IdeResource.TranslatedCommandBaseAddress,
  3499. logicalUnit->TargetId,
  3500. errorCount));
  3501. if ((errorCount == PDO_CONSECUTIVE_TIMEOUT_WARNING_LIMIT) &&
  3502. !(logicalUnit->LuFlags & PD_RESCAN_ACTIVE)) {
  3503. //
  3504. // the device not looking good
  3505. // make sure it is still there
  3506. //
  3507. IoInvalidateDeviceRelations (
  3508. DeviceExtension->AttacheePdo,
  3509. BusRelations
  3510. );
  3511. }
  3512. if (logicalUnit->PagingPathCount) {
  3513. errorCountLimit = PDO_CONSECUTIVE_PAGING_TIMEOUT_LIMIT;
  3514. } else {
  3515. errorCountLimit = PDO_CONSECUTIVE_TIMEOUT_LIMIT;
  3516. }
  3517. if (errorCount >= errorCountLimit) {
  3518. DebugPrint ((DBG_ALWAYS, "0x%x target %d has too many timeout. it is a goner...\n",
  3519. logicalUnit->ParentDeviceExtension->IdeResource.TranslatedCommandBaseAddress,
  3520. logicalUnit->TargetId));
  3521. //
  3522. // looks like the device is dead.
  3523. //
  3524. KeAcquireSpinLockAtDpcLevel(&logicalUnit->PdoSpinLock);
  3525. SETMASK (logicalUnit->PdoState, PDOS_DEADMEAT);
  3526. IdeLogDeadMeatReason( logicalUnit->DeadmeatRecord.Reason,
  3527. tooManyTimeout
  3528. );
  3529. KeReleaseSpinLockFromDpcLevel(&logicalUnit->PdoSpinLock);
  3530. if (!(logicalUnit->LuFlags & PD_RESCAN_ACTIVE)) {
  3531. IoInvalidateDeviceRelations (
  3532. DeviceExtension->AttacheePdo,
  3533. BusRelations
  3534. );
  3535. }
  3536. }
  3537. }
  3538. }
  3539. }
  3540. } else {
  3541. //
  3542. // reset error count
  3543. //
  3544. InterlockedExchange(&logicalUnit->ConsecutiveTimeoutCount, 0);
  3545. }
  3546. if (SRB_STATUS(srb->SrbStatus) == SRB_STATUS_PARITY_ERROR) {
  3547. ULONG errorCount;
  3548. errorCount = InterlockedIncrement(&logicalUnit->CrcErrorCount);
  3549. if (errorCount == PDO_UDMA_CRC_ERROR_LIMIT) {
  3550. ERROR_LOG_ENTRY errorLogEntry;
  3551. ULONG xferMode;
  3552. errorLogEntry.ErrorCode = SP_BUS_PARITY_ERROR;
  3553. errorLogEntry.MajorFunctionCode = IRP_MJ_SCSI;
  3554. errorLogEntry.PathId = srb->PathId;
  3555. errorLogEntry.TargetId = srb->TargetId;
  3556. errorLogEntry.Lun = srb->Lun;
  3557. errorLogEntry.UniqueId = ERRLOGID_TOO_MANY_CRC_ERROR;
  3558. errorLogEntry.ErrorLogRetryCount = errorCount;
  3559. errorLogEntry.SequenceNumber = 0;
  3560. LogErrorEntry(
  3561. DeviceExtension,
  3562. &errorLogEntry
  3563. );
  3564. //
  3565. //Procure the selected transfer mode again.
  3566. //
  3567. GetHighestDMATransferMode(hwDeviceExtension->DeviceParameters[srb->TargetId].TransferModeSelected,
  3568. xferMode);
  3569. //
  3570. //Gradual degradation.
  3571. //
  3572. if (xferMode > UDMA0) {
  3573. hwDeviceExtension->DeviceParameters[srb->TargetId].TransferModeMask |= (1 << xferMode);
  3574. } else if (xferMode == UDMA0) {
  3575. // Don't use MWDMA and SWDMA
  3576. hwDeviceExtension->DeviceParameters[srb->TargetId].TransferModeMask |= DMA_SUPPORT;
  3577. }
  3578. DebugPrint ((DBG_ALWAYS,
  3579. "ATAPI ERROR: 0x%x target %d has too many crc error, degrading to a lower DMA mode\n",
  3580. DeviceExtension->IdeResource.TranslatedCommandBaseAddress,
  3581. srb->TargetId
  3582. ));
  3583. //
  3584. // rescan the bus to update transfer mode
  3585. //
  3586. if (!(logicalUnit->LuFlags & PD_RESCAN_ACTIVE)) {
  3587. IoInvalidateDeviceRelations (
  3588. DeviceExtension->AttacheePdo,
  3589. BusRelations
  3590. );
  3591. }
  3592. }
  3593. }
  3594. if ((srb->ScsiStatus == SCSISTAT_BUSY ||
  3595. srb->SrbStatus == SRB_STATUS_BUSY ||
  3596. srb->ScsiStatus == SCSISTAT_QUEUE_FULL) &&
  3597. !(srb->SrbFlags & SRB_FLAGS_BYPASS_FROZEN_QUEUE)) {
  3598. //
  3599. // Perform busy processing if a busy type status was returned and this
  3600. // is not a by-pass request.
  3601. //
  3602. DebugPrint((1,
  3603. "SCSIPORT: Busy SRB status %x, SCSI status %x)\n",
  3604. srb->SrbStatus,
  3605. srb->ScsiStatus));
  3606. //
  3607. // If there is already a pending busy request or the queue is frozen
  3608. // then just requeue this request.
  3609. //
  3610. if (logicalUnit->LuFlags & (PD_LOGICAL_UNIT_IS_BUSY | PD_QUEUE_FROZEN)) {
  3611. DebugPrint((1,
  3612. "IdeProcessCompletedRequest: Requeuing busy request\n"));
  3613. srb->SrbStatus = SRB_STATUS_PENDING;
  3614. srb->ScsiStatus = 0;
  3615. if (!KeInsertByKeyDeviceQueue(&logicalUnit->DeviceObject->DeviceQueue,
  3616. &irp->Tail.Overlay.DeviceQueueEntry,
  3617. srb->QueueSortKey)) {
  3618. //
  3619. // This should never occur since there is a busy request.
  3620. //
  3621. srb->SrbStatus = SRB_STATUS_ERROR;
  3622. srb->ScsiStatus = SCSISTAT_BUSY;
  3623. ASSERT(FALSE);
  3624. goto BusyError;
  3625. }
  3626. //
  3627. // Release the spinlock.
  3628. //
  3629. KeReleaseSpinLockFromDpcLevel(&DeviceExtension->SpinLock);
  3630. } else if (logicalUnit->RetryCount++ < BUSY_RETRY_COUNT) {
  3631. //
  3632. // If busy status is returned, then indicate that the logical
  3633. // unit is busy. The timeout code will restart the request
  3634. // when it fires. Reset the status to pending.
  3635. //
  3636. srb->SrbStatus = SRB_STATUS_PENDING;
  3637. srb->ScsiStatus = 0;
  3638. logicalUnit->LuFlags |= PD_LOGICAL_UNIT_IS_BUSY;
  3639. logicalUnit->BusyRequest = irp;
  3640. if (logicalUnit->RetryCount == (BUSY_RETRY_COUNT/2) ) {
  3641. RESET_CONTEXT resetContext;
  3642. DebugPrint ((0,
  3643. "ATAPI: PDO 0x%x 0x%x seems to be DEAD. try a reset to bring it back.\n",
  3644. logicalUnit, logicalUnit->ParentDeviceExtension->IdeResource.TranslatedCommandBaseAddress
  3645. ));
  3646. resetContext.DeviceExtension = DeviceExtension;
  3647. resetContext.PathId = srb->PathId;
  3648. resetContext.NewResetSequence = TRUE;
  3649. resetContext.ResetSrb = NULL;
  3650. KeSynchronizeExecution(DeviceExtension->InterruptObject,
  3651. IdeResetBusSynchronized,
  3652. &resetContext);
  3653. #if DBG
  3654. IdeDebugHungControllerCounter = 0;
  3655. #endif // DBG
  3656. }
  3657. //
  3658. // Release the spinlock.
  3659. //
  3660. KeReleaseSpinLockFromDpcLevel(&DeviceExtension->SpinLock);
  3661. } else {
  3662. BusyError:
  3663. //
  3664. // Indicate the queue is frozen.
  3665. //
  3666. if (!(srb->SrbFlags & SRB_FLAGS_NO_QUEUE_FREEZE)) {
  3667. srb->SrbStatus |= SRB_STATUS_QUEUE_FROZEN;
  3668. logicalUnit->LuFlags |= PD_QUEUE_FROZEN;
  3669. }
  3670. //#if DBG
  3671. // if (logicalUnit->PdoState & PDOS_DEADMEAT) {
  3672. // DbgBreakPoint();
  3673. // }
  3674. //#endif
  3675. //
  3676. // Release the spinlock. Start the next request.
  3677. //
  3678. if (!(srb->SrbFlags & SRB_FLAGS_BYPASS_FROZEN_QUEUE) &&
  3679. logicalUnit->RequestTimeoutCounter == PD_TIMER_STOPPED) {
  3680. //
  3681. // This is a normal request start the next packet.
  3682. //
  3683. GetNextLuRequest(DeviceExtension, logicalUnit);
  3684. } else {
  3685. //
  3686. // Release the spinlock.
  3687. //
  3688. KeReleaseSpinLockFromDpcLevel(&DeviceExtension->SpinLock);
  3689. }
  3690. if (!TestForEnumProbing(srb)) {
  3691. //
  3692. // Log an a timeout erorr if we are not probing during bus-renum.
  3693. //
  3694. errorLogPacket = (PIO_ERROR_LOG_PACKET)
  3695. IoAllocateErrorLogEntry(DeviceExtension->DeviceObject,
  3696. sizeof(IO_ERROR_LOG_PACKET) + 4 * sizeof(ULONG));
  3697. if (errorLogPacket != NULL) {
  3698. errorLogPacket->ErrorCode = IO_ERR_NOT_READY;
  3699. errorLogPacket->SequenceNumber = sequenceNumber;
  3700. errorLogPacket->MajorFunctionCode =
  3701. IoGetCurrentIrpStackLocation(irp)->MajorFunction;
  3702. errorLogPacket->RetryCount = logicalUnit->RetryCount;
  3703. errorLogPacket->UniqueErrorValue = 259;
  3704. errorLogPacket->FinalStatus = STATUS_DEVICE_NOT_READY;
  3705. errorLogPacket->DumpDataSize = 5 * sizeof(ULONG);
  3706. errorLogPacket->DumpData[0] = srb->PathId;
  3707. errorLogPacket->DumpData[1] = srb->TargetId;
  3708. errorLogPacket->DumpData[2] = srb->Lun;
  3709. errorLogPacket->DumpData[3] = srb->ScsiStatus;
  3710. errorLogPacket->DumpData[4] = SP_REQUEST_TIMEOUT;
  3711. IoWriteErrorLogEntry(errorLogPacket);
  3712. }
  3713. }
  3714. irp->IoStatus.Status = STATUS_DEVICE_NOT_READY;
  3715. //
  3716. // Decrement the logUnitExtension reference count
  3717. //
  3718. UnrefLogicalUnitExtensionWithTag(
  3719. DeviceExtension,
  3720. logicalUnit,
  3721. irp
  3722. );
  3723. IoCompleteRequest(irp, IO_DISK_INCREMENT);
  3724. }
  3725. return;
  3726. }
  3727. //
  3728. // If the request sense data is valid, or none is needed and this request
  3729. // is not going to freeze the queue, then start the next request for this
  3730. // logical unit if it is idle.
  3731. //
  3732. if (!NEED_REQUEST_SENSE(srb) && srb->SrbFlags & SRB_FLAGS_NO_QUEUE_FREEZE) {
  3733. if (logicalUnit->RequestTimeoutCounter == PD_TIMER_STOPPED) {
  3734. GetNextLuRequest(DeviceExtension, logicalUnit);
  3735. //
  3736. // The spinlock is released by GetNextLuRequest.
  3737. //
  3738. } else {
  3739. //
  3740. // Release the spinlock.
  3741. //
  3742. KeReleaseSpinLockFromDpcLevel(&DeviceExtension->SpinLock);
  3743. }
  3744. } else {
  3745. //
  3746. // NOTE: This will also freeze the queue. For a case where there
  3747. // is no request sense.
  3748. //
  3749. // if (srb->SrbFlags & SRB_FLAGS_NO_QUEUE_FREEZE) {
  3750. // DebugPrint ((DBG_ALWAYS, "BAD BAD BAD: Freezing queue even with a no_queue_freeze request srb = 0x%x\n", srb));
  3751. // }
  3752. if (!(srb->SrbFlags & SRB_FLAGS_NO_QUEUE_FREEZE)) {
  3753. srb->SrbStatus |= SRB_STATUS_QUEUE_FROZEN;
  3754. logicalUnit->LuFlags |= PD_QUEUE_FROZEN;
  3755. }
  3756. //#if DBG
  3757. // if (logicalUnit->PdoState & PDOS_DEADMEAT) {
  3758. // DbgBreakPoint();
  3759. // }
  3760. //#endif
  3761. //
  3762. // Determine if a REQUEST SENSE command needs to be done.
  3763. // Check that a CHECK_CONDITION was received, an autosense has not
  3764. // been done already, and that autosense has been requested.
  3765. //
  3766. if (NEED_REQUEST_SENSE(srb)) {
  3767. srb->SrbStatus |= SRB_STATUS_QUEUE_FROZEN;
  3768. logicalUnit->LuFlags |= PD_QUEUE_FROZEN;
  3769. //
  3770. // If a request sense is going to be issued then any busy
  3771. // requests must be requeue so that the time out routine does
  3772. // not restart them while the request sense is being executed.
  3773. //
  3774. if (logicalUnit->LuFlags & PD_LOGICAL_UNIT_IS_BUSY) {
  3775. DebugPrint((1, "IdeProcessCompletedRequest: Requeueing busy request to allow request sense.\n"));
  3776. if (!KeInsertByKeyDeviceQueue(
  3777. &logicalUnit->DeviceObject->DeviceQueue,
  3778. &logicalUnit->BusyRequest->Tail.Overlay.DeviceQueueEntry,
  3779. srb->QueueSortKey)) {
  3780. //
  3781. // This should never occur since there is a busy request.
  3782. // Complete the current request without request sense
  3783. // informaiton.
  3784. //
  3785. ASSERT(FALSE);
  3786. DebugPrint((3, "IdeProcessCompletedRequests: Iocompletion IRP %lx\n", irp ));
  3787. //
  3788. // Release the spinlock.
  3789. //
  3790. KeReleaseSpinLockFromDpcLevel(&DeviceExtension->SpinLock);
  3791. //
  3792. // Decrement the logUnitExtension reference count
  3793. //
  3794. UnrefLogicalUnitExtensionWithTag(
  3795. DeviceExtension,
  3796. logicalUnit,
  3797. irp
  3798. );
  3799. IoCompleteRequest(irp, IO_DISK_INCREMENT);
  3800. return;
  3801. }
  3802. //
  3803. // Clear the busy flag.
  3804. //
  3805. CLRMASK (logicalUnit->LuFlags, PD_LOGICAL_UNIT_IS_BUSY | PD_QUEUE_IS_FULL);
  3806. }
  3807. //
  3808. // Release the spinlock.
  3809. //
  3810. KeReleaseSpinLockFromDpcLevel(&DeviceExtension->SpinLock);
  3811. //
  3812. // Call IssueRequestSense and it will complete the request
  3813. // after the REQUEST SENSE completes.
  3814. //
  3815. IssueRequestSense(logicalUnit, srb);
  3816. return;
  3817. }
  3818. //
  3819. // Release the spinlock.
  3820. //
  3821. KeReleaseSpinLockFromDpcLevel(&DeviceExtension->SpinLock);
  3822. }
  3823. //
  3824. // Decrement the logUnitExtension reference count
  3825. //
  3826. UnrefLogicalUnitExtensionWithTag(
  3827. DeviceExtension,
  3828. logicalUnit,
  3829. irp
  3830. );
  3831. IoCompleteRequest(irp, IO_DISK_INCREMENT);
  3832. }
  3833. PSRB_DATA
  3834. IdeGetSrbData(
  3835. IN PFDO_EXTENSION DeviceExtension,
  3836. IN PSCSI_REQUEST_BLOCK Srb
  3837. )
  3838. /*++
  3839. Routine Description:
  3840. This function returns the SRB data for the addressed unit.
  3841. Arguments:
  3842. DeviceExtension - Supplies a pointer to the device extension.
  3843. Srb - Supplies the scsi request block
  3844. Return Value:
  3845. Returns a pointer to the SRB data. NULL is returned if the address is not
  3846. valid.
  3847. --*/
  3848. {
  3849. PIRP irp;
  3850. PIO_STACK_LOCATION irpStack;
  3851. PLOGICAL_UNIT_EXTENSION logicalUnit;
  3852. irp = Srb->OriginalRequest;
  3853. if (irp == NULL) {
  3854. return NULL;
  3855. }
  3856. irpStack = IoGetCurrentIrpStackLocation(irp);
  3857. logicalUnit = IDEPORT_GET_LUNEXT_IN_IRP (irpStack);
  3858. if (logicalUnit == NULL) {
  3859. return NULL;
  3860. }
  3861. return &logicalUnit->SrbData;
  3862. }
  3863. VOID
  3864. IdeCompleteRequest(
  3865. IN PFDO_EXTENSION DeviceExtension,
  3866. IN PSRB_DATA SrbData,
  3867. IN UCHAR SrbStatus
  3868. )
  3869. /*++
  3870. Routine Description:
  3871. The routine completes the specified request.
  3872. Arguments:
  3873. DeviceExtension - Supplies a pointer to the device extension.
  3874. SrbData - Supplies a pointer to the SrbData for the request to be
  3875. completed.
  3876. Return Value:
  3877. None.
  3878. --*/
  3879. {
  3880. PSCSI_REQUEST_BLOCK srb;
  3881. //
  3882. // Make sure there is a current request.
  3883. //
  3884. ASSERT(SrbData->CurrentSrb);
  3885. srb = SrbData->CurrentSrb;
  3886. if (srb == NULL || !(srb->SrbFlags & SRB_FLAGS_IS_ACTIVE)) {
  3887. return;
  3888. }
  3889. //
  3890. // Update SRB status.
  3891. //
  3892. srb->SrbStatus = SrbStatus;
  3893. //
  3894. // Indicate no bytes transferred.
  3895. //
  3896. if (!SRB_USES_DMA(srb)) {
  3897. srb->DataTransferLength = 0;
  3898. } else {
  3899. // if we are doing DMA, preserve DataTransferLength.
  3900. // so retry will know how many bytes to transfer
  3901. }
  3902. //
  3903. // Call notification routine.
  3904. //
  3905. IdePortNotification(IdeRequestComplete,
  3906. (PVOID)(DeviceExtension + 1),
  3907. srb);
  3908. }
  3909. NTSTATUS
  3910. IdeSendMiniPortIoctl(
  3911. IN PFDO_EXTENSION DeviceExtension,
  3912. IN PIRP RequestIrp
  3913. )
  3914. /*++
  3915. Routine Description:
  3916. This function sends a miniport ioctl to the miniport driver.
  3917. It creates an srb which is processed normally by the port driver.
  3918. This call is synchronous.
  3919. Arguments:
  3920. DeviceExtension - Supplies a pointer the SCSI adapter device extension.
  3921. RequestIrp - Supplies a pointe to the Irp which made the original request.
  3922. Return Value:
  3923. Returns a status indicating the success or failure of the operation.
  3924. --*/
  3925. {
  3926. PIRP irp;
  3927. PIO_STACK_LOCATION irpStack;
  3928. PSRB_IO_CONTROL srbControl;
  3929. SCSI_REQUEST_BLOCK srb;
  3930. KEVENT event;
  3931. LARGE_INTEGER startingOffset;
  3932. IO_STATUS_BLOCK ioStatusBlock;
  3933. PLOGICAL_UNIT_EXTENSION logicalUnit;
  3934. ULONG outputLength;
  3935. ULONG length;
  3936. ULONG target;
  3937. IDE_PATH_ID pathId;
  3938. PAGED_CODE();
  3939. startingOffset.QuadPart = (LONGLONG) 1;
  3940. DebugPrint((3,"IdeSendMiniPortIoctl: Enter routine\n"));
  3941. //
  3942. // Get a pointer to the control block.
  3943. //
  3944. irpStack = IoGetCurrentIrpStackLocation(RequestIrp);
  3945. srbControl = RequestIrp->AssociatedIrp.SystemBuffer;
  3946. RequestIrp->IoStatus.Information = 0;
  3947. //
  3948. // check for kernel mode
  3949. //
  3950. if (RequestIrp->RequestorMode != KernelMode) {
  3951. RequestIrp->IoStatus.Status = STATUS_INVALID_PARAMETER;
  3952. return(STATUS_INVALID_PARAMETER);
  3953. }
  3954. //
  3955. // Validiate the user buffer.
  3956. //
  3957. if (irpStack->Parameters.DeviceIoControl.InputBufferLength <
  3958. sizeof(SRB_IO_CONTROL)) {
  3959. RequestIrp->IoStatus.Status = STATUS_INVALID_PARAMETER;
  3960. return(STATUS_INVALID_PARAMETER);
  3961. }
  3962. if (srbControl->HeaderLength != sizeof(SRB_IO_CONTROL)) {
  3963. RequestIrp->IoStatus.Status = STATUS_REVISION_MISMATCH;
  3964. return(STATUS_REVISION_MISMATCH);
  3965. }
  3966. length = srbControl->HeaderLength + srbControl->Length;
  3967. if ((length < srbControl->HeaderLength) ||
  3968. (length < srbControl->Length)) {
  3969. //
  3970. // total length overflows a ULONG
  3971. //
  3972. return(STATUS_INVALID_PARAMETER);
  3973. }
  3974. outputLength = irpStack->Parameters.DeviceIoControl.OutputBufferLength;
  3975. if (irpStack->Parameters.DeviceIoControl.OutputBufferLength < length &&
  3976. irpStack->Parameters.DeviceIoControl.InputBufferLength < length ) {
  3977. RequestIrp->IoStatus.Status = STATUS_BUFFER_TOO_SMALL;
  3978. return(STATUS_BUFFER_TOO_SMALL);
  3979. }
  3980. //
  3981. // Set the logical unit addressing to the first logical unit. This is
  3982. // merely used for addressing purposes.
  3983. //
  3984. pathId.l = 0;
  3985. while (logicalUnit = NextLogUnitExtensionWithTag(
  3986. DeviceExtension,
  3987. &pathId,
  3988. FALSE,
  3989. RequestIrp
  3990. )) {
  3991. //
  3992. // Walk the logical unit list to the end, looking for a safe one.
  3993. // If it was created for a rescan, it might be freed before this request is
  3994. // complete.
  3995. //
  3996. if (!(logicalUnit->LuFlags & PD_RESCAN_ACTIVE)) {
  3997. //
  3998. // Found a good one!
  3999. //
  4000. break;
  4001. }
  4002. UnrefLogicalUnitExtensionWithTag (
  4003. DeviceExtension,
  4004. logicalUnit,
  4005. RequestIrp
  4006. );
  4007. }
  4008. if (logicalUnit == NULL) {
  4009. RequestIrp->IoStatus.Status = STATUS_DEVICE_DOES_NOT_EXIST;
  4010. return(STATUS_DEVICE_DOES_NOT_EXIST);
  4011. }
  4012. //
  4013. // Initialize the notification event.
  4014. //
  4015. KeInitializeEvent(&event,
  4016. NotificationEvent,
  4017. FALSE);
  4018. //
  4019. // Build IRP for this request.
  4020. // Note we do this synchronously for two reasons. If it was done
  4021. // asynchonously then the completion code would have to make a special
  4022. // check to deallocate the buffer. Second if a completion routine were
  4023. // used then an additional IRP stack location would be needed.
  4024. //
  4025. irp = IoBuildSynchronousFsdRequest(
  4026. IRP_MJ_SCSI,
  4027. logicalUnit->DeviceObject,
  4028. srbControl,
  4029. length,
  4030. &startingOffset,
  4031. &event,
  4032. &ioStatusBlock);
  4033. if (irp==NULL) {
  4034. IdeLogNoMemoryError(DeviceExtension,
  4035. logicalUnit->TargetId,
  4036. NonPagedPool,
  4037. IoSizeOfIrp(logicalUnit->DeviceObject->StackSize),
  4038. IDEPORT_TAG_MPIOCTL_IRP
  4039. );
  4040. UnrefLogicalUnitExtensionWithTag (
  4041. DeviceExtension,
  4042. logicalUnit,
  4043. RequestIrp
  4044. );
  4045. RequestIrp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
  4046. return RequestIrp->IoStatus.Status;
  4047. }
  4048. irpStack = IoGetNextIrpStackLocation(irp);
  4049. //
  4050. // Set major and minor codes.
  4051. //
  4052. irpStack->MajorFunction = IRP_MJ_SCSI;
  4053. //
  4054. // Fill in SRB fields.
  4055. //
  4056. irpStack->Parameters.Others.Argument1 = &srb;
  4057. //
  4058. // Zero out the srb.
  4059. //
  4060. RtlZeroMemory(&srb, sizeof(SCSI_REQUEST_BLOCK));
  4061. srb.PathId = logicalUnit->PathId;
  4062. srb.TargetId = logicalUnit->TargetId;
  4063. srb.Lun = logicalUnit->Lun;
  4064. srb.Function = SRB_FUNCTION_IO_CONTROL;
  4065. srb.Length = sizeof(SCSI_REQUEST_BLOCK);
  4066. srb.SrbFlags = SRB_FLAGS_DATA_IN | SRB_FLAGS_NO_QUEUE_FREEZE;
  4067. srb.OriginalRequest = irp;
  4068. //
  4069. // Set timeout to requested value.
  4070. //
  4071. srb.TimeOutValue = srbControl->Timeout;
  4072. //
  4073. // Set the data buffer.
  4074. //
  4075. srb.DataBuffer = srbControl;
  4076. srb.DataTransferLength = length;
  4077. //
  4078. // Flush the data buffer for output. This will insure that the data is
  4079. // written back to memory. Since the data-in flag is the the port driver
  4080. // will flush the data again for input which will ensure the data is not
  4081. // in the cache.
  4082. //
  4083. KeFlushIoBuffers(irp->MdlAddress, FALSE, TRUE);
  4084. //
  4085. // Call port driver to handle this request.
  4086. //
  4087. IoCallDriver(logicalUnit->DeviceObject, irp);
  4088. //
  4089. // Wait for request to complete.
  4090. //
  4091. KeWaitForSingleObject(&event,
  4092. Executive,
  4093. KernelMode,
  4094. FALSE,
  4095. NULL);
  4096. //
  4097. // Set the information length to the smaller of the output buffer length
  4098. // and the length returned in the srb.
  4099. //
  4100. RequestIrp->IoStatus.Information = srb.DataTransferLength > outputLength ?
  4101. outputLength : srb.DataTransferLength;
  4102. RequestIrp->IoStatus.Status = ioStatusBlock.Status;
  4103. UnrefLogicalUnitExtensionWithTag (
  4104. DeviceExtension,
  4105. logicalUnit,
  4106. RequestIrp
  4107. );
  4108. return RequestIrp->IoStatus.Status;
  4109. }
  4110. NTSTATUS
  4111. IdeGetInquiryData(
  4112. IN PFDO_EXTENSION DeviceExtension,
  4113. IN PIRP Irp
  4114. )
  4115. /*++
  4116. Routine Description:
  4117. This functions copies the inquiry data to the system buffer. The data
  4118. is translate from the port driver's internal format to the user mode
  4119. format.
  4120. Arguments:
  4121. DeviceExtension - Supplies a pointer the SCSI adapter device extension.
  4122. Irp - Supplies a pointer to the Irp which made the original request.
  4123. Return Value:
  4124. Returns a status indicating the success or failure of the operation.
  4125. --*/
  4126. {
  4127. PUCHAR bufferStart;
  4128. PIO_STACK_LOCATION irpStack;
  4129. PSCSI_ADAPTER_BUS_INFO adapterInfo;
  4130. PSCSI_BUS_DATA busData;
  4131. PSCSI_INQUIRY_DATA inquiryData;
  4132. ULONG inquiryDataSize;
  4133. ULONG length;
  4134. ULONG numberOfBuses;
  4135. ULONG numberOfLus;
  4136. ULONG j;
  4137. PLOGICAL_UNIT_EXTENSION logUnitExtension;
  4138. IDE_PATH_ID pathId;
  4139. PAGED_CODE();
  4140. DebugPrint((3,"IdeGetInquiryData: Enter routine\n"));
  4141. //
  4142. // Get a pointer to the control block.
  4143. //
  4144. irpStack = IoGetCurrentIrpStackLocation(Irp);
  4145. bufferStart = Irp->AssociatedIrp.SystemBuffer;
  4146. numberOfBuses = MAX_IDE_BUS;
  4147. // this number could be changing...
  4148. // but we would always fill in the right info for the numLus.
  4149. numberOfLus = DeviceExtension->NumberOfLogicalUnits;
  4150. //
  4151. // Caculate the size of the logical unit structure and round it to a word
  4152. // alignment.
  4153. //
  4154. inquiryDataSize = ((sizeof(SCSI_INQUIRY_DATA) - 1 + INQUIRYDATABUFFERSIZE +
  4155. sizeof(ULONG) - 1) & ~(sizeof(ULONG) - 1));
  4156. // Based on the number of buses and logical unit, determine the minimum
  4157. // buffer length to hold all of the data.
  4158. //
  4159. length = sizeof(SCSI_ADAPTER_BUS_INFO) +
  4160. (numberOfBuses - 1) * sizeof(SCSI_BUS_DATA);
  4161. length += inquiryDataSize * numberOfLus;
  4162. if (irpStack->Parameters.DeviceIoControl.OutputBufferLength < length) {
  4163. Irp->IoStatus.Status = STATUS_BUFFER_TOO_SMALL;
  4164. return(STATUS_BUFFER_TOO_SMALL);
  4165. }
  4166. //
  4167. // zero out the buffer so that we don't return uninitialized
  4168. // memory
  4169. //
  4170. RtlZeroMemory(bufferStart, length);
  4171. //
  4172. // Set the information field.
  4173. //
  4174. Irp->IoStatus.Information = length;
  4175. //
  4176. // Fill in the bus information.
  4177. //
  4178. adapterInfo = (PSCSI_ADAPTER_BUS_INFO) bufferStart;
  4179. adapterInfo->NumberOfBuses = (UCHAR) numberOfBuses;
  4180. inquiryData = (PSCSI_INQUIRY_DATA)(bufferStart + sizeof(SCSI_ADAPTER_BUS_INFO) +
  4181. (numberOfBuses - 1) * sizeof(SCSI_BUS_DATA));
  4182. for (j = 0; j < numberOfBuses; j++) {
  4183. busData = &adapterInfo->BusData[j];
  4184. busData->NumberOfLogicalUnits = 0;
  4185. busData->InitiatorBusId = IDE_PSUEDO_INITIATOR_ID;
  4186. //
  4187. // Copy the data for the logical units.
  4188. //
  4189. busData->InquiryDataOffset = (ULONG)((PUCHAR) inquiryData - bufferStart);
  4190. pathId.l = 0;
  4191. pathId.b.Path = j;
  4192. while (logUnitExtension = NextLogUnitExtensionWithTag (
  4193. DeviceExtension,
  4194. &pathId,
  4195. TRUE,
  4196. Irp
  4197. )) {
  4198. INQUIRYDATA InquiryData;
  4199. NTSTATUS status;
  4200. if (pathId.b.Path != j) {
  4201. UnrefLogicalUnitExtensionWithTag (
  4202. DeviceExtension,
  4203. logUnitExtension,
  4204. Irp
  4205. );
  4206. break;
  4207. }
  4208. inquiryData->PathId = logUnitExtension->PathId;
  4209. inquiryData->TargetId = logUnitExtension->TargetId;
  4210. inquiryData->Lun = logUnitExtension->Lun;
  4211. inquiryData->DeviceClaimed = (BOOLEAN) (logUnitExtension->PdoState & PDOS_DEVICE_CLIAMED);
  4212. inquiryData->InquiryDataLength = INQUIRYDATABUFFERSIZE;
  4213. inquiryData->NextInquiryDataOffset = (ULONG)((PUCHAR) inquiryData +
  4214. inquiryDataSize - bufferStart);
  4215. status = IssueInquirySafe(logUnitExtension->ParentDeviceExtension, logUnitExtension, &InquiryData, FALSE);
  4216. if (NT_SUCCESS(status) || (status == STATUS_DATA_OVERRUN)) {
  4217. RtlCopyMemory(
  4218. inquiryData->InquiryData,
  4219. &InquiryData,
  4220. INQUIRYDATABUFFERSIZE
  4221. );
  4222. }
  4223. inquiryData = (PSCSI_INQUIRY_DATA) ((PCHAR) inquiryData + inquiryDataSize);
  4224. UnrefLogicalUnitExtensionWithTag (
  4225. DeviceExtension,
  4226. logUnitExtension,
  4227. Irp
  4228. );
  4229. busData->NumberOfLogicalUnits++;
  4230. if (busData->NumberOfLogicalUnits >= (UCHAR) numberOfLus) {
  4231. break;
  4232. }
  4233. }
  4234. //
  4235. // Fix up the last entry of the list.
  4236. //
  4237. if (busData->NumberOfLogicalUnits == 0) {
  4238. busData->InquiryDataOffset = 0;
  4239. } else {
  4240. ((PSCSI_INQUIRY_DATA) ((PCHAR) inquiryData - inquiryDataSize))->
  4241. NextInquiryDataOffset = 0;
  4242. }
  4243. }
  4244. Irp->IoStatus.Status = STATUS_SUCCESS;
  4245. return(STATUS_SUCCESS);
  4246. }
  4247. NTSTATUS
  4248. IdeSendScsiPassThrough (
  4249. IN PFDO_EXTENSION DeviceExtension,
  4250. IN PIRP RequestIrp,
  4251. IN BOOLEAN Direct
  4252. )
  4253. /*++
  4254. Routine Description;
  4255. Validates the scsi pass through structure and invokes
  4256. IdeSendValidScsiPassThru to service the request.
  4257. Arguments:
  4258. DeviceExtension : Fdo Extension.
  4259. RequestIrp : The irp containing the scsi pass through request
  4260. Return Value:
  4261. The irp's io status
  4262. --*/
  4263. {
  4264. PIO_STACK_LOCATION irpStack;
  4265. NTSTATUS status;
  4266. UCHAR pathId;
  4267. UCHAR targetId;
  4268. UCHAR lun;
  4269. PAGED_CODE();
  4270. status = PortGetPassThroughAddress (RequestIrp,
  4271. &pathId,
  4272. &targetId,
  4273. &lun
  4274. );
  4275. if (NT_SUCCESS(status)) {
  4276. PLOGICAL_UNIT_EXTENSION logicalUnit;
  4277. //
  4278. // If this request came through a normal device control rather than from
  4279. // class driver then the device must exist and be unclaimed. Class drivers
  4280. // will set the minor function code for the device control. It is always
  4281. // zero for a user request.
  4282. //
  4283. logicalUnit = RefLogicalUnitExtensionWithTag(DeviceExtension,
  4284. pathId,
  4285. targetId,
  4286. lun,
  4287. FALSE,
  4288. RequestIrp
  4289. );
  4290. if (logicalUnit) {
  4291. irpStack = IoGetCurrentIrpStackLocation(RequestIrp);
  4292. if (irpStack->MinorFunction == 0) {
  4293. if (logicalUnit->PdoState & PDOS_DEVICE_CLIAMED) {
  4294. UnrefLogicalUnitExtensionWithTag(
  4295. DeviceExtension,
  4296. logicalUnit,
  4297. RequestIrp
  4298. );
  4299. logicalUnit = NULL;
  4300. }
  4301. }
  4302. }
  4303. if (logicalUnit != NULL) {
  4304. status = PortSendPassThrough( logicalUnit->DeviceObject,
  4305. RequestIrp,
  4306. Direct,
  4307. 0,
  4308. &DeviceExtension->Capabilities
  4309. );
  4310. UnrefLogicalUnitExtensionWithTag(
  4311. DeviceExtension,
  4312. logicalUnit,
  4313. RequestIrp
  4314. );
  4315. } else {
  4316. status = STATUS_INVALID_PARAMETER;
  4317. }
  4318. }
  4319. return status;
  4320. }
  4321. VOID
  4322. SyncAtaPassThroughCompletionRoutine (
  4323. IN PDEVICE_OBJECT DeviceObject,
  4324. IN PVOID Context,
  4325. IN NTSTATUS Status
  4326. )
  4327. {
  4328. PSYNC_ATA_PASSTHROUGH_CONTEXT context = Context;
  4329. context->Status = Status;
  4330. KeSetEvent (&context->Event, 0, FALSE);
  4331. }
  4332. //
  4333. // <= DISPATCH_LEVEL
  4334. //
  4335. NTSTATUS
  4336. IssueAsyncAtaPassThroughSafe (
  4337. IN PFDO_EXTENSION DeviceExtension,
  4338. IN PLOGICAL_UNIT_EXTENSION LogUnitExtension,
  4339. IN OUT PATA_PASS_THROUGH AtaPassThroughData,
  4340. IN BOOLEAN DataIn,
  4341. IN ASYNC_PASS_THROUGH_COMPLETION Completion,
  4342. IN PVOID CallerContext,
  4343. IN BOOLEAN PowerRelated,
  4344. IN ULONG TimeOut,
  4345. IN BOOLEAN MustSucceed
  4346. )
  4347. {
  4348. PIRP irp;
  4349. PIO_STACK_LOCATION irpStack;
  4350. IO_STATUS_BLOCK ioStatusBlock;
  4351. KIRQL currentIrql;
  4352. NTSTATUS status;
  4353. PSCSI_REQUEST_BLOCK srb;
  4354. PSENSE_DATA senseInfoBuffer;
  4355. ULONG totalBufferSize;
  4356. PATA_PASSTHROUGH_CONTEXT context;
  4357. PENUMERATION_STRUCT enumStruct;
  4358. status = STATUS_UNSUCCESSFUL;
  4359. senseInfoBuffer = NULL;
  4360. srb = NULL;
  4361. irp = NULL;
  4362. if (MustSucceed) {
  4363. enumStruct = DeviceExtension->PreAllocEnumStruct;
  4364. if (enumStruct == NULL) {
  4365. ASSERT (DeviceExtension->PreAllocEnumStruct);
  4366. //
  4367. // Fall back to the usual course of action
  4368. //
  4369. MustSucceed=FALSE;
  4370. } else {
  4371. context = enumStruct->Context;
  4372. ASSERT (context);
  4373. senseInfoBuffer = enumStruct->SenseInfoBuffer;
  4374. ASSERT (senseInfoBuffer);
  4375. srb = enumStruct->Srb;
  4376. ASSERT (srb);
  4377. totalBufferSize = FIELD_OFFSET(ATA_PASS_THROUGH, DataBuffer) + AtaPassThroughData->DataBufferSize;
  4378. irp = enumStruct->Irp1;
  4379. ASSERT (irp);
  4380. //
  4381. // this irp has always a stack size of 1. Use the same
  4382. // stack size when initializing the irp.
  4383. //
  4384. IoInitializeIrp(irp,
  4385. IoSizeOfIrp(PREALLOC_STACK_LOCATIONS),
  4386. PREALLOC_STACK_LOCATIONS);
  4387. irp->MdlAddress = enumStruct->MdlAddress;
  4388. ASSERT (enumStruct->DataBufferSize >= totalBufferSize);
  4389. RtlCopyMemory(enumStruct->DataBuffer, AtaPassThroughData, totalBufferSize);
  4390. }
  4391. }
  4392. if (!MustSucceed) {
  4393. context = ExAllocatePool(NonPagedPool, sizeof (ATA_PASSTHROUGH_CONTEXT));
  4394. if (context == NULL) {
  4395. DebugPrint((1,"IssueAsyncAtaPassThrough: Can't allocate context buffer\n"));
  4396. IdeLogNoMemoryError(DeviceExtension,
  4397. LogUnitExtension->TargetId,
  4398. NonPagedPool,
  4399. sizeof(ATA_PASSTHROUGH_CONTEXT),
  4400. (IDEPORT_TAG_ATAPASS_CONTEXT+AtaPassThroughData->IdeReg.bCommandReg)
  4401. );
  4402. status = STATUS_INSUFFICIENT_RESOURCES;
  4403. goto GetOut;
  4404. }
  4405. senseInfoBuffer = ExAllocatePool( NonPagedPoolCacheAligned, SENSE_BUFFER_SIZE);
  4406. if (senseInfoBuffer == NULL) {
  4407. DebugPrint((1,"IssueAsyncAtaPassThrough: Can't allocate request sense buffer\n"));
  4408. IdeLogNoMemoryError(DeviceExtension,
  4409. LogUnitExtension->TargetId,
  4410. NonPagedPoolCacheAligned,
  4411. SENSE_BUFFER_SIZE,
  4412. (IDEPORT_TAG_ATAPASS_SENSE+AtaPassThroughData->IdeReg.bCommandReg)
  4413. );
  4414. status = STATUS_INSUFFICIENT_RESOURCES;
  4415. goto GetOut;
  4416. }
  4417. srb = ExAllocatePool (NonPagedPool, sizeof (SCSI_REQUEST_BLOCK));
  4418. if (srb == NULL) {
  4419. DebugPrint((1,"IssueAsyncAtaPassThrough: Can't SRB\n"));
  4420. IdeLogNoMemoryError(DeviceExtension,
  4421. LogUnitExtension->TargetId,
  4422. NonPagedPool,
  4423. sizeof(SCSI_REQUEST_BLOCK),
  4424. (IDEPORT_TAG_ATAPASS_SRB+AtaPassThroughData->IdeReg.bCommandReg)
  4425. );
  4426. status = STATUS_INSUFFICIENT_RESOURCES;
  4427. goto GetOut;
  4428. }
  4429. totalBufferSize = FIELD_OFFSET(ATA_PASS_THROUGH, DataBuffer) + AtaPassThroughData->DataBufferSize;
  4430. //
  4431. // Build IRP for this request.
  4432. //
  4433. irp = IoAllocateIrp (
  4434. (CCHAR) (LogUnitExtension->DeviceObject->StackSize),
  4435. FALSE
  4436. );
  4437. if (irp == NULL) {
  4438. IdeLogNoMemoryError(DeviceExtension,
  4439. LogUnitExtension->TargetId,
  4440. NonPagedPool,
  4441. IoSizeOfIrp(LogUnitExtension->DeviceObject->StackSize),
  4442. (IDEPORT_TAG_ATAPASS_IRP+AtaPassThroughData->IdeReg.bCommandReg)
  4443. );
  4444. status = STATUS_INSUFFICIENT_RESOURCES;
  4445. goto GetOut;
  4446. }
  4447. irp->MdlAddress = IoAllocateMdl( AtaPassThroughData,
  4448. totalBufferSize,
  4449. FALSE,
  4450. FALSE,
  4451. (PIRP) NULL );
  4452. if (irp->MdlAddress == NULL) {
  4453. IdeLogNoMemoryError(DeviceExtension,
  4454. LogUnitExtension->TargetId,
  4455. NonPagedPool,
  4456. totalBufferSize,
  4457. (IDEPORT_TAG_ATAPASS_MDL+AtaPassThroughData->IdeReg.bCommandReg)
  4458. );
  4459. status = STATUS_INSUFFICIENT_RESOURCES;
  4460. goto GetOut;
  4461. }
  4462. MmBuildMdlForNonPagedPool(irp->MdlAddress);
  4463. }
  4464. irpStack = IoGetNextIrpStackLocation(irp);
  4465. irpStack->MajorFunction = IRP_MJ_SCSI;
  4466. //
  4467. // Fill in SRB fields.
  4468. //
  4469. RtlZeroMemory(srb, sizeof(SCSI_REQUEST_BLOCK));
  4470. irpStack->Parameters.Scsi.Srb = srb;
  4471. srb->PathId = LogUnitExtension->PathId;
  4472. srb->TargetId = LogUnitExtension->TargetId;
  4473. srb->Lun = LogUnitExtension->Lun;
  4474. if (PowerRelated) {
  4475. srb->Function = SRB_FUNCTION_ATA_POWER_PASS_THROUGH;
  4476. srb->QueueSortKey = MAXULONG;
  4477. } else {
  4478. srb->Function = SRB_FUNCTION_ATA_PASS_THROUGH;
  4479. srb->QueueSortKey = 0;
  4480. }
  4481. srb->Length = sizeof(SCSI_REQUEST_BLOCK);
  4482. //
  4483. // Set flags to disable synchronous negociation.
  4484. //
  4485. srb->SrbFlags = SRB_FLAGS_DATA_IN | SRB_FLAGS_DISABLE_SYNCH_TRANSFER;
  4486. srb->SrbFlags |= DataIn ? 0 : SRB_FLAGS_DATA_OUT;
  4487. if (AtaPassThroughData->IdeReg.bReserved & ATA_PTFLAGS_URGENT) {
  4488. srb->SrbFlags |= SRB_FLAGS_BYPASS_FROZEN_QUEUE;
  4489. }
  4490. srb->SrbStatus = srb->ScsiStatus = 0;
  4491. srb->NextSrb = 0;
  4492. srb->OriginalRequest = irp;
  4493. //
  4494. // Set timeout to 15 seconds.
  4495. //
  4496. srb->TimeOutValue = TimeOut;
  4497. srb->CdbLength = 6;
  4498. //
  4499. // Enable auto request sense.
  4500. //
  4501. srb->SenseInfoBuffer = senseInfoBuffer;
  4502. srb->SenseInfoBufferLength = SENSE_BUFFER_SIZE;
  4503. srb->DataBuffer = MmGetMdlVirtualAddress(irp->MdlAddress);
  4504. srb->DataTransferLength = totalBufferSize;
  4505. IoSetCompletionRoutine(
  4506. irp,
  4507. AtaPassThroughCompletionRoutine,
  4508. context,
  4509. TRUE,
  4510. TRUE,
  4511. TRUE
  4512. );
  4513. context->DeviceObject = LogUnitExtension->DeviceObject;
  4514. context->CallerCompletion = Completion;
  4515. context->CallerContext = CallerContext;
  4516. context->SenseInfoBuffer = senseInfoBuffer;
  4517. context->Srb = srb;
  4518. context->MustSucceed = MustSucceed? 1 : 0;
  4519. context->DataBuffer = AtaPassThroughData;
  4520. //
  4521. // send the pass through irp
  4522. //
  4523. status = IoCallDriver(LogUnitExtension->DeviceObject, irp);
  4524. //
  4525. // always return STATUS_PENDING when we actually send out the irp
  4526. //
  4527. return STATUS_PENDING;
  4528. GetOut:
  4529. ASSERT (!MustSucceed);
  4530. if (context) {
  4531. ExFreePool (context);
  4532. }
  4533. if (senseInfoBuffer) {
  4534. ExFreePool (senseInfoBuffer);
  4535. }
  4536. if (srb) {
  4537. ExFreePool (srb);
  4538. }
  4539. if (irp && irp->MdlAddress) {
  4540. IoFreeMdl (irp->MdlAddress);
  4541. }
  4542. if (irp) {
  4543. IoFreeIrp( irp );
  4544. }
  4545. return status;
  4546. } // IssueAtaPassThrough
  4547. NTSTATUS
  4548. IssueSyncAtaPassThroughSafe (
  4549. IN PFDO_EXTENSION DeviceExtension,
  4550. IN PLOGICAL_UNIT_EXTENSION LogUnitExtension,
  4551. IN OUT PATA_PASS_THROUGH AtaPassThroughData,
  4552. IN BOOLEAN DataIn,
  4553. IN BOOLEAN PowerRelated,
  4554. IN ULONG TimeOut,
  4555. IN BOOLEAN MustSucceed
  4556. )
  4557. {
  4558. NTSTATUS status;
  4559. SYNC_ATA_PASSTHROUGH_CONTEXT context;
  4560. ULONG retryCount=10;
  4561. ULONG locked;
  4562. status=STATUS_INSUFFICIENT_RESOURCES;
  4563. if (MustSucceed) {
  4564. //Lock
  4565. ASSERT(InterlockedCompareExchange(&(DeviceExtension->EnumStructLock), 1, 0) == 0);
  4566. }
  4567. while ((status == STATUS_UNSUCCESSFUL || status == STATUS_INSUFFICIENT_RESOURCES) && retryCount--) {
  4568. //
  4569. // Initialize the notification event.
  4570. //
  4571. KeInitializeEvent(&context.Event,
  4572. NotificationEvent,
  4573. FALSE);
  4574. status = IssueAsyncAtaPassThroughSafe (
  4575. DeviceExtension,
  4576. LogUnitExtension,
  4577. AtaPassThroughData,
  4578. DataIn,
  4579. SyncAtaPassThroughCompletionRoutine,
  4580. &context,
  4581. PowerRelated,
  4582. TimeOut,
  4583. MustSucceed
  4584. );
  4585. if (status == STATUS_PENDING) {
  4586. KeWaitForSingleObject(&context.Event,
  4587. Executive,
  4588. KernelMode,
  4589. FALSE,
  4590. NULL);
  4591. status=context.Status;
  4592. }
  4593. if (status == STATUS_UNSUCCESSFUL) {
  4594. DebugPrint((1, "Retrying flushed request\n"));
  4595. }
  4596. }
  4597. if (MustSucceed) {
  4598. //Unlock
  4599. ASSERT(InterlockedCompareExchange(&(DeviceExtension->EnumStructLock), 0, 1) == 1);
  4600. }
  4601. if (NT_SUCCESS(status)) {
  4602. return context.Status;
  4603. } else {
  4604. return status;
  4605. }
  4606. }
  4607. NTSTATUS
  4608. AtaPassThroughCompletionRoutine(
  4609. PDEVICE_OBJECT DeviceObject,
  4610. PIRP Irp,
  4611. PVOID Context
  4612. )
  4613. {
  4614. PATA_PASSTHROUGH_CONTEXT context = Context;
  4615. PATA_PASS_THROUGH ataPassThroughData;
  4616. DebugPrint((1, "AtaPassThroughCompletionRoutine: Irp = 0x%x status=%x\n",
  4617. Irp, Irp->IoStatus.Status));
  4618. if (context->Srb->SrbStatus & SRB_STATUS_QUEUE_FROZEN) {
  4619. PLOGICAL_UNIT_EXTENSION logicalUnit;
  4620. KIRQL currentIrql;
  4621. DebugPrint((1, "AtaPassThroughCompletionRoutine: Unfreeze Queue TID %d\n",
  4622. context->Srb->TargetId));
  4623. logicalUnit = context->DeviceObject->DeviceExtension;
  4624. ASSERT (logicalUnit);
  4625. CLRMASK (logicalUnit->LuFlags, PD_QUEUE_FROZEN);
  4626. KeAcquireSpinLock(&logicalUnit->ParentDeviceExtension->SpinLock, &currentIrql);
  4627. GetNextLuRequest(logicalUnit->ParentDeviceExtension, logicalUnit);
  4628. KeLowerIrql(currentIrql);
  4629. }
  4630. ataPassThroughData = (PATA_PASS_THROUGH) context->Srb->DataBuffer;
  4631. if (ataPassThroughData->IdeReg.bReserved & ATA_PTFLAGS_OK_TO_FAIL) {
  4632. Irp->IoStatus.Status = STATUS_SUCCESS;
  4633. }
  4634. if (context->MustSucceed) {
  4635. RtlCopyMemory(context->DataBuffer,
  4636. context->Srb->DataBuffer, context->Srb->DataTransferLength);
  4637. DebugPrint((1, "AtaCompletionSafe: Device =%x, Status= %x, SrbStatus=%x\n",
  4638. context->Srb->TargetId, Irp->IoStatus.Status, context->Srb->SrbStatus));
  4639. }
  4640. if (context->CallerCompletion) {
  4641. context->CallerCompletion (context->DeviceObject, context->CallerContext, Irp->IoStatus.Status);
  4642. }
  4643. if (context->MustSucceed) {
  4644. return STATUS_MORE_PROCESSING_REQUIRED;
  4645. }
  4646. ExFreePool (context->SenseInfoBuffer);
  4647. ExFreePool (context->Srb);
  4648. ExFreePool (context);
  4649. if (Irp->MdlAddress) {
  4650. IoFreeMdl (Irp->MdlAddress);
  4651. }
  4652. IoFreeIrp (Irp);
  4653. return STATUS_MORE_PROCESSING_REQUIRED;
  4654. }
  4655. NTSTATUS
  4656. IdeClaimLogicalUnit(
  4657. IN PFDO_EXTENSION DeviceExtension,
  4658. IN PIRP Irp
  4659. )
  4660. /*++
  4661. Routine Description:
  4662. This function finds the specified device in the logical unit information
  4663. and either updates the device object point or claims the device. If the
  4664. device is already claimed, then the request fails. If the request succeeds,
  4665. then the current device object is returned in the data buffer pointer
  4666. of the SRB.
  4667. Arguments:
  4668. DeviceExtension - Supplies a pointer the SCSI adapter device extension.
  4669. Irp - Supplies a pointer to the Irp which made the original request.
  4670. Return Value:
  4671. Returns the status of the operation. Either success, no device or busy.
  4672. --*/
  4673. {
  4674. KIRQL currentIrql;
  4675. PIO_STACK_LOCATION irpStack;
  4676. PSCSI_REQUEST_BLOCK srb;
  4677. PDEVICE_OBJECT saveDevice;
  4678. PPDO_EXTENSION pdoExtension;
  4679. PVOID sectionHandle;
  4680. PAGED_CODE();
  4681. //
  4682. // Get SRB address from current IRP stack.
  4683. //
  4684. irpStack = IoGetCurrentIrpStackLocation(Irp);
  4685. srb = (PSCSI_REQUEST_BLOCK) irpStack->Parameters.Others.Argument1;
  4686. pdoExtension = IDEPORT_GET_LUNEXT_IN_IRP (irpStack);
  4687. ASSERT (pdoExtension);
  4688. #ifdef ALLOC_PRAGMA
  4689. sectionHandle = MmLockPagableCodeSection(IdeClaimLogicalUnit);
  4690. #endif
  4691. //
  4692. // Lock the data.
  4693. //
  4694. KeAcquireSpinLock(&pdoExtension->PdoSpinLock, &currentIrql);
  4695. if (srb->Function == SRB_FUNCTION_RELEASE_DEVICE) {
  4696. CLRMASK (pdoExtension->PdoState, PDOS_DEVICE_CLIAMED | PDOS_LEGACY_ATTACHER);
  4697. KeReleaseSpinLock(&pdoExtension->PdoSpinLock, currentIrql);
  4698. srb->SrbStatus = SRB_STATUS_SUCCESS;
  4699. #ifdef ALLOC_PRAGMA
  4700. MmUnlockPagableImageSection(sectionHandle);
  4701. #endif
  4702. return(STATUS_SUCCESS);
  4703. }
  4704. //
  4705. // Check for a claimed device.
  4706. //
  4707. if (pdoExtension->PdoState & PDOS_DEVICE_CLIAMED) {
  4708. KeReleaseSpinLock(&pdoExtension->PdoSpinLock, currentIrql);
  4709. srb->SrbStatus = SRB_STATUS_BUSY;
  4710. #ifdef ALLOC_PRAGMA
  4711. MmUnlockPagableImageSection(sectionHandle);
  4712. #endif
  4713. return(STATUS_DEVICE_BUSY);
  4714. }
  4715. //
  4716. // Save the current device object.
  4717. //
  4718. saveDevice = pdoExtension->AttacherDeviceObject;
  4719. //
  4720. // Update the lun information based on the operation type.
  4721. //
  4722. if (srb->Function == SRB_FUNCTION_CLAIM_DEVICE) {
  4723. pdoExtension->PdoState |= PDOS_DEVICE_CLIAMED;
  4724. }
  4725. if (srb->Function == SRB_FUNCTION_ATTACH_DEVICE) {
  4726. pdoExtension->AttacherDeviceObject = srb->DataBuffer;
  4727. }
  4728. srb->DataBuffer = saveDevice;
  4729. if (irpStack->DeviceObject == pdoExtension->ParentDeviceExtension->DeviceObject) {
  4730. //
  4731. // The original irp is sent to the parent. The attacher must
  4732. // be legacy class driver. We can never do pnp remove safely.
  4733. //
  4734. pdoExtension->PdoState |= PDOS_LEGACY_ATTACHER;
  4735. }
  4736. KeReleaseSpinLock(&pdoExtension->PdoSpinLock, currentIrql);
  4737. srb->SrbStatus = SRB_STATUS_SUCCESS;
  4738. #ifdef ALLOC_PRAGMA
  4739. MmUnlockPagableImageSection(sectionHandle);
  4740. #endif
  4741. return(STATUS_SUCCESS);
  4742. }
  4743. NTSTATUS
  4744. IdeRemoveDevice(
  4745. IN PFDO_EXTENSION DeviceExtension,
  4746. IN PIRP Irp
  4747. )
  4748. /*++
  4749. Routine Description:
  4750. This function finds the specified device in the logical unit information
  4751. and deletes it. This is done in preparation for a failing device to be
  4752. physically removed from a SCSI bus. An assumption is that the system
  4753. utility controlling the device removal has locked the volumes so there
  4754. is no outstanding IO to this device.
  4755. Arguments:
  4756. DeviceExtension - Supplies a pointer the SCSI adapter device extension.
  4757. Irp - Supplies a pointer to the Irp which made the original request.
  4758. Return Value:
  4759. Returns the status of the operation. Either success or no device.
  4760. --*/
  4761. {
  4762. KIRQL currentIrql;
  4763. PPDO_EXTENSION pdoExtension;
  4764. PIO_STACK_LOCATION irpStack;
  4765. PSCSI_REQUEST_BLOCK srb;
  4766. NTSTATUS status;
  4767. PAGED_CODE();
  4768. // ISSUE:2000/02/11 : need to test this
  4769. //
  4770. // Get SRB address from current IRP stack.
  4771. //
  4772. irpStack = IoGetCurrentIrpStackLocation(Irp);
  4773. srb = (PSCSI_REQUEST_BLOCK) irpStack->Parameters.Others.Argument1;
  4774. srb->SrbStatus = SRB_STATUS_NO_DEVICE;
  4775. status = STATUS_DEVICE_DOES_NOT_EXIST;
  4776. pdoExtension = RefLogicalUnitExtensionWithTag(
  4777. DeviceExtension,
  4778. srb->PathId,
  4779. srb->TargetId,
  4780. srb->Lun,
  4781. FALSE,
  4782. IdeRemoveDevice
  4783. );
  4784. if (pdoExtension) {
  4785. DebugPrint((1, "IdeRemove device removing Pdo %x\n", pdoExtension));
  4786. status = FreePdoWithTag (pdoExtension, TRUE, TRUE, IdeRemoveDevice);
  4787. if (NT_SUCCESS(status)) {
  4788. srb->SrbStatus = SRB_STATUS_SUCCESS;
  4789. }
  4790. }
  4791. return status;
  4792. }
  4793. VOID
  4794. IdeMiniPortTimerDpc(
  4795. IN struct _KDPC *Dpc,
  4796. IN PVOID DeviceObject,
  4797. IN PVOID SystemArgument1,
  4798. IN PVOID SystemArgument2
  4799. )
  4800. /*++
  4801. Routine Description:
  4802. This routine calls the miniport when its requested timer fires.
  4803. It interlocks either with the port spinlock and the interrupt object.
  4804. Arguments:
  4805. Dpc - Unsed.
  4806. DeviceObject - Supplies a pointer to the device object for this adapter.
  4807. SystemArgument1 - Unused.
  4808. SystemArgument2 - Unused.
  4809. Return Value:
  4810. None.
  4811. --*/
  4812. {
  4813. PFDO_EXTENSION deviceExtension = ((PDEVICE_OBJECT) DeviceObject)->DeviceExtension;
  4814. //
  4815. // Acquire the port spinlock.
  4816. //
  4817. KeAcquireSpinLockAtDpcLevel(&deviceExtension->SpinLock);
  4818. //
  4819. // Make sure the timer routine is still desired.
  4820. //
  4821. if (deviceExtension->HwTimerRequest != NULL) {
  4822. KeSynchronizeExecution (
  4823. deviceExtension->InterruptObject,
  4824. (PKSYNCHRONIZE_ROUTINE) deviceExtension->HwTimerRequest,
  4825. deviceExtension->HwDeviceExtension
  4826. );
  4827. }
  4828. //
  4829. // Release the spinlock.
  4830. //
  4831. KeReleaseSpinLockFromDpcLevel(&deviceExtension->SpinLock);
  4832. //
  4833. // Check for miniport work requests. Note this is an unsynchonized
  4834. // test on a bit that can be set by the interrupt routine; however,
  4835. // the worst that can happen is that the completion DPC checks for work
  4836. // twice.
  4837. //
  4838. if (deviceExtension->InterruptData.InterruptFlags & PD_NOTIFICATION_REQUIRED) {
  4839. //
  4840. // Call the completion DPC directly.
  4841. //
  4842. IdePortCompletionDpc( NULL,
  4843. deviceExtension->DeviceObject,
  4844. NULL,
  4845. NULL);
  4846. }
  4847. }
  4848. NTSTATUS
  4849. IdePortFlushLogicalUnit (
  4850. PFDO_EXTENSION FdoExtension,
  4851. PLOGICAL_UNIT_EXTENSION LogUnitExtension,
  4852. BOOLEAN Forced
  4853. )
  4854. {
  4855. NTSTATUS status;
  4856. PIO_STACK_LOCATION irpStack;
  4857. PSCSI_REQUEST_BLOCK srb;
  4858. PKDEVICE_QUEUE_ENTRY packet;
  4859. KIRQL currentIrql;
  4860. PIRP nextIrp;
  4861. PIRP listIrp;
  4862. PIRP powerRelatedIrp;
  4863. //
  4864. // Acquire the spinlock to protect the flags structure and the saved
  4865. // interrupt context.
  4866. //
  4867. KeAcquireSpinLock(&FdoExtension->SpinLock, &currentIrql);
  4868. //
  4869. // Make sure the queue is frozen.
  4870. //
  4871. if ((!(LogUnitExtension->LuFlags & PD_QUEUE_FROZEN)) && (!Forced)) {
  4872. DebugPrint((1,"IdePortFlushLogicalUnit: Request to flush an unfrozen queue!\n"));
  4873. KeReleaseSpinLock(&FdoExtension->SpinLock, currentIrql);
  4874. status = STATUS_INVALID_DEVICE_REQUEST;
  4875. } else {
  4876. listIrp = NULL;
  4877. powerRelatedIrp = NULL;
  4878. //
  4879. // The queue may not be busy so we have to use the IfBusy variant.
  4880. // Use a zero key to pull items from the head of it (if any are there)
  4881. //
  4882. while ((packet =
  4883. KeRemoveByKeyDeviceQueueIfBusy(
  4884. &(LogUnitExtension->DeviceObject->DeviceQueue),
  4885. 0))
  4886. != NULL) {
  4887. nextIrp = CONTAINING_RECORD(packet, IRP, Tail.Overlay.DeviceQueueEntry);
  4888. //
  4889. // Get the srb.
  4890. //
  4891. irpStack = IoGetCurrentIrpStackLocation(nextIrp);
  4892. srb = irpStack->Parameters.Scsi.Srb;
  4893. if (srb->Function == SRB_FUNCTION_ATA_POWER_PASS_THROUGH) {
  4894. ASSERT (!powerRelatedIrp);
  4895. powerRelatedIrp = nextIrp;
  4896. continue;
  4897. }
  4898. //
  4899. // Set the status code.
  4900. //
  4901. srb->SrbStatus = SRB_STATUS_REQUEST_FLUSHED;
  4902. nextIrp->IoStatus.Status = STATUS_UNSUCCESSFUL;
  4903. //
  4904. // Link the requests. They will be completed after the
  4905. // spinlock is released.
  4906. //
  4907. nextIrp->Tail.Overlay.ListEntry.Flink = (PLIST_ENTRY)
  4908. listIrp;
  4909. listIrp = nextIrp;
  4910. }
  4911. //
  4912. // clear the pending reuqest blocked by busy device
  4913. //
  4914. if ((LogUnitExtension->LuFlags & PD_LOGICAL_UNIT_IS_BUSY) &&
  4915. (LogUnitExtension->BusyRequest)) {
  4916. nextIrp = LogUnitExtension->BusyRequest;
  4917. irpStack = IoGetCurrentIrpStackLocation(nextIrp);
  4918. srb = irpStack->Parameters.Scsi.Srb;
  4919. LogUnitExtension->BusyRequest = NULL;
  4920. CLRMASK (LogUnitExtension->LuFlags, PD_LOGICAL_UNIT_IS_BUSY);
  4921. if (srb->Function == SRB_FUNCTION_ATA_POWER_PASS_THROUGH) {
  4922. ASSERT (!powerRelatedIrp);
  4923. powerRelatedIrp = nextIrp;
  4924. } else {
  4925. srb->SrbStatus = SRB_STATUS_REQUEST_FLUSHED;
  4926. nextIrp->IoStatus.Status = STATUS_UNSUCCESSFUL;
  4927. nextIrp->Tail.Overlay.ListEntry.Flink = (PLIST_ENTRY)
  4928. listIrp;
  4929. listIrp = nextIrp;
  4930. }
  4931. }
  4932. if (LogUnitExtension->PendingRequest) {
  4933. nextIrp = LogUnitExtension->PendingRequest;
  4934. LogUnitExtension->PendingRequest = NULL;
  4935. irpStack = IoGetCurrentIrpStackLocation(nextIrp);
  4936. srb = irpStack->Parameters.Scsi.Srb;
  4937. if (srb->Function == SRB_FUNCTION_ATA_POWER_PASS_THROUGH) {
  4938. ASSERT (!powerRelatedIrp);
  4939. powerRelatedIrp = nextIrp;
  4940. } else {
  4941. srb->SrbStatus = SRB_STATUS_REQUEST_FLUSHED;
  4942. nextIrp->IoStatus.Status = STATUS_UNSUCCESSFUL;
  4943. nextIrp->Tail.Overlay.ListEntry.Flink = (PLIST_ENTRY)
  4944. listIrp;
  4945. listIrp = nextIrp;
  4946. }
  4947. }
  4948. //
  4949. // Mark the queue as unfrozen. Since all the requests have
  4950. // been removed and the device queue is no longer busy, it
  4951. // is effectively unfrozen.
  4952. //
  4953. CLRMASK (LogUnitExtension->LuFlags, PD_QUEUE_FROZEN);
  4954. //
  4955. // Release the spinlock.
  4956. //
  4957. KeReleaseSpinLock(&FdoExtension->SpinLock, currentIrql);
  4958. if (powerRelatedIrp) {
  4959. PDEVICE_OBJECT deviceObject = LogUnitExtension->DeviceObject;
  4960. DebugPrint ((DBG_POWER, "Resending power related pass through reuqest 0x%x\n", powerRelatedIrp));
  4961. UnrefPdoWithTag(
  4962. LogUnitExtension,
  4963. powerRelatedIrp
  4964. );
  4965. IdePortDispatch(
  4966. deviceObject,
  4967. powerRelatedIrp
  4968. );
  4969. }
  4970. //
  4971. // Complete the flushed requests.
  4972. //
  4973. while (listIrp != NULL) {
  4974. nextIrp = listIrp;
  4975. listIrp = (PIRP) nextIrp->Tail.Overlay.ListEntry.Flink;
  4976. UnrefLogicalUnitExtensionWithTag(
  4977. FdoExtension,
  4978. LogUnitExtension,
  4979. nextIrp
  4980. );
  4981. IoCompleteRequest(nextIrp, 0);
  4982. }
  4983. status = STATUS_SUCCESS;
  4984. }
  4985. return status;
  4986. }
  4987. PVOID
  4988. IdeMapLockedPagesWithReservedMapping (
  4989. IN PFDO_EXTENSION DeviceExtension,
  4990. IN PSRB_DATA SrbData,
  4991. IN PMDL Mdl
  4992. )
  4993. /*++
  4994. Routine Description:
  4995. This routine attempts to map the physical pages represented by the supplied
  4996. MDL using the adapter's reserved page range.
  4997. Arguments:
  4998. DeviceExtension - Points to the FDO extension
  4999. SrbData - Points to SrbData structure for this request
  5000. Mdl - Points to an MDL that describes the physical range we
  5001. are tring to map.
  5002. Return Value:
  5003. Kernel VA of the mapped pages if mapped successfully.
  5004. NULL if the reserved page range is too small or if the pages are
  5005. not successfully mapped.
  5006. -1 if the reserved pages are already in use.
  5007. Notes:
  5008. This routine is called with the spinlock held.
  5009. --*/
  5010. {
  5011. ULONG_PTR numberOfPages;
  5012. PVOID startingVa;
  5013. PVOID systemAddress;
  5014. //
  5015. // Check if the reserve pages are already in use
  5016. //
  5017. if (DeviceExtension->Flags & PD_RESERVED_PAGES_IN_USE) {
  5018. DebugPrint((1,
  5019. "Reserve pages in use...\n"
  5020. ));
  5021. return (PVOID)-1;
  5022. }
  5023. startingVa = (PVOID)((PCHAR)Mdl->StartVa + Mdl->ByteOffset);
  5024. numberOfPages = ADDRESS_AND_SIZE_TO_SPAN_PAGES(startingVa, Mdl->ByteCount);
  5025. if (numberOfPages > IDE_NUM_RESERVED_PAGES) {
  5026. systemAddress = NULL;
  5027. } else {
  5028. //
  5029. // The reserved range is large enough to map all the pages. Go ahead
  5030. // and try to map them. Since we are specifying MmCached as cache
  5031. // type and we've ensured that we have enough reserved pages to
  5032. // cover the request, this should never fail.
  5033. //
  5034. systemAddress = MmMapLockedPagesWithReservedMapping (DeviceExtension->ReservedPages,
  5035. 'PedI',
  5036. Mdl,
  5037. MmCached );
  5038. if (systemAddress == NULL) {
  5039. DebugPrint((1,
  5040. "mapping failed....\n"
  5041. ));
  5042. ASSERT(systemAddress);
  5043. } else {
  5044. DebugPrint((1,
  5045. "mapping....\n"
  5046. ));
  5047. //
  5048. // We need this flag to verify if the reserved pages are already
  5049. // in use. The per request srbData flag is not available to make
  5050. // this check
  5051. //
  5052. ASSERT(!(DeviceExtension->Flags & PD_RESERVED_PAGES_IN_USE));
  5053. SETMASK(DeviceExtension->Flags, PD_RESERVED_PAGES_IN_USE);
  5054. //
  5055. // we need this flag to unmap the pages. The flag in the
  5056. // device extension cannot be relied upon as it might indicate
  5057. // the flags for the next request
  5058. //
  5059. ASSERT(!(SrbData->Flags & SRB_DATA_RESERVED_PAGES));
  5060. SETMASK(SrbData->Flags, SRB_DATA_RESERVED_PAGES);
  5061. }
  5062. }
  5063. return systemAddress;
  5064. }
  5065. VOID
  5066. IdeUnmapReservedMapping (
  5067. IN PFDO_EXTENSION DeviceExtension,
  5068. IN PSRB_DATA SrbData,
  5069. IN PMDL Mdl
  5070. )
  5071. /*++
  5072. Routine Description :
  5073. Unmap the physical pages represented by the Mdl
  5074. Arguments:
  5075. DeviceExtension: The Fdo extension
  5076. Mdl: Mdl for the request
  5077. Return Value:
  5078. No return value
  5079. Notes:
  5080. This routine is called with the spinlock held
  5081. --*/
  5082. {
  5083. DebugPrint((1,
  5084. "Unmapping....\n"
  5085. ));
  5086. ASSERT(DeviceExtension->Flags & PD_RESERVED_PAGES_IN_USE);
  5087. CLRMASK(DeviceExtension->Flags, PD_RESERVED_PAGES_IN_USE);
  5088. ASSERT(SrbData->Flags & SRB_DATA_RESERVED_PAGES);
  5089. CLRMASK(SrbData->Flags, SRB_DATA_RESERVED_PAGES);
  5090. MmUnmapReservedMapping (
  5091. DeviceExtension->ReservedPages,
  5092. 'PedI',
  5093. Mdl
  5094. );
  5095. }
  5096. #ifdef LOG_GET_NEXT_CALLER
  5097. VOID
  5098. IdeLogCompletedCommand(
  5099. PFDO_EXTENSION FdoExtension,
  5100. PSCSI_REQUEST_BLOCK Srb
  5101. )
  5102. {
  5103. ULONG index = FdoExtension->CompletedCommandIndex;
  5104. RtlCopyMemory(&(FdoExtension->CompletedCommandQueue[index].Srb),
  5105. Srb,
  5106. sizeof(SCSI_REQUEST_BLOCK)
  5107. );
  5108. FdoExtension->CompletedCommandIndex =
  5109. (FdoExtension->CompletedCommandIndex + 1) % GET_NEXT_LOG_LENGTH;
  5110. return;
  5111. }
  5112. VOID
  5113. IdeLogGetNextLuCaller (
  5114. PFDO_EXTENSION FdoExtension,
  5115. PPDO_EXTENSION PdoExtension,
  5116. PUCHAR FileName,
  5117. ULONG LineNumber
  5118. )
  5119. /*++
  5120. Routine Description:
  5121. Temporary routine to log the last few GetNextLuRequest caller. This
  5122. routine was added to catch a bug where we fail to process further
  5123. requests on an LU
  5124. Arguments:
  5125. Return Value:
  5126. None.
  5127. --*/
  5128. {
  5129. ULONG index = FdoExtension->GetNextLuIndex;
  5130. FdoExtension->GetNextLuCallerLineNumber[index] = LineNumber;
  5131. strncpy (FdoExtension->GetNextLuCallerFileName[index], FileName, 255);
  5132. FdoExtension->GetNextLuCallerFlags[index] = PdoExtension->LuFlags;
  5133. FdoExtension->GetNextLuIndex =
  5134. (FdoExtension->GetNextLuIndex + 1) % GET_NEXT_LOG_LENGTH;
  5135. return;
  5136. }
  5137. #endif