libata-core.c 191 KB

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