libata-core.c 118 KB

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