libata-core.c 177 KB

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