libata-core.c 146 KB

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