libata-core.c 172 KB

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