libata-core.c 173 KB

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