adma.c 125 KB

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