libata-core.c 126 KB

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