libata-core.c 123 KB

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