libata-core.c 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927
  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. * NOTE!!! Do not use this function as probeinit if a low level
  1636. * driver implements only hardreset. Just pass NULL as probeinit
  1637. * in that case. Using this function is probably okay but doing
  1638. * so makes reset sequence different from the original
  1639. * ->phy_reset implementation and Jeff nervous. :-P
  1640. */
  1641. extern void ata_std_probeinit(struct ata_port *ap)
  1642. {
  1643. if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
  1644. sata_phy_resume(ap);
  1645. if (sata_dev_present(ap))
  1646. ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  1647. }
  1648. }
  1649. /**
  1650. * ata_std_softreset - reset host port via ATA SRST
  1651. * @ap: port to reset
  1652. * @verbose: fail verbosely
  1653. * @classes: resulting classes of attached devices
  1654. *
  1655. * Reset host port using ATA SRST. This function is to be used
  1656. * as standard callback for ata_drive_*_reset() functions.
  1657. *
  1658. * LOCKING:
  1659. * Kernel thread context (may sleep)
  1660. *
  1661. * RETURNS:
  1662. * 0 on success, -errno otherwise.
  1663. */
  1664. int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
  1665. {
  1666. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  1667. unsigned int devmask = 0, err_mask;
  1668. u8 err;
  1669. DPRINTK("ENTER\n");
  1670. if (ap->ops->scr_read && !sata_dev_present(ap)) {
  1671. classes[0] = ATA_DEV_NONE;
  1672. goto out;
  1673. }
  1674. /* determine if device 0/1 are present */
  1675. if (ata_devchk(ap, 0))
  1676. devmask |= (1 << 0);
  1677. if (slave_possible && ata_devchk(ap, 1))
  1678. devmask |= (1 << 1);
  1679. /* select device 0 again */
  1680. ap->ops->dev_select(ap, 0);
  1681. /* issue bus reset */
  1682. DPRINTK("about to softreset, devmask=%x\n", devmask);
  1683. err_mask = ata_bus_softreset(ap, devmask);
  1684. if (err_mask) {
  1685. if (verbose)
  1686. printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
  1687. ap->id, err_mask);
  1688. else
  1689. DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
  1690. err_mask);
  1691. return -EIO;
  1692. }
  1693. /* determine by signature whether we have ATA or ATAPI devices */
  1694. classes[0] = ata_dev_try_classify(ap, 0, &err);
  1695. if (slave_possible && err != 0x81)
  1696. classes[1] = ata_dev_try_classify(ap, 1, &err);
  1697. out:
  1698. DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
  1699. return 0;
  1700. }
  1701. /**
  1702. * sata_std_hardreset - reset host port via SATA phy reset
  1703. * @ap: port to reset
  1704. * @verbose: fail verbosely
  1705. * @class: resulting class of attached device
  1706. *
  1707. * SATA phy-reset host port using DET bits of SControl register.
  1708. * This function is to be used as standard callback for
  1709. * ata_drive_*_reset().
  1710. *
  1711. * LOCKING:
  1712. * Kernel thread context (may sleep)
  1713. *
  1714. * RETURNS:
  1715. * 0 on success, -errno otherwise.
  1716. */
  1717. int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
  1718. {
  1719. DPRINTK("ENTER\n");
  1720. /* Issue phy wake/reset */
  1721. scr_write_flush(ap, SCR_CONTROL, 0x301);
  1722. /*
  1723. * Couldn't find anything in SATA I/II specs, but AHCI-1.1
  1724. * 10.4.2 says at least 1 ms.
  1725. */
  1726. msleep(1);
  1727. /* Bring phy back */
  1728. sata_phy_resume(ap);
  1729. /* TODO: phy layer with polling, timeouts, etc. */
  1730. if (!sata_dev_present(ap)) {
  1731. *class = ATA_DEV_NONE;
  1732. DPRINTK("EXIT, link offline\n");
  1733. return 0;
  1734. }
  1735. if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
  1736. if (verbose)
  1737. printk(KERN_ERR "ata%u: COMRESET failed "
  1738. "(device not ready)\n", ap->id);
  1739. else
  1740. DPRINTK("EXIT, device not ready\n");
  1741. return -EIO;
  1742. }
  1743. ap->ops->dev_select(ap, 0); /* probably unnecessary */
  1744. *class = ata_dev_try_classify(ap, 0, NULL);
  1745. DPRINTK("EXIT, class=%u\n", *class);
  1746. return 0;
  1747. }
  1748. /**
  1749. * ata_std_postreset - standard postreset callback
  1750. * @ap: the target ata_port
  1751. * @classes: classes of attached devices
  1752. *
  1753. * This function is invoked after a successful reset. Note that
  1754. * the device might have been reset more than once using
  1755. * different reset methods before postreset is invoked.
  1756. * postreset is also reponsible for setting cable type.
  1757. *
  1758. * This function is to be used as standard callback for
  1759. * ata_drive_*_reset().
  1760. *
  1761. * LOCKING:
  1762. * Kernel thread context (may sleep)
  1763. */
  1764. void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
  1765. {
  1766. DPRINTK("ENTER\n");
  1767. /* set cable type */
  1768. if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
  1769. ap->cbl = ATA_CBL_SATA;
  1770. /* print link status */
  1771. if (ap->cbl == ATA_CBL_SATA)
  1772. sata_print_link_status(ap);
  1773. /* re-enable interrupts */
  1774. if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
  1775. ata_irq_on(ap);
  1776. /* is double-select really necessary? */
  1777. if (classes[0] != ATA_DEV_NONE)
  1778. ap->ops->dev_select(ap, 1);
  1779. if (classes[1] != ATA_DEV_NONE)
  1780. ap->ops->dev_select(ap, 0);
  1781. /* bail out if no device is present */
  1782. if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
  1783. DPRINTK("EXIT, no device\n");
  1784. return;
  1785. }
  1786. /* set up device control */
  1787. if (ap->ioaddr.ctl_addr) {
  1788. if (ap->flags & ATA_FLAG_MMIO)
  1789. writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
  1790. else
  1791. outb(ap->ctl, ap->ioaddr.ctl_addr);
  1792. }
  1793. DPRINTK("EXIT\n");
  1794. }
  1795. /**
  1796. * ata_std_probe_reset - standard probe reset method
  1797. * @ap: prot to perform probe-reset
  1798. * @classes: resulting classes of attached devices
  1799. *
  1800. * The stock off-the-shelf ->probe_reset method.
  1801. *
  1802. * LOCKING:
  1803. * Kernel thread context (may sleep)
  1804. *
  1805. * RETURNS:
  1806. * 0 on success, -errno otherwise.
  1807. */
  1808. int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
  1809. {
  1810. ata_reset_fn_t hardreset;
  1811. hardreset = NULL;
  1812. if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
  1813. hardreset = sata_std_hardreset;
  1814. return ata_drive_probe_reset(ap, ata_std_probeinit,
  1815. ata_std_softreset, hardreset,
  1816. ata_std_postreset, classes);
  1817. }
  1818. static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
  1819. ata_postreset_fn_t postreset,
  1820. unsigned int *classes)
  1821. {
  1822. int i, rc;
  1823. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1824. classes[i] = ATA_DEV_UNKNOWN;
  1825. rc = reset(ap, 0, classes);
  1826. if (rc)
  1827. return rc;
  1828. /* If any class isn't ATA_DEV_UNKNOWN, consider classification
  1829. * is complete and convert all ATA_DEV_UNKNOWN to
  1830. * ATA_DEV_NONE.
  1831. */
  1832. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1833. if (classes[i] != ATA_DEV_UNKNOWN)
  1834. break;
  1835. if (i < ATA_MAX_DEVICES)
  1836. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1837. if (classes[i] == ATA_DEV_UNKNOWN)
  1838. classes[i] = ATA_DEV_NONE;
  1839. if (postreset)
  1840. postreset(ap, classes);
  1841. return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
  1842. }
  1843. /**
  1844. * ata_drive_probe_reset - Perform probe reset with given methods
  1845. * @ap: port to reset
  1846. * @probeinit: probeinit method (can be NULL)
  1847. * @softreset: softreset method (can be NULL)
  1848. * @hardreset: hardreset method (can be NULL)
  1849. * @postreset: postreset method (can be NULL)
  1850. * @classes: resulting classes of attached devices
  1851. *
  1852. * Reset the specified port and classify attached devices using
  1853. * given methods. This function prefers softreset but tries all
  1854. * possible reset sequences to reset and classify devices. This
  1855. * function is intended to be used for constructing ->probe_reset
  1856. * callback by low level drivers.
  1857. *
  1858. * Reset methods should follow the following rules.
  1859. *
  1860. * - Return 0 on sucess, -errno on failure.
  1861. * - If classification is supported, fill classes[] with
  1862. * recognized class codes.
  1863. * - If classification is not supported, leave classes[] alone.
  1864. * - If verbose is non-zero, print error message on failure;
  1865. * otherwise, shut up.
  1866. *
  1867. * LOCKING:
  1868. * Kernel thread context (may sleep)
  1869. *
  1870. * RETURNS:
  1871. * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
  1872. * if classification fails, and any error code from reset
  1873. * methods.
  1874. */
  1875. int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
  1876. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  1877. ata_postreset_fn_t postreset, unsigned int *classes)
  1878. {
  1879. int rc = -EINVAL;
  1880. if (probeinit)
  1881. probeinit(ap);
  1882. if (softreset) {
  1883. rc = do_probe_reset(ap, softreset, postreset, classes);
  1884. if (rc == 0)
  1885. return 0;
  1886. }
  1887. if (!hardreset)
  1888. return rc;
  1889. rc = do_probe_reset(ap, hardreset, postreset, classes);
  1890. if (rc == 0 || rc != -ENODEV)
  1891. return rc;
  1892. if (softreset)
  1893. rc = do_probe_reset(ap, softreset, postreset, classes);
  1894. return rc;
  1895. }
  1896. static void ata_pr_blacklisted(const struct ata_port *ap,
  1897. const struct ata_device *dev)
  1898. {
  1899. printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
  1900. ap->id, dev->devno);
  1901. }
  1902. static const char * const ata_dma_blacklist [] = {
  1903. "WDC AC11000H",
  1904. "WDC AC22100H",
  1905. "WDC AC32500H",
  1906. "WDC AC33100H",
  1907. "WDC AC31600H",
  1908. "WDC AC32100H",
  1909. "WDC AC23200L",
  1910. "Compaq CRD-8241B",
  1911. "CRD-8400B",
  1912. "CRD-8480B",
  1913. "CRD-8482B",
  1914. "CRD-84",
  1915. "SanDisk SDP3B",
  1916. "SanDisk SDP3B-64",
  1917. "SANYO CD-ROM CRD",
  1918. "HITACHI CDR-8",
  1919. "HITACHI CDR-8335",
  1920. "HITACHI CDR-8435",
  1921. "Toshiba CD-ROM XM-6202B",
  1922. "TOSHIBA CD-ROM XM-1702BC",
  1923. "CD-532E-A",
  1924. "E-IDE CD-ROM CR-840",
  1925. "CD-ROM Drive/F5A",
  1926. "WPI CDD-820",
  1927. "SAMSUNG CD-ROM SC-148C",
  1928. "SAMSUNG CD-ROM SC",
  1929. "SanDisk SDP3B-64",
  1930. "ATAPI CD-ROM DRIVE 40X MAXIMUM",
  1931. "_NEC DV5800A",
  1932. };
  1933. static int ata_dma_blacklisted(const struct ata_device *dev)
  1934. {
  1935. unsigned char model_num[40];
  1936. char *s;
  1937. unsigned int len;
  1938. int i;
  1939. ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
  1940. sizeof(model_num));
  1941. s = &model_num[0];
  1942. len = strnlen(s, sizeof(model_num));
  1943. /* ATAPI specifies that empty space is blank-filled; remove blanks */
  1944. while ((len > 0) && (s[len - 1] == ' ')) {
  1945. len--;
  1946. s[len] = 0;
  1947. }
  1948. for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
  1949. if (!strncmp(ata_dma_blacklist[i], s, len))
  1950. return 1;
  1951. return 0;
  1952. }
  1953. static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
  1954. {
  1955. const struct ata_device *master, *slave;
  1956. unsigned int mask;
  1957. master = &ap->device[0];
  1958. slave = &ap->device[1];
  1959. assert (ata_dev_present(master) || ata_dev_present(slave));
  1960. if (shift == ATA_SHIFT_UDMA) {
  1961. mask = ap->udma_mask;
  1962. if (ata_dev_present(master)) {
  1963. mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
  1964. if (ata_dma_blacklisted(master)) {
  1965. mask = 0;
  1966. ata_pr_blacklisted(ap, master);
  1967. }
  1968. }
  1969. if (ata_dev_present(slave)) {
  1970. mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
  1971. if (ata_dma_blacklisted(slave)) {
  1972. mask = 0;
  1973. ata_pr_blacklisted(ap, slave);
  1974. }
  1975. }
  1976. }
  1977. else if (shift == ATA_SHIFT_MWDMA) {
  1978. mask = ap->mwdma_mask;
  1979. if (ata_dev_present(master)) {
  1980. mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
  1981. if (ata_dma_blacklisted(master)) {
  1982. mask = 0;
  1983. ata_pr_blacklisted(ap, master);
  1984. }
  1985. }
  1986. if (ata_dev_present(slave)) {
  1987. mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
  1988. if (ata_dma_blacklisted(slave)) {
  1989. mask = 0;
  1990. ata_pr_blacklisted(ap, slave);
  1991. }
  1992. }
  1993. }
  1994. else if (shift == ATA_SHIFT_PIO) {
  1995. mask = ap->pio_mask;
  1996. if (ata_dev_present(master)) {
  1997. /* spec doesn't return explicit support for
  1998. * PIO0-2, so we fake it
  1999. */
  2000. u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
  2001. tmp_mode <<= 3;
  2002. tmp_mode |= 0x7;
  2003. mask &= tmp_mode;
  2004. }
  2005. if (ata_dev_present(slave)) {
  2006. /* spec doesn't return explicit support for
  2007. * PIO0-2, so we fake it
  2008. */
  2009. u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
  2010. tmp_mode <<= 3;
  2011. tmp_mode |= 0x7;
  2012. mask &= tmp_mode;
  2013. }
  2014. }
  2015. else {
  2016. mask = 0xffffffff; /* shut up compiler warning */
  2017. BUG();
  2018. }
  2019. return mask;
  2020. }
  2021. /* find greatest bit */
  2022. static int fgb(u32 bitmap)
  2023. {
  2024. unsigned int i;
  2025. int x = -1;
  2026. for (i = 0; i < 32; i++)
  2027. if (bitmap & (1 << i))
  2028. x = i;
  2029. return x;
  2030. }
  2031. /**
  2032. * ata_choose_xfer_mode - attempt to find best transfer mode
  2033. * @ap: Port for which an xfer mode will be selected
  2034. * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
  2035. * @xfer_shift_out: (output) bit shift that selects this mode
  2036. *
  2037. * Based on host and device capabilities, determine the
  2038. * maximum transfer mode that is amenable to all.
  2039. *
  2040. * LOCKING:
  2041. * PCI/etc. bus probe sem.
  2042. *
  2043. * RETURNS:
  2044. * Zero on success, negative on error.
  2045. */
  2046. static int ata_choose_xfer_mode(const struct ata_port *ap,
  2047. u8 *xfer_mode_out,
  2048. unsigned int *xfer_shift_out)
  2049. {
  2050. unsigned int mask, shift;
  2051. int x, i;
  2052. for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
  2053. shift = xfer_mode_classes[i].shift;
  2054. mask = ata_get_mode_mask(ap, shift);
  2055. x = fgb(mask);
  2056. if (x >= 0) {
  2057. *xfer_mode_out = xfer_mode_classes[i].base + x;
  2058. *xfer_shift_out = shift;
  2059. return 0;
  2060. }
  2061. }
  2062. return -1;
  2063. }
  2064. /**
  2065. * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
  2066. * @ap: Port associated with device @dev
  2067. * @dev: Device to which command will be sent
  2068. *
  2069. * Issue SET FEATURES - XFER MODE command to device @dev
  2070. * on port @ap.
  2071. *
  2072. * LOCKING:
  2073. * PCI/etc. bus probe sem.
  2074. */
  2075. static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
  2076. {
  2077. struct ata_taskfile tf;
  2078. /* set up set-features taskfile */
  2079. DPRINTK("set features - xfer mode\n");
  2080. ata_tf_init(ap, &tf, dev->devno);
  2081. tf.command = ATA_CMD_SET_FEATURES;
  2082. tf.feature = SETFEATURES_XFER;
  2083. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2084. tf.protocol = ATA_PROT_NODATA;
  2085. tf.nsect = dev->xfer_mode;
  2086. if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
  2087. printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
  2088. ap->id);
  2089. ata_port_disable(ap);
  2090. }
  2091. DPRINTK("EXIT\n");
  2092. }
  2093. /**
  2094. * ata_dev_reread_id - Reread the device identify device info
  2095. * @ap: port where the device is
  2096. * @dev: device to reread the identify device info
  2097. *
  2098. * LOCKING:
  2099. */
  2100. static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
  2101. {
  2102. struct ata_taskfile tf;
  2103. ata_tf_init(ap, &tf, dev->devno);
  2104. if (dev->class == ATA_DEV_ATA) {
  2105. tf.command = ATA_CMD_ID_ATA;
  2106. DPRINTK("do ATA identify\n");
  2107. } else {
  2108. tf.command = ATA_CMD_ID_ATAPI;
  2109. DPRINTK("do ATAPI identify\n");
  2110. }
  2111. tf.flags |= ATA_TFLAG_DEVICE;
  2112. tf.protocol = ATA_PROT_PIO;
  2113. if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
  2114. dev->id, sizeof(dev->id)))
  2115. goto err_out;
  2116. swap_buf_le16(dev->id, ATA_ID_WORDS);
  2117. ata_dump_id(dev);
  2118. DPRINTK("EXIT\n");
  2119. return;
  2120. err_out:
  2121. printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
  2122. ata_port_disable(ap);
  2123. }
  2124. /**
  2125. * ata_dev_init_params - Issue INIT DEV PARAMS command
  2126. * @ap: Port associated with device @dev
  2127. * @dev: Device to which command will be sent
  2128. *
  2129. * LOCKING:
  2130. */
  2131. static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
  2132. {
  2133. struct ata_taskfile tf;
  2134. u16 sectors = dev->id[6];
  2135. u16 heads = dev->id[3];
  2136. /* Number of sectors per track 1-255. Number of heads 1-16 */
  2137. if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
  2138. return;
  2139. /* set up init dev params taskfile */
  2140. DPRINTK("init dev params \n");
  2141. ata_tf_init(ap, &tf, dev->devno);
  2142. tf.command = ATA_CMD_INIT_DEV_PARAMS;
  2143. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2144. tf.protocol = ATA_PROT_NODATA;
  2145. tf.nsect = sectors;
  2146. tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
  2147. if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
  2148. printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
  2149. ap->id);
  2150. ata_port_disable(ap);
  2151. }
  2152. DPRINTK("EXIT\n");
  2153. }
  2154. /**
  2155. * ata_sg_clean - Unmap DMA memory associated with command
  2156. * @qc: Command containing DMA memory to be released
  2157. *
  2158. * Unmap all mapped DMA memory associated with this command.
  2159. *
  2160. * LOCKING:
  2161. * spin_lock_irqsave(host_set lock)
  2162. */
  2163. static void ata_sg_clean(struct ata_queued_cmd *qc)
  2164. {
  2165. struct ata_port *ap = qc->ap;
  2166. struct scatterlist *sg = qc->__sg;
  2167. int dir = qc->dma_dir;
  2168. void *pad_buf = NULL;
  2169. assert(qc->flags & ATA_QCFLAG_DMAMAP);
  2170. assert(sg != NULL);
  2171. if (qc->flags & ATA_QCFLAG_SINGLE)
  2172. assert(qc->n_elem == 1);
  2173. VPRINTK("unmapping %u sg elements\n", qc->n_elem);
  2174. /* if we padded the buffer out to 32-bit bound, and data
  2175. * xfer direction is from-device, we must copy from the
  2176. * pad buffer back into the supplied buffer
  2177. */
  2178. if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
  2179. pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  2180. if (qc->flags & ATA_QCFLAG_SG) {
  2181. if (qc->n_elem)
  2182. dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
  2183. /* restore last sg */
  2184. sg[qc->orig_n_elem - 1].length += qc->pad_len;
  2185. if (pad_buf) {
  2186. struct scatterlist *psg = &qc->pad_sgent;
  2187. void *addr = kmap_atomic(psg->page, KM_IRQ0);
  2188. memcpy(addr + psg->offset, pad_buf, qc->pad_len);
  2189. kunmap_atomic(addr, KM_IRQ0);
  2190. }
  2191. } else {
  2192. if (sg_dma_len(&sg[0]) > 0)
  2193. dma_unmap_single(ap->host_set->dev,
  2194. sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
  2195. dir);
  2196. /* restore sg */
  2197. sg->length += qc->pad_len;
  2198. if (pad_buf)
  2199. memcpy(qc->buf_virt + sg->length - qc->pad_len,
  2200. pad_buf, qc->pad_len);
  2201. }
  2202. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  2203. qc->__sg = NULL;
  2204. }
  2205. /**
  2206. * ata_fill_sg - Fill PCI IDE PRD table
  2207. * @qc: Metadata associated with taskfile to be transferred
  2208. *
  2209. * Fill PCI IDE PRD (scatter-gather) table with segments
  2210. * associated with the current disk command.
  2211. *
  2212. * LOCKING:
  2213. * spin_lock_irqsave(host_set lock)
  2214. *
  2215. */
  2216. static void ata_fill_sg(struct ata_queued_cmd *qc)
  2217. {
  2218. struct ata_port *ap = qc->ap;
  2219. struct scatterlist *sg;
  2220. unsigned int idx;
  2221. assert(qc->__sg != NULL);
  2222. assert(qc->n_elem > 0);
  2223. idx = 0;
  2224. ata_for_each_sg(sg, qc) {
  2225. u32 addr, offset;
  2226. u32 sg_len, len;
  2227. /* determine if physical DMA addr spans 64K boundary.
  2228. * Note h/w doesn't support 64-bit, so we unconditionally
  2229. * truncate dma_addr_t to u32.
  2230. */
  2231. addr = (u32) sg_dma_address(sg);
  2232. sg_len = sg_dma_len(sg);
  2233. while (sg_len) {
  2234. offset = addr & 0xffff;
  2235. len = sg_len;
  2236. if ((offset + sg_len) > 0x10000)
  2237. len = 0x10000 - offset;
  2238. ap->prd[idx].addr = cpu_to_le32(addr);
  2239. ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
  2240. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
  2241. idx++;
  2242. sg_len -= len;
  2243. addr += len;
  2244. }
  2245. }
  2246. if (idx)
  2247. ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
  2248. }
  2249. /**
  2250. * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
  2251. * @qc: Metadata associated with taskfile to check
  2252. *
  2253. * Allow low-level driver to filter ATA PACKET commands, returning
  2254. * a status indicating whether or not it is OK to use DMA for the
  2255. * supplied PACKET command.
  2256. *
  2257. * LOCKING:
  2258. * spin_lock_irqsave(host_set lock)
  2259. *
  2260. * RETURNS: 0 when ATAPI DMA can be used
  2261. * nonzero otherwise
  2262. */
  2263. int ata_check_atapi_dma(struct ata_queued_cmd *qc)
  2264. {
  2265. struct ata_port *ap = qc->ap;
  2266. int rc = 0; /* Assume ATAPI DMA is OK by default */
  2267. if (ap->ops->check_atapi_dma)
  2268. rc = ap->ops->check_atapi_dma(qc);
  2269. return rc;
  2270. }
  2271. /**
  2272. * ata_qc_prep - Prepare taskfile for submission
  2273. * @qc: Metadata associated with taskfile to be prepared
  2274. *
  2275. * Prepare ATA taskfile for submission.
  2276. *
  2277. * LOCKING:
  2278. * spin_lock_irqsave(host_set lock)
  2279. */
  2280. void ata_qc_prep(struct ata_queued_cmd *qc)
  2281. {
  2282. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  2283. return;
  2284. ata_fill_sg(qc);
  2285. }
  2286. /**
  2287. * ata_sg_init_one - Associate command with memory buffer
  2288. * @qc: Command to be associated
  2289. * @buf: Memory buffer
  2290. * @buflen: Length of memory buffer, in bytes.
  2291. *
  2292. * Initialize the data-related elements of queued_cmd @qc
  2293. * to point to a single memory buffer, @buf of byte length @buflen.
  2294. *
  2295. * LOCKING:
  2296. * spin_lock_irqsave(host_set lock)
  2297. */
  2298. void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
  2299. {
  2300. struct scatterlist *sg;
  2301. qc->flags |= ATA_QCFLAG_SINGLE;
  2302. memset(&qc->sgent, 0, sizeof(qc->sgent));
  2303. qc->__sg = &qc->sgent;
  2304. qc->n_elem = 1;
  2305. qc->orig_n_elem = 1;
  2306. qc->buf_virt = buf;
  2307. sg = qc->__sg;
  2308. sg_init_one(sg, buf, buflen);
  2309. }
  2310. /**
  2311. * ata_sg_init - Associate command with scatter-gather table.
  2312. * @qc: Command to be associated
  2313. * @sg: Scatter-gather table.
  2314. * @n_elem: Number of elements in s/g table.
  2315. *
  2316. * Initialize the data-related elements of queued_cmd @qc
  2317. * to point to a scatter-gather table @sg, containing @n_elem
  2318. * elements.
  2319. *
  2320. * LOCKING:
  2321. * spin_lock_irqsave(host_set lock)
  2322. */
  2323. void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
  2324. unsigned int n_elem)
  2325. {
  2326. qc->flags |= ATA_QCFLAG_SG;
  2327. qc->__sg = sg;
  2328. qc->n_elem = n_elem;
  2329. qc->orig_n_elem = n_elem;
  2330. }
  2331. /**
  2332. * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
  2333. * @qc: Command with memory buffer to be mapped.
  2334. *
  2335. * DMA-map the memory buffer associated with queued_cmd @qc.
  2336. *
  2337. * LOCKING:
  2338. * spin_lock_irqsave(host_set lock)
  2339. *
  2340. * RETURNS:
  2341. * Zero on success, negative on error.
  2342. */
  2343. static int ata_sg_setup_one(struct ata_queued_cmd *qc)
  2344. {
  2345. struct ata_port *ap = qc->ap;
  2346. int dir = qc->dma_dir;
  2347. struct scatterlist *sg = qc->__sg;
  2348. dma_addr_t dma_address;
  2349. /* we must lengthen transfers to end on a 32-bit boundary */
  2350. qc->pad_len = sg->length & 3;
  2351. if (qc->pad_len) {
  2352. void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  2353. struct scatterlist *psg = &qc->pad_sgent;
  2354. assert(qc->dev->class == ATA_DEV_ATAPI);
  2355. memset(pad_buf, 0, ATA_DMA_PAD_SZ);
  2356. if (qc->tf.flags & ATA_TFLAG_WRITE)
  2357. memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
  2358. qc->pad_len);
  2359. sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
  2360. sg_dma_len(psg) = ATA_DMA_PAD_SZ;
  2361. /* trim sg */
  2362. sg->length -= qc->pad_len;
  2363. DPRINTK("padding done, sg->length=%u pad_len=%u\n",
  2364. sg->length, qc->pad_len);
  2365. }
  2366. if (!sg->length) {
  2367. sg_dma_address(sg) = 0;
  2368. goto skip_map;
  2369. }
  2370. dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
  2371. sg->length, dir);
  2372. if (dma_mapping_error(dma_address)) {
  2373. /* restore sg */
  2374. sg->length += qc->pad_len;
  2375. return -1;
  2376. }
  2377. sg_dma_address(sg) = dma_address;
  2378. skip_map:
  2379. sg_dma_len(sg) = sg->length;
  2380. DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
  2381. qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  2382. return 0;
  2383. }
  2384. /**
  2385. * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
  2386. * @qc: Command with scatter-gather table to be mapped.
  2387. *
  2388. * DMA-map the scatter-gather table associated with queued_cmd @qc.
  2389. *
  2390. * LOCKING:
  2391. * spin_lock_irqsave(host_set lock)
  2392. *
  2393. * RETURNS:
  2394. * Zero on success, negative on error.
  2395. *
  2396. */
  2397. static int ata_sg_setup(struct ata_queued_cmd *qc)
  2398. {
  2399. struct ata_port *ap = qc->ap;
  2400. struct scatterlist *sg = qc->__sg;
  2401. struct scatterlist *lsg = &sg[qc->n_elem - 1];
  2402. int n_elem, pre_n_elem, dir, trim_sg = 0;
  2403. VPRINTK("ENTER, ata%u\n", ap->id);
  2404. assert(qc->flags & ATA_QCFLAG_SG);
  2405. /* we must lengthen transfers to end on a 32-bit boundary */
  2406. qc->pad_len = lsg->length & 3;
  2407. if (qc->pad_len) {
  2408. void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
  2409. struct scatterlist *psg = &qc->pad_sgent;
  2410. unsigned int offset;
  2411. assert(qc->dev->class == ATA_DEV_ATAPI);
  2412. memset(pad_buf, 0, ATA_DMA_PAD_SZ);
  2413. /*
  2414. * psg->page/offset are used to copy to-be-written
  2415. * data in this function or read data in ata_sg_clean.
  2416. */
  2417. offset = lsg->offset + lsg->length - qc->pad_len;
  2418. psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
  2419. psg->offset = offset_in_page(offset);
  2420. if (qc->tf.flags & ATA_TFLAG_WRITE) {
  2421. void *addr = kmap_atomic(psg->page, KM_IRQ0);
  2422. memcpy(pad_buf, addr + psg->offset, qc->pad_len);
  2423. kunmap_atomic(addr, KM_IRQ0);
  2424. }
  2425. sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
  2426. sg_dma_len(psg) = ATA_DMA_PAD_SZ;
  2427. /* trim last sg */
  2428. lsg->length -= qc->pad_len;
  2429. if (lsg->length == 0)
  2430. trim_sg = 1;
  2431. DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
  2432. qc->n_elem - 1, lsg->length, qc->pad_len);
  2433. }
  2434. pre_n_elem = qc->n_elem;
  2435. if (trim_sg && pre_n_elem)
  2436. pre_n_elem--;
  2437. if (!pre_n_elem) {
  2438. n_elem = 0;
  2439. goto skip_map;
  2440. }
  2441. dir = qc->dma_dir;
  2442. n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
  2443. if (n_elem < 1) {
  2444. /* restore last sg */
  2445. lsg->length += qc->pad_len;
  2446. return -1;
  2447. }
  2448. DPRINTK("%d sg elements mapped\n", n_elem);
  2449. skip_map:
  2450. qc->n_elem = n_elem;
  2451. return 0;
  2452. }
  2453. /**
  2454. * ata_poll_qc_complete - turn irq back on and finish qc
  2455. * @qc: Command to complete
  2456. * @err_mask: ATA status register content
  2457. *
  2458. * LOCKING:
  2459. * None. (grabs host lock)
  2460. */
  2461. void ata_poll_qc_complete(struct ata_queued_cmd *qc)
  2462. {
  2463. struct ata_port *ap = qc->ap;
  2464. unsigned long flags;
  2465. spin_lock_irqsave(&ap->host_set->lock, flags);
  2466. ap->flags &= ~ATA_FLAG_NOINTR;
  2467. ata_irq_on(ap);
  2468. ata_qc_complete(qc);
  2469. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  2470. }
  2471. /**
  2472. * ata_pio_poll - poll using PIO, depending on current state
  2473. * @ap: the target ata_port
  2474. *
  2475. * LOCKING:
  2476. * None. (executing in kernel thread context)
  2477. *
  2478. * RETURNS:
  2479. * timeout value to use
  2480. */
  2481. static unsigned long ata_pio_poll(struct ata_port *ap)
  2482. {
  2483. struct ata_queued_cmd *qc;
  2484. u8 status;
  2485. unsigned int poll_state = HSM_ST_UNKNOWN;
  2486. unsigned int reg_state = HSM_ST_UNKNOWN;
  2487. qc = ata_qc_from_tag(ap, ap->active_tag);
  2488. assert(qc != NULL);
  2489. switch (ap->hsm_task_state) {
  2490. case HSM_ST:
  2491. case HSM_ST_POLL:
  2492. poll_state = HSM_ST_POLL;
  2493. reg_state = HSM_ST;
  2494. break;
  2495. case HSM_ST_LAST:
  2496. case HSM_ST_LAST_POLL:
  2497. poll_state = HSM_ST_LAST_POLL;
  2498. reg_state = HSM_ST_LAST;
  2499. break;
  2500. default:
  2501. BUG();
  2502. break;
  2503. }
  2504. status = ata_chk_status(ap);
  2505. if (status & ATA_BUSY) {
  2506. if (time_after(jiffies, ap->pio_task_timeout)) {
  2507. qc->err_mask |= AC_ERR_TIMEOUT;
  2508. ap->hsm_task_state = HSM_ST_TMOUT;
  2509. return 0;
  2510. }
  2511. ap->hsm_task_state = poll_state;
  2512. return ATA_SHORT_PAUSE;
  2513. }
  2514. ap->hsm_task_state = reg_state;
  2515. return 0;
  2516. }
  2517. /**
  2518. * ata_pio_complete - check if drive is busy or idle
  2519. * @ap: the target ata_port
  2520. *
  2521. * LOCKING:
  2522. * None. (executing in kernel thread context)
  2523. *
  2524. * RETURNS:
  2525. * Non-zero if qc completed, zero otherwise.
  2526. */
  2527. static int ata_pio_complete (struct ata_port *ap)
  2528. {
  2529. struct ata_queued_cmd *qc;
  2530. u8 drv_stat;
  2531. /*
  2532. * This is purely heuristic. This is a fast path. Sometimes when
  2533. * we enter, BSY will be cleared in a chk-status or two. If not,
  2534. * the drive is probably seeking or something. Snooze for a couple
  2535. * msecs, then chk-status again. If still busy, fall back to
  2536. * HSM_ST_POLL state.
  2537. */
  2538. drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
  2539. if (drv_stat & ATA_BUSY) {
  2540. msleep(2);
  2541. drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
  2542. if (drv_stat & ATA_BUSY) {
  2543. ap->hsm_task_state = HSM_ST_LAST_POLL;
  2544. ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
  2545. return 0;
  2546. }
  2547. }
  2548. qc = ata_qc_from_tag(ap, ap->active_tag);
  2549. assert(qc != NULL);
  2550. drv_stat = ata_wait_idle(ap);
  2551. if (!ata_ok(drv_stat)) {
  2552. qc->err_mask |= __ac_err_mask(drv_stat);
  2553. ap->hsm_task_state = HSM_ST_ERR;
  2554. return 0;
  2555. }
  2556. ap->hsm_task_state = HSM_ST_IDLE;
  2557. assert(qc->err_mask == 0);
  2558. ata_poll_qc_complete(qc);
  2559. /* another command may start at this point */
  2560. return 1;
  2561. }
  2562. /**
  2563. * swap_buf_le16 - swap halves of 16-bit words in place
  2564. * @buf: Buffer to swap
  2565. * @buf_words: Number of 16-bit words in buffer.
  2566. *
  2567. * Swap halves of 16-bit words if needed to convert from
  2568. * little-endian byte order to native cpu byte order, or
  2569. * vice-versa.
  2570. *
  2571. * LOCKING:
  2572. * Inherited from caller.
  2573. */
  2574. void swap_buf_le16(u16 *buf, unsigned int buf_words)
  2575. {
  2576. #ifdef __BIG_ENDIAN
  2577. unsigned int i;
  2578. for (i = 0; i < buf_words; i++)
  2579. buf[i] = le16_to_cpu(buf[i]);
  2580. #endif /* __BIG_ENDIAN */
  2581. }
  2582. /**
  2583. * ata_mmio_data_xfer - Transfer data by MMIO
  2584. * @ap: port to read/write
  2585. * @buf: data buffer
  2586. * @buflen: buffer length
  2587. * @write_data: read/write
  2588. *
  2589. * Transfer data from/to the device data register by MMIO.
  2590. *
  2591. * LOCKING:
  2592. * Inherited from caller.
  2593. */
  2594. static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
  2595. unsigned int buflen, int write_data)
  2596. {
  2597. unsigned int i;
  2598. unsigned int words = buflen >> 1;
  2599. u16 *buf16 = (u16 *) buf;
  2600. void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
  2601. /* Transfer multiple of 2 bytes */
  2602. if (write_data) {
  2603. for (i = 0; i < words; i++)
  2604. writew(le16_to_cpu(buf16[i]), mmio);
  2605. } else {
  2606. for (i = 0; i < words; i++)
  2607. buf16[i] = cpu_to_le16(readw(mmio));
  2608. }
  2609. /* Transfer trailing 1 byte, if any. */
  2610. if (unlikely(buflen & 0x01)) {
  2611. u16 align_buf[1] = { 0 };
  2612. unsigned char *trailing_buf = buf + buflen - 1;
  2613. if (write_data) {
  2614. memcpy(align_buf, trailing_buf, 1);
  2615. writew(le16_to_cpu(align_buf[0]), mmio);
  2616. } else {
  2617. align_buf[0] = cpu_to_le16(readw(mmio));
  2618. memcpy(trailing_buf, align_buf, 1);
  2619. }
  2620. }
  2621. }
  2622. /**
  2623. * ata_pio_data_xfer - Transfer data by PIO
  2624. * @ap: port to read/write
  2625. * @buf: data buffer
  2626. * @buflen: buffer length
  2627. * @write_data: read/write
  2628. *
  2629. * Transfer data from/to the device data register by PIO.
  2630. *
  2631. * LOCKING:
  2632. * Inherited from caller.
  2633. */
  2634. static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
  2635. unsigned int buflen, int write_data)
  2636. {
  2637. unsigned int words = buflen >> 1;
  2638. /* Transfer multiple of 2 bytes */
  2639. if (write_data)
  2640. outsw(ap->ioaddr.data_addr, buf, words);
  2641. else
  2642. insw(ap->ioaddr.data_addr, buf, words);
  2643. /* Transfer trailing 1 byte, if any. */
  2644. if (unlikely(buflen & 0x01)) {
  2645. u16 align_buf[1] = { 0 };
  2646. unsigned char *trailing_buf = buf + buflen - 1;
  2647. if (write_data) {
  2648. memcpy(align_buf, trailing_buf, 1);
  2649. outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
  2650. } else {
  2651. align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
  2652. memcpy(trailing_buf, align_buf, 1);
  2653. }
  2654. }
  2655. }
  2656. /**
  2657. * ata_data_xfer - Transfer data from/to the data register.
  2658. * @ap: port to read/write
  2659. * @buf: data buffer
  2660. * @buflen: buffer length
  2661. * @do_write: read/write
  2662. *
  2663. * Transfer data from/to the device data register.
  2664. *
  2665. * LOCKING:
  2666. * Inherited from caller.
  2667. */
  2668. static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
  2669. unsigned int buflen, int do_write)
  2670. {
  2671. /* Make the crap hardware pay the costs not the good stuff */
  2672. if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
  2673. unsigned long flags;
  2674. local_irq_save(flags);
  2675. if (ap->flags & ATA_FLAG_MMIO)
  2676. ata_mmio_data_xfer(ap, buf, buflen, do_write);
  2677. else
  2678. ata_pio_data_xfer(ap, buf, buflen, do_write);
  2679. local_irq_restore(flags);
  2680. } else {
  2681. if (ap->flags & ATA_FLAG_MMIO)
  2682. ata_mmio_data_xfer(ap, buf, buflen, do_write);
  2683. else
  2684. ata_pio_data_xfer(ap, buf, buflen, do_write);
  2685. }
  2686. }
  2687. /**
  2688. * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
  2689. * @qc: Command on going
  2690. *
  2691. * Transfer ATA_SECT_SIZE of data from/to the ATA device.
  2692. *
  2693. * LOCKING:
  2694. * Inherited from caller.
  2695. */
  2696. static void ata_pio_sector(struct ata_queued_cmd *qc)
  2697. {
  2698. int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  2699. struct scatterlist *sg = qc->__sg;
  2700. struct ata_port *ap = qc->ap;
  2701. struct page *page;
  2702. unsigned int offset;
  2703. unsigned char *buf;
  2704. if (qc->cursect == (qc->nsect - 1))
  2705. ap->hsm_task_state = HSM_ST_LAST;
  2706. page = sg[qc->cursg].page;
  2707. offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
  2708. /* get the current page and offset */
  2709. page = nth_page(page, (offset >> PAGE_SHIFT));
  2710. offset %= PAGE_SIZE;
  2711. buf = kmap(page) + offset;
  2712. qc->cursect++;
  2713. qc->cursg_ofs++;
  2714. if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
  2715. qc->cursg++;
  2716. qc->cursg_ofs = 0;
  2717. }
  2718. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  2719. /* do the actual data transfer */
  2720. do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  2721. ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
  2722. kunmap(page);
  2723. }
  2724. /**
  2725. * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
  2726. * @qc: Command on going
  2727. * @bytes: number of bytes
  2728. *
  2729. * Transfer Transfer data from/to the ATAPI device.
  2730. *
  2731. * LOCKING:
  2732. * Inherited from caller.
  2733. *
  2734. */
  2735. static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
  2736. {
  2737. int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  2738. struct scatterlist *sg = qc->__sg;
  2739. struct ata_port *ap = qc->ap;
  2740. struct page *page;
  2741. unsigned char *buf;
  2742. unsigned int offset, count;
  2743. if (qc->curbytes + bytes >= qc->nbytes)
  2744. ap->hsm_task_state = HSM_ST_LAST;
  2745. next_sg:
  2746. if (unlikely(qc->cursg >= qc->n_elem)) {
  2747. /*
  2748. * The end of qc->sg is reached and the device expects
  2749. * more data to transfer. In order not to overrun qc->sg
  2750. * and fulfill length specified in the byte count register,
  2751. * - for read case, discard trailing data from the device
  2752. * - for write case, padding zero data to the device
  2753. */
  2754. u16 pad_buf[1] = { 0 };
  2755. unsigned int words = bytes >> 1;
  2756. unsigned int i;
  2757. if (words) /* warning if bytes > 1 */
  2758. printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
  2759. ap->id, bytes);
  2760. for (i = 0; i < words; i++)
  2761. ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
  2762. ap->hsm_task_state = HSM_ST_LAST;
  2763. return;
  2764. }
  2765. sg = &qc->__sg[qc->cursg];
  2766. page = sg->page;
  2767. offset = sg->offset + qc->cursg_ofs;
  2768. /* get the current page and offset */
  2769. page = nth_page(page, (offset >> PAGE_SHIFT));
  2770. offset %= PAGE_SIZE;
  2771. /* don't overrun current sg */
  2772. count = min(sg->length - qc->cursg_ofs, bytes);
  2773. /* don't cross page boundaries */
  2774. count = min(count, (unsigned int)PAGE_SIZE - offset);
  2775. buf = kmap(page) + offset;
  2776. bytes -= count;
  2777. qc->curbytes += count;
  2778. qc->cursg_ofs += count;
  2779. if (qc->cursg_ofs == sg->length) {
  2780. qc->cursg++;
  2781. qc->cursg_ofs = 0;
  2782. }
  2783. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  2784. /* do the actual data transfer */
  2785. ata_data_xfer(ap, buf, count, do_write);
  2786. kunmap(page);
  2787. if (bytes)
  2788. goto next_sg;
  2789. }
  2790. /**
  2791. * atapi_pio_bytes - Transfer data from/to the ATAPI device.
  2792. * @qc: Command on going
  2793. *
  2794. * Transfer Transfer data from/to the ATAPI device.
  2795. *
  2796. * LOCKING:
  2797. * Inherited from caller.
  2798. */
  2799. static void atapi_pio_bytes(struct ata_queued_cmd *qc)
  2800. {
  2801. struct ata_port *ap = qc->ap;
  2802. struct ata_device *dev = qc->dev;
  2803. unsigned int ireason, bc_lo, bc_hi, bytes;
  2804. int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
  2805. ap->ops->tf_read(ap, &qc->tf);
  2806. ireason = qc->tf.nsect;
  2807. bc_lo = qc->tf.lbam;
  2808. bc_hi = qc->tf.lbah;
  2809. bytes = (bc_hi << 8) | bc_lo;
  2810. /* shall be cleared to zero, indicating xfer of data */
  2811. if (ireason & (1 << 0))
  2812. goto err_out;
  2813. /* make sure transfer direction matches expected */
  2814. i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
  2815. if (do_write != i_write)
  2816. goto err_out;
  2817. __atapi_pio_bytes(qc, bytes);
  2818. return;
  2819. err_out:
  2820. printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
  2821. ap->id, dev->devno);
  2822. qc->err_mask |= AC_ERR_HSM;
  2823. ap->hsm_task_state = HSM_ST_ERR;
  2824. }
  2825. /**
  2826. * ata_pio_block - start PIO on a block
  2827. * @ap: the target ata_port
  2828. *
  2829. * LOCKING:
  2830. * None. (executing in kernel thread context)
  2831. */
  2832. static void ata_pio_block(struct ata_port *ap)
  2833. {
  2834. struct ata_queued_cmd *qc;
  2835. u8 status;
  2836. /*
  2837. * This is purely heuristic. This is a fast path.
  2838. * Sometimes when we enter, BSY will be cleared in
  2839. * a chk-status or two. If not, the drive is probably seeking
  2840. * or something. Snooze for a couple msecs, then
  2841. * chk-status again. If still busy, fall back to
  2842. * HSM_ST_POLL state.
  2843. */
  2844. status = ata_busy_wait(ap, ATA_BUSY, 5);
  2845. if (status & ATA_BUSY) {
  2846. msleep(2);
  2847. status = ata_busy_wait(ap, ATA_BUSY, 10);
  2848. if (status & ATA_BUSY) {
  2849. ap->hsm_task_state = HSM_ST_POLL;
  2850. ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
  2851. return;
  2852. }
  2853. }
  2854. qc = ata_qc_from_tag(ap, ap->active_tag);
  2855. assert(qc != NULL);
  2856. /* check error */
  2857. if (status & (ATA_ERR | ATA_DF)) {
  2858. qc->err_mask |= AC_ERR_DEV;
  2859. ap->hsm_task_state = HSM_ST_ERR;
  2860. return;
  2861. }
  2862. /* transfer data if any */
  2863. if (is_atapi_taskfile(&qc->tf)) {
  2864. /* DRQ=0 means no more data to transfer */
  2865. if ((status & ATA_DRQ) == 0) {
  2866. ap->hsm_task_state = HSM_ST_LAST;
  2867. return;
  2868. }
  2869. atapi_pio_bytes(qc);
  2870. } else {
  2871. /* handle BSY=0, DRQ=0 as error */
  2872. if ((status & ATA_DRQ) == 0) {
  2873. qc->err_mask |= AC_ERR_HSM;
  2874. ap->hsm_task_state = HSM_ST_ERR;
  2875. return;
  2876. }
  2877. ata_pio_sector(qc);
  2878. }
  2879. }
  2880. static void ata_pio_error(struct ata_port *ap)
  2881. {
  2882. struct ata_queued_cmd *qc;
  2883. printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
  2884. qc = ata_qc_from_tag(ap, ap->active_tag);
  2885. assert(qc != NULL);
  2886. /* make sure qc->err_mask is available to
  2887. * know what's wrong and recover
  2888. */
  2889. assert(qc->err_mask);
  2890. ap->hsm_task_state = HSM_ST_IDLE;
  2891. ata_poll_qc_complete(qc);
  2892. }
  2893. static void ata_pio_task(void *_data)
  2894. {
  2895. struct ata_port *ap = _data;
  2896. unsigned long timeout;
  2897. int qc_completed;
  2898. fsm_start:
  2899. timeout = 0;
  2900. qc_completed = 0;
  2901. switch (ap->hsm_task_state) {
  2902. case HSM_ST_IDLE:
  2903. return;
  2904. case HSM_ST:
  2905. ata_pio_block(ap);
  2906. break;
  2907. case HSM_ST_LAST:
  2908. qc_completed = ata_pio_complete(ap);
  2909. break;
  2910. case HSM_ST_POLL:
  2911. case HSM_ST_LAST_POLL:
  2912. timeout = ata_pio_poll(ap);
  2913. break;
  2914. case HSM_ST_TMOUT:
  2915. case HSM_ST_ERR:
  2916. ata_pio_error(ap);
  2917. return;
  2918. }
  2919. if (timeout)
  2920. ata_queue_delayed_pio_task(ap, timeout);
  2921. else if (!qc_completed)
  2922. goto fsm_start;
  2923. }
  2924. /**
  2925. * ata_qc_timeout - Handle timeout of queued command
  2926. * @qc: Command that timed out
  2927. *
  2928. * Some part of the kernel (currently, only the SCSI layer)
  2929. * has noticed that the active command on port @ap has not
  2930. * completed after a specified length of time. Handle this
  2931. * condition by disabling DMA (if necessary) and completing
  2932. * transactions, with error if necessary.
  2933. *
  2934. * This also handles the case of the "lost interrupt", where
  2935. * for some reason (possibly hardware bug, possibly driver bug)
  2936. * an interrupt was not delivered to the driver, even though the
  2937. * transaction completed successfully.
  2938. *
  2939. * LOCKING:
  2940. * Inherited from SCSI layer (none, can sleep)
  2941. */
  2942. static void ata_qc_timeout(struct ata_queued_cmd *qc)
  2943. {
  2944. struct ata_port *ap = qc->ap;
  2945. struct ata_host_set *host_set = ap->host_set;
  2946. u8 host_stat = 0, drv_stat;
  2947. unsigned long flags;
  2948. DPRINTK("ENTER\n");
  2949. ata_flush_pio_tasks(ap);
  2950. ap->hsm_task_state = HSM_ST_IDLE;
  2951. spin_lock_irqsave(&host_set->lock, flags);
  2952. switch (qc->tf.protocol) {
  2953. case ATA_PROT_DMA:
  2954. case ATA_PROT_ATAPI_DMA:
  2955. host_stat = ap->ops->bmdma_status(ap);
  2956. /* before we do anything else, clear DMA-Start bit */
  2957. ap->ops->bmdma_stop(qc);
  2958. /* fall through */
  2959. default:
  2960. ata_altstatus(ap);
  2961. drv_stat = ata_chk_status(ap);
  2962. /* ack bmdma irq events */
  2963. ap->ops->irq_clear(ap);
  2964. printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
  2965. ap->id, qc->tf.command, drv_stat, host_stat);
  2966. /* complete taskfile transaction */
  2967. qc->err_mask |= ac_err_mask(drv_stat);
  2968. break;
  2969. }
  2970. spin_unlock_irqrestore(&host_set->lock, flags);
  2971. ata_eh_qc_complete(qc);
  2972. DPRINTK("EXIT\n");
  2973. }
  2974. /**
  2975. * ata_eng_timeout - Handle timeout of queued command
  2976. * @ap: Port on which timed-out command is active
  2977. *
  2978. * Some part of the kernel (currently, only the SCSI layer)
  2979. * has noticed that the active command on port @ap has not
  2980. * completed after a specified length of time. Handle this
  2981. * condition by disabling DMA (if necessary) and completing
  2982. * transactions, with error if necessary.
  2983. *
  2984. * This also handles the case of the "lost interrupt", where
  2985. * for some reason (possibly hardware bug, possibly driver bug)
  2986. * an interrupt was not delivered to the driver, even though the
  2987. * transaction completed successfully.
  2988. *
  2989. * LOCKING:
  2990. * Inherited from SCSI layer (none, can sleep)
  2991. */
  2992. void ata_eng_timeout(struct ata_port *ap)
  2993. {
  2994. DPRINTK("ENTER\n");
  2995. ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
  2996. DPRINTK("EXIT\n");
  2997. }
  2998. /**
  2999. * ata_qc_new - Request an available ATA command, for queueing
  3000. * @ap: Port associated with device @dev
  3001. * @dev: Device from whom we request an available command structure
  3002. *
  3003. * LOCKING:
  3004. * None.
  3005. */
  3006. static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
  3007. {
  3008. struct ata_queued_cmd *qc = NULL;
  3009. unsigned int i;
  3010. for (i = 0; i < ATA_MAX_QUEUE; i++)
  3011. if (!test_and_set_bit(i, &ap->qactive)) {
  3012. qc = ata_qc_from_tag(ap, i);
  3013. break;
  3014. }
  3015. if (qc)
  3016. qc->tag = i;
  3017. return qc;
  3018. }
  3019. /**
  3020. * ata_qc_new_init - Request an available ATA command, and initialize it
  3021. * @ap: Port associated with device @dev
  3022. * @dev: Device from whom we request an available command structure
  3023. *
  3024. * LOCKING:
  3025. * None.
  3026. */
  3027. struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
  3028. struct ata_device *dev)
  3029. {
  3030. struct ata_queued_cmd *qc;
  3031. qc = ata_qc_new(ap);
  3032. if (qc) {
  3033. qc->scsicmd = NULL;
  3034. qc->ap = ap;
  3035. qc->dev = dev;
  3036. ata_qc_reinit(qc);
  3037. }
  3038. return qc;
  3039. }
  3040. /**
  3041. * ata_qc_free - free unused ata_queued_cmd
  3042. * @qc: Command to complete
  3043. *
  3044. * Designed to free unused ata_queued_cmd object
  3045. * in case something prevents using it.
  3046. *
  3047. * LOCKING:
  3048. * spin_lock_irqsave(host_set lock)
  3049. */
  3050. void ata_qc_free(struct ata_queued_cmd *qc)
  3051. {
  3052. struct ata_port *ap = qc->ap;
  3053. unsigned int tag;
  3054. assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
  3055. qc->flags = 0;
  3056. tag = qc->tag;
  3057. if (likely(ata_tag_valid(tag))) {
  3058. if (tag == ap->active_tag)
  3059. ap->active_tag = ATA_TAG_POISON;
  3060. qc->tag = ATA_TAG_POISON;
  3061. clear_bit(tag, &ap->qactive);
  3062. }
  3063. }
  3064. void __ata_qc_complete(struct ata_queued_cmd *qc)
  3065. {
  3066. assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
  3067. assert(qc->flags & ATA_QCFLAG_ACTIVE);
  3068. if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
  3069. ata_sg_clean(qc);
  3070. /* atapi: mark qc as inactive to prevent the interrupt handler
  3071. * from completing the command twice later, before the error handler
  3072. * is called. (when rc != 0 and atapi request sense is needed)
  3073. */
  3074. qc->flags &= ~ATA_QCFLAG_ACTIVE;
  3075. /* call completion callback */
  3076. qc->complete_fn(qc);
  3077. }
  3078. static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
  3079. {
  3080. struct ata_port *ap = qc->ap;
  3081. switch (qc->tf.protocol) {
  3082. case ATA_PROT_DMA:
  3083. case ATA_PROT_ATAPI_DMA:
  3084. return 1;
  3085. case ATA_PROT_ATAPI:
  3086. case ATA_PROT_PIO:
  3087. case ATA_PROT_PIO_MULT:
  3088. if (ap->flags & ATA_FLAG_PIO_DMA)
  3089. return 1;
  3090. /* fall through */
  3091. default:
  3092. return 0;
  3093. }
  3094. /* never reached */
  3095. }
  3096. /**
  3097. * ata_qc_issue - issue taskfile to device
  3098. * @qc: command to issue to device
  3099. *
  3100. * Prepare an ATA command to submission to device.
  3101. * This includes mapping the data into a DMA-able
  3102. * area, filling in the S/G table, and finally
  3103. * writing the taskfile to hardware, starting the command.
  3104. *
  3105. * LOCKING:
  3106. * spin_lock_irqsave(host_set lock)
  3107. *
  3108. * RETURNS:
  3109. * Zero on success, AC_ERR_* mask on failure
  3110. */
  3111. unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
  3112. {
  3113. struct ata_port *ap = qc->ap;
  3114. if (ata_should_dma_map(qc)) {
  3115. if (qc->flags & ATA_QCFLAG_SG) {
  3116. if (ata_sg_setup(qc))
  3117. goto sg_err;
  3118. } else if (qc->flags & ATA_QCFLAG_SINGLE) {
  3119. if (ata_sg_setup_one(qc))
  3120. goto sg_err;
  3121. }
  3122. } else {
  3123. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  3124. }
  3125. ap->ops->qc_prep(qc);
  3126. qc->ap->active_tag = qc->tag;
  3127. qc->flags |= ATA_QCFLAG_ACTIVE;
  3128. return ap->ops->qc_issue(qc);
  3129. sg_err:
  3130. qc->flags &= ~ATA_QCFLAG_DMAMAP;
  3131. return AC_ERR_SYSTEM;
  3132. }
  3133. /**
  3134. * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
  3135. * @qc: command to issue to device
  3136. *
  3137. * Using various libata functions and hooks, this function
  3138. * starts an ATA command. ATA commands are grouped into
  3139. * classes called "protocols", and issuing each type of protocol
  3140. * is slightly different.
  3141. *
  3142. * May be used as the qc_issue() entry in ata_port_operations.
  3143. *
  3144. * LOCKING:
  3145. * spin_lock_irqsave(host_set lock)
  3146. *
  3147. * RETURNS:
  3148. * Zero on success, AC_ERR_* mask on failure
  3149. */
  3150. unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
  3151. {
  3152. struct ata_port *ap = qc->ap;
  3153. ata_dev_select(ap, qc->dev->devno, 1, 0);
  3154. switch (qc->tf.protocol) {
  3155. case ATA_PROT_NODATA:
  3156. ata_tf_to_host(ap, &qc->tf);
  3157. break;
  3158. case ATA_PROT_DMA:
  3159. ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
  3160. ap->ops->bmdma_setup(qc); /* set up bmdma */
  3161. ap->ops->bmdma_start(qc); /* initiate bmdma */
  3162. break;
  3163. case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
  3164. ata_qc_set_polling(qc);
  3165. ata_tf_to_host(ap, &qc->tf);
  3166. ap->hsm_task_state = HSM_ST;
  3167. ata_queue_pio_task(ap);
  3168. break;
  3169. case ATA_PROT_ATAPI:
  3170. ata_qc_set_polling(qc);
  3171. ata_tf_to_host(ap, &qc->tf);
  3172. ata_queue_packet_task(ap);
  3173. break;
  3174. case ATA_PROT_ATAPI_NODATA:
  3175. ap->flags |= ATA_FLAG_NOINTR;
  3176. ata_tf_to_host(ap, &qc->tf);
  3177. ata_queue_packet_task(ap);
  3178. break;
  3179. case ATA_PROT_ATAPI_DMA:
  3180. ap->flags |= ATA_FLAG_NOINTR;
  3181. ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
  3182. ap->ops->bmdma_setup(qc); /* set up bmdma */
  3183. ata_queue_packet_task(ap);
  3184. break;
  3185. default:
  3186. WARN_ON(1);
  3187. return AC_ERR_SYSTEM;
  3188. }
  3189. return 0;
  3190. }
  3191. /**
  3192. * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
  3193. * @qc: Info associated with this ATA transaction.
  3194. *
  3195. * LOCKING:
  3196. * spin_lock_irqsave(host_set lock)
  3197. */
  3198. static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
  3199. {
  3200. struct ata_port *ap = qc->ap;
  3201. unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
  3202. u8 dmactl;
  3203. void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
  3204. /* load PRD table addr. */
  3205. mb(); /* make sure PRD table writes are visible to controller */
  3206. writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
  3207. /* specify data direction, triple-check start bit is clear */
  3208. dmactl = readb(mmio + ATA_DMA_CMD);
  3209. dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
  3210. if (!rw)
  3211. dmactl |= ATA_DMA_WR;
  3212. writeb(dmactl, mmio + ATA_DMA_CMD);
  3213. /* issue r/w command */
  3214. ap->ops->exec_command(ap, &qc->tf);
  3215. }
  3216. /**
  3217. * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
  3218. * @qc: Info associated with this ATA transaction.
  3219. *
  3220. * LOCKING:
  3221. * spin_lock_irqsave(host_set lock)
  3222. */
  3223. static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
  3224. {
  3225. struct ata_port *ap = qc->ap;
  3226. void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
  3227. u8 dmactl;
  3228. /* start host DMA transaction */
  3229. dmactl = readb(mmio + ATA_DMA_CMD);
  3230. writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
  3231. /* Strictly, one may wish to issue a readb() here, to
  3232. * flush the mmio write. However, control also passes
  3233. * to the hardware at this point, and it will interrupt
  3234. * us when we are to resume control. So, in effect,
  3235. * we don't care when the mmio write flushes.
  3236. * Further, a read of the DMA status register _immediately_
  3237. * following the write may not be what certain flaky hardware
  3238. * is expected, so I think it is best to not add a readb()
  3239. * without first all the MMIO ATA cards/mobos.
  3240. * Or maybe I'm just being paranoid.
  3241. */
  3242. }
  3243. /**
  3244. * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
  3245. * @qc: Info associated with this ATA transaction.
  3246. *
  3247. * LOCKING:
  3248. * spin_lock_irqsave(host_set lock)
  3249. */
  3250. static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
  3251. {
  3252. struct ata_port *ap = qc->ap;
  3253. unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
  3254. u8 dmactl;
  3255. /* load PRD table addr. */
  3256. outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
  3257. /* specify data direction, triple-check start bit is clear */
  3258. dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  3259. dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
  3260. if (!rw)
  3261. dmactl |= ATA_DMA_WR;
  3262. outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  3263. /* issue r/w command */
  3264. ap->ops->exec_command(ap, &qc->tf);
  3265. }
  3266. /**
  3267. * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
  3268. * @qc: Info associated with this ATA transaction.
  3269. *
  3270. * LOCKING:
  3271. * spin_lock_irqsave(host_set lock)
  3272. */
  3273. static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
  3274. {
  3275. struct ata_port *ap = qc->ap;
  3276. u8 dmactl;
  3277. /* start host DMA transaction */
  3278. dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  3279. outb(dmactl | ATA_DMA_START,
  3280. ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  3281. }
  3282. /**
  3283. * ata_bmdma_start - Start a PCI IDE BMDMA transaction
  3284. * @qc: Info associated with this ATA transaction.
  3285. *
  3286. * Writes the ATA_DMA_START flag to the DMA command register.
  3287. *
  3288. * May be used as the bmdma_start() entry in ata_port_operations.
  3289. *
  3290. * LOCKING:
  3291. * spin_lock_irqsave(host_set lock)
  3292. */
  3293. void ata_bmdma_start(struct ata_queued_cmd *qc)
  3294. {
  3295. if (qc->ap->flags & ATA_FLAG_MMIO)
  3296. ata_bmdma_start_mmio(qc);
  3297. else
  3298. ata_bmdma_start_pio(qc);
  3299. }
  3300. /**
  3301. * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
  3302. * @qc: Info associated with this ATA transaction.
  3303. *
  3304. * Writes address of PRD table to device's PRD Table Address
  3305. * register, sets the DMA control register, and calls
  3306. * ops->exec_command() to start the transfer.
  3307. *
  3308. * May be used as the bmdma_setup() entry in ata_port_operations.
  3309. *
  3310. * LOCKING:
  3311. * spin_lock_irqsave(host_set lock)
  3312. */
  3313. void ata_bmdma_setup(struct ata_queued_cmd *qc)
  3314. {
  3315. if (qc->ap->flags & ATA_FLAG_MMIO)
  3316. ata_bmdma_setup_mmio(qc);
  3317. else
  3318. ata_bmdma_setup_pio(qc);
  3319. }
  3320. /**
  3321. * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
  3322. * @ap: Port associated with this ATA transaction.
  3323. *
  3324. * Clear interrupt and error flags in DMA status register.
  3325. *
  3326. * May be used as the irq_clear() entry in ata_port_operations.
  3327. *
  3328. * LOCKING:
  3329. * spin_lock_irqsave(host_set lock)
  3330. */
  3331. void ata_bmdma_irq_clear(struct ata_port *ap)
  3332. {
  3333. if (ap->flags & ATA_FLAG_MMIO) {
  3334. void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
  3335. writeb(readb(mmio), mmio);
  3336. } else {
  3337. unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
  3338. outb(inb(addr), addr);
  3339. }
  3340. }
  3341. /**
  3342. * ata_bmdma_status - Read PCI IDE BMDMA status
  3343. * @ap: Port associated with this ATA transaction.
  3344. *
  3345. * Read and return BMDMA status register.
  3346. *
  3347. * May be used as the bmdma_status() entry in ata_port_operations.
  3348. *
  3349. * LOCKING:
  3350. * spin_lock_irqsave(host_set lock)
  3351. */
  3352. u8 ata_bmdma_status(struct ata_port *ap)
  3353. {
  3354. u8 host_stat;
  3355. if (ap->flags & ATA_FLAG_MMIO) {
  3356. void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
  3357. host_stat = readb(mmio + ATA_DMA_STATUS);
  3358. } else
  3359. host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
  3360. return host_stat;
  3361. }
  3362. /**
  3363. * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
  3364. * @qc: Command we are ending DMA for
  3365. *
  3366. * Clears the ATA_DMA_START flag in the dma control register
  3367. *
  3368. * May be used as the bmdma_stop() entry in ata_port_operations.
  3369. *
  3370. * LOCKING:
  3371. * spin_lock_irqsave(host_set lock)
  3372. */
  3373. void ata_bmdma_stop(struct ata_queued_cmd *qc)
  3374. {
  3375. struct ata_port *ap = qc->ap;
  3376. if (ap->flags & ATA_FLAG_MMIO) {
  3377. void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
  3378. /* clear start/stop bit */
  3379. writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
  3380. mmio + ATA_DMA_CMD);
  3381. } else {
  3382. /* clear start/stop bit */
  3383. outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
  3384. ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  3385. }
  3386. /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
  3387. ata_altstatus(ap); /* dummy read */
  3388. }
  3389. /**
  3390. * ata_host_intr - Handle host interrupt for given (port, task)
  3391. * @ap: Port on which interrupt arrived (possibly...)
  3392. * @qc: Taskfile currently active in engine
  3393. *
  3394. * Handle host interrupt for given queued command. Currently,
  3395. * only DMA interrupts are handled. All other commands are
  3396. * handled via polling with interrupts disabled (nIEN bit).
  3397. *
  3398. * LOCKING:
  3399. * spin_lock_irqsave(host_set lock)
  3400. *
  3401. * RETURNS:
  3402. * One if interrupt was handled, zero if not (shared irq).
  3403. */
  3404. inline unsigned int ata_host_intr (struct ata_port *ap,
  3405. struct ata_queued_cmd *qc)
  3406. {
  3407. u8 status, host_stat;
  3408. switch (qc->tf.protocol) {
  3409. case ATA_PROT_DMA:
  3410. case ATA_PROT_ATAPI_DMA:
  3411. case ATA_PROT_ATAPI:
  3412. /* check status of DMA engine */
  3413. host_stat = ap->ops->bmdma_status(ap);
  3414. VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
  3415. /* if it's not our irq... */
  3416. if (!(host_stat & ATA_DMA_INTR))
  3417. goto idle_irq;
  3418. /* before we do anything else, clear DMA-Start bit */
  3419. ap->ops->bmdma_stop(qc);
  3420. /* fall through */
  3421. case ATA_PROT_ATAPI_NODATA:
  3422. case ATA_PROT_NODATA:
  3423. /* check altstatus */
  3424. status = ata_altstatus(ap);
  3425. if (status & ATA_BUSY)
  3426. goto idle_irq;
  3427. /* check main status, clearing INTRQ */
  3428. status = ata_chk_status(ap);
  3429. if (unlikely(status & ATA_BUSY))
  3430. goto idle_irq;
  3431. DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
  3432. ap->id, qc->tf.protocol, status);
  3433. /* ack bmdma irq events */
  3434. ap->ops->irq_clear(ap);
  3435. /* complete taskfile transaction */
  3436. qc->err_mask |= ac_err_mask(status);
  3437. ata_qc_complete(qc);
  3438. break;
  3439. default:
  3440. goto idle_irq;
  3441. }
  3442. return 1; /* irq handled */
  3443. idle_irq:
  3444. ap->stats.idle_irq++;
  3445. #ifdef ATA_IRQ_TRAP
  3446. if ((ap->stats.idle_irq % 1000) == 0) {
  3447. handled = 1;
  3448. ata_irq_ack(ap, 0); /* debug trap */
  3449. printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
  3450. }
  3451. #endif
  3452. return 0; /* irq not handled */
  3453. }
  3454. /**
  3455. * ata_interrupt - Default ATA host interrupt handler
  3456. * @irq: irq line (unused)
  3457. * @dev_instance: pointer to our ata_host_set information structure
  3458. * @regs: unused
  3459. *
  3460. * Default interrupt handler for PCI IDE devices. Calls
  3461. * ata_host_intr() for each port that is not disabled.
  3462. *
  3463. * LOCKING:
  3464. * Obtains host_set lock during operation.
  3465. *
  3466. * RETURNS:
  3467. * IRQ_NONE or IRQ_HANDLED.
  3468. */
  3469. irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
  3470. {
  3471. struct ata_host_set *host_set = dev_instance;
  3472. unsigned int i;
  3473. unsigned int handled = 0;
  3474. unsigned long flags;
  3475. /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
  3476. spin_lock_irqsave(&host_set->lock, flags);
  3477. for (i = 0; i < host_set->n_ports; i++) {
  3478. struct ata_port *ap;
  3479. ap = host_set->ports[i];
  3480. if (ap &&
  3481. !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
  3482. struct ata_queued_cmd *qc;
  3483. qc = ata_qc_from_tag(ap, ap->active_tag);
  3484. if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
  3485. (qc->flags & ATA_QCFLAG_ACTIVE))
  3486. handled |= ata_host_intr(ap, qc);
  3487. }
  3488. }
  3489. spin_unlock_irqrestore(&host_set->lock, flags);
  3490. return IRQ_RETVAL(handled);
  3491. }
  3492. /**
  3493. * atapi_packet_task - Write CDB bytes to hardware
  3494. * @_data: Port to which ATAPI device is attached.
  3495. *
  3496. * When device has indicated its readiness to accept
  3497. * a CDB, this function is called. Send the CDB.
  3498. * If DMA is to be performed, exit immediately.
  3499. * Otherwise, we are in polling mode, so poll
  3500. * status under operation succeeds or fails.
  3501. *
  3502. * LOCKING:
  3503. * Kernel thread context (may sleep)
  3504. */
  3505. static void atapi_packet_task(void *_data)
  3506. {
  3507. struct ata_port *ap = _data;
  3508. struct ata_queued_cmd *qc;
  3509. u8 status;
  3510. qc = ata_qc_from_tag(ap, ap->active_tag);
  3511. assert(qc != NULL);
  3512. assert(qc->flags & ATA_QCFLAG_ACTIVE);
  3513. /* sleep-wait for BSY to clear */
  3514. DPRINTK("busy wait\n");
  3515. if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
  3516. qc->err_mask |= AC_ERR_TIMEOUT;
  3517. goto err_out;
  3518. }
  3519. /* make sure DRQ is set */
  3520. status = ata_chk_status(ap);
  3521. if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
  3522. qc->err_mask |= AC_ERR_HSM;
  3523. goto err_out;
  3524. }
  3525. /* send SCSI cdb */
  3526. DPRINTK("send cdb\n");
  3527. assert(ap->cdb_len >= 12);
  3528. if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
  3529. qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
  3530. unsigned long flags;
  3531. /* Once we're done issuing command and kicking bmdma,
  3532. * irq handler takes over. To not lose irq, we need
  3533. * to clear NOINTR flag before sending cdb, but
  3534. * interrupt handler shouldn't be invoked before we're
  3535. * finished. Hence, the following locking.
  3536. */
  3537. spin_lock_irqsave(&ap->host_set->lock, flags);
  3538. ap->flags &= ~ATA_FLAG_NOINTR;
  3539. ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
  3540. if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
  3541. ap->ops->bmdma_start(qc); /* initiate bmdma */
  3542. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  3543. } else {
  3544. ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
  3545. /* PIO commands are handled by polling */
  3546. ap->hsm_task_state = HSM_ST;
  3547. ata_queue_pio_task(ap);
  3548. }
  3549. return;
  3550. err_out:
  3551. ata_poll_qc_complete(qc);
  3552. }
  3553. /*
  3554. * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
  3555. * without filling any other registers
  3556. */
  3557. static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
  3558. u8 cmd)
  3559. {
  3560. struct ata_taskfile tf;
  3561. int err;
  3562. ata_tf_init(ap, &tf, dev->devno);
  3563. tf.command = cmd;
  3564. tf.flags |= ATA_TFLAG_DEVICE;
  3565. tf.protocol = ATA_PROT_NODATA;
  3566. err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
  3567. if (err)
  3568. printk(KERN_ERR "%s: ata command failed: %d\n",
  3569. __FUNCTION__, err);
  3570. return err;
  3571. }
  3572. static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
  3573. {
  3574. u8 cmd;
  3575. if (!ata_try_flush_cache(dev))
  3576. return 0;
  3577. if (ata_id_has_flush_ext(dev->id))
  3578. cmd = ATA_CMD_FLUSH_EXT;
  3579. else
  3580. cmd = ATA_CMD_FLUSH;
  3581. return ata_do_simple_cmd(ap, dev, cmd);
  3582. }
  3583. static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
  3584. {
  3585. return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
  3586. }
  3587. static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
  3588. {
  3589. return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
  3590. }
  3591. /**
  3592. * ata_device_resume - wakeup a previously suspended devices
  3593. * @ap: port the device is connected to
  3594. * @dev: the device to resume
  3595. *
  3596. * Kick the drive back into action, by sending it an idle immediate
  3597. * command and making sure its transfer mode matches between drive
  3598. * and host.
  3599. *
  3600. */
  3601. int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
  3602. {
  3603. if (ap->flags & ATA_FLAG_SUSPENDED) {
  3604. ap->flags &= ~ATA_FLAG_SUSPENDED;
  3605. ata_set_mode(ap);
  3606. }
  3607. if (!ata_dev_present(dev))
  3608. return 0;
  3609. if (dev->class == ATA_DEV_ATA)
  3610. ata_start_drive(ap, dev);
  3611. return 0;
  3612. }
  3613. /**
  3614. * ata_device_suspend - prepare a device for suspend
  3615. * @ap: port the device is connected to
  3616. * @dev: the device to suspend
  3617. *
  3618. * Flush the cache on the drive, if appropriate, then issue a
  3619. * standbynow command.
  3620. */
  3621. int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
  3622. {
  3623. if (!ata_dev_present(dev))
  3624. return 0;
  3625. if (dev->class == ATA_DEV_ATA)
  3626. ata_flush_cache(ap, dev);
  3627. ata_standby_drive(ap, dev);
  3628. ap->flags |= ATA_FLAG_SUSPENDED;
  3629. return 0;
  3630. }
  3631. /**
  3632. * ata_port_start - Set port up for dma.
  3633. * @ap: Port to initialize
  3634. *
  3635. * Called just after data structures for each port are
  3636. * initialized. Allocates space for PRD table.
  3637. *
  3638. * May be used as the port_start() entry in ata_port_operations.
  3639. *
  3640. * LOCKING:
  3641. * Inherited from caller.
  3642. */
  3643. int ata_port_start (struct ata_port *ap)
  3644. {
  3645. struct device *dev = ap->host_set->dev;
  3646. int rc;
  3647. ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
  3648. if (!ap->prd)
  3649. return -ENOMEM;
  3650. rc = ata_pad_alloc(ap, dev);
  3651. if (rc) {
  3652. dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
  3653. return rc;
  3654. }
  3655. DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
  3656. return 0;
  3657. }
  3658. /**
  3659. * ata_port_stop - Undo ata_port_start()
  3660. * @ap: Port to shut down
  3661. *
  3662. * Frees the PRD table.
  3663. *
  3664. * May be used as the port_stop() entry in ata_port_operations.
  3665. *
  3666. * LOCKING:
  3667. * Inherited from caller.
  3668. */
  3669. void ata_port_stop (struct ata_port *ap)
  3670. {
  3671. struct device *dev = ap->host_set->dev;
  3672. dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
  3673. ata_pad_free(ap, dev);
  3674. }
  3675. void ata_host_stop (struct ata_host_set *host_set)
  3676. {
  3677. if (host_set->mmio_base)
  3678. iounmap(host_set->mmio_base);
  3679. }
  3680. /**
  3681. * ata_host_remove - Unregister SCSI host structure with upper layers
  3682. * @ap: Port to unregister
  3683. * @do_unregister: 1 if we fully unregister, 0 to just stop the port
  3684. *
  3685. * LOCKING:
  3686. * Inherited from caller.
  3687. */
  3688. static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
  3689. {
  3690. struct Scsi_Host *sh = ap->host;
  3691. DPRINTK("ENTER\n");
  3692. if (do_unregister)
  3693. scsi_remove_host(sh);
  3694. ap->ops->port_stop(ap);
  3695. }
  3696. /**
  3697. * ata_host_init - Initialize an ata_port structure
  3698. * @ap: Structure to initialize
  3699. * @host: associated SCSI mid-layer structure
  3700. * @host_set: Collection of hosts to which @ap belongs
  3701. * @ent: Probe information provided by low-level driver
  3702. * @port_no: Port number associated with this ata_port
  3703. *
  3704. * Initialize a new ata_port structure, and its associated
  3705. * scsi_host.
  3706. *
  3707. * LOCKING:
  3708. * Inherited from caller.
  3709. */
  3710. static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
  3711. struct ata_host_set *host_set,
  3712. const struct ata_probe_ent *ent, unsigned int port_no)
  3713. {
  3714. unsigned int i;
  3715. host->max_id = 16;
  3716. host->max_lun = 1;
  3717. host->max_channel = 1;
  3718. host->unique_id = ata_unique_id++;
  3719. host->max_cmd_len = 12;
  3720. ap->flags = ATA_FLAG_PORT_DISABLED;
  3721. ap->id = host->unique_id;
  3722. ap->host = host;
  3723. ap->ctl = ATA_DEVCTL_OBS;
  3724. ap->host_set = host_set;
  3725. ap->port_no = port_no;
  3726. ap->hard_port_no =
  3727. ent->legacy_mode ? ent->hard_port_no : port_no;
  3728. ap->pio_mask = ent->pio_mask;
  3729. ap->mwdma_mask = ent->mwdma_mask;
  3730. ap->udma_mask = ent->udma_mask;
  3731. ap->flags |= ent->host_flags;
  3732. ap->ops = ent->port_ops;
  3733. ap->cbl = ATA_CBL_NONE;
  3734. ap->active_tag = ATA_TAG_POISON;
  3735. ap->last_ctl = 0xFF;
  3736. INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
  3737. INIT_WORK(&ap->pio_task, ata_pio_task, ap);
  3738. INIT_LIST_HEAD(&ap->eh_done_q);
  3739. for (i = 0; i < ATA_MAX_DEVICES; i++)
  3740. ap->device[i].devno = i;
  3741. #ifdef ATA_IRQ_TRAP
  3742. ap->stats.unhandled_irq = 1;
  3743. ap->stats.idle_irq = 1;
  3744. #endif
  3745. memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
  3746. }
  3747. /**
  3748. * ata_host_add - Attach low-level ATA driver to system
  3749. * @ent: Information provided by low-level driver
  3750. * @host_set: Collections of ports to which we add
  3751. * @port_no: Port number associated with this host
  3752. *
  3753. * Attach low-level ATA driver to system.
  3754. *
  3755. * LOCKING:
  3756. * PCI/etc. bus probe sem.
  3757. *
  3758. * RETURNS:
  3759. * New ata_port on success, for NULL on error.
  3760. */
  3761. static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
  3762. struct ata_host_set *host_set,
  3763. unsigned int port_no)
  3764. {
  3765. struct Scsi_Host *host;
  3766. struct ata_port *ap;
  3767. int rc;
  3768. DPRINTK("ENTER\n");
  3769. host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
  3770. if (!host)
  3771. return NULL;
  3772. ap = (struct ata_port *) &host->hostdata[0];
  3773. ata_host_init(ap, host, host_set, ent, port_no);
  3774. rc = ap->ops->port_start(ap);
  3775. if (rc)
  3776. goto err_out;
  3777. return ap;
  3778. err_out:
  3779. scsi_host_put(host);
  3780. return NULL;
  3781. }
  3782. /**
  3783. * ata_device_add - Register hardware device with ATA and SCSI layers
  3784. * @ent: Probe information describing hardware device to be registered
  3785. *
  3786. * This function processes the information provided in the probe
  3787. * information struct @ent, allocates the necessary ATA and SCSI
  3788. * host information structures, initializes them, and registers
  3789. * everything with requisite kernel subsystems.
  3790. *
  3791. * This function requests irqs, probes the ATA bus, and probes
  3792. * the SCSI bus.
  3793. *
  3794. * LOCKING:
  3795. * PCI/etc. bus probe sem.
  3796. *
  3797. * RETURNS:
  3798. * Number of ports registered. Zero on error (no ports registered).
  3799. */
  3800. int ata_device_add(const struct ata_probe_ent *ent)
  3801. {
  3802. unsigned int count = 0, i;
  3803. struct device *dev = ent->dev;
  3804. struct ata_host_set *host_set;
  3805. DPRINTK("ENTER\n");
  3806. /* alloc a container for our list of ATA ports (buses) */
  3807. host_set = kzalloc(sizeof(struct ata_host_set) +
  3808. (ent->n_ports * sizeof(void *)), GFP_KERNEL);
  3809. if (!host_set)
  3810. return 0;
  3811. spin_lock_init(&host_set->lock);
  3812. host_set->dev = dev;
  3813. host_set->n_ports = ent->n_ports;
  3814. host_set->irq = ent->irq;
  3815. host_set->mmio_base = ent->mmio_base;
  3816. host_set->private_data = ent->private_data;
  3817. host_set->ops = ent->port_ops;
  3818. /* register each port bound to this device */
  3819. for (i = 0; i < ent->n_ports; i++) {
  3820. struct ata_port *ap;
  3821. unsigned long xfer_mode_mask;
  3822. ap = ata_host_add(ent, host_set, i);
  3823. if (!ap)
  3824. goto err_out;
  3825. host_set->ports[i] = ap;
  3826. xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
  3827. (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
  3828. (ap->pio_mask << ATA_SHIFT_PIO);
  3829. /* print per-port info to dmesg */
  3830. printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
  3831. "bmdma 0x%lX irq %lu\n",
  3832. ap->id,
  3833. ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
  3834. ata_mode_string(xfer_mode_mask),
  3835. ap->ioaddr.cmd_addr,
  3836. ap->ioaddr.ctl_addr,
  3837. ap->ioaddr.bmdma_addr,
  3838. ent->irq);
  3839. ata_chk_status(ap);
  3840. host_set->ops->irq_clear(ap);
  3841. count++;
  3842. }
  3843. if (!count)
  3844. goto err_free_ret;
  3845. /* obtain irq, that is shared between channels */
  3846. if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
  3847. DRV_NAME, host_set))
  3848. goto err_out;
  3849. /* perform each probe synchronously */
  3850. DPRINTK("probe begin\n");
  3851. for (i = 0; i < count; i++) {
  3852. struct ata_port *ap;
  3853. int rc;
  3854. ap = host_set->ports[i];
  3855. DPRINTK("ata%u: bus probe begin\n", ap->id);
  3856. rc = ata_bus_probe(ap);
  3857. DPRINTK("ata%u: bus probe end\n", ap->id);
  3858. if (rc) {
  3859. /* FIXME: do something useful here?
  3860. * Current libata behavior will
  3861. * tear down everything when
  3862. * the module is removed
  3863. * or the h/w is unplugged.
  3864. */
  3865. }
  3866. rc = scsi_add_host(ap->host, dev);
  3867. if (rc) {
  3868. printk(KERN_ERR "ata%u: scsi_add_host failed\n",
  3869. ap->id);
  3870. /* FIXME: do something useful here */
  3871. /* FIXME: handle unconditional calls to
  3872. * scsi_scan_host and ata_host_remove, below,
  3873. * at the very least
  3874. */
  3875. }
  3876. }
  3877. /* probes are done, now scan each port's disk(s) */
  3878. DPRINTK("host probe begin\n");
  3879. for (i = 0; i < count; i++) {
  3880. struct ata_port *ap = host_set->ports[i];
  3881. ata_scsi_scan_host(ap);
  3882. }
  3883. dev_set_drvdata(dev, host_set);
  3884. VPRINTK("EXIT, returning %u\n", ent->n_ports);
  3885. return ent->n_ports; /* success */
  3886. err_out:
  3887. for (i = 0; i < count; i++) {
  3888. ata_host_remove(host_set->ports[i], 1);
  3889. scsi_host_put(host_set->ports[i]->host);
  3890. }
  3891. err_free_ret:
  3892. kfree(host_set);
  3893. VPRINTK("EXIT, returning 0\n");
  3894. return 0;
  3895. }
  3896. /**
  3897. * ata_host_set_remove - PCI layer callback for device removal
  3898. * @host_set: ATA host set that was removed
  3899. *
  3900. * Unregister all objects associated with this host set. Free those
  3901. * objects.
  3902. *
  3903. * LOCKING:
  3904. * Inherited from calling layer (may sleep).
  3905. */
  3906. void ata_host_set_remove(struct ata_host_set *host_set)
  3907. {
  3908. struct ata_port *ap;
  3909. unsigned int i;
  3910. for (i = 0; i < host_set->n_ports; i++) {
  3911. ap = host_set->ports[i];
  3912. scsi_remove_host(ap->host);
  3913. }
  3914. free_irq(host_set->irq, host_set);
  3915. for (i = 0; i < host_set->n_ports; i++) {
  3916. ap = host_set->ports[i];
  3917. ata_scsi_release(ap->host);
  3918. if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
  3919. struct ata_ioports *ioaddr = &ap->ioaddr;
  3920. if (ioaddr->cmd_addr == 0x1f0)
  3921. release_region(0x1f0, 8);
  3922. else if (ioaddr->cmd_addr == 0x170)
  3923. release_region(0x170, 8);
  3924. }
  3925. scsi_host_put(ap->host);
  3926. }
  3927. if (host_set->ops->host_stop)
  3928. host_set->ops->host_stop(host_set);
  3929. kfree(host_set);
  3930. }
  3931. /**
  3932. * ata_scsi_release - SCSI layer callback hook for host unload
  3933. * @host: libata host to be unloaded
  3934. *
  3935. * Performs all duties necessary to shut down a libata port...
  3936. * Kill port kthread, disable port, and release resources.
  3937. *
  3938. * LOCKING:
  3939. * Inherited from SCSI layer.
  3940. *
  3941. * RETURNS:
  3942. * One.
  3943. */
  3944. int ata_scsi_release(struct Scsi_Host *host)
  3945. {
  3946. struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
  3947. DPRINTK("ENTER\n");
  3948. ap->ops->port_disable(ap);
  3949. ata_host_remove(ap, 0);
  3950. DPRINTK("EXIT\n");
  3951. return 1;
  3952. }
  3953. /**
  3954. * ata_std_ports - initialize ioaddr with standard port offsets.
  3955. * @ioaddr: IO address structure to be initialized
  3956. *
  3957. * Utility function which initializes data_addr, error_addr,
  3958. * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
  3959. * device_addr, status_addr, and command_addr to standard offsets
  3960. * relative to cmd_addr.
  3961. *
  3962. * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
  3963. */
  3964. void ata_std_ports(struct ata_ioports *ioaddr)
  3965. {
  3966. ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
  3967. ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
  3968. ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
  3969. ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
  3970. ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
  3971. ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
  3972. ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
  3973. ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
  3974. ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
  3975. ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
  3976. }
  3977. #ifdef CONFIG_PCI
  3978. void ata_pci_host_stop (struct ata_host_set *host_set)
  3979. {
  3980. struct pci_dev *pdev = to_pci_dev(host_set->dev);
  3981. pci_iounmap(pdev, host_set->mmio_base);
  3982. }
  3983. /**
  3984. * ata_pci_remove_one - PCI layer callback for device removal
  3985. * @pdev: PCI device that was removed
  3986. *
  3987. * PCI layer indicates to libata via this hook that
  3988. * hot-unplug or module unload event has occurred.
  3989. * Handle this by unregistering all objects associated
  3990. * with this PCI device. Free those objects. Then finally
  3991. * release PCI resources and disable device.
  3992. *
  3993. * LOCKING:
  3994. * Inherited from PCI layer (may sleep).
  3995. */
  3996. void ata_pci_remove_one (struct pci_dev *pdev)
  3997. {
  3998. struct device *dev = pci_dev_to_dev(pdev);
  3999. struct ata_host_set *host_set = dev_get_drvdata(dev);
  4000. ata_host_set_remove(host_set);
  4001. pci_release_regions(pdev);
  4002. pci_disable_device(pdev);
  4003. dev_set_drvdata(dev, NULL);
  4004. }
  4005. /* move to PCI subsystem */
  4006. int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
  4007. {
  4008. unsigned long tmp = 0;
  4009. switch (bits->width) {
  4010. case 1: {
  4011. u8 tmp8 = 0;
  4012. pci_read_config_byte(pdev, bits->reg, &tmp8);
  4013. tmp = tmp8;
  4014. break;
  4015. }
  4016. case 2: {
  4017. u16 tmp16 = 0;
  4018. pci_read_config_word(pdev, bits->reg, &tmp16);
  4019. tmp = tmp16;
  4020. break;
  4021. }
  4022. case 4: {
  4023. u32 tmp32 = 0;
  4024. pci_read_config_dword(pdev, bits->reg, &tmp32);
  4025. tmp = tmp32;
  4026. break;
  4027. }
  4028. default:
  4029. return -EINVAL;
  4030. }
  4031. tmp &= bits->mask;
  4032. return (tmp == bits->val) ? 1 : 0;
  4033. }
  4034. int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
  4035. {
  4036. pci_save_state(pdev);
  4037. pci_disable_device(pdev);
  4038. pci_set_power_state(pdev, PCI_D3hot);
  4039. return 0;
  4040. }
  4041. int ata_pci_device_resume(struct pci_dev *pdev)
  4042. {
  4043. pci_set_power_state(pdev, PCI_D0);
  4044. pci_restore_state(pdev);
  4045. pci_enable_device(pdev);
  4046. pci_set_master(pdev);
  4047. return 0;
  4048. }
  4049. #endif /* CONFIG_PCI */
  4050. static int __init ata_init(void)
  4051. {
  4052. ata_wq = create_workqueue("ata");
  4053. if (!ata_wq)
  4054. return -ENOMEM;
  4055. printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
  4056. return 0;
  4057. }
  4058. static void __exit ata_exit(void)
  4059. {
  4060. destroy_workqueue(ata_wq);
  4061. }
  4062. module_init(ata_init);
  4063. module_exit(ata_exit);
  4064. static unsigned long ratelimit_time;
  4065. static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
  4066. int ata_ratelimit(void)
  4067. {
  4068. int rc;
  4069. unsigned long flags;
  4070. spin_lock_irqsave(&ata_ratelimit_lock, flags);
  4071. if (time_after(jiffies, ratelimit_time)) {
  4072. rc = 1;
  4073. ratelimit_time = jiffies + (HZ/5);
  4074. } else
  4075. rc = 0;
  4076. spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
  4077. return rc;
  4078. }
  4079. /*
  4080. * libata is essentially a library of internal helper functions for
  4081. * low-level ATA host controller drivers. As such, the API/ABI is
  4082. * likely to change as new drivers are added and updated.
  4083. * Do not depend on ABI/API stability.
  4084. */
  4085. EXPORT_SYMBOL_GPL(ata_std_bios_param);
  4086. EXPORT_SYMBOL_GPL(ata_std_ports);
  4087. EXPORT_SYMBOL_GPL(ata_device_add);
  4088. EXPORT_SYMBOL_GPL(ata_host_set_remove);
  4089. EXPORT_SYMBOL_GPL(ata_sg_init);
  4090. EXPORT_SYMBOL_GPL(ata_sg_init_one);
  4091. EXPORT_SYMBOL_GPL(__ata_qc_complete);
  4092. EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
  4093. EXPORT_SYMBOL_GPL(ata_eng_timeout);
  4094. EXPORT_SYMBOL_GPL(ata_tf_load);
  4095. EXPORT_SYMBOL_GPL(ata_tf_read);
  4096. EXPORT_SYMBOL_GPL(ata_noop_dev_select);
  4097. EXPORT_SYMBOL_GPL(ata_std_dev_select);
  4098. EXPORT_SYMBOL_GPL(ata_tf_to_fis);
  4099. EXPORT_SYMBOL_GPL(ata_tf_from_fis);
  4100. EXPORT_SYMBOL_GPL(ata_check_status);
  4101. EXPORT_SYMBOL_GPL(ata_altstatus);
  4102. EXPORT_SYMBOL_GPL(ata_exec_command);
  4103. EXPORT_SYMBOL_GPL(ata_port_start);
  4104. EXPORT_SYMBOL_GPL(ata_port_stop);
  4105. EXPORT_SYMBOL_GPL(ata_host_stop);
  4106. EXPORT_SYMBOL_GPL(ata_interrupt);
  4107. EXPORT_SYMBOL_GPL(ata_qc_prep);
  4108. EXPORT_SYMBOL_GPL(ata_bmdma_setup);
  4109. EXPORT_SYMBOL_GPL(ata_bmdma_start);
  4110. EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
  4111. EXPORT_SYMBOL_GPL(ata_bmdma_status);
  4112. EXPORT_SYMBOL_GPL(ata_bmdma_stop);
  4113. EXPORT_SYMBOL_GPL(ata_port_probe);
  4114. EXPORT_SYMBOL_GPL(sata_phy_reset);
  4115. EXPORT_SYMBOL_GPL(__sata_phy_reset);
  4116. EXPORT_SYMBOL_GPL(ata_bus_reset);
  4117. EXPORT_SYMBOL_GPL(ata_std_probeinit);
  4118. EXPORT_SYMBOL_GPL(ata_std_softreset);
  4119. EXPORT_SYMBOL_GPL(sata_std_hardreset);
  4120. EXPORT_SYMBOL_GPL(ata_std_postreset);
  4121. EXPORT_SYMBOL_GPL(ata_std_probe_reset);
  4122. EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
  4123. EXPORT_SYMBOL_GPL(ata_port_disable);
  4124. EXPORT_SYMBOL_GPL(ata_ratelimit);
  4125. EXPORT_SYMBOL_GPL(ata_busy_sleep);
  4126. EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
  4127. EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
  4128. EXPORT_SYMBOL_GPL(ata_scsi_timed_out);
  4129. EXPORT_SYMBOL_GPL(ata_scsi_error);
  4130. EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
  4131. EXPORT_SYMBOL_GPL(ata_scsi_release);
  4132. EXPORT_SYMBOL_GPL(ata_host_intr);
  4133. EXPORT_SYMBOL_GPL(ata_dev_classify);
  4134. EXPORT_SYMBOL_GPL(ata_dev_id_string);
  4135. EXPORT_SYMBOL_GPL(ata_dev_config);
  4136. EXPORT_SYMBOL_GPL(ata_scsi_simulate);
  4137. EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
  4138. EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
  4139. EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
  4140. EXPORT_SYMBOL_GPL(ata_timing_compute);
  4141. EXPORT_SYMBOL_GPL(ata_timing_merge);
  4142. #ifdef CONFIG_PCI
  4143. EXPORT_SYMBOL_GPL(pci_test_config_bits);
  4144. EXPORT_SYMBOL_GPL(ata_pci_host_stop);
  4145. EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
  4146. EXPORT_SYMBOL_GPL(ata_pci_init_one);
  4147. EXPORT_SYMBOL_GPL(ata_pci_remove_one);
  4148. EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
  4149. EXPORT_SYMBOL_GPL(ata_pci_device_resume);
  4150. #endif /* CONFIG_PCI */
  4151. EXPORT_SYMBOL_GPL(ata_device_suspend);
  4152. EXPORT_SYMBOL_GPL(ata_device_resume);
  4153. EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
  4154. EXPORT_SYMBOL_GPL(ata_scsi_device_resume);