libata-core.c 155 KB

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