libata-core.c 128 KB

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