libata-core.c 117 KB

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