libata-core.c 172 KB

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