libata-core.c 144 KB

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