libata-core.c 120 KB

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