2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * IOATAStandardController.cpp
27 #include <IOKit/ata/IOATAStandardInterface.h>
28 #include <IOKit/IOSyncer.h>
31 #define super IOService
33 OSDefineMetaClass( IOATAStandardController
, IOService
)
34 OSDefineAbstractStructors( IOATAStandardController
, IOService
);
36 #define round(x,y) (((int)(x) + (y) - 1) & ~((y)-1))
42 bool IOATAStandardController::start( IOService
*forProvider
)
44 provider
= forProvider
;
48 if ( provider
->open( this ) != true )
53 if ( createWorkLoop() != true )
58 if ( configureController() != true )
60 provider
->close( this );
64 if ( scanATABus() != true )
66 provider
->close( this );
78 bool IOATAStandardController::scanATABus()
80 if ( createDeviceNubs() != true )
87 if ( initTimings() == false )
92 disableControllerInterrupts();
94 if ( reset() != kIOReturnSuccess
)
99 enableControllerInterrupts();
101 if ( probeDeviceNubs() != true )
106 if ( registerDeviceNubs() != true )
120 bool IOATAStandardController::createDeviceNubs()
123 IOATAStandardDevice
*ataDev
;
125 for (i
= 0; i
< controllerInfo
.maxDevicesSupported
; i
++ )
127 ataDev
= targets
[i
].device
= new IOATAStandardDevice
;
129 if ( ataDev
->init( this, i
) != true )
132 targets
[i
].device
= NULL
;
136 resetCmd
= allocCommand( 0 );
137 resetCmd
->cmdType
= kATACommandBusReset
;
138 resetCmd
->setTimeout( kATAResetTimeoutmS
);
139 resetCmd
->setPointers( 0, 0, false );
149 bool IOATAStandardController::probeDeviceNubs()
152 IOATAStandardDevice
*ataDev
;
154 for (i
= 0; i
< controllerInfo
.maxDevicesSupported
; i
++ )
156 ataDev
= targets
[i
].device
;
157 if ( ataDev
->probeDeviceType() == kATADeviceNone
)
160 targets
[i
].device
= NULL
;
164 for (i
= 0; i
< controllerInfo
.maxDevicesSupported
; i
++ )
166 ataDev
= targets
[i
].device
;
167 if ( ataDev
== NULL
)
172 if ( ataDev
->probeDevice() != true )
175 targets
[i
].device
= NULL
;
188 bool IOATAStandardController::registerDeviceNubs()
191 IOATAStandardDevice
*ataDev
;
193 for (i
= 0; i
< controllerInfo
.maxDevicesSupported
; i
++ )
195 ataDev
= targets
[i
].device
;
196 if ( ataDev
!= NULL
)
198 ataDev
->attach( this );
199 ataDev
->registerService();
211 bool IOATAStandardController::initTimings()
213 ATATiming initPIOTiming
;
215 initPIOTiming
.timingProtocol
= kATATimingPIO
;
216 initPIOTiming
.featureSetting
= 0;
217 initPIOTiming
.mode
= 0;
218 initPIOTiming
.minDataAccess
= 165;
219 initPIOTiming
.minDataCycle
= 600;
220 initPIOTiming
.minCmdAccess
= 290;
221 initPIOTiming
.minCmdCycle
= 600;
223 if ( calculateTiming( 0, &initPIOTiming
) != true )
228 if ( calculateTiming( 1, &initPIOTiming
) != true )
241 bool IOATAStandardController::matchNubWithPropertyTable( IOService
*nub
, OSDictionary
*table
)
245 rc
= nub
->compareProperty( table
, kATAPropertyLocation
);
257 void IOATAStandardController::releaseDevice( IOATAStandardDevice
*device
)
259 workLoopRequest( kWorkLoopReleaseDevice
, (UInt32
) device
);
269 bool IOATAStandardController::workLoopRequest( WorkLoopReqType type
, UInt32 p1
, UInt32 p2
, UInt32 p3
)
271 WorkLoopRequest workLoopReq
;
273 bzero( &workLoopReq
, sizeof(WorkLoopRequest
) );
274 workLoopReq
.type
= type
;
275 workLoopReq
.sync
= IOSyncer::create();
277 workLoopReqGate
->runCommand( &workLoopReq
, (void *)p1
, (void *)p2
, (void *)p3
);
279 workLoopReq
.sync
->wait();
281 return( workLoopReq
.rc
);
290 void IOATAStandardController::workLoopProcessRequest( WorkLoopRequest
*workLoopReq
, void *p1
, void *p2
, void *p3
)
293 IOATAStandardDevice
*device
;
295 switch ( workLoopReq
->type
)
298 case kWorkLoopInitDevice
:
299 device
= (IOATAStandardDevice
*) p1
;
301 rc
= allocateDevice( device
->unit
);
304 case kWorkLoopReleaseDevice
:
305 device
= (IOATAStandardDevice
*) p1
;
306 deleteDevice( device
);
310 workLoopReq
->rc
= rc
;
311 workLoopReq
->sync
->signal();
319 void IOATAStandardController::addDevice( IOATAStandardDevice
*forDevice
)
323 unit
= forDevice
->unit
;
325 forDevice
->target
= &targets
[unit
];
326 targets
[unit
].device
= forDevice
;
334 void IOATAStandardController::deleteDevice( IOATAStandardDevice
*forDevice
)
338 unit
= forDevice
->unit
;
339 targets
[unit
].device
= 0;
348 bool IOATAStandardController::allocateDevice( ATAUnit unit
)
358 void IOATAStandardController::deallocateDevice( ATAUnit unit
)
368 void *IOATAStandardController::getDeviceData( ATAUnit unit
)
370 IOATAStandardDevice
*device
;
372 device
= targets
[unit
].device
;
374 if ( device
== 0 ) return 0;
376 return device
->devicePrivateData
;
385 IOReturn
IOATAStandardController::reset()
387 if ( busResetState
!= kStateIdle
)
389 return kIOReturnNoResources
;
392 busResetState
= kStateIssue
;
395 while ( busResetState
!= kStateIdle
)
400 return resetCmd
->getResults( (ATAResults
*)0 );
408 void IOATAStandardController::resetATABus()
410 if ( busResetState
!= kStateIssue
)
415 busResetState
= kStateActive
;
419 resetCommand( resetCmd
);
427 void IOATAStandardController::resetStarted()
429 IOATAStandardDevice
*device
;
432 for (i
=0; i
< controllerInfo
.maxDevicesSupported
; i
++ )
434 device
= targets
[i
].device
;
436 if ( (device
!= 0) && (device
->client
!= 0) && (device
->abortCmdPending
!= kATACommandDeviceReset
) )
438 device
->client
->message( kATAClientMsgBusReset
, device
);
449 bool IOATAStandardController::checkBusReset()
451 if ( busResetState
== kStateIdle
)
455 if ( busResetState
== kStateIssue
)
467 void IOATAStandardController::timer( IOTimerEventSource
* /* timer */ )
470 IOATAStandardDevice
*device
;
475 if ( !--disableTimer
)
477 disableTimeoutOccurred();
481 for (i
=0; i
< controllerInfo
.maxDevicesSupported
; i
++ )
483 device
= targets
[i
].device
;
490 timerEvent
->setTimeoutMS(kATATimerIntervalmS
);
499 void IOATAStandardController::completeCommand( IOATAStandardCommand
*ataCmd
)
501 switch ( ataCmd
->cmdType
)
503 case kATACommandBusReset
:
504 busResetState
= kStateIdle
;
517 void IOATAStandardController::resetOccurred()
520 IOATAStandardDevice
*device
;
522 for (i
=0; i
< controllerInfo
.maxDevicesSupported
; i
++ )
524 device
= targets
[i
].device
;
526 if ( device
== 0 ) continue;
528 if ( device
->abortCmdPending
!= kATACommandDeviceReset
)
530 device
->resetOccurred( (ATAClientMessage
) (kATAClientMsgBusReset
| kATAClientMsgDone
) );
540 bool IOATAStandardController::createWorkLoop()
542 workLoop
= getWorkLoop();
545 workLoop
= new IOWorkLoop
;
552 if ( workLoop
->init() != true )
557 timerEvent
= IOTimerEventSource::timerEventSource( this, (IOTimerEventSource::Action
) &IOATAStandardController::timer
);
558 if ( timerEvent
== NULL
)
563 if ( workLoop
->addEventSource( timerEvent
) != kIOReturnSuccess
)
571 dispatchEvent
= IOInterruptEventSource::interruptEventSource( this,
572 (IOInterruptEventAction
) &IOATAStandardController::dispatch
,
574 if ( dispatchEvent
== 0 )
579 if ( workLoop
->addEventSource( dispatchEvent
) != kIOReturnSuccess
)
584 workLoopReqGate
= IOCommandGate::commandGate( this, (IOCommandGate::Action
) &IOATAStandardController::workLoopProcessRequest
);
585 if ( workLoopReqGate
== NULL
)
590 if ( workLoop
->addEventSource( workLoopReqGate
) != kIOReturnSuccess
)
604 IOATAStandardCommand
*IOATAStandardController::findCommandWithNexus( IOATAStandardDevice
*device
, UInt32 tagValue
= (UInt32
)-1 )
606 return ((IOATAStandardDevice
*)device
)->findCommandWithNexus( tagValue
);
614 bool IOATAStandardController::configureController()
618 if ( configure( provider
, &controllerInfo
) == false )
623 controllerInfo
.commandPrivateDataSize
= round( controllerInfo
.commandPrivateDataSize
, 16 );
625 targetsSize
= controllerInfo
.maxDevicesSupported
* sizeof(ATATarget
);
626 targets
= (ATATarget
*)IOMalloc( targetsSize
);
627 bzero( targets
, targetsSize
);
629 commandLimit
= commandLimitSave
= (UInt32
)-1;
639 void IOATAStandardController::setCommandLimit( IOATAStandardDevice
*device
, UInt32 newCommandLimit
)
641 ((IOATAStandardDevice
*)device
)->commandLimit
= newCommandLimit
;
650 void IOATAStandardController::disableControllerInterrupts()
652 workLoop
->disableAllInterrupts();
660 void IOATAStandardController::enableControllerInterrupts()
662 workLoop
->enableAllInterrupts();
671 IOWorkLoop
*IOATAStandardController::getWorkLoop() const
681 void IOATAStandardController::disableCommands( UInt32 disableTimeoutmS
)
683 commandDisable
= true;
685 disableTimer
= ( disableTimeoutmS
!= 0 ) ? (disableTimeoutmS
/ kATATimerIntervalmS
+ 1) : 0;
694 void IOATAStandardController::disableCommands()
696 UInt32 disableTimeout
;
698 commandDisable
= true;
700 disableTimeout
= kATADisableTimeoutmS
;
702 if ( noDisconnectCmd
!= 0 )
704 disableTimeout
= noDisconnectCmd
->getTimeout();
705 if ( disableTimeout
!= 0 ) disableTimeout
+= kATADisableTimeoutmS
;
708 disableTimer
= ( disableTimeout
!= 0 ) ? (disableTimeout
/ kATATimerIntervalmS
+ 1) : 0;
716 void IOATAStandardController::disableTimeoutOccurred()
718 busResetState
= kStateIssue
;
728 UInt32
IOATAStandardController::getCommandCount()
738 void IOATAStandardController::suspendDevice( IOATAStandardDevice
*device
)
740 ((IOATAStandardDevice
*)device
)->suspend();
748 void IOATAStandardController::resumeDevice( IOATAStandardDevice
*device
)
750 ((IOATAStandardDevice
*)device
)->resume();
758 IOATAStandardDevice
*IOATAStandardController::selectDevice()
760 IOATAStandardDevice
*ataDev
;
761 IOATAStandardDevice
*selectedDevice
= 0;
762 AbsoluteTime maxSuspendTime
;
765 AbsoluteTime_to_scalar(&maxSuspendTime
) = 0;
767 for (i
= 0; i
< controllerInfo
.maxDevicesSupported
; i
++ )
769 ataDev
= targets
[i
].device
;
770 if ( ataDev
!= NULL
)
772 if ( ataDev
->isSuspended
== true )
774 if ( CMP_ABSOLUTETIME(&ataDev
->suspendTime
, &maxSuspendTime
) > 0 )
776 selectedDevice
= ataDev
;
777 AbsoluteTime_to_scalar( &maxSuspendTime
) = AbsoluteTime_to_scalar( &ataDev
->suspendTime
);
783 return (IOATAStandardDevice
*) selectedDevice
;
792 void IOATAStandardController::rescheduleCommand( IOATAStandardCommand
*forATACmd
)
794 forATACmd
->getDevice(kIOATAStandardDevice
)->rescheduleCommand( forATACmd
);
802 void IOATAStandardController::enableCommands()
804 commandDisable
= false;
816 void IOATAStandardController::dispatchRequest()
818 dispatchEvent
->interruptOccurred(0, 0, 0);
827 void IOATAStandardController::dispatch()
830 IOATAStandardDevice
*device
;
831 UInt32 dispatchAction
;
834 if ( checkBusReset() == true )
839 for ( i
= 0; i
< controllerInfo
.maxDevicesSupported
; i
++ )
841 target
= &targets
[i
];
843 device
= target
->device
;
844 if ( device
== 0 ) continue;
846 if ( target
->state
== kStateActive
)
848 if ( device
->dispatch( &dispatchAction
) == false )
850 target
->state
= kStateIdle
;
853 switch ( dispatchAction
)
855 case kDispatchNextDevice
:
872 IOATAStandardCommand
*IOATAStandardController::allocCommand(UInt32 clientDataSize
)
874 IOATAStandardCommand
*cmd
;
877 size
= controllerInfo
.commandPrivateDataSize
+ round(clientDataSize
, 16);
879 cmd
= new IOATAStandardCommand
;
888 cmd
->dataArea
= (void *)IOMallocContiguous( (vm_size_t
)size
, 16, 0 );
889 if ( !cmd
->dataArea
)
895 bzero( cmd
->dataArea
, size
);
897 cmd
->dataSize
= size
;
899 if ( controllerInfo
.commandPrivateDataSize
)
901 cmd
->commandPrivateData
= cmd
->dataArea
;
903 if ( clientDataSize
)
905 cmd
->clientData
= (void *)((UInt8
*)cmd
->dataArea
+ controllerInfo
.commandPrivateDataSize
);
909 cmd
->controller
= this;
919 void IOATAStandardController::free()
923 if ( timerEvent
!= 0 ) timerEvent
->release();
925 if ( workLoopReqGate
!= 0 ) workLoopReqGate
->release();
927 if ( dispatchEvent
!= 0 ) dispatchEvent
->release();
929 if ( resetCmd
!= 0 ) resetCmd
->release();
931 if ( workLoop
!= 0 ) workLoop
->release();
935 targetsSize
= controllerInfo
.maxDevicesSupported
* sizeof(ATATarget
);
936 IOFree( targets
, targetsSize
);
947 void IOATAStandardCommand::free()
951 IOFreeContiguous( dataArea
, dataSize
);