libata-core.c 125 KB

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