-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIOATABlockStorageDevice.cpp
786 lines (520 loc) · 17.2 KB
/
IOATABlockStorageDevice.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
/*
* 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/IOLib.h>
#include <IOKit/IOKitKeys.h>
#include <IOKit/storage/IOStorageDeviceCharacteristics.h>
#include <IOKit/IOPlatformExpert.h>
#include "IOATABlockStorageDriver.h"
#include "IOATABlockStorageDevice.h"
#include "ATASMARTLibPriv.h"
#define super IOBlockStorageDevice
OSDefineMetaClassAndStructors ( IOATABlockStorageDevice, IOBlockStorageDevice )
OSMetaClassDefineReservedUsed ( IOATABlockStorageDevice, 1 );
OSMetaClassDefineReservedUnused ( IOATABlockStorageDevice, 2 );
OSMetaClassDefineReservedUnused ( IOATABlockStorageDevice, 3 );
OSMetaClassDefineReservedUnused ( IOATABlockStorageDevice, 4 );
OSMetaClassDefineReservedUnused ( IOATABlockStorageDevice, 5 );
OSMetaClassDefineReservedUnused ( IOATABlockStorageDevice, 6 );
OSMetaClassDefineReservedUnused ( IOATABlockStorageDevice, 7 );
OSMetaClassDefineReservedUnused ( IOATABlockStorageDevice, 8 );
OSMetaClassDefineReservedUnused ( IOATABlockStorageDevice, 9 );
OSMetaClassDefineReservedUnused ( IOATABlockStorageDevice, 10 );
OSMetaClassDefineReservedUnused ( IOATABlockStorageDevice, 11 );
OSMetaClassDefineReservedUnused ( IOATABlockStorageDevice, 12 );
OSMetaClassDefineReservedUnused ( IOATABlockStorageDevice, 13 );
OSMetaClassDefineReservedUnused ( IOATABlockStorageDevice, 14 );
OSMetaClassDefineReservedUnused ( IOATABlockStorageDevice, 15 );
OSMetaClassDefineReservedUnused ( IOATABlockStorageDevice, 16 );
#if ( ATA_BLOCK_STORAGE_DEVICE_DEBUGGING_LEVEL >= 1 )
#define PANIC_NOW(x) IOPanic x
#else
#define PANIC_NOW(x)
#endif
#if ( ATA_BLOCK_STORAGE_DEVICE_DEBUGGING_LEVEL >= 2 )
#define ERROR_LOG(x) IOLog x
#else
#define ERROR_LOG(x)
#endif
#if ( ATA_BLOCK_STORAGE_DEVICE_DEBUGGING_LEVEL >= 3 )
#define STATUS_LOG(x) IOLog x
#else
#define STATUS_LOG(x)
#endif
//---------------------------------------------------------------------------
// attach to provider.
bool
IOATABlockStorageDevice::attach ( IOService * provider )
{
OSDictionary * dictionary = NULL;
if ( !super::attach ( provider ) )
return false;
fProvider = OSDynamicCast (IOATABlockStorageDriver_PD, provider);
if ( fProvider == NULL )
{
ERROR_LOG ( ( "IOATABlockStorageDevice: attach; wrong provider type!\n" ) );
return false;
}
reserved = IONew ( ExpansionData, 1 );
if ( reserved == NULL )
{
ERROR_LOG ( ( "IOATABlockStorageDevice: attach; can't allocate ExpansionData!\n" ) );
return false;
}
bzero ( reserved, sizeof ( ExpansionData ) );
dictionary = OSDictionary::withCapacity ( 2 );
if ( dictionary != NULL )
{
dictionary->setObject ( kIOPropertyPhysicalInterconnectTypeKey, fProvider->getProperty ( kIOPropertyPhysicalInterconnectTypeKey ) );
dictionary->setObject ( kIOPropertyPhysicalInterconnectLocationKey, fProvider->getProperty ( kIOPropertyPhysicalInterconnectLocationKey ) );
setProperty ( kIOPropertyProtocolCharacteristicsKey, dictionary );
dictionary->release ( );
}
dictionary = OSDictionary::withCapacity ( 1 );
if ( dictionary != NULL )
{
char * string;
OSString * theString;
OSNumber * value;
UInt32 features;
string = getVendorString ( );
theString = OSString::withCString ( string );
if ( theString != NULL )
{
dictionary->setObject ( kIOPropertyVendorNameKey, theString );
theString->release ( );
}
string = getProductString ( );
theString = OSString::withCString ( string );
if ( theString != NULL )
{
dictionary->setObject ( kIOPropertyProductNameKey, theString );
theString->release ( );
}
string = getRevisionString ( );
theString = OSString::withCString ( string );
if ( theString != NULL )
{
dictionary->setObject ( kIOPropertyProductRevisionLevelKey, theString );
theString->release ( );
}
dictionary->setObject ( kIOATASupportedFeaturesKey, fProvider->getProperty ( kIOATASupportedFeaturesKey ) );
value = OSDynamicCast ( OSNumber, fProvider->getProperty ( kIOATASupportedFeaturesKey ) );
if ( value != NULL )
{
features = value->unsigned32BitValue ( );
if ( features & kIOATAFeatureSMART )
{
OSDictionary * userClientDict = OSDictionary::withCapacity ( 1 );
OSString * string1;
OSString * string2;
string1 = OSString::withCString ( kATASMARTUserClientTypeIDKey );
string2 = OSString::withCString ( kATASMARTUserClientLibLocationKey );
userClientDict->setObject ( string1, string2 );
setProperty ( kIOUserClientClassKey, kATASMARTUserClientClassKey );
setProperty ( kIOCFPlugInTypesKey, userClientDict );
if ( userClientDict != NULL )
{
userClientDict->release ( );
userClientDict = NULL;
}
if ( string1 != NULL )
{
string1->release ( );
string1 = NULL;
}
if ( string2 != NULL )
{
string2->release ( );
string2 = NULL;
}
}
}
setProperty ( kIOPropertyDeviceCharacteristicsKey, dictionary );
dictionary->release ( );
}
return true;
}
//---------------------------------------------------------------------------
// detach from provider.
void
IOATABlockStorageDevice::detach ( IOService * provider )
{
if ( fProvider == provider )
fProvider = 0;
if ( reserved != NULL )
{
if ( fClients != NULL )
{
fClients->release ( );
fClients = NULL;
}
IODelete ( reserved, ExpansionData, 1 );
reserved = NULL;
}
super::detach ( provider );
}
//---------------------------------------------------------------------------
// handleOpen from client.
bool
IOATABlockStorageDevice::handleOpen ( IOService * client, IOOptionBits options, void * access )
{
// If this isn't a user client, pass through to superclass.
if ( ( options & kIOATASMARTUserClientAccessMask ) == 0 )
{
return super::handleOpen ( client, options, access );
}
// It's the user client, so add it to the set
if ( fClients == NULL )
fClients = OSSet::withCapacity ( 1 );
if ( fClients == NULL )
return false;
// Check if we already have a user client open
if ( fClients->getCount ( ) != 0 )
{
ERROR_LOG ( ( "User client already open\n" ) );
return false;
}
fClients->setObject ( client );
return true;
}
//---------------------------------------------------------------------------
// handleClose from client.
void
IOATABlockStorageDevice::handleClose ( IOService * client, IOOptionBits options )
{
// If this isn't a user client, pass through to superclass.
if ( ( options & kIOATASMARTUserClientAccessMask ) == 0 )
super::handleClose ( client, options );
else
{
STATUS_LOG ( ( "Removing user client\n" ) );
fClients->removeObject ( client );
if ( fClients->getCount ( ) != 0 )
{
ERROR_LOG ( ( "Removed client and still have count = %d\n", fClients->getCount ( ) ) );
return;
}
}
}
//---------------------------------------------------------------------------
// handleIsOpen from client.
bool
IOATABlockStorageDevice::handleIsOpen ( const IOService * client ) const
{
// General case (is anybody open)
if ( client == NULL )
{
STATUS_LOG ( ( "IOATABlockStorageDevice::handleIsOpen, client is NULL\n" ) );
if ( ( fClients != NULL ) && ( fClients->getCount ( ) > 0 ) )
return true;
STATUS_LOG ( ( "calling super\n" ) );
return super::handleIsOpen ( client );
}
STATUS_LOG ( ( "IOATABlockStorageDevice::handleIsOpen, client = %p\n", client ) );
// specific case (is this client open)
if ( ( fClients != NULL ) && ( fClients->containsObject ( client ) ) )
return true;
STATUS_LOG ( ( "calling super\n" ) );
return super::handleIsOpen ( client );
}
//---------------------------------------------------------------------------
//
IOReturn
IOATABlockStorageDevice::setProperties ( OSObject * properties )
{
OSDictionary * dict = NULL;
OSNumber * number = NULL;
IOReturn status = kIOReturnSuccess;
UInt32 features = 0;
boolean_t success = false;
char modelName[50];
STATUS_LOG ( ( "IOATABlockStorageDevice::setProperties called\n" ) );
status = super::setProperties ( properties );
if ( status == kIOReturnSuccess )
{
return status;
}
number = ( OSNumber * ) fProvider->getProperty ( kIOATASupportedFeaturesKey );
features = number->unsigned32BitValue ( );
// Check if the drive supports this feature or not.
if ( ( features & kIOATAFeatureAdvancedPowerManagement ) == 0 )
{
ERROR_LOG ( ( "IOATABlockStorageDevice::setProperties called on unsupported drive\n" ) );
status = kIOReturnUnsupported;
return status;
}
// Don't send the command to non-powerbooks
success = PEGetModelName ( modelName, sizeof ( modelName ) );
if ( success == true )
{
if ( strncmp ( modelName, "PowerBook", 9 ) != 0 )
{
ERROR_LOG ( ( "IOATABlockStorageDevice::setProperties called on non-powerbook drive\n" ) );
status = kIOReturnUnsupported;
return status;
}
}
else
{
ERROR_LOG ( ( "IOATABlockStorageDevice::setProperties couldn't get model name\n" ) );
status = kIOReturnUnsupported;
return status;
}
dict = OSDynamicCast ( OSDictionary, properties );
if ( dict != NULL )
{
number = OSDynamicCast ( OSNumber, dict->getObject ( "APM Level" ) );
}
if ( number != NULL )
{
UInt8 apmLevel = number->unsigned8BitValue ( );
if ( ( apmLevel == 0 ) || ( apmLevel == 0xFF ) )
{
status = kIOReturnBadArgument;
return status;
}
STATUS_LOG ( ( "apmLevel = %d\n", apmLevel ) );
status = fProvider->setAdvancedPowerManagementLevel ( apmLevel, true );
}
else
{
status = kIOReturnBadArgument;
}
STATUS_LOG ( ( "IOATABlockStorageDevice::leave setProperties\n" ) );
return status;
}
//---------------------------------------------------------------------------
//
IOReturn
IOATABlockStorageDevice::doAsyncReadWrite ( IOMemoryDescriptor *buffer,
UInt64 block, UInt64 nblks,
IOStorageAttributes *attributes,
IOStorageCompletion *completion )
{
// Block incoming I/O if we have been terminated
if ( isInactive ( ) != false )
{
return kIOReturnNotAttached;
}
fProvider->checkPowerState ( );
return fProvider->doAsyncReadWrite ( buffer, (UInt32) block, (UInt32) nblks, *completion );
}
//---------------------------------------------------------------------------
//
IOReturn
IOATABlockStorageDevice::doSyncReadWrite ( IOMemoryDescriptor * buffer,
UInt32 block, UInt32 nblks )
{
// Block incoming I/O if we have been terminated
if ( isInactive ( ) != false )
{
return kIOReturnNotAttached;
}
fProvider->checkPowerState ( );
return fProvider->doSyncReadWrite ( buffer, block, nblks );
}
//---------------------------------------------------------------------------
//
IOReturn
IOATABlockStorageDevice::doEjectMedia ( void )
{
return fProvider->doEjectMedia ( );
}
//---------------------------------------------------------------------------
//
IOReturn
IOATABlockStorageDevice::doFormatMedia ( UInt64 byteCapacity )
{
return fProvider->doFormatMedia ( byteCapacity );
}
//---------------------------------------------------------------------------
//
UInt32
IOATABlockStorageDevice::doGetFormatCapacities ( UInt64 * capacities,
UInt32 capacitiesMaxCount ) const
{
return fProvider->doGetFormatCapacities ( capacities, capacitiesMaxCount );
}
//---------------------------------------------------------------------------
//
IOReturn
IOATABlockStorageDevice::doLockUnlockMedia ( bool doLock )
{
return fProvider->doLockUnlockMedia ( doLock );
}
//---------------------------------------------------------------------------
//
IOReturn
IOATABlockStorageDevice::doSynchronizeCache ( void )
{
// Block incoming I/O if we have been terminated
if ( isInactive ( ) != false )
{
return kIOReturnNotAttached;
}
fProvider->checkPowerState ( );
return fProvider->doSynchronizeCache ( );
}
//---------------------------------------------------------------------------
//
char *
IOATABlockStorageDevice::getVendorString ( void )
{
return fProvider->getVendorString ( );
}
//---------------------------------------------------------------------------
//
char *
IOATABlockStorageDevice::getProductString ( void )
{
return fProvider->getProductString ( );
}
//---------------------------------------------------------------------------
//
char *
IOATABlockStorageDevice::getRevisionString ( void )
{
return fProvider->getRevisionString ( );
}
//---------------------------------------------------------------------------
//
char *
IOATABlockStorageDevice::getAdditionalDeviceInfoString ( void )
{
return fProvider->getAdditionalDeviceInfoString ( );
}
//---------------------------------------------------------------------------
//
IOReturn
IOATABlockStorageDevice::reportBlockSize ( UInt64 * blockSize )
{
return fProvider->reportBlockSize ( blockSize );
}
//---------------------------------------------------------------------------
//
IOReturn
IOATABlockStorageDevice::reportEjectability ( bool * isEjectable )
{
return fProvider->reportEjectability ( isEjectable );
}
//---------------------------------------------------------------------------
//
IOReturn
IOATABlockStorageDevice::reportLockability ( bool * isLockable )
{
return fProvider->reportLockability ( isLockable );
}
//---------------------------------------------------------------------------
//
IOReturn
IOATABlockStorageDevice::reportPollRequirements ( bool * pollIsRequired,
bool * pollIsExpensive )
{
return fProvider->reportPollRequirements ( pollIsRequired, pollIsExpensive );
}
//---------------------------------------------------------------------------
//
IOReturn
IOATABlockStorageDevice::reportMaxReadTransfer ( UInt64 blockSize,
UInt64 * max )
{
return fProvider->reportMaxReadTransfer ( blockSize, max );
}
//---------------------------------------------------------------------------
//
IOReturn
IOATABlockStorageDevice::reportMaxValidBlock ( UInt64 * maxBlock )
{
return fProvider->reportMaxValidBlock ( maxBlock );
}
//---------------------------------------------------------------------------
//
IOReturn
IOATABlockStorageDevice::reportMaxWriteTransfer ( UInt64 blockSize,
UInt64 * max )
{
return fProvider->reportMaxWriteTransfer ( blockSize, max );
}
//---------------------------------------------------------------------------
//
IOReturn
IOATABlockStorageDevice::reportMediaState ( bool * mediaPresent,
bool * changed )
{
return fProvider->reportMediaState ( mediaPresent, changed );
}
//---------------------------------------------------------------------------
//
IOReturn
IOATABlockStorageDevice::reportRemovability ( bool * isRemovable )
{
return fProvider->reportRemovability ( isRemovable );
}
//---------------------------------------------------------------------------
//
IOReturn
IOATABlockStorageDevice::reportWriteProtection ( bool * isWriteProtected )
{
return fProvider->reportWriteProtection ( isWriteProtected );
}
//---------------------------------------------------------------------------
//
IOReturn
IOATABlockStorageDevice::getWriteCacheState ( bool * enabled )
{
// Block incoming I/O if we have been terminated
if ( isInactive ( ) != false )
{
return kIOReturnNotAttached;
}
return fProvider->getWriteCacheState ( enabled );
}
//---------------------------------------------------------------------------
//
IOReturn
IOATABlockStorageDevice::setWriteCacheState ( bool enabled )
{
// Block incoming I/O if we have been terminated
if ( isInactive ( ) != false )
{
return kIOReturnNotAttached;
}
return fProvider->setWriteCacheState ( enabled );
}
//---------------------------------------------------------------------------
//
IOReturn
IOATABlockStorageDevice::sendSMARTCommand ( IOATACommand * command )
{
// Block incoming I/O if we have been terminated
if ( isInactive ( ) != false )
{
return kIOReturnNotAttached;
}
return fProvider->sendSMARTCommand ( command );
}
//--------------------------------------------------------------------------------------
// End Of File
//--------------------------------------------------------------------------------------