libata-core.c 176 KB

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