libata-core.c 191 KB

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