libata-core.c 157 KB

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