adma.c 134 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993
  1. /*
  2. * Copyright (C) 2006-2009 DENX Software Engineering.
  3. *
  4. * Author: Yuri Tikhonov <yur@emcraft.com>
  5. *
  6. * Further porting to arch/powerpc by
  7. * Anatolij Gustschin <agust@denx.de>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation; either version 2 of the License, or (at your option)
  12. * any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  17. * more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along with
  20. * this program; if not, write to the Free Software Foundation, Inc., 59
  21. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. * The full GNU General Public License is included in this distribution in the
  24. * file called COPYING.
  25. */
  26. /*
  27. * This driver supports the asynchrounous DMA copy and RAID engines available
  28. * on the AMCC PPC440SPe Processors.
  29. * Based on the Intel Xscale(R) family of I/O Processors (IOP 32x, 33x, 134x)
  30. * ADMA driver written by D.Williams.
  31. */
  32. #include <linux/init.h>
  33. #include <linux/module.h>
  34. #include <linux/async_tx.h>
  35. #include <linux/delay.h>
  36. #include <linux/dma-mapping.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/slab.h>
  40. #include <linux/uaccess.h>
  41. #include <linux/proc_fs.h>
  42. #include <linux/of.h>
  43. #include <linux/of_platform.h>
  44. #include <asm/dcr.h>
  45. #include <asm/dcr-regs.h>
  46. #include "adma.h"
  47. #include "../dmaengine.h"
  48. enum ppc_adma_init_code {
  49. PPC_ADMA_INIT_OK = 0,
  50. PPC_ADMA_INIT_MEMRES,
  51. PPC_ADMA_INIT_MEMREG,
  52. PPC_ADMA_INIT_ALLOC,
  53. PPC_ADMA_INIT_COHERENT,
  54. PPC_ADMA_INIT_CHANNEL,
  55. PPC_ADMA_INIT_IRQ1,
  56. PPC_ADMA_INIT_IRQ2,
  57. PPC_ADMA_INIT_REGISTER
  58. };
  59. static char *ppc_adma_errors[] = {
  60. [PPC_ADMA_INIT_OK] = "ok",
  61. [PPC_ADMA_INIT_MEMRES] = "failed to get memory resource",
  62. [PPC_ADMA_INIT_MEMREG] = "failed to request memory region",
  63. [PPC_ADMA_INIT_ALLOC] = "failed to allocate memory for adev "
  64. "structure",
  65. [PPC_ADMA_INIT_COHERENT] = "failed to allocate coherent memory for "
  66. "hardware descriptors",
  67. [PPC_ADMA_INIT_CHANNEL] = "failed to allocate memory for channel",
  68. [PPC_ADMA_INIT_IRQ1] = "failed to request first irq",
  69. [PPC_ADMA_INIT_IRQ2] = "failed to request second irq",
  70. [PPC_ADMA_INIT_REGISTER] = "failed to register dma async device",
  71. };
  72. static enum ppc_adma_init_code
  73. ppc440spe_adma_devices[PPC440SPE_ADMA_ENGINES_NUM];
  74. struct ppc_dma_chan_ref {
  75. struct dma_chan *chan;
  76. struct list_head node;
  77. };
  78. /* The list of channels exported by ppc440spe ADMA */
  79. struct list_head
  80. ppc440spe_adma_chan_list = LIST_HEAD_INIT(ppc440spe_adma_chan_list);
  81. /* This flag is set when want to refetch the xor chain in the interrupt
  82. * handler
  83. */
  84. static u32 do_xor_refetch;
  85. /* Pointer to DMA0, DMA1 CP/CS FIFO */
  86. static void *ppc440spe_dma_fifo_buf;
  87. /* Pointers to last submitted to DMA0, DMA1 CDBs */
  88. static struct ppc440spe_adma_desc_slot *chan_last_sub[3];
  89. static struct ppc440spe_adma_desc_slot *chan_first_cdb[3];
  90. /* Pointer to last linked and submitted xor CB */
  91. static struct ppc440spe_adma_desc_slot *xor_last_linked;
  92. static struct ppc440spe_adma_desc_slot *xor_last_submit;
  93. /* This array is used in data-check operations for storing a pattern */
  94. static char ppc440spe_qword[16];
  95. static atomic_t ppc440spe_adma_err_irq_ref;
  96. static dcr_host_t ppc440spe_mq_dcr_host;
  97. static unsigned int ppc440spe_mq_dcr_len;
  98. /* Since RXOR operations use the common register (MQ0_CF2H) for setting-up
  99. * the block size in transactions, then we do not allow to activate more than
  100. * only one RXOR transactions simultaneously. So use this var to store
  101. * the information about is RXOR currently active (PPC440SPE_RXOR_RUN bit is
  102. * set) or not (PPC440SPE_RXOR_RUN is clear).
  103. */
  104. static unsigned long ppc440spe_rxor_state;
  105. /* These are used in enable & check routines
  106. */
  107. static u32 ppc440spe_r6_enabled;
  108. static struct ppc440spe_adma_chan *ppc440spe_r6_tchan;
  109. static struct completion ppc440spe_r6_test_comp;
  110. static int ppc440spe_adma_dma2rxor_prep_src(
  111. struct ppc440spe_adma_desc_slot *desc,
  112. struct ppc440spe_rxor *cursor, int index,
  113. int src_cnt, u32 addr);
  114. static void ppc440spe_adma_dma2rxor_set_src(
  115. struct ppc440spe_adma_desc_slot *desc,
  116. int index, dma_addr_t addr);
  117. static void ppc440spe_adma_dma2rxor_set_mult(
  118. struct ppc440spe_adma_desc_slot *desc,
  119. int index, u8 mult);
  120. #ifdef ADMA_LL_DEBUG
  121. #define ADMA_LL_DBG(x) ({ if (1) x; 0; })
  122. #else
  123. #define ADMA_LL_DBG(x) ({ if (0) x; 0; })
  124. #endif
  125. static void print_cb(struct ppc440spe_adma_chan *chan, void *block)
  126. {
  127. struct dma_cdb *cdb;
  128. struct xor_cb *cb;
  129. int i;
  130. switch (chan->device->id) {
  131. case 0:
  132. case 1:
  133. cdb = block;
  134. pr_debug("CDB at %p [%d]:\n"
  135. "\t attr 0x%02x opc 0x%02x cnt 0x%08x\n"
  136. "\t sg1u 0x%08x sg1l 0x%08x\n"
  137. "\t sg2u 0x%08x sg2l 0x%08x\n"
  138. "\t sg3u 0x%08x sg3l 0x%08x\n",
  139. cdb, chan->device->id,
  140. cdb->attr, cdb->opc, le32_to_cpu(cdb->cnt),
  141. le32_to_cpu(cdb->sg1u), le32_to_cpu(cdb->sg1l),
  142. le32_to_cpu(cdb->sg2u), le32_to_cpu(cdb->sg2l),
  143. le32_to_cpu(cdb->sg3u), le32_to_cpu(cdb->sg3l)
  144. );
  145. break;
  146. case 2:
  147. cb = block;
  148. pr_debug("CB at %p [%d]:\n"
  149. "\t cbc 0x%08x cbbc 0x%08x cbs 0x%08x\n"
  150. "\t cbtah 0x%08x cbtal 0x%08x\n"
  151. "\t cblah 0x%08x cblal 0x%08x\n",
  152. cb, chan->device->id,
  153. cb->cbc, cb->cbbc, cb->cbs,
  154. cb->cbtah, cb->cbtal,
  155. cb->cblah, cb->cblal);
  156. for (i = 0; i < 16; i++) {
  157. if (i && !cb->ops[i].h && !cb->ops[i].l)
  158. continue;
  159. pr_debug("\t ops[%2d]: h 0x%08x l 0x%08x\n",
  160. i, cb->ops[i].h, cb->ops[i].l);
  161. }
  162. break;
  163. }
  164. }
  165. static void print_cb_list(struct ppc440spe_adma_chan *chan,
  166. struct ppc440spe_adma_desc_slot *iter)
  167. {
  168. for (; iter; iter = iter->hw_next)
  169. print_cb(chan, iter->hw_desc);
  170. }
  171. static void prep_dma_xor_dbg(int id, dma_addr_t dst, dma_addr_t *src,
  172. unsigned int src_cnt)
  173. {
  174. int i;
  175. pr_debug("\n%s(%d):\nsrc: ", __func__, id);
  176. for (i = 0; i < src_cnt; i++)
  177. pr_debug("\t0x%016llx ", src[i]);
  178. pr_debug("dst:\n\t0x%016llx\n", dst);
  179. }
  180. static void prep_dma_pq_dbg(int id, dma_addr_t *dst, dma_addr_t *src,
  181. unsigned int src_cnt)
  182. {
  183. int i;
  184. pr_debug("\n%s(%d):\nsrc: ", __func__, id);
  185. for (i = 0; i < src_cnt; i++)
  186. pr_debug("\t0x%016llx ", src[i]);
  187. pr_debug("dst: ");
  188. for (i = 0; i < 2; i++)
  189. pr_debug("\t0x%016llx ", dst[i]);
  190. }
  191. static void prep_dma_pqzero_sum_dbg(int id, dma_addr_t *src,
  192. unsigned int src_cnt,
  193. const unsigned char *scf)
  194. {
  195. int i;
  196. pr_debug("\n%s(%d):\nsrc(coef): ", __func__, id);
  197. if (scf) {
  198. for (i = 0; i < src_cnt; i++)
  199. pr_debug("\t0x%016llx(0x%02x) ", src[i], scf[i]);
  200. } else {
  201. for (i = 0; i < src_cnt; i++)
  202. pr_debug("\t0x%016llx(no) ", src[i]);
  203. }
  204. pr_debug("dst: ");
  205. for (i = 0; i < 2; i++)
  206. pr_debug("\t0x%016llx ", src[src_cnt + i]);
  207. }
  208. /******************************************************************************
  209. * Command (Descriptor) Blocks low-level routines
  210. ******************************************************************************/
  211. /**
  212. * ppc440spe_desc_init_interrupt - initialize the descriptor for INTERRUPT
  213. * pseudo operation
  214. */
  215. static void ppc440spe_desc_init_interrupt(struct ppc440spe_adma_desc_slot *desc,
  216. struct ppc440spe_adma_chan *chan)
  217. {
  218. struct xor_cb *p;
  219. switch (chan->device->id) {
  220. case PPC440SPE_XOR_ID:
  221. p = desc->hw_desc;
  222. memset(desc->hw_desc, 0, sizeof(struct xor_cb));
  223. /* NOP with Command Block Complete Enable */
  224. p->cbc = XOR_CBCR_CBCE_BIT;
  225. break;
  226. case PPC440SPE_DMA0_ID:
  227. case PPC440SPE_DMA1_ID:
  228. memset(desc->hw_desc, 0, sizeof(struct dma_cdb));
  229. /* NOP with interrupt */
  230. set_bit(PPC440SPE_DESC_INT, &desc->flags);
  231. break;
  232. default:
  233. printk(KERN_ERR "Unsupported id %d in %s\n", chan->device->id,
  234. __func__);
  235. break;
  236. }
  237. }
  238. /**
  239. * ppc440spe_desc_init_null_xor - initialize the descriptor for NULL XOR
  240. * pseudo operation
  241. */
  242. static void ppc440spe_desc_init_null_xor(struct ppc440spe_adma_desc_slot *desc)
  243. {
  244. memset(desc->hw_desc, 0, sizeof(struct xor_cb));
  245. desc->hw_next = NULL;
  246. desc->src_cnt = 0;
  247. desc->dst_cnt = 1;
  248. }
  249. /**
  250. * ppc440spe_desc_init_xor - initialize the descriptor for XOR operation
  251. */
  252. static void ppc440spe_desc_init_xor(struct ppc440spe_adma_desc_slot *desc,
  253. int src_cnt, unsigned long flags)
  254. {
  255. struct xor_cb *hw_desc = desc->hw_desc;
  256. memset(desc->hw_desc, 0, sizeof(struct xor_cb));
  257. desc->hw_next = NULL;
  258. desc->src_cnt = src_cnt;
  259. desc->dst_cnt = 1;
  260. hw_desc->cbc = XOR_CBCR_TGT_BIT | src_cnt;
  261. if (flags & DMA_PREP_INTERRUPT)
  262. /* Enable interrupt on completion */
  263. hw_desc->cbc |= XOR_CBCR_CBCE_BIT;
  264. }
  265. /**
  266. * ppc440spe_desc_init_dma2pq - initialize the descriptor for PQ
  267. * operation in DMA2 controller
  268. */
  269. static void ppc440spe_desc_init_dma2pq(struct ppc440spe_adma_desc_slot *desc,
  270. int dst_cnt, int src_cnt, unsigned long flags)
  271. {
  272. struct xor_cb *hw_desc = desc->hw_desc;
  273. memset(desc->hw_desc, 0, sizeof(struct xor_cb));
  274. desc->hw_next = NULL;
  275. desc->src_cnt = src_cnt;
  276. desc->dst_cnt = dst_cnt;
  277. memset(desc->reverse_flags, 0, sizeof(desc->reverse_flags));
  278. desc->descs_per_op = 0;
  279. hw_desc->cbc = XOR_CBCR_TGT_BIT;
  280. if (flags & DMA_PREP_INTERRUPT)
  281. /* Enable interrupt on completion */
  282. hw_desc->cbc |= XOR_CBCR_CBCE_BIT;
  283. }
  284. #define DMA_CTRL_FLAGS_LAST DMA_PREP_FENCE
  285. #define DMA_PREP_ZERO_P (DMA_CTRL_FLAGS_LAST << 1)
  286. #define DMA_PREP_ZERO_Q (DMA_PREP_ZERO_P << 1)
  287. /**
  288. * ppc440spe_desc_init_dma01pq - initialize the descriptors for PQ operation
  289. * with DMA0/1
  290. */
  291. static void ppc440spe_desc_init_dma01pq(struct ppc440spe_adma_desc_slot *desc,
  292. int dst_cnt, int src_cnt, unsigned long flags,
  293. unsigned long op)
  294. {
  295. struct dma_cdb *hw_desc;
  296. struct ppc440spe_adma_desc_slot *iter;
  297. u8 dopc;
  298. /* Common initialization of a PQ descriptors chain */
  299. set_bits(op, &desc->flags);
  300. desc->src_cnt = src_cnt;
  301. desc->dst_cnt = dst_cnt;
  302. /* WXOR MULTICAST if both P and Q are being computed
  303. * MV_SG1_SG2 if Q only
  304. */
  305. dopc = (desc->dst_cnt == DMA_DEST_MAX_NUM) ?
  306. DMA_CDB_OPC_MULTICAST : DMA_CDB_OPC_MV_SG1_SG2;
  307. list_for_each_entry(iter, &desc->group_list, chain_node) {
  308. hw_desc = iter->hw_desc;
  309. memset(iter->hw_desc, 0, sizeof(struct dma_cdb));
  310. if (likely(!list_is_last(&iter->chain_node,
  311. &desc->group_list))) {
  312. /* set 'next' pointer */
  313. iter->hw_next = list_entry(iter->chain_node.next,
  314. struct ppc440spe_adma_desc_slot, chain_node);
  315. clear_bit(PPC440SPE_DESC_INT, &iter->flags);
  316. } else {
  317. /* this is the last descriptor.
  318. * this slot will be pasted from ADMA level
  319. * each time it wants to configure parameters
  320. * of the transaction (src, dst, ...)
  321. */
  322. iter->hw_next = NULL;
  323. if (flags & DMA_PREP_INTERRUPT)
  324. set_bit(PPC440SPE_DESC_INT, &iter->flags);
  325. else
  326. clear_bit(PPC440SPE_DESC_INT, &iter->flags);
  327. }
  328. }
  329. /* Set OPS depending on WXOR/RXOR type of operation */
  330. if (!test_bit(PPC440SPE_DESC_RXOR, &desc->flags)) {
  331. /* This is a WXOR only chain:
  332. * - first descriptors are for zeroing destinations
  333. * if PPC440SPE_ZERO_P/Q set;
  334. * - descriptors remained are for GF-XOR operations.
  335. */
  336. iter = list_first_entry(&desc->group_list,
  337. struct ppc440spe_adma_desc_slot,
  338. chain_node);
  339. if (test_bit(PPC440SPE_ZERO_P, &desc->flags)) {
  340. hw_desc = iter->hw_desc;
  341. hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
  342. iter = list_first_entry(&iter->chain_node,
  343. struct ppc440spe_adma_desc_slot,
  344. chain_node);
  345. }
  346. if (test_bit(PPC440SPE_ZERO_Q, &desc->flags)) {
  347. hw_desc = iter->hw_desc;
  348. hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
  349. iter = list_first_entry(&iter->chain_node,
  350. struct ppc440spe_adma_desc_slot,
  351. chain_node);
  352. }
  353. list_for_each_entry_from(iter, &desc->group_list, chain_node) {
  354. hw_desc = iter->hw_desc;
  355. hw_desc->opc = dopc;
  356. }
  357. } else {
  358. /* This is either RXOR-only or mixed RXOR/WXOR */
  359. /* The first 1 or 2 slots in chain are always RXOR,
  360. * if need to calculate P & Q, then there are two
  361. * RXOR slots; if only P or only Q, then there is one
  362. */
  363. iter = list_first_entry(&desc->group_list,
  364. struct ppc440spe_adma_desc_slot,
  365. chain_node);
  366. hw_desc = iter->hw_desc;
  367. hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
  368. if (desc->dst_cnt == DMA_DEST_MAX_NUM) {
  369. iter = list_first_entry(&iter->chain_node,
  370. struct ppc440spe_adma_desc_slot,
  371. chain_node);
  372. hw_desc = iter->hw_desc;
  373. hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
  374. }
  375. /* The remaining descs (if any) are WXORs */
  376. if (test_bit(PPC440SPE_DESC_WXOR, &desc->flags)) {
  377. iter = list_first_entry(&iter->chain_node,
  378. struct ppc440spe_adma_desc_slot,
  379. chain_node);
  380. list_for_each_entry_from(iter, &desc->group_list,
  381. chain_node) {
  382. hw_desc = iter->hw_desc;
  383. hw_desc->opc = dopc;
  384. }
  385. }
  386. }
  387. }
  388. /**
  389. * ppc440spe_desc_init_dma01pqzero_sum - initialize the descriptor
  390. * for PQ_ZERO_SUM operation
  391. */
  392. static void ppc440spe_desc_init_dma01pqzero_sum(
  393. struct ppc440spe_adma_desc_slot *desc,
  394. int dst_cnt, int src_cnt)
  395. {
  396. struct dma_cdb *hw_desc;
  397. struct ppc440spe_adma_desc_slot *iter;
  398. int i = 0;
  399. u8 dopc = (dst_cnt == 2) ? DMA_CDB_OPC_MULTICAST :
  400. DMA_CDB_OPC_MV_SG1_SG2;
  401. /*
  402. * Initialize starting from 2nd or 3rd descriptor dependent
  403. * on dst_cnt. First one or two slots are for cloning P
  404. * and/or Q to chan->pdest and/or chan->qdest as we have
  405. * to preserve original P/Q.
  406. */
  407. iter = list_first_entry(&desc->group_list,
  408. struct ppc440spe_adma_desc_slot, chain_node);
  409. iter = list_entry(iter->chain_node.next,
  410. struct ppc440spe_adma_desc_slot, chain_node);
  411. if (dst_cnt > 1) {
  412. iter = list_entry(iter->chain_node.next,
  413. struct ppc440spe_adma_desc_slot, chain_node);
  414. }
  415. /* initialize each source descriptor in chain */
  416. list_for_each_entry_from(iter, &desc->group_list, chain_node) {
  417. hw_desc = iter->hw_desc;
  418. memset(iter->hw_desc, 0, sizeof(struct dma_cdb));
  419. iter->src_cnt = 0;
  420. iter->dst_cnt = 0;
  421. /* This is a ZERO_SUM operation:
  422. * - <src_cnt> descriptors starting from 2nd or 3rd
  423. * descriptor are for GF-XOR operations;
  424. * - remaining <dst_cnt> descriptors are for checking the result
  425. */
  426. if (i++ < src_cnt)
  427. /* MV_SG1_SG2 if only Q is being verified
  428. * MULTICAST if both P and Q are being verified
  429. */
  430. hw_desc->opc = dopc;
  431. else
  432. /* DMA_CDB_OPC_DCHECK128 operation */
  433. hw_desc->opc = DMA_CDB_OPC_DCHECK128;
  434. if (likely(!list_is_last(&iter->chain_node,
  435. &desc->group_list))) {
  436. /* set 'next' pointer */
  437. iter->hw_next = list_entry(iter->chain_node.next,
  438. struct ppc440spe_adma_desc_slot,
  439. chain_node);
  440. } else {
  441. /* this is the last descriptor.
  442. * this slot will be pasted from ADMA level
  443. * each time it wants to configure parameters
  444. * of the transaction (src, dst, ...)
  445. */
  446. iter->hw_next = NULL;
  447. /* always enable interrupt generation since we get
  448. * the status of pqzero from the handler
  449. */
  450. set_bit(PPC440SPE_DESC_INT, &iter->flags);
  451. }
  452. }
  453. desc->src_cnt = src_cnt;
  454. desc->dst_cnt = dst_cnt;
  455. }
  456. /**
  457. * ppc440spe_desc_init_memcpy - initialize the descriptor for MEMCPY operation
  458. */
  459. static void ppc440spe_desc_init_memcpy(struct ppc440spe_adma_desc_slot *desc,
  460. unsigned long flags)
  461. {
  462. struct dma_cdb *hw_desc = desc->hw_desc;
  463. memset(desc->hw_desc, 0, sizeof(struct dma_cdb));
  464. desc->hw_next = NULL;
  465. desc->src_cnt = 1;
  466. desc->dst_cnt = 1;
  467. if (flags & DMA_PREP_INTERRUPT)
  468. set_bit(PPC440SPE_DESC_INT, &desc->flags);
  469. else
  470. clear_bit(PPC440SPE_DESC_INT, &desc->flags);
  471. hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
  472. }
  473. /**
  474. * ppc440spe_desc_init_memset - initialize the descriptor for MEMSET operation
  475. */
  476. static void ppc440spe_desc_init_memset(struct ppc440spe_adma_desc_slot *desc,
  477. int value, unsigned long flags)
  478. {
  479. struct dma_cdb *hw_desc = desc->hw_desc;
  480. memset(desc->hw_desc, 0, sizeof(struct dma_cdb));
  481. desc->hw_next = NULL;
  482. desc->src_cnt = 1;
  483. desc->dst_cnt = 1;
  484. if (flags & DMA_PREP_INTERRUPT)
  485. set_bit(PPC440SPE_DESC_INT, &desc->flags);
  486. else
  487. clear_bit(PPC440SPE_DESC_INT, &desc->flags);
  488. hw_desc->sg1u = hw_desc->sg1l = cpu_to_le32((u32)value);
  489. hw_desc->sg3u = hw_desc->sg3l = cpu_to_le32((u32)value);
  490. hw_desc->opc = DMA_CDB_OPC_DFILL128;
  491. }
  492. /**
  493. * ppc440spe_desc_set_src_addr - set source address into the descriptor
  494. */
  495. static void ppc440spe_desc_set_src_addr(struct ppc440spe_adma_desc_slot *desc,
  496. struct ppc440spe_adma_chan *chan,
  497. int src_idx, dma_addr_t addrh,
  498. dma_addr_t addrl)
  499. {
  500. struct dma_cdb *dma_hw_desc;
  501. struct xor_cb *xor_hw_desc;
  502. phys_addr_t addr64, tmplow, tmphi;
  503. switch (chan->device->id) {
  504. case PPC440SPE_DMA0_ID:
  505. case PPC440SPE_DMA1_ID:
  506. if (!addrh) {
  507. addr64 = addrl;
  508. tmphi = (addr64 >> 32);
  509. tmplow = (addr64 & 0xFFFFFFFF);
  510. } else {
  511. tmphi = addrh;
  512. tmplow = addrl;
  513. }
  514. dma_hw_desc = desc->hw_desc;
  515. dma_hw_desc->sg1l = cpu_to_le32((u32)tmplow);
  516. dma_hw_desc->sg1u |= cpu_to_le32((u32)tmphi);
  517. break;
  518. case PPC440SPE_XOR_ID:
  519. xor_hw_desc = desc->hw_desc;
  520. xor_hw_desc->ops[src_idx].l = addrl;
  521. xor_hw_desc->ops[src_idx].h |= addrh;
  522. break;
  523. }
  524. }
  525. /**
  526. * ppc440spe_desc_set_src_mult - set source address mult into the descriptor
  527. */
  528. static void ppc440spe_desc_set_src_mult(struct ppc440spe_adma_desc_slot *desc,
  529. struct ppc440spe_adma_chan *chan, u32 mult_index,
  530. int sg_index, unsigned char mult_value)
  531. {
  532. struct dma_cdb *dma_hw_desc;
  533. struct xor_cb *xor_hw_desc;
  534. u32 *psgu;
  535. switch (chan->device->id) {
  536. case PPC440SPE_DMA0_ID:
  537. case PPC440SPE_DMA1_ID:
  538. dma_hw_desc = desc->hw_desc;
  539. switch (sg_index) {
  540. /* for RXOR operations set multiplier
  541. * into source cued address
  542. */
  543. case DMA_CDB_SG_SRC:
  544. psgu = &dma_hw_desc->sg1u;
  545. break;
  546. /* for WXOR operations set multiplier
  547. * into destination cued address(es)
  548. */
  549. case DMA_CDB_SG_DST1:
  550. psgu = &dma_hw_desc->sg2u;
  551. break;
  552. case DMA_CDB_SG_DST2:
  553. psgu = &dma_hw_desc->sg3u;
  554. break;
  555. default:
  556. BUG();
  557. }
  558. *psgu |= cpu_to_le32(mult_value << mult_index);
  559. break;
  560. case PPC440SPE_XOR_ID:
  561. xor_hw_desc = desc->hw_desc;
  562. break;
  563. default:
  564. BUG();
  565. }
  566. }
  567. /**
  568. * ppc440spe_desc_set_dest_addr - set destination address into the descriptor
  569. */
  570. static void ppc440spe_desc_set_dest_addr(struct ppc440spe_adma_desc_slot *desc,
  571. struct ppc440spe_adma_chan *chan,
  572. dma_addr_t addrh, dma_addr_t addrl,
  573. u32 dst_idx)
  574. {
  575. struct dma_cdb *dma_hw_desc;
  576. struct xor_cb *xor_hw_desc;
  577. phys_addr_t addr64, tmphi, tmplow;
  578. u32 *psgu, *psgl;
  579. switch (chan->device->id) {
  580. case PPC440SPE_DMA0_ID:
  581. case PPC440SPE_DMA1_ID:
  582. if (!addrh) {
  583. addr64 = addrl;
  584. tmphi = (addr64 >> 32);
  585. tmplow = (addr64 & 0xFFFFFFFF);
  586. } else {
  587. tmphi = addrh;
  588. tmplow = addrl;
  589. }
  590. dma_hw_desc = desc->hw_desc;
  591. psgu = dst_idx ? &dma_hw_desc->sg3u : &dma_hw_desc->sg2u;
  592. psgl = dst_idx ? &dma_hw_desc->sg3l : &dma_hw_desc->sg2l;
  593. *psgl = cpu_to_le32((u32)tmplow);
  594. *psgu |= cpu_to_le32((u32)tmphi);
  595. break;
  596. case PPC440SPE_XOR_ID:
  597. xor_hw_desc = desc->hw_desc;
  598. xor_hw_desc->cbtal = addrl;
  599. xor_hw_desc->cbtah |= addrh;
  600. break;
  601. }
  602. }
  603. /**
  604. * ppc440spe_desc_set_byte_count - set number of data bytes involved
  605. * into the operation
  606. */
  607. static void ppc440spe_desc_set_byte_count(struct ppc440spe_adma_desc_slot *desc,
  608. struct ppc440spe_adma_chan *chan,
  609. u32 byte_count)
  610. {
  611. struct dma_cdb *dma_hw_desc;
  612. struct xor_cb *xor_hw_desc;
  613. switch (chan->device->id) {
  614. case PPC440SPE_DMA0_ID:
  615. case PPC440SPE_DMA1_ID:
  616. dma_hw_desc = desc->hw_desc;
  617. dma_hw_desc->cnt = cpu_to_le32(byte_count);
  618. break;
  619. case PPC440SPE_XOR_ID:
  620. xor_hw_desc = desc->hw_desc;
  621. xor_hw_desc->cbbc = byte_count;
  622. break;
  623. }
  624. }
  625. /**
  626. * ppc440spe_desc_set_rxor_block_size - set RXOR block size
  627. */
  628. static inline void ppc440spe_desc_set_rxor_block_size(u32 byte_count)
  629. {
  630. /* assume that byte_count is aligned on the 512-boundary;
  631. * thus write it directly to the register (bits 23:31 are
  632. * reserved there).
  633. */
  634. dcr_write(ppc440spe_mq_dcr_host, DCRN_MQ0_CF2H, byte_count);
  635. }
  636. /**
  637. * ppc440spe_desc_set_dcheck - set CHECK pattern
  638. */
  639. static void ppc440spe_desc_set_dcheck(struct ppc440spe_adma_desc_slot *desc,
  640. struct ppc440spe_adma_chan *chan, u8 *qword)
  641. {
  642. struct dma_cdb *dma_hw_desc;
  643. switch (chan->device->id) {
  644. case PPC440SPE_DMA0_ID:
  645. case PPC440SPE_DMA1_ID:
  646. dma_hw_desc = desc->hw_desc;
  647. iowrite32(qword[0], &dma_hw_desc->sg3l);
  648. iowrite32(qword[4], &dma_hw_desc->sg3u);
  649. iowrite32(qword[8], &dma_hw_desc->sg2l);
  650. iowrite32(qword[12], &dma_hw_desc->sg2u);
  651. break;
  652. default:
  653. BUG();
  654. }
  655. }
  656. /**
  657. * ppc440spe_xor_set_link - set link address in xor CB
  658. */
  659. static void ppc440spe_xor_set_link(struct ppc440spe_adma_desc_slot *prev_desc,
  660. struct ppc440spe_adma_desc_slot *next_desc)
  661. {
  662. struct xor_cb *xor_hw_desc = prev_desc->hw_desc;
  663. if (unlikely(!next_desc || !(next_desc->phys))) {
  664. printk(KERN_ERR "%s: next_desc=0x%p; next_desc->phys=0x%llx\n",
  665. __func__, next_desc,
  666. next_desc ? next_desc->phys : 0);
  667. BUG();
  668. }
  669. xor_hw_desc->cbs = 0;
  670. xor_hw_desc->cblal = next_desc->phys;
  671. xor_hw_desc->cblah = 0;
  672. xor_hw_desc->cbc |= XOR_CBCR_LNK_BIT;
  673. }
  674. /**
  675. * ppc440spe_desc_set_link - set the address of descriptor following this
  676. * descriptor in chain
  677. */
  678. static void ppc440spe_desc_set_link(struct ppc440spe_adma_chan *chan,
  679. struct ppc440spe_adma_desc_slot *prev_desc,
  680. struct ppc440spe_adma_desc_slot *next_desc)
  681. {
  682. unsigned long flags;
  683. struct ppc440spe_adma_desc_slot *tail = next_desc;
  684. if (unlikely(!prev_desc || !next_desc ||
  685. (prev_desc->hw_next && prev_desc->hw_next != next_desc))) {
  686. /* If previous next is overwritten something is wrong.
  687. * though we may refetch from append to initiate list
  688. * processing; in this case - it's ok.
  689. */
  690. printk(KERN_ERR "%s: prev_desc=0x%p; next_desc=0x%p; "
  691. "prev->hw_next=0x%p\n", __func__, prev_desc,
  692. next_desc, prev_desc ? prev_desc->hw_next : 0);
  693. BUG();
  694. }
  695. local_irq_save(flags);
  696. /* do s/w chaining both for DMA and XOR descriptors */
  697. prev_desc->hw_next = next_desc;
  698. switch (chan->device->id) {
  699. case PPC440SPE_DMA0_ID:
  700. case PPC440SPE_DMA1_ID:
  701. break;
  702. case PPC440SPE_XOR_ID:
  703. /* bind descriptor to the chain */
  704. while (tail->hw_next)
  705. tail = tail->hw_next;
  706. xor_last_linked = tail;
  707. if (prev_desc == xor_last_submit)
  708. /* do not link to the last submitted CB */
  709. break;
  710. ppc440spe_xor_set_link(prev_desc, next_desc);
  711. break;
  712. }
  713. local_irq_restore(flags);
  714. }
  715. /**
  716. * ppc440spe_desc_get_src_addr - extract the source address from the descriptor
  717. */
  718. static u32 ppc440spe_desc_get_src_addr(struct ppc440spe_adma_desc_slot *desc,
  719. struct ppc440spe_adma_chan *chan, int src_idx)
  720. {
  721. struct dma_cdb *dma_hw_desc;
  722. struct xor_cb *xor_hw_desc;
  723. switch (chan->device->id) {
  724. case PPC440SPE_DMA0_ID:
  725. case PPC440SPE_DMA1_ID:
  726. dma_hw_desc = desc->hw_desc;
  727. /* May have 0, 1, 2, or 3 sources */
  728. switch (dma_hw_desc->opc) {
  729. case DMA_CDB_OPC_NO_OP:
  730. case DMA_CDB_OPC_DFILL128:
  731. return 0;
  732. case DMA_CDB_OPC_DCHECK128:
  733. if (unlikely(src_idx)) {
  734. printk(KERN_ERR "%s: try to get %d source for"
  735. " DCHECK128\n", __func__, src_idx);
  736. BUG();
  737. }
  738. return le32_to_cpu(dma_hw_desc->sg1l);
  739. case DMA_CDB_OPC_MULTICAST:
  740. case DMA_CDB_OPC_MV_SG1_SG2:
  741. if (unlikely(src_idx > 2)) {
  742. printk(KERN_ERR "%s: try to get %d source from"
  743. " DMA descr\n", __func__, src_idx);
  744. BUG();
  745. }
  746. if (src_idx) {
  747. if (le32_to_cpu(dma_hw_desc->sg1u) &
  748. DMA_CUED_XOR_WIN_MSK) {
  749. u8 region;
  750. if (src_idx == 1)
  751. return le32_to_cpu(
  752. dma_hw_desc->sg1l) +
  753. desc->unmap_len;
  754. region = (le32_to_cpu(
  755. dma_hw_desc->sg1u)) >>
  756. DMA_CUED_REGION_OFF;
  757. region &= DMA_CUED_REGION_MSK;
  758. switch (region) {
  759. case DMA_RXOR123:
  760. return le32_to_cpu(
  761. dma_hw_desc->sg1l) +
  762. (desc->unmap_len << 1);
  763. case DMA_RXOR124:
  764. return le32_to_cpu(
  765. dma_hw_desc->sg1l) +
  766. (desc->unmap_len * 3);
  767. case DMA_RXOR125:
  768. return le32_to_cpu(
  769. dma_hw_desc->sg1l) +
  770. (desc->unmap_len << 2);
  771. default:
  772. printk(KERN_ERR
  773. "%s: try to"
  774. " get src3 for region %02x"
  775. "PPC440SPE_DESC_RXOR12?\n",
  776. __func__, region);
  777. BUG();
  778. }
  779. } else {
  780. printk(KERN_ERR
  781. "%s: try to get %d"
  782. " source for non-cued descr\n",
  783. __func__, src_idx);
  784. BUG();
  785. }
  786. }
  787. return le32_to_cpu(dma_hw_desc->sg1l);
  788. default:
  789. printk(KERN_ERR "%s: unknown OPC 0x%02x\n",
  790. __func__, dma_hw_desc->opc);
  791. BUG();
  792. }
  793. return le32_to_cpu(dma_hw_desc->sg1l);
  794. case PPC440SPE_XOR_ID:
  795. /* May have up to 16 sources */
  796. xor_hw_desc = desc->hw_desc;
  797. return xor_hw_desc->ops[src_idx].l;
  798. }
  799. return 0;
  800. }
  801. /**
  802. * ppc440spe_desc_get_dest_addr - extract the destination address from the
  803. * descriptor
  804. */
  805. static u32 ppc440spe_desc_get_dest_addr(struct ppc440spe_adma_desc_slot *desc,
  806. struct ppc440spe_adma_chan *chan, int idx)
  807. {
  808. struct dma_cdb *dma_hw_desc;
  809. struct xor_cb *xor_hw_desc;
  810. switch (chan->device->id) {
  811. case PPC440SPE_DMA0_ID:
  812. case PPC440SPE_DMA1_ID:
  813. dma_hw_desc = desc->hw_desc;
  814. if (likely(!idx))
  815. return le32_to_cpu(dma_hw_desc->sg2l);
  816. return le32_to_cpu(dma_hw_desc->sg3l);
  817. case PPC440SPE_XOR_ID:
  818. xor_hw_desc = desc->hw_desc;
  819. return xor_hw_desc->cbtal;
  820. }
  821. return 0;
  822. }
  823. /**
  824. * ppc440spe_desc_get_src_num - extract the number of source addresses from
  825. * the descriptor
  826. */
  827. static u32 ppc440spe_desc_get_src_num(struct ppc440spe_adma_desc_slot *desc,
  828. struct ppc440spe_adma_chan *chan)
  829. {
  830. struct dma_cdb *dma_hw_desc;
  831. struct xor_cb *xor_hw_desc;
  832. switch (chan->device->id) {
  833. case PPC440SPE_DMA0_ID:
  834. case PPC440SPE_DMA1_ID:
  835. dma_hw_desc = desc->hw_desc;
  836. switch (dma_hw_desc->opc) {
  837. case DMA_CDB_OPC_NO_OP:
  838. case DMA_CDB_OPC_DFILL128:
  839. return 0;
  840. case DMA_CDB_OPC_DCHECK128:
  841. return 1;
  842. case DMA_CDB_OPC_MV_SG1_SG2:
  843. case DMA_CDB_OPC_MULTICAST:
  844. /*
  845. * Only for RXOR operations we have more than
  846. * one source
  847. */
  848. if (le32_to_cpu(dma_hw_desc->sg1u) &
  849. DMA_CUED_XOR_WIN_MSK) {
  850. /* RXOR op, there are 2 or 3 sources */
  851. if (((le32_to_cpu(dma_hw_desc->sg1u) >>
  852. DMA_CUED_REGION_OFF) &
  853. DMA_CUED_REGION_MSK) == DMA_RXOR12) {
  854. /* RXOR 1-2 */
  855. return 2;
  856. } else {
  857. /* RXOR 1-2-3/1-2-4/1-2-5 */
  858. return 3;
  859. }
  860. }
  861. return 1;
  862. default:
  863. printk(KERN_ERR "%s: unknown OPC 0x%02x\n",
  864. __func__, dma_hw_desc->opc);
  865. BUG();
  866. }
  867. case PPC440SPE_XOR_ID:
  868. /* up to 16 sources */
  869. xor_hw_desc = desc->hw_desc;
  870. return xor_hw_desc->cbc & XOR_CDCR_OAC_MSK;
  871. default:
  872. BUG();
  873. }
  874. return 0;
  875. }
  876. /**
  877. * ppc440spe_desc_get_dst_num - get the number of destination addresses in
  878. * this descriptor
  879. */
  880. static u32 ppc440spe_desc_get_dst_num(struct ppc440spe_adma_desc_slot *desc,
  881. struct ppc440spe_adma_chan *chan)
  882. {
  883. struct dma_cdb *dma_hw_desc;
  884. switch (chan->device->id) {
  885. case PPC440SPE_DMA0_ID:
  886. case PPC440SPE_DMA1_ID:
  887. /* May be 1 or 2 destinations */
  888. dma_hw_desc = desc->hw_desc;
  889. switch (dma_hw_desc->opc) {
  890. case DMA_CDB_OPC_NO_OP:
  891. case DMA_CDB_OPC_DCHECK128:
  892. return 0;
  893. case DMA_CDB_OPC_MV_SG1_SG2:
  894. case DMA_CDB_OPC_DFILL128:
  895. return 1;
  896. case DMA_CDB_OPC_MULTICAST:
  897. if (desc->dst_cnt == 2)
  898. return 2;
  899. else
  900. return 1;
  901. default:
  902. printk(KERN_ERR "%s: unknown OPC 0x%02x\n",
  903. __func__, dma_hw_desc->opc);
  904. BUG();
  905. }
  906. case PPC440SPE_XOR_ID:
  907. /* Always only 1 destination */
  908. return 1;
  909. default:
  910. BUG();
  911. }
  912. return 0;
  913. }
  914. /**
  915. * ppc440spe_desc_get_link - get the address of the descriptor that
  916. * follows this one
  917. */
  918. static inline u32 ppc440spe_desc_get_link(struct ppc440spe_adma_desc_slot *desc,
  919. struct ppc440spe_adma_chan *chan)
  920. {
  921. if (!desc->hw_next)
  922. return 0;
  923. return desc->hw_next->phys;
  924. }
  925. /**
  926. * ppc440spe_desc_is_aligned - check alignment
  927. */
  928. static inline int ppc440spe_desc_is_aligned(
  929. struct ppc440spe_adma_desc_slot *desc, int num_slots)
  930. {
  931. return (desc->idx & (num_slots - 1)) ? 0 : 1;
  932. }
  933. /**
  934. * ppc440spe_chan_xor_slot_count - get the number of slots necessary for
  935. * XOR operation
  936. */
  937. static int ppc440spe_chan_xor_slot_count(size_t len, int src_cnt,
  938. int *slots_per_op)
  939. {
  940. int slot_cnt;
  941. /* each XOR descriptor provides up to 16 source operands */
  942. slot_cnt = *slots_per_op = (src_cnt + XOR_MAX_OPS - 1)/XOR_MAX_OPS;
  943. if (likely(len <= PPC440SPE_ADMA_XOR_MAX_BYTE_COUNT))
  944. return slot_cnt;
  945. printk(KERN_ERR "%s: len %d > max %d !!\n",
  946. __func__, len, PPC440SPE_ADMA_XOR_MAX_BYTE_COUNT);
  947. BUG();
  948. return slot_cnt;
  949. }
  950. /**
  951. * ppc440spe_dma2_pq_slot_count - get the number of slots necessary for
  952. * DMA2 PQ operation
  953. */
  954. static int ppc440spe_dma2_pq_slot_count(dma_addr_t *srcs,
  955. int src_cnt, size_t len)
  956. {
  957. signed long long order = 0;
  958. int state = 0;
  959. int addr_count = 0;
  960. int i;
  961. for (i = 1; i < src_cnt; i++) {
  962. dma_addr_t cur_addr = srcs[i];
  963. dma_addr_t old_addr = srcs[i-1];
  964. switch (state) {
  965. case 0:
  966. if (cur_addr == old_addr + len) {
  967. /* direct RXOR */
  968. order = 1;
  969. state = 1;
  970. if (i == src_cnt-1)
  971. addr_count++;
  972. } else if (old_addr == cur_addr + len) {
  973. /* reverse RXOR */
  974. order = -1;
  975. state = 1;
  976. if (i == src_cnt-1)
  977. addr_count++;
  978. } else {
  979. state = 3;
  980. }
  981. break;
  982. case 1:
  983. if (i == src_cnt-2 || (order == -1
  984. && cur_addr != old_addr - len)) {
  985. order = 0;
  986. state = 0;
  987. addr_count++;
  988. } else if (cur_addr == old_addr + len*order) {
  989. state = 2;
  990. if (i == src_cnt-1)
  991. addr_count++;
  992. } else if (cur_addr == old_addr + 2*len) {
  993. state = 2;
  994. if (i == src_cnt-1)
  995. addr_count++;
  996. } else if (cur_addr == old_addr + 3*len) {
  997. state = 2;
  998. if (i == src_cnt-1)
  999. addr_count++;
  1000. } else {
  1001. order = 0;
  1002. state = 0;
  1003. addr_count++;
  1004. }
  1005. break;
  1006. case 2:
  1007. order = 0;
  1008. state = 0;
  1009. addr_count++;
  1010. break;
  1011. }
  1012. if (state == 3)
  1013. break;
  1014. }
  1015. if (src_cnt <= 1 || (state != 1 && state != 2)) {
  1016. pr_err("%s: src_cnt=%d, state=%d, addr_count=%d, order=%lld\n",
  1017. __func__, src_cnt, state, addr_count, order);
  1018. for (i = 0; i < src_cnt; i++)
  1019. pr_err("\t[%d] 0x%llx \n", i, srcs[i]);
  1020. BUG();
  1021. }
  1022. return (addr_count + XOR_MAX_OPS - 1) / XOR_MAX_OPS;
  1023. }
  1024. /******************************************************************************
  1025. * ADMA channel low-level routines
  1026. ******************************************************************************/
  1027. static u32
  1028. ppc440spe_chan_get_current_descriptor(struct ppc440spe_adma_chan *chan);
  1029. static void ppc440spe_chan_append(struct ppc440spe_adma_chan *chan);
  1030. /**
  1031. * ppc440spe_adma_device_clear_eot_status - interrupt ack to XOR or DMA engine
  1032. */
  1033. static void ppc440spe_adma_device_clear_eot_status(
  1034. struct ppc440spe_adma_chan *chan)
  1035. {
  1036. struct dma_regs *dma_reg;
  1037. struct xor_regs *xor_reg;
  1038. u8 *p = chan->device->dma_desc_pool_virt;
  1039. struct dma_cdb *cdb;
  1040. u32 rv, i;
  1041. switch (chan->device->id) {
  1042. case PPC440SPE_DMA0_ID:
  1043. case PPC440SPE_DMA1_ID:
  1044. /* read FIFO to ack */
  1045. dma_reg = chan->device->dma_reg;
  1046. while ((rv = ioread32(&dma_reg->csfpl))) {
  1047. i = rv & DMA_CDB_ADDR_MSK;
  1048. cdb = (struct dma_cdb *)&p[i -
  1049. (u32)chan->device->dma_desc_pool];
  1050. /* Clear opcode to ack. This is necessary for
  1051. * ZeroSum operations only
  1052. */
  1053. cdb->opc = 0;
  1054. if (test_bit(PPC440SPE_RXOR_RUN,
  1055. &ppc440spe_rxor_state)) {
  1056. /* probably this is a completed RXOR op,
  1057. * get pointer to CDB using the fact that
  1058. * physical and virtual addresses of CDB
  1059. * in pools have the same offsets
  1060. */
  1061. if (le32_to_cpu(cdb->sg1u) &
  1062. DMA_CUED_XOR_BASE) {
  1063. /* this is a RXOR */
  1064. clear_bit(PPC440SPE_RXOR_RUN,
  1065. &ppc440spe_rxor_state);
  1066. }
  1067. }
  1068. if (rv & DMA_CDB_STATUS_MSK) {
  1069. /* ZeroSum check failed
  1070. */
  1071. struct ppc440spe_adma_desc_slot *iter;
  1072. dma_addr_t phys = rv & ~DMA_CDB_MSK;
  1073. /*
  1074. * Update the status of corresponding
  1075. * descriptor.
  1076. */
  1077. list_for_each_entry(iter, &chan->chain,
  1078. chain_node) {
  1079. if (iter->phys == phys)
  1080. break;
  1081. }
  1082. /*
  1083. * if cannot find the corresponding
  1084. * slot it's a bug
  1085. */
  1086. BUG_ON(&iter->chain_node == &chan->chain);
  1087. if (iter->xor_check_result) {
  1088. if (test_bit(PPC440SPE_DESC_PCHECK,
  1089. &iter->flags)) {
  1090. *iter->xor_check_result |=
  1091. SUM_CHECK_P_RESULT;
  1092. } else
  1093. if (test_bit(PPC440SPE_DESC_QCHECK,
  1094. &iter->flags)) {
  1095. *iter->xor_check_result |=
  1096. SUM_CHECK_Q_RESULT;
  1097. } else
  1098. BUG();
  1099. }
  1100. }
  1101. }
  1102. rv = ioread32(&dma_reg->dsts);
  1103. if (rv) {
  1104. pr_err("DMA%d err status: 0x%x\n",
  1105. chan->device->id, rv);
  1106. /* write back to clear */
  1107. iowrite32(rv, &dma_reg->dsts);
  1108. }
  1109. break;
  1110. case PPC440SPE_XOR_ID:
  1111. /* reset status bits to ack */
  1112. xor_reg = chan->device->xor_reg;
  1113. rv = ioread32be(&xor_reg->sr);
  1114. iowrite32be(rv, &xor_reg->sr);
  1115. if (rv & (XOR_IE_ICBIE_BIT|XOR_IE_ICIE_BIT|XOR_IE_RPTIE_BIT)) {
  1116. if (rv & XOR_IE_RPTIE_BIT) {
  1117. /* Read PLB Timeout Error.
  1118. * Try to resubmit the CB
  1119. */
  1120. u32 val = ioread32be(&xor_reg->ccbalr);
  1121. iowrite32be(val, &xor_reg->cblalr);
  1122. val = ioread32be(&xor_reg->crsr);
  1123. iowrite32be(val | XOR_CRSR_XAE_BIT,
  1124. &xor_reg->crsr);
  1125. } else
  1126. pr_err("XOR ERR 0x%x status\n", rv);
  1127. break;
  1128. }
  1129. /* if the XORcore is idle, but there are unprocessed CBs
  1130. * then refetch the s/w chain here
  1131. */
  1132. if (!(ioread32be(&xor_reg->sr) & XOR_SR_XCP_BIT) &&
  1133. do_xor_refetch)
  1134. ppc440spe_chan_append(chan);
  1135. break;
  1136. }
  1137. }
  1138. /**
  1139. * ppc440spe_chan_is_busy - get the channel status
  1140. */
  1141. static int ppc440spe_chan_is_busy(struct ppc440spe_adma_chan *chan)
  1142. {
  1143. struct dma_regs *dma_reg;
  1144. struct xor_regs *xor_reg;
  1145. int busy = 0;
  1146. switch (chan->device->id) {
  1147. case PPC440SPE_DMA0_ID:
  1148. case PPC440SPE_DMA1_ID:
  1149. dma_reg = chan->device->dma_reg;
  1150. /* if command FIFO's head and tail pointers are equal and
  1151. * status tail is the same as command, then channel is free
  1152. */
  1153. if (ioread16(&dma_reg->cpfhp) != ioread16(&dma_reg->cpftp) ||
  1154. ioread16(&dma_reg->cpftp) != ioread16(&dma_reg->csftp))
  1155. busy = 1;
  1156. break;
  1157. case PPC440SPE_XOR_ID:
  1158. /* use the special status bit for the XORcore
  1159. */
  1160. xor_reg = chan->device->xor_reg;
  1161. busy = (ioread32be(&xor_reg->sr) & XOR_SR_XCP_BIT) ? 1 : 0;
  1162. break;
  1163. }
  1164. return busy;
  1165. }
  1166. /**
  1167. * ppc440spe_chan_set_first_xor_descriptor - init XORcore chain
  1168. */
  1169. static void ppc440spe_chan_set_first_xor_descriptor(
  1170. struct ppc440spe_adma_chan *chan,
  1171. struct ppc440spe_adma_desc_slot *next_desc)
  1172. {
  1173. struct xor_regs *xor_reg = chan->device->xor_reg;
  1174. if (ioread32be(&xor_reg->sr) & XOR_SR_XCP_BIT)
  1175. printk(KERN_INFO "%s: Warn: XORcore is running "
  1176. "when try to set the first CDB!\n",
  1177. __func__);
  1178. xor_last_submit = xor_last_linked = next_desc;
  1179. iowrite32be(XOR_CRSR_64BA_BIT, &xor_reg->crsr);
  1180. iowrite32be(next_desc->phys, &xor_reg->cblalr);
  1181. iowrite32be(0, &xor_reg->cblahr);
  1182. iowrite32be(ioread32be(&xor_reg->cbcr) | XOR_CBCR_LNK_BIT,
  1183. &xor_reg->cbcr);
  1184. chan->hw_chain_inited = 1;
  1185. }
  1186. /**
  1187. * ppc440spe_dma_put_desc - put DMA0,1 descriptor to FIFO.
  1188. * called with irqs disabled
  1189. */
  1190. static void ppc440spe_dma_put_desc(struct ppc440spe_adma_chan *chan,
  1191. struct ppc440spe_adma_desc_slot *desc)
  1192. {
  1193. u32 pcdb;
  1194. struct dma_regs *dma_reg = chan->device->dma_reg;
  1195. pcdb = desc->phys;
  1196. if (!test_bit(PPC440SPE_DESC_INT, &desc->flags))
  1197. pcdb |= DMA_CDB_NO_INT;
  1198. chan_last_sub[chan->device->id] = desc;
  1199. ADMA_LL_DBG(print_cb(chan, desc->hw_desc));
  1200. iowrite32(pcdb, &dma_reg->cpfpl);
  1201. }
  1202. /**
  1203. * ppc440spe_chan_append - update the h/w chain in the channel
  1204. */
  1205. static void ppc440spe_chan_append(struct ppc440spe_adma_chan *chan)
  1206. {
  1207. struct xor_regs *xor_reg;
  1208. struct ppc440spe_adma_desc_slot *iter;
  1209. struct xor_cb *xcb;
  1210. u32 cur_desc;
  1211. unsigned long flags;
  1212. local_irq_save(flags);
  1213. switch (chan->device->id) {
  1214. case PPC440SPE_DMA0_ID:
  1215. case PPC440SPE_DMA1_ID:
  1216. cur_desc = ppc440spe_chan_get_current_descriptor(chan);
  1217. if (likely(cur_desc)) {
  1218. iter = chan_last_sub[chan->device->id];
  1219. BUG_ON(!iter);
  1220. } else {
  1221. /* first peer */
  1222. iter = chan_first_cdb[chan->device->id];
  1223. BUG_ON(!iter);
  1224. ppc440spe_dma_put_desc(chan, iter);
  1225. chan->hw_chain_inited = 1;
  1226. }
  1227. /* is there something new to append */
  1228. if (!iter->hw_next)
  1229. break;
  1230. /* flush descriptors from the s/w queue to fifo */
  1231. list_for_each_entry_continue(iter, &chan->chain, chain_node) {
  1232. ppc440spe_dma_put_desc(chan, iter);
  1233. if (!iter->hw_next)
  1234. break;
  1235. }
  1236. break;
  1237. case PPC440SPE_XOR_ID:
  1238. /* update h/w links and refetch */
  1239. if (!xor_last_submit->hw_next)
  1240. break;
  1241. xor_reg = chan->device->xor_reg;
  1242. /* the last linked CDB has to generate an interrupt
  1243. * that we'd be able to append the next lists to h/w
  1244. * regardless of the XOR engine state at the moment of
  1245. * appending of these next lists
  1246. */
  1247. xcb = xor_last_linked->hw_desc;
  1248. xcb->cbc |= XOR_CBCR_CBCE_BIT;
  1249. if (!(ioread32be(&xor_reg->sr) & XOR_SR_XCP_BIT)) {
  1250. /* XORcore is idle. Refetch now */
  1251. do_xor_refetch = 0;
  1252. ppc440spe_xor_set_link(xor_last_submit,
  1253. xor_last_submit->hw_next);
  1254. ADMA_LL_DBG(print_cb_list(chan,
  1255. xor_last_submit->hw_next));
  1256. xor_last_submit = xor_last_linked;
  1257. iowrite32be(ioread32be(&xor_reg->crsr) |
  1258. XOR_CRSR_RCBE_BIT | XOR_CRSR_64BA_BIT,
  1259. &xor_reg->crsr);
  1260. } else {
  1261. /* XORcore is running. Refetch later in the handler */
  1262. do_xor_refetch = 1;
  1263. }
  1264. break;
  1265. }
  1266. local_irq_restore(flags);
  1267. }
  1268. /**
  1269. * ppc440spe_chan_get_current_descriptor - get the currently executed descriptor
  1270. */
  1271. static u32
  1272. ppc440spe_chan_get_current_descriptor(struct ppc440spe_adma_chan *chan)
  1273. {
  1274. struct dma_regs *dma_reg;
  1275. struct xor_regs *xor_reg;
  1276. if (unlikely(!chan->hw_chain_inited))
  1277. /* h/w descriptor chain is not initialized yet */
  1278. return 0;
  1279. switch (chan->device->id) {
  1280. case PPC440SPE_DMA0_ID:
  1281. case PPC440SPE_DMA1_ID:
  1282. dma_reg = chan->device->dma_reg;
  1283. return ioread32(&dma_reg->acpl) & (~DMA_CDB_MSK);
  1284. case PPC440SPE_XOR_ID:
  1285. xor_reg = chan->device->xor_reg;
  1286. return ioread32be(&xor_reg->ccbalr);
  1287. }
  1288. return 0;
  1289. }
  1290. /**
  1291. * ppc440spe_chan_run - enable the channel
  1292. */
  1293. static void ppc440spe_chan_run(struct ppc440spe_adma_chan *chan)
  1294. {
  1295. struct xor_regs *xor_reg;
  1296. switch (chan->device->id) {
  1297. case PPC440SPE_DMA0_ID:
  1298. case PPC440SPE_DMA1_ID:
  1299. /* DMAs are always enabled, do nothing */
  1300. break;
  1301. case PPC440SPE_XOR_ID:
  1302. /* drain write buffer */
  1303. xor_reg = chan->device->xor_reg;
  1304. /* fetch descriptor pointed to in <link> */
  1305. iowrite32be(XOR_CRSR_64BA_BIT | XOR_CRSR_XAE_BIT,
  1306. &xor_reg->crsr);
  1307. break;
  1308. }
  1309. }
  1310. /******************************************************************************
  1311. * ADMA device level
  1312. ******************************************************************************/
  1313. static void ppc440spe_chan_start_null_xor(struct ppc440spe_adma_chan *chan);
  1314. static int ppc440spe_adma_alloc_chan_resources(struct dma_chan *chan);
  1315. static dma_cookie_t
  1316. ppc440spe_adma_tx_submit(struct dma_async_tx_descriptor *tx);
  1317. static void ppc440spe_adma_set_dest(struct ppc440spe_adma_desc_slot *tx,
  1318. dma_addr_t addr, int index);
  1319. static void
  1320. ppc440spe_adma_memcpy_xor_set_src(struct ppc440spe_adma_desc_slot *tx,
  1321. dma_addr_t addr, int index);
  1322. static void
  1323. ppc440spe_adma_pq_set_dest(struct ppc440spe_adma_desc_slot *tx,
  1324. dma_addr_t *paddr, unsigned long flags);
  1325. static void
  1326. ppc440spe_adma_pq_set_src(struct ppc440spe_adma_desc_slot *tx,
  1327. dma_addr_t addr, int index);
  1328. static void
  1329. ppc440spe_adma_pq_set_src_mult(struct ppc440spe_adma_desc_slot *tx,
  1330. unsigned char mult, int index, int dst_pos);
  1331. static void
  1332. ppc440spe_adma_pqzero_sum_set_dest(struct ppc440spe_adma_desc_slot *tx,
  1333. dma_addr_t paddr, dma_addr_t qaddr);
  1334. static struct page *ppc440spe_rxor_srcs[32];
  1335. /**
  1336. * ppc440spe_can_rxor - check if the operands may be processed with RXOR
  1337. */
  1338. static int ppc440spe_can_rxor(struct page **srcs, int src_cnt, size_t len)
  1339. {
  1340. int i, order = 0, state = 0;
  1341. int idx = 0;
  1342. if (unlikely(!(src_cnt > 1)))
  1343. return 0;
  1344. BUG_ON(src_cnt > ARRAY_SIZE(ppc440spe_rxor_srcs));
  1345. /* Skip holes in the source list before checking */
  1346. for (i = 0; i < src_cnt; i++) {
  1347. if (!srcs[i])
  1348. continue;
  1349. ppc440spe_rxor_srcs[idx++] = srcs[i];
  1350. }
  1351. src_cnt = idx;
  1352. for (i = 1; i < src_cnt; i++) {
  1353. char *cur_addr = page_address(ppc440spe_rxor_srcs[i]);
  1354. char *old_addr = page_address(ppc440spe_rxor_srcs[i - 1]);
  1355. switch (state) {
  1356. case 0:
  1357. if (cur_addr == old_addr + len) {
  1358. /* direct RXOR */
  1359. order = 1;
  1360. state = 1;
  1361. } else if (old_addr == cur_addr + len) {
  1362. /* reverse RXOR */
  1363. order = -1;
  1364. state = 1;
  1365. } else
  1366. goto out;
  1367. break;
  1368. case 1:
  1369. if ((i == src_cnt - 2) ||
  1370. (order == -1 && cur_addr != old_addr - len)) {
  1371. order = 0;
  1372. state = 0;
  1373. } else if ((cur_addr == old_addr + len * order) ||
  1374. (cur_addr == old_addr + 2 * len) ||
  1375. (cur_addr == old_addr + 3 * len)) {
  1376. state = 2;
  1377. } else {
  1378. order = 0;
  1379. state = 0;
  1380. }
  1381. break;
  1382. case 2:
  1383. order = 0;
  1384. state = 0;
  1385. break;
  1386. }
  1387. }
  1388. out:
  1389. if (state == 1 || state == 2)
  1390. return 1;
  1391. return 0;
  1392. }
  1393. /**
  1394. * ppc440spe_adma_device_estimate - estimate the efficiency of processing
  1395. * the operation given on this channel. It's assumed that 'chan' is
  1396. * capable to process 'cap' type of operation.
  1397. * @chan: channel to use
  1398. * @cap: type of transaction
  1399. * @dst_lst: array of destination pointers
  1400. * @dst_cnt: number of destination operands
  1401. * @src_lst: array of source pointers
  1402. * @src_cnt: number of source operands
  1403. * @src_sz: size of each source operand
  1404. */
  1405. static int ppc440spe_adma_estimate(struct dma_chan *chan,
  1406. enum dma_transaction_type cap, struct page **dst_lst, int dst_cnt,
  1407. struct page **src_lst, int src_cnt, size_t src_sz)
  1408. {
  1409. int ef = 1;
  1410. if (cap == DMA_PQ || cap == DMA_PQ_VAL) {
  1411. /* If RAID-6 capabilities were not activated don't try
  1412. * to use them
  1413. */
  1414. if (unlikely(!ppc440spe_r6_enabled))
  1415. return -1;
  1416. }
  1417. /* In the current implementation of ppc440spe ADMA driver it
  1418. * makes sense to pick out only pq case, because it may be
  1419. * processed:
  1420. * (1) either using Biskup method on DMA2;
  1421. * (2) or on DMA0/1.
  1422. * Thus we give a favour to (1) if the sources are suitable;
  1423. * else let it be processed on one of the DMA0/1 engines.
  1424. * In the sum_product case where destination is also the
  1425. * source process it on DMA0/1 only.
  1426. */
  1427. if (cap == DMA_PQ && chan->chan_id == PPC440SPE_XOR_ID) {
  1428. if (dst_cnt == 1 && src_cnt == 2 && dst_lst[0] == src_lst[1])
  1429. ef = 0; /* sum_product case, process on DMA0/1 */
  1430. else if (ppc440spe_can_rxor(src_lst, src_cnt, src_sz))
  1431. ef = 3; /* override (DMA0/1 + idle) */
  1432. else
  1433. ef = 0; /* can't process on DMA2 if !rxor */
  1434. }
  1435. /* channel idleness increases the priority */
  1436. if (likely(ef) &&
  1437. !ppc440spe_chan_is_busy(to_ppc440spe_adma_chan(chan)))
  1438. ef++;
  1439. return ef;
  1440. }
  1441. struct dma_chan *
  1442. ppc440spe_async_tx_find_best_channel(enum dma_transaction_type cap,
  1443. struct page **dst_lst, int dst_cnt, struct page **src_lst,
  1444. int src_cnt, size_t src_sz)
  1445. {
  1446. struct dma_chan *best_chan = NULL;
  1447. struct ppc_dma_chan_ref *ref;
  1448. int best_rank = -1;
  1449. if (unlikely(!src_sz))
  1450. return NULL;
  1451. if (src_sz > PAGE_SIZE) {
  1452. /*
  1453. * should a user of the api ever pass > PAGE_SIZE requests
  1454. * we sort out cases where temporary page-sized buffers
  1455. * are used.
  1456. */
  1457. switch (cap) {
  1458. case DMA_PQ:
  1459. if (src_cnt == 1 && dst_lst[1] == src_lst[0])
  1460. return NULL;
  1461. if (src_cnt == 2 && dst_lst[1] == src_lst[1])
  1462. return NULL;
  1463. break;
  1464. case DMA_PQ_VAL:
  1465. case DMA_XOR_VAL:
  1466. return NULL;
  1467. default:
  1468. break;
  1469. }
  1470. }
  1471. list_for_each_entry(ref, &ppc440spe_adma_chan_list, node) {
  1472. if (dma_has_cap(cap, ref->chan->device->cap_mask)) {
  1473. int rank;
  1474. rank = ppc440spe_adma_estimate(ref->chan, cap, dst_lst,
  1475. dst_cnt, src_lst, src_cnt, src_sz);
  1476. if (rank > best_rank) {
  1477. best_rank = rank;
  1478. best_chan = ref->chan;
  1479. }
  1480. }
  1481. }
  1482. return best_chan;
  1483. }
  1484. EXPORT_SYMBOL_GPL(ppc440spe_async_tx_find_best_channel);
  1485. /**
  1486. * ppc440spe_get_group_entry - get group entry with index idx
  1487. * @tdesc: is the last allocated slot in the group.
  1488. */
  1489. static struct ppc440spe_adma_desc_slot *
  1490. ppc440spe_get_group_entry(struct ppc440spe_adma_desc_slot *tdesc, u32 entry_idx)
  1491. {
  1492. struct ppc440spe_adma_desc_slot *iter = tdesc->group_head;
  1493. int i = 0;
  1494. if (entry_idx < 0 || entry_idx >= (tdesc->src_cnt + tdesc->dst_cnt)) {
  1495. printk("%s: entry_idx %d, src_cnt %d, dst_cnt %d\n",
  1496. __func__, entry_idx, tdesc->src_cnt, tdesc->dst_cnt);
  1497. BUG();
  1498. }
  1499. list_for_each_entry(iter, &tdesc->group_list, chain_node) {
  1500. if (i++ == entry_idx)
  1501. break;
  1502. }
  1503. return iter;
  1504. }
  1505. /**
  1506. * ppc440spe_adma_free_slots - flags descriptor slots for reuse
  1507. * @slot: Slot to free
  1508. * Caller must hold &ppc440spe_chan->lock while calling this function
  1509. */
  1510. static void ppc440spe_adma_free_slots(struct ppc440spe_adma_desc_slot *slot,
  1511. struct ppc440spe_adma_chan *chan)
  1512. {
  1513. int stride = slot->slots_per_op;
  1514. while (stride--) {
  1515. slot->slots_per_op = 0;
  1516. slot = list_entry(slot->slot_node.next,
  1517. struct ppc440spe_adma_desc_slot,
  1518. slot_node);
  1519. }
  1520. }
  1521. static void ppc440spe_adma_unmap(struct ppc440spe_adma_chan *chan,
  1522. struct ppc440spe_adma_desc_slot *desc)
  1523. {
  1524. u32 src_cnt, dst_cnt;
  1525. dma_addr_t addr;
  1526. /*
  1527. * get the number of sources & destination
  1528. * included in this descriptor and unmap
  1529. * them all
  1530. */
  1531. src_cnt = ppc440spe_desc_get_src_num(desc, chan);
  1532. dst_cnt = ppc440spe_desc_get_dst_num(desc, chan);
  1533. /* unmap destinations */
  1534. if (!(desc->async_tx.flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
  1535. while (dst_cnt--) {
  1536. addr = ppc440spe_desc_get_dest_addr(
  1537. desc, chan, dst_cnt);
  1538. dma_unmap_page(chan->device->dev,
  1539. addr, desc->unmap_len,
  1540. DMA_FROM_DEVICE);
  1541. }
  1542. }
  1543. /* unmap sources */
  1544. if (!(desc->async_tx.flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
  1545. while (src_cnt--) {
  1546. addr = ppc440spe_desc_get_src_addr(
  1547. desc, chan, src_cnt);
  1548. dma_unmap_page(chan->device->dev,
  1549. addr, desc->unmap_len,
  1550. DMA_TO_DEVICE);
  1551. }
  1552. }
  1553. }
  1554. /**
  1555. * ppc440spe_adma_run_tx_complete_actions - call functions to be called
  1556. * upon completion
  1557. */
  1558. static dma_cookie_t ppc440spe_adma_run_tx_complete_actions(
  1559. struct ppc440spe_adma_desc_slot *desc,
  1560. struct ppc440spe_adma_chan *chan,
  1561. dma_cookie_t cookie)
  1562. {
  1563. int i;
  1564. BUG_ON(desc->async_tx.cookie < 0);
  1565. if (desc->async_tx.cookie > 0) {
  1566. cookie = desc->async_tx.cookie;
  1567. desc->async_tx.cookie = 0;
  1568. /* call the callback (must not sleep or submit new
  1569. * operations to this channel)
  1570. */
  1571. if (desc->async_tx.callback)
  1572. desc->async_tx.callback(
  1573. desc->async_tx.callback_param);
  1574. /* unmap dma addresses
  1575. * (unmap_single vs unmap_page?)
  1576. *
  1577. * actually, ppc's dma_unmap_page() functions are empty, so
  1578. * the following code is just for the sake of completeness
  1579. */
  1580. if (chan && chan->needs_unmap && desc->group_head &&
  1581. desc->unmap_len) {
  1582. struct ppc440spe_adma_desc_slot *unmap =
  1583. desc->group_head;
  1584. /* assume 1 slot per op always */
  1585. u32 slot_count = unmap->slot_cnt;
  1586. /* Run through the group list and unmap addresses */
  1587. for (i = 0; i < slot_count; i++) {
  1588. BUG_ON(!unmap);
  1589. ppc440spe_adma_unmap(chan, unmap);
  1590. unmap = unmap->hw_next;
  1591. }
  1592. }
  1593. }
  1594. /* run dependent operations */
  1595. dma_run_dependencies(&desc->async_tx);
  1596. return cookie;
  1597. }
  1598. /**
  1599. * ppc440spe_adma_clean_slot - clean up CDB slot (if ack is set)
  1600. */
  1601. static int ppc440spe_adma_clean_slot(struct ppc440spe_adma_desc_slot *desc,
  1602. struct ppc440spe_adma_chan *chan)
  1603. {
  1604. /* the client is allowed to attach dependent operations
  1605. * until 'ack' is set
  1606. */
  1607. if (!async_tx_test_ack(&desc->async_tx))
  1608. return 0;
  1609. /* leave the last descriptor in the chain
  1610. * so we can append to it
  1611. */
  1612. if (list_is_last(&desc->chain_node, &chan->chain) ||
  1613. desc->phys == ppc440spe_chan_get_current_descriptor(chan))
  1614. return 1;
  1615. if (chan->device->id != PPC440SPE_XOR_ID) {
  1616. /* our DMA interrupt handler clears opc field of
  1617. * each processed descriptor. For all types of
  1618. * operations except for ZeroSum we do not actually
  1619. * need ack from the interrupt handler. ZeroSum is a
  1620. * special case since the result of this operation
  1621. * is available from the handler only, so if we see
  1622. * such type of descriptor (which is unprocessed yet)
  1623. * then leave it in chain.
  1624. */
  1625. struct dma_cdb *cdb = desc->hw_desc;
  1626. if (cdb->opc == DMA_CDB_OPC_DCHECK128)
  1627. return 1;
  1628. }
  1629. dev_dbg(chan->device->common.dev, "\tfree slot %llx: %d stride: %d\n",
  1630. desc->phys, desc->idx, desc->slots_per_op);
  1631. list_del(&desc->chain_node);
  1632. ppc440spe_adma_free_slots(desc, chan);
  1633. return 0;
  1634. }
  1635. /**
  1636. * __ppc440spe_adma_slot_cleanup - this is the common clean-up routine
  1637. * which runs through the channel CDBs list until reach the descriptor
  1638. * currently processed. When routine determines that all CDBs of group
  1639. * are completed then corresponding callbacks (if any) are called and slots
  1640. * are freed.
  1641. */
  1642. static void __ppc440spe_adma_slot_cleanup(struct ppc440spe_adma_chan *chan)
  1643. {
  1644. struct ppc440spe_adma_desc_slot *iter, *_iter, *group_start = NULL;
  1645. dma_cookie_t cookie = 0;
  1646. u32 current_desc = ppc440spe_chan_get_current_descriptor(chan);
  1647. int busy = ppc440spe_chan_is_busy(chan);
  1648. int seen_current = 0, slot_cnt = 0, slots_per_op = 0;
  1649. dev_dbg(chan->device->common.dev, "ppc440spe adma%d: %s\n",
  1650. chan->device->id, __func__);
  1651. if (!current_desc) {
  1652. /* There were no transactions yet, so
  1653. * nothing to clean
  1654. */
  1655. return;
  1656. }
  1657. /* free completed slots from the chain starting with
  1658. * the oldest descriptor
  1659. */
  1660. list_for_each_entry_safe(iter, _iter, &chan->chain,
  1661. chain_node) {
  1662. dev_dbg(chan->device->common.dev, "\tcookie: %d slot: %d "
  1663. "busy: %d this_desc: %#llx next_desc: %#x "
  1664. "cur: %#x ack: %d\n",
  1665. iter->async_tx.cookie, iter->idx, busy, iter->phys,
  1666. ppc440spe_desc_get_link(iter, chan), current_desc,
  1667. async_tx_test_ack(&iter->async_tx));
  1668. prefetch(_iter);
  1669. prefetch(&_iter->async_tx);
  1670. /* do not advance past the current descriptor loaded into the
  1671. * hardware channel,subsequent descriptors are either in process
  1672. * or have not been submitted
  1673. */
  1674. if (seen_current)
  1675. break;
  1676. /* stop the search if we reach the current descriptor and the
  1677. * channel is busy, or if it appears that the current descriptor
  1678. * needs to be re-read (i.e. has been appended to)
  1679. */
  1680. if (iter->phys == current_desc) {
  1681. BUG_ON(seen_current++);
  1682. if (busy || ppc440spe_desc_get_link(iter, chan)) {
  1683. /* not all descriptors of the group have
  1684. * been completed; exit.
  1685. */
  1686. break;
  1687. }
  1688. }
  1689. /* detect the start of a group transaction */
  1690. if (!slot_cnt && !slots_per_op) {
  1691. slot_cnt = iter->slot_cnt;
  1692. slots_per_op = iter->slots_per_op;
  1693. if (slot_cnt <= slots_per_op) {
  1694. slot_cnt = 0;
  1695. slots_per_op = 0;
  1696. }
  1697. }
  1698. if (slot_cnt) {
  1699. if (!group_start)
  1700. group_start = iter;
  1701. slot_cnt -= slots_per_op;
  1702. }
  1703. /* all the members of a group are complete */
  1704. if (slots_per_op != 0 && slot_cnt == 0) {
  1705. struct ppc440spe_adma_desc_slot *grp_iter, *_grp_iter;
  1706. int end_of_chain = 0;
  1707. /* clean up the group */
  1708. slot_cnt = group_start->slot_cnt;
  1709. grp_iter = group_start;
  1710. list_for_each_entry_safe_from(grp_iter, _grp_iter,
  1711. &chan->chain, chain_node) {
  1712. cookie = ppc440spe_adma_run_tx_complete_actions(
  1713. grp_iter, chan, cookie);
  1714. slot_cnt -= slots_per_op;
  1715. end_of_chain = ppc440spe_adma_clean_slot(
  1716. grp_iter, chan);
  1717. if (end_of_chain && slot_cnt) {
  1718. /* Should wait for ZeroSum completion */
  1719. if (cookie > 0)
  1720. chan->common.completed_cookie = cookie;
  1721. return;
  1722. }
  1723. if (slot_cnt == 0 || end_of_chain)
  1724. break;
  1725. }
  1726. /* the group should be complete at this point */
  1727. BUG_ON(slot_cnt);
  1728. slots_per_op = 0;
  1729. group_start = NULL;
  1730. if (end_of_chain)
  1731. break;
  1732. else
  1733. continue;
  1734. } else if (slots_per_op) /* wait for group completion */
  1735. continue;
  1736. cookie = ppc440spe_adma_run_tx_complete_actions(iter, chan,
  1737. cookie);
  1738. if (ppc440spe_adma_clean_slot(iter, chan))
  1739. break;
  1740. }
  1741. BUG_ON(!seen_current);
  1742. if (cookie > 0) {
  1743. chan->common.completed_cookie = cookie;
  1744. pr_debug("\tcompleted cookie %d\n", cookie);
  1745. }
  1746. }
  1747. /**
  1748. * ppc440spe_adma_tasklet - clean up watch-dog initiator
  1749. */
  1750. static void ppc440spe_adma_tasklet(unsigned long data)
  1751. {
  1752. struct ppc440spe_adma_chan *chan = (struct ppc440spe_adma_chan *) data;
  1753. spin_lock_nested(&chan->lock, SINGLE_DEPTH_NESTING);
  1754. __ppc440spe_adma_slot_cleanup(chan);
  1755. spin_unlock(&chan->lock);
  1756. }
  1757. /**
  1758. * ppc440spe_adma_slot_cleanup - clean up scheduled initiator
  1759. */
  1760. static void ppc440spe_adma_slot_cleanup(struct ppc440spe_adma_chan *chan)
  1761. {
  1762. spin_lock_bh(&chan->lock);
  1763. __ppc440spe_adma_slot_cleanup(chan);
  1764. spin_unlock_bh(&chan->lock);
  1765. }
  1766. /**
  1767. * ppc440spe_adma_alloc_slots - allocate free slots (if any)
  1768. */
  1769. static struct ppc440spe_adma_desc_slot *ppc440spe_adma_alloc_slots(
  1770. struct ppc440spe_adma_chan *chan, int num_slots,
  1771. int slots_per_op)
  1772. {
  1773. struct ppc440spe_adma_desc_slot *iter = NULL, *_iter;
  1774. struct ppc440spe_adma_desc_slot *alloc_start = NULL;
  1775. struct list_head chain = LIST_HEAD_INIT(chain);
  1776. int slots_found, retry = 0;
  1777. BUG_ON(!num_slots || !slots_per_op);
  1778. /* start search from the last allocated descrtiptor
  1779. * if a contiguous allocation can not be found start searching
  1780. * from the beginning of the list
  1781. */
  1782. retry:
  1783. slots_found = 0;
  1784. if (retry == 0)
  1785. iter = chan->last_used;
  1786. else
  1787. iter = list_entry(&chan->all_slots,
  1788. struct ppc440spe_adma_desc_slot,
  1789. slot_node);
  1790. list_for_each_entry_safe_continue(iter, _iter, &chan->all_slots,
  1791. slot_node) {
  1792. prefetch(_iter);
  1793. prefetch(&_iter->async_tx);
  1794. if (iter->slots_per_op) {
  1795. slots_found = 0;
  1796. continue;
  1797. }
  1798. /* start the allocation if the slot is correctly aligned */
  1799. if (!slots_found++)
  1800. alloc_start = iter;
  1801. if (slots_found == num_slots) {
  1802. struct ppc440spe_adma_desc_slot *alloc_tail = NULL;
  1803. struct ppc440spe_adma_desc_slot *last_used = NULL;
  1804. iter = alloc_start;
  1805. while (num_slots) {
  1806. int i;
  1807. /* pre-ack all but the last descriptor */
  1808. if (num_slots != slots_per_op)
  1809. async_tx_ack(&iter->async_tx);
  1810. list_add_tail(&iter->chain_node, &chain);
  1811. alloc_tail = iter;
  1812. iter->async_tx.cookie = 0;
  1813. iter->hw_next = NULL;
  1814. iter->flags = 0;
  1815. iter->slot_cnt = num_slots;
  1816. iter->xor_check_result = NULL;
  1817. for (i = 0; i < slots_per_op; i++) {
  1818. iter->slots_per_op = slots_per_op - i;
  1819. last_used = iter;
  1820. iter = list_entry(iter->slot_node.next,
  1821. struct ppc440spe_adma_desc_slot,
  1822. slot_node);
  1823. }
  1824. num_slots -= slots_per_op;
  1825. }
  1826. alloc_tail->group_head = alloc_start;
  1827. alloc_tail->async_tx.cookie = -EBUSY;
  1828. list_splice(&chain, &alloc_tail->group_list);
  1829. chan->last_used = last_used;
  1830. return alloc_tail;
  1831. }
  1832. }
  1833. if (!retry++)
  1834. goto retry;
  1835. /* try to free some slots if the allocation fails */
  1836. tasklet_schedule(&chan->irq_tasklet);
  1837. return NULL;
  1838. }
  1839. /**
  1840. * ppc440spe_adma_alloc_chan_resources - allocate pools for CDB slots
  1841. */
  1842. static int ppc440spe_adma_alloc_chan_resources(struct dma_chan *chan)
  1843. {
  1844. struct ppc440spe_adma_chan *ppc440spe_chan;
  1845. struct ppc440spe_adma_desc_slot *slot = NULL;
  1846. char *hw_desc;
  1847. int i, db_sz;
  1848. int init;
  1849. ppc440spe_chan = to_ppc440spe_adma_chan(chan);
  1850. init = ppc440spe_chan->slots_allocated ? 0 : 1;
  1851. chan->chan_id = ppc440spe_chan->device->id;
  1852. /* Allocate descriptor slots */
  1853. i = ppc440spe_chan->slots_allocated;
  1854. if (ppc440spe_chan->device->id != PPC440SPE_XOR_ID)
  1855. db_sz = sizeof(struct dma_cdb);
  1856. else
  1857. db_sz = sizeof(struct xor_cb);
  1858. for (; i < (ppc440spe_chan->device->pool_size / db_sz); i++) {
  1859. slot = kzalloc(sizeof(struct ppc440spe_adma_desc_slot),
  1860. GFP_KERNEL);
  1861. if (!slot) {
  1862. printk(KERN_INFO "SPE ADMA Channel only initialized"
  1863. " %d descriptor slots", i--);
  1864. break;
  1865. }
  1866. hw_desc = (char *) ppc440spe_chan->device->dma_desc_pool_virt;
  1867. slot->hw_desc = (void *) &hw_desc[i * db_sz];
  1868. dma_async_tx_descriptor_init(&slot->async_tx, chan);
  1869. slot->async_tx.tx_submit = ppc440spe_adma_tx_submit;
  1870. INIT_LIST_HEAD(&slot->chain_node);
  1871. INIT_LIST_HEAD(&slot->slot_node);
  1872. INIT_LIST_HEAD(&slot->group_list);
  1873. slot->phys = ppc440spe_chan->device->dma_desc_pool + i * db_sz;
  1874. slot->idx = i;
  1875. spin_lock_bh(&ppc440spe_chan->lock);
  1876. ppc440spe_chan->slots_allocated++;
  1877. list_add_tail(&slot->slot_node, &ppc440spe_chan->all_slots);
  1878. spin_unlock_bh(&ppc440spe_chan->lock);
  1879. }
  1880. if (i && !ppc440spe_chan->last_used) {
  1881. ppc440spe_chan->last_used =
  1882. list_entry(ppc440spe_chan->all_slots.next,
  1883. struct ppc440spe_adma_desc_slot,
  1884. slot_node);
  1885. }
  1886. dev_dbg(ppc440spe_chan->device->common.dev,
  1887. "ppc440spe adma%d: allocated %d descriptor slots\n",
  1888. ppc440spe_chan->device->id, i);
  1889. /* initialize the channel and the chain with a null operation */
  1890. if (init) {
  1891. switch (ppc440spe_chan->device->id) {
  1892. case PPC440SPE_DMA0_ID:
  1893. case PPC440SPE_DMA1_ID:
  1894. ppc440spe_chan->hw_chain_inited = 0;
  1895. /* Use WXOR for self-testing */
  1896. if (!ppc440spe_r6_tchan)
  1897. ppc440spe_r6_tchan = ppc440spe_chan;
  1898. break;
  1899. case PPC440SPE_XOR_ID:
  1900. ppc440spe_chan_start_null_xor(ppc440spe_chan);
  1901. break;
  1902. default:
  1903. BUG();
  1904. }
  1905. ppc440spe_chan->needs_unmap = 1;
  1906. }
  1907. return (i > 0) ? i : -ENOMEM;
  1908. }
  1909. /**
  1910. * ppc440spe_rxor_set_region_data -
  1911. */
  1912. static void ppc440spe_rxor_set_region(struct ppc440spe_adma_desc_slot *desc,
  1913. u8 xor_arg_no, u32 mask)
  1914. {
  1915. struct xor_cb *xcb = desc->hw_desc;
  1916. xcb->ops[xor_arg_no].h |= mask;
  1917. }
  1918. /**
  1919. * ppc440spe_rxor_set_src -
  1920. */
  1921. static void ppc440spe_rxor_set_src(struct ppc440spe_adma_desc_slot *desc,
  1922. u8 xor_arg_no, dma_addr_t addr)
  1923. {
  1924. struct xor_cb *xcb = desc->hw_desc;
  1925. xcb->ops[xor_arg_no].h |= DMA_CUED_XOR_BASE;
  1926. xcb->ops[xor_arg_no].l = addr;
  1927. }
  1928. /**
  1929. * ppc440spe_rxor_set_mult -
  1930. */
  1931. static void ppc440spe_rxor_set_mult(struct ppc440spe_adma_desc_slot *desc,
  1932. u8 xor_arg_no, u8 idx, u8 mult)
  1933. {
  1934. struct xor_cb *xcb = desc->hw_desc;
  1935. xcb->ops[xor_arg_no].h |= mult << (DMA_CUED_MULT1_OFF + idx * 8);
  1936. }
  1937. /**
  1938. * ppc440spe_adma_check_threshold - append CDBs to h/w chain if threshold
  1939. * has been achieved
  1940. */
  1941. static void ppc440spe_adma_check_threshold(struct ppc440spe_adma_chan *chan)
  1942. {
  1943. dev_dbg(chan->device->common.dev, "ppc440spe adma%d: pending: %d\n",
  1944. chan->device->id, chan->pending);
  1945. if (chan->pending >= PPC440SPE_ADMA_THRESHOLD) {
  1946. chan->pending = 0;
  1947. ppc440spe_chan_append(chan);
  1948. }
  1949. }
  1950. /**
  1951. * ppc440spe_adma_tx_submit - submit new descriptor group to the channel
  1952. * (it's not necessary that descriptors will be submitted to the h/w
  1953. * chains too right now)
  1954. */
  1955. static dma_cookie_t ppc440spe_adma_tx_submit(struct dma_async_tx_descriptor *tx)
  1956. {
  1957. struct ppc440spe_adma_desc_slot *sw_desc;
  1958. struct ppc440spe_adma_chan *chan = to_ppc440spe_adma_chan(tx->chan);
  1959. struct ppc440spe_adma_desc_slot *group_start, *old_chain_tail;
  1960. int slot_cnt;
  1961. int slots_per_op;
  1962. dma_cookie_t cookie;
  1963. sw_desc = tx_to_ppc440spe_adma_slot(tx);
  1964. group_start = sw_desc->group_head;
  1965. slot_cnt = group_start->slot_cnt;
  1966. slots_per_op = group_start->slots_per_op;
  1967. spin_lock_bh(&chan->lock);
  1968. cookie = dma_cookie_assign(tx);
  1969. if (unlikely(list_empty(&chan->chain))) {
  1970. /* first peer */
  1971. list_splice_init(&sw_desc->group_list, &chan->chain);
  1972. chan_first_cdb[chan->device->id] = group_start;
  1973. } else {
  1974. /* isn't first peer, bind CDBs to chain */
  1975. old_chain_tail = list_entry(chan->chain.prev,
  1976. struct ppc440spe_adma_desc_slot,
  1977. chain_node);
  1978. list_splice_init(&sw_desc->group_list,
  1979. &old_chain_tail->chain_node);
  1980. /* fix up the hardware chain */
  1981. ppc440spe_desc_set_link(chan, old_chain_tail, group_start);
  1982. }
  1983. /* increment the pending count by the number of operations */
  1984. chan->pending += slot_cnt / slots_per_op;
  1985. ppc440spe_adma_check_threshold(chan);
  1986. spin_unlock_bh(&chan->lock);
  1987. dev_dbg(chan->device->common.dev,
  1988. "ppc440spe adma%d: %s cookie: %d slot: %d tx %p\n",
  1989. chan->device->id, __func__,
  1990. sw_desc->async_tx.cookie, sw_desc->idx, sw_desc);
  1991. return cookie;
  1992. }
  1993. /**
  1994. * ppc440spe_adma_prep_dma_interrupt - prepare CDB for a pseudo DMA operation
  1995. */
  1996. static struct dma_async_tx_descriptor *ppc440spe_adma_prep_dma_interrupt(
  1997. struct dma_chan *chan, unsigned long flags)
  1998. {
  1999. struct ppc440spe_adma_chan *ppc440spe_chan;
  2000. struct ppc440spe_adma_desc_slot *sw_desc, *group_start;
  2001. int slot_cnt, slots_per_op;
  2002. ppc440spe_chan = to_ppc440spe_adma_chan(chan);
  2003. dev_dbg(ppc440spe_chan->device->common.dev,
  2004. "ppc440spe adma%d: %s\n", ppc440spe_chan->device->id,
  2005. __func__);
  2006. spin_lock_bh(&ppc440spe_chan->lock);
  2007. slot_cnt = slots_per_op = 1;
  2008. sw_desc = ppc440spe_adma_alloc_slots(ppc440spe_chan, slot_cnt,
  2009. slots_per_op);
  2010. if (sw_desc) {
  2011. group_start = sw_desc->group_head;
  2012. ppc440spe_desc_init_interrupt(group_start, ppc440spe_chan);
  2013. group_start->unmap_len = 0;
  2014. sw_desc->async_tx.flags = flags;
  2015. }
  2016. spin_unlock_bh(&ppc440spe_chan->lock);
  2017. return sw_desc ? &sw_desc->async_tx : NULL;
  2018. }
  2019. /**
  2020. * ppc440spe_adma_prep_dma_memcpy - prepare CDB for a MEMCPY operation
  2021. */
  2022. static struct dma_async_tx_descriptor *ppc440spe_adma_prep_dma_memcpy(
  2023. struct dma_chan *chan, dma_addr_t dma_dest,
  2024. dma_addr_t dma_src, size_t len, unsigned long flags)
  2025. {
  2026. struct ppc440spe_adma_chan *ppc440spe_chan;
  2027. struct ppc440spe_adma_desc_slot *sw_desc, *group_start;
  2028. int slot_cnt, slots_per_op;
  2029. ppc440spe_chan = to_ppc440spe_adma_chan(chan);
  2030. if (unlikely(!len))
  2031. return NULL;
  2032. BUG_ON(len > PPC440SPE_ADMA_DMA_MAX_BYTE_COUNT);
  2033. spin_lock_bh(&ppc440spe_chan->lock);
  2034. dev_dbg(ppc440spe_chan->device->common.dev,
  2035. "ppc440spe adma%d: %s len: %u int_en %d\n",
  2036. ppc440spe_chan->device->id, __func__, len,
  2037. flags & DMA_PREP_INTERRUPT ? 1 : 0);
  2038. slot_cnt = slots_per_op = 1;
  2039. sw_desc = ppc440spe_adma_alloc_slots(ppc440spe_chan, slot_cnt,
  2040. slots_per_op);
  2041. if (sw_desc) {
  2042. group_start = sw_desc->group_head;
  2043. ppc440spe_desc_init_memcpy(group_start, flags);
  2044. ppc440spe_adma_set_dest(group_start, dma_dest, 0);
  2045. ppc440spe_adma_memcpy_xor_set_src(group_start, dma_src, 0);
  2046. ppc440spe_desc_set_byte_count(group_start, ppc440spe_chan, len);
  2047. sw_desc->unmap_len = len;
  2048. sw_desc->async_tx.flags = flags;
  2049. }
  2050. spin_unlock_bh(&ppc440spe_chan->lock);
  2051. return sw_desc ? &sw_desc->async_tx : NULL;
  2052. }
  2053. /**
  2054. * ppc440spe_adma_prep_dma_memset - prepare CDB for a MEMSET operation
  2055. */
  2056. static struct dma_async_tx_descriptor *ppc440spe_adma_prep_dma_memset(
  2057. struct dma_chan *chan, dma_addr_t dma_dest, int value,
  2058. size_t len, unsigned long flags)
  2059. {
  2060. struct ppc440spe_adma_chan *ppc440spe_chan;
  2061. struct ppc440spe_adma_desc_slot *sw_desc, *group_start;
  2062. int slot_cnt, slots_per_op;
  2063. ppc440spe_chan = to_ppc440spe_adma_chan(chan);
  2064. if (unlikely(!len))
  2065. return NULL;
  2066. BUG_ON(len > PPC440SPE_ADMA_DMA_MAX_BYTE_COUNT);
  2067. spin_lock_bh(&ppc440spe_chan->lock);
  2068. dev_dbg(ppc440spe_chan->device->common.dev,
  2069. "ppc440spe adma%d: %s cal: %u len: %u int_en %d\n",
  2070. ppc440spe_chan->device->id, __func__, value, len,
  2071. flags & DMA_PREP_INTERRUPT ? 1 : 0);
  2072. slot_cnt = slots_per_op = 1;
  2073. sw_desc = ppc440spe_adma_alloc_slots(ppc440spe_chan, slot_cnt,
  2074. slots_per_op);
  2075. if (sw_desc) {
  2076. group_start = sw_desc->group_head;
  2077. ppc440spe_desc_init_memset(group_start, value, flags);
  2078. ppc440spe_adma_set_dest(group_start, dma_dest, 0);
  2079. ppc440spe_desc_set_byte_count(group_start, ppc440spe_chan, len);
  2080. sw_desc->unmap_len = len;
  2081. sw_desc->async_tx.flags = flags;
  2082. }
  2083. spin_unlock_bh(&ppc440spe_chan->lock);
  2084. return sw_desc ? &sw_desc->async_tx : NULL;
  2085. }
  2086. /**
  2087. * ppc440spe_adma_prep_dma_xor - prepare CDB for a XOR operation
  2088. */
  2089. static struct dma_async_tx_descriptor *ppc440spe_adma_prep_dma_xor(
  2090. struct dma_chan *chan, dma_addr_t dma_dest,
  2091. dma_addr_t *dma_src, u32 src_cnt, size_t len,
  2092. unsigned long flags)
  2093. {
  2094. struct ppc440spe_adma_chan *ppc440spe_chan;
  2095. struct ppc440spe_adma_desc_slot *sw_desc, *group_start;
  2096. int slot_cnt, slots_per_op;
  2097. ppc440spe_chan = to_ppc440spe_adma_chan(chan);
  2098. ADMA_LL_DBG(prep_dma_xor_dbg(ppc440spe_chan->device->id,
  2099. dma_dest, dma_src, src_cnt));
  2100. if (unlikely(!len))
  2101. return NULL;
  2102. BUG_ON(len > PPC440SPE_ADMA_XOR_MAX_BYTE_COUNT);
  2103. dev_dbg(ppc440spe_chan->device->common.dev,
  2104. "ppc440spe adma%d: %s src_cnt: %d len: %u int_en: %d\n",
  2105. ppc440spe_chan->device->id, __func__, src_cnt, len,
  2106. flags & DMA_PREP_INTERRUPT ? 1 : 0);
  2107. spin_lock_bh(&ppc440spe_chan->lock);
  2108. slot_cnt = ppc440spe_chan_xor_slot_count(len, src_cnt, &slots_per_op);
  2109. sw_desc = ppc440spe_adma_alloc_slots(ppc440spe_chan, slot_cnt,
  2110. slots_per_op);
  2111. if (sw_desc) {
  2112. group_start = sw_desc->group_head;
  2113. ppc440spe_desc_init_xor(group_start, src_cnt, flags);
  2114. ppc440spe_adma_set_dest(group_start, dma_dest, 0);
  2115. while (src_cnt--)
  2116. ppc440spe_adma_memcpy_xor_set_src(group_start,
  2117. dma_src[src_cnt], src_cnt);
  2118. ppc440spe_desc_set_byte_count(group_start, ppc440spe_chan, len);
  2119. sw_desc->unmap_len = len;
  2120. sw_desc->async_tx.flags = flags;
  2121. }
  2122. spin_unlock_bh(&ppc440spe_chan->lock);
  2123. return sw_desc ? &sw_desc->async_tx : NULL;
  2124. }
  2125. static inline void
  2126. ppc440spe_desc_set_xor_src_cnt(struct ppc440spe_adma_desc_slot *desc,
  2127. int src_cnt);
  2128. static void ppc440spe_init_rxor_cursor(struct ppc440spe_rxor *cursor);
  2129. /**
  2130. * ppc440spe_adma_init_dma2rxor_slot -
  2131. */
  2132. static void ppc440spe_adma_init_dma2rxor_slot(
  2133. struct ppc440spe_adma_desc_slot *desc,
  2134. dma_addr_t *src, int src_cnt)
  2135. {
  2136. int i;
  2137. /* initialize CDB */
  2138. for (i = 0; i < src_cnt; i++) {
  2139. ppc440spe_adma_dma2rxor_prep_src(desc, &desc->rxor_cursor, i,
  2140. desc->src_cnt, (u32)src[i]);
  2141. }
  2142. }
  2143. /**
  2144. * ppc440spe_dma01_prep_mult -
  2145. * for Q operation where destination is also the source
  2146. */
  2147. static struct ppc440spe_adma_desc_slot *ppc440spe_dma01_prep_mult(
  2148. struct ppc440spe_adma_chan *ppc440spe_chan,
  2149. dma_addr_t *dst, int dst_cnt, dma_addr_t *src, int src_cnt,
  2150. const unsigned char *scf, size_t len, unsigned long flags)
  2151. {
  2152. struct ppc440spe_adma_desc_slot *sw_desc = NULL;
  2153. unsigned long op = 0;
  2154. int slot_cnt;
  2155. set_bit(PPC440SPE_DESC_WXOR, &op);
  2156. slot_cnt = 2;
  2157. spin_lock_bh(&ppc440spe_chan->lock);
  2158. /* use WXOR, each descriptor occupies one slot */
  2159. sw_desc = ppc440spe_adma_alloc_slots(ppc440spe_chan, slot_cnt, 1);
  2160. if (sw_desc) {
  2161. struct ppc440spe_adma_chan *chan;
  2162. struct ppc440spe_adma_desc_slot *iter;
  2163. struct dma_cdb *hw_desc;
  2164. chan = to_ppc440spe_adma_chan(sw_desc->async_tx.chan);
  2165. set_bits(op, &sw_desc->flags);
  2166. sw_desc->src_cnt = src_cnt;
  2167. sw_desc->dst_cnt = dst_cnt;
  2168. /* First descriptor, zero data in the destination and copy it
  2169. * to q page using MULTICAST transfer.
  2170. */
  2171. iter = list_first_entry(&sw_desc->group_list,
  2172. struct ppc440spe_adma_desc_slot,
  2173. chain_node);
  2174. memset(iter->hw_desc, 0, sizeof(struct dma_cdb));
  2175. /* set 'next' pointer */
  2176. iter->hw_next = list_entry(iter->chain_node.next,
  2177. struct ppc440spe_adma_desc_slot,
  2178. chain_node);
  2179. clear_bit(PPC440SPE_DESC_INT, &iter->flags);
  2180. hw_desc = iter->hw_desc;
  2181. hw_desc->opc = DMA_CDB_OPC_MULTICAST;
  2182. ppc440spe_desc_set_dest_addr(iter, chan,
  2183. DMA_CUED_XOR_BASE, dst[0], 0);
  2184. ppc440spe_desc_set_dest_addr(iter, chan, 0, dst[1], 1);
  2185. ppc440spe_desc_set_src_addr(iter, chan, 0, DMA_CUED_XOR_HB,
  2186. src[0]);
  2187. ppc440spe_desc_set_byte_count(iter, ppc440spe_chan, len);
  2188. iter->unmap_len = len;
  2189. /*
  2190. * Second descriptor, multiply data from the q page
  2191. * and store the result in real destination.
  2192. */
  2193. iter = list_first_entry(&iter->chain_node,
  2194. struct ppc440spe_adma_desc_slot,
  2195. chain_node);
  2196. memset(iter->hw_desc, 0, sizeof(struct dma_cdb));
  2197. iter->hw_next = NULL;
  2198. if (flags & DMA_PREP_INTERRUPT)
  2199. set_bit(PPC440SPE_DESC_INT, &iter->flags);
  2200. else
  2201. clear_bit(PPC440SPE_DESC_INT, &iter->flags);
  2202. hw_desc = iter->hw_desc;
  2203. hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
  2204. ppc440spe_desc_set_src_addr(iter, chan, 0,
  2205. DMA_CUED_XOR_HB, dst[1]);
  2206. ppc440spe_desc_set_dest_addr(iter, chan,
  2207. DMA_CUED_XOR_BASE, dst[0], 0);
  2208. ppc440spe_desc_set_src_mult(iter, chan, DMA_CUED_MULT1_OFF,
  2209. DMA_CDB_SG_DST1, scf[0]);
  2210. ppc440spe_desc_set_byte_count(iter, ppc440spe_chan, len);
  2211. iter->unmap_len = len;
  2212. sw_desc->async_tx.flags = flags;
  2213. }
  2214. spin_unlock_bh(&ppc440spe_chan->lock);
  2215. return sw_desc;
  2216. }
  2217. /**
  2218. * ppc440spe_dma01_prep_sum_product -
  2219. * Dx = A*(P+Pxy) + B*(Q+Qxy) operation where destination is also
  2220. * the source.
  2221. */
  2222. static struct ppc440spe_adma_desc_slot *ppc440spe_dma01_prep_sum_product(
  2223. struct ppc440spe_adma_chan *ppc440spe_chan,
  2224. dma_addr_t *dst, dma_addr_t *src, int src_cnt,
  2225. const unsigned char *scf, size_t len, unsigned long flags)
  2226. {
  2227. struct ppc440spe_adma_desc_slot *sw_desc = NULL;
  2228. unsigned long op = 0;
  2229. int slot_cnt;
  2230. set_bit(PPC440SPE_DESC_WXOR, &op);
  2231. slot_cnt = 3;
  2232. spin_lock_bh(&ppc440spe_chan->lock);
  2233. /* WXOR, each descriptor occupies one slot */
  2234. sw_desc = ppc440spe_adma_alloc_slots(ppc440spe_chan, slot_cnt, 1);
  2235. if (sw_desc) {
  2236. struct ppc440spe_adma_chan *chan;
  2237. struct ppc440spe_adma_desc_slot *iter;
  2238. struct dma_cdb *hw_desc;
  2239. chan = to_ppc440spe_adma_chan(sw_desc->async_tx.chan);
  2240. set_bits(op, &sw_desc->flags);
  2241. sw_desc->src_cnt = src_cnt;
  2242. sw_desc->dst_cnt = 1;
  2243. /* 1st descriptor, src[1] data to q page and zero destination */
  2244. iter = list_first_entry(&sw_desc->group_list,
  2245. struct ppc440spe_adma_desc_slot,
  2246. chain_node);
  2247. memset(iter->hw_desc, 0, sizeof(struct dma_cdb));
  2248. iter->hw_next = list_entry(iter->chain_node.next,
  2249. struct ppc440spe_adma_desc_slot,
  2250. chain_node);
  2251. clear_bit(PPC440SPE_DESC_INT, &iter->flags);
  2252. hw_desc = iter->hw_desc;
  2253. hw_desc->opc = DMA_CDB_OPC_MULTICAST;
  2254. ppc440spe_desc_set_dest_addr(iter, chan, DMA_CUED_XOR_BASE,
  2255. *dst, 0);
  2256. ppc440spe_desc_set_dest_addr(iter, chan, 0,
  2257. ppc440spe_chan->qdest, 1);
  2258. ppc440spe_desc_set_src_addr(iter, chan, 0, DMA_CUED_XOR_HB,
  2259. src[1]);
  2260. ppc440spe_desc_set_byte_count(iter, ppc440spe_chan, len);
  2261. iter->unmap_len = len;
  2262. /* 2nd descriptor, multiply src[1] data and store the
  2263. * result in destination */
  2264. iter = list_first_entry(&iter->chain_node,
  2265. struct ppc440spe_adma_desc_slot,
  2266. chain_node);
  2267. memset(iter->hw_desc, 0, sizeof(struct dma_cdb));
  2268. /* set 'next' pointer */
  2269. iter->hw_next = list_entry(iter->chain_node.next,
  2270. struct ppc440spe_adma_desc_slot,
  2271. chain_node);
  2272. if (flags & DMA_PREP_INTERRUPT)
  2273. set_bit(PPC440SPE_DESC_INT, &iter->flags);
  2274. else
  2275. clear_bit(PPC440SPE_DESC_INT, &iter->flags);
  2276. hw_desc = iter->hw_desc;
  2277. hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
  2278. ppc440spe_desc_set_src_addr(iter, chan, 0, DMA_CUED_XOR_HB,
  2279. ppc440spe_chan->qdest);
  2280. ppc440spe_desc_set_dest_addr(iter, chan, DMA_CUED_XOR_BASE,
  2281. *dst, 0);
  2282. ppc440spe_desc_set_src_mult(iter, chan, DMA_CUED_MULT1_OFF,
  2283. DMA_CDB_SG_DST1, scf[1]);
  2284. ppc440spe_desc_set_byte_count(iter, ppc440spe_chan, len);
  2285. iter->unmap_len = len;
  2286. /*
  2287. * 3rd descriptor, multiply src[0] data and xor it
  2288. * with destination
  2289. */
  2290. iter = list_first_entry(&iter->chain_node,
  2291. struct ppc440spe_adma_desc_slot,
  2292. chain_node);
  2293. memset(iter->hw_desc, 0, sizeof(struct dma_cdb));
  2294. iter->hw_next = NULL;
  2295. if (flags & DMA_PREP_INTERRUPT)
  2296. set_bit(PPC440SPE_DESC_INT, &iter->flags);
  2297. else
  2298. clear_bit(PPC440SPE_DESC_INT, &iter->flags);
  2299. hw_desc = iter->hw_desc;
  2300. hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
  2301. ppc440spe_desc_set_src_addr(iter, chan, 0, DMA_CUED_XOR_HB,
  2302. src[0]);
  2303. ppc440spe_desc_set_dest_addr(iter, chan, DMA_CUED_XOR_BASE,
  2304. *dst, 0);
  2305. ppc440spe_desc_set_src_mult(iter, chan, DMA_CUED_MULT1_OFF,
  2306. DMA_CDB_SG_DST1, scf[0]);
  2307. ppc440spe_desc_set_byte_count(iter, ppc440spe_chan, len);
  2308. iter->unmap_len = len;
  2309. sw_desc->async_tx.flags = flags;
  2310. }
  2311. spin_unlock_bh(&ppc440spe_chan->lock);
  2312. return sw_desc;
  2313. }
  2314. static struct ppc440spe_adma_desc_slot *ppc440spe_dma01_prep_pq(
  2315. struct ppc440spe_adma_chan *ppc440spe_chan,
  2316. dma_addr_t *dst, int dst_cnt, dma_addr_t *src, int src_cnt,
  2317. const unsigned char *scf, size_t len, unsigned long flags)
  2318. {
  2319. int slot_cnt;
  2320. struct ppc440spe_adma_desc_slot *sw_desc = NULL, *iter;
  2321. unsigned long op = 0;
  2322. unsigned char mult = 1;
  2323. pr_debug("%s: dst_cnt %d, src_cnt %d, len %d\n",
  2324. __func__, dst_cnt, src_cnt, len);
  2325. /* select operations WXOR/RXOR depending on the
  2326. * source addresses of operators and the number
  2327. * of destinations (RXOR support only Q-parity calculations)
  2328. */
  2329. set_bit(PPC440SPE_DESC_WXOR, &op);
  2330. if (!test_and_set_bit(PPC440SPE_RXOR_RUN, &ppc440spe_rxor_state)) {
  2331. /* no active RXOR;
  2332. * do RXOR if:
  2333. * - there are more than 1 source,
  2334. * - len is aligned on 512-byte boundary,
  2335. * - source addresses fit to one of 4 possible regions.
  2336. */
  2337. if (src_cnt > 1 &&
  2338. !(len & MQ0_CF2H_RXOR_BS_MASK) &&
  2339. (src[0] + len) == src[1]) {
  2340. /* may do RXOR R1 R2 */
  2341. set_bit(PPC440SPE_DESC_RXOR, &op);
  2342. if (src_cnt != 2) {
  2343. /* may try to enhance region of RXOR */
  2344. if ((src[1] + len) == src[2]) {
  2345. /* do RXOR R1 R2 R3 */
  2346. set_bit(PPC440SPE_DESC_RXOR123,
  2347. &op);
  2348. } else if ((src[1] + len * 2) == src[2]) {
  2349. /* do RXOR R1 R2 R4 */
  2350. set_bit(PPC440SPE_DESC_RXOR124, &op);
  2351. } else if ((src[1] + len * 3) == src[2]) {
  2352. /* do RXOR R1 R2 R5 */
  2353. set_bit(PPC440SPE_DESC_RXOR125,
  2354. &op);
  2355. } else {
  2356. /* do RXOR R1 R2 */
  2357. set_bit(PPC440SPE_DESC_RXOR12,
  2358. &op);
  2359. }
  2360. } else {
  2361. /* do RXOR R1 R2 */
  2362. set_bit(PPC440SPE_DESC_RXOR12, &op);
  2363. }
  2364. }
  2365. if (!test_bit(PPC440SPE_DESC_RXOR, &op)) {
  2366. /* can not do this operation with RXOR */
  2367. clear_bit(PPC440SPE_RXOR_RUN,
  2368. &ppc440spe_rxor_state);
  2369. } else {
  2370. /* can do; set block size right now */
  2371. ppc440spe_desc_set_rxor_block_size(len);
  2372. }
  2373. }
  2374. /* Number of necessary slots depends on operation type selected */
  2375. if (!test_bit(PPC440SPE_DESC_RXOR, &op)) {
  2376. /* This is a WXOR only chain. Need descriptors for each
  2377. * source to GF-XOR them with WXOR, and need descriptors
  2378. * for each destination to zero them with WXOR
  2379. */
  2380. slot_cnt = src_cnt;
  2381. if (flags & DMA_PREP_ZERO_P) {
  2382. slot_cnt++;
  2383. set_bit(PPC440SPE_ZERO_P, &op);
  2384. }
  2385. if (flags & DMA_PREP_ZERO_Q) {
  2386. slot_cnt++;
  2387. set_bit(PPC440SPE_ZERO_Q, &op);
  2388. }
  2389. } else {
  2390. /* Need 1/2 descriptor for RXOR operation, and
  2391. * need (src_cnt - (2 or 3)) for WXOR of sources
  2392. * remained (if any)
  2393. */
  2394. slot_cnt = dst_cnt;
  2395. if (flags & DMA_PREP_ZERO_P)
  2396. set_bit(PPC440SPE_ZERO_P, &op);
  2397. if (flags & DMA_PREP_ZERO_Q)
  2398. set_bit(PPC440SPE_ZERO_Q, &op);
  2399. if (test_bit(PPC440SPE_DESC_RXOR12, &op))
  2400. slot_cnt += src_cnt - 2;
  2401. else
  2402. slot_cnt += src_cnt - 3;
  2403. /* Thus we have either RXOR only chain or
  2404. * mixed RXOR/WXOR
  2405. */
  2406. if (slot_cnt == dst_cnt)
  2407. /* RXOR only chain */
  2408. clear_bit(PPC440SPE_DESC_WXOR, &op);
  2409. }
  2410. spin_lock_bh(&ppc440spe_chan->lock);
  2411. /* for both RXOR/WXOR each descriptor occupies one slot */
  2412. sw_desc = ppc440spe_adma_alloc_slots(ppc440spe_chan, slot_cnt, 1);
  2413. if (sw_desc) {
  2414. ppc440spe_desc_init_dma01pq(sw_desc, dst_cnt, src_cnt,
  2415. flags, op);
  2416. /* setup dst/src/mult */
  2417. pr_debug("%s: set dst descriptor 0, 1: 0x%016llx, 0x%016llx\n",
  2418. __func__, dst[0], dst[1]);
  2419. ppc440spe_adma_pq_set_dest(sw_desc, dst, flags);
  2420. while (src_cnt--) {
  2421. ppc440spe_adma_pq_set_src(sw_desc, src[src_cnt],
  2422. src_cnt);
  2423. /* NOTE: "Multi = 0 is equivalent to = 1" as it
  2424. * stated in 440SPSPe_RAID6_Addendum_UM_1_17.pdf
  2425. * doesn't work for RXOR with DMA0/1! Instead, multi=0
  2426. * leads to zeroing source data after RXOR.
  2427. * So, for P case set-up mult=1 explicitly.
  2428. */
  2429. if (!(flags & DMA_PREP_PQ_DISABLE_Q))
  2430. mult = scf[src_cnt];
  2431. ppc440spe_adma_pq_set_src_mult(sw_desc,
  2432. mult, src_cnt, dst_cnt - 1);
  2433. }
  2434. /* Setup byte count foreach slot just allocated */
  2435. sw_desc->async_tx.flags = flags;
  2436. list_for_each_entry(iter, &sw_desc->group_list,
  2437. chain_node) {
  2438. ppc440spe_desc_set_byte_count(iter,
  2439. ppc440spe_chan, len);
  2440. iter->unmap_len = len;
  2441. }
  2442. }
  2443. spin_unlock_bh(&ppc440spe_chan->lock);
  2444. return sw_desc;
  2445. }
  2446. static struct ppc440spe_adma_desc_slot *ppc440spe_dma2_prep_pq(
  2447. struct ppc440spe_adma_chan *ppc440spe_chan,
  2448. dma_addr_t *dst, int dst_cnt, dma_addr_t *src, int src_cnt,
  2449. const unsigned char *scf, size_t len, unsigned long flags)
  2450. {
  2451. int slot_cnt, descs_per_op;
  2452. struct ppc440spe_adma_desc_slot *sw_desc = NULL, *iter;
  2453. unsigned long op = 0;
  2454. unsigned char mult = 1;
  2455. BUG_ON(!dst_cnt);
  2456. /*pr_debug("%s: dst_cnt %d, src_cnt %d, len %d\n",
  2457. __func__, dst_cnt, src_cnt, len);*/
  2458. spin_lock_bh(&ppc440spe_chan->lock);
  2459. descs_per_op = ppc440spe_dma2_pq_slot_count(src, src_cnt, len);
  2460. if (descs_per_op < 0) {
  2461. spin_unlock_bh(&ppc440spe_chan->lock);
  2462. return NULL;
  2463. }
  2464. /* depending on number of sources we have 1 or 2 RXOR chains */
  2465. slot_cnt = descs_per_op * dst_cnt;
  2466. sw_desc = ppc440spe_adma_alloc_slots(ppc440spe_chan, slot_cnt, 1);
  2467. if (sw_desc) {
  2468. op = slot_cnt;
  2469. sw_desc->async_tx.flags = flags;
  2470. list_for_each_entry(iter, &sw_desc->group_list, chain_node) {
  2471. ppc440spe_desc_init_dma2pq(iter, dst_cnt, src_cnt,
  2472. --op ? 0 : flags);
  2473. ppc440spe_desc_set_byte_count(iter, ppc440spe_chan,
  2474. len);
  2475. iter->unmap_len = len;
  2476. ppc440spe_init_rxor_cursor(&(iter->rxor_cursor));
  2477. iter->rxor_cursor.len = len;
  2478. iter->descs_per_op = descs_per_op;
  2479. }
  2480. op = 0;
  2481. list_for_each_entry(iter, &sw_desc->group_list, chain_node) {
  2482. op++;
  2483. if (op % descs_per_op == 0)
  2484. ppc440spe_adma_init_dma2rxor_slot(iter, src,
  2485. src_cnt);
  2486. if (likely(!list_is_last(&iter->chain_node,
  2487. &sw_desc->group_list))) {
  2488. /* set 'next' pointer */
  2489. iter->hw_next =
  2490. list_entry(iter->chain_node.next,
  2491. struct ppc440spe_adma_desc_slot,
  2492. chain_node);
  2493. ppc440spe_xor_set_link(iter, iter->hw_next);
  2494. } else {
  2495. /* this is the last descriptor. */
  2496. iter->hw_next = NULL;
  2497. }
  2498. }
  2499. /* fixup head descriptor */
  2500. sw_desc->dst_cnt = dst_cnt;
  2501. if (flags & DMA_PREP_ZERO_P)
  2502. set_bit(PPC440SPE_ZERO_P, &sw_desc->flags);
  2503. if (flags & DMA_PREP_ZERO_Q)
  2504. set_bit(PPC440SPE_ZERO_Q, &sw_desc->flags);
  2505. /* setup dst/src/mult */
  2506. ppc440spe_adma_pq_set_dest(sw_desc, dst, flags);
  2507. while (src_cnt--) {
  2508. /* handle descriptors (if dst_cnt == 2) inside
  2509. * the ppc440spe_adma_pq_set_srcxxx() functions
  2510. */
  2511. ppc440spe_adma_pq_set_src(sw_desc, src[src_cnt],
  2512. src_cnt);
  2513. if (!(flags & DMA_PREP_PQ_DISABLE_Q))
  2514. mult = scf[src_cnt];
  2515. ppc440spe_adma_pq_set_src_mult(sw_desc,
  2516. mult, src_cnt, dst_cnt - 1);
  2517. }
  2518. }
  2519. spin_unlock_bh(&ppc440spe_chan->lock);
  2520. ppc440spe_desc_set_rxor_block_size(len);
  2521. return sw_desc;
  2522. }
  2523. /**
  2524. * ppc440spe_adma_prep_dma_pq - prepare CDB (group) for a GF-XOR operation
  2525. */
  2526. static struct dma_async_tx_descriptor *ppc440spe_adma_prep_dma_pq(
  2527. struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
  2528. unsigned int src_cnt, const unsigned char *scf,
  2529. size_t len, unsigned long flags)
  2530. {
  2531. struct ppc440spe_adma_chan *ppc440spe_chan;
  2532. struct ppc440spe_adma_desc_slot *sw_desc = NULL;
  2533. int dst_cnt = 0;
  2534. ppc440spe_chan = to_ppc440spe_adma_chan(chan);
  2535. ADMA_LL_DBG(prep_dma_pq_dbg(ppc440spe_chan->device->id,
  2536. dst, src, src_cnt));
  2537. BUG_ON(!len);
  2538. BUG_ON(len > PPC440SPE_ADMA_XOR_MAX_BYTE_COUNT);
  2539. BUG_ON(!src_cnt);
  2540. if (src_cnt == 1 && dst[1] == src[0]) {
  2541. dma_addr_t dest[2];
  2542. /* dst[1] is real destination (Q) */
  2543. dest[0] = dst[1];
  2544. /* this is the page to multicast source data to */
  2545. dest[1] = ppc440spe_chan->qdest;
  2546. sw_desc = ppc440spe_dma01_prep_mult(ppc440spe_chan,
  2547. dest, 2, src, src_cnt, scf, len, flags);
  2548. return sw_desc ? &sw_desc->async_tx : NULL;
  2549. }
  2550. if (src_cnt == 2 && dst[1] == src[1]) {
  2551. sw_desc = ppc440spe_dma01_prep_sum_product(ppc440spe_chan,
  2552. &dst[1], src, 2, scf, len, flags);
  2553. return sw_desc ? &sw_desc->async_tx : NULL;
  2554. }
  2555. if (!(flags & DMA_PREP_PQ_DISABLE_P)) {
  2556. BUG_ON(!dst[0]);
  2557. dst_cnt++;
  2558. flags |= DMA_PREP_ZERO_P;
  2559. }
  2560. if (!(flags & DMA_PREP_PQ_DISABLE_Q)) {
  2561. BUG_ON(!dst[1]);
  2562. dst_cnt++;
  2563. flags |= DMA_PREP_ZERO_Q;
  2564. }
  2565. BUG_ON(!dst_cnt);
  2566. dev_dbg(ppc440spe_chan->device->common.dev,
  2567. "ppc440spe adma%d: %s src_cnt: %d len: %u int_en: %d\n",
  2568. ppc440spe_chan->device->id, __func__, src_cnt, len,
  2569. flags & DMA_PREP_INTERRUPT ? 1 : 0);
  2570. switch (ppc440spe_chan->device->id) {
  2571. case PPC440SPE_DMA0_ID:
  2572. case PPC440SPE_DMA1_ID:
  2573. sw_desc = ppc440spe_dma01_prep_pq(ppc440spe_chan,
  2574. dst, dst_cnt, src, src_cnt, scf,
  2575. len, flags);
  2576. break;
  2577. case PPC440SPE_XOR_ID:
  2578. sw_desc = ppc440spe_dma2_prep_pq(ppc440spe_chan,
  2579. dst, dst_cnt, src, src_cnt, scf,
  2580. len, flags);
  2581. break;
  2582. }
  2583. return sw_desc ? &sw_desc->async_tx : NULL;
  2584. }
  2585. /**
  2586. * ppc440spe_adma_prep_dma_pqzero_sum - prepare CDB group for
  2587. * a PQ_ZERO_SUM operation
  2588. */
  2589. static struct dma_async_tx_descriptor *ppc440spe_adma_prep_dma_pqzero_sum(
  2590. struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
  2591. unsigned int src_cnt, const unsigned char *scf, size_t len,
  2592. enum sum_check_flags *pqres, unsigned long flags)
  2593. {
  2594. struct ppc440spe_adma_chan *ppc440spe_chan;
  2595. struct ppc440spe_adma_desc_slot *sw_desc, *iter;
  2596. dma_addr_t pdest, qdest;
  2597. int slot_cnt, slots_per_op, idst, dst_cnt;
  2598. ppc440spe_chan = to_ppc440spe_adma_chan(chan);
  2599. if (flags & DMA_PREP_PQ_DISABLE_P)
  2600. pdest = 0;
  2601. else
  2602. pdest = pq[0];
  2603. if (flags & DMA_PREP_PQ_DISABLE_Q)
  2604. qdest = 0;
  2605. else
  2606. qdest = pq[1];
  2607. ADMA_LL_DBG(prep_dma_pqzero_sum_dbg(ppc440spe_chan->device->id,
  2608. src, src_cnt, scf));
  2609. /* Always use WXOR for P/Q calculations (two destinations).
  2610. * Need 1 or 2 extra slots to verify results are zero.
  2611. */
  2612. idst = dst_cnt = (pdest && qdest) ? 2 : 1;
  2613. /* One additional slot per destination to clone P/Q
  2614. * before calculation (we have to preserve destinations).
  2615. */
  2616. slot_cnt = src_cnt + dst_cnt * 2;
  2617. slots_per_op = 1;
  2618. spin_lock_bh(&ppc440spe_chan->lock);
  2619. sw_desc = ppc440spe_adma_alloc_slots(ppc440spe_chan, slot_cnt,
  2620. slots_per_op);
  2621. if (sw_desc) {
  2622. ppc440spe_desc_init_dma01pqzero_sum(sw_desc, dst_cnt, src_cnt);
  2623. /* Setup byte count for each slot just allocated */
  2624. sw_desc->async_tx.flags = flags;
  2625. list_for_each_entry(iter, &sw_desc->group_list, chain_node) {
  2626. ppc440spe_desc_set_byte_count(iter, ppc440spe_chan,
  2627. len);
  2628. iter->unmap_len = len;
  2629. }
  2630. if (pdest) {
  2631. struct dma_cdb *hw_desc;
  2632. struct ppc440spe_adma_chan *chan;
  2633. iter = sw_desc->group_head;
  2634. chan = to_ppc440spe_adma_chan(iter->async_tx.chan);
  2635. memset(iter->hw_desc, 0, sizeof(struct dma_cdb));
  2636. iter->hw_next = list_entry(iter->chain_node.next,
  2637. struct ppc440spe_adma_desc_slot,
  2638. chain_node);
  2639. hw_desc = iter->hw_desc;
  2640. hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
  2641. iter->src_cnt = 0;
  2642. iter->dst_cnt = 0;
  2643. ppc440spe_desc_set_dest_addr(iter, chan, 0,
  2644. ppc440spe_chan->pdest, 0);
  2645. ppc440spe_desc_set_src_addr(iter, chan, 0, 0, pdest);
  2646. ppc440spe_desc_set_byte_count(iter, ppc440spe_chan,
  2647. len);
  2648. iter->unmap_len = 0;
  2649. /* override pdest to preserve original P */
  2650. pdest = ppc440spe_chan->pdest;
  2651. }
  2652. if (qdest) {
  2653. struct dma_cdb *hw_desc;
  2654. struct ppc440spe_adma_chan *chan;
  2655. iter = list_first_entry(&sw_desc->group_list,
  2656. struct ppc440spe_adma_desc_slot,
  2657. chain_node);
  2658. chan = to_ppc440spe_adma_chan(iter->async_tx.chan);
  2659. if (pdest) {
  2660. iter = list_entry(iter->chain_node.next,
  2661. struct ppc440spe_adma_desc_slot,
  2662. chain_node);
  2663. }
  2664. memset(iter->hw_desc, 0, sizeof(struct dma_cdb));
  2665. iter->hw_next = list_entry(iter->chain_node.next,
  2666. struct ppc440spe_adma_desc_slot,
  2667. chain_node);
  2668. hw_desc = iter->hw_desc;
  2669. hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
  2670. iter->src_cnt = 0;
  2671. iter->dst_cnt = 0;
  2672. ppc440spe_desc_set_dest_addr(iter, chan, 0,
  2673. ppc440spe_chan->qdest, 0);
  2674. ppc440spe_desc_set_src_addr(iter, chan, 0, 0, qdest);
  2675. ppc440spe_desc_set_byte_count(iter, ppc440spe_chan,
  2676. len);
  2677. iter->unmap_len = 0;
  2678. /* override qdest to preserve original Q */
  2679. qdest = ppc440spe_chan->qdest;
  2680. }
  2681. /* Setup destinations for P/Q ops */
  2682. ppc440spe_adma_pqzero_sum_set_dest(sw_desc, pdest, qdest);
  2683. /* Setup zero QWORDs into DCHECK CDBs */
  2684. idst = dst_cnt;
  2685. list_for_each_entry_reverse(iter, &sw_desc->group_list,
  2686. chain_node) {
  2687. /*
  2688. * The last CDB corresponds to Q-parity check,
  2689. * the one before last CDB corresponds
  2690. * P-parity check
  2691. */
  2692. if (idst == DMA_DEST_MAX_NUM) {
  2693. if (idst == dst_cnt) {
  2694. set_bit(PPC440SPE_DESC_QCHECK,
  2695. &iter->flags);
  2696. } else {
  2697. set_bit(PPC440SPE_DESC_PCHECK,
  2698. &iter->flags);
  2699. }
  2700. } else {
  2701. if (qdest) {
  2702. set_bit(PPC440SPE_DESC_QCHECK,
  2703. &iter->flags);
  2704. } else {
  2705. set_bit(PPC440SPE_DESC_PCHECK,
  2706. &iter->flags);
  2707. }
  2708. }
  2709. iter->xor_check_result = pqres;
  2710. /*
  2711. * set it to zero, if check fail then result will
  2712. * be updated
  2713. */
  2714. *iter->xor_check_result = 0;
  2715. ppc440spe_desc_set_dcheck(iter, ppc440spe_chan,
  2716. ppc440spe_qword);
  2717. if (!(--dst_cnt))
  2718. break;
  2719. }
  2720. /* Setup sources and mults for P/Q ops */
  2721. list_for_each_entry_continue_reverse(iter, &sw_desc->group_list,
  2722. chain_node) {
  2723. struct ppc440spe_adma_chan *chan;
  2724. u32 mult_dst;
  2725. chan = to_ppc440spe_adma_chan(iter->async_tx.chan);
  2726. ppc440spe_desc_set_src_addr(iter, chan, 0,
  2727. DMA_CUED_XOR_HB,
  2728. src[src_cnt - 1]);
  2729. if (qdest) {
  2730. mult_dst = (dst_cnt - 1) ? DMA_CDB_SG_DST2 :
  2731. DMA_CDB_SG_DST1;
  2732. ppc440spe_desc_set_src_mult(iter, chan,
  2733. DMA_CUED_MULT1_OFF,
  2734. mult_dst,
  2735. scf[src_cnt - 1]);
  2736. }
  2737. if (!(--src_cnt))
  2738. break;
  2739. }
  2740. }
  2741. spin_unlock_bh(&ppc440spe_chan->lock);
  2742. return sw_desc ? &sw_desc->async_tx : NULL;
  2743. }
  2744. /**
  2745. * ppc440spe_adma_prep_dma_xor_zero_sum - prepare CDB group for
  2746. * XOR ZERO_SUM operation
  2747. */
  2748. static struct dma_async_tx_descriptor *ppc440spe_adma_prep_dma_xor_zero_sum(
  2749. struct dma_chan *chan, dma_addr_t *src, unsigned int src_cnt,
  2750. size_t len, enum sum_check_flags *result, unsigned long flags)
  2751. {
  2752. struct dma_async_tx_descriptor *tx;
  2753. dma_addr_t pq[2];
  2754. /* validate P, disable Q */
  2755. pq[0] = src[0];
  2756. pq[1] = 0;
  2757. flags |= DMA_PREP_PQ_DISABLE_Q;
  2758. tx = ppc440spe_adma_prep_dma_pqzero_sum(chan, pq, &src[1],
  2759. src_cnt - 1, 0, len,
  2760. result, flags);
  2761. return tx;
  2762. }
  2763. /**
  2764. * ppc440spe_adma_set_dest - set destination address into descriptor
  2765. */
  2766. static void ppc440spe_adma_set_dest(struct ppc440spe_adma_desc_slot *sw_desc,
  2767. dma_addr_t addr, int index)
  2768. {
  2769. struct ppc440spe_adma_chan *chan;
  2770. BUG_ON(index >= sw_desc->dst_cnt);
  2771. chan = to_ppc440spe_adma_chan(sw_desc->async_tx.chan);
  2772. switch (chan->device->id) {
  2773. case PPC440SPE_DMA0_ID:
  2774. case PPC440SPE_DMA1_ID:
  2775. /* to do: support transfers lengths >
  2776. * PPC440SPE_ADMA_DMA/XOR_MAX_BYTE_COUNT
  2777. */
  2778. ppc440spe_desc_set_dest_addr(sw_desc->group_head,
  2779. chan, 0, addr, index);
  2780. break;
  2781. case PPC440SPE_XOR_ID:
  2782. sw_desc = ppc440spe_get_group_entry(sw_desc, index);
  2783. ppc440spe_desc_set_dest_addr(sw_desc,
  2784. chan, 0, addr, index);
  2785. break;
  2786. }
  2787. }
  2788. static void ppc440spe_adma_pq_zero_op(struct ppc440spe_adma_desc_slot *iter,
  2789. struct ppc440spe_adma_chan *chan, dma_addr_t addr)
  2790. {
  2791. /* To clear destinations update the descriptor
  2792. * (P or Q depending on index) as follows:
  2793. * addr is destination (0 corresponds to SG2):
  2794. */
  2795. ppc440spe_desc_set_dest_addr(iter, chan, DMA_CUED_XOR_BASE, addr, 0);
  2796. /* ... and the addr is source: */
  2797. ppc440spe_desc_set_src_addr(iter, chan, 0, DMA_CUED_XOR_HB, addr);
  2798. /* addr is always SG2 then the mult is always DST1 */
  2799. ppc440spe_desc_set_src_mult(iter, chan, DMA_CUED_MULT1_OFF,
  2800. DMA_CDB_SG_DST1, 1);
  2801. }
  2802. /**
  2803. * ppc440spe_adma_pq_set_dest - set destination address into descriptor
  2804. * for the PQXOR operation
  2805. */
  2806. static void ppc440spe_adma_pq_set_dest(struct ppc440spe_adma_desc_slot *sw_desc,
  2807. dma_addr_t *addrs, unsigned long flags)
  2808. {
  2809. struct ppc440spe_adma_desc_slot *iter;
  2810. struct ppc440spe_adma_chan *chan;
  2811. dma_addr_t paddr, qaddr;
  2812. dma_addr_t addr = 0, ppath, qpath;
  2813. int index = 0, i;
  2814. chan = to_ppc440spe_adma_chan(sw_desc->async_tx.chan);
  2815. if (flags & DMA_PREP_PQ_DISABLE_P)
  2816. paddr = 0;
  2817. else
  2818. paddr = addrs[0];
  2819. if (flags & DMA_PREP_PQ_DISABLE_Q)
  2820. qaddr = 0;
  2821. else
  2822. qaddr = addrs[1];
  2823. if (!paddr || !qaddr)
  2824. addr = paddr ? paddr : qaddr;
  2825. switch (chan->device->id) {
  2826. case PPC440SPE_DMA0_ID:
  2827. case PPC440SPE_DMA1_ID:
  2828. /* walk through the WXOR source list and set P/Q-destinations
  2829. * for each slot:
  2830. */
  2831. if (!test_bit(PPC440SPE_DESC_RXOR, &sw_desc->flags)) {
  2832. /* This is WXOR-only chain; may have 1/2 zero descs */
  2833. if (test_bit(PPC440SPE_ZERO_P, &sw_desc->flags))
  2834. index++;
  2835. if (test_bit(PPC440SPE_ZERO_Q, &sw_desc->flags))
  2836. index++;
  2837. iter = ppc440spe_get_group_entry(sw_desc, index);
  2838. if (addr) {
  2839. /* one destination */
  2840. list_for_each_entry_from(iter,
  2841. &sw_desc->group_list, chain_node)
  2842. ppc440spe_desc_set_dest_addr(iter, chan,
  2843. DMA_CUED_XOR_BASE, addr, 0);
  2844. } else {
  2845. /* two destinations */
  2846. list_for_each_entry_from(iter,
  2847. &sw_desc->group_list, chain_node) {
  2848. ppc440spe_desc_set_dest_addr(iter, chan,
  2849. DMA_CUED_XOR_BASE, paddr, 0);
  2850. ppc440spe_desc_set_dest_addr(iter, chan,
  2851. DMA_CUED_XOR_BASE, qaddr, 1);
  2852. }
  2853. }
  2854. if (index) {
  2855. /* To clear destinations update the descriptor
  2856. * (1st,2nd, or both depending on flags)
  2857. */
  2858. index = 0;
  2859. if (test_bit(PPC440SPE_ZERO_P,
  2860. &sw_desc->flags)) {
  2861. iter = ppc440spe_get_group_entry(
  2862. sw_desc, index++);
  2863. ppc440spe_adma_pq_zero_op(iter, chan,
  2864. paddr);
  2865. }
  2866. if (test_bit(PPC440SPE_ZERO_Q,
  2867. &sw_desc->flags)) {
  2868. iter = ppc440spe_get_group_entry(
  2869. sw_desc, index++);
  2870. ppc440spe_adma_pq_zero_op(iter, chan,
  2871. qaddr);
  2872. }
  2873. return;
  2874. }
  2875. } else {
  2876. /* This is RXOR-only or RXOR/WXOR mixed chain */
  2877. /* If we want to include destination into calculations,
  2878. * then make dest addresses cued with mult=1 (XOR).
  2879. */
  2880. ppath = test_bit(PPC440SPE_ZERO_P, &sw_desc->flags) ?
  2881. DMA_CUED_XOR_HB :
  2882. DMA_CUED_XOR_BASE |
  2883. (1 << DMA_CUED_MULT1_OFF);
  2884. qpath = test_bit(PPC440SPE_ZERO_Q, &sw_desc->flags) ?
  2885. DMA_CUED_XOR_HB :
  2886. DMA_CUED_XOR_BASE |
  2887. (1 << DMA_CUED_MULT1_OFF);
  2888. /* Setup destination(s) in RXOR slot(s) */
  2889. iter = ppc440spe_get_group_entry(sw_desc, index++);
  2890. ppc440spe_desc_set_dest_addr(iter, chan,
  2891. paddr ? ppath : qpath,
  2892. paddr ? paddr : qaddr, 0);
  2893. if (!addr) {
  2894. /* two destinations */
  2895. iter = ppc440spe_get_group_entry(sw_desc,
  2896. index++);
  2897. ppc440spe_desc_set_dest_addr(iter, chan,
  2898. qpath, qaddr, 0);
  2899. }
  2900. if (test_bit(PPC440SPE_DESC_WXOR, &sw_desc->flags)) {
  2901. /* Setup destination(s) in remaining WXOR
  2902. * slots
  2903. */
  2904. iter = ppc440spe_get_group_entry(sw_desc,
  2905. index);
  2906. if (addr) {
  2907. /* one destination */
  2908. list_for_each_entry_from(iter,
  2909. &sw_desc->group_list,
  2910. chain_node)
  2911. ppc440spe_desc_set_dest_addr(
  2912. iter, chan,
  2913. DMA_CUED_XOR_BASE,
  2914. addr, 0);
  2915. } else {
  2916. /* two destinations */
  2917. list_for_each_entry_from(iter,
  2918. &sw_desc->group_list,
  2919. chain_node) {
  2920. ppc440spe_desc_set_dest_addr(
  2921. iter, chan,
  2922. DMA_CUED_XOR_BASE,
  2923. paddr, 0);
  2924. ppc440spe_desc_set_dest_addr(
  2925. iter, chan,
  2926. DMA_CUED_XOR_BASE,
  2927. qaddr, 1);
  2928. }
  2929. }
  2930. }
  2931. }
  2932. break;
  2933. case PPC440SPE_XOR_ID:
  2934. /* DMA2 descriptors have only 1 destination, so there are
  2935. * two chains - one for each dest.
  2936. * If we want to include destination into calculations,
  2937. * then make dest addresses cued with mult=1 (XOR).
  2938. */
  2939. ppath = test_bit(PPC440SPE_ZERO_P, &sw_desc->flags) ?
  2940. DMA_CUED_XOR_HB :
  2941. DMA_CUED_XOR_BASE |
  2942. (1 << DMA_CUED_MULT1_OFF);
  2943. qpath = test_bit(PPC440SPE_ZERO_Q, &sw_desc->flags) ?
  2944. DMA_CUED_XOR_HB :
  2945. DMA_CUED_XOR_BASE |
  2946. (1 << DMA_CUED_MULT1_OFF);
  2947. iter = ppc440spe_get_group_entry(sw_desc, 0);
  2948. for (i = 0; i < sw_desc->descs_per_op; i++) {
  2949. ppc440spe_desc_set_dest_addr(iter, chan,
  2950. paddr ? ppath : qpath,
  2951. paddr ? paddr : qaddr, 0);
  2952. iter = list_entry(iter->chain_node.next,
  2953. struct ppc440spe_adma_desc_slot,
  2954. chain_node);
  2955. }
  2956. if (!addr) {
  2957. /* Two destinations; setup Q here */
  2958. iter = ppc440spe_get_group_entry(sw_desc,
  2959. sw_desc->descs_per_op);
  2960. for (i = 0; i < sw_desc->descs_per_op; i++) {
  2961. ppc440spe_desc_set_dest_addr(iter,
  2962. chan, qpath, qaddr, 0);
  2963. iter = list_entry(iter->chain_node.next,
  2964. struct ppc440spe_adma_desc_slot,
  2965. chain_node);
  2966. }
  2967. }
  2968. break;
  2969. }
  2970. }
  2971. /**
  2972. * ppc440spe_adma_pq_zero_sum_set_dest - set destination address into descriptor
  2973. * for the PQ_ZERO_SUM operation
  2974. */
  2975. static void ppc440spe_adma_pqzero_sum_set_dest(
  2976. struct ppc440spe_adma_desc_slot *sw_desc,
  2977. dma_addr_t paddr, dma_addr_t qaddr)
  2978. {
  2979. struct ppc440spe_adma_desc_slot *iter, *end;
  2980. struct ppc440spe_adma_chan *chan;
  2981. dma_addr_t addr = 0;
  2982. int idx;
  2983. chan = to_ppc440spe_adma_chan(sw_desc->async_tx.chan);
  2984. /* walk through the WXOR source list and set P/Q-destinations
  2985. * for each slot
  2986. */
  2987. idx = (paddr && qaddr) ? 2 : 1;
  2988. /* set end */
  2989. list_for_each_entry_reverse(end, &sw_desc->group_list,
  2990. chain_node) {
  2991. if (!(--idx))
  2992. break;
  2993. }
  2994. /* set start */
  2995. idx = (paddr && qaddr) ? 2 : 1;
  2996. iter = ppc440spe_get_group_entry(sw_desc, idx);
  2997. if (paddr && qaddr) {
  2998. /* two destinations */
  2999. list_for_each_entry_from(iter, &sw_desc->group_list,
  3000. chain_node) {
  3001. if (unlikely(iter == end))
  3002. break;
  3003. ppc440spe_desc_set_dest_addr(iter, chan,
  3004. DMA_CUED_XOR_BASE, paddr, 0);
  3005. ppc440spe_desc_set_dest_addr(iter, chan,
  3006. DMA_CUED_XOR_BASE, qaddr, 1);
  3007. }
  3008. } else {
  3009. /* one destination */
  3010. addr = paddr ? paddr : qaddr;
  3011. list_for_each_entry_from(iter, &sw_desc->group_list,
  3012. chain_node) {
  3013. if (unlikely(iter == end))
  3014. break;
  3015. ppc440spe_desc_set_dest_addr(iter, chan,
  3016. DMA_CUED_XOR_BASE, addr, 0);
  3017. }
  3018. }
  3019. /* The remaining descriptors are DATACHECK. These have no need in
  3020. * destination. Actually, these destinations are used there
  3021. * as sources for check operation. So, set addr as source.
  3022. */
  3023. ppc440spe_desc_set_src_addr(end, chan, 0, 0, addr ? addr : paddr);
  3024. if (!addr) {
  3025. end = list_entry(end->chain_node.next,
  3026. struct ppc440spe_adma_desc_slot, chain_node);
  3027. ppc440spe_desc_set_src_addr(end, chan, 0, 0, qaddr);
  3028. }
  3029. }
  3030. /**
  3031. * ppc440spe_desc_set_xor_src_cnt - set source count into descriptor
  3032. */
  3033. static inline void ppc440spe_desc_set_xor_src_cnt(
  3034. struct ppc440spe_adma_desc_slot *desc,
  3035. int src_cnt)
  3036. {
  3037. struct xor_cb *hw_desc = desc->hw_desc;
  3038. hw_desc->cbc &= ~XOR_CDCR_OAC_MSK;
  3039. hw_desc->cbc |= src_cnt;
  3040. }
  3041. /**
  3042. * ppc440spe_adma_pq_set_src - set source address into descriptor
  3043. */
  3044. static void ppc440spe_adma_pq_set_src(struct ppc440spe_adma_desc_slot *sw_desc,
  3045. dma_addr_t addr, int index)
  3046. {
  3047. struct ppc440spe_adma_chan *chan;
  3048. dma_addr_t haddr = 0;
  3049. struct ppc440spe_adma_desc_slot *iter = NULL;
  3050. chan = to_ppc440spe_adma_chan(sw_desc->async_tx.chan);
  3051. switch (chan->device->id) {
  3052. case PPC440SPE_DMA0_ID:
  3053. case PPC440SPE_DMA1_ID:
  3054. /* DMA0,1 may do: WXOR, RXOR, RXOR+WXORs chain
  3055. */
  3056. if (test_bit(PPC440SPE_DESC_RXOR, &sw_desc->flags)) {
  3057. /* RXOR-only or RXOR/WXOR operation */
  3058. int iskip = test_bit(PPC440SPE_DESC_RXOR12,
  3059. &sw_desc->flags) ? 2 : 3;
  3060. if (index == 0) {
  3061. /* 1st slot (RXOR) */
  3062. /* setup sources region (R1-2-3, R1-2-4,
  3063. * or R1-2-5)
  3064. */
  3065. if (test_bit(PPC440SPE_DESC_RXOR12,
  3066. &sw_desc->flags))
  3067. haddr = DMA_RXOR12 <<
  3068. DMA_CUED_REGION_OFF;
  3069. else if (test_bit(PPC440SPE_DESC_RXOR123,
  3070. &sw_desc->flags))
  3071. haddr = DMA_RXOR123 <<
  3072. DMA_CUED_REGION_OFF;
  3073. else if (test_bit(PPC440SPE_DESC_RXOR124,
  3074. &sw_desc->flags))
  3075. haddr = DMA_RXOR124 <<
  3076. DMA_CUED_REGION_OFF;
  3077. else if (test_bit(PPC440SPE_DESC_RXOR125,
  3078. &sw_desc->flags))
  3079. haddr = DMA_RXOR125 <<
  3080. DMA_CUED_REGION_OFF;
  3081. else
  3082. BUG();
  3083. haddr |= DMA_CUED_XOR_BASE;
  3084. iter = ppc440spe_get_group_entry(sw_desc, 0);
  3085. } else if (index < iskip) {
  3086. /* 1st slot (RXOR)
  3087. * shall actually set source address only once
  3088. * instead of first <iskip>
  3089. */
  3090. iter = NULL;
  3091. } else {
  3092. /* 2nd/3d and next slots (WXOR);
  3093. * skip first slot with RXOR
  3094. */
  3095. haddr = DMA_CUED_XOR_HB;
  3096. iter = ppc440spe_get_group_entry(sw_desc,
  3097. index - iskip + sw_desc->dst_cnt);
  3098. }
  3099. } else {
  3100. int znum = 0;
  3101. /* WXOR-only operation; skip first slots with
  3102. * zeroing destinations
  3103. */
  3104. if (test_bit(PPC440SPE_ZERO_P, &sw_desc->flags))
  3105. znum++;
  3106. if (test_bit(PPC440SPE_ZERO_Q, &sw_desc->flags))
  3107. znum++;
  3108. haddr = DMA_CUED_XOR_HB;
  3109. iter = ppc440spe_get_group_entry(sw_desc,
  3110. index + znum);
  3111. }
  3112. if (likely(iter)) {
  3113. ppc440spe_desc_set_src_addr(iter, chan, 0, haddr, addr);
  3114. if (!index &&
  3115. test_bit(PPC440SPE_DESC_RXOR, &sw_desc->flags) &&
  3116. sw_desc->dst_cnt == 2) {
  3117. /* if we have two destinations for RXOR, then
  3118. * setup source in the second descr too
  3119. */
  3120. iter = ppc440spe_get_group_entry(sw_desc, 1);
  3121. ppc440spe_desc_set_src_addr(iter, chan, 0,
  3122. haddr, addr);
  3123. }
  3124. }
  3125. break;
  3126. case PPC440SPE_XOR_ID:
  3127. /* DMA2 may do Biskup */
  3128. iter = sw_desc->group_head;
  3129. if (iter->dst_cnt == 2) {
  3130. /* both P & Q calculations required; set P src here */
  3131. ppc440spe_adma_dma2rxor_set_src(iter, index, addr);
  3132. /* this is for Q */
  3133. iter = ppc440spe_get_group_entry(sw_desc,
  3134. sw_desc->descs_per_op);
  3135. }
  3136. ppc440spe_adma_dma2rxor_set_src(iter, index, addr);
  3137. break;
  3138. }
  3139. }
  3140. /**
  3141. * ppc440spe_adma_memcpy_xor_set_src - set source address into descriptor
  3142. */
  3143. static void ppc440spe_adma_memcpy_xor_set_src(
  3144. struct ppc440spe_adma_desc_slot *sw_desc,
  3145. dma_addr_t addr, int index)
  3146. {
  3147. struct ppc440spe_adma_chan *chan;
  3148. chan = to_ppc440spe_adma_chan(sw_desc->async_tx.chan);
  3149. sw_desc = sw_desc->group_head;
  3150. if (likely(sw_desc))
  3151. ppc440spe_desc_set_src_addr(sw_desc, chan, index, 0, addr);
  3152. }
  3153. /**
  3154. * ppc440spe_adma_dma2rxor_inc_addr -
  3155. */
  3156. static void ppc440spe_adma_dma2rxor_inc_addr(
  3157. struct ppc440spe_adma_desc_slot *desc,
  3158. struct ppc440spe_rxor *cursor, int index, int src_cnt)
  3159. {
  3160. cursor->addr_count++;
  3161. if (index == src_cnt - 1) {
  3162. ppc440spe_desc_set_xor_src_cnt(desc, cursor->addr_count);
  3163. } else if (cursor->addr_count == XOR_MAX_OPS) {
  3164. ppc440spe_desc_set_xor_src_cnt(desc, cursor->addr_count);
  3165. cursor->addr_count = 0;
  3166. cursor->desc_count++;
  3167. }
  3168. }
  3169. /**
  3170. * ppc440spe_adma_dma2rxor_prep_src - setup RXOR types in DMA2 CDB
  3171. */
  3172. static int ppc440spe_adma_dma2rxor_prep_src(
  3173. struct ppc440spe_adma_desc_slot *hdesc,
  3174. struct ppc440spe_rxor *cursor, int index,
  3175. int src_cnt, u32 addr)
  3176. {
  3177. int rval = 0;
  3178. u32 sign;
  3179. struct ppc440spe_adma_desc_slot *desc = hdesc;
  3180. int i;
  3181. for (i = 0; i < cursor->desc_count; i++) {
  3182. desc = list_entry(hdesc->chain_node.next,
  3183. struct ppc440spe_adma_desc_slot,
  3184. chain_node);
  3185. }
  3186. switch (cursor->state) {
  3187. case 0:
  3188. if (addr == cursor->addrl + cursor->len) {
  3189. /* direct RXOR */
  3190. cursor->state = 1;
  3191. cursor->xor_count++;
  3192. if (index == src_cnt-1) {
  3193. ppc440spe_rxor_set_region(desc,
  3194. cursor->addr_count,
  3195. DMA_RXOR12 << DMA_CUED_REGION_OFF);
  3196. ppc440spe_adma_dma2rxor_inc_addr(
  3197. desc, cursor, index, src_cnt);
  3198. }
  3199. } else if (cursor->addrl == addr + cursor->len) {
  3200. /* reverse RXOR */
  3201. cursor->state = 1;
  3202. cursor->xor_count++;
  3203. set_bit(cursor->addr_count, &desc->reverse_flags[0]);
  3204. if (index == src_cnt-1) {
  3205. ppc440spe_rxor_set_region(desc,
  3206. cursor->addr_count,
  3207. DMA_RXOR12 << DMA_CUED_REGION_OFF);
  3208. ppc440spe_adma_dma2rxor_inc_addr(
  3209. desc, cursor, index, src_cnt);
  3210. }
  3211. } else {
  3212. printk(KERN_ERR "Cannot build "
  3213. "DMA2 RXOR command block.\n");
  3214. BUG();
  3215. }
  3216. break;
  3217. case 1:
  3218. sign = test_bit(cursor->addr_count,
  3219. desc->reverse_flags)
  3220. ? -1 : 1;
  3221. if (index == src_cnt-2 || (sign == -1
  3222. && addr != cursor->addrl - 2*cursor->len)) {
  3223. cursor->state = 0;
  3224. cursor->xor_count = 1;
  3225. cursor->addrl = addr;
  3226. ppc440spe_rxor_set_region(desc,
  3227. cursor->addr_count,
  3228. DMA_RXOR12 << DMA_CUED_REGION_OFF);
  3229. ppc440spe_adma_dma2rxor_inc_addr(
  3230. desc, cursor, index, src_cnt);
  3231. } else if (addr == cursor->addrl + 2*sign*cursor->len) {
  3232. cursor->state = 2;
  3233. cursor->xor_count = 0;
  3234. ppc440spe_rxor_set_region(desc,
  3235. cursor->addr_count,
  3236. DMA_RXOR123 << DMA_CUED_REGION_OFF);
  3237. if (index == src_cnt-1) {
  3238. ppc440spe_adma_dma2rxor_inc_addr(
  3239. desc, cursor, index, src_cnt);
  3240. }
  3241. } else if (addr == cursor->addrl + 3*cursor->len) {
  3242. cursor->state = 2;
  3243. cursor->xor_count = 0;
  3244. ppc440spe_rxor_set_region(desc,
  3245. cursor->addr_count,
  3246. DMA_RXOR124 << DMA_CUED_REGION_OFF);
  3247. if (index == src_cnt-1) {
  3248. ppc440spe_adma_dma2rxor_inc_addr(
  3249. desc, cursor, index, src_cnt);
  3250. }
  3251. } else if (addr == cursor->addrl + 4*cursor->len) {
  3252. cursor->state = 2;
  3253. cursor->xor_count = 0;
  3254. ppc440spe_rxor_set_region(desc,
  3255. cursor->addr_count,
  3256. DMA_RXOR125 << DMA_CUED_REGION_OFF);
  3257. if (index == src_cnt-1) {
  3258. ppc440spe_adma_dma2rxor_inc_addr(
  3259. desc, cursor, index, src_cnt);
  3260. }
  3261. } else {
  3262. cursor->state = 0;
  3263. cursor->xor_count = 1;
  3264. cursor->addrl = addr;
  3265. ppc440spe_rxor_set_region(desc,
  3266. cursor->addr_count,
  3267. DMA_RXOR12 << DMA_CUED_REGION_OFF);
  3268. ppc440spe_adma_dma2rxor_inc_addr(
  3269. desc, cursor, index, src_cnt);
  3270. }
  3271. break;
  3272. case 2:
  3273. cursor->state = 0;
  3274. cursor->addrl = addr;
  3275. cursor->xor_count++;
  3276. if (index) {
  3277. ppc440spe_adma_dma2rxor_inc_addr(
  3278. desc, cursor, index, src_cnt);
  3279. }
  3280. break;
  3281. }
  3282. return rval;
  3283. }
  3284. /**
  3285. * ppc440spe_adma_dma2rxor_set_src - set RXOR source address; it's assumed that
  3286. * ppc440spe_adma_dma2rxor_prep_src() has already done prior this call
  3287. */
  3288. static void ppc440spe_adma_dma2rxor_set_src(
  3289. struct ppc440spe_adma_desc_slot *desc,
  3290. int index, dma_addr_t addr)
  3291. {
  3292. struct xor_cb *xcb = desc->hw_desc;
  3293. int k = 0, op = 0, lop = 0;
  3294. /* get the RXOR operand which corresponds to index addr */
  3295. while (op <= index) {
  3296. lop = op;
  3297. if (k == XOR_MAX_OPS) {
  3298. k = 0;
  3299. desc = list_entry(desc->chain_node.next,
  3300. struct ppc440spe_adma_desc_slot, chain_node);
  3301. xcb = desc->hw_desc;
  3302. }
  3303. if ((xcb->ops[k++].h & (DMA_RXOR12 << DMA_CUED_REGION_OFF)) ==
  3304. (DMA_RXOR12 << DMA_CUED_REGION_OFF))
  3305. op += 2;
  3306. else
  3307. op += 3;
  3308. }
  3309. BUG_ON(k < 1);
  3310. if (test_bit(k-1, desc->reverse_flags)) {
  3311. /* reverse operand order; put last op in RXOR group */
  3312. if (index == op - 1)
  3313. ppc440spe_rxor_set_src(desc, k - 1, addr);
  3314. } else {
  3315. /* direct operand order; put first op in RXOR group */
  3316. if (index == lop)
  3317. ppc440spe_rxor_set_src(desc, k - 1, addr);
  3318. }
  3319. }
  3320. /**
  3321. * ppc440spe_adma_dma2rxor_set_mult - set RXOR multipliers; it's assumed that
  3322. * ppc440spe_adma_dma2rxor_prep_src() has already done prior this call
  3323. */
  3324. static void ppc440spe_adma_dma2rxor_set_mult(
  3325. struct ppc440spe_adma_desc_slot *desc,
  3326. int index, u8 mult)
  3327. {
  3328. struct xor_cb *xcb = desc->hw_desc;
  3329. int k = 0, op = 0, lop = 0;
  3330. /* get the RXOR operand which corresponds to index mult */
  3331. while (op <= index) {
  3332. lop = op;
  3333. if (k == XOR_MAX_OPS) {
  3334. k = 0;
  3335. desc = list_entry(desc->chain_node.next,
  3336. struct ppc440spe_adma_desc_slot,
  3337. chain_node);
  3338. xcb = desc->hw_desc;
  3339. }
  3340. if ((xcb->ops[k++].h & (DMA_RXOR12 << DMA_CUED_REGION_OFF)) ==
  3341. (DMA_RXOR12 << DMA_CUED_REGION_OFF))
  3342. op += 2;
  3343. else
  3344. op += 3;
  3345. }
  3346. BUG_ON(k < 1);
  3347. if (test_bit(k-1, desc->reverse_flags)) {
  3348. /* reverse order */
  3349. ppc440spe_rxor_set_mult(desc, k - 1, op - index - 1, mult);
  3350. } else {
  3351. /* direct order */
  3352. ppc440spe_rxor_set_mult(desc, k - 1, index - lop, mult);
  3353. }
  3354. }
  3355. /**
  3356. * ppc440spe_init_rxor_cursor -
  3357. */
  3358. static void ppc440spe_init_rxor_cursor(struct ppc440spe_rxor *cursor)
  3359. {
  3360. memset(cursor, 0, sizeof(struct ppc440spe_rxor));
  3361. cursor->state = 2;
  3362. }
  3363. /**
  3364. * ppc440spe_adma_pq_set_src_mult - set multiplication coefficient into
  3365. * descriptor for the PQXOR operation
  3366. */
  3367. static void ppc440spe_adma_pq_set_src_mult(
  3368. struct ppc440spe_adma_desc_slot *sw_desc,
  3369. unsigned char mult, int index, int dst_pos)
  3370. {
  3371. struct ppc440spe_adma_chan *chan;
  3372. u32 mult_idx, mult_dst;
  3373. struct ppc440spe_adma_desc_slot *iter = NULL, *iter1 = NULL;
  3374. chan = to_ppc440spe_adma_chan(sw_desc->async_tx.chan);
  3375. switch (chan->device->id) {
  3376. case PPC440SPE_DMA0_ID:
  3377. case PPC440SPE_DMA1_ID:
  3378. if (test_bit(PPC440SPE_DESC_RXOR, &sw_desc->flags)) {
  3379. int region = test_bit(PPC440SPE_DESC_RXOR12,
  3380. &sw_desc->flags) ? 2 : 3;
  3381. if (index < region) {
  3382. /* RXOR multipliers */
  3383. iter = ppc440spe_get_group_entry(sw_desc,
  3384. sw_desc->dst_cnt - 1);
  3385. if (sw_desc->dst_cnt == 2)
  3386. iter1 = ppc440spe_get_group_entry(
  3387. sw_desc, 0);
  3388. mult_idx = DMA_CUED_MULT1_OFF + (index << 3);
  3389. mult_dst = DMA_CDB_SG_SRC;
  3390. } else {
  3391. /* WXOR multiplier */
  3392. iter = ppc440spe_get_group_entry(sw_desc,
  3393. index - region +
  3394. sw_desc->dst_cnt);
  3395. mult_idx = DMA_CUED_MULT1_OFF;
  3396. mult_dst = dst_pos ? DMA_CDB_SG_DST2 :
  3397. DMA_CDB_SG_DST1;
  3398. }
  3399. } else {
  3400. int znum = 0;
  3401. /* WXOR-only;
  3402. * skip first slots with destinations (if ZERO_DST has
  3403. * place)
  3404. */
  3405. if (test_bit(PPC440SPE_ZERO_P, &sw_desc->flags))
  3406. znum++;
  3407. if (test_bit(PPC440SPE_ZERO_Q, &sw_desc->flags))
  3408. znum++;
  3409. iter = ppc440spe_get_group_entry(sw_desc, index + znum);
  3410. mult_idx = DMA_CUED_MULT1_OFF;
  3411. mult_dst = dst_pos ? DMA_CDB_SG_DST2 : DMA_CDB_SG_DST1;
  3412. }
  3413. if (likely(iter)) {
  3414. ppc440spe_desc_set_src_mult(iter, chan,
  3415. mult_idx, mult_dst, mult);
  3416. if (unlikely(iter1)) {
  3417. /* if we have two destinations for RXOR, then
  3418. * we've just set Q mult. Set-up P now.
  3419. */
  3420. ppc440spe_desc_set_src_mult(iter1, chan,
  3421. mult_idx, mult_dst, 1);
  3422. }
  3423. }
  3424. break;
  3425. case PPC440SPE_XOR_ID:
  3426. iter = sw_desc->group_head;
  3427. if (sw_desc->dst_cnt == 2) {
  3428. /* both P & Q calculations required; set P mult here */
  3429. ppc440spe_adma_dma2rxor_set_mult(iter, index, 1);
  3430. /* and then set Q mult */
  3431. iter = ppc440spe_get_group_entry(sw_desc,
  3432. sw_desc->descs_per_op);
  3433. }
  3434. ppc440spe_adma_dma2rxor_set_mult(iter, index, mult);
  3435. break;
  3436. }
  3437. }
  3438. /**
  3439. * ppc440spe_adma_free_chan_resources - free the resources allocated
  3440. */
  3441. static void ppc440spe_adma_free_chan_resources(struct dma_chan *chan)
  3442. {
  3443. struct ppc440spe_adma_chan *ppc440spe_chan;
  3444. struct ppc440spe_adma_desc_slot *iter, *_iter;
  3445. int in_use_descs = 0;
  3446. ppc440spe_chan = to_ppc440spe_adma_chan(chan);
  3447. ppc440spe_adma_slot_cleanup(ppc440spe_chan);
  3448. spin_lock_bh(&ppc440spe_chan->lock);
  3449. list_for_each_entry_safe(iter, _iter, &ppc440spe_chan->chain,
  3450. chain_node) {
  3451. in_use_descs++;
  3452. list_del(&iter->chain_node);
  3453. }
  3454. list_for_each_entry_safe_reverse(iter, _iter,
  3455. &ppc440spe_chan->all_slots, slot_node) {
  3456. list_del(&iter->slot_node);
  3457. kfree(iter);
  3458. ppc440spe_chan->slots_allocated--;
  3459. }
  3460. ppc440spe_chan->last_used = NULL;
  3461. dev_dbg(ppc440spe_chan->device->common.dev,
  3462. "ppc440spe adma%d %s slots_allocated %d\n",
  3463. ppc440spe_chan->device->id,
  3464. __func__, ppc440spe_chan->slots_allocated);
  3465. spin_unlock_bh(&ppc440spe_chan->lock);
  3466. /* one is ok since we left it on there on purpose */
  3467. if (in_use_descs > 1)
  3468. printk(KERN_ERR "SPE: Freeing %d in use descriptors!\n",
  3469. in_use_descs - 1);
  3470. }
  3471. /**
  3472. * ppc440spe_adma_tx_status - poll the status of an ADMA transaction
  3473. * @chan: ADMA channel handle
  3474. * @cookie: ADMA transaction identifier
  3475. * @txstate: a holder for the current state of the channel
  3476. */
  3477. static enum dma_status ppc440spe_adma_tx_status(struct dma_chan *chan,
  3478. dma_cookie_t cookie, struct dma_tx_state *txstate)
  3479. {
  3480. struct ppc440spe_adma_chan *ppc440spe_chan;
  3481. enum dma_status ret;
  3482. ppc440spe_chan = to_ppc440spe_adma_chan(chan);
  3483. ret = dma_cookie_status(chan, cookie, txstate);
  3484. if (ret == DMA_SUCCESS)
  3485. return ret;
  3486. ppc440spe_adma_slot_cleanup(ppc440spe_chan);
  3487. return dma_cookie_status(chan, cookie, txstate);
  3488. }
  3489. /**
  3490. * ppc440spe_adma_eot_handler - end of transfer interrupt handler
  3491. */
  3492. static irqreturn_t ppc440spe_adma_eot_handler(int irq, void *data)
  3493. {
  3494. struct ppc440spe_adma_chan *chan = data;
  3495. dev_dbg(chan->device->common.dev,
  3496. "ppc440spe adma%d: %s\n", chan->device->id, __func__);
  3497. tasklet_schedule(&chan->irq_tasklet);
  3498. ppc440spe_adma_device_clear_eot_status(chan);
  3499. return IRQ_HANDLED;
  3500. }
  3501. /**
  3502. * ppc440spe_adma_err_handler - DMA error interrupt handler;
  3503. * do the same things as a eot handler
  3504. */
  3505. static irqreturn_t ppc440spe_adma_err_handler(int irq, void *data)
  3506. {
  3507. struct ppc440spe_adma_chan *chan = data;
  3508. dev_dbg(chan->device->common.dev,
  3509. "ppc440spe adma%d: %s\n", chan->device->id, __func__);
  3510. tasklet_schedule(&chan->irq_tasklet);
  3511. ppc440spe_adma_device_clear_eot_status(chan);
  3512. return IRQ_HANDLED;
  3513. }
  3514. /**
  3515. * ppc440spe_test_callback - called when test operation has been done
  3516. */
  3517. static void ppc440spe_test_callback(void *unused)
  3518. {
  3519. complete(&ppc440spe_r6_test_comp);
  3520. }
  3521. /**
  3522. * ppc440spe_adma_issue_pending - flush all pending descriptors to h/w
  3523. */
  3524. static void ppc440spe_adma_issue_pending(struct dma_chan *chan)
  3525. {
  3526. struct ppc440spe_adma_chan *ppc440spe_chan;
  3527. ppc440spe_chan = to_ppc440spe_adma_chan(chan);
  3528. dev_dbg(ppc440spe_chan->device->common.dev,
  3529. "ppc440spe adma%d: %s %d \n", ppc440spe_chan->device->id,
  3530. __func__, ppc440spe_chan->pending);
  3531. if (ppc440spe_chan->pending) {
  3532. ppc440spe_chan->pending = 0;
  3533. ppc440spe_chan_append(ppc440spe_chan);
  3534. }
  3535. }
  3536. /**
  3537. * ppc440spe_chan_start_null_xor - initiate the first XOR operation (DMA engines
  3538. * use FIFOs (as opposite to chains used in XOR) so this is a XOR
  3539. * specific operation)
  3540. */
  3541. static void ppc440spe_chan_start_null_xor(struct ppc440spe_adma_chan *chan)
  3542. {
  3543. struct ppc440spe_adma_desc_slot *sw_desc, *group_start;
  3544. dma_cookie_t cookie;
  3545. int slot_cnt, slots_per_op;
  3546. dev_dbg(chan->device->common.dev,
  3547. "ppc440spe adma%d: %s\n", chan->device->id, __func__);
  3548. spin_lock_bh(&chan->lock);
  3549. slot_cnt = ppc440spe_chan_xor_slot_count(0, 2, &slots_per_op);
  3550. sw_desc = ppc440spe_adma_alloc_slots(chan, slot_cnt, slots_per_op);
  3551. if (sw_desc) {
  3552. group_start = sw_desc->group_head;
  3553. list_splice_init(&sw_desc->group_list, &chan->chain);
  3554. async_tx_ack(&sw_desc->async_tx);
  3555. ppc440spe_desc_init_null_xor(group_start);
  3556. cookie = dma_cookie_assign(&sw_desc->async_tx);
  3557. /* initialize the completed cookie to be less than
  3558. * the most recently used cookie
  3559. */
  3560. chan->common.completed_cookie = cookie - 1;
  3561. /* channel should not be busy */
  3562. BUG_ON(ppc440spe_chan_is_busy(chan));
  3563. /* set the descriptor address */
  3564. ppc440spe_chan_set_first_xor_descriptor(chan, sw_desc);
  3565. /* run the descriptor */
  3566. ppc440spe_chan_run(chan);
  3567. } else
  3568. printk(KERN_ERR "ppc440spe adma%d"
  3569. " failed to allocate null descriptor\n",
  3570. chan->device->id);
  3571. spin_unlock_bh(&chan->lock);
  3572. }
  3573. /**
  3574. * ppc440spe_test_raid6 - test are RAID-6 capabilities enabled successfully.
  3575. * For this we just perform one WXOR operation with the same source
  3576. * and destination addresses, the GF-multiplier is 1; so if RAID-6
  3577. * capabilities are enabled then we'll get src/dst filled with zero.
  3578. */
  3579. static int ppc440spe_test_raid6(struct ppc440spe_adma_chan *chan)
  3580. {
  3581. struct ppc440spe_adma_desc_slot *sw_desc, *iter;
  3582. struct page *pg;
  3583. char *a;
  3584. dma_addr_t dma_addr, addrs[2];
  3585. unsigned long op = 0;
  3586. int rval = 0;
  3587. set_bit(PPC440SPE_DESC_WXOR, &op);
  3588. pg = alloc_page(GFP_KERNEL);
  3589. if (!pg)
  3590. return -ENOMEM;
  3591. spin_lock_bh(&chan->lock);
  3592. sw_desc = ppc440spe_adma_alloc_slots(chan, 1, 1);
  3593. if (sw_desc) {
  3594. /* 1 src, 1 dsr, int_ena, WXOR */
  3595. ppc440spe_desc_init_dma01pq(sw_desc, 1, 1, 1, op);
  3596. list_for_each_entry(iter, &sw_desc->group_list, chain_node) {
  3597. ppc440spe_desc_set_byte_count(iter, chan, PAGE_SIZE);
  3598. iter->unmap_len = PAGE_SIZE;
  3599. }
  3600. } else {
  3601. rval = -EFAULT;
  3602. spin_unlock_bh(&chan->lock);
  3603. goto exit;
  3604. }
  3605. spin_unlock_bh(&chan->lock);
  3606. /* Fill the test page with ones */
  3607. memset(page_address(pg), 0xFF, PAGE_SIZE);
  3608. dma_addr = dma_map_page(chan->device->dev, pg, 0,
  3609. PAGE_SIZE, DMA_BIDIRECTIONAL);
  3610. /* Setup addresses */
  3611. ppc440spe_adma_pq_set_src(sw_desc, dma_addr, 0);
  3612. ppc440spe_adma_pq_set_src_mult(sw_desc, 1, 0, 0);
  3613. addrs[0] = dma_addr;
  3614. addrs[1] = 0;
  3615. ppc440spe_adma_pq_set_dest(sw_desc, addrs, DMA_PREP_PQ_DISABLE_Q);
  3616. async_tx_ack(&sw_desc->async_tx);
  3617. sw_desc->async_tx.callback = ppc440spe_test_callback;
  3618. sw_desc->async_tx.callback_param = NULL;
  3619. init_completion(&ppc440spe_r6_test_comp);
  3620. ppc440spe_adma_tx_submit(&sw_desc->async_tx);
  3621. ppc440spe_adma_issue_pending(&chan->common);
  3622. wait_for_completion(&ppc440spe_r6_test_comp);
  3623. /* Now check if the test page is zeroed */
  3624. a = page_address(pg);
  3625. if ((*(u32 *)a) == 0 && memcmp(a, a+4, PAGE_SIZE-4) == 0) {
  3626. /* page is zero - RAID-6 enabled */
  3627. rval = 0;
  3628. } else {
  3629. /* RAID-6 was not enabled */
  3630. rval = -EINVAL;
  3631. }
  3632. exit:
  3633. __free_page(pg);
  3634. return rval;
  3635. }
  3636. static void ppc440spe_adma_init_capabilities(struct ppc440spe_adma_device *adev)
  3637. {
  3638. switch (adev->id) {
  3639. case PPC440SPE_DMA0_ID:
  3640. case PPC440SPE_DMA1_ID:
  3641. dma_cap_set(DMA_MEMCPY, adev->common.cap_mask);
  3642. dma_cap_set(DMA_INTERRUPT, adev->common.cap_mask);
  3643. dma_cap_set(DMA_MEMSET, adev->common.cap_mask);
  3644. dma_cap_set(DMA_PQ, adev->common.cap_mask);
  3645. dma_cap_set(DMA_PQ_VAL, adev->common.cap_mask);
  3646. dma_cap_set(DMA_XOR_VAL, adev->common.cap_mask);
  3647. break;
  3648. case PPC440SPE_XOR_ID:
  3649. dma_cap_set(DMA_XOR, adev->common.cap_mask);
  3650. dma_cap_set(DMA_PQ, adev->common.cap_mask);
  3651. dma_cap_set(DMA_INTERRUPT, adev->common.cap_mask);
  3652. adev->common.cap_mask = adev->common.cap_mask;
  3653. break;
  3654. }
  3655. /* Set base routines */
  3656. adev->common.device_alloc_chan_resources =
  3657. ppc440spe_adma_alloc_chan_resources;
  3658. adev->common.device_free_chan_resources =
  3659. ppc440spe_adma_free_chan_resources;
  3660. adev->common.device_tx_status = ppc440spe_adma_tx_status;
  3661. adev->common.device_issue_pending = ppc440spe_adma_issue_pending;
  3662. /* Set prep routines based on capability */
  3663. if (dma_has_cap(DMA_MEMCPY, adev->common.cap_mask)) {
  3664. adev->common.device_prep_dma_memcpy =
  3665. ppc440spe_adma_prep_dma_memcpy;
  3666. }
  3667. if (dma_has_cap(DMA_MEMSET, adev->common.cap_mask)) {
  3668. adev->common.device_prep_dma_memset =
  3669. ppc440spe_adma_prep_dma_memset;
  3670. }
  3671. if (dma_has_cap(DMA_XOR, adev->common.cap_mask)) {
  3672. adev->common.max_xor = XOR_MAX_OPS;
  3673. adev->common.device_prep_dma_xor =
  3674. ppc440spe_adma_prep_dma_xor;
  3675. }
  3676. if (dma_has_cap(DMA_PQ, adev->common.cap_mask)) {
  3677. switch (adev->id) {
  3678. case PPC440SPE_DMA0_ID:
  3679. dma_set_maxpq(&adev->common,
  3680. DMA0_FIFO_SIZE / sizeof(struct dma_cdb), 0);
  3681. break;
  3682. case PPC440SPE_DMA1_ID:
  3683. dma_set_maxpq(&adev->common,
  3684. DMA1_FIFO_SIZE / sizeof(struct dma_cdb), 0);
  3685. break;
  3686. case PPC440SPE_XOR_ID:
  3687. adev->common.max_pq = XOR_MAX_OPS * 3;
  3688. break;
  3689. }
  3690. adev->common.device_prep_dma_pq =
  3691. ppc440spe_adma_prep_dma_pq;
  3692. }
  3693. if (dma_has_cap(DMA_PQ_VAL, adev->common.cap_mask)) {
  3694. switch (adev->id) {
  3695. case PPC440SPE_DMA0_ID:
  3696. adev->common.max_pq = DMA0_FIFO_SIZE /
  3697. sizeof(struct dma_cdb);
  3698. break;
  3699. case PPC440SPE_DMA1_ID:
  3700. adev->common.max_pq = DMA1_FIFO_SIZE /
  3701. sizeof(struct dma_cdb);
  3702. break;
  3703. }
  3704. adev->common.device_prep_dma_pq_val =
  3705. ppc440spe_adma_prep_dma_pqzero_sum;
  3706. }
  3707. if (dma_has_cap(DMA_XOR_VAL, adev->common.cap_mask)) {
  3708. switch (adev->id) {
  3709. case PPC440SPE_DMA0_ID:
  3710. adev->common.max_xor = DMA0_FIFO_SIZE /
  3711. sizeof(struct dma_cdb);
  3712. break;
  3713. case PPC440SPE_DMA1_ID:
  3714. adev->common.max_xor = DMA1_FIFO_SIZE /
  3715. sizeof(struct dma_cdb);
  3716. break;
  3717. }
  3718. adev->common.device_prep_dma_xor_val =
  3719. ppc440spe_adma_prep_dma_xor_zero_sum;
  3720. }
  3721. if (dma_has_cap(DMA_INTERRUPT, adev->common.cap_mask)) {
  3722. adev->common.device_prep_dma_interrupt =
  3723. ppc440spe_adma_prep_dma_interrupt;
  3724. }
  3725. pr_info("%s: AMCC(R) PPC440SP(E) ADMA Engine: "
  3726. "( %s%s%s%s%s%s%s)\n",
  3727. dev_name(adev->dev),
  3728. dma_has_cap(DMA_PQ, adev->common.cap_mask) ? "pq " : "",
  3729. dma_has_cap(DMA_PQ_VAL, adev->common.cap_mask) ? "pq_val " : "",
  3730. dma_has_cap(DMA_XOR, adev->common.cap_mask) ? "xor " : "",
  3731. dma_has_cap(DMA_XOR_VAL, adev->common.cap_mask) ? "xor_val " : "",
  3732. dma_has_cap(DMA_MEMCPY, adev->common.cap_mask) ? "memcpy " : "",
  3733. dma_has_cap(DMA_MEMSET, adev->common.cap_mask) ? "memset " : "",
  3734. dma_has_cap(DMA_INTERRUPT, adev->common.cap_mask) ? "intr " : "");
  3735. }
  3736. static int ppc440spe_adma_setup_irqs(struct ppc440spe_adma_device *adev,
  3737. struct ppc440spe_adma_chan *chan,
  3738. int *initcode)
  3739. {
  3740. struct platform_device *ofdev;
  3741. struct device_node *np;
  3742. int ret;
  3743. ofdev = container_of(adev->dev, struct platform_device, dev);
  3744. np = ofdev->dev.of_node;
  3745. if (adev->id != PPC440SPE_XOR_ID) {
  3746. adev->err_irq = irq_of_parse_and_map(np, 1);
  3747. if (adev->err_irq == NO_IRQ) {
  3748. dev_warn(adev->dev, "no err irq resource?\n");
  3749. *initcode = PPC_ADMA_INIT_IRQ2;
  3750. adev->err_irq = -ENXIO;
  3751. } else
  3752. atomic_inc(&ppc440spe_adma_err_irq_ref);
  3753. } else {
  3754. adev->err_irq = -ENXIO;
  3755. }
  3756. adev->irq = irq_of_parse_and_map(np, 0);
  3757. if (adev->irq == NO_IRQ) {
  3758. dev_err(adev->dev, "no irq resource\n");
  3759. *initcode = PPC_ADMA_INIT_IRQ1;
  3760. ret = -ENXIO;
  3761. goto err_irq_map;
  3762. }
  3763. dev_dbg(adev->dev, "irq %d, err irq %d\n",
  3764. adev->irq, adev->err_irq);
  3765. ret = request_irq(adev->irq, ppc440spe_adma_eot_handler,
  3766. 0, dev_driver_string(adev->dev), chan);
  3767. if (ret) {
  3768. dev_err(adev->dev, "can't request irq %d\n",
  3769. adev->irq);
  3770. *initcode = PPC_ADMA_INIT_IRQ1;
  3771. ret = -EIO;
  3772. goto err_req1;
  3773. }
  3774. /* only DMA engines have a separate error IRQ
  3775. * so it's Ok if err_irq < 0 in XOR engine case.
  3776. */
  3777. if (adev->err_irq > 0) {
  3778. /* both DMA engines share common error IRQ */
  3779. ret = request_irq(adev->err_irq,
  3780. ppc440spe_adma_err_handler,
  3781. IRQF_SHARED,
  3782. dev_driver_string(adev->dev),
  3783. chan);
  3784. if (ret) {
  3785. dev_err(adev->dev, "can't request irq %d\n",
  3786. adev->err_irq);
  3787. *initcode = PPC_ADMA_INIT_IRQ2;
  3788. ret = -EIO;
  3789. goto err_req2;
  3790. }
  3791. }
  3792. if (adev->id == PPC440SPE_XOR_ID) {
  3793. /* enable XOR engine interrupts */
  3794. iowrite32be(XOR_IE_CBCIE_BIT | XOR_IE_ICBIE_BIT |
  3795. XOR_IE_ICIE_BIT | XOR_IE_RPTIE_BIT,
  3796. &adev->xor_reg->ier);
  3797. } else {
  3798. u32 mask, enable;
  3799. np = of_find_compatible_node(NULL, NULL, "ibm,i2o-440spe");
  3800. if (!np) {
  3801. pr_err("%s: can't find I2O device tree node\n",
  3802. __func__);
  3803. ret = -ENODEV;
  3804. goto err_req2;
  3805. }
  3806. adev->i2o_reg = of_iomap(np, 0);
  3807. if (!adev->i2o_reg) {
  3808. pr_err("%s: failed to map I2O registers\n", __func__);
  3809. of_node_put(np);
  3810. ret = -EINVAL;
  3811. goto err_req2;
  3812. }
  3813. of_node_put(np);
  3814. /* Unmask 'CS FIFO Attention' interrupts and
  3815. * enable generating interrupts on errors
  3816. */
  3817. enable = (adev->id == PPC440SPE_DMA0_ID) ?
  3818. ~(I2O_IOPIM_P0SNE | I2O_IOPIM_P0EM) :
  3819. ~(I2O_IOPIM_P1SNE | I2O_IOPIM_P1EM);
  3820. mask = ioread32(&adev->i2o_reg->iopim) & enable;
  3821. iowrite32(mask, &adev->i2o_reg->iopim);
  3822. }
  3823. return 0;
  3824. err_req2:
  3825. free_irq(adev->irq, chan);
  3826. err_req1:
  3827. irq_dispose_mapping(adev->irq);
  3828. err_irq_map:
  3829. if (adev->err_irq > 0) {
  3830. if (atomic_dec_and_test(&ppc440spe_adma_err_irq_ref))
  3831. irq_dispose_mapping(adev->err_irq);
  3832. }
  3833. return ret;
  3834. }
  3835. static void ppc440spe_adma_release_irqs(struct ppc440spe_adma_device *adev,
  3836. struct ppc440spe_adma_chan *chan)
  3837. {
  3838. u32 mask, disable;
  3839. if (adev->id == PPC440SPE_XOR_ID) {
  3840. /* disable XOR engine interrupts */
  3841. mask = ioread32be(&adev->xor_reg->ier);
  3842. mask &= ~(XOR_IE_CBCIE_BIT | XOR_IE_ICBIE_BIT |
  3843. XOR_IE_ICIE_BIT | XOR_IE_RPTIE_BIT);
  3844. iowrite32be(mask, &adev->xor_reg->ier);
  3845. } else {
  3846. /* disable DMAx engine interrupts */
  3847. disable = (adev->id == PPC440SPE_DMA0_ID) ?
  3848. (I2O_IOPIM_P0SNE | I2O_IOPIM_P0EM) :
  3849. (I2O_IOPIM_P1SNE | I2O_IOPIM_P1EM);
  3850. mask = ioread32(&adev->i2o_reg->iopim) | disable;
  3851. iowrite32(mask, &adev->i2o_reg->iopim);
  3852. }
  3853. free_irq(adev->irq, chan);
  3854. irq_dispose_mapping(adev->irq);
  3855. if (adev->err_irq > 0) {
  3856. free_irq(adev->err_irq, chan);
  3857. if (atomic_dec_and_test(&ppc440spe_adma_err_irq_ref)) {
  3858. irq_dispose_mapping(adev->err_irq);
  3859. iounmap(adev->i2o_reg);
  3860. }
  3861. }
  3862. }
  3863. /**
  3864. * ppc440spe_adma_probe - probe the asynch device
  3865. */
  3866. static int __devinit ppc440spe_adma_probe(struct platform_device *ofdev)
  3867. {
  3868. struct device_node *np = ofdev->dev.of_node;
  3869. struct resource res;
  3870. struct ppc440spe_adma_device *adev;
  3871. struct ppc440spe_adma_chan *chan;
  3872. struct ppc_dma_chan_ref *ref, *_ref;
  3873. int ret = 0, initcode = PPC_ADMA_INIT_OK;
  3874. const u32 *idx;
  3875. int len;
  3876. void *regs;
  3877. u32 id, pool_size;
  3878. if (of_device_is_compatible(np, "amcc,xor-accelerator")) {
  3879. id = PPC440SPE_XOR_ID;
  3880. /* As far as the XOR engine is concerned, it does not
  3881. * use FIFOs but uses linked list. So there is no dependency
  3882. * between pool size to allocate and the engine configuration.
  3883. */
  3884. pool_size = PAGE_SIZE << 1;
  3885. } else {
  3886. /* it is DMA0 or DMA1 */
  3887. idx = of_get_property(np, "cell-index", &len);
  3888. if (!idx || (len != sizeof(u32))) {
  3889. dev_err(&ofdev->dev, "Device node %s has missing "
  3890. "or invalid cell-index property\n",
  3891. np->full_name);
  3892. return -EINVAL;
  3893. }
  3894. id = *idx;
  3895. /* DMA0,1 engines use FIFO to maintain CDBs, so we
  3896. * should allocate the pool accordingly to size of this
  3897. * FIFO. Thus, the pool size depends on the FIFO depth:
  3898. * how much CDBs pointers the FIFO may contain then so
  3899. * much CDBs we should provide in the pool.
  3900. * That is
  3901. * CDB size = 32B;
  3902. * CDBs number = (DMA0_FIFO_SIZE >> 3);
  3903. * Pool size = CDBs number * CDB size =
  3904. * = (DMA0_FIFO_SIZE >> 3) << 5 = DMA0_FIFO_SIZE << 2.
  3905. */
  3906. pool_size = (id == PPC440SPE_DMA0_ID) ?
  3907. DMA0_FIFO_SIZE : DMA1_FIFO_SIZE;
  3908. pool_size <<= 2;
  3909. }
  3910. if (of_address_to_resource(np, 0, &res)) {
  3911. dev_err(&ofdev->dev, "failed to get memory resource\n");
  3912. initcode = PPC_ADMA_INIT_MEMRES;
  3913. ret = -ENODEV;
  3914. goto out;
  3915. }
  3916. if (!request_mem_region(res.start, resource_size(&res),
  3917. dev_driver_string(&ofdev->dev))) {
  3918. dev_err(&ofdev->dev, "failed to request memory region %pR\n",
  3919. &res);
  3920. initcode = PPC_ADMA_INIT_MEMREG;
  3921. ret = -EBUSY;
  3922. goto out;
  3923. }
  3924. /* create a device */
  3925. adev = kzalloc(sizeof(*adev), GFP_KERNEL);
  3926. if (!adev) {
  3927. dev_err(&ofdev->dev, "failed to allocate device\n");
  3928. initcode = PPC_ADMA_INIT_ALLOC;
  3929. ret = -ENOMEM;
  3930. goto err_adev_alloc;
  3931. }
  3932. adev->id = id;
  3933. adev->pool_size = pool_size;
  3934. /* allocate coherent memory for hardware descriptors */
  3935. adev->dma_desc_pool_virt = dma_alloc_coherent(&ofdev->dev,
  3936. adev->pool_size, &adev->dma_desc_pool,
  3937. GFP_KERNEL);
  3938. if (adev->dma_desc_pool_virt == NULL) {
  3939. dev_err(&ofdev->dev, "failed to allocate %d bytes of coherent "
  3940. "memory for hardware descriptors\n",
  3941. adev->pool_size);
  3942. initcode = PPC_ADMA_INIT_COHERENT;
  3943. ret = -ENOMEM;
  3944. goto err_dma_alloc;
  3945. }
  3946. dev_dbg(&ofdev->dev, "allocted descriptor pool virt 0x%p phys 0x%llx\n",
  3947. adev->dma_desc_pool_virt, (u64)adev->dma_desc_pool);
  3948. regs = ioremap(res.start, resource_size(&res));
  3949. if (!regs) {
  3950. dev_err(&ofdev->dev, "failed to ioremap regs!\n");
  3951. goto err_regs_alloc;
  3952. }
  3953. if (adev->id == PPC440SPE_XOR_ID) {
  3954. adev->xor_reg = regs;
  3955. /* Reset XOR */
  3956. iowrite32be(XOR_CRSR_XASR_BIT, &adev->xor_reg->crsr);
  3957. iowrite32be(XOR_CRSR_64BA_BIT, &adev->xor_reg->crrr);
  3958. } else {
  3959. size_t fifo_size = (adev->id == PPC440SPE_DMA0_ID) ?
  3960. DMA0_FIFO_SIZE : DMA1_FIFO_SIZE;
  3961. adev->dma_reg = regs;
  3962. /* DMAx_FIFO_SIZE is defined in bytes,
  3963. * <fsiz> - is defined in number of CDB pointers (8byte).
  3964. * DMA FIFO Length = CSlength + CPlength, where
  3965. * CSlength = CPlength = (fsiz + 1) * 8.
  3966. */
  3967. iowrite32(DMA_FIFO_ENABLE | ((fifo_size >> 3) - 2),
  3968. &adev->dma_reg->fsiz);
  3969. /* Configure DMA engine */
  3970. iowrite32(DMA_CFG_DXEPR_HP | DMA_CFG_DFMPP_HP | DMA_CFG_FALGN,
  3971. &adev->dma_reg->cfg);
  3972. /* Clear Status */
  3973. iowrite32(~0, &adev->dma_reg->dsts);
  3974. }
  3975. adev->dev = &ofdev->dev;
  3976. adev->common.dev = &ofdev->dev;
  3977. INIT_LIST_HEAD(&adev->common.channels);
  3978. dev_set_drvdata(&ofdev->dev, adev);
  3979. /* create a channel */
  3980. chan = kzalloc(sizeof(*chan), GFP_KERNEL);
  3981. if (!chan) {
  3982. dev_err(&ofdev->dev, "can't allocate channel structure\n");
  3983. initcode = PPC_ADMA_INIT_CHANNEL;
  3984. ret = -ENOMEM;
  3985. goto err_chan_alloc;
  3986. }
  3987. spin_lock_init(&chan->lock);
  3988. INIT_LIST_HEAD(&chan->chain);
  3989. INIT_LIST_HEAD(&chan->all_slots);
  3990. chan->device = adev;
  3991. chan->common.device = &adev->common;
  3992. dma_cookie_init(&chan->common);
  3993. list_add_tail(&chan->common.device_node, &adev->common.channels);
  3994. tasklet_init(&chan->irq_tasklet, ppc440spe_adma_tasklet,
  3995. (unsigned long)chan);
  3996. /* allocate and map helper pages for async validation or
  3997. * async_mult/async_sum_product operations on DMA0/1.
  3998. */
  3999. if (adev->id != PPC440SPE_XOR_ID) {
  4000. chan->pdest_page = alloc_page(GFP_KERNEL);
  4001. chan->qdest_page = alloc_page(GFP_KERNEL);
  4002. if (!chan->pdest_page ||
  4003. !chan->qdest_page) {
  4004. if (chan->pdest_page)
  4005. __free_page(chan->pdest_page);
  4006. if (chan->qdest_page)
  4007. __free_page(chan->qdest_page);
  4008. ret = -ENOMEM;
  4009. goto err_page_alloc;
  4010. }
  4011. chan->pdest = dma_map_page(&ofdev->dev, chan->pdest_page, 0,
  4012. PAGE_SIZE, DMA_BIDIRECTIONAL);
  4013. chan->qdest = dma_map_page(&ofdev->dev, chan->qdest_page, 0,
  4014. PAGE_SIZE, DMA_BIDIRECTIONAL);
  4015. }
  4016. ref = kmalloc(sizeof(*ref), GFP_KERNEL);
  4017. if (ref) {
  4018. ref->chan = &chan->common;
  4019. INIT_LIST_HEAD(&ref->node);
  4020. list_add_tail(&ref->node, &ppc440spe_adma_chan_list);
  4021. } else {
  4022. dev_err(&ofdev->dev, "failed to allocate channel reference!\n");
  4023. ret = -ENOMEM;
  4024. goto err_ref_alloc;
  4025. }
  4026. ret = ppc440spe_adma_setup_irqs(adev, chan, &initcode);
  4027. if (ret)
  4028. goto err_irq;
  4029. ppc440spe_adma_init_capabilities(adev);
  4030. ret = dma_async_device_register(&adev->common);
  4031. if (ret) {
  4032. initcode = PPC_ADMA_INIT_REGISTER;
  4033. dev_err(&ofdev->dev, "failed to register dma device\n");
  4034. goto err_dev_reg;
  4035. }
  4036. goto out;
  4037. err_dev_reg:
  4038. ppc440spe_adma_release_irqs(adev, chan);
  4039. err_irq:
  4040. list_for_each_entry_safe(ref, _ref, &ppc440spe_adma_chan_list, node) {
  4041. if (chan == to_ppc440spe_adma_chan(ref->chan)) {
  4042. list_del(&ref->node);
  4043. kfree(ref);
  4044. }
  4045. }
  4046. err_ref_alloc:
  4047. if (adev->id != PPC440SPE_XOR_ID) {
  4048. dma_unmap_page(&ofdev->dev, chan->pdest,
  4049. PAGE_SIZE, DMA_BIDIRECTIONAL);
  4050. dma_unmap_page(&ofdev->dev, chan->qdest,
  4051. PAGE_SIZE, DMA_BIDIRECTIONAL);
  4052. __free_page(chan->pdest_page);
  4053. __free_page(chan->qdest_page);
  4054. }
  4055. err_page_alloc:
  4056. kfree(chan);
  4057. err_chan_alloc:
  4058. if (adev->id == PPC440SPE_XOR_ID)
  4059. iounmap(adev->xor_reg);
  4060. else
  4061. iounmap(adev->dma_reg);
  4062. err_regs_alloc:
  4063. dma_free_coherent(adev->dev, adev->pool_size,
  4064. adev->dma_desc_pool_virt,
  4065. adev->dma_desc_pool);
  4066. err_dma_alloc:
  4067. kfree(adev);
  4068. err_adev_alloc:
  4069. release_mem_region(res.start, resource_size(&res));
  4070. out:
  4071. if (id < PPC440SPE_ADMA_ENGINES_NUM)
  4072. ppc440spe_adma_devices[id] = initcode;
  4073. return ret;
  4074. }
  4075. /**
  4076. * ppc440spe_adma_remove - remove the asynch device
  4077. */
  4078. static int __devexit ppc440spe_adma_remove(struct platform_device *ofdev)
  4079. {
  4080. struct ppc440spe_adma_device *adev = dev_get_drvdata(&ofdev->dev);
  4081. struct device_node *np = ofdev->dev.of_node;
  4082. struct resource res;
  4083. struct dma_chan *chan, *_chan;
  4084. struct ppc_dma_chan_ref *ref, *_ref;
  4085. struct ppc440spe_adma_chan *ppc440spe_chan;
  4086. dev_set_drvdata(&ofdev->dev, NULL);
  4087. if (adev->id < PPC440SPE_ADMA_ENGINES_NUM)
  4088. ppc440spe_adma_devices[adev->id] = -1;
  4089. dma_async_device_unregister(&adev->common);
  4090. list_for_each_entry_safe(chan, _chan, &adev->common.channels,
  4091. device_node) {
  4092. ppc440spe_chan = to_ppc440spe_adma_chan(chan);
  4093. ppc440spe_adma_release_irqs(adev, ppc440spe_chan);
  4094. tasklet_kill(&ppc440spe_chan->irq_tasklet);
  4095. if (adev->id != PPC440SPE_XOR_ID) {
  4096. dma_unmap_page(&ofdev->dev, ppc440spe_chan->pdest,
  4097. PAGE_SIZE, DMA_BIDIRECTIONAL);
  4098. dma_unmap_page(&ofdev->dev, ppc440spe_chan->qdest,
  4099. PAGE_SIZE, DMA_BIDIRECTIONAL);
  4100. __free_page(ppc440spe_chan->pdest_page);
  4101. __free_page(ppc440spe_chan->qdest_page);
  4102. }
  4103. list_for_each_entry_safe(ref, _ref, &ppc440spe_adma_chan_list,
  4104. node) {
  4105. if (ppc440spe_chan ==
  4106. to_ppc440spe_adma_chan(ref->chan)) {
  4107. list_del(&ref->node);
  4108. kfree(ref);
  4109. }
  4110. }
  4111. list_del(&chan->device_node);
  4112. kfree(ppc440spe_chan);
  4113. }
  4114. dma_free_coherent(adev->dev, adev->pool_size,
  4115. adev->dma_desc_pool_virt, adev->dma_desc_pool);
  4116. if (adev->id == PPC440SPE_XOR_ID)
  4117. iounmap(adev->xor_reg);
  4118. else
  4119. iounmap(adev->dma_reg);
  4120. of_address_to_resource(np, 0, &res);
  4121. release_mem_region(res.start, resource_size(&res));
  4122. kfree(adev);
  4123. return 0;
  4124. }
  4125. /*
  4126. * /sys driver interface to enable h/w RAID-6 capabilities
  4127. * Files created in e.g. /sys/devices/plb.0/400100100.dma0/driver/
  4128. * directory are "devices", "enable" and "poly".
  4129. * "devices" shows available engines.
  4130. * "enable" is used to enable RAID-6 capabilities or to check
  4131. * whether these has been activated.
  4132. * "poly" allows setting/checking used polynomial (for PPC440SPe only).
  4133. */
  4134. static ssize_t show_ppc440spe_devices(struct device_driver *dev, char *buf)
  4135. {
  4136. ssize_t size = 0;
  4137. int i;
  4138. for (i = 0; i < PPC440SPE_ADMA_ENGINES_NUM; i++) {
  4139. if (ppc440spe_adma_devices[i] == -1)
  4140. continue;
  4141. size += snprintf(buf + size, PAGE_SIZE - size,
  4142. "PPC440SP(E)-ADMA.%d: %s\n", i,
  4143. ppc_adma_errors[ppc440spe_adma_devices[i]]);
  4144. }
  4145. return size;
  4146. }
  4147. static ssize_t show_ppc440spe_r6enable(struct device_driver *dev, char *buf)
  4148. {
  4149. return snprintf(buf, PAGE_SIZE,
  4150. "PPC440SP(e) RAID-6 capabilities are %sABLED.\n",
  4151. ppc440spe_r6_enabled ? "EN" : "DIS");
  4152. }
  4153. static ssize_t store_ppc440spe_r6enable(struct device_driver *dev,
  4154. const char *buf, size_t count)
  4155. {
  4156. unsigned long val;
  4157. if (!count || count > 11)
  4158. return -EINVAL;
  4159. if (!ppc440spe_r6_tchan)
  4160. return -EFAULT;
  4161. /* Write a key */
  4162. sscanf(buf, "%lx", &val);
  4163. dcr_write(ppc440spe_mq_dcr_host, DCRN_MQ0_XORBA, val);
  4164. isync();
  4165. /* Verify whether it really works now */
  4166. if (ppc440spe_test_raid6(ppc440spe_r6_tchan) == 0) {
  4167. pr_info("PPC440SP(e) RAID-6 has been activated "
  4168. "successfully\n");
  4169. ppc440spe_r6_enabled = 1;
  4170. } else {
  4171. pr_info("PPC440SP(e) RAID-6 hasn't been activated!"
  4172. " Error key ?\n");
  4173. ppc440spe_r6_enabled = 0;
  4174. }
  4175. return count;
  4176. }
  4177. static ssize_t show_ppc440spe_r6poly(struct device_driver *dev, char *buf)
  4178. {
  4179. ssize_t size = 0;
  4180. u32 reg;
  4181. #ifdef CONFIG_440SP
  4182. /* 440SP has fixed polynomial */
  4183. reg = 0x4d;
  4184. #else
  4185. reg = dcr_read(ppc440spe_mq_dcr_host, DCRN_MQ0_CFBHL);
  4186. reg >>= MQ0_CFBHL_POLY;
  4187. reg &= 0xFF;
  4188. #endif
  4189. size = snprintf(buf, PAGE_SIZE, "PPC440SP(e) RAID-6 driver "
  4190. "uses 0x1%02x polynomial.\n", reg);
  4191. return size;
  4192. }
  4193. static ssize_t store_ppc440spe_r6poly(struct device_driver *dev,
  4194. const char *buf, size_t count)
  4195. {
  4196. unsigned long reg, val;
  4197. #ifdef CONFIG_440SP
  4198. /* 440SP uses default 0x14D polynomial only */
  4199. return -EINVAL;
  4200. #endif
  4201. if (!count || count > 6)
  4202. return -EINVAL;
  4203. /* e.g., 0x14D or 0x11D */
  4204. sscanf(buf, "%lx", &val);
  4205. if (val & ~0x1FF)
  4206. return -EINVAL;
  4207. val &= 0xFF;
  4208. reg = dcr_read(ppc440spe_mq_dcr_host, DCRN_MQ0_CFBHL);
  4209. reg &= ~(0xFF << MQ0_CFBHL_POLY);
  4210. reg |= val << MQ0_CFBHL_POLY;
  4211. dcr_write(ppc440spe_mq_dcr_host, DCRN_MQ0_CFBHL, reg);
  4212. return count;
  4213. }
  4214. static DRIVER_ATTR(devices, S_IRUGO, show_ppc440spe_devices, NULL);
  4215. static DRIVER_ATTR(enable, S_IRUGO | S_IWUSR, show_ppc440spe_r6enable,
  4216. store_ppc440spe_r6enable);
  4217. static DRIVER_ATTR(poly, S_IRUGO | S_IWUSR, show_ppc440spe_r6poly,
  4218. store_ppc440spe_r6poly);
  4219. /*
  4220. * Common initialisation for RAID engines; allocate memory for
  4221. * DMAx FIFOs, perform configuration common for all DMA engines.
  4222. * Further DMA engine specific configuration is done at probe time.
  4223. */
  4224. static int ppc440spe_configure_raid_devices(void)
  4225. {
  4226. struct device_node *np;
  4227. struct resource i2o_res;
  4228. struct i2o_regs __iomem *i2o_reg;
  4229. dcr_host_t i2o_dcr_host;
  4230. unsigned int dcr_base, dcr_len;
  4231. int i, ret;
  4232. np = of_find_compatible_node(NULL, NULL, "ibm,i2o-440spe");
  4233. if (!np) {
  4234. pr_err("%s: can't find I2O device tree node\n",
  4235. __func__);
  4236. return -ENODEV;
  4237. }
  4238. if (of_address_to_resource(np, 0, &i2o_res)) {
  4239. of_node_put(np);
  4240. return -EINVAL;
  4241. }
  4242. i2o_reg = of_iomap(np, 0);
  4243. if (!i2o_reg) {
  4244. pr_err("%s: failed to map I2O registers\n", __func__);
  4245. of_node_put(np);
  4246. return -EINVAL;
  4247. }
  4248. /* Get I2O DCRs base */
  4249. dcr_base = dcr_resource_start(np, 0);
  4250. dcr_len = dcr_resource_len(np, 0);
  4251. if (!dcr_base && !dcr_len) {
  4252. pr_err("%s: can't get DCR registers base/len!\n",
  4253. np->full_name);
  4254. of_node_put(np);
  4255. iounmap(i2o_reg);
  4256. return -ENODEV;
  4257. }
  4258. i2o_dcr_host = dcr_map(np, dcr_base, dcr_len);
  4259. if (!DCR_MAP_OK(i2o_dcr_host)) {
  4260. pr_err("%s: failed to map DCRs!\n", np->full_name);
  4261. of_node_put(np);
  4262. iounmap(i2o_reg);
  4263. return -ENODEV;
  4264. }
  4265. of_node_put(np);
  4266. /* Provide memory regions for DMA's FIFOs: I2O, DMA0 and DMA1 share
  4267. * the base address of FIFO memory space.
  4268. * Actually we need twice more physical memory than programmed in the
  4269. * <fsiz> register (because there are two FIFOs for each DMA: CP and CS)
  4270. */
  4271. ppc440spe_dma_fifo_buf = kmalloc((DMA0_FIFO_SIZE + DMA1_FIFO_SIZE) << 1,
  4272. GFP_KERNEL);
  4273. if (!ppc440spe_dma_fifo_buf) {
  4274. pr_err("%s: DMA FIFO buffer allocation failed.\n", __func__);
  4275. iounmap(i2o_reg);
  4276. dcr_unmap(i2o_dcr_host, dcr_len);
  4277. return -ENOMEM;
  4278. }
  4279. /*
  4280. * Configure h/w
  4281. */
  4282. /* Reset I2O/DMA */
  4283. mtdcri(SDR0, DCRN_SDR0_SRST, DCRN_SDR0_SRST_I2ODMA);
  4284. mtdcri(SDR0, DCRN_SDR0_SRST, 0);
  4285. /* Setup the base address of mmaped registers */
  4286. dcr_write(i2o_dcr_host, DCRN_I2O0_IBAH, (u32)(i2o_res.start >> 32));
  4287. dcr_write(i2o_dcr_host, DCRN_I2O0_IBAL, (u32)(i2o_res.start) |
  4288. I2O_REG_ENABLE);
  4289. dcr_unmap(i2o_dcr_host, dcr_len);
  4290. /* Setup FIFO memory space base address */
  4291. iowrite32(0, &i2o_reg->ifbah);
  4292. iowrite32(((u32)__pa(ppc440spe_dma_fifo_buf)), &i2o_reg->ifbal);
  4293. /* set zero FIFO size for I2O, so the whole
  4294. * ppc440spe_dma_fifo_buf is used by DMAs.
  4295. * DMAx_FIFOs will be configured while probe.
  4296. */
  4297. iowrite32(0, &i2o_reg->ifsiz);
  4298. iounmap(i2o_reg);
  4299. /* To prepare WXOR/RXOR functionality we need access to
  4300. * Memory Queue Module DCRs (finally it will be enabled
  4301. * via /sys interface of the ppc440spe ADMA driver).
  4302. */
  4303. np = of_find_compatible_node(NULL, NULL, "ibm,mq-440spe");
  4304. if (!np) {
  4305. pr_err("%s: can't find MQ device tree node\n",
  4306. __func__);
  4307. ret = -ENODEV;
  4308. goto out_free;
  4309. }
  4310. /* Get MQ DCRs base */
  4311. dcr_base = dcr_resource_start(np, 0);
  4312. dcr_len = dcr_resource_len(np, 0);
  4313. if (!dcr_base && !dcr_len) {
  4314. pr_err("%s: can't get DCR registers base/len!\n",
  4315. np->full_name);
  4316. ret = -ENODEV;
  4317. goto out_mq;
  4318. }
  4319. ppc440spe_mq_dcr_host = dcr_map(np, dcr_base, dcr_len);
  4320. if (!DCR_MAP_OK(ppc440spe_mq_dcr_host)) {
  4321. pr_err("%s: failed to map DCRs!\n", np->full_name);
  4322. ret = -ENODEV;
  4323. goto out_mq;
  4324. }
  4325. of_node_put(np);
  4326. ppc440spe_mq_dcr_len = dcr_len;
  4327. /* Set HB alias */
  4328. dcr_write(ppc440spe_mq_dcr_host, DCRN_MQ0_BAUH, DMA_CUED_XOR_HB);
  4329. /* Set:
  4330. * - LL transaction passing limit to 1;
  4331. * - Memory controller cycle limit to 1;
  4332. * - Galois Polynomial to 0x14d (default)
  4333. */
  4334. dcr_write(ppc440spe_mq_dcr_host, DCRN_MQ0_CFBHL,
  4335. (1 << MQ0_CFBHL_TPLM) | (1 << MQ0_CFBHL_HBCL) |
  4336. (PPC440SPE_DEFAULT_POLY << MQ0_CFBHL_POLY));
  4337. atomic_set(&ppc440spe_adma_err_irq_ref, 0);
  4338. for (i = 0; i < PPC440SPE_ADMA_ENGINES_NUM; i++)
  4339. ppc440spe_adma_devices[i] = -1;
  4340. return 0;
  4341. out_mq:
  4342. of_node_put(np);
  4343. out_free:
  4344. kfree(ppc440spe_dma_fifo_buf);
  4345. return ret;
  4346. }
  4347. static const struct of_device_id ppc440spe_adma_of_match[] __devinitconst = {
  4348. { .compatible = "ibm,dma-440spe", },
  4349. { .compatible = "amcc,xor-accelerator", },
  4350. {},
  4351. };
  4352. MODULE_DEVICE_TABLE(of, ppc440spe_adma_of_match);
  4353. static struct platform_driver ppc440spe_adma_driver = {
  4354. .probe = ppc440spe_adma_probe,
  4355. .remove = __devexit_p(ppc440spe_adma_remove),
  4356. .driver = {
  4357. .name = "PPC440SP(E)-ADMA",
  4358. .owner = THIS_MODULE,
  4359. .of_match_table = ppc440spe_adma_of_match,
  4360. },
  4361. };
  4362. static __init int ppc440spe_adma_init(void)
  4363. {
  4364. int ret;
  4365. ret = ppc440spe_configure_raid_devices();
  4366. if (ret)
  4367. return ret;
  4368. ret = platform_driver_register(&ppc440spe_adma_driver);
  4369. if (ret) {
  4370. pr_err("%s: failed to register platform driver\n",
  4371. __func__);
  4372. goto out_reg;
  4373. }
  4374. /* Initialization status */
  4375. ret = driver_create_file(&ppc440spe_adma_driver.driver,
  4376. &driver_attr_devices);
  4377. if (ret)
  4378. goto out_dev;
  4379. /* RAID-6 h/w enable entry */
  4380. ret = driver_create_file(&ppc440spe_adma_driver.driver,
  4381. &driver_attr_enable);
  4382. if (ret)
  4383. goto out_en;
  4384. /* GF polynomial to use */
  4385. ret = driver_create_file(&ppc440spe_adma_driver.driver,
  4386. &driver_attr_poly);
  4387. if (!ret)
  4388. return ret;
  4389. driver_remove_file(&ppc440spe_adma_driver.driver,
  4390. &driver_attr_enable);
  4391. out_en:
  4392. driver_remove_file(&ppc440spe_adma_driver.driver,
  4393. &driver_attr_devices);
  4394. out_dev:
  4395. /* User will not be able to enable h/w RAID-6 */
  4396. pr_err("%s: failed to create RAID-6 driver interface\n",
  4397. __func__);
  4398. platform_driver_unregister(&ppc440spe_adma_driver);
  4399. out_reg:
  4400. dcr_unmap(ppc440spe_mq_dcr_host, ppc440spe_mq_dcr_len);
  4401. kfree(ppc440spe_dma_fifo_buf);
  4402. return ret;
  4403. }
  4404. static void __exit ppc440spe_adma_exit(void)
  4405. {
  4406. driver_remove_file(&ppc440spe_adma_driver.driver,
  4407. &driver_attr_poly);
  4408. driver_remove_file(&ppc440spe_adma_driver.driver,
  4409. &driver_attr_enable);
  4410. driver_remove_file(&ppc440spe_adma_driver.driver,
  4411. &driver_attr_devices);
  4412. platform_driver_unregister(&ppc440spe_adma_driver);
  4413. dcr_unmap(ppc440spe_mq_dcr_host, ppc440spe_mq_dcr_len);
  4414. kfree(ppc440spe_dma_fifo_buf);
  4415. }
  4416. arch_initcall(ppc440spe_adma_init);
  4417. module_exit(ppc440spe_adma_exit);
  4418. MODULE_AUTHOR("Yuri Tikhonov <yur@emcraft.com>");
  4419. MODULE_DESCRIPTION("PPC440SPE ADMA Engine Driver");
  4420. MODULE_LICENSE("GPL");