libata-core.c 157 KB

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