libata-core.c 118 KB

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