libata-core.c 143 KB

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