libata-core.c 118 KB

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