libata-core.c 156 KB

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