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 * IOATAHDCommand.cpp - Performs ATA command processing.
28 * Aug 27, 1999 jliu - Ported from AppleATADrive.
31 #include <IOKit/assert.h>
32 #include <IOKit/IOSyncer.h>
33 #include <IOKit/storage/ata/IOATAHDDrive.h>
35 // Enable this define to generate debugging messages.
36 // #define DEBUG_LOG 1
38 //---------------------------------------------------------------------------
39 // Select the device timing protocol.
42 IOATAHDDrive::selectTimingProtocol()
47 ATATimingProtocol timing
;
50 ret
= _ataDevice
->getTimingsSupported(&timing
);
53 IOLog("%s: getTimingsSupported() error\n", getName());
54 timing
= kATATimingPIO
;
57 // IOLog("%s: device supported timings: %08x\n", getName(), timing);
59 if (timing
& (kATATimingUltraDMA66
| kATATimingUltraDMA33
| kATATimingDMA
))
61 if (timing
& kATATimingUltraDMA66
)
63 protocolName
= "U-DMA/66";
64 timing
= kATATimingUltraDMA66
;
66 else if (timing
& kATATimingUltraDMA33
)
68 protocolName
= "U-DMA/33";
69 timing
= kATATimingUltraDMA33
;
74 timing
= kATATimingDMA
;
77 selectCommandProtocol(true);
79 switch ( _ataProtocol
)
81 case kATAProtocolDMAQueued
:
82 ataReadCmd
= kIOATACommandReadDMAQueued
;
83 ataWriteCmd
= kIOATACommandWriteDMAQueued
;
88 ataReadCmd
= kIOATACommandReadDMA
;
89 ataWriteCmd
= kIOATACommandWriteDMA
;
95 timing
= kATATimingPIO
;
96 ataReadCmd
= kIOATACommandReadPIO
;
97 ataWriteCmd
= kIOATACommandWritePIO
;
98 selectCommandProtocol(false);
101 _timingProtocol
= timing
;
102 _ataReadCmd
= ataReadCmd
;
103 _ataWriteCmd
= ataWriteCmd
;
106 // Select device timing.
108 ret
= _ataDevice
->selectTiming( _timingProtocol
, false );
112 IOLog("%s: %s selectTiming() failed\n", getName(), protocolName
);
114 if (_timingProtocol
!= kATATimingPIO
)
116 // Non PIO mode selection failed, defaulting to PIO mode and
117 // try one more time.
119 protocolName
= "PIO";
120 _timingProtocol
= kATATimingPIO
;
121 _ataReadCmd
= kIOATACommandReadPIO
;
122 _ataWriteCmd
= kIOATACommandWritePIO
;
123 selectCommandProtocol(false);
125 ret
= _ataDevice
->selectTiming( _timingProtocol
, false );
127 IOLog("%s: %s selectTiming() retry failed\n",
128 getName(), protocolName
);
132 if (ret
&& _logSelectedTimingProtocol
)
133 IOLog("%s: Using %s transfers\n", getName(), protocolName
);
138 //---------------------------------------------------------------------------
139 // Select the command protocol to use (e.g. ataProtocolPIO, ataProtocolDMA).
142 IOATAHDDrive::selectCommandProtocol(bool isDMA
)
144 ATAProtocol protocolsSupported
;
146 if ( _ataDevice
->getProtocolsSupported( &protocolsSupported
) == false )
148 IOLog("%s: getProtocolsSupported() failed\n", getName());
152 if ( (protocolsSupported
& kATAProtocolDMAQueued
) != 0 )
155 _ataProtocol
= kATAProtocolDMAQueued
;
157 _ataProtocol
= kATAProtocolDMA
;
160 else if ( (protocolsSupported
& kATAProtocolDMA
) != 0 )
162 _ataProtocol
= kATAProtocolDMA
;
166 _ataProtocol
= kATAProtocolPIO
;
172 //---------------------------------------------------------------------------
173 // Configure the ATA/ATAPI device when the driver is initialized, and
174 // after every device reset.
177 IOATAHDDrive::configureDevice(IOATADevice
* device
)
181 // Select device timing.
183 ret
= device
->selectTiming( _timingProtocol
, true );
185 IOLog("%s: selectTiming() failed\n", getName());
192 //---------------------------------------------------------------------------
193 // Setup an ATATaskFile from the parameters given, and write the taskfile
194 // to the ATATaskfile structure pointer provided.
196 // taskfile - pointer to a taskfile structure.
197 // protocol - An ATA transfer protocol (ataProtocolPIO, ataProtocolDMA, etc)
198 // command - ATA command byte.
199 // block - Initial transfer block.
200 // nblks - Number of blocks to transfer.
203 IOATAHDDrive::setupReadWriteTaskFile(ATATaskfile
* taskfile
,
204 ATAProtocol protocol
,
209 bzero( taskfile
, sizeof(ATATaskfile
) );
211 taskfile
->protocol
= protocol
;
213 // Mask of all taskfile registers that shall contain valid
214 // data and should be written to the hardware registers.
216 taskfile
->regmask
= ATARegtoMask(kATARegSectorNumber
) |
217 ATARegtoMask(kATARegCylinderLow
) |
218 ATARegtoMask(kATARegCylinderHigh
) |
219 ATARegtoMask(kATARegDriveHead
) |
220 ATARegtoMask(kATARegSectorCount
) |
221 ATARegtoMask(kATARegFeatures
) |
222 ATARegtoMask(kATARegCommand
);
224 taskfile
->resultmask
= 0;
226 taskfile
->ataRegs
[kATARegSectorNumber
] = block
& 0x0ff;
227 taskfile
->ataRegs
[kATARegCylinderLow
] = (block
>> 8) & 0xff;
228 taskfile
->ataRegs
[kATARegCylinderHigh
] = (block
>> 16) & 0xff;
229 taskfile
->ataRegs
[kATARegDriveHead
] = ((block
>> 24) & 0x0f) |
230 kATAModeLBA
| (_unit
<< 4);
232 if ( protocol
== kATAProtocolDMAQueued
)
234 taskfile
->ataRegs
[kATARegFeatures
] =
235 (nblks
== kIOATAMaxBlocksPerXfer
) ? 0 : nblks
;
236 taskfile
->ataRegs
[kATARegSectorCount
] = 0;
240 taskfile
->ataRegs
[kATARegFeatures
] = 0;
241 taskfile
->ataRegs
[kATARegSectorCount
] =
242 (nblks
== kIOATAMaxBlocksPerXfer
) ? 0 : nblks
;
245 taskfile
->ataRegs
[kATARegCommand
] = command
;
248 //---------------------------------------------------------------------------
249 // Allocate and return an IOATACommand that is initialized to perform
250 // a read/write operation.
252 // buffer - IOMemoryDescriptor object describing this transfer.
253 // block - Initial transfer block.
254 // nblks - Number of blocks to transfer.
257 IOATAHDDrive::ataCommandReadWrite(IOMemoryDescriptor
* buffer
,
261 ATATaskfile taskfile
;
263 IOATACommand
* cmd
= allocateCommand();
267 if (!cmd
) return 0; // error, command allocation failed.
269 isWrite
= (buffer
->getDirection() == kIODirectionOut
) ?
273 IOLog("%s::ataCommandReadWrite %08x (%d) %s %d %d\n",
277 isWrite
? "WR" : "RD",
282 #if 0 // used for testing - force PIO mode
283 setupReadWriteTaskFile(&taskfile
,
285 isWrite
? kIOATACommandWritePIO
:
286 kIOATACommandReadPIO
,
291 // Setup the taskfile structure with the size and direction of the
292 // transfer. This structure will be written to the actual taskfile
293 // registers when this command is processed.
295 setupReadWriteTaskFile(&taskfile
,
297 isWrite
? _ataWriteCmd
: _ataReadCmd
,
302 // Get a pointer to the client data buffer, and record parameters
303 // which shall be later used by the completion routine.
305 ATA_CLIENT_DATA(cmd
)->buffer
= buffer
;
307 cmd
->setTaskfile(&taskfile
);
309 cmd
->setPointers(buffer
, /* (IOMemoryDescriptor *) */
310 buffer
->getLength(), /* transferCount (bytes) */
311 isWrite
); /* isWrite */
316 //---------------------------------------------------------------------------
317 // Allocate and return a ATA SetFeatures command.
320 IOATAHDDrive::ataCommandSetFeatures(UInt8 features
,
326 ATATaskfile taskfile
;
327 IOATACommand
* cmd
= allocateCommand();
329 if (!cmd
) return 0; // error, command allocation failed.
331 taskfile
.protocol
= kATAProtocolPIO
;
333 taskfile
.regmask
= ATARegtoMask(kATARegSectorNumber
) |
334 ATARegtoMask(kATARegCylinderLow
) |
335 ATARegtoMask(kATARegCylinderHigh
) |
336 ATARegtoMask(kATARegDriveHead
) |
337 ATARegtoMask(kATARegSectorCount
) |
338 ATARegtoMask(kATARegCommand
);
340 taskfile
.resultmask
= ATARegtoMask(kATARegError
) |
341 ATARegtoMask(kATARegStatus
);
343 taskfile
.ataRegs
[kATARegFeatures
] = features
;
344 taskfile
.ataRegs
[kATARegSectorNumber
] = SectorNumber
;
345 taskfile
.ataRegs
[kATARegCylinderLow
] = CylinderLow
;
346 taskfile
.ataRegs
[kATARegCylinderHigh
] = CyclinderHigh
;
347 taskfile
.ataRegs
[kATARegDriveHead
] = kATAModeLBA
| (_unit
<< 4);
348 taskfile
.ataRegs
[kATARegSectorCount
] = SectorCount
;
349 taskfile
.ataRegs
[kATARegCommand
] = kIOATACommandSetFeatures
;
351 cmd
->setTaskfile(&taskfile
);
353 // This is a way to issue a command which will wait
354 // for an interrupt, but does no data transfer.
356 cmd
->setPointers(0, 0, false);
361 //---------------------------------------------------------------------------
362 // Return a Flush Cache command.
365 IOATAHDDrive::ataCommandFlushCache()
367 ATATaskfile taskfile
;
368 IOATACommand
* cmd
= allocateCommand();
370 if (!cmd
) return 0; // error, command allocation failed.
372 // kATAProtocolSetRegs does not wait for an interrupt from the drive.
374 taskfile
.protocol
= kATAProtocolPIO
;
376 taskfile
.regmask
= ATARegtoMask(kATARegDriveHead
) |
377 ATARegtoMask(kATARegCommand
);
379 taskfile
.resultmask
= ATARegtoMask(kATARegError
) |
380 ATARegtoMask(kATARegSectorNumber
) |
381 ATARegtoMask(kATARegCylinderLow
) |
382 ATARegtoMask(kATARegCylinderHigh
) |
383 ATARegtoMask(kATARegDriveHead
) |
384 ATARegtoMask(kATARegStatus
);
386 taskfile
.ataRegs
[kATARegDriveHead
] = kATAModeLBA
| (_unit
<< 4);
387 taskfile
.ataRegs
[kATARegCommand
] = kIOATACommandFlushCache
;
389 cmd
->setTaskfile(&taskfile
);
391 // This is a way to issue a command which will wait
392 // for an interrupt, but does no data transfer.
394 cmd
->setPointers(0, 0, false);
399 //---------------------------------------------------------------------------
400 // Return a STANDBY IMMEDIATE command.
403 IOATAHDDrive::ataCommandStandby()
405 ATATaskfile taskfile
;
406 IOATACommand
* cmd
= allocateCommand();
408 if (!cmd
) return 0; // error, command allocation failed.
410 // kATAProtocolSetRegs does not wait for an interrupt from the drive.
412 taskfile
.protocol
= kATAProtocolPIO
;
414 taskfile
.regmask
= ATARegtoMask(kATARegDriveHead
) |
415 ATARegtoMask(kATARegCommand
);
417 taskfile
.resultmask
= ATARegtoMask(kATARegError
) |
418 ATARegtoMask(kATARegStatus
);
420 taskfile
.ataRegs
[kATARegDriveHead
] = kATAModeLBA
| (_unit
<< 4);
421 taskfile
.ataRegs
[kATARegCommand
] = kIOATACommandStandbyImmediate
;
423 cmd
->setTaskfile(&taskfile
);
425 // This is a way to issue a command which will wait
426 // for an interrupt, but does no data transfer.
428 cmd
->setPointers(0, 0, false);
433 //---------------------------------------------------------------------------
434 // This routine is called by our provider when a command processing has
438 IOATAHDDrive::sHandleCommandCompletion(IOATAHDDrive
* self
,
442 IOATADevice
* device
;
443 IOATAClientData
* clientData
;
446 device
= cmd
->getDevice(kIOATADevice
);
449 clientData
= ATA_CLIENT_DATA(cmd
);
452 if ((cmd
->getResults(&results
) != kIOReturnSuccess
) &&
453 (clientData
->maxRetries
-- > 0))
460 // Force command retry to test retry logic.
461 // Controller will reset the IOMemoryDescriptor's position, right?
463 cmd
->getResults(&results
);
464 if (clientData
->maxRetries
-- > 2) {
471 IOLog("%s: sHandleCommandCompletion %08x %08x %08x %08x %d\n",
472 getName(), device
, cmd
, refcon
, results
.returnCode
,
473 results
.bytesTransferred
);
476 // Return IOReturn for sync commands.
478 clientData
->returnCode
= results
.returnCode
;
480 if (clientData
->isSync
) {
481 // For sync commands, unblock the client thread.
483 assert(clientData
->completion
.syncLock
);
484 clientData
->completion
.syncLock
->signal(); // unblock the client.
487 // Signal the completion routine that the request has been completed.
490 IOStorage::complete(clientData
->completion
.async
,
492 (UInt64
) results
.bytesTransferred
);
495 // Release the IOMemoryDescriptor.
497 if (clientData
->buffer
)
498 clientData
->buffer
->release();
500 // Command processing is complete, release the command object.
505 //---------------------------------------------------------------------------
506 // Issue a synchronous ATA command.
509 IOATAHDDrive::syncExecute(IOATACommand
* cmd
, /* command object */
510 UInt32 timeout
, /* timeout in ms */
511 UInt retries
, /* max retries */
512 IOMemoryDescriptor
* senseData
)
514 IOATAClientData
* clientData
= ATA_CLIENT_DATA(cmd
);
518 activityTickle( kIOPMSuperclassPolicy1
, 1 );
521 // Bump the retain count on the command. The completion handler
522 // will decrement the retain count.
526 // Set timeout and register the completion handler.
528 cmd
->setPointers(senseData
,
529 senseData
? senseData
->getLength() : 0,
531 true ); /* isSense */
532 cmd
->setTimeout(timeout
);
533 cmd
->setCallback(this,
534 (CallbackFn
) &IOATAHDDrive::sHandleCommandCompletion
,
537 // Increment the retain count on the IOMemoryDescriptor.
538 // Release when the completion routine gets called.
540 if (clientData
->buffer
)
541 clientData
->buffer
->retain();
543 // Set the max retry count. If retry count is 0, then the command shall
544 // not be retried if an error occurs.
546 clientData
->maxRetries
= retries
;
547 clientData
->completion
.syncLock
= IOSyncer::create();
548 clientData
->isSync
= true;
552 // Block client thread on lock until the completion handler
553 // receives an indication that the processing is complete.
555 clientData
->completion
.syncLock
->wait();
557 return clientData
->returnCode
;
560 //---------------------------------------------------------------------------
561 // Issue an asynchronous ATA command.
564 IOATAHDDrive::asyncExecute(IOATACommand
* cmd
, /* command object */
565 IOStorageCompletion completion
,
566 UInt32 timeout
, /* timeout in ms */
567 UInt retries
) /* max retries */
569 IOATAClientData
* clientData
= ATA_CLIENT_DATA(cmd
);
573 activityTickle( kIOPMSuperclassPolicy1
, 1 );
576 // Bump the retain count on the command. The completion handler
577 // will decrement the retain count.
581 // Set timeout and register the completion handler.
583 cmd
->setTimeout(timeout
);
584 cmd
->setCallback(this,
585 (CallbackFn
) &IOATAHDDrive::sHandleCommandCompletion
,
588 // Increment the retain count on the IOMemoryDescriptor.
589 // Release when the completion routine gets called.
591 if (clientData
->buffer
)
592 clientData
->buffer
->retain();
594 // Set the max retry count. If retry count is 0, then the command shall
595 // not be retried if an error occurs.
597 clientData
->maxRetries
= retries
;
598 clientData
->isSync
= false;
600 clientData
->completion
.async
= completion
;
602 return (cmd
->execute() ? kIOReturnSuccess
: kIOReturnNoResources
);
605 //---------------------------------------------------------------------------
606 // Allocate an IOATACommand object with a fixed client data area.
609 IOATAHDDrive::allocateCommand()
611 return _ataDevice
->allocCommand(kIOATADevice
, sizeof(IOATAClientData
));