libata-core.c 184 KB

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