libata-core.c 132 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473
  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/config.h>
  35. #include <linux/kernel.h>
  36. #include <linux/module.h>
  37. #include <linux/pci.h>
  38. #include <linux/init.h>
  39. #include <linux/list.h>
  40. #include <linux/mm.h>
  41. #include <linux/highmem.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/blkdev.h>
  44. #include <linux/delay.h>
  45. #include <linux/timer.h>
  46. #include <linux/interrupt.h>
  47. #include <linux/completion.h>
  48. #include <linux/suspend.h>
  49. #include <linux/workqueue.h>
  50. #include <linux/jiffies.h>
  51. #include <linux/scatterlist.h>
  52. #include <scsi/scsi.h>
  53. #include "scsi_priv.h"
  54. #include <scsi/scsi_cmnd.h>
  55. #include <scsi/scsi_host.h>
  56. #include <linux/libata.h>
  57. #include <asm/io.h>
  58. #include <asm/semaphore.h>
  59. #include <asm/byteorder.h>
  60. #include "libata.h"
  61. static unsigned int ata_busy_sleep (struct ata_port *ap,
  62. unsigned long tmout_pat,
  63. unsigned long tmout);
  64. static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
  65. static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
  66. static void ata_set_mode(struct ata_port *ap);
  67. static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
  68. static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
  69. static int fgb(u32 bitmap);
  70. static int ata_choose_xfer_mode(const struct ata_port *ap,
  71. u8 *xfer_mode_out,
  72. unsigned int *xfer_shift_out);
  73. static void ata_pio_error(struct ata_port *ap);
  74. static unsigned int ata_unique_id = 1;
  75. static struct workqueue_struct *ata_wq;
  76. int atapi_enabled = 0;
  77. module_param(atapi_enabled, int, 0444);
  78. MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
  79. MODULE_AUTHOR("Jeff Garzik");
  80. MODULE_DESCRIPTION("Library module for ATA devices");
  81. MODULE_LICENSE("GPL");
  82. MODULE_VERSION(DRV_VERSION);
  83. /**
  84. * ata_tf_load_pio - send taskfile registers to host controller
  85. * @ap: Port to which output is sent
  86. * @tf: ATA taskfile register set
  87. *
  88. * Outputs ATA taskfile to standard ATA host controller.
  89. *
  90. * LOCKING:
  91. * Inherited from caller.
  92. */
  93. static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
  94. {
  95. struct ata_ioports *ioaddr = &ap->ioaddr;
  96. unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
  97. if (tf->ctl != ap->last_ctl) {
  98. outb(tf->ctl, ioaddr->ctl_addr);
  99. ap->last_ctl = tf->ctl;
  100. ata_wait_idle(ap);
  101. }
  102. if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
  103. outb(tf->hob_feature, ioaddr->feature_addr);
  104. outb(tf->hob_nsect, ioaddr->nsect_addr);
  105. outb(tf->hob_lbal, ioaddr->lbal_addr);
  106. outb(tf->hob_lbam, ioaddr->lbam_addr);
  107. outb(tf->hob_lbah, ioaddr->lbah_addr);
  108. VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
  109. tf->hob_feature,
  110. tf->hob_nsect,
  111. tf->hob_lbal,
  112. tf->hob_lbam,
  113. tf->hob_lbah);
  114. }
  115. if (is_addr) {
  116. outb(tf->feature, ioaddr->feature_addr);
  117. outb(tf->nsect, ioaddr->nsect_addr);
  118. outb(tf->lbal, ioaddr->lbal_addr);
  119. outb(tf->lbam, ioaddr->lbam_addr);
  120. outb(tf->lbah, ioaddr->lbah_addr);
  121. VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
  122. tf->feature,
  123. tf->nsect,
  124. tf->lbal,
  125. tf->lbam,
  126. tf->lbah);
  127. }
  128. if (tf->flags & ATA_TFLAG_DEVICE) {
  129. outb(tf->device, ioaddr->device_addr);
  130. VPRINTK("device 0x%X\n", tf->device);
  131. }
  132. ata_wait_idle(ap);
  133. }
  134. /**
  135. * ata_tf_load_mmio - send taskfile registers to host controller
  136. * @ap: Port to which output is sent
  137. * @tf: ATA taskfile register set
  138. *
  139. * Outputs ATA taskfile to standard ATA host controller using MMIO.
  140. *
  141. * LOCKING:
  142. * Inherited from caller.
  143. */
  144. static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
  145. {
  146. struct ata_ioports *ioaddr = &ap->ioaddr;
  147. unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
  148. if (tf->ctl != ap->last_ctl) {
  149. writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
  150. ap->last_ctl = tf->ctl;
  151. ata_wait_idle(ap);
  152. }
  153. if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
  154. writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
  155. writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
  156. writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
  157. writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
  158. writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
  159. VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
  160. tf->hob_feature,
  161. tf->hob_nsect,
  162. tf->hob_lbal,
  163. tf->hob_lbam,
  164. tf->hob_lbah);
  165. }
  166. if (is_addr) {
  167. writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
  168. writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
  169. writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
  170. writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
  171. writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
  172. VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
  173. tf->feature,
  174. tf->nsect,
  175. tf->lbal,
  176. tf->lbam,
  177. tf->lbah);
  178. }
  179. if (tf->flags & ATA_TFLAG_DEVICE) {
  180. writeb(tf->device, (void __iomem *) ioaddr->device_addr);
  181. VPRINTK("device 0x%X\n", tf->device);
  182. }
  183. ata_wait_idle(ap);
  184. }
  185. /**
  186. * ata_tf_load - send taskfile registers to host controller
  187. * @ap: Port to which output is sent
  188. * @tf: ATA taskfile register set
  189. *
  190. * Outputs ATA taskfile to standard ATA host controller using MMIO
  191. * or PIO as indicated by the ATA_FLAG_MMIO flag.
  192. * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
  193. * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
  194. * hob_lbal, hob_lbam, and hob_lbah.
  195. *
  196. * This function waits for idle (!BUSY and !DRQ) after writing
  197. * registers. If the control register has a new value, this
  198. * function also waits for idle after writing control and before
  199. * writing the remaining registers.
  200. *
  201. * May be used as the tf_load() entry in ata_port_operations.
  202. *
  203. * LOCKING:
  204. * Inherited from caller.
  205. */
  206. void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
  207. {
  208. if (ap->flags & ATA_FLAG_MMIO)
  209. ata_tf_load_mmio(ap, tf);
  210. else
  211. ata_tf_load_pio(ap, tf);
  212. }
  213. /**
  214. * ata_exec_command_pio - issue ATA command to host controller
  215. * @ap: port to which command is being issued
  216. * @tf: ATA taskfile register set
  217. *
  218. * Issues PIO write to ATA command register, with proper
  219. * synchronization with interrupt handler / other threads.
  220. *
  221. * LOCKING:
  222. * spin_lock_irqsave(host_set lock)
  223. */
  224. static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
  225. {
  226. DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
  227. outb(tf->command, ap->ioaddr.command_addr);
  228. ata_pause(ap);
  229. }
  230. /**
  231. * ata_exec_command_mmio - issue ATA command to host controller
  232. * @ap: port to which command is being issued
  233. * @tf: ATA taskfile register set
  234. *
  235. * Issues MMIO write to ATA command register, with proper
  236. * synchronization with interrupt handler / other threads.
  237. *
  238. * LOCKING:
  239. * spin_lock_irqsave(host_set lock)
  240. */
  241. static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
  242. {
  243. DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
  244. writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
  245. ata_pause(ap);
  246. }
  247. /**
  248. * ata_exec_command - issue ATA command to host controller
  249. * @ap: port to which command is being issued
  250. * @tf: ATA taskfile register set
  251. *
  252. * Issues PIO/MMIO write to ATA command register, with proper
  253. * synchronization with interrupt handler / other threads.
  254. *
  255. * LOCKING:
  256. * spin_lock_irqsave(host_set lock)
  257. */
  258. void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
  259. {
  260. if (ap->flags & ATA_FLAG_MMIO)
  261. ata_exec_command_mmio(ap, tf);
  262. else
  263. ata_exec_command_pio(ap, tf);
  264. }
  265. /**
  266. * ata_tf_to_host - issue ATA taskfile to host controller
  267. * @ap: port to which command is being issued
  268. * @tf: ATA taskfile register set
  269. *
  270. * Issues ATA taskfile register set to ATA host controller,
  271. * with proper synchronization with interrupt handler and
  272. * other threads.
  273. *
  274. * LOCKING:
  275. * spin_lock_irqsave(host_set lock)
  276. */
  277. static inline void ata_tf_to_host(struct ata_port *ap,
  278. const struct ata_taskfile *tf)
  279. {
  280. ap->ops->tf_load(ap, tf);
  281. ap->ops->exec_command(ap, tf);
  282. }
  283. /**
  284. * ata_tf_read_pio - input device's ATA taskfile shadow registers
  285. * @ap: Port from which input is read
  286. * @tf: ATA taskfile register set for storing input
  287. *
  288. * Reads ATA taskfile registers for currently-selected device
  289. * into @tf.
  290. *
  291. * LOCKING:
  292. * Inherited from caller.
  293. */
  294. static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
  295. {
  296. struct ata_ioports *ioaddr = &ap->ioaddr;
  297. tf->command = ata_check_status(ap);
  298. tf->feature = inb(ioaddr->error_addr);
  299. tf->nsect = inb(ioaddr->nsect_addr);
  300. tf->lbal = inb(ioaddr->lbal_addr);
  301. tf->lbam = inb(ioaddr->lbam_addr);
  302. tf->lbah = inb(ioaddr->lbah_addr);
  303. tf->device = inb(ioaddr->device_addr);
  304. if (tf->flags & ATA_TFLAG_LBA48) {
  305. outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
  306. tf->hob_feature = inb(ioaddr->error_addr);
  307. tf->hob_nsect = inb(ioaddr->nsect_addr);
  308. tf->hob_lbal = inb(ioaddr->lbal_addr);
  309. tf->hob_lbam = inb(ioaddr->lbam_addr);
  310. tf->hob_lbah = inb(ioaddr->lbah_addr);
  311. }
  312. }
  313. /**
  314. * ata_tf_read_mmio - input device's ATA taskfile shadow registers
  315. * @ap: Port from which input is read
  316. * @tf: ATA taskfile register set for storing input
  317. *
  318. * Reads ATA taskfile registers for currently-selected device
  319. * into @tf via MMIO.
  320. *
  321. * LOCKING:
  322. * Inherited from caller.
  323. */
  324. static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
  325. {
  326. struct ata_ioports *ioaddr = &ap->ioaddr;
  327. tf->command = ata_check_status(ap);
  328. tf->feature = readb((void __iomem *)ioaddr->error_addr);
  329. tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
  330. tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
  331. tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
  332. tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
  333. tf->device = readb((void __iomem *)ioaddr->device_addr);
  334. if (tf->flags & ATA_TFLAG_LBA48) {
  335. writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
  336. tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
  337. tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
  338. tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
  339. tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
  340. tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
  341. }
  342. }
  343. /**
  344. * ata_tf_read - input device's ATA taskfile shadow registers
  345. * @ap: Port from which input is read
  346. * @tf: ATA taskfile register set for storing input
  347. *
  348. * Reads ATA taskfile registers for currently-selected device
  349. * into @tf.
  350. *
  351. * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
  352. * is set, also reads the hob registers.
  353. *
  354. * May be used as the tf_read() entry in ata_port_operations.
  355. *
  356. * LOCKING:
  357. * Inherited from caller.
  358. */
  359. void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  360. {
  361. if (ap->flags & ATA_FLAG_MMIO)
  362. ata_tf_read_mmio(ap, tf);
  363. else
  364. ata_tf_read_pio(ap, tf);
  365. }
  366. /**
  367. * ata_check_status_pio - Read device status reg & clear interrupt
  368. * @ap: port where the device is
  369. *
  370. * Reads ATA taskfile status register for currently-selected device
  371. * and return its value. This also clears pending interrupts
  372. * from this device
  373. *
  374. * LOCKING:
  375. * Inherited from caller.
  376. */
  377. static u8 ata_check_status_pio(struct ata_port *ap)
  378. {
  379. return inb(ap->ioaddr.status_addr);
  380. }
  381. /**
  382. * ata_check_status_mmio - Read device status reg & clear interrupt
  383. * @ap: port where the device is
  384. *
  385. * Reads ATA taskfile status register for currently-selected device
  386. * via MMIO and return its value. This also clears pending interrupts
  387. * from this device
  388. *
  389. * LOCKING:
  390. * Inherited from caller.
  391. */
  392. static u8 ata_check_status_mmio(struct ata_port *ap)
  393. {
  394. return readb((void __iomem *) ap->ioaddr.status_addr);
  395. }
  396. /**
  397. * ata_check_status - Read device status reg & clear interrupt
  398. * @ap: port where the device is
  399. *
  400. * Reads ATA taskfile status register for currently-selected device
  401. * and return its value. This also clears pending interrupts
  402. * from this device
  403. *
  404. * May be used as the check_status() entry in ata_port_operations.
  405. *
  406. * LOCKING:
  407. * Inherited from caller.
  408. */
  409. u8 ata_check_status(struct ata_port *ap)
  410. {
  411. if (ap->flags & ATA_FLAG_MMIO)
  412. return ata_check_status_mmio(ap);
  413. return ata_check_status_pio(ap);
  414. }
  415. /**
  416. * ata_altstatus - Read device alternate status reg
  417. * @ap: port where the device is
  418. *
  419. * Reads ATA taskfile alternate status register for
  420. * currently-selected device and return its value.
  421. *
  422. * Note: may NOT be used as the check_altstatus() entry in
  423. * ata_port_operations.
  424. *
  425. * LOCKING:
  426. * Inherited from caller.
  427. */
  428. u8 ata_altstatus(struct ata_port *ap)
  429. {
  430. if (ap->ops->check_altstatus)
  431. return ap->ops->check_altstatus(ap);
  432. if (ap->flags & ATA_FLAG_MMIO)
  433. return readb((void __iomem *)ap->ioaddr.altstatus_addr);
  434. return inb(ap->ioaddr.altstatus_addr);
  435. }
  436. /**
  437. * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
  438. * @tf: Taskfile to convert
  439. * @fis: Buffer into which data will output
  440. * @pmp: Port multiplier port
  441. *
  442. * Converts a standard ATA taskfile to a Serial ATA
  443. * FIS structure (Register - Host to Device).
  444. *
  445. * LOCKING:
  446. * Inherited from caller.
  447. */
  448. void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
  449. {
  450. fis[0] = 0x27; /* Register - Host to Device FIS */
  451. fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
  452. bit 7 indicates Command FIS */
  453. fis[2] = tf->command;
  454. fis[3] = tf->feature;
  455. fis[4] = tf->lbal;
  456. fis[5] = tf->lbam;
  457. fis[6] = tf->lbah;
  458. fis[7] = tf->device;
  459. fis[8] = tf->hob_lbal;
  460. fis[9] = tf->hob_lbam;
  461. fis[10] = tf->hob_lbah;
  462. fis[11] = tf->hob_feature;
  463. fis[12] = tf->nsect;
  464. fis[13] = tf->hob_nsect;
  465. fis[14] = 0;
  466. fis[15] = tf->ctl;
  467. fis[16] = 0;
  468. fis[17] = 0;
  469. fis[18] = 0;
  470. fis[19] = 0;
  471. }
  472. /**
  473. * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
  474. * @fis: Buffer from which data will be input
  475. * @tf: Taskfile to output
  476. *
  477. * Converts a serial ATA FIS structure to a standard ATA taskfile.
  478. *
  479. * LOCKING:
  480. * Inherited from caller.
  481. */
  482. void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
  483. {
  484. tf->command = fis[2]; /* status */
  485. tf->feature = fis[3]; /* error */
  486. tf->lbal = fis[4];
  487. tf->lbam = fis[5];
  488. tf->lbah = fis[6];
  489. tf->device = fis[7];
  490. tf->hob_lbal = fis[8];
  491. tf->hob_lbam = fis[9];
  492. tf->hob_lbah = fis[10];
  493. tf->nsect = fis[12];
  494. tf->hob_nsect = fis[13];
  495. }
  496. static const u8 ata_rw_cmds[] = {
  497. /* pio multi */
  498. ATA_CMD_READ_MULTI,
  499. ATA_CMD_WRITE_MULTI,
  500. ATA_CMD_READ_MULTI_EXT,
  501. ATA_CMD_WRITE_MULTI_EXT,
  502. 0,
  503. 0,
  504. 0,
  505. ATA_CMD_WRITE_MULTI_FUA_EXT,
  506. /* pio */
  507. ATA_CMD_PIO_READ,
  508. ATA_CMD_PIO_WRITE,
  509. ATA_CMD_PIO_READ_EXT,
  510. ATA_CMD_PIO_WRITE_EXT,
  511. 0,
  512. 0,
  513. 0,
  514. 0,
  515. /* dma */
  516. ATA_CMD_READ,
  517. ATA_CMD_WRITE,
  518. ATA_CMD_READ_EXT,
  519. ATA_CMD_WRITE_EXT,
  520. 0,
  521. 0,
  522. 0,
  523. ATA_CMD_WRITE_FUA_EXT
  524. };
  525. /**
  526. * ata_rwcmd_protocol - set taskfile r/w commands and protocol
  527. * @qc: command to examine and configure
  528. *
  529. * Examine the device configuration and tf->flags to calculate
  530. * the proper read/write commands and protocol to use.
  531. *
  532. * LOCKING:
  533. * caller.
  534. */
  535. int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
  536. {
  537. struct ata_taskfile *tf = &qc->tf;
  538. struct ata_device *dev = qc->dev;
  539. u8 cmd;
  540. int index, fua, lba48, write;
  541. fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
  542. lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
  543. write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
  544. if (dev->flags & ATA_DFLAG_PIO) {
  545. tf->protocol = ATA_PROT_PIO;
  546. index = dev->multi_count ? 0 : 8;
  547. } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
  548. /* Unable to use DMA due to host limitation */
  549. tf->protocol = ATA_PROT_PIO;
  550. index = dev->multi_count ? 0 : 4;
  551. } else {
  552. tf->protocol = ATA_PROT_DMA;
  553. index = 16;
  554. }
  555. cmd = ata_rw_cmds[index + fua + lba48 + write];
  556. if (cmd) {
  557. tf->command = cmd;
  558. return 0;
  559. }
  560. return -1;
  561. }
  562. static const char * const xfer_mode_str[] = {
  563. "UDMA/16",
  564. "UDMA/25",
  565. "UDMA/33",
  566. "UDMA/44",
  567. "UDMA/66",
  568. "UDMA/100",
  569. "UDMA/133",
  570. "UDMA7",
  571. "MWDMA0",
  572. "MWDMA1",
  573. "MWDMA2",
  574. "PIO0",
  575. "PIO1",
  576. "PIO2",
  577. "PIO3",
  578. "PIO4",
  579. };
  580. /**
  581. * ata_udma_string - convert UDMA bit offset to string
  582. * @mask: mask of bits supported; only highest bit counts.
  583. *
  584. * Determine string which represents the highest speed
  585. * (highest bit in @udma_mask).
  586. *
  587. * LOCKING:
  588. * None.
  589. *
  590. * RETURNS:
  591. * Constant C string representing highest speed listed in
  592. * @udma_mask, or the constant C string "<n/a>".
  593. */
  594. static const char *ata_mode_string(unsigned int mask)
  595. {
  596. int i;
  597. for (i = 7; i >= 0; i--)
  598. if (mask & (1 << i))
  599. goto out;
  600. for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
  601. if (mask & (1 << i))
  602. goto out;
  603. for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
  604. if (mask & (1 << i))
  605. goto out;
  606. return "<n/a>";
  607. out:
  608. return xfer_mode_str[i];
  609. }
  610. /**
  611. * ata_pio_devchk - PATA device presence detection
  612. * @ap: ATA channel to examine
  613. * @device: Device to examine (starting at zero)
  614. *
  615. * This technique was originally described in
  616. * Hale Landis's ATADRVR (www.ata-atapi.com), and
  617. * later found its way into the ATA/ATAPI spec.
  618. *
  619. * Write a pattern to the ATA shadow registers,
  620. * and if a device is present, it will respond by
  621. * correctly storing and echoing back the
  622. * ATA shadow register contents.
  623. *
  624. * LOCKING:
  625. * caller.
  626. */
  627. static unsigned int ata_pio_devchk(struct ata_port *ap,
  628. unsigned int device)
  629. {
  630. struct ata_ioports *ioaddr = &ap->ioaddr;
  631. u8 nsect, lbal;
  632. ap->ops->dev_select(ap, device);
  633. outb(0x55, ioaddr->nsect_addr);
  634. outb(0xaa, ioaddr->lbal_addr);
  635. outb(0xaa, ioaddr->nsect_addr);
  636. outb(0x55, ioaddr->lbal_addr);
  637. outb(0x55, ioaddr->nsect_addr);
  638. outb(0xaa, ioaddr->lbal_addr);
  639. nsect = inb(ioaddr->nsect_addr);
  640. lbal = inb(ioaddr->lbal_addr);
  641. if ((nsect == 0x55) && (lbal == 0xaa))
  642. return 1; /* we found a device */
  643. return 0; /* nothing found */
  644. }
  645. /**
  646. * ata_mmio_devchk - PATA device presence detection
  647. * @ap: ATA channel to examine
  648. * @device: Device to examine (starting at zero)
  649. *
  650. * This technique was originally described in
  651. * Hale Landis's ATADRVR (www.ata-atapi.com), and
  652. * later found its way into the ATA/ATAPI spec.
  653. *
  654. * Write a pattern to the ATA shadow registers,
  655. * and if a device is present, it will respond by
  656. * correctly storing and echoing back the
  657. * ATA shadow register contents.
  658. *
  659. * LOCKING:
  660. * caller.
  661. */
  662. static unsigned int ata_mmio_devchk(struct ata_port *ap,
  663. unsigned int device)
  664. {
  665. struct ata_ioports *ioaddr = &ap->ioaddr;
  666. u8 nsect, lbal;
  667. ap->ops->dev_select(ap, device);
  668. writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
  669. writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
  670. writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
  671. writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
  672. writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
  673. writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
  674. nsect = readb((void __iomem *) ioaddr->nsect_addr);
  675. lbal = readb((void __iomem *) ioaddr->lbal_addr);
  676. if ((nsect == 0x55) && (lbal == 0xaa))
  677. return 1; /* we found a device */
  678. return 0; /* nothing found */
  679. }
  680. /**
  681. * ata_devchk - PATA device presence detection
  682. * @ap: ATA channel to examine
  683. * @device: Device to examine (starting at zero)
  684. *
  685. * Dispatch ATA device presence detection, depending
  686. * on whether we are using PIO or MMIO to talk to the
  687. * ATA shadow registers.
  688. *
  689. * LOCKING:
  690. * caller.
  691. */
  692. static unsigned int ata_devchk(struct ata_port *ap,
  693. unsigned int device)
  694. {
  695. if (ap->flags & ATA_FLAG_MMIO)
  696. return ata_mmio_devchk(ap, device);
  697. return ata_pio_devchk(ap, device);
  698. }
  699. /**
  700. * ata_dev_classify - determine device type based on ATA-spec signature
  701. * @tf: ATA taskfile register set for device to be identified
  702. *
  703. * Determine from taskfile register contents whether a device is
  704. * ATA or ATAPI, as per "Signature and persistence" section
  705. * of ATA/PI spec (volume 1, sect 5.14).
  706. *
  707. * LOCKING:
  708. * None.
  709. *
  710. * RETURNS:
  711. * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
  712. * the event of failure.
  713. */
  714. unsigned int ata_dev_classify(const struct ata_taskfile *tf)
  715. {
  716. /* Apple's open source Darwin code hints that some devices only
  717. * put a proper signature into the LBA mid/high registers,
  718. * So, we only check those. It's sufficient for uniqueness.
  719. */
  720. if (((tf->lbam == 0) && (tf->lbah == 0)) ||
  721. ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
  722. DPRINTK("found ATA device by sig\n");
  723. return ATA_DEV_ATA;
  724. }
  725. if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
  726. ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
  727. DPRINTK("found ATAPI device by sig\n");
  728. return ATA_DEV_ATAPI;
  729. }
  730. DPRINTK("unknown device\n");
  731. return ATA_DEV_UNKNOWN;
  732. }
  733. /**
  734. * ata_dev_try_classify - Parse returned ATA device signature
  735. * @ap: ATA channel to examine
  736. * @device: Device to examine (starting at zero)
  737. *
  738. * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
  739. * an ATA/ATAPI-defined set of values is placed in the ATA
  740. * shadow registers, indicating the results of device detection
  741. * and diagnostics.
  742. *
  743. * Select the ATA device, and read the values from the ATA shadow
  744. * registers. Then parse according to the Error register value,
  745. * and the spec-defined values examined by ata_dev_classify().
  746. *
  747. * LOCKING:
  748. * caller.
  749. */
  750. static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
  751. {
  752. struct ata_device *dev = &ap->device[device];
  753. struct ata_taskfile tf;
  754. unsigned int class;
  755. u8 err;
  756. ap->ops->dev_select(ap, device);
  757. memset(&tf, 0, sizeof(tf));
  758. ap->ops->tf_read(ap, &tf);
  759. err = tf.feature;
  760. dev->class = ATA_DEV_NONE;
  761. /* see if device passed diags */
  762. if (err == 1)
  763. /* do nothing */ ;
  764. else if ((device == 0) && (err == 0x81))
  765. /* do nothing */ ;
  766. else
  767. return err;
  768. /* determine if device if ATA or ATAPI */
  769. class = ata_dev_classify(&tf);
  770. if (class == ATA_DEV_UNKNOWN)
  771. return err;
  772. if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
  773. return err;
  774. dev->class = class;
  775. return err;
  776. }
  777. /**
  778. * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
  779. * @id: IDENTIFY DEVICE results we will examine
  780. * @s: string into which data is output
  781. * @ofs: offset into identify device page
  782. * @len: length of string to return. must be an even number.
  783. *
  784. * The strings in the IDENTIFY DEVICE page are broken up into
  785. * 16-bit chunks. Run through the string, and output each
  786. * 8-bit chunk linearly, regardless of platform.
  787. *
  788. * LOCKING:
  789. * caller.
  790. */
  791. void ata_dev_id_string(const u16 *id, unsigned char *s,
  792. unsigned int ofs, unsigned int len)
  793. {
  794. unsigned int c;
  795. while (len > 0) {
  796. c = id[ofs] >> 8;
  797. *s = c;
  798. s++;
  799. c = id[ofs] & 0xff;
  800. *s = c;
  801. s++;
  802. ofs++;
  803. len -= 2;
  804. }
  805. }
  806. /**
  807. * ata_noop_dev_select - Select device 0/1 on ATA bus
  808. * @ap: ATA channel to manipulate
  809. * @device: ATA device (numbered from zero) to select
  810. *
  811. * This function performs no actual function.
  812. *
  813. * May be used as the dev_select() entry in ata_port_operations.
  814. *
  815. * LOCKING:
  816. * caller.
  817. */
  818. void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
  819. {
  820. }
  821. /**
  822. * ata_std_dev_select - Select device 0/1 on ATA bus
  823. * @ap: ATA channel to manipulate
  824. * @device: ATA device (numbered from zero) to select
  825. *
  826. * Use the method defined in the ATA specification to
  827. * make either device 0, or device 1, active on the
  828. * ATA channel. Works with both PIO and MMIO.
  829. *
  830. * May be used as the dev_select() entry in ata_port_operations.
  831. *
  832. * LOCKING:
  833. * caller.
  834. */
  835. void ata_std_dev_select (struct ata_port *ap, unsigned int device)
  836. {
  837. u8 tmp;
  838. if (device == 0)
  839. tmp = ATA_DEVICE_OBS;
  840. else
  841. tmp = ATA_DEVICE_OBS | ATA_DEV1;
  842. if (ap->flags & ATA_FLAG_MMIO) {
  843. writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
  844. } else {
  845. outb(tmp, ap->ioaddr.device_addr);
  846. }
  847. ata_pause(ap); /* needed; also flushes, for mmio */
  848. }
  849. /**
  850. * ata_dev_select - Select device 0/1 on ATA bus
  851. * @ap: ATA channel to manipulate
  852. * @device: ATA device (numbered from zero) to select
  853. * @wait: non-zero to wait for Status register BSY bit to clear
  854. * @can_sleep: non-zero if context allows sleeping
  855. *
  856. * Use the method defined in the ATA specification to
  857. * make either device 0, or device 1, active on the
  858. * ATA channel.
  859. *
  860. * This is a high-level version of ata_std_dev_select(),
  861. * which additionally provides the services of inserting
  862. * the proper pauses and status polling, where needed.
  863. *
  864. * LOCKING:
  865. * caller.
  866. */
  867. void ata_dev_select(struct ata_port *ap, unsigned int device,
  868. unsigned int wait, unsigned int can_sleep)
  869. {
  870. VPRINTK("ENTER, ata%u: device %u, wait %u\n",
  871. ap->id, device, wait);
  872. if (wait)
  873. ata_wait_idle(ap);
  874. ap->ops->dev_select(ap, device);
  875. if (wait) {
  876. if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
  877. msleep(150);
  878. ata_wait_idle(ap);
  879. }
  880. }
  881. /**
  882. * ata_dump_id - IDENTIFY DEVICE info debugging output
  883. * @dev: Device whose IDENTIFY DEVICE page we will dump
  884. *
  885. * Dump selected 16-bit words from a detected device's
  886. * IDENTIFY PAGE page.
  887. *
  888. * LOCKING:
  889. * caller.
  890. */
  891. static inline void ata_dump_id(const struct ata_device *dev)
  892. {
  893. DPRINTK("49==0x%04x "
  894. "53==0x%04x "
  895. "63==0x%04x "
  896. "64==0x%04x "
  897. "75==0x%04x \n",
  898. dev->id[49],
  899. dev->id[53],
  900. dev->id[63],
  901. dev->id[64],
  902. dev->id[75]);
  903. DPRINTK("80==0x%04x "
  904. "81==0x%04x "
  905. "82==0x%04x "
  906. "83==0x%04x "
  907. "84==0x%04x \n",
  908. dev->id[80],
  909. dev->id[81],
  910. dev->id[82],
  911. dev->id[83],
  912. dev->id[84]);
  913. DPRINTK("88==0x%04x "
  914. "93==0x%04x\n",
  915. dev->id[88],
  916. dev->id[93]);
  917. }
  918. /*
  919. * Compute the PIO modes available for this device. This is not as
  920. * trivial as it seems if we must consider early devices correctly.
  921. *
  922. * FIXME: pre IDE drive timing (do we care ?).
  923. */
  924. static unsigned int ata_pio_modes(const struct ata_device *adev)
  925. {
  926. u16 modes;
  927. /* Usual case. Word 53 indicates word 64 is valid */
  928. if (adev->id[ATA_ID_FIELD_VALID] & (1 << 1)) {
  929. modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
  930. modes <<= 3;
  931. modes |= 0x7;
  932. return modes;
  933. }
  934. /* If word 64 isn't valid then Word 51 high byte holds the PIO timing
  935. number for the maximum. Turn it into a mask and return it */
  936. modes = (2 << ((adev->id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF)) - 1 ;
  937. return modes;
  938. /* But wait.. there's more. Design your standards by committee and
  939. you too can get a free iordy field to process. However its the
  940. speeds not the modes that are supported... Note drivers using the
  941. timing API will get this right anyway */
  942. }
  943. void ata_qc_complete_internal(struct ata_queued_cmd *qc)
  944. {
  945. struct completion *waiting = qc->private_data;
  946. qc->ap->ops->tf_read(qc->ap, &qc->tf);
  947. complete(waiting);
  948. }
  949. /**
  950. * ata_exec_internal - execute libata internal command
  951. * @ap: Port to which the command is sent
  952. * @dev: Device to which the command is sent
  953. * @tf: Taskfile registers for the command and the result
  954. * @dma_dir: Data tranfer direction of the command
  955. * @buf: Data buffer of the command
  956. * @buflen: Length of data buffer
  957. *
  958. * Executes libata internal command with timeout. @tf contains
  959. * command on entry and result on return. Timeout and error
  960. * conditions are reported via return value. No recovery action
  961. * is taken after a command times out. It's caller's duty to
  962. * clean up after timeout.
  963. *
  964. * LOCKING:
  965. * None. Should be called with kernel context, might sleep.
  966. */
  967. static unsigned
  968. ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
  969. struct ata_taskfile *tf,
  970. int dma_dir, void *buf, unsigned int buflen)
  971. {
  972. u8 command = tf->command;
  973. struct ata_queued_cmd *qc;
  974. DECLARE_COMPLETION(wait);
  975. unsigned long flags;
  976. unsigned int err_mask;
  977. spin_lock_irqsave(&ap->host_set->lock, flags);
  978. qc = ata_qc_new_init(ap, dev);
  979. BUG_ON(qc == NULL);
  980. qc->tf = *tf;
  981. qc->dma_dir = dma_dir;
  982. if (dma_dir != DMA_NONE) {
  983. ata_sg_init_one(qc, buf, buflen);
  984. qc->nsect = buflen / ATA_SECT_SIZE;
  985. }
  986. qc->private_data = &wait;
  987. qc->complete_fn = ata_qc_complete_internal;
  988. if (ata_qc_issue(qc))
  989. goto issue_fail;
  990. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  991. if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
  992. spin_lock_irqsave(&ap->host_set->lock, flags);
  993. /* We're racing with irq here. If we lose, the
  994. * following test prevents us from completing the qc
  995. * again. If completion irq occurs after here but
  996. * before the caller cleans up, it will result in a
  997. * spurious interrupt. We can live with that.
  998. */
  999. if (qc->flags & ATA_QCFLAG_ACTIVE) {
  1000. qc->err_mask = AC_ERR_OTHER;
  1001. ata_qc_complete(qc);
  1002. printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
  1003. ap->id, command);
  1004. }
  1005. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  1006. }
  1007. *tf = qc->tf;
  1008. err_mask = qc->err_mask;
  1009. ata_qc_free(qc);
  1010. return err_mask;
  1011. issue_fail:
  1012. ata_qc_free(qc);
  1013. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  1014. return AC_ERR_OTHER;
  1015. }
  1016. /**
  1017. * ata_pio_need_iordy - check if iordy needed
  1018. * @adev: ATA device
  1019. *
  1020. * Check if the current speed of the device requires IORDY. Used
  1021. * by various controllers for chip configuration.
  1022. */
  1023. unsigned int ata_pio_need_iordy(const struct ata_device *adev)
  1024. {
  1025. int pio;
  1026. int speed = adev->pio_mode - XFER_PIO_0;
  1027. if (speed < 2)
  1028. return 0;
  1029. if (speed > 2)
  1030. return 1;
  1031. /* If we have no drive specific rule, then PIO 2 is non IORDY */
  1032. if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
  1033. pio = adev->id[ATA_ID_EIDE_PIO];
  1034. /* Is the speed faster than the drive allows non IORDY ? */
  1035. if (pio) {
  1036. /* This is cycle times not frequency - watch the logic! */
  1037. if (pio > 240) /* PIO2 is 240nS per cycle */
  1038. return 1;
  1039. return 0;
  1040. }
  1041. }
  1042. return 0;
  1043. }
  1044. /**
  1045. * ata_dev_identify - obtain IDENTIFY x DEVICE page
  1046. * @ap: port on which device we wish to probe resides
  1047. * @device: device bus address, starting at zero
  1048. *
  1049. * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
  1050. * command, and read back the 512-byte device information page.
  1051. * The device information page is fed to us via the standard
  1052. * PIO-IN protocol, but we hand-code it here. (TODO: investigate
  1053. * using standard PIO-IN paths)
  1054. *
  1055. * After reading the device information page, we use several
  1056. * bits of information from it to initialize data structures
  1057. * that will be used during the lifetime of the ata_device.
  1058. * Other data from the info page is used to disqualify certain
  1059. * older ATA devices we do not wish to support.
  1060. *
  1061. * LOCKING:
  1062. * Inherited from caller. Some functions called by this function
  1063. * obtain the host_set lock.
  1064. */
  1065. static void ata_dev_identify(struct ata_port *ap, unsigned int device)
  1066. {
  1067. struct ata_device *dev = &ap->device[device];
  1068. unsigned int major_version;
  1069. u16 tmp;
  1070. unsigned long xfer_modes;
  1071. unsigned int using_edd;
  1072. struct ata_taskfile tf;
  1073. unsigned int err_mask;
  1074. int rc;
  1075. if (!ata_dev_present(dev)) {
  1076. DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
  1077. ap->id, device);
  1078. return;
  1079. }
  1080. if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
  1081. using_edd = 0;
  1082. else
  1083. using_edd = 1;
  1084. DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
  1085. assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
  1086. dev->class == ATA_DEV_NONE);
  1087. ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
  1088. retry:
  1089. ata_tf_init(ap, &tf, device);
  1090. if (dev->class == ATA_DEV_ATA) {
  1091. tf.command = ATA_CMD_ID_ATA;
  1092. DPRINTK("do ATA identify\n");
  1093. } else {
  1094. tf.command = ATA_CMD_ID_ATAPI;
  1095. DPRINTK("do ATAPI identify\n");
  1096. }
  1097. tf.protocol = ATA_PROT_PIO;
  1098. err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
  1099. dev->id, sizeof(dev->id));
  1100. if (err_mask) {
  1101. if (err_mask & ~AC_ERR_DEV)
  1102. goto err_out;
  1103. /*
  1104. * arg! EDD works for all test cases, but seems to return
  1105. * the ATA signature for some ATAPI devices. Until the
  1106. * reason for this is found and fixed, we fix up the mess
  1107. * here. If IDENTIFY DEVICE returns command aborted
  1108. * (as ATAPI devices do), then we issue an
  1109. * IDENTIFY PACKET DEVICE.
  1110. *
  1111. * ATA software reset (SRST, the default) does not appear
  1112. * to have this problem.
  1113. */
  1114. if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
  1115. u8 err = tf.feature;
  1116. if (err & ATA_ABORTED) {
  1117. dev->class = ATA_DEV_ATAPI;
  1118. goto retry;
  1119. }
  1120. }
  1121. goto err_out;
  1122. }
  1123. swap_buf_le16(dev->id, ATA_ID_WORDS);
  1124. /* print device capabilities */
  1125. printk(KERN_DEBUG "ata%u: dev %u cfg "
  1126. "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
  1127. ap->id, device, dev->id[49],
  1128. dev->id[82], dev->id[83], dev->id[84],
  1129. dev->id[85], dev->id[86], dev->id[87],
  1130. dev->id[88]);
  1131. /*
  1132. * common ATA, ATAPI feature tests
  1133. */
  1134. /* we require DMA support (bits 8 of word 49) */
  1135. if (!ata_id_has_dma(dev->id)) {
  1136. printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
  1137. goto err_out_nosup;
  1138. }
  1139. /* quick-n-dirty find max transfer mode; for printk only */
  1140. xfer_modes = dev->id[ATA_ID_UDMA_MODES];
  1141. if (!xfer_modes)
  1142. xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
  1143. if (!xfer_modes)
  1144. xfer_modes = ata_pio_modes(dev);
  1145. ata_dump_id(dev);
  1146. /* ATA-specific feature tests */
  1147. if (dev->class == ATA_DEV_ATA) {
  1148. if (!ata_id_is_ata(dev->id)) /* sanity check */
  1149. goto err_out_nosup;
  1150. /* get major version */
  1151. tmp = dev->id[ATA_ID_MAJOR_VER];
  1152. for (major_version = 14; major_version >= 1; major_version--)
  1153. if (tmp & (1 << major_version))
  1154. break;
  1155. /*
  1156. * The exact sequence expected by certain pre-ATA4 drives is:
  1157. * SRST RESET
  1158. * IDENTIFY
  1159. * INITIALIZE DEVICE PARAMETERS
  1160. * anything else..
  1161. * Some drives were very specific about that exact sequence.
  1162. */
  1163. if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
  1164. ata_dev_init_params(ap, dev);
  1165. /* current CHS translation info (id[53-58]) might be
  1166. * changed. reread the identify device info.
  1167. */
  1168. ata_dev_reread_id(ap, dev);
  1169. }
  1170. if (ata_id_has_lba(dev->id)) {
  1171. dev->flags |= ATA_DFLAG_LBA;
  1172. if (ata_id_has_lba48(dev->id)) {
  1173. dev->flags |= ATA_DFLAG_LBA48;
  1174. dev->n_sectors = ata_id_u64(dev->id, 100);
  1175. } else {
  1176. dev->n_sectors = ata_id_u32(dev->id, 60);
  1177. }
  1178. /* print device info to dmesg */
  1179. printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
  1180. ap->id, device,
  1181. major_version,
  1182. ata_mode_string(xfer_modes),
  1183. (unsigned long long)dev->n_sectors,
  1184. dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
  1185. } else {
  1186. /* CHS */
  1187. /* Default translation */
  1188. dev->cylinders = dev->id[1];
  1189. dev->heads = dev->id[3];
  1190. dev->sectors = dev->id[6];
  1191. dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
  1192. if (ata_id_current_chs_valid(dev->id)) {
  1193. /* Current CHS translation is valid. */
  1194. dev->cylinders = dev->id[54];
  1195. dev->heads = dev->id[55];
  1196. dev->sectors = dev->id[56];
  1197. dev->n_sectors = ata_id_u32(dev->id, 57);
  1198. }
  1199. /* print device info to dmesg */
  1200. printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
  1201. ap->id, device,
  1202. major_version,
  1203. ata_mode_string(xfer_modes),
  1204. (unsigned long long)dev->n_sectors,
  1205. (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
  1206. }
  1207. if (dev->id[59] & 0x100) {
  1208. dev->multi_count = dev->id[59] & 0xff;
  1209. DPRINTK("ata%u: dev %u multi count %u\n",
  1210. ap->id, device, dev->multi_count);
  1211. }
  1212. ap->host->max_cmd_len = 16;
  1213. }
  1214. /* ATAPI-specific feature tests */
  1215. else if (dev->class == ATA_DEV_ATAPI) {
  1216. if (ata_id_is_ata(dev->id)) /* sanity check */
  1217. goto err_out_nosup;
  1218. rc = atapi_cdb_len(dev->id);
  1219. if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
  1220. printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
  1221. goto err_out_nosup;
  1222. }
  1223. ap->cdb_len = (unsigned int) rc;
  1224. ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
  1225. if (ata_id_cdb_intr(dev->id))
  1226. dev->flags |= ATA_DFLAG_CDB_INTR;
  1227. /* print device info to dmesg */
  1228. printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
  1229. ap->id, device,
  1230. ata_mode_string(xfer_modes));
  1231. }
  1232. DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
  1233. return;
  1234. err_out_nosup:
  1235. printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
  1236. ap->id, device);
  1237. err_out:
  1238. dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
  1239. DPRINTK("EXIT, err\n");
  1240. }
  1241. static inline u8 ata_dev_knobble(const struct ata_port *ap)
  1242. {
  1243. return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
  1244. }
  1245. /**
  1246. * ata_dev_config - Run device specific handlers and check for
  1247. * SATA->PATA bridges
  1248. * @ap: Bus
  1249. * @i: Device
  1250. *
  1251. * LOCKING:
  1252. */
  1253. void ata_dev_config(struct ata_port *ap, unsigned int i)
  1254. {
  1255. /* limit bridge transfers to udma5, 200 sectors */
  1256. if (ata_dev_knobble(ap)) {
  1257. printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
  1258. ap->id, ap->device->devno);
  1259. ap->udma_mask &= ATA_UDMA5;
  1260. ap->host->max_sectors = ATA_MAX_SECTORS;
  1261. ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
  1262. ap->device[i].flags |= ATA_DFLAG_LOCK_SECTORS;
  1263. }
  1264. if (ap->ops->dev_config)
  1265. ap->ops->dev_config(ap, &ap->device[i]);
  1266. }
  1267. /**
  1268. * ata_bus_probe - Reset and probe ATA bus
  1269. * @ap: Bus to probe
  1270. *
  1271. * Master ATA bus probing function. Initiates a hardware-dependent
  1272. * bus reset, then attempts to identify any devices found on
  1273. * the bus.
  1274. *
  1275. * LOCKING:
  1276. * PCI/etc. bus probe sem.
  1277. *
  1278. * RETURNS:
  1279. * Zero on success, non-zero on error.
  1280. */
  1281. static int ata_bus_probe(struct ata_port *ap)
  1282. {
  1283. unsigned int i, found = 0;
  1284. ap->ops->phy_reset(ap);
  1285. if (ap->flags & ATA_FLAG_PORT_DISABLED)
  1286. goto err_out;
  1287. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1288. ata_dev_identify(ap, i);
  1289. if (ata_dev_present(&ap->device[i])) {
  1290. found = 1;
  1291. ata_dev_config(ap,i);
  1292. }
  1293. }
  1294. if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
  1295. goto err_out_disable;
  1296. ata_set_mode(ap);
  1297. if (ap->flags & ATA_FLAG_PORT_DISABLED)
  1298. goto err_out_disable;
  1299. return 0;
  1300. err_out_disable:
  1301. ap->ops->port_disable(ap);
  1302. err_out:
  1303. return -1;
  1304. }
  1305. /**
  1306. * ata_port_probe - Mark port as enabled
  1307. * @ap: Port for which we indicate enablement
  1308. *
  1309. * Modify @ap data structure such that the system
  1310. * thinks that the entire port is enabled.
  1311. *
  1312. * LOCKING: host_set lock, or some other form of
  1313. * serialization.
  1314. */
  1315. void ata_port_probe(struct ata_port *ap)
  1316. {
  1317. ap->flags &= ~ATA_FLAG_PORT_DISABLED;
  1318. }
  1319. /**
  1320. * sata_print_link_status - Print SATA link status
  1321. * @ap: SATA port to printk link status about
  1322. *
  1323. * This function prints link speed and status of a SATA link.
  1324. *
  1325. * LOCKING:
  1326. * None.
  1327. */
  1328. static void sata_print_link_status(struct ata_port *ap)
  1329. {
  1330. u32 sstatus, tmp;
  1331. const char *speed;
  1332. if (!ap->ops->scr_read)
  1333. return;
  1334. sstatus = scr_read(ap, SCR_STATUS);
  1335. if (sata_dev_present(ap)) {
  1336. tmp = (sstatus >> 4) & 0xf;
  1337. if (tmp & (1 << 0))
  1338. speed = "1.5";
  1339. else if (tmp & (1 << 1))
  1340. speed = "3.0";
  1341. else
  1342. speed = "<unknown>";
  1343. printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
  1344. ap->id, speed, sstatus);
  1345. } else {
  1346. printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
  1347. ap->id, sstatus);
  1348. }
  1349. }
  1350. /**
  1351. * __sata_phy_reset - Wake/reset a low-level SATA PHY
  1352. * @ap: SATA port associated with target SATA PHY.
  1353. *
  1354. * This function issues commands to standard SATA Sxxx
  1355. * PHY registers, to wake up the phy (and device), and
  1356. * clear any reset condition.
  1357. *
  1358. * LOCKING:
  1359. * PCI/etc. bus probe sem.
  1360. *
  1361. */
  1362. void __sata_phy_reset(struct ata_port *ap)
  1363. {
  1364. u32 sstatus;
  1365. unsigned long timeout = jiffies + (HZ * 5);
  1366. if (ap->flags & ATA_FLAG_SATA_RESET) {
  1367. /* issue phy wake/reset */
  1368. scr_write_flush(ap, SCR_CONTROL, 0x301);
  1369. /* Couldn't find anything in SATA I/II specs, but
  1370. * AHCI-1.1 10.4.2 says at least 1 ms. */
  1371. mdelay(1);
  1372. }
  1373. scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
  1374. /* wait for phy to become ready, if necessary */
  1375. do {
  1376. msleep(200);
  1377. sstatus = scr_read(ap, SCR_STATUS);
  1378. if ((sstatus & 0xf) != 1)
  1379. break;
  1380. } while (time_before(jiffies, timeout));
  1381. /* print link status */
  1382. sata_print_link_status(ap);
  1383. /* TODO: phy layer with polling, timeouts, etc. */
  1384. if (sata_dev_present(ap))
  1385. ata_port_probe(ap);
  1386. else
  1387. ata_port_disable(ap);
  1388. if (ap->flags & ATA_FLAG_PORT_DISABLED)
  1389. return;
  1390. if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
  1391. ata_port_disable(ap);
  1392. return;
  1393. }
  1394. ap->cbl = ATA_CBL_SATA;
  1395. }
  1396. /**
  1397. * sata_phy_reset - Reset SATA bus.
  1398. * @ap: SATA port associated with target SATA PHY.
  1399. *
  1400. * This function resets the SATA bus, and then probes
  1401. * the bus for devices.
  1402. *
  1403. * LOCKING:
  1404. * PCI/etc. bus probe sem.
  1405. *
  1406. */
  1407. void sata_phy_reset(struct ata_port *ap)
  1408. {
  1409. __sata_phy_reset(ap);
  1410. if (ap->flags & ATA_FLAG_PORT_DISABLED)
  1411. return;
  1412. ata_bus_reset(ap);
  1413. }
  1414. /**
  1415. * ata_port_disable - Disable port.
  1416. * @ap: Port to be disabled.
  1417. *
  1418. * Modify @ap data structure such that the system
  1419. * thinks that the entire port is disabled, and should
  1420. * never attempt to probe or communicate with devices
  1421. * on this port.
  1422. *
  1423. * LOCKING: host_set lock, or some other form of
  1424. * serialization.
  1425. */
  1426. void ata_port_disable(struct ata_port *ap)
  1427. {
  1428. ap->device[0].class = ATA_DEV_NONE;
  1429. ap->device[1].class = ATA_DEV_NONE;
  1430. ap->flags |= ATA_FLAG_PORT_DISABLED;
  1431. }
  1432. /*
  1433. * This mode timing computation functionality is ported over from
  1434. * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
  1435. */
  1436. /*
  1437. * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
  1438. * These were taken from ATA/ATAPI-6 standard, rev 0a, except
  1439. * for PIO 5, which is a nonstandard extension and UDMA6, which
  1440. * is currently supported only by Maxtor drives.
  1441. */
  1442. static const struct ata_timing ata_timing[] = {
  1443. { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
  1444. { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
  1445. { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
  1446. { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
  1447. { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
  1448. { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
  1449. { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
  1450. /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
  1451. { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
  1452. { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
  1453. { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
  1454. { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
  1455. { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
  1456. { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
  1457. /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
  1458. { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
  1459. { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
  1460. { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
  1461. { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
  1462. { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
  1463. /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
  1464. { 0xFF }
  1465. };
  1466. #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
  1467. #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
  1468. static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
  1469. {
  1470. q->setup = EZ(t->setup * 1000, T);
  1471. q->act8b = EZ(t->act8b * 1000, T);
  1472. q->rec8b = EZ(t->rec8b * 1000, T);
  1473. q->cyc8b = EZ(t->cyc8b * 1000, T);
  1474. q->active = EZ(t->active * 1000, T);
  1475. q->recover = EZ(t->recover * 1000, T);
  1476. q->cycle = EZ(t->cycle * 1000, T);
  1477. q->udma = EZ(t->udma * 1000, UT);
  1478. }
  1479. void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
  1480. struct ata_timing *m, unsigned int what)
  1481. {
  1482. if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
  1483. if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
  1484. if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
  1485. if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
  1486. if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
  1487. if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
  1488. if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
  1489. if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
  1490. }
  1491. static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
  1492. {
  1493. const struct ata_timing *t;
  1494. for (t = ata_timing; t->mode != speed; t++)
  1495. if (t->mode == 0xFF)
  1496. return NULL;
  1497. return t;
  1498. }
  1499. int ata_timing_compute(struct ata_device *adev, unsigned short speed,
  1500. struct ata_timing *t, int T, int UT)
  1501. {
  1502. const struct ata_timing *s;
  1503. struct ata_timing p;
  1504. /*
  1505. * Find the mode.
  1506. */
  1507. if (!(s = ata_timing_find_mode(speed)))
  1508. return -EINVAL;
  1509. memcpy(t, s, sizeof(*s));
  1510. /*
  1511. * If the drive is an EIDE drive, it can tell us it needs extended
  1512. * PIO/MW_DMA cycle timing.
  1513. */
  1514. if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
  1515. memset(&p, 0, sizeof(p));
  1516. if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
  1517. if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
  1518. else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
  1519. } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
  1520. p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
  1521. }
  1522. ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
  1523. }
  1524. /*
  1525. * Convert the timing to bus clock counts.
  1526. */
  1527. ata_timing_quantize(t, t, T, UT);
  1528. /*
  1529. * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
  1530. * and some other commands. We have to ensure that the DMA cycle timing is
  1531. * slower/equal than the fastest PIO timing.
  1532. */
  1533. if (speed > XFER_PIO_4) {
  1534. ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
  1535. ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
  1536. }
  1537. /*
  1538. * Lenghten active & recovery time so that cycle time is correct.
  1539. */
  1540. if (t->act8b + t->rec8b < t->cyc8b) {
  1541. t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
  1542. t->rec8b = t->cyc8b - t->act8b;
  1543. }
  1544. if (t->active + t->recover < t->cycle) {
  1545. t->active += (t->cycle - (t->active + t->recover)) / 2;
  1546. t->recover = t->cycle - t->active;
  1547. }
  1548. return 0;
  1549. }
  1550. static const struct {
  1551. unsigned int shift;
  1552. u8 base;
  1553. } xfer_mode_classes[] = {
  1554. { ATA_SHIFT_UDMA, XFER_UDMA_0 },
  1555. { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
  1556. { ATA_SHIFT_PIO, XFER_PIO_0 },
  1557. };
  1558. static u8 base_from_shift(unsigned int shift)
  1559. {
  1560. int i;
  1561. for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
  1562. if (xfer_mode_classes[i].shift == shift)
  1563. return xfer_mode_classes[i].base;
  1564. return 0xff;
  1565. }
  1566. static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
  1567. {
  1568. int ofs, idx;
  1569. u8 base;
  1570. if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
  1571. return;
  1572. if (dev->xfer_shift == ATA_SHIFT_PIO)
  1573. dev->flags |= ATA_DFLAG_PIO;
  1574. ata_dev_set_xfermode(ap, dev);
  1575. base = base_from_shift(dev->xfer_shift);
  1576. ofs = dev->xfer_mode - base;
  1577. idx = ofs + dev->xfer_shift;
  1578. WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
  1579. DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
  1580. idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
  1581. printk(KERN_INFO "ata%u: dev %u configured for %s\n",
  1582. ap->id, dev->devno, xfer_mode_str[idx]);
  1583. }
  1584. static int ata_host_set_pio(struct ata_port *ap)
  1585. {
  1586. unsigned int mask;
  1587. int x, i;
  1588. u8 base, xfer_mode;
  1589. mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
  1590. x = fgb(mask);
  1591. if (x < 0) {
  1592. printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
  1593. return -1;
  1594. }
  1595. base = base_from_shift(ATA_SHIFT_PIO);
  1596. xfer_mode = base + x;
  1597. DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
  1598. (int)base, (int)xfer_mode, mask, x);
  1599. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1600. struct ata_device *dev = &ap->device[i];
  1601. if (ata_dev_present(dev)) {
  1602. dev->pio_mode = xfer_mode;
  1603. dev->xfer_mode = xfer_mode;
  1604. dev->xfer_shift = ATA_SHIFT_PIO;
  1605. if (ap->ops->set_piomode)
  1606. ap->ops->set_piomode(ap, dev);
  1607. }
  1608. }
  1609. return 0;
  1610. }
  1611. static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
  1612. unsigned int xfer_shift)
  1613. {
  1614. int i;
  1615. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1616. struct ata_device *dev = &ap->device[i];
  1617. if (ata_dev_present(dev)) {
  1618. dev->dma_mode = xfer_mode;
  1619. dev->xfer_mode = xfer_mode;
  1620. dev->xfer_shift = xfer_shift;
  1621. if (ap->ops->set_dmamode)
  1622. ap->ops->set_dmamode(ap, dev);
  1623. }
  1624. }
  1625. }
  1626. /**
  1627. * ata_set_mode - Program timings and issue SET FEATURES - XFER
  1628. * @ap: port on which timings will be programmed
  1629. *
  1630. * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
  1631. *
  1632. * LOCKING:
  1633. * PCI/etc. bus probe sem.
  1634. *
  1635. */
  1636. static void ata_set_mode(struct ata_port *ap)
  1637. {
  1638. unsigned int xfer_shift;
  1639. u8 xfer_mode;
  1640. int rc;
  1641. /* step 1: always set host PIO timings */
  1642. rc = ata_host_set_pio(ap);
  1643. if (rc)
  1644. goto err_out;
  1645. /* step 2: choose the best data xfer mode */
  1646. xfer_mode = xfer_shift = 0;
  1647. rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
  1648. if (rc)
  1649. goto err_out;
  1650. /* step 3: if that xfer mode isn't PIO, set host DMA timings */
  1651. if (xfer_shift != ATA_SHIFT_PIO)
  1652. ata_host_set_dma(ap, xfer_mode, xfer_shift);
  1653. /* step 4: update devices' xfer mode */
  1654. ata_dev_set_mode(ap, &ap->device[0]);
  1655. ata_dev_set_mode(ap, &ap->device[1]);
  1656. if (ap->flags & ATA_FLAG_PORT_DISABLED)
  1657. return;
  1658. if (ap->ops->post_set_mode)
  1659. ap->ops->post_set_mode(ap);
  1660. return;
  1661. err_out:
  1662. ata_port_disable(ap);
  1663. }
  1664. /**
  1665. * ata_busy_sleep - sleep until BSY clears, or timeout
  1666. * @ap: port containing status register to be polled
  1667. * @tmout_pat: impatience timeout
  1668. * @tmout: overall timeout
  1669. *
  1670. * Sleep until ATA Status register bit BSY clears,
  1671. * or a timeout occurs.
  1672. *
  1673. * LOCKING: None.
  1674. *
  1675. */
  1676. static unsigned int ata_busy_sleep (struct ata_port *ap,
  1677. unsigned long tmout_pat,
  1678. unsigned long tmout)
  1679. {
  1680. unsigned long timer_start, timeout;
  1681. u8 status;
  1682. status = ata_busy_wait(ap, ATA_BUSY, 300);
  1683. timer_start = jiffies;
  1684. timeout = timer_start + tmout_pat;
  1685. while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
  1686. msleep(50);
  1687. status = ata_busy_wait(ap, ATA_BUSY, 3);
  1688. }
  1689. if (status & ATA_BUSY)
  1690. printk(KERN_WARNING "ata%u is slow to respond, "
  1691. "please be patient\n", ap->id);
  1692. timeout = timer_start + tmout;
  1693. while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
  1694. msleep(50);
  1695. status = ata_chk_status(ap);
  1696. }
  1697. if (status & ATA_BUSY) {
  1698. printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
  1699. ap->id, tmout / HZ);
  1700. return 1;
  1701. }
  1702. return 0;
  1703. }
  1704. static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
  1705. {
  1706. struct ata_ioports *ioaddr = &ap->ioaddr;
  1707. unsigned int dev0 = devmask & (1 << 0);
  1708. unsigned int dev1 = devmask & (1 << 1);
  1709. unsigned long timeout;
  1710. /* if device 0 was found in ata_devchk, wait for its
  1711. * BSY bit to clear
  1712. */
  1713. if (dev0)
  1714. ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  1715. /* if device 1 was found in ata_devchk, wait for
  1716. * register access, then wait for BSY to clear
  1717. */
  1718. timeout = jiffies + ATA_TMOUT_BOOT;
  1719. while (dev1) {
  1720. u8 nsect, lbal;
  1721. ap->ops->dev_select(ap, 1);
  1722. if (ap->flags & ATA_FLAG_MMIO) {
  1723. nsect = readb((void __iomem *) ioaddr->nsect_addr);
  1724. lbal = readb((void __iomem *) ioaddr->lbal_addr);
  1725. } else {
  1726. nsect = inb(ioaddr->nsect_addr);
  1727. lbal = inb(ioaddr->lbal_addr);
  1728. }
  1729. if ((nsect == 1) && (lbal == 1))
  1730. break;
  1731. if (time_after(jiffies, timeout)) {
  1732. dev1 = 0;
  1733. break;
  1734. }
  1735. msleep(50); /* give drive a breather */
  1736. }
  1737. if (dev1)
  1738. ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  1739. /* is all this really necessary? */
  1740. ap->ops->dev_select(ap, 0);
  1741. if (dev1)
  1742. ap->ops->dev_select(ap, 1);
  1743. if (dev0)
  1744. ap->ops->dev_select(ap, 0);
  1745. }
  1746. /**
  1747. * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
  1748. * @ap: Port to reset and probe
  1749. *
  1750. * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
  1751. * probe the bus. Not often used these days.
  1752. *
  1753. * LOCKING:
  1754. * PCI/etc. bus probe sem.
  1755. * Obtains host_set lock.
  1756. *
  1757. */
  1758. static unsigned int ata_bus_edd(struct ata_port *ap)
  1759. {
  1760. struct ata_taskfile tf;
  1761. unsigned long flags;
  1762. /* set up execute-device-diag (bus reset) taskfile */
  1763. /* also, take interrupts to a known state (disabled) */
  1764. DPRINTK("execute-device-diag\n");
  1765. ata_tf_init(ap, &tf, 0);
  1766. tf.ctl |= ATA_NIEN;
  1767. tf.command = ATA_CMD_EDD;
  1768. tf.protocol = ATA_PROT_NODATA;
  1769. /* do bus reset */
  1770. spin_lock_irqsave(&ap->host_set->lock, flags);
  1771. ata_tf_to_host(ap, &tf);
  1772. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  1773. /* spec says at least 2ms. but who knows with those
  1774. * crazy ATAPI devices...
  1775. */
  1776. msleep(150);
  1777. return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  1778. }
  1779. static unsigned int ata_bus_softreset(struct ata_port *ap,
  1780. unsigned int devmask)
  1781. {
  1782. struct ata_ioports *ioaddr = &ap->ioaddr;
  1783. DPRINTK("ata%u: bus reset via SRST\n", ap->id);
  1784. /* software reset. causes dev0 to be selected */
  1785. if (ap->flags & ATA_FLAG_MMIO) {
  1786. writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
  1787. udelay(20); /* FIXME: flush */
  1788. writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
  1789. udelay(20); /* FIXME: flush */
  1790. writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
  1791. } else {
  1792. outb(ap->ctl, ioaddr->ctl_addr);
  1793. udelay(10);
  1794. outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
  1795. udelay(10);
  1796. outb(ap->ctl, ioaddr->ctl_addr);
  1797. }
  1798. /* spec mandates ">= 2ms" before checking status.
  1799. * We wait 150ms, because that was the magic delay used for
  1800. * ATAPI devices in Hale Landis's ATADRVR, for the period of time
  1801. * between when the ATA command register is written, and then
  1802. * status is checked. Because waiting for "a while" before
  1803. * checking status is fine, post SRST, we perform this magic
  1804. * delay here as well.
  1805. */
  1806. msleep(150);
  1807. ata_bus_post_reset(ap, devmask);
  1808. return 0;
  1809. }
  1810. /**
  1811. * ata_bus_reset - reset host port and associated ATA channel
  1812. * @ap: port to reset
  1813. *
  1814. * This is typically the first time we actually start issuing
  1815. * commands to the ATA channel. We wait for BSY to clear, then
  1816. * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
  1817. * result. Determine what devices, if any, are on the channel
  1818. * by looking at the device 0/1 error register. Look at the signature
  1819. * stored in each device's taskfile registers, to determine if
  1820. * the device is ATA or ATAPI.
  1821. *
  1822. * LOCKING:
  1823. * PCI/etc. bus probe sem.
  1824. * Obtains host_set lock.
  1825. *
  1826. * SIDE EFFECTS:
  1827. * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
  1828. */
  1829. void ata_bus_reset(struct ata_port *ap)
  1830. {
  1831. struct ata_ioports *ioaddr = &ap->ioaddr;
  1832. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  1833. u8 err;
  1834. unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
  1835. DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
  1836. /* determine if device 0/1 are present */
  1837. if (ap->flags & ATA_FLAG_SATA_RESET)
  1838. dev0 = 1;
  1839. else {
  1840. dev0 = ata_devchk(ap, 0);
  1841. if (slave_possible)
  1842. dev1 = ata_devchk(ap, 1);
  1843. }
  1844. if (dev0)
  1845. devmask |= (1 << 0);
  1846. if (dev1)
  1847. devmask |= (1 << 1);
  1848. /* select device 0 again */
  1849. ap->ops->dev_select(ap, 0);
  1850. /* issue bus reset */
  1851. if (ap->flags & ATA_FLAG_SRST)
  1852. rc = ata_bus_softreset(ap, devmask);
  1853. else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
  1854. /* set up device control */
  1855. if (ap->flags & ATA_FLAG_MMIO)
  1856. writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
  1857. else
  1858. outb(ap->ctl, ioaddr->ctl_addr);
  1859. rc = ata_bus_edd(ap);
  1860. }
  1861. if (rc)
  1862. goto err_out;
  1863. /*
  1864. * determine by signature whether we have ATA or ATAPI devices
  1865. */
  1866. err = ata_dev_try_classify(ap, 0);
  1867. if ((slave_possible) && (err != 0x81))
  1868. ata_dev_try_classify(ap, 1);
  1869. /* re-enable interrupts */
  1870. if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
  1871. ata_irq_on(ap);
  1872. /* is double-select really necessary? */
  1873. if (ap->device[1].class != ATA_DEV_NONE)
  1874. ap->ops->dev_select(ap, 1);
  1875. if (ap->device[0].class != ATA_DEV_NONE)
  1876. ap->ops->dev_select(ap, 0);
  1877. /* if no devices were detected, disable this port */
  1878. if ((ap->device[0].class == ATA_DEV_NONE) &&
  1879. (ap->device[1].class == ATA_DEV_NONE))
  1880. goto err_out;
  1881. if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
  1882. /* set up device control for ATA_FLAG_SATA_RESET */
  1883. if (ap->flags & ATA_FLAG_MMIO)
  1884. writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
  1885. else
  1886. outb(ap->ctl, ioaddr->ctl_addr);
  1887. }
  1888. DPRINTK("EXIT\n");
  1889. return;
  1890. err_out:
  1891. printk(KERN_ERR "ata%u: disabling port\n", ap->id);
  1892. ap->ops->port_disable(ap);
  1893. DPRINTK("EXIT\n");
  1894. }
  1895. static void ata_pr_blacklisted(const struct ata_port *ap,
  1896. const struct ata_device *dev)
  1897. {
  1898. printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
  1899. ap->id, dev->devno);
  1900. }
  1901. static const char * const ata_dma_blacklist [] = {
  1902. "WDC AC11000H",
  1903. "WDC AC22100H",
  1904. "WDC AC32500H",
  1905. "WDC AC33100H",
  1906. "WDC AC31600H",
  1907. "WDC AC32100H",
  1908. "WDC AC23200L",
  1909. "Compaq CRD-8241B",
  1910. "CRD-8400B",
  1911. "CRD-8480B",
  1912. "CRD-8482B",
  1913. "CRD-84",
  1914. "SanDisk SDP3B",
  1915. "SanDisk SDP3B-64",
  1916. "SANYO CD-ROM CRD",
  1917. "HITACHI CDR-8",
  1918. "HITACHI CDR-8335",
  1919. "HITACHI CDR-8435",
  1920. "Toshiba CD-ROM XM-6202B",
  1921. "TOSHIBA CD-ROM XM-1702BC",
  1922. "CD-532E-A",
  1923. "E-IDE CD-ROM CR-840",
  1924. "CD-ROM Drive/F5A",
  1925. "WPI CDD-820",
  1926. "SAMSUNG CD-ROM SC-148C",
  1927. "SAMSUNG CD-ROM SC",
  1928. "SanDisk SDP3B-64",
  1929. "ATAPI CD-ROM DRIVE 40X MAXIMUM",
  1930. "_NEC DV5800A",
  1931. };
  1932. static int ata_dma_blacklisted(const struct ata_device *dev)
  1933. {
  1934. unsigned char model_num[40];
  1935. char *s;
  1936. unsigned int len;
  1937. int i;
  1938. ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
  1939. sizeof(model_num));
  1940. s = &model_num[0];
  1941. len = strnlen(s, sizeof(model_num));
  1942. /* ATAPI specifies that empty space is blank-filled; remove blanks */
  1943. while ((len > 0) && (s[len - 1] == ' ')) {
  1944. len--;
  1945. s[len] = 0;
  1946. }
  1947. for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
  1948. if (!strncmp(ata_dma_blacklist[i], s, len))
  1949. return 1;
  1950. return 0;
  1951. }
  1952. static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
  1953. {
  1954. const struct ata_device *master, *slave;
  1955. unsigned int mask;
  1956. master = &ap->device[0];
  1957. slave = &ap->device[1];
  1958. assert (ata_dev_present(master) || ata_dev_present(slave));
  1959. if (shift == ATA_SHIFT_UDMA) {
  1960. mask = ap->udma_mask;
  1961. if (ata_dev_present(master)) {
  1962. mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
  1963. if (ata_dma_blacklisted(master)) {
  1964. mask = 0;
  1965. ata_pr_blacklisted(ap, master);
  1966. }
  1967. }
  1968. if (ata_dev_present(slave)) {
  1969. mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
  1970. if (ata_dma_blacklisted(slave)) {
  1971. mask = 0;
  1972. ata_pr_blacklisted(ap, slave);
  1973. }
  1974. }
  1975. }
  1976. else if (shift == ATA_SHIFT_MWDMA) {
  1977. mask = ap->mwdma_mask;
  1978. if (ata_dev_present(master)) {
  1979. mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
  1980. if (ata_dma_blacklisted(master)) {
  1981. mask = 0;
  1982. ata_pr_blacklisted(ap, master);
  1983. }
  1984. }
  1985. if (ata_dev_present(slave)) {
  1986. mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
  1987. if (ata_dma_blacklisted(slave)) {
  1988. mask = 0;
  1989. ata_pr_blacklisted(ap, slave);
  1990. }
  1991. }
  1992. }
  1993. else if (shift == ATA_SHIFT_PIO) {
  1994. mask = ap->pio_mask;
  1995. if (ata_dev_present(master)) {
  1996. /* spec doesn't return explicit support for
  1997. * PIO0-2, so we fake it
  1998. */
  1999. u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
  2000. tmp_mode <<= 3;
  2001. tmp_mode |= 0x7;
  2002. mask &= tmp_mode;
  2003. }
  2004. if (ata_dev_present(slave)) {
  2005. /* spec doesn't return explicit support for
  2006. * PIO0-2, so we fake it
  2007. */
  2008. u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
  2009. tmp_mode <<= 3;
  2010. tmp_mode |= 0x7;
  2011. mask &= tmp_mode;
  2012. }
  2013. }
  2014. else {
  2015. mask = 0xffffffff; /* shut up compiler warning */
  2016. BUG();
  2017. }
  2018. return mask;
  2019. }
  2020. /* find greatest bit */
  2021. static int fgb(u32 bitmap)
  2022. {
  2023. unsigned int i;
  2024. int x = -1;
  2025. for (i = 0; i < 32; i++)
  2026. if (bitmap & (1 << i))
  2027. x = i;
  2028. return x;
  2029. }
  2030. /**
  2031. * ata_choose_xfer_mode - attempt to find best transfer mode
  2032. * @ap: Port for which an xfer mode will be selected
  2033. * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
  2034. * @xfer_shift_out: (output) bit shift that selects this mode
  2035. *
  2036. * Based on host and device capabilities, determine the
  2037. * maximum transfer mode that is amenable to all.
  2038. *
  2039. * LOCKING:
  2040. * PCI/etc. bus probe sem.
  2041. *
  2042. * RETURNS:
  2043. * Zero on success, negative on error.
  2044. */
  2045. static int ata_choose_xfer_mode(const struct ata_port *ap,
  2046. u8 *xfer_mode_out,
  2047. unsigned int *xfer_shift_out)
  2048. {
  2049. unsigned int mask, shift;
  2050. int x, i;
  2051. for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
  2052. shift = xfer_mode_classes[i].shift;
  2053. mask = ata_get_mode_mask(ap, shift);
  2054. x = fgb(mask);
  2055. if (x >= 0) {
  2056. *xfer_mode_out = xfer_mode_classes[i].base + x;
  2057. *xfer_shift_out = shift;
  2058. return 0;
  2059. }
  2060. }
  2061. return -1;
  2062. }
  2063. /**
  2064. * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
  2065. * @ap: Port associated with device @dev
  2066. * @dev: Device to which command will be sent
  2067. *
  2068. * Issue SET FEATURES - XFER MODE command to device @dev
  2069. * on port @ap.
  2070. *
  2071. * LOCKING:
  2072. * PCI/etc. bus probe sem.
  2073. */
  2074. static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
  2075. {
  2076. struct ata_taskfile tf;
  2077. /* set up set-features taskfile */
  2078. DPRINTK("set features - xfer mode\n");
  2079. ata_tf_init(ap, &tf, dev->devno);
  2080. tf.command = ATA_CMD_SET_FEATURES;
  2081. tf.feature = SETFEATURES_XFER;
  2082. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2083. tf.protocol = ATA_PROT_NODATA;
  2084. tf.nsect = dev->xfer_mode;
  2085. if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
  2086. printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
  2087. ap->id);
  2088. ata_port_disable(ap);
  2089. }
  2090. DPRINTK("EXIT\n");
  2091. }
  2092. /**
  2093. * ata_dev_reread_id - Reread the device identify device info
  2094. * @ap: port where the device is
  2095. * @dev: device to reread the identify device info
  2096. *
  2097. * LOCKING:
  2098. */
  2099. static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
  2100. {
  2101. struct ata_taskfile tf;
  2102. ata_tf_init(ap, &tf, dev->devno);
  2103. if (dev->class == ATA_DEV_ATA) {
  2104. tf.command = ATA_CMD_ID_ATA;
  2105. DPRINTK("do ATA identify\n");
  2106. } else {
  2107. tf.command = ATA_CMD_ID_ATAPI;
  2108. DPRINTK("do ATAPI identify\n");
  2109. }
  2110. tf.flags |= ATA_TFLAG_DEVICE;
  2111. tf.protocol = ATA_PROT_PIO;
  2112. if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
  2113. dev->id, sizeof(dev->id)))
  2114. goto err_out;
  2115. swap_buf_le16(dev->id, ATA_ID_WORDS);
  2116. ata_dump_id(dev);
  2117. DPRINTK("EXIT\n");
  2118. return;
  2119. err_out:
  2120. printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
  2121. ata_port_disable(ap);
  2122. }
  2123. /**
  2124. * ata_dev_init_params - Issue INIT DEV PARAMS command
  2125. * @ap: Port associated with device @dev
  2126. * @dev: Device to which command will be sent
  2127. *
  2128. * LOCKING:
  2129. */
  2130. static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
  2131. {
  2132. struct ata_taskfile tf;
  2133. u16 sectors = dev->id[6];
  2134. u16 heads = dev->id[3];
  2135. /* Number of sectors per track 1-255. Number of heads 1-16 */
  2136. if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
  2137. return;
  2138. /* set up init dev params taskfile */
  2139. DPRINTK("init dev params \n");
  2140. ata_tf_init(ap, &tf, dev->devno);
  2141. tf.command = ATA_CMD_INIT_DEV_PARAMS;
  2142. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2143. tf.protocol = ATA_PROT_NODATA;
  2144. tf.nsect = sectors;
  2145. tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
  2146. if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
  2147. printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
  2148. ap->id);
  2149. ata_port_disable(ap);
  2150. }
  2151. DPRINTK("EXIT\n");
  2152. }
  2153. /**
  2154. * ata_sg_clean - Unmap DMA memory associated with command
  2155. * @qc: Command containing DMA memory to be released
  2156. *
  2157. * Unmap all mapped DMA memory associated with this command.
  2158. *
  2159. * LOCKING:
  2160. * spin_lock_irqsave(host_set lock)
  2161. */
  2162. static void ata_sg_clean(struct ata_queued_cmd *qc)
  2163. {
  2164. struct ata_port *ap = qc->ap;
  2165. struct scatterlist *sg = qc->__sg;
  2166. int dir = qc->dma_dir;
  2167. void *pad_buf = NULL;
  2168. assert(qc->flags & ATA_QCFLAG_DMAMAP);
  2169. assert(sg != NULL);
  2170. if (qc->flags & ATA_QCFLAG_SINGLE)
  2171. assert(qc->n_elem == 1);
  2172. VPRINTK("unmapping %u sg elements\n", qc->n_elem);
  2173. /* if we padded the buffer out to 32-bit bound, and data
  2174. * xfer direction is from-device, we must copy from the
  2175. * pad buffer back into the supplied buffer
  2176. */
  2177. if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
  2178. pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  2179. if (qc->flags & ATA_QCFLAG_SG) {
  2180. if (qc->n_elem)
  2181. dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
  2182. /* restore last sg */
  2183. sg[qc->orig_n_elem - 1].length += qc->pad_len;
  2184. if (pad_buf) {
  2185. struct scatterlist *psg = &qc->pad_sgent;
  2186. void *addr = kmap_atomic(psg->page, KM_IRQ0);
  2187. memcpy(addr + psg->offset, pad_buf, qc->pad_len);
  2188. kunmap_atomic(addr, KM_IRQ0);
  2189. }
  2190. } else {
  2191. if (sg_dma_len(&sg[0]) > 0)
  2192. dma_unmap_single(ap->host_set->dev,
  2193. sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
  2194. dir);
  2195. /* restore sg */
  2196. sg->length += qc->pad_len;
  2197. if (pad_buf)
  2198. memcpy(qc->buf_virt + sg->length - qc->pad_len,
  2199. pad_buf, qc->pad_len);
  2200. }
  2201. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  2202. qc->__sg = NULL;
  2203. }
  2204. /**
  2205. * ata_fill_sg - Fill PCI IDE PRD table
  2206. * @qc: Metadata associated with taskfile to be transferred
  2207. *
  2208. * Fill PCI IDE PRD (scatter-gather) table with segments
  2209. * associated with the current disk command.
  2210. *
  2211. * LOCKING:
  2212. * spin_lock_irqsave(host_set lock)
  2213. *
  2214. */
  2215. static void ata_fill_sg(struct ata_queued_cmd *qc)
  2216. {
  2217. struct ata_port *ap = qc->ap;
  2218. struct scatterlist *sg;
  2219. unsigned int idx;
  2220. assert(qc->__sg != NULL);
  2221. assert(qc->n_elem > 0);
  2222. idx = 0;
  2223. ata_for_each_sg(sg, qc) {
  2224. u32 addr, offset;
  2225. u32 sg_len, len;
  2226. /* determine if physical DMA addr spans 64K boundary.
  2227. * Note h/w doesn't support 64-bit, so we unconditionally
  2228. * truncate dma_addr_t to u32.
  2229. */
  2230. addr = (u32) sg_dma_address(sg);
  2231. sg_len = sg_dma_len(sg);
  2232. while (sg_len) {
  2233. offset = addr & 0xffff;
  2234. len = sg_len;
  2235. if ((offset + sg_len) > 0x10000)
  2236. len = 0x10000 - offset;
  2237. ap->prd[idx].addr = cpu_to_le32(addr);
  2238. ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
  2239. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
  2240. idx++;
  2241. sg_len -= len;
  2242. addr += len;
  2243. }
  2244. }
  2245. if (idx)
  2246. ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
  2247. }
  2248. /**
  2249. * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
  2250. * @qc: Metadata associated with taskfile to check
  2251. *
  2252. * Allow low-level driver to filter ATA PACKET commands, returning
  2253. * a status indicating whether or not it is OK to use DMA for the
  2254. * supplied PACKET command.
  2255. *
  2256. * LOCKING:
  2257. * spin_lock_irqsave(host_set lock)
  2258. *
  2259. * RETURNS: 0 when ATAPI DMA can be used
  2260. * nonzero otherwise
  2261. */
  2262. int ata_check_atapi_dma(struct ata_queued_cmd *qc)
  2263. {
  2264. struct ata_port *ap = qc->ap;
  2265. int rc = 0; /* Assume ATAPI DMA is OK by default */
  2266. if (ap->ops->check_atapi_dma)
  2267. rc = ap->ops->check_atapi_dma(qc);
  2268. return rc;
  2269. }
  2270. /**
  2271. * ata_qc_prep - Prepare taskfile for submission
  2272. * @qc: Metadata associated with taskfile to be prepared
  2273. *
  2274. * Prepare ATA taskfile for submission.
  2275. *
  2276. * LOCKING:
  2277. * spin_lock_irqsave(host_set lock)
  2278. */
  2279. void ata_qc_prep(struct ata_queued_cmd *qc)
  2280. {
  2281. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  2282. return;
  2283. ata_fill_sg(qc);
  2284. }
  2285. /**
  2286. * ata_sg_init_one - Associate command with memory buffer
  2287. * @qc: Command to be associated
  2288. * @buf: Memory buffer
  2289. * @buflen: Length of memory buffer, in bytes.
  2290. *
  2291. * Initialize the data-related elements of queued_cmd @qc
  2292. * to point to a single memory buffer, @buf of byte length @buflen.
  2293. *
  2294. * LOCKING:
  2295. * spin_lock_irqsave(host_set lock)
  2296. */
  2297. void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
  2298. {
  2299. struct scatterlist *sg;
  2300. qc->flags |= ATA_QCFLAG_SINGLE;
  2301. memset(&qc->sgent, 0, sizeof(qc->sgent));
  2302. qc->__sg = &qc->sgent;
  2303. qc->n_elem = 1;
  2304. qc->orig_n_elem = 1;
  2305. qc->buf_virt = buf;
  2306. sg = qc->__sg;
  2307. sg_init_one(sg, buf, buflen);
  2308. }
  2309. /**
  2310. * ata_sg_init - Associate command with scatter-gather table.
  2311. * @qc: Command to be associated
  2312. * @sg: Scatter-gather table.
  2313. * @n_elem: Number of elements in s/g table.
  2314. *
  2315. * Initialize the data-related elements of queued_cmd @qc
  2316. * to point to a scatter-gather table @sg, containing @n_elem
  2317. * elements.
  2318. *
  2319. * LOCKING:
  2320. * spin_lock_irqsave(host_set lock)
  2321. */
  2322. void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
  2323. unsigned int n_elem)
  2324. {
  2325. qc->flags |= ATA_QCFLAG_SG;
  2326. qc->__sg = sg;
  2327. qc->n_elem = n_elem;
  2328. qc->orig_n_elem = n_elem;
  2329. }
  2330. /**
  2331. * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
  2332. * @qc: Command with memory buffer to be mapped.
  2333. *
  2334. * DMA-map the memory buffer associated with queued_cmd @qc.
  2335. *
  2336. * LOCKING:
  2337. * spin_lock_irqsave(host_set lock)
  2338. *
  2339. * RETURNS:
  2340. * Zero on success, negative on error.
  2341. */
  2342. static int ata_sg_setup_one(struct ata_queued_cmd *qc)
  2343. {
  2344. struct ata_port *ap = qc->ap;
  2345. int dir = qc->dma_dir;
  2346. struct scatterlist *sg = qc->__sg;
  2347. dma_addr_t dma_address;
  2348. /* we must lengthen transfers to end on a 32-bit boundary */
  2349. qc->pad_len = sg->length & 3;
  2350. if (qc->pad_len) {
  2351. void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  2352. struct scatterlist *psg = &qc->pad_sgent;
  2353. assert(qc->dev->class == ATA_DEV_ATAPI);
  2354. memset(pad_buf, 0, ATA_DMA_PAD_SZ);
  2355. if (qc->tf.flags & ATA_TFLAG_WRITE)
  2356. memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
  2357. qc->pad_len);
  2358. sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
  2359. sg_dma_len(psg) = ATA_DMA_PAD_SZ;
  2360. /* trim sg */
  2361. sg->length -= qc->pad_len;
  2362. DPRINTK("padding done, sg->length=%u pad_len=%u\n",
  2363. sg->length, qc->pad_len);
  2364. }
  2365. if (!sg->length) {
  2366. sg_dma_address(sg) = 0;
  2367. goto skip_map;
  2368. }
  2369. dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
  2370. sg->length, dir);
  2371. if (dma_mapping_error(dma_address)) {
  2372. /* restore sg */
  2373. sg->length += qc->pad_len;
  2374. return -1;
  2375. }
  2376. sg_dma_address(sg) = dma_address;
  2377. skip_map:
  2378. sg_dma_len(sg) = sg->length;
  2379. DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
  2380. qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  2381. return 0;
  2382. }
  2383. /**
  2384. * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
  2385. * @qc: Command with scatter-gather table to be mapped.
  2386. *
  2387. * DMA-map the scatter-gather table associated with queued_cmd @qc.
  2388. *
  2389. * LOCKING:
  2390. * spin_lock_irqsave(host_set lock)
  2391. *
  2392. * RETURNS:
  2393. * Zero on success, negative on error.
  2394. *
  2395. */
  2396. static int ata_sg_setup(struct ata_queued_cmd *qc)
  2397. {
  2398. struct ata_port *ap = qc->ap;
  2399. struct scatterlist *sg = qc->__sg;
  2400. struct scatterlist *lsg = &sg[qc->n_elem - 1];
  2401. int n_elem, pre_n_elem, dir, trim_sg = 0;
  2402. VPRINTK("ENTER, ata%u\n", ap->id);
  2403. assert(qc->flags & ATA_QCFLAG_SG);
  2404. /* we must lengthen transfers to end on a 32-bit boundary */
  2405. qc->pad_len = lsg->length & 3;
  2406. if (qc->pad_len) {
  2407. void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  2408. struct scatterlist *psg = &qc->pad_sgent;
  2409. unsigned int offset;
  2410. assert(qc->dev->class == ATA_DEV_ATAPI);
  2411. memset(pad_buf, 0, ATA_DMA_PAD_SZ);
  2412. /*
  2413. * psg->page/offset are used to copy to-be-written
  2414. * data in this function or read data in ata_sg_clean.
  2415. */
  2416. offset = lsg->offset + lsg->length - qc->pad_len;
  2417. psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
  2418. psg->offset = offset_in_page(offset);
  2419. if (qc->tf.flags & ATA_TFLAG_WRITE) {
  2420. void *addr = kmap_atomic(psg->page, KM_IRQ0);
  2421. memcpy(pad_buf, addr + psg->offset, qc->pad_len);
  2422. kunmap_atomic(addr, KM_IRQ0);
  2423. }
  2424. sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
  2425. sg_dma_len(psg) = ATA_DMA_PAD_SZ;
  2426. /* trim last sg */
  2427. lsg->length -= qc->pad_len;
  2428. if (lsg->length == 0)
  2429. trim_sg = 1;
  2430. DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
  2431. qc->n_elem - 1, lsg->length, qc->pad_len);
  2432. }
  2433. pre_n_elem = qc->n_elem;
  2434. if (trim_sg && pre_n_elem)
  2435. pre_n_elem--;
  2436. if (!pre_n_elem) {
  2437. n_elem = 0;
  2438. goto skip_map;
  2439. }
  2440. dir = qc->dma_dir;
  2441. n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
  2442. if (n_elem < 1) {
  2443. /* restore last sg */
  2444. lsg->length += qc->pad_len;
  2445. return -1;
  2446. }
  2447. DPRINTK("%d sg elements mapped\n", n_elem);
  2448. skip_map:
  2449. qc->n_elem = n_elem;
  2450. return 0;
  2451. }
  2452. /**
  2453. * ata_poll_qc_complete - turn irq back on and finish qc
  2454. * @qc: Command to complete
  2455. * @err_mask: ATA status register content
  2456. *
  2457. * LOCKING:
  2458. * None. (grabs host lock)
  2459. */
  2460. void ata_poll_qc_complete(struct ata_queued_cmd *qc)
  2461. {
  2462. struct ata_port *ap = qc->ap;
  2463. unsigned long flags;
  2464. spin_lock_irqsave(&ap->host_set->lock, flags);
  2465. ata_irq_on(ap);
  2466. ata_qc_complete(qc);
  2467. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  2468. }
  2469. /**
  2470. * ata_pio_poll -
  2471. * @ap: the target ata_port
  2472. *
  2473. * LOCKING:
  2474. * None. (executing in kernel thread context)
  2475. *
  2476. * RETURNS:
  2477. * timeout value to use
  2478. */
  2479. static unsigned long ata_pio_poll(struct ata_port *ap)
  2480. {
  2481. struct ata_queued_cmd *qc;
  2482. u8 status;
  2483. unsigned int poll_state = HSM_ST_UNKNOWN;
  2484. unsigned int reg_state = HSM_ST_UNKNOWN;
  2485. qc = ata_qc_from_tag(ap, ap->active_tag);
  2486. assert(qc != NULL);
  2487. switch (ap->hsm_task_state) {
  2488. case HSM_ST:
  2489. case HSM_ST_POLL:
  2490. poll_state = HSM_ST_POLL;
  2491. reg_state = HSM_ST;
  2492. break;
  2493. case HSM_ST_LAST:
  2494. case HSM_ST_LAST_POLL:
  2495. poll_state = HSM_ST_LAST_POLL;
  2496. reg_state = HSM_ST_LAST;
  2497. break;
  2498. default:
  2499. BUG();
  2500. break;
  2501. }
  2502. status = ata_chk_status(ap);
  2503. if (status & ATA_BUSY) {
  2504. if (time_after(jiffies, ap->pio_task_timeout)) {
  2505. qc->err_mask |= AC_ERR_ATA_BUS;
  2506. ap->hsm_task_state = HSM_ST_TMOUT;
  2507. return 0;
  2508. }
  2509. ap->hsm_task_state = poll_state;
  2510. return ATA_SHORT_PAUSE;
  2511. }
  2512. ap->hsm_task_state = reg_state;
  2513. return 0;
  2514. }
  2515. /**
  2516. * ata_pio_complete - check if drive is busy or idle
  2517. * @ap: the target ata_port
  2518. *
  2519. * LOCKING:
  2520. * None. (executing in kernel thread context)
  2521. *
  2522. * RETURNS:
  2523. * Zero if qc completed.
  2524. * Non-zero if has next.
  2525. */
  2526. static int ata_pio_complete (struct ata_port *ap)
  2527. {
  2528. struct ata_queued_cmd *qc;
  2529. u8 drv_stat;
  2530. /*
  2531. * This is purely heuristic. This is a fast path. Sometimes when
  2532. * we enter, BSY will be cleared in a chk-status or two. If not,
  2533. * the drive is probably seeking or something. Snooze for a couple
  2534. * msecs, then chk-status again. If still busy, fall back to
  2535. * HSM_ST_LAST_POLL state.
  2536. */
  2537. drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
  2538. if (drv_stat & ATA_BUSY) {
  2539. msleep(2);
  2540. drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
  2541. if (drv_stat & ATA_BUSY) {
  2542. ap->hsm_task_state = HSM_ST_LAST_POLL;
  2543. ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
  2544. return 1;
  2545. }
  2546. }
  2547. qc = ata_qc_from_tag(ap, ap->active_tag);
  2548. assert(qc != NULL);
  2549. drv_stat = ata_wait_idle(ap);
  2550. if (!ata_ok(drv_stat)) {
  2551. qc->err_mask |= __ac_err_mask(drv_stat);
  2552. ap->hsm_task_state = HSM_ST_ERR;
  2553. return 1;
  2554. }
  2555. ap->hsm_task_state = HSM_ST_IDLE;
  2556. assert(qc->err_mask == 0);
  2557. ata_poll_qc_complete(qc);
  2558. /* another command may start at this point */
  2559. return 0;
  2560. }
  2561. /**
  2562. * swap_buf_le16 - swap halves of 16-words in place
  2563. * @buf: Buffer to swap
  2564. * @buf_words: Number of 16-bit words in buffer.
  2565. *
  2566. * Swap halves of 16-bit words if needed to convert from
  2567. * little-endian byte order to native cpu byte order, or
  2568. * vice-versa.
  2569. *
  2570. * LOCKING:
  2571. * Inherited from caller.
  2572. */
  2573. void swap_buf_le16(u16 *buf, unsigned int buf_words)
  2574. {
  2575. #ifdef __BIG_ENDIAN
  2576. unsigned int i;
  2577. for (i = 0; i < buf_words; i++)
  2578. buf[i] = le16_to_cpu(buf[i]);
  2579. #endif /* __BIG_ENDIAN */
  2580. }
  2581. /**
  2582. * ata_mmio_data_xfer - Transfer data by MMIO
  2583. * @ap: port to read/write
  2584. * @buf: data buffer
  2585. * @buflen: buffer length
  2586. * @write_data: read/write
  2587. *
  2588. * Transfer data from/to the device data register by MMIO.
  2589. *
  2590. * LOCKING:
  2591. * Inherited from caller.
  2592. */
  2593. static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
  2594. unsigned int buflen, int write_data)
  2595. {
  2596. unsigned int i;
  2597. unsigned int words = buflen >> 1;
  2598. u16 *buf16 = (u16 *) buf;
  2599. void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
  2600. /* Transfer multiple of 2 bytes */
  2601. if (write_data) {
  2602. for (i = 0; i < words; i++)
  2603. writew(le16_to_cpu(buf16[i]), mmio);
  2604. } else {
  2605. for (i = 0; i < words; i++)
  2606. buf16[i] = cpu_to_le16(readw(mmio));
  2607. }
  2608. /* Transfer trailing 1 byte, if any. */
  2609. if (unlikely(buflen & 0x01)) {
  2610. u16 align_buf[1] = { 0 };
  2611. unsigned char *trailing_buf = buf + buflen - 1;
  2612. if (write_data) {
  2613. memcpy(align_buf, trailing_buf, 1);
  2614. writew(le16_to_cpu(align_buf[0]), mmio);
  2615. } else {
  2616. align_buf[0] = cpu_to_le16(readw(mmio));
  2617. memcpy(trailing_buf, align_buf, 1);
  2618. }
  2619. }
  2620. }
  2621. /**
  2622. * ata_pio_data_xfer - Transfer data by PIO
  2623. * @ap: port to read/write
  2624. * @buf: data buffer
  2625. * @buflen: buffer length
  2626. * @write_data: read/write
  2627. *
  2628. * Transfer data from/to the device data register by PIO.
  2629. *
  2630. * LOCKING:
  2631. * Inherited from caller.
  2632. */
  2633. static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
  2634. unsigned int buflen, int write_data)
  2635. {
  2636. unsigned int words = buflen >> 1;
  2637. /* Transfer multiple of 2 bytes */
  2638. if (write_data)
  2639. outsw(ap->ioaddr.data_addr, buf, words);
  2640. else
  2641. insw(ap->ioaddr.data_addr, buf, words);
  2642. /* Transfer trailing 1 byte, if any. */
  2643. if (unlikely(buflen & 0x01)) {
  2644. u16 align_buf[1] = { 0 };
  2645. unsigned char *trailing_buf = buf + buflen - 1;
  2646. if (write_data) {
  2647. memcpy(align_buf, trailing_buf, 1);
  2648. outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
  2649. } else {
  2650. align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
  2651. memcpy(trailing_buf, align_buf, 1);
  2652. }
  2653. }
  2654. }
  2655. /**
  2656. * ata_data_xfer - Transfer data from/to the data register.
  2657. * @ap: port to read/write
  2658. * @buf: data buffer
  2659. * @buflen: buffer length
  2660. * @do_write: read/write
  2661. *
  2662. * Transfer data from/to the device data register.
  2663. *
  2664. * LOCKING:
  2665. * Inherited from caller.
  2666. */
  2667. static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
  2668. unsigned int buflen, int do_write)
  2669. {
  2670. /* Make the crap hardware pay the costs not the good stuff */
  2671. if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
  2672. unsigned long flags;
  2673. local_irq_save(flags);
  2674. if (ap->flags & ATA_FLAG_MMIO)
  2675. ata_mmio_data_xfer(ap, buf, buflen, do_write);
  2676. else
  2677. ata_pio_data_xfer(ap, buf, buflen, do_write);
  2678. local_irq_restore(flags);
  2679. } else {
  2680. if (ap->flags & ATA_FLAG_MMIO)
  2681. ata_mmio_data_xfer(ap, buf, buflen, do_write);
  2682. else
  2683. ata_pio_data_xfer(ap, buf, buflen, do_write);
  2684. }
  2685. }
  2686. /**
  2687. * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
  2688. * @qc: Command on going
  2689. *
  2690. * Transfer ATA_SECT_SIZE of data from/to the ATA device.
  2691. *
  2692. * LOCKING:
  2693. * Inherited from caller.
  2694. */
  2695. static void ata_pio_sector(struct ata_queued_cmd *qc)
  2696. {
  2697. int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  2698. struct scatterlist *sg = qc->__sg;
  2699. struct ata_port *ap = qc->ap;
  2700. struct page *page;
  2701. unsigned int offset;
  2702. unsigned char *buf;
  2703. if (qc->cursect == (qc->nsect - 1))
  2704. ap->hsm_task_state = HSM_ST_LAST;
  2705. page = sg[qc->cursg].page;
  2706. offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
  2707. /* get the current page and offset */
  2708. page = nth_page(page, (offset >> PAGE_SHIFT));
  2709. offset %= PAGE_SIZE;
  2710. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  2711. if (PageHighMem(page)) {
  2712. unsigned long flags;
  2713. local_irq_save(flags);
  2714. buf = kmap_atomic(page, KM_IRQ0);
  2715. /* do the actual data transfer */
  2716. ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
  2717. kunmap_atomic(buf, KM_IRQ0);
  2718. local_irq_restore(flags);
  2719. } else {
  2720. buf = page_address(page);
  2721. ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
  2722. }
  2723. qc->cursect++;
  2724. qc->cursg_ofs++;
  2725. if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
  2726. qc->cursg++;
  2727. qc->cursg_ofs = 0;
  2728. }
  2729. }
  2730. /**
  2731. * ata_pio_sectors - Transfer one or many 512-byte sectors.
  2732. * @qc: Command on going
  2733. *
  2734. * Transfer one or many ATA_SECT_SIZE of data from/to the
  2735. * ATA device for the DRQ request.
  2736. *
  2737. * LOCKING:
  2738. * Inherited from caller.
  2739. */
  2740. static void ata_pio_sectors(struct ata_queued_cmd *qc)
  2741. {
  2742. if (is_multi_taskfile(&qc->tf)) {
  2743. /* READ/WRITE MULTIPLE */
  2744. unsigned int nsect;
  2745. assert(qc->dev->multi_count);
  2746. nsect = min(qc->nsect - qc->cursect, qc->dev->multi_count);
  2747. while (nsect--)
  2748. ata_pio_sector(qc);
  2749. } else
  2750. ata_pio_sector(qc);
  2751. }
  2752. /**
  2753. * atapi_send_cdb - Write CDB bytes to hardware
  2754. * @ap: Port to which ATAPI device is attached.
  2755. * @qc: Taskfile currently active
  2756. *
  2757. * When device has indicated its readiness to accept
  2758. * a CDB, this function is called. Send the CDB.
  2759. *
  2760. * LOCKING:
  2761. * caller.
  2762. */
  2763. static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
  2764. {
  2765. /* send SCSI cdb */
  2766. DPRINTK("send cdb\n");
  2767. assert(ap->cdb_len >= 12);
  2768. ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
  2769. ata_altstatus(ap); /* flush */
  2770. switch (qc->tf.protocol) {
  2771. case ATA_PROT_ATAPI:
  2772. ap->hsm_task_state = HSM_ST;
  2773. break;
  2774. case ATA_PROT_ATAPI_NODATA:
  2775. ap->hsm_task_state = HSM_ST_LAST;
  2776. break;
  2777. case ATA_PROT_ATAPI_DMA:
  2778. ap->hsm_task_state = HSM_ST_LAST;
  2779. /* initiate bmdma */
  2780. ap->ops->bmdma_start(qc);
  2781. break;
  2782. }
  2783. }
  2784. /**
  2785. * ata_pio_first_block - Write first data block to hardware
  2786. * @ap: Port to which ATA/ATAPI device is attached.
  2787. *
  2788. * When device has indicated its readiness to accept
  2789. * the data, this function sends out the CDB or
  2790. * the first data block by PIO.
  2791. * After this,
  2792. * - If polling, ata_pio_task() handles the rest.
  2793. * - Otherwise, interrupt handler takes over.
  2794. *
  2795. * LOCKING:
  2796. * Kernel thread context (may sleep)
  2797. *
  2798. * RETURNS:
  2799. * Zero if irq handler takes over
  2800. * Non-zero if has next (polling).
  2801. */
  2802. static int ata_pio_first_block(struct ata_port *ap)
  2803. {
  2804. struct ata_queued_cmd *qc;
  2805. u8 status;
  2806. unsigned long flags;
  2807. int has_next;
  2808. qc = ata_qc_from_tag(ap, ap->active_tag);
  2809. assert(qc != NULL);
  2810. assert(qc->flags & ATA_QCFLAG_ACTIVE);
  2811. /* if polling, we will stay in the work queue after sending the data.
  2812. * otherwise, interrupt handler takes over after sending the data.
  2813. */
  2814. has_next = (qc->tf.flags & ATA_TFLAG_POLLING);
  2815. /* sleep-wait for BSY to clear */
  2816. DPRINTK("busy wait\n");
  2817. if (ata_busy_sleep(ap, ATA_TMOUT_DATAOUT_QUICK, ATA_TMOUT_DATAOUT)) {
  2818. qc->err_mask |= AC_ERR_ATA_BUS;
  2819. ap->hsm_task_state = HSM_ST_TMOUT;
  2820. goto err_out;
  2821. }
  2822. /* make sure DRQ is set */
  2823. status = ata_chk_status(ap);
  2824. if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
  2825. /* device status error */
  2826. qc->err_mask |= AC_ERR_ATA_BUS;
  2827. ap->hsm_task_state = HSM_ST_ERR;
  2828. goto err_out;
  2829. }
  2830. /* Send the CDB (atapi) or the first data block (ata pio out).
  2831. * During the state transition, interrupt handler shouldn't
  2832. * be invoked before the data transfer is complete and
  2833. * hsm_task_state is changed. Hence, the following locking.
  2834. */
  2835. spin_lock_irqsave(&ap->host_set->lock, flags);
  2836. if (qc->tf.protocol == ATA_PROT_PIO) {
  2837. /* PIO data out protocol.
  2838. * send first data block.
  2839. */
  2840. /* ata_pio_sectors() might change the state to HSM_ST_LAST.
  2841. * so, the state is changed here before ata_pio_sectors().
  2842. */
  2843. ap->hsm_task_state = HSM_ST;
  2844. ata_pio_sectors(qc);
  2845. ata_altstatus(ap); /* flush */
  2846. } else
  2847. /* send CDB */
  2848. atapi_send_cdb(ap, qc);
  2849. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  2850. /* if polling, ata_pio_task() handles the rest.
  2851. * otherwise, interrupt handler takes over from here.
  2852. */
  2853. return has_next;
  2854. err_out:
  2855. return 1; /* has next */
  2856. }
  2857. /**
  2858. * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
  2859. * @qc: Command on going
  2860. * @bytes: number of bytes
  2861. *
  2862. * Transfer Transfer data from/to the ATAPI device.
  2863. *
  2864. * LOCKING:
  2865. * Inherited from caller.
  2866. *
  2867. */
  2868. static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
  2869. {
  2870. int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  2871. struct scatterlist *sg = qc->__sg;
  2872. struct ata_port *ap = qc->ap;
  2873. struct page *page;
  2874. unsigned char *buf;
  2875. unsigned int offset, count;
  2876. if (qc->curbytes + bytes >= qc->nbytes)
  2877. ap->hsm_task_state = HSM_ST_LAST;
  2878. next_sg:
  2879. if (unlikely(qc->cursg >= qc->n_elem)) {
  2880. /*
  2881. * The end of qc->sg is reached and the device expects
  2882. * more data to transfer. In order not to overrun qc->sg
  2883. * and fulfill length specified in the byte count register,
  2884. * - for read case, discard trailing data from the device
  2885. * - for write case, padding zero data to the device
  2886. */
  2887. u16 pad_buf[1] = { 0 };
  2888. unsigned int words = bytes >> 1;
  2889. unsigned int i;
  2890. if (words) /* warning if bytes > 1 */
  2891. printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
  2892. ap->id, bytes);
  2893. for (i = 0; i < words; i++)
  2894. ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
  2895. ap->hsm_task_state = HSM_ST_LAST;
  2896. return;
  2897. }
  2898. sg = &qc->__sg[qc->cursg];
  2899. page = sg->page;
  2900. offset = sg->offset + qc->cursg_ofs;
  2901. /* get the current page and offset */
  2902. page = nth_page(page, (offset >> PAGE_SHIFT));
  2903. offset %= PAGE_SIZE;
  2904. /* don't overrun current sg */
  2905. count = min(sg->length - qc->cursg_ofs, bytes);
  2906. /* don't cross page boundaries */
  2907. count = min(count, (unsigned int)PAGE_SIZE - offset);
  2908. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  2909. if (PageHighMem(page)) {
  2910. unsigned long flags;
  2911. local_irq_save(flags);
  2912. buf = kmap_atomic(page, KM_IRQ0);
  2913. /* do the actual data transfer */
  2914. ata_data_xfer(ap, buf + offset, count, do_write);
  2915. kunmap_atomic(buf, KM_IRQ0);
  2916. local_irq_restore(flags);
  2917. } else {
  2918. buf = page_address(page);
  2919. ata_data_xfer(ap, buf + offset, count, do_write);
  2920. }
  2921. bytes -= count;
  2922. qc->curbytes += count;
  2923. qc->cursg_ofs += count;
  2924. if (qc->cursg_ofs == sg->length) {
  2925. qc->cursg++;
  2926. qc->cursg_ofs = 0;
  2927. }
  2928. if (bytes)
  2929. goto next_sg;
  2930. }
  2931. /**
  2932. * atapi_pio_bytes - Transfer data from/to the ATAPI device.
  2933. * @qc: Command on going
  2934. *
  2935. * Transfer Transfer data from/to the ATAPI device.
  2936. *
  2937. * LOCKING:
  2938. * Inherited from caller.
  2939. */
  2940. static void atapi_pio_bytes(struct ata_queued_cmd *qc)
  2941. {
  2942. struct ata_port *ap = qc->ap;
  2943. struct ata_device *dev = qc->dev;
  2944. unsigned int ireason, bc_lo, bc_hi, bytes;
  2945. int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
  2946. ap->ops->tf_read(ap, &qc->tf);
  2947. ireason = qc->tf.nsect;
  2948. bc_lo = qc->tf.lbam;
  2949. bc_hi = qc->tf.lbah;
  2950. bytes = (bc_hi << 8) | bc_lo;
  2951. /* shall be cleared to zero, indicating xfer of data */
  2952. if (ireason & (1 << 0))
  2953. goto err_out;
  2954. /* make sure transfer direction matches expected */
  2955. i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
  2956. if (do_write != i_write)
  2957. goto err_out;
  2958. VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
  2959. __atapi_pio_bytes(qc, bytes);
  2960. return;
  2961. err_out:
  2962. printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
  2963. ap->id, dev->devno);
  2964. qc->err_mask |= AC_ERR_ATA_BUS;
  2965. ap->hsm_task_state = HSM_ST_ERR;
  2966. }
  2967. /**
  2968. * ata_pio_block - start PIO on a block
  2969. * @ap: the target ata_port
  2970. *
  2971. * LOCKING:
  2972. * None. (executing in kernel thread context)
  2973. */
  2974. static void ata_pio_block(struct ata_port *ap)
  2975. {
  2976. struct ata_queued_cmd *qc;
  2977. u8 status;
  2978. /*
  2979. * This is purely heuristic. This is a fast path.
  2980. * Sometimes when we enter, BSY will be cleared in
  2981. * a chk-status or two. If not, the drive is probably seeking
  2982. * or something. Snooze for a couple msecs, then
  2983. * chk-status again. If still busy, fall back to
  2984. * HSM_ST_POLL state.
  2985. */
  2986. status = ata_busy_wait(ap, ATA_BUSY, 5);
  2987. if (status & ATA_BUSY) {
  2988. msleep(2);
  2989. status = ata_busy_wait(ap, ATA_BUSY, 10);
  2990. if (status & ATA_BUSY) {
  2991. ap->hsm_task_state = HSM_ST_POLL;
  2992. ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
  2993. return;
  2994. }
  2995. }
  2996. qc = ata_qc_from_tag(ap, ap->active_tag);
  2997. assert(qc != NULL);
  2998. /* check error */
  2999. if (status & (ATA_ERR | ATA_DF)) {
  3000. qc->err_mask |= AC_ERR_DEV;
  3001. ap->hsm_task_state = HSM_ST_ERR;
  3002. return;
  3003. }
  3004. /* transfer data if any */
  3005. if (is_atapi_taskfile(&qc->tf)) {
  3006. /* DRQ=0 means no more data to transfer */
  3007. if ((status & ATA_DRQ) == 0) {
  3008. ap->hsm_task_state = HSM_ST_LAST;
  3009. return;
  3010. }
  3011. atapi_pio_bytes(qc);
  3012. } else {
  3013. /* handle BSY=0, DRQ=0 as error */
  3014. if ((status & ATA_DRQ) == 0) {
  3015. qc->err_mask |= AC_ERR_ATA_BUS;
  3016. ap->hsm_task_state = HSM_ST_ERR;
  3017. return;
  3018. }
  3019. ata_pio_sectors(qc);
  3020. }
  3021. ata_altstatus(ap); /* flush */
  3022. }
  3023. static void ata_pio_error(struct ata_port *ap)
  3024. {
  3025. struct ata_queued_cmd *qc;
  3026. qc = ata_qc_from_tag(ap, ap->active_tag);
  3027. assert(qc != NULL);
  3028. if (qc->tf.command != ATA_CMD_PACKET)
  3029. printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
  3030. /* make sure qc->err_mask is available to
  3031. * know what's wrong and recover
  3032. */
  3033. assert(qc->err_mask);
  3034. ap->hsm_task_state = HSM_ST_IDLE;
  3035. ata_poll_qc_complete(qc);
  3036. }
  3037. static void ata_pio_task(void *_data)
  3038. {
  3039. struct ata_port *ap = _data;
  3040. unsigned long timeout;
  3041. int has_next;
  3042. fsm_start:
  3043. timeout = 0;
  3044. has_next = 1;
  3045. switch (ap->hsm_task_state) {
  3046. case HSM_ST_FIRST:
  3047. has_next = ata_pio_first_block(ap);
  3048. break;
  3049. case HSM_ST:
  3050. ata_pio_block(ap);
  3051. break;
  3052. case HSM_ST_LAST:
  3053. has_next = ata_pio_complete(ap);
  3054. break;
  3055. case HSM_ST_POLL:
  3056. case HSM_ST_LAST_POLL:
  3057. timeout = ata_pio_poll(ap);
  3058. break;
  3059. case HSM_ST_TMOUT:
  3060. case HSM_ST_ERR:
  3061. ata_pio_error(ap);
  3062. return;
  3063. default:
  3064. BUG();
  3065. return;
  3066. }
  3067. if (timeout)
  3068. queue_delayed_work(ata_wq, &ap->pio_task, timeout);
  3069. else if (has_next)
  3070. goto fsm_start;
  3071. }
  3072. /**
  3073. * ata_qc_timeout - Handle timeout of queued command
  3074. * @qc: Command that timed out
  3075. *
  3076. * Some part of the kernel (currently, only the SCSI layer)
  3077. * has noticed that the active command on port @ap has not
  3078. * completed after a specified length of time. Handle this
  3079. * condition by disabling DMA (if necessary) and completing
  3080. * transactions, with error if necessary.
  3081. *
  3082. * This also handles the case of the "lost interrupt", where
  3083. * for some reason (possibly hardware bug, possibly driver bug)
  3084. * an interrupt was not delivered to the driver, even though the
  3085. * transaction completed successfully.
  3086. *
  3087. * LOCKING:
  3088. * Inherited from SCSI layer (none, can sleep)
  3089. */
  3090. static void ata_qc_timeout(struct ata_queued_cmd *qc)
  3091. {
  3092. struct ata_port *ap = qc->ap;
  3093. struct ata_host_set *host_set = ap->host_set;
  3094. u8 host_stat = 0, drv_stat;
  3095. unsigned long flags;
  3096. DPRINTK("ENTER\n");
  3097. spin_lock_irqsave(&host_set->lock, flags);
  3098. /* hack alert! We cannot use the supplied completion
  3099. * function from inside the ->eh_strategy_handler() thread.
  3100. * libata is the only user of ->eh_strategy_handler() in
  3101. * any kernel, so the default scsi_done() assumes it is
  3102. * not being called from the SCSI EH.
  3103. */
  3104. qc->scsidone = scsi_finish_command;
  3105. switch (qc->tf.protocol) {
  3106. case ATA_PROT_DMA:
  3107. case ATA_PROT_ATAPI_DMA:
  3108. host_stat = ap->ops->bmdma_status(ap);
  3109. /* before we do anything else, clear DMA-Start bit */
  3110. ap->ops->bmdma_stop(qc);
  3111. /* fall through */
  3112. default:
  3113. ata_altstatus(ap);
  3114. drv_stat = ata_chk_status(ap);
  3115. /* ack bmdma irq events */
  3116. ap->ops->irq_clear(ap);
  3117. printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
  3118. ap->id, qc->tf.command, drv_stat, host_stat);
  3119. ap->hsm_task_state = HSM_ST_IDLE;
  3120. /* complete taskfile transaction */
  3121. qc->err_mask |= ac_err_mask(drv_stat);
  3122. ata_qc_complete(qc);
  3123. break;
  3124. }
  3125. spin_unlock_irqrestore(&host_set->lock, flags);
  3126. DPRINTK("EXIT\n");
  3127. }
  3128. /**
  3129. * ata_eng_timeout - Handle timeout of queued command
  3130. * @ap: Port on which timed-out command is active
  3131. *
  3132. * Some part of the kernel (currently, only the SCSI layer)
  3133. * has noticed that the active command on port @ap has not
  3134. * completed after a specified length of time. Handle this
  3135. * condition by disabling DMA (if necessary) and completing
  3136. * transactions, with error if necessary.
  3137. *
  3138. * This also handles the case of the "lost interrupt", where
  3139. * for some reason (possibly hardware bug, possibly driver bug)
  3140. * an interrupt was not delivered to the driver, even though the
  3141. * transaction completed successfully.
  3142. *
  3143. * LOCKING:
  3144. * Inherited from SCSI layer (none, can sleep)
  3145. */
  3146. void ata_eng_timeout(struct ata_port *ap)
  3147. {
  3148. struct ata_queued_cmd *qc;
  3149. DPRINTK("ENTER\n");
  3150. qc = ata_qc_from_tag(ap, ap->active_tag);
  3151. if (qc)
  3152. ata_qc_timeout(qc);
  3153. else {
  3154. printk(KERN_ERR "ata%u: BUG: timeout without command\n",
  3155. ap->id);
  3156. goto out;
  3157. }
  3158. out:
  3159. DPRINTK("EXIT\n");
  3160. }
  3161. /**
  3162. * ata_qc_new - Request an available ATA command, for queueing
  3163. * @ap: Port associated with device @dev
  3164. * @dev: Device from whom we request an available command structure
  3165. *
  3166. * LOCKING:
  3167. * None.
  3168. */
  3169. static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
  3170. {
  3171. struct ata_queued_cmd *qc = NULL;
  3172. unsigned int i;
  3173. for (i = 0; i < ATA_MAX_QUEUE; i++)
  3174. if (!test_and_set_bit(i, &ap->qactive)) {
  3175. qc = ata_qc_from_tag(ap, i);
  3176. break;
  3177. }
  3178. if (qc)
  3179. qc->tag = i;
  3180. return qc;
  3181. }
  3182. /**
  3183. * ata_qc_new_init - Request an available ATA command, and initialize it
  3184. * @ap: Port associated with device @dev
  3185. * @dev: Device from whom we request an available command structure
  3186. *
  3187. * LOCKING:
  3188. * None.
  3189. */
  3190. struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
  3191. struct ata_device *dev)
  3192. {
  3193. struct ata_queued_cmd *qc;
  3194. qc = ata_qc_new(ap);
  3195. if (qc) {
  3196. qc->scsicmd = NULL;
  3197. qc->ap = ap;
  3198. qc->dev = dev;
  3199. ata_qc_reinit(qc);
  3200. }
  3201. return qc;
  3202. }
  3203. /**
  3204. * ata_qc_free - free unused ata_queued_cmd
  3205. * @qc: Command to complete
  3206. *
  3207. * Designed to free unused ata_queued_cmd object
  3208. * in case something prevents using it.
  3209. *
  3210. * LOCKING:
  3211. * spin_lock_irqsave(host_set lock)
  3212. */
  3213. void ata_qc_free(struct ata_queued_cmd *qc)
  3214. {
  3215. struct ata_port *ap = qc->ap;
  3216. unsigned int tag;
  3217. assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
  3218. qc->flags = 0;
  3219. tag = qc->tag;
  3220. if (likely(ata_tag_valid(tag))) {
  3221. if (tag == ap->active_tag)
  3222. ap->active_tag = ATA_TAG_POISON;
  3223. qc->tag = ATA_TAG_POISON;
  3224. clear_bit(tag, &ap->qactive);
  3225. }
  3226. }
  3227. /**
  3228. * ata_qc_complete - Complete an active ATA command
  3229. * @qc: Command to complete
  3230. * @err_mask: ATA Status register contents
  3231. *
  3232. * Indicate to the mid and upper layers that an ATA
  3233. * command has completed, with either an ok or not-ok status.
  3234. *
  3235. * LOCKING:
  3236. * spin_lock_irqsave(host_set lock)
  3237. */
  3238. void ata_qc_complete(struct ata_queued_cmd *qc)
  3239. {
  3240. assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
  3241. assert(qc->flags & ATA_QCFLAG_ACTIVE);
  3242. if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
  3243. ata_sg_clean(qc);
  3244. /* atapi: mark qc as inactive to prevent the interrupt handler
  3245. * from completing the command twice later, before the error handler
  3246. * is called. (when rc != 0 and atapi request sense is needed)
  3247. */
  3248. qc->flags &= ~ATA_QCFLAG_ACTIVE;
  3249. /* call completion callback */
  3250. qc->complete_fn(qc);
  3251. }
  3252. static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
  3253. {
  3254. struct ata_port *ap = qc->ap;
  3255. switch (qc->tf.protocol) {
  3256. case ATA_PROT_DMA:
  3257. case ATA_PROT_ATAPI_DMA:
  3258. return 1;
  3259. case ATA_PROT_ATAPI:
  3260. case ATA_PROT_PIO:
  3261. case ATA_PROT_PIO_MULT:
  3262. if (ap->flags & ATA_FLAG_PIO_DMA)
  3263. return 1;
  3264. /* fall through */
  3265. default:
  3266. return 0;
  3267. }
  3268. /* never reached */
  3269. }
  3270. /**
  3271. * ata_qc_issue - issue taskfile to device
  3272. * @qc: command to issue to device
  3273. *
  3274. * Prepare an ATA command to submission to device.
  3275. * This includes mapping the data into a DMA-able
  3276. * area, filling in the S/G table, and finally
  3277. * writing the taskfile to hardware, starting the command.
  3278. *
  3279. * LOCKING:
  3280. * spin_lock_irqsave(host_set lock)
  3281. *
  3282. * RETURNS:
  3283. * Zero on success, negative on error.
  3284. */
  3285. int ata_qc_issue(struct ata_queued_cmd *qc)
  3286. {
  3287. struct ata_port *ap = qc->ap;
  3288. if (ata_should_dma_map(qc)) {
  3289. if (qc->flags & ATA_QCFLAG_SG) {
  3290. if (ata_sg_setup(qc))
  3291. goto err_out;
  3292. } else if (qc->flags & ATA_QCFLAG_SINGLE) {
  3293. if (ata_sg_setup_one(qc))
  3294. goto err_out;
  3295. }
  3296. } else {
  3297. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  3298. }
  3299. ap->ops->qc_prep(qc);
  3300. qc->ap->active_tag = qc->tag;
  3301. qc->flags |= ATA_QCFLAG_ACTIVE;
  3302. return ap->ops->qc_issue(qc);
  3303. err_out:
  3304. return -1;
  3305. }
  3306. /**
  3307. * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
  3308. * @qc: command to issue to device
  3309. *
  3310. * Using various libata functions and hooks, this function
  3311. * starts an ATA command. ATA commands are grouped into
  3312. * classes called "protocols", and issuing each type of protocol
  3313. * is slightly different.
  3314. *
  3315. * May be used as the qc_issue() entry in ata_port_operations.
  3316. *
  3317. * LOCKING:
  3318. * spin_lock_irqsave(host_set lock)
  3319. *
  3320. * RETURNS:
  3321. * Zero on success, negative on error.
  3322. */
  3323. int ata_qc_issue_prot(struct ata_queued_cmd *qc)
  3324. {
  3325. struct ata_port *ap = qc->ap;
  3326. /* Use polling pio if the LLD doesn't handle
  3327. * interrupt driven pio and atapi CDB interrupt.
  3328. */
  3329. if (ap->flags & ATA_FLAG_PIO_POLLING) {
  3330. switch (qc->tf.protocol) {
  3331. case ATA_PROT_PIO:
  3332. case ATA_PROT_ATAPI:
  3333. case ATA_PROT_ATAPI_NODATA:
  3334. qc->tf.flags |= ATA_TFLAG_POLLING;
  3335. break;
  3336. case ATA_PROT_ATAPI_DMA:
  3337. if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
  3338. BUG();
  3339. break;
  3340. default:
  3341. break;
  3342. }
  3343. }
  3344. /* select the device */
  3345. ata_dev_select(ap, qc->dev->devno, 1, 0);
  3346. /* start the command */
  3347. switch (qc->tf.protocol) {
  3348. case ATA_PROT_NODATA:
  3349. if (qc->tf.flags & ATA_TFLAG_POLLING)
  3350. ata_qc_set_polling(qc);
  3351. ata_tf_to_host(ap, &qc->tf);
  3352. ap->hsm_task_state = HSM_ST_LAST;
  3353. if (qc->tf.flags & ATA_TFLAG_POLLING)
  3354. queue_work(ata_wq, &ap->pio_task);
  3355. break;
  3356. case ATA_PROT_DMA:
  3357. assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
  3358. ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
  3359. ap->ops->bmdma_setup(qc); /* set up bmdma */
  3360. ap->ops->bmdma_start(qc); /* initiate bmdma */
  3361. ap->hsm_task_state = HSM_ST_LAST;
  3362. break;
  3363. case ATA_PROT_PIO:
  3364. if (qc->tf.flags & ATA_TFLAG_POLLING)
  3365. ata_qc_set_polling(qc);
  3366. ata_tf_to_host(ap, &qc->tf);
  3367. if (qc->tf.flags & ATA_TFLAG_WRITE) {
  3368. /* PIO data out protocol */
  3369. ap->hsm_task_state = HSM_ST_FIRST;
  3370. queue_work(ata_wq, &ap->pio_task);
  3371. /* always send first data block using
  3372. * the ata_pio_task() codepath.
  3373. */
  3374. } else {
  3375. /* PIO data in protocol */
  3376. ap->hsm_task_state = HSM_ST;
  3377. if (qc->tf.flags & ATA_TFLAG_POLLING)
  3378. queue_work(ata_wq, &ap->pio_task);
  3379. /* if polling, ata_pio_task() handles the rest.
  3380. * otherwise, interrupt handler takes over from here.
  3381. */
  3382. }
  3383. break;
  3384. case ATA_PROT_ATAPI:
  3385. case ATA_PROT_ATAPI_NODATA:
  3386. if (qc->tf.flags & ATA_TFLAG_POLLING)
  3387. ata_qc_set_polling(qc);
  3388. ata_tf_to_host(ap, &qc->tf);
  3389. ap->hsm_task_state = HSM_ST_FIRST;
  3390. /* send cdb by polling if no cdb interrupt */
  3391. if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
  3392. (qc->tf.flags & ATA_TFLAG_POLLING))
  3393. queue_work(ata_wq, &ap->pio_task);
  3394. break;
  3395. case ATA_PROT_ATAPI_DMA:
  3396. assert(!(qc->tf.flags & ATA_TFLAG_POLLING));
  3397. ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
  3398. ap->ops->bmdma_setup(qc); /* set up bmdma */
  3399. ap->hsm_task_state = HSM_ST_FIRST;
  3400. /* send cdb by polling if no cdb interrupt */
  3401. if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  3402. queue_work(ata_wq, &ap->pio_task);
  3403. break;
  3404. default:
  3405. WARN_ON(1);
  3406. return -1;
  3407. }
  3408. return 0;
  3409. }
  3410. /**
  3411. * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
  3412. * @qc: Info associated with this ATA transaction.
  3413. *
  3414. * LOCKING:
  3415. * spin_lock_irqsave(host_set lock)
  3416. */
  3417. static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
  3418. {
  3419. struct ata_port *ap = qc->ap;
  3420. unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
  3421. u8 dmactl;
  3422. void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
  3423. /* load PRD table addr. */
  3424. mb(); /* make sure PRD table writes are visible to controller */
  3425. writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
  3426. /* specify data direction, triple-check start bit is clear */
  3427. dmactl = readb(mmio + ATA_DMA_CMD);
  3428. dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
  3429. if (!rw)
  3430. dmactl |= ATA_DMA_WR;
  3431. writeb(dmactl, mmio + ATA_DMA_CMD);
  3432. /* issue r/w command */
  3433. ap->ops->exec_command(ap, &qc->tf);
  3434. }
  3435. /**
  3436. * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
  3437. * @qc: Info associated with this ATA transaction.
  3438. *
  3439. * LOCKING:
  3440. * spin_lock_irqsave(host_set lock)
  3441. */
  3442. static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
  3443. {
  3444. struct ata_port *ap = qc->ap;
  3445. void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
  3446. u8 dmactl;
  3447. /* start host DMA transaction */
  3448. dmactl = readb(mmio + ATA_DMA_CMD);
  3449. writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
  3450. /* Strictly, one may wish to issue a readb() here, to
  3451. * flush the mmio write. However, control also passes
  3452. * to the hardware at this point, and it will interrupt
  3453. * us when we are to resume control. So, in effect,
  3454. * we don't care when the mmio write flushes.
  3455. * Further, a read of the DMA status register _immediately_
  3456. * following the write may not be what certain flaky hardware
  3457. * is expected, so I think it is best to not add a readb()
  3458. * without first all the MMIO ATA cards/mobos.
  3459. * Or maybe I'm just being paranoid.
  3460. */
  3461. }
  3462. /**
  3463. * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
  3464. * @qc: Info associated with this ATA transaction.
  3465. *
  3466. * LOCKING:
  3467. * spin_lock_irqsave(host_set lock)
  3468. */
  3469. static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
  3470. {
  3471. struct ata_port *ap = qc->ap;
  3472. unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
  3473. u8 dmactl;
  3474. /* load PRD table addr. */
  3475. outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
  3476. /* specify data direction, triple-check start bit is clear */
  3477. dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  3478. dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
  3479. if (!rw)
  3480. dmactl |= ATA_DMA_WR;
  3481. outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  3482. /* issue r/w command */
  3483. ap->ops->exec_command(ap, &qc->tf);
  3484. }
  3485. /**
  3486. * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
  3487. * @qc: Info associated with this ATA transaction.
  3488. *
  3489. * LOCKING:
  3490. * spin_lock_irqsave(host_set lock)
  3491. */
  3492. static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
  3493. {
  3494. struct ata_port *ap = qc->ap;
  3495. u8 dmactl;
  3496. /* start host DMA transaction */
  3497. dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  3498. outb(dmactl | ATA_DMA_START,
  3499. ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  3500. }
  3501. /**
  3502. * ata_bmdma_start - Start a PCI IDE BMDMA transaction
  3503. * @qc: Info associated with this ATA transaction.
  3504. *
  3505. * Writes the ATA_DMA_START flag to the DMA command register.
  3506. *
  3507. * May be used as the bmdma_start() entry in ata_port_operations.
  3508. *
  3509. * LOCKING:
  3510. * spin_lock_irqsave(host_set lock)
  3511. */
  3512. void ata_bmdma_start(struct ata_queued_cmd *qc)
  3513. {
  3514. if (qc->ap->flags & ATA_FLAG_MMIO)
  3515. ata_bmdma_start_mmio(qc);
  3516. else
  3517. ata_bmdma_start_pio(qc);
  3518. }
  3519. /**
  3520. * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
  3521. * @qc: Info associated with this ATA transaction.
  3522. *
  3523. * Writes address of PRD table to device's PRD Table Address
  3524. * register, sets the DMA control register, and calls
  3525. * ops->exec_command() to start the transfer.
  3526. *
  3527. * May be used as the bmdma_setup() entry in ata_port_operations.
  3528. *
  3529. * LOCKING:
  3530. * spin_lock_irqsave(host_set lock)
  3531. */
  3532. void ata_bmdma_setup(struct ata_queued_cmd *qc)
  3533. {
  3534. if (qc->ap->flags & ATA_FLAG_MMIO)
  3535. ata_bmdma_setup_mmio(qc);
  3536. else
  3537. ata_bmdma_setup_pio(qc);
  3538. }
  3539. /**
  3540. * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
  3541. * @ap: Port associated with this ATA transaction.
  3542. *
  3543. * Clear interrupt and error flags in DMA status register.
  3544. *
  3545. * May be used as the irq_clear() entry in ata_port_operations.
  3546. *
  3547. * LOCKING:
  3548. * spin_lock_irqsave(host_set lock)
  3549. */
  3550. void ata_bmdma_irq_clear(struct ata_port *ap)
  3551. {
  3552. if (ap->flags & ATA_FLAG_MMIO) {
  3553. void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
  3554. writeb(readb(mmio), mmio);
  3555. } else {
  3556. unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
  3557. outb(inb(addr), addr);
  3558. }
  3559. }
  3560. /**
  3561. * ata_bmdma_status - Read PCI IDE BMDMA status
  3562. * @ap: Port associated with this ATA transaction.
  3563. *
  3564. * Read and return BMDMA status register.
  3565. *
  3566. * May be used as the bmdma_status() entry in ata_port_operations.
  3567. *
  3568. * LOCKING:
  3569. * spin_lock_irqsave(host_set lock)
  3570. */
  3571. u8 ata_bmdma_status(struct ata_port *ap)
  3572. {
  3573. u8 host_stat;
  3574. if (ap->flags & ATA_FLAG_MMIO) {
  3575. void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
  3576. host_stat = readb(mmio + ATA_DMA_STATUS);
  3577. } else
  3578. host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
  3579. return host_stat;
  3580. }
  3581. /**
  3582. * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
  3583. * @qc: Command we are ending DMA for
  3584. *
  3585. * Clears the ATA_DMA_START flag in the dma control register
  3586. *
  3587. * May be used as the bmdma_stop() entry in ata_port_operations.
  3588. *
  3589. * LOCKING:
  3590. * spin_lock_irqsave(host_set lock)
  3591. */
  3592. void ata_bmdma_stop(struct ata_queued_cmd *qc)
  3593. {
  3594. struct ata_port *ap = qc->ap;
  3595. if (ap->flags & ATA_FLAG_MMIO) {
  3596. void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
  3597. /* clear start/stop bit */
  3598. writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
  3599. mmio + ATA_DMA_CMD);
  3600. } else {
  3601. /* clear start/stop bit */
  3602. outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
  3603. ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  3604. }
  3605. /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
  3606. ata_altstatus(ap); /* dummy read */
  3607. }
  3608. /**
  3609. * ata_host_intr - Handle host interrupt for given (port, task)
  3610. * @ap: Port on which interrupt arrived (possibly...)
  3611. * @qc: Taskfile currently active in engine
  3612. *
  3613. * Handle host interrupt for given queued command. Currently,
  3614. * only DMA interrupts are handled. All other commands are
  3615. * handled via polling with interrupts disabled (nIEN bit).
  3616. *
  3617. * LOCKING:
  3618. * spin_lock_irqsave(host_set lock)
  3619. *
  3620. * RETURNS:
  3621. * One if interrupt was handled, zero if not (shared irq).
  3622. */
  3623. inline unsigned int ata_host_intr (struct ata_port *ap,
  3624. struct ata_queued_cmd *qc)
  3625. {
  3626. u8 status, host_stat = 0;
  3627. VPRINTK("ata%u: protocol %d task_state %d\n",
  3628. ap->id, qc->tf.protocol, ap->hsm_task_state);
  3629. /* Check whether we are expecting interrupt in this state */
  3630. switch (ap->hsm_task_state) {
  3631. case HSM_ST_FIRST:
  3632. /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
  3633. * The flag was turned on only for atapi devices.
  3634. * No need to check is_atapi_taskfile(&qc->tf) again.
  3635. */
  3636. if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  3637. goto idle_irq;
  3638. break;
  3639. case HSM_ST_LAST:
  3640. if (qc->tf.protocol == ATA_PROT_DMA ||
  3641. qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
  3642. /* check status of DMA engine */
  3643. host_stat = ap->ops->bmdma_status(ap);
  3644. VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
  3645. /* if it's not our irq... */
  3646. if (!(host_stat & ATA_DMA_INTR))
  3647. goto idle_irq;
  3648. /* before we do anything else, clear DMA-Start bit */
  3649. ap->ops->bmdma_stop(qc);
  3650. if (unlikely(host_stat & ATA_DMA_ERR)) {
  3651. /* error when transfering data to/from memory */
  3652. qc->err_mask |= AC_ERR_HOST_BUS;
  3653. ap->hsm_task_state = HSM_ST_ERR;
  3654. }
  3655. }
  3656. break;
  3657. case HSM_ST:
  3658. break;
  3659. default:
  3660. goto idle_irq;
  3661. }
  3662. /* check altstatus */
  3663. status = ata_altstatus(ap);
  3664. if (status & ATA_BUSY)
  3665. goto idle_irq;
  3666. /* check main status, clearing INTRQ */
  3667. status = ata_chk_status(ap);
  3668. if (unlikely(status & ATA_BUSY))
  3669. goto idle_irq;
  3670. DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
  3671. ap->id, qc->tf.protocol, ap->hsm_task_state, status);
  3672. /* ack bmdma irq events */
  3673. ap->ops->irq_clear(ap);
  3674. /* check error */
  3675. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  3676. qc->err_mask |= AC_ERR_DEV;
  3677. ap->hsm_task_state = HSM_ST_ERR;
  3678. }
  3679. fsm_start:
  3680. switch (ap->hsm_task_state) {
  3681. case HSM_ST_FIRST:
  3682. /* Some pre-ATAPI-4 devices assert INTRQ
  3683. * at this state when ready to receive CDB.
  3684. */
  3685. /* check device status */
  3686. if (unlikely((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)) {
  3687. /* Wrong status. Let EH handle this */
  3688. qc->err_mask |= AC_ERR_ATA_BUS;
  3689. ap->hsm_task_state = HSM_ST_ERR;
  3690. goto fsm_start;
  3691. }
  3692. atapi_send_cdb(ap, qc);
  3693. break;
  3694. case HSM_ST:
  3695. /* complete command or read/write the data register */
  3696. if (qc->tf.protocol == ATA_PROT_ATAPI) {
  3697. /* ATAPI PIO protocol */
  3698. if ((status & ATA_DRQ) == 0) {
  3699. /* no more data to transfer */
  3700. ap->hsm_task_state = HSM_ST_LAST;
  3701. goto fsm_start;
  3702. }
  3703. atapi_pio_bytes(qc);
  3704. if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
  3705. /* bad ireason reported by device */
  3706. goto fsm_start;
  3707. } else {
  3708. /* ATA PIO protocol */
  3709. if (unlikely((status & ATA_DRQ) == 0)) {
  3710. /* handle BSY=0, DRQ=0 as error */
  3711. qc->err_mask |= AC_ERR_ATA_BUS;
  3712. ap->hsm_task_state = HSM_ST_ERR;
  3713. goto fsm_start;
  3714. }
  3715. ata_pio_sectors(qc);
  3716. if (ap->hsm_task_state == HSM_ST_LAST &&
  3717. (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
  3718. /* all data read */
  3719. ata_altstatus(ap);
  3720. status = ata_chk_status(ap);
  3721. goto fsm_start;
  3722. }
  3723. }
  3724. ata_altstatus(ap); /* flush */
  3725. break;
  3726. case HSM_ST_LAST:
  3727. if (unlikely(status & ATA_DRQ)) {
  3728. /* handle DRQ=1 as error */
  3729. qc->err_mask |= AC_ERR_ATA_BUS;
  3730. ap->hsm_task_state = HSM_ST_ERR;
  3731. goto fsm_start;
  3732. }
  3733. /* no more data to transfer */
  3734. DPRINTK("ata%u: command complete, drv_stat 0x%x\n",
  3735. ap->id, status);
  3736. ap->hsm_task_state = HSM_ST_IDLE;
  3737. /* complete taskfile transaction */
  3738. qc->err_mask |= ac_err_mask(status);
  3739. ata_qc_complete(qc);
  3740. break;
  3741. case HSM_ST_ERR:
  3742. if (qc->tf.command != ATA_CMD_PACKET)
  3743. printk(KERN_ERR "ata%u: command error, drv_stat 0x%x host_stat 0x%x\n",
  3744. ap->id, status, host_stat);
  3745. /* make sure qc->err_mask is available to
  3746. * know what's wrong and recover
  3747. */
  3748. assert(qc->err_mask);
  3749. ap->hsm_task_state = HSM_ST_IDLE;
  3750. ata_qc_complete(qc);
  3751. break;
  3752. default:
  3753. goto idle_irq;
  3754. }
  3755. return 1; /* irq handled */
  3756. idle_irq:
  3757. ap->stats.idle_irq++;
  3758. #ifdef ATA_IRQ_TRAP
  3759. if ((ap->stats.idle_irq % 1000) == 0) {
  3760. handled = 1;
  3761. ata_irq_ack(ap, 0); /* debug trap */
  3762. printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
  3763. }
  3764. #endif
  3765. return 0; /* irq not handled */
  3766. }
  3767. /**
  3768. * ata_interrupt - Default ATA host interrupt handler
  3769. * @irq: irq line (unused)
  3770. * @dev_instance: pointer to our ata_host_set information structure
  3771. * @regs: unused
  3772. *
  3773. * Default interrupt handler for PCI IDE devices. Calls
  3774. * ata_host_intr() for each port that is not disabled.
  3775. *
  3776. * LOCKING:
  3777. * Obtains host_set lock during operation.
  3778. *
  3779. * RETURNS:
  3780. * IRQ_NONE or IRQ_HANDLED.
  3781. */
  3782. irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
  3783. {
  3784. struct ata_host_set *host_set = dev_instance;
  3785. unsigned int i;
  3786. unsigned int handled = 0;
  3787. unsigned long flags;
  3788. /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
  3789. spin_lock_irqsave(&host_set->lock, flags);
  3790. for (i = 0; i < host_set->n_ports; i++) {
  3791. struct ata_port *ap;
  3792. ap = host_set->ports[i];
  3793. if (ap &&
  3794. !(ap->flags & ATA_FLAG_PORT_DISABLED)) {
  3795. struct ata_queued_cmd *qc;
  3796. qc = ata_qc_from_tag(ap, ap->active_tag);
  3797. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
  3798. (qc->flags & ATA_QCFLAG_ACTIVE))
  3799. handled |= ata_host_intr(ap, qc);
  3800. }
  3801. }
  3802. spin_unlock_irqrestore(&host_set->lock, flags);
  3803. return IRQ_RETVAL(handled);
  3804. }
  3805. /**
  3806. * ata_port_start - Set port up for dma.
  3807. * @ap: Port to initialize
  3808. *
  3809. * Called just after data structures for each port are
  3810. * initialized. Allocates space for PRD table.
  3811. *
  3812. * May be used as the port_start() entry in ata_port_operations.
  3813. *
  3814. * LOCKING:
  3815. * Inherited from caller.
  3816. */
  3817. /*
  3818. * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
  3819. * without filling any other registers
  3820. */
  3821. static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
  3822. u8 cmd)
  3823. {
  3824. struct ata_taskfile tf;
  3825. int err;
  3826. ata_tf_init(ap, &tf, dev->devno);
  3827. tf.command = cmd;
  3828. tf.flags |= ATA_TFLAG_DEVICE;
  3829. tf.protocol = ATA_PROT_NODATA;
  3830. err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
  3831. if (err)
  3832. printk(KERN_ERR "%s: ata command failed: %d\n",
  3833. __FUNCTION__, err);
  3834. return err;
  3835. }
  3836. static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
  3837. {
  3838. u8 cmd;
  3839. if (!ata_try_flush_cache(dev))
  3840. return 0;
  3841. if (ata_id_has_flush_ext(dev->id))
  3842. cmd = ATA_CMD_FLUSH_EXT;
  3843. else
  3844. cmd = ATA_CMD_FLUSH;
  3845. return ata_do_simple_cmd(ap, dev, cmd);
  3846. }
  3847. static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
  3848. {
  3849. return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
  3850. }
  3851. static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
  3852. {
  3853. return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
  3854. }
  3855. /**
  3856. * ata_device_resume - wakeup a previously suspended devices
  3857. *
  3858. * Kick the drive back into action, by sending it an idle immediate
  3859. * command and making sure its transfer mode matches between drive
  3860. * and host.
  3861. *
  3862. */
  3863. int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
  3864. {
  3865. if (ap->flags & ATA_FLAG_SUSPENDED) {
  3866. ap->flags &= ~ATA_FLAG_SUSPENDED;
  3867. ata_set_mode(ap);
  3868. }
  3869. if (!ata_dev_present(dev))
  3870. return 0;
  3871. if (dev->class == ATA_DEV_ATA)
  3872. ata_start_drive(ap, dev);
  3873. return 0;
  3874. }
  3875. /**
  3876. * ata_device_suspend - prepare a device for suspend
  3877. *
  3878. * Flush the cache on the drive, if appropriate, then issue a
  3879. * standbynow command.
  3880. *
  3881. */
  3882. int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
  3883. {
  3884. if (!ata_dev_present(dev))
  3885. return 0;
  3886. if (dev->class == ATA_DEV_ATA)
  3887. ata_flush_cache(ap, dev);
  3888. ata_standby_drive(ap, dev);
  3889. ap->flags |= ATA_FLAG_SUSPENDED;
  3890. return 0;
  3891. }
  3892. int ata_port_start (struct ata_port *ap)
  3893. {
  3894. struct device *dev = ap->host_set->dev;
  3895. int rc;
  3896. ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
  3897. if (!ap->prd)
  3898. return -ENOMEM;
  3899. rc = ata_pad_alloc(ap, dev);
  3900. if (rc) {
  3901. dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
  3902. return rc;
  3903. }
  3904. DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
  3905. return 0;
  3906. }
  3907. /**
  3908. * ata_port_stop - Undo ata_port_start()
  3909. * @ap: Port to shut down
  3910. *
  3911. * Frees the PRD table.
  3912. *
  3913. * May be used as the port_stop() entry in ata_port_operations.
  3914. *
  3915. * LOCKING:
  3916. * Inherited from caller.
  3917. */
  3918. void ata_port_stop (struct ata_port *ap)
  3919. {
  3920. struct device *dev = ap->host_set->dev;
  3921. dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
  3922. ata_pad_free(ap, dev);
  3923. }
  3924. void ata_host_stop (struct ata_host_set *host_set)
  3925. {
  3926. if (host_set->mmio_base)
  3927. iounmap(host_set->mmio_base);
  3928. }
  3929. /**
  3930. * ata_host_remove - Unregister SCSI host structure with upper layers
  3931. * @ap: Port to unregister
  3932. * @do_unregister: 1 if we fully unregister, 0 to just stop the port
  3933. *
  3934. * LOCKING:
  3935. * Inherited from caller.
  3936. */
  3937. static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
  3938. {
  3939. struct Scsi_Host *sh = ap->host;
  3940. DPRINTK("ENTER\n");
  3941. if (do_unregister)
  3942. scsi_remove_host(sh);
  3943. ap->ops->port_stop(ap);
  3944. }
  3945. /**
  3946. * ata_host_init - Initialize an ata_port structure
  3947. * @ap: Structure to initialize
  3948. * @host: associated SCSI mid-layer structure
  3949. * @host_set: Collection of hosts to which @ap belongs
  3950. * @ent: Probe information provided by low-level driver
  3951. * @port_no: Port number associated with this ata_port
  3952. *
  3953. * Initialize a new ata_port structure, and its associated
  3954. * scsi_host.
  3955. *
  3956. * LOCKING:
  3957. * Inherited from caller.
  3958. */
  3959. static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
  3960. struct ata_host_set *host_set,
  3961. const struct ata_probe_ent *ent, unsigned int port_no)
  3962. {
  3963. unsigned int i;
  3964. host->max_id = 16;
  3965. host->max_lun = 1;
  3966. host->max_channel = 1;
  3967. host->unique_id = ata_unique_id++;
  3968. host->max_cmd_len = 12;
  3969. ap->flags = ATA_FLAG_PORT_DISABLED;
  3970. ap->id = host->unique_id;
  3971. ap->host = host;
  3972. ap->ctl = ATA_DEVCTL_OBS;
  3973. ap->host_set = host_set;
  3974. ap->port_no = port_no;
  3975. ap->hard_port_no =
  3976. ent->legacy_mode ? ent->hard_port_no : port_no;
  3977. ap->pio_mask = ent->pio_mask;
  3978. ap->mwdma_mask = ent->mwdma_mask;
  3979. ap->udma_mask = ent->udma_mask;
  3980. ap->flags |= ent->host_flags;
  3981. ap->ops = ent->port_ops;
  3982. ap->cbl = ATA_CBL_NONE;
  3983. ap->active_tag = ATA_TAG_POISON;
  3984. ap->last_ctl = 0xFF;
  3985. INIT_WORK(&ap->pio_task, ata_pio_task, ap);
  3986. for (i = 0; i < ATA_MAX_DEVICES; i++)
  3987. ap->device[i].devno = i;
  3988. #ifdef ATA_IRQ_TRAP
  3989. ap->stats.unhandled_irq = 1;
  3990. ap->stats.idle_irq = 1;
  3991. #endif
  3992. memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
  3993. }
  3994. /**
  3995. * ata_host_add - Attach low-level ATA driver to system
  3996. * @ent: Information provided by low-level driver
  3997. * @host_set: Collections of ports to which we add
  3998. * @port_no: Port number associated with this host
  3999. *
  4000. * Attach low-level ATA driver to system.
  4001. *
  4002. * LOCKING:
  4003. * PCI/etc. bus probe sem.
  4004. *
  4005. * RETURNS:
  4006. * New ata_port on success, for NULL on error.
  4007. */
  4008. static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
  4009. struct ata_host_set *host_set,
  4010. unsigned int port_no)
  4011. {
  4012. struct Scsi_Host *host;
  4013. struct ata_port *ap;
  4014. int rc;
  4015. DPRINTK("ENTER\n");
  4016. host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
  4017. if (!host)
  4018. return NULL;
  4019. ap = (struct ata_port *) &host->hostdata[0];
  4020. ata_host_init(ap, host, host_set, ent, port_no);
  4021. rc = ap->ops->port_start(ap);
  4022. if (rc)
  4023. goto err_out;
  4024. return ap;
  4025. err_out:
  4026. scsi_host_put(host);
  4027. return NULL;
  4028. }
  4029. /**
  4030. * ata_device_add - Register hardware device with ATA and SCSI layers
  4031. * @ent: Probe information describing hardware device to be registered
  4032. *
  4033. * This function processes the information provided in the probe
  4034. * information struct @ent, allocates the necessary ATA and SCSI
  4035. * host information structures, initializes them, and registers
  4036. * everything with requisite kernel subsystems.
  4037. *
  4038. * This function requests irqs, probes the ATA bus, and probes
  4039. * the SCSI bus.
  4040. *
  4041. * LOCKING:
  4042. * PCI/etc. bus probe sem.
  4043. *
  4044. * RETURNS:
  4045. * Number of ports registered. Zero on error (no ports registered).
  4046. */
  4047. int ata_device_add(const struct ata_probe_ent *ent)
  4048. {
  4049. unsigned int count = 0, i;
  4050. struct device *dev = ent->dev;
  4051. struct ata_host_set *host_set;
  4052. DPRINTK("ENTER\n");
  4053. /* alloc a container for our list of ATA ports (buses) */
  4054. host_set = kzalloc(sizeof(struct ata_host_set) +
  4055. (ent->n_ports * sizeof(void *)), GFP_KERNEL);
  4056. if (!host_set)
  4057. return 0;
  4058. spin_lock_init(&host_set->lock);
  4059. host_set->dev = dev;
  4060. host_set->n_ports = ent->n_ports;
  4061. host_set->irq = ent->irq;
  4062. host_set->mmio_base = ent->mmio_base;
  4063. host_set->private_data = ent->private_data;
  4064. host_set->ops = ent->port_ops;
  4065. /* register each port bound to this device */
  4066. for (i = 0; i < ent->n_ports; i++) {
  4067. struct ata_port *ap;
  4068. unsigned long xfer_mode_mask;
  4069. ap = ata_host_add(ent, host_set, i);
  4070. if (!ap)
  4071. goto err_out;
  4072. host_set->ports[i] = ap;
  4073. xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
  4074. (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
  4075. (ap->pio_mask << ATA_SHIFT_PIO);
  4076. /* print per-port info to dmesg */
  4077. printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
  4078. "bmdma 0x%lX irq %lu\n",
  4079. ap->id,
  4080. ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
  4081. ata_mode_string(xfer_mode_mask),
  4082. ap->ioaddr.cmd_addr,
  4083. ap->ioaddr.ctl_addr,
  4084. ap->ioaddr.bmdma_addr,
  4085. ent->irq);
  4086. ata_chk_status(ap);
  4087. host_set->ops->irq_clear(ap);
  4088. count++;
  4089. }
  4090. if (!count)
  4091. goto err_free_ret;
  4092. /* obtain irq, that is shared between channels */
  4093. if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
  4094. DRV_NAME, host_set))
  4095. goto err_out;
  4096. /* perform each probe synchronously */
  4097. DPRINTK("probe begin\n");
  4098. for (i = 0; i < count; i++) {
  4099. struct ata_port *ap;
  4100. int rc;
  4101. ap = host_set->ports[i];
  4102. DPRINTK("ata%u: probe begin\n", ap->id);
  4103. rc = ata_bus_probe(ap);
  4104. DPRINTK("ata%u: probe end\n", ap->id);
  4105. if (rc) {
  4106. /* FIXME: do something useful here?
  4107. * Current libata behavior will
  4108. * tear down everything when
  4109. * the module is removed
  4110. * or the h/w is unplugged.
  4111. */
  4112. }
  4113. rc = scsi_add_host(ap->host, dev);
  4114. if (rc) {
  4115. printk(KERN_ERR "ata%u: scsi_add_host failed\n",
  4116. ap->id);
  4117. /* FIXME: do something useful here */
  4118. /* FIXME: handle unconditional calls to
  4119. * scsi_scan_host and ata_host_remove, below,
  4120. * at the very least
  4121. */
  4122. }
  4123. }
  4124. /* probes are done, now scan each port's disk(s) */
  4125. DPRINTK("probe begin\n");
  4126. for (i = 0; i < count; i++) {
  4127. struct ata_port *ap = host_set->ports[i];
  4128. ata_scsi_scan_host(ap);
  4129. }
  4130. dev_set_drvdata(dev, host_set);
  4131. VPRINTK("EXIT, returning %u\n", ent->n_ports);
  4132. return ent->n_ports; /* success */
  4133. err_out:
  4134. for (i = 0; i < count; i++) {
  4135. ata_host_remove(host_set->ports[i], 1);
  4136. scsi_host_put(host_set->ports[i]->host);
  4137. }
  4138. err_free_ret:
  4139. kfree(host_set);
  4140. VPRINTK("EXIT, returning 0\n");
  4141. return 0;
  4142. }
  4143. /**
  4144. * ata_host_set_remove - PCI layer callback for device removal
  4145. * @host_set: ATA host set that was removed
  4146. *
  4147. * Unregister all objects associated with this host set. Free those
  4148. * objects.
  4149. *
  4150. * LOCKING:
  4151. * Inherited from calling layer (may sleep).
  4152. */
  4153. void ata_host_set_remove(struct ata_host_set *host_set)
  4154. {
  4155. struct ata_port *ap;
  4156. unsigned int i;
  4157. for (i = 0; i < host_set->n_ports; i++) {
  4158. ap = host_set->ports[i];
  4159. scsi_remove_host(ap->host);
  4160. }
  4161. free_irq(host_set->irq, host_set);
  4162. for (i = 0; i < host_set->n_ports; i++) {
  4163. ap = host_set->ports[i];
  4164. ata_scsi_release(ap->host);
  4165. if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
  4166. struct ata_ioports *ioaddr = &ap->ioaddr;
  4167. if (ioaddr->cmd_addr == 0x1f0)
  4168. release_region(0x1f0, 8);
  4169. else if (ioaddr->cmd_addr == 0x170)
  4170. release_region(0x170, 8);
  4171. }
  4172. scsi_host_put(ap->host);
  4173. }
  4174. if (host_set->ops->host_stop)
  4175. host_set->ops->host_stop(host_set);
  4176. kfree(host_set);
  4177. }
  4178. /**
  4179. * ata_scsi_release - SCSI layer callback hook for host unload
  4180. * @host: libata host to be unloaded
  4181. *
  4182. * Performs all duties necessary to shut down a libata port...
  4183. * Kill port kthread, disable port, and release resources.
  4184. *
  4185. * LOCKING:
  4186. * Inherited from SCSI layer.
  4187. *
  4188. * RETURNS:
  4189. * One.
  4190. */
  4191. int ata_scsi_release(struct Scsi_Host *host)
  4192. {
  4193. struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
  4194. DPRINTK("ENTER\n");
  4195. ap->ops->port_disable(ap);
  4196. ata_host_remove(ap, 0);
  4197. DPRINTK("EXIT\n");
  4198. return 1;
  4199. }
  4200. /**
  4201. * ata_std_ports - initialize ioaddr with standard port offsets.
  4202. * @ioaddr: IO address structure to be initialized
  4203. *
  4204. * Utility function which initializes data_addr, error_addr,
  4205. * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
  4206. * device_addr, status_addr, and command_addr to standard offsets
  4207. * relative to cmd_addr.
  4208. *
  4209. * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
  4210. */
  4211. void ata_std_ports(struct ata_ioports *ioaddr)
  4212. {
  4213. ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
  4214. ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
  4215. ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
  4216. ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
  4217. ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
  4218. ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
  4219. ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
  4220. ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
  4221. ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
  4222. ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
  4223. }
  4224. static struct ata_probe_ent *
  4225. ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
  4226. {
  4227. struct ata_probe_ent *probe_ent;
  4228. probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
  4229. if (!probe_ent) {
  4230. printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
  4231. kobject_name(&(dev->kobj)));
  4232. return NULL;
  4233. }
  4234. INIT_LIST_HEAD(&probe_ent->node);
  4235. probe_ent->dev = dev;
  4236. probe_ent->sht = port->sht;
  4237. probe_ent->host_flags = port->host_flags;
  4238. probe_ent->pio_mask = port->pio_mask;
  4239. probe_ent->mwdma_mask = port->mwdma_mask;
  4240. probe_ent->udma_mask = port->udma_mask;
  4241. probe_ent->port_ops = port->port_ops;
  4242. return probe_ent;
  4243. }
  4244. #ifdef CONFIG_PCI
  4245. void ata_pci_host_stop (struct ata_host_set *host_set)
  4246. {
  4247. struct pci_dev *pdev = to_pci_dev(host_set->dev);
  4248. pci_iounmap(pdev, host_set->mmio_base);
  4249. }
  4250. /**
  4251. * ata_pci_init_native_mode - Initialize native-mode driver
  4252. * @pdev: pci device to be initialized
  4253. * @port: array[2] of pointers to port info structures.
  4254. * @ports: bitmap of ports present
  4255. *
  4256. * Utility function which allocates and initializes an
  4257. * ata_probe_ent structure for a standard dual-port
  4258. * PIO-based IDE controller. The returned ata_probe_ent
  4259. * structure can be passed to ata_device_add(). The returned
  4260. * ata_probe_ent structure should then be freed with kfree().
  4261. *
  4262. * The caller need only pass the address of the primary port, the
  4263. * secondary will be deduced automatically. If the device has non
  4264. * standard secondary port mappings this function can be called twice,
  4265. * once for each interface.
  4266. */
  4267. struct ata_probe_ent *
  4268. ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
  4269. {
  4270. struct ata_probe_ent *probe_ent =
  4271. ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
  4272. int p = 0;
  4273. if (!probe_ent)
  4274. return NULL;
  4275. probe_ent->irq = pdev->irq;
  4276. probe_ent->irq_flags = SA_SHIRQ;
  4277. probe_ent->private_data = port[0]->private_data;
  4278. if (ports & ATA_PORT_PRIMARY) {
  4279. probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
  4280. probe_ent->port[p].altstatus_addr =
  4281. probe_ent->port[p].ctl_addr =
  4282. pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
  4283. probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
  4284. ata_std_ports(&probe_ent->port[p]);
  4285. p++;
  4286. }
  4287. if (ports & ATA_PORT_SECONDARY) {
  4288. probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
  4289. probe_ent->port[p].altstatus_addr =
  4290. probe_ent->port[p].ctl_addr =
  4291. pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
  4292. probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
  4293. ata_std_ports(&probe_ent->port[p]);
  4294. p++;
  4295. }
  4296. probe_ent->n_ports = p;
  4297. return probe_ent;
  4298. }
  4299. static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev, struct ata_port_info *port, int port_num)
  4300. {
  4301. struct ata_probe_ent *probe_ent;
  4302. probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
  4303. if (!probe_ent)
  4304. return NULL;
  4305. probe_ent->legacy_mode = 1;
  4306. probe_ent->n_ports = 1;
  4307. probe_ent->hard_port_no = port_num;
  4308. probe_ent->private_data = port->private_data;
  4309. switch(port_num)
  4310. {
  4311. case 0:
  4312. probe_ent->irq = 14;
  4313. probe_ent->port[0].cmd_addr = 0x1f0;
  4314. probe_ent->port[0].altstatus_addr =
  4315. probe_ent->port[0].ctl_addr = 0x3f6;
  4316. break;
  4317. case 1:
  4318. probe_ent->irq = 15;
  4319. probe_ent->port[0].cmd_addr = 0x170;
  4320. probe_ent->port[0].altstatus_addr =
  4321. probe_ent->port[0].ctl_addr = 0x376;
  4322. break;
  4323. }
  4324. probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
  4325. ata_std_ports(&probe_ent->port[0]);
  4326. return probe_ent;
  4327. }
  4328. /**
  4329. * ata_pci_init_one - Initialize/register PCI IDE host controller
  4330. * @pdev: Controller to be initialized
  4331. * @port_info: Information from low-level host driver
  4332. * @n_ports: Number of ports attached to host controller
  4333. *
  4334. * This is a helper function which can be called from a driver's
  4335. * xxx_init_one() probe function if the hardware uses traditional
  4336. * IDE taskfile registers.
  4337. *
  4338. * This function calls pci_enable_device(), reserves its register
  4339. * regions, sets the dma mask, enables bus master mode, and calls
  4340. * ata_device_add()
  4341. *
  4342. * LOCKING:
  4343. * Inherited from PCI layer (may sleep).
  4344. *
  4345. * RETURNS:
  4346. * Zero on success, negative on errno-based value on error.
  4347. */
  4348. int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
  4349. unsigned int n_ports)
  4350. {
  4351. struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
  4352. struct ata_port_info *port[2];
  4353. u8 tmp8, mask;
  4354. unsigned int legacy_mode = 0;
  4355. int disable_dev_on_err = 1;
  4356. int rc;
  4357. DPRINTK("ENTER\n");
  4358. port[0] = port_info[0];
  4359. if (n_ports > 1)
  4360. port[1] = port_info[1];
  4361. else
  4362. port[1] = port[0];
  4363. if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
  4364. && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
  4365. /* TODO: What if one channel is in native mode ... */
  4366. pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
  4367. mask = (1 << 2) | (1 << 0);
  4368. if ((tmp8 & mask) != mask)
  4369. legacy_mode = (1 << 3);
  4370. }
  4371. /* FIXME... */
  4372. if ((!legacy_mode) && (n_ports > 2)) {
  4373. printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
  4374. n_ports = 2;
  4375. /* For now */
  4376. }
  4377. /* FIXME: Really for ATA it isn't safe because the device may be
  4378. multi-purpose and we want to leave it alone if it was already
  4379. enabled. Secondly for shared use as Arjan says we want refcounting
  4380. Checking dev->is_enabled is insufficient as this is not set at
  4381. boot for the primary video which is BIOS enabled
  4382. */
  4383. rc = pci_enable_device(pdev);
  4384. if (rc)
  4385. return rc;
  4386. rc = pci_request_regions(pdev, DRV_NAME);
  4387. if (rc) {
  4388. disable_dev_on_err = 0;
  4389. goto err_out;
  4390. }
  4391. /* FIXME: Should use platform specific mappers for legacy port ranges */
  4392. if (legacy_mode) {
  4393. if (!request_region(0x1f0, 8, "libata")) {
  4394. struct resource *conflict, res;
  4395. res.start = 0x1f0;
  4396. res.end = 0x1f0 + 8 - 1;
  4397. conflict = ____request_resource(&ioport_resource, &res);
  4398. if (!strcmp(conflict->name, "libata"))
  4399. legacy_mode |= (1 << 0);
  4400. else {
  4401. disable_dev_on_err = 0;
  4402. printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
  4403. }
  4404. } else
  4405. legacy_mode |= (1 << 0);
  4406. if (!request_region(0x170, 8, "libata")) {
  4407. struct resource *conflict, res;
  4408. res.start = 0x170;
  4409. res.end = 0x170 + 8 - 1;
  4410. conflict = ____request_resource(&ioport_resource, &res);
  4411. if (!strcmp(conflict->name, "libata"))
  4412. legacy_mode |= (1 << 1);
  4413. else {
  4414. disable_dev_on_err = 0;
  4415. printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
  4416. }
  4417. } else
  4418. legacy_mode |= (1 << 1);
  4419. }
  4420. /* we have legacy mode, but all ports are unavailable */
  4421. if (legacy_mode == (1 << 3)) {
  4422. rc = -EBUSY;
  4423. goto err_out_regions;
  4424. }
  4425. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  4426. if (rc)
  4427. goto err_out_regions;
  4428. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  4429. if (rc)
  4430. goto err_out_regions;
  4431. if (legacy_mode) {
  4432. if (legacy_mode & (1 << 0))
  4433. probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
  4434. if (legacy_mode & (1 << 1))
  4435. probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
  4436. } else {
  4437. if (n_ports == 2)
  4438. probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
  4439. else
  4440. probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
  4441. }
  4442. if (!probe_ent && !probe_ent2) {
  4443. rc = -ENOMEM;
  4444. goto err_out_regions;
  4445. }
  4446. pci_set_master(pdev);
  4447. /* FIXME: check ata_device_add return */
  4448. if (legacy_mode) {
  4449. if (legacy_mode & (1 << 0))
  4450. ata_device_add(probe_ent);
  4451. if (legacy_mode & (1 << 1))
  4452. ata_device_add(probe_ent2);
  4453. } else
  4454. ata_device_add(probe_ent);
  4455. kfree(probe_ent);
  4456. kfree(probe_ent2);
  4457. return 0;
  4458. err_out_regions:
  4459. if (legacy_mode & (1 << 0))
  4460. release_region(0x1f0, 8);
  4461. if (legacy_mode & (1 << 1))
  4462. release_region(0x170, 8);
  4463. pci_release_regions(pdev);
  4464. err_out:
  4465. if (disable_dev_on_err)
  4466. pci_disable_device(pdev);
  4467. return rc;
  4468. }
  4469. /**
  4470. * ata_pci_remove_one - PCI layer callback for device removal
  4471. * @pdev: PCI device that was removed
  4472. *
  4473. * PCI layer indicates to libata via this hook that
  4474. * hot-unplug or module unload event has occurred.
  4475. * Handle this by unregistering all objects associated
  4476. * with this PCI device. Free those objects. Then finally
  4477. * release PCI resources and disable device.
  4478. *
  4479. * LOCKING:
  4480. * Inherited from PCI layer (may sleep).
  4481. */
  4482. void ata_pci_remove_one (struct pci_dev *pdev)
  4483. {
  4484. struct device *dev = pci_dev_to_dev(pdev);
  4485. struct ata_host_set *host_set = dev_get_drvdata(dev);
  4486. ata_host_set_remove(host_set);
  4487. pci_release_regions(pdev);
  4488. pci_disable_device(pdev);
  4489. dev_set_drvdata(dev, NULL);
  4490. }
  4491. /* move to PCI subsystem */
  4492. int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
  4493. {
  4494. unsigned long tmp = 0;
  4495. switch (bits->width) {
  4496. case 1: {
  4497. u8 tmp8 = 0;
  4498. pci_read_config_byte(pdev, bits->reg, &tmp8);
  4499. tmp = tmp8;
  4500. break;
  4501. }
  4502. case 2: {
  4503. u16 tmp16 = 0;
  4504. pci_read_config_word(pdev, bits->reg, &tmp16);
  4505. tmp = tmp16;
  4506. break;
  4507. }
  4508. case 4: {
  4509. u32 tmp32 = 0;
  4510. pci_read_config_dword(pdev, bits->reg, &tmp32);
  4511. tmp = tmp32;
  4512. break;
  4513. }
  4514. default:
  4515. return -EINVAL;
  4516. }
  4517. tmp &= bits->mask;
  4518. return (tmp == bits->val) ? 1 : 0;
  4519. }
  4520. int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
  4521. {
  4522. pci_save_state(pdev);
  4523. pci_disable_device(pdev);
  4524. pci_set_power_state(pdev, PCI_D3hot);
  4525. return 0;
  4526. }
  4527. int ata_pci_device_resume(struct pci_dev *pdev)
  4528. {
  4529. pci_set_power_state(pdev, PCI_D0);
  4530. pci_restore_state(pdev);
  4531. pci_enable_device(pdev);
  4532. pci_set_master(pdev);
  4533. return 0;
  4534. }
  4535. #endif /* CONFIG_PCI */
  4536. static int __init ata_init(void)
  4537. {
  4538. ata_wq = create_workqueue("ata");
  4539. if (!ata_wq)
  4540. return -ENOMEM;
  4541. printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
  4542. return 0;
  4543. }
  4544. static void __exit ata_exit(void)
  4545. {
  4546. destroy_workqueue(ata_wq);
  4547. }
  4548. module_init(ata_init);
  4549. module_exit(ata_exit);
  4550. static unsigned long ratelimit_time;
  4551. static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
  4552. int ata_ratelimit(void)
  4553. {
  4554. int rc;
  4555. unsigned long flags;
  4556. spin_lock_irqsave(&ata_ratelimit_lock, flags);
  4557. if (time_after(jiffies, ratelimit_time)) {
  4558. rc = 1;
  4559. ratelimit_time = jiffies + (HZ/5);
  4560. } else
  4561. rc = 0;
  4562. spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
  4563. return rc;
  4564. }
  4565. /*
  4566. * libata is essentially a library of internal helper functions for
  4567. * low-level ATA host controller drivers. As such, the API/ABI is
  4568. * likely to change as new drivers are added and updated.
  4569. * Do not depend on ABI/API stability.
  4570. */
  4571. EXPORT_SYMBOL_GPL(ata_std_bios_param);
  4572. EXPORT_SYMBOL_GPL(ata_std_ports);
  4573. EXPORT_SYMBOL_GPL(ata_device_add);
  4574. EXPORT_SYMBOL_GPL(ata_host_set_remove);
  4575. EXPORT_SYMBOL_GPL(ata_sg_init);
  4576. EXPORT_SYMBOL_GPL(ata_sg_init_one);
  4577. EXPORT_SYMBOL_GPL(ata_qc_complete);
  4578. EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
  4579. EXPORT_SYMBOL_GPL(ata_eng_timeout);
  4580. EXPORT_SYMBOL_GPL(ata_tf_load);
  4581. EXPORT_SYMBOL_GPL(ata_tf_read);
  4582. EXPORT_SYMBOL_GPL(ata_noop_dev_select);
  4583. EXPORT_SYMBOL_GPL(ata_std_dev_select);
  4584. EXPORT_SYMBOL_GPL(ata_tf_to_fis);
  4585. EXPORT_SYMBOL_GPL(ata_tf_from_fis);
  4586. EXPORT_SYMBOL_GPL(ata_check_status);
  4587. EXPORT_SYMBOL_GPL(ata_altstatus);
  4588. EXPORT_SYMBOL_GPL(ata_exec_command);
  4589. EXPORT_SYMBOL_GPL(ata_port_start);
  4590. EXPORT_SYMBOL_GPL(ata_port_stop);
  4591. EXPORT_SYMBOL_GPL(ata_host_stop);
  4592. EXPORT_SYMBOL_GPL(ata_interrupt);
  4593. EXPORT_SYMBOL_GPL(ata_qc_prep);
  4594. EXPORT_SYMBOL_GPL(ata_bmdma_setup);
  4595. EXPORT_SYMBOL_GPL(ata_bmdma_start);
  4596. EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
  4597. EXPORT_SYMBOL_GPL(ata_bmdma_status);
  4598. EXPORT_SYMBOL_GPL(ata_bmdma_stop);
  4599. EXPORT_SYMBOL_GPL(ata_port_probe);
  4600. EXPORT_SYMBOL_GPL(sata_phy_reset);
  4601. EXPORT_SYMBOL_GPL(__sata_phy_reset);
  4602. EXPORT_SYMBOL_GPL(ata_bus_reset);
  4603. EXPORT_SYMBOL_GPL(ata_port_disable);
  4604. EXPORT_SYMBOL_GPL(ata_ratelimit);
  4605. EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
  4606. EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
  4607. EXPORT_SYMBOL_GPL(ata_scsi_error);
  4608. EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
  4609. EXPORT_SYMBOL_GPL(ata_scsi_release);
  4610. EXPORT_SYMBOL_GPL(ata_host_intr);
  4611. EXPORT_SYMBOL_GPL(ata_dev_classify);
  4612. EXPORT_SYMBOL_GPL(ata_dev_id_string);
  4613. EXPORT_SYMBOL_GPL(ata_dev_config);
  4614. EXPORT_SYMBOL_GPL(ata_scsi_simulate);
  4615. EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
  4616. EXPORT_SYMBOL_GPL(ata_timing_compute);
  4617. EXPORT_SYMBOL_GPL(ata_timing_merge);
  4618. #ifdef CONFIG_PCI
  4619. EXPORT_SYMBOL_GPL(pci_test_config_bits);
  4620. EXPORT_SYMBOL_GPL(ata_pci_host_stop);
  4621. EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
  4622. EXPORT_SYMBOL_GPL(ata_pci_init_one);
  4623. EXPORT_SYMBOL_GPL(ata_pci_remove_one);
  4624. EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
  4625. EXPORT_SYMBOL_GPL(ata_pci_device_resume);
  4626. #endif /* CONFIG_PCI */
  4627. EXPORT_SYMBOL_GPL(ata_device_suspend);
  4628. EXPORT_SYMBOL_GPL(ata_device_resume);
  4629. EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
  4630. EXPORT_SYMBOL_GPL(ata_scsi_device_resume);