libata-core.c 158 KB

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