-
Notifications
You must be signed in to change notification settings - Fork 1
/
IOATABlockStorageCommands.cpp
1606 lines (1127 loc) · 45.1 KB
/
IOATABlockStorageCommands.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1998-2003 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#include <IOKit/assert.h>
#include <IOKit/IOSyncer.h>
#include <IOKit/IOKitKeys.h>
#include <IOKit/storage/IOMedia.h>
#include "IOATABlockStorageDriver.h"
#define ATA_BLOCK_STORAGE_DRIVER_DEBUGGING_LEVEL 4
#if ( ATA_BLOCK_STORAGE_DRIVER_DEBUGGING_LEVEL >= 1 )
#define PANIC_NOW(x) IOPanic x
#else
#define PANIC_NOW(x)
#endif
#if ( ATA_BLOCK_STORAGE_DRIVER_DEBUGGING_LEVEL >= 2 )
#define ERROR_LOG(x) IOLog x
#else
#define ERROR_LOG(x)
#endif
#if ( ATA_BLOCK_STORAGE_DRIVER_DEBUGGING_LEVEL >= 3 )
#define STATUS_LOG(x) IOLog x
#else
#define STATUS_LOG(x)
#endif
#if ( ATA_BLOCK_STORAGE_DRIVER_PM_DEBUGGING_LEVEL >= 1 )
#define SERIAL_STATUS_LOG(x) kprintf x
#else
#define SERIAL_STATUS_LOG(x)
#endif
// Media icon keys
#define kCFBundleIdentifierKey "CFBundleIdentifier"
#define kIOATABlockStorageIdentifierKey "com.apple.iokit.IOATABlockStorage"
#define kPCCardIconFileKey "PCCard.icns"
// Configuration state machine
enum
{
kPIOTransferModeSetup = 1,
kPIOTransferModeDone = 2,
kDMATransferModeDone = 3,
kReadAheadEnableDone = 4,
kWriteCacheEnableDone = 5
};
struct ATAConfigData
{
IOATABlockStorageDriver_PD * self;
UInt32 state;
IOSyncer * syncer;
};
typedef struct ATAConfigData ATAConfigData;
//--------------------------------------------------------------------------------------
// ¥ identifyATADevice - Sends a device identify request to the device
// and uses it to configure the drive speeds
//--------------------------------------------------------------------------------------
IOReturn
IOATABlockStorageDriver_PD::identifyATADevice ( void )
{
IOReturn status = kIOReturnSuccess;
IOATACommand * cmd = NULL;
ATAClientData * clientData = NULL;
STATUS_LOG ( ( "IOATABlockStorageDriver::identifyATADevice entering.\n" ) );
// Get a new command object
cmd = getATACommandObject ( );
assert ( cmd != NULL );
clientData = ( ATAClientData * ) cmd->refCon;
// Zero the command object
cmd->zeroCommand ( );
// Start filling in the command
cmd->setUnit ( fATAUnitID );
sSetCommandBuffers ( cmd, fDeviceIdentifyBuffer, 0, 1 );
cmd->setCommand ( kATAcmdDriveIdentify );
cmd->setFlags ( mATAFlagIORead );
cmd->setOpcode ( kATAFnExecIO );
// set the device head to the correct unit
cmd->setDevice_Head ( fATAUnitID << 4 );
cmd->setRegMask ( ( ataRegMask ) ( mATAErrFeaturesValid | mATAStatusCmdValid ) );
// Save state data
clientData->command = kATAcmdDriveIdentify;
clientData->flags = mATAFlagIORead;
clientData->opCode = kATAFnExecIO;
clientData->regMask = ( ataRegMask ) ( mATAErrFeaturesValid | mATAStatusCmdValid );
STATUS_LOG ( ( "IOATABlockStorageDriver::identifyATADevice executing identify command.\n" ) );
status = syncExecute ( cmd, kATATimeout10Seconds );
if ( status != kIOReturnSuccess )
{
ERROR_LOG ( ( "IOATABlockStorageDriver::identifyATADevice result = %u.\n", (unsigned int) cmd->getResult ( ) ) );
return status;
}
// Validate the identify data before the swap!
status = sValidateIdentifyData ( ( UInt8 * ) fDeviceIdentifyData );
#if defined(__BIG_ENDIAN__)
// The identify device info needs to be byte-swapped on big-endian (ppc)
// systems becuase it is data that is produced by the drive, read across a
// 16-bit little-endian PCI interface, directly into a big-endian system.
// Regular data doesn't need to be byte-swapped because it is written and
// read from the host and is intrinsically byte-order correct.
sSwapBytes16 ( ( UInt8 * ) fDeviceIdentifyData, 512 );
#endif
STATUS_LOG ( ( "IOATABlockStorageDriver::identifyATADevice exiting with status = %u.\n", (unsigned int) status ) );
return status;
}
//--------------------------------------------------------------------------------------
// ¥ setupReadWriteTaskFile - Setup the command's taskfile registers based on
// parameters given
// block - Initial transfer block
// nblks - Number of blocks to transfer
//--------------------------------------------------------------------------------------
void
IOATABlockStorageDriver_PD::setupReadWriteTaskFile (
IOATACommand * cmd,
IOMemoryDescriptor * buffer,
UInt32 block,
UInt32 nblks )
{
ATAClientData * clientData;
UInt32 flags = mATAFlagUseConfigSpeed;
UInt8 command = 0;
bool isWrite = ( buffer->getDirection ( ) == kIODirectionOut );
STATUS_LOG ( ( "IOATABlockStorageDriver::setupReadWriteTaskFile entering.\n" ) );
clientData = ( ATAClientData * ) cmd->refCon;
// Zero the command
cmd->zeroCommand ( );
// Check if we are capable of DMA for the I/O
if ( fUltraDMAMode || fDMAMode )
{
if ( fUseExtendedLBA )
{
command = ( isWrite ) ? kATAcmdWriteDMAExtended : kATAcmdReadDMAExtended;
flags |= mATAFlag48BitLBA;
}
else
{
command = ( isWrite ) ? kATAcmdWriteDMA : kATAcmdReadDMA;
}
flags |= mATAFlagUseDMA;
}
else
{
if ( fUseExtendedLBA )
{
command = ( isWrite ) ? kATAcmdWriteExtended : kATAcmdReadExtended;
}
else
{
command = ( isWrite ) ? kATAcmdWrite : kATAcmdRead;
}
}
flags |= ( isWrite ) ? mATAFlagIOWrite : mATAFlagIORead;
// Set the ATA Command
cmd->setCommand ( command );
cmd->setSectorCount ( ( nblks == kIOATAMaxBlocksPerXfer ) ? 0 : nblks );
if ( fUseLBAAddressing )
{
if ( fUseExtendedLBA )
{
IOExtendedLBA * extLBA = cmd->getExtendedLBA ( );
extLBA->setExtendedLBA ( 0, block, fATAUnitID, ( UInt16 ) nblks, command );
clientData->useExtendedLBA = true;
clientData->lbaLow16 = extLBA->getLBALow16 ( );
clientData->lbaMid16 = extLBA->getLBAMid16 ( );
clientData->lbaHigh16 = extLBA->getLBAHigh16 ( );
clientData->sectorCount16 = extLBA->getSectorCount16 ( );
clientData->features16 = extLBA->getFeatures16 ( );
clientData->device = extLBA->getDevice ( );
clientData->command16 = extLBA->getCommand ( );
}
else
{
STATUS_LOG ( ( "IOATABlockStorageDriver::setupReadWriteTaskFile block = %x.\n", (unsigned int)block ) );
// 28bit LBA addressing supported
cmd->setLBA28 ( block, fATAUnitID );
}
clientData->sectorNumber = ( block & 0xFF );
clientData->cylLow = ( ( block & 0xFF00 ) >> 8 );
clientData->cylHigh = ( ( block & 0x00FF0000 ) >> 16 );
}
else
{
UInt32 heads = 0;
UInt32 sectors = 0;
heads = fDeviceIdentifyData[kATAIdentifyLogicalHeadCount];
sectors = fDeviceIdentifyData[kATAIdentifySectorsPerTrack];
// Not LBA, so use CHS addressing model
sConvertBlockToCHSAddress ( cmd, block, heads, sectors, fATAUnitID );
}
sSetCommandBuffers ( cmd, buffer, 0, nblks );
cmd->setUnit ( fATAUnitID );
cmd->setFlags ( flags );
cmd->setOpcode ( kATAFnExecIO );
clientData->command = command;
clientData->opCode = kATAFnExecIO;
clientData->flags = flags;
STATUS_LOG ( ( "IOATABlockStorageDriver::setupReadWriteTaskFile exiting.\n" ) );
}
//--------------------------------------------------------------------------------------
// ¥ ataCommandReadWrite - Setup the command for a read/write operation
//
// buffer - IOMemoryDescriptor object describing this transfer.
// block - Initial transfer block.
// nblks - Number of blocks to transfer.
//--------------------------------------------------------------------------------------
IOATACommand *
IOATABlockStorageDriver_PD::ataCommandReadWrite (
IOMemoryDescriptor * buffer,
UInt32 block,
UInt32 nblks )
{
IOATACommand * cmd = NULL;
STATUS_LOG ( ( "IOATABlockStorageDriver::ataCommandReadWrite entering.\n" ) );
assert ( buffer != NULL );
cmd = getATACommandObject ( );
if ( cmd == NULL )
return NULL;
// Setup the taskfile structure with the size and direction of the
// transfer. This structure will be written to the actual taskfile
// registers when this command is processed.
setupReadWriteTaskFile ( cmd, buffer, block, nblks );
STATUS_LOG ( ( "IOATABlockStorageDriver::ataCommandReadWrite exiting.\n" ) );
return cmd;
}
//--------------------------------------------------------------------------------------
// ¥ ataCommandSetFeatures - Setup the command for a set features operation
//
// features - contents of the Features register
// sectorCount - contents of Sector Count register
// sectorNumber - contents of Sector Number register
// cylinderLow - contents of the Cylinder Low register
// cylinderHigh - contents of the Cylinder High register
//--------------------------------------------------------------------------------------
IOReturn
IOATABlockStorageDriver_PD::ataCommandSetFeatures (
UInt8 features,
UInt8 sectorCount,
UInt8 sectorNumber,
UInt8 cylinderLow,
UInt8 cylinderHigh,
UInt32 flags,
bool forceSync )
{
IOReturn status = kIOReturnSuccess;
STATUS_LOG ( ( "IOATABlockStorageDriver::ataCommandSetFeatures entering.\n" ) );
// Zero the command
fConfigurationCommand->zeroCommand ( );
fConfigurationCommand->setUnit ( fATAUnitID );
fConfigurationCommand->setFeatures ( features );
fConfigurationCommand->setSectorCount ( sectorCount );
fConfigurationCommand->setSectorNumber ( sectorNumber );
fConfigurationCommand->setOpcode ( kATAFnExecIO );
fConfigurationCommand->setCommand ( kATAcmdSetFeatures );
fConfigurationCommand->setDevice_Head ( fATAUnitID << 4 );
fConfigurationCommand->setCylHi ( cylinderHigh );
fConfigurationCommand->setCylLo ( cylinderLow );
fConfigurationCommand->setFlags ( flags );
fConfigurationCommand->setTimeoutMS ( kATATimeout10Seconds );
if ( forceSync )
{
fConfigurationCommand->setCallbackPtr ( &IOATABlockStorageDriver_PD::sATAVoidCallback );
}
else
{
fConfigurationCommand->setCallbackPtr ( &IOATABlockStorageDriver_PD::sATAConfigStateMachine );
}
status = fATADevice->executeCommand ( fConfigurationCommand );
STATUS_LOG ( ( "IOATABlockStorageDriver::ataCommandSetFeatures exiting.\n" ) );
return status;
}
//--------------------------------------------------------------------------------------
// ¥ ataCommandFlushCache - Setup the command for a flush cache operation
//--------------------------------------------------------------------------------------
IOATACommand *
IOATABlockStorageDriver_PD::ataCommandFlushCache ( void )
{
IOATACommand * cmd = NULL;
ATAClientData * clientData = NULL;
STATUS_LOG ( ( "IOATABlockStorageDriver::ataCommandFlushCache entering.\n" ) );
cmd = getATACommandObject ( );
if ( cmd == NULL )
return NULL;
clientData = ( ATAClientData * ) cmd->refCon;
// Zero the command
cmd->zeroCommand ( );
if ( fDeviceIdentifyData[kATAIdentifyCommandSetSupported2] & kATASupportsFlushCacheExtendedMask )
{
IOExtendedLBA * extLBA = cmd->getExtendedLBA ( );
extLBA->setExtendedLBA ( 0, 0, fATAUnitID, 0, kATAcmdFlushCacheExtended );
cmd->setFlags ( mATAFlag48BitLBA );
clientData->useExtendedLBA = true;
clientData->lbaLow16 = extLBA->getLBALow16 ( );
clientData->lbaMid16 = extLBA->getLBAMid16 ( );
clientData->lbaHigh16 = extLBA->getLBAHigh16 ( );
clientData->sectorCount16 = extLBA->getSectorCount16 ( );
clientData->features16 = extLBA->getFeatures16 ( );
clientData->device = extLBA->getDevice ( );
clientData->command16 = extLBA->getCommand ( );
}
cmd->setOpcode ( kATAFnExecIO );
cmd->setCommand ( kATAcmdFlushCache );
cmd->setDevice_Head ( fATAUnitID << 4 );
cmd->setUnit ( fATAUnitID );
clientData->command = kATAcmdFlushCache;
clientData->opCode = kATAFnExecIO;
STATUS_LOG ( ( "IOATABlockStorageDriver::ataCommandFlushCache exiting.\n" ) );
return cmd;
}
//---------------------------------------------------------------------------
// Issue a synchronous ATA command.
//---------------------------------------------------------------------------
IOReturn
IOATABlockStorageDriver_PD::syncExecute (
IOATACommand * cmd,
UInt32 timeout,
UInt8 retries )
{
ATAClientData * clientData = NULL;
IOReturn status = kIOReturnSuccess;
STATUS_LOG ( ( "IOATABlockStorageDriver::syncExecute entering.\n" ) );
clientData = ( ATAClientData * ) cmd->refCon;
assert ( clientData != NULL );
cmd->setTimeoutMS ( timeout );
cmd->setCallbackPtr ( &IOATABlockStorageDriver_PD::sHandleCommandCompletion );
// Set up for synchronous transaction
clientData->completion.syncLock = IOSyncer::create ( );
assert ( clientData->completion.syncLock != NULL );
if ( clientData->completion.syncLock == NULL )
{
returnATACommandObject ( cmd );
return kIOReturnNoResources;
}
// set up any client data necessary for the command
clientData->isSync = true;
clientData->self = this;
clientData->maxRetries = retries;
clientData->timeout = timeout;
// save the state data in case we need to issue retries
sSaveStateData ( cmd );
fATADevice->executeCommand ( cmd );
// Block client thread on lock until the completion handler
// receives an indication that the processing is complete
clientData->completion.syncLock->wait ( );
STATUS_LOG ( ( "IOATABlockStorageDriver::syncExecute exiting.\n" ) );
// Pull the error from the clientData
status = clientData->returnCode;
// Re-enqueue the command object since we're done with it
returnATACommandObject ( cmd );
return status;
}
//---------------------------------------------------------------------------
// Issue an asynchronous ATA command.
//---------------------------------------------------------------------------
IOReturn
IOATABlockStorageDriver_PD::asyncExecute (
IOATACommand * cmd,
IOStorageCompletion completion,
UInt32 timeout,
UInt8 retries )
{
ATAClientData * clientData = NULL;
IOReturn status = kIOReturnSuccess;
STATUS_LOG ( ( "IOATABlockStorageDriver::asyncExecute entering.\n" ) );
clientData = ( ATAClientData * ) cmd->refCon;
assert ( clientData != NULL );
// Set timeout and register the completion handler.
cmd->setTimeoutMS ( timeout );
cmd->setCallbackPtr( &IOATABlockStorageDriver_PD::sHandleCommandCompletion );
// Set up for an asynchronous transaction
clientData->isSync = false;
clientData->self = this;
clientData->maxRetries = retries;
clientData->completion.async = completion;
clientData->timeout = timeout;
// save the state data in case we need to issue retries
sSaveStateData ( cmd );
// Execute the command
status = fATADevice->executeCommand ( cmd );
if ( status != kATANoErr )
{
bzero ( clientData, sizeof ( ATAClientData ) );
returnATACommandObject ( cmd );
}
return status;
}
//--------------------------------------------------------------------------------------
// ¥ allocateATACommandObjects - allocates ATA command objects
//--------------------------------------------------------------------------------------
void
IOATABlockStorageDriver_PD::allocateATACommandObjects ( void )
{
STATUS_LOG ( ( "IOATABlockStorageDriver::allocateATACommandObjects entering\n" ) );
IOATACommand * cmd = NULL;
ATAClientData * clientData = NULL;
IOSyncer * syncer = NULL;
ATAConfigData * configData = NULL;
for ( UInt32 index = 0; index < fNumCommandObjects; index++ )
{
// Allocate the command
cmd = fATADevice->allocCommand ( );
assert ( cmd != NULL );
// Allocate the command clientData
clientData = ( ATAClientData * ) IOMalloc ( sizeof ( ATAClientData ) );
assert ( clientData != NULL );
bzero ( clientData, sizeof ( ATAClientData ) );
// set the back pointers to each other
cmd->refCon = ( void * ) clientData;
clientData->cmd = cmd;
STATUS_LOG ( ( "adding command to pool\n" ) );
// Enqueue the command in the free list
fCommandPool->returnCommand ( cmd );
}
// Allocate the command
fResetCommand = fATADevice->allocCommand ( );
fPowerManagementCommand = fATADevice->allocCommand ( );
fConfigurationCommand = fATADevice->allocCommand ( );
assert ( fResetCommand != NULL );
assert ( fPowerManagementCommand != NULL );
assert ( fConfigurationCommand != NULL );
syncer = IOSyncer::create ( );
assert ( syncer != NULL );
fPowerManagementCommand->refCon = ( void * ) syncer;
configData = ( ATAConfigData * ) IOMalloc ( sizeof ( ATAConfigData ) );
assert ( configData != NULL );
bzero ( configData, sizeof ( ATAConfigData ) );
configData->syncer = IOSyncer::create ( );
assert ( configData->syncer != NULL );
fConfigurationCommand->refCon = ( void * ) configData;
STATUS_LOG ( ( "IOATABlockStorageDriver::allocateATACommandObjects exiting\n" ) );
}
//--------------------------------------------------------------------------------------
// ¥ dellocateATACommandObjects - deallocates ATA command objects
//--------------------------------------------------------------------------------------
void
IOATABlockStorageDriver_PD::deallocateATACommandObjects ( void )
{
STATUS_LOG ( ( "IOATABlockStorageDriver::dellocateATACommandObjects entering\n" ) );
IOATACommand * cmd = NULL;
ATAClientData * clientData = NULL;
IOSyncer * syncer = NULL;
ATAConfigData * configData = NULL;
cmd = ( IOATACommand * ) fCommandPool->getCommand ( false );
assert ( cmd != NULL );
//¥¥¥ Walk the in-use queue and abort the commands (potential memory leak right now)
// This handles walking the free command queue
while ( cmd != NULL )
{
clientData = ( ATAClientData * ) cmd->refCon;
assert ( clientData != NULL );
IOFree ( clientData, sizeof ( ATAClientData ) );
clientData = NULL;
fATADevice->freeCommand ( cmd );
cmd = ( IOATACommand * ) fCommandPool->getCommand ( false );
}
syncer = ( IOSyncer * ) fPowerManagementCommand->refCon;
syncer->release ( );
configData = ( ATAConfigData * ) fConfigurationCommand->refCon;
configData->syncer->release ( );
IOFree ( configData, sizeof ( ATAConfigData ) );
fATADevice->freeCommand ( fResetCommand );
fATADevice->freeCommand ( fPowerManagementCommand );
fATADevice->freeCommand ( fConfigurationCommand );
STATUS_LOG ( ( "IOATABlockStorageDriver::dellocateATACommandObjects exiting.\n" ) );
}
//--------------------------------------------------------------------------------------
// ¥ getATACommandObject - Gets the first non-busy ata command object
// If they are all busy, it returns NULL
//--------------------------------------------------------------------------------------
IOATACommand *
IOATABlockStorageDriver_PD::getATACommandObject ( bool blockForCommand )
{
IOATACommand * cmd = NULL;
STATUS_LOG ( ( "IOATABlockStorageDriver::getATACommandObject entering.\n" ) );
cmd = ( IOATACommand * ) fCommandPool->getCommand ( blockForCommand );
assert ( cmd != NULL );
return cmd;
}
//--------------------------------------------------------------------------------------
// ¥ returnATACommandObject - returns the ATA command object to the pool
//--------------------------------------------------------------------------------------
void
IOATABlockStorageDriver_PD::returnATACommandObject ( IOATACommand * cmd )
{
ATAClientData * clientData;
STATUS_LOG ( ( "IOATABlockStorageDriver::returnATACommandObject entering.\n" ) );
assert ( cmd != NULL );
clientData = ( ATAClientData * ) cmd->refCon;
assert ( clientData != NULL );
// Clear out the clientData, so we don't reuse any variables
bzero ( clientData, sizeof ( ATAClientData ) );
// set the back pointers to each other
cmd->refCon = ( void * ) clientData;
clientData->cmd = cmd;
// Finally, re-enqueue the command
fCommandPool->returnCommand ( cmd );
}
//--------------------------------------------------------------------------------------
// ¥ identifyAndConfigureATADevice - Sends a device identify request to the device
// and uses it to configure the drive speeds
//--------------------------------------------------------------------------------------
IOReturn
IOATABlockStorageDriver_PD::identifyAndConfigureATADevice ( void )
{
IOReturn status = kIOReturnSuccess;
IOATABusInfo * busInfoPtr = NULL;
IOATADevConfig * deviceConfigPtr = NULL;
UInt16 tempWord = 0;
UInt16 maxBlocks = 0;
UInt64 maxSize = 0;
// Get some info about the ATA bus
busInfoPtr = IOATABusInfo::atabusinfo ( );
if ( busInfoPtr == NULL )
return kIOReturnNoResources;
// Zero the data, and then get the information from the ATA controller
busInfoPtr->zeroData ( );
status = fATADevice->provideBusInfo ( busInfoPtr );
if ( status != kIOReturnSuccess )
{
ERROR_LOG ( ( "IOATABlockStorageDriver::identifyAndConfigureATADevice provide bus info failed thErr = %u.\n", (unsigned int) status ) );
goto ReleaseBusInfoAndBail;
}
// Get the socket type
fATASocketType = busInfoPtr->getSocketType ( );
STATUS_LOG ( ( "IOATABlockStorageDriver::identifyAndConfigureATADevice socket type = %d.\n", ( UInt8 ) fATASocketType ) );
// Set the media icon to PC-Card if it is a PC Card device
if ( fATASocketType == kPCCardSocket )
{
OSDictionary * dict = OSDictionary::withCapacity ( 2 );
if ( dict != NULL )
{
OSString * identifier = OSString::withCString ( kIOATABlockStorageIdentifierKey );
OSString * resourceFile = OSString::withCString ( kPCCardIconFileKey );
if ( ( identifier != NULL ) && ( resourceFile != NULL ) )
{
dict->setObject ( kCFBundleIdentifierKey, identifier );
dict->setObject ( kIOBundleResourceFileKey, resourceFile );
setProperty ( kIOMediaIconKey, dict );
}
if ( identifier != NULL )
identifier->release ( );
if ( resourceFile != NULL )
resourceFile->release ( );
dict->release ( );
}
}
// Execute an ATA device identify
status = identifyATADevice ( );
if ( status != kIOReturnSuccess )
{
goto ReleaseBusInfoAndBail;
}
// Parse the identify data
tempWord = fDeviceIdentifyData[kATAIdentifyCommandSetSupported];
// Check for S.M.A.R.T.
if ( ( tempWord & kATASupportsSMARTMask ) == kATASupportsSMARTMask )
fSupportedFeatures |= kIOATAFeatureSMART;
// Check for power management
if ( ( tempWord & kATASupportsPowerManagementMask ) == kATASupportsPowerManagementMask )
fSupportedFeatures |= kIOATAFeaturePowerManagement;
// Check for write cache
if ( ( tempWord & kATASupportsWriteCacheMask ) == kATASupportsWriteCacheMask )
fSupportedFeatures |= kIOATAFeatureWriteCache;
tempWord = fDeviceIdentifyData[kATAIdentifyCommandSetSupported2];
if ( ( tempWord & kATADataIsValidMask ) == 0x4000 )
{
// Check for APM
if ( ( tempWord & kATASupportsAdvancedPowerManagementMask ) == kATASupportsAdvancedPowerManagementMask )
fSupportedFeatures |= kIOATAFeatureAdvancedPowerManagement;
// Check for CF
if ( ( tempWord & kATASupportsCompactFlashMask ) == kATASupportsCompactFlashMask )
fSupportedFeatures |= kIOATAFeatureCompactFlash;
}
// Sanity check. CHS is rarely used, but it is still good to do things correctly.
if ( fDeviceIdentifyData[kATAIdentifyDriveCapabilities] & kLBASupportedMask )
fUseLBAAddressing = true;
// Check for ExtendedLBA (48-bit) support from the bus.
fUseExtendedLBA = busInfoPtr->supportsExtendedLBA ( );
// Mask with drive support
fUseExtendedLBA &= IOATADevConfig::sDriveSupports48BitLBA ( fDeviceIdentifyData );
if ( fUseExtendedLBA )
{
maxBlocks = busInfoPtr->maxBlocksExtended ( );
// 48-bit LBA supported
fSupportedFeatures |= kIOATAFeature48BitLBA;
}
else
{
// Not using 48-bit LBA, so use normal constraints
maxBlocks = kIOATAMaximumBlockCount8Bit;
}
maxSize = ( UInt64 ) maxBlocks * ( UInt64 ) kATADefaultSectorSize;
// Publish some constraints
setProperty ( kIOMaximumBlockCountReadKey, maxBlocks, 64 );
setProperty ( kIOMaximumBlockCountWriteKey, maxBlocks, 64 );
setProperty ( kIOMaximumSegmentCountReadKey, maxSize / PAGE_SIZE, 64 );
setProperty ( kIOMaximumSegmentCountWriteKey, maxSize / PAGE_SIZE, 64 );
deviceConfigPtr = IOATADevConfig::atadevconfig ( );
assert ( deviceConfigPtr != NULL );
// Get the device config from ATA Controller
status = fATADevice->provideConfig ( deviceConfigPtr );
if ( status != kIOReturnSuccess )
{
ERROR_LOG ( ( "IOATABlockStorageDriver::identifyAndConfigureATADevice provideConfig returned an error = %u.\n", (unsigned int) status ) );
goto ReleaseBusInfoAndBail;
}
// Pass the bus information and device identify info to have ATA Controller choose
// the best config for this device (if it can)
status = deviceConfigPtr->initWithBestSelection ( fDeviceIdentifyData, busInfoPtr );
if ( status != kIOReturnSuccess )
{
goto ReleaseBusInfoAndBail;
}
// Select the config it gives us
status = fATADevice->selectConfig ( deviceConfigPtr );
if ( status != kIOReturnSuccess )
{
ERROR_LOG ( ( "IOATABlockStorageDriver::identifyAndConfigureATADevice selectConfig returned error = %u.\n", (unsigned int) status ) );
}
// Store the PIOModes, DMAModes and UltraDMAModes supported
fPIOMode = deviceConfigPtr->getPIOMode ( );
fDMAMode = deviceConfigPtr->getDMAMode ( );
fUltraDMAMode = deviceConfigPtr->getUltraMode ( );
// Add any more configuration info we need (write cache enable, power management, etc.)
status = configureATADevice ( );
STATUS_LOG ( ( "IOATABlockStorageDriver::identifyAndConfigureATADevice configureATADevice returned status = %u.\n", (unsigned int) status ) );
ReleaseBusInfoAndBail:
if ( busInfoPtr != NULL )
{
STATUS_LOG ( ( "IOATABlockStorageDriver::identifyAndConfigureATADevice releasing bus info.\n" ) );
busInfoPtr->release ( );
busInfoPtr = NULL;
}
if ( deviceConfigPtr != NULL )
{
STATUS_LOG ( ( "IOATABlockStorageDriver::identifyAndConfigureATADevice releasing device config.\n" ) );
deviceConfigPtr->release ( );
deviceConfigPtr = NULL;
}
STATUS_LOG ( ( "IOATABlockStorageDriver::identifyAndConfigureATADevice returning status = %u.\n", (unsigned int) status ) );
return status;
}
//--------------------------------------------------------------------------------------
// ¥ configureATADevice - Configures the ATA Device
//--------------------------------------------------------------------------------------
IOReturn
IOATABlockStorageDriver_PD::configureATADevice ( void )
{
ATAConfigData * configData;
STATUS_LOG ( ( "IOATABlockStorageDriver::configureATADevice entering.\n" ) );
configData = ( ATAConfigData * ) fConfigurationCommand->refCon;
configData->self = this;
configData->state = kPIOTransferModeSetup;
sATAConfigStateMachine ( fConfigurationCommand );
configData->syncer->reinit ( );
configData->syncer->wait ( false );
return kIOReturnSuccess;
}
//--------------------------------------------------------------------------------------
// ¥ reconfigureATADevice - Reconfigures the ATA Device
//--------------------------------------------------------------------------------------
IOReturn
IOATABlockStorageDriver_PD::reconfigureATADevice ( void )
{
// There is a window here on a machine with both device0 and device1 ATA
// devices where if a device is not ready and a bus reset occurs, and the
// other driver hasn't allocated resources yet but is instantiated, it
// could cause a panic. We check to make sure the fConfigurationCommand
// instance variable is not NULL to make sure all resources necessary
// for reconfiguration have been allocated before proceeding with
// the reconfiguration.
if ( fConfigurationCommand != NULL )
{
// We are on a callout from the ATA controller and need to execute
// all of these commands synchronously.
setPIOTransferMode ( true );
setDMATransferMode ( true );
// Check if a client has previously set the APM level. If so,
// reconfigure the device with that value.
if ( fAPMLevel != 0xFF )
{
setAdvancedPowerManagementLevel ( fAPMLevel, true );
}
ataCommandSetFeatures ( kATAEnableReadAhead,
0,
0,
0,
0,
mATAFlagImmediate,
true );