libata-core.c 172 KB

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