libata-core.c 186 KB

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