libata-core.c 119 KB

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