libata-core.c 170 KB

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