libata-core.c 159 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514
  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. /**
  1353. * ata_dev_configure - Configure the specified ATA/ATAPI device
  1354. * @dev: Target device to configure
  1355. *
  1356. * Configure @dev according to @dev->id. Generic and low-level
  1357. * driver specific fixups are also applied.
  1358. *
  1359. * LOCKING:
  1360. * Kernel thread context (may sleep)
  1361. *
  1362. * RETURNS:
  1363. * 0 on success, -errno otherwise
  1364. */
  1365. int ata_dev_configure(struct ata_device *dev)
  1366. {
  1367. struct ata_port *ap = dev->ap;
  1368. int print_info = ap->eh_context.i.flags & ATA_EHI_PRINTINFO;
  1369. const u16 *id = dev->id;
  1370. unsigned int xfer_mask;
  1371. char revbuf[7]; /* XYZ-99\0 */
  1372. char fwrevbuf[ATA_ID_FW_REV_LEN+1];
  1373. char modelbuf[ATA_ID_PROD_LEN+1];
  1374. int rc;
  1375. if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
  1376. ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT -- nodev\n",
  1377. __FUNCTION__);
  1378. return 0;
  1379. }
  1380. if (ata_msg_probe(ap))
  1381. ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __FUNCTION__);
  1382. /* set _SDD */
  1383. rc = ata_acpi_push_id(ap, dev->devno);
  1384. if (rc) {
  1385. ata_dev_printk(dev, KERN_WARNING, "failed to set _SDD(%d)\n",
  1386. rc);
  1387. }
  1388. /* retrieve and execute the ATA task file of _GTF */
  1389. ata_acpi_exec_tfs(ap);
  1390. /* print device capabilities */
  1391. if (ata_msg_probe(ap))
  1392. ata_dev_printk(dev, KERN_DEBUG,
  1393. "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
  1394. "85:%04x 86:%04x 87:%04x 88:%04x\n",
  1395. __FUNCTION__,
  1396. id[49], id[82], id[83], id[84],
  1397. id[85], id[86], id[87], id[88]);
  1398. /* initialize to-be-configured parameters */
  1399. dev->flags &= ~ATA_DFLAG_CFG_MASK;
  1400. dev->max_sectors = 0;
  1401. dev->cdb_len = 0;
  1402. dev->n_sectors = 0;
  1403. dev->cylinders = 0;
  1404. dev->heads = 0;
  1405. dev->sectors = 0;
  1406. /*
  1407. * common ATA, ATAPI feature tests
  1408. */
  1409. /* find max transfer mode; for printk only */
  1410. xfer_mask = ata_id_xfermask(id);
  1411. if (ata_msg_probe(ap))
  1412. ata_dump_id(id);
  1413. /* ATA-specific feature tests */
  1414. if (dev->class == ATA_DEV_ATA) {
  1415. if (ata_id_is_cfa(id)) {
  1416. if (id[162] & 1) /* CPRM may make this media unusable */
  1417. ata_dev_printk(dev, KERN_WARNING,
  1418. "supports DRM functions and may "
  1419. "not be fully accessable.\n");
  1420. snprintf(revbuf, 7, "CFA");
  1421. }
  1422. else
  1423. snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
  1424. dev->n_sectors = ata_id_n_sectors(id);
  1425. /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
  1426. ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
  1427. sizeof(fwrevbuf));
  1428. ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
  1429. sizeof(modelbuf));
  1430. if (dev->id[59] & 0x100)
  1431. dev->multi_count = dev->id[59] & 0xff;
  1432. if (ata_id_has_lba(id)) {
  1433. const char *lba_desc;
  1434. char ncq_desc[20];
  1435. lba_desc = "LBA";
  1436. dev->flags |= ATA_DFLAG_LBA;
  1437. if (ata_id_has_lba48(id)) {
  1438. dev->flags |= ATA_DFLAG_LBA48;
  1439. lba_desc = "LBA48";
  1440. if (dev->n_sectors >= (1UL << 28) &&
  1441. ata_id_has_flush_ext(id))
  1442. dev->flags |= ATA_DFLAG_FLUSH_EXT;
  1443. }
  1444. /* config NCQ */
  1445. ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
  1446. /* print device info to dmesg */
  1447. if (ata_msg_drv(ap) && print_info) {
  1448. ata_dev_printk(dev, KERN_INFO,
  1449. "%s: %s, %s, max %s\n",
  1450. revbuf, modelbuf, fwrevbuf,
  1451. ata_mode_string(xfer_mask));
  1452. ata_dev_printk(dev, KERN_INFO,
  1453. "%Lu sectors, multi %u: %s %s\n",
  1454. (unsigned long long)dev->n_sectors,
  1455. dev->multi_count, lba_desc, ncq_desc);
  1456. }
  1457. } else {
  1458. /* CHS */
  1459. /* Default translation */
  1460. dev->cylinders = id[1];
  1461. dev->heads = id[3];
  1462. dev->sectors = id[6];
  1463. if (ata_id_current_chs_valid(id)) {
  1464. /* Current CHS translation is valid. */
  1465. dev->cylinders = id[54];
  1466. dev->heads = id[55];
  1467. dev->sectors = id[56];
  1468. }
  1469. /* print device info to dmesg */
  1470. if (ata_msg_drv(ap) && print_info) {
  1471. ata_dev_printk(dev, KERN_INFO,
  1472. "%s: %s, %s, max %s\n",
  1473. revbuf, modelbuf, fwrevbuf,
  1474. ata_mode_string(xfer_mask));
  1475. ata_dev_printk(dev, KERN_INFO,
  1476. "%Lu sectors, multi %u, CHS %u/%u/%u\n",
  1477. (unsigned long long)dev->n_sectors,
  1478. dev->multi_count, dev->cylinders,
  1479. dev->heads, dev->sectors);
  1480. }
  1481. }
  1482. dev->cdb_len = 16;
  1483. }
  1484. /* ATAPI-specific feature tests */
  1485. else if (dev->class == ATA_DEV_ATAPI) {
  1486. char *cdb_intr_string = "";
  1487. rc = atapi_cdb_len(id);
  1488. if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
  1489. if (ata_msg_warn(ap))
  1490. ata_dev_printk(dev, KERN_WARNING,
  1491. "unsupported CDB len\n");
  1492. rc = -EINVAL;
  1493. goto err_out_nosup;
  1494. }
  1495. dev->cdb_len = (unsigned int) rc;
  1496. if (ata_id_cdb_intr(dev->id)) {
  1497. dev->flags |= ATA_DFLAG_CDB_INTR;
  1498. cdb_intr_string = ", CDB intr";
  1499. }
  1500. /* print device info to dmesg */
  1501. if (ata_msg_drv(ap) && print_info)
  1502. ata_dev_printk(dev, KERN_INFO, "ATAPI, max %s%s\n",
  1503. ata_mode_string(xfer_mask),
  1504. cdb_intr_string);
  1505. }
  1506. /* determine max_sectors */
  1507. dev->max_sectors = ATA_MAX_SECTORS;
  1508. if (dev->flags & ATA_DFLAG_LBA48)
  1509. dev->max_sectors = ATA_MAX_SECTORS_LBA48;
  1510. if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
  1511. /* Let the user know. We don't want to disallow opens for
  1512. rescue purposes, or in case the vendor is just a blithering
  1513. idiot */
  1514. if (print_info) {
  1515. ata_dev_printk(dev, KERN_WARNING,
  1516. "Drive reports diagnostics failure. This may indicate a drive\n");
  1517. ata_dev_printk(dev, KERN_WARNING,
  1518. "fault or invalid emulation. Contact drive vendor for information.\n");
  1519. }
  1520. }
  1521. /* limit bridge transfers to udma5, 200 sectors */
  1522. if (ata_dev_knobble(dev)) {
  1523. if (ata_msg_drv(ap) && print_info)
  1524. ata_dev_printk(dev, KERN_INFO,
  1525. "applying bridge limits\n");
  1526. dev->udma_mask &= ATA_UDMA5;
  1527. dev->max_sectors = ATA_MAX_SECTORS;
  1528. }
  1529. if (ata_device_blacklisted(dev) & ATA_HORKAGE_MAX_SEC_128)
  1530. dev->max_sectors = min(ATA_MAX_SECTORS_128, dev->max_sectors);
  1531. /* limit ATAPI DMA to R/W commands only */
  1532. if (ata_device_blacklisted(dev) & ATA_HORKAGE_DMA_RW_ONLY)
  1533. dev->horkage |= ATA_HORKAGE_DMA_RW_ONLY;
  1534. if (ap->ops->dev_config)
  1535. ap->ops->dev_config(dev);
  1536. if (ata_msg_probe(ap))
  1537. ata_dev_printk(dev, KERN_DEBUG, "%s: EXIT, drv_stat = 0x%x\n",
  1538. __FUNCTION__, ata_chk_status(ap));
  1539. return 0;
  1540. err_out_nosup:
  1541. if (ata_msg_probe(ap))
  1542. ata_dev_printk(dev, KERN_DEBUG,
  1543. "%s: EXIT, err\n", __FUNCTION__);
  1544. return rc;
  1545. }
  1546. /**
  1547. * ata_cable_40wire - return 40pin cable type
  1548. * @ap: port
  1549. *
  1550. * Helper method for drivers which want to hardwire 40 pin cable
  1551. * detection.
  1552. */
  1553. int ata_cable_40wire(struct ata_port *ap)
  1554. {
  1555. return ATA_CBL_PATA40;
  1556. }
  1557. /**
  1558. * ata_cable_80wire - return 40pin cable type
  1559. * @ap: port
  1560. *
  1561. * Helper method for drivers which want to hardwire 80 pin cable
  1562. * detection.
  1563. */
  1564. int ata_cable_80wire(struct ata_port *ap)
  1565. {
  1566. return ATA_CBL_PATA80;
  1567. }
  1568. /**
  1569. * ata_cable_unknown - return unknown PATA cable.
  1570. * @ap: port
  1571. *
  1572. * Helper method for drivers which have no PATA cable detection.
  1573. */
  1574. int ata_cable_unknown(struct ata_port *ap)
  1575. {
  1576. return ATA_CBL_PATA_UNK;
  1577. }
  1578. /**
  1579. * ata_cable_sata - return SATA cable type
  1580. * @ap: port
  1581. *
  1582. * Helper method for drivers which have SATA cables
  1583. */
  1584. int ata_cable_sata(struct ata_port *ap)
  1585. {
  1586. return ATA_CBL_SATA;
  1587. }
  1588. /**
  1589. * ata_bus_probe - Reset and probe ATA bus
  1590. * @ap: Bus to probe
  1591. *
  1592. * Master ATA bus probing function. Initiates a hardware-dependent
  1593. * bus reset, then attempts to identify any devices found on
  1594. * the bus.
  1595. *
  1596. * LOCKING:
  1597. * PCI/etc. bus probe sem.
  1598. *
  1599. * RETURNS:
  1600. * Zero on success, negative errno otherwise.
  1601. */
  1602. int ata_bus_probe(struct ata_port *ap)
  1603. {
  1604. unsigned int classes[ATA_MAX_DEVICES];
  1605. int tries[ATA_MAX_DEVICES];
  1606. int i, rc;
  1607. struct ata_device *dev;
  1608. ata_port_probe(ap);
  1609. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1610. tries[i] = ATA_PROBE_MAX_TRIES;
  1611. retry:
  1612. /* reset and determine device classes */
  1613. ap->ops->phy_reset(ap);
  1614. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1615. dev = &ap->device[i];
  1616. if (!(ap->flags & ATA_FLAG_DISABLED) &&
  1617. dev->class != ATA_DEV_UNKNOWN)
  1618. classes[dev->devno] = dev->class;
  1619. else
  1620. classes[dev->devno] = ATA_DEV_NONE;
  1621. dev->class = ATA_DEV_UNKNOWN;
  1622. }
  1623. ata_port_probe(ap);
  1624. /* after the reset the device state is PIO 0 and the controller
  1625. state is undefined. Record the mode */
  1626. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1627. ap->device[i].pio_mode = XFER_PIO_0;
  1628. /* read IDENTIFY page and configure devices. We have to do the identify
  1629. specific sequence bass-ackwards so that PDIAG- is released by
  1630. the slave device */
  1631. for (i = ATA_MAX_DEVICES - 1; i >= 0; i--) {
  1632. dev = &ap->device[i];
  1633. if (tries[i])
  1634. dev->class = classes[i];
  1635. if (!ata_dev_enabled(dev))
  1636. continue;
  1637. rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
  1638. dev->id);
  1639. if (rc)
  1640. goto fail;
  1641. }
  1642. /* Now ask for the cable type as PDIAG- should have been released */
  1643. if (ap->ops->cable_detect)
  1644. ap->cbl = ap->ops->cable_detect(ap);
  1645. /* After the identify sequence we can now set up the devices. We do
  1646. this in the normal order so that the user doesn't get confused */
  1647. for(i = 0; i < ATA_MAX_DEVICES; i++) {
  1648. dev = &ap->device[i];
  1649. if (!ata_dev_enabled(dev))
  1650. continue;
  1651. ap->eh_context.i.flags |= ATA_EHI_PRINTINFO;
  1652. rc = ata_dev_configure(dev);
  1653. ap->eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
  1654. if (rc)
  1655. goto fail;
  1656. }
  1657. /* configure transfer mode */
  1658. rc = ata_set_mode(ap, &dev);
  1659. if (rc)
  1660. goto fail;
  1661. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1662. if (ata_dev_enabled(&ap->device[i]))
  1663. return 0;
  1664. /* no device present, disable port */
  1665. ata_port_disable(ap);
  1666. ap->ops->port_disable(ap);
  1667. return -ENODEV;
  1668. fail:
  1669. tries[dev->devno]--;
  1670. switch (rc) {
  1671. case -EINVAL:
  1672. /* eeek, something went very wrong, give up */
  1673. tries[dev->devno] = 0;
  1674. break;
  1675. case -ENODEV:
  1676. /* give it just one more chance */
  1677. tries[dev->devno] = min(tries[dev->devno], 1);
  1678. case -EIO:
  1679. if (tries[dev->devno] == 1) {
  1680. /* This is the last chance, better to slow
  1681. * down than lose it.
  1682. */
  1683. sata_down_spd_limit(ap);
  1684. ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
  1685. }
  1686. }
  1687. if (!tries[dev->devno])
  1688. ata_dev_disable(dev);
  1689. goto retry;
  1690. }
  1691. /**
  1692. * ata_port_probe - Mark port as enabled
  1693. * @ap: Port for which we indicate enablement
  1694. *
  1695. * Modify @ap data structure such that the system
  1696. * thinks that the entire port is enabled.
  1697. *
  1698. * LOCKING: host lock, or some other form of
  1699. * serialization.
  1700. */
  1701. void ata_port_probe(struct ata_port *ap)
  1702. {
  1703. ap->flags &= ~ATA_FLAG_DISABLED;
  1704. }
  1705. /**
  1706. * sata_print_link_status - Print SATA link status
  1707. * @ap: SATA port to printk link status about
  1708. *
  1709. * This function prints link speed and status of a SATA link.
  1710. *
  1711. * LOCKING:
  1712. * None.
  1713. */
  1714. void sata_print_link_status(struct ata_port *ap)
  1715. {
  1716. u32 sstatus, scontrol, tmp;
  1717. if (sata_scr_read(ap, SCR_STATUS, &sstatus))
  1718. return;
  1719. sata_scr_read(ap, SCR_CONTROL, &scontrol);
  1720. if (ata_port_online(ap)) {
  1721. tmp = (sstatus >> 4) & 0xf;
  1722. ata_port_printk(ap, KERN_INFO,
  1723. "SATA link up %s (SStatus %X SControl %X)\n",
  1724. sata_spd_string(tmp), sstatus, scontrol);
  1725. } else {
  1726. ata_port_printk(ap, KERN_INFO,
  1727. "SATA link down (SStatus %X SControl %X)\n",
  1728. sstatus, scontrol);
  1729. }
  1730. }
  1731. /**
  1732. * __sata_phy_reset - Wake/reset a low-level SATA PHY
  1733. * @ap: SATA port associated with target SATA PHY.
  1734. *
  1735. * This function issues commands to standard SATA Sxxx
  1736. * PHY registers, to wake up the phy (and device), and
  1737. * clear any reset condition.
  1738. *
  1739. * LOCKING:
  1740. * PCI/etc. bus probe sem.
  1741. *
  1742. */
  1743. void __sata_phy_reset(struct ata_port *ap)
  1744. {
  1745. u32 sstatus;
  1746. unsigned long timeout = jiffies + (HZ * 5);
  1747. if (ap->flags & ATA_FLAG_SATA_RESET) {
  1748. /* issue phy wake/reset */
  1749. sata_scr_write_flush(ap, SCR_CONTROL, 0x301);
  1750. /* Couldn't find anything in SATA I/II specs, but
  1751. * AHCI-1.1 10.4.2 says at least 1 ms. */
  1752. mdelay(1);
  1753. }
  1754. /* phy wake/clear reset */
  1755. sata_scr_write_flush(ap, SCR_CONTROL, 0x300);
  1756. /* wait for phy to become ready, if necessary */
  1757. do {
  1758. msleep(200);
  1759. sata_scr_read(ap, SCR_STATUS, &sstatus);
  1760. if ((sstatus & 0xf) != 1)
  1761. break;
  1762. } while (time_before(jiffies, timeout));
  1763. /* print link status */
  1764. sata_print_link_status(ap);
  1765. /* TODO: phy layer with polling, timeouts, etc. */
  1766. if (!ata_port_offline(ap))
  1767. ata_port_probe(ap);
  1768. else
  1769. ata_port_disable(ap);
  1770. if (ap->flags & ATA_FLAG_DISABLED)
  1771. return;
  1772. if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
  1773. ata_port_disable(ap);
  1774. return;
  1775. }
  1776. ap->cbl = ATA_CBL_SATA;
  1777. }
  1778. /**
  1779. * sata_phy_reset - Reset SATA bus.
  1780. * @ap: SATA port associated with target SATA PHY.
  1781. *
  1782. * This function resets the SATA bus, and then probes
  1783. * the bus for devices.
  1784. *
  1785. * LOCKING:
  1786. * PCI/etc. bus probe sem.
  1787. *
  1788. */
  1789. void sata_phy_reset(struct ata_port *ap)
  1790. {
  1791. __sata_phy_reset(ap);
  1792. if (ap->flags & ATA_FLAG_DISABLED)
  1793. return;
  1794. ata_bus_reset(ap);
  1795. }
  1796. /**
  1797. * ata_dev_pair - return other device on cable
  1798. * @adev: device
  1799. *
  1800. * Obtain the other device on the same cable, or if none is
  1801. * present NULL is returned
  1802. */
  1803. struct ata_device *ata_dev_pair(struct ata_device *adev)
  1804. {
  1805. struct ata_port *ap = adev->ap;
  1806. struct ata_device *pair = &ap->device[1 - adev->devno];
  1807. if (!ata_dev_enabled(pair))
  1808. return NULL;
  1809. return pair;
  1810. }
  1811. /**
  1812. * ata_port_disable - Disable port.
  1813. * @ap: Port to be disabled.
  1814. *
  1815. * Modify @ap data structure such that the system
  1816. * thinks that the entire port is disabled, and should
  1817. * never attempt to probe or communicate with devices
  1818. * on this port.
  1819. *
  1820. * LOCKING: host lock, or some other form of
  1821. * serialization.
  1822. */
  1823. void ata_port_disable(struct ata_port *ap)
  1824. {
  1825. ap->device[0].class = ATA_DEV_NONE;
  1826. ap->device[1].class = ATA_DEV_NONE;
  1827. ap->flags |= ATA_FLAG_DISABLED;
  1828. }
  1829. /**
  1830. * sata_down_spd_limit - adjust SATA spd limit downward
  1831. * @ap: Port to adjust SATA spd limit for
  1832. *
  1833. * Adjust SATA spd limit of @ap downward. Note that this
  1834. * function only adjusts the limit. The change must be applied
  1835. * using sata_set_spd().
  1836. *
  1837. * LOCKING:
  1838. * Inherited from caller.
  1839. *
  1840. * RETURNS:
  1841. * 0 on success, negative errno on failure
  1842. */
  1843. int sata_down_spd_limit(struct ata_port *ap)
  1844. {
  1845. u32 sstatus, spd, mask;
  1846. int rc, highbit;
  1847. rc = sata_scr_read(ap, SCR_STATUS, &sstatus);
  1848. if (rc)
  1849. return rc;
  1850. mask = ap->sata_spd_limit;
  1851. if (mask <= 1)
  1852. return -EINVAL;
  1853. highbit = fls(mask) - 1;
  1854. mask &= ~(1 << highbit);
  1855. spd = (sstatus >> 4) & 0xf;
  1856. if (spd <= 1)
  1857. return -EINVAL;
  1858. spd--;
  1859. mask &= (1 << spd) - 1;
  1860. if (!mask)
  1861. return -EINVAL;
  1862. ap->sata_spd_limit = mask;
  1863. ata_port_printk(ap, KERN_WARNING, "limiting SATA link speed to %s\n",
  1864. sata_spd_string(fls(mask)));
  1865. return 0;
  1866. }
  1867. static int __sata_set_spd_needed(struct ata_port *ap, u32 *scontrol)
  1868. {
  1869. u32 spd, limit;
  1870. if (ap->sata_spd_limit == UINT_MAX)
  1871. limit = 0;
  1872. else
  1873. limit = fls(ap->sata_spd_limit);
  1874. spd = (*scontrol >> 4) & 0xf;
  1875. *scontrol = (*scontrol & ~0xf0) | ((limit & 0xf) << 4);
  1876. return spd != limit;
  1877. }
  1878. /**
  1879. * sata_set_spd_needed - is SATA spd configuration needed
  1880. * @ap: Port in question
  1881. *
  1882. * Test whether the spd limit in SControl matches
  1883. * @ap->sata_spd_limit. This function is used to determine
  1884. * whether hardreset is necessary to apply SATA spd
  1885. * configuration.
  1886. *
  1887. * LOCKING:
  1888. * Inherited from caller.
  1889. *
  1890. * RETURNS:
  1891. * 1 if SATA spd configuration is needed, 0 otherwise.
  1892. */
  1893. int sata_set_spd_needed(struct ata_port *ap)
  1894. {
  1895. u32 scontrol;
  1896. if (sata_scr_read(ap, SCR_CONTROL, &scontrol))
  1897. return 0;
  1898. return __sata_set_spd_needed(ap, &scontrol);
  1899. }
  1900. /**
  1901. * sata_set_spd - set SATA spd according to spd limit
  1902. * @ap: Port to set SATA spd for
  1903. *
  1904. * Set SATA spd of @ap according to sata_spd_limit.
  1905. *
  1906. * LOCKING:
  1907. * Inherited from caller.
  1908. *
  1909. * RETURNS:
  1910. * 0 if spd doesn't need to be changed, 1 if spd has been
  1911. * changed. Negative errno if SCR registers are inaccessible.
  1912. */
  1913. int sata_set_spd(struct ata_port *ap)
  1914. {
  1915. u32 scontrol;
  1916. int rc;
  1917. if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
  1918. return rc;
  1919. if (!__sata_set_spd_needed(ap, &scontrol))
  1920. return 0;
  1921. if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
  1922. return rc;
  1923. return 1;
  1924. }
  1925. /*
  1926. * This mode timing computation functionality is ported over from
  1927. * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
  1928. */
  1929. /*
  1930. * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
  1931. * These were taken from ATA/ATAPI-6 standard, rev 0a, except
  1932. * for UDMA6, which is currently supported only by Maxtor drives.
  1933. *
  1934. * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
  1935. */
  1936. static const struct ata_timing ata_timing[] = {
  1937. { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
  1938. { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
  1939. { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
  1940. { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
  1941. { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 80, 0 },
  1942. { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 100, 0 },
  1943. { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
  1944. { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
  1945. { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
  1946. /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
  1947. { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
  1948. { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
  1949. { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
  1950. { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
  1951. { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
  1952. { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
  1953. { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 80, 0 },
  1954. { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 100, 0 },
  1955. { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
  1956. { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
  1957. { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
  1958. { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
  1959. { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
  1960. /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
  1961. { 0xFF }
  1962. };
  1963. #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
  1964. #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
  1965. static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
  1966. {
  1967. q->setup = EZ(t->setup * 1000, T);
  1968. q->act8b = EZ(t->act8b * 1000, T);
  1969. q->rec8b = EZ(t->rec8b * 1000, T);
  1970. q->cyc8b = EZ(t->cyc8b * 1000, T);
  1971. q->active = EZ(t->active * 1000, T);
  1972. q->recover = EZ(t->recover * 1000, T);
  1973. q->cycle = EZ(t->cycle * 1000, T);
  1974. q->udma = EZ(t->udma * 1000, UT);
  1975. }
  1976. void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
  1977. struct ata_timing *m, unsigned int what)
  1978. {
  1979. if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
  1980. if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
  1981. if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
  1982. if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
  1983. if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
  1984. if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
  1985. if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
  1986. if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
  1987. }
  1988. static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
  1989. {
  1990. const struct ata_timing *t;
  1991. for (t = ata_timing; t->mode != speed; t++)
  1992. if (t->mode == 0xFF)
  1993. return NULL;
  1994. return t;
  1995. }
  1996. int ata_timing_compute(struct ata_device *adev, unsigned short speed,
  1997. struct ata_timing *t, int T, int UT)
  1998. {
  1999. const struct ata_timing *s;
  2000. struct ata_timing p;
  2001. /*
  2002. * Find the mode.
  2003. */
  2004. if (!(s = ata_timing_find_mode(speed)))
  2005. return -EINVAL;
  2006. memcpy(t, s, sizeof(*s));
  2007. /*
  2008. * If the drive is an EIDE drive, it can tell us it needs extended
  2009. * PIO/MW_DMA cycle timing.
  2010. */
  2011. if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
  2012. memset(&p, 0, sizeof(p));
  2013. if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
  2014. if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
  2015. else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
  2016. } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
  2017. p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
  2018. }
  2019. ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
  2020. }
  2021. /*
  2022. * Convert the timing to bus clock counts.
  2023. */
  2024. ata_timing_quantize(t, t, T, UT);
  2025. /*
  2026. * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
  2027. * S.M.A.R.T * and some other commands. We have to ensure that the
  2028. * DMA cycle timing is slower/equal than the fastest PIO timing.
  2029. */
  2030. if (speed > XFER_PIO_6) {
  2031. ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
  2032. ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
  2033. }
  2034. /*
  2035. * Lengthen active & recovery time so that cycle time is correct.
  2036. */
  2037. if (t->act8b + t->rec8b < t->cyc8b) {
  2038. t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
  2039. t->rec8b = t->cyc8b - t->act8b;
  2040. }
  2041. if (t->active + t->recover < t->cycle) {
  2042. t->active += (t->cycle - (t->active + t->recover)) / 2;
  2043. t->recover = t->cycle - t->active;
  2044. }
  2045. return 0;
  2046. }
  2047. /**
  2048. * ata_down_xfermask_limit - adjust dev xfer masks downward
  2049. * @dev: Device to adjust xfer masks
  2050. * @sel: ATA_DNXFER_* selector
  2051. *
  2052. * Adjust xfer masks of @dev downward. Note that this function
  2053. * does not apply the change. Invoking ata_set_mode() afterwards
  2054. * will apply the limit.
  2055. *
  2056. * LOCKING:
  2057. * Inherited from caller.
  2058. *
  2059. * RETURNS:
  2060. * 0 on success, negative errno on failure
  2061. */
  2062. int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
  2063. {
  2064. char buf[32];
  2065. unsigned int orig_mask, xfer_mask;
  2066. unsigned int pio_mask, mwdma_mask, udma_mask;
  2067. int quiet, highbit;
  2068. quiet = !!(sel & ATA_DNXFER_QUIET);
  2069. sel &= ~ATA_DNXFER_QUIET;
  2070. xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
  2071. dev->mwdma_mask,
  2072. dev->udma_mask);
  2073. ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
  2074. switch (sel) {
  2075. case ATA_DNXFER_PIO:
  2076. highbit = fls(pio_mask) - 1;
  2077. pio_mask &= ~(1 << highbit);
  2078. break;
  2079. case ATA_DNXFER_DMA:
  2080. if (udma_mask) {
  2081. highbit = fls(udma_mask) - 1;
  2082. udma_mask &= ~(1 << highbit);
  2083. if (!udma_mask)
  2084. return -ENOENT;
  2085. } else if (mwdma_mask) {
  2086. highbit = fls(mwdma_mask) - 1;
  2087. mwdma_mask &= ~(1 << highbit);
  2088. if (!mwdma_mask)
  2089. return -ENOENT;
  2090. }
  2091. break;
  2092. case ATA_DNXFER_40C:
  2093. udma_mask &= ATA_UDMA_MASK_40C;
  2094. break;
  2095. case ATA_DNXFER_FORCE_PIO0:
  2096. pio_mask &= 1;
  2097. case ATA_DNXFER_FORCE_PIO:
  2098. mwdma_mask = 0;
  2099. udma_mask = 0;
  2100. break;
  2101. default:
  2102. BUG();
  2103. }
  2104. xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
  2105. if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
  2106. return -ENOENT;
  2107. if (!quiet) {
  2108. if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
  2109. snprintf(buf, sizeof(buf), "%s:%s",
  2110. ata_mode_string(xfer_mask),
  2111. ata_mode_string(xfer_mask & ATA_MASK_PIO));
  2112. else
  2113. snprintf(buf, sizeof(buf), "%s",
  2114. ata_mode_string(xfer_mask));
  2115. ata_dev_printk(dev, KERN_WARNING,
  2116. "limiting speed to %s\n", buf);
  2117. }
  2118. ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
  2119. &dev->udma_mask);
  2120. return 0;
  2121. }
  2122. static int ata_dev_set_mode(struct ata_device *dev)
  2123. {
  2124. struct ata_eh_context *ehc = &dev->ap->eh_context;
  2125. unsigned int err_mask;
  2126. int rc;
  2127. dev->flags &= ~ATA_DFLAG_PIO;
  2128. if (dev->xfer_shift == ATA_SHIFT_PIO)
  2129. dev->flags |= ATA_DFLAG_PIO;
  2130. err_mask = ata_dev_set_xfermode(dev);
  2131. /* Old CFA may refuse this command, which is just fine */
  2132. if (dev->xfer_shift == ATA_SHIFT_PIO && ata_id_is_cfa(dev->id))
  2133. err_mask &= ~AC_ERR_DEV;
  2134. if (err_mask) {
  2135. ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
  2136. "(err_mask=0x%x)\n", err_mask);
  2137. return -EIO;
  2138. }
  2139. ehc->i.flags |= ATA_EHI_POST_SETMODE;
  2140. rc = ata_dev_revalidate(dev, 0);
  2141. ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
  2142. if (rc)
  2143. return rc;
  2144. DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
  2145. dev->xfer_shift, (int)dev->xfer_mode);
  2146. ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
  2147. ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
  2148. return 0;
  2149. }
  2150. /**
  2151. * ata_set_mode - Program timings and issue SET FEATURES - XFER
  2152. * @ap: port on which timings will be programmed
  2153. * @r_failed_dev: out paramter for failed device
  2154. *
  2155. * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
  2156. * ata_set_mode() fails, pointer to the failing device is
  2157. * returned in @r_failed_dev.
  2158. *
  2159. * LOCKING:
  2160. * PCI/etc. bus probe sem.
  2161. *
  2162. * RETURNS:
  2163. * 0 on success, negative errno otherwise
  2164. */
  2165. int ata_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
  2166. {
  2167. struct ata_device *dev;
  2168. int i, rc = 0, used_dma = 0, found = 0;
  2169. /* has private set_mode? */
  2170. if (ap->ops->set_mode)
  2171. return ap->ops->set_mode(ap, r_failed_dev);
  2172. /* step 1: calculate xfer_mask */
  2173. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  2174. unsigned int pio_mask, dma_mask;
  2175. dev = &ap->device[i];
  2176. if (!ata_dev_enabled(dev))
  2177. continue;
  2178. ata_dev_xfermask(dev);
  2179. pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
  2180. dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
  2181. dev->pio_mode = ata_xfer_mask2mode(pio_mask);
  2182. dev->dma_mode = ata_xfer_mask2mode(dma_mask);
  2183. found = 1;
  2184. if (dev->dma_mode)
  2185. used_dma = 1;
  2186. }
  2187. if (!found)
  2188. goto out;
  2189. /* step 2: always set host PIO timings */
  2190. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  2191. dev = &ap->device[i];
  2192. if (!ata_dev_enabled(dev))
  2193. continue;
  2194. if (!dev->pio_mode) {
  2195. ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
  2196. rc = -EINVAL;
  2197. goto out;
  2198. }
  2199. dev->xfer_mode = dev->pio_mode;
  2200. dev->xfer_shift = ATA_SHIFT_PIO;
  2201. if (ap->ops->set_piomode)
  2202. ap->ops->set_piomode(ap, dev);
  2203. }
  2204. /* step 3: set host DMA timings */
  2205. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  2206. dev = &ap->device[i];
  2207. if (!ata_dev_enabled(dev) || !dev->dma_mode)
  2208. continue;
  2209. dev->xfer_mode = dev->dma_mode;
  2210. dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
  2211. if (ap->ops->set_dmamode)
  2212. ap->ops->set_dmamode(ap, dev);
  2213. }
  2214. /* step 4: update devices' xfer mode */
  2215. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  2216. dev = &ap->device[i];
  2217. /* don't update suspended devices' xfer mode */
  2218. if (!ata_dev_ready(dev))
  2219. continue;
  2220. rc = ata_dev_set_mode(dev);
  2221. if (rc)
  2222. goto out;
  2223. }
  2224. /* Record simplex status. If we selected DMA then the other
  2225. * host channels are not permitted to do so.
  2226. */
  2227. if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
  2228. ap->host->simplex_claimed = ap;
  2229. /* step5: chip specific finalisation */
  2230. if (ap->ops->post_set_mode)
  2231. ap->ops->post_set_mode(ap);
  2232. out:
  2233. if (rc)
  2234. *r_failed_dev = dev;
  2235. return rc;
  2236. }
  2237. /**
  2238. * ata_tf_to_host - issue ATA taskfile to host controller
  2239. * @ap: port to which command is being issued
  2240. * @tf: ATA taskfile register set
  2241. *
  2242. * Issues ATA taskfile register set to ATA host controller,
  2243. * with proper synchronization with interrupt handler and
  2244. * other threads.
  2245. *
  2246. * LOCKING:
  2247. * spin_lock_irqsave(host lock)
  2248. */
  2249. static inline void ata_tf_to_host(struct ata_port *ap,
  2250. const struct ata_taskfile *tf)
  2251. {
  2252. ap->ops->tf_load(ap, tf);
  2253. ap->ops->exec_command(ap, tf);
  2254. }
  2255. /**
  2256. * ata_busy_sleep - sleep until BSY clears, or timeout
  2257. * @ap: port containing status register to be polled
  2258. * @tmout_pat: impatience timeout
  2259. * @tmout: overall timeout
  2260. *
  2261. * Sleep until ATA Status register bit BSY clears,
  2262. * or a timeout occurs.
  2263. *
  2264. * LOCKING:
  2265. * Kernel thread context (may sleep).
  2266. *
  2267. * RETURNS:
  2268. * 0 on success, -errno otherwise.
  2269. */
  2270. int ata_busy_sleep(struct ata_port *ap,
  2271. unsigned long tmout_pat, unsigned long tmout)
  2272. {
  2273. unsigned long timer_start, timeout;
  2274. u8 status;
  2275. status = ata_busy_wait(ap, ATA_BUSY, 300);
  2276. timer_start = jiffies;
  2277. timeout = timer_start + tmout_pat;
  2278. while (status != 0xff && (status & ATA_BUSY) &&
  2279. time_before(jiffies, timeout)) {
  2280. msleep(50);
  2281. status = ata_busy_wait(ap, ATA_BUSY, 3);
  2282. }
  2283. if (status != 0xff && (status & ATA_BUSY))
  2284. ata_port_printk(ap, KERN_WARNING,
  2285. "port is slow to respond, please be patient "
  2286. "(Status 0x%x)\n", status);
  2287. timeout = timer_start + tmout;
  2288. while (status != 0xff && (status & ATA_BUSY) &&
  2289. time_before(jiffies, timeout)) {
  2290. msleep(50);
  2291. status = ata_chk_status(ap);
  2292. }
  2293. if (status == 0xff)
  2294. return -ENODEV;
  2295. if (status & ATA_BUSY) {
  2296. ata_port_printk(ap, KERN_ERR, "port failed to respond "
  2297. "(%lu secs, Status 0x%x)\n",
  2298. tmout / HZ, status);
  2299. return -EBUSY;
  2300. }
  2301. return 0;
  2302. }
  2303. static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
  2304. {
  2305. struct ata_ioports *ioaddr = &ap->ioaddr;
  2306. unsigned int dev0 = devmask & (1 << 0);
  2307. unsigned int dev1 = devmask & (1 << 1);
  2308. unsigned long timeout;
  2309. /* if device 0 was found in ata_devchk, wait for its
  2310. * BSY bit to clear
  2311. */
  2312. if (dev0)
  2313. ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  2314. /* if device 1 was found in ata_devchk, wait for
  2315. * register access, then wait for BSY to clear
  2316. */
  2317. timeout = jiffies + ATA_TMOUT_BOOT;
  2318. while (dev1) {
  2319. u8 nsect, lbal;
  2320. ap->ops->dev_select(ap, 1);
  2321. nsect = ioread8(ioaddr->nsect_addr);
  2322. lbal = ioread8(ioaddr->lbal_addr);
  2323. if ((nsect == 1) && (lbal == 1))
  2324. break;
  2325. if (time_after(jiffies, timeout)) {
  2326. dev1 = 0;
  2327. break;
  2328. }
  2329. msleep(50); /* give drive a breather */
  2330. }
  2331. if (dev1)
  2332. ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  2333. /* is all this really necessary? */
  2334. ap->ops->dev_select(ap, 0);
  2335. if (dev1)
  2336. ap->ops->dev_select(ap, 1);
  2337. if (dev0)
  2338. ap->ops->dev_select(ap, 0);
  2339. }
  2340. static unsigned int ata_bus_softreset(struct ata_port *ap,
  2341. unsigned int devmask)
  2342. {
  2343. struct ata_ioports *ioaddr = &ap->ioaddr;
  2344. DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
  2345. /* software reset. causes dev0 to be selected */
  2346. iowrite8(ap->ctl, ioaddr->ctl_addr);
  2347. udelay(20); /* FIXME: flush */
  2348. iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
  2349. udelay(20); /* FIXME: flush */
  2350. iowrite8(ap->ctl, ioaddr->ctl_addr);
  2351. /* spec mandates ">= 2ms" before checking status.
  2352. * We wait 150ms, because that was the magic delay used for
  2353. * ATAPI devices in Hale Landis's ATADRVR, for the period of time
  2354. * between when the ATA command register is written, and then
  2355. * status is checked. Because waiting for "a while" before
  2356. * checking status is fine, post SRST, we perform this magic
  2357. * delay here as well.
  2358. *
  2359. * Old drivers/ide uses the 2mS rule and then waits for ready
  2360. */
  2361. msleep(150);
  2362. /* Before we perform post reset processing we want to see if
  2363. * the bus shows 0xFF because the odd clown forgets the D7
  2364. * pulldown resistor.
  2365. */
  2366. if (ata_check_status(ap) == 0xFF)
  2367. return 0;
  2368. ata_bus_post_reset(ap, devmask);
  2369. return 0;
  2370. }
  2371. /**
  2372. * ata_bus_reset - reset host port and associated ATA channel
  2373. * @ap: port to reset
  2374. *
  2375. * This is typically the first time we actually start issuing
  2376. * commands to the ATA channel. We wait for BSY to clear, then
  2377. * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
  2378. * result. Determine what devices, if any, are on the channel
  2379. * by looking at the device 0/1 error register. Look at the signature
  2380. * stored in each device's taskfile registers, to determine if
  2381. * the device is ATA or ATAPI.
  2382. *
  2383. * LOCKING:
  2384. * PCI/etc. bus probe sem.
  2385. * Obtains host lock.
  2386. *
  2387. * SIDE EFFECTS:
  2388. * Sets ATA_FLAG_DISABLED if bus reset fails.
  2389. */
  2390. void ata_bus_reset(struct ata_port *ap)
  2391. {
  2392. struct ata_ioports *ioaddr = &ap->ioaddr;
  2393. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  2394. u8 err;
  2395. unsigned int dev0, dev1 = 0, devmask = 0;
  2396. DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
  2397. /* determine if device 0/1 are present */
  2398. if (ap->flags & ATA_FLAG_SATA_RESET)
  2399. dev0 = 1;
  2400. else {
  2401. dev0 = ata_devchk(ap, 0);
  2402. if (slave_possible)
  2403. dev1 = ata_devchk(ap, 1);
  2404. }
  2405. if (dev0)
  2406. devmask |= (1 << 0);
  2407. if (dev1)
  2408. devmask |= (1 << 1);
  2409. /* select device 0 again */
  2410. ap->ops->dev_select(ap, 0);
  2411. /* issue bus reset */
  2412. if (ap->flags & ATA_FLAG_SRST)
  2413. if (ata_bus_softreset(ap, devmask))
  2414. goto err_out;
  2415. /*
  2416. * determine by signature whether we have ATA or ATAPI devices
  2417. */
  2418. ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
  2419. if ((slave_possible) && (err != 0x81))
  2420. ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
  2421. /* re-enable interrupts */
  2422. ap->ops->irq_on(ap);
  2423. /* is double-select really necessary? */
  2424. if (ap->device[1].class != ATA_DEV_NONE)
  2425. ap->ops->dev_select(ap, 1);
  2426. if (ap->device[0].class != ATA_DEV_NONE)
  2427. ap->ops->dev_select(ap, 0);
  2428. /* if no devices were detected, disable this port */
  2429. if ((ap->device[0].class == ATA_DEV_NONE) &&
  2430. (ap->device[1].class == ATA_DEV_NONE))
  2431. goto err_out;
  2432. if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
  2433. /* set up device control for ATA_FLAG_SATA_RESET */
  2434. iowrite8(ap->ctl, ioaddr->ctl_addr);
  2435. }
  2436. DPRINTK("EXIT\n");
  2437. return;
  2438. err_out:
  2439. ata_port_printk(ap, KERN_ERR, "disabling port\n");
  2440. ap->ops->port_disable(ap);
  2441. DPRINTK("EXIT\n");
  2442. }
  2443. /**
  2444. * sata_phy_debounce - debounce SATA phy status
  2445. * @ap: ATA port to debounce SATA phy status for
  2446. * @params: timing parameters { interval, duratinon, timeout } in msec
  2447. *
  2448. * Make sure SStatus of @ap reaches stable state, determined by
  2449. * holding the same value where DET is not 1 for @duration polled
  2450. * every @interval, before @timeout. Timeout constraints the
  2451. * beginning of the stable state. Because, after hot unplugging,
  2452. * DET gets stuck at 1 on some controllers, this functions waits
  2453. * until timeout then returns 0 if DET is stable at 1.
  2454. *
  2455. * LOCKING:
  2456. * Kernel thread context (may sleep)
  2457. *
  2458. * RETURNS:
  2459. * 0 on success, -errno on failure.
  2460. */
  2461. int sata_phy_debounce(struct ata_port *ap, const unsigned long *params)
  2462. {
  2463. unsigned long interval_msec = params[0];
  2464. unsigned long duration = params[1] * HZ / 1000;
  2465. unsigned long timeout = jiffies + params[2] * HZ / 1000;
  2466. unsigned long last_jiffies;
  2467. u32 last, cur;
  2468. int rc;
  2469. if ((rc = sata_scr_read(ap, SCR_STATUS, &cur)))
  2470. return rc;
  2471. cur &= 0xf;
  2472. last = cur;
  2473. last_jiffies = jiffies;
  2474. while (1) {
  2475. msleep(interval_msec);
  2476. if ((rc = sata_scr_read(ap, SCR_STATUS, &cur)))
  2477. return rc;
  2478. cur &= 0xf;
  2479. /* DET stable? */
  2480. if (cur == last) {
  2481. if (cur == 1 && time_before(jiffies, timeout))
  2482. continue;
  2483. if (time_after(jiffies, last_jiffies + duration))
  2484. return 0;
  2485. continue;
  2486. }
  2487. /* unstable, start over */
  2488. last = cur;
  2489. last_jiffies = jiffies;
  2490. /* check timeout */
  2491. if (time_after(jiffies, timeout))
  2492. return -EBUSY;
  2493. }
  2494. }
  2495. /**
  2496. * sata_phy_resume - resume SATA phy
  2497. * @ap: ATA port to resume SATA phy for
  2498. * @params: timing parameters { interval, duratinon, timeout } in msec
  2499. *
  2500. * Resume SATA phy of @ap and debounce it.
  2501. *
  2502. * LOCKING:
  2503. * Kernel thread context (may sleep)
  2504. *
  2505. * RETURNS:
  2506. * 0 on success, -errno on failure.
  2507. */
  2508. int sata_phy_resume(struct ata_port *ap, const unsigned long *params)
  2509. {
  2510. u32 scontrol;
  2511. int rc;
  2512. if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
  2513. return rc;
  2514. scontrol = (scontrol & 0x0f0) | 0x300;
  2515. if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
  2516. return rc;
  2517. /* Some PHYs react badly if SStatus is pounded immediately
  2518. * after resuming. Delay 200ms before debouncing.
  2519. */
  2520. msleep(200);
  2521. return sata_phy_debounce(ap, params);
  2522. }
  2523. static void ata_wait_spinup(struct ata_port *ap)
  2524. {
  2525. struct ata_eh_context *ehc = &ap->eh_context;
  2526. unsigned long end, secs;
  2527. int rc;
  2528. /* first, debounce phy if SATA */
  2529. if (ap->cbl == ATA_CBL_SATA) {
  2530. rc = sata_phy_debounce(ap, sata_deb_timing_hotplug);
  2531. /* if debounced successfully and offline, no need to wait */
  2532. if ((rc == 0 || rc == -EOPNOTSUPP) && ata_port_offline(ap))
  2533. return;
  2534. }
  2535. /* okay, let's give the drive time to spin up */
  2536. end = ehc->i.hotplug_timestamp + ATA_SPINUP_WAIT * HZ / 1000;
  2537. secs = ((end - jiffies) + HZ - 1) / HZ;
  2538. if (time_after(jiffies, end))
  2539. return;
  2540. if (secs > 5)
  2541. ata_port_printk(ap, KERN_INFO, "waiting for device to spin up "
  2542. "(%lu secs)\n", secs);
  2543. schedule_timeout_uninterruptible(end - jiffies);
  2544. }
  2545. /**
  2546. * ata_std_prereset - prepare for reset
  2547. * @ap: ATA port to be reset
  2548. *
  2549. * @ap is about to be reset. Initialize it.
  2550. *
  2551. * LOCKING:
  2552. * Kernel thread context (may sleep)
  2553. *
  2554. * RETURNS:
  2555. * 0 on success, -errno otherwise.
  2556. */
  2557. int ata_std_prereset(struct ata_port *ap)
  2558. {
  2559. struct ata_eh_context *ehc = &ap->eh_context;
  2560. const unsigned long *timing = sata_ehc_deb_timing(ehc);
  2561. int rc;
  2562. /* handle link resume & hotplug spinup */
  2563. if ((ehc->i.flags & ATA_EHI_RESUME_LINK) &&
  2564. (ap->flags & ATA_FLAG_HRST_TO_RESUME))
  2565. ehc->i.action |= ATA_EH_HARDRESET;
  2566. if ((ehc->i.flags & ATA_EHI_HOTPLUGGED) &&
  2567. (ap->flags & ATA_FLAG_SKIP_D2H_BSY))
  2568. ata_wait_spinup(ap);
  2569. /* if we're about to do hardreset, nothing more to do */
  2570. if (ehc->i.action & ATA_EH_HARDRESET)
  2571. return 0;
  2572. /* if SATA, resume phy */
  2573. if (ap->cbl == ATA_CBL_SATA) {
  2574. rc = sata_phy_resume(ap, timing);
  2575. if (rc && rc != -EOPNOTSUPP) {
  2576. /* phy resume failed */
  2577. ata_port_printk(ap, KERN_WARNING, "failed to resume "
  2578. "link for reset (errno=%d)\n", rc);
  2579. return rc;
  2580. }
  2581. }
  2582. /* Wait for !BSY if the controller can wait for the first D2H
  2583. * Reg FIS and we don't know that no device is attached.
  2584. */
  2585. if (!(ap->flags & ATA_FLAG_SKIP_D2H_BSY) && !ata_port_offline(ap))
  2586. ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  2587. return 0;
  2588. }
  2589. /**
  2590. * ata_std_softreset - reset host port via ATA SRST
  2591. * @ap: port to reset
  2592. * @classes: resulting classes of attached devices
  2593. *
  2594. * Reset host port using ATA SRST.
  2595. *
  2596. * LOCKING:
  2597. * Kernel thread context (may sleep)
  2598. *
  2599. * RETURNS:
  2600. * 0 on success, -errno otherwise.
  2601. */
  2602. int ata_std_softreset(struct ata_port *ap, unsigned int *classes)
  2603. {
  2604. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  2605. unsigned int devmask = 0, err_mask;
  2606. u8 err;
  2607. DPRINTK("ENTER\n");
  2608. if (ata_port_offline(ap)) {
  2609. classes[0] = ATA_DEV_NONE;
  2610. goto out;
  2611. }
  2612. /* determine if device 0/1 are present */
  2613. if (ata_devchk(ap, 0))
  2614. devmask |= (1 << 0);
  2615. if (slave_possible && ata_devchk(ap, 1))
  2616. devmask |= (1 << 1);
  2617. /* select device 0 again */
  2618. ap->ops->dev_select(ap, 0);
  2619. /* issue bus reset */
  2620. DPRINTK("about to softreset, devmask=%x\n", devmask);
  2621. err_mask = ata_bus_softreset(ap, devmask);
  2622. if (err_mask) {
  2623. ata_port_printk(ap, KERN_ERR, "SRST failed (err_mask=0x%x)\n",
  2624. err_mask);
  2625. return -EIO;
  2626. }
  2627. /* determine by signature whether we have ATA or ATAPI devices */
  2628. classes[0] = ata_dev_try_classify(ap, 0, &err);
  2629. if (slave_possible && err != 0x81)
  2630. classes[1] = ata_dev_try_classify(ap, 1, &err);
  2631. out:
  2632. DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
  2633. return 0;
  2634. }
  2635. /**
  2636. * sata_port_hardreset - reset port via SATA phy reset
  2637. * @ap: port to reset
  2638. * @timing: timing parameters { interval, duratinon, timeout } in msec
  2639. *
  2640. * SATA phy-reset host port using DET bits of SControl register.
  2641. *
  2642. * LOCKING:
  2643. * Kernel thread context (may sleep)
  2644. *
  2645. * RETURNS:
  2646. * 0 on success, -errno otherwise.
  2647. */
  2648. int sata_port_hardreset(struct ata_port *ap, const unsigned long *timing)
  2649. {
  2650. u32 scontrol;
  2651. int rc;
  2652. DPRINTK("ENTER\n");
  2653. if (sata_set_spd_needed(ap)) {
  2654. /* SATA spec says nothing about how to reconfigure
  2655. * spd. To be on the safe side, turn off phy during
  2656. * reconfiguration. This works for at least ICH7 AHCI
  2657. * and Sil3124.
  2658. */
  2659. if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
  2660. goto out;
  2661. scontrol = (scontrol & 0x0f0) | 0x304;
  2662. if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
  2663. goto out;
  2664. sata_set_spd(ap);
  2665. }
  2666. /* issue phy wake/reset */
  2667. if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
  2668. goto out;
  2669. scontrol = (scontrol & 0x0f0) | 0x301;
  2670. if ((rc = sata_scr_write_flush(ap, SCR_CONTROL, scontrol)))
  2671. goto out;
  2672. /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
  2673. * 10.4.2 says at least 1 ms.
  2674. */
  2675. msleep(1);
  2676. /* bring phy back */
  2677. rc = sata_phy_resume(ap, timing);
  2678. out:
  2679. DPRINTK("EXIT, rc=%d\n", rc);
  2680. return rc;
  2681. }
  2682. /**
  2683. * sata_std_hardreset - reset host port via SATA phy reset
  2684. * @ap: port to reset
  2685. * @class: resulting class of attached device
  2686. *
  2687. * SATA phy-reset host port using DET bits of SControl register,
  2688. * wait for !BSY and classify the attached device.
  2689. *
  2690. * LOCKING:
  2691. * Kernel thread context (may sleep)
  2692. *
  2693. * RETURNS:
  2694. * 0 on success, -errno otherwise.
  2695. */
  2696. int sata_std_hardreset(struct ata_port *ap, unsigned int *class)
  2697. {
  2698. const unsigned long *timing = sata_ehc_deb_timing(&ap->eh_context);
  2699. int rc;
  2700. DPRINTK("ENTER\n");
  2701. /* do hardreset */
  2702. rc = sata_port_hardreset(ap, timing);
  2703. if (rc) {
  2704. ata_port_printk(ap, KERN_ERR,
  2705. "COMRESET failed (errno=%d)\n", rc);
  2706. return rc;
  2707. }
  2708. /* TODO: phy layer with polling, timeouts, etc. */
  2709. if (ata_port_offline(ap)) {
  2710. *class = ATA_DEV_NONE;
  2711. DPRINTK("EXIT, link offline\n");
  2712. return 0;
  2713. }
  2714. /* wait a while before checking status, see SRST for more info */
  2715. msleep(150);
  2716. if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
  2717. ata_port_printk(ap, KERN_ERR,
  2718. "COMRESET failed (device not ready)\n");
  2719. return -EIO;
  2720. }
  2721. ap->ops->dev_select(ap, 0); /* probably unnecessary */
  2722. *class = ata_dev_try_classify(ap, 0, NULL);
  2723. DPRINTK("EXIT, class=%u\n", *class);
  2724. return 0;
  2725. }
  2726. /**
  2727. * ata_std_postreset - standard postreset callback
  2728. * @ap: the target ata_port
  2729. * @classes: classes of attached devices
  2730. *
  2731. * This function is invoked after a successful reset. Note that
  2732. * the device might have been reset more than once using
  2733. * different reset methods before postreset is invoked.
  2734. *
  2735. * LOCKING:
  2736. * Kernel thread context (may sleep)
  2737. */
  2738. void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
  2739. {
  2740. u32 serror;
  2741. DPRINTK("ENTER\n");
  2742. /* print link status */
  2743. sata_print_link_status(ap);
  2744. /* clear SError */
  2745. if (sata_scr_read(ap, SCR_ERROR, &serror) == 0)
  2746. sata_scr_write(ap, SCR_ERROR, serror);
  2747. /* re-enable interrupts */
  2748. if (!ap->ops->error_handler)
  2749. ap->ops->irq_on(ap);
  2750. /* is double-select really necessary? */
  2751. if (classes[0] != ATA_DEV_NONE)
  2752. ap->ops->dev_select(ap, 1);
  2753. if (classes[1] != ATA_DEV_NONE)
  2754. ap->ops->dev_select(ap, 0);
  2755. /* bail out if no device is present */
  2756. if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
  2757. DPRINTK("EXIT, no device\n");
  2758. return;
  2759. }
  2760. /* set up device control */
  2761. if (ap->ioaddr.ctl_addr)
  2762. iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
  2763. DPRINTK("EXIT\n");
  2764. }
  2765. /**
  2766. * ata_dev_same_device - Determine whether new ID matches configured device
  2767. * @dev: device to compare against
  2768. * @new_class: class of the new device
  2769. * @new_id: IDENTIFY page of the new device
  2770. *
  2771. * Compare @new_class and @new_id against @dev and determine
  2772. * whether @dev is the device indicated by @new_class and
  2773. * @new_id.
  2774. *
  2775. * LOCKING:
  2776. * None.
  2777. *
  2778. * RETURNS:
  2779. * 1 if @dev matches @new_class and @new_id, 0 otherwise.
  2780. */
  2781. static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
  2782. const u16 *new_id)
  2783. {
  2784. const u16 *old_id = dev->id;
  2785. unsigned char model[2][ATA_ID_PROD_LEN + 1];
  2786. unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
  2787. u64 new_n_sectors;
  2788. if (dev->class != new_class) {
  2789. ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
  2790. dev->class, new_class);
  2791. return 0;
  2792. }
  2793. ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
  2794. ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
  2795. ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
  2796. ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
  2797. new_n_sectors = ata_id_n_sectors(new_id);
  2798. if (strcmp(model[0], model[1])) {
  2799. ata_dev_printk(dev, KERN_INFO, "model number mismatch "
  2800. "'%s' != '%s'\n", model[0], model[1]);
  2801. return 0;
  2802. }
  2803. if (strcmp(serial[0], serial[1])) {
  2804. ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
  2805. "'%s' != '%s'\n", serial[0], serial[1]);
  2806. return 0;
  2807. }
  2808. if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
  2809. ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
  2810. "%llu != %llu\n",
  2811. (unsigned long long)dev->n_sectors,
  2812. (unsigned long long)new_n_sectors);
  2813. return 0;
  2814. }
  2815. return 1;
  2816. }
  2817. /**
  2818. * ata_dev_revalidate - Revalidate ATA device
  2819. * @dev: device to revalidate
  2820. * @readid_flags: read ID flags
  2821. *
  2822. * Re-read IDENTIFY page and make sure @dev is still attached to
  2823. * the port.
  2824. *
  2825. * LOCKING:
  2826. * Kernel thread context (may sleep)
  2827. *
  2828. * RETURNS:
  2829. * 0 on success, negative errno otherwise
  2830. */
  2831. int ata_dev_revalidate(struct ata_device *dev, unsigned int readid_flags)
  2832. {
  2833. unsigned int class = dev->class;
  2834. u16 *id = (void *)dev->ap->sector_buf;
  2835. int rc;
  2836. if (!ata_dev_enabled(dev)) {
  2837. rc = -ENODEV;
  2838. goto fail;
  2839. }
  2840. /* read ID data */
  2841. rc = ata_dev_read_id(dev, &class, readid_flags, id);
  2842. if (rc)
  2843. goto fail;
  2844. /* is the device still there? */
  2845. if (!ata_dev_same_device(dev, class, id)) {
  2846. rc = -ENODEV;
  2847. goto fail;
  2848. }
  2849. memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
  2850. /* configure device according to the new ID */
  2851. rc = ata_dev_configure(dev);
  2852. if (rc == 0)
  2853. return 0;
  2854. fail:
  2855. ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
  2856. return rc;
  2857. }
  2858. struct ata_blacklist_entry {
  2859. const char *model_num;
  2860. const char *model_rev;
  2861. unsigned long horkage;
  2862. };
  2863. static const struct ata_blacklist_entry ata_device_blacklist [] = {
  2864. /* Devices with DMA related problems under Linux */
  2865. { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
  2866. { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
  2867. { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
  2868. { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
  2869. { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
  2870. { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
  2871. { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
  2872. { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
  2873. { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
  2874. { "CRD-8480B", NULL, ATA_HORKAGE_NODMA },
  2875. { "CRD-8482B", NULL, ATA_HORKAGE_NODMA },
  2876. { "CRD-84", NULL, ATA_HORKAGE_NODMA },
  2877. { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
  2878. { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
  2879. { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
  2880. { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
  2881. { "HITACHI CDR-8335", NULL, ATA_HORKAGE_NODMA },
  2882. { "HITACHI CDR-8435", NULL, ATA_HORKAGE_NODMA },
  2883. { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
  2884. { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
  2885. { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
  2886. { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
  2887. { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
  2888. { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
  2889. { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
  2890. { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
  2891. { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
  2892. { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
  2893. { "SAMSUNG CD-ROM SN-124","N001", ATA_HORKAGE_NODMA },
  2894. /* Weird ATAPI devices */
  2895. { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 |
  2896. ATA_HORKAGE_DMA_RW_ONLY },
  2897. /* Devices we expect to fail diagnostics */
  2898. /* Devices where NCQ should be avoided */
  2899. /* NCQ is slow */
  2900. { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
  2901. /* http://thread.gmane.org/gmane.linux.ide/14907 */
  2902. { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
  2903. /* NCQ is broken */
  2904. { "Maxtor 6L250S0", "BANC1G10", ATA_HORKAGE_NONCQ },
  2905. /* NCQ hard hangs device under heavier load, needs hard power cycle */
  2906. { "Maxtor 6B250S0", "BANC1B70", ATA_HORKAGE_NONCQ },
  2907. /* Blacklist entries taken from Silicon Image 3124/3132
  2908. Windows driver .inf file - also several Linux problem reports */
  2909. { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
  2910. { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
  2911. { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
  2912. /* Devices with NCQ limits */
  2913. /* End Marker */
  2914. { }
  2915. };
  2916. unsigned long ata_device_blacklisted(const struct ata_device *dev)
  2917. {
  2918. unsigned char model_num[ATA_ID_PROD_LEN + 1];
  2919. unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
  2920. const struct ata_blacklist_entry *ad = ata_device_blacklist;
  2921. ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
  2922. ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
  2923. while (ad->model_num) {
  2924. if (!strcmp(ad->model_num, model_num)) {
  2925. if (ad->model_rev == NULL)
  2926. return ad->horkage;
  2927. if (!strcmp(ad->model_rev, model_rev))
  2928. return ad->horkage;
  2929. }
  2930. ad++;
  2931. }
  2932. return 0;
  2933. }
  2934. static int ata_dma_blacklisted(const struct ata_device *dev)
  2935. {
  2936. /* We don't support polling DMA.
  2937. * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
  2938. * if the LLDD handles only interrupts in the HSM_ST_LAST state.
  2939. */
  2940. if ((dev->ap->flags & ATA_FLAG_PIO_POLLING) &&
  2941. (dev->flags & ATA_DFLAG_CDB_INTR))
  2942. return 1;
  2943. return (ata_device_blacklisted(dev) & ATA_HORKAGE_NODMA) ? 1 : 0;
  2944. }
  2945. /**
  2946. * ata_dev_xfermask - Compute supported xfermask of the given device
  2947. * @dev: Device to compute xfermask for
  2948. *
  2949. * Compute supported xfermask of @dev and store it in
  2950. * dev->*_mask. This function is responsible for applying all
  2951. * known limits including host controller limits, device
  2952. * blacklist, etc...
  2953. *
  2954. * LOCKING:
  2955. * None.
  2956. */
  2957. static void ata_dev_xfermask(struct ata_device *dev)
  2958. {
  2959. struct ata_port *ap = dev->ap;
  2960. struct ata_host *host = ap->host;
  2961. unsigned long xfer_mask;
  2962. /* controller modes available */
  2963. xfer_mask = ata_pack_xfermask(ap->pio_mask,
  2964. ap->mwdma_mask, ap->udma_mask);
  2965. /* drive modes available */
  2966. xfer_mask &= ata_pack_xfermask(dev->pio_mask,
  2967. dev->mwdma_mask, dev->udma_mask);
  2968. xfer_mask &= ata_id_xfermask(dev->id);
  2969. /*
  2970. * CFA Advanced TrueIDE timings are not allowed on a shared
  2971. * cable
  2972. */
  2973. if (ata_dev_pair(dev)) {
  2974. /* No PIO5 or PIO6 */
  2975. xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
  2976. /* No MWDMA3 or MWDMA 4 */
  2977. xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
  2978. }
  2979. if (ata_dma_blacklisted(dev)) {
  2980. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  2981. ata_dev_printk(dev, KERN_WARNING,
  2982. "device is on DMA blacklist, disabling DMA\n");
  2983. }
  2984. if ((host->flags & ATA_HOST_SIMPLEX) &&
  2985. host->simplex_claimed && host->simplex_claimed != ap) {
  2986. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  2987. ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
  2988. "other device, disabling DMA\n");
  2989. }
  2990. if (ap->ops->mode_filter)
  2991. xfer_mask = ap->ops->mode_filter(ap, dev, xfer_mask);
  2992. /* Apply cable rule here. Don't apply it early because when
  2993. * we handle hot plug the cable type can itself change.
  2994. * Check this last so that we know if the transfer rate was
  2995. * solely limited by the cable.
  2996. * Unknown or 80 wire cables reported host side are checked
  2997. * drive side as well. Cases where we know a 40wire cable
  2998. * is used safely for 80 are not checked here.
  2999. */
  3000. if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
  3001. /* UDMA/44 or higher would be available */
  3002. if((ap->cbl == ATA_CBL_PATA40) ||
  3003. (ata_drive_40wire(dev->id) &&
  3004. (ap->cbl == ATA_CBL_PATA_UNK ||
  3005. ap->cbl == ATA_CBL_PATA80))) {
  3006. ata_dev_printk(dev, KERN_WARNING,
  3007. "limited to UDMA/33 due to 40-wire cable\n");
  3008. xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
  3009. }
  3010. ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
  3011. &dev->mwdma_mask, &dev->udma_mask);
  3012. }
  3013. /**
  3014. * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
  3015. * @dev: Device to which command will be sent
  3016. *
  3017. * Issue SET FEATURES - XFER MODE command to device @dev
  3018. * on port @ap.
  3019. *
  3020. * LOCKING:
  3021. * PCI/etc. bus probe sem.
  3022. *
  3023. * RETURNS:
  3024. * 0 on success, AC_ERR_* mask otherwise.
  3025. */
  3026. static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
  3027. {
  3028. struct ata_taskfile tf;
  3029. unsigned int err_mask;
  3030. /* set up set-features taskfile */
  3031. DPRINTK("set features - xfer mode\n");
  3032. ata_tf_init(dev, &tf);
  3033. tf.command = ATA_CMD_SET_FEATURES;
  3034. tf.feature = SETFEATURES_XFER;
  3035. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  3036. tf.protocol = ATA_PROT_NODATA;
  3037. tf.nsect = dev->xfer_mode;
  3038. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
  3039. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  3040. return err_mask;
  3041. }
  3042. /**
  3043. * ata_dev_init_params - Issue INIT DEV PARAMS command
  3044. * @dev: Device to which command will be sent
  3045. * @heads: Number of heads (taskfile parameter)
  3046. * @sectors: Number of sectors (taskfile parameter)
  3047. *
  3048. * LOCKING:
  3049. * Kernel thread context (may sleep)
  3050. *
  3051. * RETURNS:
  3052. * 0 on success, AC_ERR_* mask otherwise.
  3053. */
  3054. static unsigned int ata_dev_init_params(struct ata_device *dev,
  3055. u16 heads, u16 sectors)
  3056. {
  3057. struct ata_taskfile tf;
  3058. unsigned int err_mask;
  3059. /* Number of sectors per track 1-255. Number of heads 1-16 */
  3060. if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
  3061. return AC_ERR_INVALID;
  3062. /* set up init dev params taskfile */
  3063. DPRINTK("init dev params \n");
  3064. ata_tf_init(dev, &tf);
  3065. tf.command = ATA_CMD_INIT_DEV_PARAMS;
  3066. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  3067. tf.protocol = ATA_PROT_NODATA;
  3068. tf.nsect = sectors;
  3069. tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
  3070. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
  3071. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  3072. return err_mask;
  3073. }
  3074. /**
  3075. * ata_sg_clean - Unmap DMA memory associated with command
  3076. * @qc: Command containing DMA memory to be released
  3077. *
  3078. * Unmap all mapped DMA memory associated with this command.
  3079. *
  3080. * LOCKING:
  3081. * spin_lock_irqsave(host lock)
  3082. */
  3083. void ata_sg_clean(struct ata_queued_cmd *qc)
  3084. {
  3085. struct ata_port *ap = qc->ap;
  3086. struct scatterlist *sg = qc->__sg;
  3087. int dir = qc->dma_dir;
  3088. void *pad_buf = NULL;
  3089. WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
  3090. WARN_ON(sg == NULL);
  3091. if (qc->flags & ATA_QCFLAG_SINGLE)
  3092. WARN_ON(qc->n_elem > 1);
  3093. VPRINTK("unmapping %u sg elements\n", qc->n_elem);
  3094. /* if we padded the buffer out to 32-bit bound, and data
  3095. * xfer direction is from-device, we must copy from the
  3096. * pad buffer back into the supplied buffer
  3097. */
  3098. if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
  3099. pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  3100. if (qc->flags & ATA_QCFLAG_SG) {
  3101. if (qc->n_elem)
  3102. dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
  3103. /* restore last sg */
  3104. sg[qc->orig_n_elem - 1].length += qc->pad_len;
  3105. if (pad_buf) {
  3106. struct scatterlist *psg = &qc->pad_sgent;
  3107. void *addr = kmap_atomic(psg->page, KM_IRQ0);
  3108. memcpy(addr + psg->offset, pad_buf, qc->pad_len);
  3109. kunmap_atomic(addr, KM_IRQ0);
  3110. }
  3111. } else {
  3112. if (qc->n_elem)
  3113. dma_unmap_single(ap->dev,
  3114. sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
  3115. dir);
  3116. /* restore sg */
  3117. sg->length += qc->pad_len;
  3118. if (pad_buf)
  3119. memcpy(qc->buf_virt + sg->length - qc->pad_len,
  3120. pad_buf, qc->pad_len);
  3121. }
  3122. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  3123. qc->__sg = NULL;
  3124. }
  3125. /**
  3126. * ata_fill_sg - Fill PCI IDE PRD table
  3127. * @qc: Metadata associated with taskfile to be transferred
  3128. *
  3129. * Fill PCI IDE PRD (scatter-gather) table with segments
  3130. * associated with the current disk command.
  3131. *
  3132. * LOCKING:
  3133. * spin_lock_irqsave(host lock)
  3134. *
  3135. */
  3136. static void ata_fill_sg(struct ata_queued_cmd *qc)
  3137. {
  3138. struct ata_port *ap = qc->ap;
  3139. struct scatterlist *sg;
  3140. unsigned int idx;
  3141. WARN_ON(qc->__sg == NULL);
  3142. WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
  3143. idx = 0;
  3144. ata_for_each_sg(sg, qc) {
  3145. u32 addr, offset;
  3146. u32 sg_len, len;
  3147. /* determine if physical DMA addr spans 64K boundary.
  3148. * Note h/w doesn't support 64-bit, so we unconditionally
  3149. * truncate dma_addr_t to u32.
  3150. */
  3151. addr = (u32) sg_dma_address(sg);
  3152. sg_len = sg_dma_len(sg);
  3153. while (sg_len) {
  3154. offset = addr & 0xffff;
  3155. len = sg_len;
  3156. if ((offset + sg_len) > 0x10000)
  3157. len = 0x10000 - offset;
  3158. ap->prd[idx].addr = cpu_to_le32(addr);
  3159. ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
  3160. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
  3161. idx++;
  3162. sg_len -= len;
  3163. addr += len;
  3164. }
  3165. }
  3166. if (idx)
  3167. ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
  3168. }
  3169. /**
  3170. * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
  3171. * @qc: Metadata associated with taskfile to check
  3172. *
  3173. * Allow low-level driver to filter ATA PACKET commands, returning
  3174. * a status indicating whether or not it is OK to use DMA for the
  3175. * supplied PACKET command.
  3176. *
  3177. * LOCKING:
  3178. * spin_lock_irqsave(host lock)
  3179. *
  3180. * RETURNS: 0 when ATAPI DMA can be used
  3181. * nonzero otherwise
  3182. */
  3183. int ata_check_atapi_dma(struct ata_queued_cmd *qc)
  3184. {
  3185. struct ata_port *ap = qc->ap;
  3186. int rc = 0; /* Assume ATAPI DMA is OK by default */
  3187. /* some drives can only do ATAPI DMA on read/write */
  3188. if (unlikely(qc->dev->horkage & ATA_HORKAGE_DMA_RW_ONLY)) {
  3189. struct scsi_cmnd *cmd = qc->scsicmd;
  3190. u8 *scsicmd = cmd->cmnd;
  3191. switch (scsicmd[0]) {
  3192. case READ_10:
  3193. case WRITE_10:
  3194. case READ_12:
  3195. case WRITE_12:
  3196. case READ_6:
  3197. case WRITE_6:
  3198. /* atapi dma maybe ok */
  3199. break;
  3200. default:
  3201. /* turn off atapi dma */
  3202. return 1;
  3203. }
  3204. }
  3205. if (ap->ops->check_atapi_dma)
  3206. rc = ap->ops->check_atapi_dma(qc);
  3207. return rc;
  3208. }
  3209. /**
  3210. * ata_qc_prep - Prepare taskfile for submission
  3211. * @qc: Metadata associated with taskfile to be prepared
  3212. *
  3213. * Prepare ATA taskfile for submission.
  3214. *
  3215. * LOCKING:
  3216. * spin_lock_irqsave(host lock)
  3217. */
  3218. void ata_qc_prep(struct ata_queued_cmd *qc)
  3219. {
  3220. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  3221. return;
  3222. ata_fill_sg(qc);
  3223. }
  3224. void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
  3225. /**
  3226. * ata_sg_init_one - Associate command with memory buffer
  3227. * @qc: Command to be associated
  3228. * @buf: Memory buffer
  3229. * @buflen: Length of memory buffer, in bytes.
  3230. *
  3231. * Initialize the data-related elements of queued_cmd @qc
  3232. * to point to a single memory buffer, @buf of byte length @buflen.
  3233. *
  3234. * LOCKING:
  3235. * spin_lock_irqsave(host lock)
  3236. */
  3237. void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
  3238. {
  3239. qc->flags |= ATA_QCFLAG_SINGLE;
  3240. qc->__sg = &qc->sgent;
  3241. qc->n_elem = 1;
  3242. qc->orig_n_elem = 1;
  3243. qc->buf_virt = buf;
  3244. qc->nbytes = buflen;
  3245. sg_init_one(&qc->sgent, buf, buflen);
  3246. }
  3247. /**
  3248. * ata_sg_init - Associate command with scatter-gather table.
  3249. * @qc: Command to be associated
  3250. * @sg: Scatter-gather table.
  3251. * @n_elem: Number of elements in s/g table.
  3252. *
  3253. * Initialize the data-related elements of queued_cmd @qc
  3254. * to point to a scatter-gather table @sg, containing @n_elem
  3255. * elements.
  3256. *
  3257. * LOCKING:
  3258. * spin_lock_irqsave(host lock)
  3259. */
  3260. void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
  3261. unsigned int n_elem)
  3262. {
  3263. qc->flags |= ATA_QCFLAG_SG;
  3264. qc->__sg = sg;
  3265. qc->n_elem = n_elem;
  3266. qc->orig_n_elem = n_elem;
  3267. }
  3268. /**
  3269. * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
  3270. * @qc: Command with memory buffer to be mapped.
  3271. *
  3272. * DMA-map the memory buffer associated with queued_cmd @qc.
  3273. *
  3274. * LOCKING:
  3275. * spin_lock_irqsave(host lock)
  3276. *
  3277. * RETURNS:
  3278. * Zero on success, negative on error.
  3279. */
  3280. static int ata_sg_setup_one(struct ata_queued_cmd *qc)
  3281. {
  3282. struct ata_port *ap = qc->ap;
  3283. int dir = qc->dma_dir;
  3284. struct scatterlist *sg = qc->__sg;
  3285. dma_addr_t dma_address;
  3286. int trim_sg = 0;
  3287. /* we must lengthen transfers to end on a 32-bit boundary */
  3288. qc->pad_len = sg->length & 3;
  3289. if (qc->pad_len) {
  3290. void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  3291. struct scatterlist *psg = &qc->pad_sgent;
  3292. WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
  3293. memset(pad_buf, 0, ATA_DMA_PAD_SZ);
  3294. if (qc->tf.flags & ATA_TFLAG_WRITE)
  3295. memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
  3296. qc->pad_len);
  3297. sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
  3298. sg_dma_len(psg) = ATA_DMA_PAD_SZ;
  3299. /* trim sg */
  3300. sg->length -= qc->pad_len;
  3301. if (sg->length == 0)
  3302. trim_sg = 1;
  3303. DPRINTK("padding done, sg->length=%u pad_len=%u\n",
  3304. sg->length, qc->pad_len);
  3305. }
  3306. if (trim_sg) {
  3307. qc->n_elem--;
  3308. goto skip_map;
  3309. }
  3310. dma_address = dma_map_single(ap->dev, qc->buf_virt,
  3311. sg->length, dir);
  3312. if (dma_mapping_error(dma_address)) {
  3313. /* restore sg */
  3314. sg->length += qc->pad_len;
  3315. return -1;
  3316. }
  3317. sg_dma_address(sg) = dma_address;
  3318. sg_dma_len(sg) = sg->length;
  3319. skip_map:
  3320. DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
  3321. qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  3322. return 0;
  3323. }
  3324. /**
  3325. * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
  3326. * @qc: Command with scatter-gather table to be mapped.
  3327. *
  3328. * DMA-map the scatter-gather table associated with queued_cmd @qc.
  3329. *
  3330. * LOCKING:
  3331. * spin_lock_irqsave(host lock)
  3332. *
  3333. * RETURNS:
  3334. * Zero on success, negative on error.
  3335. *
  3336. */
  3337. static int ata_sg_setup(struct ata_queued_cmd *qc)
  3338. {
  3339. struct ata_port *ap = qc->ap;
  3340. struct scatterlist *sg = qc->__sg;
  3341. struct scatterlist *lsg = &sg[qc->n_elem - 1];
  3342. int n_elem, pre_n_elem, dir, trim_sg = 0;
  3343. VPRINTK("ENTER, ata%u\n", ap->print_id);
  3344. WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
  3345. /* we must lengthen transfers to end on a 32-bit boundary */
  3346. qc->pad_len = lsg->length & 3;
  3347. if (qc->pad_len) {
  3348. void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  3349. struct scatterlist *psg = &qc->pad_sgent;
  3350. unsigned int offset;
  3351. WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
  3352. memset(pad_buf, 0, ATA_DMA_PAD_SZ);
  3353. /*
  3354. * psg->page/offset are used to copy to-be-written
  3355. * data in this function or read data in ata_sg_clean.
  3356. */
  3357. offset = lsg->offset + lsg->length - qc->pad_len;
  3358. psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
  3359. psg->offset = offset_in_page(offset);
  3360. if (qc->tf.flags & ATA_TFLAG_WRITE) {
  3361. void *addr = kmap_atomic(psg->page, KM_IRQ0);
  3362. memcpy(pad_buf, addr + psg->offset, qc->pad_len);
  3363. kunmap_atomic(addr, KM_IRQ0);
  3364. }
  3365. sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
  3366. sg_dma_len(psg) = ATA_DMA_PAD_SZ;
  3367. /* trim last sg */
  3368. lsg->length -= qc->pad_len;
  3369. if (lsg->length == 0)
  3370. trim_sg = 1;
  3371. DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
  3372. qc->n_elem - 1, lsg->length, qc->pad_len);
  3373. }
  3374. pre_n_elem = qc->n_elem;
  3375. if (trim_sg && pre_n_elem)
  3376. pre_n_elem--;
  3377. if (!pre_n_elem) {
  3378. n_elem = 0;
  3379. goto skip_map;
  3380. }
  3381. dir = qc->dma_dir;
  3382. n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
  3383. if (n_elem < 1) {
  3384. /* restore last sg */
  3385. lsg->length += qc->pad_len;
  3386. return -1;
  3387. }
  3388. DPRINTK("%d sg elements mapped\n", n_elem);
  3389. skip_map:
  3390. qc->n_elem = n_elem;
  3391. return 0;
  3392. }
  3393. /**
  3394. * swap_buf_le16 - swap halves of 16-bit words in place
  3395. * @buf: Buffer to swap
  3396. * @buf_words: Number of 16-bit words in buffer.
  3397. *
  3398. * Swap halves of 16-bit words if needed to convert from
  3399. * little-endian byte order to native cpu byte order, or
  3400. * vice-versa.
  3401. *
  3402. * LOCKING:
  3403. * Inherited from caller.
  3404. */
  3405. void swap_buf_le16(u16 *buf, unsigned int buf_words)
  3406. {
  3407. #ifdef __BIG_ENDIAN
  3408. unsigned int i;
  3409. for (i = 0; i < buf_words; i++)
  3410. buf[i] = le16_to_cpu(buf[i]);
  3411. #endif /* __BIG_ENDIAN */
  3412. }
  3413. /**
  3414. * ata_data_xfer - Transfer data by PIO
  3415. * @adev: device to target
  3416. * @buf: data buffer
  3417. * @buflen: buffer length
  3418. * @write_data: read/write
  3419. *
  3420. * Transfer data from/to the device data register by PIO.
  3421. *
  3422. * LOCKING:
  3423. * Inherited from caller.
  3424. */
  3425. void ata_data_xfer(struct ata_device *adev, unsigned char *buf,
  3426. unsigned int buflen, int write_data)
  3427. {
  3428. struct ata_port *ap = adev->ap;
  3429. unsigned int words = buflen >> 1;
  3430. /* Transfer multiple of 2 bytes */
  3431. if (write_data)
  3432. iowrite16_rep(ap->ioaddr.data_addr, buf, words);
  3433. else
  3434. ioread16_rep(ap->ioaddr.data_addr, buf, words);
  3435. /* Transfer trailing 1 byte, if any. */
  3436. if (unlikely(buflen & 0x01)) {
  3437. u16 align_buf[1] = { 0 };
  3438. unsigned char *trailing_buf = buf + buflen - 1;
  3439. if (write_data) {
  3440. memcpy(align_buf, trailing_buf, 1);
  3441. iowrite16(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
  3442. } else {
  3443. align_buf[0] = cpu_to_le16(ioread16(ap->ioaddr.data_addr));
  3444. memcpy(trailing_buf, align_buf, 1);
  3445. }
  3446. }
  3447. }
  3448. /**
  3449. * ata_data_xfer_noirq - Transfer data by PIO
  3450. * @adev: device to target
  3451. * @buf: data buffer
  3452. * @buflen: buffer length
  3453. * @write_data: read/write
  3454. *
  3455. * Transfer data from/to the device data register by PIO. Do the
  3456. * transfer with interrupts disabled.
  3457. *
  3458. * LOCKING:
  3459. * Inherited from caller.
  3460. */
  3461. void ata_data_xfer_noirq(struct ata_device *adev, unsigned char *buf,
  3462. unsigned int buflen, int write_data)
  3463. {
  3464. unsigned long flags;
  3465. local_irq_save(flags);
  3466. ata_data_xfer(adev, buf, buflen, write_data);
  3467. local_irq_restore(flags);
  3468. }
  3469. /**
  3470. * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
  3471. * @qc: Command on going
  3472. *
  3473. * Transfer ATA_SECT_SIZE of data from/to the ATA device.
  3474. *
  3475. * LOCKING:
  3476. * Inherited from caller.
  3477. */
  3478. static void ata_pio_sector(struct ata_queued_cmd *qc)
  3479. {
  3480. int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  3481. struct scatterlist *sg = qc->__sg;
  3482. struct ata_port *ap = qc->ap;
  3483. struct page *page;
  3484. unsigned int offset;
  3485. unsigned char *buf;
  3486. if (qc->curbytes == qc->nbytes - ATA_SECT_SIZE)
  3487. ap->hsm_task_state = HSM_ST_LAST;
  3488. page = sg[qc->cursg].page;
  3489. offset = sg[qc->cursg].offset + qc->cursg_ofs;
  3490. /* get the current page and offset */
  3491. page = nth_page(page, (offset >> PAGE_SHIFT));
  3492. offset %= PAGE_SIZE;
  3493. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  3494. if (PageHighMem(page)) {
  3495. unsigned long flags;
  3496. /* FIXME: use a bounce buffer */
  3497. local_irq_save(flags);
  3498. buf = kmap_atomic(page, KM_IRQ0);
  3499. /* do the actual data transfer */
  3500. ap->ops->data_xfer(qc->dev, buf + offset, ATA_SECT_SIZE, do_write);
  3501. kunmap_atomic(buf, KM_IRQ0);
  3502. local_irq_restore(flags);
  3503. } else {
  3504. buf = page_address(page);
  3505. ap->ops->data_xfer(qc->dev, buf + offset, ATA_SECT_SIZE, do_write);
  3506. }
  3507. qc->curbytes += ATA_SECT_SIZE;
  3508. qc->cursg_ofs += ATA_SECT_SIZE;
  3509. if (qc->cursg_ofs == (&sg[qc->cursg])->length) {
  3510. qc->cursg++;
  3511. qc->cursg_ofs = 0;
  3512. }
  3513. }
  3514. /**
  3515. * ata_pio_sectors - Transfer one or many 512-byte sectors.
  3516. * @qc: Command on going
  3517. *
  3518. * Transfer one or many ATA_SECT_SIZE of data from/to the
  3519. * ATA device for the DRQ request.
  3520. *
  3521. * LOCKING:
  3522. * Inherited from caller.
  3523. */
  3524. static void ata_pio_sectors(struct ata_queued_cmd *qc)
  3525. {
  3526. if (is_multi_taskfile(&qc->tf)) {
  3527. /* READ/WRITE MULTIPLE */
  3528. unsigned int nsect;
  3529. WARN_ON(qc->dev->multi_count == 0);
  3530. nsect = min((qc->nbytes - qc->curbytes) / ATA_SECT_SIZE,
  3531. qc->dev->multi_count);
  3532. while (nsect--)
  3533. ata_pio_sector(qc);
  3534. } else
  3535. ata_pio_sector(qc);
  3536. }
  3537. /**
  3538. * atapi_send_cdb - Write CDB bytes to hardware
  3539. * @ap: Port to which ATAPI device is attached.
  3540. * @qc: Taskfile currently active
  3541. *
  3542. * When device has indicated its readiness to accept
  3543. * a CDB, this function is called. Send the CDB.
  3544. *
  3545. * LOCKING:
  3546. * caller.
  3547. */
  3548. static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
  3549. {
  3550. /* send SCSI cdb */
  3551. DPRINTK("send cdb\n");
  3552. WARN_ON(qc->dev->cdb_len < 12);
  3553. ap->ops->data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
  3554. ata_altstatus(ap); /* flush */
  3555. switch (qc->tf.protocol) {
  3556. case ATA_PROT_ATAPI:
  3557. ap->hsm_task_state = HSM_ST;
  3558. break;
  3559. case ATA_PROT_ATAPI_NODATA:
  3560. ap->hsm_task_state = HSM_ST_LAST;
  3561. break;
  3562. case ATA_PROT_ATAPI_DMA:
  3563. ap->hsm_task_state = HSM_ST_LAST;
  3564. /* initiate bmdma */
  3565. ap->ops->bmdma_start(qc);
  3566. break;
  3567. }
  3568. }
  3569. /**
  3570. * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
  3571. * @qc: Command on going
  3572. * @bytes: number of bytes
  3573. *
  3574. * Transfer Transfer data from/to the ATAPI device.
  3575. *
  3576. * LOCKING:
  3577. * Inherited from caller.
  3578. *
  3579. */
  3580. static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
  3581. {
  3582. int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  3583. struct scatterlist *sg = qc->__sg;
  3584. struct ata_port *ap = qc->ap;
  3585. struct page *page;
  3586. unsigned char *buf;
  3587. unsigned int offset, count;
  3588. if (qc->curbytes + bytes >= qc->nbytes)
  3589. ap->hsm_task_state = HSM_ST_LAST;
  3590. next_sg:
  3591. if (unlikely(qc->cursg >= qc->n_elem)) {
  3592. /*
  3593. * The end of qc->sg is reached and the device expects
  3594. * more data to transfer. In order not to overrun qc->sg
  3595. * and fulfill length specified in the byte count register,
  3596. * - for read case, discard trailing data from the device
  3597. * - for write case, padding zero data to the device
  3598. */
  3599. u16 pad_buf[1] = { 0 };
  3600. unsigned int words = bytes >> 1;
  3601. unsigned int i;
  3602. if (words) /* warning if bytes > 1 */
  3603. ata_dev_printk(qc->dev, KERN_WARNING,
  3604. "%u bytes trailing data\n", bytes);
  3605. for (i = 0; i < words; i++)
  3606. ap->ops->data_xfer(qc->dev, (unsigned char*)pad_buf, 2, do_write);
  3607. ap->hsm_task_state = HSM_ST_LAST;
  3608. return;
  3609. }
  3610. sg = &qc->__sg[qc->cursg];
  3611. page = sg->page;
  3612. offset = sg->offset + qc->cursg_ofs;
  3613. /* get the current page and offset */
  3614. page = nth_page(page, (offset >> PAGE_SHIFT));
  3615. offset %= PAGE_SIZE;
  3616. /* don't overrun current sg */
  3617. count = min(sg->length - qc->cursg_ofs, bytes);
  3618. /* don't cross page boundaries */
  3619. count = min(count, (unsigned int)PAGE_SIZE - offset);
  3620. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  3621. if (PageHighMem(page)) {
  3622. unsigned long flags;
  3623. /* FIXME: use bounce buffer */
  3624. local_irq_save(flags);
  3625. buf = kmap_atomic(page, KM_IRQ0);
  3626. /* do the actual data transfer */
  3627. ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
  3628. kunmap_atomic(buf, KM_IRQ0);
  3629. local_irq_restore(flags);
  3630. } else {
  3631. buf = page_address(page);
  3632. ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
  3633. }
  3634. bytes -= count;
  3635. qc->curbytes += count;
  3636. qc->cursg_ofs += count;
  3637. if (qc->cursg_ofs == sg->length) {
  3638. qc->cursg++;
  3639. qc->cursg_ofs = 0;
  3640. }
  3641. if (bytes)
  3642. goto next_sg;
  3643. }
  3644. /**
  3645. * atapi_pio_bytes - Transfer data from/to the ATAPI device.
  3646. * @qc: Command on going
  3647. *
  3648. * Transfer Transfer data from/to the ATAPI device.
  3649. *
  3650. * LOCKING:
  3651. * Inherited from caller.
  3652. */
  3653. static void atapi_pio_bytes(struct ata_queued_cmd *qc)
  3654. {
  3655. struct ata_port *ap = qc->ap;
  3656. struct ata_device *dev = qc->dev;
  3657. unsigned int ireason, bc_lo, bc_hi, bytes;
  3658. int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
  3659. /* Abuse qc->result_tf for temp storage of intermediate TF
  3660. * here to save some kernel stack usage.
  3661. * For normal completion, qc->result_tf is not relevant. For
  3662. * error, qc->result_tf is later overwritten by ata_qc_complete().
  3663. * So, the correctness of qc->result_tf is not affected.
  3664. */
  3665. ap->ops->tf_read(ap, &qc->result_tf);
  3666. ireason = qc->result_tf.nsect;
  3667. bc_lo = qc->result_tf.lbam;
  3668. bc_hi = qc->result_tf.lbah;
  3669. bytes = (bc_hi << 8) | bc_lo;
  3670. /* shall be cleared to zero, indicating xfer of data */
  3671. if (ireason & (1 << 0))
  3672. goto err_out;
  3673. /* make sure transfer direction matches expected */
  3674. i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
  3675. if (do_write != i_write)
  3676. goto err_out;
  3677. VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
  3678. __atapi_pio_bytes(qc, bytes);
  3679. return;
  3680. err_out:
  3681. ata_dev_printk(dev, KERN_INFO, "ATAPI check failed\n");
  3682. qc->err_mask |= AC_ERR_HSM;
  3683. ap->hsm_task_state = HSM_ST_ERR;
  3684. }
  3685. /**
  3686. * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
  3687. * @ap: the target ata_port
  3688. * @qc: qc on going
  3689. *
  3690. * RETURNS:
  3691. * 1 if ok in workqueue, 0 otherwise.
  3692. */
  3693. static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
  3694. {
  3695. if (qc->tf.flags & ATA_TFLAG_POLLING)
  3696. return 1;
  3697. if (ap->hsm_task_state == HSM_ST_FIRST) {
  3698. if (qc->tf.protocol == ATA_PROT_PIO &&
  3699. (qc->tf.flags & ATA_TFLAG_WRITE))
  3700. return 1;
  3701. if (is_atapi_taskfile(&qc->tf) &&
  3702. !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  3703. return 1;
  3704. }
  3705. return 0;
  3706. }
  3707. /**
  3708. * ata_hsm_qc_complete - finish a qc running on standard HSM
  3709. * @qc: Command to complete
  3710. * @in_wq: 1 if called from workqueue, 0 otherwise
  3711. *
  3712. * Finish @qc which is running on standard HSM.
  3713. *
  3714. * LOCKING:
  3715. * If @in_wq is zero, spin_lock_irqsave(host lock).
  3716. * Otherwise, none on entry and grabs host lock.
  3717. */
  3718. static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
  3719. {
  3720. struct ata_port *ap = qc->ap;
  3721. unsigned long flags;
  3722. if (ap->ops->error_handler) {
  3723. if (in_wq) {
  3724. spin_lock_irqsave(ap->lock, flags);
  3725. /* EH might have kicked in while host lock is
  3726. * released.
  3727. */
  3728. qc = ata_qc_from_tag(ap, qc->tag);
  3729. if (qc) {
  3730. if (likely(!(qc->err_mask & AC_ERR_HSM))) {
  3731. ap->ops->irq_on(ap);
  3732. ata_qc_complete(qc);
  3733. } else
  3734. ata_port_freeze(ap);
  3735. }
  3736. spin_unlock_irqrestore(ap->lock, flags);
  3737. } else {
  3738. if (likely(!(qc->err_mask & AC_ERR_HSM)))
  3739. ata_qc_complete(qc);
  3740. else
  3741. ata_port_freeze(ap);
  3742. }
  3743. } else {
  3744. if (in_wq) {
  3745. spin_lock_irqsave(ap->lock, flags);
  3746. ap->ops->irq_on(ap);
  3747. ata_qc_complete(qc);
  3748. spin_unlock_irqrestore(ap->lock, flags);
  3749. } else
  3750. ata_qc_complete(qc);
  3751. }
  3752. ata_altstatus(ap); /* flush */
  3753. }
  3754. /**
  3755. * ata_hsm_move - move the HSM to the next state.
  3756. * @ap: the target ata_port
  3757. * @qc: qc on going
  3758. * @status: current device status
  3759. * @in_wq: 1 if called from workqueue, 0 otherwise
  3760. *
  3761. * RETURNS:
  3762. * 1 when poll next status needed, 0 otherwise.
  3763. */
  3764. int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
  3765. u8 status, int in_wq)
  3766. {
  3767. unsigned long flags = 0;
  3768. int poll_next;
  3769. WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
  3770. /* Make sure ata_qc_issue_prot() does not throw things
  3771. * like DMA polling into the workqueue. Notice that
  3772. * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
  3773. */
  3774. WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
  3775. fsm_start:
  3776. DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
  3777. ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
  3778. switch (ap->hsm_task_state) {
  3779. case HSM_ST_FIRST:
  3780. /* Send first data block or PACKET CDB */
  3781. /* If polling, we will stay in the work queue after
  3782. * sending the data. Otherwise, interrupt handler
  3783. * takes over after sending the data.
  3784. */
  3785. poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
  3786. /* check device status */
  3787. if (unlikely((status & ATA_DRQ) == 0)) {
  3788. /* handle BSY=0, DRQ=0 as error */
  3789. if (likely(status & (ATA_ERR | ATA_DF)))
  3790. /* device stops HSM for abort/error */
  3791. qc->err_mask |= AC_ERR_DEV;
  3792. else
  3793. /* HSM violation. Let EH handle this */
  3794. qc->err_mask |= AC_ERR_HSM;
  3795. ap->hsm_task_state = HSM_ST_ERR;
  3796. goto fsm_start;
  3797. }
  3798. /* Device should not ask for data transfer (DRQ=1)
  3799. * when it finds something wrong.
  3800. * We ignore DRQ here and stop the HSM by
  3801. * changing hsm_task_state to HSM_ST_ERR and
  3802. * let the EH abort the command or reset the device.
  3803. */
  3804. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  3805. ata_port_printk(ap, KERN_WARNING, "DRQ=1 with device "
  3806. "error, dev_stat 0x%X\n", status);
  3807. qc->err_mask |= AC_ERR_HSM;
  3808. ap->hsm_task_state = HSM_ST_ERR;
  3809. goto fsm_start;
  3810. }
  3811. /* Send the CDB (atapi) or the first data block (ata pio out).
  3812. * During the state transition, interrupt handler shouldn't
  3813. * be invoked before the data transfer is complete and
  3814. * hsm_task_state is changed. Hence, the following locking.
  3815. */
  3816. if (in_wq)
  3817. spin_lock_irqsave(ap->lock, flags);
  3818. if (qc->tf.protocol == ATA_PROT_PIO) {
  3819. /* PIO data out protocol.
  3820. * send first data block.
  3821. */
  3822. /* ata_pio_sectors() might change the state
  3823. * to HSM_ST_LAST. so, the state is changed here
  3824. * before ata_pio_sectors().
  3825. */
  3826. ap->hsm_task_state = HSM_ST;
  3827. ata_pio_sectors(qc);
  3828. ata_altstatus(ap); /* flush */
  3829. } else
  3830. /* send CDB */
  3831. atapi_send_cdb(ap, qc);
  3832. if (in_wq)
  3833. spin_unlock_irqrestore(ap->lock, flags);
  3834. /* if polling, ata_pio_task() handles the rest.
  3835. * otherwise, interrupt handler takes over from here.
  3836. */
  3837. break;
  3838. case HSM_ST:
  3839. /* complete command or read/write the data register */
  3840. if (qc->tf.protocol == ATA_PROT_ATAPI) {
  3841. /* ATAPI PIO protocol */
  3842. if ((status & ATA_DRQ) == 0) {
  3843. /* No more data to transfer or device error.
  3844. * Device error will be tagged in HSM_ST_LAST.
  3845. */
  3846. ap->hsm_task_state = HSM_ST_LAST;
  3847. goto fsm_start;
  3848. }
  3849. /* Device should not ask for data transfer (DRQ=1)
  3850. * when it finds something wrong.
  3851. * We ignore DRQ here and stop the HSM by
  3852. * changing hsm_task_state to HSM_ST_ERR and
  3853. * let the EH abort the command or reset the device.
  3854. */
  3855. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  3856. ata_port_printk(ap, KERN_WARNING, "DRQ=1 with "
  3857. "device error, dev_stat 0x%X\n",
  3858. status);
  3859. qc->err_mask |= AC_ERR_HSM;
  3860. ap->hsm_task_state = HSM_ST_ERR;
  3861. goto fsm_start;
  3862. }
  3863. atapi_pio_bytes(qc);
  3864. if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
  3865. /* bad ireason reported by device */
  3866. goto fsm_start;
  3867. } else {
  3868. /* ATA PIO protocol */
  3869. if (unlikely((status & ATA_DRQ) == 0)) {
  3870. /* handle BSY=0, DRQ=0 as error */
  3871. if (likely(status & (ATA_ERR | ATA_DF)))
  3872. /* device stops HSM for abort/error */
  3873. qc->err_mask |= AC_ERR_DEV;
  3874. else
  3875. /* HSM violation. Let EH handle this.
  3876. * Phantom devices also trigger this
  3877. * condition. Mark hint.
  3878. */
  3879. qc->err_mask |= AC_ERR_HSM |
  3880. AC_ERR_NODEV_HINT;
  3881. ap->hsm_task_state = HSM_ST_ERR;
  3882. goto fsm_start;
  3883. }
  3884. /* For PIO reads, some devices may ask for
  3885. * data transfer (DRQ=1) alone with ERR=1.
  3886. * We respect DRQ here and transfer one
  3887. * block of junk data before changing the
  3888. * hsm_task_state to HSM_ST_ERR.
  3889. *
  3890. * For PIO writes, ERR=1 DRQ=1 doesn't make
  3891. * sense since the data block has been
  3892. * transferred to the device.
  3893. */
  3894. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  3895. /* data might be corrputed */
  3896. qc->err_mask |= AC_ERR_DEV;
  3897. if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
  3898. ata_pio_sectors(qc);
  3899. ata_altstatus(ap);
  3900. status = ata_wait_idle(ap);
  3901. }
  3902. if (status & (ATA_BUSY | ATA_DRQ))
  3903. qc->err_mask |= AC_ERR_HSM;
  3904. /* ata_pio_sectors() might change the
  3905. * state to HSM_ST_LAST. so, the state
  3906. * is changed after ata_pio_sectors().
  3907. */
  3908. ap->hsm_task_state = HSM_ST_ERR;
  3909. goto fsm_start;
  3910. }
  3911. ata_pio_sectors(qc);
  3912. if (ap->hsm_task_state == HSM_ST_LAST &&
  3913. (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
  3914. /* all data read */
  3915. ata_altstatus(ap);
  3916. status = ata_wait_idle(ap);
  3917. goto fsm_start;
  3918. }
  3919. }
  3920. ata_altstatus(ap); /* flush */
  3921. poll_next = 1;
  3922. break;
  3923. case HSM_ST_LAST:
  3924. if (unlikely(!ata_ok(status))) {
  3925. qc->err_mask |= __ac_err_mask(status);
  3926. ap->hsm_task_state = HSM_ST_ERR;
  3927. goto fsm_start;
  3928. }
  3929. /* no more data to transfer */
  3930. DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
  3931. ap->print_id, qc->dev->devno, status);
  3932. WARN_ON(qc->err_mask);
  3933. ap->hsm_task_state = HSM_ST_IDLE;
  3934. /* complete taskfile transaction */
  3935. ata_hsm_qc_complete(qc, in_wq);
  3936. poll_next = 0;
  3937. break;
  3938. case HSM_ST_ERR:
  3939. /* make sure qc->err_mask is available to
  3940. * know what's wrong and recover
  3941. */
  3942. WARN_ON(qc->err_mask == 0);
  3943. ap->hsm_task_state = HSM_ST_IDLE;
  3944. /* complete taskfile transaction */
  3945. ata_hsm_qc_complete(qc, in_wq);
  3946. poll_next = 0;
  3947. break;
  3948. default:
  3949. poll_next = 0;
  3950. BUG();
  3951. }
  3952. return poll_next;
  3953. }
  3954. static void ata_pio_task(struct work_struct *work)
  3955. {
  3956. struct ata_port *ap =
  3957. container_of(work, struct ata_port, port_task.work);
  3958. struct ata_queued_cmd *qc = ap->port_task_data;
  3959. u8 status;
  3960. int poll_next;
  3961. fsm_start:
  3962. WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
  3963. /*
  3964. * This is purely heuristic. This is a fast path.
  3965. * Sometimes when we enter, BSY will be cleared in
  3966. * a chk-status or two. If not, the drive is probably seeking
  3967. * or something. Snooze for a couple msecs, then
  3968. * chk-status again. If still busy, queue delayed work.
  3969. */
  3970. status = ata_busy_wait(ap, ATA_BUSY, 5);
  3971. if (status & ATA_BUSY) {
  3972. msleep(2);
  3973. status = ata_busy_wait(ap, ATA_BUSY, 10);
  3974. if (status & ATA_BUSY) {
  3975. ata_port_queue_task(ap, ata_pio_task, qc, ATA_SHORT_PAUSE);
  3976. return;
  3977. }
  3978. }
  3979. /* move the HSM */
  3980. poll_next = ata_hsm_move(ap, qc, status, 1);
  3981. /* another command or interrupt handler
  3982. * may be running at this point.
  3983. */
  3984. if (poll_next)
  3985. goto fsm_start;
  3986. }
  3987. /**
  3988. * ata_qc_new - Request an available ATA command, for queueing
  3989. * @ap: Port associated with device @dev
  3990. * @dev: Device from whom we request an available command structure
  3991. *
  3992. * LOCKING:
  3993. * None.
  3994. */
  3995. static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
  3996. {
  3997. struct ata_queued_cmd *qc = NULL;
  3998. unsigned int i;
  3999. /* no command while frozen */
  4000. if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
  4001. return NULL;
  4002. /* the last tag is reserved for internal command. */
  4003. for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
  4004. if (!test_and_set_bit(i, &ap->qc_allocated)) {
  4005. qc = __ata_qc_from_tag(ap, i);
  4006. break;
  4007. }
  4008. if (qc)
  4009. qc->tag = i;
  4010. return qc;
  4011. }
  4012. /**
  4013. * ata_qc_new_init - Request an available ATA command, and initialize it
  4014. * @dev: Device from whom we request an available command structure
  4015. *
  4016. * LOCKING:
  4017. * None.
  4018. */
  4019. struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
  4020. {
  4021. struct ata_port *ap = dev->ap;
  4022. struct ata_queued_cmd *qc;
  4023. qc = ata_qc_new(ap);
  4024. if (qc) {
  4025. qc->scsicmd = NULL;
  4026. qc->ap = ap;
  4027. qc->dev = dev;
  4028. ata_qc_reinit(qc);
  4029. }
  4030. return qc;
  4031. }
  4032. /**
  4033. * ata_qc_free - free unused ata_queued_cmd
  4034. * @qc: Command to complete
  4035. *
  4036. * Designed to free unused ata_queued_cmd object
  4037. * in case something prevents using it.
  4038. *
  4039. * LOCKING:
  4040. * spin_lock_irqsave(host lock)
  4041. */
  4042. void ata_qc_free(struct ata_queued_cmd *qc)
  4043. {
  4044. struct ata_port *ap = qc->ap;
  4045. unsigned int tag;
  4046. WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
  4047. qc->flags = 0;
  4048. tag = qc->tag;
  4049. if (likely(ata_tag_valid(tag))) {
  4050. qc->tag = ATA_TAG_POISON;
  4051. clear_bit(tag, &ap->qc_allocated);
  4052. }
  4053. }
  4054. void __ata_qc_complete(struct ata_queued_cmd *qc)
  4055. {
  4056. struct ata_port *ap = qc->ap;
  4057. WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
  4058. WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
  4059. if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
  4060. ata_sg_clean(qc);
  4061. /* command should be marked inactive atomically with qc completion */
  4062. if (qc->tf.protocol == ATA_PROT_NCQ)
  4063. ap->sactive &= ~(1 << qc->tag);
  4064. else
  4065. ap->active_tag = ATA_TAG_POISON;
  4066. /* atapi: mark qc as inactive to prevent the interrupt handler
  4067. * from completing the command twice later, before the error handler
  4068. * is called. (when rc != 0 and atapi request sense is needed)
  4069. */
  4070. qc->flags &= ~ATA_QCFLAG_ACTIVE;
  4071. ap->qc_active &= ~(1 << qc->tag);
  4072. /* call completion callback */
  4073. qc->complete_fn(qc);
  4074. }
  4075. static void fill_result_tf(struct ata_queued_cmd *qc)
  4076. {
  4077. struct ata_port *ap = qc->ap;
  4078. qc->result_tf.flags = qc->tf.flags;
  4079. ap->ops->tf_read(ap, &qc->result_tf);
  4080. }
  4081. /**
  4082. * ata_qc_complete - Complete an active ATA command
  4083. * @qc: Command to complete
  4084. * @err_mask: ATA Status register contents
  4085. *
  4086. * Indicate to the mid and upper layers that an ATA
  4087. * command has completed, with either an ok or not-ok status.
  4088. *
  4089. * LOCKING:
  4090. * spin_lock_irqsave(host lock)
  4091. */
  4092. void ata_qc_complete(struct ata_queued_cmd *qc)
  4093. {
  4094. struct ata_port *ap = qc->ap;
  4095. /* XXX: New EH and old EH use different mechanisms to
  4096. * synchronize EH with regular execution path.
  4097. *
  4098. * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
  4099. * Normal execution path is responsible for not accessing a
  4100. * failed qc. libata core enforces the rule by returning NULL
  4101. * from ata_qc_from_tag() for failed qcs.
  4102. *
  4103. * Old EH depends on ata_qc_complete() nullifying completion
  4104. * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
  4105. * not synchronize with interrupt handler. Only PIO task is
  4106. * taken care of.
  4107. */
  4108. if (ap->ops->error_handler) {
  4109. WARN_ON(ap->pflags & ATA_PFLAG_FROZEN);
  4110. if (unlikely(qc->err_mask))
  4111. qc->flags |= ATA_QCFLAG_FAILED;
  4112. if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
  4113. if (!ata_tag_internal(qc->tag)) {
  4114. /* always fill result TF for failed qc */
  4115. fill_result_tf(qc);
  4116. ata_qc_schedule_eh(qc);
  4117. return;
  4118. }
  4119. }
  4120. /* read result TF if requested */
  4121. if (qc->flags & ATA_QCFLAG_RESULT_TF)
  4122. fill_result_tf(qc);
  4123. __ata_qc_complete(qc);
  4124. } else {
  4125. if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
  4126. return;
  4127. /* read result TF if failed or requested */
  4128. if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
  4129. fill_result_tf(qc);
  4130. __ata_qc_complete(qc);
  4131. }
  4132. }
  4133. /**
  4134. * ata_qc_complete_multiple - Complete multiple qcs successfully
  4135. * @ap: port in question
  4136. * @qc_active: new qc_active mask
  4137. * @finish_qc: LLDD callback invoked before completing a qc
  4138. *
  4139. * Complete in-flight commands. This functions is meant to be
  4140. * called from low-level driver's interrupt routine to complete
  4141. * requests normally. ap->qc_active and @qc_active is compared
  4142. * and commands are completed accordingly.
  4143. *
  4144. * LOCKING:
  4145. * spin_lock_irqsave(host lock)
  4146. *
  4147. * RETURNS:
  4148. * Number of completed commands on success, -errno otherwise.
  4149. */
  4150. int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active,
  4151. void (*finish_qc)(struct ata_queued_cmd *))
  4152. {
  4153. int nr_done = 0;
  4154. u32 done_mask;
  4155. int i;
  4156. done_mask = ap->qc_active ^ qc_active;
  4157. if (unlikely(done_mask & qc_active)) {
  4158. ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
  4159. "(%08x->%08x)\n", ap->qc_active, qc_active);
  4160. return -EINVAL;
  4161. }
  4162. for (i = 0; i < ATA_MAX_QUEUE; i++) {
  4163. struct ata_queued_cmd *qc;
  4164. if (!(done_mask & (1 << i)))
  4165. continue;
  4166. if ((qc = ata_qc_from_tag(ap, i))) {
  4167. if (finish_qc)
  4168. finish_qc(qc);
  4169. ata_qc_complete(qc);
  4170. nr_done++;
  4171. }
  4172. }
  4173. return nr_done;
  4174. }
  4175. static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
  4176. {
  4177. struct ata_port *ap = qc->ap;
  4178. switch (qc->tf.protocol) {
  4179. case ATA_PROT_NCQ:
  4180. case ATA_PROT_DMA:
  4181. case ATA_PROT_ATAPI_DMA:
  4182. return 1;
  4183. case ATA_PROT_ATAPI:
  4184. case ATA_PROT_PIO:
  4185. if (ap->flags & ATA_FLAG_PIO_DMA)
  4186. return 1;
  4187. /* fall through */
  4188. default:
  4189. return 0;
  4190. }
  4191. /* never reached */
  4192. }
  4193. /**
  4194. * ata_qc_issue - issue taskfile to device
  4195. * @qc: command to issue to device
  4196. *
  4197. * Prepare an ATA command to submission to device.
  4198. * This includes mapping the data into a DMA-able
  4199. * area, filling in the S/G table, and finally
  4200. * writing the taskfile to hardware, starting the command.
  4201. *
  4202. * LOCKING:
  4203. * spin_lock_irqsave(host lock)
  4204. */
  4205. void ata_qc_issue(struct ata_queued_cmd *qc)
  4206. {
  4207. struct ata_port *ap = qc->ap;
  4208. /* Make sure only one non-NCQ command is outstanding. The
  4209. * check is skipped for old EH because it reuses active qc to
  4210. * request ATAPI sense.
  4211. */
  4212. WARN_ON(ap->ops->error_handler && ata_tag_valid(ap->active_tag));
  4213. if (qc->tf.protocol == ATA_PROT_NCQ) {
  4214. WARN_ON(ap->sactive & (1 << qc->tag));
  4215. ap->sactive |= 1 << qc->tag;
  4216. } else {
  4217. WARN_ON(ap->sactive);
  4218. ap->active_tag = qc->tag;
  4219. }
  4220. qc->flags |= ATA_QCFLAG_ACTIVE;
  4221. ap->qc_active |= 1 << qc->tag;
  4222. if (ata_should_dma_map(qc)) {
  4223. if (qc->flags & ATA_QCFLAG_SG) {
  4224. if (ata_sg_setup(qc))
  4225. goto sg_err;
  4226. } else if (qc->flags & ATA_QCFLAG_SINGLE) {
  4227. if (ata_sg_setup_one(qc))
  4228. goto sg_err;
  4229. }
  4230. } else {
  4231. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  4232. }
  4233. ap->ops->qc_prep(qc);
  4234. qc->err_mask |= ap->ops->qc_issue(qc);
  4235. if (unlikely(qc->err_mask))
  4236. goto err;
  4237. return;
  4238. sg_err:
  4239. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  4240. qc->err_mask |= AC_ERR_SYSTEM;
  4241. err:
  4242. ata_qc_complete(qc);
  4243. }
  4244. /**
  4245. * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
  4246. * @qc: command to issue to device
  4247. *
  4248. * Using various libata functions and hooks, this function
  4249. * starts an ATA command. ATA commands are grouped into
  4250. * classes called "protocols", and issuing each type of protocol
  4251. * is slightly different.
  4252. *
  4253. * May be used as the qc_issue() entry in ata_port_operations.
  4254. *
  4255. * LOCKING:
  4256. * spin_lock_irqsave(host lock)
  4257. *
  4258. * RETURNS:
  4259. * Zero on success, AC_ERR_* mask on failure
  4260. */
  4261. unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
  4262. {
  4263. struct ata_port *ap = qc->ap;
  4264. /* Use polling pio if the LLD doesn't handle
  4265. * interrupt driven pio and atapi CDB interrupt.
  4266. */
  4267. if (ap->flags & ATA_FLAG_PIO_POLLING) {
  4268. switch (qc->tf.protocol) {
  4269. case ATA_PROT_PIO:
  4270. case ATA_PROT_NODATA:
  4271. case ATA_PROT_ATAPI:
  4272. case ATA_PROT_ATAPI_NODATA:
  4273. qc->tf.flags |= ATA_TFLAG_POLLING;
  4274. break;
  4275. case ATA_PROT_ATAPI_DMA:
  4276. if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
  4277. /* see ata_dma_blacklisted() */
  4278. BUG();
  4279. break;
  4280. default:
  4281. break;
  4282. }
  4283. }
  4284. /* Some controllers show flaky interrupt behavior after
  4285. * setting xfer mode. Use polling instead.
  4286. */
  4287. if (unlikely(qc->tf.command == ATA_CMD_SET_FEATURES &&
  4288. qc->tf.feature == SETFEATURES_XFER) &&
  4289. (ap->flags & ATA_FLAG_SETXFER_POLLING))
  4290. qc->tf.flags |= ATA_TFLAG_POLLING;
  4291. /* select the device */
  4292. ata_dev_select(ap, qc->dev->devno, 1, 0);
  4293. /* start the command */
  4294. switch (qc->tf.protocol) {
  4295. case ATA_PROT_NODATA:
  4296. if (qc->tf.flags & ATA_TFLAG_POLLING)
  4297. ata_qc_set_polling(qc);
  4298. ata_tf_to_host(ap, &qc->tf);
  4299. ap->hsm_task_state = HSM_ST_LAST;
  4300. if (qc->tf.flags & ATA_TFLAG_POLLING)
  4301. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  4302. break;
  4303. case ATA_PROT_DMA:
  4304. WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
  4305. ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
  4306. ap->ops->bmdma_setup(qc); /* set up bmdma */
  4307. ap->ops->bmdma_start(qc); /* initiate bmdma */
  4308. ap->hsm_task_state = HSM_ST_LAST;
  4309. break;
  4310. case ATA_PROT_PIO:
  4311. if (qc->tf.flags & ATA_TFLAG_POLLING)
  4312. ata_qc_set_polling(qc);
  4313. ata_tf_to_host(ap, &qc->tf);
  4314. if (qc->tf.flags & ATA_TFLAG_WRITE) {
  4315. /* PIO data out protocol */
  4316. ap->hsm_task_state = HSM_ST_FIRST;
  4317. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  4318. /* always send first data block using
  4319. * the ata_pio_task() codepath.
  4320. */
  4321. } else {
  4322. /* PIO data in protocol */
  4323. ap->hsm_task_state = HSM_ST;
  4324. if (qc->tf.flags & ATA_TFLAG_POLLING)
  4325. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  4326. /* if polling, ata_pio_task() handles the rest.
  4327. * otherwise, interrupt handler takes over from here.
  4328. */
  4329. }
  4330. break;
  4331. case ATA_PROT_ATAPI:
  4332. case ATA_PROT_ATAPI_NODATA:
  4333. if (qc->tf.flags & ATA_TFLAG_POLLING)
  4334. ata_qc_set_polling(qc);
  4335. ata_tf_to_host(ap, &qc->tf);
  4336. ap->hsm_task_state = HSM_ST_FIRST;
  4337. /* send cdb by polling if no cdb interrupt */
  4338. if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
  4339. (qc->tf.flags & ATA_TFLAG_POLLING))
  4340. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  4341. break;
  4342. case ATA_PROT_ATAPI_DMA:
  4343. WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
  4344. ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
  4345. ap->ops->bmdma_setup(qc); /* set up bmdma */
  4346. ap->hsm_task_state = HSM_ST_FIRST;
  4347. /* send cdb by polling if no cdb interrupt */
  4348. if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  4349. ata_port_queue_task(ap, ata_pio_task, qc, 0);
  4350. break;
  4351. default:
  4352. WARN_ON(1);
  4353. return AC_ERR_SYSTEM;
  4354. }
  4355. return 0;
  4356. }
  4357. /**
  4358. * ata_host_intr - Handle host interrupt for given (port, task)
  4359. * @ap: Port on which interrupt arrived (possibly...)
  4360. * @qc: Taskfile currently active in engine
  4361. *
  4362. * Handle host interrupt for given queued command. Currently,
  4363. * only DMA interrupts are handled. All other commands are
  4364. * handled via polling with interrupts disabled (nIEN bit).
  4365. *
  4366. * LOCKING:
  4367. * spin_lock_irqsave(host lock)
  4368. *
  4369. * RETURNS:
  4370. * One if interrupt was handled, zero if not (shared irq).
  4371. */
  4372. inline unsigned int ata_host_intr (struct ata_port *ap,
  4373. struct ata_queued_cmd *qc)
  4374. {
  4375. struct ata_eh_info *ehi = &ap->eh_info;
  4376. u8 status, host_stat = 0;
  4377. VPRINTK("ata%u: protocol %d task_state %d\n",
  4378. ap->print_id, qc->tf.protocol, ap->hsm_task_state);
  4379. /* Check whether we are expecting interrupt in this state */
  4380. switch (ap->hsm_task_state) {
  4381. case HSM_ST_FIRST:
  4382. /* Some pre-ATAPI-4 devices assert INTRQ
  4383. * at this state when ready to receive CDB.
  4384. */
  4385. /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
  4386. * The flag was turned on only for atapi devices.
  4387. * No need to check is_atapi_taskfile(&qc->tf) again.
  4388. */
  4389. if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  4390. goto idle_irq;
  4391. break;
  4392. case HSM_ST_LAST:
  4393. if (qc->tf.protocol == ATA_PROT_DMA ||
  4394. qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
  4395. /* check status of DMA engine */
  4396. host_stat = ap->ops->bmdma_status(ap);
  4397. VPRINTK("ata%u: host_stat 0x%X\n",
  4398. ap->print_id, host_stat);
  4399. /* if it's not our irq... */
  4400. if (!(host_stat & ATA_DMA_INTR))
  4401. goto idle_irq;
  4402. /* before we do anything else, clear DMA-Start bit */
  4403. ap->ops->bmdma_stop(qc);
  4404. if (unlikely(host_stat & ATA_DMA_ERR)) {
  4405. /* error when transfering data to/from memory */
  4406. qc->err_mask |= AC_ERR_HOST_BUS;
  4407. ap->hsm_task_state = HSM_ST_ERR;
  4408. }
  4409. }
  4410. break;
  4411. case HSM_ST:
  4412. break;
  4413. default:
  4414. goto idle_irq;
  4415. }
  4416. /* check altstatus */
  4417. status = ata_altstatus(ap);
  4418. if (status & ATA_BUSY)
  4419. goto idle_irq;
  4420. /* check main status, clearing INTRQ */
  4421. status = ata_chk_status(ap);
  4422. if (unlikely(status & ATA_BUSY))
  4423. goto idle_irq;
  4424. /* ack bmdma irq events */
  4425. ap->ops->irq_clear(ap);
  4426. ata_hsm_move(ap, qc, status, 0);
  4427. if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
  4428. qc->tf.protocol == ATA_PROT_ATAPI_DMA))
  4429. ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
  4430. return 1; /* irq handled */
  4431. idle_irq:
  4432. ap->stats.idle_irq++;
  4433. #ifdef ATA_IRQ_TRAP
  4434. if ((ap->stats.idle_irq % 1000) == 0) {
  4435. ap->ops->irq_ack(ap, 0); /* debug trap */
  4436. ata_port_printk(ap, KERN_WARNING, "irq trap\n");
  4437. return 1;
  4438. }
  4439. #endif
  4440. return 0; /* irq not handled */
  4441. }
  4442. /**
  4443. * ata_interrupt - Default ATA host interrupt handler
  4444. * @irq: irq line (unused)
  4445. * @dev_instance: pointer to our ata_host information structure
  4446. *
  4447. * Default interrupt handler for PCI IDE devices. Calls
  4448. * ata_host_intr() for each port that is not disabled.
  4449. *
  4450. * LOCKING:
  4451. * Obtains host lock during operation.
  4452. *
  4453. * RETURNS:
  4454. * IRQ_NONE or IRQ_HANDLED.
  4455. */
  4456. irqreturn_t ata_interrupt (int irq, void *dev_instance)
  4457. {
  4458. struct ata_host *host = dev_instance;
  4459. unsigned int i;
  4460. unsigned int handled = 0;
  4461. unsigned long flags;
  4462. /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
  4463. spin_lock_irqsave(&host->lock, flags);
  4464. for (i = 0; i < host->n_ports; i++) {
  4465. struct ata_port *ap;
  4466. ap = host->ports[i];
  4467. if (ap &&
  4468. !(ap->flags & ATA_FLAG_DISABLED)) {
  4469. struct ata_queued_cmd *qc;
  4470. qc = ata_qc_from_tag(ap, ap->active_tag);
  4471. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
  4472. (qc->flags & ATA_QCFLAG_ACTIVE))
  4473. handled |= ata_host_intr(ap, qc);
  4474. }
  4475. }
  4476. spin_unlock_irqrestore(&host->lock, flags);
  4477. return IRQ_RETVAL(handled);
  4478. }
  4479. /**
  4480. * sata_scr_valid - test whether SCRs are accessible
  4481. * @ap: ATA port to test SCR accessibility for
  4482. *
  4483. * Test whether SCRs are accessible for @ap.
  4484. *
  4485. * LOCKING:
  4486. * None.
  4487. *
  4488. * RETURNS:
  4489. * 1 if SCRs are accessible, 0 otherwise.
  4490. */
  4491. int sata_scr_valid(struct ata_port *ap)
  4492. {
  4493. return ap->cbl == ATA_CBL_SATA && ap->ops->scr_read;
  4494. }
  4495. /**
  4496. * sata_scr_read - read SCR register of the specified port
  4497. * @ap: ATA port to read SCR for
  4498. * @reg: SCR to read
  4499. * @val: Place to store read value
  4500. *
  4501. * Read SCR register @reg of @ap into *@val. This function is
  4502. * guaranteed to succeed if the cable type of the port is SATA
  4503. * and the port implements ->scr_read.
  4504. *
  4505. * LOCKING:
  4506. * None.
  4507. *
  4508. * RETURNS:
  4509. * 0 on success, negative errno on failure.
  4510. */
  4511. int sata_scr_read(struct ata_port *ap, int reg, u32 *val)
  4512. {
  4513. if (sata_scr_valid(ap)) {
  4514. *val = ap->ops->scr_read(ap, reg);
  4515. return 0;
  4516. }
  4517. return -EOPNOTSUPP;
  4518. }
  4519. /**
  4520. * sata_scr_write - write SCR register of the specified port
  4521. * @ap: ATA port to write SCR for
  4522. * @reg: SCR to write
  4523. * @val: value to write
  4524. *
  4525. * Write @val to SCR register @reg of @ap. This function is
  4526. * guaranteed to succeed if the cable type of the port is SATA
  4527. * and the port implements ->scr_read.
  4528. *
  4529. * LOCKING:
  4530. * None.
  4531. *
  4532. * RETURNS:
  4533. * 0 on success, negative errno on failure.
  4534. */
  4535. int sata_scr_write(struct ata_port *ap, int reg, u32 val)
  4536. {
  4537. if (sata_scr_valid(ap)) {
  4538. ap->ops->scr_write(ap, reg, val);
  4539. return 0;
  4540. }
  4541. return -EOPNOTSUPP;
  4542. }
  4543. /**
  4544. * sata_scr_write_flush - write SCR register of the specified port and flush
  4545. * @ap: ATA port to write SCR for
  4546. * @reg: SCR to write
  4547. * @val: value to write
  4548. *
  4549. * This function is identical to sata_scr_write() except that this
  4550. * function performs flush after writing to the register.
  4551. *
  4552. * LOCKING:
  4553. * None.
  4554. *
  4555. * RETURNS:
  4556. * 0 on success, negative errno on failure.
  4557. */
  4558. int sata_scr_write_flush(struct ata_port *ap, int reg, u32 val)
  4559. {
  4560. if (sata_scr_valid(ap)) {
  4561. ap->ops->scr_write(ap, reg, val);
  4562. ap->ops->scr_read(ap, reg);
  4563. return 0;
  4564. }
  4565. return -EOPNOTSUPP;
  4566. }
  4567. /**
  4568. * ata_port_online - test whether the given port is online
  4569. * @ap: ATA port to test
  4570. *
  4571. * Test whether @ap is online. Note that this function returns 0
  4572. * if online status of @ap cannot be obtained, so
  4573. * ata_port_online(ap) != !ata_port_offline(ap).
  4574. *
  4575. * LOCKING:
  4576. * None.
  4577. *
  4578. * RETURNS:
  4579. * 1 if the port online status is available and online.
  4580. */
  4581. int ata_port_online(struct ata_port *ap)
  4582. {
  4583. u32 sstatus;
  4584. if (!sata_scr_read(ap, SCR_STATUS, &sstatus) && (sstatus & 0xf) == 0x3)
  4585. return 1;
  4586. return 0;
  4587. }
  4588. /**
  4589. * ata_port_offline - test whether the given port is offline
  4590. * @ap: ATA port to test
  4591. *
  4592. * Test whether @ap is offline. Note that this function returns
  4593. * 0 if offline status of @ap cannot be obtained, so
  4594. * ata_port_online(ap) != !ata_port_offline(ap).
  4595. *
  4596. * LOCKING:
  4597. * None.
  4598. *
  4599. * RETURNS:
  4600. * 1 if the port offline status is available and offline.
  4601. */
  4602. int ata_port_offline(struct ata_port *ap)
  4603. {
  4604. u32 sstatus;
  4605. if (!sata_scr_read(ap, SCR_STATUS, &sstatus) && (sstatus & 0xf) != 0x3)
  4606. return 1;
  4607. return 0;
  4608. }
  4609. int ata_flush_cache(struct ata_device *dev)
  4610. {
  4611. unsigned int err_mask;
  4612. u8 cmd;
  4613. if (!ata_try_flush_cache(dev))
  4614. return 0;
  4615. if (dev->flags & ATA_DFLAG_FLUSH_EXT)
  4616. cmd = ATA_CMD_FLUSH_EXT;
  4617. else
  4618. cmd = ATA_CMD_FLUSH;
  4619. err_mask = ata_do_simple_cmd(dev, cmd);
  4620. if (err_mask) {
  4621. ata_dev_printk(dev, KERN_ERR, "failed to flush cache\n");
  4622. return -EIO;
  4623. }
  4624. return 0;
  4625. }
  4626. #ifdef CONFIG_PM
  4627. static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
  4628. unsigned int action, unsigned int ehi_flags,
  4629. int wait)
  4630. {
  4631. unsigned long flags;
  4632. int i, rc;
  4633. for (i = 0; i < host->n_ports; i++) {
  4634. struct ata_port *ap = host->ports[i];
  4635. /* Previous resume operation might still be in
  4636. * progress. Wait for PM_PENDING to clear.
  4637. */
  4638. if (ap->pflags & ATA_PFLAG_PM_PENDING) {
  4639. ata_port_wait_eh(ap);
  4640. WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
  4641. }
  4642. /* request PM ops to EH */
  4643. spin_lock_irqsave(ap->lock, flags);
  4644. ap->pm_mesg = mesg;
  4645. if (wait) {
  4646. rc = 0;
  4647. ap->pm_result = &rc;
  4648. }
  4649. ap->pflags |= ATA_PFLAG_PM_PENDING;
  4650. ap->eh_info.action |= action;
  4651. ap->eh_info.flags |= ehi_flags;
  4652. ata_port_schedule_eh(ap);
  4653. spin_unlock_irqrestore(ap->lock, flags);
  4654. /* wait and check result */
  4655. if (wait) {
  4656. ata_port_wait_eh(ap);
  4657. WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
  4658. if (rc)
  4659. return rc;
  4660. }
  4661. }
  4662. return 0;
  4663. }
  4664. /**
  4665. * ata_host_suspend - suspend host
  4666. * @host: host to suspend
  4667. * @mesg: PM message
  4668. *
  4669. * Suspend @host. Actual operation is performed by EH. This
  4670. * function requests EH to perform PM operations and waits for EH
  4671. * to finish.
  4672. *
  4673. * LOCKING:
  4674. * Kernel thread context (may sleep).
  4675. *
  4676. * RETURNS:
  4677. * 0 on success, -errno on failure.
  4678. */
  4679. int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
  4680. {
  4681. int i, j, rc;
  4682. rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
  4683. if (rc)
  4684. goto fail;
  4685. /* EH is quiescent now. Fail if we have any ready device.
  4686. * This happens if hotplug occurs between completion of device
  4687. * suspension and here.
  4688. */
  4689. for (i = 0; i < host->n_ports; i++) {
  4690. struct ata_port *ap = host->ports[i];
  4691. for (j = 0; j < ATA_MAX_DEVICES; j++) {
  4692. struct ata_device *dev = &ap->device[j];
  4693. if (ata_dev_ready(dev)) {
  4694. ata_port_printk(ap, KERN_WARNING,
  4695. "suspend failed, device %d "
  4696. "still active\n", dev->devno);
  4697. rc = -EBUSY;
  4698. goto fail;
  4699. }
  4700. }
  4701. }
  4702. host->dev->power.power_state = mesg;
  4703. return 0;
  4704. fail:
  4705. ata_host_resume(host);
  4706. return rc;
  4707. }
  4708. /**
  4709. * ata_host_resume - resume host
  4710. * @host: host to resume
  4711. *
  4712. * Resume @host. Actual operation is performed by EH. This
  4713. * function requests EH to perform PM operations and returns.
  4714. * Note that all resume operations are performed parallely.
  4715. *
  4716. * LOCKING:
  4717. * Kernel thread context (may sleep).
  4718. */
  4719. void ata_host_resume(struct ata_host *host)
  4720. {
  4721. ata_host_request_pm(host, PMSG_ON, ATA_EH_SOFTRESET,
  4722. ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
  4723. host->dev->power.power_state = PMSG_ON;
  4724. }
  4725. #endif
  4726. /**
  4727. * ata_port_start - Set port up for dma.
  4728. * @ap: Port to initialize
  4729. *
  4730. * Called just after data structures for each port are
  4731. * initialized. Allocates space for PRD table.
  4732. *
  4733. * May be used as the port_start() entry in ata_port_operations.
  4734. *
  4735. * LOCKING:
  4736. * Inherited from caller.
  4737. */
  4738. int ata_port_start(struct ata_port *ap)
  4739. {
  4740. struct device *dev = ap->dev;
  4741. int rc;
  4742. ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
  4743. GFP_KERNEL);
  4744. if (!ap->prd)
  4745. return -ENOMEM;
  4746. rc = ata_pad_alloc(ap, dev);
  4747. if (rc)
  4748. return rc;
  4749. DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd,
  4750. (unsigned long long)ap->prd_dma);
  4751. return 0;
  4752. }
  4753. /**
  4754. * ata_dev_init - Initialize an ata_device structure
  4755. * @dev: Device structure to initialize
  4756. *
  4757. * Initialize @dev in preparation for probing.
  4758. *
  4759. * LOCKING:
  4760. * Inherited from caller.
  4761. */
  4762. void ata_dev_init(struct ata_device *dev)
  4763. {
  4764. struct ata_port *ap = dev->ap;
  4765. unsigned long flags;
  4766. /* SATA spd limit is bound to the first device */
  4767. ap->sata_spd_limit = ap->hw_sata_spd_limit;
  4768. /* High bits of dev->flags are used to record warm plug
  4769. * requests which occur asynchronously. Synchronize using
  4770. * host lock.
  4771. */
  4772. spin_lock_irqsave(ap->lock, flags);
  4773. dev->flags &= ~ATA_DFLAG_INIT_MASK;
  4774. spin_unlock_irqrestore(ap->lock, flags);
  4775. memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
  4776. sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
  4777. dev->pio_mask = UINT_MAX;
  4778. dev->mwdma_mask = UINT_MAX;
  4779. dev->udma_mask = UINT_MAX;
  4780. }
  4781. /**
  4782. * ata_port_init - Initialize an ata_port structure
  4783. * @ap: Structure to initialize
  4784. * @host: Collection of hosts to which @ap belongs
  4785. * @ent: Probe information provided by low-level driver
  4786. * @port_no: Port number associated with this ata_port
  4787. *
  4788. * Initialize a new ata_port structure.
  4789. *
  4790. * LOCKING:
  4791. * Inherited from caller.
  4792. */
  4793. void ata_port_init(struct ata_port *ap, struct ata_host *host,
  4794. const struct ata_probe_ent *ent, unsigned int port_no)
  4795. {
  4796. unsigned int i;
  4797. ap->lock = &host->lock;
  4798. ap->flags = ATA_FLAG_DISABLED;
  4799. ap->print_id = ata_print_id++;
  4800. ap->ctl = ATA_DEVCTL_OBS;
  4801. ap->host = host;
  4802. ap->dev = ent->dev;
  4803. ap->port_no = port_no;
  4804. if (port_no == 1 && ent->pinfo2) {
  4805. ap->pio_mask = ent->pinfo2->pio_mask;
  4806. ap->mwdma_mask = ent->pinfo2->mwdma_mask;
  4807. ap->udma_mask = ent->pinfo2->udma_mask;
  4808. ap->flags |= ent->pinfo2->flags;
  4809. ap->ops = ent->pinfo2->port_ops;
  4810. } else {
  4811. ap->pio_mask = ent->pio_mask;
  4812. ap->mwdma_mask = ent->mwdma_mask;
  4813. ap->udma_mask = ent->udma_mask;
  4814. ap->flags |= ent->port_flags;
  4815. ap->ops = ent->port_ops;
  4816. }
  4817. ap->hw_sata_spd_limit = UINT_MAX;
  4818. ap->active_tag = ATA_TAG_POISON;
  4819. ap->last_ctl = 0xFF;
  4820. #if defined(ATA_VERBOSE_DEBUG)
  4821. /* turn on all debugging levels */
  4822. ap->msg_enable = 0x00FF;
  4823. #elif defined(ATA_DEBUG)
  4824. ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
  4825. #else
  4826. ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
  4827. #endif
  4828. INIT_DELAYED_WORK(&ap->port_task, NULL);
  4829. INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
  4830. INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
  4831. INIT_LIST_HEAD(&ap->eh_done_q);
  4832. init_waitqueue_head(&ap->eh_wait_q);
  4833. /* set cable type */
  4834. ap->cbl = ATA_CBL_NONE;
  4835. if (ap->flags & ATA_FLAG_SATA)
  4836. ap->cbl = ATA_CBL_SATA;
  4837. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  4838. struct ata_device *dev = &ap->device[i];
  4839. dev->ap = ap;
  4840. dev->devno = i;
  4841. ata_dev_init(dev);
  4842. }
  4843. #ifdef ATA_IRQ_TRAP
  4844. ap->stats.unhandled_irq = 1;
  4845. ap->stats.idle_irq = 1;
  4846. #endif
  4847. memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
  4848. }
  4849. /**
  4850. * ata_port_init_shost - Initialize SCSI host associated with ATA port
  4851. * @ap: ATA port to initialize SCSI host for
  4852. * @shost: SCSI host associated with @ap
  4853. *
  4854. * Initialize SCSI host @shost associated with ATA port @ap.
  4855. *
  4856. * LOCKING:
  4857. * Inherited from caller.
  4858. */
  4859. static void ata_port_init_shost(struct ata_port *ap, struct Scsi_Host *shost)
  4860. {
  4861. ap->scsi_host = shost;
  4862. shost->unique_id = ap->print_id;
  4863. shost->max_id = 16;
  4864. shost->max_lun = 1;
  4865. shost->max_channel = 1;
  4866. shost->max_cmd_len = 16;
  4867. }
  4868. /**
  4869. * ata_port_add - Attach low-level ATA driver to system
  4870. * @ent: Information provided by low-level driver
  4871. * @host: Collections of ports to which we add
  4872. * @port_no: Port number associated with this host
  4873. *
  4874. * Attach low-level ATA driver to system.
  4875. *
  4876. * LOCKING:
  4877. * PCI/etc. bus probe sem.
  4878. *
  4879. * RETURNS:
  4880. * New ata_port on success, for NULL on error.
  4881. */
  4882. static struct ata_port * ata_port_add(const struct ata_probe_ent *ent,
  4883. struct ata_host *host,
  4884. unsigned int port_no)
  4885. {
  4886. struct Scsi_Host *shost;
  4887. struct ata_port *ap;
  4888. DPRINTK("ENTER\n");
  4889. if (!ent->port_ops->error_handler &&
  4890. !(ent->port_flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST))) {
  4891. printk(KERN_ERR "ata%u: no reset mechanism available\n",
  4892. port_no);
  4893. return NULL;
  4894. }
  4895. shost = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
  4896. if (!shost)
  4897. return NULL;
  4898. shost->transportt = &ata_scsi_transport_template;
  4899. ap = ata_shost_to_port(shost);
  4900. ata_port_init(ap, host, ent, port_no);
  4901. ata_port_init_shost(ap, shost);
  4902. return ap;
  4903. }
  4904. static void ata_host_release(struct device *gendev, void *res)
  4905. {
  4906. struct ata_host *host = dev_get_drvdata(gendev);
  4907. int i;
  4908. for (i = 0; i < host->n_ports; i++) {
  4909. struct ata_port *ap = host->ports[i];
  4910. if (ap && ap->ops->port_stop)
  4911. ap->ops->port_stop(ap);
  4912. }
  4913. if (host->ops->host_stop)
  4914. host->ops->host_stop(host);
  4915. for (i = 0; i < host->n_ports; i++) {
  4916. struct ata_port *ap = host->ports[i];
  4917. if (ap)
  4918. scsi_host_put(ap->scsi_host);
  4919. host->ports[i] = NULL;
  4920. }
  4921. dev_set_drvdata(gendev, NULL);
  4922. }
  4923. /**
  4924. * ata_sas_host_init - Initialize a host struct
  4925. * @host: host to initialize
  4926. * @dev: device host is attached to
  4927. * @flags: host flags
  4928. * @ops: port_ops
  4929. *
  4930. * LOCKING:
  4931. * PCI/etc. bus probe sem.
  4932. *
  4933. */
  4934. void ata_host_init(struct ata_host *host, struct device *dev,
  4935. unsigned long flags, const struct ata_port_operations *ops)
  4936. {
  4937. spin_lock_init(&host->lock);
  4938. host->dev = dev;
  4939. host->flags = flags;
  4940. host->ops = ops;
  4941. }
  4942. /**
  4943. * ata_device_add - Register hardware device with ATA and SCSI layers
  4944. * @ent: Probe information describing hardware device to be registered
  4945. *
  4946. * This function processes the information provided in the probe
  4947. * information struct @ent, allocates the necessary ATA and SCSI
  4948. * host information structures, initializes them, and registers
  4949. * everything with requisite kernel subsystems.
  4950. *
  4951. * This function requests irqs, probes the ATA bus, and probes
  4952. * the SCSI bus.
  4953. *
  4954. * LOCKING:
  4955. * PCI/etc. bus probe sem.
  4956. *
  4957. * RETURNS:
  4958. * Number of ports registered. Zero on error (no ports registered).
  4959. */
  4960. int ata_device_add(const struct ata_probe_ent *ent)
  4961. {
  4962. unsigned int i;
  4963. struct device *dev = ent->dev;
  4964. struct ata_host *host;
  4965. int rc;
  4966. DPRINTK("ENTER\n");
  4967. if (ent->irq == 0) {
  4968. dev_printk(KERN_ERR, dev, "is not available: No interrupt assigned.\n");
  4969. return 0;
  4970. }
  4971. if (!devres_open_group(dev, ata_device_add, GFP_KERNEL))
  4972. return 0;
  4973. /* alloc a container for our list of ATA ports (buses) */
  4974. host = devres_alloc(ata_host_release, sizeof(struct ata_host) +
  4975. (ent->n_ports * sizeof(void *)), GFP_KERNEL);
  4976. if (!host)
  4977. goto err_out;
  4978. devres_add(dev, host);
  4979. dev_set_drvdata(dev, host);
  4980. ata_host_init(host, dev, ent->_host_flags, ent->port_ops);
  4981. host->n_ports = ent->n_ports;
  4982. host->irq = ent->irq;
  4983. host->irq2 = ent->irq2;
  4984. host->iomap = ent->iomap;
  4985. host->private_data = ent->private_data;
  4986. /* register each port bound to this device */
  4987. for (i = 0; i < host->n_ports; i++) {
  4988. struct ata_port *ap;
  4989. unsigned long xfer_mode_mask;
  4990. int irq_line = ent->irq;
  4991. ap = ata_port_add(ent, host, i);
  4992. host->ports[i] = ap;
  4993. if (!ap)
  4994. goto err_out;
  4995. /* dummy? */
  4996. if (ent->dummy_port_mask & (1 << i)) {
  4997. ata_port_printk(ap, KERN_INFO, "DUMMY\n");
  4998. ap->ops = &ata_dummy_port_ops;
  4999. continue;
  5000. }
  5001. /* start port */
  5002. rc = ap->ops->port_start(ap);
  5003. if (rc) {
  5004. host->ports[i] = NULL;
  5005. scsi_host_put(ap->scsi_host);
  5006. goto err_out;
  5007. }
  5008. /* Report the secondary IRQ for second channel legacy */
  5009. if (i == 1 && ent->irq2)
  5010. irq_line = ent->irq2;
  5011. xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
  5012. (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
  5013. (ap->pio_mask << ATA_SHIFT_PIO);
  5014. /* print per-port info to dmesg */
  5015. ata_port_printk(ap, KERN_INFO, "%cATA max %s cmd 0x%p "
  5016. "ctl 0x%p bmdma 0x%p irq %d\n",
  5017. ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
  5018. ata_mode_string(xfer_mode_mask),
  5019. ap->ioaddr.cmd_addr,
  5020. ap->ioaddr.ctl_addr,
  5021. ap->ioaddr.bmdma_addr,
  5022. irq_line);
  5023. /* freeze port before requesting IRQ */
  5024. ata_eh_freeze_port(ap);
  5025. }
  5026. /* obtain irq, that may be shared between channels */
  5027. rc = devm_request_irq(dev, ent->irq, ent->port_ops->irq_handler,
  5028. ent->irq_flags, DRV_NAME, host);
  5029. if (rc) {
  5030. dev_printk(KERN_ERR, dev, "irq %lu request failed: %d\n",
  5031. ent->irq, rc);
  5032. goto err_out;
  5033. }
  5034. /* do we have a second IRQ for the other channel, eg legacy mode */
  5035. if (ent->irq2) {
  5036. /* We will get weird core code crashes later if this is true
  5037. so trap it now */
  5038. BUG_ON(ent->irq == ent->irq2);
  5039. rc = devm_request_irq(dev, ent->irq2,
  5040. ent->port_ops->irq_handler, ent->irq_flags,
  5041. DRV_NAME, host);
  5042. if (rc) {
  5043. dev_printk(KERN_ERR, dev, "irq %lu request failed: %d\n",
  5044. ent->irq2, rc);
  5045. goto err_out;
  5046. }
  5047. }
  5048. /* resource acquisition complete */
  5049. devres_remove_group(dev, ata_device_add);
  5050. /* perform each probe synchronously */
  5051. DPRINTK("probe begin\n");
  5052. for (i = 0; i < host->n_ports; i++) {
  5053. struct ata_port *ap = host->ports[i];
  5054. u32 scontrol;
  5055. int rc;
  5056. /* init sata_spd_limit to the current value */
  5057. if (sata_scr_read(ap, SCR_CONTROL, &scontrol) == 0) {
  5058. int spd = (scontrol >> 4) & 0xf;
  5059. ap->hw_sata_spd_limit &= (1 << spd) - 1;
  5060. }
  5061. ap->sata_spd_limit = ap->hw_sata_spd_limit;
  5062. rc = scsi_add_host(ap->scsi_host, dev);
  5063. if (rc) {
  5064. ata_port_printk(ap, KERN_ERR, "scsi_add_host failed\n");
  5065. /* FIXME: do something useful here */
  5066. /* FIXME: handle unconditional calls to
  5067. * scsi_scan_host and ata_host_remove, below,
  5068. * at the very least
  5069. */
  5070. }
  5071. if (ap->ops->error_handler) {
  5072. struct ata_eh_info *ehi = &ap->eh_info;
  5073. unsigned long flags;
  5074. ata_port_probe(ap);
  5075. /* kick EH for boot probing */
  5076. spin_lock_irqsave(ap->lock, flags);
  5077. ehi->probe_mask = (1 << ATA_MAX_DEVICES) - 1;
  5078. ehi->action |= ATA_EH_SOFTRESET;
  5079. ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
  5080. ap->pflags |= ATA_PFLAG_LOADING;
  5081. ata_port_schedule_eh(ap);
  5082. spin_unlock_irqrestore(ap->lock, flags);
  5083. /* wait for EH to finish */
  5084. ata_port_wait_eh(ap);
  5085. } else {
  5086. DPRINTK("ata%u: bus probe begin\n", ap->print_id);
  5087. rc = ata_bus_probe(ap);
  5088. DPRINTK("ata%u: bus probe end\n", ap->print_id);
  5089. if (rc) {
  5090. /* FIXME: do something useful here?
  5091. * Current libata behavior will
  5092. * tear down everything when
  5093. * the module is removed
  5094. * or the h/w is unplugged.
  5095. */
  5096. }
  5097. }
  5098. }
  5099. /* probes are done, now scan each port's disk(s) */
  5100. DPRINTK("host probe begin\n");
  5101. for (i = 0; i < host->n_ports; i++) {
  5102. struct ata_port *ap = host->ports[i];
  5103. ata_scsi_scan_host(ap);
  5104. }
  5105. VPRINTK("EXIT, returning %u\n", ent->n_ports);
  5106. return ent->n_ports; /* success */
  5107. err_out:
  5108. devres_release_group(dev, ata_device_add);
  5109. VPRINTK("EXIT, returning %d\n", rc);
  5110. return 0;
  5111. }
  5112. /**
  5113. * ata_port_detach - Detach ATA port in prepration of device removal
  5114. * @ap: ATA port to be detached
  5115. *
  5116. * Detach all ATA devices and the associated SCSI devices of @ap;
  5117. * then, remove the associated SCSI host. @ap is guaranteed to
  5118. * be quiescent on return from this function.
  5119. *
  5120. * LOCKING:
  5121. * Kernel thread context (may sleep).
  5122. */
  5123. void ata_port_detach(struct ata_port *ap)
  5124. {
  5125. unsigned long flags;
  5126. int i;
  5127. if (!ap->ops->error_handler)
  5128. goto skip_eh;
  5129. /* tell EH we're leaving & flush EH */
  5130. spin_lock_irqsave(ap->lock, flags);
  5131. ap->pflags |= ATA_PFLAG_UNLOADING;
  5132. spin_unlock_irqrestore(ap->lock, flags);
  5133. ata_port_wait_eh(ap);
  5134. /* EH is now guaranteed to see UNLOADING, so no new device
  5135. * will be attached. Disable all existing devices.
  5136. */
  5137. spin_lock_irqsave(ap->lock, flags);
  5138. for (i = 0; i < ATA_MAX_DEVICES; i++)
  5139. ata_dev_disable(&ap->device[i]);
  5140. spin_unlock_irqrestore(ap->lock, flags);
  5141. /* Final freeze & EH. All in-flight commands are aborted. EH
  5142. * will be skipped and retrials will be terminated with bad
  5143. * target.
  5144. */
  5145. spin_lock_irqsave(ap->lock, flags);
  5146. ata_port_freeze(ap); /* won't be thawed */
  5147. spin_unlock_irqrestore(ap->lock, flags);
  5148. ata_port_wait_eh(ap);
  5149. /* Flush hotplug task. The sequence is similar to
  5150. * ata_port_flush_task().
  5151. */
  5152. flush_workqueue(ata_aux_wq);
  5153. cancel_delayed_work(&ap->hotplug_task);
  5154. flush_workqueue(ata_aux_wq);
  5155. skip_eh:
  5156. /* remove the associated SCSI host */
  5157. scsi_remove_host(ap->scsi_host);
  5158. }
  5159. /**
  5160. * ata_host_detach - Detach all ports of an ATA host
  5161. * @host: Host to detach
  5162. *
  5163. * Detach all ports of @host.
  5164. *
  5165. * LOCKING:
  5166. * Kernel thread context (may sleep).
  5167. */
  5168. void ata_host_detach(struct ata_host *host)
  5169. {
  5170. int i;
  5171. for (i = 0; i < host->n_ports; i++)
  5172. ata_port_detach(host->ports[i]);
  5173. }
  5174. struct ata_probe_ent *
  5175. ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
  5176. {
  5177. struct ata_probe_ent *probe_ent;
  5178. probe_ent = devm_kzalloc(dev, sizeof(*probe_ent), GFP_KERNEL);
  5179. if (!probe_ent) {
  5180. printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
  5181. kobject_name(&(dev->kobj)));
  5182. return NULL;
  5183. }
  5184. INIT_LIST_HEAD(&probe_ent->node);
  5185. probe_ent->dev = dev;
  5186. probe_ent->sht = port->sht;
  5187. probe_ent->port_flags = port->flags;
  5188. probe_ent->pio_mask = port->pio_mask;
  5189. probe_ent->mwdma_mask = port->mwdma_mask;
  5190. probe_ent->udma_mask = port->udma_mask;
  5191. probe_ent->port_ops = port->port_ops;
  5192. probe_ent->private_data = port->private_data;
  5193. return probe_ent;
  5194. }
  5195. /**
  5196. * ata_std_ports - initialize ioaddr with standard port offsets.
  5197. * @ioaddr: IO address structure to be initialized
  5198. *
  5199. * Utility function which initializes data_addr, error_addr,
  5200. * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
  5201. * device_addr, status_addr, and command_addr to standard offsets
  5202. * relative to cmd_addr.
  5203. *
  5204. * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
  5205. */
  5206. void ata_std_ports(struct ata_ioports *ioaddr)
  5207. {
  5208. ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
  5209. ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
  5210. ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
  5211. ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
  5212. ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
  5213. ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
  5214. ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
  5215. ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
  5216. ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
  5217. ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
  5218. }
  5219. #ifdef CONFIG_PCI
  5220. /**
  5221. * ata_pci_remove_one - PCI layer callback for device removal
  5222. * @pdev: PCI device that was removed
  5223. *
  5224. * PCI layer indicates to libata via this hook that hot-unplug or
  5225. * module unload event has occurred. Detach all ports. Resource
  5226. * release is handled via devres.
  5227. *
  5228. * LOCKING:
  5229. * Inherited from PCI layer (may sleep).
  5230. */
  5231. void ata_pci_remove_one(struct pci_dev *pdev)
  5232. {
  5233. struct device *dev = pci_dev_to_dev(pdev);
  5234. struct ata_host *host = dev_get_drvdata(dev);
  5235. ata_host_detach(host);
  5236. }
  5237. /* move to PCI subsystem */
  5238. int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
  5239. {
  5240. unsigned long tmp = 0;
  5241. switch (bits->width) {
  5242. case 1: {
  5243. u8 tmp8 = 0;
  5244. pci_read_config_byte(pdev, bits->reg, &tmp8);
  5245. tmp = tmp8;
  5246. break;
  5247. }
  5248. case 2: {
  5249. u16 tmp16 = 0;
  5250. pci_read_config_word(pdev, bits->reg, &tmp16);
  5251. tmp = tmp16;
  5252. break;
  5253. }
  5254. case 4: {
  5255. u32 tmp32 = 0;
  5256. pci_read_config_dword(pdev, bits->reg, &tmp32);
  5257. tmp = tmp32;
  5258. break;
  5259. }
  5260. default:
  5261. return -EINVAL;
  5262. }
  5263. tmp &= bits->mask;
  5264. return (tmp == bits->val) ? 1 : 0;
  5265. }
  5266. #ifdef CONFIG_PM
  5267. void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
  5268. {
  5269. pci_save_state(pdev);
  5270. pci_disable_device(pdev);
  5271. if (mesg.event == PM_EVENT_SUSPEND)
  5272. pci_set_power_state(pdev, PCI_D3hot);
  5273. }
  5274. int ata_pci_device_do_resume(struct pci_dev *pdev)
  5275. {
  5276. int rc;
  5277. pci_set_power_state(pdev, PCI_D0);
  5278. pci_restore_state(pdev);
  5279. rc = pcim_enable_device(pdev);
  5280. if (rc) {
  5281. dev_printk(KERN_ERR, &pdev->dev,
  5282. "failed to enable device after resume (%d)\n", rc);
  5283. return rc;
  5284. }
  5285. pci_set_master(pdev);
  5286. return 0;
  5287. }
  5288. int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
  5289. {
  5290. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  5291. int rc = 0;
  5292. rc = ata_host_suspend(host, mesg);
  5293. if (rc)
  5294. return rc;
  5295. ata_pci_device_do_suspend(pdev, mesg);
  5296. return 0;
  5297. }
  5298. int ata_pci_device_resume(struct pci_dev *pdev)
  5299. {
  5300. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  5301. int rc;
  5302. rc = ata_pci_device_do_resume(pdev);
  5303. if (rc == 0)
  5304. ata_host_resume(host);
  5305. return rc;
  5306. }
  5307. #endif /* CONFIG_PM */
  5308. #endif /* CONFIG_PCI */
  5309. static int __init ata_init(void)
  5310. {
  5311. ata_probe_timeout *= HZ;
  5312. ata_wq = create_workqueue("ata");
  5313. if (!ata_wq)
  5314. return -ENOMEM;
  5315. ata_aux_wq = create_singlethread_workqueue("ata_aux");
  5316. if (!ata_aux_wq) {
  5317. destroy_workqueue(ata_wq);
  5318. return -ENOMEM;
  5319. }
  5320. printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
  5321. return 0;
  5322. }
  5323. static void __exit ata_exit(void)
  5324. {
  5325. destroy_workqueue(ata_wq);
  5326. destroy_workqueue(ata_aux_wq);
  5327. }
  5328. subsys_initcall(ata_init);
  5329. module_exit(ata_exit);
  5330. static unsigned long ratelimit_time;
  5331. static DEFINE_SPINLOCK(ata_ratelimit_lock);
  5332. int ata_ratelimit(void)
  5333. {
  5334. int rc;
  5335. unsigned long flags;
  5336. spin_lock_irqsave(&ata_ratelimit_lock, flags);
  5337. if (time_after(jiffies, ratelimit_time)) {
  5338. rc = 1;
  5339. ratelimit_time = jiffies + (HZ/5);
  5340. } else
  5341. rc = 0;
  5342. spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
  5343. return rc;
  5344. }
  5345. /**
  5346. * ata_wait_register - wait until register value changes
  5347. * @reg: IO-mapped register
  5348. * @mask: Mask to apply to read register value
  5349. * @val: Wait condition
  5350. * @interval_msec: polling interval in milliseconds
  5351. * @timeout_msec: timeout in milliseconds
  5352. *
  5353. * Waiting for some bits of register to change is a common
  5354. * operation for ATA controllers. This function reads 32bit LE
  5355. * IO-mapped register @reg and tests for the following condition.
  5356. *
  5357. * (*@reg & mask) != val
  5358. *
  5359. * If the condition is met, it returns; otherwise, the process is
  5360. * repeated after @interval_msec until timeout.
  5361. *
  5362. * LOCKING:
  5363. * Kernel thread context (may sleep)
  5364. *
  5365. * RETURNS:
  5366. * The final register value.
  5367. */
  5368. u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
  5369. unsigned long interval_msec,
  5370. unsigned long timeout_msec)
  5371. {
  5372. unsigned long timeout;
  5373. u32 tmp;
  5374. tmp = ioread32(reg);
  5375. /* Calculate timeout _after_ the first read to make sure
  5376. * preceding writes reach the controller before starting to
  5377. * eat away the timeout.
  5378. */
  5379. timeout = jiffies + (timeout_msec * HZ) / 1000;
  5380. while ((tmp & mask) == val && time_before(jiffies, timeout)) {
  5381. msleep(interval_msec);
  5382. tmp = ioread32(reg);
  5383. }
  5384. return tmp;
  5385. }
  5386. /*
  5387. * Dummy port_ops
  5388. */
  5389. static void ata_dummy_noret(struct ata_port *ap) { }
  5390. static int ata_dummy_ret0(struct ata_port *ap) { return 0; }
  5391. static void ata_dummy_qc_noret(struct ata_queued_cmd *qc) { }
  5392. static u8 ata_dummy_check_status(struct ata_port *ap)
  5393. {
  5394. return ATA_DRDY;
  5395. }
  5396. static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
  5397. {
  5398. return AC_ERR_SYSTEM;
  5399. }
  5400. const struct ata_port_operations ata_dummy_port_ops = {
  5401. .port_disable = ata_port_disable,
  5402. .check_status = ata_dummy_check_status,
  5403. .check_altstatus = ata_dummy_check_status,
  5404. .dev_select = ata_noop_dev_select,
  5405. .qc_prep = ata_noop_qc_prep,
  5406. .qc_issue = ata_dummy_qc_issue,
  5407. .freeze = ata_dummy_noret,
  5408. .thaw = ata_dummy_noret,
  5409. .error_handler = ata_dummy_noret,
  5410. .post_internal_cmd = ata_dummy_qc_noret,
  5411. .irq_clear = ata_dummy_noret,
  5412. .port_start = ata_dummy_ret0,
  5413. .port_stop = ata_dummy_noret,
  5414. };
  5415. /*
  5416. * libata is essentially a library of internal helper functions for
  5417. * low-level ATA host controller drivers. As such, the API/ABI is
  5418. * likely to change as new drivers are added and updated.
  5419. * Do not depend on ABI/API stability.
  5420. */
  5421. EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
  5422. EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
  5423. EXPORT_SYMBOL_GPL(sata_deb_timing_long);
  5424. EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
  5425. EXPORT_SYMBOL_GPL(ata_std_bios_param);
  5426. EXPORT_SYMBOL_GPL(ata_std_ports);
  5427. EXPORT_SYMBOL_GPL(ata_host_init);
  5428. EXPORT_SYMBOL_GPL(ata_device_add);
  5429. EXPORT_SYMBOL_GPL(ata_host_detach);
  5430. EXPORT_SYMBOL_GPL(ata_sg_init);
  5431. EXPORT_SYMBOL_GPL(ata_sg_init_one);
  5432. EXPORT_SYMBOL_GPL(ata_hsm_move);
  5433. EXPORT_SYMBOL_GPL(ata_qc_complete);
  5434. EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
  5435. EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
  5436. EXPORT_SYMBOL_GPL(ata_tf_load);
  5437. EXPORT_SYMBOL_GPL(ata_tf_read);
  5438. EXPORT_SYMBOL_GPL(ata_noop_dev_select);
  5439. EXPORT_SYMBOL_GPL(ata_std_dev_select);
  5440. EXPORT_SYMBOL_GPL(sata_print_link_status);
  5441. EXPORT_SYMBOL_GPL(ata_tf_to_fis);
  5442. EXPORT_SYMBOL_GPL(ata_tf_from_fis);
  5443. EXPORT_SYMBOL_GPL(ata_check_status);
  5444. EXPORT_SYMBOL_GPL(ata_altstatus);
  5445. EXPORT_SYMBOL_GPL(ata_exec_command);
  5446. EXPORT_SYMBOL_GPL(ata_port_start);
  5447. EXPORT_SYMBOL_GPL(ata_interrupt);
  5448. EXPORT_SYMBOL_GPL(ata_data_xfer);
  5449. EXPORT_SYMBOL_GPL(ata_data_xfer_noirq);
  5450. EXPORT_SYMBOL_GPL(ata_qc_prep);
  5451. EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
  5452. EXPORT_SYMBOL_GPL(ata_bmdma_setup);
  5453. EXPORT_SYMBOL_GPL(ata_bmdma_start);
  5454. EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
  5455. EXPORT_SYMBOL_GPL(ata_bmdma_status);
  5456. EXPORT_SYMBOL_GPL(ata_bmdma_stop);
  5457. EXPORT_SYMBOL_GPL(ata_bmdma_freeze);
  5458. EXPORT_SYMBOL_GPL(ata_bmdma_thaw);
  5459. EXPORT_SYMBOL_GPL(ata_bmdma_drive_eh);
  5460. EXPORT_SYMBOL_GPL(ata_bmdma_error_handler);
  5461. EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd);
  5462. EXPORT_SYMBOL_GPL(ata_port_probe);
  5463. EXPORT_SYMBOL_GPL(ata_dev_disable);
  5464. EXPORT_SYMBOL_GPL(sata_set_spd);
  5465. EXPORT_SYMBOL_GPL(sata_phy_debounce);
  5466. EXPORT_SYMBOL_GPL(sata_phy_resume);
  5467. EXPORT_SYMBOL_GPL(sata_phy_reset);
  5468. EXPORT_SYMBOL_GPL(__sata_phy_reset);
  5469. EXPORT_SYMBOL_GPL(ata_bus_reset);
  5470. EXPORT_SYMBOL_GPL(ata_std_prereset);
  5471. EXPORT_SYMBOL_GPL(ata_std_softreset);
  5472. EXPORT_SYMBOL_GPL(sata_port_hardreset);
  5473. EXPORT_SYMBOL_GPL(sata_std_hardreset);
  5474. EXPORT_SYMBOL_GPL(ata_std_postreset);
  5475. EXPORT_SYMBOL_GPL(ata_dev_classify);
  5476. EXPORT_SYMBOL_GPL(ata_dev_pair);
  5477. EXPORT_SYMBOL_GPL(ata_port_disable);
  5478. EXPORT_SYMBOL_GPL(ata_ratelimit);
  5479. EXPORT_SYMBOL_GPL(ata_wait_register);
  5480. EXPORT_SYMBOL_GPL(ata_busy_sleep);
  5481. EXPORT_SYMBOL_GPL(ata_port_queue_task);
  5482. EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
  5483. EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
  5484. EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
  5485. EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
  5486. EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
  5487. EXPORT_SYMBOL_GPL(ata_host_intr);
  5488. EXPORT_SYMBOL_GPL(sata_scr_valid);
  5489. EXPORT_SYMBOL_GPL(sata_scr_read);
  5490. EXPORT_SYMBOL_GPL(sata_scr_write);
  5491. EXPORT_SYMBOL_GPL(sata_scr_write_flush);
  5492. EXPORT_SYMBOL_GPL(ata_port_online);
  5493. EXPORT_SYMBOL_GPL(ata_port_offline);
  5494. #ifdef CONFIG_PM
  5495. EXPORT_SYMBOL_GPL(ata_host_suspend);
  5496. EXPORT_SYMBOL_GPL(ata_host_resume);
  5497. #endif /* CONFIG_PM */
  5498. EXPORT_SYMBOL_GPL(ata_id_string);
  5499. EXPORT_SYMBOL_GPL(ata_id_c_string);
  5500. EXPORT_SYMBOL_GPL(ata_id_to_dma_mode);
  5501. EXPORT_SYMBOL_GPL(ata_device_blacklisted);
  5502. EXPORT_SYMBOL_GPL(ata_scsi_simulate);
  5503. EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
  5504. EXPORT_SYMBOL_GPL(ata_timing_compute);
  5505. EXPORT_SYMBOL_GPL(ata_timing_merge);
  5506. #ifdef CONFIG_PCI
  5507. EXPORT_SYMBOL_GPL(pci_test_config_bits);
  5508. EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
  5509. EXPORT_SYMBOL_GPL(ata_pci_init_one);
  5510. EXPORT_SYMBOL_GPL(ata_pci_remove_one);
  5511. #ifdef CONFIG_PM
  5512. EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
  5513. EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
  5514. EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
  5515. EXPORT_SYMBOL_GPL(ata_pci_device_resume);
  5516. #endif /* CONFIG_PM */
  5517. EXPORT_SYMBOL_GPL(ata_pci_default_filter);
  5518. EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
  5519. #endif /* CONFIG_PCI */
  5520. #ifdef CONFIG_PM
  5521. EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
  5522. EXPORT_SYMBOL_GPL(ata_scsi_device_resume);
  5523. #endif /* CONFIG_PM */
  5524. EXPORT_SYMBOL_GPL(ata_eng_timeout);
  5525. EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
  5526. EXPORT_SYMBOL_GPL(ata_port_abort);
  5527. EXPORT_SYMBOL_GPL(ata_port_freeze);
  5528. EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
  5529. EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
  5530. EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
  5531. EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
  5532. EXPORT_SYMBOL_GPL(ata_do_eh);
  5533. EXPORT_SYMBOL_GPL(ata_irq_on);
  5534. EXPORT_SYMBOL_GPL(ata_dummy_irq_on);
  5535. EXPORT_SYMBOL_GPL(ata_irq_ack);
  5536. EXPORT_SYMBOL_GPL(ata_dummy_irq_ack);
  5537. EXPORT_SYMBOL_GPL(ata_dev_try_classify);
  5538. EXPORT_SYMBOL_GPL(ata_cable_40wire);
  5539. EXPORT_SYMBOL_GPL(ata_cable_80wire);
  5540. EXPORT_SYMBOL_GPL(ata_cable_unknown);
  5541. EXPORT_SYMBOL_GPL(ata_cable_sata);