libata-core.c 169 KB

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