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.
25 * IOATAHDDrive.cpp - Generic ATA disk driver.
28 * Aug 27, 1999 jliu - Ported from AppleATADrive.
31 #include <IOKit/assert.h>
32 #include <IOKit/storage/ata/IOATAHDDrive.h>
33 #include <IOKit/storage/ata/IOATAHDDriveNub.h>
35 #define super IOService
36 OSDefineMetaClassAndStructors( IOATAHDDrive
, IOService
)
38 //---------------------------------------------------------------------------
42 IOATAHDDrive::sHandleConfigureDevice(IOATAHDDrive
* self
)
44 self
->configureDevice(self
->_ataDevice
);
47 //---------------------------------------------------------------------------
51 IOATAHDDrive::init(OSDictionary
* properties
)
53 return (super::init(properties
));
56 //---------------------------------------------------------------------------
57 // Override probe() method inherited from IOService.
60 IOATAHDDrive::probe(IOService
* provider
, SInt32
* score
)
62 if (!super::probe(provider
, score
))
65 // Our provider must be a IOATADevice nub, most likely created
66 // by an IOATAController instance.
68 IOATADevice
* device
= OSDynamicCast(IOATADevice
, provider
);
70 return 0; // Provider is not an IOATADevice.
72 // Do ATA device type matching. Does the nub match my device type?
74 if (device
->getDeviceType() != reportATADeviceType())
75 return 0; // error, type mismatch (probably ATAPI).
77 // Cache the drive unit number (master/slave assignment).
79 _unit
= device
->getUnit();
81 return this; // probe successful.
84 //---------------------------------------------------------------------------
85 // Starts up the driver and spawn a nub.
88 IOATAHDDrive::start(IOService
* provider
)
90 // First call start() in our superclass.
92 if (super::start(provider
) == false)
95 _configThreadCall
= (void *) thread_call_allocate(
96 (thread_call_func_t
) sHandleConfigureDevice
,
97 (thread_call_param_t
) this);
98 if (!_configThreadCall
)
101 // Cache our provider.
103 _ataDevice
= OSDynamicCast(IOATADevice
, provider
);
107 // Open our provider.
109 _ataDevice
->retain();
110 if (_ataDevice
->open(this) == false)
113 // Inspect the provider.
115 if (inspectDevice(_ataDevice
) == false)
118 // Select ATA timing.
120 _logSelectedTimingProtocol
= true;
122 if (selectTimingProtocol() == false)
125 // Create an IOCommandGate (for power management support) and attach
126 // this event source to the provider's workloop.
128 _cmdGate
= IOCommandGate::commandGate(this);
132 IOWorkLoop
* workloop
= _ataDevice
->getWorkLoop();
133 if ((workloop
== 0) ||
134 (workloop
->addEventSource(_cmdGate
) != kIOReturnSuccess
))
137 // Starts up in the active state.
139 _currentATAPowerState
= kIOATAPowerStateActive
;
141 // A policy-maker must make these calls to join the PM tree,
142 // and to initialize its state.
144 PMinit(); /* initialize power management variables */
145 provider
->joinPMtree(this); /* join power management tree */
146 setIdleTimerPeriod(300); /* 300 sec inactivity timer */
148 if (_supportedFeatures
& kIOATAFeaturePowerManagement
)
151 return (createNub(provider
));
154 //---------------------------------------------------------------------------
158 IOATAHDDrive::stop(IOService
* provider
)
162 super::stop(provider
);
165 //---------------------------------------------------------------------------
166 // Release allocated resources.
171 if (_configThreadCall
) {
172 thread_call_cancel((thread_call_t
) _configThreadCall
);
173 thread_call_free((thread_call_t
) _configThreadCall
);
177 if (_ataDevice
&& (_ataDevice
->getWorkLoop()))
178 _ataDevice
->getWorkLoop()->removeEventSource(_cmdGate
);
183 _ataDevice
->release();
188 //---------------------------------------------------------------------------
189 // Fetch information about the ATA device nub.
192 IOATAHDDrive::inspectDevice(IOATADevice
* ataDevice
)
195 ATAIdentify
* identify
;
197 // Fetch ATA device information from the nub.
199 string
= OSDynamicCast(OSString
,
200 ataDevice
->getProperty(kATAPropertyModelNumber
));
202 strncpy(_model
, string
->getCStringNoCopy(), 40);
206 string
= OSDynamicCast(OSString
,
207 ataDevice
->getProperty(kATAPropertyFirmwareRev
));
209 strncpy(_revision
, string
->getCStringNoCopy(), 8);
213 // Fetch Word 82 (commandSetsSupported1) in Identify data.
215 identify
= (ATAIdentify
*) IOMalloc(sizeof(*identify
));
219 ataDevice
->getIdentifyData(identify
);
221 if (identify
->commandSetsSupported1
& 0x8)
222 _supportedFeatures
|= kIOATAFeaturePowerManagement
;
224 if (identify
->commandSetsSupported1
& 0x20)
225 _supportedFeatures
|= kIOATAFeatureWriteCache
;
227 IOFree(identify
, sizeof(*identify
));
229 // Add an OSNumber property indicating the supported features.
231 setProperty(kIOATASupportedFeaturesKey
,
233 sizeof(_supportedFeatures
) * 8);
238 //---------------------------------------------------------------------------
239 // Report the type of ATA device (ATA vs. ATAPI).
242 IOATAHDDrive::reportATADeviceType() const
244 return kATADeviceATA
;
247 //---------------------------------------------------------------------------
248 // Returns the device type.
251 IOATAHDDrive::getDeviceTypeName()
253 return kIOBlockStorageDeviceTypeGeneric
;
256 //---------------------------------------------------------------------------
257 // Instantiate an ATA specific subclass of IOBlockStorageDevice.
259 IOService
* IOATAHDDrive::instantiateNub()
261 IOService
* nub
= new IOATAHDDriveNub
;
265 //---------------------------------------------------------------------------
266 // Returns an IOATAHDDriveNub.
268 bool IOATAHDDrive::createNub(IOService
* provider
)
272 // Instantiate a generic hard disk nub so a generic driver
273 // can match above us.
275 nub
= instantiateNub();
278 IOLog("%s: instantiateNub() failed\n", getName());
284 if (!nub
->attach(this))
285 IOPanic("IOATAHDDrive::createNub() unable to attach nub");
287 nub
->registerService();
292 //---------------------------------------------------------------------------
293 // Handles read/write requests.
295 IOReturn
IOATAHDDrive::doAsyncReadWrite(IOMemoryDescriptor
* buffer
,
298 IOStorageCompletion completion
)
301 IOATACommand
* cmd
= ataCommandReadWrite(buffer
, block
, nblks
);
304 return kIOReturnNoMemory
;
306 ret
= asyncExecute(cmd
, completion
);
313 IOReturn
IOATAHDDrive::doSyncReadWrite(IOMemoryDescriptor
* buffer
,
318 IOATACommand
* cmd
= ataCommandReadWrite(buffer
, block
, nblks
);
321 return kIOReturnNoMemory
;
323 ret
= syncExecute(cmd
);
330 //---------------------------------------------------------------------------
331 // Eject the media in the drive.
333 IOReturn
IOATAHDDrive::doEjectMedia()
335 return kIOReturnUnsupported
; // No support for removable ATA devices.
338 //---------------------------------------------------------------------------
339 // Format the media in the drive.
340 // ATA devices does not support low level formatting.
342 IOReturn
IOATAHDDrive::doFormatMedia(UInt64 byteCapacity
)
344 return kIOReturnUnsupported
;
347 //---------------------------------------------------------------------------
348 // Returns disk capacity.
350 UInt32
IOATAHDDrive::doGetFormatCapacities(UInt64
* capacities
,
351 UInt32 capacitiesMaxCount
) const
353 UInt32 blockCount
= 0;
354 UInt32 blockSize
= 0;
358 if (_ataDevice
->getDeviceCapacity(&blockCount
, &blockSize
) &&
359 (capacities
!= NULL
) && (capacitiesMaxCount
> 0))
361 UInt64 count
= blockCount
;
362 UInt64 size
= blockSize
;
364 *capacities
= size
* (count
+ 1);
372 //---------------------------------------------------------------------------
373 // Lock the media and prevent a user-initiated eject.
375 IOReturn
IOATAHDDrive::doLockUnlockMedia(bool doLock
)
377 return kIOReturnUnsupported
; // No removable ATA device support.
380 //---------------------------------------------------------------------------
381 // Flush the write-cache to the physical media.
383 IOReturn
IOATAHDDrive::doSynchronizeCache()
386 IOATACommand
* cmd
= ataCommandFlushCache();
389 return kIOReturnNoMemory
;
391 ret
= syncExecute(cmd
, 60000);
398 //---------------------------------------------------------------------------
399 // Handle a Start Unit command.
402 IOATAHDDrive::doStart()
404 return kIOReturnSuccess
;
407 //---------------------------------------------------------------------------
408 // Handle a Stop Unit command.
411 IOATAHDDrive::doStop()
413 return kIOReturnSuccess
;
416 //---------------------------------------------------------------------------
417 // Return device identification strings.
419 char * IOATAHDDrive::getAdditionalDeviceInfoString()
424 char * IOATAHDDrive::getProductString()
429 char * IOATAHDDrive::getRevisionString()
434 char * IOATAHDDrive::getVendorString()
439 //---------------------------------------------------------------------------
440 // Report the device block size in bytes. We ask the device nub for the
441 // block size. We expect this to be 512-bytes.
443 IOReturn
IOATAHDDrive::reportBlockSize(UInt64
* blockSize
)
450 if (!_ataDevice
->getDeviceCapacity(&blkCount
, &blkSize
))
451 return kIOReturnNoDevice
;
453 *blockSize
= blkSize
;
454 return kIOReturnSuccess
;
457 //---------------------------------------------------------------------------
458 // Report the media in the ATA device as non-ejectable.
460 IOReturn
IOATAHDDrive::reportEjectability(bool * isEjectable
)
462 *isEjectable
= false;
463 return kIOReturnSuccess
;
466 //---------------------------------------------------------------------------
467 // Fixed media, locking is invalid.
469 IOReturn
IOATAHDDrive::reportLockability(bool * isLockable
)
472 return kIOReturnSuccess
;
475 //---------------------------------------------------------------------------
476 // Report the polling requirements for a removable media.
478 IOReturn
IOATAHDDrive::reportPollRequirements(bool * pollRequired
,
479 bool * pollIsExpensive
)
481 *pollIsExpensive
= false;
482 *pollRequired
= false;
484 return kIOReturnSuccess
;
487 //---------------------------------------------------------------------------
488 // Report the max number of bytes transferred for an ATA read command.
490 IOReturn
IOATAHDDrive::reportMaxReadTransfer(UInt64 blocksize
, UInt64
* max
)
492 *max
= blocksize
* kIOATAMaxBlocksPerXfer
;
493 return kIOReturnSuccess
;
496 //---------------------------------------------------------------------------
497 // Report the max number of bytes transferred for an ATA write command.
499 IOReturn
IOATAHDDrive::reportMaxWriteTransfer(UInt64 blocksize
, UInt64
* max
)
501 // Same as read transfer limits.
503 return reportMaxReadTransfer(blocksize
, max
);
506 //---------------------------------------------------------------------------
507 // Returns the maximum addressable sector number.
509 IOReturn
IOATAHDDrive::reportMaxValidBlock(UInt64
* maxBlock
)
511 UInt32 blockCount
= 0;
512 UInt32 blockSize
= 0;
514 assert(_ataDevice
&& maxBlock
);
516 if (!_ataDevice
->getDeviceCapacity(&blockCount
, &blockSize
))
517 return kIOReturnNoDevice
;
519 *maxBlock
= blockCount
;
521 return kIOReturnSuccess
;
524 //---------------------------------------------------------------------------
525 // Report whether the media is currently present, and whether a media
526 // change has been registered since the last reporting.
528 IOReturn
IOATAHDDrive::reportMediaState(bool * mediaPresent
, bool * changed
)
530 *mediaPresent
= true;
533 return kIOReturnSuccess
;
536 //---------------------------------------------------------------------------
537 // Report whether the media is removable.
539 IOReturn
IOATAHDDrive::reportRemovability(bool * isRemovable
)
541 *isRemovable
= false;
542 return kIOReturnSuccess
;
545 //---------------------------------------------------------------------------
546 // Report if the media is write-protected.
548 IOReturn
IOATAHDDrive::reportWriteProtection(bool * isWriteProtected
)
550 *isWriteProtected
= false;
551 return kIOReturnSuccess
;
554 //---------------------------------------------------------------------------
555 // Handles messages from our provider.
558 IOATAHDDrive::message(UInt32 type
, IOService
* provider
, void * argument
)
560 IOReturn ret
= kIOReturnSuccess
;
562 // IOLog("IOATAHDDrive::message %p %lx\n", this, type);
566 case kATAClientMsgBusReset
:
567 _ataDevice
->holdQueue(kATAQTypeNormalQ
);
570 case kATAClientMsgBusReset
| kATAClientMsgDone
:
571 configureDevice( _ataDevice
);
574 case kATAClientMsgSelectTiming
| kATAClientMsgDone
:
575 _ataDevice
->releaseQueue(kATAQTypeNormalQ
);
579 ret
= super::message(type
, provider
, argument
);