libata-core.c 147 KB

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