libata-core.c 200 KB

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