libata-core.c 125 KB

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