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 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
28 * Aug 27, 1999 jliu - Ported from AppleATADrive.
31 #ifndef _IOATAHDDRIVE_H
32 #define _IOATAHDDRIVE_H
34 #include <IOKit/IOTypes.h>
35 #include <IOKit/ata/IOATADeviceInterface.h>
36 #include <IOKit/IOCommandGate.h>
37 #include <IOKit/IOWorkLoop.h>
38 #include <IOKit/storage/IOStorage.h>
44 #define kIOATASectorSize 512
45 #define kIOATAMaxBlocksPerXfer 256
50 kIOATACommandReadPIO
= 0x20,
51 kIOATACommandWritePIO
= 0x30,
52 kIOATACommandReadDMA
= 0xc8,
53 kIOATACommandWriteDMA
= 0xca,
54 kIOATACommandReadDMAQueued
= 0xc7,
55 kIOATACommandWriteDMAQueued
= 0xcc,
56 kIOATACommandStandbyImmediate
= 0xe0,
57 kIOATACommandSleep
= 0xe6,
58 kIOATACommandFlushCache
= 0xe7,
59 kIOATACommandSetFeatures
= 0xef,
62 // ATA power states, from lowest to highest power usage.
65 kIOATAPowerStateSleep
= 0,
66 kIOATAPowerStateStandby
,
68 kIOATAPowerStateActive
71 // ATA supported features.
74 kIOATAFeaturePowerManagement
= 0x01,
75 kIOATAFeatureWriteCache
= 0x02
78 // Stages to transition into each power state.
81 kIOATAStandbyStage0
, /* hold the queue */
82 kIOATAStandbyStage1
, /* flush disk write cache */
83 kIOATAStandbyStage2
, /* issue ATA STANDBY IMMEDIATE command */
84 kIOATAStandbyStage3
/* finalize state transition */
88 kIOATAActiveStage0
, /* issue a software reset */
89 kIOATAActiveStage1
, /* spin up the drive */
90 kIOATAActiveStage2
, /* release the queue */
91 kIOATAActiveStage3
/* finalize state transition */
94 // Property table keys.
96 #define kIOATASupportedFeaturesKey "ATA Features"
97 #define kIOATAEnableWriteCacheKey "Enable Write Cache"
99 //===========================================================================
100 // IOATAClientData - This structure is stored on the IOATACommand's
101 // driver private area.
102 //===========================================================================
104 struct IOATAClientData
106 IOATACommand
* command
; // back pointer to command object.
107 IOMemoryDescriptor
* buffer
; // transfer buffer descriptor.
109 IOStorageCompletion async
; // completion target/action/param.
110 IOSyncer
* syncLock
; // used by sync commands.
112 bool isSync
; // command is synchronous.
113 SInt32 maxRetries
; // max retry attempts (0 = no retry).
114 IOReturn returnCode
; // sync command return code.
117 // Get driver private data (IOATAClientData) from an IOATACommand object.
119 #define ATA_CLIENT_DATA(x) ((IOATAClientData *)((x)->getClientData()))
121 //===========================================================================
123 //===========================================================================
125 class IOATAHDDrive
: public IOService
127 OSDeclareDefaultStructors(IOATAHDDrive
)
130 IOATADevice
* _ataDevice
;
131 IOCommandGate
* _cmdGate
;
133 ATATimingProtocol _timingProtocol
;
134 ATAProtocol _ataProtocol
;
139 bool _powerStateChanging
;
140 bool _setPowerAckPending
;
141 bool _logSelectedTimingProtocol
;
142 IOOptionBits _supportedFeatures
;
143 IOATAPowerState _currentATAPowerState
;
144 IOATAPowerState _proposedATAPowerState
;
145 void * _configThreadCall
;
148 //-----------------------------------------------------------------------
149 // Default timeout (in milliseconds) for async and sync commands.
151 static const UInt kATADefaultTimeout
= 30000; // 30 seconds
153 //-----------------------------------------------------------------------
154 // Default retry count for async and sync commands.
156 static const UInt kATADefaultRetries
= 4;
157 static const UInt kATAZeroRetry
= 0;
159 //-----------------------------------------------------------------------
160 // Static member functions called by IOCommandGate, or registered
161 // as completion routines.
163 static void sHandleCommandCompletion(IOATAHDDrive
* self
,
166 static void sHandleSetPowerState(IOATAHDDrive
* self
,
167 UInt32 powerStateOrdinal
,
168 IOService
* whatDevice
,
169 IOReturn
* handlerReturn
);
171 static void sHandleSleepStateTransition(IOATAHDDrive
* self
,
174 UInt64 bytesTransferred
);
176 static void sHandleActiveStateTransition(IOATAHDDrive
* self
,
179 UInt64 bytesTransferred
);
181 static void sHandleIdleStateTransition(IOATAHDDrive
* self
,
184 UInt64 bytesTransferred
);
186 static void sHandleStandbyStateTransition(IOATAHDDrive
* self
,
189 UInt64 bytesTransferred
);
191 static void sHandleInitialPowerStateForDomainState(
193 IOPMPowerFlags domainState
,
196 static void sHandleConfigureDevice(IOATAHDDrive
* self
);
198 //-----------------------------------------------------------------------
199 // Release all allocated resource before calling super::free().
203 //-----------------------------------------------------------------------
204 // Select the device timing protocol.
206 virtual bool selectTimingProtocol();
208 //-----------------------------------------------------------------------
209 // Select the ATA protocol.
211 virtual bool selectCommandProtocol(bool isDMA
);
213 //-----------------------------------------------------------------------
214 // Setup an ATATaskFile from the parameters given, and write the taskfile
215 // to the ATATaskfile structure pointer provided.
217 virtual void setupReadWriteTaskFile(ATATaskfile
* taskfile
,
218 ATAProtocol protocol
,
223 //-----------------------------------------------------------------------
224 // Return an IOATACommand initialized to perform a read/write operation.
226 virtual IOATACommand
* ataCommandReadWrite(IOMemoryDescriptor
* buffer
,
230 //-----------------------------------------------------------------------
231 // Return a ATA Set Features command.
233 virtual IOATACommand
* ataCommandSetFeatures(UInt8 features
,
234 UInt8 SectorCount
= 0,
235 UInt8 SectorNumber
= 0,
236 UInt8 CylinderLow
= 0,
237 UInt8 CyclinderHigh
= 0);
239 //-----------------------------------------------------------------------
240 // Return a ATA Flush Cache command.
242 virtual IOATACommand
* ataCommandFlushCache();
244 //-----------------------------------------------------------------------
245 // Return a ATA Standby Immediate command.
247 virtual IOATACommand
* ataCommandStandby();
249 //-----------------------------------------------------------------------
250 // Issue a synchronous ATA command.
252 virtual IOReturn
syncExecute(IOATACommand
* cmd
,
253 UInt32 timeout
= kATADefaultTimeout
,
254 UInt retries
= kATADefaultRetries
,
255 IOMemoryDescriptor
* senseData
= 0);
257 //-----------------------------------------------------------------------
258 // Issue an asynchronous ATA command.
260 virtual IOReturn
asyncExecute(
262 IOStorageCompletion completion
,
263 UInt32 timeout
= kATADefaultTimeout
,
264 UInt retries
= kATADefaultRetries
);
266 //-----------------------------------------------------------------------
267 // Allocate an IOATACommand object.
269 virtual IOATACommand
* allocateCommand();
271 //-----------------------------------------------------------------------
272 // Inspect the ATA device.
274 virtual bool inspectDevice(IOATADevice
* device
);
276 //-----------------------------------------------------------------------
277 // Configure the ATA device.
279 virtual bool configureDevice(IOATADevice
* device
);
281 //-----------------------------------------------------------------------
282 // Returns an IOATAHDDriveNub instance.
284 virtual IOService
* instantiateNub();
286 //-----------------------------------------------------------------------
287 // Calls instantiateNub() then initialize, attach, and register the
290 virtual bool createNub(IOService
* provider
);
292 //-----------------------------------------------------------------------
293 // Power management support. Subclasses can override these functions
294 // to replace/enhance the default power management support.
296 virtual void initForPM();
298 virtual UInt32
handleInitialPowerStateForDomainState(
299 IOPMPowerFlags domainState
);
301 virtual IOReturn
handleSetPowerState(UInt32 powerStateOrdinal
,
302 IOService
* whatDevice
);
304 virtual IOATAPowerState
getATAPowerStateForStateOrdinal(
305 UInt32 stateOrdinal
);
307 virtual void startATAPowerStateTransition(IOATAPowerState ataPowerState
);
309 virtual void endATAPowerStateTransition(IOATAPowerState ataPowerState
);
311 virtual void abortATAPowerStateTransition();
313 virtual void handleSleepStateTransition(UInt32 stage
, IOReturn status
);
315 virtual void handleActiveStateTransition(UInt32 stage
, IOReturn status
);
317 virtual void handleIdleStateTransition(UInt32 stage
, IOReturn status
);
319 virtual void handleStandbyStateTransition( UInt32 stage
, IOReturn status
);
321 virtual IOReturn
readSector(IOStorageCompletion completion
,
324 static void acknowledgeATAPowerStateTransition(void *castMeToIOATAHDDrive
, void*);
328 * Overrides from IOService.
330 virtual bool init(OSDictionary
* properties
);
331 virtual IOService
* probe(IOService
* provider
, SInt32
* score
);
332 virtual bool start(IOService
* provider
);
333 virtual void stop(IOService
* provider
);
335 //-----------------------------------------------------------------------
336 // Report the type of ATA device (ATA vs. ATAPI).
338 virtual ATADeviceType
reportATADeviceType() const;
340 //-----------------------------------------------------------------------
341 // Handles read/write requests.
343 virtual IOReturn
doAsyncReadWrite(IOMemoryDescriptor
* buffer
,
346 IOStorageCompletion completion
);
348 virtual IOReturn
doSyncReadWrite(IOMemoryDescriptor
* buffer
,
352 //-----------------------------------------------------------------------
353 // Eject the media in the drive.
355 virtual IOReturn
doEjectMedia();
357 //-----------------------------------------------------------------------
358 // Format the media in the drive.
360 virtual IOReturn
doFormatMedia(UInt64 byteCapacity
);
362 //-----------------------------------------------------------------------
363 // Returns disk capacity in bytes.
365 virtual UInt32
doGetFormatCapacities(UInt64
* capacities
,
366 UInt32 capacitiesMaxCount
) const;
368 //-----------------------------------------------------------------------
369 // Lock the media and prevent a user-initiated eject.
371 virtual IOReturn
doLockUnlockMedia(bool doLock
);
373 //-----------------------------------------------------------------------
374 // Flush the write-cache to the physical media.
376 virtual IOReturn
doSynchronizeCache();
378 //-----------------------------------------------------------------------
379 // Start/stop the drive.
381 virtual IOReturn
doStart();
382 virtual IOReturn
doStop();
384 //-----------------------------------------------------------------------
385 // Return device identification strings
387 virtual char * getAdditionalDeviceInfoString();
388 virtual char * getProductString();
389 virtual char * getRevisionString();
390 virtual char * getVendorString();
392 //-----------------------------------------------------------------------
393 // Report the device block size in bytes.
395 virtual IOReturn
reportBlockSize(UInt64
* blockSize
);
397 //-----------------------------------------------------------------------
398 // Report whether the media in the ATA device is ejectable.
400 virtual IOReturn
reportEjectability(bool * isEjectable
);
402 //-----------------------------------------------------------------------
403 // Report whether the media can be locked.
405 virtual IOReturn
reportLockability(bool * isLockable
);
407 //-----------------------------------------------------------------------
408 // Report the polling requirements for a removable media.
410 virtual IOReturn
reportPollRequirements(bool * pollRequired
,
411 bool * pollIsExpensive
);
413 //-----------------------------------------------------------------------
414 // Report the max number of bytes transferred for an ATA read command.
416 virtual IOReturn
reportMaxReadTransfer(UInt64 blocksize
,
419 //-----------------------------------------------------------------------
420 // Report the max number of bytes transferred for an ATA write command.
422 virtual IOReturn
reportMaxWriteTransfer(UInt64 blocksize
,
425 //-----------------------------------------------------------------------
426 // Returns the maximum addressable sector number.
428 virtual IOReturn
reportMaxValidBlock(UInt64
* maxBlock
);
430 //-----------------------------------------------------------------------
431 // Report whether the media is currently present, and whether a media
432 // change has been registered since the last reporting.
434 virtual IOReturn
reportMediaState(bool * mediaPresent
,
437 //-----------------------------------------------------------------------
438 // Report whether the media is removable.
440 virtual IOReturn
reportRemovability(bool * isRemovable
);
442 //-----------------------------------------------------------------------
443 // Report if the media is write-protected.
445 virtual IOReturn
reportWriteProtection(bool * isWriteProtected
);
447 //-----------------------------------------------------------------------
448 // Handles messages (notifications) from our provider.
450 virtual IOReturn
message(UInt32 type
,
451 IOService
* provider
,
454 //-----------------------------------------------------------------------
455 // Returns the device type.
457 virtual const char * getDeviceTypeName();
459 //-----------------------------------------------------------------------
460 // Power management support. Functions inherited from IOService.
462 virtual IOReturn
setAggressiveness(UInt32 type
, UInt32 minutes
);
464 virtual UInt32
initialPowerStateForDomainState(IOPMPowerFlags domainState
);
466 virtual IOReturn
setPowerState(UInt32 powerStateOrdinal
,
467 IOService
* whatDevice
);
470 #endif /* !_IOATAHDDRIVE_H */