libata-core.c 171 KB

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