libata-core.c 158 KB

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