libata-core.c 171 KB

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