libata-core.c 125 KB

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