-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIOATABlockStorageDriverPM.cpp
689 lines (485 loc) · 19.2 KB
/
IOATABlockStorageDriverPM.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
/*
* 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@
*/
/*
* ATA power management as defined in T13:1321-D Revision 3 2000.02.29,
* AT Attachment with Packet Interface - 5 (ATA/ATAPI-5), states that there
* are four (4) basic power states: Sleep, Standby, Idle, and Active. This
* implementation does not yet include support for the Advanced Power
* Management modes defined in the ATA-5 specification.
*
*/
// IOKit Power Management headers
#include <IOKit/pwr_mgt/RootDomain.h>
#include "IOATABlockStorageDriver.h"
#define super IOService
#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
enum
{
kThreadDelayInterval = 500, //( 0.5 second in ms )
k100SecondsInMicroSeconds = 100 * 1000 * 1000
};
enum
{
kATAIdleDevice = 1,
kATAStandbyDevice = 2,
kATASleepDevice = 3
};
enum
{
kPowerManagementScaleFactor = kSecondsInAMinute / ( kIOATAPowerStateActive - kIOATAPowerStateSleep )
};
//---------------------------------------------------------------------------
// Inform the policy-maker that an ATA hard-drive is capable of five power
// states. The sPowerStates[] array encodes information about each state
//---------------------------------------------------------------------------
static IOPMPowerState sPowerStates[kIOATAPowerStates] =
{
{ kIOPMPowerStateVersion1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* never entered voluntarily */
{ kIOPMPowerStateVersion1, 0, IOPMPowerOn, IOPMPowerOn, 0, 0, 0, 0, 0, 0, 0, 0 },
{ kIOPMPowerStateVersion1, (IOPMDeviceUsable | kIOPMPreventIdleSleep), IOPMPowerOn, IOPMPowerOn, 0, 0, 0, 0, 0, 0, 0, 0 },
{ kIOPMPowerStateVersion1, (IOPMDeviceUsable | kIOPMPreventIdleSleep), IOPMPowerOn, IOPMPowerOn, 0, 0, 0, 0, 0, 0, 0, 0 },
{ kIOPMPowerStateVersion1, (IOPMDeviceUsable | IOPMMaxPerformance | kIOPMPreventIdleSleep), IOPMPowerOn, IOPMPowerOn, 0, 0, 0, 0, 0, 0, 0, 0 }
};
#if ( ATA_MASS_STORAGE_DEBUG == 1 )
static const char * sPowerStateNames[] =
{
"System Sleep",
"Sleep",
"Standby",
"Idle",
"Active"
};
#endif /* ATA_MASS_STORAGE_DEBUG */
// Static prototypes
static IOReturn
IOATABlockStorageDriverPowerDownHandler ( void * target,
void * refCon,
UInt32 messageType,
IOService * provider,
void * messageArgument,
vm_size_t argSize );
//---------------------------------------------------------------------------
// ¥ IOATABlockStorageDriverPowerDownHandler - C->C++ Glue code for Power
// Down Notifications.
//---------------------------------------------------------------------------
static IOReturn
IOATABlockStorageDriverPowerDownHandler ( void * target,
void * refCon,
UInt32 messageType,
IOService * provider,
void * messageArgument,
vm_size_t argSize )
{
return ( ( IOATABlockStorageDriver_PD * ) target )->powerDownHandler (
refCon,
messageType,
provider,
messageArgument,
argSize );
}
//---------------------------------------------------------------------------
// ¥ PowerDownHandler - Method called at sleep/restart/shutdown time.
//---------------------------------------------------------------------------
IOReturn
IOATABlockStorageDriver_PD::powerDownHandler ( void * refCon,
UInt32 messageType,
IOService * provider,
void * messageArgument,
vm_size_t argSize )
{
IOReturn status = kIOReturnSuccess;
if ( messageType == kIOMessageSystemWillPowerOff )
setProperty ( "Power Off", true );
return status;
}
//---------------------------------------------------------------------------
// ¥ initialPowerStateForDomainState - Returns to the power manager what
// initial state the device should be in.
//---------------------------------------------------------------------------
unsigned long
IOATABlockStorageDriver_PD::initialPowerStateForDomainState (
IOPMPowerFlags flags )
{
// We ignore the flags since we are always active at startup time and we
// just report what our initial power state is.
return kIOATAPowerStateActive;
}
//---------------------------------------------------------------------------
// ¥ initForPM - Register the driver with our policy-maker
// (also in the same class).
//---------------------------------------------------------------------------
void
IOATABlockStorageDriver_PD::initForPM ( void )
{
SERIAL_STATUS_LOG ( ( "IOATABlockStorageDriver::initForPM called\n" ) );
registerPowerDriver ( this, sPowerStates, kIOATAPowerStates );
SERIAL_STATUS_LOG ( ( "registerPowerDriver\n" ) );
// Install handler for shutdown notifications
fPowerDownNotifier = registerPrioritySleepWakeInterest (
( IOServiceInterestHandler ) IOATABlockStorageDriverPowerDownHandler,
this );
changePowerStateTo ( kIOATAPowerStateSleep );
SERIAL_STATUS_LOG ( ( "changePowerStateTo\n" ) );
}
//---------------------------------------------------------------------------
// ¥ setAggressiveness - Policy-maker code which intercepts
// kPMMinutesToSpinDown settings and then call
// setIdleTimerPeriod() to adjust the idle timer.
//---------------------------------------------------------------------------
IOReturn
IOATABlockStorageDriver_PD::setAggressiveness ( UInt32 type, UInt32 minutes )
{
SERIAL_STATUS_LOG ( ( "IOATABlockStorageDriver::setAggressiveness called\n" ) );
if ( type == kPMMinutesToSpinDown )
{
SERIAL_STATUS_LOG ( ( "IOATABlockStorageDriver: setting idle timer to %ld min\n", minutes ) );
setIdleTimerPeriod ( minutes * kPowerManagementScaleFactor );
}
return ( super::setAggressiveness ( type, minutes ) );
}
//---------------------------------------------------------------------------
// ¥ checkPowerState - Checks if it is ok for I/O to come through. If it is
// not ok, then we block the thread.
//---------------------------------------------------------------------------
void
IOATABlockStorageDriver_PD::checkPowerState ( void )
{
SERIAL_STATUS_LOG ( ( "IOATABlockStorageDriver::checkPowerState called\n" ) );
activityTickle ( kIOPMSuperclassPolicy1, ( UInt32 ) kIOATAPowerStateActive );
fCommandGate->runAction ( ( IOCommandGate::Action )
&IOATABlockStorageDriver_PD::sHandleCheckPowerState );
}
//---------------------------------------------------------------------------
// ¥ sHandleCheckPowerState - Static routine which calls through to other
// side of command gate
//---------------------------------------------------------------------------
void
IOATABlockStorageDriver_PD::sHandleCheckPowerState ( IOATABlockStorageDriver_PD * self )
{
self->handleCheckPowerState ( );
}
//---------------------------------------------------------------------------
// ¥ handleCheckPowerState - Called on safe side of command gate to serialize
// access to the sleep related variables
//---------------------------------------------------------------------------
void
IOATABlockStorageDriver_PD::handleCheckPowerState ( void )
{
while ( fCurrentPowerState != kIOATAPowerStateActive )
{
fCommandGate->commandSleep ( &fCurrentPowerState, THREAD_UNINT );
}
fNumCommandsOutstanding++;
}
//---------------------------------------------------------------------------
// ¥ setPowerState - Set/Change the power state of the ATA hard-drive
//---------------------------------------------------------------------------
IOReturn
IOATABlockStorageDriver_PD::setPowerState (
UInt32 powerStateOrdinal,
IOService * whichDevice )
{
SERIAL_STATUS_LOG ( ( "IOATABlockStorageDriver::setPowerState called\n" ) );
SERIAL_STATUS_LOG ( ( "powerStateOrdinal = %ld\n", powerStateOrdinal ) );
fCommandGate->runAction ( ( IOCommandGate::Action )
&IOATABlockStorageDriver_PD::sHandleSetPowerState,
( void * ) powerStateOrdinal );
return k100SecondsInMicroSeconds;
}
//---------------------------------------------------------------------------
// ¥ sHandleSetPowerState - Static member function called by the
// IOCommandGate to translate setPowerState() calls
// to the synchronized handleSetPowerState() call
//---------------------------------------------------------------------------
void
IOATABlockStorageDriver_PD::sHandleSetPowerState (
IOATABlockStorageDriver_PD * self,
UInt32 powerStateOrdinal )
{
if ( self->isInactive ( ) == false )
{
self->handleSetPowerState ( powerStateOrdinal );
}
}
//---------------------------------------------------------------------------
// ¥ handleSetPowerState - Synchronized form of changing a power state
//---------------------------------------------------------------------------
void
IOATABlockStorageDriver_PD::handleSetPowerState ( UInt32 powerStateOrdinal )
{
AbsoluteTime time;
fProposedPowerState = powerStateOrdinal;
if ( ( fPowerTransitionInProgress == false ) || fPowerAckInProgress )
{
// mark us as being in progress, then call the thread which is
// the power management state machine
fPowerTransitionInProgress = true;
clock_interval_to_deadline ( kThreadDelayInterval, kMillisecondScale, &time );
( void ) thread_call_enter_delayed ( fPowerManagementThread, time );
}
}
//---------------------------------------------------------------------------
// ¥ sPowerManagement - Called on its own thread to handle power management
//---------------------------------------------------------------------------
void
IOATABlockStorageDriver_PD::sPowerManagement ( thread_call_param_t whichDevice )
{
IOATABlockStorageDriver_PD * self;
self = ( IOATABlockStorageDriver_PD * ) whichDevice;
if ( ( self != NULL ) && ( self->isInactive ( ) == false ) )
{
self->retain ( );
self->handlePowerChange ( );
self->fPowerAckInProgress = true;
self->acknowledgeSetPowerState ( );
self->fPowerAckInProgress = false;
self->fPowerTransitionInProgress = false;
self->release ( );
}
else
{
self->fPowerTransitionInProgress = false;
}
}
//---------------------------------------------------------------------------
// ¥ handlePowerChange - Handles the state machine for power management
//---------------------------------------------------------------------------
void
IOATABlockStorageDriver_PD::handlePowerChange ( void )
{
UInt32 count = 0;
SERIAL_STATUS_LOG ( ( "IOATABlockStorageDriver::handlePowerChange called\n" ) );
while ( fProposedPowerState != fCurrentPowerState )
{
// If we've gone through this loop a ton of times, there's something
// wrong, so break out to avoid an infinite loop.
if ( count > 32 )
{
break;
}
// We look at what state has been proposed. In some cases, the device
// may be asleep and we need to hit it with a reset before issuing
// the other power change command. The reset brings the device back
// into standby mode.
switch ( fProposedPowerState )
{
case kIOATAPowerStateSystemSleep:
if ( fCurrentPowerState == kIOATAPowerStateSleep )
{
fCurrentPowerState = kIOATAPowerStateSystemSleep;
break;
}
// else intentionally fall through to kIOATAPowerStateSleep
case kIOATAPowerStateSleep:
{
bool resetOccurred = false;
// We never have to check if we are in sleep here since that
// is handled by the while loop check. So we can always just
// send the sleep command straight on through.
fCurrentPowerState = fProposedPowerState;
fCommandGate->runAction ( ( IOCommandGate::Action )
&IOATABlockStorageDriver_PD::sSetWakeupResetOccurred,
( void * ) resetOccurred );
while ( fNumCommandsOutstanding != 0 )
{
// Sleep this thread for 1ms until all outstanding commands
// have completed. This prevents a sleep command from entering
// the queue before an I/O and causing a problem on wakeup.
IOSleep ( 1 );
}
( void ) issuePowerTransition ( kATASleepDevice );
}
break;
case kIOATAPowerStateStandby:
{
if ( fCurrentPowerState <= kIOATAPowerStateSleep )
{
// issue the reset command
( void ) resetATADevice ( );
fCurrentPowerState = kIOATAPowerStateStandby;
}
else if ( fCurrentPowerState != kIOATAPowerStateStandby )
{
// We have found several drives that will hang the bus when going from
// kATAStandbyDevice or kATAIdleDevice to kATASleepDevice. For now
// to be safe we are going to simplify power transitions to kATASleepDevice
// ( void ) issuePowerTransition ( kATAStandbyDevice );
fCurrentPowerState = kIOATAPowerStateStandby;
}
}
break;
case kIOATAPowerStateIdle:
{
if ( fCurrentPowerState <= kIOATAPowerStateSleep )
{
// issue the reset command
( void ) resetATADevice ( );
fCurrentPowerState = kIOATAPowerStateStandby;
}
if ( fCurrentPowerState != kIOATAPowerStateIdle )
{
// We have found several drives that will hang the bus when going from
// kATAStandbyDevice or kATAIdleDevice to kATASleepDevice. For now
// to be safe we are going to simplify power transitions to kATASleepDevice
// ( void ) issuePowerTransition ( kATAIdleDevice );
fCurrentPowerState = kIOATAPowerStateIdle;
}
}
break;
case kIOATAPowerStateActive:
{
if ( fCurrentPowerState <= kIOATAPowerStateSleep )
{
bool resetOccurred = false;
fCommandGate->runAction ( ( IOCommandGate::Action )
&IOATABlockStorageDriver_PD::sCheckWakeupResetOccurred,
( void * ) &resetOccurred );
// Check to see if a reset already occurred. Since there might be
// a case where master/slave causes the reset to occur for both
// devices, we don't want to do two resets on the bus.
if ( resetOccurred == false )
{
( void ) resetATADevice ( );
}
}
// We lie and say we are active, even though
// we are in standby mode after a reset. It doesn't
// really matter, since the next I/O will put
// us in active mode. This avoids doing an I/O
// and discarding the result.
fCurrentPowerState = kIOATAPowerStateActive;
// wake up any pending I/O
fCommandGate->commandWakeup ( &fCurrentPowerState, false );
}
break;
default:
PANIC_NOW ( ( "Undefined power state issued\n" ) );
break;
}
count++;
}
}
//---------------------------------------------------------------------------
// ¥ issuePowerTransition - Issues a power change command
//---------------------------------------------------------------------------
IOReturn
IOATABlockStorageDriver_PD::issuePowerTransition ( UInt32 function )
{
IOReturn status = kIOReturnSuccess;
IOSyncer * syncer;
// Check if the device is CompactFlash. We wonÕt do power transitions for
// CF since they don't seem to like them.
if ( fATASocketType == kPCCardSocket )
{
return status;
}
syncer = ( IOSyncer * ) fPowerManagementCommand->refCon;
// Zero the command
fPowerManagementCommand->zeroCommand ( );
fPowerManagementCommand->setUnit ( fATAUnitID );
fPowerManagementCommand->setTimeoutMS ( kATATimeout10Seconds );
fPowerManagementCommand->setDevice_Head ( fATAUnitID << 4 );
fPowerManagementCommand->setOpcode ( kATAFnExecIO );
fPowerManagementCommand->setCallbackPtr (
&IOATABlockStorageDriver_PD::sHandleSimpleSyncTransaction );
switch ( function )
{
case kATAIdleDevice:
fPowerManagementCommand->setCommand ( kATAcmdIdleImmed );
break;
case kATAStandbyDevice:
fPowerManagementCommand->setCommand ( kATAcmdStandbyImmed );
break;
case kATASleepDevice:
fPowerManagementCommand->setCommand ( kATAcmdSleep );
break;
default:
PANIC_NOW ( ( "Invalid command in IOATABlockStorageDriver::issuePowerTransition\n" ) );
break;
}
fATADevice->executeCommand ( fPowerManagementCommand );
status = syncer->wait ( false );
syncer->reinit ( );
return status;
}
//--------------------------------------------------------------------------------------
// ¥ setWakeupResetOccurred - Called on safe side of command gate to set state
//--------------------------------------------------------------------------------------
void
IOATABlockStorageDriver_PD::setWakeupResetOccurred ( bool resetOccurred )
{
fWakeUpResetOccurred = resetOccurred;
}
//--------------------------------------------------------------------------------------
// ¥ sSetWakeupResetOccurred - Called on safe side of command gate
//--------------------------------------------------------------------------------------
void
IOATABlockStorageDriver_PD::sSetWakeupResetOccurred ( IOATABlockStorageDriver_PD * driver,
bool resetOccurred )
{
driver->setWakeupResetOccurred ( resetOccurred );
}
//--------------------------------------------------------------------------------------
// ¥ checkWakeupResetOccurred - Called on safe side of command gate to find out if
// the driver has already received a reset message
//--------------------------------------------------------------------------------------
bool
IOATABlockStorageDriver_PD::checkWakeupResetOccurred ( void )
{
return fWakeUpResetOccurred;
}
//--------------------------------------------------------------------------------------
// ¥ sCheckWakeupResetOccurred - Called on safe side of command gate
//--------------------------------------------------------------------------------------
void
IOATABlockStorageDriver_PD::sCheckWakeupResetOccurred ( IOATABlockStorageDriver_PD * driver,
void * resetOccurred )
{
bool * reset = ( bool * ) resetOccurred;
*reset = driver->checkWakeupResetOccurred ( );
return;
}
//---------------------------------------------------------------------------
// End Of File
//---------------------------------------------------------------------------