libata-core.c 170 KB

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