libata-core.c 123 KB

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