libata-core.c 172 KB

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