libata-core.c 119 KB

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