libata-core.c 123 KB

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