libata-core.c 122 KB

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