libata-core.c 171 KB

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