libata-core.c 191 KB

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