libata-core.c 169 KB

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