adma.c 125 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640
  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 %pOF has missing "
  3577. "or invalid cell-index property\n",
  3578. np);
  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 devices_show(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 DRIVER_ATTR_RO(devices);
  3833. static ssize_t enable_show(struct device_driver *dev, char *buf)
  3834. {
  3835. return snprintf(buf, PAGE_SIZE,
  3836. "PPC440SP(e) RAID-6 capabilities are %sABLED.\n",
  3837. ppc440spe_r6_enabled ? "EN" : "DIS");
  3838. }
  3839. static ssize_t enable_store(struct device_driver *dev, const char *buf,
  3840. size_t count)
  3841. {
  3842. unsigned long val;
  3843. if (!count || count > 11)
  3844. return -EINVAL;
  3845. if (!ppc440spe_r6_tchan)
  3846. return -EFAULT;
  3847. /* Write a key */
  3848. sscanf(buf, "%lx", &val);
  3849. dcr_write(ppc440spe_mq_dcr_host, DCRN_MQ0_XORBA, val);
  3850. isync();
  3851. /* Verify whether it really works now */
  3852. if (ppc440spe_test_raid6(ppc440spe_r6_tchan) == 0) {
  3853. pr_info("PPC440SP(e) RAID-6 has been activated "
  3854. "successfully\n");
  3855. ppc440spe_r6_enabled = 1;
  3856. } else {
  3857. pr_info("PPC440SP(e) RAID-6 hasn't been activated!"
  3858. " Error key ?\n");
  3859. ppc440spe_r6_enabled = 0;
  3860. }
  3861. return count;
  3862. }
  3863. static DRIVER_ATTR_RW(enable);
  3864. static ssize_t poly_show(struct device_driver *dev, char *buf)
  3865. {
  3866. ssize_t size = 0;
  3867. u32 reg;
  3868. #ifdef CONFIG_440SP
  3869. /* 440SP has fixed polynomial */
  3870. reg = 0x4d;
  3871. #else
  3872. reg = dcr_read(ppc440spe_mq_dcr_host, DCRN_MQ0_CFBHL);
  3873. reg >>= MQ0_CFBHL_POLY;
  3874. reg &= 0xFF;
  3875. #endif
  3876. size = snprintf(buf, PAGE_SIZE, "PPC440SP(e) RAID-6 driver "
  3877. "uses 0x1%02x polynomial.\n", reg);
  3878. return size;
  3879. }
  3880. static ssize_t poly_store(struct device_driver *dev, const char *buf,
  3881. size_t count)
  3882. {
  3883. unsigned long reg, val;
  3884. #ifdef CONFIG_440SP
  3885. /* 440SP uses default 0x14D polynomial only */
  3886. return -EINVAL;
  3887. #endif
  3888. if (!count || count > 6)
  3889. return -EINVAL;
  3890. /* e.g., 0x14D or 0x11D */
  3891. sscanf(buf, "%lx", &val);
  3892. if (val & ~0x1FF)
  3893. return -EINVAL;
  3894. val &= 0xFF;
  3895. reg = dcr_read(ppc440spe_mq_dcr_host, DCRN_MQ0_CFBHL);
  3896. reg &= ~(0xFF << MQ0_CFBHL_POLY);
  3897. reg |= val << MQ0_CFBHL_POLY;
  3898. dcr_write(ppc440spe_mq_dcr_host, DCRN_MQ0_CFBHL, reg);
  3899. return count;
  3900. }
  3901. static DRIVER_ATTR_RW(poly);
  3902. /*
  3903. * Common initialisation for RAID engines; allocate memory for
  3904. * DMAx FIFOs, perform configuration common for all DMA engines.
  3905. * Further DMA engine specific configuration is done at probe time.
  3906. */
  3907. static int ppc440spe_configure_raid_devices(void)
  3908. {
  3909. struct device_node *np;
  3910. struct resource i2o_res;
  3911. struct i2o_regs __iomem *i2o_reg;
  3912. dcr_host_t i2o_dcr_host;
  3913. unsigned int dcr_base, dcr_len;
  3914. int i, ret;
  3915. np = of_find_compatible_node(NULL, NULL, "ibm,i2o-440spe");
  3916. if (!np) {
  3917. pr_err("%s: can't find I2O device tree node\n",
  3918. __func__);
  3919. return -ENODEV;
  3920. }
  3921. if (of_address_to_resource(np, 0, &i2o_res)) {
  3922. of_node_put(np);
  3923. return -EINVAL;
  3924. }
  3925. i2o_reg = of_iomap(np, 0);
  3926. if (!i2o_reg) {
  3927. pr_err("%s: failed to map I2O registers\n", __func__);
  3928. of_node_put(np);
  3929. return -EINVAL;
  3930. }
  3931. /* Get I2O DCRs base */
  3932. dcr_base = dcr_resource_start(np, 0);
  3933. dcr_len = dcr_resource_len(np, 0);
  3934. if (!dcr_base && !dcr_len) {
  3935. pr_err("%pOF: can't get DCR registers base/len!\n", np);
  3936. of_node_put(np);
  3937. iounmap(i2o_reg);
  3938. return -ENODEV;
  3939. }
  3940. i2o_dcr_host = dcr_map(np, dcr_base, dcr_len);
  3941. if (!DCR_MAP_OK(i2o_dcr_host)) {
  3942. pr_err("%pOF: failed to map DCRs!\n", np);
  3943. of_node_put(np);
  3944. iounmap(i2o_reg);
  3945. return -ENODEV;
  3946. }
  3947. of_node_put(np);
  3948. /* Provide memory regions for DMA's FIFOs: I2O, DMA0 and DMA1 share
  3949. * the base address of FIFO memory space.
  3950. * Actually we need twice more physical memory than programmed in the
  3951. * <fsiz> register (because there are two FIFOs for each DMA: CP and CS)
  3952. */
  3953. ppc440spe_dma_fifo_buf = kmalloc((DMA0_FIFO_SIZE + DMA1_FIFO_SIZE) << 1,
  3954. GFP_KERNEL);
  3955. if (!ppc440spe_dma_fifo_buf) {
  3956. pr_err("%s: DMA FIFO buffer allocation failed.\n", __func__);
  3957. iounmap(i2o_reg);
  3958. dcr_unmap(i2o_dcr_host, dcr_len);
  3959. return -ENOMEM;
  3960. }
  3961. /*
  3962. * Configure h/w
  3963. */
  3964. /* Reset I2O/DMA */
  3965. mtdcri(SDR0, DCRN_SDR0_SRST, DCRN_SDR0_SRST_I2ODMA);
  3966. mtdcri(SDR0, DCRN_SDR0_SRST, 0);
  3967. /* Setup the base address of mmaped registers */
  3968. dcr_write(i2o_dcr_host, DCRN_I2O0_IBAH, (u32)(i2o_res.start >> 32));
  3969. dcr_write(i2o_dcr_host, DCRN_I2O0_IBAL, (u32)(i2o_res.start) |
  3970. I2O_REG_ENABLE);
  3971. dcr_unmap(i2o_dcr_host, dcr_len);
  3972. /* Setup FIFO memory space base address */
  3973. iowrite32(0, &i2o_reg->ifbah);
  3974. iowrite32(((u32)__pa(ppc440spe_dma_fifo_buf)), &i2o_reg->ifbal);
  3975. /* set zero FIFO size for I2O, so the whole
  3976. * ppc440spe_dma_fifo_buf is used by DMAs.
  3977. * DMAx_FIFOs will be configured while probe.
  3978. */
  3979. iowrite32(0, &i2o_reg->ifsiz);
  3980. iounmap(i2o_reg);
  3981. /* To prepare WXOR/RXOR functionality we need access to
  3982. * Memory Queue Module DCRs (finally it will be enabled
  3983. * via /sys interface of the ppc440spe ADMA driver).
  3984. */
  3985. np = of_find_compatible_node(NULL, NULL, "ibm,mq-440spe");
  3986. if (!np) {
  3987. pr_err("%s: can't find MQ device tree node\n",
  3988. __func__);
  3989. ret = -ENODEV;
  3990. goto out_free;
  3991. }
  3992. /* Get MQ DCRs base */
  3993. dcr_base = dcr_resource_start(np, 0);
  3994. dcr_len = dcr_resource_len(np, 0);
  3995. if (!dcr_base && !dcr_len) {
  3996. pr_err("%pOF: can't get DCR registers base/len!\n", np);
  3997. ret = -ENODEV;
  3998. goto out_mq;
  3999. }
  4000. ppc440spe_mq_dcr_host = dcr_map(np, dcr_base, dcr_len);
  4001. if (!DCR_MAP_OK(ppc440spe_mq_dcr_host)) {
  4002. pr_err("%pOF: failed to map DCRs!\n", np);
  4003. ret = -ENODEV;
  4004. goto out_mq;
  4005. }
  4006. of_node_put(np);
  4007. ppc440spe_mq_dcr_len = dcr_len;
  4008. /* Set HB alias */
  4009. dcr_write(ppc440spe_mq_dcr_host, DCRN_MQ0_BAUH, DMA_CUED_XOR_HB);
  4010. /* Set:
  4011. * - LL transaction passing limit to 1;
  4012. * - Memory controller cycle limit to 1;
  4013. * - Galois Polynomial to 0x14d (default)
  4014. */
  4015. dcr_write(ppc440spe_mq_dcr_host, DCRN_MQ0_CFBHL,
  4016. (1 << MQ0_CFBHL_TPLM) | (1 << MQ0_CFBHL_HBCL) |
  4017. (PPC440SPE_DEFAULT_POLY << MQ0_CFBHL_POLY));
  4018. atomic_set(&ppc440spe_adma_err_irq_ref, 0);
  4019. for (i = 0; i < PPC440SPE_ADMA_ENGINES_NUM; i++)
  4020. ppc440spe_adma_devices[i] = -1;
  4021. return 0;
  4022. out_mq:
  4023. of_node_put(np);
  4024. out_free:
  4025. kfree(ppc440spe_dma_fifo_buf);
  4026. return ret;
  4027. }
  4028. static const struct of_device_id ppc440spe_adma_of_match[] = {
  4029. { .compatible = "ibm,dma-440spe", },
  4030. { .compatible = "amcc,xor-accelerator", },
  4031. {},
  4032. };
  4033. MODULE_DEVICE_TABLE(of, ppc440spe_adma_of_match);
  4034. static struct platform_driver ppc440spe_adma_driver = {
  4035. .probe = ppc440spe_adma_probe,
  4036. .remove = ppc440spe_adma_remove,
  4037. .driver = {
  4038. .name = "PPC440SP(E)-ADMA",
  4039. .of_match_table = ppc440spe_adma_of_match,
  4040. },
  4041. };
  4042. static __init int ppc440spe_adma_init(void)
  4043. {
  4044. int ret;
  4045. ret = ppc440spe_configure_raid_devices();
  4046. if (ret)
  4047. return ret;
  4048. ret = platform_driver_register(&ppc440spe_adma_driver);
  4049. if (ret) {
  4050. pr_err("%s: failed to register platform driver\n",
  4051. __func__);
  4052. goto out_reg;
  4053. }
  4054. /* Initialization status */
  4055. ret = driver_create_file(&ppc440spe_adma_driver.driver,
  4056. &driver_attr_devices);
  4057. if (ret)
  4058. goto out_dev;
  4059. /* RAID-6 h/w enable entry */
  4060. ret = driver_create_file(&ppc440spe_adma_driver.driver,
  4061. &driver_attr_enable);
  4062. if (ret)
  4063. goto out_en;
  4064. /* GF polynomial to use */
  4065. ret = driver_create_file(&ppc440spe_adma_driver.driver,
  4066. &driver_attr_poly);
  4067. if (!ret)
  4068. return ret;
  4069. driver_remove_file(&ppc440spe_adma_driver.driver,
  4070. &driver_attr_enable);
  4071. out_en:
  4072. driver_remove_file(&ppc440spe_adma_driver.driver,
  4073. &driver_attr_devices);
  4074. out_dev:
  4075. /* User will not be able to enable h/w RAID-6 */
  4076. pr_err("%s: failed to create RAID-6 driver interface\n",
  4077. __func__);
  4078. platform_driver_unregister(&ppc440spe_adma_driver);
  4079. out_reg:
  4080. dcr_unmap(ppc440spe_mq_dcr_host, ppc440spe_mq_dcr_len);
  4081. kfree(ppc440spe_dma_fifo_buf);
  4082. return ret;
  4083. }
  4084. static void __exit ppc440spe_adma_exit(void)
  4085. {
  4086. driver_remove_file(&ppc440spe_adma_driver.driver,
  4087. &driver_attr_poly);
  4088. driver_remove_file(&ppc440spe_adma_driver.driver,
  4089. &driver_attr_enable);
  4090. driver_remove_file(&ppc440spe_adma_driver.driver,
  4091. &driver_attr_devices);
  4092. platform_driver_unregister(&ppc440spe_adma_driver);
  4093. dcr_unmap(ppc440spe_mq_dcr_host, ppc440spe_mq_dcr_len);
  4094. kfree(ppc440spe_dma_fifo_buf);
  4095. }
  4096. arch_initcall(ppc440spe_adma_init);
  4097. module_exit(ppc440spe_adma_exit);
  4098. MODULE_AUTHOR("Yuri Tikhonov <yur@emcraft.com>");
  4099. MODULE_DESCRIPTION("PPC440SPE ADMA Engine Driver");
  4100. MODULE_LICENSE("GPL");