libata-core.c 182 KB

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