libata-core.c 126 KB

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