libata-core.c 190 KB

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