Source code of Windows XP (NT5)
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.

3423 lines
102 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1991 - 1999
  3. Module Name:
  4. diskwmi.c
  5. Abstract:
  6. SCSI disk class driver - WMI support routines
  7. Environment:
  8. kernel mode only
  9. Notes:
  10. Revision History:
  11. --*/
  12. #include "disk.h"
  13. NTSTATUS
  14. DiskSendFailurePredictIoctl(
  15. PFUNCTIONAL_DEVICE_EXTENSION FdoExtension,
  16. PSTORAGE_PREDICT_FAILURE checkFailure
  17. );
  18. NTSTATUS
  19. DiskGetIdentifyInfo(
  20. PFUNCTIONAL_DEVICE_EXTENSION FdoExtension,
  21. PBOOLEAN SupportSmart
  22. );
  23. NTSTATUS
  24. DiskDetectFailurePrediction(
  25. PFUNCTIONAL_DEVICE_EXTENSION FdoExtension,
  26. PFAILURE_PREDICTION_METHOD FailurePredictCapability
  27. );
  28. NTSTATUS
  29. DiskReadFailurePredictThresholds(
  30. PFUNCTIONAL_DEVICE_EXTENSION FdoExtension,
  31. PSTORAGE_FAILURE_PREDICT_THRESHOLDS DiskSmartThresholds
  32. );
  33. NTSTATUS
  34. DiskReadSmartLog(
  35. IN PFUNCTIONAL_DEVICE_EXTENSION FdoExtension,
  36. IN UCHAR SectorCount,
  37. IN UCHAR LogAddress,
  38. OUT PUCHAR Buffer
  39. );
  40. NTSTATUS
  41. DiskWriteSmartLog(
  42. IN PFUNCTIONAL_DEVICE_EXTENSION FdoExtension,
  43. IN UCHAR SectorCount,
  44. IN UCHAR LogAddress,
  45. IN PUCHAR Buffer
  46. );
  47. void DiskReregWorker(
  48. IN PVOID Context
  49. );
  50. //
  51. // WMI reregistration globals
  52. //
  53. // Since it will take too long to do a mode sense on some drive, we
  54. // need a good way to effect the mode sense for the info exceptions
  55. // mode page so that we can determine if SMART is supported and enabled
  56. // for the drive. So the strategy is to do an asynchronous mode sense
  57. // when the device starts and then look at the info exceptions mode
  58. // page within the completion routine. Now within the completion
  59. // routine we cannot call IoWMIRegistrationControl since we are at DPC
  60. // level, so we create a stack of device objects that will be processed
  61. // by a single work item that is fired off only when the stack
  62. // transitions from empty to non empty.
  63. //
  64. WORK_QUEUE_ITEM DiskReregWorkItem;
  65. SINGLE_LIST_ENTRY DiskReregHead;
  66. KSPIN_LOCK DiskReregSpinlock;
  67. LONG DiskReregWorkItems;
  68. GUIDREGINFO DiskWmiFdoGuidList[] =
  69. {
  70. {
  71. WMI_DISK_GEOMETRY_GUID,
  72. 1,
  73. 0
  74. },
  75. {
  76. WMI_STORAGE_FAILURE_PREDICT_STATUS_GUID,
  77. 1,
  78. WMIREG_FLAG_EXPENSIVE
  79. },
  80. {
  81. WMI_STORAGE_FAILURE_PREDICT_DATA_GUID,
  82. 1,
  83. WMIREG_FLAG_EXPENSIVE
  84. },
  85. {
  86. WMI_STORAGE_FAILURE_PREDICT_FUNCTION_GUID,
  87. 1,
  88. WMIREG_FLAG_EXPENSIVE
  89. },
  90. {
  91. WMI_STORAGE_PREDICT_FAILURE_EVENT_GUID,
  92. 1,
  93. WMIREG_FLAG_EVENT_ONLY_GUID
  94. },
  95. {
  96. WMI_STORAGE_FAILURE_PREDICT_THRESHOLDS_GUID,
  97. 1,
  98. WMIREG_FLAG_EXPENSIVE
  99. },
  100. {
  101. WMI_STORAGE_SCSI_INFO_EXCEPTIONS_GUID,
  102. 1,
  103. 0
  104. },
  105. };
  106. GUID DiskPredictFailureEventGuid = WMI_STORAGE_PREDICT_FAILURE_EVENT_GUID;
  107. #define DiskGeometryGuid 0
  108. #define SmartStatusGuid 1
  109. #define SmartDataGuid 2
  110. #define SmartPerformFunction 3
  111. #define AllowDisallowPerformanceHit 1
  112. #define EnableDisableHardwareFailurePrediction 2
  113. #define EnableDisableFailurePredictionPolling 3
  114. #define GetFailurePredictionCapability 4
  115. #define EnableOfflineDiags 5
  116. #define SmartEventGuid 4
  117. #define SmartThresholdsGuid 5
  118. #define ScsiInfoExceptionsGuid 6
  119. #if 0
  120. //
  121. // Enable this to add WMI support for PDOs
  122. GUIDREGINFO DiskWmiPdoGuidList[] =
  123. {
  124. {
  125. // {25007F51-57C2-11d1-A528-00A0C9062910}
  126. { 0x25007f52, 0x57c2, 0x11d1,
  127. { 0xa5, 0x28, 0x0, 0xa0, 0xc9, 0x6, 0x29, 0x10 } },
  128. 0
  129. },
  130. };
  131. ULONG DiskDummyData[4] = { 1, 2, 3, 4};
  132. #endif
  133. #ifdef ALLOC_PRAGMA
  134. #pragma alloc_text(PAGE, DiskWmiFunctionControl)
  135. #pragma alloc_text(PAGE, DiskFdoQueryWmiRegInfo)
  136. #pragma alloc_text(PAGE, DiskFdoQueryWmiDataBlock)
  137. #pragma alloc_text(PAGE, DiskFdoSetWmiDataBlock)
  138. #pragma alloc_text(PAGE, DiskFdoSetWmiDataItem)
  139. #pragma alloc_text(PAGE, DiskFdoExecuteWmiMethod)
  140. #pragma alloc_text(PAGE, DiskDetectFailurePrediction)
  141. #pragma alloc_text(PAGE, DiskEnableDisableFailurePrediction)
  142. #pragma alloc_text(PAGE, DiskEnableDisableFailurePredictPolling)
  143. #pragma alloc_text(PAGE, DiskReadFailurePredictStatus)
  144. #pragma alloc_text(PAGE, DiskReadFailurePredictData)
  145. #pragma alloc_text(PAGE, DiskReadFailurePredictThresholds)
  146. #pragma alloc_text(PAGE, DiskGetIdentifyInfo)
  147. #pragma alloc_text(PAGE, DiskReadSmartLog)
  148. #pragma alloc_text(PAGE, DiskWriteSmartLog)
  149. #pragma alloc_text(PAGE, DiskPerformSmartCommand)
  150. #pragma alloc_text(PAGE, DiskSendFailurePredictIoctl)
  151. #pragma alloc_text(PAGE, DiskReregWorker)
  152. #pragma alloc_text(PAGE, DiskInitializeReregistration)
  153. #endif
  154. //
  155. // SMART/IDE specific routines
  156. //
  157. // Read SMART data attributes.
  158. // SrbControl should be sizeof(SRB_IO_CONTROL) +
  159. // (sizeof(SENDCMDINPARAMS)-1) +
  160. // READ_ATTRIBUTE_BUFFER_SIZE
  161. // Attribute data returned at &SendCmdOutParams->bBuffer[0]
  162. //
  163. #define DiskReadSmartData(FdoExtension, \
  164. SrbControl, \
  165. BufferSize) \
  166. DiskPerformSmartCommand(FdoExtension, \
  167. IOCTL_SCSI_MINIPORT_READ_SMART_ATTRIBS, \
  168. SMART_CMD, \
  169. READ_ATTRIBUTES, \
  170. 0, \
  171. 0, \
  172. (SrbControl), \
  173. (BufferSize))
  174. //
  175. // Read SMART data thresholds.
  176. // SrbControl should be sizeof(SRB_IO_CONTROL) +
  177. // (sizeof(SENDCMDINPARAMS)-1) +
  178. // READ_THRESHOLD_BUFFER_SIZE
  179. // Attribute data returned at &SendCmdOutParams->bBuffer[0]
  180. //
  181. #define DiskReadSmartThresholds(FdoExtension, \
  182. SrbControl, \
  183. BufferSize) \
  184. DiskPerformSmartCommand(FdoExtension, \
  185. IOCTL_SCSI_MINIPORT_READ_SMART_THRESHOLDS, \
  186. SMART_CMD, \
  187. READ_THRESHOLDS, \
  188. 0, \
  189. 0, \
  190. (SrbControl), \
  191. (BufferSize))
  192. //
  193. // Read SMART status
  194. // SrbControl should be sizeof(SRB_IO_CONTROL) +
  195. // (sizeof(SENDCMDINPARAMS)-1) +
  196. // sizeof(IDEREGS)
  197. // Failure predicted if cmdOutParameters[3] == 0xf4 and [4] == 0x2c
  198. //
  199. #define DiskReadSmartStatus(FdoExtension, \
  200. SrbControl, \
  201. BufferSize) \
  202. DiskPerformSmartCommand(FdoExtension, \
  203. IOCTL_SCSI_MINIPORT_RETURN_STATUS, \
  204. SMART_CMD, \
  205. RETURN_SMART_STATUS, \
  206. 0, \
  207. 0, \
  208. (SrbControl), \
  209. (BufferSize))
  210. //
  211. // Read disks IDENTIFY data
  212. // SrbControl should be sizeof(SRB_IO_CONTROL) +
  213. // (sizeof(SENDCMDINPARAMS)-1) +
  214. // sizeof(IDENTIFY_BUFFER_SIZE)
  215. // Identify data returned at &cmdOutParams.bBuffer[0]
  216. //
  217. #define DiskGetIdentifyData(FdoExtension, \
  218. SrbControl, \
  219. BufferSize) \
  220. DiskPerformSmartCommand(FdoExtension, \
  221. IOCTL_SCSI_MINIPORT_IDENTIFY, \
  222. ID_CMD, \
  223. 0, \
  224. 0, \
  225. 0, \
  226. (SrbControl), \
  227. (BufferSize))
  228. //
  229. // Enable SMART
  230. //
  231. _inline NTSTATUS
  232. DiskEnableSmart(
  233. PFUNCTIONAL_DEVICE_EXTENSION FdoExtension
  234. )
  235. {
  236. UCHAR srbControl[sizeof(SRB_IO_CONTROL) + sizeof(SENDCMDINPARAMS)];
  237. ULONG bufferSize = sizeof(srbControl);
  238. return DiskPerformSmartCommand(FdoExtension,
  239. IOCTL_SCSI_MINIPORT_ENABLE_SMART,
  240. SMART_CMD,
  241. ENABLE_SMART,
  242. 0,
  243. 0,
  244. (PSRB_IO_CONTROL)srbControl,
  245. &bufferSize);
  246. }
  247. //
  248. // Disable SMART
  249. //
  250. _inline NTSTATUS
  251. DiskDisableSmart(
  252. PFUNCTIONAL_DEVICE_EXTENSION FdoExtension
  253. )
  254. {
  255. UCHAR srbControl[sizeof(SRB_IO_CONTROL) + sizeof(SENDCMDINPARAMS)];
  256. ULONG bufferSize = sizeof(srbControl);
  257. return DiskPerformSmartCommand(FdoExtension,
  258. IOCTL_SCSI_MINIPORT_DISABLE_SMART,
  259. SMART_CMD,
  260. DISABLE_SMART,
  261. 0,
  262. 0,
  263. (PSRB_IO_CONTROL)srbControl,
  264. &bufferSize);
  265. }
  266. //
  267. // Enable Attribute Autosave
  268. //
  269. _inline NTSTATUS
  270. DiskEnableSmartAttributeAutosave(
  271. PFUNCTIONAL_DEVICE_EXTENSION FdoExtension
  272. )
  273. {
  274. UCHAR srbControl[sizeof(SRB_IO_CONTROL) + sizeof(SENDCMDINPARAMS)];
  275. ULONG bufferSize = sizeof(srbControl);
  276. return DiskPerformSmartCommand(FdoExtension,
  277. IOCTL_SCSI_MINIPORT_ENABLE_DISABLE_AUTOSAVE,
  278. SMART_CMD,
  279. ENABLE_DISABLE_AUTOSAVE,
  280. 0xf1,
  281. 0,
  282. (PSRB_IO_CONTROL)srbControl,
  283. &bufferSize);
  284. }
  285. //
  286. // Disable Attribute Autosave
  287. //
  288. _inline NTSTATUS
  289. DiskDisableSmartAttributeAutosave(
  290. PFUNCTIONAL_DEVICE_EXTENSION FdoExtension
  291. )
  292. {
  293. UCHAR srbControl[sizeof(SRB_IO_CONTROL) + sizeof(SENDCMDINPARAMS)];
  294. ULONG bufferSize = sizeof(srbControl);
  295. return DiskPerformSmartCommand(FdoExtension,
  296. IOCTL_SCSI_MINIPORT_ENABLE_DISABLE_AUTOSAVE,
  297. SMART_CMD,
  298. ENABLE_DISABLE_AUTOSAVE,
  299. 0x00,
  300. 0,
  301. (PSRB_IO_CONTROL)srbControl,
  302. &bufferSize);
  303. }
  304. //
  305. // Initialize execution of SMART online diagnostics
  306. //
  307. _inline NTSTATUS
  308. DiskExecuteSmartDiagnostics(
  309. PFUNCTIONAL_DEVICE_EXTENSION FdoExtension,
  310. UCHAR Subcommand
  311. )
  312. {
  313. UCHAR srbControl[sizeof(SRB_IO_CONTROL) + sizeof(SENDCMDINPARAMS)];
  314. ULONG bufferSize = sizeof(srbControl);
  315. return DiskPerformSmartCommand(FdoExtension,
  316. IOCTL_SCSI_MINIPORT_EXECUTE_OFFLINE_DIAGS,
  317. SMART_CMD,
  318. EXECUTE_OFFLINE_DIAGS,
  319. 0,
  320. Subcommand,
  321. (PSRB_IO_CONTROL)srbControl,
  322. &bufferSize);
  323. }
  324. NTSTATUS
  325. DiskReadSmartLog(
  326. IN PFUNCTIONAL_DEVICE_EXTENSION FdoExtension,
  327. IN UCHAR SectorCount,
  328. IN UCHAR LogAddress,
  329. OUT PUCHAR Buffer
  330. )
  331. {
  332. PSRB_IO_CONTROL srbControl;
  333. NTSTATUS status;
  334. PSENDCMDOUTPARAMS sendCmdOutParams;
  335. ULONG logSize, bufferSize;
  336. PAGED_CODE();
  337. logSize = SectorCount * SMART_LOG_SECTOR_SIZE;
  338. bufferSize = sizeof(SRB_IO_CONTROL) + sizeof(SENDCMDINPARAMS) - 1 +
  339. logSize;
  340. srbControl = ExAllocatePoolWithTag(NonPagedPool,
  341. bufferSize,
  342. DISK_TAG_SMART);
  343. if (srbControl != NULL)
  344. {
  345. status = DiskPerformSmartCommand(FdoExtension,
  346. IOCTL_SCSI_MINIPORT_READ_SMART_LOG,
  347. SMART_CMD,
  348. SMART_READ_LOG,
  349. SectorCount,
  350. LogAddress,
  351. srbControl,
  352. &bufferSize);
  353. if (NT_SUCCESS(status))
  354. {
  355. sendCmdOutParams = (PSENDCMDOUTPARAMS)((PUCHAR)srbControl +
  356. sizeof(SRB_IO_CONTROL));
  357. RtlCopyMemory(Buffer,
  358. &sendCmdOutParams->bBuffer[0],
  359. logSize);
  360. }
  361. ExFreePool(srbControl);
  362. } else {
  363. status = STATUS_INSUFFICIENT_RESOURCES;
  364. }
  365. return(status);
  366. }
  367. NTSTATUS
  368. DiskWriteSmartLog(
  369. IN PFUNCTIONAL_DEVICE_EXTENSION FdoExtension,
  370. IN UCHAR SectorCount,
  371. IN UCHAR LogAddress,
  372. IN PUCHAR Buffer
  373. )
  374. {
  375. PSRB_IO_CONTROL srbControl;
  376. NTSTATUS status;
  377. PSENDCMDINPARAMS sendCmdInParams;
  378. ULONG logSize, bufferSize;
  379. PAGED_CODE();
  380. logSize = SectorCount * SMART_LOG_SECTOR_SIZE;
  381. bufferSize = sizeof(SRB_IO_CONTROL) + sizeof(SENDCMDINPARAMS) - 1 +
  382. logSize;
  383. srbControl = ExAllocatePoolWithTag(NonPagedPool,
  384. bufferSize,
  385. DISK_TAG_SMART);
  386. if (srbControl != NULL)
  387. {
  388. sendCmdInParams = (PSENDCMDINPARAMS)((PUCHAR)srbControl +
  389. sizeof(SRB_IO_CONTROL));
  390. RtlCopyMemory(&sendCmdInParams->bBuffer[0],
  391. Buffer,
  392. logSize);
  393. status = DiskPerformSmartCommand(FdoExtension,
  394. IOCTL_SCSI_MINIPORT_WRITE_SMART_LOG,
  395. SMART_CMD,
  396. SMART_WRITE_LOG,
  397. SectorCount,
  398. LogAddress,
  399. srbControl,
  400. &bufferSize);
  401. ExFreePool(srbControl);
  402. } else {
  403. status = STATUS_INSUFFICIENT_RESOURCES;
  404. }
  405. return(status);
  406. }
  407. NTSTATUS
  408. DiskPerformSmartCommand(
  409. IN PFUNCTIONAL_DEVICE_EXTENSION FdoExtension,
  410. IN ULONG SrbControlCode,
  411. IN UCHAR Command,
  412. IN UCHAR Feature,
  413. IN UCHAR SectorCount,
  414. IN UCHAR SectorNumber,
  415. IN OUT PSRB_IO_CONTROL SrbControl,
  416. OUT PULONG BufferSize
  417. )
  418. /*++
  419. Routine Description:
  420. This routine will perform some SMART command
  421. Arguments:
  422. FdoExtension is the FDO device extension
  423. SrbControlCode is the SRB control code to use for the request
  424. Command is the SMART command to be executed. It may be SMART_CMD or
  425. ID_CMD.
  426. Feature is the value to place in the IDE feature register.
  427. SectorCount is the value to place in the IDE SectorCount register
  428. SrbControl is the buffer used to build the SRB_IO_CONTROL and pass
  429. any input parameters. It also returns the output parameters.
  430. *BufferSize on entry has total size of SrbControl and on return has
  431. the size used in SrbControl.
  432. Return Value:
  433. status
  434. --*/
  435. {
  436. PCOMMON_DEVICE_EXTENSION commonExtension = (PCOMMON_DEVICE_EXTENSION)FdoExtension;
  437. PDISK_DATA diskData = (PDISK_DATA)(commonExtension->DriverData);
  438. PUCHAR buffer;
  439. PSENDCMDINPARAMS cmdInParameters;
  440. PSENDCMDOUTPARAMS cmdOutParameters;
  441. ULONG outBufferSize;
  442. NTSTATUS status;
  443. ULONG availableBufferSize;
  444. KEVENT event;
  445. PIRP irp;
  446. IO_STATUS_BLOCK ioStatus;
  447. SCSI_REQUEST_BLOCK srb;
  448. LARGE_INTEGER startingOffset;
  449. ULONG length;
  450. PIO_STACK_LOCATION irpStack;
  451. PAGED_CODE();
  452. //
  453. // Point to the 'buffer' portion of the SRB_CONTROL and compute how
  454. // much room we have left in the srb control
  455. //
  456. buffer = (PUCHAR)SrbControl;
  457. (ULONG_PTR)buffer += sizeof(SRB_IO_CONTROL);
  458. cmdInParameters = (PSENDCMDINPARAMS)buffer;
  459. cmdOutParameters = (PSENDCMDOUTPARAMS)buffer;
  460. availableBufferSize = *BufferSize - sizeof(SRB_IO_CONTROL);
  461. #if DBG
  462. //
  463. // Ensure control codes and buffer lengths passed are correct
  464. //
  465. {
  466. ULONG controlCode;
  467. ULONG lengthNeeded = sizeof(SENDCMDINPARAMS) - 1;
  468. if (Command == SMART_CMD)
  469. {
  470. switch (Feature)
  471. {
  472. case ENABLE_SMART:
  473. {
  474. controlCode = IOCTL_SCSI_MINIPORT_ENABLE_SMART;
  475. break;
  476. }
  477. case DISABLE_SMART:
  478. {
  479. controlCode = IOCTL_SCSI_MINIPORT_DISABLE_SMART;
  480. break;
  481. }
  482. case RETURN_SMART_STATUS:
  483. {
  484. //
  485. // Ensure bBuffer is at least 2 bytes (to hold the values of
  486. // cylinderLow and cylinderHigh).
  487. //
  488. lengthNeeded = sizeof(SENDCMDINPARAMS) - 1 + sizeof(IDEREGS);
  489. controlCode = IOCTL_SCSI_MINIPORT_RETURN_STATUS;
  490. break;
  491. }
  492. case ENABLE_DISABLE_AUTOSAVE:
  493. {
  494. controlCode = IOCTL_SCSI_MINIPORT_ENABLE_DISABLE_AUTOSAVE;
  495. break;
  496. }
  497. case SAVE_ATTRIBUTE_VALUES:
  498. {
  499. controlCode = IOCTL_SCSI_MINIPORT_SAVE_ATTRIBUTE_VALUES;
  500. break;
  501. }
  502. case EXECUTE_OFFLINE_DIAGS:
  503. {
  504. controlCode = IOCTL_SCSI_MINIPORT_EXECUTE_OFFLINE_DIAGS;
  505. break;
  506. }
  507. case READ_ATTRIBUTES:
  508. {
  509. controlCode = IOCTL_SCSI_MINIPORT_READ_SMART_ATTRIBS;
  510. lengthNeeded = READ_ATTRIBUTE_BUFFER_SIZE + sizeof(SENDCMDOUTPARAMS) - 1;
  511. break;
  512. }
  513. case READ_THRESHOLDS:
  514. {
  515. controlCode = IOCTL_SCSI_MINIPORT_READ_SMART_THRESHOLDS;
  516. lengthNeeded = READ_THRESHOLD_BUFFER_SIZE + sizeof(SENDCMDOUTPARAMS) - 1;
  517. break;
  518. }
  519. case SMART_READ_LOG:
  520. {
  521. controlCode = IOCTL_SCSI_MINIPORT_READ_SMART_LOG;
  522. lengthNeeded = (SectorCount * SMART_LOG_SECTOR_SIZE) +
  523. sizeof(SENDCMDINPARAMS) - 1;
  524. break;
  525. }
  526. case SMART_WRITE_LOG:
  527. {
  528. controlCode = IOCTL_SCSI_MINIPORT_WRITE_SMART_LOG;
  529. lengthNeeded = (SectorCount * SMART_LOG_SECTOR_SIZE) +
  530. sizeof(SENDCMDINPARAMS) - 1;
  531. break;
  532. }
  533. }
  534. } else if (Command == ID_CMD) {
  535. controlCode = IOCTL_SCSI_MINIPORT_IDENTIFY;
  536. lengthNeeded = IDENTIFY_BUFFER_SIZE + sizeof(SENDCMDOUTPARAMS) -1;
  537. } else {
  538. ASSERT(FALSE);
  539. }
  540. ASSERT(controlCode == SrbControlCode);
  541. ASSERT(availableBufferSize >= lengthNeeded);
  542. }
  543. #endif
  544. //
  545. // Build SrbControl and input to SMART command
  546. //
  547. SrbControl->HeaderLength = sizeof(SRB_IO_CONTROL);
  548. RtlMoveMemory (SrbControl->Signature, "SCSIDISK", 8);
  549. SrbControl->Timeout = FdoExtension->TimeOutValue;
  550. SrbControl->Length = availableBufferSize;
  551. SrbControl->ControlCode = SrbControlCode;
  552. cmdInParameters->cBufferSize = sizeof(SENDCMDINPARAMS);
  553. cmdInParameters->bDriveNumber = diskData->ScsiAddress.TargetId;
  554. cmdInParameters->irDriveRegs.bFeaturesReg = Feature;
  555. cmdInParameters->irDriveRegs.bSectorCountReg = SectorCount;
  556. cmdInParameters->irDriveRegs.bSectorNumberReg = SectorNumber;
  557. cmdInParameters->irDriveRegs.bCylLowReg = SMART_CYL_LOW;
  558. cmdInParameters->irDriveRegs.bCylHighReg = SMART_CYL_HI;
  559. cmdInParameters->irDriveRegs.bCommandReg = Command;
  560. //
  561. // Create and send irp
  562. //
  563. KeInitializeEvent(&event, NotificationEvent, FALSE);
  564. startingOffset.QuadPart = (LONGLONG) 1;
  565. length = SrbControl->HeaderLength + SrbControl->Length;
  566. irp = IoBuildSynchronousFsdRequest(
  567. IRP_MJ_SCSI,
  568. commonExtension->LowerDeviceObject,
  569. SrbControl,
  570. length,
  571. &startingOffset,
  572. &event,
  573. &ioStatus);
  574. if (irp == NULL) {
  575. return STATUS_INSUFFICIENT_RESOURCES;
  576. }
  577. irpStack = IoGetNextIrpStackLocation(irp);
  578. //
  579. // Set major and minor codes.
  580. //
  581. irpStack->MajorFunction = IRP_MJ_SCSI;
  582. irpStack->MinorFunction = 1;
  583. //
  584. // Fill in SRB fields.
  585. //
  586. irpStack->Parameters.Others.Argument1 = &srb;
  587. //
  588. // Zero out the srb.
  589. //
  590. RtlZeroMemory(&srb, sizeof(SCSI_REQUEST_BLOCK));
  591. srb.PathId = diskData->ScsiAddress.PathId;
  592. srb.TargetId = diskData->ScsiAddress.TargetId;
  593. srb.Lun = diskData->ScsiAddress.Lun;
  594. srb.Function = SRB_FUNCTION_IO_CONTROL;
  595. srb.Length = sizeof(SCSI_REQUEST_BLOCK);
  596. srb.SrbFlags = FdoExtension->SrbFlags;
  597. SET_FLAG(srb.SrbFlags, SRB_FLAGS_DATA_IN);
  598. SET_FLAG(srb.SrbFlags, SRB_FLAGS_NO_QUEUE_FREEZE);
  599. SET_FLAG(srb.SrbFlags, SRB_FLAGS_NO_KEEP_AWAKE);
  600. srb.QueueAction = SRB_SIMPLE_TAG_REQUEST;
  601. srb.QueueTag = SP_UNTAGGED;
  602. srb.OriginalRequest = irp;
  603. //
  604. // Set timeout to requested value.
  605. //
  606. srb.TimeOutValue = SrbControl->Timeout;
  607. //
  608. // Set the data buffer.
  609. //
  610. srb.DataBuffer = SrbControl;
  611. srb.DataTransferLength = length;
  612. //
  613. // Flush the data buffer for output. This will insure that the data is
  614. // written back to memory. Since the data-in flag is the the port driver
  615. // will flush the data again for input which will ensure the data is not
  616. // in the cache.
  617. //
  618. KeFlushIoBuffers(irp->MdlAddress, FALSE, TRUE);
  619. //
  620. // Call port driver to handle this request.
  621. //
  622. status = IoCallDriver(commonExtension->LowerDeviceObject, irp);
  623. if (status == STATUS_PENDING) {
  624. KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
  625. status = ioStatus.Status;
  626. }
  627. return status;
  628. }
  629. NTSTATUS
  630. DiskGetIdentifyInfo(
  631. PFUNCTIONAL_DEVICE_EXTENSION FdoExtension,
  632. PBOOLEAN SupportSmart
  633. )
  634. {
  635. UCHAR outBuffer[sizeof(SRB_IO_CONTROL) + (sizeof(SENDCMDINPARAMS)-1) + IDENTIFY_BUFFER_SIZE];
  636. ULONG outBufferSize = sizeof(outBuffer);
  637. NTSTATUS status;
  638. PAGED_CODE();
  639. status = DiskGetIdentifyData(FdoExtension,
  640. (PSRB_IO_CONTROL)outBuffer,
  641. &outBufferSize);
  642. if (NT_SUCCESS(status))
  643. {
  644. PUSHORT identifyData = (PUSHORT)&(outBuffer[sizeof(SRB_IO_CONTROL) + sizeof(SENDCMDOUTPARAMS)-1]);
  645. USHORT commandSetSupported = identifyData[82];
  646. *SupportSmart = ((commandSetSupported != 0xffff) &&
  647. (commandSetSupported != 0) &&
  648. ((commandSetSupported & 1) == 1));
  649. } else {
  650. *SupportSmart = FALSE;
  651. }
  652. DebugPrint((3, "DiskGetIdentifyInfo: SMART %s supported for device %p, status %lx\n",
  653. *SupportSmart ? "is" : "is not",
  654. FdoExtension->DeviceObject,
  655. status));
  656. return status;
  657. }
  658. //
  659. // FP Ioctl specific routines
  660. //
  661. NTSTATUS
  662. DiskSendFailurePredictIoctl(
  663. PFUNCTIONAL_DEVICE_EXTENSION FdoExtension,
  664. PSTORAGE_PREDICT_FAILURE checkFailure
  665. )
  666. {
  667. KEVENT event;
  668. PDEVICE_OBJECT deviceObject;
  669. IO_STATUS_BLOCK ioStatus;
  670. PIRP irp;
  671. NTSTATUS status;
  672. PAGED_CODE();
  673. KeInitializeEvent(&event, SynchronizationEvent, FALSE);
  674. deviceObject = IoGetAttachedDeviceReference(FdoExtension->DeviceObject);
  675. irp = IoBuildDeviceIoControlRequest(
  676. IOCTL_STORAGE_PREDICT_FAILURE,
  677. deviceObject,
  678. NULL,
  679. 0,
  680. checkFailure,
  681. sizeof(STORAGE_PREDICT_FAILURE),
  682. FALSE,
  683. &event,
  684. &ioStatus);
  685. if (irp != NULL)
  686. {
  687. status = IoCallDriver(deviceObject, irp);
  688. if (status == STATUS_PENDING)
  689. {
  690. KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
  691. status = ioStatus.Status;
  692. }
  693. } else {
  694. status = STATUS_INSUFFICIENT_RESOURCES;
  695. }
  696. ObDereferenceObject(deviceObject);
  697. return status;
  698. }
  699. //
  700. // FP type independent routines
  701. //
  702. NTSTATUS
  703. DiskEnableDisableFailurePrediction(
  704. PFUNCTIONAL_DEVICE_EXTENSION FdoExtension,
  705. BOOLEAN Enable
  706. )
  707. /*++
  708. Routine Description:
  709. Enable or disable failure prediction at the hardware level
  710. Arguments:
  711. FdoExtension
  712. Enable
  713. Return Value:
  714. NT Status
  715. --*/
  716. {
  717. NTSTATUS status;
  718. PCOMMON_DEVICE_EXTENSION commonExtension = &(FdoExtension->CommonExtension);
  719. PDISK_DATA diskData = (PDISK_DATA)(commonExtension->DriverData);
  720. PAGED_CODE();
  721. switch(diskData->FailurePredictionCapability)
  722. {
  723. case FailurePredictionSmart:
  724. {
  725. if (Enable)
  726. {
  727. status = DiskEnableSmart(FdoExtension);
  728. } else {
  729. status = DiskDisableSmart(FdoExtension);
  730. }
  731. break;
  732. }
  733. case FailurePredictionSense:
  734. case FailurePredictionIoctl:
  735. {
  736. //
  737. // We assume that the drive is already setup properly for
  738. // failure prediction
  739. //
  740. status = STATUS_SUCCESS;
  741. break;
  742. }
  743. default:
  744. {
  745. status = STATUS_INVALID_DEVICE_REQUEST;
  746. }
  747. }
  748. return status;
  749. }
  750. NTSTATUS
  751. DiskEnableDisableFailurePredictPolling(
  752. PFUNCTIONAL_DEVICE_EXTENSION FdoExtension,
  753. BOOLEAN Enable,
  754. ULONG PollTimeInSeconds
  755. )
  756. /*++
  757. Routine Description:
  758. Enable or disable polling for hardware failure detection
  759. Arguments:
  760. FdoExtension
  761. Enable
  762. PollTimeInSeconds - if 0 then no change to current polling timer
  763. Return Value:
  764. NT Status
  765. --*/
  766. {
  767. NTSTATUS status;
  768. PCOMMON_DEVICE_EXTENSION commonExtension = (PCOMMON_DEVICE_EXTENSION)FdoExtension;
  769. PDISK_DATA diskData = (PDISK_DATA)(commonExtension->DriverData);
  770. PAGED_CODE();
  771. if (Enable)
  772. {
  773. status = DiskEnableDisableFailurePrediction(FdoExtension,
  774. Enable);
  775. } else {
  776. status = STATUS_SUCCESS;
  777. }
  778. if (NT_SUCCESS(status))
  779. {
  780. status = ClassSetFailurePredictionPoll(FdoExtension,
  781. Enable ? diskData->FailurePredictionCapability :
  782. FailurePredictionNone,
  783. PollTimeInSeconds);
  784. //
  785. // Even if this failed we do not want to disable FP on the
  786. // hardware. FP is only ever disabled on the hardware by
  787. // specific command of the user.
  788. //
  789. }
  790. return status;
  791. }
  792. NTSTATUS
  793. DiskReadFailurePredictStatus(
  794. PFUNCTIONAL_DEVICE_EXTENSION FdoExtension,
  795. PSTORAGE_FAILURE_PREDICT_STATUS DiskSmartStatus
  796. )
  797. /*++
  798. Routine Description:
  799. Obtains current failure prediction status
  800. Arguments:
  801. FdoExtension
  802. DiskSmartStatus
  803. Return Value:
  804. NT Status
  805. --*/
  806. {
  807. PCOMMON_DEVICE_EXTENSION commonExtension = (PCOMMON_DEVICE_EXTENSION)FdoExtension;
  808. PDISK_DATA diskData = (PDISK_DATA)(commonExtension->DriverData);
  809. NTSTATUS status;
  810. PAGED_CODE();
  811. DiskSmartStatus->PredictFailure = FALSE;
  812. switch(diskData->FailurePredictionCapability)
  813. {
  814. case FailurePredictionSmart:
  815. {
  816. UCHAR outBuffer[sizeof(SRB_IO_CONTROL) + (sizeof(SENDCMDINPARAMS) - 1 + sizeof(IDEREGS))];
  817. ULONG outBufferSize = sizeof(outBuffer);
  818. PSENDCMDOUTPARAMS cmdOutParameters;
  819. status = DiskReadSmartStatus(FdoExtension,
  820. (PSRB_IO_CONTROL)outBuffer,
  821. &outBufferSize);
  822. if (NT_SUCCESS(status))
  823. {
  824. cmdOutParameters = (PSENDCMDOUTPARAMS)(outBuffer +
  825. sizeof(SRB_IO_CONTROL));
  826. DiskSmartStatus->Reason = 0; // Unknown;
  827. DiskSmartStatus->PredictFailure = ((cmdOutParameters->bBuffer[3] == 0xf4) &&
  828. (cmdOutParameters->bBuffer[4] == 0x2c));
  829. }
  830. break;
  831. }
  832. case FailurePredictionSense:
  833. {
  834. DiskSmartStatus->Reason = FdoExtension->FailureReason;
  835. DiskSmartStatus->PredictFailure = FdoExtension->FailurePredicted;
  836. status = STATUS_SUCCESS;
  837. break;
  838. }
  839. case FailurePredictionIoctl:
  840. case FailurePredictionNone:
  841. default:
  842. {
  843. status = STATUS_INVALID_DEVICE_REQUEST;
  844. break;
  845. }
  846. }
  847. return status;
  848. }
  849. NTSTATUS
  850. DiskReadFailurePredictData(
  851. PFUNCTIONAL_DEVICE_EXTENSION FdoExtension,
  852. PSTORAGE_FAILURE_PREDICT_DATA DiskSmartData
  853. )
  854. /*++
  855. Routine Description:
  856. Obtains current failure prediction data. Not available for
  857. FAILURE_PREDICT_SENSE types.
  858. Arguments:
  859. FdoExtension
  860. DiskSmartData
  861. Return Value:
  862. NT Status
  863. --*/
  864. {
  865. PCOMMON_DEVICE_EXTENSION commonExtension = (PCOMMON_DEVICE_EXTENSION)FdoExtension;
  866. PDISK_DATA diskData = (PDISK_DATA)(commonExtension->DriverData);
  867. NTSTATUS status;
  868. PAGED_CODE();
  869. switch(diskData->FailurePredictionCapability)
  870. {
  871. case FailurePredictionSmart:
  872. {
  873. PUCHAR outBuffer;
  874. ULONG outBufferSize;
  875. PSENDCMDOUTPARAMS cmdOutParameters;
  876. outBufferSize = sizeof(SRB_IO_CONTROL) +
  877. (sizeof(SENDCMDOUTPARAMS)-1) +
  878. READ_ATTRIBUTE_BUFFER_SIZE;
  879. outBuffer = ExAllocatePoolWithTag(NonPagedPool,
  880. outBufferSize,
  881. DISK_TAG_SMART);
  882. if (outBuffer != NULL)
  883. {
  884. status = DiskReadSmartData(FdoExtension,
  885. (PSRB_IO_CONTROL)outBuffer,
  886. &outBufferSize);
  887. if (NT_SUCCESS(status))
  888. {
  889. cmdOutParameters = (PSENDCMDOUTPARAMS)(outBuffer +
  890. sizeof(SRB_IO_CONTROL));
  891. DiskSmartData->Length = READ_ATTRIBUTE_BUFFER_SIZE;
  892. RtlCopyMemory(DiskSmartData->VendorSpecific,
  893. cmdOutParameters->bBuffer,
  894. READ_ATTRIBUTE_BUFFER_SIZE);
  895. }
  896. ExFreePool(outBuffer);
  897. } else {
  898. status = STATUS_INSUFFICIENT_RESOURCES;
  899. }
  900. break;
  901. }
  902. case FailurePredictionSense:
  903. {
  904. DiskSmartData->Length = sizeof(ULONG);
  905. *((PULONG)DiskSmartData->VendorSpecific) = FdoExtension->FailureReason;
  906. status = STATUS_SUCCESS;
  907. break;
  908. }
  909. case FailurePredictionIoctl:
  910. case FailurePredictionNone:
  911. default:
  912. {
  913. status = STATUS_INVALID_DEVICE_REQUEST;
  914. break;
  915. }
  916. }
  917. return status;
  918. }
  919. NTSTATUS
  920. DiskReadFailurePredictThresholds(
  921. PFUNCTIONAL_DEVICE_EXTENSION FdoExtension,
  922. PSTORAGE_FAILURE_PREDICT_THRESHOLDS DiskSmartThresholds
  923. )
  924. /*++
  925. Routine Description:
  926. Obtains current failure prediction thresholds. Not available for
  927. FAILURE_PREDICT_SENSE types.
  928. Arguments:
  929. FdoExtension
  930. DiskSmartData
  931. Return Value:
  932. NT Status
  933. --*/
  934. {
  935. PCOMMON_DEVICE_EXTENSION commonExtension = (PCOMMON_DEVICE_EXTENSION)FdoExtension;
  936. PDISK_DATA diskData = (PDISK_DATA)(commonExtension->DriverData);
  937. NTSTATUS status;
  938. PAGED_CODE();
  939. switch(diskData->FailurePredictionCapability)
  940. {
  941. case FailurePredictionSmart:
  942. {
  943. PUCHAR outBuffer;
  944. PSENDCMDOUTPARAMS cmdOutParameters;
  945. ULONG outBufferSize;
  946. outBufferSize = sizeof(SRB_IO_CONTROL) +
  947. (sizeof(SENDCMDOUTPARAMS)-1) +
  948. READ_THRESHOLD_BUFFER_SIZE;
  949. outBuffer = ExAllocatePoolWithTag(NonPagedPool,
  950. outBufferSize,
  951. DISK_TAG_SMART);
  952. if (outBuffer != NULL)
  953. {
  954. status = DiskReadSmartThresholds(FdoExtension,
  955. (PSRB_IO_CONTROL)outBuffer,
  956. &outBufferSize);
  957. if (NT_SUCCESS(status))
  958. {
  959. cmdOutParameters = (PSENDCMDOUTPARAMS)(outBuffer +
  960. sizeof(SRB_IO_CONTROL));
  961. RtlCopyMemory(DiskSmartThresholds->VendorSpecific,
  962. cmdOutParameters->bBuffer,
  963. READ_THRESHOLD_BUFFER_SIZE);
  964. }
  965. ExFreePool(outBuffer);
  966. } else {
  967. status = STATUS_INSUFFICIENT_RESOURCES;
  968. }
  969. break;
  970. }
  971. case FailurePredictionSense:
  972. case FailurePredictionIoctl:
  973. case FailurePredictionNone:
  974. default:
  975. {
  976. status = STATUS_INVALID_DEVICE_REQUEST;
  977. break;
  978. }
  979. }
  980. return status;
  981. }
  982. void DiskReregWorker(
  983. IN PVOID Context
  984. )
  985. {
  986. PDISKREREGREQUEST reregRequest;
  987. NTSTATUS status;
  988. PDEVICE_OBJECT deviceObject;
  989. PIRP irp;
  990. PAGED_CODE();
  991. do
  992. {
  993. reregRequest = (PDISKREREGREQUEST)ExInterlockedPopEntryList(
  994. &DiskReregHead,
  995. &DiskReregSpinlock);
  996. deviceObject = reregRequest->DeviceObject;
  997. irp = reregRequest->Irp;
  998. status = IoWMIRegistrationControl(deviceObject,
  999. WMIREG_ACTION_UPDATE_GUIDS);
  1000. if (! NT_SUCCESS(status))
  1001. {
  1002. DebugPrint((1, "DiskReregWorker: Reregistration failed %x\n",
  1003. status));
  1004. }
  1005. //
  1006. // Release remove lock and free irp, now that we are done
  1007. // processing this
  1008. //
  1009. ClassReleaseRemoveLock(deviceObject, irp);
  1010. IoFreeMdl(irp->MdlAddress);
  1011. IoFreeIrp(irp);
  1012. ExFreePool(reregRequest);
  1013. } while (InterlockedDecrement(&DiskReregWorkItems));
  1014. }
  1015. NTSTATUS DiskInitializeReregistration(
  1016. void
  1017. )
  1018. {
  1019. PAGED_CODE();
  1020. //
  1021. // Initialize the global work item and spinlock used to manage the
  1022. // list of disks reregistering their guids
  1023. //
  1024. ExInitializeWorkItem( &DiskReregWorkItem,
  1025. DiskReregWorker,
  1026. NULL );
  1027. KeInitializeSpinLock(&DiskReregSpinlock);
  1028. return(STATUS_SUCCESS);
  1029. }
  1030. NTSTATUS DiskPostReregisterRequest(
  1031. PDEVICE_OBJECT DeviceObject,
  1032. PIRP Irp
  1033. )
  1034. {
  1035. PDISKREREGREQUEST reregRequest;
  1036. NTSTATUS status;
  1037. reregRequest = ExAllocatePoolWithTag(NonPagedPool,
  1038. sizeof(DISKREREGREQUEST),
  1039. DISK_TAG_SMART);
  1040. if (reregRequest != NULL)
  1041. {
  1042. //
  1043. // add the disk that needs reregistration to the stack of disks
  1044. // to reregister. If the list is transitioning from empty to
  1045. // non empty then also kick off the work item so that the
  1046. // reregistration worker can do the reregister.
  1047. //
  1048. reregRequest->DeviceObject = DeviceObject;
  1049. reregRequest->Irp = Irp;
  1050. ExInterlockedPushEntryList(
  1051. &DiskReregHead,
  1052. &reregRequest->Next,
  1053. &DiskReregSpinlock);
  1054. if (InterlockedIncrement(&DiskReregWorkItems) == 1)
  1055. {
  1056. ExQueueWorkItem( &DiskReregWorkItem, DelayedWorkQueue );
  1057. }
  1058. status = STATUS_SUCCESS;
  1059. } else {
  1060. DebugPrint((1, "DiskPostReregisterRequest: could not allocate reregRequest for %p\n",
  1061. DeviceObject));
  1062. status = STATUS_INSUFFICIENT_RESOURCES;
  1063. }
  1064. return(status);
  1065. }
  1066. NTSTATUS DiskInfoExceptionComplete(
  1067. PDEVICE_OBJECT DeviceObject,
  1068. PIRP Irp,
  1069. PVOID Context
  1070. )
  1071. {
  1072. PFUNCTIONAL_DEVICE_EXTENSION fdoExtension = DeviceObject->DeviceExtension;
  1073. PCOMMON_DEVICE_EXTENSION commonExtension = DeviceObject->DeviceExtension;
  1074. PDISK_DATA diskData = (PDISK_DATA)(commonExtension->DriverData);
  1075. PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(Irp);
  1076. PIO_STACK_LOCATION nextIrpStack = IoGetNextIrpStackLocation(Irp);
  1077. PSCSI_REQUEST_BLOCK srb = Context;
  1078. NTSTATUS status;
  1079. BOOLEAN retry;
  1080. ULONG retryInterval;
  1081. ULONG srbStatus;
  1082. BOOLEAN freeLockAndIrp = TRUE;
  1083. KIRQL oldIrql;
  1084. ASSERT(fdoExtension->CommonExtension.IsFdo);
  1085. srbStatus = SRB_STATUS(srb->SrbStatus);
  1086. //
  1087. // Check SRB status for success of completing request.
  1088. // SRB_STATUS_DATA_OVERRUN also indicates success.
  1089. //
  1090. if ((srbStatus != SRB_STATUS_SUCCESS) &&
  1091. (srbStatus != SRB_STATUS_DATA_OVERRUN))
  1092. {
  1093. DebugPrint((2, "DiskInfoExceptionComplete: IRP %p, SRB %p\n", Irp, srb));
  1094. retry = ClassInterpretSenseInfo(
  1095. DeviceObject,
  1096. srb,
  1097. irpStack->MajorFunction,
  1098. 0,
  1099. MAXIMUM_RETRIES -
  1100. ((ULONG)(ULONG_PTR)irpStack->Parameters.Others.Argument4),
  1101. &status,
  1102. &retryInterval);
  1103. //
  1104. // If the status is verified required and the this request
  1105. // should bypass verify required then retry the request.
  1106. //
  1107. if (TEST_FLAG(irpStack->Flags, SL_OVERRIDE_VERIFY_VOLUME) &&
  1108. status == STATUS_VERIFY_REQUIRED)
  1109. {
  1110. status = STATUS_IO_DEVICE_ERROR;
  1111. retry = TRUE;
  1112. }
  1113. if (retry && ((ULONG)(ULONG_PTR)irpStack->Parameters.Others.Argument4)--)
  1114. {
  1115. //
  1116. // Retry request.
  1117. //
  1118. DebugPrint((1, "DiskInfoExceptionComplete: Retry request %p\n", Irp));
  1119. ASSERT(srb->DataBuffer == MmGetMdlVirtualAddress(Irp->MdlAddress));
  1120. //
  1121. // Reset byte count of transfer in SRB Extension.
  1122. //
  1123. srb->DataTransferLength = Irp->MdlAddress->ByteCount;
  1124. //
  1125. // Zero SRB statuses.
  1126. //
  1127. srb->SrbStatus = srb->ScsiStatus = 0;
  1128. //
  1129. // Set the no disconnect flag, disable synchronous data transfers and
  1130. // disable tagged queuing. This fixes some errors.
  1131. //
  1132. SET_FLAG(srb->SrbFlags, SRB_FLAGS_DISABLE_DISCONNECT);
  1133. SET_FLAG(srb->SrbFlags, SRB_FLAGS_DISABLE_SYNCH_TRANSFER);
  1134. CLEAR_FLAG(srb->SrbFlags, SRB_FLAGS_QUEUE_ACTION_ENABLE);
  1135. srb->QueueAction = SRB_SIMPLE_TAG_REQUEST;
  1136. srb->QueueTag = SP_UNTAGGED;
  1137. //
  1138. // Set up major SCSI function.
  1139. //
  1140. nextIrpStack->MajorFunction = IRP_MJ_SCSI;
  1141. //
  1142. // Save SRB address in next stack for port driver.
  1143. //
  1144. nextIrpStack->Parameters.Scsi.Srb = srb;
  1145. IoSetCompletionRoutine(Irp,
  1146. DiskInfoExceptionComplete,
  1147. srb,
  1148. TRUE, TRUE, TRUE);
  1149. (VOID)IoCallDriver(commonExtension->LowerDeviceObject, Irp);
  1150. return STATUS_MORE_PROCESSING_REQUIRED;
  1151. }
  1152. } else {
  1153. //
  1154. // Get the results from the mode sense
  1155. //
  1156. PMODE_INFO_EXCEPTIONS pageData;
  1157. PMODE_PARAMETER_HEADER modeData;
  1158. ULONG modeDataLength;
  1159. modeData = srb->DataBuffer;
  1160. modeDataLength = srb->DataTransferLength;
  1161. pageData = ClassFindModePage((PUCHAR) modeData,
  1162. modeDataLength,
  1163. MODE_PAGE_FAULT_REPORTING,
  1164. TRUE);
  1165. if (pageData != NULL)
  1166. {
  1167. DebugPrint((1, "DiskInfoExceptionComplete: %p supports SMART\n",
  1168. DeviceObject));
  1169. if (pageData->Dexcpt == 0)
  1170. {
  1171. diskData->FailurePredictionCapability = FailurePredictionSense;
  1172. status = DiskPostReregisterRequest(DeviceObject, Irp);
  1173. if (NT_SUCCESS(status))
  1174. {
  1175. //
  1176. // Make sure we won't free the remove lock and the irp
  1177. // since we need to keep these until after the work
  1178. // item has completed running
  1179. //
  1180. freeLockAndIrp = FALSE;
  1181. }
  1182. } else {
  1183. DebugPrint((1, "DiskInfoExceptionComplete: %p is not enabled for SMART\n",
  1184. DeviceObject));
  1185. }
  1186. } else {
  1187. DebugPrint((1, "DiskInfoExceptionComplete: %p does not supports SMART\n",
  1188. DeviceObject));
  1189. }
  1190. //
  1191. // Set status for successful request
  1192. //
  1193. status = STATUS_SUCCESS;
  1194. } // end if (SRB_STATUS(srb->SrbStatus) == SRB_STATUS_SUCCESS)
  1195. //
  1196. // Free the srb
  1197. //
  1198. ExFreePool(srb->SenseInfoBuffer);
  1199. ExFreePool(srb->DataBuffer);
  1200. ExFreePool(srb);
  1201. if (freeLockAndIrp)
  1202. {
  1203. //
  1204. // Set status in completing IRP.
  1205. //
  1206. Irp->IoStatus.Status = status;
  1207. //
  1208. // If pending has be returned for this irp then mark the current stack as
  1209. // pending.
  1210. //
  1211. if (Irp->PendingReturned) {
  1212. IoMarkIrpPending(Irp);
  1213. }
  1214. ClassReleaseRemoveLock(DeviceObject, Irp);
  1215. IoFreeMdl(Irp->MdlAddress);
  1216. IoFreeIrp(Irp);
  1217. }
  1218. return(STATUS_MORE_PROCESSING_REQUIRED);
  1219. }
  1220. NTSTATUS DiskInfoExceptionCheck(
  1221. PFUNCTIONAL_DEVICE_EXTENSION FdoExtension
  1222. )
  1223. {
  1224. PUCHAR modeData;
  1225. PSCSI_REQUEST_BLOCK srb;
  1226. PCDB cdb;
  1227. PIRP irp;
  1228. PIO_STACK_LOCATION irpStack;
  1229. PVOID senseInfoBuffer;
  1230. ULONG isRemoved;
  1231. modeData = ExAllocatePoolWithTag(NonPagedPoolCacheAligned,
  1232. MODE_DATA_SIZE,
  1233. DISK_TAG_INFO_EXCEPTION);
  1234. if (modeData == NULL)
  1235. {
  1236. DebugPrint((1, "DiskInfoExceptionCheck: Can't allocate mode data "
  1237. "buffer\n"));
  1238. return(STATUS_INSUFFICIENT_RESOURCES);
  1239. }
  1240. srb = ExAllocatePoolWithTag(NonPagedPool,
  1241. SCSI_REQUEST_BLOCK_SIZE,
  1242. DISK_TAG_SRB);
  1243. if (srb == NULL)
  1244. {
  1245. ExFreePool(modeData);
  1246. DebugPrint((1, "DiskInfoExceptionCheck: Can't allocate srb "
  1247. "buffer\n"));
  1248. return(STATUS_INSUFFICIENT_RESOURCES);
  1249. }
  1250. //
  1251. // Build the MODE SENSE CDB.
  1252. //
  1253. RtlZeroMemory(srb, SCSI_REQUEST_BLOCK_SIZE);
  1254. cdb = (PCDB)srb->Cdb;
  1255. srb->CdbLength = 6;
  1256. cdb = (PCDB)srb->Cdb;
  1257. //
  1258. // Set timeout value from device extension.
  1259. //
  1260. srb->TimeOutValue = FdoExtension->TimeOutValue;
  1261. cdb->MODE_SENSE.OperationCode = SCSIOP_MODE_SENSE;
  1262. cdb->MODE_SENSE.PageCode = MODE_PAGE_FAULT_REPORTING;
  1263. cdb->MODE_SENSE.AllocationLength = MODE_DATA_SIZE;
  1264. //
  1265. // Write length to SRB.
  1266. //
  1267. srb->Length = SCSI_REQUEST_BLOCK_SIZE;
  1268. //
  1269. // Set SCSI bus address.
  1270. //
  1271. srb->Function = SRB_FUNCTION_EXECUTE_SCSI;
  1272. //
  1273. // Enable auto request sense.
  1274. //
  1275. srb->SenseInfoBufferLength = SENSE_BUFFER_SIZE;
  1276. //
  1277. // Sense buffer is in aligned nonpaged pool.
  1278. //
  1279. senseInfoBuffer = ExAllocatePoolWithTag(NonPagedPoolCacheAligned,
  1280. SENSE_BUFFER_SIZE,
  1281. '7CcS');
  1282. if (senseInfoBuffer == NULL)
  1283. {
  1284. ExFreePool(srb);
  1285. ExFreePool(modeData);
  1286. DebugPrint((1, "DiskInfoExceptionCheck: Can't allocate request sense "
  1287. "buffer\n"));
  1288. return(STATUS_INSUFFICIENT_RESOURCES);
  1289. }
  1290. srb->SenseInfoBuffer = senseInfoBuffer;
  1291. srb->DataBuffer = modeData;
  1292. srb->SrbFlags = FdoExtension->SrbFlags;
  1293. SET_FLAG(srb->SrbFlags, SRB_FLAGS_DATA_IN);
  1294. //
  1295. // Disable synchronous transfer for these requests.
  1296. //
  1297. SET_FLAG(srb->SrbFlags, SRB_FLAGS_DISABLE_SYNCH_TRANSFER);
  1298. //
  1299. // Don't freeze the queue on an error
  1300. //
  1301. SET_FLAG(srb->SrbFlags, SRB_FLAGS_NO_QUEUE_FREEZE);
  1302. srb->QueueAction = SRB_SIMPLE_TAG_REQUEST;
  1303. srb->QueueTag = SP_UNTAGGED;
  1304. //
  1305. // Build device I/O control request with METHOD_NEITHER data transfer.
  1306. // We'll queue a completion routine to cleanup the MDL's and such ourself.
  1307. //
  1308. irp = IoAllocateIrp(
  1309. (CCHAR) (FdoExtension->CommonExtension.LowerDeviceObject->StackSize + 1),
  1310. FALSE);
  1311. if (irp == NULL)
  1312. {
  1313. ExFreePool(senseInfoBuffer);
  1314. ExFreePool(srb);
  1315. ExFreePool(modeData);
  1316. DebugPrint((1, "DiskInfoExceptionCheck: Can't allocate Irp\n"));
  1317. return(STATUS_INSUFFICIENT_RESOURCES);
  1318. }
  1319. isRemoved = ClassAcquireRemoveLock(FdoExtension->DeviceObject, irp);
  1320. if (isRemoved)
  1321. {
  1322. ClassReleaseRemoveLock(FdoExtension->DeviceObject, irp);
  1323. IoFreeIrp(irp);
  1324. ExFreePool(senseInfoBuffer);
  1325. ExFreePool(srb);
  1326. ExFreePool(modeData);
  1327. DebugPrint((1, "DiskInfoExceptionCheck: RemoveLock says isRemoved\n"));
  1328. return(STATUS_DEVICE_DOES_NOT_EXIST);
  1329. }
  1330. //
  1331. // Get next stack location.
  1332. //
  1333. IoSetNextIrpStackLocation(irp);
  1334. irpStack = IoGetCurrentIrpStackLocation(irp);
  1335. irpStack->DeviceObject = FdoExtension->DeviceObject;
  1336. //
  1337. // Save retry count in current Irp stack.
  1338. //
  1339. irpStack->Parameters.Others.Argument4 = (PVOID)MAXIMUM_RETRIES;
  1340. irpStack = IoGetNextIrpStackLocation(irp);
  1341. //
  1342. // Set up SRB for execute scsi request. Save SRB address in next stack
  1343. // for the port driver.
  1344. //
  1345. irpStack->MajorFunction = IRP_MJ_SCSI;
  1346. irpStack->Parameters.Scsi.Srb = srb;
  1347. IoSetCompletionRoutine(irp,
  1348. DiskInfoExceptionComplete,
  1349. srb,
  1350. TRUE,
  1351. TRUE,
  1352. TRUE);
  1353. irp->MdlAddress = IoAllocateMdl( modeData,
  1354. MODE_DATA_SIZE,
  1355. FALSE,
  1356. FALSE,
  1357. irp );
  1358. if (irp->MdlAddress == NULL)
  1359. {
  1360. ClassReleaseRemoveLock(FdoExtension->DeviceObject, irp);
  1361. ExFreePool(srb);
  1362. ExFreePool(modeData);
  1363. ExFreePool(senseInfoBuffer);
  1364. IoFreeIrp( irp );
  1365. DebugPrint((1, "DiskINfoExceptionCheck: Can't allocate MDL\n"));
  1366. return STATUS_INSUFFICIENT_RESOURCES;
  1367. }
  1368. MmBuildMdlForNonPagedPool(irp->MdlAddress);
  1369. //
  1370. // Set the transfer length.
  1371. //
  1372. srb->DataTransferLength = MODE_DATA_SIZE;
  1373. //
  1374. // Zero out status.
  1375. //
  1376. srb->ScsiStatus = srb->SrbStatus = 0;
  1377. srb->NextSrb = 0;
  1378. //
  1379. // Set up IRP Address.
  1380. //
  1381. srb->OriginalRequest = irp;
  1382. //
  1383. // Call the port driver with the request and wait for it to complete.
  1384. //
  1385. IoMarkIrpPending(irp);
  1386. IoCallDriver(FdoExtension->CommonExtension.LowerDeviceObject,
  1387. irp);
  1388. return(STATUS_PENDING);
  1389. }
  1390. NTSTATUS
  1391. DiskDetectFailurePrediction(
  1392. PFUNCTIONAL_DEVICE_EXTENSION FdoExtension,
  1393. PFAILURE_PREDICTION_METHOD FailurePredictCapability
  1394. )
  1395. /*++
  1396. Routine Description:
  1397. Detect if device has any failure prediction capabilities. First we
  1398. check for IDE SMART capability. This is done by sending the drive an
  1399. IDENTIFY command and checking if the SMART command set bit is set.
  1400. Next we check if SCSI SMART (aka Information Exception Control Page,
  1401. X3T10/94-190 Rev 4). This is done by querying for the Information
  1402. Exception mode page.
  1403. Lastly we check if the device has IOCTL failure prediction. This mechanism
  1404. a filter driver implements IOCTL_STORAGE_PREDICT_FAILURE and will respond
  1405. with the information in the IOCTL. We do this by sending the ioctl and
  1406. if the status returned is STATUS_SUCCESS we assume that it is supported.
  1407. Arguments:
  1408. FdoExtension
  1409. *FailurePredictCapability
  1410. Return Value:
  1411. NT Status
  1412. --*/
  1413. {
  1414. PCOMMON_DEVICE_EXTENSION commonExtension = (PCOMMON_DEVICE_EXTENSION)FdoExtension;
  1415. PDISK_DATA diskData = (PDISK_DATA)(commonExtension->DriverData);
  1416. BOOLEAN supportFP;
  1417. NTSTATUS status;
  1418. STORAGE_PREDICT_FAILURE checkFailure;
  1419. STORAGE_FAILURE_PREDICT_STATUS diskSmartStatus;
  1420. BOOLEAN logErr;
  1421. PAGED_CODE();
  1422. //
  1423. // Assume no failure predict mechanisms
  1424. //
  1425. *FailurePredictCapability = FailurePredictionNone;
  1426. //
  1427. // See if this is an IDE drive that supports SMART. If so enable SMART
  1428. // and then ensure that it suports the SMART READ STATUS command
  1429. //
  1430. status = DiskGetIdentifyInfo(FdoExtension,
  1431. &supportFP);
  1432. if (supportFP)
  1433. {
  1434. status = DiskEnableSmart(FdoExtension);
  1435. if (NT_SUCCESS(status))
  1436. {
  1437. *FailurePredictCapability = FailurePredictionSmart;
  1438. status = DiskReadFailurePredictStatus(FdoExtension,
  1439. &diskSmartStatus);
  1440. DebugPrint((1, "Disk: Device %p %s IDE SMART\n",
  1441. FdoExtension->DeviceObject,
  1442. NT_SUCCESS(status) ? "does" : "does not"));
  1443. if (! NT_SUCCESS(status))
  1444. {
  1445. *FailurePredictCapability = FailurePredictionNone;
  1446. }
  1447. }
  1448. return(status);
  1449. }
  1450. //
  1451. // See if there is a a filter driver to intercept
  1452. // IOCTL_STORAGE_PREDICT_FAILURE
  1453. //
  1454. status = DiskSendFailurePredictIoctl(FdoExtension,
  1455. &checkFailure);
  1456. DebugPrint((1, "Disk: Device %p %s IOCTL_STORAGE_FAILURE_PREDICT\n",
  1457. FdoExtension->DeviceObject,
  1458. NT_SUCCESS(status) ? "does" : "does not"));
  1459. if (NT_SUCCESS(status))
  1460. {
  1461. *FailurePredictCapability = FailurePredictionIoctl;
  1462. if (checkFailure.PredictFailure)
  1463. {
  1464. checkFailure.PredictFailure = 512;
  1465. ClassNotifyFailurePredicted(FdoExtension,
  1466. (PUCHAR)&checkFailure,
  1467. sizeof(checkFailure),
  1468. (BOOLEAN)(FdoExtension->FailurePredicted == FALSE),
  1469. 0x11,
  1470. diskData->ScsiAddress.PathId,
  1471. diskData->ScsiAddress.TargetId,
  1472. diskData->ScsiAddress.Lun);
  1473. FdoExtension->FailurePredicted = TRUE;
  1474. }
  1475. return(status);
  1476. }
  1477. //
  1478. // Finally we assume it will not be a scsi smart drive. but
  1479. // we'll also send off an asynchronous mode sense so that if
  1480. // it is SMART we'll reregister the device object
  1481. //
  1482. DiskInfoExceptionCheck(FdoExtension);
  1483. *FailurePredictCapability = FailurePredictionNone;
  1484. return(STATUS_SUCCESS);
  1485. }
  1486. NTSTATUS
  1487. DiskWmiFunctionControl(
  1488. IN PDEVICE_OBJECT DeviceObject,
  1489. IN PIRP Irp,
  1490. IN ULONG GuidIndex,
  1491. IN CLASSENABLEDISABLEFUNCTION Function,
  1492. IN BOOLEAN Enable
  1493. )
  1494. /*++
  1495. Routine Description:
  1496. This routine is a callback into the driver to enabled or disable event
  1497. generation or data block collection. A device should only expect a
  1498. single enable when the first event or data consumer enables events or
  1499. data collection and a single disable when the last event or data
  1500. consumer disables events or data collection. Data blocks will only
  1501. receive collection enable/disable if they were registered as requiring
  1502. it.
  1503. When NT boots, failure prediction is not automatically enabled, although
  1504. it may have been persistantly enabled on a previous boot. Polling is also
  1505. not automatically enabled. When the first data block that accesses SMART
  1506. such as SmartStatusGuid, SmartDataGuid, SmartPerformFunction, or
  1507. SmartEventGuid is accessed then SMART is automatically enabled in the
  1508. hardware. Polling is enabled when SmartEventGuid is enabled and disabled
  1509. when it is disabled. Hardware SMART is only disabled when the DisableSmart
  1510. method is called. Polling is also disabled when this is called regardless
  1511. of the status of the other guids or events.
  1512. Arguments:
  1513. DeviceObject is the device whose data block is being queried
  1514. GuidIndex is the index into the list of guids provided when the
  1515. device registered
  1516. Function specifies which functionality is being enabled or disabled
  1517. Enable is TRUE then the function is being enabled else disabled
  1518. Return Value:
  1519. status
  1520. --*/
  1521. {
  1522. NTSTATUS status = STATUS_SUCCESS;
  1523. PCOMMON_DEVICE_EXTENSION commonExtension = DeviceObject->DeviceExtension;
  1524. PFUNCTIONAL_DEVICE_EXTENSION fdoExtension = DeviceObject->DeviceExtension;
  1525. PDISK_DATA diskData = (PDISK_DATA)(commonExtension->DriverData);
  1526. ULONG enableCount;
  1527. PAGED_CODE();
  1528. if ((Function == DataBlockCollection) && Enable)
  1529. {
  1530. if ((GuidIndex == SmartStatusGuid) ||
  1531. (GuidIndex == SmartDataGuid) ||
  1532. (GuidIndex == SmartThresholdsGuid) ||
  1533. (GuidIndex == SmartPerformFunction))
  1534. {
  1535. status = DiskEnableDisableFailurePrediction(fdoExtension,
  1536. TRUE);
  1537. DebugPrint((3, "Disk: DeviceObject %p, Irp %p Enable -> %lx\n",
  1538. DeviceObject,
  1539. Irp,
  1540. status));
  1541. } else {
  1542. DebugPrint((3, "Disk: DeviceObject %p, Irp %p, GuidIndex %d %s for Collection\n",
  1543. DeviceObject, Irp,
  1544. GuidIndex,
  1545. Enable ? "Enabled" : "Disabled")); }
  1546. } else if (Function == EventGeneration) {
  1547. DebugPrint((3, "Disk: DeviceObject %p, Irp %p, GuidIndex %d %s for Event Generation\n",
  1548. DeviceObject, Irp,
  1549. GuidIndex,
  1550. Enable ? "Enabled" : "Disabled"));
  1551. if ((GuidIndex == SmartEventGuid) && Enable)
  1552. {
  1553. status = DiskEnableDisableFailurePredictPolling(fdoExtension,
  1554. Enable,
  1555. 0);
  1556. DebugPrint((3, "Disk: DeviceObject %p, Irp %p %s -> %lx\n",
  1557. DeviceObject,
  1558. Irp,
  1559. Enable ? "DiskEnableSmartPolling" : "DiskDisableSmartPolling",
  1560. status));
  1561. }
  1562. #if DBG
  1563. } else {
  1564. DebugPrint((3, "Disk: DeviceObject %p, Irp %p, GuidIndex %d %s for function %d\n",
  1565. DeviceObject, Irp,
  1566. GuidIndex,
  1567. Enable ? "Enabled" : "Disabled",
  1568. Function));
  1569. #endif
  1570. }
  1571. status = ClassWmiCompleteRequest(DeviceObject,
  1572. Irp,
  1573. status,
  1574. 0,
  1575. IO_NO_INCREMENT);
  1576. return status;
  1577. }
  1578. NTSTATUS
  1579. DiskFdoQueryWmiRegInfo(
  1580. IN PDEVICE_OBJECT DeviceObject,
  1581. OUT ULONG *RegFlags,
  1582. OUT PUNICODE_STRING InstanceName
  1583. )
  1584. /*++
  1585. Routine Description:
  1586. This routine is a callback into the driver to retrieve the list of
  1587. guids or data blocks that the driver wants to register with WMI. This
  1588. routine may not pend or block. Driver should NOT call
  1589. ClassWmiCompleteRequest.
  1590. Arguments:
  1591. DeviceObject is the device whose data block is being queried
  1592. *RegFlags returns with a set of flags that describe the guids being
  1593. registered for this device. If the device wants enable and disable
  1594. collection callbacks before receiving queries for the registered
  1595. guids then it should return the WMIREG_FLAG_EXPENSIVE flag. Also the
  1596. returned flags may specify WMIREG_FLAG_INSTANCE_PDO in which case
  1597. the instance name is determined from the PDO associated with the
  1598. device object. Note that the PDO must have an associated devnode. If
  1599. WMIREG_FLAG_INSTANCE_PDO is not set then Name must return a unique
  1600. name for the device.
  1601. InstanceName returns with the instance name for the guids if
  1602. WMIREG_FLAG_INSTANCE_PDO is not set in the returned *RegFlags. The
  1603. caller will call ExFreePool with the buffer returned.
  1604. Return Value:
  1605. status
  1606. --*/
  1607. {
  1608. PFUNCTIONAL_DEVICE_EXTENSION fdoExtension = DeviceObject->DeviceExtension;
  1609. PCOMMON_DEVICE_EXTENSION commonExtension = DeviceObject->DeviceExtension;
  1610. PDISK_DATA diskData = (PDISK_DATA)(commonExtension->DriverData);
  1611. NTSTATUS status;
  1612. PAGED_CODE();
  1613. SET_FLAG(DiskWmiFdoGuidList[SmartThresholdsGuid].Flags, WMIREG_FLAG_REMOVE_GUID);
  1614. SET_FLAG(DiskWmiFdoGuidList[ScsiInfoExceptionsGuid].Flags, WMIREG_FLAG_REMOVE_GUID);
  1615. switch (diskData->FailurePredictionCapability)
  1616. {
  1617. case FailurePredictionSmart:
  1618. {
  1619. CLEAR_FLAG(DiskWmiFdoGuidList[SmartThresholdsGuid].Flags, WMIREG_FLAG_REMOVE_GUID);
  1620. //
  1621. // Fall Through
  1622. //
  1623. }
  1624. case FailurePredictionIoctl:
  1625. {
  1626. CLEAR_FLAG(DiskWmiFdoGuidList[SmartStatusGuid].Flags, WMIREG_FLAG_REMOVE_GUID);
  1627. CLEAR_FLAG(DiskWmiFdoGuidList[SmartDataGuid].Flags, WMIREG_FLAG_REMOVE_GUID);
  1628. CLEAR_FLAG(DiskWmiFdoGuidList[SmartEventGuid].Flags, WMIREG_FLAG_REMOVE_GUID);
  1629. CLEAR_FLAG(DiskWmiFdoGuidList[SmartPerformFunction].Flags, WMIREG_FLAG_REMOVE_GUID);
  1630. break;
  1631. }
  1632. case FailurePredictionSense:
  1633. {
  1634. CLEAR_FLAG(DiskWmiFdoGuidList[SmartStatusGuid].Flags, WMIREG_FLAG_REMOVE_GUID);
  1635. CLEAR_FLAG(DiskWmiFdoGuidList[SmartEventGuid].Flags, WMIREG_FLAG_REMOVE_GUID);
  1636. CLEAR_FLAG(DiskWmiFdoGuidList[SmartPerformFunction].Flags, WMIREG_FLAG_REMOVE_GUID);
  1637. CLEAR_FLAG(DiskWmiFdoGuidList[ScsiInfoExceptionsGuid].Flags, WMIREG_FLAG_REMOVE_GUID);
  1638. SET_FLAG (DiskWmiFdoGuidList[SmartDataGuid].Flags, WMIREG_FLAG_REMOVE_GUID);
  1639. break;
  1640. }
  1641. default:
  1642. {
  1643. SET_FLAG (DiskWmiFdoGuidList[SmartStatusGuid].Flags, WMIREG_FLAG_REMOVE_GUID);
  1644. SET_FLAG (DiskWmiFdoGuidList[SmartDataGuid].Flags, WMIREG_FLAG_REMOVE_GUID);
  1645. SET_FLAG (DiskWmiFdoGuidList[SmartEventGuid].Flags, WMIREG_FLAG_REMOVE_GUID);
  1646. SET_FLAG (DiskWmiFdoGuidList[SmartPerformFunction].Flags, WMIREG_FLAG_REMOVE_GUID);
  1647. break;
  1648. }
  1649. }
  1650. //
  1651. // Use devnode for FDOs
  1652. *RegFlags = WMIREG_FLAG_INSTANCE_PDO;
  1653. return STATUS_SUCCESS;
  1654. }
  1655. NTSTATUS
  1656. DiskFdoQueryWmiRegInfoEx(
  1657. IN PDEVICE_OBJECT DeviceObject,
  1658. OUT ULONG *RegFlags,
  1659. OUT PUNICODE_STRING InstanceName,
  1660. OUT PUNICODE_STRING MofName
  1661. )
  1662. /*++
  1663. Routine Description:
  1664. This routine is a callback into the driver to retrieve the list of
  1665. guids or data blocks that the driver wants to register with WMI. This
  1666. routine may not pend or block. Driver should NOT call
  1667. ClassWmiCompleteRequest.
  1668. Arguments:
  1669. DeviceObject is the device whose data block is being queried
  1670. *RegFlags returns with a set of flags that describe the guids being
  1671. registered for this device. If the device wants enable and disable
  1672. collection callbacks before receiving queries for the registered
  1673. guids then it should return the WMIREG_FLAG_EXPENSIVE flag. Also the
  1674. returned flags may specify WMIREG_FLAG_INSTANCE_PDO in which case
  1675. the instance name is determined from the PDO associated with the
  1676. device object. Note that the PDO must have an associated devnode. If
  1677. WMIREG_FLAG_INSTANCE_PDO is not set then Name must return a unique
  1678. name for the device.
  1679. InstanceName returns with the instance name for the guids if
  1680. WMIREG_FLAG_INSTANCE_PDO is not set in the returned *RegFlags. The
  1681. caller will call ExFreePool with the buffer returned.
  1682. MofName returns initialized with the mof resource name for the
  1683. binary mof resource attached to the driver's image file. If the
  1684. driver does not have a mof resource then it should leave this
  1685. parameter untouched.
  1686. Return Value:
  1687. status
  1688. --*/
  1689. {
  1690. NTSTATUS status;
  1691. status = DiskFdoQueryWmiRegInfo(DeviceObject,
  1692. RegFlags,
  1693. InstanceName);
  1694. //
  1695. // Leave MofName alone since disk doesn't have one
  1696. //
  1697. return(status);
  1698. }
  1699. NTSTATUS
  1700. DiskFdoQueryWmiDataBlock(
  1701. IN PDEVICE_OBJECT DeviceObject,
  1702. IN PIRP Irp,
  1703. IN ULONG GuidIndex,
  1704. IN ULONG BufferAvail,
  1705. OUT PUCHAR Buffer
  1706. )
  1707. /*++
  1708. Routine Description:
  1709. This routine is a callback into the driver to query for the contents of
  1710. a data block. When the driver has finished filling the data block it
  1711. must call ClassWmiCompleteRequest to complete the irp. The driver can
  1712. return STATUS_PENDING if the irp cannot be completed immediately.
  1713. Arguments:
  1714. DeviceObject is the device whose data block is being queried
  1715. Irp is the Irp that makes this request
  1716. GuidIndex is the index into the list of guids provided when the
  1717. device registered
  1718. BufferAvail on has the maximum size available to write the data
  1719. block.
  1720. Buffer on return is filled with the returned data block
  1721. Return Value:
  1722. status
  1723. --*/
  1724. {
  1725. NTSTATUS status;
  1726. PFUNCTIONAL_DEVICE_EXTENSION fdoExtension = DeviceObject->DeviceExtension;
  1727. PCOMMON_DEVICE_EXTENSION commonExtension = DeviceObject->DeviceExtension;
  1728. PDISK_DATA diskData = (PDISK_DATA)(commonExtension->DriverData);
  1729. ULONG sizeNeeded;
  1730. PAGED_CODE();
  1731. DebugPrint((3, "Disk: DiskQueryWmiDataBlock, Device %p, Irp %p, GuiIndex %d\n"
  1732. " BufferAvail %lx Buffer %lx\n",
  1733. DeviceObject, Irp,
  1734. GuidIndex, BufferAvail, Buffer));
  1735. switch (GuidIndex)
  1736. {
  1737. case DiskGeometryGuid:
  1738. {
  1739. sizeNeeded = sizeof(DISK_GEOMETRY);
  1740. if (BufferAvail >= sizeNeeded)
  1741. {
  1742. if (DeviceObject->Characteristics & FILE_REMOVABLE_MEDIA)
  1743. {
  1744. //
  1745. // Issue ReadCapacity to update device extension
  1746. // with information for current media.
  1747. status = DiskReadDriveCapacity(commonExtension->PartitionZeroExtension->DeviceObject);
  1748. //
  1749. // Note whether the drive is ready.
  1750. diskData->ReadyStatus = status;
  1751. if (!NT_SUCCESS(status))
  1752. {
  1753. break;
  1754. }
  1755. }
  1756. //
  1757. // Copy drive geometry information from device extension.
  1758. RtlMoveMemory(Buffer,
  1759. &(fdoExtension->DiskGeometry),
  1760. sizeof(DISK_GEOMETRY));
  1761. status = STATUS_SUCCESS;
  1762. } else {
  1763. status = STATUS_BUFFER_TOO_SMALL;
  1764. }
  1765. break;
  1766. }
  1767. case SmartStatusGuid:
  1768. {
  1769. PSTORAGE_FAILURE_PREDICT_STATUS diskSmartStatus;
  1770. ASSERT(diskData->FailurePredictionCapability != FailurePredictionNone);
  1771. sizeNeeded = sizeof(STORAGE_FAILURE_PREDICT_STATUS);
  1772. if (BufferAvail >= sizeNeeded)
  1773. {
  1774. STORAGE_PREDICT_FAILURE checkFailure;
  1775. diskSmartStatus = (PSTORAGE_FAILURE_PREDICT_STATUS)Buffer;
  1776. status = DiskSendFailurePredictIoctl(fdoExtension,
  1777. &checkFailure);
  1778. if (NT_SUCCESS(status))
  1779. {
  1780. if (diskData->FailurePredictionCapability ==
  1781. FailurePredictionSense)
  1782. {
  1783. diskSmartStatus->Reason = *((PULONG)checkFailure.VendorSpecific);
  1784. } else {
  1785. diskSmartStatus->Reason = 0; // unknown
  1786. }
  1787. diskSmartStatus->PredictFailure = (checkFailure.PredictFailure != 0);
  1788. }
  1789. } else {
  1790. status = STATUS_BUFFER_TOO_SMALL;
  1791. }
  1792. break;
  1793. }
  1794. case SmartDataGuid:
  1795. {
  1796. PSTORAGE_FAILURE_PREDICT_DATA diskSmartData;
  1797. ASSERT((diskData->FailurePredictionCapability ==
  1798. FailurePredictionSmart) ||
  1799. (diskData->FailurePredictionCapability ==
  1800. FailurePredictionIoctl));
  1801. sizeNeeded = sizeof(STORAGE_FAILURE_PREDICT_DATA);
  1802. if (BufferAvail >= sizeNeeded)
  1803. {
  1804. PSTORAGE_PREDICT_FAILURE checkFailure = (PSTORAGE_PREDICT_FAILURE)Buffer;
  1805. diskSmartData = (PSTORAGE_FAILURE_PREDICT_DATA)Buffer;
  1806. status = DiskSendFailurePredictIoctl(fdoExtension,
  1807. checkFailure);
  1808. if (NT_SUCCESS(status))
  1809. {
  1810. diskSmartData->Length = 512;
  1811. }
  1812. } else {
  1813. status = STATUS_BUFFER_TOO_SMALL;
  1814. }
  1815. break;
  1816. }
  1817. case SmartThresholdsGuid:
  1818. {
  1819. PSTORAGE_FAILURE_PREDICT_THRESHOLDS diskSmartThresholds;
  1820. ASSERT((diskData->FailurePredictionCapability ==
  1821. FailurePredictionSmart));
  1822. sizeNeeded = sizeof(STORAGE_FAILURE_PREDICT_THRESHOLDS);
  1823. if (BufferAvail >= sizeNeeded)
  1824. {
  1825. diskSmartThresholds = (PSTORAGE_FAILURE_PREDICT_THRESHOLDS)Buffer;
  1826. status = DiskReadFailurePredictThresholds(fdoExtension,
  1827. diskSmartThresholds);
  1828. } else {
  1829. status = STATUS_BUFFER_TOO_SMALL;
  1830. }
  1831. break;
  1832. }
  1833. case SmartPerformFunction:
  1834. {
  1835. sizeNeeded = 0;
  1836. status = STATUS_SUCCESS;
  1837. break;
  1838. }
  1839. case ScsiInfoExceptionsGuid:
  1840. {
  1841. PSTORAGE_SCSI_INFO_EXCEPTIONS infoExceptions;
  1842. MODE_INFO_EXCEPTIONS modeInfo;
  1843. ASSERT((diskData->FailurePredictionCapability ==
  1844. FailurePredictionSense));
  1845. sizeNeeded = sizeof(STORAGE_SCSI_INFO_EXCEPTIONS);
  1846. if (BufferAvail >= sizeNeeded)
  1847. {
  1848. infoExceptions = (PSTORAGE_SCSI_INFO_EXCEPTIONS)Buffer;
  1849. status = DiskGetInfoExceptionInformation(fdoExtension,
  1850. &modeInfo);
  1851. if (NT_SUCCESS(status))
  1852. {
  1853. infoExceptions->PageSavable = modeInfo.PSBit;
  1854. infoExceptions->Flags = modeInfo.Flags;
  1855. infoExceptions->MRIE = modeInfo.ReportMethod;
  1856. infoExceptions->Padding = 0;
  1857. REVERSE_BYTES(&infoExceptions->IntervalTimer,
  1858. &modeInfo.IntervalTimer);
  1859. REVERSE_BYTES(&infoExceptions->ReportCount,
  1860. &modeInfo.ReportCount)
  1861. }
  1862. } else {
  1863. status = STATUS_BUFFER_TOO_SMALL;
  1864. }
  1865. break;
  1866. }
  1867. default:
  1868. {
  1869. sizeNeeded = 0;
  1870. status = STATUS_WMI_GUID_NOT_FOUND;
  1871. }
  1872. }
  1873. DebugPrint((3, "Disk: DiskQueryWmiDataBlock Device %p, Irp %p returns %lx\n",
  1874. DeviceObject, Irp, status));
  1875. status = ClassWmiCompleteRequest(DeviceObject,
  1876. Irp,
  1877. status,
  1878. sizeNeeded,
  1879. IO_NO_INCREMENT);
  1880. return status;
  1881. }
  1882. NTSTATUS
  1883. DiskFdoSetWmiDataBlock(
  1884. IN PDEVICE_OBJECT DeviceObject,
  1885. IN PIRP Irp,
  1886. IN ULONG GuidIndex,
  1887. IN ULONG BufferSize,
  1888. IN PUCHAR Buffer
  1889. )
  1890. /*++
  1891. Routine Description:
  1892. This routine is a callback into the driver to query for the contents of
  1893. a data block. When the driver has finished filling the data block it
  1894. must call ClassWmiCompleteRequest to complete the irp. The driver can
  1895. return STATUS_PENDING if the irp cannot be completed immediately.
  1896. Arguments:
  1897. DeviceObject is the device whose data block is being queried
  1898. Irp is the Irp that makes this request
  1899. GuidIndex is the index into the list of guids provided when the
  1900. device registered
  1901. BufferSize has the size of the data block passed
  1902. Buffer has the new values for the data block
  1903. Return Value:
  1904. status
  1905. --*/
  1906. {
  1907. NTSTATUS status;
  1908. PFUNCTIONAL_DEVICE_EXTENSION fdoExtension = DeviceObject->DeviceExtension;
  1909. PCOMMON_DEVICE_EXTENSION commonExtension = DeviceObject->DeviceExtension;
  1910. PDISK_DATA diskData = (PDISK_DATA)(commonExtension->DriverData);
  1911. PAGED_CODE();
  1912. DebugPrint((3, "Disk: DiskSetWmiDataBlock, Device %p, Irp %p, GuiIndex %d\n"
  1913. " BufferSize %#x Buffer %p\n",
  1914. DeviceObject, Irp,
  1915. GuidIndex, BufferSize, Buffer));
  1916. if (GuidIndex == ScsiInfoExceptionsGuid)
  1917. {
  1918. PSTORAGE_SCSI_INFO_EXCEPTIONS infoExceptions;
  1919. MODE_INFO_EXCEPTIONS modeInfo;
  1920. if (BufferSize >= sizeof(STORAGE_SCSI_INFO_EXCEPTIONS))
  1921. {
  1922. infoExceptions = (PSTORAGE_SCSI_INFO_EXCEPTIONS)Buffer;
  1923. modeInfo.PageCode = MODE_PAGE_FAULT_REPORTING;
  1924. modeInfo.PageLength = sizeof(MODE_INFO_EXCEPTIONS) - 2;
  1925. modeInfo.PSBit = 0;
  1926. modeInfo.Flags = infoExceptions->Flags;
  1927. modeInfo.ReportMethod = infoExceptions->MRIE;
  1928. REVERSE_BYTES(&modeInfo.IntervalTimer[0],
  1929. &infoExceptions->IntervalTimer);
  1930. REVERSE_BYTES(&modeInfo.ReportCount[0],
  1931. &infoExceptions->ReportCount);
  1932. if (modeInfo.Perf == 1)
  1933. {
  1934. diskData->AllowFPPerfHit = FALSE;
  1935. } else {
  1936. diskData->AllowFPPerfHit = TRUE;
  1937. }
  1938. status = DiskSetInfoExceptionInformation(fdoExtension,
  1939. &modeInfo);
  1940. } else {
  1941. status = STATUS_INVALID_PARAMETER;
  1942. }
  1943. } else if (GuidIndex <= SmartEventGuid)
  1944. {
  1945. status = STATUS_WMI_READ_ONLY;
  1946. } else {
  1947. status = STATUS_WMI_GUID_NOT_FOUND;
  1948. }
  1949. DebugPrint((3, "Disk: DiskSetWmiDataBlock Device %p, Irp %p returns %lx\n",
  1950. DeviceObject, Irp, status));
  1951. status = ClassWmiCompleteRequest(DeviceObject,
  1952. Irp,
  1953. status,
  1954. 0,
  1955. IO_NO_INCREMENT);
  1956. return status;
  1957. }
  1958. NTSTATUS
  1959. DiskFdoSetWmiDataItem(
  1960. IN PDEVICE_OBJECT DeviceObject,
  1961. IN PIRP Irp,
  1962. IN ULONG GuidIndex,
  1963. IN ULONG DataItemId,
  1964. IN ULONG BufferSize,
  1965. IN PUCHAR Buffer
  1966. )
  1967. /*++
  1968. Routine Description:
  1969. This routine is a callback into the driver to query for the contents of
  1970. a data block. When the driver has finished filling the data block it
  1971. must call ClassWmiCompleteRequest to complete the irp. The driver can
  1972. return STATUS_PENDING if the irp cannot be completed immediately.
  1973. Arguments:
  1974. DeviceObject is the device whose data block is being queried
  1975. Irp is the Irp that makes this request
  1976. GuidIndex is the index into the list of guids provided when the
  1977. device registered
  1978. DataItemId has the id of the data item being set
  1979. BufferSize has the size of the data item passed
  1980. Buffer has the new values for the data item
  1981. Return Value:
  1982. status
  1983. --*/
  1984. {
  1985. NTSTATUS status;
  1986. PAGED_CODE();
  1987. DebugPrint((3, "Disk: DiskSetWmiDataItem, Device %p, Irp %p, GuiIndex %d, DataId %d\n"
  1988. " BufferSize %#x Buffer %p\n",
  1989. DeviceObject, Irp,
  1990. GuidIndex, DataItemId, BufferSize, Buffer));
  1991. if (GuidIndex <= SmartEventGuid)
  1992. {
  1993. status = STATUS_WMI_READ_ONLY;
  1994. } else {
  1995. status = STATUS_WMI_GUID_NOT_FOUND;
  1996. }
  1997. DebugPrint((3, "Disk: DiskSetWmiDataItem Device %p, Irp %p returns %lx\n",
  1998. DeviceObject, Irp, status));
  1999. status = ClassWmiCompleteRequest(DeviceObject,
  2000. Irp,
  2001. status,
  2002. 0,
  2003. IO_NO_INCREMENT);
  2004. return status;
  2005. }
  2006. NTSTATUS
  2007. DiskFdoExecuteWmiMethod(
  2008. IN PDEVICE_OBJECT DeviceObject,
  2009. IN PIRP Irp,
  2010. IN ULONG GuidIndex,
  2011. IN ULONG MethodId,
  2012. IN ULONG InBufferSize,
  2013. IN ULONG OutBufferSize,
  2014. IN PUCHAR Buffer
  2015. )
  2016. /*++
  2017. Routine Description:
  2018. This routine is a callback into the driver to execute a method. When the
  2019. driver has finished filling the data block it must call
  2020. ClassWmiCompleteRequest to complete the irp. The driver can
  2021. return STATUS_PENDING if the irp cannot be completed immediately.
  2022. Arguments:
  2023. DeviceObject is the device whose data block is being queried
  2024. Irp is the Irp that makes this request
  2025. GuidIndex is the index into the list of guids provided when the
  2026. device registered
  2027. MethodId has the id of the method being called
  2028. InBufferSize has the size of the data block passed in as the input to
  2029. the method.
  2030. OutBufferSize on entry has the maximum size available to write the
  2031. returned data block.
  2032. Buffer is filled with the returned data block
  2033. Return Value:
  2034. status
  2035. --*/
  2036. {
  2037. PFUNCTIONAL_DEVICE_EXTENSION fdoExtension = DeviceObject->DeviceExtension;
  2038. PCOMMON_DEVICE_EXTENSION commonExtension = DeviceObject->DeviceExtension;
  2039. PDISK_DATA diskData = (PDISK_DATA)(commonExtension->DriverData);
  2040. ULONG sizeNeeded;
  2041. NTSTATUS status;
  2042. PAGED_CODE();
  2043. DebugPrint((3, "Disk: DiskExecuteWmiMethod, DeviceObject %p, Irp %p, Guid Id %d, MethodId %d\n"
  2044. " InBufferSize %#x, OutBufferSize %#x, Buffer %p\n",
  2045. DeviceObject, Irp,
  2046. GuidIndex, MethodId, InBufferSize, OutBufferSize, Buffer));
  2047. switch(GuidIndex)
  2048. {
  2049. case SmartPerformFunction:
  2050. {
  2051. ASSERT((diskData->FailurePredictionCapability ==
  2052. FailurePredictionSmart) ||
  2053. (diskData->FailurePredictionCapability ==
  2054. FailurePredictionIoctl) ||
  2055. (diskData->FailurePredictionCapability ==
  2056. FailurePredictionSense));
  2057. switch(MethodId)
  2058. {
  2059. //
  2060. // void AllowPerformanceHit([in] boolean Allow)
  2061. //
  2062. case AllowDisallowPerformanceHit:
  2063. {
  2064. BOOLEAN allowPerfHit;
  2065. sizeNeeded = 0;
  2066. if (InBufferSize >= sizeof(BOOLEAN))
  2067. {
  2068. status = STATUS_SUCCESS;
  2069. allowPerfHit = *((PBOOLEAN)Buffer);
  2070. if (diskData->AllowFPPerfHit != allowPerfHit)
  2071. {
  2072. diskData->AllowFPPerfHit = allowPerfHit;
  2073. if (diskData->FailurePredictionCapability ==
  2074. FailurePredictionSense)
  2075. {
  2076. MODE_INFO_EXCEPTIONS modeInfo;
  2077. status = DiskGetInfoExceptionInformation(fdoExtension,
  2078. &modeInfo);
  2079. if (NT_SUCCESS(status))
  2080. {
  2081. modeInfo.Perf = allowPerfHit ? 0 : 1;
  2082. status = DiskSetInfoExceptionInformation(fdoExtension,
  2083. &modeInfo);
  2084. }
  2085. }
  2086. }
  2087. DebugPrint((3, "DiskFdoWmiExecuteMethod: AllowPerformanceHit %x for device %p --> %lx\n",
  2088. allowPerfHit,
  2089. fdoExtension->DeviceObject,
  2090. status));
  2091. } else {
  2092. status = STATUS_INVALID_PARAMETER;
  2093. }
  2094. break;
  2095. }
  2096. //
  2097. // void EnableDisableHardwareFailurePrediction([in] boolean Enable)
  2098. //
  2099. case EnableDisableHardwareFailurePrediction:
  2100. {
  2101. BOOLEAN enable;
  2102. sizeNeeded = 0;
  2103. if (InBufferSize >= sizeof(BOOLEAN))
  2104. {
  2105. status = STATUS_SUCCESS;
  2106. enable = *((PBOOLEAN)Buffer);
  2107. if (! enable)
  2108. {
  2109. //
  2110. // If we are disabling we need to also disable
  2111. // polling
  2112. //
  2113. DiskEnableDisableFailurePredictPolling(
  2114. fdoExtension,
  2115. enable,
  2116. 0);
  2117. }
  2118. status = DiskEnableDisableFailurePrediction(
  2119. fdoExtension,
  2120. enable);
  2121. DebugPrint((3, "DiskFdoWmiExecuteMethod: EnableDisableHardwareFailurePrediction: %x for device %p --> %lx\n",
  2122. enable,
  2123. fdoExtension->DeviceObject,
  2124. status));
  2125. } else {
  2126. status = STATUS_INVALID_PARAMETER;
  2127. }
  2128. break;
  2129. }
  2130. //
  2131. // void EnableDisableFailurePredictionPolling(
  2132. // [in] uint32 Period,
  2133. // [in] boolean Enable)
  2134. //
  2135. case EnableDisableFailurePredictionPolling:
  2136. {
  2137. BOOLEAN enable;
  2138. ULONG period;
  2139. sizeNeeded = 0;
  2140. if (InBufferSize >= (sizeof(ULONG) + sizeof(BOOLEAN)))
  2141. {
  2142. period = *((PULONG)Buffer);
  2143. Buffer += sizeof(ULONG);
  2144. enable = *((PBOOLEAN)Buffer);
  2145. status = DiskEnableDisableFailurePredictPolling(
  2146. fdoExtension,
  2147. enable,
  2148. period);
  2149. DebugPrint((3, "DiskFdoWmiExecuteMethod: EnableDisableFailurePredictionPolling: %x %x for device %p --> %lx\n",
  2150. enable,
  2151. period,
  2152. fdoExtension->DeviceObject,
  2153. status));
  2154. } else {
  2155. status = STATUS_INVALID_PARAMETER;
  2156. }
  2157. break;
  2158. }
  2159. //
  2160. // void GetFailurePredictionCapability([out] uint32 Capability)
  2161. //
  2162. case GetFailurePredictionCapability:
  2163. {
  2164. sizeNeeded = sizeof(ULONG);
  2165. if (OutBufferSize >= sizeNeeded)
  2166. {
  2167. status = STATUS_SUCCESS;
  2168. *((PFAILURE_PREDICTION_METHOD)Buffer) = diskData->FailurePredictionCapability;
  2169. DebugPrint((3, "DiskFdoWmiExecuteMethod: GetFailurePredictionCapability: %x for device %p --> %lx\n",
  2170. *((PFAILURE_PREDICTION_METHOD)Buffer),
  2171. fdoExtension->DeviceObject,
  2172. status));
  2173. } else {
  2174. status = STATUS_BUFFER_TOO_SMALL;
  2175. }
  2176. break;
  2177. }
  2178. //
  2179. // void EnableOfflineDiags([out] boolean Success);
  2180. //
  2181. case EnableOfflineDiags:
  2182. {
  2183. sizeNeeded = sizeof(BOOLEAN);
  2184. if (OutBufferSize >= sizeNeeded)
  2185. {
  2186. if (diskData->FailurePredictionCapability ==
  2187. FailurePredictionSmart)
  2188. {
  2189. //
  2190. // Initiate or resume offline diagnostics.
  2191. // This may cause a loss of performance
  2192. // to the disk, but mayincrease the amount
  2193. // of disk checking.
  2194. //
  2195. status = DiskExecuteSmartDiagnostics(fdoExtension,
  2196. 0);
  2197. } else {
  2198. status = STATUS_INVALID_DEVICE_REQUEST;
  2199. }
  2200. *((PBOOLEAN)Buffer) = NT_SUCCESS(status);
  2201. DebugPrint((3, "DiskFdoWmiExecuteMethod: EnableOfflineDiags for device %p --> %lx\n",
  2202. fdoExtension->DeviceObject,
  2203. status));
  2204. } else {
  2205. status = STATUS_BUFFER_TOO_SMALL;
  2206. }
  2207. break;
  2208. }
  2209. //
  2210. // void ReadLogSectors([in] uint8 LogAddress,
  2211. // [in] uint8 SectorCount,
  2212. // [out] uint32 Length,
  2213. // [out, WmiSizeIs("Length")] uint8 LogSectors[]
  2214. // );
  2215. //
  2216. case ReadLogSectors:
  2217. {
  2218. if (diskData->FailurePredictionCapability ==
  2219. FailurePredictionSmart)
  2220. {
  2221. if (InBufferSize >= sizeof(READ_LOG_SECTORS_IN))
  2222. {
  2223. PREAD_LOG_SECTORS_IN inParams;
  2224. PREAD_LOG_SECTORS_OUT outParams;
  2225. ULONG readSize;
  2226. inParams = (PREAD_LOG_SECTORS_IN)Buffer;
  2227. readSize = inParams->SectorCount * SMART_LOG_SECTOR_SIZE;
  2228. sizeNeeded = FIELD_OFFSET(READ_LOG_SECTORS_OUT,
  2229. LogSectors) + readSize;
  2230. if (OutBufferSize >= sizeNeeded)
  2231. {
  2232. outParams = (PREAD_LOG_SECTORS_OUT)Buffer;
  2233. status = DiskReadSmartLog(fdoExtension,
  2234. inParams->SectorCount,
  2235. inParams->LogAddress,
  2236. outParams->LogSectors);
  2237. if (NT_SUCCESS(status))
  2238. {
  2239. outParams->Length = readSize;
  2240. } else {
  2241. //
  2242. // SMART command failure is
  2243. // indicated by successful
  2244. // execution, but no data returned
  2245. //
  2246. outParams->Length = 0;
  2247. status = STATUS_SUCCESS;
  2248. }
  2249. } else {
  2250. status = STATUS_BUFFER_TOO_SMALL;
  2251. }
  2252. } else {
  2253. status = STATUS_INVALID_PARAMETER;
  2254. }
  2255. } else {
  2256. status = STATUS_INVALID_DEVICE_REQUEST;
  2257. }
  2258. break;
  2259. }
  2260. // void WriteLogSectors([in] uint8 LogAddress,
  2261. // [in] uint8 SectorCount,
  2262. // [in] uint32 Length,
  2263. // [in, WmiSizeIs("Length")] uint8 LogSectors[],
  2264. // [out] boolean Success
  2265. // );
  2266. case WriteLogSectors:
  2267. {
  2268. if (diskData->FailurePredictionCapability ==
  2269. FailurePredictionSmart)
  2270. {
  2271. if (InBufferSize >= FIELD_OFFSET(WRITE_LOG_SECTORS_IN,
  2272. LogSectors))
  2273. {
  2274. PWRITE_LOG_SECTORS_IN inParams;
  2275. PWRITE_LOG_SECTORS_OUT outParams;
  2276. ULONG writeSize;
  2277. inParams = (PWRITE_LOG_SECTORS_IN)Buffer;
  2278. writeSize = inParams->SectorCount * SMART_LOG_SECTOR_SIZE;
  2279. if (InBufferSize >= (FIELD_OFFSET(WRITE_LOG_SECTORS_IN,
  2280. LogSectors) +
  2281. writeSize))
  2282. {
  2283. sizeNeeded = sizeof(WRITE_LOG_SECTORS_OUT);
  2284. if (OutBufferSize >= sizeNeeded)
  2285. {
  2286. outParams = (PWRITE_LOG_SECTORS_OUT)Buffer;
  2287. status = DiskWriteSmartLog(fdoExtension,
  2288. inParams->SectorCount,
  2289. inParams->LogAddress,
  2290. inParams->LogSectors);
  2291. if (NT_SUCCESS(status))
  2292. {
  2293. outParams->Success = TRUE;
  2294. } else {
  2295. outParams->Success = FALSE;
  2296. status = STATUS_SUCCESS;
  2297. }
  2298. } else {
  2299. status = STATUS_BUFFER_TOO_SMALL;
  2300. }
  2301. } else {
  2302. status = STATUS_INVALID_PARAMETER;
  2303. }
  2304. } else {
  2305. status = STATUS_INVALID_PARAMETER;
  2306. }
  2307. } else {
  2308. status = STATUS_INVALID_DEVICE_REQUEST;
  2309. }
  2310. break;
  2311. }
  2312. // void ExecuteSelfTest([in] uint8 Subcommand,
  2313. // [out,
  2314. // Values{"0", "1", "2"},
  2315. // ValueMap{"Successful Completion",
  2316. // "Captive Mode Required",
  2317. // "Unsuccessful Completion"}
  2318. // ]
  2319. // uint32 ReturnCode);
  2320. case ExecuteSelfTest:
  2321. {
  2322. if (diskData->FailurePredictionCapability ==
  2323. FailurePredictionSmart)
  2324. {
  2325. if (InBufferSize >= sizeof(EXECUTE_SELF_TEST_IN))
  2326. {
  2327. sizeNeeded = sizeof(EXECUTE_SELF_TEST_OUT);
  2328. if (OutBufferSize >= sizeNeeded)
  2329. {
  2330. PEXECUTE_SELF_TEST_IN inParam;
  2331. PEXECUTE_SELF_TEST_OUT outParam;
  2332. inParam = (PEXECUTE_SELF_TEST_IN)Buffer;
  2333. outParam = (PEXECUTE_SELF_TEST_OUT)Buffer;
  2334. if (DiskIsValidSmartSelfTest(inParam->Subcommand))
  2335. {
  2336. status = DiskExecuteSmartDiagnostics(fdoExtension,
  2337. inParam->Subcommand);
  2338. if (NT_SUCCESS(status))
  2339. {
  2340. //
  2341. // Return self test executed
  2342. // without a problem
  2343. //
  2344. outParam->ReturnCode = 0;
  2345. } else {
  2346. //
  2347. // Return Self test execution
  2348. // failed status
  2349. //
  2350. outParam->ReturnCode = 2;
  2351. status = STATUS_SUCCESS;
  2352. }
  2353. } else {
  2354. //
  2355. // If self test subcommand requires
  2356. // captive mode then return that
  2357. // status
  2358. //
  2359. outParam->ReturnCode = 1;
  2360. status = STATUS_SUCCESS;
  2361. }
  2362. } else {
  2363. status = STATUS_BUFFER_TOO_SMALL;
  2364. }
  2365. } else {
  2366. status = STATUS_INVALID_PARAMETER;
  2367. }
  2368. } else {
  2369. status = STATUS_INVALID_DEVICE_REQUEST;
  2370. }
  2371. break;
  2372. }
  2373. default :
  2374. {
  2375. sizeNeeded = 0;
  2376. status = STATUS_WMI_ITEMID_NOT_FOUND;
  2377. break;
  2378. }
  2379. }
  2380. break;
  2381. }
  2382. case DiskGeometryGuid:
  2383. case SmartStatusGuid:
  2384. case SmartDataGuid:
  2385. {
  2386. sizeNeeded = 0;
  2387. status = STATUS_INVALID_DEVICE_REQUEST;
  2388. break;
  2389. }
  2390. default:
  2391. {
  2392. sizeNeeded = 0;
  2393. status = STATUS_WMI_GUID_NOT_FOUND;
  2394. }
  2395. }
  2396. DebugPrint((3, "Disk: DiskExecuteMethod Device %p, Irp %p returns %lx\n",
  2397. DeviceObject, Irp, status));
  2398. status = ClassWmiCompleteRequest(DeviceObject,
  2399. Irp,
  2400. status,
  2401. sizeNeeded,
  2402. IO_NO_INCREMENT);
  2403. return status;
  2404. }
  2405. #if 0
  2406. //
  2407. // Enable this to add WMI support for PDOs
  2408. NTSTATUS
  2409. DiskPdoQueryWmiRegInfo(
  2410. IN PDEVICE_OBJECT DeviceObject,
  2411. OUT ULONG *RegFlags,
  2412. OUT PUNICODE_STRING InstanceName
  2413. )
  2414. /*++
  2415. Routine Description:
  2416. This routine is a callback into the driver to retrieve the list of
  2417. guids or data blocks that the driver wants to register with WMI. This
  2418. routine may not pend or block. Driver should NOT call
  2419. ClassWmiCompleteRequest.
  2420. Arguments:
  2421. DeviceObject is the device whose data block is being queried
  2422. *RegFlags returns with a set of flags that describe the guids being
  2423. registered for this device. If the device wants enable and disable
  2424. collection callbacks before receiving queries for the registered
  2425. guids then it should return the WMIREG_FLAG_EXPENSIVE flag. Also the
  2426. returned flags may specify WMIREG_FLAG_INSTANCE_PDO in which case
  2427. the instance name is determined from the PDO associated with the
  2428. device object. Note that the PDO must have an associated devnode. If
  2429. WMIREG_FLAG_INSTANCE_PDO is not set then Name must return a unique
  2430. name for the device.
  2431. InstanceName returns with the instance name for the guids if
  2432. WMIREG_FLAG_INSTANCE_PDO is not set in the returned *RegFlags. The
  2433. caller will call ExFreePool with the buffer returned.
  2434. Return Value:
  2435. status
  2436. --*/
  2437. {
  2438. PCOMMON_DEVICE_EXTENSION commonExtension = DeviceObject->DeviceExtension;
  2439. PFUNCTIONAL_DEVICE_EXTENSION parentFunctionalExtension;
  2440. ANSI_STRING ansiString;
  2441. CHAR name[256];
  2442. NTSTATUS status;
  2443. //
  2444. // We need to pick a name for PDOs since they do not have a devnode
  2445. parentFunctionalExtension = commonExtension->PartitionZeroExtension;
  2446. sprintf(name,
  2447. "Disk(%d)_Partition(%d)_Start(%#I64x)_Length(%#I64x)",
  2448. parentFunctionalExtension->DeviceNumber,
  2449. commonExtension->PartitionNumber,
  2450. commonExtension->StartingOffset.QuadPart,
  2451. commonExtension->PartitionLength.QuadPart);
  2452. RtlInitAnsiString(&ansiString,
  2453. name);
  2454. status = RtlAnsiStringToUnicodeString(InstanceName,
  2455. &ansiString,
  2456. TRUE);
  2457. return status;
  2458. }
  2459. NTSTATUS
  2460. DiskPdoQueryWmiDataBlock(
  2461. IN PDEVICE_OBJECT DeviceObject,
  2462. IN PIRP Irp,
  2463. IN ULONG GuidIndex,
  2464. IN ULONG BufferAvail,
  2465. OUT PUCHAR Buffer
  2466. )
  2467. /*++
  2468. Routine Description:
  2469. This routine is a callback into the driver to query for the contents of
  2470. a data block. When the driver has finished filling the data block it
  2471. must call ClassWmiCompleteRequest to complete the irp. The driver can
  2472. return STATUS_PENDING if the irp cannot be completed immediately.
  2473. Arguments:
  2474. DeviceObject is the device whose data block is being queried
  2475. Irp is the Irp that makes this request
  2476. GuidIndex is the index into the list of guids provided when the
  2477. device registered
  2478. BufferAvail on has the maximum size available to write the data
  2479. block.
  2480. Buffer on return is filled with the returned data block
  2481. Return Value:
  2482. status
  2483. --*/
  2484. {
  2485. NTSTATUS status;
  2486. PCOMMON_DEVICE_EXTENSION commonExtension = DeviceObject->DeviceExtension;
  2487. PDISK_DATA diskData = (PDISK_DATA)(commonExtension->DriverData);
  2488. ULONG sizeNeeded;
  2489. DebugPrint((3, "Disk: DiskQueryWmiDataBlock, Device %p, Irp %p, GuiIndex %d\n"
  2490. " BufferAvail %#x Buffer %p\n",
  2491. DeviceObject, Irp,
  2492. GuidIndex, BufferAvail, Buffer));
  2493. switch (GuidIndex)
  2494. {
  2495. case 0:
  2496. {
  2497. sizeNeeded = 4 * sizeof(ULONG);
  2498. if (BufferAvail >= sizeNeeded)
  2499. {
  2500. RtlCopyMemory(Buffer, DiskDummyData, sizeNeeded);
  2501. status = STATUS_SUCCESS;
  2502. } else {
  2503. status = STATUS_BUFFER_TOO_SMALL;
  2504. }
  2505. break;
  2506. }
  2507. default:
  2508. {
  2509. status = STATUS_WMI_GUID_NOT_FOUND;
  2510. }
  2511. }
  2512. DebugPrint((3, "Disk: DiskQueryWmiDataBlock Device %p, Irp %p returns %lx\n",
  2513. DeviceObject, Irp, status));
  2514. status = ClassWmiCompleteRequest(DeviceObject,
  2515. Irp,
  2516. status,
  2517. sizeNeeded,
  2518. IO_NO_INCREMENT);
  2519. return status;
  2520. }
  2521. NTSTATUS
  2522. DiskPdoSetWmiDataBlock(
  2523. IN PDEVICE_OBJECT DeviceObject,
  2524. IN PIRP Irp,
  2525. IN ULONG GuidIndex,
  2526. IN ULONG BufferSize,
  2527. IN PUCHAR Buffer
  2528. )
  2529. /*++
  2530. Routine Description:
  2531. This routine is a callback into the driver to query for the contents of
  2532. a data block. When the driver has finished filling the data block it
  2533. must call ClassWmiCompleteRequest to complete the irp. The driver can
  2534. return STATUS_PENDING if the irp cannot be completed immediately.
  2535. Arguments:
  2536. DeviceObject is the device whose data block is being queried
  2537. Irp is the Irp that makes this request
  2538. GuidIndex is the index into the list of guids provided when the
  2539. device registered
  2540. BufferSize has the size of the data block passed
  2541. Buffer has the new values for the data block
  2542. Return Value:
  2543. status
  2544. --*/
  2545. {
  2546. NTSTATUS status;
  2547. PCOMMON_DEVICE_EXTENSION commonExtension = DeviceObject->DeviceExtension;
  2548. ULONG sizeNeeded;
  2549. DebugPrint((3, "Disk: DiskSetWmiDataBlock, Device %p, Irp %p, GuiIndex %d\n"
  2550. " BufferSize %#x Buffer %p\n",
  2551. DeviceObject, Irp,
  2552. GuidIndex, BufferSize, Buffer));
  2553. switch(GuidIndex)
  2554. {
  2555. case 0:
  2556. {
  2557. sizeNeeded = 4 * sizeof(ULONG);
  2558. if (BufferSize == sizeNeeded)
  2559. {
  2560. RtlCopyMemory(DiskDummyData, Buffer, sizeNeeded);
  2561. status = STATUS_SUCCESS;
  2562. } else {
  2563. status = STATUS_INFO_LENGTH_MISMATCH;
  2564. }
  2565. break;
  2566. }
  2567. default:
  2568. {
  2569. status = STATUS_WMI_GUID_NOT_FOUND;
  2570. }
  2571. }
  2572. DebugPrint((3, "Disk: DiskSetWmiDataBlock Device %p, Irp %p returns %lx\n",
  2573. DeviceObject, Irp, status));
  2574. status = ClassWmiCompleteRequest(DeviceObject,
  2575. Irp,
  2576. status,
  2577. 0,
  2578. IO_NO_INCREMENT);
  2579. return status;
  2580. }
  2581. NTSTATUS
  2582. DiskPdoSetWmiDataItem(
  2583. IN PDEVICE_OBJECT DeviceObject,
  2584. IN PIRP Irp,
  2585. IN ULONG GuidIndex,
  2586. IN ULONG DataItemId,
  2587. IN ULONG BufferSize,
  2588. IN PUCHAR Buffer
  2589. )
  2590. /*++
  2591. Routine Description:
  2592. This routine is a callback into the driver to query for the contents of
  2593. a data block. When the driver has finished filling the data block it
  2594. must call ClassWmiCompleteRequest to complete the irp. The driver can
  2595. return STATUS_PENDING if the irp cannot be completed immediately.
  2596. Arguments:
  2597. DeviceObject is the device whose data block is being queried
  2598. Irp is the Irp that makes this request
  2599. GuidIndex is the index into the list of guids provided when the
  2600. device registered
  2601. DataItemId has the id of the data item being set
  2602. BufferSize has the size of the data item passed
  2603. Buffer has the new values for the data item
  2604. Return Value:
  2605. status
  2606. --*/
  2607. {
  2608. NTSTATUS status;
  2609. DebugPrint((3, "Disk: DiskSetWmiDataItem, Device %p, Irp %p, GuiIndex %d, DataId %d\n"
  2610. " BufferSize %#x Buffer %p\n",
  2611. DeviceObject, Irp,
  2612. GuidIndex, DataItemId, BufferSize, Buffer));
  2613. switch(GuidIndex)
  2614. {
  2615. case 0:
  2616. {
  2617. if ((BufferSize == sizeof(ULONG)) &&
  2618. (DataItemId <= 3))
  2619. {
  2620. DiskDummyData[DataItemId] = *((PULONG)Buffer);
  2621. status = STATUS_SUCCESS;
  2622. } else {
  2623. status = STATUS_INVALID_DEVICE_REQUEST;
  2624. }
  2625. break;
  2626. }
  2627. default:
  2628. {
  2629. status = STATUS_WMI_GUID_NOT_FOUND;
  2630. }
  2631. }
  2632. DebugPrint((3, "Disk: DiskSetWmiDataItem Device %p, Irp %p returns %lx\n",
  2633. DeviceObject, Irp, status));
  2634. status = ClassWmiCompleteRequest(DeviceObject,
  2635. Irp,
  2636. status,
  2637. 0,
  2638. IO_NO_INCREMENT);
  2639. return status;
  2640. }
  2641. NTSTATUS
  2642. DiskPdoExecuteWmiMethod(
  2643. IN PDEVICE_OBJECT DeviceObject,
  2644. IN PIRP Irp,
  2645. IN ULONG GuidIndex,
  2646. IN ULONG MethodId,
  2647. IN ULONG InBufferSize,
  2648. IN ULONG OutBufferSize,
  2649. IN PUCHAR Buffer
  2650. )
  2651. /*++
  2652. Routine Description:
  2653. This routine is a callback into the driver to execute a method. When the
  2654. driver has finished filling the data block it must call
  2655. ClassWmiCompleteRequest to complete the irp. The driver can
  2656. return STATUS_PENDING if the irp cannot be completed immediately.
  2657. Arguments:
  2658. DeviceObject is the device whose data block is being queried
  2659. Irp is the Irp that makes this request
  2660. GuidIndex is the index into the list of guids provided when the
  2661. device registered
  2662. MethodId has the id of the method being called
  2663. InBufferSize has the size of the data block passed in as the input to
  2664. the method.
  2665. OutBufferSize on entry has the maximum size available to write the
  2666. returned data block.
  2667. Buffer is filled with the returned data block
  2668. Return Value:
  2669. status
  2670. --*/
  2671. {
  2672. ULONG sizeNeeded = 4 * sizeof(ULONG);
  2673. NTSTATUS status;
  2674. ULONG tempData[4];
  2675. DebugPrint((3, "Disk: DiskExecuteWmiMethod, DeviceObject %p, Irp %p, Guid Id %d, MethodId %d\n"
  2676. " InBufferSize %#x, OutBufferSize %#x, Buffer %p\n",
  2677. DeviceObject, Irp,
  2678. GuidIndex, MethodId, InBufferSize, OutBufferSize, Buffer));
  2679. switch(GuidIndex)
  2680. {
  2681. case 0:
  2682. {
  2683. if (MethodId == 1)
  2684. {
  2685. if (OutBufferSize >= sizeNeeded)
  2686. {
  2687. if (InBufferSize == sizeNeeded)
  2688. {
  2689. RtlCopyMemory(tempData, Buffer, sizeNeeded);
  2690. RtlCopyMemory(Buffer, DiskDummyData, sizeNeeded);
  2691. RtlCopyMemory(DiskDummyData, tempData, sizeNeeded);
  2692. status = STATUS_SUCCESS;
  2693. } else {
  2694. status = STATUS_INVALID_DEVICE_REQUEST;
  2695. }
  2696. } else {
  2697. status = STATUS_BUFFER_TOO_SMALL;
  2698. }
  2699. } else {
  2700. status = STATUS_INVALID_DEVICE_REQUEST;
  2701. }
  2702. break;
  2703. }
  2704. default:
  2705. {
  2706. status = STATUS_WMI_GUID_NOT_FOUND;
  2707. }
  2708. }
  2709. DebugPrint((3, "Disk: DiskExecuteMethod Device %p, Irp %p returns %lx\n",
  2710. DeviceObject, Irp, status));
  2711. status = ClassWmiCompleteRequest(DeviceObject,
  2712. Irp,
  2713. status,
  2714. 0,
  2715. IO_NO_INCREMENT);
  2716. return status;
  2717. }
  2718. #endif