libata-core.c 119 KB

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