libata-core.c 176 KB

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