libata-core.c 159 KB

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