libata-core.c 125 KB

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