This repository has been archived by the owner on Jul 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIOPCIATA.cpp
651 lines (476 loc) · 16.3 KB
/
IOPCIATA.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
/*
* Copyright (c) 2000-2008 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* The contents of this file constitute Original Code as defined in and
* are subject to the Apple Public Source License Version 1.1 (the
* "License"). You may not use this file except in compliance with the
* License. Please obtain a copy of the License at
* http://www.apple.com/publicsource and read it before using this file.
*
* This 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 OR NON-INFRINGEMENT. Please see the
* License for the specific language governing rights and limitations
* under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/*
*
* IOPCIATA.cpp
*
* class defining the portions of PCI-ATA class controllers which
* are generally common to most PCI ATA bus controllers, such as DMA
* programming.
*
*/
#include <IOKit/IOTypes.h>
#include "IOATATypes.h"
#include "IOATAController.h"
#include "IOATADevice.h"
#include "IOATABusInfo.h"
#include "IOATADevConfig.h"
#include <IOKit/IOMemoryCursor.h>
#include <libkern/OSByteOrder.h>
#include <libkern/OSAtomic.h>
#include "IOPCIATA.h"
#include "IOATABusCommand.h"
#ifdef DLOG
#undef DLOG
#endif
//#define ATA_DEBUG 1
#ifdef ATA_DEBUG
#define DLOG(fmt, args...) IOLog(fmt, ## args)
#else
#define DLOG(fmt, args...)
#endif
// some day, we'll have an ATA recorder for IOKit
#define ATARecordEventMACRO(type,param,bus,data) (void) (type); (void) (param); (void) (bus); (void) (data)
#pragma mark -IOService Overrides -
// 33 prd descriptors are normally required to satisfy a
// maximum ATA transaction of 256 * 512 byte sectors.
// however due to restrictions on PCI style DMA engines,
// the number of descriptors must increase to something more
// than that.
#define kATAXferDMADesc 64
#define kATAMaxDMADesc kATAXferDMADesc
// up to 256 ATA sectors per transfer
#define kMaxATAXfer 512 * 256
#define _prdBuffer reserved->_prdBuffer
//---------------------------------------------------------------------------
#define super IOATAController
OSDefineMetaClass( IOPCIATA, IOATAController )
OSDefineAbstractStructors( IOPCIATA, IOATAController )
OSMetaClassDefineReservedUnused(IOPCIATA, 0);
OSMetaClassDefineReservedUnused(IOPCIATA, 1);
OSMetaClassDefineReservedUnused(IOPCIATA, 2);
OSMetaClassDefineReservedUnused(IOPCIATA, 3);
OSMetaClassDefineReservedUnused(IOPCIATA, 4);
OSMetaClassDefineReservedUnused(IOPCIATA, 5);
OSMetaClassDefineReservedUnused(IOPCIATA, 6);
OSMetaClassDefineReservedUnused(IOPCIATA, 7);
OSMetaClassDefineReservedUnused(IOPCIATA, 8);
OSMetaClassDefineReservedUnused(IOPCIATA, 9);
OSMetaClassDefineReservedUnused(IOPCIATA, 10);
OSMetaClassDefineReservedUnused(IOPCIATA, 11);
OSMetaClassDefineReservedUnused(IOPCIATA, 12);
OSMetaClassDefineReservedUnused(IOPCIATA, 13);
OSMetaClassDefineReservedUnused(IOPCIATA, 14);
OSMetaClassDefineReservedUnused(IOPCIATA, 15);
OSMetaClassDefineReservedUnused(IOPCIATA, 16);
OSMetaClassDefineReservedUnused(IOPCIATA, 17);
OSMetaClassDefineReservedUnused(IOPCIATA, 18);
OSMetaClassDefineReservedUnused(IOPCIATA, 19);
OSMetaClassDefineReservedUnused(IOPCIATA, 20);
//---------------------------------------------------------------------------
bool
IOPCIATA::init(OSDictionary * properties)
{
DLOG("IOPCIATA::init() starting\n");
// Initialize instance variables.
_bmCommandReg = 0;
_bmStatusReg = 0;
_bmPRDAddresReg = 0;
_prdTable = 0;
_prdTablePhysical = 0;
_DMACursor = 0;
_dmaState = IOPCIATA::kATADMAInactive;
if (super::init(properties) == false)
{
DLOG("IOPCIATA: super::init() failed\n");
return false;
}
DLOG("IOPCIATA::init() done\n");
return true;
}
/*---------------------------------------------------------------------------
*
* Override IOService start.
*
* Subclasses should override the start method, call the super::start
* first then add interrupt sources and probe their busses for devices
* and create device nubs as needed.
---------------------------------------------------------------------------*/
bool
IOPCIATA::start(IOService *provider)
{
DLOG("IOPCIATA::start() begin\n");
// call start on the superclass
if (!super::start( provider))
{
DLOG("IOPCIATA: super::start() failed\n");
return false;
}
reserved = ( ExpansionData * ) IOMalloc ( sizeof ( ExpansionData ) );
if ( !reserved )
return false;
bzero ( reserved, sizeof ( ExpansionData ) );
// Allocate the DMA descriptor area
if( ! allocDMAChannel() )
{
DLOG("IOPCIATA: allocDMAChannel failed\n");
return false;
}
DLOG("IOPCIATA::start() done\n");
return true;
}
/*---------------------------------------------------------------------------
* free() - the pseudo destructor. Let go of what we don't need anymore.
*
*
---------------------------------------------------------------------------*/
void
IOPCIATA::free()
{
freeDMAChannel();
if ( reserved )
{
IOFree ( reserved, sizeof ( ExpansionData ) );
reserved = NULL;
}
super::free();
}
#pragma mark -initialization-
/*---------------------------------------------------------------------------
*
* allocate memory and resources for the DMA descriptors.
*
*
---------------------------------------------------------------------------*/
bool
IOPCIATA::allocDMAChannel(void)
{
if( _bmCommandReg == 0
|| _bmStatusReg == 0
|| _bmPRDAddresReg == 0 )
{
DLOG("IOPCIATA bm regs not initialised.\n");
return false;
}
_prdBuffer = IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
kernel_task,
kIODirectionInOut | kIOMemoryPhysicallyContiguous,
sizeof(PRD) * kATAMaxDMADesc,
0xFFFFFFF0UL );
if ( !_prdBuffer )
{
IOLog("%s: PRD buffer allocation failed\n", getName());
return false;
}
_prdBuffer->prepare ( );
_prdTable = (PRD *) _prdBuffer->getBytesNoCopy();
_prdTablePhysical = _prdBuffer->getPhysicalAddress();
_DMACursor = IONaturalMemoryCursor::withSpecification(0x10000, /*64K*/
kMaxATAXfer /* 256 * 512 */
/*inAlignment - Memory descriptors and cursors don't support alignment
flags yet. */);
if( ! _DMACursor )
{
freeDMAChannel();
DLOG("IOPCIATA alloc DMACursor failed\n");
return false;
}
// fill the chain with stop commands to initialize it.
initATADMAChains(_prdTable);
return true;
}
/*---------------------------------------------------------------------------
*
* deallocate memory and resources for the DMA descriptors.
*
*
---------------------------------------------------------------------------*/
bool
IOPCIATA::freeDMAChannel(void)
{
if( _prdBuffer )
{
// make sure the engine is stopped
stopDMA();
// free the descriptor table.
_prdBuffer->complete();
_prdBuffer->release();
_prdBuffer = NULL;
_prdTable = NULL;
_prdTablePhysical = 0;
}
if( _DMACursor )
{
_DMACursor->release();
_DMACursor = NULL;
}
return true;
}
#pragma mark -DMA Interface-
/*---------------------------------------------------------------------------
*
* Subclasses should take necessary action to create DMA channel programs,
* for the current memory descriptor in _currentCommand and activate the
* the DMA hardware
---------------------------------------------------------------------------*/
IOReturn
IOPCIATA::startDMA( void )
{
IOReturn err = kATANoErr;
// first make sure the engine is stopped.
stopDMA();
// reality check the memory descriptor in the current command
// state flag
_dmaState = kATADMAStarting;
// create the channel commands
err = createChannelCommands();
if( err )
{
DLOG("IOPCIATA error createChannelCmds err = %ld\n", (long int)err);
stopDMA();
return err;
}
// fire the engine
activateDMAEngine();
return err;
}
/*---------------------------------------------------------------------------
* Subclasses should take all actions necesary to safely shutdown DMA engines
* in any state of activity, whether finished, pending or stopped. Calling
* this function must be harmless reguardless of the state of the engine.
*
---------------------------------------------------------------------------*/
IOReturn
IOPCIATA::stopDMA( void )
{
if(_dmaState != kATADMAInactive)
shutDownATADMA();
_dmaState = kATADMAInactive;
return kATANoErr;
}
#pragma mark -DMA Implementation-
//----------------------------------------------------------------------------------------
// Function: InitATADMAChains
// Description: Initializes the chains with STOP commands.
//
// Input: Pointer to the DBDMA descriptor area: descPtr
//
// Output: None
//----------------------------------------------------------------------------------------
void
IOPCIATA::initATADMAChains (PRD* descPtr)
{
UInt32 i;
/* Initialize the data-transfer PRD channel command descriptors. */
for (i = 0; i < kATAMaxDMADesc; i++)
{
descPtr->bufferPtr = 0;
descPtr->byteCount = 1;
// set the stop DMA bit on the last transaction.
descPtr->flags = OSSwapHostToLittleInt16( (UInt16) kLast_PRD);
descPtr++;
}
}
/*----------------------------------------------------------------------------------------
// Function: stopDMAEngine
// Description: Stops the DMA engine itself. on the ATA bus
// Input: none
// Output: None
----------------------------------------------------------------------------------------*/
void
IOPCIATA::stopDMAEngine(void)
{
OSSynchronizeIO();
*_bmCommandReg = mBMCmdStop;
}
/*----------------------------------------------------------------------------------------
// Function: activateDMAEngine
// Description: Activate the dma engine on the ATA bus associated with current device.
engine will begin executing the command chain already programmed.
// Input: None
// Output: None
//----------------------------------------------------------------------------------------*/
void
IOPCIATA::activateDMAEngine(void)
{
DLOG("IOPCIATA prd table is at: %lx\n", _prdTablePhysical);
// clear error bit prior to starting.
*_bmStatusReg = (UInt8) mBMStatusError | mBMStatusInt | (_currentCommand->getUnit() == 0 ? mBMStatusDrv0 : mBMStatusDrv1) ;
OSSynchronizeIO();
// set the address pointer.
*_bmPRDAddresReg = OSSwapHostToLittleInt32((UInt32) _prdTablePhysical);
OSSynchronizeIO();
// active the DMA engine.
UInt8 theCommand = (_currentCommand->getFlags() & mATAFlagIORead) ? mBMCmdStartInput : mBMCmdStartOutput;
DLOG("IOPCIATA: bmCommand is %X\n", theCommand);
*_bmCommandReg = theCommand;
OSSynchronizeIO();
DLOG("IOPCIATA: bmStaus is %X\n", *_bmStatusReg);
}
//----------------------------------------------------------------------------------------
// Function: ShutDownATADMA
// Description: Stops the dma engine on the current ATA bus.
// This routine is used to stop the DMA
// such as may be desired during error recovery.
// Input: None
// Output: None
//----------------------------------------------------------------------------------------
void
IOPCIATA::shutDownATADMA (void)
{
// set the state semaphore
_dmaState = kATADMAInactive;
stopDMAEngine();
}
/********************************************************************************
* *
* s e t P R D *
* *
*********************************************************************************
*
* Purpose: Fills in the "Physical Region Descriptor" with the correct
* endian-ness
*
* Input: bffr - pointer to data
* count - of bytes in this data buffer
* tableElement - points to PRD to use
*
* Output: PRD - filled in
*
********************************************************************************/
void
IOPCIATA::setPRD(UInt8 *bffr, UInt16 count, PRD *tableElement, UInt16 end)
{
DLOG("IOPCIATA set PRD ptr = %lx count = %x flags = %x\n", (long) bffr, count, end);
tableElement->bufferPtr = OSSwapHostToLittleInt32((UInt32)(uintptr_t)bffr);
tableElement->byteCount = OSSwapHostToLittleInt16(count);
tableElement->flags = OSSwapHostToLittleInt16(end);
}
/*---------------------------------------------------------------------------
*
* create the DMA channel commands.
*
*
---------------------------------------------------------------------------*/
IOReturn
IOPCIATA::createChannelCommands(void)
{
IOMemoryDescriptor* descriptor = _currentCommand->getBuffer();
IOMemoryCursor::PhysicalSegment physSegment;
UInt32 index = 0;
UInt8 *xferDataPtr, *ptr2EndData, *next64KBlock, *starting64KBlock;
UInt32 xferCount, count2Next64KBlock;
if( ! descriptor )
{
DLOG("IOPCIATA nil buffer!\n");
return -1;
}
// This form of DMA engine can only do 1 pass. It cannot execute multiple chains.
// calculate remaining bytes in this transfer
IOByteCount bytesRemaining = _currentCommand->getByteCount() ;
// calculate position pointer
IOByteCount xfrPosition = _currentCommand->getPosition() ;
IOByteCount transferSize = 0;
// There's a unique problem with pci-style controllers, in that each dma transaction is not allowed to
// cross a 64K boundary. This leaves us with the yucky task of picking apart any descriptor segments that
// cross such a boundary ourselves.
while( _DMACursor->getPhysicalSegments(
descriptor,
xfrPosition,
&physSegment,
1,
bytesRemaining, // limit to the requested number of bytes in the event the descriptors is larger
&transferSize) )
{
xferDataPtr = (UInt8*) physSegment.location;
xferCount = physSegment.length;
// update the bytes remaining after this pass
bytesRemaining -= xferCount;
xfrPosition += xferCount;
// now we have to examine the segment to see whether it crosses (a) 64k boundary(s)
starting64KBlock = (UInt8*) ( (uintptr_t) xferDataPtr & 0xffff0000);
ptr2EndData = xferDataPtr + xferCount;
next64KBlock = (starting64KBlock + 0x10000);
// loop until this physical segment is fully accounted for.
// it is possible to have a memory descriptor which crosses more than one 64K boundary in a
// single span.
while( xferCount > 0 )
{
if (ptr2EndData > next64KBlock)
{
count2Next64KBlock = next64KBlock - xferDataPtr;
if (index < kATAMaxDMADesc)
{
setPRD(xferDataPtr, (UInt16)count2Next64KBlock, &_prdTable[index], kContinue_PRD);
xferDataPtr = next64KBlock;
next64KBlock += 0x10000;
xferCount -= count2Next64KBlock;
index++;
} else {
DLOG("IOPCIATA dma too big, PRD table exhausted A.\n");
_dmaState = kATADMAError;
return -1;
}
} else {
if (index < kATAMaxDMADesc)
{
setPRD(xferDataPtr, (UInt16) xferCount, &_prdTable[index], (bytesRemaining == 0) ? kLast_PRD : kContinue_PRD);
xferCount = 0;
index++;
} else {
DLOG("IOPCIATA dma too big, PRD table exhausted B.\n");
_dmaState = kATADMAError;
return -1;
}
}
}
} // end of segment counting loop.
// transfer is satisfied and only need to check status on interrupt.
_dmaState = kATADMAStatus;
DLOG("IOPCIATA PRD chain end %ld \n", index);
// chain is now ready for execution.
return kATANoErr;
}
/*---------------------------------------------------------------------------
*
* handleDeviceInterrupt - overriden here so we can make sure that the DMA has
* processed in the event first.
*
---------------------------------------------------------------------------*/
IOReturn
IOPCIATA::handleDeviceInterrupt(void)
{
if( _dmaState == kATADMAStatus )
{
OSSynchronizeIO();
UInt8 bmStatus = *_bmStatusReg;
if( bmStatus & mBMStatusError )
{
_dmaState = kATADMAError;
} else {
_currentCommand->setActualTransfer(_currentCommand->getByteCount());
_dmaState = kATADMAComplete;
}
stopDMA();
}
return super::handleDeviceInterrupt();
}