libata-core.c 177 KB

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