libata-core.c 158 KB

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