libata-core.c 122 KB

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