Identify bad disk before IRP_MN_START_DEVICE

Hi,

We have a situation where we want to identify whether a disk is bad as soon as it is enumerated and presented to our SCSI class driver.
By the disk being “bad” I mean the disk fails TEST_UNIT_READY commands.
But by the time we realize that, it is too late for us as we’ve built up our device stack for that disk and cleaning it up is going to be very expensive.

To fix this, I figured if I could fire a TEST_UNIT_READY command before attaching an FDO to the disk PDO and if it fails, we’ll return failure from AddDevice and we’ll be all set.
But apparently sending an SRB synchronously to a disk while we’re in the middle of processing AddDevice (for the same disk) crashes the system.

So I thought the next best place to do that could be from the IRP_MN_START_DEVICE notification.
We can let AddDevice succeed and when we get START_DEVICE, we fire the TEST_UNIT_READY at the underlying disk and only if that call succeeds, we’ll return success from START_DEVICE.
If the call fails we’ll return FAILURE and PNP will do the cleanup of objects for us.

However the system leads to a crash regardless.

What is going wrong here?
Is there a better approach to check whether a disk is good or bad?

Any help will be greatly appreciated.

I don’t think it makes sense to speculate about crashes without looking at !analyze -v output.

If I remember correctly the PnP manager doesn’t cleanup with IRP_MN_REMOVE the device stack if an error is returned from the START_DEVICE. It just stops building the related stacks( BusRelations etc ) and marks the device as not started so IRP_MJ_CREATE fails. The device is still present in the system and the stack will be torn down when the bus to which the device has been connected reports the device as missing or there is a request to the PnP Manager to disable the device so IRP_MN_REMOVE is sent but the stack is not being rebuilt though a PDO for the device is present at the level the device is disabled.