libata-core.c 119 KB

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