libata-core.c 197 KB

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