libata-core.c 149 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181
  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. */
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/pci.h>
  37. #include <linux/init.h>
  38. #include <linux/list.h>
  39. #include <linux/mm.h>
  40. #include <linux/highmem.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/blkdev.h>
  43. #include <linux/delay.h>
  44. #include <linux/timer.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/completion.h>
  47. #include <linux/suspend.h>
  48. #include <linux/workqueue.h>
  49. #include <linux/jiffies.h>
  50. #include <linux/scatterlist.h>
  51. #include <scsi/scsi.h>
  52. #include <scsi/scsi_cmnd.h>
  53. #include <scsi/scsi_host.h>
  54. #include <linux/libata.h>
  55. #include <asm/io.h>
  56. #include <asm/semaphore.h>
  57. #include <asm/byteorder.h>
  58. #include "libata.h"
  59. /* debounce timing parameters in msecs { interval, duration, timeout } */
  60. const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
  61. const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
  62. const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
  63. static unsigned int ata_dev_init_params(struct ata_device *dev,
  64. u16 heads, u16 sectors);
  65. static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
  66. static void ata_dev_xfermask(struct ata_device *dev);
  67. static unsigned int ata_unique_id = 1;
  68. static struct workqueue_struct *ata_wq;
  69. struct workqueue_struct *ata_aux_wq;
  70. int atapi_enabled = 1;
  71. module_param(atapi_enabled, int, 0444);
  72. MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
  73. int atapi_dmadir = 0;
  74. module_param(atapi_dmadir, int, 0444);
  75. MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off, 1=on)");
  76. int libata_fua = 0;
  77. module_param_named(fua, libata_fua, int, 0444);
  78. MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
  79. static int ata_probe_timeout = ATA_TMOUT_INTERNAL / HZ;
  80. module_param(ata_probe_timeout, int, 0444);
  81. MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
  82. MODULE_AUTHOR("Jeff Garzik");
  83. MODULE_DESCRIPTION("Library module for ATA devices");
  84. MODULE_LICENSE("GPL");
  85. MODULE_VERSION(DRV_VERSION);
  86. /**
  87. * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
  88. * @tf: Taskfile to convert
  89. * @fis: Buffer into which data will output
  90. * @pmp: Port multiplier port
  91. *
  92. * Converts a standard ATA taskfile to a Serial ATA
  93. * FIS structure (Register - Host to Device).
  94. *
  95. * LOCKING:
  96. * Inherited from caller.
  97. */
  98. void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
  99. {
  100. fis[0] = 0x27; /* Register - Host to Device FIS */
  101. fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
  102. bit 7 indicates Command FIS */
  103. fis[2] = tf->command;
  104. fis[3] = tf->feature;
  105. fis[4] = tf->lbal;
  106. fis[5] = tf->lbam;
  107. fis[6] = tf->lbah;
  108. fis[7] = tf->device;
  109. fis[8] = tf->hob_lbal;
  110. fis[9] = tf->hob_lbam;
  111. fis[10] = tf->hob_lbah;
  112. fis[11] = tf->hob_feature;
  113. fis[12] = tf->nsect;
  114. fis[13] = tf->hob_nsect;
  115. fis[14] = 0;
  116. fis[15] = tf->ctl;
  117. fis[16] = 0;
  118. fis[17] = 0;
  119. fis[18] = 0;
  120. fis[19] = 0;
  121. }
  122. /**
  123. * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
  124. * @fis: Buffer from which data will be input
  125. * @tf: Taskfile to output
  126. *
  127. * Converts a serial ATA FIS structure to a standard ATA taskfile.
  128. *
  129. * LOCKING:
  130. * Inherited from caller.
  131. */
  132. void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
  133. {
  134. tf->command = fis[2]; /* status */
  135. tf->feature = fis[3]; /* error */
  136. tf->lbal = fis[4];
  137. tf->lbam = fis[5];
  138. tf->lbah = fis[6];
  139. tf->device = fis[7];
  140. tf->hob_lbal = fis[8];
  141. tf->hob_lbam = fis[9];
  142. tf->hob_lbah = fis[10];
  143. tf->nsect = fis[12];
  144. tf->hob_nsect = fis[13];
  145. }
  146. static const u8 ata_rw_cmds[] = {
  147. /* pio multi */
  148. ATA_CMD_READ_MULTI,
  149. ATA_CMD_WRITE_MULTI,
  150. ATA_CMD_READ_MULTI_EXT,
  151. ATA_CMD_WRITE_MULTI_EXT,
  152. 0,
  153. 0,
  154. 0,
  155. ATA_CMD_WRITE_MULTI_FUA_EXT,
  156. /* pio */
  157. ATA_CMD_PIO_READ,
  158. ATA_CMD_PIO_WRITE,
  159. ATA_CMD_PIO_READ_EXT,
  160. ATA_CMD_PIO_WRITE_EXT,
  161. 0,
  162. 0,
  163. 0,
  164. 0,
  165. /* dma */
  166. ATA_CMD_READ,
  167. ATA_CMD_WRITE,
  168. ATA_CMD_READ_EXT,
  169. ATA_CMD_WRITE_EXT,
  170. 0,
  171. 0,
  172. 0,
  173. ATA_CMD_WRITE_FUA_EXT
  174. };
  175. /**
  176. * ata_rwcmd_protocol - set taskfile r/w commands and protocol
  177. * @qc: command to examine and configure
  178. *
  179. * Examine the device configuration and tf->flags to calculate
  180. * the proper read/write commands and protocol to use.
  181. *
  182. * LOCKING:
  183. * caller.
  184. */
  185. int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
  186. {
  187. struct ata_taskfile *tf = &qc->tf;
  188. struct ata_device *dev = qc->dev;
  189. u8 cmd;
  190. int index, fua, lba48, write;
  191. fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
  192. lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
  193. write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
  194. if (dev->flags & ATA_DFLAG_PIO) {
  195. tf->protocol = ATA_PROT_PIO;
  196. index = dev->multi_count ? 0 : 8;
  197. } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
  198. /* Unable to use DMA due to host limitation */
  199. tf->protocol = ATA_PROT_PIO;
  200. index = dev->multi_count ? 0 : 8;
  201. } else {
  202. tf->protocol = ATA_PROT_DMA;
  203. index = 16;
  204. }
  205. cmd = ata_rw_cmds[index + fua + lba48 + write];
  206. if (cmd) {
  207. tf->command = cmd;
  208. return 0;
  209. }
  210. return -1;
  211. }
  212. /**
  213. * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
  214. * @pio_mask: pio_mask
  215. * @mwdma_mask: mwdma_mask
  216. * @udma_mask: udma_mask
  217. *
  218. * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
  219. * unsigned int xfer_mask.
  220. *
  221. * LOCKING:
  222. * None.
  223. *
  224. * RETURNS:
  225. * Packed xfer_mask.
  226. */
  227. static unsigned int ata_pack_xfermask(unsigned int pio_mask,
  228. unsigned int mwdma_mask,
  229. unsigned int udma_mask)
  230. {
  231. return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
  232. ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
  233. ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
  234. }
  235. /**
  236. * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
  237. * @xfer_mask: xfer_mask to unpack
  238. * @pio_mask: resulting pio_mask
  239. * @mwdma_mask: resulting mwdma_mask
  240. * @udma_mask: resulting udma_mask
  241. *
  242. * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
  243. * Any NULL distination masks will be ignored.
  244. */
  245. static void ata_unpack_xfermask(unsigned int xfer_mask,
  246. unsigned int *pio_mask,
  247. unsigned int *mwdma_mask,
  248. unsigned int *udma_mask)
  249. {
  250. if (pio_mask)
  251. *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
  252. if (mwdma_mask)
  253. *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
  254. if (udma_mask)
  255. *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
  256. }
  257. static const struct ata_xfer_ent {
  258. int shift, bits;
  259. u8 base;
  260. } ata_xfer_tbl[] = {
  261. { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
  262. { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
  263. { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
  264. { -1, },
  265. };
  266. /**
  267. * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
  268. * @xfer_mask: xfer_mask of interest
  269. *
  270. * Return matching XFER_* value for @xfer_mask. Only the highest
  271. * bit of @xfer_mask is considered.
  272. *
  273. * LOCKING:
  274. * None.
  275. *
  276. * RETURNS:
  277. * Matching XFER_* value, 0 if no match found.
  278. */
  279. static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
  280. {
  281. int highbit = fls(xfer_mask) - 1;
  282. const struct ata_xfer_ent *ent;
  283. for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
  284. if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
  285. return ent->base + highbit - ent->shift;
  286. return 0;
  287. }
  288. /**
  289. * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
  290. * @xfer_mode: XFER_* of interest
  291. *
  292. * Return matching xfer_mask for @xfer_mode.
  293. *
  294. * LOCKING:
  295. * None.
  296. *
  297. * RETURNS:
  298. * Matching xfer_mask, 0 if no match found.
  299. */
  300. static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
  301. {
  302. const struct ata_xfer_ent *ent;
  303. for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
  304. if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
  305. return 1 << (ent->shift + xfer_mode - ent->base);
  306. return 0;
  307. }
  308. /**
  309. * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
  310. * @xfer_mode: XFER_* of interest
  311. *
  312. * Return matching xfer_shift for @xfer_mode.
  313. *
  314. * LOCKING:
  315. * None.
  316. *
  317. * RETURNS:
  318. * Matching xfer_shift, -1 if no match found.
  319. */
  320. static int ata_xfer_mode2shift(unsigned int xfer_mode)
  321. {
  322. const struct ata_xfer_ent *ent;
  323. for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
  324. if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
  325. return ent->shift;
  326. return -1;
  327. }
  328. /**
  329. * ata_mode_string - convert xfer_mask to string
  330. * @xfer_mask: mask of bits supported; only highest bit counts.
  331. *
  332. * Determine string which represents the highest speed
  333. * (highest bit in @modemask).
  334. *
  335. * LOCKING:
  336. * None.
  337. *
  338. * RETURNS:
  339. * Constant C string representing highest speed listed in
  340. * @mode_mask, or the constant C string "<n/a>".
  341. */
  342. static const char *ata_mode_string(unsigned int xfer_mask)
  343. {
  344. static const char * const xfer_mode_str[] = {
  345. "PIO0",
  346. "PIO1",
  347. "PIO2",
  348. "PIO3",
  349. "PIO4",
  350. "PIO5",
  351. "PIO6",
  352. "MWDMA0",
  353. "MWDMA1",
  354. "MWDMA2",
  355. "MWDMA3",
  356. "MWDMA4",
  357. "UDMA/16",
  358. "UDMA/25",
  359. "UDMA/33",
  360. "UDMA/44",
  361. "UDMA/66",
  362. "UDMA/100",
  363. "UDMA/133",
  364. "UDMA7",
  365. };
  366. int highbit;
  367. highbit = fls(xfer_mask) - 1;
  368. if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
  369. return xfer_mode_str[highbit];
  370. return "<n/a>";
  371. }
  372. static const char *sata_spd_string(unsigned int spd)
  373. {
  374. static const char * const spd_str[] = {
  375. "1.5 Gbps",
  376. "3.0 Gbps",
  377. };
  378. if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
  379. return "<unknown>";
  380. return spd_str[spd - 1];
  381. }
  382. void ata_dev_disable(struct ata_device *dev)
  383. {
  384. if (ata_dev_enabled(dev) && ata_msg_drv(dev->ap)) {
  385. ata_dev_printk(dev, KERN_WARNING, "disabled\n");
  386. dev->class++;
  387. }
  388. }
  389. /**
  390. * ata_pio_devchk - PATA device presence detection
  391. * @ap: ATA channel to examine
  392. * @device: Device to examine (starting at zero)
  393. *
  394. * This technique was originally described in
  395. * Hale Landis's ATADRVR (www.ata-atapi.com), and
  396. * later found its way into the ATA/ATAPI spec.
  397. *
  398. * Write a pattern to the ATA shadow registers,
  399. * and if a device is present, it will respond by
  400. * correctly storing and echoing back the
  401. * ATA shadow register contents.
  402. *
  403. * LOCKING:
  404. * caller.
  405. */
  406. static unsigned int ata_pio_devchk(struct ata_port *ap,
  407. unsigned int device)
  408. {
  409. struct ata_ioports *ioaddr = &ap->ioaddr;
  410. u8 nsect, lbal;
  411. ap->ops->dev_select(ap, device);
  412. outb(0x55, ioaddr->nsect_addr);
  413. outb(0xaa, ioaddr->lbal_addr);
  414. outb(0xaa, ioaddr->nsect_addr);
  415. outb(0x55, ioaddr->lbal_addr);
  416. outb(0x55, ioaddr->nsect_addr);
  417. outb(0xaa, ioaddr->lbal_addr);
  418. nsect = inb(ioaddr->nsect_addr);
  419. lbal = inb(ioaddr->lbal_addr);
  420. if ((nsect == 0x55) && (lbal == 0xaa))
  421. return 1; /* we found a device */
  422. return 0; /* nothing found */
  423. }
  424. /**
  425. * ata_mmio_devchk - PATA device presence detection
  426. * @ap: ATA channel to examine
  427. * @device: Device to examine (starting at zero)
  428. *
  429. * This technique was originally described in
  430. * Hale Landis's ATADRVR (www.ata-atapi.com), and
  431. * later found its way into the ATA/ATAPI spec.
  432. *
  433. * Write a pattern to the ATA shadow registers,
  434. * and if a device is present, it will respond by
  435. * correctly storing and echoing back the
  436. * ATA shadow register contents.
  437. *
  438. * LOCKING:
  439. * caller.
  440. */
  441. static unsigned int ata_mmio_devchk(struct ata_port *ap,
  442. unsigned int device)
  443. {
  444. struct ata_ioports *ioaddr = &ap->ioaddr;
  445. u8 nsect, lbal;
  446. ap->ops->dev_select(ap, device);
  447. writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
  448. writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
  449. writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
  450. writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
  451. writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
  452. writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
  453. nsect = readb((void __iomem *) ioaddr->nsect_addr);
  454. lbal = readb((void __iomem *) ioaddr->lbal_addr);
  455. if ((nsect == 0x55) && (lbal == 0xaa))
  456. return 1; /* we found a device */
  457. return 0; /* nothing found */
  458. }
  459. /**
  460. * ata_devchk - PATA device presence detection
  461. * @ap: ATA channel to examine
  462. * @device: Device to examine (starting at zero)
  463. *
  464. * Dispatch ATA device presence detection, depending
  465. * on whether we are using PIO or MMIO to talk to the
  466. * ATA shadow registers.
  467. *
  468. * LOCKING:
  469. * caller.
  470. */
  471. static unsigned int ata_devchk(struct ata_port *ap,
  472. unsigned int device)
  473. {
  474. if (ap->flags & ATA_FLAG_MMIO)
  475. return ata_mmio_devchk(ap, device);
  476. return ata_pio_devchk(ap, device);
  477. }
  478. /**
  479. * ata_dev_classify - determine device type based on ATA-spec signature
  480. * @tf: ATA taskfile register set for device to be identified
  481. *
  482. * Determine from taskfile register contents whether a device is
  483. * ATA or ATAPI, as per "Signature and persistence" section
  484. * of ATA/PI spec (volume 1, sect 5.14).
  485. *
  486. * LOCKING:
  487. * None.
  488. *
  489. * RETURNS:
  490. * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
  491. * the event of failure.
  492. */
  493. unsigned int ata_dev_classify(const struct ata_taskfile *tf)
  494. {
  495. /* Apple's open source Darwin code hints that some devices only
  496. * put a proper signature into the LBA mid/high registers,
  497. * So, we only check those. It's sufficient for uniqueness.
  498. */
  499. if (((tf->lbam == 0) && (tf->lbah == 0)) ||
  500. ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
  501. DPRINTK("found ATA device by sig\n");
  502. return ATA_DEV_ATA;
  503. }
  504. if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
  505. ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
  506. DPRINTK("found ATAPI device by sig\n");
  507. return ATA_DEV_ATAPI;
  508. }
  509. DPRINTK("unknown device\n");
  510. return ATA_DEV_UNKNOWN;
  511. }
  512. /**
  513. * ata_dev_try_classify - Parse returned ATA device signature
  514. * @ap: ATA channel to examine
  515. * @device: Device to examine (starting at zero)
  516. * @r_err: Value of error register on completion
  517. *
  518. * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
  519. * an ATA/ATAPI-defined set of values is placed in the ATA
  520. * shadow registers, indicating the results of device detection
  521. * and diagnostics.
  522. *
  523. * Select the ATA device, and read the values from the ATA shadow
  524. * registers. Then parse according to the Error register value,
  525. * and the spec-defined values examined by ata_dev_classify().
  526. *
  527. * LOCKING:
  528. * caller.
  529. *
  530. * RETURNS:
  531. * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
  532. */
  533. static unsigned int
  534. ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
  535. {
  536. struct ata_taskfile tf;
  537. unsigned int class;
  538. u8 err;
  539. ap->ops->dev_select(ap, device);
  540. memset(&tf, 0, sizeof(tf));
  541. ap->ops->tf_read(ap, &tf);
  542. err = tf.feature;
  543. if (r_err)
  544. *r_err = err;
  545. /* see if device passed diags: if master then continue and warn later */
  546. if (err == 0 && device == 0)
  547. /* diagnostic fail : do nothing _YET_ */
  548. ap->device[device].horkage |= ATA_HORKAGE_DIAGNOSTIC;
  549. else if (err == 1)
  550. /* do nothing */ ;
  551. else if ((device == 0) && (err == 0x81))
  552. /* do nothing */ ;
  553. else
  554. return ATA_DEV_NONE;
  555. /* determine if device is ATA or ATAPI */
  556. class = ata_dev_classify(&tf);
  557. if (class == ATA_DEV_UNKNOWN)
  558. return ATA_DEV_NONE;
  559. if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
  560. return ATA_DEV_NONE;
  561. return class;
  562. }
  563. /**
  564. * ata_id_string - Convert IDENTIFY DEVICE page into string
  565. * @id: IDENTIFY DEVICE results we will examine
  566. * @s: string into which data is output
  567. * @ofs: offset into identify device page
  568. * @len: length of string to return. must be an even number.
  569. *
  570. * The strings in the IDENTIFY DEVICE page are broken up into
  571. * 16-bit chunks. Run through the string, and output each
  572. * 8-bit chunk linearly, regardless of platform.
  573. *
  574. * LOCKING:
  575. * caller.
  576. */
  577. void ata_id_string(const u16 *id, unsigned char *s,
  578. unsigned int ofs, unsigned int len)
  579. {
  580. unsigned int c;
  581. while (len > 0) {
  582. c = id[ofs] >> 8;
  583. *s = c;
  584. s++;
  585. c = id[ofs] & 0xff;
  586. *s = c;
  587. s++;
  588. ofs++;
  589. len -= 2;
  590. }
  591. }
  592. /**
  593. * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
  594. * @id: IDENTIFY DEVICE results we will examine
  595. * @s: string into which data is output
  596. * @ofs: offset into identify device page
  597. * @len: length of string to return. must be an odd number.
  598. *
  599. * This function is identical to ata_id_string except that it
  600. * trims trailing spaces and terminates the resulting string with
  601. * null. @len must be actual maximum length (even number) + 1.
  602. *
  603. * LOCKING:
  604. * caller.
  605. */
  606. void ata_id_c_string(const u16 *id, unsigned char *s,
  607. unsigned int ofs, unsigned int len)
  608. {
  609. unsigned char *p;
  610. WARN_ON(!(len & 1));
  611. ata_id_string(id, s, ofs, len - 1);
  612. p = s + strnlen(s, len - 1);
  613. while (p > s && p[-1] == ' ')
  614. p--;
  615. *p = '\0';
  616. }
  617. static u64 ata_id_n_sectors(const u16 *id)
  618. {
  619. if (ata_id_has_lba(id)) {
  620. if (ata_id_has_lba48(id))
  621. return ata_id_u64(id, 100);
  622. else
  623. return ata_id_u32(id, 60);
  624. } else {
  625. if (ata_id_current_chs_valid(id))
  626. return ata_id_u32(id, 57);
  627. else
  628. return id[1] * id[3] * id[6];
  629. }
  630. }
  631. /**
  632. * ata_noop_dev_select - Select device 0/1 on ATA bus
  633. * @ap: ATA channel to manipulate
  634. * @device: ATA device (numbered from zero) to select
  635. *
  636. * This function performs no actual function.
  637. *
  638. * May be used as the dev_select() entry in ata_port_operations.
  639. *
  640. * LOCKING:
  641. * caller.
  642. */
  643. void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
  644. {
  645. }
  646. /**
  647. * ata_std_dev_select - Select device 0/1 on ATA bus
  648. * @ap: ATA channel to manipulate
  649. * @device: ATA device (numbered from zero) to select
  650. *
  651. * Use the method defined in the ATA specification to
  652. * make either device 0, or device 1, active on the
  653. * ATA channel. Works with both PIO and MMIO.
  654. *
  655. * May be used as the dev_select() entry in ata_port_operations.
  656. *
  657. * LOCKING:
  658. * caller.
  659. */
  660. void ata_std_dev_select (struct ata_port *ap, unsigned int device)
  661. {
  662. u8 tmp;
  663. if (device == 0)
  664. tmp = ATA_DEVICE_OBS;
  665. else
  666. tmp = ATA_DEVICE_OBS | ATA_DEV1;
  667. if (ap->flags & ATA_FLAG_MMIO) {
  668. writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
  669. } else {
  670. outb(tmp, ap->ioaddr.device_addr);
  671. }
  672. ata_pause(ap); /* needed; also flushes, for mmio */
  673. }
  674. /**
  675. * ata_dev_select - Select device 0/1 on ATA bus
  676. * @ap: ATA channel to manipulate
  677. * @device: ATA device (numbered from zero) to select
  678. * @wait: non-zero to wait for Status register BSY bit to clear
  679. * @can_sleep: non-zero if context allows sleeping
  680. *
  681. * Use the method defined in the ATA specification to
  682. * make either device 0, or device 1, active on the
  683. * ATA channel.
  684. *
  685. * This is a high-level version of ata_std_dev_select(),
  686. * which additionally provides the services of inserting
  687. * the proper pauses and status polling, where needed.
  688. *
  689. * LOCKING:
  690. * caller.
  691. */
  692. void ata_dev_select(struct ata_port *ap, unsigned int device,
  693. unsigned int wait, unsigned int can_sleep)
  694. {
  695. if (ata_msg_probe(ap))
  696. ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, ata%u: "
  697. "device %u, wait %u\n", ap->id, device, wait);
  698. if (wait)
  699. ata_wait_idle(ap);
  700. ap->ops->dev_select(ap, device);
  701. if (wait) {
  702. if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
  703. msleep(150);
  704. ata_wait_idle(ap);
  705. }
  706. }
  707. /**
  708. * ata_dump_id - IDENTIFY DEVICE info debugging output
  709. * @id: IDENTIFY DEVICE page to dump
  710. *
  711. * Dump selected 16-bit words from the given IDENTIFY DEVICE
  712. * page.
  713. *
  714. * LOCKING:
  715. * caller.
  716. */
  717. static inline void ata_dump_id(const u16 *id)
  718. {
  719. DPRINTK("49==0x%04x "
  720. "53==0x%04x "
  721. "63==0x%04x "
  722. "64==0x%04x "
  723. "75==0x%04x \n",
  724. id[49],
  725. id[53],
  726. id[63],
  727. id[64],
  728. id[75]);
  729. DPRINTK("80==0x%04x "
  730. "81==0x%04x "
  731. "82==0x%04x "
  732. "83==0x%04x "
  733. "84==0x%04x \n",
  734. id[80],
  735. id[81],
  736. id[82],
  737. id[83],
  738. id[84]);
  739. DPRINTK("88==0x%04x "
  740. "93==0x%04x\n",
  741. id[88],
  742. id[93]);
  743. }
  744. /**
  745. * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
  746. * @id: IDENTIFY data to compute xfer mask from
  747. *
  748. * Compute the xfermask for this device. This is not as trivial
  749. * as it seems if we must consider early devices correctly.
  750. *
  751. * FIXME: pre IDE drive timing (do we care ?).
  752. *
  753. * LOCKING:
  754. * None.
  755. *
  756. * RETURNS:
  757. * Computed xfermask
  758. */
  759. static unsigned int ata_id_xfermask(const u16 *id)
  760. {
  761. unsigned int pio_mask, mwdma_mask, udma_mask;
  762. /* Usual case. Word 53 indicates word 64 is valid */
  763. if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
  764. pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
  765. pio_mask <<= 3;
  766. pio_mask |= 0x7;
  767. } else {
  768. /* If word 64 isn't valid then Word 51 high byte holds
  769. * the PIO timing number for the maximum. Turn it into
  770. * a mask.
  771. */
  772. u8 mode = id[ATA_ID_OLD_PIO_MODES] & 0xFF;
  773. if (mode < 5) /* Valid PIO range */
  774. pio_mask = (2 << mode) - 1;
  775. else
  776. pio_mask = 1;
  777. /* But wait.. there's more. Design your standards by
  778. * committee and you too can get a free iordy field to
  779. * process. However its the speeds not the modes that
  780. * are supported... Note drivers using the timing API
  781. * will get this right anyway
  782. */
  783. }
  784. mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
  785. if (ata_id_is_cfa(id)) {
  786. /*
  787. * Process compact flash extended modes
  788. */
  789. int pio = id[163] & 0x7;
  790. int dma = (id[163] >> 3) & 7;
  791. if (pio)
  792. pio_mask |= (1 << 5);
  793. if (pio > 1)
  794. pio_mask |= (1 << 6);
  795. if (dma)
  796. mwdma_mask |= (1 << 3);
  797. if (dma > 1)
  798. mwdma_mask |= (1 << 4);
  799. }
  800. udma_mask = 0;
  801. if (id[ATA_ID_FIELD_VALID] & (1 << 2))
  802. udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
  803. return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
  804. }
  805. /**
  806. * ata_port_queue_task - Queue port_task
  807. * @ap: The ata_port to queue port_task for
  808. * @fn: workqueue function to be scheduled
  809. * @data: data value to pass to workqueue function
  810. * @delay: delay time for workqueue function
  811. *
  812. * Schedule @fn(@data) for execution after @delay jiffies using
  813. * port_task. There is one port_task per port and it's the
  814. * user(low level driver)'s responsibility to make sure that only
  815. * one task is active at any given time.
  816. *
  817. * libata core layer takes care of synchronization between
  818. * port_task and EH. ata_port_queue_task() may be ignored for EH
  819. * synchronization.
  820. *
  821. * LOCKING:
  822. * Inherited from caller.
  823. */
  824. void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
  825. unsigned long delay)
  826. {
  827. int rc;
  828. if (ap->pflags & ATA_PFLAG_FLUSH_PORT_TASK)
  829. return;
  830. PREPARE_WORK(&ap->port_task, fn, data);
  831. if (!delay)
  832. rc = queue_work(ata_wq, &ap->port_task);
  833. else
  834. rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
  835. /* rc == 0 means that another user is using port task */
  836. WARN_ON(rc == 0);
  837. }
  838. /**
  839. * ata_port_flush_task - Flush port_task
  840. * @ap: The ata_port to flush port_task for
  841. *
  842. * After this function completes, port_task is guranteed not to
  843. * be running or scheduled.
  844. *
  845. * LOCKING:
  846. * Kernel thread context (may sleep)
  847. */
  848. void ata_port_flush_task(struct ata_port *ap)
  849. {
  850. unsigned long flags;
  851. DPRINTK("ENTER\n");
  852. spin_lock_irqsave(ap->lock, flags);
  853. ap->pflags |= ATA_PFLAG_FLUSH_PORT_TASK;
  854. spin_unlock_irqrestore(ap->lock, flags);
  855. DPRINTK("flush #1\n");
  856. flush_workqueue(ata_wq);
  857. /*
  858. * At this point, if a task is running, it's guaranteed to see
  859. * the FLUSH flag; thus, it will never queue pio tasks again.
  860. * Cancel and flush.
  861. */
  862. if (!cancel_delayed_work(&ap->port_task)) {
  863. if (ata_msg_ctl(ap))
  864. ata_port_printk(ap, KERN_DEBUG, "%s: flush #2\n",
  865. __FUNCTION__);
  866. flush_workqueue(ata_wq);
  867. }
  868. spin_lock_irqsave(ap->lock, flags);
  869. ap->pflags &= ~ATA_PFLAG_FLUSH_PORT_TASK;
  870. spin_unlock_irqrestore(ap->lock, flags);
  871. if (ata_msg_ctl(ap))
  872. ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __FUNCTION__);
  873. }
  874. void ata_qc_complete_internal(struct ata_queued_cmd *qc)
  875. {
  876. struct completion *waiting = qc->private_data;
  877. complete(waiting);
  878. }
  879. /**
  880. * ata_exec_internal - execute libata internal command
  881. * @dev: Device to which the command is sent
  882. * @tf: Taskfile registers for the command and the result
  883. * @cdb: CDB for packet command
  884. * @dma_dir: Data tranfer direction of the command
  885. * @buf: Data buffer of the command
  886. * @buflen: Length of data buffer
  887. *
  888. * Executes libata internal command with timeout. @tf contains
  889. * command on entry and result on return. Timeout and error
  890. * conditions are reported via return value. No recovery action
  891. * is taken after a command times out. It's caller's duty to
  892. * clean up after timeout.
  893. *
  894. * LOCKING:
  895. * None. Should be called with kernel context, might sleep.
  896. *
  897. * RETURNS:
  898. * Zero on success, AC_ERR_* mask on failure
  899. */
  900. unsigned ata_exec_internal(struct ata_device *dev,
  901. struct ata_taskfile *tf, const u8 *cdb,
  902. int dma_dir, void *buf, unsigned int buflen)
  903. {
  904. struct ata_port *ap = dev->ap;
  905. u8 command = tf->command;
  906. struct ata_queued_cmd *qc;
  907. unsigned int tag, preempted_tag;
  908. u32 preempted_sactive, preempted_qc_active;
  909. DECLARE_COMPLETION_ONSTACK(wait);
  910. unsigned long flags;
  911. unsigned int err_mask;
  912. int rc;
  913. spin_lock_irqsave(ap->lock, flags);
  914. /* no internal command while frozen */
  915. if (ap->pflags & ATA_PFLAG_FROZEN) {
  916. spin_unlock_irqrestore(ap->lock, flags);
  917. return AC_ERR_SYSTEM;
  918. }
  919. /* initialize internal qc */
  920. /* XXX: Tag 0 is used for drivers with legacy EH as some
  921. * drivers choke if any other tag is given. This breaks
  922. * ata_tag_internal() test for those drivers. Don't use new
  923. * EH stuff without converting to it.
  924. */
  925. if (ap->ops->error_handler)
  926. tag = ATA_TAG_INTERNAL;
  927. else
  928. tag = 0;
  929. if (test_and_set_bit(tag, &ap->qc_allocated))
  930. BUG();
  931. qc = __ata_qc_from_tag(ap, tag);
  932. qc->tag = tag;
  933. qc->scsicmd = NULL;
  934. qc->ap = ap;
  935. qc->dev = dev;
  936. ata_qc_reinit(qc);
  937. preempted_tag = ap->active_tag;
  938. preempted_sactive = ap->sactive;
  939. preempted_qc_active = ap->qc_active;
  940. ap->active_tag = ATA_TAG_POISON;
  941. ap->sactive = 0;
  942. ap->qc_active = 0;
  943. /* prepare & issue qc */
  944. qc->tf = *tf;
  945. if (cdb)
  946. memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
  947. qc->flags |= ATA_QCFLAG_RESULT_TF;
  948. qc->dma_dir = dma_dir;
  949. if (dma_dir != DMA_NONE) {
  950. ata_sg_init_one(qc, buf, buflen);
  951. qc->nsect = buflen / ATA_SECT_SIZE;
  952. }
  953. qc->private_data = &wait;
  954. qc->complete_fn = ata_qc_complete_internal;
  955. ata_qc_issue(qc);
  956. spin_unlock_irqrestore(ap->lock, flags);
  957. rc = wait_for_completion_timeout(&wait, ata_probe_timeout);
  958. ata_port_flush_task(ap);
  959. if (!rc) {
  960. spin_lock_irqsave(ap->lock, flags);
  961. /* We're racing with irq here. If we lose, the
  962. * following test prevents us from completing the qc
  963. * twice. If we win, the port is frozen and will be
  964. * cleaned up by ->post_internal_cmd().
  965. */
  966. if (qc->flags & ATA_QCFLAG_ACTIVE) {
  967. qc->err_mask |= AC_ERR_TIMEOUT;
  968. if (ap->ops->error_handler)
  969. ata_port_freeze(ap);
  970. else
  971. ata_qc_complete(qc);
  972. if (ata_msg_warn(ap))
  973. ata_dev_printk(dev, KERN_WARNING,
  974. "qc timeout (cmd 0x%x)\n", command);
  975. }
  976. spin_unlock_irqrestore(ap->lock, flags);
  977. }
  978. /* do post_internal_cmd */
  979. if (ap->ops->post_internal_cmd)
  980. ap->ops->post_internal_cmd(qc);
  981. if (qc->flags & ATA_QCFLAG_FAILED && !qc->err_mask) {
  982. if (ata_msg_warn(ap))
  983. ata_dev_printk(dev, KERN_WARNING,
  984. "zero err_mask for failed "
  985. "internal command, assuming AC_ERR_OTHER\n");
  986. qc->err_mask |= AC_ERR_OTHER;
  987. }
  988. /* finish up */
  989. spin_lock_irqsave(ap->lock, flags);
  990. *tf = qc->result_tf;
  991. err_mask = qc->err_mask;
  992. ata_qc_free(qc);
  993. ap->active_tag = preempted_tag;
  994. ap->sactive = preempted_sactive;
  995. ap->qc_active = preempted_qc_active;
  996. /* XXX - Some LLDDs (sata_mv) disable port on command failure.
  997. * Until those drivers are fixed, we detect the condition
  998. * here, fail the command with AC_ERR_SYSTEM and reenable the
  999. * port.
  1000. *
  1001. * Note that this doesn't change any behavior as internal
  1002. * command failure results in disabling the device in the
  1003. * higher layer for LLDDs without new reset/EH callbacks.
  1004. *
  1005. * Kill the following code as soon as those drivers are fixed.
  1006. */
  1007. if (ap->flags & ATA_FLAG_DISABLED) {
  1008. err_mask |= AC_ERR_SYSTEM;
  1009. ata_port_probe(ap);
  1010. }
  1011. spin_unlock_irqrestore(ap->lock, flags);
  1012. return err_mask;
  1013. }
  1014. /**
  1015. * ata_do_simple_cmd - execute simple internal command
  1016. * @dev: Device to which the command is sent
  1017. * @cmd: Opcode to execute
  1018. *
  1019. * Execute a 'simple' command, that only consists of the opcode
  1020. * 'cmd' itself, without filling any other registers
  1021. *
  1022. * LOCKING:
  1023. * Kernel thread context (may sleep).
  1024. *
  1025. * RETURNS:
  1026. * Zero on success, AC_ERR_* mask on failure
  1027. */
  1028. unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
  1029. {
  1030. struct ata_taskfile tf;
  1031. ata_tf_init(dev, &tf);
  1032. tf.command = cmd;
  1033. tf.flags |= ATA_TFLAG_DEVICE;
  1034. tf.protocol = ATA_PROT_NODATA;
  1035. return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
  1036. }
  1037. /**
  1038. * ata_pio_need_iordy - check if iordy needed
  1039. * @adev: ATA device
  1040. *
  1041. * Check if the current speed of the device requires IORDY. Used
  1042. * by various controllers for chip configuration.
  1043. */
  1044. unsigned int ata_pio_need_iordy(const struct ata_device *adev)
  1045. {
  1046. int pio;
  1047. int speed = adev->pio_mode - XFER_PIO_0;
  1048. if (speed < 2)
  1049. return 0;
  1050. if (speed > 2)
  1051. return 1;
  1052. /* If we have no drive specific rule, then PIO 2 is non IORDY */
  1053. if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
  1054. pio = adev->id[ATA_ID_EIDE_PIO];
  1055. /* Is the speed faster than the drive allows non IORDY ? */
  1056. if (pio) {
  1057. /* This is cycle times not frequency - watch the logic! */
  1058. if (pio > 240) /* PIO2 is 240nS per cycle */
  1059. return 1;
  1060. return 0;
  1061. }
  1062. }
  1063. return 0;
  1064. }
  1065. /**
  1066. * ata_dev_read_id - Read ID data from the specified device
  1067. * @dev: target device
  1068. * @p_class: pointer to class of the target device (may be changed)
  1069. * @post_reset: is this read ID post-reset?
  1070. * @id: buffer to read IDENTIFY data into
  1071. *
  1072. * Read ID data from the specified device. ATA_CMD_ID_ATA is
  1073. * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
  1074. * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
  1075. * for pre-ATA4 drives.
  1076. *
  1077. * LOCKING:
  1078. * Kernel thread context (may sleep)
  1079. *
  1080. * RETURNS:
  1081. * 0 on success, -errno otherwise.
  1082. */
  1083. int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
  1084. int post_reset, u16 *id)
  1085. {
  1086. struct ata_port *ap = dev->ap;
  1087. unsigned int class = *p_class;
  1088. struct ata_taskfile tf;
  1089. unsigned int err_mask = 0;
  1090. const char *reason;
  1091. int rc;
  1092. if (ata_msg_ctl(ap))
  1093. ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER, host %u, dev %u\n",
  1094. __FUNCTION__, ap->id, dev->devno);
  1095. ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
  1096. retry:
  1097. ata_tf_init(dev, &tf);
  1098. switch (class) {
  1099. case ATA_DEV_ATA:
  1100. tf.command = ATA_CMD_ID_ATA;
  1101. break;
  1102. case ATA_DEV_ATAPI:
  1103. tf.command = ATA_CMD_ID_ATAPI;
  1104. break;
  1105. default:
  1106. rc = -ENODEV;
  1107. reason = "unsupported class";
  1108. goto err_out;
  1109. }
  1110. tf.protocol = ATA_PROT_PIO;
  1111. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
  1112. id, sizeof(id[0]) * ATA_ID_WORDS);
  1113. if (err_mask) {
  1114. rc = -EIO;
  1115. reason = "I/O error";
  1116. goto err_out;
  1117. }
  1118. swap_buf_le16(id, ATA_ID_WORDS);
  1119. /* sanity check */
  1120. rc = -EINVAL;
  1121. reason = "device reports illegal type";
  1122. if (class == ATA_DEV_ATA) {
  1123. if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
  1124. goto err_out;
  1125. } else {
  1126. if (ata_id_is_ata(id))
  1127. goto err_out;
  1128. }
  1129. if (post_reset && class == ATA_DEV_ATA) {
  1130. /*
  1131. * The exact sequence expected by certain pre-ATA4 drives is:
  1132. * SRST RESET
  1133. * IDENTIFY
  1134. * INITIALIZE DEVICE PARAMETERS
  1135. * anything else..
  1136. * Some drives were very specific about that exact sequence.
  1137. */
  1138. if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
  1139. err_mask = ata_dev_init_params(dev, id[3], id[6]);
  1140. if (err_mask) {
  1141. rc = -EIO;
  1142. reason = "INIT_DEV_PARAMS failed";
  1143. goto err_out;
  1144. }
  1145. /* current CHS translation info (id[53-58]) might be
  1146. * changed. reread the identify device info.
  1147. */
  1148. post_reset = 0;
  1149. goto retry;
  1150. }
  1151. }
  1152. *p_class = class;
  1153. return 0;
  1154. err_out:
  1155. if (ata_msg_warn(ap))
  1156. ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
  1157. "(%s, err_mask=0x%x)\n", reason, err_mask);
  1158. return rc;
  1159. }
  1160. static inline u8 ata_dev_knobble(struct ata_device *dev)
  1161. {
  1162. return ((dev->ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
  1163. }
  1164. static void ata_dev_config_ncq(struct ata_device *dev,
  1165. char *desc, size_t desc_sz)
  1166. {
  1167. struct ata_port *ap = dev->ap;
  1168. int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
  1169. if (!ata_id_has_ncq(dev->id)) {
  1170. desc[0] = '\0';
  1171. return;
  1172. }
  1173. if (ap->flags & ATA_FLAG_NCQ) {
  1174. hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
  1175. dev->flags |= ATA_DFLAG_NCQ;
  1176. }
  1177. if (hdepth >= ddepth)
  1178. snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
  1179. else
  1180. snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
  1181. }
  1182. static void ata_set_port_max_cmd_len(struct ata_port *ap)
  1183. {
  1184. int i;
  1185. if (ap->scsi_host) {
  1186. unsigned int len = 0;
  1187. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1188. len = max(len, ap->device[i].cdb_len);
  1189. ap->scsi_host->max_cmd_len = len;
  1190. }
  1191. }
  1192. /**
  1193. * ata_dev_configure - Configure the specified ATA/ATAPI device
  1194. * @dev: Target device to configure
  1195. * @print_info: Enable device info printout
  1196. *
  1197. * Configure @dev according to @dev->id. Generic and low-level
  1198. * driver specific fixups are also applied.
  1199. *
  1200. * LOCKING:
  1201. * Kernel thread context (may sleep)
  1202. *
  1203. * RETURNS:
  1204. * 0 on success, -errno otherwise
  1205. */
  1206. int ata_dev_configure(struct ata_device *dev, int print_info)
  1207. {
  1208. struct ata_port *ap = dev->ap;
  1209. const u16 *id = dev->id;
  1210. unsigned int xfer_mask;
  1211. char revbuf[7]; /* XYZ-99\0 */
  1212. int rc;
  1213. if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
  1214. ata_dev_printk(dev, KERN_INFO,
  1215. "%s: ENTER/EXIT (host %u, dev %u) -- nodev\n",
  1216. __FUNCTION__, ap->id, dev->devno);
  1217. return 0;
  1218. }
  1219. if (ata_msg_probe(ap))
  1220. ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER, host %u, dev %u\n",
  1221. __FUNCTION__, ap->id, dev->devno);
  1222. /* print device capabilities */
  1223. if (ata_msg_probe(ap))
  1224. ata_dev_printk(dev, KERN_DEBUG,
  1225. "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
  1226. "85:%04x 86:%04x 87:%04x 88:%04x\n",
  1227. __FUNCTION__,
  1228. id[49], id[82], id[83], id[84],
  1229. id[85], id[86], id[87], id[88]);
  1230. /* initialize to-be-configured parameters */
  1231. dev->flags &= ~ATA_DFLAG_CFG_MASK;
  1232. dev->max_sectors = 0;
  1233. dev->cdb_len = 0;
  1234. dev->n_sectors = 0;
  1235. dev->cylinders = 0;
  1236. dev->heads = 0;
  1237. dev->sectors = 0;
  1238. /*
  1239. * common ATA, ATAPI feature tests
  1240. */
  1241. /* find max transfer mode; for printk only */
  1242. xfer_mask = ata_id_xfermask(id);
  1243. if (ata_msg_probe(ap))
  1244. ata_dump_id(id);
  1245. /* ATA-specific feature tests */
  1246. if (dev->class == ATA_DEV_ATA) {
  1247. if (ata_id_is_cfa(id)) {
  1248. if (id[162] & 1) /* CPRM may make this media unusable */
  1249. ata_dev_printk(dev, KERN_WARNING, "ata%u: device %u supports DRM functions and may not be fully accessable.\n",
  1250. ap->id, dev->devno);
  1251. snprintf(revbuf, 7, "CFA");
  1252. }
  1253. else
  1254. snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
  1255. dev->n_sectors = ata_id_n_sectors(id);
  1256. if (ata_id_has_lba(id)) {
  1257. const char *lba_desc;
  1258. char ncq_desc[20];
  1259. lba_desc = "LBA";
  1260. dev->flags |= ATA_DFLAG_LBA;
  1261. if (ata_id_has_lba48(id)) {
  1262. dev->flags |= ATA_DFLAG_LBA48;
  1263. lba_desc = "LBA48";
  1264. }
  1265. /* config NCQ */
  1266. ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
  1267. /* print device info to dmesg */
  1268. if (ata_msg_drv(ap) && print_info)
  1269. ata_dev_printk(dev, KERN_INFO, "%s, "
  1270. "max %s, %Lu sectors: %s %s\n",
  1271. revbuf,
  1272. ata_mode_string(xfer_mask),
  1273. (unsigned long long)dev->n_sectors,
  1274. lba_desc, ncq_desc);
  1275. } else {
  1276. /* CHS */
  1277. /* Default translation */
  1278. dev->cylinders = id[1];
  1279. dev->heads = id[3];
  1280. dev->sectors = id[6];
  1281. if (ata_id_current_chs_valid(id)) {
  1282. /* Current CHS translation is valid. */
  1283. dev->cylinders = id[54];
  1284. dev->heads = id[55];
  1285. dev->sectors = id[56];
  1286. }
  1287. /* print device info to dmesg */
  1288. if (ata_msg_drv(ap) && print_info)
  1289. ata_dev_printk(dev, KERN_INFO, "%s, "
  1290. "max %s, %Lu sectors: CHS %u/%u/%u\n",
  1291. revbuf,
  1292. ata_mode_string(xfer_mask),
  1293. (unsigned long long)dev->n_sectors,
  1294. dev->cylinders, dev->heads,
  1295. dev->sectors);
  1296. }
  1297. if (dev->id[59] & 0x100) {
  1298. dev->multi_count = dev->id[59] & 0xff;
  1299. if (ata_msg_drv(ap) && print_info)
  1300. ata_dev_printk(dev, KERN_INFO,
  1301. "ata%u: dev %u multi count %u\n",
  1302. ap->id, dev->devno, dev->multi_count);
  1303. }
  1304. dev->cdb_len = 16;
  1305. }
  1306. /* ATAPI-specific feature tests */
  1307. else if (dev->class == ATA_DEV_ATAPI) {
  1308. char *cdb_intr_string = "";
  1309. rc = atapi_cdb_len(id);
  1310. if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
  1311. if (ata_msg_warn(ap))
  1312. ata_dev_printk(dev, KERN_WARNING,
  1313. "unsupported CDB len\n");
  1314. rc = -EINVAL;
  1315. goto err_out_nosup;
  1316. }
  1317. dev->cdb_len = (unsigned int) rc;
  1318. if (ata_id_cdb_intr(dev->id)) {
  1319. dev->flags |= ATA_DFLAG_CDB_INTR;
  1320. cdb_intr_string = ", CDB intr";
  1321. }
  1322. /* print device info to dmesg */
  1323. if (ata_msg_drv(ap) && print_info)
  1324. ata_dev_printk(dev, KERN_INFO, "ATAPI, max %s%s\n",
  1325. ata_mode_string(xfer_mask),
  1326. cdb_intr_string);
  1327. }
  1328. if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
  1329. /* Let the user know. We don't want to disallow opens for
  1330. rescue purposes, or in case the vendor is just a blithering
  1331. idiot */
  1332. if (print_info) {
  1333. ata_dev_printk(dev, KERN_WARNING,
  1334. "Drive reports diagnostics failure. This may indicate a drive\n");
  1335. ata_dev_printk(dev, KERN_WARNING,
  1336. "fault or invalid emulation. Contact drive vendor for information.\n");
  1337. }
  1338. }
  1339. ata_set_port_max_cmd_len(ap);
  1340. /* limit bridge transfers to udma5, 200 sectors */
  1341. if (ata_dev_knobble(dev)) {
  1342. if (ata_msg_drv(ap) && print_info)
  1343. ata_dev_printk(dev, KERN_INFO,
  1344. "applying bridge limits\n");
  1345. dev->udma_mask &= ATA_UDMA5;
  1346. dev->max_sectors = ATA_MAX_SECTORS;
  1347. }
  1348. if (ap->ops->dev_config)
  1349. ap->ops->dev_config(ap, dev);
  1350. if (ata_msg_probe(ap))
  1351. ata_dev_printk(dev, KERN_DEBUG, "%s: EXIT, drv_stat = 0x%x\n",
  1352. __FUNCTION__, ata_chk_status(ap));
  1353. return 0;
  1354. err_out_nosup:
  1355. if (ata_msg_probe(ap))
  1356. ata_dev_printk(dev, KERN_DEBUG,
  1357. "%s: EXIT, err\n", __FUNCTION__);
  1358. return rc;
  1359. }
  1360. /**
  1361. * ata_bus_probe - Reset and probe ATA bus
  1362. * @ap: Bus to probe
  1363. *
  1364. * Master ATA bus probing function. Initiates a hardware-dependent
  1365. * bus reset, then attempts to identify any devices found on
  1366. * the bus.
  1367. *
  1368. * LOCKING:
  1369. * PCI/etc. bus probe sem.
  1370. *
  1371. * RETURNS:
  1372. * Zero on success, negative errno otherwise.
  1373. */
  1374. int ata_bus_probe(struct ata_port *ap)
  1375. {
  1376. unsigned int classes[ATA_MAX_DEVICES];
  1377. int tries[ATA_MAX_DEVICES];
  1378. int i, rc, down_xfermask;
  1379. struct ata_device *dev;
  1380. ata_port_probe(ap);
  1381. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1382. tries[i] = ATA_PROBE_MAX_TRIES;
  1383. retry:
  1384. down_xfermask = 0;
  1385. /* reset and determine device classes */
  1386. ap->ops->phy_reset(ap);
  1387. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1388. dev = &ap->device[i];
  1389. if (!(ap->flags & ATA_FLAG_DISABLED) &&
  1390. dev->class != ATA_DEV_UNKNOWN)
  1391. classes[dev->devno] = dev->class;
  1392. else
  1393. classes[dev->devno] = ATA_DEV_NONE;
  1394. dev->class = ATA_DEV_UNKNOWN;
  1395. }
  1396. ata_port_probe(ap);
  1397. /* after the reset the device state is PIO 0 and the controller
  1398. state is undefined. Record the mode */
  1399. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1400. ap->device[i].pio_mode = XFER_PIO_0;
  1401. /* read IDENTIFY page and configure devices */
  1402. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1403. dev = &ap->device[i];
  1404. if (tries[i])
  1405. dev->class = classes[i];
  1406. if (!ata_dev_enabled(dev))
  1407. continue;
  1408. rc = ata_dev_read_id(dev, &dev->class, 1, dev->id);
  1409. if (rc)
  1410. goto fail;
  1411. rc = ata_dev_configure(dev, 1);
  1412. if (rc)
  1413. goto fail;
  1414. }
  1415. /* configure transfer mode */
  1416. rc = ata_set_mode(ap, &dev);
  1417. if (rc) {
  1418. down_xfermask = 1;
  1419. goto fail;
  1420. }
  1421. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1422. if (ata_dev_enabled(&ap->device[i]))
  1423. return 0;
  1424. /* no device present, disable port */
  1425. ata_port_disable(ap);
  1426. ap->ops->port_disable(ap);
  1427. return -ENODEV;
  1428. fail:
  1429. switch (rc) {
  1430. case -EINVAL:
  1431. case -ENODEV:
  1432. tries[dev->devno] = 0;
  1433. break;
  1434. case -EIO:
  1435. sata_down_spd_limit(ap);
  1436. /* fall through */
  1437. default:
  1438. tries[dev->devno]--;
  1439. if (down_xfermask &&
  1440. ata_down_xfermask_limit(dev, tries[dev->devno] == 1))
  1441. tries[dev->devno] = 0;
  1442. }
  1443. if (!tries[dev->devno]) {
  1444. ata_down_xfermask_limit(dev, 1);
  1445. ata_dev_disable(dev);
  1446. }
  1447. goto retry;
  1448. }
  1449. /**
  1450. * ata_port_probe - Mark port as enabled
  1451. * @ap: Port for which we indicate enablement
  1452. *
  1453. * Modify @ap data structure such that the system
  1454. * thinks that the entire port is enabled.
  1455. *
  1456. * LOCKING: host lock, or some other form of
  1457. * serialization.
  1458. */
  1459. void ata_port_probe(struct ata_port *ap)
  1460. {
  1461. ap->flags &= ~ATA_FLAG_DISABLED;
  1462. }
  1463. /**
  1464. * sata_print_link_status - Print SATA link status
  1465. * @ap: SATA port to printk link status about
  1466. *
  1467. * This function prints link speed and status of a SATA link.
  1468. *
  1469. * LOCKING:
  1470. * None.
  1471. */
  1472. static void sata_print_link_status(struct ata_port *ap)
  1473. {
  1474. u32 sstatus, scontrol, tmp;
  1475. if (sata_scr_read(ap, SCR_STATUS, &sstatus))
  1476. return;
  1477. sata_scr_read(ap, SCR_CONTROL, &scontrol);
  1478. if (ata_port_online(ap)) {
  1479. tmp = (sstatus >> 4) & 0xf;
  1480. ata_port_printk(ap, KERN_INFO,
  1481. "SATA link up %s (SStatus %X SControl %X)\n",
  1482. sata_spd_string(tmp), sstatus, scontrol);
  1483. } else {
  1484. ata_port_printk(ap, KERN_INFO,
  1485. "SATA link down (SStatus %X SControl %X)\n",
  1486. sstatus, scontrol);
  1487. }
  1488. }
  1489. /**
  1490. * __sata_phy_reset - Wake/reset a low-level SATA PHY
  1491. * @ap: SATA port associated with target SATA PHY.
  1492. *
  1493. * This function issues commands to standard SATA Sxxx
  1494. * PHY registers, to wake up the phy (and device), and
  1495. * clear any reset condition.
  1496. *
  1497. * LOCKING:
  1498. * PCI/etc. bus probe sem.
  1499. *
  1500. */
  1501. void __sata_phy_reset(struct ata_port *ap)
  1502. {
  1503. u32 sstatus;
  1504. unsigned long timeout = jiffies + (HZ * 5);
  1505. if (ap->flags & ATA_FLAG_SATA_RESET) {
  1506. /* issue phy wake/reset */
  1507. sata_scr_write_flush(ap, SCR_CONTROL, 0x301);
  1508. /* Couldn't find anything in SATA I/II specs, but
  1509. * AHCI-1.1 10.4.2 says at least 1 ms. */
  1510. mdelay(1);
  1511. }
  1512. /* phy wake/clear reset */
  1513. sata_scr_write_flush(ap, SCR_CONTROL, 0x300);
  1514. /* wait for phy to become ready, if necessary */
  1515. do {
  1516. msleep(200);
  1517. sata_scr_read(ap, SCR_STATUS, &sstatus);
  1518. if ((sstatus & 0xf) != 1)
  1519. break;
  1520. } while (time_before(jiffies, timeout));
  1521. /* print link status */
  1522. sata_print_link_status(ap);
  1523. /* TODO: phy layer with polling, timeouts, etc. */
  1524. if (!ata_port_offline(ap))
  1525. ata_port_probe(ap);
  1526. else
  1527. ata_port_disable(ap);
  1528. if (ap->flags & ATA_FLAG_DISABLED)
  1529. return;
  1530. if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
  1531. ata_port_disable(ap);
  1532. return;
  1533. }
  1534. ap->cbl = ATA_CBL_SATA;
  1535. }
  1536. /**
  1537. * sata_phy_reset - Reset SATA bus.
  1538. * @ap: SATA port associated with target SATA PHY.
  1539. *
  1540. * This function resets the SATA bus, and then probes
  1541. * the bus for devices.
  1542. *
  1543. * LOCKING:
  1544. * PCI/etc. bus probe sem.
  1545. *
  1546. */
  1547. void sata_phy_reset(struct ata_port *ap)
  1548. {
  1549. __sata_phy_reset(ap);
  1550. if (ap->flags & ATA_FLAG_DISABLED)
  1551. return;
  1552. ata_bus_reset(ap);
  1553. }
  1554. /**
  1555. * ata_dev_pair - return other device on cable
  1556. * @adev: device
  1557. *
  1558. * Obtain the other device on the same cable, or if none is
  1559. * present NULL is returned
  1560. */
  1561. struct ata_device *ata_dev_pair(struct ata_device *adev)
  1562. {
  1563. struct ata_port *ap = adev->ap;
  1564. struct ata_device *pair = &ap->device[1 - adev->devno];
  1565. if (!ata_dev_enabled(pair))
  1566. return NULL;
  1567. return pair;
  1568. }
  1569. /**
  1570. * ata_port_disable - Disable port.
  1571. * @ap: Port to be disabled.
  1572. *
  1573. * Modify @ap data structure such that the system
  1574. * thinks that the entire port is disabled, and should
  1575. * never attempt to probe or communicate with devices
  1576. * on this port.
  1577. *
  1578. * LOCKING: host lock, or some other form of
  1579. * serialization.
  1580. */
  1581. void ata_port_disable(struct ata_port *ap)
  1582. {
  1583. ap->device[0].class = ATA_DEV_NONE;
  1584. ap->device[1].class = ATA_DEV_NONE;
  1585. ap->flags |= ATA_FLAG_DISABLED;
  1586. }
  1587. /**
  1588. * sata_down_spd_limit - adjust SATA spd limit downward
  1589. * @ap: Port to adjust SATA spd limit for
  1590. *
  1591. * Adjust SATA spd limit of @ap downward. Note that this
  1592. * function only adjusts the limit. The change must be applied
  1593. * using sata_set_spd().
  1594. *
  1595. * LOCKING:
  1596. * Inherited from caller.
  1597. *
  1598. * RETURNS:
  1599. * 0 on success, negative errno on failure
  1600. */
  1601. int sata_down_spd_limit(struct ata_port *ap)
  1602. {
  1603. u32 sstatus, spd, mask;
  1604. int rc, highbit;
  1605. rc = sata_scr_read(ap, SCR_STATUS, &sstatus);
  1606. if (rc)
  1607. return rc;
  1608. mask = ap->sata_spd_limit;
  1609. if (mask <= 1)
  1610. return -EINVAL;
  1611. highbit = fls(mask) - 1;
  1612. mask &= ~(1 << highbit);
  1613. spd = (sstatus >> 4) & 0xf;
  1614. if (spd <= 1)
  1615. return -EINVAL;
  1616. spd--;
  1617. mask &= (1 << spd) - 1;
  1618. if (!mask)
  1619. return -EINVAL;
  1620. ap->sata_spd_limit = mask;
  1621. ata_port_printk(ap, KERN_WARNING, "limiting SATA link speed to %s\n",
  1622. sata_spd_string(fls(mask)));
  1623. return 0;
  1624. }
  1625. static int __sata_set_spd_needed(struct ata_port *ap, u32 *scontrol)
  1626. {
  1627. u32 spd, limit;
  1628. if (ap->sata_spd_limit == UINT_MAX)
  1629. limit = 0;
  1630. else
  1631. limit = fls(ap->sata_spd_limit);
  1632. spd = (*scontrol >> 4) & 0xf;
  1633. *scontrol = (*scontrol & ~0xf0) | ((limit & 0xf) << 4);
  1634. return spd != limit;
  1635. }
  1636. /**
  1637. * sata_set_spd_needed - is SATA spd configuration needed
  1638. * @ap: Port in question
  1639. *
  1640. * Test whether the spd limit in SControl matches
  1641. * @ap->sata_spd_limit. This function is used to determine
  1642. * whether hardreset is necessary to apply SATA spd
  1643. * configuration.
  1644. *
  1645. * LOCKING:
  1646. * Inherited from caller.
  1647. *
  1648. * RETURNS:
  1649. * 1 if SATA spd configuration is needed, 0 otherwise.
  1650. */
  1651. int sata_set_spd_needed(struct ata_port *ap)
  1652. {
  1653. u32 scontrol;
  1654. if (sata_scr_read(ap, SCR_CONTROL, &scontrol))
  1655. return 0;
  1656. return __sata_set_spd_needed(ap, &scontrol);
  1657. }
  1658. /**
  1659. * sata_set_spd - set SATA spd according to spd limit
  1660. * @ap: Port to set SATA spd for
  1661. *
  1662. * Set SATA spd of @ap according to sata_spd_limit.
  1663. *
  1664. * LOCKING:
  1665. * Inherited from caller.
  1666. *
  1667. * RETURNS:
  1668. * 0 if spd doesn't need to be changed, 1 if spd has been
  1669. * changed. Negative errno if SCR registers are inaccessible.
  1670. */
  1671. int sata_set_spd(struct ata_port *ap)
  1672. {
  1673. u32 scontrol;
  1674. int rc;
  1675. if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
  1676. return rc;
  1677. if (!__sata_set_spd_needed(ap, &scontrol))
  1678. return 0;
  1679. if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
  1680. return rc;
  1681. return 1;
  1682. }
  1683. /*
  1684. * This mode timing computation functionality is ported over from
  1685. * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
  1686. */
  1687. /*
  1688. * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
  1689. * These were taken from ATA/ATAPI-6 standard, rev 0a, except
  1690. * for UDMA6, which is currently supported only by Maxtor drives.
  1691. *
  1692. * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
  1693. */
  1694. static const struct ata_timing ata_timing[] = {
  1695. { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
  1696. { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
  1697. { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
  1698. { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
  1699. { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 80, 0 },
  1700. { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 100, 0 },
  1701. { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
  1702. { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
  1703. { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
  1704. /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
  1705. { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
  1706. { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
  1707. { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
  1708. { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
  1709. { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
  1710. { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
  1711. { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 80, 0 },
  1712. { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 100, 0 },
  1713. { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
  1714. { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
  1715. { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
  1716. { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
  1717. { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
  1718. /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
  1719. { 0xFF }
  1720. };
  1721. #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
  1722. #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
  1723. static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
  1724. {
  1725. q->setup = EZ(t->setup * 1000, T);
  1726. q->act8b = EZ(t->act8b * 1000, T);
  1727. q->rec8b = EZ(t->rec8b * 1000, T);
  1728. q->cyc8b = EZ(t->cyc8b * 1000, T);
  1729. q->active = EZ(t->active * 1000, T);
  1730. q->recover = EZ(t->recover * 1000, T);
  1731. q->cycle = EZ(t->cycle * 1000, T);
  1732. q->udma = EZ(t->udma * 1000, UT);
  1733. }
  1734. void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
  1735. struct ata_timing *m, unsigned int what)
  1736. {
  1737. if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
  1738. if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
  1739. if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
  1740. if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
  1741. if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
  1742. if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
  1743. if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
  1744. if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
  1745. }
  1746. static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
  1747. {
  1748. const struct ata_timing *t;
  1749. for (t = ata_timing; t->mode != speed; t++)
  1750. if (t->mode == 0xFF)
  1751. return NULL;
  1752. return t;
  1753. }
  1754. int ata_timing_compute(struct ata_device *adev, unsigned short speed,
  1755. struct ata_timing *t, int T, int UT)
  1756. {
  1757. const struct ata_timing *s;
  1758. struct ata_timing p;
  1759. /*
  1760. * Find the mode.
  1761. */
  1762. if (!(s = ata_timing_find_mode(speed)))
  1763. return -EINVAL;
  1764. memcpy(t, s, sizeof(*s));
  1765. /*
  1766. * If the drive is an EIDE drive, it can tell us it needs extended
  1767. * PIO/MW_DMA cycle timing.
  1768. */
  1769. if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
  1770. memset(&p, 0, sizeof(p));
  1771. if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
  1772. if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
  1773. else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
  1774. } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
  1775. p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
  1776. }
  1777. ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
  1778. }
  1779. /*
  1780. * Convert the timing to bus clock counts.
  1781. */
  1782. ata_timing_quantize(t, t, T, UT);
  1783. /*
  1784. * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
  1785. * S.M.A.R.T * and some other commands. We have to ensure that the
  1786. * DMA cycle timing is slower/equal than the fastest PIO timing.
  1787. */
  1788. if (speed > XFER_PIO_4) {
  1789. ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
  1790. ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
  1791. }
  1792. /*
  1793. * Lengthen active & recovery time so that cycle time is correct.
  1794. */
  1795. if (t->act8b + t->rec8b < t->cyc8b) {
  1796. t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
  1797. t->rec8b = t->cyc8b - t->act8b;
  1798. }
  1799. if (t->active + t->recover < t->cycle) {
  1800. t->active += (t->cycle - (t->active + t->recover)) / 2;
  1801. t->recover = t->cycle - t->active;
  1802. }
  1803. return 0;
  1804. }
  1805. /**
  1806. * ata_down_xfermask_limit - adjust dev xfer masks downward
  1807. * @dev: Device to adjust xfer masks
  1808. * @force_pio0: Force PIO0
  1809. *
  1810. * Adjust xfer masks of @dev downward. Note that this function
  1811. * does not apply the change. Invoking ata_set_mode() afterwards
  1812. * will apply the limit.
  1813. *
  1814. * LOCKING:
  1815. * Inherited from caller.
  1816. *
  1817. * RETURNS:
  1818. * 0 on success, negative errno on failure
  1819. */
  1820. int ata_down_xfermask_limit(struct ata_device *dev, int force_pio0)
  1821. {
  1822. unsigned long xfer_mask;
  1823. int highbit;
  1824. xfer_mask = ata_pack_xfermask(dev->pio_mask, dev->mwdma_mask,
  1825. dev->udma_mask);
  1826. if (!xfer_mask)
  1827. goto fail;
  1828. /* don't gear down to MWDMA from UDMA, go directly to PIO */
  1829. if (xfer_mask & ATA_MASK_UDMA)
  1830. xfer_mask &= ~ATA_MASK_MWDMA;
  1831. highbit = fls(xfer_mask) - 1;
  1832. xfer_mask &= ~(1 << highbit);
  1833. if (force_pio0)
  1834. xfer_mask &= 1 << ATA_SHIFT_PIO;
  1835. if (!xfer_mask)
  1836. goto fail;
  1837. ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
  1838. &dev->udma_mask);
  1839. ata_dev_printk(dev, KERN_WARNING, "limiting speed to %s\n",
  1840. ata_mode_string(xfer_mask));
  1841. return 0;
  1842. fail:
  1843. return -EINVAL;
  1844. }
  1845. static int ata_dev_set_mode(struct ata_device *dev)
  1846. {
  1847. unsigned int err_mask;
  1848. int rc;
  1849. dev->flags &= ~ATA_DFLAG_PIO;
  1850. if (dev->xfer_shift == ATA_SHIFT_PIO)
  1851. dev->flags |= ATA_DFLAG_PIO;
  1852. err_mask = ata_dev_set_xfermode(dev);
  1853. if (err_mask) {
  1854. ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
  1855. "(err_mask=0x%x)\n", err_mask);
  1856. return -EIO;
  1857. }
  1858. rc = ata_dev_revalidate(dev, 0);
  1859. if (rc)
  1860. return rc;
  1861. DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
  1862. dev->xfer_shift, (int)dev->xfer_mode);
  1863. ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
  1864. ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
  1865. return 0;
  1866. }
  1867. /**
  1868. * ata_set_mode - Program timings and issue SET FEATURES - XFER
  1869. * @ap: port on which timings will be programmed
  1870. * @r_failed_dev: out paramter for failed device
  1871. *
  1872. * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
  1873. * ata_set_mode() fails, pointer to the failing device is
  1874. * returned in @r_failed_dev.
  1875. *
  1876. * LOCKING:
  1877. * PCI/etc. bus probe sem.
  1878. *
  1879. * RETURNS:
  1880. * 0 on success, negative errno otherwise
  1881. */
  1882. int ata_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
  1883. {
  1884. struct ata_device *dev;
  1885. int i, rc = 0, used_dma = 0, found = 0;
  1886. /* has private set_mode? */
  1887. if (ap->ops->set_mode) {
  1888. /* FIXME: make ->set_mode handle no device case and
  1889. * return error code and failing device on failure.
  1890. */
  1891. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1892. if (ata_dev_ready(&ap->device[i])) {
  1893. ap->ops->set_mode(ap);
  1894. break;
  1895. }
  1896. }
  1897. return 0;
  1898. }
  1899. /* step 1: calculate xfer_mask */
  1900. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1901. unsigned int pio_mask, dma_mask;
  1902. dev = &ap->device[i];
  1903. if (!ata_dev_enabled(dev))
  1904. continue;
  1905. ata_dev_xfermask(dev);
  1906. pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
  1907. dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
  1908. dev->pio_mode = ata_xfer_mask2mode(pio_mask);
  1909. dev->dma_mode = ata_xfer_mask2mode(dma_mask);
  1910. found = 1;
  1911. if (dev->dma_mode)
  1912. used_dma = 1;
  1913. }
  1914. if (!found)
  1915. goto out;
  1916. /* step 2: always set host PIO timings */
  1917. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1918. dev = &ap->device[i];
  1919. if (!ata_dev_enabled(dev))
  1920. continue;
  1921. if (!dev->pio_mode) {
  1922. ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
  1923. rc = -EINVAL;
  1924. goto out;
  1925. }
  1926. dev->xfer_mode = dev->pio_mode;
  1927. dev->xfer_shift = ATA_SHIFT_PIO;
  1928. if (ap->ops->set_piomode)
  1929. ap->ops->set_piomode(ap, dev);
  1930. }
  1931. /* step 3: set host DMA timings */
  1932. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1933. dev = &ap->device[i];
  1934. if (!ata_dev_enabled(dev) || !dev->dma_mode)
  1935. continue;
  1936. dev->xfer_mode = dev->dma_mode;
  1937. dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
  1938. if (ap->ops->set_dmamode)
  1939. ap->ops->set_dmamode(ap, dev);
  1940. }
  1941. /* step 4: update devices' xfer mode */
  1942. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1943. dev = &ap->device[i];
  1944. /* don't udpate suspended devices' xfer mode */
  1945. if (!ata_dev_ready(dev))
  1946. continue;
  1947. rc = ata_dev_set_mode(dev);
  1948. if (rc)
  1949. goto out;
  1950. }
  1951. /* Record simplex status. If we selected DMA then the other
  1952. * host channels are not permitted to do so.
  1953. */
  1954. if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
  1955. ap->host->simplex_claimed = 1;
  1956. /* step5: chip specific finalisation */
  1957. if (ap->ops->post_set_mode)
  1958. ap->ops->post_set_mode(ap);
  1959. out:
  1960. if (rc)
  1961. *r_failed_dev = dev;
  1962. return rc;
  1963. }
  1964. /**
  1965. * ata_tf_to_host - issue ATA taskfile to host controller
  1966. * @ap: port to which command is being issued
  1967. * @tf: ATA taskfile register set
  1968. *
  1969. * Issues ATA taskfile register set to ATA host controller,
  1970. * with proper synchronization with interrupt handler and
  1971. * other threads.
  1972. *
  1973. * LOCKING:
  1974. * spin_lock_irqsave(host lock)
  1975. */
  1976. static inline void ata_tf_to_host(struct ata_port *ap,
  1977. const struct ata_taskfile *tf)
  1978. {
  1979. ap->ops->tf_load(ap, tf);
  1980. ap->ops->exec_command(ap, tf);
  1981. }
  1982. /**
  1983. * ata_busy_sleep - sleep until BSY clears, or timeout
  1984. * @ap: port containing status register to be polled
  1985. * @tmout_pat: impatience timeout
  1986. * @tmout: overall timeout
  1987. *
  1988. * Sleep until ATA Status register bit BSY clears,
  1989. * or a timeout occurs.
  1990. *
  1991. * LOCKING: None.
  1992. */
  1993. unsigned int ata_busy_sleep (struct ata_port *ap,
  1994. unsigned long tmout_pat, unsigned long tmout)
  1995. {
  1996. unsigned long timer_start, timeout;
  1997. u8 status;
  1998. status = ata_busy_wait(ap, ATA_BUSY, 300);
  1999. timer_start = jiffies;
  2000. timeout = timer_start + tmout_pat;
  2001. while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
  2002. msleep(50);
  2003. status = ata_busy_wait(ap, ATA_BUSY, 3);
  2004. }
  2005. if (status & ATA_BUSY)
  2006. ata_port_printk(ap, KERN_WARNING,
  2007. "port is slow to respond, please be patient "
  2008. "(Status 0x%x)\n", status);
  2009. timeout = timer_start + tmout;
  2010. while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
  2011. msleep(50);
  2012. status = ata_chk_status(ap);
  2013. }
  2014. if (status & ATA_BUSY) {
  2015. ata_port_printk(ap, KERN_ERR, "port failed to respond "
  2016. "(%lu secs, Status 0x%x)\n",
  2017. tmout / HZ, status);
  2018. return 1;
  2019. }
  2020. return 0;
  2021. }
  2022. static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
  2023. {
  2024. struct ata_ioports *ioaddr = &ap->ioaddr;
  2025. unsigned int dev0 = devmask & (1 << 0);
  2026. unsigned int dev1 = devmask & (1 << 1);
  2027. unsigned long timeout;
  2028. /* if device 0 was found in ata_devchk, wait for its
  2029. * BSY bit to clear
  2030. */
  2031. if (dev0)
  2032. ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  2033. /* if device 1 was found in ata_devchk, wait for
  2034. * register access, then wait for BSY to clear
  2035. */
  2036. timeout = jiffies + ATA_TMOUT_BOOT;
  2037. while (dev1) {
  2038. u8 nsect, lbal;
  2039. ap->ops->dev_select(ap, 1);
  2040. if (ap->flags & ATA_FLAG_MMIO) {
  2041. nsect = readb((void __iomem *) ioaddr->nsect_addr);
  2042. lbal = readb((void __iomem *) ioaddr->lbal_addr);
  2043. } else {
  2044. nsect = inb(ioaddr->nsect_addr);
  2045. lbal = inb(ioaddr->lbal_addr);
  2046. }
  2047. if ((nsect == 1) && (lbal == 1))
  2048. break;
  2049. if (time_after(jiffies, timeout)) {
  2050. dev1 = 0;
  2051. break;
  2052. }
  2053. msleep(50); /* give drive a breather */
  2054. }
  2055. if (dev1)
  2056. ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  2057. /* is all this really necessary? */
  2058. ap->ops->dev_select(ap, 0);
  2059. if (dev1)
  2060. ap->ops->dev_select(ap, 1);
  2061. if (dev0)
  2062. ap->ops->dev_select(ap, 0);
  2063. }
  2064. static unsigned int ata_bus_softreset(struct ata_port *ap,
  2065. unsigned int devmask)
  2066. {
  2067. struct ata_ioports *ioaddr = &ap->ioaddr;
  2068. DPRINTK("ata%u: bus reset via SRST\n", ap->id);
  2069. /* software reset. causes dev0 to be selected */
  2070. if (ap->flags & ATA_FLAG_MMIO) {
  2071. writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
  2072. udelay(20); /* FIXME: flush */
  2073. writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
  2074. udelay(20); /* FIXME: flush */
  2075. writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
  2076. } else {
  2077. outb(ap->ctl, ioaddr->ctl_addr);
  2078. udelay(10);
  2079. outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
  2080. udelay(10);
  2081. outb(ap->ctl, ioaddr->ctl_addr);
  2082. }
  2083. /* spec mandates ">= 2ms" before checking status.
  2084. * We wait 150ms, because that was the magic delay used for
  2085. * ATAPI devices in Hale Landis's ATADRVR, for the period of time
  2086. * between when the ATA command register is written, and then
  2087. * status is checked. Because waiting for "a while" before
  2088. * checking status is fine, post SRST, we perform this magic
  2089. * delay here as well.
  2090. *
  2091. * Old drivers/ide uses the 2mS rule and then waits for ready
  2092. */
  2093. msleep(150);
  2094. /* Before we perform post reset processing we want to see if
  2095. * the bus shows 0xFF because the odd clown forgets the D7
  2096. * pulldown resistor.
  2097. */
  2098. if (ata_check_status(ap) == 0xFF) {
  2099. ata_port_printk(ap, KERN_ERR, "SRST failed (status 0xFF)\n");
  2100. return AC_ERR_OTHER;
  2101. }
  2102. ata_bus_post_reset(ap, devmask);
  2103. return 0;
  2104. }
  2105. /**
  2106. * ata_bus_reset - reset host port and associated ATA channel
  2107. * @ap: port to reset
  2108. *
  2109. * This is typically the first time we actually start issuing
  2110. * commands to the ATA channel. We wait for BSY to clear, then
  2111. * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
  2112. * result. Determine what devices, if any, are on the channel
  2113. * by looking at the device 0/1 error register. Look at the signature
  2114. * stored in each device's taskfile registers, to determine if
  2115. * the device is ATA or ATAPI.
  2116. *
  2117. * LOCKING:
  2118. * PCI/etc. bus probe sem.
  2119. * Obtains host lock.
  2120. *
  2121. * SIDE EFFECTS:
  2122. * Sets ATA_FLAG_DISABLED if bus reset fails.
  2123. */
  2124. void ata_bus_reset(struct ata_port *ap)
  2125. {
  2126. struct ata_ioports *ioaddr = &ap->ioaddr;
  2127. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  2128. u8 err;
  2129. unsigned int dev0, dev1 = 0, devmask = 0;
  2130. DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
  2131. /* determine if device 0/1 are present */
  2132. if (ap->flags & ATA_FLAG_SATA_RESET)
  2133. dev0 = 1;
  2134. else {
  2135. dev0 = ata_devchk(ap, 0);
  2136. if (slave_possible)
  2137. dev1 = ata_devchk(ap, 1);
  2138. }
  2139. if (dev0)
  2140. devmask |= (1 << 0);
  2141. if (dev1)
  2142. devmask |= (1 << 1);
  2143. /* select device 0 again */
  2144. ap->ops->dev_select(ap, 0);
  2145. /* issue bus reset */
  2146. if (ap->flags & ATA_FLAG_SRST)
  2147. if (ata_bus_softreset(ap, devmask))
  2148. goto err_out;
  2149. /*
  2150. * determine by signature whether we have ATA or ATAPI devices
  2151. */
  2152. ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
  2153. if ((slave_possible) && (err != 0x81))
  2154. ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
  2155. /* re-enable interrupts */
  2156. if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
  2157. ata_irq_on(ap);
  2158. /* is double-select really necessary? */
  2159. if (ap->device[1].class != ATA_DEV_NONE)
  2160. ap->ops->dev_select(ap, 1);
  2161. if (ap->device[0].class != ATA_DEV_NONE)
  2162. ap->ops->dev_select(ap, 0);
  2163. /* if no devices were detected, disable this port */
  2164. if ((ap->device[0].class == ATA_DEV_NONE) &&
  2165. (ap->device[1].class == ATA_DEV_NONE))
  2166. goto err_out;
  2167. if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
  2168. /* set up device control for ATA_FLAG_SATA_RESET */
  2169. if (ap->flags & ATA_FLAG_MMIO)
  2170. writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
  2171. else
  2172. outb(ap->ctl, ioaddr->ctl_addr);
  2173. }
  2174. DPRINTK("EXIT\n");
  2175. return;
  2176. err_out:
  2177. ata_port_printk(ap, KERN_ERR, "disabling port\n");
  2178. ap->ops->port_disable(ap);
  2179. DPRINTK("EXIT\n");
  2180. }
  2181. /**
  2182. * sata_phy_debounce - debounce SATA phy status
  2183. * @ap: ATA port to debounce SATA phy status for
  2184. * @params: timing parameters { interval, duratinon, timeout } in msec
  2185. *
  2186. * Make sure SStatus of @ap reaches stable state, determined by
  2187. * holding the same value where DET is not 1 for @duration polled
  2188. * every @interval, before @timeout. Timeout constraints the
  2189. * beginning of the stable state. Because, after hot unplugging,
  2190. * DET gets stuck at 1 on some controllers, this functions waits
  2191. * until timeout then returns 0 if DET is stable at 1.
  2192. *
  2193. * LOCKING:
  2194. * Kernel thread context (may sleep)
  2195. *
  2196. * RETURNS:
  2197. * 0 on success, -errno on failure.
  2198. */
  2199. int sata_phy_debounce(struct ata_port *ap, const unsigned long *params)
  2200. {
  2201. unsigned long interval_msec = params[0];
  2202. unsigned long duration = params[1] * HZ / 1000;
  2203. unsigned long timeout = jiffies + params[2] * HZ / 1000;
  2204. unsigned long last_jiffies;
  2205. u32 last, cur;
  2206. int rc;
  2207. if ((rc = sata_scr_read(ap, SCR_STATUS, &cur)))
  2208. return rc;
  2209. cur &= 0xf;
  2210. last = cur;
  2211. last_jiffies = jiffies;
  2212. while (1) {
  2213. msleep(interval_msec);
  2214. if ((rc = sata_scr_read(ap, SCR_STATUS, &cur)))
  2215. return rc;
  2216. cur &= 0xf;
  2217. /* DET stable? */
  2218. if (cur == last) {
  2219. if (cur == 1 && time_before(jiffies, timeout))
  2220. continue;
  2221. if (time_after(jiffies, last_jiffies + duration))
  2222. return 0;
  2223. continue;
  2224. }
  2225. /* unstable, start over */
  2226. last = cur;
  2227. last_jiffies = jiffies;
  2228. /* check timeout */
  2229. if (time_after(jiffies, timeout))
  2230. return -EBUSY;
  2231. }
  2232. }
  2233. /**
  2234. * sata_phy_resume - resume SATA phy
  2235. * @ap: ATA port to resume SATA phy for
  2236. * @params: timing parameters { interval, duratinon, timeout } in msec
  2237. *
  2238. * Resume SATA phy of @ap and debounce it.
  2239. *
  2240. * LOCKING:
  2241. * Kernel thread context (may sleep)
  2242. *
  2243. * RETURNS:
  2244. * 0 on success, -errno on failure.
  2245. */
  2246. int sata_phy_resume(struct ata_port *ap, const unsigned long *params)
  2247. {
  2248. u32 scontrol;
  2249. int rc;
  2250. if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
  2251. return rc;
  2252. scontrol = (scontrol & 0x0f0) | 0x300;
  2253. if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
  2254. return rc;
  2255. /* Some PHYs react badly if SStatus is pounded immediately
  2256. * after resuming. Delay 200ms before debouncing.
  2257. */
  2258. msleep(200);
  2259. return sata_phy_debounce(ap, params);
  2260. }
  2261. static void ata_wait_spinup(struct ata_port *ap)
  2262. {
  2263. struct ata_eh_context *ehc = &ap->eh_context;
  2264. unsigned long end, secs;
  2265. int rc;
  2266. /* first, debounce phy if SATA */
  2267. if (ap->cbl == ATA_CBL_SATA) {
  2268. rc = sata_phy_debounce(ap, sata_deb_timing_hotplug);
  2269. /* if debounced successfully and offline, no need to wait */
  2270. if ((rc == 0 || rc == -EOPNOTSUPP) && ata_port_offline(ap))
  2271. return;
  2272. }
  2273. /* okay, let's give the drive time to spin up */
  2274. end = ehc->i.hotplug_timestamp + ATA_SPINUP_WAIT * HZ / 1000;
  2275. secs = ((end - jiffies) + HZ - 1) / HZ;
  2276. if (time_after(jiffies, end))
  2277. return;
  2278. if (secs > 5)
  2279. ata_port_printk(ap, KERN_INFO, "waiting for device to spin up "
  2280. "(%lu secs)\n", secs);
  2281. schedule_timeout_uninterruptible(end - jiffies);
  2282. }
  2283. /**
  2284. * ata_std_prereset - prepare for reset
  2285. * @ap: ATA port to be reset
  2286. *
  2287. * @ap is about to be reset. Initialize it.
  2288. *
  2289. * LOCKING:
  2290. * Kernel thread context (may sleep)
  2291. *
  2292. * RETURNS:
  2293. * 0 on success, -errno otherwise.
  2294. */
  2295. int ata_std_prereset(struct ata_port *ap)
  2296. {
  2297. struct ata_eh_context *ehc = &ap->eh_context;
  2298. const unsigned long *timing = sata_ehc_deb_timing(ehc);
  2299. int rc;
  2300. /* handle link resume & hotplug spinup */
  2301. if ((ehc->i.flags & ATA_EHI_RESUME_LINK) &&
  2302. (ap->flags & ATA_FLAG_HRST_TO_RESUME))
  2303. ehc->i.action |= ATA_EH_HARDRESET;
  2304. if ((ehc->i.flags & ATA_EHI_HOTPLUGGED) &&
  2305. (ap->flags & ATA_FLAG_SKIP_D2H_BSY))
  2306. ata_wait_spinup(ap);
  2307. /* if we're about to do hardreset, nothing more to do */
  2308. if (ehc->i.action & ATA_EH_HARDRESET)
  2309. return 0;
  2310. /* if SATA, resume phy */
  2311. if (ap->cbl == ATA_CBL_SATA) {
  2312. rc = sata_phy_resume(ap, timing);
  2313. if (rc && rc != -EOPNOTSUPP) {
  2314. /* phy resume failed */
  2315. ata_port_printk(ap, KERN_WARNING, "failed to resume "
  2316. "link for reset (errno=%d)\n", rc);
  2317. return rc;
  2318. }
  2319. }
  2320. /* Wait for !BSY if the controller can wait for the first D2H
  2321. * Reg FIS and we don't know that no device is attached.
  2322. */
  2323. if (!(ap->flags & ATA_FLAG_SKIP_D2H_BSY) && !ata_port_offline(ap))
  2324. ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  2325. return 0;
  2326. }
  2327. /**
  2328. * ata_std_softreset - reset host port via ATA SRST
  2329. * @ap: port to reset
  2330. * @classes: resulting classes of attached devices
  2331. *
  2332. * Reset host port using ATA SRST.
  2333. *
  2334. * LOCKING:
  2335. * Kernel thread context (may sleep)
  2336. *
  2337. * RETURNS:
  2338. * 0 on success, -errno otherwise.
  2339. */
  2340. int ata_std_softreset(struct ata_port *ap, unsigned int *classes)
  2341. {
  2342. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  2343. unsigned int devmask = 0, err_mask;
  2344. u8 err;
  2345. DPRINTK("ENTER\n");
  2346. if (ata_port_offline(ap)) {
  2347. classes[0] = ATA_DEV_NONE;
  2348. goto out;
  2349. }
  2350. /* determine if device 0/1 are present */
  2351. if (ata_devchk(ap, 0))
  2352. devmask |= (1 << 0);
  2353. if (slave_possible && ata_devchk(ap, 1))
  2354. devmask |= (1 << 1);
  2355. /* select device 0 again */
  2356. ap->ops->dev_select(ap, 0);
  2357. /* issue bus reset */
  2358. DPRINTK("about to softreset, devmask=%x\n", devmask);
  2359. err_mask = ata_bus_softreset(ap, devmask);
  2360. if (err_mask) {
  2361. ata_port_printk(ap, KERN_ERR, "SRST failed (err_mask=0x%x)\n",
  2362. err_mask);
  2363. return -EIO;
  2364. }
  2365. /* determine by signature whether we have ATA or ATAPI devices */
  2366. classes[0] = ata_dev_try_classify(ap, 0, &err);
  2367. if (slave_possible && err != 0x81)
  2368. classes[1] = ata_dev_try_classify(ap, 1, &err);
  2369. out:
  2370. DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
  2371. return 0;
  2372. }
  2373. /**
  2374. * sata_std_hardreset - reset host port via SATA phy reset
  2375. * @ap: port to reset
  2376. * @class: resulting class of attached device
  2377. *
  2378. * SATA phy-reset host port using DET bits of SControl register.
  2379. *
  2380. * LOCKING:
  2381. * Kernel thread context (may sleep)
  2382. *
  2383. * RETURNS:
  2384. * 0 on success, -errno otherwise.
  2385. */
  2386. int sata_std_hardreset(struct ata_port *ap, unsigned int *class)
  2387. {
  2388. struct ata_eh_context *ehc = &ap->eh_context;
  2389. const unsigned long *timing = sata_ehc_deb_timing(ehc);
  2390. u32 scontrol;
  2391. int rc;
  2392. DPRINTK("ENTER\n");
  2393. if (sata_set_spd_needed(ap)) {
  2394. /* SATA spec says nothing about how to reconfigure
  2395. * spd. To be on the safe side, turn off phy during
  2396. * reconfiguration. This works for at least ICH7 AHCI
  2397. * and Sil3124.
  2398. */
  2399. if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
  2400. return rc;
  2401. scontrol = (scontrol & 0x0f0) | 0x304;
  2402. if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
  2403. return rc;
  2404. sata_set_spd(ap);
  2405. }
  2406. /* issue phy wake/reset */
  2407. if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
  2408. return rc;
  2409. scontrol = (scontrol & 0x0f0) | 0x301;
  2410. if ((rc = sata_scr_write_flush(ap, SCR_CONTROL, scontrol)))
  2411. return rc;
  2412. /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
  2413. * 10.4.2 says at least 1 ms.
  2414. */
  2415. msleep(1);
  2416. /* bring phy back */
  2417. sata_phy_resume(ap, timing);
  2418. /* TODO: phy layer with polling, timeouts, etc. */
  2419. if (ata_port_offline(ap)) {
  2420. *class = ATA_DEV_NONE;
  2421. DPRINTK("EXIT, link offline\n");
  2422. return 0;
  2423. }
  2424. if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
  2425. ata_port_printk(ap, KERN_ERR,
  2426. "COMRESET failed (device not ready)\n");
  2427. return -EIO;
  2428. }
  2429. ap->ops->dev_select(ap, 0); /* probably unnecessary */
  2430. *class = ata_dev_try_classify(ap, 0, NULL);
  2431. DPRINTK("EXIT, class=%u\n", *class);
  2432. return 0;
  2433. }
  2434. /**
  2435. * ata_std_postreset - standard postreset callback
  2436. * @ap: the target ata_port
  2437. * @classes: classes of attached devices
  2438. *
  2439. * This function is invoked after a successful reset. Note that
  2440. * the device might have been reset more than once using
  2441. * different reset methods before postreset is invoked.
  2442. *
  2443. * LOCKING:
  2444. * Kernel thread context (may sleep)
  2445. */
  2446. void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
  2447. {
  2448. u32 serror;
  2449. DPRINTK("ENTER\n");
  2450. /* print link status */
  2451. sata_print_link_status(ap);
  2452. /* clear SError */
  2453. if (sata_scr_read(ap, SCR_ERROR, &serror) == 0)
  2454. sata_scr_write(ap, SCR_ERROR, serror);
  2455. /* re-enable interrupts */
  2456. if (!ap->ops->error_handler) {
  2457. /* FIXME: hack. create a hook instead */
  2458. if (ap->ioaddr.ctl_addr)
  2459. ata_irq_on(ap);
  2460. }
  2461. /* is double-select really necessary? */
  2462. if (classes[0] != ATA_DEV_NONE)
  2463. ap->ops->dev_select(ap, 1);
  2464. if (classes[1] != ATA_DEV_NONE)
  2465. ap->ops->dev_select(ap, 0);
  2466. /* bail out if no device is present */
  2467. if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
  2468. DPRINTK("EXIT, no device\n");
  2469. return;
  2470. }
  2471. /* set up device control */
  2472. if (ap->ioaddr.ctl_addr) {
  2473. if (ap->flags & ATA_FLAG_MMIO)
  2474. writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
  2475. else
  2476. outb(ap->ctl, ap->ioaddr.ctl_addr);
  2477. }
  2478. DPRINTK("EXIT\n");
  2479. }
  2480. /**
  2481. * ata_dev_same_device - Determine whether new ID matches configured device
  2482. * @dev: device to compare against
  2483. * @new_class: class of the new device
  2484. * @new_id: IDENTIFY page of the new device
  2485. *
  2486. * Compare @new_class and @new_id against @dev and determine
  2487. * whether @dev is the device indicated by @new_class and
  2488. * @new_id.
  2489. *
  2490. * LOCKING:
  2491. * None.
  2492. *
  2493. * RETURNS:
  2494. * 1 if @dev matches @new_class and @new_id, 0 otherwise.
  2495. */
  2496. static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
  2497. const u16 *new_id)
  2498. {
  2499. const u16 *old_id = dev->id;
  2500. unsigned char model[2][41], serial[2][21];
  2501. u64 new_n_sectors;
  2502. if (dev->class != new_class) {
  2503. ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
  2504. dev->class, new_class);
  2505. return 0;
  2506. }
  2507. ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
  2508. ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
  2509. ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
  2510. ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
  2511. new_n_sectors = ata_id_n_sectors(new_id);
  2512. if (strcmp(model[0], model[1])) {
  2513. ata_dev_printk(dev, KERN_INFO, "model number mismatch "
  2514. "'%s' != '%s'\n", model[0], model[1]);
  2515. return 0;
  2516. }
  2517. if (strcmp(serial[0], serial[1])) {
  2518. ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
  2519. "'%s' != '%s'\n", serial[0], serial[1]);
  2520. return 0;
  2521. }
  2522. if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
  2523. ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
  2524. "%llu != %llu\n",
  2525. (unsigned long long)dev->n_sectors,
  2526. (unsigned long long)new_n_sectors);
  2527. return 0;
  2528. }
  2529. return 1;
  2530. }
  2531. /**
  2532. * ata_dev_revalidate - Revalidate ATA device
  2533. * @dev: device to revalidate
  2534. * @post_reset: is this revalidation after reset?
  2535. *
  2536. * Re-read IDENTIFY page and make sure @dev is still attached to
  2537. * the port.
  2538. *
  2539. * LOCKING:
  2540. * Kernel thread context (may sleep)
  2541. *
  2542. * RETURNS:
  2543. * 0 on success, negative errno otherwise
  2544. */
  2545. int ata_dev_revalidate(struct ata_device *dev, int post_reset)
  2546. {
  2547. unsigned int class = dev->class;
  2548. u16 *id = (void *)dev->ap->sector_buf;
  2549. int rc;
  2550. if (!ata_dev_enabled(dev)) {
  2551. rc = -ENODEV;
  2552. goto fail;
  2553. }
  2554. /* read ID data */
  2555. rc = ata_dev_read_id(dev, &class, post_reset, id);
  2556. if (rc)
  2557. goto fail;
  2558. /* is the device still there? */
  2559. if (!ata_dev_same_device(dev, class, id)) {
  2560. rc = -ENODEV;
  2561. goto fail;
  2562. }
  2563. memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
  2564. /* configure device according to the new ID */
  2565. rc = ata_dev_configure(dev, 0);
  2566. if (rc == 0)
  2567. return 0;
  2568. fail:
  2569. ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
  2570. return rc;
  2571. }
  2572. static const char * const ata_dma_blacklist [] = {
  2573. "WDC AC11000H", NULL,
  2574. "WDC AC22100H", NULL,
  2575. "WDC AC32500H", NULL,
  2576. "WDC AC33100H", NULL,
  2577. "WDC AC31600H", NULL,
  2578. "WDC AC32100H", "24.09P07",
  2579. "WDC AC23200L", "21.10N21",
  2580. "Compaq CRD-8241B", NULL,
  2581. "CRD-8400B", NULL,
  2582. "CRD-8480B", NULL,
  2583. "CRD-8482B", NULL,
  2584. "CRD-84", NULL,
  2585. "SanDisk SDP3B", NULL,
  2586. "SanDisk SDP3B-64", NULL,
  2587. "SANYO CD-ROM CRD", NULL,
  2588. "HITACHI CDR-8", NULL,
  2589. "HITACHI CDR-8335", NULL,
  2590. "HITACHI CDR-8435", NULL,
  2591. "Toshiba CD-ROM XM-6202B", NULL,
  2592. "TOSHIBA CD-ROM XM-1702BC", NULL,
  2593. "CD-532E-A", NULL,
  2594. "E-IDE CD-ROM CR-840", NULL,
  2595. "CD-ROM Drive/F5A", NULL,
  2596. "WPI CDD-820", NULL,
  2597. "SAMSUNG CD-ROM SC-148C", NULL,
  2598. "SAMSUNG CD-ROM SC", NULL,
  2599. "SanDisk SDP3B-64", NULL,
  2600. "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,
  2601. "_NEC DV5800A", NULL,
  2602. "SAMSUNG CD-ROM SN-124", "N001"
  2603. };
  2604. static int ata_strim(char *s, size_t len)
  2605. {
  2606. len = strnlen(s, len);
  2607. /* ATAPI specifies that empty space is blank-filled; remove blanks */
  2608. while ((len > 0) && (s[len - 1] == ' ')) {
  2609. len--;
  2610. s[len] = 0;
  2611. }
  2612. return len;
  2613. }
  2614. static int ata_dma_blacklisted(const struct ata_device *dev)
  2615. {
  2616. unsigned char model_num[40];
  2617. unsigned char model_rev[16];
  2618. unsigned int nlen, rlen;
  2619. int i;
  2620. /* We don't support polling DMA.
  2621. * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
  2622. * if the LLDD handles only interrupts in the HSM_ST_LAST state.
  2623. */
  2624. if ((dev->ap->flags & ATA_FLAG_PIO_POLLING) &&
  2625. (dev->flags & ATA_DFLAG_CDB_INTR))
  2626. return 1;
  2627. ata_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
  2628. sizeof(model_num));
  2629. ata_id_string(dev->id, model_rev, ATA_ID_FW_REV_OFS,
  2630. sizeof(model_rev));
  2631. nlen = ata_strim(model_num, sizeof(model_num));
  2632. rlen = ata_strim(model_rev, sizeof(model_rev));
  2633. for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i += 2) {
  2634. if (!strncmp(ata_dma_blacklist[i], model_num, nlen)) {
  2635. if (ata_dma_blacklist[i+1] == NULL)
  2636. return 1;
  2637. if (!strncmp(ata_dma_blacklist[i], model_rev, rlen))
  2638. return 1;
  2639. }
  2640. }
  2641. return 0;
  2642. }
  2643. /**
  2644. * ata_dev_xfermask - Compute supported xfermask of the given device
  2645. * @dev: Device to compute xfermask for
  2646. *
  2647. * Compute supported xfermask of @dev and store it in
  2648. * dev->*_mask. This function is responsible for applying all
  2649. * known limits including host controller limits, device
  2650. * blacklist, etc...
  2651. *
  2652. * LOCKING:
  2653. * None.
  2654. */
  2655. static void ata_dev_xfermask(struct ata_device *dev)
  2656. {
  2657. struct ata_port *ap = dev->ap;
  2658. struct ata_host *host = ap->host;
  2659. unsigned long xfer_mask;
  2660. /* controller modes available */
  2661. xfer_mask = ata_pack_xfermask(ap->pio_mask,
  2662. ap->mwdma_mask, ap->udma_mask);
  2663. /* Apply cable rule here. Don't apply it early because when
  2664. * we handle hot plug the cable type can itself change.
  2665. */
  2666. if (ap->cbl == ATA_CBL_PATA40)
  2667. xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
  2668. xfer_mask &= ata_pack_xfermask(dev->pio_mask,
  2669. dev->mwdma_mask, dev->udma_mask);
  2670. xfer_mask &= ata_id_xfermask(dev->id);
  2671. /*
  2672. * CFA Advanced TrueIDE timings are not allowed on a shared
  2673. * cable
  2674. */
  2675. if (ata_dev_pair(dev)) {
  2676. /* No PIO5 or PIO6 */
  2677. xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
  2678. /* No MWDMA3 or MWDMA 4 */
  2679. xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
  2680. }
  2681. if (ata_dma_blacklisted(dev)) {
  2682. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  2683. ata_dev_printk(dev, KERN_WARNING,
  2684. "device is on DMA blacklist, disabling DMA\n");
  2685. }
  2686. if ((host->flags & ATA_HOST_SIMPLEX) && host->simplex_claimed) {
  2687. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  2688. ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
  2689. "other device, disabling DMA\n");
  2690. }
  2691. if (ap->ops->mode_filter)
  2692. xfer_mask = ap->ops->mode_filter(ap, dev, xfer_mask);
  2693. ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
  2694. &dev->mwdma_mask, &dev->udma_mask);
  2695. }
  2696. /**
  2697. * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
  2698. * @dev: Device to which command will be sent
  2699. *
  2700. * Issue SET FEATURES - XFER MODE command to device @dev
  2701. * on port @ap.
  2702. *
  2703. * LOCKING:
  2704. * PCI/etc. bus probe sem.
  2705. *
  2706. * RETURNS:
  2707. * 0 on success, AC_ERR_* mask otherwise.
  2708. */
  2709. static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
  2710. {
  2711. struct ata_taskfile tf;
  2712. unsigned int err_mask;
  2713. /* set up set-features taskfile */
  2714. DPRINTK("set features - xfer mode\n");
  2715. ata_tf_init(dev, &tf);
  2716. tf.command = ATA_CMD_SET_FEATURES;
  2717. tf.feature = SETFEATURES_XFER;
  2718. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2719. tf.protocol = ATA_PROT_NODATA;
  2720. tf.nsect = dev->xfer_mode;
  2721. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
  2722. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  2723. return err_mask;
  2724. }
  2725. /**
  2726. * ata_dev_init_params - Issue INIT DEV PARAMS command
  2727. * @dev: Device to which command will be sent
  2728. * @heads: Number of heads (taskfile parameter)
  2729. * @sectors: Number of sectors (taskfile parameter)
  2730. *
  2731. * LOCKING:
  2732. * Kernel thread context (may sleep)
  2733. *
  2734. * RETURNS:
  2735. * 0 on success, AC_ERR_* mask otherwise.
  2736. */
  2737. static unsigned int ata_dev_init_params(struct ata_device *dev,
  2738. u16 heads, u16 sectors)
  2739. {
  2740. struct ata_taskfile tf;
  2741. unsigned int err_mask;
  2742. /* Number of sectors per track 1-255. Number of heads 1-16 */
  2743. if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
  2744. return AC_ERR_INVALID;
  2745. /* set up init dev params taskfile */
  2746. DPRINTK("init dev params \n");
  2747. ata_tf_init(dev, &tf);
  2748. tf.command = ATA_CMD_INIT_DEV_PARAMS;
  2749. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2750. tf.protocol = ATA_PROT_NODATA;
  2751. tf.nsect = sectors;
  2752. tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
  2753. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
  2754. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  2755. return err_mask;
  2756. }
  2757. /**
  2758. * ata_sg_clean - Unmap DMA memory associated with command
  2759. * @qc: Command containing DMA memory to be released
  2760. *
  2761. * Unmap all mapped DMA memory associated with this command.
  2762. *
  2763. * LOCKING:
  2764. * spin_lock_irqsave(host lock)
  2765. */
  2766. static void ata_sg_clean(struct ata_queued_cmd *qc)
  2767. {
  2768. struct ata_port *ap = qc->ap;
  2769. struct scatterlist *sg = qc->__sg;
  2770. int dir = qc->dma_dir;
  2771. void *pad_buf = NULL;
  2772. WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
  2773. WARN_ON(sg == NULL);
  2774. if (qc->flags & ATA_QCFLAG_SINGLE)
  2775. WARN_ON(qc->n_elem > 1);
  2776. VPRINTK("unmapping %u sg elements\n", qc->n_elem);
  2777. /* if we padded the buffer out to 32-bit bound, and data
  2778. * xfer direction is from-device, we must copy from the
  2779. * pad buffer back into the supplied buffer
  2780. */
  2781. if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
  2782. pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  2783. if (qc->flags & ATA_QCFLAG_SG) {
  2784. if (qc->n_elem)
  2785. dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
  2786. /* restore last sg */
  2787. sg[qc->orig_n_elem - 1].length += qc->pad_len;
  2788. if (pad_buf) {
  2789. struct scatterlist *psg = &qc->pad_sgent;
  2790. void *addr = kmap_atomic(psg->page, KM_IRQ0);
  2791. memcpy(addr + psg->offset, pad_buf, qc->pad_len);
  2792. kunmap_atomic(addr, KM_IRQ0);
  2793. }
  2794. } else {
  2795. if (qc->n_elem)
  2796. dma_unmap_single(ap->dev,
  2797. sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
  2798. dir);
  2799. /* restore sg */
  2800. sg->length += qc->pad_len;
  2801. if (pad_buf)
  2802. memcpy(qc->buf_virt + sg->length - qc->pad_len,
  2803. pad_buf, qc->pad_len);
  2804. }
  2805. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  2806. qc->__sg = NULL;
  2807. }
  2808. /**
  2809. * ata_fill_sg - Fill PCI IDE PRD table
  2810. * @qc: Metadata associated with taskfile to be transferred
  2811. *
  2812. * Fill PCI IDE PRD (scatter-gather) table with segments
  2813. * associated with the current disk command.
  2814. *
  2815. * LOCKING:
  2816. * spin_lock_irqsave(host lock)
  2817. *
  2818. */
  2819. static void ata_fill_sg(struct ata_queued_cmd *qc)
  2820. {
  2821. struct ata_port *ap = qc->ap;
  2822. struct scatterlist *sg;
  2823. unsigned int idx;
  2824. WARN_ON(qc->__sg == NULL);
  2825. WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
  2826. idx = 0;
  2827. ata_for_each_sg(sg, qc) {
  2828. u32 addr, offset;
  2829. u32 sg_len, len;
  2830. /* determine if physical DMA addr spans 64K boundary.
  2831. * Note h/w doesn't support 64-bit, so we unconditionally
  2832. * truncate dma_addr_t to u32.
  2833. */
  2834. addr = (u32) sg_dma_address(sg);
  2835. sg_len = sg_dma_len(sg);
  2836. while (sg_len) {
  2837. offset = addr & 0xffff;
  2838. len = sg_len;
  2839. if ((offset + sg_len) > 0x10000)
  2840. len = 0x10000 - offset;
  2841. ap->prd[idx].addr = cpu_to_le32(addr);
  2842. ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
  2843. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
  2844. idx++;
  2845. sg_len -= len;
  2846. addr += len;
  2847. }
  2848. }
  2849. if (idx)
  2850. ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
  2851. }
  2852. /**
  2853. * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
  2854. * @qc: Metadata associated with taskfile to check
  2855. *
  2856. * Allow low-level driver to filter ATA PACKET commands, returning
  2857. * a status indicating whether or not it is OK to use DMA for the
  2858. * supplied PACKET command.
  2859. *
  2860. * LOCKING:
  2861. * spin_lock_irqsave(host lock)
  2862. *
  2863. * RETURNS: 0 when ATAPI DMA can be used
  2864. * nonzero otherwise
  2865. */
  2866. int ata_check_atapi_dma(struct ata_queued_cmd *qc)
  2867. {
  2868. struct ata_port *ap = qc->ap;
  2869. int rc = 0; /* Assume ATAPI DMA is OK by default */
  2870. if (ap->ops->check_atapi_dma)
  2871. rc = ap->ops->check_atapi_dma(qc);
  2872. return rc;
  2873. }
  2874. /**
  2875. * ata_qc_prep - Prepare taskfile for submission
  2876. * @qc: Metadata associated with taskfile to be prepared
  2877. *
  2878. * Prepare ATA taskfile for submission.
  2879. *
  2880. * LOCKING:
  2881. * spin_lock_irqsave(host lock)
  2882. */
  2883. void ata_qc_prep(struct ata_queued_cmd *qc)
  2884. {
  2885. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  2886. return;
  2887. ata_fill_sg(qc);
  2888. }
  2889. void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
  2890. /**
  2891. * ata_sg_init_one - Associate command with memory buffer
  2892. * @qc: Command to be associated
  2893. * @buf: Memory buffer
  2894. * @buflen: Length of memory buffer, in bytes.
  2895. *
  2896. * Initialize the data-related elements of queued_cmd @qc
  2897. * to point to a single memory buffer, @buf of byte length @buflen.
  2898. *
  2899. * LOCKING:
  2900. * spin_lock_irqsave(host lock)
  2901. */
  2902. void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
  2903. {
  2904. struct scatterlist *sg;
  2905. qc->flags |= ATA_QCFLAG_SINGLE;
  2906. memset(&qc->sgent, 0, sizeof(qc->sgent));
  2907. qc->__sg = &qc->sgent;
  2908. qc->n_elem = 1;
  2909. qc->orig_n_elem = 1;
  2910. qc->buf_virt = buf;
  2911. qc->nbytes = buflen;
  2912. sg = qc->__sg;
  2913. sg_init_one(sg, buf, buflen);
  2914. }
  2915. /**
  2916. * ata_sg_init - Associate command with scatter-gather table.
  2917. * @qc: Command to be associated
  2918. * @sg: Scatter-gather table.
  2919. * @n_elem: Number of elements in s/g table.
  2920. *
  2921. * Initialize the data-related elements of queued_cmd @qc
  2922. * to point to a scatter-gather table @sg, containing @n_elem
  2923. * elements.
  2924. *
  2925. * LOCKING:
  2926. * spin_lock_irqsave(host lock)
  2927. */
  2928. void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
  2929. unsigned int n_elem)
  2930. {
  2931. qc->flags |= ATA_QCFLAG_SG;
  2932. qc->__sg = sg;
  2933. qc->n_elem = n_elem;
  2934. qc->orig_n_elem = n_elem;
  2935. }
  2936. /**
  2937. * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
  2938. * @qc: Command with memory buffer to be mapped.
  2939. *
  2940. * DMA-map the memory buffer associated with queued_cmd @qc.
  2941. *
  2942. * LOCKING:
  2943. * spin_lock_irqsave(host lock)
  2944. *
  2945. * RETURNS:
  2946. * Zero on success, negative on error.
  2947. */
  2948. static int ata_sg_setup_one(struct ata_queued_cmd *qc)
  2949. {
  2950. struct ata_port *ap = qc->ap;
  2951. int dir = qc->dma_dir;
  2952. struct scatterlist *sg = qc->__sg;
  2953. dma_addr_t dma_address;
  2954. int trim_sg = 0;
  2955. /* we must lengthen transfers to end on a 32-bit boundary */
  2956. qc->pad_len = sg->length & 3;
  2957. if (qc->pad_len) {
  2958. void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  2959. struct scatterlist *psg = &qc->pad_sgent;
  2960. WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
  2961. memset(pad_buf, 0, ATA_DMA_PAD_SZ);
  2962. if (qc->tf.flags & ATA_TFLAG_WRITE)
  2963. memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
  2964. qc->pad_len);
  2965. sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
  2966. sg_dma_len(psg) = ATA_DMA_PAD_SZ;
  2967. /* trim sg */
  2968. sg->length -= qc->pad_len;
  2969. if (sg->length == 0)
  2970. trim_sg = 1;
  2971. DPRINTK("padding done, sg->length=%u pad_len=%u\n",
  2972. sg->length, qc->pad_len);
  2973. }
  2974. if (trim_sg) {
  2975. qc->n_elem--;
  2976. goto skip_map;
  2977. }
  2978. dma_address = dma_map_single(ap->dev, qc->buf_virt,
  2979. sg->length, dir);
  2980. if (dma_mapping_error(dma_address)) {
  2981. /* restore sg */
  2982. sg->length += qc->pad_len;
  2983. return -1;
  2984. }
  2985. sg_dma_address(sg) = dma_address;
  2986. sg_dma_len(sg) = sg->length;
  2987. skip_map:
  2988. DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
  2989. qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  2990. return 0;
  2991. }
  2992. /**
  2993. * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
  2994. * @qc: Command with scatter-gather table to be mapped.
  2995. *
  2996. * DMA-map the scatter-gather table associated with queued_cmd @qc.
  2997. *
  2998. * LOCKING:
  2999. * spin_lock_irqsave(host lock)
  3000. *
  3001. * RETURNS:
  3002. * Zero on success, negative on error.
  3003. *
  3004. */
  3005. static int ata_sg_setup(struct ata_queued_cmd *qc)
  3006. {
  3007. struct ata_port *ap = qc->ap;
  3008. struct scatterlist *sg = qc->__sg;
  3009. struct scatterlist *lsg = &sg[qc->n_elem - 1];
  3010. int n_elem, pre_n_elem, dir, trim_sg = 0;
  3011. VPRINTK("ENTER, ata%u\n", ap->id);
  3012. WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
  3013. /* we must lengthen transfers to end on a 32-bit boundary */
  3014. qc->pad_len = lsg->length & 3;
  3015. if (qc->pad_len) {
  3016. void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  3017. struct scatterlist *psg = &qc->pad_sgent;
  3018. unsigned int offset;
  3019. WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
  3020. memset(pad_buf, 0, ATA_DMA_PAD_SZ);
  3021. /*
  3022. * psg->page/offset are used to copy to-be-written
  3023. * data in this function or read data in ata_sg_clean.
  3024. */
  3025. offset = lsg->offset + lsg->length - qc->pad_len;
  3026. psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
  3027. psg->offset = offset_in_page(offset);
  3028. if (qc->tf.flags & ATA_TFLAG_WRITE) {
  3029. void *addr = kmap_atomic(psg->page, KM_IRQ0);
  3030. memcpy(pad_buf, addr + psg->offset, qc->pad_len);
  3031. kunmap_atomic(addr, KM_IRQ0);
  3032. }
  3033. sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
  3034. sg_dma_len(psg) = ATA_DMA_PAD_SZ;
  3035. /* trim last sg */
  3036. lsg->length -= qc->pad_len;
  3037. if (lsg->length == 0)
  3038. trim_sg = 1;
  3039. DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
  3040. qc->n_elem - 1, lsg->length, qc->pad_len);
  3041. }
  3042. pre_n_elem = qc->n_elem;
  3043. if (trim_sg && pre_n_elem)
  3044. pre_n_elem--;
  3045. if (!pre_n_elem) {
  3046. n_elem = 0;
  3047. goto skip_map;
  3048. }
  3049. dir = qc->dma_dir;
  3050. n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
  3051. if (n_elem < 1) {
  3052. /* restore last sg */
  3053. lsg->length += qc->pad_len;
  3054. return -1;
  3055. }
  3056. DPRINTK("%d sg elements mapped\n", n_elem);
  3057. skip_map:
  3058. qc->n_elem = n_elem;
  3059. return 0;
  3060. }
  3061. /**
  3062. * swap_buf_le16 - swap halves of 16-bit words in place
  3063. * @buf: Buffer to swap
  3064. * @buf_words: Number of 16-bit words in buffer.
  3065. *
  3066. * Swap halves of 16-bit words if needed to convert from
  3067. * little-endian byte order to native cpu byte order, or
  3068. * vice-versa.
  3069. *
  3070. * LOCKING:
  3071. * Inherited from caller.
  3072. */
  3073. void swap_buf_le16(u16 *buf, unsigned int buf_words)
  3074. {
  3075. #ifdef __BIG_ENDIAN
  3076. unsigned int i;
  3077. for (i = 0; i < buf_words; i++)
  3078. buf[i] = le16_to_cpu(buf[i]);
  3079. #endif /* __BIG_ENDIAN */
  3080. }
  3081. /**
  3082. * ata_mmio_data_xfer - Transfer data by MMIO
  3083. * @adev: device for this I/O
  3084. * @buf: data buffer
  3085. * @buflen: buffer length
  3086. * @write_data: read/write
  3087. *
  3088. * Transfer data from/to the device data register by MMIO.
  3089. *
  3090. * LOCKING:
  3091. * Inherited from caller.
  3092. */
  3093. void ata_mmio_data_xfer(struct ata_device *adev, unsigned char *buf,
  3094. unsigned int buflen, int write_data)
  3095. {
  3096. struct ata_port *ap = adev->ap;
  3097. unsigned int i;
  3098. unsigned int words = buflen >> 1;
  3099. u16 *buf16 = (u16 *) buf;
  3100. void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
  3101. /* Transfer multiple of 2 bytes */
  3102. if (write_data) {
  3103. for (i = 0; i < words; i++)
  3104. writew(le16_to_cpu(buf16[i]), mmio);
  3105. } else {
  3106. for (i = 0; i < words; i++)
  3107. buf16[i] = cpu_to_le16(readw(mmio));
  3108. }
  3109. /* Transfer trailing 1 byte, if any. */
  3110. if (unlikely(buflen & 0x01)) {
  3111. u16 align_buf[1] = { 0 };
  3112. unsigned char *trailing_buf = buf + buflen - 1;
  3113. if (write_data) {
  3114. memcpy(align_buf, trailing_buf, 1);
  3115. writew(le16_to_cpu(align_buf[0]), mmio);
  3116. } else {
  3117. align_buf[0] = cpu_to_le16(readw(mmio));
  3118. memcpy(trailing_buf, align_buf, 1);
  3119. }
  3120. }
  3121. }
  3122. /**
  3123. * ata_pio_data_xfer - Transfer data by PIO
  3124. * @adev: device to target
  3125. * @buf: data buffer
  3126. * @buflen: buffer length
  3127. * @write_data: read/write
  3128. *
  3129. * Transfer data from/to the device data register by PIO.
  3130. *
  3131. * LOCKING:
  3132. * Inherited from caller.
  3133. */
  3134. void ata_pio_data_xfer(struct ata_device *adev, unsigned char *buf,
  3135. unsigned int buflen, int write_data)
  3136. {
  3137. struct ata_port *ap = adev->ap;
  3138. unsigned int words = buflen >> 1;
  3139. /* Transfer multiple of 2 bytes */
  3140. if (write_data)
  3141. outsw(ap->ioaddr.data_addr, buf, words);
  3142. else
  3143. insw(ap->ioaddr.data_addr, buf, words);
  3144. /* Transfer trailing 1 byte, if any. */
  3145. if (unlikely(buflen & 0x01)) {
  3146. u16 align_buf[1] = { 0 };
  3147. unsigned char *trailing_buf = buf + buflen - 1;
  3148. if (write_data) {
  3149. memcpy(align_buf, trailing_buf, 1);
  3150. outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
  3151. } else {
  3152. align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
  3153. memcpy(trailing_buf, align_buf, 1);
  3154. }
  3155. }
  3156. }
  3157. /**
  3158. * ata_pio_data_xfer_noirq - Transfer data by PIO
  3159. * @adev: device to target
  3160. * @buf: data buffer
  3161. * @buflen: buffer length
  3162. * @write_data: read/write
  3163. *
  3164. * Transfer data from/to the device data register by PIO. Do the
  3165. * transfer with interrupts disabled.
  3166. *
  3167. * LOCKING:
  3168. * Inherited from caller.
  3169. */
  3170. void ata_pio_data_xfer_noirq(struct ata_device *adev, unsigned char *buf,
  3171. unsigned int buflen, int write_data)
  3172. {
  3173. unsigned long flags;
  3174. local_irq_save(flags);
  3175. ata_pio_data_xfer(adev, buf, buflen, write_data);
  3176. local_irq_restore(flags);
  3177. }
  3178. /**
  3179. * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
  3180. * @qc: Command on going
  3181. *
  3182. * Transfer ATA_SECT_SIZE of data from/to the ATA device.
  3183. *
  3184. * LOCKING:
  3185. * Inherited from caller.
  3186. */
  3187. static void ata_pio_sector(struct ata_queued_cmd *qc)
  3188. {
  3189. int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  3190. struct scatterlist *sg = qc->__sg;
  3191. struct ata_port *ap = qc->ap;
  3192. struct page *page;
  3193. unsigned int offset;
  3194. unsigned char *buf;
  3195. if (qc->cursect == (qc->nsect - 1))
  3196. ap->hsm_task_state = HSM_ST_LAST;
  3197. page = sg[qc->cursg].page;
  3198. offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
  3199. /* get the current page and offset */
  3200. page = nth_page(page, (offset >> PAGE_SHIFT));
  3201. offset %= PAGE_SIZE;
  3202. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  3203. if (PageHighMem(page)) {
  3204. unsigned long flags;
  3205. /* FIXME: use a bounce buffer */
  3206. local_irq_save(flags);
  3207. buf = kmap_atomic(page, KM_IRQ0);
  3208. /* do the actual data transfer */
  3209. ap->ops->data_xfer(qc->dev, buf + offset, ATA_SECT_SIZE, do_write);
  3210. kunmap_atomic(buf, KM_IRQ0);
  3211. local_irq_restore(flags);
  3212. } else {
  3213. buf = page_address(page);
  3214. ap->ops->data_xfer(qc->dev, buf + offset, ATA_SECT_SIZE, do_write);
  3215. }
  3216. qc->cursect++;
  3217. qc->cursg_ofs++;
  3218. if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
  3219. qc->cursg++;
  3220. qc->cursg_ofs = 0;
  3221. }
  3222. }
  3223. /**
  3224. * ata_pio_sectors - Transfer one or many 512-byte sectors.
  3225. * @qc: Command on going
  3226. *
  3227. * Transfer one or many ATA_SECT_SIZE of data from/to the
  3228. * ATA device for the DRQ request.
  3229. *
  3230. * LOCKING:
  3231. * Inherited from caller.
  3232. */
  3233. static void ata_pio_sectors(struct ata_queued_cmd *qc)
  3234. {
  3235. if (is_multi_taskfile(&qc->tf)) {
  3236. /* READ/WRITE MULTIPLE */
  3237. unsigned int nsect;
  3238. WARN_ON(qc->dev->multi_count == 0);
  3239. nsect = min(qc->nsect - qc->cursect, qc->dev->multi_count);
  3240. while (nsect--)
  3241. ata_pio_sector(qc);
  3242. } else
  3243. ata_pio_sector(qc);
  3244. }
  3245. /**
  3246. * atapi_send_cdb - Write CDB bytes to hardware
  3247. * @ap: Port to which ATAPI device is attached.
  3248. * @qc: Taskfile currently active
  3249. *
  3250. * When device has indicated its readiness to accept
  3251. * a CDB, this function is called. Send the CDB.
  3252. *
  3253. * LOCKING:
  3254. * caller.
  3255. */
  3256. static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
  3257. {
  3258. /* send SCSI cdb */
  3259. DPRINTK("send cdb\n");
  3260. WARN_ON(qc->dev->cdb_len < 12);
  3261. ap->ops->data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
  3262. ata_altstatus(ap); /* flush */
  3263. switch (qc->tf.protocol) {
  3264. case ATA_PROT_ATAPI:
  3265. ap->hsm_task_state = HSM_ST;
  3266. break;
  3267. case ATA_PROT_ATAPI_NODATA:
  3268. ap->hsm_task_state = HSM_ST_LAST;
  3269. break;
  3270. case ATA_PROT_ATAPI_DMA:
  3271. ap->hsm_task_state = HSM_ST_LAST;
  3272. /* initiate bmdma */
  3273. ap->ops->bmdma_start(qc);
  3274. break;
  3275. }
  3276. }
  3277. /**
  3278. * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
  3279. * @qc: Command on going
  3280. * @bytes: number of bytes
  3281. *
  3282. * Transfer Transfer data from/to the ATAPI device.
  3283. *
  3284. * LOCKING:
  3285. * Inherited from caller.
  3286. *
  3287. */
  3288. static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
  3289. {
  3290. int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  3291. struct scatterlist *sg = qc->__sg;
  3292. struct ata_port *ap = qc->ap;
  3293. struct page *page;
  3294. unsigned char *buf;
  3295. unsigned int offset, count;
  3296. if (qc->curbytes + bytes >= qc->nbytes)
  3297. ap->hsm_task_state = HSM_ST_LAST;
  3298. next_sg:
  3299. if (unlikely(qc->cursg >= qc->n_elem)) {
  3300. /*
  3301. * The end of qc->sg is reached and the device expects
  3302. * more data to transfer. In order not to overrun qc->sg
  3303. * and fulfill length specified in the byte count register,
  3304. * - for read case, discard trailing data from the device
  3305. * - for write case, padding zero data to the device
  3306. */
  3307. u16 pad_buf[1] = { 0 };
  3308. unsigned int words = bytes >> 1;
  3309. unsigned int i;
  3310. if (words) /* warning if bytes > 1 */
  3311. ata_dev_printk(qc->dev, KERN_WARNING,
  3312. "%u bytes trailing data\n", bytes);
  3313. for (i = 0; i < words; i++)
  3314. ap->ops->data_xfer(qc->dev, (unsigned char*)pad_buf, 2, do_write);
  3315. ap->hsm_task_state = HSM_ST_LAST;
  3316. return;
  3317. }
  3318. sg = &qc->__sg[qc->cursg];
  3319. page = sg->page;
  3320. offset = sg->offset + qc->cursg_ofs;
  3321. /* get the current page and offset */
  3322. page = nth_page(page, (offset >> PAGE_SHIFT));
  3323. offset %= PAGE_SIZE;
  3324. /* don't overrun current sg */
  3325. count = min(sg->length - qc->cursg_ofs, bytes);
  3326. /* don't cross page boundaries */
  3327. count = min(count, (unsigned int)PAGE_SIZE - offset);
  3328. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  3329. if (PageHighMem(page)) {
  3330. unsigned long flags;
  3331. /* FIXME: use bounce buffer */
  3332. local_irq_save(flags);
  3333. buf = kmap_atomic(page, KM_IRQ0);
  3334. /* do the actual data transfer */
  3335. ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
  3336. kunmap_atomic(buf, KM_IRQ0);
  3337. local_irq_restore(flags);
  3338. } else {
  3339. buf = page_address(page);
  3340. ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
  3341. }
  3342. bytes -= count;
  3343. qc->curbytes += count;
  3344. qc->cursg_ofs += count;
  3345. if (qc->cursg_ofs == sg->length) {
  3346. qc->cursg++;
  3347. qc->cursg_ofs = 0;
  3348. }
  3349. if (bytes)
  3350. goto next_sg;
  3351. }
  3352. /**
  3353. * atapi_pio_bytes - Transfer data from/to the ATAPI device.
  3354. * @qc: Command on going
  3355. *
  3356. * Transfer Transfer data from/to the ATAPI device.
  3357. *
  3358. * LOCKING:
  3359. * Inherited from caller.
  3360. */
  3361. static void atapi_pio_bytes(struct ata_queued_cmd *qc)
  3362. {
  3363. struct ata_port *ap = qc->ap;
  3364. struct ata_device *dev = qc->dev;
  3365. unsigned int ireason, bc_lo, bc_hi, bytes;
  3366. int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
  3367. /* Abuse qc->result_tf for temp storage of intermediate TF
  3368. * here to save some kernel stack usage.
  3369. * For normal completion, qc->result_tf is not relevant. For
  3370. * error, qc->result_tf is later overwritten by ata_qc_complete().
  3371. * So, the correctness of qc->result_tf is not affected.
  3372. */
  3373. ap->ops->tf_read(ap, &qc->result_tf);
  3374. ireason = qc->result_tf.nsect;
  3375. bc_lo = qc->result_tf.lbam;
  3376. bc_hi = qc->result_tf.lbah;
  3377. bytes = (bc_hi << 8) | bc_lo;
  3378. /* shall be cleared to zero, indicating xfer of data */
  3379. if (ireason & (1 << 0))
  3380. goto err_out;
  3381. /* make sure transfer direction matches expected */
  3382. i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
  3383. if (do_write != i_write)
  3384. goto err_out;
  3385. VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
  3386. __atapi_pio_bytes(qc, bytes);
  3387. return;
  3388. err_out:
  3389. ata_dev_printk(dev, KERN_INFO, "ATAPI check failed\n");
  3390. qc->err_mask |= AC_ERR_HSM;
  3391. ap->hsm_task_state = HSM_ST_ERR;
  3392. }
  3393. /**
  3394. * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
  3395. * @ap: the target ata_port
  3396. * @qc: qc on going
  3397. *
  3398. * RETURNS:
  3399. * 1 if ok in workqueue, 0 otherwise.
  3400. */
  3401. static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
  3402. {
  3403. if (qc->tf.flags & ATA_TFLAG_POLLING)
  3404. return 1;
  3405. if (ap->hsm_task_state == HSM_ST_FIRST) {
  3406. if (qc->tf.protocol == ATA_PROT_PIO &&
  3407. (qc->tf.flags & ATA_TFLAG_WRITE))
  3408. return 1;
  3409. if (is_atapi_taskfile(&qc->tf) &&
  3410. !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  3411. return 1;
  3412. }
  3413. return 0;
  3414. }
  3415. /**
  3416. * ata_hsm_qc_complete - finish a qc running on standard HSM
  3417. * @qc: Command to complete
  3418. * @in_wq: 1 if called from workqueue, 0 otherwise
  3419. *
  3420. * Finish @qc which is running on standard HSM.
  3421. *
  3422. * LOCKING:
  3423. * If @in_wq is zero, spin_lock_irqsave(host lock).
  3424. * Otherwise, none on entry and grabs host lock.
  3425. */
  3426. static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
  3427. {
  3428. struct ata_port *ap = qc->ap;
  3429. unsigned long flags;
  3430. if (ap->ops->error_handler) {
  3431. if (in_wq) {
  3432. spin_lock_irqsave(ap->lock, flags);
  3433. /* EH might have kicked in while host lock is
  3434. * released.
  3435. */
  3436. qc = ata_qc_from_tag(ap, qc->tag);
  3437. if (qc) {
  3438. if (likely(!(qc->err_mask & AC_ERR_HSM))) {
  3439. ata_irq_on(ap);
  3440. ata_qc_complete(qc);
  3441. } else
  3442. ata_port_freeze(ap);
  3443. }
  3444. spin_unlock_irqrestore(ap->lock, flags);
  3445. } else {
  3446. if (likely(!(qc->err_mask & AC_ERR_HSM)))
  3447. ata_qc_complete(qc);
  3448. else
  3449. ata_port_freeze(ap);
  3450. }
  3451. } else {
  3452. if (in_wq) {
  3453. spin_lock_irqsave(ap->lock, flags);
  3454. ata_irq_on(ap);
  3455. ata_qc_complete(qc);
  3456. spin_unlock_irqrestore(ap->lock, flags);
  3457. } else
  3458. ata_qc_complete(qc);
  3459. }
  3460. ata_altstatus(ap); /* flush */
  3461. }
  3462. /**
  3463. * ata_hsm_move - move the HSM to the next state.
  3464. * @ap: the target ata_port
  3465. * @qc: qc on going
  3466. * @status: current device status
  3467. * @in_wq: 1 if called from workqueue, 0 otherwise
  3468. *
  3469. * RETURNS:
  3470. * 1 when poll next status needed, 0 otherwise.
  3471. */
  3472. int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
  3473. u8 status, int in_wq)
  3474. {
  3475. unsigned long flags = 0;
  3476. int poll_next;
  3477. WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
  3478. /* Make sure ata_qc_issue_prot() does not throw things
  3479. * like DMA polling into the workqueue. Notice that
  3480. * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
  3481. */
  3482. WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
  3483. fsm_start:
  3484. DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
  3485. ap->id, qc->tf.protocol, ap->hsm_task_state, status);
  3486. switch (ap->hsm_task_state) {
  3487. case HSM_ST_FIRST:
  3488. /* Send first data block or PACKET CDB */
  3489. /* If polling, we will stay in the work queue after
  3490. * sending the data. Otherwise, interrupt handler
  3491. * takes over after sending the data.
  3492. */
  3493. poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
  3494. /* check device status */
  3495. if (unlikely((status & ATA_DRQ) == 0)) {
  3496. /* handle BSY=0, DRQ=0 as error */
  3497. if (likely(status & (ATA_ERR | ATA_DF)))
  3498. /* device stops HSM for abort/error */
  3499. qc->err_mask |= AC_ERR_DEV;
  3500. else
  3501. /* HSM violation. Let EH handle this */
  3502. qc->err_mask |= AC_ERR_HSM;
  3503. ap->hsm_task_state = HSM_ST_ERR;
  3504. goto fsm_start;
  3505. }
  3506. /* Device should not ask for data transfer (DRQ=1)
  3507. * when it finds something wrong.
  3508. * We ignore DRQ here and stop the HSM by
  3509. * changing hsm_task_state to HSM_ST_ERR and
  3510. * let the EH abort the command or reset the device.
  3511. */
  3512. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  3513. printk(KERN_WARNING "ata%d: DRQ=1 with device error, dev_stat 0x%X\n",
  3514. ap->id, status);
  3515. qc->err_mask |= AC_ERR_HSM;
  3516. ap->hsm_task_state = HSM_ST_ERR;
  3517. goto fsm_start;
  3518. }
  3519. /* Send the CDB (atapi) or the first data block (ata pio out).
  3520. * During the state transition, interrupt handler shouldn't
  3521. * be invoked before the data transfer is complete and
  3522. * hsm_task_state is changed. Hence, the following locking.
  3523. */
  3524. if (in_wq)
  3525. spin_lock_irqsave(ap->lock, flags);
  3526. if (qc->tf.protocol == ATA_PROT_PIO) {
  3527. /* PIO data out protocol.
  3528. * send first data block.
  3529. */
  3530. /* ata_pio_sectors() might change the state
  3531. * to HSM_ST_LAST. so, the state is changed here
  3532. * before ata_pio_sectors().
  3533. */
  3534. ap->hsm_task_state = HSM_ST;
  3535. ata_pio_sectors(qc);
  3536. ata_altstatus(ap); /* flush */
  3537. } else
  3538. /* send CDB */
  3539. atapi_send_cdb(ap, qc);
  3540. if (in_wq)
  3541. spin_unlock_irqrestore(ap->lock, flags);
  3542. /* if polling, ata_pio_task() handles the rest.
  3543. * otherwise, interrupt handler takes over from here.
  3544. */
  3545. break;
  3546. case HSM_ST:
  3547. /* complete command or read/write the data register */
  3548. if (qc->tf.protocol == ATA_PROT_ATAPI) {
  3549. /* ATAPI PIO protocol */
  3550. if ((status & ATA_DRQ) == 0) {
  3551. /* No more data to transfer or device error.
  3552. * Device error will be tagged in HSM_ST_LAST.
  3553. */
  3554. ap->hsm_task_state = HSM_ST_LAST;
  3555. goto fsm_start;
  3556. }
  3557. /* Device should not ask for data transfer (DRQ=1)
  3558. * when it finds something wrong.
  3559. * We ignore DRQ here and stop the HSM by
  3560. * changing hsm_task_state to HSM_ST_ERR and
  3561. * let the EH abort the command or reset the device.
  3562. */
  3563. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  3564. printk(KERN_WARNING "ata%d: DRQ=1 with device error, dev_stat 0x%X\n",
  3565. ap->id, status);
  3566. qc->err_mask |= AC_ERR_HSM;
  3567. ap->hsm_task_state = HSM_ST_ERR;
  3568. goto fsm_start;
  3569. }
  3570. atapi_pio_bytes(qc);
  3571. if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
  3572. /* bad ireason reported by device */
  3573. goto fsm_start;
  3574. } else {
  3575. /* ATA PIO protocol */
  3576. if (unlikely((status & ATA_DRQ) == 0)) {
  3577. /* handle BSY=0, DRQ=0 as error */
  3578. if (likely(status & (ATA_ERR | ATA_DF)))
  3579. /* device stops HSM for abort/error */
  3580. qc->err_mask |= AC_ERR_DEV;
  3581. else
  3582. /* HSM violation. Let EH handle this */
  3583. qc->err_mask |= AC_ERR_HSM;
  3584. ap->hsm_task_state = HSM_ST_ERR;
  3585. goto fsm_start;
  3586. }
  3587. /* For PIO reads, some devices may ask for
  3588. * data transfer (DRQ=1) alone with ERR=1.
  3589. * We respect DRQ here and transfer one
  3590. * block of junk data before changing the
  3591. * hsm_task_state to HSM_ST_ERR.
  3592. *
  3593. * For PIO writes, ERR=1 DRQ=1 doesn't make
  3594. * sense since the data block has been
  3595. * transferred to the device.
  3596. */
  3597. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  3598. /* data might be corrputed */
  3599. qc->err_mask |= AC_ERR_DEV;
  3600. if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
  3601. ata_pio_sectors(qc);
  3602. ata_altstatus(ap);
  3603. status = ata_wait_idle(ap);
  3604. }
  3605. if (status & (ATA_BUSY | ATA_DRQ))
  3606. qc->err_mask |= AC_ERR_HSM;
  3607. /* ata_pio_sectors() might change the
  3608. * state to HSM_ST_LAST. so, the state
  3609. * is changed after ata_pio_sectors().
  3610. */
  3611. ap->hsm_task_state = HSM_ST_ERR;
  3612. goto fsm_start;
  3613. }
  3614. ata_pio_sectors(qc);
  3615. if (ap->hsm_task_state == HSM_ST_LAST &&
  3616. (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
  3617. /* all data read */
  3618. ata_altstatus(ap);
  3619. status = ata_wait_idle(ap);
  3620. goto fsm_start;
  3621. }
  3622. }
  3623. ata_altstatus(ap); /* flush */
  3624. poll_next = 1;
  3625. break;
  3626. case HSM_ST_LAST:
  3627. if (unlikely(!ata_ok(status))) {
  3628. qc->err_mask |= __ac_err_mask(status);
  3629. ap->hsm_task_state = HSM_ST_ERR;
  3630. goto fsm_start;
  3631. }
  3632. /* no more data to transfer */
  3633. DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
  3634. ap->id, qc->dev->devno, status);
  3635. WARN_ON(qc->err_mask);
  3636. ap->hsm_task_state = HSM_ST_IDLE;
  3637. /* complete taskfile transaction */
  3638. ata_hsm_qc_complete(qc, in_wq);
  3639. poll_next = 0;
  3640. break;
  3641. case HSM_ST_ERR:
  3642. /* make sure qc->err_mask is available to
  3643. * know what's wrong and recover
  3644. */
  3645. WARN_ON(qc->err_mask == 0);
  3646. ap->hsm_task_state = HSM_ST_IDLE;
  3647. /* complete taskfile transaction */
  3648. ata_hsm_qc_complete(qc, in_wq);
  3649. poll_next = 0;
  3650. break;
  3651. default:
  3652. poll_next = 0;
  3653. BUG();
  3654. }
  3655. return poll_next;
  3656. }
  3657. static void ata_pio_task(void *_data)
  3658. {
  3659. struct ata_queued_cmd *qc = _data;
  3660. struct ata_port *ap = qc->ap;
  3661. u8 status;
  3662. int poll_next;
  3663. fsm_start:
  3664. WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
  3665. /*
  3666. * This is purely heuristic. This is a fast path.
  3667. * Sometimes when we enter, BSY will be cleared in
  3668. * a chk-status or two. If not, the drive is probably seeking
  3669. * or something. Snooze for a couple msecs, then
  3670. * chk-status again. If still busy, queue delayed work.
  3671. */
  3672. status = ata_busy_wait(ap, ATA_BUSY, 5);
  3673. if (status & ATA_BUSY) {
  3674. msleep(2);
  3675. status = ata_busy_wait(ap, ATA_BUSY, 10);
  3676. if (status & ATA_BUSY) {
  3677. ata_port_queue_task(ap, ata_pio_task, qc, ATA_SHORT_PAUSE);
  3678. return;
  3679. }
  3680. }
  3681. /* move the HSM */
  3682. poll_next = ata_hsm_move(ap, qc, status, 1);
  3683. /* another command or interrupt handler
  3684. * may be running at this point.
  3685. */
  3686. if (poll_next)
  3687. goto fsm_start;
  3688. }
  3689. /**
  3690. * ata_qc_new - Request an available ATA command, for queueing
  3691. * @ap: Port associated with device @dev
  3692. * @dev: Device from whom we request an available command structure
  3693. *
  3694. * LOCKING:
  3695. * None.
  3696. */
  3697. static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
  3698. {
  3699. struct ata_queued_cmd *qc = NULL;
  3700. unsigned int i;
  3701. /* no command while frozen */
  3702. if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
  3703. return NULL;
  3704. /* the last tag is reserved for internal command. */
  3705. for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
  3706. if (!test_and_set_bit(i, &ap->qc_allocated)) {
  3707. qc = __ata_qc_from_tag(ap, i);
  3708. break;
  3709. }
  3710. if (qc)
  3711. qc->tag = i;
  3712. return qc;
  3713. }
  3714. /**
  3715. * ata_qc_new_init - Request an available ATA command, and initialize it
  3716. * @dev: Device from whom we request an available command structure
  3717. *
  3718. * LOCKING:
  3719. * None.
  3720. */
  3721. struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
  3722. {
  3723. struct ata_port *ap = dev->ap;
  3724. struct ata_queued_cmd *qc;
  3725. qc = ata_qc_new(ap);
  3726. if (qc) {
  3727. qc->scsicmd = NULL;
  3728. qc->ap = ap;
  3729. qc->dev = dev;
  3730. ata_qc_reinit(qc);
  3731. }
  3732. return qc;
  3733. }
  3734. /**
  3735. * ata_qc_free - free unused ata_queued_cmd
  3736. * @qc: Command to complete
  3737. *
  3738. * Designed to free unused ata_queued_cmd object
  3739. * in case something prevents using it.
  3740. *
  3741. * LOCKING:
  3742. * spin_lock_irqsave(host lock)
  3743. */
  3744. void ata_qc_free(struct ata_queued_cmd *qc)
  3745. {
  3746. struct ata_port *ap = qc->ap;
  3747. unsigned int tag;
  3748. WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
  3749. qc->flags = 0;
  3750. tag = qc->tag;
  3751. if (likely(ata_tag_valid(tag))) {
  3752. qc->tag = ATA_TAG_POISON;
  3753. clear_bit(tag, &ap->qc_allocated);
  3754. }
  3755. }
  3756. void __ata_qc_complete(struct ata_queued_cmd *qc)
  3757. {
  3758. struct ata_port *ap = qc->ap;
  3759. WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
  3760. WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
  3761. if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
  3762. ata_sg_clean(qc);
  3763. /* command should be marked inactive atomically with qc completion */
  3764. if (qc->tf.protocol == ATA_PROT_NCQ)
  3765. ap->sactive &= ~(1 << qc->tag);
  3766. else
  3767. ap->active_tag = ATA_TAG_POISON;
  3768. /* atapi: mark qc as inactive to prevent the interrupt handler
  3769. * from completing the command twice later, before the error handler
  3770. * is called. (when rc != 0 and atapi request sense is needed)
  3771. */
  3772. qc->flags &= ~ATA_QCFLAG_ACTIVE;
  3773. ap->qc_active &= ~(1 << qc->tag);
  3774. /* call completion callback */
  3775. qc->complete_fn(qc);
  3776. }
  3777. /**
  3778. * ata_qc_complete - Complete an active ATA command
  3779. * @qc: Command to complete
  3780. * @err_mask: ATA Status register contents
  3781. *
  3782. * Indicate to the mid and upper layers that an ATA
  3783. * command has completed, with either an ok or not-ok status.
  3784. *
  3785. * LOCKING:
  3786. * spin_lock_irqsave(host lock)
  3787. */
  3788. void ata_qc_complete(struct ata_queued_cmd *qc)
  3789. {
  3790. struct ata_port *ap = qc->ap;
  3791. /* XXX: New EH and old EH use different mechanisms to
  3792. * synchronize EH with regular execution path.
  3793. *
  3794. * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
  3795. * Normal execution path is responsible for not accessing a
  3796. * failed qc. libata core enforces the rule by returning NULL
  3797. * from ata_qc_from_tag() for failed qcs.
  3798. *
  3799. * Old EH depends on ata_qc_complete() nullifying completion
  3800. * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
  3801. * not synchronize with interrupt handler. Only PIO task is
  3802. * taken care of.
  3803. */
  3804. if (ap->ops->error_handler) {
  3805. WARN_ON(ap->pflags & ATA_PFLAG_FROZEN);
  3806. if (unlikely(qc->err_mask))
  3807. qc->flags |= ATA_QCFLAG_FAILED;
  3808. if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
  3809. if (!ata_tag_internal(qc->tag)) {
  3810. /* always fill result TF for failed qc */
  3811. ap->ops->tf_read(ap, &qc->result_tf);
  3812. ata_qc_schedule_eh(qc);
  3813. return;
  3814. }
  3815. }
  3816. /* read result TF if requested */
  3817. if (qc->flags & ATA_QCFLAG_RESULT_TF)
  3818. ap->ops->tf_read(ap, &qc->result_tf);
  3819. __ata_qc_complete(qc);
  3820. } else {
  3821. if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
  3822. return;
  3823. /* read result TF if failed or requested */
  3824. if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
  3825. ap->ops->tf_read(ap, &qc->result_tf);
  3826. __ata_qc_complete(qc);
  3827. }
  3828. }
  3829. /**
  3830. * ata_qc_complete_multiple - Complete multiple qcs successfully
  3831. * @ap: port in question
  3832. * @qc_active: new qc_active mask
  3833. * @finish_qc: LLDD callback invoked before completing a qc
  3834. *
  3835. * Complete in-flight commands. This functions is meant to be
  3836. * called from low-level driver's interrupt routine to complete
  3837. * requests normally. ap->qc_active and @qc_active is compared
  3838. * and commands are completed accordingly.
  3839. *
  3840. * LOCKING:
  3841. * spin_lock_irqsave(host lock)
  3842. *
  3843. * RETURNS:
  3844. * Number of completed commands on success, -errno otherwise.
  3845. */
  3846. int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active,
  3847. void (*finish_qc)(struct ata_queued_cmd *))
  3848. {
  3849. int nr_done = 0;
  3850. u32 done_mask;
  3851. int i;
  3852. done_mask = ap->qc_active ^ qc_active;
  3853. if (unlikely(done_mask & qc_active)) {
  3854. ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
  3855. "(%08x->%08x)\n", ap->qc_active, qc_active);
  3856. return -EINVAL;
  3857. }
  3858. for (i = 0; i < ATA_MAX_QUEUE; i++) {
  3859. struct ata_queued_cmd *qc;
  3860. if (!(done_mask & (1 << i)))
  3861. continue;
  3862. if ((qc = ata_qc_from_tag(ap, i))) {
  3863. if (finish_qc)
  3864. finish_qc(qc);
  3865. ata_qc_complete(qc);
  3866. nr_done++;
  3867. }
  3868. }
  3869. return nr_done;
  3870. }
  3871. static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
  3872. {
  3873. struct ata_port *ap = qc->ap;
  3874. switch (qc->tf.protocol) {
  3875. case ATA_PROT_NCQ:
  3876. case ATA_PROT_DMA:
  3877. case ATA_PROT_ATAPI_DMA:
  3878. return 1;
  3879. case ATA_PROT_ATAPI:
  3880. case ATA_PROT_PIO:
  3881. if (ap->flags & ATA_FLAG_PIO_DMA)
  3882. return 1;
  3883. /* fall through */
  3884. default:
  3885. return 0;
  3886. }
  3887. /* never reached */
  3888. }
  3889. /**
  3890. * ata_qc_issue - issue taskfile to device
  3891. * @qc: command to issue to device
  3892. *
  3893. * Prepare an ATA command to submission to device.
  3894. * This includes mapping the data into a DMA-able
  3895. * area, filling in the S/G table, and finally
  3896. * writing the taskfile to hardware, starting the command.
  3897. *
  3898. * LOCKING:
  3899. * spin_lock_irqsave(host lock)
  3900. */
  3901. void ata_qc_issue(struct ata_queued_cmd *qc)
  3902. {
  3903. struct ata_port *ap = qc->ap;
  3904. /* Make sure only one non-NCQ command is outstanding. The
  3905. * check is skipped for old EH because it reuses active qc to
  3906. * request ATAPI sense.
  3907. */
  3908. WARN_ON(ap->ops->error_handler && ata_tag_valid(ap->active_tag));
  3909. if (qc->tf.protocol == ATA_PROT_NCQ) {
  3910. WARN_ON(ap->sactive & (1 << qc->tag));
  3911. ap->sactive |= 1 << qc->tag;
  3912. } else {
  3913. WARN_ON(ap->sactive);
  3914. ap->active_tag = qc->tag;
  3915. }
  3916. qc->flags |= ATA_QCFLAG_ACTIVE;
  3917. ap->qc_active |= 1 << qc->tag;
  3918. if (ata_should_dma_map(qc)) {
  3919. if (qc->flags & ATA_QCFLAG_SG) {
  3920. if (ata_sg_setup(qc))
  3921. goto sg_err;
  3922. } else if (qc->flags & ATA_QCFLAG_SINGLE) {
  3923. if (ata_sg_setup_one(qc))
  3924. goto sg_err;
  3925. }
  3926. } else {
  3927. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  3928. }
  3929. ap->ops->qc_prep(qc);
  3930. qc->err_mask |= ap->ops->qc_issue(qc);
  3931. if (unlikely(qc->err_mask))
  3932. goto err;
  3933. return;
  3934. sg_err:
  3935. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  3936. qc->err_mask |= AC_ERR_SYSTEM;
  3937. err:
  3938. ata_qc_complete(qc);
  3939. }
  3940. /**
  3941. * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
  3942. * @qc: command to issue to device
  3943. *
  3944. * Using various libata functions and hooks, this function
  3945. * starts an ATA command. ATA commands are grouped into
  3946. * classes called "protocols", and issuing each type of protocol
  3947. * is slightly different.
  3948. *
  3949. * May be used as the qc_issue() entry in ata_port_operations.
  3950. *
  3951. * LOCKING:
  3952. * spin_lock_irqsave(host lock)
  3953. *
  3954. * RETURNS:
  3955. * Zero on success, AC_ERR_* mask on failure
  3956. */
  3957. unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
  3958. {
  3959. struct ata_port *ap = qc->ap;
  3960. /* Use polling pio if the LLD doesn't handle
  3961. * interrupt driven pio and atapi CDB interrupt.
  3962. */
  3963. if (ap->flags & ATA_FLAG_PIO_POLLING) {
  3964. switch (qc->tf.protocol) {
  3965. case ATA_PROT_PIO:
  3966. case ATA_PROT_ATAPI:
  3967. case ATA_PROT_ATAPI_NODATA:
  3968. qc->tf.flags |= ATA_TFLAG_POLLING;
  3969. break;
  3970. case ATA_PROT_ATAPI_DMA:
  3971. if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
  3972. /* see ata_dma_blacklisted() */
  3973. BUG();
  3974. break;
  3975. default:
  3976. break;
  3977. }
  3978. }
  3979. /* select the device */
  3980. ata_dev_select(ap, qc->dev->devno, 1, 0);
  3981. /* start the command */
  3982. switch (qc->tf.protocol) {
  3983. case ATA_PROT_NODATA:
  3984. if (qc->tf.flags & ATA_TFLAG_POLLING)
  3985. ata_qc_set_polling(qc);
  3986. ata_tf_to_host(ap, &qc->tf);
  3987. ap->hsm_task_state = HSM_ST_LAST;
  3988. if (qc->tf.flags & ATA_TFLAG_POLLING)
  3989. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  3990. break;
  3991. case ATA_PROT_DMA:
  3992. WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
  3993. ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
  3994. ap->ops->bmdma_setup(qc); /* set up bmdma */
  3995. ap->ops->bmdma_start(qc); /* initiate bmdma */
  3996. ap->hsm_task_state = HSM_ST_LAST;
  3997. break;
  3998. case ATA_PROT_PIO:
  3999. if (qc->tf.flags & ATA_TFLAG_POLLING)
  4000. ata_qc_set_polling(qc);
  4001. ata_tf_to_host(ap, &qc->tf);
  4002. if (qc->tf.flags & ATA_TFLAG_WRITE) {
  4003. /* PIO data out protocol */
  4004. ap->hsm_task_state = HSM_ST_FIRST;
  4005. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  4006. /* always send first data block using
  4007. * the ata_pio_task() codepath.
  4008. */
  4009. } else {
  4010. /* PIO data in protocol */
  4011. ap->hsm_task_state = HSM_ST;
  4012. if (qc->tf.flags & ATA_TFLAG_POLLING)
  4013. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  4014. /* if polling, ata_pio_task() handles the rest.
  4015. * otherwise, interrupt handler takes over from here.
  4016. */
  4017. }
  4018. break;
  4019. case ATA_PROT_ATAPI:
  4020. case ATA_PROT_ATAPI_NODATA:
  4021. if (qc->tf.flags & ATA_TFLAG_POLLING)
  4022. ata_qc_set_polling(qc);
  4023. ata_tf_to_host(ap, &qc->tf);
  4024. ap->hsm_task_state = HSM_ST_FIRST;
  4025. /* send cdb by polling if no cdb interrupt */
  4026. if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
  4027. (qc->tf.flags & ATA_TFLAG_POLLING))
  4028. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  4029. break;
  4030. case ATA_PROT_ATAPI_DMA:
  4031. WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
  4032. ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
  4033. ap->ops->bmdma_setup(qc); /* set up bmdma */
  4034. ap->hsm_task_state = HSM_ST_FIRST;
  4035. /* send cdb by polling if no cdb interrupt */
  4036. if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  4037. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  4038. break;
  4039. default:
  4040. WARN_ON(1);
  4041. return AC_ERR_SYSTEM;
  4042. }
  4043. return 0;
  4044. }
  4045. /**
  4046. * ata_host_intr - Handle host interrupt for given (port, task)
  4047. * @ap: Port on which interrupt arrived (possibly...)
  4048. * @qc: Taskfile currently active in engine
  4049. *
  4050. * Handle host interrupt for given queued command. Currently,
  4051. * only DMA interrupts are handled. All other commands are
  4052. * handled via polling with interrupts disabled (nIEN bit).
  4053. *
  4054. * LOCKING:
  4055. * spin_lock_irqsave(host lock)
  4056. *
  4057. * RETURNS:
  4058. * One if interrupt was handled, zero if not (shared irq).
  4059. */
  4060. inline unsigned int ata_host_intr (struct ata_port *ap,
  4061. struct ata_queued_cmd *qc)
  4062. {
  4063. u8 status, host_stat = 0;
  4064. VPRINTK("ata%u: protocol %d task_state %d\n",
  4065. ap->id, qc->tf.protocol, ap->hsm_task_state);
  4066. /* Check whether we are expecting interrupt in this state */
  4067. switch (ap->hsm_task_state) {
  4068. case HSM_ST_FIRST:
  4069. /* Some pre-ATAPI-4 devices assert INTRQ
  4070. * at this state when ready to receive CDB.
  4071. */
  4072. /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
  4073. * The flag was turned on only for atapi devices.
  4074. * No need to check is_atapi_taskfile(&qc->tf) again.
  4075. */
  4076. if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  4077. goto idle_irq;
  4078. break;
  4079. case HSM_ST_LAST:
  4080. if (qc->tf.protocol == ATA_PROT_DMA ||
  4081. qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
  4082. /* check status of DMA engine */
  4083. host_stat = ap->ops->bmdma_status(ap);
  4084. VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
  4085. /* if it's not our irq... */
  4086. if (!(host_stat & ATA_DMA_INTR))
  4087. goto idle_irq;
  4088. /* before we do anything else, clear DMA-Start bit */
  4089. ap->ops->bmdma_stop(qc);
  4090. if (unlikely(host_stat & ATA_DMA_ERR)) {
  4091. /* error when transfering data to/from memory */
  4092. qc->err_mask |= AC_ERR_HOST_BUS;
  4093. ap->hsm_task_state = HSM_ST_ERR;
  4094. }
  4095. }
  4096. break;
  4097. case HSM_ST:
  4098. break;
  4099. default:
  4100. goto idle_irq;
  4101. }
  4102. /* check altstatus */
  4103. status = ata_altstatus(ap);
  4104. if (status & ATA_BUSY)
  4105. goto idle_irq;
  4106. /* check main status, clearing INTRQ */
  4107. status = ata_chk_status(ap);
  4108. if (unlikely(status & ATA_BUSY))
  4109. goto idle_irq;
  4110. /* ack bmdma irq events */
  4111. ap->ops->irq_clear(ap);
  4112. ata_hsm_move(ap, qc, status, 0);
  4113. return 1; /* irq handled */
  4114. idle_irq:
  4115. ap->stats.idle_irq++;
  4116. #ifdef ATA_IRQ_TRAP
  4117. if ((ap->stats.idle_irq % 1000) == 0) {
  4118. ata_irq_ack(ap, 0); /* debug trap */
  4119. ata_port_printk(ap, KERN_WARNING, "irq trap\n");
  4120. return 1;
  4121. }
  4122. #endif
  4123. return 0; /* irq not handled */
  4124. }
  4125. /**
  4126. * ata_interrupt - Default ATA host interrupt handler
  4127. * @irq: irq line (unused)
  4128. * @dev_instance: pointer to our ata_host information structure
  4129. *
  4130. * Default interrupt handler for PCI IDE devices. Calls
  4131. * ata_host_intr() for each port that is not disabled.
  4132. *
  4133. * LOCKING:
  4134. * Obtains host lock during operation.
  4135. *
  4136. * RETURNS:
  4137. * IRQ_NONE or IRQ_HANDLED.
  4138. */
  4139. irqreturn_t ata_interrupt (int irq, void *dev_instance)
  4140. {
  4141. struct ata_host *host = dev_instance;
  4142. unsigned int i;
  4143. unsigned int handled = 0;
  4144. unsigned long flags;
  4145. /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
  4146. spin_lock_irqsave(&host->lock, flags);
  4147. for (i = 0; i < host->n_ports; i++) {
  4148. struct ata_port *ap;
  4149. ap = host->ports[i];
  4150. if (ap &&
  4151. !(ap->flags & ATA_FLAG_DISABLED)) {
  4152. struct ata_queued_cmd *qc;
  4153. qc = ata_qc_from_tag(ap, ap->active_tag);
  4154. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
  4155. (qc->flags & ATA_QCFLAG_ACTIVE))
  4156. handled |= ata_host_intr(ap, qc);
  4157. }
  4158. }
  4159. spin_unlock_irqrestore(&host->lock, flags);
  4160. return IRQ_RETVAL(handled);
  4161. }
  4162. /**
  4163. * sata_scr_valid - test whether SCRs are accessible
  4164. * @ap: ATA port to test SCR accessibility for
  4165. *
  4166. * Test whether SCRs are accessible for @ap.
  4167. *
  4168. * LOCKING:
  4169. * None.
  4170. *
  4171. * RETURNS:
  4172. * 1 if SCRs are accessible, 0 otherwise.
  4173. */
  4174. int sata_scr_valid(struct ata_port *ap)
  4175. {
  4176. return ap->cbl == ATA_CBL_SATA && ap->ops->scr_read;
  4177. }
  4178. /**
  4179. * sata_scr_read - read SCR register of the specified port
  4180. * @ap: ATA port to read SCR for
  4181. * @reg: SCR to read
  4182. * @val: Place to store read value
  4183. *
  4184. * Read SCR register @reg of @ap into *@val. This function is
  4185. * guaranteed to succeed if the cable type of the port is SATA
  4186. * and the port implements ->scr_read.
  4187. *
  4188. * LOCKING:
  4189. * None.
  4190. *
  4191. * RETURNS:
  4192. * 0 on success, negative errno on failure.
  4193. */
  4194. int sata_scr_read(struct ata_port *ap, int reg, u32 *val)
  4195. {
  4196. if (sata_scr_valid(ap)) {
  4197. *val = ap->ops->scr_read(ap, reg);
  4198. return 0;
  4199. }
  4200. return -EOPNOTSUPP;
  4201. }
  4202. /**
  4203. * sata_scr_write - write SCR register of the specified port
  4204. * @ap: ATA port to write SCR for
  4205. * @reg: SCR to write
  4206. * @val: value to write
  4207. *
  4208. * Write @val to SCR register @reg of @ap. This function is
  4209. * guaranteed to succeed if the cable type of the port is SATA
  4210. * and the port implements ->scr_read.
  4211. *
  4212. * LOCKING:
  4213. * None.
  4214. *
  4215. * RETURNS:
  4216. * 0 on success, negative errno on failure.
  4217. */
  4218. int sata_scr_write(struct ata_port *ap, int reg, u32 val)
  4219. {
  4220. if (sata_scr_valid(ap)) {
  4221. ap->ops->scr_write(ap, reg, val);
  4222. return 0;
  4223. }
  4224. return -EOPNOTSUPP;
  4225. }
  4226. /**
  4227. * sata_scr_write_flush - write SCR register of the specified port and flush
  4228. * @ap: ATA port to write SCR for
  4229. * @reg: SCR to write
  4230. * @val: value to write
  4231. *
  4232. * This function is identical to sata_scr_write() except that this
  4233. * function performs flush after writing to the register.
  4234. *
  4235. * LOCKING:
  4236. * None.
  4237. *
  4238. * RETURNS:
  4239. * 0 on success, negative errno on failure.
  4240. */
  4241. int sata_scr_write_flush(struct ata_port *ap, int reg, u32 val)
  4242. {
  4243. if (sata_scr_valid(ap)) {
  4244. ap->ops->scr_write(ap, reg, val);
  4245. ap->ops->scr_read(ap, reg);
  4246. return 0;
  4247. }
  4248. return -EOPNOTSUPP;
  4249. }
  4250. /**
  4251. * ata_port_online - test whether the given port is online
  4252. * @ap: ATA port to test
  4253. *
  4254. * Test whether @ap is online. Note that this function returns 0
  4255. * if online status of @ap cannot be obtained, so
  4256. * ata_port_online(ap) != !ata_port_offline(ap).
  4257. *
  4258. * LOCKING:
  4259. * None.
  4260. *
  4261. * RETURNS:
  4262. * 1 if the port online status is available and online.
  4263. */
  4264. int ata_port_online(struct ata_port *ap)
  4265. {
  4266. u32 sstatus;
  4267. if (!sata_scr_read(ap, SCR_STATUS, &sstatus) && (sstatus & 0xf) == 0x3)
  4268. return 1;
  4269. return 0;
  4270. }
  4271. /**
  4272. * ata_port_offline - test whether the given port is offline
  4273. * @ap: ATA port to test
  4274. *
  4275. * Test whether @ap is offline. Note that this function returns
  4276. * 0 if offline status of @ap cannot be obtained, so
  4277. * ata_port_online(ap) != !ata_port_offline(ap).
  4278. *
  4279. * LOCKING:
  4280. * None.
  4281. *
  4282. * RETURNS:
  4283. * 1 if the port offline status is available and offline.
  4284. */
  4285. int ata_port_offline(struct ata_port *ap)
  4286. {
  4287. u32 sstatus;
  4288. if (!sata_scr_read(ap, SCR_STATUS, &sstatus) && (sstatus & 0xf) != 0x3)
  4289. return 1;
  4290. return 0;
  4291. }
  4292. int ata_flush_cache(struct ata_device *dev)
  4293. {
  4294. unsigned int err_mask;
  4295. u8 cmd;
  4296. if (!ata_try_flush_cache(dev))
  4297. return 0;
  4298. if (ata_id_has_flush_ext(dev->id))
  4299. cmd = ATA_CMD_FLUSH_EXT;
  4300. else
  4301. cmd = ATA_CMD_FLUSH;
  4302. err_mask = ata_do_simple_cmd(dev, cmd);
  4303. if (err_mask) {
  4304. ata_dev_printk(dev, KERN_ERR, "failed to flush cache\n");
  4305. return -EIO;
  4306. }
  4307. return 0;
  4308. }
  4309. static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
  4310. unsigned int action, unsigned int ehi_flags,
  4311. int wait)
  4312. {
  4313. unsigned long flags;
  4314. int i, rc;
  4315. for (i = 0; i < host->n_ports; i++) {
  4316. struct ata_port *ap = host->ports[i];
  4317. /* Previous resume operation might still be in
  4318. * progress. Wait for PM_PENDING to clear.
  4319. */
  4320. if (ap->pflags & ATA_PFLAG_PM_PENDING) {
  4321. ata_port_wait_eh(ap);
  4322. WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
  4323. }
  4324. /* request PM ops to EH */
  4325. spin_lock_irqsave(ap->lock, flags);
  4326. ap->pm_mesg = mesg;
  4327. if (wait) {
  4328. rc = 0;
  4329. ap->pm_result = &rc;
  4330. }
  4331. ap->pflags |= ATA_PFLAG_PM_PENDING;
  4332. ap->eh_info.action |= action;
  4333. ap->eh_info.flags |= ehi_flags;
  4334. ata_port_schedule_eh(ap);
  4335. spin_unlock_irqrestore(ap->lock, flags);
  4336. /* wait and check result */
  4337. if (wait) {
  4338. ata_port_wait_eh(ap);
  4339. WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
  4340. if (rc)
  4341. return rc;
  4342. }
  4343. }
  4344. return 0;
  4345. }
  4346. /**
  4347. * ata_host_suspend - suspend host
  4348. * @host: host to suspend
  4349. * @mesg: PM message
  4350. *
  4351. * Suspend @host. Actual operation is performed by EH. This
  4352. * function requests EH to perform PM operations and waits for EH
  4353. * to finish.
  4354. *
  4355. * LOCKING:
  4356. * Kernel thread context (may sleep).
  4357. *
  4358. * RETURNS:
  4359. * 0 on success, -errno on failure.
  4360. */
  4361. int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
  4362. {
  4363. int i, j, rc;
  4364. rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
  4365. if (rc)
  4366. goto fail;
  4367. /* EH is quiescent now. Fail if we have any ready device.
  4368. * This happens if hotplug occurs between completion of device
  4369. * suspension and here.
  4370. */
  4371. for (i = 0; i < host->n_ports; i++) {
  4372. struct ata_port *ap = host->ports[i];
  4373. for (j = 0; j < ATA_MAX_DEVICES; j++) {
  4374. struct ata_device *dev = &ap->device[j];
  4375. if (ata_dev_ready(dev)) {
  4376. ata_port_printk(ap, KERN_WARNING,
  4377. "suspend failed, device %d "
  4378. "still active\n", dev->devno);
  4379. rc = -EBUSY;
  4380. goto fail;
  4381. }
  4382. }
  4383. }
  4384. host->dev->power.power_state = mesg;
  4385. return 0;
  4386. fail:
  4387. ata_host_resume(host);
  4388. return rc;
  4389. }
  4390. /**
  4391. * ata_host_resume - resume host
  4392. * @host: host to resume
  4393. *
  4394. * Resume @host. Actual operation is performed by EH. This
  4395. * function requests EH to perform PM operations and returns.
  4396. * Note that all resume operations are performed parallely.
  4397. *
  4398. * LOCKING:
  4399. * Kernel thread context (may sleep).
  4400. */
  4401. void ata_host_resume(struct ata_host *host)
  4402. {
  4403. ata_host_request_pm(host, PMSG_ON, ATA_EH_SOFTRESET,
  4404. ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
  4405. host->dev->power.power_state = PMSG_ON;
  4406. }
  4407. /**
  4408. * ata_port_start - Set port up for dma.
  4409. * @ap: Port to initialize
  4410. *
  4411. * Called just after data structures for each port are
  4412. * initialized. Allocates space for PRD table.
  4413. *
  4414. * May be used as the port_start() entry in ata_port_operations.
  4415. *
  4416. * LOCKING:
  4417. * Inherited from caller.
  4418. */
  4419. int ata_port_start (struct ata_port *ap)
  4420. {
  4421. struct device *dev = ap->dev;
  4422. int rc;
  4423. ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
  4424. if (!ap->prd)
  4425. return -ENOMEM;
  4426. rc = ata_pad_alloc(ap, dev);
  4427. if (rc) {
  4428. dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
  4429. return rc;
  4430. }
  4431. DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
  4432. return 0;
  4433. }
  4434. /**
  4435. * ata_port_stop - Undo ata_port_start()
  4436. * @ap: Port to shut down
  4437. *
  4438. * Frees the PRD table.
  4439. *
  4440. * May be used as the port_stop() entry in ata_port_operations.
  4441. *
  4442. * LOCKING:
  4443. * Inherited from caller.
  4444. */
  4445. void ata_port_stop (struct ata_port *ap)
  4446. {
  4447. struct device *dev = ap->dev;
  4448. dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
  4449. ata_pad_free(ap, dev);
  4450. }
  4451. void ata_host_stop (struct ata_host *host)
  4452. {
  4453. if (host->mmio_base)
  4454. iounmap(host->mmio_base);
  4455. }
  4456. /**
  4457. * ata_dev_init - Initialize an ata_device structure
  4458. * @dev: Device structure to initialize
  4459. *
  4460. * Initialize @dev in preparation for probing.
  4461. *
  4462. * LOCKING:
  4463. * Inherited from caller.
  4464. */
  4465. void ata_dev_init(struct ata_device *dev)
  4466. {
  4467. struct ata_port *ap = dev->ap;
  4468. unsigned long flags;
  4469. /* SATA spd limit is bound to the first device */
  4470. ap->sata_spd_limit = ap->hw_sata_spd_limit;
  4471. /* High bits of dev->flags are used to record warm plug
  4472. * requests which occur asynchronously. Synchronize using
  4473. * host lock.
  4474. */
  4475. spin_lock_irqsave(ap->lock, flags);
  4476. dev->flags &= ~ATA_DFLAG_INIT_MASK;
  4477. spin_unlock_irqrestore(ap->lock, flags);
  4478. memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
  4479. sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
  4480. dev->pio_mask = UINT_MAX;
  4481. dev->mwdma_mask = UINT_MAX;
  4482. dev->udma_mask = UINT_MAX;
  4483. }
  4484. /**
  4485. * ata_port_init - Initialize an ata_port structure
  4486. * @ap: Structure to initialize
  4487. * @host: Collection of hosts to which @ap belongs
  4488. * @ent: Probe information provided by low-level driver
  4489. * @port_no: Port number associated with this ata_port
  4490. *
  4491. * Initialize a new ata_port structure.
  4492. *
  4493. * LOCKING:
  4494. * Inherited from caller.
  4495. */
  4496. void ata_port_init(struct ata_port *ap, struct ata_host *host,
  4497. const struct ata_probe_ent *ent, unsigned int port_no)
  4498. {
  4499. unsigned int i;
  4500. ap->lock = &host->lock;
  4501. ap->flags = ATA_FLAG_DISABLED;
  4502. ap->id = ata_unique_id++;
  4503. ap->ctl = ATA_DEVCTL_OBS;
  4504. ap->host = host;
  4505. ap->dev = ent->dev;
  4506. ap->port_no = port_no;
  4507. if (port_no == 1 && ent->pinfo2) {
  4508. ap->pio_mask = ent->pinfo2->pio_mask;
  4509. ap->mwdma_mask = ent->pinfo2->mwdma_mask;
  4510. ap->udma_mask = ent->pinfo2->udma_mask;
  4511. ap->flags |= ent->pinfo2->flags;
  4512. ap->ops = ent->pinfo2->port_ops;
  4513. } else {
  4514. ap->pio_mask = ent->pio_mask;
  4515. ap->mwdma_mask = ent->mwdma_mask;
  4516. ap->udma_mask = ent->udma_mask;
  4517. ap->flags |= ent->port_flags;
  4518. ap->ops = ent->port_ops;
  4519. }
  4520. ap->hw_sata_spd_limit = UINT_MAX;
  4521. ap->active_tag = ATA_TAG_POISON;
  4522. ap->last_ctl = 0xFF;
  4523. #if defined(ATA_VERBOSE_DEBUG)
  4524. /* turn on all debugging levels */
  4525. ap->msg_enable = 0x00FF;
  4526. #elif defined(ATA_DEBUG)
  4527. ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
  4528. #else
  4529. ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
  4530. #endif
  4531. INIT_WORK(&ap->port_task, NULL, NULL);
  4532. INIT_WORK(&ap->hotplug_task, ata_scsi_hotplug, ap);
  4533. INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan, ap);
  4534. INIT_LIST_HEAD(&ap->eh_done_q);
  4535. init_waitqueue_head(&ap->eh_wait_q);
  4536. /* set cable type */
  4537. ap->cbl = ATA_CBL_NONE;
  4538. if (ap->flags & ATA_FLAG_SATA)
  4539. ap->cbl = ATA_CBL_SATA;
  4540. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  4541. struct ata_device *dev = &ap->device[i];
  4542. dev->ap = ap;
  4543. dev->devno = i;
  4544. ata_dev_init(dev);
  4545. }
  4546. #ifdef ATA_IRQ_TRAP
  4547. ap->stats.unhandled_irq = 1;
  4548. ap->stats.idle_irq = 1;
  4549. #endif
  4550. memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
  4551. }
  4552. /**
  4553. * ata_port_init_shost - Initialize SCSI host associated with ATA port
  4554. * @ap: ATA port to initialize SCSI host for
  4555. * @shost: SCSI host associated with @ap
  4556. *
  4557. * Initialize SCSI host @shost associated with ATA port @ap.
  4558. *
  4559. * LOCKING:
  4560. * Inherited from caller.
  4561. */
  4562. static void ata_port_init_shost(struct ata_port *ap, struct Scsi_Host *shost)
  4563. {
  4564. ap->scsi_host = shost;
  4565. shost->unique_id = ap->id;
  4566. shost->max_id = 16;
  4567. shost->max_lun = 1;
  4568. shost->max_channel = 1;
  4569. shost->max_cmd_len = 12;
  4570. }
  4571. /**
  4572. * ata_port_add - Attach low-level ATA driver to system
  4573. * @ent: Information provided by low-level driver
  4574. * @host: Collections of ports to which we add
  4575. * @port_no: Port number associated with this host
  4576. *
  4577. * Attach low-level ATA driver to system.
  4578. *
  4579. * LOCKING:
  4580. * PCI/etc. bus probe sem.
  4581. *
  4582. * RETURNS:
  4583. * New ata_port on success, for NULL on error.
  4584. */
  4585. static struct ata_port * ata_port_add(const struct ata_probe_ent *ent,
  4586. struct ata_host *host,
  4587. unsigned int port_no)
  4588. {
  4589. struct Scsi_Host *shost;
  4590. struct ata_port *ap;
  4591. DPRINTK("ENTER\n");
  4592. if (!ent->port_ops->error_handler &&
  4593. !(ent->port_flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST))) {
  4594. printk(KERN_ERR "ata%u: no reset mechanism available\n",
  4595. port_no);
  4596. return NULL;
  4597. }
  4598. shost = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
  4599. if (!shost)
  4600. return NULL;
  4601. shost->transportt = &ata_scsi_transport_template;
  4602. ap = ata_shost_to_port(shost);
  4603. ata_port_init(ap, host, ent, port_no);
  4604. ata_port_init_shost(ap, shost);
  4605. return ap;
  4606. }
  4607. /**
  4608. * ata_sas_host_init - Initialize a host struct
  4609. * @host: host to initialize
  4610. * @dev: device host is attached to
  4611. * @flags: host flags
  4612. * @ops: port_ops
  4613. *
  4614. * LOCKING:
  4615. * PCI/etc. bus probe sem.
  4616. *
  4617. */
  4618. void ata_host_init(struct ata_host *host, struct device *dev,
  4619. unsigned long flags, const struct ata_port_operations *ops)
  4620. {
  4621. spin_lock_init(&host->lock);
  4622. host->dev = dev;
  4623. host->flags = flags;
  4624. host->ops = ops;
  4625. }
  4626. /**
  4627. * ata_device_add - Register hardware device with ATA and SCSI layers
  4628. * @ent: Probe information describing hardware device to be registered
  4629. *
  4630. * This function processes the information provided in the probe
  4631. * information struct @ent, allocates the necessary ATA and SCSI
  4632. * host information structures, initializes them, and registers
  4633. * everything with requisite kernel subsystems.
  4634. *
  4635. * This function requests irqs, probes the ATA bus, and probes
  4636. * the SCSI bus.
  4637. *
  4638. * LOCKING:
  4639. * PCI/etc. bus probe sem.
  4640. *
  4641. * RETURNS:
  4642. * Number of ports registered. Zero on error (no ports registered).
  4643. */
  4644. int ata_device_add(const struct ata_probe_ent *ent)
  4645. {
  4646. unsigned int i;
  4647. struct device *dev = ent->dev;
  4648. struct ata_host *host;
  4649. int rc;
  4650. DPRINTK("ENTER\n");
  4651. if (ent->irq == 0) {
  4652. dev_printk(KERN_ERR, dev, "is not available: No interrupt assigned.\n");
  4653. return 0;
  4654. }
  4655. /* alloc a container for our list of ATA ports (buses) */
  4656. host = kzalloc(sizeof(struct ata_host) +
  4657. (ent->n_ports * sizeof(void *)), GFP_KERNEL);
  4658. if (!host)
  4659. return 0;
  4660. ata_host_init(host, dev, ent->_host_flags, ent->port_ops);
  4661. host->n_ports = ent->n_ports;
  4662. host->irq = ent->irq;
  4663. host->irq2 = ent->irq2;
  4664. host->mmio_base = ent->mmio_base;
  4665. host->private_data = ent->private_data;
  4666. /* register each port bound to this device */
  4667. for (i = 0; i < host->n_ports; i++) {
  4668. struct ata_port *ap;
  4669. unsigned long xfer_mode_mask;
  4670. int irq_line = ent->irq;
  4671. ap = ata_port_add(ent, host, i);
  4672. host->ports[i] = ap;
  4673. if (!ap)
  4674. goto err_out;
  4675. /* dummy? */
  4676. if (ent->dummy_port_mask & (1 << i)) {
  4677. ata_port_printk(ap, KERN_INFO, "DUMMY\n");
  4678. ap->ops = &ata_dummy_port_ops;
  4679. continue;
  4680. }
  4681. /* start port */
  4682. rc = ap->ops->port_start(ap);
  4683. if (rc) {
  4684. host->ports[i] = NULL;
  4685. scsi_host_put(ap->scsi_host);
  4686. goto err_out;
  4687. }
  4688. /* Report the secondary IRQ for second channel legacy */
  4689. if (i == 1 && ent->irq2)
  4690. irq_line = ent->irq2;
  4691. xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
  4692. (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
  4693. (ap->pio_mask << ATA_SHIFT_PIO);
  4694. /* print per-port info to dmesg */
  4695. ata_port_printk(ap, KERN_INFO, "%cATA max %s cmd 0x%lX "
  4696. "ctl 0x%lX bmdma 0x%lX irq %d\n",
  4697. ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
  4698. ata_mode_string(xfer_mode_mask),
  4699. ap->ioaddr.cmd_addr,
  4700. ap->ioaddr.ctl_addr,
  4701. ap->ioaddr.bmdma_addr,
  4702. irq_line);
  4703. ata_chk_status(ap);
  4704. host->ops->irq_clear(ap);
  4705. ata_eh_freeze_port(ap); /* freeze port before requesting IRQ */
  4706. }
  4707. /* obtain irq, that may be shared between channels */
  4708. rc = request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
  4709. DRV_NAME, host);
  4710. if (rc) {
  4711. dev_printk(KERN_ERR, dev, "irq %lu request failed: %d\n",
  4712. ent->irq, rc);
  4713. goto err_out;
  4714. }
  4715. /* do we have a second IRQ for the other channel, eg legacy mode */
  4716. if (ent->irq2) {
  4717. /* We will get weird core code crashes later if this is true
  4718. so trap it now */
  4719. BUG_ON(ent->irq == ent->irq2);
  4720. rc = request_irq(ent->irq2, ent->port_ops->irq_handler, ent->irq_flags,
  4721. DRV_NAME, host);
  4722. if (rc) {
  4723. dev_printk(KERN_ERR, dev, "irq %lu request failed: %d\n",
  4724. ent->irq2, rc);
  4725. goto err_out_free_irq;
  4726. }
  4727. }
  4728. /* perform each probe synchronously */
  4729. DPRINTK("probe begin\n");
  4730. for (i = 0; i < host->n_ports; i++) {
  4731. struct ata_port *ap = host->ports[i];
  4732. u32 scontrol;
  4733. int rc;
  4734. /* init sata_spd_limit to the current value */
  4735. if (sata_scr_read(ap, SCR_CONTROL, &scontrol) == 0) {
  4736. int spd = (scontrol >> 4) & 0xf;
  4737. ap->hw_sata_spd_limit &= (1 << spd) - 1;
  4738. }
  4739. ap->sata_spd_limit = ap->hw_sata_spd_limit;
  4740. rc = scsi_add_host(ap->scsi_host, dev);
  4741. if (rc) {
  4742. ata_port_printk(ap, KERN_ERR, "scsi_add_host failed\n");
  4743. /* FIXME: do something useful here */
  4744. /* FIXME: handle unconditional calls to
  4745. * scsi_scan_host and ata_host_remove, below,
  4746. * at the very least
  4747. */
  4748. }
  4749. if (ap->ops->error_handler) {
  4750. struct ata_eh_info *ehi = &ap->eh_info;
  4751. unsigned long flags;
  4752. ata_port_probe(ap);
  4753. /* kick EH for boot probing */
  4754. spin_lock_irqsave(ap->lock, flags);
  4755. ehi->probe_mask = (1 << ATA_MAX_DEVICES) - 1;
  4756. ehi->action |= ATA_EH_SOFTRESET;
  4757. ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
  4758. ap->pflags |= ATA_PFLAG_LOADING;
  4759. ata_port_schedule_eh(ap);
  4760. spin_unlock_irqrestore(ap->lock, flags);
  4761. /* wait for EH to finish */
  4762. ata_port_wait_eh(ap);
  4763. } else {
  4764. DPRINTK("ata%u: bus probe begin\n", ap->id);
  4765. rc = ata_bus_probe(ap);
  4766. DPRINTK("ata%u: bus probe end\n", ap->id);
  4767. if (rc) {
  4768. /* FIXME: do something useful here?
  4769. * Current libata behavior will
  4770. * tear down everything when
  4771. * the module is removed
  4772. * or the h/w is unplugged.
  4773. */
  4774. }
  4775. }
  4776. }
  4777. /* probes are done, now scan each port's disk(s) */
  4778. DPRINTK("host probe begin\n");
  4779. for (i = 0; i < host->n_ports; i++) {
  4780. struct ata_port *ap = host->ports[i];
  4781. ata_scsi_scan_host(ap);
  4782. }
  4783. dev_set_drvdata(dev, host);
  4784. VPRINTK("EXIT, returning %u\n", ent->n_ports);
  4785. return ent->n_ports; /* success */
  4786. err_out_free_irq:
  4787. free_irq(ent->irq, host);
  4788. err_out:
  4789. for (i = 0; i < host->n_ports; i++) {
  4790. struct ata_port *ap = host->ports[i];
  4791. if (ap) {
  4792. ap->ops->port_stop(ap);
  4793. scsi_host_put(ap->scsi_host);
  4794. }
  4795. }
  4796. kfree(host);
  4797. VPRINTK("EXIT, returning 0\n");
  4798. return 0;
  4799. }
  4800. /**
  4801. * ata_port_detach - Detach ATA port in prepration of device removal
  4802. * @ap: ATA port to be detached
  4803. *
  4804. * Detach all ATA devices and the associated SCSI devices of @ap;
  4805. * then, remove the associated SCSI host. @ap is guaranteed to
  4806. * be quiescent on return from this function.
  4807. *
  4808. * LOCKING:
  4809. * Kernel thread context (may sleep).
  4810. */
  4811. void ata_port_detach(struct ata_port *ap)
  4812. {
  4813. unsigned long flags;
  4814. int i;
  4815. if (!ap->ops->error_handler)
  4816. goto skip_eh;
  4817. /* tell EH we're leaving & flush EH */
  4818. spin_lock_irqsave(ap->lock, flags);
  4819. ap->pflags |= ATA_PFLAG_UNLOADING;
  4820. spin_unlock_irqrestore(ap->lock, flags);
  4821. ata_port_wait_eh(ap);
  4822. /* EH is now guaranteed to see UNLOADING, so no new device
  4823. * will be attached. Disable all existing devices.
  4824. */
  4825. spin_lock_irqsave(ap->lock, flags);
  4826. for (i = 0; i < ATA_MAX_DEVICES; i++)
  4827. ata_dev_disable(&ap->device[i]);
  4828. spin_unlock_irqrestore(ap->lock, flags);
  4829. /* Final freeze & EH. All in-flight commands are aborted. EH
  4830. * will be skipped and retrials will be terminated with bad
  4831. * target.
  4832. */
  4833. spin_lock_irqsave(ap->lock, flags);
  4834. ata_port_freeze(ap); /* won't be thawed */
  4835. spin_unlock_irqrestore(ap->lock, flags);
  4836. ata_port_wait_eh(ap);
  4837. /* Flush hotplug task. The sequence is similar to
  4838. * ata_port_flush_task().
  4839. */
  4840. flush_workqueue(ata_aux_wq);
  4841. cancel_delayed_work(&ap->hotplug_task);
  4842. flush_workqueue(ata_aux_wq);
  4843. skip_eh:
  4844. /* remove the associated SCSI host */
  4845. scsi_remove_host(ap->scsi_host);
  4846. }
  4847. /**
  4848. * ata_host_remove - PCI layer callback for device removal
  4849. * @host: ATA host set that was removed
  4850. *
  4851. * Unregister all objects associated with this host set. Free those
  4852. * objects.
  4853. *
  4854. * LOCKING:
  4855. * Inherited from calling layer (may sleep).
  4856. */
  4857. void ata_host_remove(struct ata_host *host)
  4858. {
  4859. unsigned int i;
  4860. for (i = 0; i < host->n_ports; i++)
  4861. ata_port_detach(host->ports[i]);
  4862. free_irq(host->irq, host);
  4863. if (host->irq2)
  4864. free_irq(host->irq2, host);
  4865. for (i = 0; i < host->n_ports; i++) {
  4866. struct ata_port *ap = host->ports[i];
  4867. ata_scsi_release(ap->scsi_host);
  4868. if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
  4869. struct ata_ioports *ioaddr = &ap->ioaddr;
  4870. /* FIXME: Add -ac IDE pci mods to remove these special cases */
  4871. if (ioaddr->cmd_addr == ATA_PRIMARY_CMD)
  4872. release_region(ATA_PRIMARY_CMD, 8);
  4873. else if (ioaddr->cmd_addr == ATA_SECONDARY_CMD)
  4874. release_region(ATA_SECONDARY_CMD, 8);
  4875. }
  4876. scsi_host_put(ap->scsi_host);
  4877. }
  4878. if (host->ops->host_stop)
  4879. host->ops->host_stop(host);
  4880. kfree(host);
  4881. }
  4882. /**
  4883. * ata_scsi_release - SCSI layer callback hook for host unload
  4884. * @shost: libata host to be unloaded
  4885. *
  4886. * Performs all duties necessary to shut down a libata port...
  4887. * Kill port kthread, disable port, and release resources.
  4888. *
  4889. * LOCKING:
  4890. * Inherited from SCSI layer.
  4891. *
  4892. * RETURNS:
  4893. * One.
  4894. */
  4895. int ata_scsi_release(struct Scsi_Host *shost)
  4896. {
  4897. struct ata_port *ap = ata_shost_to_port(shost);
  4898. DPRINTK("ENTER\n");
  4899. ap->ops->port_disable(ap);
  4900. ap->ops->port_stop(ap);
  4901. DPRINTK("EXIT\n");
  4902. return 1;
  4903. }
  4904. struct ata_probe_ent *
  4905. ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
  4906. {
  4907. struct ata_probe_ent *probe_ent;
  4908. probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
  4909. if (!probe_ent) {
  4910. printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
  4911. kobject_name(&(dev->kobj)));
  4912. return NULL;
  4913. }
  4914. INIT_LIST_HEAD(&probe_ent->node);
  4915. probe_ent->dev = dev;
  4916. probe_ent->sht = port->sht;
  4917. probe_ent->port_flags = port->flags;
  4918. probe_ent->pio_mask = port->pio_mask;
  4919. probe_ent->mwdma_mask = port->mwdma_mask;
  4920. probe_ent->udma_mask = port->udma_mask;
  4921. probe_ent->port_ops = port->port_ops;
  4922. probe_ent->private_data = port->private_data;
  4923. return probe_ent;
  4924. }
  4925. /**
  4926. * ata_std_ports - initialize ioaddr with standard port offsets.
  4927. * @ioaddr: IO address structure to be initialized
  4928. *
  4929. * Utility function which initializes data_addr, error_addr,
  4930. * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
  4931. * device_addr, status_addr, and command_addr to standard offsets
  4932. * relative to cmd_addr.
  4933. *
  4934. * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
  4935. */
  4936. void ata_std_ports(struct ata_ioports *ioaddr)
  4937. {
  4938. ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
  4939. ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
  4940. ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
  4941. ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
  4942. ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
  4943. ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
  4944. ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
  4945. ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
  4946. ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
  4947. ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
  4948. }
  4949. #ifdef CONFIG_PCI
  4950. void ata_pci_host_stop (struct ata_host *host)
  4951. {
  4952. struct pci_dev *pdev = to_pci_dev(host->dev);
  4953. pci_iounmap(pdev, host->mmio_base);
  4954. }
  4955. /**
  4956. * ata_pci_remove_one - PCI layer callback for device removal
  4957. * @pdev: PCI device that was removed
  4958. *
  4959. * PCI layer indicates to libata via this hook that
  4960. * hot-unplug or module unload event has occurred.
  4961. * Handle this by unregistering all objects associated
  4962. * with this PCI device. Free those objects. Then finally
  4963. * release PCI resources and disable device.
  4964. *
  4965. * LOCKING:
  4966. * Inherited from PCI layer (may sleep).
  4967. */
  4968. void ata_pci_remove_one (struct pci_dev *pdev)
  4969. {
  4970. struct device *dev = pci_dev_to_dev(pdev);
  4971. struct ata_host *host = dev_get_drvdata(dev);
  4972. ata_host_remove(host);
  4973. pci_release_regions(pdev);
  4974. pci_disable_device(pdev);
  4975. dev_set_drvdata(dev, NULL);
  4976. }
  4977. /* move to PCI subsystem */
  4978. int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
  4979. {
  4980. unsigned long tmp = 0;
  4981. switch (bits->width) {
  4982. case 1: {
  4983. u8 tmp8 = 0;
  4984. pci_read_config_byte(pdev, bits->reg, &tmp8);
  4985. tmp = tmp8;
  4986. break;
  4987. }
  4988. case 2: {
  4989. u16 tmp16 = 0;
  4990. pci_read_config_word(pdev, bits->reg, &tmp16);
  4991. tmp = tmp16;
  4992. break;
  4993. }
  4994. case 4: {
  4995. u32 tmp32 = 0;
  4996. pci_read_config_dword(pdev, bits->reg, &tmp32);
  4997. tmp = tmp32;
  4998. break;
  4999. }
  5000. default:
  5001. return -EINVAL;
  5002. }
  5003. tmp &= bits->mask;
  5004. return (tmp == bits->val) ? 1 : 0;
  5005. }
  5006. void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
  5007. {
  5008. pci_save_state(pdev);
  5009. if (mesg.event == PM_EVENT_SUSPEND) {
  5010. pci_disable_device(pdev);
  5011. pci_set_power_state(pdev, PCI_D3hot);
  5012. }
  5013. }
  5014. void ata_pci_device_do_resume(struct pci_dev *pdev)
  5015. {
  5016. pci_set_power_state(pdev, PCI_D0);
  5017. pci_restore_state(pdev);
  5018. pci_enable_device(pdev);
  5019. pci_set_master(pdev);
  5020. }
  5021. int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
  5022. {
  5023. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  5024. int rc = 0;
  5025. rc = ata_host_suspend(host, mesg);
  5026. if (rc)
  5027. return rc;
  5028. ata_pci_device_do_suspend(pdev, mesg);
  5029. return 0;
  5030. }
  5031. int ata_pci_device_resume(struct pci_dev *pdev)
  5032. {
  5033. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  5034. ata_pci_device_do_resume(pdev);
  5035. ata_host_resume(host);
  5036. return 0;
  5037. }
  5038. #endif /* CONFIG_PCI */
  5039. static int __init ata_init(void)
  5040. {
  5041. ata_probe_timeout *= HZ;
  5042. ata_wq = create_workqueue("ata");
  5043. if (!ata_wq)
  5044. return -ENOMEM;
  5045. ata_aux_wq = create_singlethread_workqueue("ata_aux");
  5046. if (!ata_aux_wq) {
  5047. destroy_workqueue(ata_wq);
  5048. return -ENOMEM;
  5049. }
  5050. printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
  5051. return 0;
  5052. }
  5053. static void __exit ata_exit(void)
  5054. {
  5055. destroy_workqueue(ata_wq);
  5056. destroy_workqueue(ata_aux_wq);
  5057. }
  5058. module_init(ata_init);
  5059. module_exit(ata_exit);
  5060. static unsigned long ratelimit_time;
  5061. static DEFINE_SPINLOCK(ata_ratelimit_lock);
  5062. int ata_ratelimit(void)
  5063. {
  5064. int rc;
  5065. unsigned long flags;
  5066. spin_lock_irqsave(&ata_ratelimit_lock, flags);
  5067. if (time_after(jiffies, ratelimit_time)) {
  5068. rc = 1;
  5069. ratelimit_time = jiffies + (HZ/5);
  5070. } else
  5071. rc = 0;
  5072. spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
  5073. return rc;
  5074. }
  5075. /**
  5076. * ata_wait_register - wait until register value changes
  5077. * @reg: IO-mapped register
  5078. * @mask: Mask to apply to read register value
  5079. * @val: Wait condition
  5080. * @interval_msec: polling interval in milliseconds
  5081. * @timeout_msec: timeout in milliseconds
  5082. *
  5083. * Waiting for some bits of register to change is a common
  5084. * operation for ATA controllers. This function reads 32bit LE
  5085. * IO-mapped register @reg and tests for the following condition.
  5086. *
  5087. * (*@reg & mask) != val
  5088. *
  5089. * If the condition is met, it returns; otherwise, the process is
  5090. * repeated after @interval_msec until timeout.
  5091. *
  5092. * LOCKING:
  5093. * Kernel thread context (may sleep)
  5094. *
  5095. * RETURNS:
  5096. * The final register value.
  5097. */
  5098. u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
  5099. unsigned long interval_msec,
  5100. unsigned long timeout_msec)
  5101. {
  5102. unsigned long timeout;
  5103. u32 tmp;
  5104. tmp = ioread32(reg);
  5105. /* Calculate timeout _after_ the first read to make sure
  5106. * preceding writes reach the controller before starting to
  5107. * eat away the timeout.
  5108. */
  5109. timeout = jiffies + (timeout_msec * HZ) / 1000;
  5110. while ((tmp & mask) == val && time_before(jiffies, timeout)) {
  5111. msleep(interval_msec);
  5112. tmp = ioread32(reg);
  5113. }
  5114. return tmp;
  5115. }
  5116. /*
  5117. * Dummy port_ops
  5118. */
  5119. static void ata_dummy_noret(struct ata_port *ap) { }
  5120. static int ata_dummy_ret0(struct ata_port *ap) { return 0; }
  5121. static void ata_dummy_qc_noret(struct ata_queued_cmd *qc) { }
  5122. static u8 ata_dummy_check_status(struct ata_port *ap)
  5123. {
  5124. return ATA_DRDY;
  5125. }
  5126. static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
  5127. {
  5128. return AC_ERR_SYSTEM;
  5129. }
  5130. const struct ata_port_operations ata_dummy_port_ops = {
  5131. .port_disable = ata_port_disable,
  5132. .check_status = ata_dummy_check_status,
  5133. .check_altstatus = ata_dummy_check_status,
  5134. .dev_select = ata_noop_dev_select,
  5135. .qc_prep = ata_noop_qc_prep,
  5136. .qc_issue = ata_dummy_qc_issue,
  5137. .freeze = ata_dummy_noret,
  5138. .thaw = ata_dummy_noret,
  5139. .error_handler = ata_dummy_noret,
  5140. .post_internal_cmd = ata_dummy_qc_noret,
  5141. .irq_clear = ata_dummy_noret,
  5142. .port_start = ata_dummy_ret0,
  5143. .port_stop = ata_dummy_noret,
  5144. };
  5145. /*
  5146. * libata is essentially a library of internal helper functions for
  5147. * low-level ATA host controller drivers. As such, the API/ABI is
  5148. * likely to change as new drivers are added and updated.
  5149. * Do not depend on ABI/API stability.
  5150. */
  5151. EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
  5152. EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
  5153. EXPORT_SYMBOL_GPL(sata_deb_timing_long);
  5154. EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
  5155. EXPORT_SYMBOL_GPL(ata_std_bios_param);
  5156. EXPORT_SYMBOL_GPL(ata_std_ports);
  5157. EXPORT_SYMBOL_GPL(ata_host_init);
  5158. EXPORT_SYMBOL_GPL(ata_device_add);
  5159. EXPORT_SYMBOL_GPL(ata_port_detach);
  5160. EXPORT_SYMBOL_GPL(ata_host_remove);
  5161. EXPORT_SYMBOL_GPL(ata_sg_init);
  5162. EXPORT_SYMBOL_GPL(ata_sg_init_one);
  5163. EXPORT_SYMBOL_GPL(ata_hsm_move);
  5164. EXPORT_SYMBOL_GPL(ata_qc_complete);
  5165. EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
  5166. EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
  5167. EXPORT_SYMBOL_GPL(ata_tf_load);
  5168. EXPORT_SYMBOL_GPL(ata_tf_read);
  5169. EXPORT_SYMBOL_GPL(ata_noop_dev_select);
  5170. EXPORT_SYMBOL_GPL(ata_std_dev_select);
  5171. EXPORT_SYMBOL_GPL(ata_tf_to_fis);
  5172. EXPORT_SYMBOL_GPL(ata_tf_from_fis);
  5173. EXPORT_SYMBOL_GPL(ata_check_status);
  5174. EXPORT_SYMBOL_GPL(ata_altstatus);
  5175. EXPORT_SYMBOL_GPL(ata_exec_command);
  5176. EXPORT_SYMBOL_GPL(ata_port_start);
  5177. EXPORT_SYMBOL_GPL(ata_port_stop);
  5178. EXPORT_SYMBOL_GPL(ata_host_stop);
  5179. EXPORT_SYMBOL_GPL(ata_interrupt);
  5180. EXPORT_SYMBOL_GPL(ata_mmio_data_xfer);
  5181. EXPORT_SYMBOL_GPL(ata_pio_data_xfer);
  5182. EXPORT_SYMBOL_GPL(ata_pio_data_xfer_noirq);
  5183. EXPORT_SYMBOL_GPL(ata_qc_prep);
  5184. EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
  5185. EXPORT_SYMBOL_GPL(ata_bmdma_setup);
  5186. EXPORT_SYMBOL_GPL(ata_bmdma_start);
  5187. EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
  5188. EXPORT_SYMBOL_GPL(ata_bmdma_status);
  5189. EXPORT_SYMBOL_GPL(ata_bmdma_stop);
  5190. EXPORT_SYMBOL_GPL(ata_bmdma_freeze);
  5191. EXPORT_SYMBOL_GPL(ata_bmdma_thaw);
  5192. EXPORT_SYMBOL_GPL(ata_bmdma_drive_eh);
  5193. EXPORT_SYMBOL_GPL(ata_bmdma_error_handler);
  5194. EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd);
  5195. EXPORT_SYMBOL_GPL(ata_port_probe);
  5196. EXPORT_SYMBOL_GPL(sata_set_spd);
  5197. EXPORT_SYMBOL_GPL(sata_phy_debounce);
  5198. EXPORT_SYMBOL_GPL(sata_phy_resume);
  5199. EXPORT_SYMBOL_GPL(sata_phy_reset);
  5200. EXPORT_SYMBOL_GPL(__sata_phy_reset);
  5201. EXPORT_SYMBOL_GPL(ata_bus_reset);
  5202. EXPORT_SYMBOL_GPL(ata_std_prereset);
  5203. EXPORT_SYMBOL_GPL(ata_std_softreset);
  5204. EXPORT_SYMBOL_GPL(sata_std_hardreset);
  5205. EXPORT_SYMBOL_GPL(ata_std_postreset);
  5206. EXPORT_SYMBOL_GPL(ata_dev_revalidate);
  5207. EXPORT_SYMBOL_GPL(ata_dev_classify);
  5208. EXPORT_SYMBOL_GPL(ata_dev_pair);
  5209. EXPORT_SYMBOL_GPL(ata_port_disable);
  5210. EXPORT_SYMBOL_GPL(ata_ratelimit);
  5211. EXPORT_SYMBOL_GPL(ata_wait_register);
  5212. EXPORT_SYMBOL_GPL(ata_busy_sleep);
  5213. EXPORT_SYMBOL_GPL(ata_port_queue_task);
  5214. EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
  5215. EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
  5216. EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
  5217. EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
  5218. EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
  5219. EXPORT_SYMBOL_GPL(ata_scsi_release);
  5220. EXPORT_SYMBOL_GPL(ata_host_intr);
  5221. EXPORT_SYMBOL_GPL(sata_scr_valid);
  5222. EXPORT_SYMBOL_GPL(sata_scr_read);
  5223. EXPORT_SYMBOL_GPL(sata_scr_write);
  5224. EXPORT_SYMBOL_GPL(sata_scr_write_flush);
  5225. EXPORT_SYMBOL_GPL(ata_port_online);
  5226. EXPORT_SYMBOL_GPL(ata_port_offline);
  5227. EXPORT_SYMBOL_GPL(ata_host_suspend);
  5228. EXPORT_SYMBOL_GPL(ata_host_resume);
  5229. EXPORT_SYMBOL_GPL(ata_id_string);
  5230. EXPORT_SYMBOL_GPL(ata_id_c_string);
  5231. EXPORT_SYMBOL_GPL(ata_scsi_simulate);
  5232. EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
  5233. EXPORT_SYMBOL_GPL(ata_timing_compute);
  5234. EXPORT_SYMBOL_GPL(ata_timing_merge);
  5235. #ifdef CONFIG_PCI
  5236. EXPORT_SYMBOL_GPL(pci_test_config_bits);
  5237. EXPORT_SYMBOL_GPL(ata_pci_host_stop);
  5238. EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
  5239. EXPORT_SYMBOL_GPL(ata_pci_init_one);
  5240. EXPORT_SYMBOL_GPL(ata_pci_remove_one);
  5241. EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
  5242. EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
  5243. EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
  5244. EXPORT_SYMBOL_GPL(ata_pci_device_resume);
  5245. EXPORT_SYMBOL_GPL(ata_pci_default_filter);
  5246. EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
  5247. #endif /* CONFIG_PCI */
  5248. EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
  5249. EXPORT_SYMBOL_GPL(ata_scsi_device_resume);
  5250. EXPORT_SYMBOL_GPL(ata_eng_timeout);
  5251. EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
  5252. EXPORT_SYMBOL_GPL(ata_port_abort);
  5253. EXPORT_SYMBOL_GPL(ata_port_freeze);
  5254. EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
  5255. EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
  5256. EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
  5257. EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
  5258. EXPORT_SYMBOL_GPL(ata_do_eh);