libata-core.c 169 KB

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