libata-core.c 118 KB

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