libata-core.c 121 KB

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