IOSCSIReducedBlockCommandsDevicePM.cpp [plain text]
#include <IOKit/scsi-commands/SCSITask.h>
#include <IOKit/scsi-commands/IOSCSIReducedBlockCommandsDevice.h>
#if ( SCSI_RBC_DEVICE_DEBUGGING_LEVEL >= 1 )
#define PANIC_NOW(x) IOPanic x
#else
#define PANIC_NOW(x)
#endif
#if ( SCSI_RBC_DEVICE_DEBUGGING_LEVEL >= 2 )
#define ERROR_LOG(x) IOLog x
#else
#define ERROR_LOG(x)
#endif
#if ( SCSI_RBC_DEVICE_DEBUGGING_LEVEL >= 3 )
#define STATUS_LOG(x) IOLog x
#else
#define STATUS_LOG(x)
#endif
#define super IOSCSIPrimaryCommandsDevice
static IOPMPowerState sPowerStates[kRBCNumPowerStates] =
{
{ kIOPMPowerStateVersion1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ kIOPMPowerStateVersion1, 0, IOPMPowerOn, IOPMPowerOn, 0, 0, 0, 0, 0, 0, 0, 0 },
{ kIOPMPowerStateVersion1, (IOPMDeviceUsable | kIOPMPreventIdleSleep), IOPMPowerOn, IOPMPowerOn, 0, 0, 0, 0, 0, 0, 0, 0 },
{ kIOPMPowerStateVersion1, (IOPMDeviceUsable | kIOPMPreventIdleSleep), IOPMPowerOn, IOPMPowerOn, 0, 0, 0, 0, 0, 0, 0, 0 },
{ kIOPMPowerStateVersion1, (IOPMDeviceUsable | IOPMMaxPerformance | kIOPMPreventIdleSleep), IOPMPowerOn, IOPMPowerOn, 0, 0, 0, 0, 0, 0, 0, 0 }
};
UInt32
IOSCSIReducedBlockCommandsDevice::GetInitialPowerState ( void )
{
return kRBCPowerStateActive;
}
UInt32
IOSCSIReducedBlockCommandsDevice::GetNumberOfPowerStateTransitions ( void )
{
return ( kRBCPowerStateActive - kRBCPowerStateSleep );
}
void
IOSCSIReducedBlockCommandsDevice::InitializePowerManagement ( IOService * provider )
{
STATUS_LOG ( ( "IOSCSIReducedBlockCommandsDevice::initForPM called\n" ) );
fCurrentPowerState = kRBCPowerStateActive;
super::InitializePowerManagement ( provider );
registerPowerDriver ( this, sPowerStates, kRBCNumPowerStates );
changePowerStateTo ( kRBCPowerStateSleep );
}
void
IOSCSIReducedBlockCommandsDevice::HandleCheckPowerState ( void )
{
if ( IsDeviceAccessEnabled ( ) )
{
super::HandleCheckPowerState ( kRBCPowerStateActive );
}
}
void
IOSCSIReducedBlockCommandsDevice::TicklePowerManager ( void )
{
( void ) super::TicklePowerManager ( kRBCPowerStateActive );
}
void
IOSCSIReducedBlockCommandsDevice::HandlePowerChange ( void )
{
SCSIServiceResponse serviceResponse;
SCSITaskIdentifier request = NULL;
STATUS_LOG ( ( "IOSCSIReducedBlockCommandsDevice::HandlePowerChange called\n" ) );
request = GetSCSITask ( );
while ( ( fProposedPowerState != fCurrentPowerState ) && ( isInactive ( ) == false ) )
{
STATUS_LOG ( ( "fProposedPowerState = %ld, fCurrentPowerState = %ld\n",
fProposedPowerState, fCurrentPowerState ) );
if ( ( fCurrentPowerState <= kRBCPowerStateSleep ) && ( fProposedPowerState > kRBCPowerStateSleep ) )
{
STATUS_LOG ( ( "We think we're in sleep\n" ) );
STATUS_LOG ( ( "calling ClearPowerOnReset\n" ) );
if ( ClearPowerOnReset ( ) == false )
break;
STATUS_LOG ( ( "calling ClearNotReadyStatus\n" ) );
if ( ClearNotReadyStatus ( ) == false )
break;
if ( fMediaIsRemovable == true )
{
if ( fMediaPresent == true )
{
bool mediaPresent = CheckMediaPresence ( );
if ( mediaPresent != fMediaPresent )
{
PANIC_NOW ( ( "Unexpected Media Removal when waking up from sleep\n" ) );
}
else
{
if ( PREVENT_ALLOW_MEDIUM_REMOVAL ( request, 1 ) == true )
{
serviceResponse = SendCommand ( request, 0 );
}
else
{
PANIC_NOW ( ( "IOSCSIBlockCommandsDevice::PollForMedia malformed command" ) );
}
}
}
else
{
EnablePolling ( );
}
}
}
switch ( fProposedPowerState )
{
case kRBCPowerStateSystemSleep:
{
UInt32 previousPowerState;
STATUS_LOG ( ( "case kRBCPowerStateSystemSleep\n" ) );
if ( fMediaPresent == false )
{
STATUS_LOG ( ( "Disabling polling\n" ) );
DisablePolling ( );
}
previousPowerState = fCurrentPowerState;
fCurrentPowerState = fProposedPowerState;
while ( fNumCommandsOutstanding > 1 )
{
IOSleep ( 1 );
}
if ( previousPowerState != kRBCPowerStateSleep )
{
if ( fDeviceSupportsPowerConditions )
{
STATUS_LOG ( ( "Sending START_STOP_UNIT to drive to turn it off\n" ) );
if ( START_STOP_UNIT ( request, 1, 0x05, 0, 0 ) == true )
{
( void ) SendCommand ( request, 0 );
}
}
else
{
STATUS_LOG ( ( "Power conditions not supported, make sure drive is spun down\n" ) );
if ( START_STOP_UNIT ( request, 1, 0, 0, 0 ) == true )
{
serviceResponse = SendCommand ( request, 0 );
}
}
}
fCurrentPowerState = kRBCPowerStateSystemSleep;
}
break;
case kRBCPowerStateSleep:
{
STATUS_LOG ( ( "case kRBCPowerStateSleep\n" ) );
if ( fCurrentPowerState > kRBCPowerStateSleep )
{
STATUS_LOG ( ( "At minimum, make sure drive is spun down.\n" ) );
if ( START_STOP_UNIT ( request, 1, 0, 0, 0 ) == true )
{
serviceResponse = SendCommand ( request, 0 );
}
}
fCurrentPowerState = kRBCPowerStateSleep;
}
break;
case kRBCPowerStateStandby:
{
STATUS_LOG ( ( "case kRBCPowerStateStandby\n" ) );
if ( fDeviceSupportsPowerConditions )
{
STATUS_LOG ( ( "Sending START_STOP_UNIT to put drive in standby mode\n" ) );
if ( START_STOP_UNIT ( request, 1, 0x03, 0, 0 ) == true )
{
serviceResponse = SendCommand ( request, 0 );
}
}
fCurrentPowerState = kRBCPowerStateStandby;
}
break;
case kRBCPowerStateIdle:
{
STATUS_LOG ( ( "case kRBCPowerStateIdle\n" ) );
if ( fDeviceSupportsPowerConditions )
{
if ( START_STOP_UNIT ( request, 1, 0x02, 0, 0 ) == true )
{
serviceResponse = SendCommand ( request, 0 );
}
}
fCurrentPowerState = kRBCPowerStateIdle;
}
break;
case kRBCPowerStateActive:
{
STATUS_LOG ( ( "case kRBCPowerStateActive\n" ) );
if ( fDeviceSupportsPowerConditions )
{
STATUS_LOG ( ( "Sending START_STOP_UNIT to drive to put it in active mode\n" ) );
if ( START_STOP_UNIT ( request, 1, 0x01, 0, 0 ) == true )
{
serviceResponse = SendCommand ( request, 0 );
}
}
if ( fMediaPresent )
{
if ( START_STOP_UNIT ( request, 0x00, 0x00, 0x00, 0x01 ) == true )
{
STATUS_LOG ( ( "Sending START_STOP_UNIT.\n" ) );
serviceResponse = SendCommand ( request, 0 );
}
}
fCurrentPowerState = kRBCPowerStateActive;
fCommandGate->commandWakeup ( &fCurrentPowerState, false );
}
break;
default:
PANIC_NOW ( ( "Undefined power state issued\n" ) );
break;
}
}
ReleaseSCSITask ( request );
}
bool
IOSCSIReducedBlockCommandsDevice::CheckMediaPresence ( void )
{
SCSI_Sense_Data senseBuffer;
IOMemoryDescriptor * bufferDesc;
SCSITaskIdentifier request;
bool mediaPresent = false;
bool driveReady = false;
SCSIServiceResponse serviceResponse = kSCSIServiceResponse_SERVICE_DELIVERY_OR_TARGET_FAILURE;
STATUS_LOG ( ( "%s::%s called\n", getName ( ), __FUNCTION__ ) );
bufferDesc = IOMemoryDescriptor::withAddress ( ( void * ) &senseBuffer,
kSenseDefaultSize,
kIODirectionIn );
request = GetSCSITask ( );
do
{
if ( TEST_UNIT_READY ( request ) == true )
{
STATUS_LOG ( ( "sending TUR.\n" ) );
serviceResponse = SendCommand ( request, 0 );
}
else
{
PANIC_NOW ( ( "IOSCSIReducedBlockCommandsDevice::CheckMediaPresence malformed command" ) );
}
if ( serviceResponse == kSCSIServiceResponse_TASK_COMPLETE )
{
STATUS_LOG ( ( "TUR completed.\n" ) );
bool validSense = false;
if ( GetTaskStatus ( request ) == kSCSITaskStatus_CHECK_CONDITION )
{
validSense = GetAutoSenseData ( request, &senseBuffer );
if ( validSense == false )
{
if ( REQUEST_SENSE ( request, bufferDesc, kSenseDefaultSize ) == true )
{
STATUS_LOG ( ( "sending REQ_SENSE.\n" ) );
serviceResponse = SendCommand ( request, 0 );
}
else
{
PANIC_NOW ( ( "IOSCSIReducedBlockCommandsDevice::CheckMediaPresence malformed command" ) );
}
if ( serviceResponse == kSCSIServiceResponse_TASK_COMPLETE )
{
STATUS_LOG ( ( "validSense data.\n" ) );
validSense = true;
}
}
if ( validSense == true )
{
STATUS_LOG ( ( "sense data: %01x, %02x, %02x\n",
( senseBuffer.SENSE_KEY & kSENSE_KEY_Mask ),
senseBuffer.ADDITIONAL_SENSE_CODE,
senseBuffer.ADDITIONAL_SENSE_CODE_QUALIFIER ) );
if ( ( ( senseBuffer.SENSE_KEY & kSENSE_KEY_Mask ) == kSENSE_KEY_NOT_READY ) ||
( ( senseBuffer.SENSE_KEY & kSENSE_KEY_Mask ) == kSENSE_KEY_MEDIUM_ERROR ) )
{
if ( ( senseBuffer.ADDITIONAL_SENSE_CODE == 0x04 ) &&
( senseBuffer.ADDITIONAL_SENSE_CODE_QUALIFIER == 0x02 ) )
{
if ( START_STOP_UNIT ( request, 0x00, 0x00, 0x00, 0x01 ) == true )
{
STATUS_LOG ( ( "Sending START_STOP_UNIT.\n" ) );
serviceResponse = SendCommand ( request, 0 );
}
else
{
PANIC_NOW ( ( "IOSCSIReducedBlockCommandsDevice::CheckMediaPresence malformed command" ) );
}
STATUS_LOG ( ( "%s::drive NOT READY\n", getName ( ) ) );
IOSleep ( 200 );
continue;
}
else if ( ( senseBuffer.ADDITIONAL_SENSE_CODE == 0x3A ) &&
( senseBuffer.ADDITIONAL_SENSE_CODE_QUALIFIER == 0x00 ) )
{
STATUS_LOG ( ( "No Media.\n" ) );
driveReady = true;
mediaPresent = false;
}
else
{
STATUS_LOG ( ( "%s::drive NOT READY\n", getName ( ) ) );
IOSleep ( 200 );
continue;
}
}
else
{
STATUS_LOG ( ( "%s::drive READY, media present\n", getName ( ) ) );
driveReady = true;
mediaPresent = true;
}
}
}
else
{
STATUS_LOG ( ( "%s::drive READY, media present\n", getName ( ) ) );
driveReady = true;
mediaPresent = true;
}
}
else
{
IOSleep ( 200 );
}
} while ( ( driveReady == false ) && ( isInactive ( ) == false ) );
bufferDesc->release ( );
ReleaseSCSITask ( request );
return mediaPresent;
}