libata-core.c 153 KB

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