adma.c 134 KB

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