libata-core.c 118 KB

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