1/* 2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 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. 11 * 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 18 * under the License. 19 * 20 * @APPLE_LICENSE_HEADER_END@ 21 */ 22 23/* 24 * 25 * ATADeviceNub.cpp 26 * 27 */ 28 29 30#include <IOKit/IOTypes.h> 31#include "IOATATypes.h" 32#include "IOATADevice.h" 33#include "IOATAController.h" 34#include "ATADeviceNub.h" 35#include "IOATADevConfig.h" 36 37#include <IOKit/IOSyncer.h> 38 39enum{ 40 41 kDoIDDataComplete, 42 kDoSetFeatureComplete 43}; 44 45 46struct completionInfo{ 47 48 UInt32 whatToDo; 49 IOSyncer* sync; 50 51}; 52 53#ifdef DLOG 54#undef DLOG 55#endif 56 57#ifdef ATA_DEBUG 58#define DLOG(fmt, args...) IOLog(fmt, ## args) 59#else 60#define DLOG(fmt, args...) 61#endif 62 63 64#define kIDBufferBytes 512 65 66//--------------------------------------------------------------------------- 67 68#define super IOATADevice 69 70OSDefineMetaClassAndStructors( ATADeviceNub, IOATADevice ) 71 OSMetaClassDefineReservedUnused(ATADeviceNub, 0); 72 OSMetaClassDefineReservedUnused(ATADeviceNub, 1); 73 OSMetaClassDefineReservedUnused(ATADeviceNub, 2); 74 OSMetaClassDefineReservedUnused(ATADeviceNub, 3); 75 OSMetaClassDefineReservedUnused(ATADeviceNub, 4); 76 OSMetaClassDefineReservedUnused(ATADeviceNub, 5); 77 OSMetaClassDefineReservedUnused(ATADeviceNub, 6); 78 OSMetaClassDefineReservedUnused(ATADeviceNub, 7); 79 OSMetaClassDefineReservedUnused(ATADeviceNub, 8); 80 OSMetaClassDefineReservedUnused(ATADeviceNub, 9); 81 OSMetaClassDefineReservedUnused(ATADeviceNub, 10); 82 OSMetaClassDefineReservedUnused(ATADeviceNub, 11); 83 OSMetaClassDefineReservedUnused(ATADeviceNub, 12); 84 OSMetaClassDefineReservedUnused(ATADeviceNub, 13); 85 OSMetaClassDefineReservedUnused(ATADeviceNub, 14); 86 OSMetaClassDefineReservedUnused(ATADeviceNub, 15); 87 OSMetaClassDefineReservedUnused(ATADeviceNub, 16); 88 OSMetaClassDefineReservedUnused(ATADeviceNub, 17); 89 OSMetaClassDefineReservedUnused(ATADeviceNub, 18); 90 OSMetaClassDefineReservedUnused(ATADeviceNub, 19); 91 OSMetaClassDefineReservedUnused(ATADeviceNub, 20); 92 93//--------------------------------------------------------------------------- 94 95 96 97// static creator function - used by IOATAControllers to create nubs. 98ATADeviceNub* 99ATADeviceNub::ataDeviceNub( IOATAController* provider, ataUnitID unit, ataDeviceType devType) 100{ 101 102 ATADeviceNub* nub = new ATADeviceNub; 103 104 if( ! nub ) 105 return 0L; 106 107 if( !nub->init( provider, unit, devType) ) 108 { 109 nub->release(); 110 return 0L; 111 } 112 return nub; 113 114} 115 116 117//--------------------------------------------------------------------------- 118 119bool 120ATADeviceNub::init(IOATAController* provider, ataUnitID unit, ataDeviceType devType) 121{ 122 123 if( !super::init( (OSDictionary*) 0L) ) 124 return false; 125 126 _provider = provider; 127 _unitNumber = unit; 128 _deviceType = devType; 129 130 // allocate a buffer for the identify info from the device 131 buffer = (UInt8*) IOMalloc( kIDBufferBytes ); 132 133 if( !buffer ) 134 return false; 135 136 IOReturn err = kATANoErr; 137 138 // issue the identify command so we can get the vendor strings 139 err = getDeviceID(); 140 141 if( err ) 142 { 143 DLOG("ATADeviceNub failed identify device %ld\n", (long int) err); 144 145 IOFree( buffer, kIDBufferBytes); 146 return false; 147 } 148 149 publishProperties(); 150 publishBusProperties(); 151 publishVendorProperties(); 152 153 IOFree( buffer, kIDBufferBytes); 154 buffer = 0L; 155 156 return true; 157 158} 159 160 161 162//--------------------------------------------------------------------------- 163 164 165//--------------------------------------------------------------------------- 166bool 167ATADeviceNub::attach(IOService* provider ) 168{ 169 170 IOATAController* controller = OSDynamicCast( IOATAController, provider); 171 172 if( !controller ) 173 { 174 DLOG("ATANub: Provider not IOATAController\n"); 175 return false; 176 } 177 178 179 if( !super::attach( provider) ) 180 return false; 181 182 183 return true; 184 185} 186 187 188 189//--------------------------------------------------------------------------- 190 191// create and destroy IOATACommands 192//--------------------------------------------------------------------------- 193 194IOATACommand* 195ATADeviceNub::allocCommand( void ) 196{ 197 198 IOATABusCommand64* cmd = IOATABusCommand64::allocateCmd32(); 199 200 return (IOATACommand*) cmd; 201 202} 203 204//--------------------------------------------------------------------------- 205 206//--------------------------------------------------------------------------- 207void 208ATADeviceNub::freeCommand( IOATACommand* inCommand) 209{ 210 211 inCommand->release(); 212} 213 214 215 216 217//--------------------------------------------------------------------------- 218 219 220//--------------------------------------------------------------------------- 221 222// Submit IO requests 223IOReturn 224ATADeviceNub::executeCommand(IOATACommand* command) 225{ 226 227 IOSyncer* mySync = 0L; 228 IOATABusCommand* cmd = OSDynamicCast( IOATABusCommand, command); 229 230 if( !cmd ) 231 return -1; 232 233 if( cmd->getCallbackPtr() == 0L) 234 { 235 mySync = IOSyncer::create(); 236 cmd->syncer = mySync; 237 238 } 239 240 IOReturn err = _provider->executeCommand( this, cmd); 241 242 if( mySync ) 243 { 244 mySync->wait(); 245 err = cmd->getResult(); 246 } 247 248 return err; 249 250} 251 252 253 254/*--------------------------------------------------------------------------- 255 256 257 258 259---------------------------------------------------------------------------*/ 260IOReturn 261ATADeviceNub::getDeviceID( void ) 262{ 263 OSString* string; 264 265 IOMemoryDescriptor* desc = IOMemoryDescriptor::withAddress((void *) buffer, 266 kIDBufferBytes, 267 kIODirectionIn); 268 269 if( !desc ) 270 { 271 272 string = OSString::withCString( "failed" ); 273 setProperty( "Alloc descriptor", (OSObject *)string ); 274 string->release(); 275 return -1; 276 277 } 278 279 IOATABusCommand* cmd = (IOATABusCommand*) allocCommand(); 280 281 if(!cmd) 282 { 283 284 string = OSString::withCString( "failed" ); 285 setProperty( "Alloc command", (OSObject *)string ); 286 string->release(); 287 return -1; 288 } 289 290 291 // tell the bus what to do, what unit and how long to allow 292 cmd->setOpcode( kATAFnExecIO); 293 cmd->setFlags(mATAFlagIORead); 294 cmd->setUnit( _unitNumber ); 295 cmd->setTimeoutMS( 30000); 296 297 // setup the buffer for the data 298 cmd->setBuffer ( desc); 299 cmd->setPosition ((IOByteCount) 0); 300 cmd->setByteCount ((IOByteCount) kIDBufferBytes); 301 302 303 // setup the actual taskfile params for the device 304 // only two parameters are needed, the device bit for the unit 305 // and the actual command for the device to execute 306 cmd->setDevice_Head( ((UInt8)_unitNumber) << 4); 307 308 if(_deviceType == kATADeviceType) 309 { 310 cmd->setCommand ( kATAcmdDriveIdentify ); 311 312 } else { 313 314 cmd->setCommand ( 0xA1 ); // packet identify 315 } 316 317 // set up a call back pointer for the command to complete. 318 // the IOATAController only allows async commands 319 320 cmd->setCallbackPtr ( (IOATACompletionFunction*) MyATACallback); 321 322 323 // set the refCon so the callback knows what to do. 324 completionInfo* completion = (completionInfo*)IOMalloc(sizeof(completionInfo)); 325 completion->whatToDo = kDoIDDataComplete; 326 completion->sync = IOSyncer::create(); 327 cmd->refCon = (void*) completion; 328 cmd->refCon2 = (void*) this; 329 330 desc->prepare(kIODirectionIn); 331 // tell the bus to exec the command 332 DLOG("Sending ID command to bus controller\n"); 333 IOReturn err = executeCommand( cmd); 334 DLOG("Command returned error = %ld\n",(long int)err ); 335 if(!err) 336 { 337 completion->sync->wait(); 338 } 339 340 desc->complete( kIODirectionIn ); 341 342 IOFree( completion, sizeof(completionInfo)); 343 344 if( cmd->getResult() ) 345 { 346 err = cmd->getResult(); 347 } 348 349 freeCommand(cmd); 350 351#if defined(__BIG_ENDIAN__) 352// The identify device info needs to be byte-swapped on ppc (big-endian) 353// systems becuase it is data that is produced by the drive, read across a 354// 16-bit little-endian PCI interface, directly into a big-endian system. 355// Regular data doesn't need to be byte-swapped because it is written and 356// read from the host and is intrinsically byte-order correct. 357 swapBytes16( buffer, kIDBufferBytes); 358#else /* __LITTLE_ENDIAN__ */ 359 // Swap the strings in the identify data. 360 swapBytes16( &buffer[46], 8); // Firmware revision 361 swapBytes16( &buffer[54], 40); // Model number 362 swapBytes16( &buffer[20], 20); // Serial number 363#endif 364 365 return err; 366 367 // the 512 byte buffer should contain the correctly byte-ordered 368 // raw identity info from the device at this point. 369} 370 371 372 373//--------------------------------------------------------------------------- 374 375//--------------------------------------------------------------------------- 376 377void 378ATADeviceNub::publishBusProperties( void ) 379{ 380 381 OSString* string; 382// OSNumber* number; 383 384 // get some bus info 385 386 IOATABusInfo* theInfo = IOATABusInfo::atabusinfo(); 387 if( !theInfo ) 388 { 389 DLOG("ATANub IOATABusInfo alloc fail\n"); 390 391 return; 392 } 393 394 if(_provider->provideBusInfo( theInfo )) 395 { 396 // blow it off on error 397 DLOG("ATANub provide info failed\n"); 398 theInfo->release(); 399 return; 400 } 401 402 switch( theInfo->getSocketType() ) 403 { 404 case kInternalATASocket: 405 string = OSString::withCString( kATAInternalSocketString ); 406 break; 407 408 case kMediaBaySocket: 409 string = OSString::withCString( kATAMediaBaySocketString ); 410 break; 411 412 case kPCCardSocket: 413 string = OSString::withCString( kATAPCCardSocketString ); 414 break; 415 416 case kInternalSATA: 417 string = OSString::withCString( kATAInternalSATAString ); 418 break; 419 420 case kSATABay: 421 string = OSString::withCString( kATASATABayString ); 422 break; 423 424 case kInternalSATA2: 425 string = OSString::withCString( kATAInternalSATA2 ); 426 break; 427 428 429 case kSATA2Bay: 430 string = OSString::withCString( kATASATA2BayString ); 431 break; 432 433 434 default: 435 string = OSString::withCString( kATAUnkownSocketString ); 436 break; 437 438 } 439 440 setProperty( kATASocketKey, (OSObject *)string ); 441 string->release(); 442 443 444 // these properties may be published in the future 445 // if conditions warrant 446/* 447 number = OSNumber::withNumber( theInfo->getPIOModes(), 32 ); 448 setProperty( "piomode bitmap", (OSObject *) number); 449 number->release(); 450 451 number = OSNumber::withNumber( theInfo->getDMAModes(), 32 ); 452 setProperty( "dmamode bitmap", (OSObject *) number); 453 number->release(); 454 455 number = OSNumber::withNumber( theInfo->getUltraModes(), 32 ); 456 setProperty( "ultramode bitmap", (OSObject *) number); 457 number->release(); 458 459 number = OSNumber::withNumber( theInfo->getUnits(), 32 ); 460 setProperty( "units on bus", (OSObject *) number); 461 number->release(); 462*/ 463 464 465 // these properties may be published in the future for support of advanced ATA modes. 466/* setProperty( "DMA supported", theInfo->supportsDMA()); 467 setProperty( "48-bit LBA supported", theInfo->supportsExtendedLBA()); 468 setProperty( "command overlap supported", theInfo->supportsOverlapped()); 469 setProperty( "DMA-Queued supported", theInfo->supportsDMAQueued()); 470*/ 471 472 theInfo->release(); 473 474} 475 476 477//--------------------------------------------------------------------------- 478 479//--------------------------------------------------------------------------- 480void 481ATADeviceNub::publishProperties( void ) 482{ 483 484 485 OSString* string; 486 487 switch( _deviceType ) 488 { 489 case kATADeviceType: 490 string = OSString::withCString( kATATypeATAString ); 491 break; 492 493 case kATAPIDeviceType: 494 string = OSString::withCString( kATATypeATAPIString ); 495 break; 496 497 default: 498 string = OSString::withCString( kATATypeUnknownString ); 499 break; 500 } 501 502 setProperty( kATADevPropertyKey, (OSObject *)string ); 503 string->release(); 504 505 506 OSNumber* number = OSNumber::withNumber( _unitNumber, 32 ); 507 508 setProperty( kATAUnitNumberKey, (OSObject *) number); 509 setProperty( "IOUnit", (OSObject *) number); 510 number->release(); 511 512 if( _unitNumber == 0 ) 513 { 514 setLocation("0"); 515 516 } else { 517 518 setLocation("1"); 519 } 520 521 522 523} 524 525//--------------------------------------------------------------------------- 526 527//--------------------------------------------------------------------------- 528void 529ATADeviceNub::publishVendorProperties(void) 530{ 531 532 if( IOATADevConfig::sDriveSupports48BitLBA( ( const UInt16*) buffer ) ) 533 { 534 UInt32 upperLBA, lowerLBA; 535 IOATADevConfig::sDriveExtendedLBASize( &upperLBA, &lowerLBA, ( const UInt16*) buffer ); 536 UInt64 largeLBASize = 0; 537 538 largeLBASize = ( ((UInt64) upperLBA) << 32) | ((UInt64) lowerLBA ); 539 540 OSNumber* extendedCapacity = OSNumber::withNumber( largeLBASize, 64 ); 541 setProperty( "extended LBA capacity", (OSObject *) extendedCapacity); 542 extendedCapacity->release(); 543 544 } 545 // terminate the strings with 0's 546 // this changes the identify data, so we MUST do this part last. 547 buffer[94] = 0; 548 buffer[40] = 0; 549 550 // Model number runs from byte 54 to 93 inclusive - byte 94 is set to 551 // zero to terminate that string 552 OSString* modelNum = OSString::withCString((const char*) &buffer[54]); 553 554 // now that we have made a deep copy of the model string, poke a 0 into byte 54 555 // in order to terminate the fw-vers string which runs from bytes 46 to 53 inclusive. 556 buffer[54] = 0; 557 558 OSString* firmVers = OSString::withCString((const char*) &buffer[46]); 559 560 // serial number runs from byte 20 to byte 39 inclusive and byte 40 has been terminated with a null 561 OSString* serial = OSString::withCString( (const char*) &buffer[20]); 562 563 setProperty( kATAVendorPropertyKey, (OSObject *)modelNum ); 564 setProperty( kATARevisionPropertyKey, (OSObject *)firmVers ); 565 setProperty( kATASerialNumPropertyKey, (OSObject *)serial ); 566 567 568 serial->release(); 569 modelNum->release(); 570 firmVers->release(); 571 572} 573 574 575//--------------------------------------------------------------------------- 576 577//--------------------------------------------------------------------------- 578 579 580 581void 582ATADeviceNub::MyATACallback(IOATACommand* command ) 583{ 584 if( command->getResult() ) 585 { 586 587 DLOG("Command result error = %ld\n",(long int)command->getResult() ); 588 589 } 590 591 592 ATADeviceNub* self = (ATADeviceNub*) command->refCon2; 593 594 self->processCallback( command ); 595 596 597 598} 599 600//--------------------------------------------------------------------------- 601 602//--------------------------------------------------------------------------- 603 604void 605ATADeviceNub::processCallback(IOATACommand* command ) 606{ 607 completionInfo* completer = (completionInfo*) command->refCon; 608 609 switch( completer->whatToDo ) 610 { 611 case kDoIDDataComplete: 612 613 completer->sync->signal(); 614 break; 615 616 617 618 // do nothing on set features. 619 case kDoSetFeatureComplete: 620 621 completer->sync->signal(); 622 default: 623 break; 624 625 }// end switch 626 627} 628 629//--------------------------------------------------------------------------- 630void 631ATADeviceNub::swapBytes16( UInt8* dataBuffer, IOByteCount length) 632{ 633 634 IOByteCount i; 635 UInt8 c; 636 unsigned char* firstBytePtr; 637 638 for (i = 0; i < length; i+=2) 639 { 640 firstBytePtr = dataBuffer; // save pointer 641 c = *dataBuffer++; // Save Byte0, point to Byte1 642 *firstBytePtr = *dataBuffer; // Byte0 = Byte1 643 *dataBuffer++= c; // Byte1 = Byte0 644 } 645 646 647} 648 649 650 651 652