libata-core.c 179 KB

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