libata-core.c 151 KB

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