libata-core.c 161 KB

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