libata-core.c 168 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634
  1. /*
  2. * libata-core.c - helper library for ATA
  3. *
  4. * Maintained by: Jeff Garzik <jgarzik@pobox.com>
  5. * Please ALWAYS copy linux-ide@vger.kernel.org
  6. * on emails.
  7. *
  8. * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
  9. * Copyright 2003-2004 Jeff Garzik
  10. *
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; see the file COPYING. If not, write to
  24. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. *
  27. * libata documentation is available via 'make {ps|pdf}docs',
  28. * as Documentation/DocBook/libata.*
  29. *
  30. * Hardware documentation available from http://www.t13.org/ and
  31. * http://www.sata-io.org/
  32. *
  33. * Standards documents from:
  34. * http://www.t13.org (ATA standards, PCI DMA IDE spec)
  35. * http://www.t10.org (SCSI MMC - for ATAPI MMC)
  36. * http://www.sata-io.org (SATA)
  37. * http://www.compactflash.org (CF)
  38. * http://www.qic.org (QIC157 - Tape and DSC)
  39. * http://www.ce-ata.org (CE-ATA: not supported)
  40. *
  41. */
  42. #include <linux/kernel.h>
  43. #include <linux/module.h>
  44. #include <linux/pci.h>
  45. #include <linux/init.h>
  46. #include <linux/list.h>
  47. #include <linux/mm.h>
  48. #include <linux/spinlock.h>
  49. #include <linux/blkdev.h>
  50. #include <linux/delay.h>
  51. #include <linux/timer.h>
  52. #include <linux/interrupt.h>
  53. #include <linux/completion.h>
  54. #include <linux/suspend.h>
  55. #include <linux/workqueue.h>
  56. #include <linux/scatterlist.h>
  57. #include <linux/io.h>
  58. #include <scsi/scsi.h>
  59. #include <scsi/scsi_cmnd.h>
  60. #include <scsi/scsi_host.h>
  61. #include <linux/libata.h>
  62. #include <asm/byteorder.h>
  63. #include <linux/cdrom.h>
  64. #include "libata.h"
  65. /* debounce timing parameters in msecs { interval, duration, timeout } */
  66. const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
  67. const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
  68. const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
  69. const struct ata_port_operations ata_base_port_ops = {
  70. .prereset = ata_std_prereset,
  71. .postreset = ata_std_postreset,
  72. .error_handler = ata_std_error_handler,
  73. };
  74. const struct ata_port_operations sata_port_ops = {
  75. .inherits = &ata_base_port_ops,
  76. .qc_defer = ata_std_qc_defer,
  77. .hardreset = sata_std_hardreset,
  78. };
  79. static unsigned int ata_dev_init_params(struct ata_device *dev,
  80. u16 heads, u16 sectors);
  81. static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
  82. static unsigned int ata_dev_set_feature(struct ata_device *dev,
  83. u8 enable, u8 feature);
  84. static void ata_dev_xfermask(struct ata_device *dev);
  85. static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
  86. unsigned int ata_print_id = 1;
  87. static struct workqueue_struct *ata_wq;
  88. struct workqueue_struct *ata_aux_wq;
  89. struct ata_force_param {
  90. const char *name;
  91. unsigned int cbl;
  92. int spd_limit;
  93. unsigned long xfer_mask;
  94. unsigned int horkage_on;
  95. unsigned int horkage_off;
  96. unsigned int lflags;
  97. };
  98. struct ata_force_ent {
  99. int port;
  100. int device;
  101. struct ata_force_param param;
  102. };
  103. static struct ata_force_ent *ata_force_tbl;
  104. static int ata_force_tbl_size;
  105. static char ata_force_param_buf[PAGE_SIZE] __initdata;
  106. /* param_buf is thrown away after initialization, disallow read */
  107. module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
  108. MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/kernel-parameters.txt for details)");
  109. static int atapi_enabled = 1;
  110. module_param(atapi_enabled, int, 0444);
  111. MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
  112. static int atapi_dmadir = 0;
  113. module_param(atapi_dmadir, int, 0444);
  114. MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off, 1=on)");
  115. int atapi_passthru16 = 1;
  116. module_param(atapi_passthru16, int, 0444);
  117. MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices; on by default (0=off, 1=on)");
  118. int libata_fua = 0;
  119. module_param_named(fua, libata_fua, int, 0444);
  120. MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
  121. static int ata_ignore_hpa;
  122. module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
  123. MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
  124. static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
  125. module_param_named(dma, libata_dma_mask, int, 0444);
  126. MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
  127. static int ata_probe_timeout;
  128. module_param(ata_probe_timeout, int, 0444);
  129. MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
  130. int libata_noacpi = 0;
  131. module_param_named(noacpi, libata_noacpi, int, 0444);
  132. MODULE_PARM_DESC(noacpi, "Disables the use of ACPI in probe/suspend/resume when set");
  133. int libata_allow_tpm = 0;
  134. module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
  135. MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands");
  136. MODULE_AUTHOR("Jeff Garzik");
  137. MODULE_DESCRIPTION("Library module for ATA devices");
  138. MODULE_LICENSE("GPL");
  139. MODULE_VERSION(DRV_VERSION);
  140. /*
  141. * Iterator helpers. Don't use directly.
  142. *
  143. * LOCKING:
  144. * Host lock or EH context.
  145. */
  146. struct ata_link *__ata_port_next_link(struct ata_port *ap,
  147. struct ata_link *link, bool dev_only)
  148. {
  149. /* NULL link indicates start of iteration */
  150. if (!link) {
  151. if (dev_only && sata_pmp_attached(ap))
  152. return ap->pmp_link;
  153. return &ap->link;
  154. }
  155. /* we just iterated over the host master link, what's next? */
  156. if (link == &ap->link) {
  157. if (!sata_pmp_attached(ap)) {
  158. if (unlikely(ap->slave_link) && !dev_only)
  159. return ap->slave_link;
  160. return NULL;
  161. }
  162. return ap->pmp_link;
  163. }
  164. /* slave_link excludes PMP */
  165. if (unlikely(link == ap->slave_link))
  166. return NULL;
  167. /* iterate to the next PMP link */
  168. if (++link < ap->pmp_link + ap->nr_pmp_links)
  169. return link;
  170. return NULL;
  171. }
  172. /**
  173. * ata_dev_phys_link - find physical link for a device
  174. * @dev: ATA device to look up physical link for
  175. *
  176. * Look up physical link which @dev is attached to. Note that
  177. * this is different from @dev->link only when @dev is on slave
  178. * link. For all other cases, it's the same as @dev->link.
  179. *
  180. * LOCKING:
  181. * Don't care.
  182. *
  183. * RETURNS:
  184. * Pointer to the found physical link.
  185. */
  186. struct ata_link *ata_dev_phys_link(struct ata_device *dev)
  187. {
  188. struct ata_port *ap = dev->link->ap;
  189. if (!ap->slave_link)
  190. return dev->link;
  191. if (!dev->devno)
  192. return &ap->link;
  193. return ap->slave_link;
  194. }
  195. /**
  196. * ata_force_cbl - force cable type according to libata.force
  197. * @ap: ATA port of interest
  198. *
  199. * Force cable type according to libata.force and whine about it.
  200. * The last entry which has matching port number is used, so it
  201. * can be specified as part of device force parameters. For
  202. * example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
  203. * same effect.
  204. *
  205. * LOCKING:
  206. * EH context.
  207. */
  208. void ata_force_cbl(struct ata_port *ap)
  209. {
  210. int i;
  211. for (i = ata_force_tbl_size - 1; i >= 0; i--) {
  212. const struct ata_force_ent *fe = &ata_force_tbl[i];
  213. if (fe->port != -1 && fe->port != ap->print_id)
  214. continue;
  215. if (fe->param.cbl == ATA_CBL_NONE)
  216. continue;
  217. ap->cbl = fe->param.cbl;
  218. ata_port_printk(ap, KERN_NOTICE,
  219. "FORCE: cable set to %s\n", fe->param.name);
  220. return;
  221. }
  222. }
  223. /**
  224. * ata_force_link_limits - force link limits according to libata.force
  225. * @link: ATA link of interest
  226. *
  227. * Force link flags and SATA spd limit according to libata.force
  228. * and whine about it. When only the port part is specified
  229. * (e.g. 1:), the limit applies to all links connected to both
  230. * the host link and all fan-out ports connected via PMP. If the
  231. * device part is specified as 0 (e.g. 1.00:), it specifies the
  232. * first fan-out link not the host link. Device number 15 always
  233. * points to the host link whether PMP is attached or not. If the
  234. * controller has slave link, device number 16 points to it.
  235. *
  236. * LOCKING:
  237. * EH context.
  238. */
  239. static void ata_force_link_limits(struct ata_link *link)
  240. {
  241. bool did_spd = false;
  242. int linkno = link->pmp;
  243. int i;
  244. if (ata_is_host_link(link))
  245. linkno += 15;
  246. for (i = ata_force_tbl_size - 1; i >= 0; i--) {
  247. const struct ata_force_ent *fe = &ata_force_tbl[i];
  248. if (fe->port != -1 && fe->port != link->ap->print_id)
  249. continue;
  250. if (fe->device != -1 && fe->device != linkno)
  251. continue;
  252. /* only honor the first spd limit */
  253. if (!did_spd && fe->param.spd_limit) {
  254. link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
  255. ata_link_printk(link, KERN_NOTICE,
  256. "FORCE: PHY spd limit set to %s\n",
  257. fe->param.name);
  258. did_spd = true;
  259. }
  260. /* let lflags stack */
  261. if (fe->param.lflags) {
  262. link->flags |= fe->param.lflags;
  263. ata_link_printk(link, KERN_NOTICE,
  264. "FORCE: link flag 0x%x forced -> 0x%x\n",
  265. fe->param.lflags, link->flags);
  266. }
  267. }
  268. }
  269. /**
  270. * ata_force_xfermask - force xfermask according to libata.force
  271. * @dev: ATA device of interest
  272. *
  273. * Force xfer_mask according to libata.force and whine about it.
  274. * For consistency with link selection, device number 15 selects
  275. * the first device connected to the host link.
  276. *
  277. * LOCKING:
  278. * EH context.
  279. */
  280. static void ata_force_xfermask(struct ata_device *dev)
  281. {
  282. int devno = dev->link->pmp + dev->devno;
  283. int alt_devno = devno;
  284. int i;
  285. /* allow n.15/16 for devices attached to host port */
  286. if (ata_is_host_link(dev->link))
  287. alt_devno += 15;
  288. for (i = ata_force_tbl_size - 1; i >= 0; i--) {
  289. const struct ata_force_ent *fe = &ata_force_tbl[i];
  290. unsigned long pio_mask, mwdma_mask, udma_mask;
  291. if (fe->port != -1 && fe->port != dev->link->ap->print_id)
  292. continue;
  293. if (fe->device != -1 && fe->device != devno &&
  294. fe->device != alt_devno)
  295. continue;
  296. if (!fe->param.xfer_mask)
  297. continue;
  298. ata_unpack_xfermask(fe->param.xfer_mask,
  299. &pio_mask, &mwdma_mask, &udma_mask);
  300. if (udma_mask)
  301. dev->udma_mask = udma_mask;
  302. else if (mwdma_mask) {
  303. dev->udma_mask = 0;
  304. dev->mwdma_mask = mwdma_mask;
  305. } else {
  306. dev->udma_mask = 0;
  307. dev->mwdma_mask = 0;
  308. dev->pio_mask = pio_mask;
  309. }
  310. ata_dev_printk(dev, KERN_NOTICE,
  311. "FORCE: xfer_mask set to %s\n", fe->param.name);
  312. return;
  313. }
  314. }
  315. /**
  316. * ata_force_horkage - force horkage according to libata.force
  317. * @dev: ATA device of interest
  318. *
  319. * Force horkage according to libata.force and whine about it.
  320. * For consistency with link selection, device number 15 selects
  321. * the first device connected to the host link.
  322. *
  323. * LOCKING:
  324. * EH context.
  325. */
  326. static void ata_force_horkage(struct ata_device *dev)
  327. {
  328. int devno = dev->link->pmp + dev->devno;
  329. int alt_devno = devno;
  330. int i;
  331. /* allow n.15/16 for devices attached to host port */
  332. if (ata_is_host_link(dev->link))
  333. alt_devno += 15;
  334. for (i = 0; i < ata_force_tbl_size; i++) {
  335. const struct ata_force_ent *fe = &ata_force_tbl[i];
  336. if (fe->port != -1 && fe->port != dev->link->ap->print_id)
  337. continue;
  338. if (fe->device != -1 && fe->device != devno &&
  339. fe->device != alt_devno)
  340. continue;
  341. if (!(~dev->horkage & fe->param.horkage_on) &&
  342. !(dev->horkage & fe->param.horkage_off))
  343. continue;
  344. dev->horkage |= fe->param.horkage_on;
  345. dev->horkage &= ~fe->param.horkage_off;
  346. ata_dev_printk(dev, KERN_NOTICE,
  347. "FORCE: horkage modified (%s)\n", fe->param.name);
  348. }
  349. }
  350. /**
  351. * atapi_cmd_type - Determine ATAPI command type from SCSI opcode
  352. * @opcode: SCSI opcode
  353. *
  354. * Determine ATAPI command type from @opcode.
  355. *
  356. * LOCKING:
  357. * None.
  358. *
  359. * RETURNS:
  360. * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
  361. */
  362. int atapi_cmd_type(u8 opcode)
  363. {
  364. switch (opcode) {
  365. case GPCMD_READ_10:
  366. case GPCMD_READ_12:
  367. return ATAPI_READ;
  368. case GPCMD_WRITE_10:
  369. case GPCMD_WRITE_12:
  370. case GPCMD_WRITE_AND_VERIFY_10:
  371. return ATAPI_WRITE;
  372. case GPCMD_READ_CD:
  373. case GPCMD_READ_CD_MSF:
  374. return ATAPI_READ_CD;
  375. case ATA_16:
  376. case ATA_12:
  377. if (atapi_passthru16)
  378. return ATAPI_PASS_THRU;
  379. /* fall thru */
  380. default:
  381. return ATAPI_MISC;
  382. }
  383. }
  384. /**
  385. * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
  386. * @tf: Taskfile to convert
  387. * @pmp: Port multiplier port
  388. * @is_cmd: This FIS is for command
  389. * @fis: Buffer into which data will output
  390. *
  391. * Converts a standard ATA taskfile to a Serial ATA
  392. * FIS structure (Register - Host to Device).
  393. *
  394. * LOCKING:
  395. * Inherited from caller.
  396. */
  397. void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
  398. {
  399. fis[0] = 0x27; /* Register - Host to Device FIS */
  400. fis[1] = pmp & 0xf; /* Port multiplier number*/
  401. if (is_cmd)
  402. fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */
  403. fis[2] = tf->command;
  404. fis[3] = tf->feature;
  405. fis[4] = tf->lbal;
  406. fis[5] = tf->lbam;
  407. fis[6] = tf->lbah;
  408. fis[7] = tf->device;
  409. fis[8] = tf->hob_lbal;
  410. fis[9] = tf->hob_lbam;
  411. fis[10] = tf->hob_lbah;
  412. fis[11] = tf->hob_feature;
  413. fis[12] = tf->nsect;
  414. fis[13] = tf->hob_nsect;
  415. fis[14] = 0;
  416. fis[15] = tf->ctl;
  417. fis[16] = 0;
  418. fis[17] = 0;
  419. fis[18] = 0;
  420. fis[19] = 0;
  421. }
  422. /**
  423. * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
  424. * @fis: Buffer from which data will be input
  425. * @tf: Taskfile to output
  426. *
  427. * Converts a serial ATA FIS structure to a standard ATA taskfile.
  428. *
  429. * LOCKING:
  430. * Inherited from caller.
  431. */
  432. void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
  433. {
  434. tf->command = fis[2]; /* status */
  435. tf->feature = fis[3]; /* error */
  436. tf->lbal = fis[4];
  437. tf->lbam = fis[5];
  438. tf->lbah = fis[6];
  439. tf->device = fis[7];
  440. tf->hob_lbal = fis[8];
  441. tf->hob_lbam = fis[9];
  442. tf->hob_lbah = fis[10];
  443. tf->nsect = fis[12];
  444. tf->hob_nsect = fis[13];
  445. }
  446. static const u8 ata_rw_cmds[] = {
  447. /* pio multi */
  448. ATA_CMD_READ_MULTI,
  449. ATA_CMD_WRITE_MULTI,
  450. ATA_CMD_READ_MULTI_EXT,
  451. ATA_CMD_WRITE_MULTI_EXT,
  452. 0,
  453. 0,
  454. 0,
  455. ATA_CMD_WRITE_MULTI_FUA_EXT,
  456. /* pio */
  457. ATA_CMD_PIO_READ,
  458. ATA_CMD_PIO_WRITE,
  459. ATA_CMD_PIO_READ_EXT,
  460. ATA_CMD_PIO_WRITE_EXT,
  461. 0,
  462. 0,
  463. 0,
  464. 0,
  465. /* dma */
  466. ATA_CMD_READ,
  467. ATA_CMD_WRITE,
  468. ATA_CMD_READ_EXT,
  469. ATA_CMD_WRITE_EXT,
  470. 0,
  471. 0,
  472. 0,
  473. ATA_CMD_WRITE_FUA_EXT
  474. };
  475. /**
  476. * ata_rwcmd_protocol - set taskfile r/w commands and protocol
  477. * @tf: command to examine and configure
  478. * @dev: device tf belongs to
  479. *
  480. * Examine the device configuration and tf->flags to calculate
  481. * the proper read/write commands and protocol to use.
  482. *
  483. * LOCKING:
  484. * caller.
  485. */
  486. static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
  487. {
  488. u8 cmd;
  489. int index, fua, lba48, write;
  490. fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
  491. lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
  492. write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
  493. if (dev->flags & ATA_DFLAG_PIO) {
  494. tf->protocol = ATA_PROT_PIO;
  495. index = dev->multi_count ? 0 : 8;
  496. } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
  497. /* Unable to use DMA due to host limitation */
  498. tf->protocol = ATA_PROT_PIO;
  499. index = dev->multi_count ? 0 : 8;
  500. } else {
  501. tf->protocol = ATA_PROT_DMA;
  502. index = 16;
  503. }
  504. cmd = ata_rw_cmds[index + fua + lba48 + write];
  505. if (cmd) {
  506. tf->command = cmd;
  507. return 0;
  508. }
  509. return -1;
  510. }
  511. /**
  512. * ata_tf_read_block - Read block address from ATA taskfile
  513. * @tf: ATA taskfile of interest
  514. * @dev: ATA device @tf belongs to
  515. *
  516. * LOCKING:
  517. * None.
  518. *
  519. * Read block address from @tf. This function can handle all
  520. * three address formats - LBA, LBA48 and CHS. tf->protocol and
  521. * flags select the address format to use.
  522. *
  523. * RETURNS:
  524. * Block address read from @tf.
  525. */
  526. u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
  527. {
  528. u64 block = 0;
  529. if (tf->flags & ATA_TFLAG_LBA) {
  530. if (tf->flags & ATA_TFLAG_LBA48) {
  531. block |= (u64)tf->hob_lbah << 40;
  532. block |= (u64)tf->hob_lbam << 32;
  533. block |= (u64)tf->hob_lbal << 24;
  534. } else
  535. block |= (tf->device & 0xf) << 24;
  536. block |= tf->lbah << 16;
  537. block |= tf->lbam << 8;
  538. block |= tf->lbal;
  539. } else {
  540. u32 cyl, head, sect;
  541. cyl = tf->lbam | (tf->lbah << 8);
  542. head = tf->device & 0xf;
  543. sect = tf->lbal;
  544. block = (cyl * dev->heads + head) * dev->sectors + sect;
  545. }
  546. return block;
  547. }
  548. /**
  549. * ata_build_rw_tf - Build ATA taskfile for given read/write request
  550. * @tf: Target ATA taskfile
  551. * @dev: ATA device @tf belongs to
  552. * @block: Block address
  553. * @n_block: Number of blocks
  554. * @tf_flags: RW/FUA etc...
  555. * @tag: tag
  556. *
  557. * LOCKING:
  558. * None.
  559. *
  560. * Build ATA taskfile @tf for read/write request described by
  561. * @block, @n_block, @tf_flags and @tag on @dev.
  562. *
  563. * RETURNS:
  564. *
  565. * 0 on success, -ERANGE if the request is too large for @dev,
  566. * -EINVAL if the request is invalid.
  567. */
  568. int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
  569. u64 block, u32 n_block, unsigned int tf_flags,
  570. unsigned int tag)
  571. {
  572. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  573. tf->flags |= tf_flags;
  574. if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
  575. /* yay, NCQ */
  576. if (!lba_48_ok(block, n_block))
  577. return -ERANGE;
  578. tf->protocol = ATA_PROT_NCQ;
  579. tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
  580. if (tf->flags & ATA_TFLAG_WRITE)
  581. tf->command = ATA_CMD_FPDMA_WRITE;
  582. else
  583. tf->command = ATA_CMD_FPDMA_READ;
  584. tf->nsect = tag << 3;
  585. tf->hob_feature = (n_block >> 8) & 0xff;
  586. tf->feature = n_block & 0xff;
  587. tf->hob_lbah = (block >> 40) & 0xff;
  588. tf->hob_lbam = (block >> 32) & 0xff;
  589. tf->hob_lbal = (block >> 24) & 0xff;
  590. tf->lbah = (block >> 16) & 0xff;
  591. tf->lbam = (block >> 8) & 0xff;
  592. tf->lbal = block & 0xff;
  593. tf->device = 1 << 6;
  594. if (tf->flags & ATA_TFLAG_FUA)
  595. tf->device |= 1 << 7;
  596. } else if (dev->flags & ATA_DFLAG_LBA) {
  597. tf->flags |= ATA_TFLAG_LBA;
  598. if (lba_28_ok(block, n_block)) {
  599. /* use LBA28 */
  600. tf->device |= (block >> 24) & 0xf;
  601. } else if (lba_48_ok(block, n_block)) {
  602. if (!(dev->flags & ATA_DFLAG_LBA48))
  603. return -ERANGE;
  604. /* use LBA48 */
  605. tf->flags |= ATA_TFLAG_LBA48;
  606. tf->hob_nsect = (n_block >> 8) & 0xff;
  607. tf->hob_lbah = (block >> 40) & 0xff;
  608. tf->hob_lbam = (block >> 32) & 0xff;
  609. tf->hob_lbal = (block >> 24) & 0xff;
  610. } else
  611. /* request too large even for LBA48 */
  612. return -ERANGE;
  613. if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
  614. return -EINVAL;
  615. tf->nsect = n_block & 0xff;
  616. tf->lbah = (block >> 16) & 0xff;
  617. tf->lbam = (block >> 8) & 0xff;
  618. tf->lbal = block & 0xff;
  619. tf->device |= ATA_LBA;
  620. } else {
  621. /* CHS */
  622. u32 sect, head, cyl, track;
  623. /* The request -may- be too large for CHS addressing. */
  624. if (!lba_28_ok(block, n_block))
  625. return -ERANGE;
  626. if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
  627. return -EINVAL;
  628. /* Convert LBA to CHS */
  629. track = (u32)block / dev->sectors;
  630. cyl = track / dev->heads;
  631. head = track % dev->heads;
  632. sect = (u32)block % dev->sectors + 1;
  633. DPRINTK("block %u track %u cyl %u head %u sect %u\n",
  634. (u32)block, track, cyl, head, sect);
  635. /* Check whether the converted CHS can fit.
  636. Cylinder: 0-65535
  637. Head: 0-15
  638. Sector: 1-255*/
  639. if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
  640. return -ERANGE;
  641. tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
  642. tf->lbal = sect;
  643. tf->lbam = cyl;
  644. tf->lbah = cyl >> 8;
  645. tf->device |= head;
  646. }
  647. return 0;
  648. }
  649. /**
  650. * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
  651. * @pio_mask: pio_mask
  652. * @mwdma_mask: mwdma_mask
  653. * @udma_mask: udma_mask
  654. *
  655. * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
  656. * unsigned int xfer_mask.
  657. *
  658. * LOCKING:
  659. * None.
  660. *
  661. * RETURNS:
  662. * Packed xfer_mask.
  663. */
  664. unsigned long ata_pack_xfermask(unsigned long pio_mask,
  665. unsigned long mwdma_mask,
  666. unsigned long udma_mask)
  667. {
  668. return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
  669. ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
  670. ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
  671. }
  672. /**
  673. * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
  674. * @xfer_mask: xfer_mask to unpack
  675. * @pio_mask: resulting pio_mask
  676. * @mwdma_mask: resulting mwdma_mask
  677. * @udma_mask: resulting udma_mask
  678. *
  679. * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
  680. * Any NULL distination masks will be ignored.
  681. */
  682. void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
  683. unsigned long *mwdma_mask, unsigned long *udma_mask)
  684. {
  685. if (pio_mask)
  686. *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
  687. if (mwdma_mask)
  688. *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
  689. if (udma_mask)
  690. *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
  691. }
  692. static const struct ata_xfer_ent {
  693. int shift, bits;
  694. u8 base;
  695. } ata_xfer_tbl[] = {
  696. { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
  697. { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
  698. { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
  699. { -1, },
  700. };
  701. /**
  702. * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
  703. * @xfer_mask: xfer_mask of interest
  704. *
  705. * Return matching XFER_* value for @xfer_mask. Only the highest
  706. * bit of @xfer_mask is considered.
  707. *
  708. * LOCKING:
  709. * None.
  710. *
  711. * RETURNS:
  712. * Matching XFER_* value, 0xff if no match found.
  713. */
  714. u8 ata_xfer_mask2mode(unsigned long xfer_mask)
  715. {
  716. int highbit = fls(xfer_mask) - 1;
  717. const struct ata_xfer_ent *ent;
  718. for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
  719. if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
  720. return ent->base + highbit - ent->shift;
  721. return 0xff;
  722. }
  723. /**
  724. * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
  725. * @xfer_mode: XFER_* of interest
  726. *
  727. * Return matching xfer_mask for @xfer_mode.
  728. *
  729. * LOCKING:
  730. * None.
  731. *
  732. * RETURNS:
  733. * Matching xfer_mask, 0 if no match found.
  734. */
  735. unsigned long ata_xfer_mode2mask(u8 xfer_mode)
  736. {
  737. const struct ata_xfer_ent *ent;
  738. for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
  739. if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
  740. return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
  741. & ~((1 << ent->shift) - 1);
  742. return 0;
  743. }
  744. /**
  745. * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
  746. * @xfer_mode: XFER_* of interest
  747. *
  748. * Return matching xfer_shift for @xfer_mode.
  749. *
  750. * LOCKING:
  751. * None.
  752. *
  753. * RETURNS:
  754. * Matching xfer_shift, -1 if no match found.
  755. */
  756. int ata_xfer_mode2shift(unsigned long xfer_mode)
  757. {
  758. const struct ata_xfer_ent *ent;
  759. for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
  760. if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
  761. return ent->shift;
  762. return -1;
  763. }
  764. /**
  765. * ata_mode_string - convert xfer_mask to string
  766. * @xfer_mask: mask of bits supported; only highest bit counts.
  767. *
  768. * Determine string which represents the highest speed
  769. * (highest bit in @modemask).
  770. *
  771. * LOCKING:
  772. * None.
  773. *
  774. * RETURNS:
  775. * Constant C string representing highest speed listed in
  776. * @mode_mask, or the constant C string "<n/a>".
  777. */
  778. const char *ata_mode_string(unsigned long xfer_mask)
  779. {
  780. static const char * const xfer_mode_str[] = {
  781. "PIO0",
  782. "PIO1",
  783. "PIO2",
  784. "PIO3",
  785. "PIO4",
  786. "PIO5",
  787. "PIO6",
  788. "MWDMA0",
  789. "MWDMA1",
  790. "MWDMA2",
  791. "MWDMA3",
  792. "MWDMA4",
  793. "UDMA/16",
  794. "UDMA/25",
  795. "UDMA/33",
  796. "UDMA/44",
  797. "UDMA/66",
  798. "UDMA/100",
  799. "UDMA/133",
  800. "UDMA7",
  801. };
  802. int highbit;
  803. highbit = fls(xfer_mask) - 1;
  804. if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
  805. return xfer_mode_str[highbit];
  806. return "<n/a>";
  807. }
  808. static const char *sata_spd_string(unsigned int spd)
  809. {
  810. static const char * const spd_str[] = {
  811. "1.5 Gbps",
  812. "3.0 Gbps",
  813. };
  814. if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
  815. return "<unknown>";
  816. return spd_str[spd - 1];
  817. }
  818. void ata_dev_disable(struct ata_device *dev)
  819. {
  820. if (ata_dev_enabled(dev)) {
  821. if (ata_msg_drv(dev->link->ap))
  822. ata_dev_printk(dev, KERN_WARNING, "disabled\n");
  823. ata_acpi_on_disable(dev);
  824. ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 |
  825. ATA_DNXFER_QUIET);
  826. dev->class++;
  827. }
  828. }
  829. static int ata_dev_set_dipm(struct ata_device *dev, enum link_pm policy)
  830. {
  831. struct ata_link *link = dev->link;
  832. struct ata_port *ap = link->ap;
  833. u32 scontrol;
  834. unsigned int err_mask;
  835. int rc;
  836. /*
  837. * disallow DIPM for drivers which haven't set
  838. * ATA_FLAG_IPM. This is because when DIPM is enabled,
  839. * phy ready will be set in the interrupt status on
  840. * state changes, which will cause some drivers to
  841. * think there are errors - additionally drivers will
  842. * need to disable hot plug.
  843. */
  844. if (!(ap->flags & ATA_FLAG_IPM) || !ata_dev_enabled(dev)) {
  845. ap->pm_policy = NOT_AVAILABLE;
  846. return -EINVAL;
  847. }
  848. /*
  849. * For DIPM, we will only enable it for the
  850. * min_power setting.
  851. *
  852. * Why? Because Disks are too stupid to know that
  853. * If the host rejects a request to go to SLUMBER
  854. * they should retry at PARTIAL, and instead it
  855. * just would give up. So, for medium_power to
  856. * work at all, we need to only allow HIPM.
  857. */
  858. rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
  859. if (rc)
  860. return rc;
  861. switch (policy) {
  862. case MIN_POWER:
  863. /* no restrictions on IPM transitions */
  864. scontrol &= ~(0x3 << 8);
  865. rc = sata_scr_write(link, SCR_CONTROL, scontrol);
  866. if (rc)
  867. return rc;
  868. /* enable DIPM */
  869. if (dev->flags & ATA_DFLAG_DIPM)
  870. err_mask = ata_dev_set_feature(dev,
  871. SETFEATURES_SATA_ENABLE, SATA_DIPM);
  872. break;
  873. case MEDIUM_POWER:
  874. /* allow IPM to PARTIAL */
  875. scontrol &= ~(0x1 << 8);
  876. scontrol |= (0x2 << 8);
  877. rc = sata_scr_write(link, SCR_CONTROL, scontrol);
  878. if (rc)
  879. return rc;
  880. /*
  881. * we don't have to disable DIPM since IPM flags
  882. * disallow transitions to SLUMBER, which effectively
  883. * disable DIPM if it does not support PARTIAL
  884. */
  885. break;
  886. case NOT_AVAILABLE:
  887. case MAX_PERFORMANCE:
  888. /* disable all IPM transitions */
  889. scontrol |= (0x3 << 8);
  890. rc = sata_scr_write(link, SCR_CONTROL, scontrol);
  891. if (rc)
  892. return rc;
  893. /*
  894. * we don't have to disable DIPM since IPM flags
  895. * disallow all transitions which effectively
  896. * disable DIPM anyway.
  897. */
  898. break;
  899. }
  900. /* FIXME: handle SET FEATURES failure */
  901. (void) err_mask;
  902. return 0;
  903. }
  904. /**
  905. * ata_dev_enable_pm - enable SATA interface power management
  906. * @dev: device to enable power management
  907. * @policy: the link power management policy
  908. *
  909. * Enable SATA Interface power management. This will enable
  910. * Device Interface Power Management (DIPM) for min_power
  911. * policy, and then call driver specific callbacks for
  912. * enabling Host Initiated Power management.
  913. *
  914. * Locking: Caller.
  915. * Returns: -EINVAL if IPM is not supported, 0 otherwise.
  916. */
  917. void ata_dev_enable_pm(struct ata_device *dev, enum link_pm policy)
  918. {
  919. int rc = 0;
  920. struct ata_port *ap = dev->link->ap;
  921. /* set HIPM first, then DIPM */
  922. if (ap->ops->enable_pm)
  923. rc = ap->ops->enable_pm(ap, policy);
  924. if (rc)
  925. goto enable_pm_out;
  926. rc = ata_dev_set_dipm(dev, policy);
  927. enable_pm_out:
  928. if (rc)
  929. ap->pm_policy = MAX_PERFORMANCE;
  930. else
  931. ap->pm_policy = policy;
  932. return /* rc */; /* hopefully we can use 'rc' eventually */
  933. }
  934. #ifdef CONFIG_PM
  935. /**
  936. * ata_dev_disable_pm - disable SATA interface power management
  937. * @dev: device to disable power management
  938. *
  939. * Disable SATA Interface power management. This will disable
  940. * Device Interface Power Management (DIPM) without changing
  941. * policy, call driver specific callbacks for disabling Host
  942. * Initiated Power management.
  943. *
  944. * Locking: Caller.
  945. * Returns: void
  946. */
  947. static void ata_dev_disable_pm(struct ata_device *dev)
  948. {
  949. struct ata_port *ap = dev->link->ap;
  950. ata_dev_set_dipm(dev, MAX_PERFORMANCE);
  951. if (ap->ops->disable_pm)
  952. ap->ops->disable_pm(ap);
  953. }
  954. #endif /* CONFIG_PM */
  955. void ata_lpm_schedule(struct ata_port *ap, enum link_pm policy)
  956. {
  957. ap->pm_policy = policy;
  958. ap->link.eh_info.action |= ATA_EH_LPM;
  959. ap->link.eh_info.flags |= ATA_EHI_NO_AUTOPSY;
  960. ata_port_schedule_eh(ap);
  961. }
  962. #ifdef CONFIG_PM
  963. static void ata_lpm_enable(struct ata_host *host)
  964. {
  965. struct ata_link *link;
  966. struct ata_port *ap;
  967. struct ata_device *dev;
  968. int i;
  969. for (i = 0; i < host->n_ports; i++) {
  970. ap = host->ports[i];
  971. ata_port_for_each_link(link, ap) {
  972. ata_link_for_each_dev(dev, link)
  973. ata_dev_disable_pm(dev);
  974. }
  975. }
  976. }
  977. static void ata_lpm_disable(struct ata_host *host)
  978. {
  979. int i;
  980. for (i = 0; i < host->n_ports; i++) {
  981. struct ata_port *ap = host->ports[i];
  982. ata_lpm_schedule(ap, ap->pm_policy);
  983. }
  984. }
  985. #endif /* CONFIG_PM */
  986. /**
  987. * ata_dev_classify - determine device type based on ATA-spec signature
  988. * @tf: ATA taskfile register set for device to be identified
  989. *
  990. * Determine from taskfile register contents whether a device is
  991. * ATA or ATAPI, as per "Signature and persistence" section
  992. * of ATA/PI spec (volume 1, sect 5.14).
  993. *
  994. * LOCKING:
  995. * None.
  996. *
  997. * RETURNS:
  998. * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP or
  999. * %ATA_DEV_UNKNOWN the event of failure.
  1000. */
  1001. unsigned int ata_dev_classify(const struct ata_taskfile *tf)
  1002. {
  1003. /* Apple's open source Darwin code hints that some devices only
  1004. * put a proper signature into the LBA mid/high registers,
  1005. * So, we only check those. It's sufficient for uniqueness.
  1006. *
  1007. * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
  1008. * signatures for ATA and ATAPI devices attached on SerialATA,
  1009. * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA
  1010. * spec has never mentioned about using different signatures
  1011. * for ATA/ATAPI devices. Then, Serial ATA II: Port
  1012. * Multiplier specification began to use 0x69/0x96 to identify
  1013. * port multpliers and 0x3c/0xc3 to identify SEMB device.
  1014. * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
  1015. * 0x69/0x96 shortly and described them as reserved for
  1016. * SerialATA.
  1017. *
  1018. * We follow the current spec and consider that 0x69/0x96
  1019. * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
  1020. */
  1021. if ((tf->lbam == 0) && (tf->lbah == 0)) {
  1022. DPRINTK("found ATA device by sig\n");
  1023. return ATA_DEV_ATA;
  1024. }
  1025. if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
  1026. DPRINTK("found ATAPI device by sig\n");
  1027. return ATA_DEV_ATAPI;
  1028. }
  1029. if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
  1030. DPRINTK("found PMP device by sig\n");
  1031. return ATA_DEV_PMP;
  1032. }
  1033. if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
  1034. printk(KERN_INFO "ata: SEMB device ignored\n");
  1035. return ATA_DEV_SEMB_UNSUP; /* not yet */
  1036. }
  1037. DPRINTK("unknown device\n");
  1038. return ATA_DEV_UNKNOWN;
  1039. }
  1040. /**
  1041. * ata_id_string - Convert IDENTIFY DEVICE page into string
  1042. * @id: IDENTIFY DEVICE results we will examine
  1043. * @s: string into which data is output
  1044. * @ofs: offset into identify device page
  1045. * @len: length of string to return. must be an even number.
  1046. *
  1047. * The strings in the IDENTIFY DEVICE page are broken up into
  1048. * 16-bit chunks. Run through the string, and output each
  1049. * 8-bit chunk linearly, regardless of platform.
  1050. *
  1051. * LOCKING:
  1052. * caller.
  1053. */
  1054. void ata_id_string(const u16 *id, unsigned char *s,
  1055. unsigned int ofs, unsigned int len)
  1056. {
  1057. unsigned int c;
  1058. BUG_ON(len & 1);
  1059. while (len > 0) {
  1060. c = id[ofs] >> 8;
  1061. *s = c;
  1062. s++;
  1063. c = id[ofs] & 0xff;
  1064. *s = c;
  1065. s++;
  1066. ofs++;
  1067. len -= 2;
  1068. }
  1069. }
  1070. /**
  1071. * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
  1072. * @id: IDENTIFY DEVICE results we will examine
  1073. * @s: string into which data is output
  1074. * @ofs: offset into identify device page
  1075. * @len: length of string to return. must be an odd number.
  1076. *
  1077. * This function is identical to ata_id_string except that it
  1078. * trims trailing spaces and terminates the resulting string with
  1079. * null. @len must be actual maximum length (even number) + 1.
  1080. *
  1081. * LOCKING:
  1082. * caller.
  1083. */
  1084. void ata_id_c_string(const u16 *id, unsigned char *s,
  1085. unsigned int ofs, unsigned int len)
  1086. {
  1087. unsigned char *p;
  1088. ata_id_string(id, s, ofs, len - 1);
  1089. p = s + strnlen(s, len - 1);
  1090. while (p > s && p[-1] == ' ')
  1091. p--;
  1092. *p = '\0';
  1093. }
  1094. static u64 ata_id_n_sectors(const u16 *id)
  1095. {
  1096. if (ata_id_has_lba(id)) {
  1097. if (ata_id_has_lba48(id))
  1098. return ata_id_u64(id, 100);
  1099. else
  1100. return ata_id_u32(id, 60);
  1101. } else {
  1102. if (ata_id_current_chs_valid(id))
  1103. return ata_id_u32(id, 57);
  1104. else
  1105. return id[1] * id[3] * id[6];
  1106. }
  1107. }
  1108. u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
  1109. {
  1110. u64 sectors = 0;
  1111. sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
  1112. sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
  1113. sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
  1114. sectors |= (tf->lbah & 0xff) << 16;
  1115. sectors |= (tf->lbam & 0xff) << 8;
  1116. sectors |= (tf->lbal & 0xff);
  1117. return sectors;
  1118. }
  1119. u64 ata_tf_to_lba(const struct ata_taskfile *tf)
  1120. {
  1121. u64 sectors = 0;
  1122. sectors |= (tf->device & 0x0f) << 24;
  1123. sectors |= (tf->lbah & 0xff) << 16;
  1124. sectors |= (tf->lbam & 0xff) << 8;
  1125. sectors |= (tf->lbal & 0xff);
  1126. return sectors;
  1127. }
  1128. /**
  1129. * ata_read_native_max_address - Read native max address
  1130. * @dev: target device
  1131. * @max_sectors: out parameter for the result native max address
  1132. *
  1133. * Perform an LBA48 or LBA28 native size query upon the device in
  1134. * question.
  1135. *
  1136. * RETURNS:
  1137. * 0 on success, -EACCES if command is aborted by the drive.
  1138. * -EIO on other errors.
  1139. */
  1140. static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
  1141. {
  1142. unsigned int err_mask;
  1143. struct ata_taskfile tf;
  1144. int lba48 = ata_id_has_lba48(dev->id);
  1145. ata_tf_init(dev, &tf);
  1146. /* always clear all address registers */
  1147. tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  1148. if (lba48) {
  1149. tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
  1150. tf.flags |= ATA_TFLAG_LBA48;
  1151. } else
  1152. tf.command = ATA_CMD_READ_NATIVE_MAX;
  1153. tf.protocol |= ATA_PROT_NODATA;
  1154. tf.device |= ATA_LBA;
  1155. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
  1156. if (err_mask) {
  1157. ata_dev_printk(dev, KERN_WARNING, "failed to read native "
  1158. "max address (err_mask=0x%x)\n", err_mask);
  1159. if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
  1160. return -EACCES;
  1161. return -EIO;
  1162. }
  1163. if (lba48)
  1164. *max_sectors = ata_tf_to_lba48(&tf) + 1;
  1165. else
  1166. *max_sectors = ata_tf_to_lba(&tf) + 1;
  1167. if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
  1168. (*max_sectors)--;
  1169. return 0;
  1170. }
  1171. /**
  1172. * ata_set_max_sectors - Set max sectors
  1173. * @dev: target device
  1174. * @new_sectors: new max sectors value to set for the device
  1175. *
  1176. * Set max sectors of @dev to @new_sectors.
  1177. *
  1178. * RETURNS:
  1179. * 0 on success, -EACCES if command is aborted or denied (due to
  1180. * previous non-volatile SET_MAX) by the drive. -EIO on other
  1181. * errors.
  1182. */
  1183. static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
  1184. {
  1185. unsigned int err_mask;
  1186. struct ata_taskfile tf;
  1187. int lba48 = ata_id_has_lba48(dev->id);
  1188. new_sectors--;
  1189. ata_tf_init(dev, &tf);
  1190. tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  1191. if (lba48) {
  1192. tf.command = ATA_CMD_SET_MAX_EXT;
  1193. tf.flags |= ATA_TFLAG_LBA48;
  1194. tf.hob_lbal = (new_sectors >> 24) & 0xff;
  1195. tf.hob_lbam = (new_sectors >> 32) & 0xff;
  1196. tf.hob_lbah = (new_sectors >> 40) & 0xff;
  1197. } else {
  1198. tf.command = ATA_CMD_SET_MAX;
  1199. tf.device |= (new_sectors >> 24) & 0xf;
  1200. }
  1201. tf.protocol |= ATA_PROT_NODATA;
  1202. tf.device |= ATA_LBA;
  1203. tf.lbal = (new_sectors >> 0) & 0xff;
  1204. tf.lbam = (new_sectors >> 8) & 0xff;
  1205. tf.lbah = (new_sectors >> 16) & 0xff;
  1206. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
  1207. if (err_mask) {
  1208. ata_dev_printk(dev, KERN_WARNING, "failed to set "
  1209. "max address (err_mask=0x%x)\n", err_mask);
  1210. if (err_mask == AC_ERR_DEV &&
  1211. (tf.feature & (ATA_ABORTED | ATA_IDNF)))
  1212. return -EACCES;
  1213. return -EIO;
  1214. }
  1215. return 0;
  1216. }
  1217. /**
  1218. * ata_hpa_resize - Resize a device with an HPA set
  1219. * @dev: Device to resize
  1220. *
  1221. * Read the size of an LBA28 or LBA48 disk with HPA features and resize
  1222. * it if required to the full size of the media. The caller must check
  1223. * the drive has the HPA feature set enabled.
  1224. *
  1225. * RETURNS:
  1226. * 0 on success, -errno on failure.
  1227. */
  1228. static int ata_hpa_resize(struct ata_device *dev)
  1229. {
  1230. struct ata_eh_context *ehc = &dev->link->eh_context;
  1231. int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
  1232. u64 sectors = ata_id_n_sectors(dev->id);
  1233. u64 native_sectors;
  1234. int rc;
  1235. /* do we need to do it? */
  1236. if (dev->class != ATA_DEV_ATA ||
  1237. !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
  1238. (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
  1239. return 0;
  1240. /* read native max address */
  1241. rc = ata_read_native_max_address(dev, &native_sectors);
  1242. if (rc) {
  1243. /* If device aborted the command or HPA isn't going to
  1244. * be unlocked, skip HPA resizing.
  1245. */
  1246. if (rc == -EACCES || !ata_ignore_hpa) {
  1247. ata_dev_printk(dev, KERN_WARNING, "HPA support seems "
  1248. "broken, skipping HPA handling\n");
  1249. dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
  1250. /* we can continue if device aborted the command */
  1251. if (rc == -EACCES)
  1252. rc = 0;
  1253. }
  1254. return rc;
  1255. }
  1256. /* nothing to do? */
  1257. if (native_sectors <= sectors || !ata_ignore_hpa) {
  1258. if (!print_info || native_sectors == sectors)
  1259. return 0;
  1260. if (native_sectors > sectors)
  1261. ata_dev_printk(dev, KERN_INFO,
  1262. "HPA detected: current %llu, native %llu\n",
  1263. (unsigned long long)sectors,
  1264. (unsigned long long)native_sectors);
  1265. else if (native_sectors < sectors)
  1266. ata_dev_printk(dev, KERN_WARNING,
  1267. "native sectors (%llu) is smaller than "
  1268. "sectors (%llu)\n",
  1269. (unsigned long long)native_sectors,
  1270. (unsigned long long)sectors);
  1271. return 0;
  1272. }
  1273. /* let's unlock HPA */
  1274. rc = ata_set_max_sectors(dev, native_sectors);
  1275. if (rc == -EACCES) {
  1276. /* if device aborted the command, skip HPA resizing */
  1277. ata_dev_printk(dev, KERN_WARNING, "device aborted resize "
  1278. "(%llu -> %llu), skipping HPA handling\n",
  1279. (unsigned long long)sectors,
  1280. (unsigned long long)native_sectors);
  1281. dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
  1282. return 0;
  1283. } else if (rc)
  1284. return rc;
  1285. /* re-read IDENTIFY data */
  1286. rc = ata_dev_reread_id(dev, 0);
  1287. if (rc) {
  1288. ata_dev_printk(dev, KERN_ERR, "failed to re-read IDENTIFY "
  1289. "data after HPA resizing\n");
  1290. return rc;
  1291. }
  1292. if (print_info) {
  1293. u64 new_sectors = ata_id_n_sectors(dev->id);
  1294. ata_dev_printk(dev, KERN_INFO,
  1295. "HPA unlocked: %llu -> %llu, native %llu\n",
  1296. (unsigned long long)sectors,
  1297. (unsigned long long)new_sectors,
  1298. (unsigned long long)native_sectors);
  1299. }
  1300. return 0;
  1301. }
  1302. /**
  1303. * ata_dump_id - IDENTIFY DEVICE info debugging output
  1304. * @id: IDENTIFY DEVICE page to dump
  1305. *
  1306. * Dump selected 16-bit words from the given IDENTIFY DEVICE
  1307. * page.
  1308. *
  1309. * LOCKING:
  1310. * caller.
  1311. */
  1312. static inline void ata_dump_id(const u16 *id)
  1313. {
  1314. DPRINTK("49==0x%04x "
  1315. "53==0x%04x "
  1316. "63==0x%04x "
  1317. "64==0x%04x "
  1318. "75==0x%04x \n",
  1319. id[49],
  1320. id[53],
  1321. id[63],
  1322. id[64],
  1323. id[75]);
  1324. DPRINTK("80==0x%04x "
  1325. "81==0x%04x "
  1326. "82==0x%04x "
  1327. "83==0x%04x "
  1328. "84==0x%04x \n",
  1329. id[80],
  1330. id[81],
  1331. id[82],
  1332. id[83],
  1333. id[84]);
  1334. DPRINTK("88==0x%04x "
  1335. "93==0x%04x\n",
  1336. id[88],
  1337. id[93]);
  1338. }
  1339. /**
  1340. * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
  1341. * @id: IDENTIFY data to compute xfer mask from
  1342. *
  1343. * Compute the xfermask for this device. This is not as trivial
  1344. * as it seems if we must consider early devices correctly.
  1345. *
  1346. * FIXME: pre IDE drive timing (do we care ?).
  1347. *
  1348. * LOCKING:
  1349. * None.
  1350. *
  1351. * RETURNS:
  1352. * Computed xfermask
  1353. */
  1354. unsigned long ata_id_xfermask(const u16 *id)
  1355. {
  1356. unsigned long pio_mask, mwdma_mask, udma_mask;
  1357. /* Usual case. Word 53 indicates word 64 is valid */
  1358. if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
  1359. pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
  1360. pio_mask <<= 3;
  1361. pio_mask |= 0x7;
  1362. } else {
  1363. /* If word 64 isn't valid then Word 51 high byte holds
  1364. * the PIO timing number for the maximum. Turn it into
  1365. * a mask.
  1366. */
  1367. u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
  1368. if (mode < 5) /* Valid PIO range */
  1369. pio_mask = (2 << mode) - 1;
  1370. else
  1371. pio_mask = 1;
  1372. /* But wait.. there's more. Design your standards by
  1373. * committee and you too can get a free iordy field to
  1374. * process. However its the speeds not the modes that
  1375. * are supported... Note drivers using the timing API
  1376. * will get this right anyway
  1377. */
  1378. }
  1379. mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
  1380. if (ata_id_is_cfa(id)) {
  1381. /*
  1382. * Process compact flash extended modes
  1383. */
  1384. int pio = id[163] & 0x7;
  1385. int dma = (id[163] >> 3) & 7;
  1386. if (pio)
  1387. pio_mask |= (1 << 5);
  1388. if (pio > 1)
  1389. pio_mask |= (1 << 6);
  1390. if (dma)
  1391. mwdma_mask |= (1 << 3);
  1392. if (dma > 1)
  1393. mwdma_mask |= (1 << 4);
  1394. }
  1395. udma_mask = 0;
  1396. if (id[ATA_ID_FIELD_VALID] & (1 << 2))
  1397. udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
  1398. return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
  1399. }
  1400. /**
  1401. * ata_pio_queue_task - Queue port_task
  1402. * @ap: The ata_port to queue port_task for
  1403. * @data: data for @fn to use
  1404. * @delay: delay time in msecs for workqueue function
  1405. *
  1406. * Schedule @fn(@data) for execution after @delay jiffies using
  1407. * port_task. There is one port_task per port and it's the
  1408. * user(low level driver)'s responsibility to make sure that only
  1409. * one task is active at any given time.
  1410. *
  1411. * libata core layer takes care of synchronization between
  1412. * port_task and EH. ata_pio_queue_task() may be ignored for EH
  1413. * synchronization.
  1414. *
  1415. * LOCKING:
  1416. * Inherited from caller.
  1417. */
  1418. void ata_pio_queue_task(struct ata_port *ap, void *data, unsigned long delay)
  1419. {
  1420. ap->port_task_data = data;
  1421. /* may fail if ata_port_flush_task() in progress */
  1422. queue_delayed_work(ata_wq, &ap->port_task, msecs_to_jiffies(delay));
  1423. }
  1424. /**
  1425. * ata_port_flush_task - Flush port_task
  1426. * @ap: The ata_port to flush port_task for
  1427. *
  1428. * After this function completes, port_task is guranteed not to
  1429. * be running or scheduled.
  1430. *
  1431. * LOCKING:
  1432. * Kernel thread context (may sleep)
  1433. */
  1434. void ata_port_flush_task(struct ata_port *ap)
  1435. {
  1436. DPRINTK("ENTER\n");
  1437. cancel_rearming_delayed_work(&ap->port_task);
  1438. if (ata_msg_ctl(ap))
  1439. ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __func__);
  1440. }
  1441. static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
  1442. {
  1443. struct completion *waiting = qc->private_data;
  1444. complete(waiting);
  1445. }
  1446. /**
  1447. * ata_exec_internal_sg - execute libata internal command
  1448. * @dev: Device to which the command is sent
  1449. * @tf: Taskfile registers for the command and the result
  1450. * @cdb: CDB for packet command
  1451. * @dma_dir: Data tranfer direction of the command
  1452. * @sgl: sg list for the data buffer of the command
  1453. * @n_elem: Number of sg entries
  1454. * @timeout: Timeout in msecs (0 for default)
  1455. *
  1456. * Executes libata internal command with timeout. @tf contains
  1457. * command on entry and result on return. Timeout and error
  1458. * conditions are reported via return value. No recovery action
  1459. * is taken after a command times out. It's caller's duty to
  1460. * clean up after timeout.
  1461. *
  1462. * LOCKING:
  1463. * None. Should be called with kernel context, might sleep.
  1464. *
  1465. * RETURNS:
  1466. * Zero on success, AC_ERR_* mask on failure
  1467. */
  1468. unsigned ata_exec_internal_sg(struct ata_device *dev,
  1469. struct ata_taskfile *tf, const u8 *cdb,
  1470. int dma_dir, struct scatterlist *sgl,
  1471. unsigned int n_elem, unsigned long timeout)
  1472. {
  1473. struct ata_link *link = dev->link;
  1474. struct ata_port *ap = link->ap;
  1475. u8 command = tf->command;
  1476. int auto_timeout = 0;
  1477. struct ata_queued_cmd *qc;
  1478. unsigned int tag, preempted_tag;
  1479. u32 preempted_sactive, preempted_qc_active;
  1480. int preempted_nr_active_links;
  1481. DECLARE_COMPLETION_ONSTACK(wait);
  1482. unsigned long flags;
  1483. unsigned int err_mask;
  1484. int rc;
  1485. spin_lock_irqsave(ap->lock, flags);
  1486. /* no internal command while frozen */
  1487. if (ap->pflags & ATA_PFLAG_FROZEN) {
  1488. spin_unlock_irqrestore(ap->lock, flags);
  1489. return AC_ERR_SYSTEM;
  1490. }
  1491. /* initialize internal qc */
  1492. /* XXX: Tag 0 is used for drivers with legacy EH as some
  1493. * drivers choke if any other tag is given. This breaks
  1494. * ata_tag_internal() test for those drivers. Don't use new
  1495. * EH stuff without converting to it.
  1496. */
  1497. if (ap->ops->error_handler)
  1498. tag = ATA_TAG_INTERNAL;
  1499. else
  1500. tag = 0;
  1501. if (test_and_set_bit(tag, &ap->qc_allocated))
  1502. BUG();
  1503. qc = __ata_qc_from_tag(ap, tag);
  1504. qc->tag = tag;
  1505. qc->scsicmd = NULL;
  1506. qc->ap = ap;
  1507. qc->dev = dev;
  1508. ata_qc_reinit(qc);
  1509. preempted_tag = link->active_tag;
  1510. preempted_sactive = link->sactive;
  1511. preempted_qc_active = ap->qc_active;
  1512. preempted_nr_active_links = ap->nr_active_links;
  1513. link->active_tag = ATA_TAG_POISON;
  1514. link->sactive = 0;
  1515. ap->qc_active = 0;
  1516. ap->nr_active_links = 0;
  1517. /* prepare & issue qc */
  1518. qc->tf = *tf;
  1519. if (cdb)
  1520. memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
  1521. qc->flags |= ATA_QCFLAG_RESULT_TF;
  1522. qc->dma_dir = dma_dir;
  1523. if (dma_dir != DMA_NONE) {
  1524. unsigned int i, buflen = 0;
  1525. struct scatterlist *sg;
  1526. for_each_sg(sgl, sg, n_elem, i)
  1527. buflen += sg->length;
  1528. ata_sg_init(qc, sgl, n_elem);
  1529. qc->nbytes = buflen;
  1530. }
  1531. qc->private_data = &wait;
  1532. qc->complete_fn = ata_qc_complete_internal;
  1533. ata_qc_issue(qc);
  1534. spin_unlock_irqrestore(ap->lock, flags);
  1535. if (!timeout) {
  1536. if (ata_probe_timeout)
  1537. timeout = ata_probe_timeout * 1000;
  1538. else {
  1539. timeout = ata_internal_cmd_timeout(dev, command);
  1540. auto_timeout = 1;
  1541. }
  1542. }
  1543. rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
  1544. ata_port_flush_task(ap);
  1545. if (!rc) {
  1546. spin_lock_irqsave(ap->lock, flags);
  1547. /* We're racing with irq here. If we lose, the
  1548. * following test prevents us from completing the qc
  1549. * twice. If we win, the port is frozen and will be
  1550. * cleaned up by ->post_internal_cmd().
  1551. */
  1552. if (qc->flags & ATA_QCFLAG_ACTIVE) {
  1553. qc->err_mask |= AC_ERR_TIMEOUT;
  1554. if (ap->ops->error_handler)
  1555. ata_port_freeze(ap);
  1556. else
  1557. ata_qc_complete(qc);
  1558. if (ata_msg_warn(ap))
  1559. ata_dev_printk(dev, KERN_WARNING,
  1560. "qc timeout (cmd 0x%x)\n", command);
  1561. }
  1562. spin_unlock_irqrestore(ap->lock, flags);
  1563. }
  1564. /* do post_internal_cmd */
  1565. if (ap->ops->post_internal_cmd)
  1566. ap->ops->post_internal_cmd(qc);
  1567. /* perform minimal error analysis */
  1568. if (qc->flags & ATA_QCFLAG_FAILED) {
  1569. if (qc->result_tf.command & (ATA_ERR | ATA_DF))
  1570. qc->err_mask |= AC_ERR_DEV;
  1571. if (!qc->err_mask)
  1572. qc->err_mask |= AC_ERR_OTHER;
  1573. if (qc->err_mask & ~AC_ERR_OTHER)
  1574. qc->err_mask &= ~AC_ERR_OTHER;
  1575. }
  1576. /* finish up */
  1577. spin_lock_irqsave(ap->lock, flags);
  1578. *tf = qc->result_tf;
  1579. err_mask = qc->err_mask;
  1580. ata_qc_free(qc);
  1581. link->active_tag = preempted_tag;
  1582. link->sactive = preempted_sactive;
  1583. ap->qc_active = preempted_qc_active;
  1584. ap->nr_active_links = preempted_nr_active_links;
  1585. /* XXX - Some LLDDs (sata_mv) disable port on command failure.
  1586. * Until those drivers are fixed, we detect the condition
  1587. * here, fail the command with AC_ERR_SYSTEM and reenable the
  1588. * port.
  1589. *
  1590. * Note that this doesn't change any behavior as internal
  1591. * command failure results in disabling the device in the
  1592. * higher layer for LLDDs without new reset/EH callbacks.
  1593. *
  1594. * Kill the following code as soon as those drivers are fixed.
  1595. */
  1596. if (ap->flags & ATA_FLAG_DISABLED) {
  1597. err_mask |= AC_ERR_SYSTEM;
  1598. ata_port_probe(ap);
  1599. }
  1600. spin_unlock_irqrestore(ap->lock, flags);
  1601. if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
  1602. ata_internal_cmd_timed_out(dev, command);
  1603. return err_mask;
  1604. }
  1605. /**
  1606. * ata_exec_internal - execute libata internal command
  1607. * @dev: Device to which the command is sent
  1608. * @tf: Taskfile registers for the command and the result
  1609. * @cdb: CDB for packet command
  1610. * @dma_dir: Data tranfer direction of the command
  1611. * @buf: Data buffer of the command
  1612. * @buflen: Length of data buffer
  1613. * @timeout: Timeout in msecs (0 for default)
  1614. *
  1615. * Wrapper around ata_exec_internal_sg() which takes simple
  1616. * buffer instead of sg list.
  1617. *
  1618. * LOCKING:
  1619. * None. Should be called with kernel context, might sleep.
  1620. *
  1621. * RETURNS:
  1622. * Zero on success, AC_ERR_* mask on failure
  1623. */
  1624. unsigned ata_exec_internal(struct ata_device *dev,
  1625. struct ata_taskfile *tf, const u8 *cdb,
  1626. int dma_dir, void *buf, unsigned int buflen,
  1627. unsigned long timeout)
  1628. {
  1629. struct scatterlist *psg = NULL, sg;
  1630. unsigned int n_elem = 0;
  1631. if (dma_dir != DMA_NONE) {
  1632. WARN_ON(!buf);
  1633. sg_init_one(&sg, buf, buflen);
  1634. psg = &sg;
  1635. n_elem++;
  1636. }
  1637. return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
  1638. timeout);
  1639. }
  1640. /**
  1641. * ata_do_simple_cmd - execute simple internal command
  1642. * @dev: Device to which the command is sent
  1643. * @cmd: Opcode to execute
  1644. *
  1645. * Execute a 'simple' command, that only consists of the opcode
  1646. * 'cmd' itself, without filling any other registers
  1647. *
  1648. * LOCKING:
  1649. * Kernel thread context (may sleep).
  1650. *
  1651. * RETURNS:
  1652. * Zero on success, AC_ERR_* mask on failure
  1653. */
  1654. unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
  1655. {
  1656. struct ata_taskfile tf;
  1657. ata_tf_init(dev, &tf);
  1658. tf.command = cmd;
  1659. tf.flags |= ATA_TFLAG_DEVICE;
  1660. tf.protocol = ATA_PROT_NODATA;
  1661. return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
  1662. }
  1663. /**
  1664. * ata_pio_need_iordy - check if iordy needed
  1665. * @adev: ATA device
  1666. *
  1667. * Check if the current speed of the device requires IORDY. Used
  1668. * by various controllers for chip configuration.
  1669. */
  1670. unsigned int ata_pio_need_iordy(const struct ata_device *adev)
  1671. {
  1672. /* Controller doesn't support IORDY. Probably a pointless check
  1673. as the caller should know this */
  1674. if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
  1675. return 0;
  1676. /* PIO3 and higher it is mandatory */
  1677. if (adev->pio_mode > XFER_PIO_2)
  1678. return 1;
  1679. /* We turn it on when possible */
  1680. if (ata_id_has_iordy(adev->id))
  1681. return 1;
  1682. return 0;
  1683. }
  1684. /**
  1685. * ata_pio_mask_no_iordy - Return the non IORDY mask
  1686. * @adev: ATA device
  1687. *
  1688. * Compute the highest mode possible if we are not using iordy. Return
  1689. * -1 if no iordy mode is available.
  1690. */
  1691. static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
  1692. {
  1693. /* If we have no drive specific rule, then PIO 2 is non IORDY */
  1694. if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
  1695. u16 pio = adev->id[ATA_ID_EIDE_PIO];
  1696. /* Is the speed faster than the drive allows non IORDY ? */
  1697. if (pio) {
  1698. /* This is cycle times not frequency - watch the logic! */
  1699. if (pio > 240) /* PIO2 is 240nS per cycle */
  1700. return 3 << ATA_SHIFT_PIO;
  1701. return 7 << ATA_SHIFT_PIO;
  1702. }
  1703. }
  1704. return 3 << ATA_SHIFT_PIO;
  1705. }
  1706. /**
  1707. * ata_do_dev_read_id - default ID read method
  1708. * @dev: device
  1709. * @tf: proposed taskfile
  1710. * @id: data buffer
  1711. *
  1712. * Issue the identify taskfile and hand back the buffer containing
  1713. * identify data. For some RAID controllers and for pre ATA devices
  1714. * this function is wrapped or replaced by the driver
  1715. */
  1716. unsigned int ata_do_dev_read_id(struct ata_device *dev,
  1717. struct ata_taskfile *tf, u16 *id)
  1718. {
  1719. return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
  1720. id, sizeof(id[0]) * ATA_ID_WORDS, 0);
  1721. }
  1722. /**
  1723. * ata_dev_read_id - Read ID data from the specified device
  1724. * @dev: target device
  1725. * @p_class: pointer to class of the target device (may be changed)
  1726. * @flags: ATA_READID_* flags
  1727. * @id: buffer to read IDENTIFY data into
  1728. *
  1729. * Read ID data from the specified device. ATA_CMD_ID_ATA is
  1730. * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
  1731. * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
  1732. * for pre-ATA4 drives.
  1733. *
  1734. * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
  1735. * now we abort if we hit that case.
  1736. *
  1737. * LOCKING:
  1738. * Kernel thread context (may sleep)
  1739. *
  1740. * RETURNS:
  1741. * 0 on success, -errno otherwise.
  1742. */
  1743. int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
  1744. unsigned int flags, u16 *id)
  1745. {
  1746. struct ata_port *ap = dev->link->ap;
  1747. unsigned int class = *p_class;
  1748. struct ata_taskfile tf;
  1749. unsigned int err_mask = 0;
  1750. const char *reason;
  1751. int may_fallback = 1, tried_spinup = 0;
  1752. int rc;
  1753. if (ata_msg_ctl(ap))
  1754. ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
  1755. retry:
  1756. ata_tf_init(dev, &tf);
  1757. switch (class) {
  1758. case ATA_DEV_ATA:
  1759. tf.command = ATA_CMD_ID_ATA;
  1760. break;
  1761. case ATA_DEV_ATAPI:
  1762. tf.command = ATA_CMD_ID_ATAPI;
  1763. break;
  1764. default:
  1765. rc = -ENODEV;
  1766. reason = "unsupported class";
  1767. goto err_out;
  1768. }
  1769. tf.protocol = ATA_PROT_PIO;
  1770. /* Some devices choke if TF registers contain garbage. Make
  1771. * sure those are properly initialized.
  1772. */
  1773. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1774. /* Device presence detection is unreliable on some
  1775. * controllers. Always poll IDENTIFY if available.
  1776. */
  1777. tf.flags |= ATA_TFLAG_POLLING;
  1778. if (ap->ops->read_id)
  1779. err_mask = ap->ops->read_id(dev, &tf, id);
  1780. else
  1781. err_mask = ata_do_dev_read_id(dev, &tf, id);
  1782. if (err_mask) {
  1783. if (err_mask & AC_ERR_NODEV_HINT) {
  1784. ata_dev_printk(dev, KERN_DEBUG,
  1785. "NODEV after polling detection\n");
  1786. return -ENOENT;
  1787. }
  1788. if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
  1789. /* Device or controller might have reported
  1790. * the wrong device class. Give a shot at the
  1791. * other IDENTIFY if the current one is
  1792. * aborted by the device.
  1793. */
  1794. if (may_fallback) {
  1795. may_fallback = 0;
  1796. if (class == ATA_DEV_ATA)
  1797. class = ATA_DEV_ATAPI;
  1798. else
  1799. class = ATA_DEV_ATA;
  1800. goto retry;
  1801. }
  1802. /* Control reaches here iff the device aborted
  1803. * both flavors of IDENTIFYs which happens
  1804. * sometimes with phantom devices.
  1805. */
  1806. ata_dev_printk(dev, KERN_DEBUG,
  1807. "both IDENTIFYs aborted, assuming NODEV\n");
  1808. return -ENOENT;
  1809. }
  1810. rc = -EIO;
  1811. reason = "I/O error";
  1812. goto err_out;
  1813. }
  1814. /* Falling back doesn't make sense if ID data was read
  1815. * successfully at least once.
  1816. */
  1817. may_fallback = 0;
  1818. swap_buf_le16(id, ATA_ID_WORDS);
  1819. /* sanity check */
  1820. rc = -EINVAL;
  1821. reason = "device reports invalid type";
  1822. if (class == ATA_DEV_ATA) {
  1823. if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
  1824. goto err_out;
  1825. } else {
  1826. if (ata_id_is_ata(id))
  1827. goto err_out;
  1828. }
  1829. if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
  1830. tried_spinup = 1;
  1831. /*
  1832. * Drive powered-up in standby mode, and requires a specific
  1833. * SET_FEATURES spin-up subcommand before it will accept
  1834. * anything other than the original IDENTIFY command.
  1835. */
  1836. err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
  1837. if (err_mask && id[2] != 0x738c) {
  1838. rc = -EIO;
  1839. reason = "SPINUP failed";
  1840. goto err_out;
  1841. }
  1842. /*
  1843. * If the drive initially returned incomplete IDENTIFY info,
  1844. * we now must reissue the IDENTIFY command.
  1845. */
  1846. if (id[2] == 0x37c8)
  1847. goto retry;
  1848. }
  1849. if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
  1850. /*
  1851. * The exact sequence expected by certain pre-ATA4 drives is:
  1852. * SRST RESET
  1853. * IDENTIFY (optional in early ATA)
  1854. * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
  1855. * anything else..
  1856. * Some drives were very specific about that exact sequence.
  1857. *
  1858. * Note that ATA4 says lba is mandatory so the second check
  1859. * shoud never trigger.
  1860. */
  1861. if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
  1862. err_mask = ata_dev_init_params(dev, id[3], id[6]);
  1863. if (err_mask) {
  1864. rc = -EIO;
  1865. reason = "INIT_DEV_PARAMS failed";
  1866. goto err_out;
  1867. }
  1868. /* current CHS translation info (id[53-58]) might be
  1869. * changed. reread the identify device info.
  1870. */
  1871. flags &= ~ATA_READID_POSTRESET;
  1872. goto retry;
  1873. }
  1874. }
  1875. *p_class = class;
  1876. return 0;
  1877. err_out:
  1878. if (ata_msg_warn(ap))
  1879. ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
  1880. "(%s, err_mask=0x%x)\n", reason, err_mask);
  1881. return rc;
  1882. }
  1883. static inline u8 ata_dev_knobble(struct ata_device *dev)
  1884. {
  1885. struct ata_port *ap = dev->link->ap;
  1886. if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
  1887. return 0;
  1888. return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
  1889. }
  1890. static void ata_dev_config_ncq(struct ata_device *dev,
  1891. char *desc, size_t desc_sz)
  1892. {
  1893. struct ata_port *ap = dev->link->ap;
  1894. int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
  1895. if (!ata_id_has_ncq(dev->id)) {
  1896. desc[0] = '\0';
  1897. return;
  1898. }
  1899. if (dev->horkage & ATA_HORKAGE_NONCQ) {
  1900. snprintf(desc, desc_sz, "NCQ (not used)");
  1901. return;
  1902. }
  1903. if (ap->flags & ATA_FLAG_NCQ) {
  1904. hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
  1905. dev->flags |= ATA_DFLAG_NCQ;
  1906. }
  1907. if (hdepth >= ddepth)
  1908. snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
  1909. else
  1910. snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
  1911. }
  1912. /**
  1913. * ata_dev_configure - Configure the specified ATA/ATAPI device
  1914. * @dev: Target device to configure
  1915. *
  1916. * Configure @dev according to @dev->id. Generic and low-level
  1917. * driver specific fixups are also applied.
  1918. *
  1919. * LOCKING:
  1920. * Kernel thread context (may sleep)
  1921. *
  1922. * RETURNS:
  1923. * 0 on success, -errno otherwise
  1924. */
  1925. int ata_dev_configure(struct ata_device *dev)
  1926. {
  1927. struct ata_port *ap = dev->link->ap;
  1928. struct ata_eh_context *ehc = &dev->link->eh_context;
  1929. int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
  1930. const u16 *id = dev->id;
  1931. unsigned long xfer_mask;
  1932. char revbuf[7]; /* XYZ-99\0 */
  1933. char fwrevbuf[ATA_ID_FW_REV_LEN+1];
  1934. char modelbuf[ATA_ID_PROD_LEN+1];
  1935. int rc;
  1936. if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
  1937. ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT -- nodev\n",
  1938. __func__);
  1939. return 0;
  1940. }
  1941. if (ata_msg_probe(ap))
  1942. ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
  1943. /* set horkage */
  1944. dev->horkage |= ata_dev_blacklisted(dev);
  1945. ata_force_horkage(dev);
  1946. if (dev->horkage & ATA_HORKAGE_DISABLE) {
  1947. ata_dev_printk(dev, KERN_INFO,
  1948. "unsupported device, disabling\n");
  1949. ata_dev_disable(dev);
  1950. return 0;
  1951. }
  1952. if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
  1953. dev->class == ATA_DEV_ATAPI) {
  1954. ata_dev_printk(dev, KERN_WARNING,
  1955. "WARNING: ATAPI is %s, device ignored.\n",
  1956. atapi_enabled ? "not supported with this driver"
  1957. : "disabled");
  1958. ata_dev_disable(dev);
  1959. return 0;
  1960. }
  1961. /* let ACPI work its magic */
  1962. rc = ata_acpi_on_devcfg(dev);
  1963. if (rc)
  1964. return rc;
  1965. /* massage HPA, do it early as it might change IDENTIFY data */
  1966. rc = ata_hpa_resize(dev);
  1967. if (rc)
  1968. return rc;
  1969. /* print device capabilities */
  1970. if (ata_msg_probe(ap))
  1971. ata_dev_printk(dev, KERN_DEBUG,
  1972. "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
  1973. "85:%04x 86:%04x 87:%04x 88:%04x\n",
  1974. __func__,
  1975. id[49], id[82], id[83], id[84],
  1976. id[85], id[86], id[87], id[88]);
  1977. /* initialize to-be-configured parameters */
  1978. dev->flags &= ~ATA_DFLAG_CFG_MASK;
  1979. dev->max_sectors = 0;
  1980. dev->cdb_len = 0;
  1981. dev->n_sectors = 0;
  1982. dev->cylinders = 0;
  1983. dev->heads = 0;
  1984. dev->sectors = 0;
  1985. /*
  1986. * common ATA, ATAPI feature tests
  1987. */
  1988. /* find max transfer mode; for printk only */
  1989. xfer_mask = ata_id_xfermask(id);
  1990. if (ata_msg_probe(ap))
  1991. ata_dump_id(id);
  1992. /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
  1993. ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
  1994. sizeof(fwrevbuf));
  1995. ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
  1996. sizeof(modelbuf));
  1997. /* ATA-specific feature tests */
  1998. if (dev->class == ATA_DEV_ATA) {
  1999. if (ata_id_is_cfa(id)) {
  2000. if (id[162] & 1) /* CPRM may make this media unusable */
  2001. ata_dev_printk(dev, KERN_WARNING,
  2002. "supports DRM functions and may "
  2003. "not be fully accessable.\n");
  2004. snprintf(revbuf, 7, "CFA");
  2005. } else {
  2006. snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
  2007. /* Warn the user if the device has TPM extensions */
  2008. if (ata_id_has_tpm(id))
  2009. ata_dev_printk(dev, KERN_WARNING,
  2010. "supports DRM functions and may "
  2011. "not be fully accessable.\n");
  2012. }
  2013. dev->n_sectors = ata_id_n_sectors(id);
  2014. if (dev->id[59] & 0x100)
  2015. dev->multi_count = dev->id[59] & 0xff;
  2016. if (ata_id_has_lba(id)) {
  2017. const char *lba_desc;
  2018. char ncq_desc[20];
  2019. lba_desc = "LBA";
  2020. dev->flags |= ATA_DFLAG_LBA;
  2021. if (ata_id_has_lba48(id)) {
  2022. dev->flags |= ATA_DFLAG_LBA48;
  2023. lba_desc = "LBA48";
  2024. if (dev->n_sectors >= (1UL << 28) &&
  2025. ata_id_has_flush_ext(id))
  2026. dev->flags |= ATA_DFLAG_FLUSH_EXT;
  2027. }
  2028. /* config NCQ */
  2029. ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
  2030. /* print device info to dmesg */
  2031. if (ata_msg_drv(ap) && print_info) {
  2032. ata_dev_printk(dev, KERN_INFO,
  2033. "%s: %s, %s, max %s\n",
  2034. revbuf, modelbuf, fwrevbuf,
  2035. ata_mode_string(xfer_mask));
  2036. ata_dev_printk(dev, KERN_INFO,
  2037. "%Lu sectors, multi %u: %s %s\n",
  2038. (unsigned long long)dev->n_sectors,
  2039. dev->multi_count, lba_desc, ncq_desc);
  2040. }
  2041. } else {
  2042. /* CHS */
  2043. /* Default translation */
  2044. dev->cylinders = id[1];
  2045. dev->heads = id[3];
  2046. dev->sectors = id[6];
  2047. if (ata_id_current_chs_valid(id)) {
  2048. /* Current CHS translation is valid. */
  2049. dev->cylinders = id[54];
  2050. dev->heads = id[55];
  2051. dev->sectors = id[56];
  2052. }
  2053. /* print device info to dmesg */
  2054. if (ata_msg_drv(ap) && print_info) {
  2055. ata_dev_printk(dev, KERN_INFO,
  2056. "%s: %s, %s, max %s\n",
  2057. revbuf, modelbuf, fwrevbuf,
  2058. ata_mode_string(xfer_mask));
  2059. ata_dev_printk(dev, KERN_INFO,
  2060. "%Lu sectors, multi %u, CHS %u/%u/%u\n",
  2061. (unsigned long long)dev->n_sectors,
  2062. dev->multi_count, dev->cylinders,
  2063. dev->heads, dev->sectors);
  2064. }
  2065. }
  2066. dev->cdb_len = 16;
  2067. }
  2068. /* ATAPI-specific feature tests */
  2069. else if (dev->class == ATA_DEV_ATAPI) {
  2070. const char *cdb_intr_string = "";
  2071. const char *atapi_an_string = "";
  2072. const char *dma_dir_string = "";
  2073. u32 sntf;
  2074. rc = atapi_cdb_len(id);
  2075. if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
  2076. if (ata_msg_warn(ap))
  2077. ata_dev_printk(dev, KERN_WARNING,
  2078. "unsupported CDB len\n");
  2079. rc = -EINVAL;
  2080. goto err_out_nosup;
  2081. }
  2082. dev->cdb_len = (unsigned int) rc;
  2083. /* Enable ATAPI AN if both the host and device have
  2084. * the support. If PMP is attached, SNTF is required
  2085. * to enable ATAPI AN to discern between PHY status
  2086. * changed notifications and ATAPI ANs.
  2087. */
  2088. if ((ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
  2089. (!sata_pmp_attached(ap) ||
  2090. sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
  2091. unsigned int err_mask;
  2092. /* issue SET feature command to turn this on */
  2093. err_mask = ata_dev_set_feature(dev,
  2094. SETFEATURES_SATA_ENABLE, SATA_AN);
  2095. if (err_mask)
  2096. ata_dev_printk(dev, KERN_ERR,
  2097. "failed to enable ATAPI AN "
  2098. "(err_mask=0x%x)\n", err_mask);
  2099. else {
  2100. dev->flags |= ATA_DFLAG_AN;
  2101. atapi_an_string = ", ATAPI AN";
  2102. }
  2103. }
  2104. if (ata_id_cdb_intr(dev->id)) {
  2105. dev->flags |= ATA_DFLAG_CDB_INTR;
  2106. cdb_intr_string = ", CDB intr";
  2107. }
  2108. if (atapi_dmadir || atapi_id_dmadir(dev->id)) {
  2109. dev->flags |= ATA_DFLAG_DMADIR;
  2110. dma_dir_string = ", DMADIR";
  2111. }
  2112. /* print device info to dmesg */
  2113. if (ata_msg_drv(ap) && print_info)
  2114. ata_dev_printk(dev, KERN_INFO,
  2115. "ATAPI: %s, %s, max %s%s%s%s\n",
  2116. modelbuf, fwrevbuf,
  2117. ata_mode_string(xfer_mask),
  2118. cdb_intr_string, atapi_an_string,
  2119. dma_dir_string);
  2120. }
  2121. /* determine max_sectors */
  2122. dev->max_sectors = ATA_MAX_SECTORS;
  2123. if (dev->flags & ATA_DFLAG_LBA48)
  2124. dev->max_sectors = ATA_MAX_SECTORS_LBA48;
  2125. if (!(dev->horkage & ATA_HORKAGE_IPM)) {
  2126. if (ata_id_has_hipm(dev->id))
  2127. dev->flags |= ATA_DFLAG_HIPM;
  2128. if (ata_id_has_dipm(dev->id))
  2129. dev->flags |= ATA_DFLAG_DIPM;
  2130. }
  2131. /* Limit PATA drive on SATA cable bridge transfers to udma5,
  2132. 200 sectors */
  2133. if (ata_dev_knobble(dev)) {
  2134. if (ata_msg_drv(ap) && print_info)
  2135. ata_dev_printk(dev, KERN_INFO,
  2136. "applying bridge limits\n");
  2137. dev->udma_mask &= ATA_UDMA5;
  2138. dev->max_sectors = ATA_MAX_SECTORS;
  2139. }
  2140. if ((dev->class == ATA_DEV_ATAPI) &&
  2141. (atapi_command_packet_set(id) == TYPE_TAPE)) {
  2142. dev->max_sectors = ATA_MAX_SECTORS_TAPE;
  2143. dev->horkage |= ATA_HORKAGE_STUCK_ERR;
  2144. }
  2145. if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
  2146. dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
  2147. dev->max_sectors);
  2148. if (ata_dev_blacklisted(dev) & ATA_HORKAGE_IPM) {
  2149. dev->horkage |= ATA_HORKAGE_IPM;
  2150. /* reset link pm_policy for this port to no pm */
  2151. ap->pm_policy = MAX_PERFORMANCE;
  2152. }
  2153. if (ap->ops->dev_config)
  2154. ap->ops->dev_config(dev);
  2155. if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
  2156. /* Let the user know. We don't want to disallow opens for
  2157. rescue purposes, or in case the vendor is just a blithering
  2158. idiot. Do this after the dev_config call as some controllers
  2159. with buggy firmware may want to avoid reporting false device
  2160. bugs */
  2161. if (print_info) {
  2162. ata_dev_printk(dev, KERN_WARNING,
  2163. "Drive reports diagnostics failure. This may indicate a drive\n");
  2164. ata_dev_printk(dev, KERN_WARNING,
  2165. "fault or invalid emulation. Contact drive vendor for information.\n");
  2166. }
  2167. }
  2168. if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
  2169. ata_dev_printk(dev, KERN_WARNING, "WARNING: device requires "
  2170. "firmware update to be fully functional.\n");
  2171. ata_dev_printk(dev, KERN_WARNING, " contact the vendor "
  2172. "or visit http://ata.wiki.kernel.org.\n");
  2173. }
  2174. return 0;
  2175. err_out_nosup:
  2176. if (ata_msg_probe(ap))
  2177. ata_dev_printk(dev, KERN_DEBUG,
  2178. "%s: EXIT, err\n", __func__);
  2179. return rc;
  2180. }
  2181. /**
  2182. * ata_cable_40wire - return 40 wire cable type
  2183. * @ap: port
  2184. *
  2185. * Helper method for drivers which want to hardwire 40 wire cable
  2186. * detection.
  2187. */
  2188. int ata_cable_40wire(struct ata_port *ap)
  2189. {
  2190. return ATA_CBL_PATA40;
  2191. }
  2192. /**
  2193. * ata_cable_80wire - return 80 wire cable type
  2194. * @ap: port
  2195. *
  2196. * Helper method for drivers which want to hardwire 80 wire cable
  2197. * detection.
  2198. */
  2199. int ata_cable_80wire(struct ata_port *ap)
  2200. {
  2201. return ATA_CBL_PATA80;
  2202. }
  2203. /**
  2204. * ata_cable_unknown - return unknown PATA cable.
  2205. * @ap: port
  2206. *
  2207. * Helper method for drivers which have no PATA cable detection.
  2208. */
  2209. int ata_cable_unknown(struct ata_port *ap)
  2210. {
  2211. return ATA_CBL_PATA_UNK;
  2212. }
  2213. /**
  2214. * ata_cable_ignore - return ignored PATA cable.
  2215. * @ap: port
  2216. *
  2217. * Helper method for drivers which don't use cable type to limit
  2218. * transfer mode.
  2219. */
  2220. int ata_cable_ignore(struct ata_port *ap)
  2221. {
  2222. return ATA_CBL_PATA_IGN;
  2223. }
  2224. /**
  2225. * ata_cable_sata - return SATA cable type
  2226. * @ap: port
  2227. *
  2228. * Helper method for drivers which have SATA cables
  2229. */
  2230. int ata_cable_sata(struct ata_port *ap)
  2231. {
  2232. return ATA_CBL_SATA;
  2233. }
  2234. /**
  2235. * ata_bus_probe - Reset and probe ATA bus
  2236. * @ap: Bus to probe
  2237. *
  2238. * Master ATA bus probing function. Initiates a hardware-dependent
  2239. * bus reset, then attempts to identify any devices found on
  2240. * the bus.
  2241. *
  2242. * LOCKING:
  2243. * PCI/etc. bus probe sem.
  2244. *
  2245. * RETURNS:
  2246. * Zero on success, negative errno otherwise.
  2247. */
  2248. int ata_bus_probe(struct ata_port *ap)
  2249. {
  2250. unsigned int classes[ATA_MAX_DEVICES];
  2251. int tries[ATA_MAX_DEVICES];
  2252. int rc;
  2253. struct ata_device *dev;
  2254. ata_port_probe(ap);
  2255. ata_link_for_each_dev(dev, &ap->link)
  2256. tries[dev->devno] = ATA_PROBE_MAX_TRIES;
  2257. retry:
  2258. ata_link_for_each_dev(dev, &ap->link) {
  2259. /* If we issue an SRST then an ATA drive (not ATAPI)
  2260. * may change configuration and be in PIO0 timing. If
  2261. * we do a hard reset (or are coming from power on)
  2262. * this is true for ATA or ATAPI. Until we've set a
  2263. * suitable controller mode we should not touch the
  2264. * bus as we may be talking too fast.
  2265. */
  2266. dev->pio_mode = XFER_PIO_0;
  2267. /* If the controller has a pio mode setup function
  2268. * then use it to set the chipset to rights. Don't
  2269. * touch the DMA setup as that will be dealt with when
  2270. * configuring devices.
  2271. */
  2272. if (ap->ops->set_piomode)
  2273. ap->ops->set_piomode(ap, dev);
  2274. }
  2275. /* reset and determine device classes */
  2276. ap->ops->phy_reset(ap);
  2277. ata_link_for_each_dev(dev, &ap->link) {
  2278. if (!(ap->flags & ATA_FLAG_DISABLED) &&
  2279. dev->class != ATA_DEV_UNKNOWN)
  2280. classes[dev->devno] = dev->class;
  2281. else
  2282. classes[dev->devno] = ATA_DEV_NONE;
  2283. dev->class = ATA_DEV_UNKNOWN;
  2284. }
  2285. ata_port_probe(ap);
  2286. /* read IDENTIFY page and configure devices. We have to do the identify
  2287. specific sequence bass-ackwards so that PDIAG- is released by
  2288. the slave device */
  2289. ata_link_for_each_dev_reverse(dev, &ap->link) {
  2290. if (tries[dev->devno])
  2291. dev->class = classes[dev->devno];
  2292. if (!ata_dev_enabled(dev))
  2293. continue;
  2294. rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
  2295. dev->id);
  2296. if (rc)
  2297. goto fail;
  2298. }
  2299. /* Now ask for the cable type as PDIAG- should have been released */
  2300. if (ap->ops->cable_detect)
  2301. ap->cbl = ap->ops->cable_detect(ap);
  2302. /* We may have SATA bridge glue hiding here irrespective of the
  2303. reported cable types and sensed types */
  2304. ata_link_for_each_dev(dev, &ap->link) {
  2305. if (!ata_dev_enabled(dev))
  2306. continue;
  2307. /* SATA drives indicate we have a bridge. We don't know which
  2308. end of the link the bridge is which is a problem */
  2309. if (ata_id_is_sata(dev->id))
  2310. ap->cbl = ATA_CBL_SATA;
  2311. }
  2312. /* After the identify sequence we can now set up the devices. We do
  2313. this in the normal order so that the user doesn't get confused */
  2314. ata_link_for_each_dev(dev, &ap->link) {
  2315. if (!ata_dev_enabled(dev))
  2316. continue;
  2317. ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
  2318. rc = ata_dev_configure(dev);
  2319. ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
  2320. if (rc)
  2321. goto fail;
  2322. }
  2323. /* configure transfer mode */
  2324. rc = ata_set_mode(&ap->link, &dev);
  2325. if (rc)
  2326. goto fail;
  2327. ata_link_for_each_dev(dev, &ap->link)
  2328. if (ata_dev_enabled(dev))
  2329. return 0;
  2330. /* no device present, disable port */
  2331. ata_port_disable(ap);
  2332. return -ENODEV;
  2333. fail:
  2334. tries[dev->devno]--;
  2335. switch (rc) {
  2336. case -EINVAL:
  2337. /* eeek, something went very wrong, give up */
  2338. tries[dev->devno] = 0;
  2339. break;
  2340. case -ENODEV:
  2341. /* give it just one more chance */
  2342. tries[dev->devno] = min(tries[dev->devno], 1);
  2343. case -EIO:
  2344. if (tries[dev->devno] == 1) {
  2345. /* This is the last chance, better to slow
  2346. * down than lose it.
  2347. */
  2348. sata_down_spd_limit(&ap->link);
  2349. ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
  2350. }
  2351. }
  2352. if (!tries[dev->devno])
  2353. ata_dev_disable(dev);
  2354. goto retry;
  2355. }
  2356. /**
  2357. * ata_port_probe - Mark port as enabled
  2358. * @ap: Port for which we indicate enablement
  2359. *
  2360. * Modify @ap data structure such that the system
  2361. * thinks that the entire port is enabled.
  2362. *
  2363. * LOCKING: host lock, or some other form of
  2364. * serialization.
  2365. */
  2366. void ata_port_probe(struct ata_port *ap)
  2367. {
  2368. ap->flags &= ~ATA_FLAG_DISABLED;
  2369. }
  2370. /**
  2371. * sata_print_link_status - Print SATA link status
  2372. * @link: SATA link to printk link status about
  2373. *
  2374. * This function prints link speed and status of a SATA link.
  2375. *
  2376. * LOCKING:
  2377. * None.
  2378. */
  2379. static void sata_print_link_status(struct ata_link *link)
  2380. {
  2381. u32 sstatus, scontrol, tmp;
  2382. if (sata_scr_read(link, SCR_STATUS, &sstatus))
  2383. return;
  2384. sata_scr_read(link, SCR_CONTROL, &scontrol);
  2385. if (ata_phys_link_online(link)) {
  2386. tmp = (sstatus >> 4) & 0xf;
  2387. ata_link_printk(link, KERN_INFO,
  2388. "SATA link up %s (SStatus %X SControl %X)\n",
  2389. sata_spd_string(tmp), sstatus, scontrol);
  2390. } else {
  2391. ata_link_printk(link, KERN_INFO,
  2392. "SATA link down (SStatus %X SControl %X)\n",
  2393. sstatus, scontrol);
  2394. }
  2395. }
  2396. /**
  2397. * ata_dev_pair - return other device on cable
  2398. * @adev: device
  2399. *
  2400. * Obtain the other device on the same cable, or if none is
  2401. * present NULL is returned
  2402. */
  2403. struct ata_device *ata_dev_pair(struct ata_device *adev)
  2404. {
  2405. struct ata_link *link = adev->link;
  2406. struct ata_device *pair = &link->device[1 - adev->devno];
  2407. if (!ata_dev_enabled(pair))
  2408. return NULL;
  2409. return pair;
  2410. }
  2411. /**
  2412. * ata_port_disable - Disable port.
  2413. * @ap: Port to be disabled.
  2414. *
  2415. * Modify @ap data structure such that the system
  2416. * thinks that the entire port is disabled, and should
  2417. * never attempt to probe or communicate with devices
  2418. * on this port.
  2419. *
  2420. * LOCKING: host lock, or some other form of
  2421. * serialization.
  2422. */
  2423. void ata_port_disable(struct ata_port *ap)
  2424. {
  2425. ap->link.device[0].class = ATA_DEV_NONE;
  2426. ap->link.device[1].class = ATA_DEV_NONE;
  2427. ap->flags |= ATA_FLAG_DISABLED;
  2428. }
  2429. /**
  2430. * sata_down_spd_limit - adjust SATA spd limit downward
  2431. * @link: Link to adjust SATA spd limit for
  2432. *
  2433. * Adjust SATA spd limit of @link downward. Note that this
  2434. * function only adjusts the limit. The change must be applied
  2435. * using sata_set_spd().
  2436. *
  2437. * LOCKING:
  2438. * Inherited from caller.
  2439. *
  2440. * RETURNS:
  2441. * 0 on success, negative errno on failure
  2442. */
  2443. int sata_down_spd_limit(struct ata_link *link)
  2444. {
  2445. u32 sstatus, spd, mask;
  2446. int rc, highbit;
  2447. if (!sata_scr_valid(link))
  2448. return -EOPNOTSUPP;
  2449. /* If SCR can be read, use it to determine the current SPD.
  2450. * If not, use cached value in link->sata_spd.
  2451. */
  2452. rc = sata_scr_read(link, SCR_STATUS, &sstatus);
  2453. if (rc == 0)
  2454. spd = (sstatus >> 4) & 0xf;
  2455. else
  2456. spd = link->sata_spd;
  2457. mask = link->sata_spd_limit;
  2458. if (mask <= 1)
  2459. return -EINVAL;
  2460. /* unconditionally mask off the highest bit */
  2461. highbit = fls(mask) - 1;
  2462. mask &= ~(1 << highbit);
  2463. /* Mask off all speeds higher than or equal to the current
  2464. * one. Force 1.5Gbps if current SPD is not available.
  2465. */
  2466. if (spd > 1)
  2467. mask &= (1 << (spd - 1)) - 1;
  2468. else
  2469. mask &= 1;
  2470. /* were we already at the bottom? */
  2471. if (!mask)
  2472. return -EINVAL;
  2473. link->sata_spd_limit = mask;
  2474. ata_link_printk(link, KERN_WARNING, "limiting SATA link speed to %s\n",
  2475. sata_spd_string(fls(mask)));
  2476. return 0;
  2477. }
  2478. static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
  2479. {
  2480. struct ata_link *host_link = &link->ap->link;
  2481. u32 limit, target, spd;
  2482. limit = link->sata_spd_limit;
  2483. /* Don't configure downstream link faster than upstream link.
  2484. * It doesn't speed up anything and some PMPs choke on such
  2485. * configuration.
  2486. */
  2487. if (!ata_is_host_link(link) && host_link->sata_spd)
  2488. limit &= (1 << host_link->sata_spd) - 1;
  2489. if (limit == UINT_MAX)
  2490. target = 0;
  2491. else
  2492. target = fls(limit);
  2493. spd = (*scontrol >> 4) & 0xf;
  2494. *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
  2495. return spd != target;
  2496. }
  2497. /**
  2498. * sata_set_spd_needed - is SATA spd configuration needed
  2499. * @link: Link in question
  2500. *
  2501. * Test whether the spd limit in SControl matches
  2502. * @link->sata_spd_limit. This function is used to determine
  2503. * whether hardreset is necessary to apply SATA spd
  2504. * configuration.
  2505. *
  2506. * LOCKING:
  2507. * Inherited from caller.
  2508. *
  2509. * RETURNS:
  2510. * 1 if SATA spd configuration is needed, 0 otherwise.
  2511. */
  2512. static int sata_set_spd_needed(struct ata_link *link)
  2513. {
  2514. u32 scontrol;
  2515. if (sata_scr_read(link, SCR_CONTROL, &scontrol))
  2516. return 1;
  2517. return __sata_set_spd_needed(link, &scontrol);
  2518. }
  2519. /**
  2520. * sata_set_spd - set SATA spd according to spd limit
  2521. * @link: Link to set SATA spd for
  2522. *
  2523. * Set SATA spd of @link according to sata_spd_limit.
  2524. *
  2525. * LOCKING:
  2526. * Inherited from caller.
  2527. *
  2528. * RETURNS:
  2529. * 0 if spd doesn't need to be changed, 1 if spd has been
  2530. * changed. Negative errno if SCR registers are inaccessible.
  2531. */
  2532. int sata_set_spd(struct ata_link *link)
  2533. {
  2534. u32 scontrol;
  2535. int rc;
  2536. if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
  2537. return rc;
  2538. if (!__sata_set_spd_needed(link, &scontrol))
  2539. return 0;
  2540. if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
  2541. return rc;
  2542. return 1;
  2543. }
  2544. /*
  2545. * This mode timing computation functionality is ported over from
  2546. * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
  2547. */
  2548. /*
  2549. * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
  2550. * These were taken from ATA/ATAPI-6 standard, rev 0a, except
  2551. * for UDMA6, which is currently supported only by Maxtor drives.
  2552. *
  2553. * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
  2554. */
  2555. static const struct ata_timing ata_timing[] = {
  2556. /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
  2557. { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
  2558. { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
  2559. { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
  2560. { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
  2561. { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
  2562. { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 100, 0 },
  2563. { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 80, 0 },
  2564. { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
  2565. { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
  2566. { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
  2567. { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
  2568. { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
  2569. { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
  2570. { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 100, 0 },
  2571. { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 80, 0 },
  2572. /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
  2573. { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
  2574. { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
  2575. { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
  2576. { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
  2577. { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
  2578. { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
  2579. { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
  2580. { 0xFF }
  2581. };
  2582. #define ENOUGH(v, unit) (((v)-1)/(unit)+1)
  2583. #define EZ(v, unit) ((v)?ENOUGH(v, unit):0)
  2584. static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
  2585. {
  2586. q->setup = EZ(t->setup * 1000, T);
  2587. q->act8b = EZ(t->act8b * 1000, T);
  2588. q->rec8b = EZ(t->rec8b * 1000, T);
  2589. q->cyc8b = EZ(t->cyc8b * 1000, T);
  2590. q->active = EZ(t->active * 1000, T);
  2591. q->recover = EZ(t->recover * 1000, T);
  2592. q->cycle = EZ(t->cycle * 1000, T);
  2593. q->udma = EZ(t->udma * 1000, UT);
  2594. }
  2595. void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
  2596. struct ata_timing *m, unsigned int what)
  2597. {
  2598. if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
  2599. if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
  2600. if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
  2601. if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
  2602. if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
  2603. if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
  2604. if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
  2605. if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
  2606. }
  2607. const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
  2608. {
  2609. const struct ata_timing *t = ata_timing;
  2610. while (xfer_mode > t->mode)
  2611. t++;
  2612. if (xfer_mode == t->mode)
  2613. return t;
  2614. return NULL;
  2615. }
  2616. int ata_timing_compute(struct ata_device *adev, unsigned short speed,
  2617. struct ata_timing *t, int T, int UT)
  2618. {
  2619. const struct ata_timing *s;
  2620. struct ata_timing p;
  2621. /*
  2622. * Find the mode.
  2623. */
  2624. if (!(s = ata_timing_find_mode(speed)))
  2625. return -EINVAL;
  2626. memcpy(t, s, sizeof(*s));
  2627. /*
  2628. * If the drive is an EIDE drive, it can tell us it needs extended
  2629. * PIO/MW_DMA cycle timing.
  2630. */
  2631. if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
  2632. memset(&p, 0, sizeof(p));
  2633. if (speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
  2634. if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
  2635. else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
  2636. } else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
  2637. p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
  2638. }
  2639. ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
  2640. }
  2641. /*
  2642. * Convert the timing to bus clock counts.
  2643. */
  2644. ata_timing_quantize(t, t, T, UT);
  2645. /*
  2646. * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
  2647. * S.M.A.R.T * and some other commands. We have to ensure that the
  2648. * DMA cycle timing is slower/equal than the fastest PIO timing.
  2649. */
  2650. if (speed > XFER_PIO_6) {
  2651. ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
  2652. ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
  2653. }
  2654. /*
  2655. * Lengthen active & recovery time so that cycle time is correct.
  2656. */
  2657. if (t->act8b + t->rec8b < t->cyc8b) {
  2658. t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
  2659. t->rec8b = t->cyc8b - t->act8b;
  2660. }
  2661. if (t->active + t->recover < t->cycle) {
  2662. t->active += (t->cycle - (t->active + t->recover)) / 2;
  2663. t->recover = t->cycle - t->active;
  2664. }
  2665. /* In a few cases quantisation may produce enough errors to
  2666. leave t->cycle too low for the sum of active and recovery
  2667. if so we must correct this */
  2668. if (t->active + t->recover > t->cycle)
  2669. t->cycle = t->active + t->recover;
  2670. return 0;
  2671. }
  2672. /**
  2673. * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
  2674. * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
  2675. * @cycle: cycle duration in ns
  2676. *
  2677. * Return matching xfer mode for @cycle. The returned mode is of
  2678. * the transfer type specified by @xfer_shift. If @cycle is too
  2679. * slow for @xfer_shift, 0xff is returned. If @cycle is faster
  2680. * than the fastest known mode, the fasted mode is returned.
  2681. *
  2682. * LOCKING:
  2683. * None.
  2684. *
  2685. * RETURNS:
  2686. * Matching xfer_mode, 0xff if no match found.
  2687. */
  2688. u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
  2689. {
  2690. u8 base_mode = 0xff, last_mode = 0xff;
  2691. const struct ata_xfer_ent *ent;
  2692. const struct ata_timing *t;
  2693. for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
  2694. if (ent->shift == xfer_shift)
  2695. base_mode = ent->base;
  2696. for (t = ata_timing_find_mode(base_mode);
  2697. t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
  2698. unsigned short this_cycle;
  2699. switch (xfer_shift) {
  2700. case ATA_SHIFT_PIO:
  2701. case ATA_SHIFT_MWDMA:
  2702. this_cycle = t->cycle;
  2703. break;
  2704. case ATA_SHIFT_UDMA:
  2705. this_cycle = t->udma;
  2706. break;
  2707. default:
  2708. return 0xff;
  2709. }
  2710. if (cycle > this_cycle)
  2711. break;
  2712. last_mode = t->mode;
  2713. }
  2714. return last_mode;
  2715. }
  2716. /**
  2717. * ata_down_xfermask_limit - adjust dev xfer masks downward
  2718. * @dev: Device to adjust xfer masks
  2719. * @sel: ATA_DNXFER_* selector
  2720. *
  2721. * Adjust xfer masks of @dev downward. Note that this function
  2722. * does not apply the change. Invoking ata_set_mode() afterwards
  2723. * will apply the limit.
  2724. *
  2725. * LOCKING:
  2726. * Inherited from caller.
  2727. *
  2728. * RETURNS:
  2729. * 0 on success, negative errno on failure
  2730. */
  2731. int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
  2732. {
  2733. char buf[32];
  2734. unsigned long orig_mask, xfer_mask;
  2735. unsigned long pio_mask, mwdma_mask, udma_mask;
  2736. int quiet, highbit;
  2737. quiet = !!(sel & ATA_DNXFER_QUIET);
  2738. sel &= ~ATA_DNXFER_QUIET;
  2739. xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
  2740. dev->mwdma_mask,
  2741. dev->udma_mask);
  2742. ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
  2743. switch (sel) {
  2744. case ATA_DNXFER_PIO:
  2745. highbit = fls(pio_mask) - 1;
  2746. pio_mask &= ~(1 << highbit);
  2747. break;
  2748. case ATA_DNXFER_DMA:
  2749. if (udma_mask) {
  2750. highbit = fls(udma_mask) - 1;
  2751. udma_mask &= ~(1 << highbit);
  2752. if (!udma_mask)
  2753. return -ENOENT;
  2754. } else if (mwdma_mask) {
  2755. highbit = fls(mwdma_mask) - 1;
  2756. mwdma_mask &= ~(1 << highbit);
  2757. if (!mwdma_mask)
  2758. return -ENOENT;
  2759. }
  2760. break;
  2761. case ATA_DNXFER_40C:
  2762. udma_mask &= ATA_UDMA_MASK_40C;
  2763. break;
  2764. case ATA_DNXFER_FORCE_PIO0:
  2765. pio_mask &= 1;
  2766. case ATA_DNXFER_FORCE_PIO:
  2767. mwdma_mask = 0;
  2768. udma_mask = 0;
  2769. break;
  2770. default:
  2771. BUG();
  2772. }
  2773. xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
  2774. if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
  2775. return -ENOENT;
  2776. if (!quiet) {
  2777. if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
  2778. snprintf(buf, sizeof(buf), "%s:%s",
  2779. ata_mode_string(xfer_mask),
  2780. ata_mode_string(xfer_mask & ATA_MASK_PIO));
  2781. else
  2782. snprintf(buf, sizeof(buf), "%s",
  2783. ata_mode_string(xfer_mask));
  2784. ata_dev_printk(dev, KERN_WARNING,
  2785. "limiting speed to %s\n", buf);
  2786. }
  2787. ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
  2788. &dev->udma_mask);
  2789. return 0;
  2790. }
  2791. static int ata_dev_set_mode(struct ata_device *dev)
  2792. {
  2793. struct ata_eh_context *ehc = &dev->link->eh_context;
  2794. const char *dev_err_whine = "";
  2795. int ign_dev_err = 0;
  2796. unsigned int err_mask;
  2797. int rc;
  2798. dev->flags &= ~ATA_DFLAG_PIO;
  2799. if (dev->xfer_shift == ATA_SHIFT_PIO)
  2800. dev->flags |= ATA_DFLAG_PIO;
  2801. err_mask = ata_dev_set_xfermode(dev);
  2802. if (err_mask & ~AC_ERR_DEV)
  2803. goto fail;
  2804. /* revalidate */
  2805. ehc->i.flags |= ATA_EHI_POST_SETMODE;
  2806. rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
  2807. ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
  2808. if (rc)
  2809. return rc;
  2810. if (dev->xfer_shift == ATA_SHIFT_PIO) {
  2811. /* Old CFA may refuse this command, which is just fine */
  2812. if (ata_id_is_cfa(dev->id))
  2813. ign_dev_err = 1;
  2814. /* Catch several broken garbage emulations plus some pre
  2815. ATA devices */
  2816. if (ata_id_major_version(dev->id) == 0 &&
  2817. dev->pio_mode <= XFER_PIO_2)
  2818. ign_dev_err = 1;
  2819. /* Some very old devices and some bad newer ones fail
  2820. any kind of SET_XFERMODE request but support PIO0-2
  2821. timings and no IORDY */
  2822. if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
  2823. ign_dev_err = 1;
  2824. }
  2825. /* Early MWDMA devices do DMA but don't allow DMA mode setting.
  2826. Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
  2827. if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
  2828. dev->dma_mode == XFER_MW_DMA_0 &&
  2829. (dev->id[63] >> 8) & 1)
  2830. ign_dev_err = 1;
  2831. /* if the device is actually configured correctly, ignore dev err */
  2832. if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
  2833. ign_dev_err = 1;
  2834. if (err_mask & AC_ERR_DEV) {
  2835. if (!ign_dev_err)
  2836. goto fail;
  2837. else
  2838. dev_err_whine = " (device error ignored)";
  2839. }
  2840. DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
  2841. dev->xfer_shift, (int)dev->xfer_mode);
  2842. ata_dev_printk(dev, KERN_INFO, "configured for %s%s\n",
  2843. ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
  2844. dev_err_whine);
  2845. return 0;
  2846. fail:
  2847. ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
  2848. "(err_mask=0x%x)\n", err_mask);
  2849. return -EIO;
  2850. }
  2851. /**
  2852. * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
  2853. * @link: link on which timings will be programmed
  2854. * @r_failed_dev: out parameter for failed device
  2855. *
  2856. * Standard implementation of the function used to tune and set
  2857. * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
  2858. * ata_dev_set_mode() fails, pointer to the failing device is
  2859. * returned in @r_failed_dev.
  2860. *
  2861. * LOCKING:
  2862. * PCI/etc. bus probe sem.
  2863. *
  2864. * RETURNS:
  2865. * 0 on success, negative errno otherwise
  2866. */
  2867. int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
  2868. {
  2869. struct ata_port *ap = link->ap;
  2870. struct ata_device *dev;
  2871. int rc = 0, used_dma = 0, found = 0;
  2872. /* step 1: calculate xfer_mask */
  2873. ata_link_for_each_dev(dev, link) {
  2874. unsigned long pio_mask, dma_mask;
  2875. unsigned int mode_mask;
  2876. if (!ata_dev_enabled(dev))
  2877. continue;
  2878. mode_mask = ATA_DMA_MASK_ATA;
  2879. if (dev->class == ATA_DEV_ATAPI)
  2880. mode_mask = ATA_DMA_MASK_ATAPI;
  2881. else if (ata_id_is_cfa(dev->id))
  2882. mode_mask = ATA_DMA_MASK_CFA;
  2883. ata_dev_xfermask(dev);
  2884. ata_force_xfermask(dev);
  2885. pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
  2886. dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
  2887. if (libata_dma_mask & mode_mask)
  2888. dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
  2889. else
  2890. dma_mask = 0;
  2891. dev->pio_mode = ata_xfer_mask2mode(pio_mask);
  2892. dev->dma_mode = ata_xfer_mask2mode(dma_mask);
  2893. found = 1;
  2894. if (ata_dma_enabled(dev))
  2895. used_dma = 1;
  2896. }
  2897. if (!found)
  2898. goto out;
  2899. /* step 2: always set host PIO timings */
  2900. ata_link_for_each_dev(dev, link) {
  2901. if (!ata_dev_enabled(dev))
  2902. continue;
  2903. if (dev->pio_mode == 0xff) {
  2904. ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
  2905. rc = -EINVAL;
  2906. goto out;
  2907. }
  2908. dev->xfer_mode = dev->pio_mode;
  2909. dev->xfer_shift = ATA_SHIFT_PIO;
  2910. if (ap->ops->set_piomode)
  2911. ap->ops->set_piomode(ap, dev);
  2912. }
  2913. /* step 3: set host DMA timings */
  2914. ata_link_for_each_dev(dev, link) {
  2915. if (!ata_dev_enabled(dev) || !ata_dma_enabled(dev))
  2916. continue;
  2917. dev->xfer_mode = dev->dma_mode;
  2918. dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
  2919. if (ap->ops->set_dmamode)
  2920. ap->ops->set_dmamode(ap, dev);
  2921. }
  2922. /* step 4: update devices' xfer mode */
  2923. ata_link_for_each_dev(dev, link) {
  2924. /* don't update suspended devices' xfer mode */
  2925. if (!ata_dev_enabled(dev))
  2926. continue;
  2927. rc = ata_dev_set_mode(dev);
  2928. if (rc)
  2929. goto out;
  2930. }
  2931. /* Record simplex status. If we selected DMA then the other
  2932. * host channels are not permitted to do so.
  2933. */
  2934. if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
  2935. ap->host->simplex_claimed = ap;
  2936. out:
  2937. if (rc)
  2938. *r_failed_dev = dev;
  2939. return rc;
  2940. }
  2941. /**
  2942. * ata_wait_ready - wait for link to become ready
  2943. * @link: link to be waited on
  2944. * @deadline: deadline jiffies for the operation
  2945. * @check_ready: callback to check link readiness
  2946. *
  2947. * Wait for @link to become ready. @check_ready should return
  2948. * positive number if @link is ready, 0 if it isn't, -ENODEV if
  2949. * link doesn't seem to be occupied, other errno for other error
  2950. * conditions.
  2951. *
  2952. * Transient -ENODEV conditions are allowed for
  2953. * ATA_TMOUT_FF_WAIT.
  2954. *
  2955. * LOCKING:
  2956. * EH context.
  2957. *
  2958. * RETURNS:
  2959. * 0 if @linke is ready before @deadline; otherwise, -errno.
  2960. */
  2961. int ata_wait_ready(struct ata_link *link, unsigned long deadline,
  2962. int (*check_ready)(struct ata_link *link))
  2963. {
  2964. unsigned long start = jiffies;
  2965. unsigned long nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
  2966. int warned = 0;
  2967. /* Slave readiness can't be tested separately from master. On
  2968. * M/S emulation configuration, this function should be called
  2969. * only on the master and it will handle both master and slave.
  2970. */
  2971. WARN_ON(link == link->ap->slave_link);
  2972. if (time_after(nodev_deadline, deadline))
  2973. nodev_deadline = deadline;
  2974. while (1) {
  2975. unsigned long now = jiffies;
  2976. int ready, tmp;
  2977. ready = tmp = check_ready(link);
  2978. if (ready > 0)
  2979. return 0;
  2980. /* -ENODEV could be transient. Ignore -ENODEV if link
  2981. * is online. Also, some SATA devices take a long
  2982. * time to clear 0xff after reset. For example,
  2983. * HHD424020F7SV00 iVDR needs >= 800ms while Quantum
  2984. * GoVault needs even more than that. Wait for
  2985. * ATA_TMOUT_FF_WAIT on -ENODEV if link isn't offline.
  2986. *
  2987. * Note that some PATA controllers (pata_ali) explode
  2988. * if status register is read more than once when
  2989. * there's no device attached.
  2990. */
  2991. if (ready == -ENODEV) {
  2992. if (ata_link_online(link))
  2993. ready = 0;
  2994. else if ((link->ap->flags & ATA_FLAG_SATA) &&
  2995. !ata_link_offline(link) &&
  2996. time_before(now, nodev_deadline))
  2997. ready = 0;
  2998. }
  2999. if (ready)
  3000. return ready;
  3001. if (time_after(now, deadline))
  3002. return -EBUSY;
  3003. if (!warned && time_after(now, start + 5 * HZ) &&
  3004. (deadline - now > 3 * HZ)) {
  3005. ata_link_printk(link, KERN_WARNING,
  3006. "link is slow to respond, please be patient "
  3007. "(ready=%d)\n", tmp);
  3008. warned = 1;
  3009. }
  3010. msleep(50);
  3011. }
  3012. }
  3013. /**
  3014. * ata_wait_after_reset - wait for link to become ready after reset
  3015. * @link: link to be waited on
  3016. * @deadline: deadline jiffies for the operation
  3017. * @check_ready: callback to check link readiness
  3018. *
  3019. * Wait for @link to become ready after reset.
  3020. *
  3021. * LOCKING:
  3022. * EH context.
  3023. *
  3024. * RETURNS:
  3025. * 0 if @linke is ready before @deadline; otherwise, -errno.
  3026. */
  3027. int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
  3028. int (*check_ready)(struct ata_link *link))
  3029. {
  3030. msleep(ATA_WAIT_AFTER_RESET);
  3031. return ata_wait_ready(link, deadline, check_ready);
  3032. }
  3033. /**
  3034. * sata_link_debounce - debounce SATA phy status
  3035. * @link: ATA link to debounce SATA phy status for
  3036. * @params: timing parameters { interval, duratinon, timeout } in msec
  3037. * @deadline: deadline jiffies for the operation
  3038. *
  3039. * Make sure SStatus of @link reaches stable state, determined by
  3040. * holding the same value where DET is not 1 for @duration polled
  3041. * every @interval, before @timeout. Timeout constraints the
  3042. * beginning of the stable state. Because DET gets stuck at 1 on
  3043. * some controllers after hot unplugging, this functions waits
  3044. * until timeout then returns 0 if DET is stable at 1.
  3045. *
  3046. * @timeout is further limited by @deadline. The sooner of the
  3047. * two is used.
  3048. *
  3049. * LOCKING:
  3050. * Kernel thread context (may sleep)
  3051. *
  3052. * RETURNS:
  3053. * 0 on success, -errno on failure.
  3054. */
  3055. int sata_link_debounce(struct ata_link *link, const unsigned long *params,
  3056. unsigned long deadline)
  3057. {
  3058. unsigned long interval = params[0];
  3059. unsigned long duration = params[1];
  3060. unsigned long last_jiffies, t;
  3061. u32 last, cur;
  3062. int rc;
  3063. t = ata_deadline(jiffies, params[2]);
  3064. if (time_before(t, deadline))
  3065. deadline = t;
  3066. if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
  3067. return rc;
  3068. cur &= 0xf;
  3069. last = cur;
  3070. last_jiffies = jiffies;
  3071. while (1) {
  3072. msleep(interval);
  3073. if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
  3074. return rc;
  3075. cur &= 0xf;
  3076. /* DET stable? */
  3077. if (cur == last) {
  3078. if (cur == 1 && time_before(jiffies, deadline))
  3079. continue;
  3080. if (time_after(jiffies,
  3081. ata_deadline(last_jiffies, duration)))
  3082. return 0;
  3083. continue;
  3084. }
  3085. /* unstable, start over */
  3086. last = cur;
  3087. last_jiffies = jiffies;
  3088. /* Check deadline. If debouncing failed, return
  3089. * -EPIPE to tell upper layer to lower link speed.
  3090. */
  3091. if (time_after(jiffies, deadline))
  3092. return -EPIPE;
  3093. }
  3094. }
  3095. /**
  3096. * sata_link_resume - resume SATA link
  3097. * @link: ATA link to resume SATA
  3098. * @params: timing parameters { interval, duratinon, timeout } in msec
  3099. * @deadline: deadline jiffies for the operation
  3100. *
  3101. * Resume SATA phy @link and debounce it.
  3102. *
  3103. * LOCKING:
  3104. * Kernel thread context (may sleep)
  3105. *
  3106. * RETURNS:
  3107. * 0 on success, -errno on failure.
  3108. */
  3109. int sata_link_resume(struct ata_link *link, const unsigned long *params,
  3110. unsigned long deadline)
  3111. {
  3112. u32 scontrol, serror;
  3113. int rc;
  3114. if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
  3115. return rc;
  3116. scontrol = (scontrol & 0x0f0) | 0x300;
  3117. if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
  3118. return rc;
  3119. /* Some PHYs react badly if SStatus is pounded immediately
  3120. * after resuming. Delay 200ms before debouncing.
  3121. */
  3122. msleep(200);
  3123. if ((rc = sata_link_debounce(link, params, deadline)))
  3124. return rc;
  3125. /* clear SError, some PHYs require this even for SRST to work */
  3126. if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
  3127. rc = sata_scr_write(link, SCR_ERROR, serror);
  3128. return rc != -EINVAL ? rc : 0;
  3129. }
  3130. /**
  3131. * ata_std_prereset - prepare for reset
  3132. * @link: ATA link to be reset
  3133. * @deadline: deadline jiffies for the operation
  3134. *
  3135. * @link is about to be reset. Initialize it. Failure from
  3136. * prereset makes libata abort whole reset sequence and give up
  3137. * that port, so prereset should be best-effort. It does its
  3138. * best to prepare for reset sequence but if things go wrong, it
  3139. * should just whine, not fail.
  3140. *
  3141. * LOCKING:
  3142. * Kernel thread context (may sleep)
  3143. *
  3144. * RETURNS:
  3145. * 0 on success, -errno otherwise.
  3146. */
  3147. int ata_std_prereset(struct ata_link *link, unsigned long deadline)
  3148. {
  3149. struct ata_port *ap = link->ap;
  3150. struct ata_eh_context *ehc = &link->eh_context;
  3151. const unsigned long *timing = sata_ehc_deb_timing(ehc);
  3152. int rc;
  3153. /* if we're about to do hardreset, nothing more to do */
  3154. if (ehc->i.action & ATA_EH_HARDRESET)
  3155. return 0;
  3156. /* if SATA, resume link */
  3157. if (ap->flags & ATA_FLAG_SATA) {
  3158. rc = sata_link_resume(link, timing, deadline);
  3159. /* whine about phy resume failure but proceed */
  3160. if (rc && rc != -EOPNOTSUPP)
  3161. ata_link_printk(link, KERN_WARNING, "failed to resume "
  3162. "link for reset (errno=%d)\n", rc);
  3163. }
  3164. /* no point in trying softreset on offline link */
  3165. if (ata_phys_link_offline(link))
  3166. ehc->i.action &= ~ATA_EH_SOFTRESET;
  3167. return 0;
  3168. }
  3169. /**
  3170. * sata_link_hardreset - reset link via SATA phy reset
  3171. * @link: link to reset
  3172. * @timing: timing parameters { interval, duratinon, timeout } in msec
  3173. * @deadline: deadline jiffies for the operation
  3174. * @online: optional out parameter indicating link onlineness
  3175. * @check_ready: optional callback to check link readiness
  3176. *
  3177. * SATA phy-reset @link using DET bits of SControl register.
  3178. * After hardreset, link readiness is waited upon using
  3179. * ata_wait_ready() if @check_ready is specified. LLDs are
  3180. * allowed to not specify @check_ready and wait itself after this
  3181. * function returns. Device classification is LLD's
  3182. * responsibility.
  3183. *
  3184. * *@online is set to one iff reset succeeded and @link is online
  3185. * after reset.
  3186. *
  3187. * LOCKING:
  3188. * Kernel thread context (may sleep)
  3189. *
  3190. * RETURNS:
  3191. * 0 on success, -errno otherwise.
  3192. */
  3193. int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
  3194. unsigned long deadline,
  3195. bool *online, int (*check_ready)(struct ata_link *))
  3196. {
  3197. u32 scontrol;
  3198. int rc;
  3199. DPRINTK("ENTER\n");
  3200. if (online)
  3201. *online = false;
  3202. if (sata_set_spd_needed(link)) {
  3203. /* SATA spec says nothing about how to reconfigure
  3204. * spd. To be on the safe side, turn off phy during
  3205. * reconfiguration. This works for at least ICH7 AHCI
  3206. * and Sil3124.
  3207. */
  3208. if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
  3209. goto out;
  3210. scontrol = (scontrol & 0x0f0) | 0x304;
  3211. if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
  3212. goto out;
  3213. sata_set_spd(link);
  3214. }
  3215. /* issue phy wake/reset */
  3216. if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
  3217. goto out;
  3218. scontrol = (scontrol & 0x0f0) | 0x301;
  3219. if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
  3220. goto out;
  3221. /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
  3222. * 10.4.2 says at least 1 ms.
  3223. */
  3224. msleep(1);
  3225. /* bring link back */
  3226. rc = sata_link_resume(link, timing, deadline);
  3227. if (rc)
  3228. goto out;
  3229. /* if link is offline nothing more to do */
  3230. if (ata_phys_link_offline(link))
  3231. goto out;
  3232. /* Link is online. From this point, -ENODEV too is an error. */
  3233. if (online)
  3234. *online = true;
  3235. if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
  3236. /* If PMP is supported, we have to do follow-up SRST.
  3237. * Some PMPs don't send D2H Reg FIS after hardreset if
  3238. * the first port is empty. Wait only for
  3239. * ATA_TMOUT_PMP_SRST_WAIT.
  3240. */
  3241. if (check_ready) {
  3242. unsigned long pmp_deadline;
  3243. pmp_deadline = ata_deadline(jiffies,
  3244. ATA_TMOUT_PMP_SRST_WAIT);
  3245. if (time_after(pmp_deadline, deadline))
  3246. pmp_deadline = deadline;
  3247. ata_wait_ready(link, pmp_deadline, check_ready);
  3248. }
  3249. rc = -EAGAIN;
  3250. goto out;
  3251. }
  3252. rc = 0;
  3253. if (check_ready)
  3254. rc = ata_wait_ready(link, deadline, check_ready);
  3255. out:
  3256. if (rc && rc != -EAGAIN) {
  3257. /* online is set iff link is online && reset succeeded */
  3258. if (online)
  3259. *online = false;
  3260. ata_link_printk(link, KERN_ERR,
  3261. "COMRESET failed (errno=%d)\n", rc);
  3262. }
  3263. DPRINTK("EXIT, rc=%d\n", rc);
  3264. return rc;
  3265. }
  3266. /**
  3267. * sata_std_hardreset - COMRESET w/o waiting or classification
  3268. * @link: link to reset
  3269. * @class: resulting class of attached device
  3270. * @deadline: deadline jiffies for the operation
  3271. *
  3272. * Standard SATA COMRESET w/o waiting or classification.
  3273. *
  3274. * LOCKING:
  3275. * Kernel thread context (may sleep)
  3276. *
  3277. * RETURNS:
  3278. * 0 if link offline, -EAGAIN if link online, -errno on errors.
  3279. */
  3280. int sata_std_hardreset(struct ata_link *link, unsigned int *class,
  3281. unsigned long deadline)
  3282. {
  3283. const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
  3284. bool online;
  3285. int rc;
  3286. /* do hardreset */
  3287. rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
  3288. return online ? -EAGAIN : rc;
  3289. }
  3290. /**
  3291. * ata_std_postreset - standard postreset callback
  3292. * @link: the target ata_link
  3293. * @classes: classes of attached devices
  3294. *
  3295. * This function is invoked after a successful reset. Note that
  3296. * the device might have been reset more than once using
  3297. * different reset methods before postreset is invoked.
  3298. *
  3299. * LOCKING:
  3300. * Kernel thread context (may sleep)
  3301. */
  3302. void ata_std_postreset(struct ata_link *link, unsigned int *classes)
  3303. {
  3304. u32 serror;
  3305. DPRINTK("ENTER\n");
  3306. /* reset complete, clear SError */
  3307. if (!sata_scr_read(link, SCR_ERROR, &serror))
  3308. sata_scr_write(link, SCR_ERROR, serror);
  3309. /* print link status */
  3310. sata_print_link_status(link);
  3311. DPRINTK("EXIT\n");
  3312. }
  3313. /**
  3314. * ata_dev_same_device - Determine whether new ID matches configured device
  3315. * @dev: device to compare against
  3316. * @new_class: class of the new device
  3317. * @new_id: IDENTIFY page of the new device
  3318. *
  3319. * Compare @new_class and @new_id against @dev and determine
  3320. * whether @dev is the device indicated by @new_class and
  3321. * @new_id.
  3322. *
  3323. * LOCKING:
  3324. * None.
  3325. *
  3326. * RETURNS:
  3327. * 1 if @dev matches @new_class and @new_id, 0 otherwise.
  3328. */
  3329. static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
  3330. const u16 *new_id)
  3331. {
  3332. const u16 *old_id = dev->id;
  3333. unsigned char model[2][ATA_ID_PROD_LEN + 1];
  3334. unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
  3335. if (dev->class != new_class) {
  3336. ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
  3337. dev->class, new_class);
  3338. return 0;
  3339. }
  3340. ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
  3341. ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
  3342. ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
  3343. ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
  3344. if (strcmp(model[0], model[1])) {
  3345. ata_dev_printk(dev, KERN_INFO, "model number mismatch "
  3346. "'%s' != '%s'\n", model[0], model[1]);
  3347. return 0;
  3348. }
  3349. if (strcmp(serial[0], serial[1])) {
  3350. ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
  3351. "'%s' != '%s'\n", serial[0], serial[1]);
  3352. return 0;
  3353. }
  3354. return 1;
  3355. }
  3356. /**
  3357. * ata_dev_reread_id - Re-read IDENTIFY data
  3358. * @dev: target ATA device
  3359. * @readid_flags: read ID flags
  3360. *
  3361. * Re-read IDENTIFY page and make sure @dev is still attached to
  3362. * the port.
  3363. *
  3364. * LOCKING:
  3365. * Kernel thread context (may sleep)
  3366. *
  3367. * RETURNS:
  3368. * 0 on success, negative errno otherwise
  3369. */
  3370. int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
  3371. {
  3372. unsigned int class = dev->class;
  3373. u16 *id = (void *)dev->link->ap->sector_buf;
  3374. int rc;
  3375. /* read ID data */
  3376. rc = ata_dev_read_id(dev, &class, readid_flags, id);
  3377. if (rc)
  3378. return rc;
  3379. /* is the device still there? */
  3380. if (!ata_dev_same_device(dev, class, id))
  3381. return -ENODEV;
  3382. memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
  3383. return 0;
  3384. }
  3385. /**
  3386. * ata_dev_revalidate - Revalidate ATA device
  3387. * @dev: device to revalidate
  3388. * @new_class: new class code
  3389. * @readid_flags: read ID flags
  3390. *
  3391. * Re-read IDENTIFY page, make sure @dev is still attached to the
  3392. * port and reconfigure it according to the new IDENTIFY page.
  3393. *
  3394. * LOCKING:
  3395. * Kernel thread context (may sleep)
  3396. *
  3397. * RETURNS:
  3398. * 0 on success, negative errno otherwise
  3399. */
  3400. int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
  3401. unsigned int readid_flags)
  3402. {
  3403. u64 n_sectors = dev->n_sectors;
  3404. int rc;
  3405. if (!ata_dev_enabled(dev))
  3406. return -ENODEV;
  3407. /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
  3408. if (ata_class_enabled(new_class) &&
  3409. new_class != ATA_DEV_ATA && new_class != ATA_DEV_ATAPI) {
  3410. ata_dev_printk(dev, KERN_INFO, "class mismatch %u != %u\n",
  3411. dev->class, new_class);
  3412. rc = -ENODEV;
  3413. goto fail;
  3414. }
  3415. /* re-read ID */
  3416. rc = ata_dev_reread_id(dev, readid_flags);
  3417. if (rc)
  3418. goto fail;
  3419. /* configure device according to the new ID */
  3420. rc = ata_dev_configure(dev);
  3421. if (rc)
  3422. goto fail;
  3423. /* verify n_sectors hasn't changed */
  3424. if (dev->class == ATA_DEV_ATA && n_sectors &&
  3425. dev->n_sectors != n_sectors) {
  3426. ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
  3427. "%llu != %llu\n",
  3428. (unsigned long long)n_sectors,
  3429. (unsigned long long)dev->n_sectors);
  3430. /* restore original n_sectors */
  3431. dev->n_sectors = n_sectors;
  3432. rc = -ENODEV;
  3433. goto fail;
  3434. }
  3435. return 0;
  3436. fail:
  3437. ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
  3438. return rc;
  3439. }
  3440. struct ata_blacklist_entry {
  3441. const char *model_num;
  3442. const char *model_rev;
  3443. unsigned long horkage;
  3444. };
  3445. static const struct ata_blacklist_entry ata_device_blacklist [] = {
  3446. /* Devices with DMA related problems under Linux */
  3447. { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
  3448. { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
  3449. { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
  3450. { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
  3451. { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
  3452. { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
  3453. { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
  3454. { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
  3455. { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
  3456. { "CRD-8480B", NULL, ATA_HORKAGE_NODMA },
  3457. { "CRD-8482B", NULL, ATA_HORKAGE_NODMA },
  3458. { "CRD-84", NULL, ATA_HORKAGE_NODMA },
  3459. { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
  3460. { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
  3461. { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
  3462. { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
  3463. { "HITACHI CDR-8335", NULL, ATA_HORKAGE_NODMA },
  3464. { "HITACHI CDR-8435", NULL, ATA_HORKAGE_NODMA },
  3465. { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
  3466. { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
  3467. { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
  3468. { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
  3469. { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
  3470. { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
  3471. { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
  3472. { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
  3473. { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
  3474. { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
  3475. { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA },
  3476. { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
  3477. /* Odd clown on sil3726/4726 PMPs */
  3478. { "Config Disk", NULL, ATA_HORKAGE_DISABLE },
  3479. /* Weird ATAPI devices */
  3480. { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
  3481. { "QUANTUM DAT DAT72-000", NULL, ATA_HORKAGE_ATAPI_MOD16_DMA },
  3482. /* Devices we expect to fail diagnostics */
  3483. /* Devices where NCQ should be avoided */
  3484. /* NCQ is slow */
  3485. { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
  3486. { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
  3487. /* http://thread.gmane.org/gmane.linux.ide/14907 */
  3488. { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
  3489. /* NCQ is broken */
  3490. { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
  3491. { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
  3492. { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ },
  3493. { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ },
  3494. /* Seagate NCQ + FLUSH CACHE firmware bug */
  3495. { "ST31500341AS", "SD15", ATA_HORKAGE_NONCQ |
  3496. ATA_HORKAGE_FIRMWARE_WARN },
  3497. { "ST31500341AS", "SD16", ATA_HORKAGE_NONCQ |
  3498. ATA_HORKAGE_FIRMWARE_WARN },
  3499. { "ST31500341AS", "SD17", ATA_HORKAGE_NONCQ |
  3500. ATA_HORKAGE_FIRMWARE_WARN },
  3501. { "ST31500341AS", "SD18", ATA_HORKAGE_NONCQ |
  3502. ATA_HORKAGE_FIRMWARE_WARN },
  3503. { "ST31500341AS", "SD19", ATA_HORKAGE_NONCQ |
  3504. ATA_HORKAGE_FIRMWARE_WARN },
  3505. { "ST31000333AS", "SD15", ATA_HORKAGE_NONCQ |
  3506. ATA_HORKAGE_FIRMWARE_WARN },
  3507. { "ST31000333AS", "SD16", ATA_HORKAGE_NONCQ |
  3508. ATA_HORKAGE_FIRMWARE_WARN },
  3509. { "ST31000333AS", "SD17", ATA_HORKAGE_NONCQ |
  3510. ATA_HORKAGE_FIRMWARE_WARN },
  3511. { "ST31000333AS", "SD18", ATA_HORKAGE_NONCQ |
  3512. ATA_HORKAGE_FIRMWARE_WARN },
  3513. { "ST31000333AS", "SD19", ATA_HORKAGE_NONCQ |
  3514. ATA_HORKAGE_FIRMWARE_WARN },
  3515. { "ST3640623AS", "SD15", ATA_HORKAGE_NONCQ |
  3516. ATA_HORKAGE_FIRMWARE_WARN },
  3517. { "ST3640623AS", "SD16", ATA_HORKAGE_NONCQ |
  3518. ATA_HORKAGE_FIRMWARE_WARN },
  3519. { "ST3640623AS", "SD17", ATA_HORKAGE_NONCQ |
  3520. ATA_HORKAGE_FIRMWARE_WARN },
  3521. { "ST3640623AS", "SD18", ATA_HORKAGE_NONCQ |
  3522. ATA_HORKAGE_FIRMWARE_WARN },
  3523. { "ST3640623AS", "SD19", ATA_HORKAGE_NONCQ |
  3524. ATA_HORKAGE_FIRMWARE_WARN },
  3525. { "ST3640323AS", "SD15", ATA_HORKAGE_NONCQ |
  3526. ATA_HORKAGE_FIRMWARE_WARN },
  3527. { "ST3640323AS", "SD16", ATA_HORKAGE_NONCQ |
  3528. ATA_HORKAGE_FIRMWARE_WARN },
  3529. { "ST3640323AS", "SD17", ATA_HORKAGE_NONCQ |
  3530. ATA_HORKAGE_FIRMWARE_WARN },
  3531. { "ST3640323AS", "SD18", ATA_HORKAGE_NONCQ |
  3532. ATA_HORKAGE_FIRMWARE_WARN },
  3533. { "ST3640323AS", "SD19", ATA_HORKAGE_NONCQ |
  3534. ATA_HORKAGE_FIRMWARE_WARN },
  3535. { "ST3320813AS", "SD15", ATA_HORKAGE_NONCQ |
  3536. ATA_HORKAGE_FIRMWARE_WARN },
  3537. { "ST3320813AS", "SD16", ATA_HORKAGE_NONCQ |
  3538. ATA_HORKAGE_FIRMWARE_WARN },
  3539. { "ST3320813AS", "SD17", ATA_HORKAGE_NONCQ |
  3540. ATA_HORKAGE_FIRMWARE_WARN },
  3541. { "ST3320813AS", "SD18", ATA_HORKAGE_NONCQ |
  3542. ATA_HORKAGE_FIRMWARE_WARN },
  3543. { "ST3320813AS", "SD19", ATA_HORKAGE_NONCQ |
  3544. ATA_HORKAGE_FIRMWARE_WARN },
  3545. { "ST3320613AS", "SD15", ATA_HORKAGE_NONCQ |
  3546. ATA_HORKAGE_FIRMWARE_WARN },
  3547. { "ST3320613AS", "SD16", ATA_HORKAGE_NONCQ |
  3548. ATA_HORKAGE_FIRMWARE_WARN },
  3549. { "ST3320613AS", "SD17", ATA_HORKAGE_NONCQ |
  3550. ATA_HORKAGE_FIRMWARE_WARN },
  3551. { "ST3320613AS", "SD18", ATA_HORKAGE_NONCQ |
  3552. ATA_HORKAGE_FIRMWARE_WARN },
  3553. { "ST3320613AS", "SD19", ATA_HORKAGE_NONCQ |
  3554. ATA_HORKAGE_FIRMWARE_WARN },
  3555. /* Blacklist entries taken from Silicon Image 3124/3132
  3556. Windows driver .inf file - also several Linux problem reports */
  3557. { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
  3558. { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
  3559. { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
  3560. /* devices which puke on READ_NATIVE_MAX */
  3561. { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
  3562. { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
  3563. { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
  3564. { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
  3565. /* Devices which report 1 sector over size HPA */
  3566. { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, },
  3567. { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, },
  3568. { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE, },
  3569. /* Devices which get the IVB wrong */
  3570. { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
  3571. /* Maybe we should just blacklist TSSTcorp... */
  3572. { "TSSTcorp CDDVDW SH-S202H", "SB00", ATA_HORKAGE_IVB, },
  3573. { "TSSTcorp CDDVDW SH-S202H", "SB01", ATA_HORKAGE_IVB, },
  3574. { "TSSTcorp CDDVDW SH-S202J", "SB00", ATA_HORKAGE_IVB, },
  3575. { "TSSTcorp CDDVDW SH-S202J", "SB01", ATA_HORKAGE_IVB, },
  3576. { "TSSTcorp CDDVDW SH-S202N", "SB00", ATA_HORKAGE_IVB, },
  3577. { "TSSTcorp CDDVDW SH-S202N", "SB01", ATA_HORKAGE_IVB, },
  3578. /* Devices that do not need bridging limits applied */
  3579. { "MTRON MSP-SATA*", NULL, ATA_HORKAGE_BRIDGE_OK, },
  3580. /* End Marker */
  3581. { }
  3582. };
  3583. static int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
  3584. {
  3585. const char *p;
  3586. int len;
  3587. /*
  3588. * check for trailing wildcard: *\0
  3589. */
  3590. p = strchr(patt, wildchar);
  3591. if (p && ((*(p + 1)) == 0))
  3592. len = p - patt;
  3593. else {
  3594. len = strlen(name);
  3595. if (!len) {
  3596. if (!*patt)
  3597. return 0;
  3598. return -1;
  3599. }
  3600. }
  3601. return strncmp(patt, name, len);
  3602. }
  3603. static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
  3604. {
  3605. unsigned char model_num[ATA_ID_PROD_LEN + 1];
  3606. unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
  3607. const struct ata_blacklist_entry *ad = ata_device_blacklist;
  3608. ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
  3609. ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
  3610. while (ad->model_num) {
  3611. if (!strn_pattern_cmp(ad->model_num, model_num, '*')) {
  3612. if (ad->model_rev == NULL)
  3613. return ad->horkage;
  3614. if (!strn_pattern_cmp(ad->model_rev, model_rev, '*'))
  3615. return ad->horkage;
  3616. }
  3617. ad++;
  3618. }
  3619. return 0;
  3620. }
  3621. static int ata_dma_blacklisted(const struct ata_device *dev)
  3622. {
  3623. /* We don't support polling DMA.
  3624. * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
  3625. * if the LLDD handles only interrupts in the HSM_ST_LAST state.
  3626. */
  3627. if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
  3628. (dev->flags & ATA_DFLAG_CDB_INTR))
  3629. return 1;
  3630. return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
  3631. }
  3632. /**
  3633. * ata_is_40wire - check drive side detection
  3634. * @dev: device
  3635. *
  3636. * Perform drive side detection decoding, allowing for device vendors
  3637. * who can't follow the documentation.
  3638. */
  3639. static int ata_is_40wire(struct ata_device *dev)
  3640. {
  3641. if (dev->horkage & ATA_HORKAGE_IVB)
  3642. return ata_drive_40wire_relaxed(dev->id);
  3643. return ata_drive_40wire(dev->id);
  3644. }
  3645. /**
  3646. * cable_is_40wire - 40/80/SATA decider
  3647. * @ap: port to consider
  3648. *
  3649. * This function encapsulates the policy for speed management
  3650. * in one place. At the moment we don't cache the result but
  3651. * there is a good case for setting ap->cbl to the result when
  3652. * we are called with unknown cables (and figuring out if it
  3653. * impacts hotplug at all).
  3654. *
  3655. * Return 1 if the cable appears to be 40 wire.
  3656. */
  3657. static int cable_is_40wire(struct ata_port *ap)
  3658. {
  3659. struct ata_link *link;
  3660. struct ata_device *dev;
  3661. /* If the controller thinks we are 40 wire, we are. */
  3662. if (ap->cbl == ATA_CBL_PATA40)
  3663. return 1;
  3664. /* If the controller thinks we are 80 wire, we are. */
  3665. if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
  3666. return 0;
  3667. /* If the system is known to be 40 wire short cable (eg
  3668. * laptop), then we allow 80 wire modes even if the drive
  3669. * isn't sure.
  3670. */
  3671. if (ap->cbl == ATA_CBL_PATA40_SHORT)
  3672. return 0;
  3673. /* If the controller doesn't know, we scan.
  3674. *
  3675. * Note: We look for all 40 wire detects at this point. Any
  3676. * 80 wire detect is taken to be 80 wire cable because
  3677. * - in many setups only the one drive (slave if present) will
  3678. * give a valid detect
  3679. * - if you have a non detect capable drive you don't want it
  3680. * to colour the choice
  3681. */
  3682. ata_port_for_each_link(link, ap) {
  3683. ata_link_for_each_dev(dev, link) {
  3684. if (ata_dev_enabled(dev) && !ata_is_40wire(dev))
  3685. return 0;
  3686. }
  3687. }
  3688. return 1;
  3689. }
  3690. /**
  3691. * ata_dev_xfermask - Compute supported xfermask of the given device
  3692. * @dev: Device to compute xfermask for
  3693. *
  3694. * Compute supported xfermask of @dev and store it in
  3695. * dev->*_mask. This function is responsible for applying all
  3696. * known limits including host controller limits, device
  3697. * blacklist, etc...
  3698. *
  3699. * LOCKING:
  3700. * None.
  3701. */
  3702. static void ata_dev_xfermask(struct ata_device *dev)
  3703. {
  3704. struct ata_link *link = dev->link;
  3705. struct ata_port *ap = link->ap;
  3706. struct ata_host *host = ap->host;
  3707. unsigned long xfer_mask;
  3708. /* controller modes available */
  3709. xfer_mask = ata_pack_xfermask(ap->pio_mask,
  3710. ap->mwdma_mask, ap->udma_mask);
  3711. /* drive modes available */
  3712. xfer_mask &= ata_pack_xfermask(dev->pio_mask,
  3713. dev->mwdma_mask, dev->udma_mask);
  3714. xfer_mask &= ata_id_xfermask(dev->id);
  3715. /*
  3716. * CFA Advanced TrueIDE timings are not allowed on a shared
  3717. * cable
  3718. */
  3719. if (ata_dev_pair(dev)) {
  3720. /* No PIO5 or PIO6 */
  3721. xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
  3722. /* No MWDMA3 or MWDMA 4 */
  3723. xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
  3724. }
  3725. if (ata_dma_blacklisted(dev)) {
  3726. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  3727. ata_dev_printk(dev, KERN_WARNING,
  3728. "device is on DMA blacklist, disabling DMA\n");
  3729. }
  3730. if ((host->flags & ATA_HOST_SIMPLEX) &&
  3731. host->simplex_claimed && host->simplex_claimed != ap) {
  3732. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  3733. ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
  3734. "other device, disabling DMA\n");
  3735. }
  3736. if (ap->flags & ATA_FLAG_NO_IORDY)
  3737. xfer_mask &= ata_pio_mask_no_iordy(dev);
  3738. if (ap->ops->mode_filter)
  3739. xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
  3740. /* Apply cable rule here. Don't apply it early because when
  3741. * we handle hot plug the cable type can itself change.
  3742. * Check this last so that we know if the transfer rate was
  3743. * solely limited by the cable.
  3744. * Unknown or 80 wire cables reported host side are checked
  3745. * drive side as well. Cases where we know a 40wire cable
  3746. * is used safely for 80 are not checked here.
  3747. */
  3748. if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
  3749. /* UDMA/44 or higher would be available */
  3750. if (cable_is_40wire(ap)) {
  3751. ata_dev_printk(dev, KERN_WARNING,
  3752. "limited to UDMA/33 due to 40-wire cable\n");
  3753. xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
  3754. }
  3755. ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
  3756. &dev->mwdma_mask, &dev->udma_mask);
  3757. }
  3758. /**
  3759. * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
  3760. * @dev: Device to which command will be sent
  3761. *
  3762. * Issue SET FEATURES - XFER MODE command to device @dev
  3763. * on port @ap.
  3764. *
  3765. * LOCKING:
  3766. * PCI/etc. bus probe sem.
  3767. *
  3768. * RETURNS:
  3769. * 0 on success, AC_ERR_* mask otherwise.
  3770. */
  3771. static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
  3772. {
  3773. struct ata_taskfile tf;
  3774. unsigned int err_mask;
  3775. /* set up set-features taskfile */
  3776. DPRINTK("set features - xfer mode\n");
  3777. /* Some controllers and ATAPI devices show flaky interrupt
  3778. * behavior after setting xfer mode. Use polling instead.
  3779. */
  3780. ata_tf_init(dev, &tf);
  3781. tf.command = ATA_CMD_SET_FEATURES;
  3782. tf.feature = SETFEATURES_XFER;
  3783. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
  3784. tf.protocol = ATA_PROT_NODATA;
  3785. /* If we are using IORDY we must send the mode setting command */
  3786. if (ata_pio_need_iordy(dev))
  3787. tf.nsect = dev->xfer_mode;
  3788. /* If the device has IORDY and the controller does not - turn it off */
  3789. else if (ata_id_has_iordy(dev->id))
  3790. tf.nsect = 0x01;
  3791. else /* In the ancient relic department - skip all of this */
  3792. return 0;
  3793. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
  3794. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  3795. return err_mask;
  3796. }
  3797. /**
  3798. * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
  3799. * @dev: Device to which command will be sent
  3800. * @enable: Whether to enable or disable the feature
  3801. * @feature: The sector count represents the feature to set
  3802. *
  3803. * Issue SET FEATURES - SATA FEATURES command to device @dev
  3804. * on port @ap with sector count
  3805. *
  3806. * LOCKING:
  3807. * PCI/etc. bus probe sem.
  3808. *
  3809. * RETURNS:
  3810. * 0 on success, AC_ERR_* mask otherwise.
  3811. */
  3812. static unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable,
  3813. u8 feature)
  3814. {
  3815. struct ata_taskfile tf;
  3816. unsigned int err_mask;
  3817. /* set up set-features taskfile */
  3818. DPRINTK("set features - SATA features\n");
  3819. ata_tf_init(dev, &tf);
  3820. tf.command = ATA_CMD_SET_FEATURES;
  3821. tf.feature = enable;
  3822. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  3823. tf.protocol = ATA_PROT_NODATA;
  3824. tf.nsect = feature;
  3825. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
  3826. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  3827. return err_mask;
  3828. }
  3829. /**
  3830. * ata_dev_init_params - Issue INIT DEV PARAMS command
  3831. * @dev: Device to which command will be sent
  3832. * @heads: Number of heads (taskfile parameter)
  3833. * @sectors: Number of sectors (taskfile parameter)
  3834. *
  3835. * LOCKING:
  3836. * Kernel thread context (may sleep)
  3837. *
  3838. * RETURNS:
  3839. * 0 on success, AC_ERR_* mask otherwise.
  3840. */
  3841. static unsigned int ata_dev_init_params(struct ata_device *dev,
  3842. u16 heads, u16 sectors)
  3843. {
  3844. struct ata_taskfile tf;
  3845. unsigned int err_mask;
  3846. /* Number of sectors per track 1-255. Number of heads 1-16 */
  3847. if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
  3848. return AC_ERR_INVALID;
  3849. /* set up init dev params taskfile */
  3850. DPRINTK("init dev params \n");
  3851. ata_tf_init(dev, &tf);
  3852. tf.command = ATA_CMD_INIT_DEV_PARAMS;
  3853. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  3854. tf.protocol = ATA_PROT_NODATA;
  3855. tf.nsect = sectors;
  3856. tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
  3857. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
  3858. /* A clean abort indicates an original or just out of spec drive
  3859. and we should continue as we issue the setup based on the
  3860. drive reported working geometry */
  3861. if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
  3862. err_mask = 0;
  3863. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  3864. return err_mask;
  3865. }
  3866. /**
  3867. * ata_sg_clean - Unmap DMA memory associated with command
  3868. * @qc: Command containing DMA memory to be released
  3869. *
  3870. * Unmap all mapped DMA memory associated with this command.
  3871. *
  3872. * LOCKING:
  3873. * spin_lock_irqsave(host lock)
  3874. */
  3875. void ata_sg_clean(struct ata_queued_cmd *qc)
  3876. {
  3877. struct ata_port *ap = qc->ap;
  3878. struct scatterlist *sg = qc->sg;
  3879. int dir = qc->dma_dir;
  3880. WARN_ON(sg == NULL);
  3881. VPRINTK("unmapping %u sg elements\n", qc->n_elem);
  3882. if (qc->n_elem)
  3883. dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
  3884. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  3885. qc->sg = NULL;
  3886. }
  3887. /**
  3888. * atapi_check_dma - Check whether ATAPI DMA can be supported
  3889. * @qc: Metadata associated with taskfile to check
  3890. *
  3891. * Allow low-level driver to filter ATA PACKET commands, returning
  3892. * a status indicating whether or not it is OK to use DMA for the
  3893. * supplied PACKET command.
  3894. *
  3895. * LOCKING:
  3896. * spin_lock_irqsave(host lock)
  3897. *
  3898. * RETURNS: 0 when ATAPI DMA can be used
  3899. * nonzero otherwise
  3900. */
  3901. int atapi_check_dma(struct ata_queued_cmd *qc)
  3902. {
  3903. struct ata_port *ap = qc->ap;
  3904. /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
  3905. * few ATAPI devices choke on such DMA requests.
  3906. */
  3907. if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
  3908. unlikely(qc->nbytes & 15))
  3909. return 1;
  3910. if (ap->ops->check_atapi_dma)
  3911. return ap->ops->check_atapi_dma(qc);
  3912. return 0;
  3913. }
  3914. /**
  3915. * ata_std_qc_defer - Check whether a qc needs to be deferred
  3916. * @qc: ATA command in question
  3917. *
  3918. * Non-NCQ commands cannot run with any other command, NCQ or
  3919. * not. As upper layer only knows the queue depth, we are
  3920. * responsible for maintaining exclusion. This function checks
  3921. * whether a new command @qc can be issued.
  3922. *
  3923. * LOCKING:
  3924. * spin_lock_irqsave(host lock)
  3925. *
  3926. * RETURNS:
  3927. * ATA_DEFER_* if deferring is needed, 0 otherwise.
  3928. */
  3929. int ata_std_qc_defer(struct ata_queued_cmd *qc)
  3930. {
  3931. struct ata_link *link = qc->dev->link;
  3932. if (qc->tf.protocol == ATA_PROT_NCQ) {
  3933. if (!ata_tag_valid(link->active_tag))
  3934. return 0;
  3935. } else {
  3936. if (!ata_tag_valid(link->active_tag) && !link->sactive)
  3937. return 0;
  3938. }
  3939. return ATA_DEFER_LINK;
  3940. }
  3941. void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
  3942. /**
  3943. * ata_sg_init - Associate command with scatter-gather table.
  3944. * @qc: Command to be associated
  3945. * @sg: Scatter-gather table.
  3946. * @n_elem: Number of elements in s/g table.
  3947. *
  3948. * Initialize the data-related elements of queued_cmd @qc
  3949. * to point to a scatter-gather table @sg, containing @n_elem
  3950. * elements.
  3951. *
  3952. * LOCKING:
  3953. * spin_lock_irqsave(host lock)
  3954. */
  3955. void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
  3956. unsigned int n_elem)
  3957. {
  3958. qc->sg = sg;
  3959. qc->n_elem = n_elem;
  3960. qc->cursg = qc->sg;
  3961. }
  3962. /**
  3963. * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
  3964. * @qc: Command with scatter-gather table to be mapped.
  3965. *
  3966. * DMA-map the scatter-gather table associated with queued_cmd @qc.
  3967. *
  3968. * LOCKING:
  3969. * spin_lock_irqsave(host lock)
  3970. *
  3971. * RETURNS:
  3972. * Zero on success, negative on error.
  3973. *
  3974. */
  3975. static int ata_sg_setup(struct ata_queued_cmd *qc)
  3976. {
  3977. struct ata_port *ap = qc->ap;
  3978. unsigned int n_elem;
  3979. VPRINTK("ENTER, ata%u\n", ap->print_id);
  3980. n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
  3981. if (n_elem < 1)
  3982. return -1;
  3983. DPRINTK("%d sg elements mapped\n", n_elem);
  3984. qc->n_elem = n_elem;
  3985. qc->flags |= ATA_QCFLAG_DMAMAP;
  3986. return 0;
  3987. }
  3988. /**
  3989. * swap_buf_le16 - swap halves of 16-bit words in place
  3990. * @buf: Buffer to swap
  3991. * @buf_words: Number of 16-bit words in buffer.
  3992. *
  3993. * Swap halves of 16-bit words if needed to convert from
  3994. * little-endian byte order to native cpu byte order, or
  3995. * vice-versa.
  3996. *
  3997. * LOCKING:
  3998. * Inherited from caller.
  3999. */
  4000. void swap_buf_le16(u16 *buf, unsigned int buf_words)
  4001. {
  4002. #ifdef __BIG_ENDIAN
  4003. unsigned int i;
  4004. for (i = 0; i < buf_words; i++)
  4005. buf[i] = le16_to_cpu(buf[i]);
  4006. #endif /* __BIG_ENDIAN */
  4007. }
  4008. /**
  4009. * ata_qc_new - Request an available ATA command, for queueing
  4010. * @ap: Port associated with device @dev
  4011. * @dev: Device from whom we request an available command structure
  4012. *
  4013. * LOCKING:
  4014. * None.
  4015. */
  4016. static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
  4017. {
  4018. struct ata_queued_cmd *qc = NULL;
  4019. unsigned int i;
  4020. /* no command while frozen */
  4021. if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
  4022. return NULL;
  4023. /* the last tag is reserved for internal command. */
  4024. for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
  4025. if (!test_and_set_bit(i, &ap->qc_allocated)) {
  4026. qc = __ata_qc_from_tag(ap, i);
  4027. break;
  4028. }
  4029. if (qc)
  4030. qc->tag = i;
  4031. return qc;
  4032. }
  4033. /**
  4034. * ata_qc_new_init - Request an available ATA command, and initialize it
  4035. * @dev: Device from whom we request an available command structure
  4036. * @tag: command tag
  4037. *
  4038. * LOCKING:
  4039. * None.
  4040. */
  4041. struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
  4042. {
  4043. struct ata_port *ap = dev->link->ap;
  4044. struct ata_queued_cmd *qc;
  4045. qc = ata_qc_new(ap);
  4046. if (qc) {
  4047. qc->scsicmd = NULL;
  4048. qc->ap = ap;
  4049. qc->dev = dev;
  4050. ata_qc_reinit(qc);
  4051. }
  4052. return qc;
  4053. }
  4054. /**
  4055. * ata_qc_free - free unused ata_queued_cmd
  4056. * @qc: Command to complete
  4057. *
  4058. * Designed to free unused ata_queued_cmd object
  4059. * in case something prevents using it.
  4060. *
  4061. * LOCKING:
  4062. * spin_lock_irqsave(host lock)
  4063. */
  4064. void ata_qc_free(struct ata_queued_cmd *qc)
  4065. {
  4066. struct ata_port *ap = qc->ap;
  4067. unsigned int tag;
  4068. WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
  4069. qc->flags = 0;
  4070. tag = qc->tag;
  4071. if (likely(ata_tag_valid(tag))) {
  4072. qc->tag = ATA_TAG_POISON;
  4073. clear_bit(tag, &ap->qc_allocated);
  4074. }
  4075. }
  4076. void __ata_qc_complete(struct ata_queued_cmd *qc)
  4077. {
  4078. struct ata_port *ap = qc->ap;
  4079. struct ata_link *link = qc->dev->link;
  4080. WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
  4081. WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
  4082. if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
  4083. ata_sg_clean(qc);
  4084. /* command should be marked inactive atomically with qc completion */
  4085. if (qc->tf.protocol == ATA_PROT_NCQ) {
  4086. link->sactive &= ~(1 << qc->tag);
  4087. if (!link->sactive)
  4088. ap->nr_active_links--;
  4089. } else {
  4090. link->active_tag = ATA_TAG_POISON;
  4091. ap->nr_active_links--;
  4092. }
  4093. /* clear exclusive status */
  4094. if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
  4095. ap->excl_link == link))
  4096. ap->excl_link = NULL;
  4097. /* atapi: mark qc as inactive to prevent the interrupt handler
  4098. * from completing the command twice later, before the error handler
  4099. * is called. (when rc != 0 and atapi request sense is needed)
  4100. */
  4101. qc->flags &= ~ATA_QCFLAG_ACTIVE;
  4102. ap->qc_active &= ~(1 << qc->tag);
  4103. /* call completion callback */
  4104. qc->complete_fn(qc);
  4105. }
  4106. static void fill_result_tf(struct ata_queued_cmd *qc)
  4107. {
  4108. struct ata_port *ap = qc->ap;
  4109. qc->result_tf.flags = qc->tf.flags;
  4110. ap->ops->qc_fill_rtf(qc);
  4111. }
  4112. static void ata_verify_xfer(struct ata_queued_cmd *qc)
  4113. {
  4114. struct ata_device *dev = qc->dev;
  4115. if (ata_tag_internal(qc->tag))
  4116. return;
  4117. if (ata_is_nodata(qc->tf.protocol))
  4118. return;
  4119. if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
  4120. return;
  4121. dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
  4122. }
  4123. /**
  4124. * ata_qc_complete - Complete an active ATA command
  4125. * @qc: Command to complete
  4126. *
  4127. * Indicate to the mid and upper layers that an ATA
  4128. * command has completed, with either an ok or not-ok status.
  4129. *
  4130. * LOCKING:
  4131. * spin_lock_irqsave(host lock)
  4132. */
  4133. void ata_qc_complete(struct ata_queued_cmd *qc)
  4134. {
  4135. struct ata_port *ap = qc->ap;
  4136. /* XXX: New EH and old EH use different mechanisms to
  4137. * synchronize EH with regular execution path.
  4138. *
  4139. * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
  4140. * Normal execution path is responsible for not accessing a
  4141. * failed qc. libata core enforces the rule by returning NULL
  4142. * from ata_qc_from_tag() for failed qcs.
  4143. *
  4144. * Old EH depends on ata_qc_complete() nullifying completion
  4145. * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
  4146. * not synchronize with interrupt handler. Only PIO task is
  4147. * taken care of.
  4148. */
  4149. if (ap->ops->error_handler) {
  4150. struct ata_device *dev = qc->dev;
  4151. struct ata_eh_info *ehi = &dev->link->eh_info;
  4152. WARN_ON(ap->pflags & ATA_PFLAG_FROZEN);
  4153. if (unlikely(qc->err_mask))
  4154. qc->flags |= ATA_QCFLAG_FAILED;
  4155. if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
  4156. if (!ata_tag_internal(qc->tag)) {
  4157. /* always fill result TF for failed qc */
  4158. fill_result_tf(qc);
  4159. ata_qc_schedule_eh(qc);
  4160. return;
  4161. }
  4162. }
  4163. /* read result TF if requested */
  4164. if (qc->flags & ATA_QCFLAG_RESULT_TF)
  4165. fill_result_tf(qc);
  4166. /* Some commands need post-processing after successful
  4167. * completion.
  4168. */
  4169. switch (qc->tf.command) {
  4170. case ATA_CMD_SET_FEATURES:
  4171. if (qc->tf.feature != SETFEATURES_WC_ON &&
  4172. qc->tf.feature != SETFEATURES_WC_OFF)
  4173. break;
  4174. /* fall through */
  4175. case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
  4176. case ATA_CMD_SET_MULTI: /* multi_count changed */
  4177. /* revalidate device */
  4178. ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
  4179. ata_port_schedule_eh(ap);
  4180. break;
  4181. case ATA_CMD_SLEEP:
  4182. dev->flags |= ATA_DFLAG_SLEEPING;
  4183. break;
  4184. }
  4185. if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
  4186. ata_verify_xfer(qc);
  4187. __ata_qc_complete(qc);
  4188. } else {
  4189. if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
  4190. return;
  4191. /* read result TF if failed or requested */
  4192. if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
  4193. fill_result_tf(qc);
  4194. __ata_qc_complete(qc);
  4195. }
  4196. }
  4197. /**
  4198. * ata_qc_complete_multiple - Complete multiple qcs successfully
  4199. * @ap: port in question
  4200. * @qc_active: new qc_active mask
  4201. *
  4202. * Complete in-flight commands. This functions is meant to be
  4203. * called from low-level driver's interrupt routine to complete
  4204. * requests normally. ap->qc_active and @qc_active is compared
  4205. * and commands are completed accordingly.
  4206. *
  4207. * LOCKING:
  4208. * spin_lock_irqsave(host lock)
  4209. *
  4210. * RETURNS:
  4211. * Number of completed commands on success, -errno otherwise.
  4212. */
  4213. int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active)
  4214. {
  4215. int nr_done = 0;
  4216. u32 done_mask;
  4217. int i;
  4218. done_mask = ap->qc_active ^ qc_active;
  4219. if (unlikely(done_mask & qc_active)) {
  4220. ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
  4221. "(%08x->%08x)\n", ap->qc_active, qc_active);
  4222. return -EINVAL;
  4223. }
  4224. for (i = 0; i < ATA_MAX_QUEUE; i++) {
  4225. struct ata_queued_cmd *qc;
  4226. if (!(done_mask & (1 << i)))
  4227. continue;
  4228. if ((qc = ata_qc_from_tag(ap, i))) {
  4229. ata_qc_complete(qc);
  4230. nr_done++;
  4231. }
  4232. }
  4233. return nr_done;
  4234. }
  4235. /**
  4236. * ata_qc_issue - issue taskfile to device
  4237. * @qc: command to issue to device
  4238. *
  4239. * Prepare an ATA command to submission to device.
  4240. * This includes mapping the data into a DMA-able
  4241. * area, filling in the S/G table, and finally
  4242. * writing the taskfile to hardware, starting the command.
  4243. *
  4244. * LOCKING:
  4245. * spin_lock_irqsave(host lock)
  4246. */
  4247. void ata_qc_issue(struct ata_queued_cmd *qc)
  4248. {
  4249. struct ata_port *ap = qc->ap;
  4250. struct ata_link *link = qc->dev->link;
  4251. u8 prot = qc->tf.protocol;
  4252. /* Make sure only one non-NCQ command is outstanding. The
  4253. * check is skipped for old EH because it reuses active qc to
  4254. * request ATAPI sense.
  4255. */
  4256. WARN_ON(ap->ops->error_handler && ata_tag_valid(link->active_tag));
  4257. if (ata_is_ncq(prot)) {
  4258. WARN_ON(link->sactive & (1 << qc->tag));
  4259. if (!link->sactive)
  4260. ap->nr_active_links++;
  4261. link->sactive |= 1 << qc->tag;
  4262. } else {
  4263. WARN_ON(link->sactive);
  4264. ap->nr_active_links++;
  4265. link->active_tag = qc->tag;
  4266. }
  4267. qc->flags |= ATA_QCFLAG_ACTIVE;
  4268. ap->qc_active |= 1 << qc->tag;
  4269. /* We guarantee to LLDs that they will have at least one
  4270. * non-zero sg if the command is a data command.
  4271. */
  4272. BUG_ON(ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes));
  4273. if (ata_is_dma(prot) || (ata_is_pio(prot) &&
  4274. (ap->flags & ATA_FLAG_PIO_DMA)))
  4275. if (ata_sg_setup(qc))
  4276. goto sg_err;
  4277. /* if device is sleeping, schedule reset and abort the link */
  4278. if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
  4279. link->eh_info.action |= ATA_EH_RESET;
  4280. ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
  4281. ata_link_abort(link);
  4282. return;
  4283. }
  4284. ap->ops->qc_prep(qc);
  4285. qc->err_mask |= ap->ops->qc_issue(qc);
  4286. if (unlikely(qc->err_mask))
  4287. goto err;
  4288. return;
  4289. sg_err:
  4290. qc->err_mask |= AC_ERR_SYSTEM;
  4291. err:
  4292. ata_qc_complete(qc);
  4293. }
  4294. /**
  4295. * sata_scr_valid - test whether SCRs are accessible
  4296. * @link: ATA link to test SCR accessibility for
  4297. *
  4298. * Test whether SCRs are accessible for @link.
  4299. *
  4300. * LOCKING:
  4301. * None.
  4302. *
  4303. * RETURNS:
  4304. * 1 if SCRs are accessible, 0 otherwise.
  4305. */
  4306. int sata_scr_valid(struct ata_link *link)
  4307. {
  4308. struct ata_port *ap = link->ap;
  4309. return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
  4310. }
  4311. /**
  4312. * sata_scr_read - read SCR register of the specified port
  4313. * @link: ATA link to read SCR for
  4314. * @reg: SCR to read
  4315. * @val: Place to store read value
  4316. *
  4317. * Read SCR register @reg of @link into *@val. This function is
  4318. * guaranteed to succeed if @link is ap->link, the cable type of
  4319. * the port is SATA and the port implements ->scr_read.
  4320. *
  4321. * LOCKING:
  4322. * None if @link is ap->link. Kernel thread context otherwise.
  4323. *
  4324. * RETURNS:
  4325. * 0 on success, negative errno on failure.
  4326. */
  4327. int sata_scr_read(struct ata_link *link, int reg, u32 *val)
  4328. {
  4329. if (ata_is_host_link(link)) {
  4330. if (sata_scr_valid(link))
  4331. return link->ap->ops->scr_read(link, reg, val);
  4332. return -EOPNOTSUPP;
  4333. }
  4334. return sata_pmp_scr_read(link, reg, val);
  4335. }
  4336. /**
  4337. * sata_scr_write - write SCR register of the specified port
  4338. * @link: ATA link to write SCR for
  4339. * @reg: SCR to write
  4340. * @val: value to write
  4341. *
  4342. * Write @val to SCR register @reg of @link. This function is
  4343. * guaranteed to succeed if @link is ap->link, the cable type of
  4344. * the port is SATA and the port implements ->scr_read.
  4345. *
  4346. * LOCKING:
  4347. * None if @link is ap->link. Kernel thread context otherwise.
  4348. *
  4349. * RETURNS:
  4350. * 0 on success, negative errno on failure.
  4351. */
  4352. int sata_scr_write(struct ata_link *link, int reg, u32 val)
  4353. {
  4354. if (ata_is_host_link(link)) {
  4355. if (sata_scr_valid(link))
  4356. return link->ap->ops->scr_write(link, reg, val);
  4357. return -EOPNOTSUPP;
  4358. }
  4359. return sata_pmp_scr_write(link, reg, val);
  4360. }
  4361. /**
  4362. * sata_scr_write_flush - write SCR register of the specified port and flush
  4363. * @link: ATA link to write SCR for
  4364. * @reg: SCR to write
  4365. * @val: value to write
  4366. *
  4367. * This function is identical to sata_scr_write() except that this
  4368. * function performs flush after writing to the register.
  4369. *
  4370. * LOCKING:
  4371. * None if @link is ap->link. Kernel thread context otherwise.
  4372. *
  4373. * RETURNS:
  4374. * 0 on success, negative errno on failure.
  4375. */
  4376. int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
  4377. {
  4378. if (ata_is_host_link(link)) {
  4379. int rc;
  4380. if (sata_scr_valid(link)) {
  4381. rc = link->ap->ops->scr_write(link, reg, val);
  4382. if (rc == 0)
  4383. rc = link->ap->ops->scr_read(link, reg, &val);
  4384. return rc;
  4385. }
  4386. return -EOPNOTSUPP;
  4387. }
  4388. return sata_pmp_scr_write(link, reg, val);
  4389. }
  4390. /**
  4391. * ata_phys_link_online - test whether the given link is online
  4392. * @link: ATA link to test
  4393. *
  4394. * Test whether @link is online. Note that this function returns
  4395. * 0 if online status of @link cannot be obtained, so
  4396. * ata_link_online(link) != !ata_link_offline(link).
  4397. *
  4398. * LOCKING:
  4399. * None.
  4400. *
  4401. * RETURNS:
  4402. * True if the port online status is available and online.
  4403. */
  4404. bool ata_phys_link_online(struct ata_link *link)
  4405. {
  4406. u32 sstatus;
  4407. if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
  4408. (sstatus & 0xf) == 0x3)
  4409. return true;
  4410. return false;
  4411. }
  4412. /**
  4413. * ata_phys_link_offline - test whether the given link is offline
  4414. * @link: ATA link to test
  4415. *
  4416. * Test whether @link is offline. Note that this function
  4417. * returns 0 if offline status of @link cannot be obtained, so
  4418. * ata_link_online(link) != !ata_link_offline(link).
  4419. *
  4420. * LOCKING:
  4421. * None.
  4422. *
  4423. * RETURNS:
  4424. * True if the port offline status is available and offline.
  4425. */
  4426. bool ata_phys_link_offline(struct ata_link *link)
  4427. {
  4428. u32 sstatus;
  4429. if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
  4430. (sstatus & 0xf) != 0x3)
  4431. return true;
  4432. return false;
  4433. }
  4434. /**
  4435. * ata_link_online - test whether the given link is online
  4436. * @link: ATA link to test
  4437. *
  4438. * Test whether @link is online. This is identical to
  4439. * ata_phys_link_online() when there's no slave link. When
  4440. * there's a slave link, this function should only be called on
  4441. * the master link and will return true if any of M/S links is
  4442. * online.
  4443. *
  4444. * LOCKING:
  4445. * None.
  4446. *
  4447. * RETURNS:
  4448. * True if the port online status is available and online.
  4449. */
  4450. bool ata_link_online(struct ata_link *link)
  4451. {
  4452. struct ata_link *slave = link->ap->slave_link;
  4453. WARN_ON(link == slave); /* shouldn't be called on slave link */
  4454. return ata_phys_link_online(link) ||
  4455. (slave && ata_phys_link_online(slave));
  4456. }
  4457. /**
  4458. * ata_link_offline - test whether the given link is offline
  4459. * @link: ATA link to test
  4460. *
  4461. * Test whether @link is offline. This is identical to
  4462. * ata_phys_link_offline() when there's no slave link. When
  4463. * there's a slave link, this function should only be called on
  4464. * the master link and will return true if both M/S links are
  4465. * offline.
  4466. *
  4467. * LOCKING:
  4468. * None.
  4469. *
  4470. * RETURNS:
  4471. * True if the port offline status is available and offline.
  4472. */
  4473. bool ata_link_offline(struct ata_link *link)
  4474. {
  4475. struct ata_link *slave = link->ap->slave_link;
  4476. WARN_ON(link == slave); /* shouldn't be called on slave link */
  4477. return ata_phys_link_offline(link) &&
  4478. (!slave || ata_phys_link_offline(slave));
  4479. }
  4480. #ifdef CONFIG_PM
  4481. static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
  4482. unsigned int action, unsigned int ehi_flags,
  4483. int wait)
  4484. {
  4485. unsigned long flags;
  4486. int i, rc;
  4487. for (i = 0; i < host->n_ports; i++) {
  4488. struct ata_port *ap = host->ports[i];
  4489. struct ata_link *link;
  4490. /* Previous resume operation might still be in
  4491. * progress. Wait for PM_PENDING to clear.
  4492. */
  4493. if (ap->pflags & ATA_PFLAG_PM_PENDING) {
  4494. ata_port_wait_eh(ap);
  4495. WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
  4496. }
  4497. /* request PM ops to EH */
  4498. spin_lock_irqsave(ap->lock, flags);
  4499. ap->pm_mesg = mesg;
  4500. if (wait) {
  4501. rc = 0;
  4502. ap->pm_result = &rc;
  4503. }
  4504. ap->pflags |= ATA_PFLAG_PM_PENDING;
  4505. __ata_port_for_each_link(link, ap) {
  4506. link->eh_info.action |= action;
  4507. link->eh_info.flags |= ehi_flags;
  4508. }
  4509. ata_port_schedule_eh(ap);
  4510. spin_unlock_irqrestore(ap->lock, flags);
  4511. /* wait and check result */
  4512. if (wait) {
  4513. ata_port_wait_eh(ap);
  4514. WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
  4515. if (rc)
  4516. return rc;
  4517. }
  4518. }
  4519. return 0;
  4520. }
  4521. /**
  4522. * ata_host_suspend - suspend host
  4523. * @host: host to suspend
  4524. * @mesg: PM message
  4525. *
  4526. * Suspend @host. Actual operation is performed by EH. This
  4527. * function requests EH to perform PM operations and waits for EH
  4528. * to finish.
  4529. *
  4530. * LOCKING:
  4531. * Kernel thread context (may sleep).
  4532. *
  4533. * RETURNS:
  4534. * 0 on success, -errno on failure.
  4535. */
  4536. int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
  4537. {
  4538. int rc;
  4539. /*
  4540. * disable link pm on all ports before requesting
  4541. * any pm activity
  4542. */
  4543. ata_lpm_enable(host);
  4544. rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
  4545. if (rc == 0)
  4546. host->dev->power.power_state = mesg;
  4547. return rc;
  4548. }
  4549. /**
  4550. * ata_host_resume - resume host
  4551. * @host: host to resume
  4552. *
  4553. * Resume @host. Actual operation is performed by EH. This
  4554. * function requests EH to perform PM operations and returns.
  4555. * Note that all resume operations are performed parallely.
  4556. *
  4557. * LOCKING:
  4558. * Kernel thread context (may sleep).
  4559. */
  4560. void ata_host_resume(struct ata_host *host)
  4561. {
  4562. ata_host_request_pm(host, PMSG_ON, ATA_EH_RESET,
  4563. ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
  4564. host->dev->power.power_state = PMSG_ON;
  4565. /* reenable link pm */
  4566. ata_lpm_disable(host);
  4567. }
  4568. #endif
  4569. /**
  4570. * ata_port_start - Set port up for dma.
  4571. * @ap: Port to initialize
  4572. *
  4573. * Called just after data structures for each port are
  4574. * initialized. Allocates space for PRD table.
  4575. *
  4576. * May be used as the port_start() entry in ata_port_operations.
  4577. *
  4578. * LOCKING:
  4579. * Inherited from caller.
  4580. */
  4581. int ata_port_start(struct ata_port *ap)
  4582. {
  4583. struct device *dev = ap->dev;
  4584. ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
  4585. GFP_KERNEL);
  4586. if (!ap->prd)
  4587. return -ENOMEM;
  4588. return 0;
  4589. }
  4590. /**
  4591. * ata_dev_init - Initialize an ata_device structure
  4592. * @dev: Device structure to initialize
  4593. *
  4594. * Initialize @dev in preparation for probing.
  4595. *
  4596. * LOCKING:
  4597. * Inherited from caller.
  4598. */
  4599. void ata_dev_init(struct ata_device *dev)
  4600. {
  4601. struct ata_link *link = ata_dev_phys_link(dev);
  4602. struct ata_port *ap = link->ap;
  4603. unsigned long flags;
  4604. /* SATA spd limit is bound to the attached device, reset together */
  4605. link->sata_spd_limit = link->hw_sata_spd_limit;
  4606. link->sata_spd = 0;
  4607. /* High bits of dev->flags are used to record warm plug
  4608. * requests which occur asynchronously. Synchronize using
  4609. * host lock.
  4610. */
  4611. spin_lock_irqsave(ap->lock, flags);
  4612. dev->flags &= ~ATA_DFLAG_INIT_MASK;
  4613. dev->horkage = 0;
  4614. spin_unlock_irqrestore(ap->lock, flags);
  4615. memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
  4616. sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
  4617. dev->pio_mask = UINT_MAX;
  4618. dev->mwdma_mask = UINT_MAX;
  4619. dev->udma_mask = UINT_MAX;
  4620. }
  4621. /**
  4622. * ata_link_init - Initialize an ata_link structure
  4623. * @ap: ATA port link is attached to
  4624. * @link: Link structure to initialize
  4625. * @pmp: Port multiplier port number
  4626. *
  4627. * Initialize @link.
  4628. *
  4629. * LOCKING:
  4630. * Kernel thread context (may sleep)
  4631. */
  4632. void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
  4633. {
  4634. int i;
  4635. /* clear everything except for devices */
  4636. memset(link, 0, offsetof(struct ata_link, device[0]));
  4637. link->ap = ap;
  4638. link->pmp = pmp;
  4639. link->active_tag = ATA_TAG_POISON;
  4640. link->hw_sata_spd_limit = UINT_MAX;
  4641. /* can't use iterator, ap isn't initialized yet */
  4642. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  4643. struct ata_device *dev = &link->device[i];
  4644. dev->link = link;
  4645. dev->devno = dev - link->device;
  4646. ata_dev_init(dev);
  4647. }
  4648. }
  4649. /**
  4650. * sata_link_init_spd - Initialize link->sata_spd_limit
  4651. * @link: Link to configure sata_spd_limit for
  4652. *
  4653. * Initialize @link->[hw_]sata_spd_limit to the currently
  4654. * configured value.
  4655. *
  4656. * LOCKING:
  4657. * Kernel thread context (may sleep).
  4658. *
  4659. * RETURNS:
  4660. * 0 on success, -errno on failure.
  4661. */
  4662. int sata_link_init_spd(struct ata_link *link)
  4663. {
  4664. u8 spd;
  4665. int rc;
  4666. rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
  4667. if (rc)
  4668. return rc;
  4669. spd = (link->saved_scontrol >> 4) & 0xf;
  4670. if (spd)
  4671. link->hw_sata_spd_limit &= (1 << spd) - 1;
  4672. ata_force_link_limits(link);
  4673. link->sata_spd_limit = link->hw_sata_spd_limit;
  4674. return 0;
  4675. }
  4676. /**
  4677. * ata_port_alloc - allocate and initialize basic ATA port resources
  4678. * @host: ATA host this allocated port belongs to
  4679. *
  4680. * Allocate and initialize basic ATA port resources.
  4681. *
  4682. * RETURNS:
  4683. * Allocate ATA port on success, NULL on failure.
  4684. *
  4685. * LOCKING:
  4686. * Inherited from calling layer (may sleep).
  4687. */
  4688. struct ata_port *ata_port_alloc(struct ata_host *host)
  4689. {
  4690. struct ata_port *ap;
  4691. DPRINTK("ENTER\n");
  4692. ap = kzalloc(sizeof(*ap), GFP_KERNEL);
  4693. if (!ap)
  4694. return NULL;
  4695. ap->pflags |= ATA_PFLAG_INITIALIZING;
  4696. ap->lock = &host->lock;
  4697. ap->flags = ATA_FLAG_DISABLED;
  4698. ap->print_id = -1;
  4699. ap->ctl = ATA_DEVCTL_OBS;
  4700. ap->host = host;
  4701. ap->dev = host->dev;
  4702. ap->last_ctl = 0xFF;
  4703. #if defined(ATA_VERBOSE_DEBUG)
  4704. /* turn on all debugging levels */
  4705. ap->msg_enable = 0x00FF;
  4706. #elif defined(ATA_DEBUG)
  4707. ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
  4708. #else
  4709. ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
  4710. #endif
  4711. #ifdef CONFIG_ATA_SFF
  4712. INIT_DELAYED_WORK(&ap->port_task, ata_pio_task);
  4713. #else
  4714. INIT_DELAYED_WORK(&ap->port_task, NULL);
  4715. #endif
  4716. INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
  4717. INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
  4718. INIT_LIST_HEAD(&ap->eh_done_q);
  4719. init_waitqueue_head(&ap->eh_wait_q);
  4720. init_completion(&ap->park_req_pending);
  4721. init_timer_deferrable(&ap->fastdrain_timer);
  4722. ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
  4723. ap->fastdrain_timer.data = (unsigned long)ap;
  4724. ap->cbl = ATA_CBL_NONE;
  4725. ata_link_init(ap, &ap->link, 0);
  4726. #ifdef ATA_IRQ_TRAP
  4727. ap->stats.unhandled_irq = 1;
  4728. ap->stats.idle_irq = 1;
  4729. #endif
  4730. return ap;
  4731. }
  4732. static void ata_host_release(struct device *gendev, void *res)
  4733. {
  4734. struct ata_host *host = dev_get_drvdata(gendev);
  4735. int i;
  4736. for (i = 0; i < host->n_ports; i++) {
  4737. struct ata_port *ap = host->ports[i];
  4738. if (!ap)
  4739. continue;
  4740. if (ap->scsi_host)
  4741. scsi_host_put(ap->scsi_host);
  4742. kfree(ap->pmp_link);
  4743. kfree(ap->slave_link);
  4744. kfree(ap);
  4745. host->ports[i] = NULL;
  4746. }
  4747. dev_set_drvdata(gendev, NULL);
  4748. }
  4749. /**
  4750. * ata_host_alloc - allocate and init basic ATA host resources
  4751. * @dev: generic device this host is associated with
  4752. * @max_ports: maximum number of ATA ports associated with this host
  4753. *
  4754. * Allocate and initialize basic ATA host resources. LLD calls
  4755. * this function to allocate a host, initializes it fully and
  4756. * attaches it using ata_host_register().
  4757. *
  4758. * @max_ports ports are allocated and host->n_ports is
  4759. * initialized to @max_ports. The caller is allowed to decrease
  4760. * host->n_ports before calling ata_host_register(). The unused
  4761. * ports will be automatically freed on registration.
  4762. *
  4763. * RETURNS:
  4764. * Allocate ATA host on success, NULL on failure.
  4765. *
  4766. * LOCKING:
  4767. * Inherited from calling layer (may sleep).
  4768. */
  4769. struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
  4770. {
  4771. struct ata_host *host;
  4772. size_t sz;
  4773. int i;
  4774. DPRINTK("ENTER\n");
  4775. if (!devres_open_group(dev, NULL, GFP_KERNEL))
  4776. return NULL;
  4777. /* alloc a container for our list of ATA ports (buses) */
  4778. sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
  4779. /* alloc a container for our list of ATA ports (buses) */
  4780. host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
  4781. if (!host)
  4782. goto err_out;
  4783. devres_add(dev, host);
  4784. dev_set_drvdata(dev, host);
  4785. spin_lock_init(&host->lock);
  4786. host->dev = dev;
  4787. host->n_ports = max_ports;
  4788. /* allocate ports bound to this host */
  4789. for (i = 0; i < max_ports; i++) {
  4790. struct ata_port *ap;
  4791. ap = ata_port_alloc(host);
  4792. if (!ap)
  4793. goto err_out;
  4794. ap->port_no = i;
  4795. host->ports[i] = ap;
  4796. }
  4797. devres_remove_group(dev, NULL);
  4798. return host;
  4799. err_out:
  4800. devres_release_group(dev, NULL);
  4801. return NULL;
  4802. }
  4803. /**
  4804. * ata_host_alloc_pinfo - alloc host and init with port_info array
  4805. * @dev: generic device this host is associated with
  4806. * @ppi: array of ATA port_info to initialize host with
  4807. * @n_ports: number of ATA ports attached to this host
  4808. *
  4809. * Allocate ATA host and initialize with info from @ppi. If NULL
  4810. * terminated, @ppi may contain fewer entries than @n_ports. The
  4811. * last entry will be used for the remaining ports.
  4812. *
  4813. * RETURNS:
  4814. * Allocate ATA host on success, NULL on failure.
  4815. *
  4816. * LOCKING:
  4817. * Inherited from calling layer (may sleep).
  4818. */
  4819. struct ata_host *ata_host_alloc_pinfo(struct device *dev,
  4820. const struct ata_port_info * const * ppi,
  4821. int n_ports)
  4822. {
  4823. const struct ata_port_info *pi;
  4824. struct ata_host *host;
  4825. int i, j;
  4826. host = ata_host_alloc(dev, n_ports);
  4827. if (!host)
  4828. return NULL;
  4829. for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
  4830. struct ata_port *ap = host->ports[i];
  4831. if (ppi[j])
  4832. pi = ppi[j++];
  4833. ap->pio_mask = pi->pio_mask;
  4834. ap->mwdma_mask = pi->mwdma_mask;
  4835. ap->udma_mask = pi->udma_mask;
  4836. ap->flags |= pi->flags;
  4837. ap->link.flags |= pi->link_flags;
  4838. ap->ops = pi->port_ops;
  4839. if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
  4840. host->ops = pi->port_ops;
  4841. }
  4842. return host;
  4843. }
  4844. /**
  4845. * ata_slave_link_init - initialize slave link
  4846. * @ap: port to initialize slave link for
  4847. *
  4848. * Create and initialize slave link for @ap. This enables slave
  4849. * link handling on the port.
  4850. *
  4851. * In libata, a port contains links and a link contains devices.
  4852. * There is single host link but if a PMP is attached to it,
  4853. * there can be multiple fan-out links. On SATA, there's usually
  4854. * a single device connected to a link but PATA and SATA
  4855. * controllers emulating TF based interface can have two - master
  4856. * and slave.
  4857. *
  4858. * However, there are a few controllers which don't fit into this
  4859. * abstraction too well - SATA controllers which emulate TF
  4860. * interface with both master and slave devices but also have
  4861. * separate SCR register sets for each device. These controllers
  4862. * need separate links for physical link handling
  4863. * (e.g. onlineness, link speed) but should be treated like a
  4864. * traditional M/S controller for everything else (e.g. command
  4865. * issue, softreset).
  4866. *
  4867. * slave_link is libata's way of handling this class of
  4868. * controllers without impacting core layer too much. For
  4869. * anything other than physical link handling, the default host
  4870. * link is used for both master and slave. For physical link
  4871. * handling, separate @ap->slave_link is used. All dirty details
  4872. * are implemented inside libata core layer. From LLD's POV, the
  4873. * only difference is that prereset, hardreset and postreset are
  4874. * called once more for the slave link, so the reset sequence
  4875. * looks like the following.
  4876. *
  4877. * prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
  4878. * softreset(M) -> postreset(M) -> postreset(S)
  4879. *
  4880. * Note that softreset is called only for the master. Softreset
  4881. * resets both M/S by definition, so SRST on master should handle
  4882. * both (the standard method will work just fine).
  4883. *
  4884. * LOCKING:
  4885. * Should be called before host is registered.
  4886. *
  4887. * RETURNS:
  4888. * 0 on success, -errno on failure.
  4889. */
  4890. int ata_slave_link_init(struct ata_port *ap)
  4891. {
  4892. struct ata_link *link;
  4893. WARN_ON(ap->slave_link);
  4894. WARN_ON(ap->flags & ATA_FLAG_PMP);
  4895. link = kzalloc(sizeof(*link), GFP_KERNEL);
  4896. if (!link)
  4897. return -ENOMEM;
  4898. ata_link_init(ap, link, 1);
  4899. ap->slave_link = link;
  4900. return 0;
  4901. }
  4902. static void ata_host_stop(struct device *gendev, void *res)
  4903. {
  4904. struct ata_host *host = dev_get_drvdata(gendev);
  4905. int i;
  4906. WARN_ON(!(host->flags & ATA_HOST_STARTED));
  4907. for (i = 0; i < host->n_ports; i++) {
  4908. struct ata_port *ap = host->ports[i];
  4909. if (ap->ops->port_stop)
  4910. ap->ops->port_stop(ap);
  4911. }
  4912. if (host->ops->host_stop)
  4913. host->ops->host_stop(host);
  4914. }
  4915. /**
  4916. * ata_finalize_port_ops - finalize ata_port_operations
  4917. * @ops: ata_port_operations to finalize
  4918. *
  4919. * An ata_port_operations can inherit from another ops and that
  4920. * ops can again inherit from another. This can go on as many
  4921. * times as necessary as long as there is no loop in the
  4922. * inheritance chain.
  4923. *
  4924. * Ops tables are finalized when the host is started. NULL or
  4925. * unspecified entries are inherited from the closet ancestor
  4926. * which has the method and the entry is populated with it.
  4927. * After finalization, the ops table directly points to all the
  4928. * methods and ->inherits is no longer necessary and cleared.
  4929. *
  4930. * Using ATA_OP_NULL, inheriting ops can force a method to NULL.
  4931. *
  4932. * LOCKING:
  4933. * None.
  4934. */
  4935. static void ata_finalize_port_ops(struct ata_port_operations *ops)
  4936. {
  4937. static DEFINE_SPINLOCK(lock);
  4938. const struct ata_port_operations *cur;
  4939. void **begin = (void **)ops;
  4940. void **end = (void **)&ops->inherits;
  4941. void **pp;
  4942. if (!ops || !ops->inherits)
  4943. return;
  4944. spin_lock(&lock);
  4945. for (cur = ops->inherits; cur; cur = cur->inherits) {
  4946. void **inherit = (void **)cur;
  4947. for (pp = begin; pp < end; pp++, inherit++)
  4948. if (!*pp)
  4949. *pp = *inherit;
  4950. }
  4951. for (pp = begin; pp < end; pp++)
  4952. if (IS_ERR(*pp))
  4953. *pp = NULL;
  4954. ops->inherits = NULL;
  4955. spin_unlock(&lock);
  4956. }
  4957. /**
  4958. * ata_host_start - start and freeze ports of an ATA host
  4959. * @host: ATA host to start ports for
  4960. *
  4961. * Start and then freeze ports of @host. Started status is
  4962. * recorded in host->flags, so this function can be called
  4963. * multiple times. Ports are guaranteed to get started only
  4964. * once. If host->ops isn't initialized yet, its set to the
  4965. * first non-dummy port ops.
  4966. *
  4967. * LOCKING:
  4968. * Inherited from calling layer (may sleep).
  4969. *
  4970. * RETURNS:
  4971. * 0 if all ports are started successfully, -errno otherwise.
  4972. */
  4973. int ata_host_start(struct ata_host *host)
  4974. {
  4975. int have_stop = 0;
  4976. void *start_dr = NULL;
  4977. int i, rc;
  4978. if (host->flags & ATA_HOST_STARTED)
  4979. return 0;
  4980. ata_finalize_port_ops(host->ops);
  4981. for (i = 0; i < host->n_ports; i++) {
  4982. struct ata_port *ap = host->ports[i];
  4983. ata_finalize_port_ops(ap->ops);
  4984. if (!host->ops && !ata_port_is_dummy(ap))
  4985. host->ops = ap->ops;
  4986. if (ap->ops->port_stop)
  4987. have_stop = 1;
  4988. }
  4989. if (host->ops->host_stop)
  4990. have_stop = 1;
  4991. if (have_stop) {
  4992. start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
  4993. if (!start_dr)
  4994. return -ENOMEM;
  4995. }
  4996. for (i = 0; i < host->n_ports; i++) {
  4997. struct ata_port *ap = host->ports[i];
  4998. if (ap->ops->port_start) {
  4999. rc = ap->ops->port_start(ap);
  5000. if (rc) {
  5001. if (rc != -ENODEV)
  5002. dev_printk(KERN_ERR, host->dev,
  5003. "failed to start port %d "
  5004. "(errno=%d)\n", i, rc);
  5005. goto err_out;
  5006. }
  5007. }
  5008. ata_eh_freeze_port(ap);
  5009. }
  5010. if (start_dr)
  5011. devres_add(host->dev, start_dr);
  5012. host->flags |= ATA_HOST_STARTED;
  5013. return 0;
  5014. err_out:
  5015. while (--i >= 0) {
  5016. struct ata_port *ap = host->ports[i];
  5017. if (ap->ops->port_stop)
  5018. ap->ops->port_stop(ap);
  5019. }
  5020. devres_free(start_dr);
  5021. return rc;
  5022. }
  5023. /**
  5024. * ata_sas_host_init - Initialize a host struct
  5025. * @host: host to initialize
  5026. * @dev: device host is attached to
  5027. * @flags: host flags
  5028. * @ops: port_ops
  5029. *
  5030. * LOCKING:
  5031. * PCI/etc. bus probe sem.
  5032. *
  5033. */
  5034. /* KILLME - the only user left is ipr */
  5035. void ata_host_init(struct ata_host *host, struct device *dev,
  5036. unsigned long flags, struct ata_port_operations *ops)
  5037. {
  5038. spin_lock_init(&host->lock);
  5039. host->dev = dev;
  5040. host->flags = flags;
  5041. host->ops = ops;
  5042. }
  5043. /**
  5044. * ata_host_register - register initialized ATA host
  5045. * @host: ATA host to register
  5046. * @sht: template for SCSI host
  5047. *
  5048. * Register initialized ATA host. @host is allocated using
  5049. * ata_host_alloc() and fully initialized by LLD. This function
  5050. * starts ports, registers @host with ATA and SCSI layers and
  5051. * probe registered devices.
  5052. *
  5053. * LOCKING:
  5054. * Inherited from calling layer (may sleep).
  5055. *
  5056. * RETURNS:
  5057. * 0 on success, -errno otherwise.
  5058. */
  5059. int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
  5060. {
  5061. int i, rc;
  5062. /* host must have been started */
  5063. if (!(host->flags & ATA_HOST_STARTED)) {
  5064. dev_printk(KERN_ERR, host->dev,
  5065. "BUG: trying to register unstarted host\n");
  5066. WARN_ON(1);
  5067. return -EINVAL;
  5068. }
  5069. /* Blow away unused ports. This happens when LLD can't
  5070. * determine the exact number of ports to allocate at
  5071. * allocation time.
  5072. */
  5073. for (i = host->n_ports; host->ports[i]; i++)
  5074. kfree(host->ports[i]);
  5075. /* give ports names and add SCSI hosts */
  5076. for (i = 0; i < host->n_ports; i++)
  5077. host->ports[i]->print_id = ata_print_id++;
  5078. rc = ata_scsi_add_hosts(host, sht);
  5079. if (rc)
  5080. return rc;
  5081. /* associate with ACPI nodes */
  5082. ata_acpi_associate(host);
  5083. /* set cable, sata_spd_limit and report */
  5084. for (i = 0; i < host->n_ports; i++) {
  5085. struct ata_port *ap = host->ports[i];
  5086. unsigned long xfer_mask;
  5087. /* set SATA cable type if still unset */
  5088. if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
  5089. ap->cbl = ATA_CBL_SATA;
  5090. /* init sata_spd_limit to the current value */
  5091. sata_link_init_spd(&ap->link);
  5092. if (ap->slave_link)
  5093. sata_link_init_spd(ap->slave_link);
  5094. /* print per-port info to dmesg */
  5095. xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
  5096. ap->udma_mask);
  5097. if (!ata_port_is_dummy(ap)) {
  5098. ata_port_printk(ap, KERN_INFO,
  5099. "%cATA max %s %s\n",
  5100. (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
  5101. ata_mode_string(xfer_mask),
  5102. ap->link.eh_info.desc);
  5103. ata_ehi_clear_desc(&ap->link.eh_info);
  5104. } else
  5105. ata_port_printk(ap, KERN_INFO, "DUMMY\n");
  5106. }
  5107. /* perform each probe synchronously */
  5108. DPRINTK("probe begin\n");
  5109. for (i = 0; i < host->n_ports; i++) {
  5110. struct ata_port *ap = host->ports[i];
  5111. /* probe */
  5112. if (ap->ops->error_handler) {
  5113. struct ata_eh_info *ehi = &ap->link.eh_info;
  5114. unsigned long flags;
  5115. ata_port_probe(ap);
  5116. /* kick EH for boot probing */
  5117. spin_lock_irqsave(ap->lock, flags);
  5118. ehi->probe_mask |= ATA_ALL_DEVICES;
  5119. ehi->action |= ATA_EH_RESET | ATA_EH_LPM;
  5120. ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
  5121. ap->pflags &= ~ATA_PFLAG_INITIALIZING;
  5122. ap->pflags |= ATA_PFLAG_LOADING;
  5123. ata_port_schedule_eh(ap);
  5124. spin_unlock_irqrestore(ap->lock, flags);
  5125. /* wait for EH to finish */
  5126. ata_port_wait_eh(ap);
  5127. } else {
  5128. DPRINTK("ata%u: bus probe begin\n", ap->print_id);
  5129. rc = ata_bus_probe(ap);
  5130. DPRINTK("ata%u: bus probe end\n", ap->print_id);
  5131. if (rc) {
  5132. /* FIXME: do something useful here?
  5133. * Current libata behavior will
  5134. * tear down everything when
  5135. * the module is removed
  5136. * or the h/w is unplugged.
  5137. */
  5138. }
  5139. }
  5140. }
  5141. /* probes are done, now scan each port's disk(s) */
  5142. DPRINTK("host probe begin\n");
  5143. for (i = 0; i < host->n_ports; i++) {
  5144. struct ata_port *ap = host->ports[i];
  5145. ata_scsi_scan_host(ap, 1);
  5146. }
  5147. return 0;
  5148. }
  5149. /**
  5150. * ata_host_activate - start host, request IRQ and register it
  5151. * @host: target ATA host
  5152. * @irq: IRQ to request
  5153. * @irq_handler: irq_handler used when requesting IRQ
  5154. * @irq_flags: irq_flags used when requesting IRQ
  5155. * @sht: scsi_host_template to use when registering the host
  5156. *
  5157. * After allocating an ATA host and initializing it, most libata
  5158. * LLDs perform three steps to activate the host - start host,
  5159. * request IRQ and register it. This helper takes necessasry
  5160. * arguments and performs the three steps in one go.
  5161. *
  5162. * An invalid IRQ skips the IRQ registration and expects the host to
  5163. * have set polling mode on the port. In this case, @irq_handler
  5164. * should be NULL.
  5165. *
  5166. * LOCKING:
  5167. * Inherited from calling layer (may sleep).
  5168. *
  5169. * RETURNS:
  5170. * 0 on success, -errno otherwise.
  5171. */
  5172. int ata_host_activate(struct ata_host *host, int irq,
  5173. irq_handler_t irq_handler, unsigned long irq_flags,
  5174. struct scsi_host_template *sht)
  5175. {
  5176. int i, rc;
  5177. rc = ata_host_start(host);
  5178. if (rc)
  5179. return rc;
  5180. /* Special case for polling mode */
  5181. if (!irq) {
  5182. WARN_ON(irq_handler);
  5183. return ata_host_register(host, sht);
  5184. }
  5185. rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
  5186. dev_driver_string(host->dev), host);
  5187. if (rc)
  5188. return rc;
  5189. for (i = 0; i < host->n_ports; i++)
  5190. ata_port_desc(host->ports[i], "irq %d", irq);
  5191. rc = ata_host_register(host, sht);
  5192. /* if failed, just free the IRQ and leave ports alone */
  5193. if (rc)
  5194. devm_free_irq(host->dev, irq, host);
  5195. return rc;
  5196. }
  5197. /**
  5198. * ata_port_detach - Detach ATA port in prepration of device removal
  5199. * @ap: ATA port to be detached
  5200. *
  5201. * Detach all ATA devices and the associated SCSI devices of @ap;
  5202. * then, remove the associated SCSI host. @ap is guaranteed to
  5203. * be quiescent on return from this function.
  5204. *
  5205. * LOCKING:
  5206. * Kernel thread context (may sleep).
  5207. */
  5208. static void ata_port_detach(struct ata_port *ap)
  5209. {
  5210. unsigned long flags;
  5211. struct ata_link *link;
  5212. struct ata_device *dev;
  5213. if (!ap->ops->error_handler)
  5214. goto skip_eh;
  5215. /* tell EH we're leaving & flush EH */
  5216. spin_lock_irqsave(ap->lock, flags);
  5217. ap->pflags |= ATA_PFLAG_UNLOADING;
  5218. spin_unlock_irqrestore(ap->lock, flags);
  5219. ata_port_wait_eh(ap);
  5220. /* EH is now guaranteed to see UNLOADING - EH context belongs
  5221. * to us. Restore SControl and disable all existing devices.
  5222. */
  5223. __ata_port_for_each_link(link, ap) {
  5224. sata_scr_write(link, SCR_CONTROL, link->saved_scontrol & 0xff0);
  5225. ata_link_for_each_dev(dev, link)
  5226. ata_dev_disable(dev);
  5227. }
  5228. /* Final freeze & EH. All in-flight commands are aborted. EH
  5229. * will be skipped and retrials will be terminated with bad
  5230. * target.
  5231. */
  5232. spin_lock_irqsave(ap->lock, flags);
  5233. ata_port_freeze(ap); /* won't be thawed */
  5234. spin_unlock_irqrestore(ap->lock, flags);
  5235. ata_port_wait_eh(ap);
  5236. cancel_rearming_delayed_work(&ap->hotplug_task);
  5237. skip_eh:
  5238. /* remove the associated SCSI host */
  5239. scsi_remove_host(ap->scsi_host);
  5240. }
  5241. /**
  5242. * ata_host_detach - Detach all ports of an ATA host
  5243. * @host: Host to detach
  5244. *
  5245. * Detach all ports of @host.
  5246. *
  5247. * LOCKING:
  5248. * Kernel thread context (may sleep).
  5249. */
  5250. void ata_host_detach(struct ata_host *host)
  5251. {
  5252. int i;
  5253. for (i = 0; i < host->n_ports; i++)
  5254. ata_port_detach(host->ports[i]);
  5255. /* the host is dead now, dissociate ACPI */
  5256. ata_acpi_dissociate(host);
  5257. }
  5258. #ifdef CONFIG_PCI
  5259. /**
  5260. * ata_pci_remove_one - PCI layer callback for device removal
  5261. * @pdev: PCI device that was removed
  5262. *
  5263. * PCI layer indicates to libata via this hook that hot-unplug or
  5264. * module unload event has occurred. Detach all ports. Resource
  5265. * release is handled via devres.
  5266. *
  5267. * LOCKING:
  5268. * Inherited from PCI layer (may sleep).
  5269. */
  5270. void ata_pci_remove_one(struct pci_dev *pdev)
  5271. {
  5272. struct device *dev = &pdev->dev;
  5273. struct ata_host *host = dev_get_drvdata(dev);
  5274. ata_host_detach(host);
  5275. }
  5276. /* move to PCI subsystem */
  5277. int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
  5278. {
  5279. unsigned long tmp = 0;
  5280. switch (bits->width) {
  5281. case 1: {
  5282. u8 tmp8 = 0;
  5283. pci_read_config_byte(pdev, bits->reg, &tmp8);
  5284. tmp = tmp8;
  5285. break;
  5286. }
  5287. case 2: {
  5288. u16 tmp16 = 0;
  5289. pci_read_config_word(pdev, bits->reg, &tmp16);
  5290. tmp = tmp16;
  5291. break;
  5292. }
  5293. case 4: {
  5294. u32 tmp32 = 0;
  5295. pci_read_config_dword(pdev, bits->reg, &tmp32);
  5296. tmp = tmp32;
  5297. break;
  5298. }
  5299. default:
  5300. return -EINVAL;
  5301. }
  5302. tmp &= bits->mask;
  5303. return (tmp == bits->val) ? 1 : 0;
  5304. }
  5305. #ifdef CONFIG_PM
  5306. void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
  5307. {
  5308. pci_save_state(pdev);
  5309. pci_disable_device(pdev);
  5310. if (mesg.event & PM_EVENT_SLEEP)
  5311. pci_set_power_state(pdev, PCI_D3hot);
  5312. }
  5313. int ata_pci_device_do_resume(struct pci_dev *pdev)
  5314. {
  5315. int rc;
  5316. pci_set_power_state(pdev, PCI_D0);
  5317. pci_restore_state(pdev);
  5318. rc = pcim_enable_device(pdev);
  5319. if (rc) {
  5320. dev_printk(KERN_ERR, &pdev->dev,
  5321. "failed to enable device after resume (%d)\n", rc);
  5322. return rc;
  5323. }
  5324. pci_set_master(pdev);
  5325. return 0;
  5326. }
  5327. int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
  5328. {
  5329. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  5330. int rc = 0;
  5331. rc = ata_host_suspend(host, mesg);
  5332. if (rc)
  5333. return rc;
  5334. ata_pci_device_do_suspend(pdev, mesg);
  5335. return 0;
  5336. }
  5337. int ata_pci_device_resume(struct pci_dev *pdev)
  5338. {
  5339. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  5340. int rc;
  5341. rc = ata_pci_device_do_resume(pdev);
  5342. if (rc == 0)
  5343. ata_host_resume(host);
  5344. return rc;
  5345. }
  5346. #endif /* CONFIG_PM */
  5347. #endif /* CONFIG_PCI */
  5348. static int __init ata_parse_force_one(char **cur,
  5349. struct ata_force_ent *force_ent,
  5350. const char **reason)
  5351. {
  5352. /* FIXME: Currently, there's no way to tag init const data and
  5353. * using __initdata causes build failure on some versions of
  5354. * gcc. Once __initdataconst is implemented, add const to the
  5355. * following structure.
  5356. */
  5357. static struct ata_force_param force_tbl[] __initdata = {
  5358. { "40c", .cbl = ATA_CBL_PATA40 },
  5359. { "80c", .cbl = ATA_CBL_PATA80 },
  5360. { "short40c", .cbl = ATA_CBL_PATA40_SHORT },
  5361. { "unk", .cbl = ATA_CBL_PATA_UNK },
  5362. { "ign", .cbl = ATA_CBL_PATA_IGN },
  5363. { "sata", .cbl = ATA_CBL_SATA },
  5364. { "1.5Gbps", .spd_limit = 1 },
  5365. { "3.0Gbps", .spd_limit = 2 },
  5366. { "noncq", .horkage_on = ATA_HORKAGE_NONCQ },
  5367. { "ncq", .horkage_off = ATA_HORKAGE_NONCQ },
  5368. { "pio0", .xfer_mask = 1 << (ATA_SHIFT_PIO + 0) },
  5369. { "pio1", .xfer_mask = 1 << (ATA_SHIFT_PIO + 1) },
  5370. { "pio2", .xfer_mask = 1 << (ATA_SHIFT_PIO + 2) },
  5371. { "pio3", .xfer_mask = 1 << (ATA_SHIFT_PIO + 3) },
  5372. { "pio4", .xfer_mask = 1 << (ATA_SHIFT_PIO + 4) },
  5373. { "pio5", .xfer_mask = 1 << (ATA_SHIFT_PIO + 5) },
  5374. { "pio6", .xfer_mask = 1 << (ATA_SHIFT_PIO + 6) },
  5375. { "mwdma0", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 0) },
  5376. { "mwdma1", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 1) },
  5377. { "mwdma2", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 2) },
  5378. { "mwdma3", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 3) },
  5379. { "mwdma4", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 4) },
  5380. { "udma0", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
  5381. { "udma16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
  5382. { "udma/16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
  5383. { "udma1", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
  5384. { "udma25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
  5385. { "udma/25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
  5386. { "udma2", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
  5387. { "udma33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
  5388. { "udma/33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
  5389. { "udma3", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
  5390. { "udma44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
  5391. { "udma/44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
  5392. { "udma4", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
  5393. { "udma66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
  5394. { "udma/66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
  5395. { "udma5", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
  5396. { "udma100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
  5397. { "udma/100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
  5398. { "udma6", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
  5399. { "udma133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
  5400. { "udma/133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
  5401. { "udma7", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 7) },
  5402. { "nohrst", .lflags = ATA_LFLAG_NO_HRST },
  5403. { "nosrst", .lflags = ATA_LFLAG_NO_SRST },
  5404. { "norst", .lflags = ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
  5405. };
  5406. char *start = *cur, *p = *cur;
  5407. char *id, *val, *endp;
  5408. const struct ata_force_param *match_fp = NULL;
  5409. int nr_matches = 0, i;
  5410. /* find where this param ends and update *cur */
  5411. while (*p != '\0' && *p != ',')
  5412. p++;
  5413. if (*p == '\0')
  5414. *cur = p;
  5415. else
  5416. *cur = p + 1;
  5417. *p = '\0';
  5418. /* parse */
  5419. p = strchr(start, ':');
  5420. if (!p) {
  5421. val = strstrip(start);
  5422. goto parse_val;
  5423. }
  5424. *p = '\0';
  5425. id = strstrip(start);
  5426. val = strstrip(p + 1);
  5427. /* parse id */
  5428. p = strchr(id, '.');
  5429. if (p) {
  5430. *p++ = '\0';
  5431. force_ent->device = simple_strtoul(p, &endp, 10);
  5432. if (p == endp || *endp != '\0') {
  5433. *reason = "invalid device";
  5434. return -EINVAL;
  5435. }
  5436. }
  5437. force_ent->port = simple_strtoul(id, &endp, 10);
  5438. if (p == endp || *endp != '\0') {
  5439. *reason = "invalid port/link";
  5440. return -EINVAL;
  5441. }
  5442. parse_val:
  5443. /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
  5444. for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
  5445. const struct ata_force_param *fp = &force_tbl[i];
  5446. if (strncasecmp(val, fp->name, strlen(val)))
  5447. continue;
  5448. nr_matches++;
  5449. match_fp = fp;
  5450. if (strcasecmp(val, fp->name) == 0) {
  5451. nr_matches = 1;
  5452. break;
  5453. }
  5454. }
  5455. if (!nr_matches) {
  5456. *reason = "unknown value";
  5457. return -EINVAL;
  5458. }
  5459. if (nr_matches > 1) {
  5460. *reason = "ambigious value";
  5461. return -EINVAL;
  5462. }
  5463. force_ent->param = *match_fp;
  5464. return 0;
  5465. }
  5466. static void __init ata_parse_force_param(void)
  5467. {
  5468. int idx = 0, size = 1;
  5469. int last_port = -1, last_device = -1;
  5470. char *p, *cur, *next;
  5471. /* calculate maximum number of params and allocate force_tbl */
  5472. for (p = ata_force_param_buf; *p; p++)
  5473. if (*p == ',')
  5474. size++;
  5475. ata_force_tbl = kzalloc(sizeof(ata_force_tbl[0]) * size, GFP_KERNEL);
  5476. if (!ata_force_tbl) {
  5477. printk(KERN_WARNING "ata: failed to extend force table, "
  5478. "libata.force ignored\n");
  5479. return;
  5480. }
  5481. /* parse and populate the table */
  5482. for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
  5483. const char *reason = "";
  5484. struct ata_force_ent te = { .port = -1, .device = -1 };
  5485. next = cur;
  5486. if (ata_parse_force_one(&next, &te, &reason)) {
  5487. printk(KERN_WARNING "ata: failed to parse force "
  5488. "parameter \"%s\" (%s)\n",
  5489. cur, reason);
  5490. continue;
  5491. }
  5492. if (te.port == -1) {
  5493. te.port = last_port;
  5494. te.device = last_device;
  5495. }
  5496. ata_force_tbl[idx++] = te;
  5497. last_port = te.port;
  5498. last_device = te.device;
  5499. }
  5500. ata_force_tbl_size = idx;
  5501. }
  5502. static int __init ata_init(void)
  5503. {
  5504. ata_parse_force_param();
  5505. ata_wq = create_workqueue("ata");
  5506. if (!ata_wq)
  5507. goto free_force_tbl;
  5508. ata_aux_wq = create_singlethread_workqueue("ata_aux");
  5509. if (!ata_aux_wq)
  5510. goto free_wq;
  5511. printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
  5512. return 0;
  5513. free_wq:
  5514. destroy_workqueue(ata_wq);
  5515. free_force_tbl:
  5516. kfree(ata_force_tbl);
  5517. return -ENOMEM;
  5518. }
  5519. static void __exit ata_exit(void)
  5520. {
  5521. kfree(ata_force_tbl);
  5522. destroy_workqueue(ata_wq);
  5523. destroy_workqueue(ata_aux_wq);
  5524. }
  5525. subsys_initcall(ata_init);
  5526. module_exit(ata_exit);
  5527. static unsigned long ratelimit_time;
  5528. static DEFINE_SPINLOCK(ata_ratelimit_lock);
  5529. int ata_ratelimit(void)
  5530. {
  5531. int rc;
  5532. unsigned long flags;
  5533. spin_lock_irqsave(&ata_ratelimit_lock, flags);
  5534. if (time_after(jiffies, ratelimit_time)) {
  5535. rc = 1;
  5536. ratelimit_time = jiffies + (HZ/5);
  5537. } else
  5538. rc = 0;
  5539. spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
  5540. return rc;
  5541. }
  5542. /**
  5543. * ata_wait_register - wait until register value changes
  5544. * @reg: IO-mapped register
  5545. * @mask: Mask to apply to read register value
  5546. * @val: Wait condition
  5547. * @interval: polling interval in milliseconds
  5548. * @timeout: timeout in milliseconds
  5549. *
  5550. * Waiting for some bits of register to change is a common
  5551. * operation for ATA controllers. This function reads 32bit LE
  5552. * IO-mapped register @reg and tests for the following condition.
  5553. *
  5554. * (*@reg & mask) != val
  5555. *
  5556. * If the condition is met, it returns; otherwise, the process is
  5557. * repeated after @interval_msec until timeout.
  5558. *
  5559. * LOCKING:
  5560. * Kernel thread context (may sleep)
  5561. *
  5562. * RETURNS:
  5563. * The final register value.
  5564. */
  5565. u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
  5566. unsigned long interval, unsigned long timeout)
  5567. {
  5568. unsigned long deadline;
  5569. u32 tmp;
  5570. tmp = ioread32(reg);
  5571. /* Calculate timeout _after_ the first read to make sure
  5572. * preceding writes reach the controller before starting to
  5573. * eat away the timeout.
  5574. */
  5575. deadline = ata_deadline(jiffies, timeout);
  5576. while ((tmp & mask) == val && time_before(jiffies, deadline)) {
  5577. msleep(interval);
  5578. tmp = ioread32(reg);
  5579. }
  5580. return tmp;
  5581. }
  5582. /*
  5583. * Dummy port_ops
  5584. */
  5585. static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
  5586. {
  5587. return AC_ERR_SYSTEM;
  5588. }
  5589. static void ata_dummy_error_handler(struct ata_port *ap)
  5590. {
  5591. /* truly dummy */
  5592. }
  5593. struct ata_port_operations ata_dummy_port_ops = {
  5594. .qc_prep = ata_noop_qc_prep,
  5595. .qc_issue = ata_dummy_qc_issue,
  5596. .error_handler = ata_dummy_error_handler,
  5597. };
  5598. const struct ata_port_info ata_dummy_port_info = {
  5599. .port_ops = &ata_dummy_port_ops,
  5600. };
  5601. /*
  5602. * libata is essentially a library of internal helper functions for
  5603. * low-level ATA host controller drivers. As such, the API/ABI is
  5604. * likely to change as new drivers are added and updated.
  5605. * Do not depend on ABI/API stability.
  5606. */
  5607. EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
  5608. EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
  5609. EXPORT_SYMBOL_GPL(sata_deb_timing_long);
  5610. EXPORT_SYMBOL_GPL(ata_base_port_ops);
  5611. EXPORT_SYMBOL_GPL(sata_port_ops);
  5612. EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
  5613. EXPORT_SYMBOL_GPL(ata_dummy_port_info);
  5614. EXPORT_SYMBOL_GPL(__ata_port_next_link);
  5615. EXPORT_SYMBOL_GPL(ata_std_bios_param);
  5616. EXPORT_SYMBOL_GPL(ata_host_init);
  5617. EXPORT_SYMBOL_GPL(ata_host_alloc);
  5618. EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
  5619. EXPORT_SYMBOL_GPL(ata_slave_link_init);
  5620. EXPORT_SYMBOL_GPL(ata_host_start);
  5621. EXPORT_SYMBOL_GPL(ata_host_register);
  5622. EXPORT_SYMBOL_GPL(ata_host_activate);
  5623. EXPORT_SYMBOL_GPL(ata_host_detach);
  5624. EXPORT_SYMBOL_GPL(ata_sg_init);
  5625. EXPORT_SYMBOL_GPL(ata_qc_complete);
  5626. EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
  5627. EXPORT_SYMBOL_GPL(atapi_cmd_type);
  5628. EXPORT_SYMBOL_GPL(ata_tf_to_fis);
  5629. EXPORT_SYMBOL_GPL(ata_tf_from_fis);
  5630. EXPORT_SYMBOL_GPL(ata_pack_xfermask);
  5631. EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
  5632. EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
  5633. EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
  5634. EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
  5635. EXPORT_SYMBOL_GPL(ata_mode_string);
  5636. EXPORT_SYMBOL_GPL(ata_id_xfermask);
  5637. EXPORT_SYMBOL_GPL(ata_port_start);
  5638. EXPORT_SYMBOL_GPL(ata_do_set_mode);
  5639. EXPORT_SYMBOL_GPL(ata_std_qc_defer);
  5640. EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
  5641. EXPORT_SYMBOL_GPL(ata_port_probe);
  5642. EXPORT_SYMBOL_GPL(ata_dev_disable);
  5643. EXPORT_SYMBOL_GPL(sata_set_spd);
  5644. EXPORT_SYMBOL_GPL(ata_wait_after_reset);
  5645. EXPORT_SYMBOL_GPL(sata_link_debounce);
  5646. EXPORT_SYMBOL_GPL(sata_link_resume);
  5647. EXPORT_SYMBOL_GPL(ata_std_prereset);
  5648. EXPORT_SYMBOL_GPL(sata_link_hardreset);
  5649. EXPORT_SYMBOL_GPL(sata_std_hardreset);
  5650. EXPORT_SYMBOL_GPL(ata_std_postreset);
  5651. EXPORT_SYMBOL_GPL(ata_dev_classify);
  5652. EXPORT_SYMBOL_GPL(ata_dev_pair);
  5653. EXPORT_SYMBOL_GPL(ata_port_disable);
  5654. EXPORT_SYMBOL_GPL(ata_ratelimit);
  5655. EXPORT_SYMBOL_GPL(ata_wait_register);
  5656. EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
  5657. EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
  5658. EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
  5659. EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
  5660. EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
  5661. EXPORT_SYMBOL_GPL(sata_scr_valid);
  5662. EXPORT_SYMBOL_GPL(sata_scr_read);
  5663. EXPORT_SYMBOL_GPL(sata_scr_write);
  5664. EXPORT_SYMBOL_GPL(sata_scr_write_flush);
  5665. EXPORT_SYMBOL_GPL(ata_link_online);
  5666. EXPORT_SYMBOL_GPL(ata_link_offline);
  5667. #ifdef CONFIG_PM
  5668. EXPORT_SYMBOL_GPL(ata_host_suspend);
  5669. EXPORT_SYMBOL_GPL(ata_host_resume);
  5670. #endif /* CONFIG_PM */
  5671. EXPORT_SYMBOL_GPL(ata_id_string);
  5672. EXPORT_SYMBOL_GPL(ata_id_c_string);
  5673. EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
  5674. EXPORT_SYMBOL_GPL(ata_scsi_simulate);
  5675. EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
  5676. EXPORT_SYMBOL_GPL(ata_timing_find_mode);
  5677. EXPORT_SYMBOL_GPL(ata_timing_compute);
  5678. EXPORT_SYMBOL_GPL(ata_timing_merge);
  5679. EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
  5680. #ifdef CONFIG_PCI
  5681. EXPORT_SYMBOL_GPL(pci_test_config_bits);
  5682. EXPORT_SYMBOL_GPL(ata_pci_remove_one);
  5683. #ifdef CONFIG_PM
  5684. EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
  5685. EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
  5686. EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
  5687. EXPORT_SYMBOL_GPL(ata_pci_device_resume);
  5688. #endif /* CONFIG_PM */
  5689. #endif /* CONFIG_PCI */
  5690. EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
  5691. EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
  5692. EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
  5693. EXPORT_SYMBOL_GPL(ata_port_desc);
  5694. #ifdef CONFIG_PCI
  5695. EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
  5696. #endif /* CONFIG_PCI */
  5697. EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
  5698. EXPORT_SYMBOL_GPL(ata_link_abort);
  5699. EXPORT_SYMBOL_GPL(ata_port_abort);
  5700. EXPORT_SYMBOL_GPL(ata_port_freeze);
  5701. EXPORT_SYMBOL_GPL(sata_async_notification);
  5702. EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
  5703. EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
  5704. EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
  5705. EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
  5706. EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
  5707. EXPORT_SYMBOL_GPL(ata_do_eh);
  5708. EXPORT_SYMBOL_GPL(ata_std_error_handler);
  5709. EXPORT_SYMBOL_GPL(ata_cable_40wire);
  5710. EXPORT_SYMBOL_GPL(ata_cable_80wire);
  5711. EXPORT_SYMBOL_GPL(ata_cable_unknown);
  5712. EXPORT_SYMBOL_GPL(ata_cable_ignore);
  5713. EXPORT_SYMBOL_GPL(ata_cable_sata);