You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hermon.c 116KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023
  1. /*
  2. * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
  3. * Copyright (C) 2008 Mellanox Technologies Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of the
  8. * License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA.
  19. */
  20. FILE_LICENCE ( GPL2_OR_LATER );
  21. #include <stdint.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <strings.h>
  26. #include <unistd.h>
  27. #include <errno.h>
  28. #include <byteswap.h>
  29. #include <ipxe/io.h>
  30. #include <ipxe/pci.h>
  31. #include <ipxe/pcibackup.h>
  32. #include <ipxe/malloc.h>
  33. #include <ipxe/umalloc.h>
  34. #include <ipxe/iobuf.h>
  35. #include <ipxe/netdevice.h>
  36. #include <ipxe/infiniband.h>
  37. #include <ipxe/ib_smc.h>
  38. #include <ipxe/if_ether.h>
  39. #include <ipxe/ethernet.h>
  40. #include <ipxe/fcoe.h>
  41. #include <ipxe/vlan.h>
  42. #include <ipxe/bofm.h>
  43. #include <ipxe/nvsvpd.h>
  44. #include <ipxe/nvo.h>
  45. #include "hermon.h"
  46. /**
  47. * @file
  48. *
  49. * Mellanox Hermon Infiniband HCA
  50. *
  51. */
  52. /***************************************************************************
  53. *
  54. * Queue number allocation
  55. *
  56. ***************************************************************************
  57. */
  58. /**
  59. * Allocate offsets within usage bitmask
  60. *
  61. * @v bits Usage bitmask
  62. * @v bits_len Length of usage bitmask
  63. * @v num_bits Number of contiguous bits to allocate within bitmask
  64. * @ret bit First free bit within bitmask, or negative error
  65. */
  66. static int hermon_bitmask_alloc ( hermon_bitmask_t *bits,
  67. unsigned int bits_len,
  68. unsigned int num_bits ) {
  69. unsigned int bit = 0;
  70. hermon_bitmask_t mask = 1;
  71. unsigned int found = 0;
  72. /* Search bits for num_bits contiguous free bits */
  73. while ( bit < bits_len ) {
  74. if ( ( mask & *bits ) == 0 ) {
  75. if ( ++found == num_bits )
  76. goto found;
  77. } else {
  78. found = 0;
  79. }
  80. bit++;
  81. mask = ( mask << 1 ) | ( mask >> ( 8 * sizeof ( mask ) - 1 ) );
  82. if ( mask == 1 )
  83. bits++;
  84. }
  85. return -ENFILE;
  86. found:
  87. /* Mark bits as in-use */
  88. do {
  89. *bits |= mask;
  90. if ( mask == 1 )
  91. bits--;
  92. mask = ( mask >> 1 ) | ( mask << ( 8 * sizeof ( mask ) - 1 ) );
  93. } while ( --found );
  94. return ( bit - num_bits + 1 );
  95. }
  96. /**
  97. * Free offsets within usage bitmask
  98. *
  99. * @v bits Usage bitmask
  100. * @v bit Starting bit within bitmask
  101. * @v num_bits Number of contiguous bits to free within bitmask
  102. */
  103. static void hermon_bitmask_free ( hermon_bitmask_t *bits,
  104. int bit, unsigned int num_bits ) {
  105. hermon_bitmask_t mask;
  106. for ( ; num_bits ; bit++, num_bits-- ) {
  107. mask = ( 1 << ( bit % ( 8 * sizeof ( mask ) ) ) );
  108. bits[ ( bit / ( 8 * sizeof ( mask ) ) ) ] &= ~mask;
  109. }
  110. }
  111. /***************************************************************************
  112. *
  113. * HCA commands
  114. *
  115. ***************************************************************************
  116. */
  117. /**
  118. * Wait for Hermon command completion
  119. *
  120. * @v hermon Hermon device
  121. * @v hcr HCA command registers
  122. * @ret rc Return status code
  123. */
  124. static int hermon_cmd_wait ( struct hermon *hermon,
  125. struct hermonprm_hca_command_register *hcr ) {
  126. unsigned int wait;
  127. for ( wait = HERMON_HCR_MAX_WAIT_MS ; wait ; wait-- ) {
  128. hcr->u.dwords[6] =
  129. readl ( hermon->config + HERMON_HCR_REG ( 6 ) );
  130. if ( ( MLX_GET ( hcr, go ) == 0 ) &&
  131. ( MLX_GET ( hcr, t ) == hermon->toggle ) )
  132. return 0;
  133. mdelay ( 1 );
  134. }
  135. return -EBUSY;
  136. }
  137. /**
  138. * Issue HCA command
  139. *
  140. * @v hermon Hermon device
  141. * @v command Command opcode, flags and input/output lengths
  142. * @v op_mod Opcode modifier (0 if no modifier applicable)
  143. * @v in Input parameters
  144. * @v in_mod Input modifier (0 if no modifier applicable)
  145. * @v out Output parameters
  146. * @ret rc Return status code
  147. */
  148. static int hermon_cmd ( struct hermon *hermon, unsigned long command,
  149. unsigned int op_mod, const void *in,
  150. unsigned int in_mod, void *out ) {
  151. struct hermonprm_hca_command_register hcr;
  152. unsigned int opcode = HERMON_HCR_OPCODE ( command );
  153. size_t in_len = HERMON_HCR_IN_LEN ( command );
  154. size_t out_len = HERMON_HCR_OUT_LEN ( command );
  155. void *in_buffer;
  156. void *out_buffer;
  157. unsigned int status;
  158. unsigned int i;
  159. int rc;
  160. assert ( in_len <= HERMON_MBOX_SIZE );
  161. assert ( out_len <= HERMON_MBOX_SIZE );
  162. DBGC2 ( hermon, "Hermon %p command %02x in %zx%s out %zx%s\n",
  163. hermon, opcode, in_len,
  164. ( ( command & HERMON_HCR_IN_MBOX ) ? "(mbox)" : "" ), out_len,
  165. ( ( command & HERMON_HCR_OUT_MBOX ) ? "(mbox)" : "" ) );
  166. /* Check that HCR is free */
  167. if ( ( rc = hermon_cmd_wait ( hermon, &hcr ) ) != 0 ) {
  168. DBGC ( hermon, "Hermon %p command interface locked\n",
  169. hermon );
  170. return rc;
  171. }
  172. /* Flip HCR toggle */
  173. hermon->toggle = ( 1 - hermon->toggle );
  174. /* Prepare HCR */
  175. memset ( &hcr, 0, sizeof ( hcr ) );
  176. in_buffer = &hcr.u.dwords[0];
  177. if ( in_len && ( command & HERMON_HCR_IN_MBOX ) ) {
  178. memset ( hermon->mailbox_in, 0, HERMON_MBOX_SIZE );
  179. in_buffer = hermon->mailbox_in;
  180. MLX_FILL_H ( &hcr, 0, in_param_h, virt_to_bus ( in_buffer ) );
  181. MLX_FILL_1 ( &hcr, 1, in_param_l, virt_to_bus ( in_buffer ) );
  182. }
  183. memcpy ( in_buffer, in, in_len );
  184. MLX_FILL_1 ( &hcr, 2, input_modifier, in_mod );
  185. out_buffer = &hcr.u.dwords[3];
  186. if ( out_len && ( command & HERMON_HCR_OUT_MBOX ) ) {
  187. out_buffer = hermon->mailbox_out;
  188. MLX_FILL_H ( &hcr, 3, out_param_h,
  189. virt_to_bus ( out_buffer ) );
  190. MLX_FILL_1 ( &hcr, 4, out_param_l,
  191. virt_to_bus ( out_buffer ) );
  192. }
  193. MLX_FILL_4 ( &hcr, 6,
  194. opcode, opcode,
  195. opcode_modifier, op_mod,
  196. go, 1,
  197. t, hermon->toggle );
  198. DBGC ( hermon, "Hermon %p issuing command %04x\n",
  199. hermon, opcode );
  200. DBGC2_HDA ( hermon, virt_to_phys ( hermon->config + HERMON_HCR_BASE ),
  201. &hcr, sizeof ( hcr ) );
  202. if ( in_len && ( command & HERMON_HCR_IN_MBOX ) ) {
  203. DBGC2 ( hermon, "Input mailbox:\n" );
  204. DBGC2_HDA ( hermon, virt_to_phys ( in_buffer ), in_buffer,
  205. ( ( in_len < 512 ) ? in_len : 512 ) );
  206. }
  207. /* Issue command */
  208. for ( i = 0 ; i < ( sizeof ( hcr ) / sizeof ( hcr.u.dwords[0] ) ) ;
  209. i++ ) {
  210. writel ( hcr.u.dwords[i],
  211. hermon->config + HERMON_HCR_REG ( i ) );
  212. barrier();
  213. }
  214. /* Wait for command completion */
  215. if ( ( rc = hermon_cmd_wait ( hermon, &hcr ) ) != 0 ) {
  216. DBGC ( hermon, "Hermon %p timed out waiting for command:\n",
  217. hermon );
  218. DBGC_HDA ( hermon,
  219. virt_to_phys ( hermon->config + HERMON_HCR_BASE ),
  220. &hcr, sizeof ( hcr ) );
  221. return rc;
  222. }
  223. /* Check command status */
  224. status = MLX_GET ( &hcr, status );
  225. if ( status != 0 ) {
  226. DBGC ( hermon, "Hermon %p command failed with status %02x:\n",
  227. hermon, status );
  228. DBGC_HDA ( hermon,
  229. virt_to_phys ( hermon->config + HERMON_HCR_BASE ),
  230. &hcr, sizeof ( hcr ) );
  231. return -EIO;
  232. }
  233. /* Read output parameters, if any */
  234. hcr.u.dwords[3] = readl ( hermon->config + HERMON_HCR_REG ( 3 ) );
  235. hcr.u.dwords[4] = readl ( hermon->config + HERMON_HCR_REG ( 4 ) );
  236. memcpy ( out, out_buffer, out_len );
  237. if ( out_len ) {
  238. DBGC2 ( hermon, "Output%s:\n",
  239. ( command & HERMON_HCR_OUT_MBOX ) ? " mailbox" : "" );
  240. DBGC2_HDA ( hermon, virt_to_phys ( out_buffer ), out_buffer,
  241. ( ( out_len < 512 ) ? out_len : 512 ) );
  242. }
  243. return 0;
  244. }
  245. static inline int
  246. hermon_cmd_query_dev_cap ( struct hermon *hermon,
  247. struct hermonprm_query_dev_cap *dev_cap ) {
  248. return hermon_cmd ( hermon,
  249. HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_DEV_CAP,
  250. 1, sizeof ( *dev_cap ) ),
  251. 0, NULL, 0, dev_cap );
  252. }
  253. static inline int
  254. hermon_cmd_query_fw ( struct hermon *hermon, struct hermonprm_query_fw *fw ) {
  255. return hermon_cmd ( hermon,
  256. HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_FW,
  257. 1, sizeof ( *fw ) ),
  258. 0, NULL, 0, fw );
  259. }
  260. static inline int
  261. hermon_cmd_init_hca ( struct hermon *hermon,
  262. const struct hermonprm_init_hca *init_hca ) {
  263. return hermon_cmd ( hermon,
  264. HERMON_HCR_IN_CMD ( HERMON_HCR_INIT_HCA,
  265. 1, sizeof ( *init_hca ) ),
  266. 0, init_hca, 0, NULL );
  267. }
  268. static inline int
  269. hermon_cmd_close_hca ( struct hermon *hermon ) {
  270. return hermon_cmd ( hermon,
  271. HERMON_HCR_VOID_CMD ( HERMON_HCR_CLOSE_HCA ),
  272. 0, NULL, 0, NULL );
  273. }
  274. static inline int
  275. hermon_cmd_init_port ( struct hermon *hermon, unsigned int port ) {
  276. return hermon_cmd ( hermon,
  277. HERMON_HCR_VOID_CMD ( HERMON_HCR_INIT_PORT ),
  278. 0, NULL, port, NULL );
  279. }
  280. static inline int
  281. hermon_cmd_close_port ( struct hermon *hermon, unsigned int port ) {
  282. return hermon_cmd ( hermon,
  283. HERMON_HCR_VOID_CMD ( HERMON_HCR_CLOSE_PORT ),
  284. 0, NULL, port, NULL );
  285. }
  286. static inline int
  287. hermon_cmd_set_port ( struct hermon *hermon, int is_ethernet,
  288. unsigned int port_selector,
  289. const union hermonprm_set_port *set_port ) {
  290. return hermon_cmd ( hermon,
  291. HERMON_HCR_IN_CMD ( HERMON_HCR_SET_PORT,
  292. 1, sizeof ( *set_port ) ),
  293. is_ethernet, set_port, port_selector, NULL );
  294. }
  295. static inline int
  296. hermon_cmd_sw2hw_mpt ( struct hermon *hermon, unsigned int index,
  297. const struct hermonprm_mpt *mpt ) {
  298. return hermon_cmd ( hermon,
  299. HERMON_HCR_IN_CMD ( HERMON_HCR_SW2HW_MPT,
  300. 1, sizeof ( *mpt ) ),
  301. 0, mpt, index, NULL );
  302. }
  303. static inline int
  304. hermon_cmd_write_mtt ( struct hermon *hermon,
  305. const struct hermonprm_write_mtt *write_mtt ) {
  306. return hermon_cmd ( hermon,
  307. HERMON_HCR_IN_CMD ( HERMON_HCR_WRITE_MTT,
  308. 1, sizeof ( *write_mtt ) ),
  309. 0, write_mtt, 1, NULL );
  310. }
  311. static inline int
  312. hermon_cmd_map_eq ( struct hermon *hermon, unsigned long index_map,
  313. const struct hermonprm_event_mask *mask ) {
  314. return hermon_cmd ( hermon,
  315. HERMON_HCR_IN_CMD ( HERMON_HCR_MAP_EQ,
  316. 0, sizeof ( *mask ) ),
  317. 0, mask, index_map, NULL );
  318. }
  319. static inline int
  320. hermon_cmd_sw2hw_eq ( struct hermon *hermon, unsigned int index,
  321. const struct hermonprm_eqc *eqctx ) {
  322. return hermon_cmd ( hermon,
  323. HERMON_HCR_IN_CMD ( HERMON_HCR_SW2HW_EQ,
  324. 1, sizeof ( *eqctx ) ),
  325. 0, eqctx, index, NULL );
  326. }
  327. static inline int
  328. hermon_cmd_hw2sw_eq ( struct hermon *hermon, unsigned int index,
  329. struct hermonprm_eqc *eqctx ) {
  330. return hermon_cmd ( hermon,
  331. HERMON_HCR_OUT_CMD ( HERMON_HCR_HW2SW_EQ,
  332. 1, sizeof ( *eqctx ) ),
  333. 1, NULL, index, eqctx );
  334. }
  335. static inline int
  336. hermon_cmd_query_eq ( struct hermon *hermon, unsigned int index,
  337. struct hermonprm_eqc *eqctx ) {
  338. return hermon_cmd ( hermon,
  339. HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_EQ,
  340. 1, sizeof ( *eqctx ) ),
  341. 0, NULL, index, eqctx );
  342. }
  343. static inline int
  344. hermon_cmd_sw2hw_cq ( struct hermon *hermon, unsigned long cqn,
  345. const struct hermonprm_completion_queue_context *cqctx ){
  346. return hermon_cmd ( hermon,
  347. HERMON_HCR_IN_CMD ( HERMON_HCR_SW2HW_CQ,
  348. 1, sizeof ( *cqctx ) ),
  349. 0, cqctx, cqn, NULL );
  350. }
  351. static inline int
  352. hermon_cmd_hw2sw_cq ( struct hermon *hermon, unsigned long cqn,
  353. struct hermonprm_completion_queue_context *cqctx ) {
  354. return hermon_cmd ( hermon,
  355. HERMON_HCR_OUT_CMD ( HERMON_HCR_HW2SW_CQ,
  356. 1, sizeof ( *cqctx ) ),
  357. 0, NULL, cqn, cqctx );
  358. }
  359. static inline int
  360. hermon_cmd_query_cq ( struct hermon *hermon, unsigned long cqn,
  361. struct hermonprm_completion_queue_context *cqctx ) {
  362. return hermon_cmd ( hermon,
  363. HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_CQ,
  364. 1, sizeof ( *cqctx ) ),
  365. 0, NULL, cqn, cqctx );
  366. }
  367. static inline int
  368. hermon_cmd_rst2init_qp ( struct hermon *hermon, unsigned long qpn,
  369. const struct hermonprm_qp_ee_state_transitions *ctx ){
  370. return hermon_cmd ( hermon,
  371. HERMON_HCR_IN_CMD ( HERMON_HCR_RST2INIT_QP,
  372. 1, sizeof ( *ctx ) ),
  373. 0, ctx, qpn, NULL );
  374. }
  375. static inline int
  376. hermon_cmd_init2rtr_qp ( struct hermon *hermon, unsigned long qpn,
  377. const struct hermonprm_qp_ee_state_transitions *ctx ){
  378. return hermon_cmd ( hermon,
  379. HERMON_HCR_IN_CMD ( HERMON_HCR_INIT2RTR_QP,
  380. 1, sizeof ( *ctx ) ),
  381. 0, ctx, qpn, NULL );
  382. }
  383. static inline int
  384. hermon_cmd_rtr2rts_qp ( struct hermon *hermon, unsigned long qpn,
  385. const struct hermonprm_qp_ee_state_transitions *ctx ) {
  386. return hermon_cmd ( hermon,
  387. HERMON_HCR_IN_CMD ( HERMON_HCR_RTR2RTS_QP,
  388. 1, sizeof ( *ctx ) ),
  389. 0, ctx, qpn, NULL );
  390. }
  391. static inline int
  392. hermon_cmd_rts2rts_qp ( struct hermon *hermon, unsigned long qpn,
  393. const struct hermonprm_qp_ee_state_transitions *ctx ) {
  394. return hermon_cmd ( hermon,
  395. HERMON_HCR_IN_CMD ( HERMON_HCR_RTS2RTS_QP,
  396. 1, sizeof ( *ctx ) ),
  397. 0, ctx, qpn, NULL );
  398. }
  399. static inline int
  400. hermon_cmd_2rst_qp ( struct hermon *hermon, unsigned long qpn ) {
  401. return hermon_cmd ( hermon,
  402. HERMON_HCR_VOID_CMD ( HERMON_HCR_2RST_QP ),
  403. 0x03, NULL, qpn, NULL );
  404. }
  405. static inline int
  406. hermon_cmd_query_qp ( struct hermon *hermon, unsigned long qpn,
  407. struct hermonprm_qp_ee_state_transitions *ctx ) {
  408. return hermon_cmd ( hermon,
  409. HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_QP,
  410. 1, sizeof ( *ctx ) ),
  411. 0, NULL, qpn, ctx );
  412. }
  413. static inline int
  414. hermon_cmd_conf_special_qp ( struct hermon *hermon, unsigned int internal_qps,
  415. unsigned long base_qpn ) {
  416. return hermon_cmd ( hermon,
  417. HERMON_HCR_VOID_CMD ( HERMON_HCR_CONF_SPECIAL_QP ),
  418. internal_qps, NULL, base_qpn, NULL );
  419. }
  420. static inline int
  421. hermon_cmd_mad_ifc ( struct hermon *hermon, unsigned int port,
  422. union hermonprm_mad *mad ) {
  423. return hermon_cmd ( hermon,
  424. HERMON_HCR_INOUT_CMD ( HERMON_HCR_MAD_IFC,
  425. 1, sizeof ( *mad ),
  426. 1, sizeof ( *mad ) ),
  427. 0x03, mad, port, mad );
  428. }
  429. static inline int
  430. hermon_cmd_read_mcg ( struct hermon *hermon, unsigned int index,
  431. struct hermonprm_mcg_entry *mcg ) {
  432. return hermon_cmd ( hermon,
  433. HERMON_HCR_OUT_CMD ( HERMON_HCR_READ_MCG,
  434. 1, sizeof ( *mcg ) ),
  435. 0, NULL, index, mcg );
  436. }
  437. static inline int
  438. hermon_cmd_write_mcg ( struct hermon *hermon, unsigned int index,
  439. const struct hermonprm_mcg_entry *mcg ) {
  440. return hermon_cmd ( hermon,
  441. HERMON_HCR_IN_CMD ( HERMON_HCR_WRITE_MCG,
  442. 1, sizeof ( *mcg ) ),
  443. 0, mcg, index, NULL );
  444. }
  445. static inline int
  446. hermon_cmd_mgid_hash ( struct hermon *hermon, const union ib_gid *gid,
  447. struct hermonprm_mgm_hash *hash ) {
  448. return hermon_cmd ( hermon,
  449. HERMON_HCR_INOUT_CMD ( HERMON_HCR_MGID_HASH,
  450. 1, sizeof ( *gid ),
  451. 0, sizeof ( *hash ) ),
  452. 0, gid, 0, hash );
  453. }
  454. static inline int
  455. hermon_cmd_mod_stat_cfg ( struct hermon *hermon, unsigned int mode,
  456. unsigned int input_mod,
  457. struct hermonprm_scalar_parameter *portion ) {
  458. return hermon_cmd ( hermon,
  459. HERMON_HCR_INOUT_CMD ( HERMON_HCR_MOD_STAT_CFG,
  460. 0, sizeof ( *portion ),
  461. 0, sizeof ( *portion ) ),
  462. mode, portion, input_mod, portion );
  463. }
  464. static inline int
  465. hermon_cmd_query_port ( struct hermon *hermon, unsigned int port,
  466. struct hermonprm_query_port_cap *query_port ) {
  467. return hermon_cmd ( hermon,
  468. HERMON_HCR_OUT_CMD ( HERMON_HCR_QUERY_PORT,
  469. 1, sizeof ( *query_port ) ),
  470. 0, NULL, port, query_port );
  471. }
  472. static inline int
  473. hermon_cmd_sense_port ( struct hermon *hermon, unsigned int port,
  474. struct hermonprm_sense_port *port_type ) {
  475. return hermon_cmd ( hermon,
  476. HERMON_HCR_OUT_CMD ( HERMON_HCR_SENSE_PORT,
  477. 0, sizeof ( *port_type ) ),
  478. 0, NULL, port, port_type );
  479. }
  480. static inline int
  481. hermon_cmd_run_fw ( struct hermon *hermon ) {
  482. return hermon_cmd ( hermon,
  483. HERMON_HCR_VOID_CMD ( HERMON_HCR_RUN_FW ),
  484. 0, NULL, 0, NULL );
  485. }
  486. static inline int
  487. hermon_cmd_unmap_icm ( struct hermon *hermon, unsigned int page_count,
  488. const struct hermonprm_scalar_parameter *offset ) {
  489. return hermon_cmd ( hermon,
  490. HERMON_HCR_IN_CMD ( HERMON_HCR_UNMAP_ICM,
  491. 0, sizeof ( *offset ) ),
  492. 0, offset, page_count, NULL );
  493. }
  494. static inline int
  495. hermon_cmd_map_icm ( struct hermon *hermon,
  496. const struct hermonprm_virtual_physical_mapping *map ) {
  497. return hermon_cmd ( hermon,
  498. HERMON_HCR_IN_CMD ( HERMON_HCR_MAP_ICM,
  499. 1, sizeof ( *map ) ),
  500. 0, map, 1, NULL );
  501. }
  502. static inline int
  503. hermon_cmd_unmap_icm_aux ( struct hermon *hermon ) {
  504. return hermon_cmd ( hermon,
  505. HERMON_HCR_VOID_CMD ( HERMON_HCR_UNMAP_ICM_AUX ),
  506. 0, NULL, 0, NULL );
  507. }
  508. static inline int
  509. hermon_cmd_map_icm_aux ( struct hermon *hermon,
  510. const struct hermonprm_virtual_physical_mapping *map ) {
  511. return hermon_cmd ( hermon,
  512. HERMON_HCR_IN_CMD ( HERMON_HCR_MAP_ICM_AUX,
  513. 1, sizeof ( *map ) ),
  514. 0, map, 1, NULL );
  515. }
  516. static inline int
  517. hermon_cmd_set_icm_size ( struct hermon *hermon,
  518. const struct hermonprm_scalar_parameter *icm_size,
  519. struct hermonprm_scalar_parameter *icm_aux_size ) {
  520. return hermon_cmd ( hermon,
  521. HERMON_HCR_INOUT_CMD ( HERMON_HCR_SET_ICM_SIZE,
  522. 0, sizeof ( *icm_size ),
  523. 0, sizeof (*icm_aux_size) ),
  524. 0, icm_size, 0, icm_aux_size );
  525. }
  526. static inline int
  527. hermon_cmd_unmap_fa ( struct hermon *hermon ) {
  528. return hermon_cmd ( hermon,
  529. HERMON_HCR_VOID_CMD ( HERMON_HCR_UNMAP_FA ),
  530. 0, NULL, 0, NULL );
  531. }
  532. static inline int
  533. hermon_cmd_map_fa ( struct hermon *hermon,
  534. const struct hermonprm_virtual_physical_mapping *map ) {
  535. return hermon_cmd ( hermon,
  536. HERMON_HCR_IN_CMD ( HERMON_HCR_MAP_FA,
  537. 1, sizeof ( *map ) ),
  538. 0, map, 1, NULL );
  539. }
  540. /***************************************************************************
  541. *
  542. * Memory translation table operations
  543. *
  544. ***************************************************************************
  545. */
  546. /**
  547. * Allocate MTT entries
  548. *
  549. * @v hermon Hermon device
  550. * @v memory Memory to map into MTT
  551. * @v len Length of memory to map
  552. * @v mtt MTT descriptor to fill in
  553. * @ret rc Return status code
  554. */
  555. static int hermon_alloc_mtt ( struct hermon *hermon,
  556. const void *memory, size_t len,
  557. struct hermon_mtt *mtt ) {
  558. struct hermonprm_write_mtt write_mtt;
  559. physaddr_t start;
  560. physaddr_t addr;
  561. unsigned int page_offset;
  562. unsigned int num_pages;
  563. int mtt_offset;
  564. unsigned int mtt_base_addr;
  565. unsigned int i;
  566. int rc;
  567. /* Find available MTT entries */
  568. start = virt_to_phys ( memory );
  569. page_offset = ( start & ( HERMON_PAGE_SIZE - 1 ) );
  570. start -= page_offset;
  571. len += page_offset;
  572. num_pages = ( ( len + HERMON_PAGE_SIZE - 1 ) / HERMON_PAGE_SIZE );
  573. mtt_offset = hermon_bitmask_alloc ( hermon->mtt_inuse, HERMON_MAX_MTTS,
  574. num_pages );
  575. if ( mtt_offset < 0 ) {
  576. DBGC ( hermon, "Hermon %p could not allocate %d MTT entries\n",
  577. hermon, num_pages );
  578. rc = mtt_offset;
  579. goto err_mtt_offset;
  580. }
  581. mtt_base_addr = ( ( hermon->cap.reserved_mtts + mtt_offset ) *
  582. hermon->cap.mtt_entry_size );
  583. addr = start;
  584. /* Fill in MTT structure */
  585. mtt->mtt_offset = mtt_offset;
  586. mtt->num_pages = num_pages;
  587. mtt->mtt_base_addr = mtt_base_addr;
  588. mtt->page_offset = page_offset;
  589. /* Construct and issue WRITE_MTT commands */
  590. for ( i = 0 ; i < num_pages ; i++ ) {
  591. memset ( &write_mtt, 0, sizeof ( write_mtt ) );
  592. MLX_FILL_1 ( &write_mtt.mtt_base_addr, 1,
  593. value, mtt_base_addr );
  594. MLX_FILL_H ( &write_mtt.mtt, 0, ptag_h, addr );
  595. MLX_FILL_2 ( &write_mtt.mtt, 1,
  596. p, 1,
  597. ptag_l, ( addr >> 3 ) );
  598. if ( ( rc = hermon_cmd_write_mtt ( hermon,
  599. &write_mtt ) ) != 0 ) {
  600. DBGC ( hermon, "Hermon %p could not write MTT at %x\n",
  601. hermon, mtt_base_addr );
  602. goto err_write_mtt;
  603. }
  604. addr += HERMON_PAGE_SIZE;
  605. mtt_base_addr += hermon->cap.mtt_entry_size;
  606. }
  607. DBGC ( hermon, "Hermon %p MTT entries [%#x,%#x] for "
  608. "[%08lx,%08lx,%08lx,%08lx)\n", hermon, mtt->mtt_offset,
  609. ( mtt->mtt_offset + mtt->num_pages - 1 ), start,
  610. ( start + page_offset ), ( start + len ), addr );
  611. return 0;
  612. err_write_mtt:
  613. hermon_bitmask_free ( hermon->mtt_inuse, mtt_offset, num_pages );
  614. err_mtt_offset:
  615. return rc;
  616. }
  617. /**
  618. * Free MTT entries
  619. *
  620. * @v hermon Hermon device
  621. * @v mtt MTT descriptor
  622. */
  623. static void hermon_free_mtt ( struct hermon *hermon,
  624. struct hermon_mtt *mtt ) {
  625. DBGC ( hermon, "Hermon %p MTT entries [%#x,%#x] freed\n",
  626. hermon, mtt->mtt_offset,
  627. ( mtt->mtt_offset + mtt->num_pages - 1 ) );
  628. hermon_bitmask_free ( hermon->mtt_inuse, mtt->mtt_offset,
  629. mtt->num_pages );
  630. }
  631. /***************************************************************************
  632. *
  633. * Static configuration operations
  634. *
  635. ***************************************************************************
  636. */
  637. /**
  638. * Calculate offset within static configuration
  639. *
  640. * @v field Field
  641. * @ret offset Offset
  642. */
  643. #define HERMON_MOD_STAT_CFG_OFFSET( field ) \
  644. ( ( MLX_BIT_OFFSET ( struct hermonprm_mod_stat_cfg_st, field ) / 8 ) \
  645. & ~( sizeof ( struct hermonprm_scalar_parameter ) - 1 ) )
  646. /**
  647. * Query or modify static configuration
  648. *
  649. * @v hermon Hermon device
  650. * @v port Port
  651. * @v mode Command mode
  652. * @v offset Offset within static configuration
  653. * @v stat_cfg Static configuration
  654. * @ret rc Return status code
  655. */
  656. static int hermon_mod_stat_cfg ( struct hermon *hermon, unsigned int port,
  657. unsigned int mode, unsigned int offset,
  658. struct hermonprm_mod_stat_cfg *stat_cfg ) {
  659. struct hermonprm_scalar_parameter *portion =
  660. ( ( void * ) &stat_cfg->u.bytes[offset] );
  661. struct hermonprm_mod_stat_cfg_input_mod mod;
  662. int rc;
  663. /* Sanity check */
  664. assert ( ( offset % sizeof ( *portion ) ) == 0 );
  665. /* Construct input modifier */
  666. memset ( &mod, 0, sizeof ( mod ) );
  667. MLX_FILL_2 ( &mod, 0,
  668. portnum, port,
  669. offset, offset );
  670. /* Issue command */
  671. if ( ( rc = hermon_cmd_mod_stat_cfg ( hermon, mode,
  672. be32_to_cpu ( mod.u.dwords[0] ),
  673. portion ) ) != 0 )
  674. return rc;
  675. return 0;
  676. }
  677. /***************************************************************************
  678. *
  679. * MAD operations
  680. *
  681. ***************************************************************************
  682. */
  683. /**
  684. * Issue management datagram
  685. *
  686. * @v ibdev Infiniband device
  687. * @v mad Management datagram
  688. * @ret rc Return status code
  689. */
  690. static int hermon_mad ( struct ib_device *ibdev, union ib_mad *mad ) {
  691. struct hermon *hermon = ib_get_drvdata ( ibdev );
  692. union hermonprm_mad mad_ifc;
  693. int rc;
  694. linker_assert ( sizeof ( *mad ) == sizeof ( mad_ifc.mad ),
  695. mad_size_mismatch );
  696. /* Copy in request packet */
  697. memcpy ( &mad_ifc.mad, mad, sizeof ( mad_ifc.mad ) );
  698. /* Issue MAD */
  699. if ( ( rc = hermon_cmd_mad_ifc ( hermon, ibdev->port,
  700. &mad_ifc ) ) != 0 ) {
  701. DBGC ( hermon, "Hermon %p port %d could not issue MAD IFC: "
  702. "%s\n", hermon, ibdev->port, strerror ( rc ) );
  703. return rc;
  704. }
  705. /* Copy out reply packet */
  706. memcpy ( mad, &mad_ifc.mad, sizeof ( *mad ) );
  707. if ( mad->hdr.status != 0 ) {
  708. DBGC ( hermon, "Hermon %p port %d MAD IFC status %04x\n",
  709. hermon, ibdev->port, ntohs ( mad->hdr.status ) );
  710. return -EIO;
  711. }
  712. return 0;
  713. }
  714. /***************************************************************************
  715. *
  716. * Completion queue operations
  717. *
  718. ***************************************************************************
  719. */
  720. /**
  721. * Dump completion queue context (for debugging only)
  722. *
  723. * @v hermon Hermon device
  724. * @v cq Completion queue
  725. * @ret rc Return status code
  726. */
  727. static __attribute__ (( unused )) int
  728. hermon_dump_cqctx ( struct hermon *hermon, struct ib_completion_queue *cq ) {
  729. struct hermonprm_completion_queue_context cqctx;
  730. int rc;
  731. memset ( &cqctx, 0, sizeof ( cqctx ) );
  732. if ( ( rc = hermon_cmd_query_cq ( hermon, cq->cqn, &cqctx ) ) != 0 ) {
  733. DBGC ( hermon, "Hermon %p CQN %#lx QUERY_CQ failed: %s\n",
  734. hermon, cq->cqn, strerror ( rc ) );
  735. return rc;
  736. }
  737. DBGC ( hermon, "Hermon %p CQN %#lx context:\n", hermon, cq->cqn );
  738. DBGC_HDA ( hermon, 0, &cqctx, sizeof ( cqctx ) );
  739. return 0;
  740. }
  741. /**
  742. * Create completion queue
  743. *
  744. * @v ibdev Infiniband device
  745. * @v cq Completion queue
  746. * @ret rc Return status code
  747. */
  748. static int hermon_create_cq ( struct ib_device *ibdev,
  749. struct ib_completion_queue *cq ) {
  750. struct hermon *hermon = ib_get_drvdata ( ibdev );
  751. struct hermon_completion_queue *hermon_cq;
  752. struct hermonprm_completion_queue_context cqctx;
  753. int cqn_offset;
  754. unsigned int i;
  755. int rc;
  756. /* Find a free completion queue number */
  757. cqn_offset = hermon_bitmask_alloc ( hermon->cq_inuse,
  758. HERMON_MAX_CQS, 1 );
  759. if ( cqn_offset < 0 ) {
  760. DBGC ( hermon, "Hermon %p out of completion queues\n",
  761. hermon );
  762. rc = cqn_offset;
  763. goto err_cqn_offset;
  764. }
  765. cq->cqn = ( hermon->cap.reserved_cqs + cqn_offset );
  766. /* Allocate control structures */
  767. hermon_cq = zalloc ( sizeof ( *hermon_cq ) );
  768. if ( ! hermon_cq ) {
  769. rc = -ENOMEM;
  770. goto err_hermon_cq;
  771. }
  772. /* Allocate doorbell */
  773. hermon_cq->doorbell = malloc_dma ( sizeof ( hermon_cq->doorbell[0] ),
  774. sizeof ( hermon_cq->doorbell[0] ) );
  775. if ( ! hermon_cq->doorbell ) {
  776. rc = -ENOMEM;
  777. goto err_doorbell;
  778. }
  779. memset ( hermon_cq->doorbell, 0, sizeof ( hermon_cq->doorbell[0] ) );
  780. /* Allocate completion queue itself */
  781. hermon_cq->cqe_size = ( cq->num_cqes * sizeof ( hermon_cq->cqe[0] ) );
  782. hermon_cq->cqe = malloc_dma ( hermon_cq->cqe_size,
  783. sizeof ( hermon_cq->cqe[0] ) );
  784. if ( ! hermon_cq->cqe ) {
  785. rc = -ENOMEM;
  786. goto err_cqe;
  787. }
  788. memset ( hermon_cq->cqe, 0, hermon_cq->cqe_size );
  789. for ( i = 0 ; i < cq->num_cqes ; i++ ) {
  790. MLX_FILL_1 ( &hermon_cq->cqe[i].normal, 7, owner, 1 );
  791. }
  792. barrier();
  793. /* Allocate MTT entries */
  794. if ( ( rc = hermon_alloc_mtt ( hermon, hermon_cq->cqe,
  795. hermon_cq->cqe_size,
  796. &hermon_cq->mtt ) ) != 0 )
  797. goto err_alloc_mtt;
  798. /* Hand queue over to hardware */
  799. memset ( &cqctx, 0, sizeof ( cqctx ) );
  800. MLX_FILL_1 ( &cqctx, 0, st, 0xa /* "Event fired" */ );
  801. MLX_FILL_1 ( &cqctx, 2,
  802. page_offset, ( hermon_cq->mtt.page_offset >> 5 ) );
  803. MLX_FILL_2 ( &cqctx, 3,
  804. usr_page, HERMON_UAR_NON_EQ_PAGE,
  805. log_cq_size, fls ( cq->num_cqes - 1 ) );
  806. MLX_FILL_1 ( &cqctx, 5, c_eqn, hermon->eq.eqn );
  807. MLX_FILL_H ( &cqctx, 6, mtt_base_addr_h,
  808. hermon_cq->mtt.mtt_base_addr );
  809. MLX_FILL_1 ( &cqctx, 7, mtt_base_addr_l,
  810. ( hermon_cq->mtt.mtt_base_addr >> 3 ) );
  811. MLX_FILL_H ( &cqctx, 14, db_record_addr_h,
  812. virt_to_phys ( hermon_cq->doorbell ) );
  813. MLX_FILL_1 ( &cqctx, 15, db_record_addr_l,
  814. ( virt_to_phys ( hermon_cq->doorbell ) >> 3 ) );
  815. if ( ( rc = hermon_cmd_sw2hw_cq ( hermon, cq->cqn, &cqctx ) ) != 0 ) {
  816. DBGC ( hermon, "Hermon %p CQN %#lx SW2HW_CQ failed: %s\n",
  817. hermon, cq->cqn, strerror ( rc ) );
  818. goto err_sw2hw_cq;
  819. }
  820. DBGC ( hermon, "Hermon %p CQN %#lx ring [%08lx,%08lx), doorbell "
  821. "%08lx\n", hermon, cq->cqn, virt_to_phys ( hermon_cq->cqe ),
  822. ( virt_to_phys ( hermon_cq->cqe ) + hermon_cq->cqe_size ),
  823. virt_to_phys ( hermon_cq->doorbell ) );
  824. ib_cq_set_drvdata ( cq, hermon_cq );
  825. return 0;
  826. err_sw2hw_cq:
  827. hermon_free_mtt ( hermon, &hermon_cq->mtt );
  828. err_alloc_mtt:
  829. free_dma ( hermon_cq->cqe, hermon_cq->cqe_size );
  830. err_cqe:
  831. free_dma ( hermon_cq->doorbell, sizeof ( hermon_cq->doorbell[0] ) );
  832. err_doorbell:
  833. free ( hermon_cq );
  834. err_hermon_cq:
  835. hermon_bitmask_free ( hermon->cq_inuse, cqn_offset, 1 );
  836. err_cqn_offset:
  837. return rc;
  838. }
  839. /**
  840. * Destroy completion queue
  841. *
  842. * @v ibdev Infiniband device
  843. * @v cq Completion queue
  844. */
  845. static void hermon_destroy_cq ( struct ib_device *ibdev,
  846. struct ib_completion_queue *cq ) {
  847. struct hermon *hermon = ib_get_drvdata ( ibdev );
  848. struct hermon_completion_queue *hermon_cq = ib_cq_get_drvdata ( cq );
  849. struct hermonprm_completion_queue_context cqctx;
  850. int cqn_offset;
  851. int rc;
  852. /* Take ownership back from hardware */
  853. if ( ( rc = hermon_cmd_hw2sw_cq ( hermon, cq->cqn, &cqctx ) ) != 0 ) {
  854. DBGC ( hermon, "Hermon %p CQN %#lx FATAL HW2SW_CQ failed: "
  855. "%s\n", hermon, cq->cqn, strerror ( rc ) );
  856. /* Leak memory and return; at least we avoid corruption */
  857. return;
  858. }
  859. /* Free MTT entries */
  860. hermon_free_mtt ( hermon, &hermon_cq->mtt );
  861. /* Free memory */
  862. free_dma ( hermon_cq->cqe, hermon_cq->cqe_size );
  863. free_dma ( hermon_cq->doorbell, sizeof ( hermon_cq->doorbell[0] ) );
  864. free ( hermon_cq );
  865. /* Mark queue number as free */
  866. cqn_offset = ( cq->cqn - hermon->cap.reserved_cqs );
  867. hermon_bitmask_free ( hermon->cq_inuse, cqn_offset, 1 );
  868. ib_cq_set_drvdata ( cq, NULL );
  869. }
  870. /***************************************************************************
  871. *
  872. * Queue pair operations
  873. *
  874. ***************************************************************************
  875. */
  876. /**
  877. * Assign queue pair number
  878. *
  879. * @v ibdev Infiniband device
  880. * @v qp Queue pair
  881. * @ret rc Return status code
  882. */
  883. static int hermon_alloc_qpn ( struct ib_device *ibdev,
  884. struct ib_queue_pair *qp ) {
  885. struct hermon *hermon = ib_get_drvdata ( ibdev );
  886. unsigned int port_offset;
  887. int qpn_offset;
  888. /* Calculate queue pair number */
  889. port_offset = ( ibdev->port - HERMON_PORT_BASE );
  890. switch ( qp->type ) {
  891. case IB_QPT_SMI:
  892. qp->qpn = ( hermon->special_qpn_base + port_offset );
  893. return 0;
  894. case IB_QPT_GSI:
  895. qp->qpn = ( hermon->special_qpn_base + 2 + port_offset );
  896. return 0;
  897. case IB_QPT_UD:
  898. case IB_QPT_RC:
  899. case IB_QPT_ETH:
  900. /* Find a free queue pair number */
  901. qpn_offset = hermon_bitmask_alloc ( hermon->qp_inuse,
  902. HERMON_MAX_QPS, 1 );
  903. if ( qpn_offset < 0 ) {
  904. DBGC ( hermon, "Hermon %p out of queue pairs\n",
  905. hermon );
  906. return qpn_offset;
  907. }
  908. qp->qpn = ( ( random() & HERMON_QPN_RANDOM_MASK ) |
  909. ( hermon->qpn_base + qpn_offset ) );
  910. return 0;
  911. default:
  912. DBGC ( hermon, "Hermon %p unsupported QP type %d\n",
  913. hermon, qp->type );
  914. return -ENOTSUP;
  915. }
  916. }
  917. /**
  918. * Free queue pair number
  919. *
  920. * @v ibdev Infiniband device
  921. * @v qp Queue pair
  922. */
  923. static void hermon_free_qpn ( struct ib_device *ibdev,
  924. struct ib_queue_pair *qp ) {
  925. struct hermon *hermon = ib_get_drvdata ( ibdev );
  926. int qpn_offset;
  927. qpn_offset = ( ( qp->qpn & ~HERMON_QPN_RANDOM_MASK )
  928. - hermon->qpn_base );
  929. if ( qpn_offset >= 0 )
  930. hermon_bitmask_free ( hermon->qp_inuse, qpn_offset, 1 );
  931. }
  932. /**
  933. * Calculate transmission rate
  934. *
  935. * @v av Address vector
  936. * @ret hermon_rate Hermon rate
  937. */
  938. static unsigned int hermon_rate ( struct ib_address_vector *av ) {
  939. return ( ( ( av->rate >= IB_RATE_2_5 ) && ( av->rate <= IB_RATE_120 ) )
  940. ? ( av->rate + 5 ) : 0 );
  941. }
  942. /**
  943. * Calculate schedule queue
  944. *
  945. * @v ibdev Infiniband device
  946. * @v qp Queue pair
  947. * @ret sched_queue Schedule queue
  948. */
  949. static unsigned int hermon_sched_queue ( struct ib_device *ibdev,
  950. struct ib_queue_pair *qp ) {
  951. return ( ( ( qp->type == IB_QPT_SMI ) ?
  952. HERMON_SCHED_QP0 : HERMON_SCHED_DEFAULT ) |
  953. ( ( ibdev->port - 1 ) << 6 ) );
  954. }
  955. /** Queue pair transport service type map */
  956. static uint8_t hermon_qp_st[] = {
  957. [IB_QPT_SMI] = HERMON_ST_MLX,
  958. [IB_QPT_GSI] = HERMON_ST_MLX,
  959. [IB_QPT_UD] = HERMON_ST_UD,
  960. [IB_QPT_RC] = HERMON_ST_RC,
  961. [IB_QPT_ETH] = HERMON_ST_MLX,
  962. };
  963. /**
  964. * Dump queue pair context (for debugging only)
  965. *
  966. * @v hermon Hermon device
  967. * @v qp Queue pair
  968. * @ret rc Return status code
  969. */
  970. static __attribute__ (( unused )) int
  971. hermon_dump_qpctx ( struct hermon *hermon, struct ib_queue_pair *qp ) {
  972. struct hermonprm_qp_ee_state_transitions qpctx;
  973. int rc;
  974. memset ( &qpctx, 0, sizeof ( qpctx ) );
  975. if ( ( rc = hermon_cmd_query_qp ( hermon, qp->qpn, &qpctx ) ) != 0 ) {
  976. DBGC ( hermon, "Hermon %p QPN %#lx QUERY_QP failed: %s\n",
  977. hermon, qp->qpn, strerror ( rc ) );
  978. return rc;
  979. }
  980. DBGC ( hermon, "Hermon %p QPN %#lx context:\n", hermon, qp->qpn );
  981. DBGC_HDA ( hermon, 0, &qpctx.u.dwords[2], ( sizeof ( qpctx ) - 8 ) );
  982. return 0;
  983. }
  984. /**
  985. * Create queue pair
  986. *
  987. * @v ibdev Infiniband device
  988. * @v qp Queue pair
  989. * @ret rc Return status code
  990. */
  991. static int hermon_create_qp ( struct ib_device *ibdev,
  992. struct ib_queue_pair *qp ) {
  993. struct hermon *hermon = ib_get_drvdata ( ibdev );
  994. struct hermon_queue_pair *hermon_qp;
  995. struct hermonprm_qp_ee_state_transitions qpctx;
  996. struct hermonprm_wqe_segment_data_ptr *data;
  997. unsigned int i;
  998. int rc;
  999. /* Calculate queue pair number */
  1000. if ( ( rc = hermon_alloc_qpn ( ibdev, qp ) ) != 0 )
  1001. goto err_alloc_qpn;
  1002. /* Allocate control structures */
  1003. hermon_qp = zalloc ( sizeof ( *hermon_qp ) );
  1004. if ( ! hermon_qp ) {
  1005. rc = -ENOMEM;
  1006. goto err_hermon_qp;
  1007. }
  1008. /* Allocate doorbells */
  1009. hermon_qp->recv.doorbell =
  1010. malloc_dma ( sizeof ( hermon_qp->recv.doorbell[0] ),
  1011. sizeof ( hermon_qp->recv.doorbell[0] ) );
  1012. if ( ! hermon_qp->recv.doorbell ) {
  1013. rc = -ENOMEM;
  1014. goto err_recv_doorbell;
  1015. }
  1016. memset ( hermon_qp->recv.doorbell, 0,
  1017. sizeof ( hermon_qp->recv.doorbell[0] ) );
  1018. hermon_qp->send.doorbell =
  1019. ( hermon->uar + HERMON_UAR_NON_EQ_PAGE * HERMON_PAGE_SIZE +
  1020. HERMON_DB_POST_SND_OFFSET );
  1021. /* Allocate work queue buffer */
  1022. hermon_qp->send.num_wqes = ( qp->send.num_wqes /* headroom */ + 1 +
  1023. ( 2048 / sizeof ( hermon_qp->send.wqe[0] ) ) );
  1024. hermon_qp->send.num_wqes =
  1025. ( 1 << fls ( hermon_qp->send.num_wqes - 1 ) ); /* round up */
  1026. hermon_qp->send.wqe_size = ( hermon_qp->send.num_wqes *
  1027. sizeof ( hermon_qp->send.wqe[0] ) );
  1028. hermon_qp->recv.wqe_size = ( qp->recv.num_wqes *
  1029. sizeof ( hermon_qp->recv.wqe[0] ) );
  1030. if ( ( qp->type == IB_QPT_SMI ) || ( qp->type == IB_QPT_GSI ) ||
  1031. ( qp->type == IB_QPT_UD ) ) {
  1032. hermon_qp->recv.grh_size = ( qp->recv.num_wqes *
  1033. sizeof ( hermon_qp->recv.grh[0] ));
  1034. }
  1035. hermon_qp->wqe_size = ( hermon_qp->send.wqe_size +
  1036. hermon_qp->recv.wqe_size +
  1037. hermon_qp->recv.grh_size );
  1038. hermon_qp->wqe = malloc_dma ( hermon_qp->wqe_size,
  1039. sizeof ( hermon_qp->send.wqe[0] ) );
  1040. if ( ! hermon_qp->wqe ) {
  1041. rc = -ENOMEM;
  1042. goto err_alloc_wqe;
  1043. }
  1044. hermon_qp->send.wqe = hermon_qp->wqe;
  1045. hermon_qp->recv.wqe = ( hermon_qp->wqe + hermon_qp->send.wqe_size );
  1046. if ( hermon_qp->recv.grh_size ) {
  1047. hermon_qp->recv.grh = ( hermon_qp->wqe +
  1048. hermon_qp->send.wqe_size +
  1049. hermon_qp->recv.wqe_size );
  1050. }
  1051. /* Initialise work queue entries */
  1052. memset ( hermon_qp->send.wqe, 0xff, hermon_qp->send.wqe_size );
  1053. memset ( hermon_qp->recv.wqe, 0, hermon_qp->recv.wqe_size );
  1054. data = &hermon_qp->recv.wqe[0].recv.data[0];
  1055. for ( i = 0 ; i < ( hermon_qp->recv.wqe_size / sizeof ( *data ) ); i++){
  1056. MLX_FILL_1 ( data, 1, l_key, HERMON_INVALID_LKEY );
  1057. data++;
  1058. }
  1059. /* Allocate MTT entries */
  1060. if ( ( rc = hermon_alloc_mtt ( hermon, hermon_qp->wqe,
  1061. hermon_qp->wqe_size,
  1062. &hermon_qp->mtt ) ) != 0 ) {
  1063. goto err_alloc_mtt;
  1064. }
  1065. /* Transition queue to INIT state */
  1066. memset ( &qpctx, 0, sizeof ( qpctx ) );
  1067. MLX_FILL_2 ( &qpctx, 2,
  1068. qpc_eec_data.pm_state, HERMON_PM_STATE_MIGRATED,
  1069. qpc_eec_data.st, hermon_qp_st[qp->type] );
  1070. MLX_FILL_1 ( &qpctx, 3, qpc_eec_data.pd, HERMON_GLOBAL_PD );
  1071. MLX_FILL_4 ( &qpctx, 4,
  1072. qpc_eec_data.log_rq_size, fls ( qp->recv.num_wqes - 1 ),
  1073. qpc_eec_data.log_rq_stride,
  1074. ( fls ( sizeof ( hermon_qp->recv.wqe[0] ) - 1 ) - 4 ),
  1075. qpc_eec_data.log_sq_size,
  1076. fls ( hermon_qp->send.num_wqes - 1 ),
  1077. qpc_eec_data.log_sq_stride,
  1078. ( fls ( sizeof ( hermon_qp->send.wqe[0] ) - 1 ) - 4 ) );
  1079. MLX_FILL_1 ( &qpctx, 5,
  1080. qpc_eec_data.usr_page, HERMON_UAR_NON_EQ_PAGE );
  1081. MLX_FILL_1 ( &qpctx, 33, qpc_eec_data.cqn_snd, qp->send.cq->cqn );
  1082. MLX_FILL_4 ( &qpctx, 38,
  1083. qpc_eec_data.rre, 1,
  1084. qpc_eec_data.rwe, 1,
  1085. qpc_eec_data.rae, 1,
  1086. qpc_eec_data.page_offset,
  1087. ( hermon_qp->mtt.page_offset >> 6 ) );
  1088. MLX_FILL_1 ( &qpctx, 41, qpc_eec_data.cqn_rcv, qp->recv.cq->cqn );
  1089. MLX_FILL_H ( &qpctx, 42, qpc_eec_data.db_record_addr_h,
  1090. virt_to_phys ( hermon_qp->recv.doorbell ) );
  1091. MLX_FILL_1 ( &qpctx, 43, qpc_eec_data.db_record_addr_l,
  1092. ( virt_to_phys ( hermon_qp->recv.doorbell ) >> 2 ) );
  1093. MLX_FILL_H ( &qpctx, 52, qpc_eec_data.mtt_base_addr_h,
  1094. hermon_qp->mtt.mtt_base_addr );
  1095. MLX_FILL_1 ( &qpctx, 53, qpc_eec_data.mtt_base_addr_l,
  1096. ( hermon_qp->mtt.mtt_base_addr >> 3 ) );
  1097. if ( ( rc = hermon_cmd_rst2init_qp ( hermon, qp->qpn,
  1098. &qpctx ) ) != 0 ) {
  1099. DBGC ( hermon, "Hermon %p QPN %#lx RST2INIT_QP failed: %s\n",
  1100. hermon, qp->qpn, strerror ( rc ) );
  1101. goto err_rst2init_qp;
  1102. }
  1103. hermon_qp->state = HERMON_QP_ST_INIT;
  1104. DBGC ( hermon, "Hermon %p QPN %#lx send ring [%08lx,%08lx), doorbell "
  1105. "%08lx\n", hermon, qp->qpn,
  1106. virt_to_phys ( hermon_qp->send.wqe ),
  1107. ( virt_to_phys ( hermon_qp->send.wqe ) +
  1108. hermon_qp->send.wqe_size ),
  1109. virt_to_phys ( hermon_qp->send.doorbell ) );
  1110. DBGC ( hermon, "Hermon %p QPN %#lx receive ring [%08lx,%08lx), "
  1111. "doorbell %08lx\n", hermon, qp->qpn,
  1112. virt_to_phys ( hermon_qp->recv.wqe ),
  1113. ( virt_to_phys ( hermon_qp->recv.wqe ) +
  1114. hermon_qp->recv.wqe_size ),
  1115. virt_to_phys ( hermon_qp->recv.doorbell ) );
  1116. DBGC ( hermon, "Hermon %p QPN %#lx send CQN %#lx receive CQN %#lx\n",
  1117. hermon, qp->qpn, qp->send.cq->cqn, qp->recv.cq->cqn );
  1118. ib_qp_set_drvdata ( qp, hermon_qp );
  1119. return 0;
  1120. hermon_cmd_2rst_qp ( hermon, qp->qpn );
  1121. err_rst2init_qp:
  1122. hermon_free_mtt ( hermon, &hermon_qp->mtt );
  1123. err_alloc_mtt:
  1124. free_dma ( hermon_qp->wqe, hermon_qp->wqe_size );
  1125. err_alloc_wqe:
  1126. free_dma ( hermon_qp->recv.doorbell,
  1127. sizeof ( hermon_qp->recv.doorbell[0] ) );
  1128. err_recv_doorbell:
  1129. free ( hermon_qp );
  1130. err_hermon_qp:
  1131. hermon_free_qpn ( ibdev, qp );
  1132. err_alloc_qpn:
  1133. return rc;
  1134. }
  1135. /**
  1136. * Modify queue pair
  1137. *
  1138. * @v ibdev Infiniband device
  1139. * @v qp Queue pair
  1140. * @ret rc Return status code
  1141. */
  1142. static int hermon_modify_qp ( struct ib_device *ibdev,
  1143. struct ib_queue_pair *qp ) {
  1144. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1145. struct hermon_queue_pair *hermon_qp = ib_qp_get_drvdata ( qp );
  1146. struct hermonprm_qp_ee_state_transitions qpctx;
  1147. int rc;
  1148. /* Transition queue to RTR state, if applicable */
  1149. if ( hermon_qp->state < HERMON_QP_ST_RTR ) {
  1150. memset ( &qpctx, 0, sizeof ( qpctx ) );
  1151. MLX_FILL_2 ( &qpctx, 4,
  1152. qpc_eec_data.mtu,
  1153. ( ( qp->type == IB_QPT_ETH ) ?
  1154. HERMON_MTU_ETH : HERMON_MTU_2048 ),
  1155. qpc_eec_data.msg_max, 31 );
  1156. MLX_FILL_1 ( &qpctx, 7,
  1157. qpc_eec_data.remote_qpn_een, qp->av.qpn );
  1158. MLX_FILL_1 ( &qpctx, 9,
  1159. qpc_eec_data.primary_address_path.rlid,
  1160. qp->av.lid );
  1161. MLX_FILL_1 ( &qpctx, 10,
  1162. qpc_eec_data.primary_address_path.max_stat_rate,
  1163. hermon_rate ( &qp->av ) );
  1164. memcpy ( &qpctx.u.dwords[12], &qp->av.gid,
  1165. sizeof ( qp->av.gid ) );
  1166. MLX_FILL_1 ( &qpctx, 16,
  1167. qpc_eec_data.primary_address_path.sched_queue,
  1168. hermon_sched_queue ( ibdev, qp ) );
  1169. MLX_FILL_1 ( &qpctx, 39,
  1170. qpc_eec_data.next_rcv_psn, qp->recv.psn );
  1171. if ( ( rc = hermon_cmd_init2rtr_qp ( hermon, qp->qpn,
  1172. &qpctx ) ) != 0 ) {
  1173. DBGC ( hermon, "Hermon %p QPN %#lx INIT2RTR_QP failed:"
  1174. " %s\n", hermon, qp->qpn, strerror ( rc ) );
  1175. return rc;
  1176. }
  1177. hermon_qp->state = HERMON_QP_ST_RTR;
  1178. }
  1179. /* Transition queue to RTS state */
  1180. if ( hermon_qp->state < HERMON_QP_ST_RTS ) {
  1181. memset ( &qpctx, 0, sizeof ( qpctx ) );
  1182. MLX_FILL_1 ( &qpctx, 10,
  1183. qpc_eec_data.primary_address_path.ack_timeout,
  1184. 14 /* 4.096us * 2^(14) = 67ms */ );
  1185. MLX_FILL_2 ( &qpctx, 30,
  1186. qpc_eec_data.retry_count, HERMON_RETRY_MAX,
  1187. qpc_eec_data.rnr_retry, HERMON_RETRY_MAX );
  1188. MLX_FILL_1 ( &qpctx, 32,
  1189. qpc_eec_data.next_send_psn, qp->send.psn );
  1190. if ( ( rc = hermon_cmd_rtr2rts_qp ( hermon, qp->qpn,
  1191. &qpctx ) ) != 0 ) {
  1192. DBGC ( hermon, "Hermon %p QPN %#lx RTR2RTS_QP failed: "
  1193. "%s\n", hermon, qp->qpn, strerror ( rc ) );
  1194. return rc;
  1195. }
  1196. hermon_qp->state = HERMON_QP_ST_RTS;
  1197. }
  1198. /* Update parameters in RTS state */
  1199. memset ( &qpctx, 0, sizeof ( qpctx ) );
  1200. MLX_FILL_1 ( &qpctx, 0, opt_param_mask, HERMON_QP_OPT_PARAM_QKEY );
  1201. MLX_FILL_1 ( &qpctx, 44, qpc_eec_data.q_key, qp->qkey );
  1202. if ( ( rc = hermon_cmd_rts2rts_qp ( hermon, qp->qpn, &qpctx ) ) != 0 ){
  1203. DBGC ( hermon, "Hermon %p QPN %#lx RTS2RTS_QP failed: %s\n",
  1204. hermon, qp->qpn, strerror ( rc ) );
  1205. return rc;
  1206. }
  1207. return 0;
  1208. }
  1209. /**
  1210. * Destroy queue pair
  1211. *
  1212. * @v ibdev Infiniband device
  1213. * @v qp Queue pair
  1214. */
  1215. static void hermon_destroy_qp ( struct ib_device *ibdev,
  1216. struct ib_queue_pair *qp ) {
  1217. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1218. struct hermon_queue_pair *hermon_qp = ib_qp_get_drvdata ( qp );
  1219. int rc;
  1220. /* Take ownership back from hardware */
  1221. if ( ( rc = hermon_cmd_2rst_qp ( hermon, qp->qpn ) ) != 0 ) {
  1222. DBGC ( hermon, "Hermon %p QPN %#lx FATAL 2RST_QP failed: %s\n",
  1223. hermon, qp->qpn, strerror ( rc ) );
  1224. /* Leak memory and return; at least we avoid corruption */
  1225. return;
  1226. }
  1227. /* Free MTT entries */
  1228. hermon_free_mtt ( hermon, &hermon_qp->mtt );
  1229. /* Free memory */
  1230. free_dma ( hermon_qp->wqe, hermon_qp->wqe_size );
  1231. free_dma ( hermon_qp->recv.doorbell,
  1232. sizeof ( hermon_qp->recv.doorbell[0] ) );
  1233. free ( hermon_qp );
  1234. /* Mark queue number as free */
  1235. hermon_free_qpn ( ibdev, qp );
  1236. ib_qp_set_drvdata ( qp, NULL );
  1237. }
  1238. /***************************************************************************
  1239. *
  1240. * Work request operations
  1241. *
  1242. ***************************************************************************
  1243. */
  1244. /**
  1245. * Construct UD send work queue entry
  1246. *
  1247. * @v ibdev Infiniband device
  1248. * @v qp Queue pair
  1249. * @v dest Destination address vector
  1250. * @v iobuf I/O buffer
  1251. * @v wqe Send work queue entry
  1252. * @ret opcode Control opcode
  1253. */
  1254. static __attribute__ (( unused )) unsigned int
  1255. hermon_fill_nop_send_wqe ( struct ib_device *ibdev __unused,
  1256. struct ib_queue_pair *qp __unused,
  1257. struct ib_address_vector *dest __unused,
  1258. struct io_buffer *iobuf __unused,
  1259. union hermon_send_wqe *wqe ) {
  1260. MLX_FILL_1 ( &wqe->ctrl, 1, ds, ( sizeof ( wqe->ctrl ) / 16 ) );
  1261. MLX_FILL_1 ( &wqe->ctrl, 2, c, 0x03 /* generate completion */ );
  1262. return HERMON_OPCODE_NOP;
  1263. }
  1264. /**
  1265. * Construct UD send work queue entry
  1266. *
  1267. * @v ibdev Infiniband device
  1268. * @v qp Queue pair
  1269. * @v dest Destination address vector
  1270. * @v iobuf I/O buffer
  1271. * @v wqe Send work queue entry
  1272. * @ret opcode Control opcode
  1273. */
  1274. static unsigned int
  1275. hermon_fill_ud_send_wqe ( struct ib_device *ibdev,
  1276. struct ib_queue_pair *qp __unused,
  1277. struct ib_address_vector *dest,
  1278. struct io_buffer *iobuf,
  1279. union hermon_send_wqe *wqe ) {
  1280. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1281. MLX_FILL_1 ( &wqe->ud.ctrl, 1, ds,
  1282. ( ( offsetof ( typeof ( wqe->ud ), data[1] ) / 16 ) ) );
  1283. MLX_FILL_1 ( &wqe->ud.ctrl, 2, c, 0x03 /* generate completion */ );
  1284. MLX_FILL_2 ( &wqe->ud.ud, 0,
  1285. ud_address_vector.pd, HERMON_GLOBAL_PD,
  1286. ud_address_vector.port_number, ibdev->port );
  1287. MLX_FILL_2 ( &wqe->ud.ud, 1,
  1288. ud_address_vector.rlid, dest->lid,
  1289. ud_address_vector.g, dest->gid_present );
  1290. MLX_FILL_1 ( &wqe->ud.ud, 2,
  1291. ud_address_vector.max_stat_rate, hermon_rate ( dest ) );
  1292. MLX_FILL_1 ( &wqe->ud.ud, 3, ud_address_vector.sl, dest->sl );
  1293. memcpy ( &wqe->ud.ud.u.dwords[4], &dest->gid, sizeof ( dest->gid ) );
  1294. MLX_FILL_1 ( &wqe->ud.ud, 8, destination_qp, dest->qpn );
  1295. MLX_FILL_1 ( &wqe->ud.ud, 9, q_key, dest->qkey );
  1296. MLX_FILL_1 ( &wqe->ud.data[0], 0, byte_count, iob_len ( iobuf ) );
  1297. MLX_FILL_1 ( &wqe->ud.data[0], 1, l_key, hermon->lkey );
  1298. MLX_FILL_H ( &wqe->ud.data[0], 2,
  1299. local_address_h, virt_to_bus ( iobuf->data ) );
  1300. MLX_FILL_1 ( &wqe->ud.data[0], 3,
  1301. local_address_l, virt_to_bus ( iobuf->data ) );
  1302. return HERMON_OPCODE_SEND;
  1303. }
  1304. /**
  1305. * Construct MLX send work queue entry
  1306. *
  1307. * @v ibdev Infiniband device
  1308. * @v qp Queue pair
  1309. * @v dest Destination address vector
  1310. * @v iobuf I/O buffer
  1311. * @v wqe Send work queue entry
  1312. * @ret opcode Control opcode
  1313. */
  1314. static unsigned int
  1315. hermon_fill_mlx_send_wqe ( struct ib_device *ibdev,
  1316. struct ib_queue_pair *qp,
  1317. struct ib_address_vector *dest,
  1318. struct io_buffer *iobuf,
  1319. union hermon_send_wqe *wqe ) {
  1320. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1321. struct io_buffer headers;
  1322. /* Construct IB headers */
  1323. iob_populate ( &headers, &wqe->mlx.headers, 0,
  1324. sizeof ( wqe->mlx.headers ) );
  1325. iob_reserve ( &headers, sizeof ( wqe->mlx.headers ) );
  1326. ib_push ( ibdev, &headers, qp, iob_len ( iobuf ), dest );
  1327. /* Fill work queue entry */
  1328. MLX_FILL_1 ( &wqe->mlx.ctrl, 1, ds,
  1329. ( ( offsetof ( typeof ( wqe->mlx ), data[2] ) / 16 ) ) );
  1330. MLX_FILL_5 ( &wqe->mlx.ctrl, 2,
  1331. c, 0x03 /* generate completion */,
  1332. icrc, 0 /* generate ICRC */,
  1333. max_statrate, hermon_rate ( dest ),
  1334. slr, 0,
  1335. v15, ( ( qp->ext_qpn == IB_QPN_SMI ) ? 1 : 0 ) );
  1336. MLX_FILL_1 ( &wqe->mlx.ctrl, 3, rlid, dest->lid );
  1337. MLX_FILL_1 ( &wqe->mlx.data[0], 0,
  1338. byte_count, iob_len ( &headers ) );
  1339. MLX_FILL_1 ( &wqe->mlx.data[0], 1, l_key, hermon->lkey );
  1340. MLX_FILL_H ( &wqe->mlx.data[0], 2,
  1341. local_address_h, virt_to_bus ( headers.data ) );
  1342. MLX_FILL_1 ( &wqe->mlx.data[0], 3,
  1343. local_address_l, virt_to_bus ( headers.data ) );
  1344. MLX_FILL_1 ( &wqe->mlx.data[1], 0,
  1345. byte_count, ( iob_len ( iobuf ) + 4 /* ICRC */ ) );
  1346. MLX_FILL_1 ( &wqe->mlx.data[1], 1, l_key, hermon->lkey );
  1347. MLX_FILL_H ( &wqe->mlx.data[1], 2,
  1348. local_address_h, virt_to_bus ( iobuf->data ) );
  1349. MLX_FILL_1 ( &wqe->mlx.data[1], 3,
  1350. local_address_l, virt_to_bus ( iobuf->data ) );
  1351. return HERMON_OPCODE_SEND;
  1352. }
  1353. /**
  1354. * Construct RC send work queue entry
  1355. *
  1356. * @v ibdev Infiniband device
  1357. * @v qp Queue pair
  1358. * @v dest Destination address vector
  1359. * @v iobuf I/O buffer
  1360. * @v wqe Send work queue entry
  1361. * @ret opcode Control opcode
  1362. */
  1363. static unsigned int
  1364. hermon_fill_rc_send_wqe ( struct ib_device *ibdev,
  1365. struct ib_queue_pair *qp __unused,
  1366. struct ib_address_vector *dest __unused,
  1367. struct io_buffer *iobuf,
  1368. union hermon_send_wqe *wqe ) {
  1369. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1370. MLX_FILL_1 ( &wqe->rc.ctrl, 1, ds,
  1371. ( ( offsetof ( typeof ( wqe->rc ), data[1] ) / 16 ) ) );
  1372. MLX_FILL_1 ( &wqe->rc.ctrl, 2, c, 0x03 /* generate completion */ );
  1373. MLX_FILL_1 ( &wqe->rc.data[0], 0, byte_count, iob_len ( iobuf ) );
  1374. MLX_FILL_1 ( &wqe->rc.data[0], 1, l_key, hermon->lkey );
  1375. MLX_FILL_H ( &wqe->rc.data[0], 2,
  1376. local_address_h, virt_to_bus ( iobuf->data ) );
  1377. MLX_FILL_1 ( &wqe->rc.data[0], 3,
  1378. local_address_l, virt_to_bus ( iobuf->data ) );
  1379. return HERMON_OPCODE_SEND;
  1380. }
  1381. /**
  1382. * Construct Ethernet send work queue entry
  1383. *
  1384. * @v ibdev Infiniband device
  1385. * @v qp Queue pair
  1386. * @v dest Destination address vector
  1387. * @v iobuf I/O buffer
  1388. * @v wqe Send work queue entry
  1389. * @ret opcode Control opcode
  1390. */
  1391. static unsigned int
  1392. hermon_fill_eth_send_wqe ( struct ib_device *ibdev,
  1393. struct ib_queue_pair *qp __unused,
  1394. struct ib_address_vector *dest __unused,
  1395. struct io_buffer *iobuf,
  1396. union hermon_send_wqe *wqe ) {
  1397. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1398. /* Fill work queue entry */
  1399. MLX_FILL_1 ( &wqe->eth.ctrl, 1, ds,
  1400. ( ( offsetof ( typeof ( wqe->mlx ), data[1] ) / 16 ) ) );
  1401. MLX_FILL_2 ( &wqe->eth.ctrl, 2,
  1402. c, 0x03 /* generate completion */,
  1403. s, 1 /* inhibit ICRC */ );
  1404. MLX_FILL_1 ( &wqe->eth.data[0], 0,
  1405. byte_count, iob_len ( iobuf ) );
  1406. MLX_FILL_1 ( &wqe->eth.data[0], 1, l_key, hermon->lkey );
  1407. MLX_FILL_H ( &wqe->eth.data[0], 2,
  1408. local_address_h, virt_to_bus ( iobuf->data ) );
  1409. MLX_FILL_1 ( &wqe->eth.data[0], 3,
  1410. local_address_l, virt_to_bus ( iobuf->data ) );
  1411. return HERMON_OPCODE_SEND;
  1412. }
  1413. /** Work queue entry constructors */
  1414. static unsigned int
  1415. ( * hermon_fill_send_wqe[] ) ( struct ib_device *ibdev,
  1416. struct ib_queue_pair *qp,
  1417. struct ib_address_vector *dest,
  1418. struct io_buffer *iobuf,
  1419. union hermon_send_wqe *wqe ) = {
  1420. [IB_QPT_SMI] = hermon_fill_mlx_send_wqe,
  1421. [IB_QPT_GSI] = hermon_fill_mlx_send_wqe,
  1422. [IB_QPT_UD] = hermon_fill_ud_send_wqe,
  1423. [IB_QPT_RC] = hermon_fill_rc_send_wqe,
  1424. [IB_QPT_ETH] = hermon_fill_eth_send_wqe,
  1425. };
  1426. /**
  1427. * Post send work queue entry
  1428. *
  1429. * @v ibdev Infiniband device
  1430. * @v qp Queue pair
  1431. * @v dest Destination address vector
  1432. * @v iobuf I/O buffer
  1433. * @ret rc Return status code
  1434. */
  1435. static int hermon_post_send ( struct ib_device *ibdev,
  1436. struct ib_queue_pair *qp,
  1437. struct ib_address_vector *dest,
  1438. struct io_buffer *iobuf ) {
  1439. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1440. struct hermon_queue_pair *hermon_qp = ib_qp_get_drvdata ( qp );
  1441. struct ib_work_queue *wq = &qp->send;
  1442. struct hermon_send_work_queue *hermon_send_wq = &hermon_qp->send;
  1443. union hermon_send_wqe *wqe;
  1444. union hermonprm_doorbell_register db_reg;
  1445. unsigned long wqe_idx_mask;
  1446. unsigned long wqe_idx;
  1447. unsigned int owner;
  1448. unsigned int opcode;
  1449. /* Allocate work queue entry */
  1450. wqe_idx = ( wq->next_idx & ( hermon_send_wq->num_wqes - 1 ) );
  1451. owner = ( ( wq->next_idx & hermon_send_wq->num_wqes ) ? 1 : 0 );
  1452. wqe_idx_mask = ( wq->num_wqes - 1 );
  1453. if ( wq->iobufs[ wqe_idx & wqe_idx_mask ] ) {
  1454. DBGC ( hermon, "Hermon %p QPN %#lx send queue full",
  1455. hermon, qp->qpn );
  1456. return -ENOBUFS;
  1457. }
  1458. wq->iobufs[ wqe_idx & wqe_idx_mask ] = iobuf;
  1459. wqe = &hermon_send_wq->wqe[wqe_idx];
  1460. /* Construct work queue entry */
  1461. memset ( ( ( ( void * ) wqe ) + 4 /* avoid ctrl.owner */ ), 0,
  1462. ( sizeof ( *wqe ) - 4 ) );
  1463. assert ( qp->type < ( sizeof ( hermon_fill_send_wqe ) /
  1464. sizeof ( hermon_fill_send_wqe[0] ) ) );
  1465. assert ( hermon_fill_send_wqe[qp->type] != NULL );
  1466. opcode = hermon_fill_send_wqe[qp->type] ( ibdev, qp, dest, iobuf, wqe );
  1467. barrier();
  1468. MLX_FILL_2 ( &wqe->ctrl, 0,
  1469. opcode, opcode,
  1470. owner, owner );
  1471. DBGCP ( hermon, "Hermon %p QPN %#lx posting send WQE %#lx:\n",
  1472. hermon, qp->qpn, wqe_idx );
  1473. DBGCP_HDA ( hermon, virt_to_phys ( wqe ), wqe, sizeof ( *wqe ) );
  1474. /* Ring doorbell register */
  1475. MLX_FILL_1 ( &db_reg.send, 0, qn, qp->qpn );
  1476. barrier();
  1477. writel ( db_reg.dword[0], hermon_send_wq->doorbell );
  1478. /* Update work queue's index */
  1479. wq->next_idx++;
  1480. return 0;
  1481. }
  1482. /**
  1483. * Post receive work queue entry
  1484. *
  1485. * @v ibdev Infiniband device
  1486. * @v qp Queue pair
  1487. * @v iobuf I/O buffer
  1488. * @ret rc Return status code
  1489. */
  1490. static int hermon_post_recv ( struct ib_device *ibdev,
  1491. struct ib_queue_pair *qp,
  1492. struct io_buffer *iobuf ) {
  1493. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1494. struct hermon_queue_pair *hermon_qp = ib_qp_get_drvdata ( qp );
  1495. struct ib_work_queue *wq = &qp->recv;
  1496. struct hermon_recv_work_queue *hermon_recv_wq = &hermon_qp->recv;
  1497. struct hermonprm_recv_wqe *wqe;
  1498. struct hermonprm_wqe_segment_data_ptr *data;
  1499. struct ib_global_route_header *grh;
  1500. unsigned int wqe_idx_mask;
  1501. /* Allocate work queue entry */
  1502. wqe_idx_mask = ( wq->num_wqes - 1 );
  1503. if ( wq->iobufs[wq->next_idx & wqe_idx_mask] ) {
  1504. DBGC ( hermon, "Hermon %p QPN %#lx receive queue full",
  1505. hermon, qp->qpn );
  1506. return -ENOBUFS;
  1507. }
  1508. wq->iobufs[wq->next_idx & wqe_idx_mask] = iobuf;
  1509. wqe = &hermon_recv_wq->wqe[wq->next_idx & wqe_idx_mask].recv;
  1510. /* Construct work queue entry */
  1511. data = &wqe->data[0];
  1512. if ( hermon_qp->recv.grh ) {
  1513. grh = &hermon_qp->recv.grh[wq->next_idx & wqe_idx_mask];
  1514. MLX_FILL_1 ( data, 0, byte_count, sizeof ( *grh ) );
  1515. MLX_FILL_1 ( data, 1, l_key, hermon->lkey );
  1516. MLX_FILL_H ( data, 2, local_address_h, virt_to_bus ( grh ) );
  1517. MLX_FILL_1 ( data, 3, local_address_l, virt_to_bus ( grh ) );
  1518. data++;
  1519. }
  1520. MLX_FILL_1 ( data, 0, byte_count, iob_tailroom ( iobuf ) );
  1521. MLX_FILL_1 ( data, 1, l_key, hermon->lkey );
  1522. MLX_FILL_H ( data, 2, local_address_h, virt_to_bus ( iobuf->data ) );
  1523. MLX_FILL_1 ( data, 3, local_address_l, virt_to_bus ( iobuf->data ) );
  1524. /* Update work queue's index */
  1525. wq->next_idx++;
  1526. /* Update doorbell record */
  1527. barrier();
  1528. MLX_FILL_1 ( hermon_recv_wq->doorbell, 0, receive_wqe_counter,
  1529. ( wq->next_idx & 0xffff ) );
  1530. return 0;
  1531. }
  1532. /**
  1533. * Handle completion
  1534. *
  1535. * @v ibdev Infiniband device
  1536. * @v cq Completion queue
  1537. * @v cqe Hardware completion queue entry
  1538. * @ret rc Return status code
  1539. */
  1540. static int hermon_complete ( struct ib_device *ibdev,
  1541. struct ib_completion_queue *cq,
  1542. union hermonprm_completion_entry *cqe ) {
  1543. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1544. struct hermon_queue_pair *hermon_qp;
  1545. struct ib_work_queue *wq;
  1546. struct ib_queue_pair *qp;
  1547. struct io_buffer *iobuf;
  1548. struct ib_address_vector recv_dest;
  1549. struct ib_address_vector recv_source;
  1550. struct ib_global_route_header *grh;
  1551. struct ib_address_vector *source;
  1552. unsigned int opcode;
  1553. unsigned long qpn;
  1554. int is_send;
  1555. unsigned long wqe_idx;
  1556. unsigned long wqe_idx_mask;
  1557. size_t len;
  1558. int rc = 0;
  1559. /* Parse completion */
  1560. qpn = MLX_GET ( &cqe->normal, qpn );
  1561. is_send = MLX_GET ( &cqe->normal, s_r );
  1562. opcode = MLX_GET ( &cqe->normal, opcode );
  1563. if ( opcode >= HERMON_OPCODE_RECV_ERROR ) {
  1564. /* "s" field is not valid for error opcodes */
  1565. is_send = ( opcode == HERMON_OPCODE_SEND_ERROR );
  1566. DBGC ( hermon, "Hermon %p CQN %#lx syndrome %x vendor %x\n",
  1567. hermon, cq->cqn, MLX_GET ( &cqe->error, syndrome ),
  1568. MLX_GET ( &cqe->error, vendor_error_syndrome ) );
  1569. rc = -EIO;
  1570. /* Don't return immediately; propagate error to completer */
  1571. }
  1572. /* Identify work queue */
  1573. wq = ib_find_wq ( cq, qpn, is_send );
  1574. if ( ! wq ) {
  1575. DBGC ( hermon, "Hermon %p CQN %#lx unknown %s QPN %#lx\n",
  1576. hermon, cq->cqn, ( is_send ? "send" : "recv" ), qpn );
  1577. return -EIO;
  1578. }
  1579. qp = wq->qp;
  1580. hermon_qp = ib_qp_get_drvdata ( qp );
  1581. /* Identify work queue entry */
  1582. wqe_idx = MLX_GET ( &cqe->normal, wqe_counter );
  1583. wqe_idx_mask = ( wq->num_wqes - 1 );
  1584. DBGCP ( hermon, "Hermon %p CQN %#lx QPN %#lx %s WQE %#lx completed:\n",
  1585. hermon, cq->cqn, qp->qpn, ( is_send ? "send" : "recv" ),
  1586. wqe_idx );
  1587. DBGCP_HDA ( hermon, virt_to_phys ( cqe ), cqe, sizeof ( *cqe ) );
  1588. /* Identify I/O buffer */
  1589. iobuf = wq->iobufs[ wqe_idx & wqe_idx_mask ];
  1590. if ( ! iobuf ) {
  1591. DBGC ( hermon, "Hermon %p CQN %#lx QPN %#lx empty %s WQE "
  1592. "%#lx\n", hermon, cq->cqn, qp->qpn,
  1593. ( is_send ? "send" : "recv" ), wqe_idx );
  1594. return -EIO;
  1595. }
  1596. wq->iobufs[ wqe_idx & wqe_idx_mask ] = NULL;
  1597. if ( is_send ) {
  1598. /* Hand off to completion handler */
  1599. ib_complete_send ( ibdev, qp, iobuf, rc );
  1600. } else {
  1601. /* Set received length */
  1602. len = MLX_GET ( &cqe->normal, byte_cnt );
  1603. memset ( &recv_dest, 0, sizeof ( recv_dest ) );
  1604. recv_dest.qpn = qpn;
  1605. memset ( &recv_source, 0, sizeof ( recv_source ) );
  1606. switch ( qp->type ) {
  1607. case IB_QPT_SMI:
  1608. case IB_QPT_GSI:
  1609. case IB_QPT_UD:
  1610. /* Locate corresponding GRH */
  1611. assert ( hermon_qp->recv.grh != NULL );
  1612. grh = &hermon_qp->recv.grh[ wqe_idx & wqe_idx_mask ];
  1613. len -= sizeof ( *grh );
  1614. /* Construct address vector */
  1615. source = &recv_source;
  1616. source->qpn = MLX_GET ( &cqe->normal, srq_rqpn );
  1617. source->lid = MLX_GET ( &cqe->normal, slid_smac47_32 );
  1618. source->sl = MLX_GET ( &cqe->normal, sl );
  1619. recv_dest.gid_present = source->gid_present =
  1620. MLX_GET ( &cqe->normal, g );
  1621. memcpy ( &recv_dest.gid, &grh->dgid,
  1622. sizeof ( recv_dest.gid ) );
  1623. memcpy ( &source->gid, &grh->sgid,
  1624. sizeof ( source->gid ) );
  1625. break;
  1626. case IB_QPT_RC:
  1627. source = &qp->av;
  1628. break;
  1629. case IB_QPT_ETH:
  1630. /* Construct address vector */
  1631. source = &recv_source;
  1632. source->vlan_present = MLX_GET ( &cqe->normal, vlan );
  1633. source->vlan = MLX_GET ( &cqe->normal, vid );
  1634. break;
  1635. default:
  1636. assert ( 0 );
  1637. return -EINVAL;
  1638. }
  1639. assert ( len <= iob_tailroom ( iobuf ) );
  1640. iob_put ( iobuf, len );
  1641. /* Hand off to completion handler */
  1642. ib_complete_recv ( ibdev, qp, &recv_dest, source, iobuf, rc );
  1643. }
  1644. return rc;
  1645. }
  1646. /**
  1647. * Poll completion queue
  1648. *
  1649. * @v ibdev Infiniband device
  1650. * @v cq Completion queue
  1651. */
  1652. static void hermon_poll_cq ( struct ib_device *ibdev,
  1653. struct ib_completion_queue *cq ) {
  1654. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1655. struct hermon_completion_queue *hermon_cq = ib_cq_get_drvdata ( cq );
  1656. union hermonprm_completion_entry *cqe;
  1657. unsigned int cqe_idx_mask;
  1658. int rc;
  1659. while ( 1 ) {
  1660. /* Look for completion entry */
  1661. cqe_idx_mask = ( cq->num_cqes - 1 );
  1662. cqe = &hermon_cq->cqe[cq->next_idx & cqe_idx_mask];
  1663. if ( MLX_GET ( &cqe->normal, owner ) ^
  1664. ( ( cq->next_idx & cq->num_cqes ) ? 1 : 0 ) ) {
  1665. /* Entry still owned by hardware; end of poll */
  1666. break;
  1667. }
  1668. /* Handle completion */
  1669. if ( ( rc = hermon_complete ( ibdev, cq, cqe ) ) != 0 ) {
  1670. DBGC ( hermon, "Hermon %p CQN %#lx failed to complete:"
  1671. " %s\n", hermon, cq->cqn, strerror ( rc ) );
  1672. DBGC_HDA ( hermon, virt_to_phys ( cqe ),
  1673. cqe, sizeof ( *cqe ) );
  1674. }
  1675. /* Update completion queue's index */
  1676. cq->next_idx++;
  1677. /* Update doorbell record */
  1678. MLX_FILL_1 ( hermon_cq->doorbell, 0, update_ci,
  1679. ( cq->next_idx & 0x00ffffffUL ) );
  1680. }
  1681. }
  1682. /***************************************************************************
  1683. *
  1684. * Event queues
  1685. *
  1686. ***************************************************************************
  1687. */
  1688. /**
  1689. * Create event queue
  1690. *
  1691. * @v hermon Hermon device
  1692. * @ret rc Return status code
  1693. */
  1694. static int hermon_create_eq ( struct hermon *hermon ) {
  1695. struct hermon_event_queue *hermon_eq = &hermon->eq;
  1696. struct hermonprm_eqc eqctx;
  1697. struct hermonprm_event_mask mask;
  1698. unsigned int i;
  1699. int rc;
  1700. /* Select event queue number */
  1701. hermon_eq->eqn = ( 4 * hermon->cap.reserved_uars );
  1702. if ( hermon_eq->eqn < hermon->cap.reserved_eqs )
  1703. hermon_eq->eqn = hermon->cap.reserved_eqs;
  1704. /* Calculate doorbell address */
  1705. hermon_eq->doorbell =
  1706. ( hermon->uar + HERMON_DB_EQ_OFFSET ( hermon_eq->eqn ) );
  1707. /* Allocate event queue itself */
  1708. hermon_eq->eqe_size =
  1709. ( HERMON_NUM_EQES * sizeof ( hermon_eq->eqe[0] ) );
  1710. hermon_eq->eqe = malloc_dma ( hermon_eq->eqe_size,
  1711. sizeof ( hermon_eq->eqe[0] ) );
  1712. if ( ! hermon_eq->eqe ) {
  1713. rc = -ENOMEM;
  1714. goto err_eqe;
  1715. }
  1716. memset ( hermon_eq->eqe, 0, hermon_eq->eqe_size );
  1717. for ( i = 0 ; i < HERMON_NUM_EQES ; i++ ) {
  1718. MLX_FILL_1 ( &hermon_eq->eqe[i].generic, 7, owner, 1 );
  1719. }
  1720. barrier();
  1721. /* Allocate MTT entries */
  1722. if ( ( rc = hermon_alloc_mtt ( hermon, hermon_eq->eqe,
  1723. hermon_eq->eqe_size,
  1724. &hermon_eq->mtt ) ) != 0 )
  1725. goto err_alloc_mtt;
  1726. /* Hand queue over to hardware */
  1727. memset ( &eqctx, 0, sizeof ( eqctx ) );
  1728. MLX_FILL_2 ( &eqctx, 0,
  1729. st, 0xa /* "Fired" */,
  1730. oi, 1 );
  1731. MLX_FILL_1 ( &eqctx, 2,
  1732. page_offset, ( hermon_eq->mtt.page_offset >> 5 ) );
  1733. MLX_FILL_1 ( &eqctx, 3, log_eq_size, fls ( HERMON_NUM_EQES - 1 ) );
  1734. MLX_FILL_H ( &eqctx, 6, mtt_base_addr_h,
  1735. hermon_eq->mtt.mtt_base_addr );
  1736. MLX_FILL_1 ( &eqctx, 7, mtt_base_addr_l,
  1737. ( hermon_eq->mtt.mtt_base_addr >> 3 ) );
  1738. if ( ( rc = hermon_cmd_sw2hw_eq ( hermon, hermon_eq->eqn,
  1739. &eqctx ) ) != 0 ) {
  1740. DBGC ( hermon, "Hermon %p EQN %#lx SW2HW_EQ failed: %s\n",
  1741. hermon, hermon_eq->eqn, strerror ( rc ) );
  1742. goto err_sw2hw_eq;
  1743. }
  1744. /* Map all events to this event queue */
  1745. memset ( &mask, 0xff, sizeof ( mask ) );
  1746. if ( ( rc = hermon_cmd_map_eq ( hermon,
  1747. ( HERMON_MAP_EQ | hermon_eq->eqn ),
  1748. &mask ) ) != 0 ) {
  1749. DBGC ( hermon, "Hermon %p EQN %#lx MAP_EQ failed: %s\n",
  1750. hermon, hermon_eq->eqn, strerror ( rc ) );
  1751. goto err_map_eq;
  1752. }
  1753. DBGC ( hermon, "Hermon %p EQN %#lx ring [%08lx,%08lx), doorbell "
  1754. "%08lx\n", hermon, hermon_eq->eqn,
  1755. virt_to_phys ( hermon_eq->eqe ),
  1756. ( virt_to_phys ( hermon_eq->eqe ) + hermon_eq->eqe_size ),
  1757. virt_to_phys ( hermon_eq->doorbell ) );
  1758. return 0;
  1759. err_map_eq:
  1760. hermon_cmd_hw2sw_eq ( hermon, hermon_eq->eqn, &eqctx );
  1761. err_sw2hw_eq:
  1762. hermon_free_mtt ( hermon, &hermon_eq->mtt );
  1763. err_alloc_mtt:
  1764. free_dma ( hermon_eq->eqe, hermon_eq->eqe_size );
  1765. err_eqe:
  1766. memset ( hermon_eq, 0, sizeof ( *hermon_eq ) );
  1767. return rc;
  1768. }
  1769. /**
  1770. * Destroy event queue
  1771. *
  1772. * @v hermon Hermon device
  1773. */
  1774. static void hermon_destroy_eq ( struct hermon *hermon ) {
  1775. struct hermon_event_queue *hermon_eq = &hermon->eq;
  1776. struct hermonprm_eqc eqctx;
  1777. struct hermonprm_event_mask mask;
  1778. int rc;
  1779. /* Unmap events from event queue */
  1780. memset ( &mask, 0xff, sizeof ( mask ) );
  1781. if ( ( rc = hermon_cmd_map_eq ( hermon,
  1782. ( HERMON_UNMAP_EQ | hermon_eq->eqn ),
  1783. &mask ) ) != 0 ) {
  1784. DBGC ( hermon, "Hermon %p EQN %#lx FATAL MAP_EQ failed to "
  1785. "unmap: %s\n", hermon, hermon_eq->eqn, strerror ( rc ) );
  1786. /* Continue; HCA may die but system should survive */
  1787. }
  1788. /* Take ownership back from hardware */
  1789. if ( ( rc = hermon_cmd_hw2sw_eq ( hermon, hermon_eq->eqn,
  1790. &eqctx ) ) != 0 ) {
  1791. DBGC ( hermon, "Hermon %p EQN %#lx FATAL HW2SW_EQ failed: %s\n",
  1792. hermon, hermon_eq->eqn, strerror ( rc ) );
  1793. /* Leak memory and return; at least we avoid corruption */
  1794. return;
  1795. }
  1796. /* Free MTT entries */
  1797. hermon_free_mtt ( hermon, &hermon_eq->mtt );
  1798. /* Free memory */
  1799. free_dma ( hermon_eq->eqe, hermon_eq->eqe_size );
  1800. memset ( hermon_eq, 0, sizeof ( *hermon_eq ) );
  1801. }
  1802. /**
  1803. * Handle port state event
  1804. *
  1805. * @v hermon Hermon device
  1806. * @v eqe Port state change event queue entry
  1807. */
  1808. static void hermon_event_port_state_change ( struct hermon *hermon,
  1809. union hermonprm_event_entry *eqe){
  1810. unsigned int port;
  1811. int link_up;
  1812. /* Get port and link status */
  1813. port = ( MLX_GET ( &eqe->port_state_change, data.p ) - 1 );
  1814. link_up = ( MLX_GET ( &eqe->generic, event_sub_type ) & 0x04 );
  1815. DBGC ( hermon, "Hermon %p port %d link %s\n", hermon, ( port + 1 ),
  1816. ( link_up ? "up" : "down" ) );
  1817. /* Sanity check */
  1818. if ( port >= hermon->cap.num_ports ) {
  1819. DBGC ( hermon, "Hermon %p port %d does not exist!\n",
  1820. hermon, ( port + 1 ) );
  1821. return;
  1822. }
  1823. /* Notify device of port state change */
  1824. hermon->port[port].type->state_change ( hermon, &hermon->port[port],
  1825. link_up );
  1826. }
  1827. /**
  1828. * Poll event queue
  1829. *
  1830. * @v ibdev Infiniband device
  1831. */
  1832. static void hermon_poll_eq ( struct ib_device *ibdev ) {
  1833. struct hermon *hermon = ib_get_drvdata ( ibdev );
  1834. struct hermon_event_queue *hermon_eq = &hermon->eq;
  1835. union hermonprm_event_entry *eqe;
  1836. union hermonprm_doorbell_register db_reg;
  1837. unsigned int eqe_idx_mask;
  1838. unsigned int event_type;
  1839. /* No event is generated upon reaching INIT, so we must poll
  1840. * separately for link state changes while we remain DOWN.
  1841. */
  1842. if ( ib_is_open ( ibdev ) &&
  1843. ( ibdev->port_state == IB_PORT_STATE_DOWN ) ) {
  1844. ib_smc_update ( ibdev, hermon_mad );
  1845. }
  1846. /* Poll event queue */
  1847. while ( 1 ) {
  1848. /* Look for event entry */
  1849. eqe_idx_mask = ( HERMON_NUM_EQES - 1 );
  1850. eqe = &hermon_eq->eqe[hermon_eq->next_idx & eqe_idx_mask];
  1851. if ( MLX_GET ( &eqe->generic, owner ) ^
  1852. ( ( hermon_eq->next_idx & HERMON_NUM_EQES ) ? 1 : 0 ) ) {
  1853. /* Entry still owned by hardware; end of poll */
  1854. break;
  1855. }
  1856. DBGCP ( hermon, "Hermon %p EQN %#lx event:\n",
  1857. hermon, hermon_eq->eqn );
  1858. DBGCP_HDA ( hermon, virt_to_phys ( eqe ),
  1859. eqe, sizeof ( *eqe ) );
  1860. /* Handle event */
  1861. event_type = MLX_GET ( &eqe->generic, event_type );
  1862. switch ( event_type ) {
  1863. case HERMON_EV_PORT_STATE_CHANGE:
  1864. hermon_event_port_state_change ( hermon, eqe );
  1865. break;
  1866. default:
  1867. DBGC ( hermon, "Hermon %p EQN %#lx unrecognised event "
  1868. "type %#x:\n",
  1869. hermon, hermon_eq->eqn, event_type );
  1870. DBGC_HDA ( hermon, virt_to_phys ( eqe ),
  1871. eqe, sizeof ( *eqe ) );
  1872. break;
  1873. }
  1874. /* Update event queue's index */
  1875. hermon_eq->next_idx++;
  1876. /* Ring doorbell */
  1877. MLX_FILL_1 ( &db_reg.event, 0,
  1878. ci, ( hermon_eq->next_idx & 0x00ffffffUL ) );
  1879. writel ( db_reg.dword[0], hermon_eq->doorbell );
  1880. }
  1881. }
  1882. /***************************************************************************
  1883. *
  1884. * Firmware control
  1885. *
  1886. ***************************************************************************
  1887. */
  1888. /**
  1889. * Map virtual to physical address for firmware usage
  1890. *
  1891. * @v hermon Hermon device
  1892. * @v map Mapping function
  1893. * @v va Virtual address
  1894. * @v pa Physical address
  1895. * @v len Length of region
  1896. * @ret rc Return status code
  1897. */
  1898. static int hermon_map_vpm ( struct hermon *hermon,
  1899. int ( *map ) ( struct hermon *hermon,
  1900. const struct hermonprm_virtual_physical_mapping* ),
  1901. uint64_t va, physaddr_t pa, size_t len ) {
  1902. struct hermonprm_virtual_physical_mapping mapping;
  1903. physaddr_t start;
  1904. physaddr_t low;
  1905. physaddr_t high;
  1906. physaddr_t end;
  1907. size_t size;
  1908. int rc;
  1909. /* Sanity checks */
  1910. assert ( ( va & ( HERMON_PAGE_SIZE - 1 ) ) == 0 );
  1911. assert ( ( pa & ( HERMON_PAGE_SIZE - 1 ) ) == 0 );
  1912. assert ( ( len & ( HERMON_PAGE_SIZE - 1 ) ) == 0 );
  1913. assert ( len != 0 );
  1914. /* Calculate starting points */
  1915. start = pa;
  1916. end = ( start + len );
  1917. size = ( 1UL << ( fls ( start ^ end ) - 1 ) );
  1918. low = high = ( end & ~( size - 1 ) );
  1919. assert ( start < low );
  1920. assert ( high <= end );
  1921. /* These mappings tend to generate huge volumes of
  1922. * uninteresting debug data, which basically makes it
  1923. * impossible to use debugging otherwise.
  1924. */
  1925. DBG_DISABLE ( DBGLVL_LOG | DBGLVL_EXTRA );
  1926. /* Map blocks in descending order of size */
  1927. while ( size >= HERMON_PAGE_SIZE ) {
  1928. /* Find the next candidate block */
  1929. if ( ( low - size ) >= start ) {
  1930. low -= size;
  1931. pa = low;
  1932. } else if ( high <= ( end - size ) ) {
  1933. pa = high;
  1934. high += size;
  1935. } else {
  1936. size >>= 1;
  1937. continue;
  1938. }
  1939. assert ( ( va & ( size - 1 ) ) == 0 );
  1940. assert ( ( pa & ( size - 1 ) ) == 0 );
  1941. /* Map this block */
  1942. memset ( &mapping, 0, sizeof ( mapping ) );
  1943. MLX_FILL_1 ( &mapping, 0, va_h, ( va >> 32 ) );
  1944. MLX_FILL_1 ( &mapping, 1, va_l, ( va >> 12 ) );
  1945. MLX_FILL_H ( &mapping, 2, pa_h, pa );
  1946. MLX_FILL_2 ( &mapping, 3,
  1947. log2size, ( ( fls ( size ) - 1 ) - 12 ),
  1948. pa_l, ( pa >> 12 ) );
  1949. if ( ( rc = map ( hermon, &mapping ) ) != 0 ) {
  1950. DBG_ENABLE ( DBGLVL_LOG | DBGLVL_EXTRA );
  1951. DBGC ( hermon, "Hermon %p could not map %08llx+%zx to "
  1952. "%08lx: %s\n",
  1953. hermon, va, size, pa, strerror ( rc ) );
  1954. return rc;
  1955. }
  1956. va += size;
  1957. }
  1958. assert ( low == start );
  1959. assert ( high == end );
  1960. DBG_ENABLE ( DBGLVL_LOG | DBGLVL_EXTRA );
  1961. return 0;
  1962. }
  1963. /**
  1964. * Start firmware running
  1965. *
  1966. * @v hermon Hermon device
  1967. * @ret rc Return status code
  1968. */
  1969. static int hermon_start_firmware ( struct hermon *hermon ) {
  1970. struct hermonprm_query_fw fw;
  1971. unsigned int fw_pages;
  1972. size_t fw_len;
  1973. physaddr_t fw_base;
  1974. int rc;
  1975. /* Get firmware parameters */
  1976. if ( ( rc = hermon_cmd_query_fw ( hermon, &fw ) ) != 0 ) {
  1977. DBGC ( hermon, "Hermon %p could not query firmware: %s\n",
  1978. hermon, strerror ( rc ) );
  1979. goto err_query_fw;
  1980. }
  1981. DBGC ( hermon, "Hermon %p firmware version %d.%d.%d\n", hermon,
  1982. MLX_GET ( &fw, fw_rev_major ), MLX_GET ( &fw, fw_rev_minor ),
  1983. MLX_GET ( &fw, fw_rev_subminor ) );
  1984. fw_pages = MLX_GET ( &fw, fw_pages );
  1985. DBGC ( hermon, "Hermon %p requires %d pages (%d kB) for firmware\n",
  1986. hermon, fw_pages, ( fw_pages * 4 ) );
  1987. /* Allocate firmware pages and map firmware area */
  1988. fw_len = ( fw_pages * HERMON_PAGE_SIZE );
  1989. if ( ! hermon->firmware_area ) {
  1990. hermon->firmware_len = fw_len;
  1991. hermon->firmware_area = umalloc ( hermon->firmware_len );
  1992. if ( ! hermon->firmware_area ) {
  1993. rc = -ENOMEM;
  1994. goto err_alloc_fa;
  1995. }
  1996. } else {
  1997. assert ( hermon->firmware_len == fw_len );
  1998. }
  1999. fw_base = user_to_phys ( hermon->firmware_area, 0 );
  2000. DBGC ( hermon, "Hermon %p firmware area at physical [%08lx,%08lx)\n",
  2001. hermon, fw_base, ( fw_base + fw_len ) );
  2002. if ( ( rc = hermon_map_vpm ( hermon, hermon_cmd_map_fa,
  2003. 0, fw_base, fw_len ) ) != 0 ) {
  2004. DBGC ( hermon, "Hermon %p could not map firmware: %s\n",
  2005. hermon, strerror ( rc ) );
  2006. goto err_map_fa;
  2007. }
  2008. /* Start firmware */
  2009. if ( ( rc = hermon_cmd_run_fw ( hermon ) ) != 0 ) {
  2010. DBGC ( hermon, "Hermon %p could not run firmware: %s\n",
  2011. hermon, strerror ( rc ) );
  2012. goto err_run_fw;
  2013. }
  2014. DBGC ( hermon, "Hermon %p firmware started\n", hermon );
  2015. return 0;
  2016. err_run_fw:
  2017. err_map_fa:
  2018. hermon_cmd_unmap_fa ( hermon );
  2019. err_alloc_fa:
  2020. err_query_fw:
  2021. return rc;
  2022. }
  2023. /**
  2024. * Stop firmware running
  2025. *
  2026. * @v hermon Hermon device
  2027. */
  2028. static void hermon_stop_firmware ( struct hermon *hermon ) {
  2029. int rc;
  2030. if ( ( rc = hermon_cmd_unmap_fa ( hermon ) ) != 0 ) {
  2031. DBGC ( hermon, "Hermon %p FATAL could not stop firmware: %s\n",
  2032. hermon, strerror ( rc ) );
  2033. /* Leak memory and return; at least we avoid corruption */
  2034. hermon->firmware_area = UNULL;
  2035. return;
  2036. }
  2037. }
  2038. /***************************************************************************
  2039. *
  2040. * Infinihost Context Memory management
  2041. *
  2042. ***************************************************************************
  2043. */
  2044. /**
  2045. * Get device limits
  2046. *
  2047. * @v hermon Hermon device
  2048. * @ret rc Return status code
  2049. */
  2050. static int hermon_get_cap ( struct hermon *hermon ) {
  2051. struct hermonprm_query_dev_cap dev_cap;
  2052. int rc;
  2053. if ( ( rc = hermon_cmd_query_dev_cap ( hermon, &dev_cap ) ) != 0 ) {
  2054. DBGC ( hermon, "Hermon %p could not get device limits: %s\n",
  2055. hermon, strerror ( rc ) );
  2056. return rc;
  2057. }
  2058. hermon->cap.cmpt_entry_size = MLX_GET ( &dev_cap, c_mpt_entry_sz );
  2059. hermon->cap.reserved_qps =
  2060. ( 1 << MLX_GET ( &dev_cap, log2_rsvd_qps ) );
  2061. hermon->cap.qpc_entry_size = MLX_GET ( &dev_cap, qpc_entry_sz );
  2062. hermon->cap.altc_entry_size = MLX_GET ( &dev_cap, altc_entry_sz );
  2063. hermon->cap.auxc_entry_size = MLX_GET ( &dev_cap, aux_entry_sz );
  2064. hermon->cap.reserved_srqs =
  2065. ( 1 << MLX_GET ( &dev_cap, log2_rsvd_srqs ) );
  2066. hermon->cap.srqc_entry_size = MLX_GET ( &dev_cap, srq_entry_sz );
  2067. hermon->cap.reserved_cqs =
  2068. ( 1 << MLX_GET ( &dev_cap, log2_rsvd_cqs ) );
  2069. hermon->cap.cqc_entry_size = MLX_GET ( &dev_cap, cqc_entry_sz );
  2070. hermon->cap.reserved_eqs = MLX_GET ( &dev_cap, num_rsvd_eqs );
  2071. if ( hermon->cap.reserved_eqs == 0 ) {
  2072. /* Backward compatibility */
  2073. hermon->cap.reserved_eqs =
  2074. ( 1 << MLX_GET ( &dev_cap, log2_rsvd_eqs ) );
  2075. }
  2076. hermon->cap.eqc_entry_size = MLX_GET ( &dev_cap, eqc_entry_sz );
  2077. hermon->cap.reserved_mtts =
  2078. ( 1 << MLX_GET ( &dev_cap, log2_rsvd_mtts ) );
  2079. hermon->cap.mtt_entry_size = MLX_GET ( &dev_cap, mtt_entry_sz );
  2080. hermon->cap.reserved_mrws =
  2081. ( 1 << MLX_GET ( &dev_cap, log2_rsvd_mrws ) );
  2082. hermon->cap.dmpt_entry_size = MLX_GET ( &dev_cap, d_mpt_entry_sz );
  2083. hermon->cap.reserved_uars = MLX_GET ( &dev_cap, num_rsvd_uars );
  2084. hermon->cap.num_ports = MLX_GET ( &dev_cap, num_ports );
  2085. hermon->cap.dpdp = MLX_GET ( &dev_cap, dpdp );
  2086. /* Sanity check */
  2087. if ( hermon->cap.num_ports > HERMON_MAX_PORTS ) {
  2088. DBGC ( hermon, "Hermon %p has %d ports (only %d supported)\n",
  2089. hermon, hermon->cap.num_ports, HERMON_MAX_PORTS );
  2090. hermon->cap.num_ports = HERMON_MAX_PORTS;
  2091. }
  2092. return 0;
  2093. }
  2094. /**
  2095. * Align ICM table
  2096. *
  2097. * @v icm_offset Current ICM offset
  2098. * @v len ICM table length
  2099. * @ret icm_offset ICM offset
  2100. */
  2101. static uint64_t icm_align ( uint64_t icm_offset, size_t len ) {
  2102. /* Round up to a multiple of the table size */
  2103. assert ( len == ( 1UL << ( fls ( len ) - 1 ) ) );
  2104. return ( ( icm_offset + len - 1 ) & ~( ( ( uint64_t ) len ) - 1 ) );
  2105. }
  2106. /**
  2107. * Map ICM (allocating if necessary)
  2108. *
  2109. * @v hermon Hermon device
  2110. * @v init_hca INIT_HCA structure to fill in
  2111. * @ret rc Return status code
  2112. */
  2113. static int hermon_map_icm ( struct hermon *hermon,
  2114. struct hermonprm_init_hca *init_hca ) {
  2115. struct hermonprm_scalar_parameter icm_size;
  2116. struct hermonprm_scalar_parameter icm_aux_size;
  2117. uint64_t icm_offset = 0;
  2118. unsigned int log_num_qps, log_num_srqs, log_num_cqs, log_num_eqs;
  2119. unsigned int log_num_mtts, log_num_mpts, log_num_mcs;
  2120. size_t cmpt_max_len;
  2121. size_t icm_len, icm_aux_len;
  2122. size_t len;
  2123. physaddr_t icm_phys;
  2124. int i;
  2125. int rc;
  2126. /*
  2127. * Start by carving up the ICM virtual address space
  2128. *
  2129. */
  2130. /* Calculate number of each object type within ICM */
  2131. log_num_qps = fls ( hermon->cap.reserved_qps +
  2132. HERMON_RSVD_SPECIAL_QPS + HERMON_MAX_QPS - 1 );
  2133. log_num_srqs = fls ( hermon->cap.reserved_srqs - 1 );
  2134. log_num_cqs = fls ( hermon->cap.reserved_cqs + HERMON_MAX_CQS - 1 );
  2135. log_num_eqs = fls ( hermon->cap.reserved_eqs + HERMON_MAX_EQS - 1 );
  2136. log_num_mtts = fls ( hermon->cap.reserved_mtts + HERMON_MAX_MTTS - 1 );
  2137. log_num_mpts = fls ( hermon->cap.reserved_mrws + 1 - 1 );
  2138. log_num_mcs = HERMON_LOG_MULTICAST_HASH_SIZE;
  2139. /* ICM starts with the cMPT tables, which are sparse */
  2140. cmpt_max_len = ( HERMON_CMPT_MAX_ENTRIES *
  2141. ( ( uint64_t ) hermon->cap.cmpt_entry_size ) );
  2142. len = ( ( ( ( 1 << log_num_qps ) * hermon->cap.cmpt_entry_size ) +
  2143. HERMON_PAGE_SIZE - 1 ) & ~( HERMON_PAGE_SIZE - 1 ) );
  2144. hermon->icm_map[HERMON_ICM_QP_CMPT].offset = icm_offset;
  2145. hermon->icm_map[HERMON_ICM_QP_CMPT].len = len;
  2146. icm_offset += cmpt_max_len;
  2147. len = ( ( ( ( 1 << log_num_srqs ) * hermon->cap.cmpt_entry_size ) +
  2148. HERMON_PAGE_SIZE - 1 ) & ~( HERMON_PAGE_SIZE - 1 ) );
  2149. hermon->icm_map[HERMON_ICM_SRQ_CMPT].offset = icm_offset;
  2150. hermon->icm_map[HERMON_ICM_SRQ_CMPT].len = len;
  2151. icm_offset += cmpt_max_len;
  2152. len = ( ( ( ( 1 << log_num_cqs ) * hermon->cap.cmpt_entry_size ) +
  2153. HERMON_PAGE_SIZE - 1 ) & ~( HERMON_PAGE_SIZE - 1 ) );
  2154. hermon->icm_map[HERMON_ICM_CQ_CMPT].offset = icm_offset;
  2155. hermon->icm_map[HERMON_ICM_CQ_CMPT].len = len;
  2156. icm_offset += cmpt_max_len;
  2157. len = ( ( ( ( 1 << log_num_eqs ) * hermon->cap.cmpt_entry_size ) +
  2158. HERMON_PAGE_SIZE - 1 ) & ~( HERMON_PAGE_SIZE - 1 ) );
  2159. hermon->icm_map[HERMON_ICM_EQ_CMPT].offset = icm_offset;
  2160. hermon->icm_map[HERMON_ICM_EQ_CMPT].len = len;
  2161. icm_offset += cmpt_max_len;
  2162. hermon->icm_map[HERMON_ICM_OTHER].offset = icm_offset;
  2163. /* Queue pair contexts */
  2164. len = ( ( 1 << log_num_qps ) * hermon->cap.qpc_entry_size );
  2165. icm_offset = icm_align ( icm_offset, len );
  2166. MLX_FILL_1 ( init_hca, 12,
  2167. qpc_eec_cqc_eqc_rdb_parameters.qpc_base_addr_h,
  2168. ( icm_offset >> 32 ) );
  2169. MLX_FILL_2 ( init_hca, 13,
  2170. qpc_eec_cqc_eqc_rdb_parameters.qpc_base_addr_l,
  2171. ( icm_offset >> 5 ),
  2172. qpc_eec_cqc_eqc_rdb_parameters.log_num_of_qp,
  2173. log_num_qps );
  2174. DBGC ( hermon, "Hermon %p ICM QPC is %d x %#zx at [%08llx,%08llx)\n",
  2175. hermon, ( 1 << log_num_qps ), hermon->cap.qpc_entry_size,
  2176. icm_offset, ( icm_offset + len ) );
  2177. icm_offset += len;
  2178. /* Extended alternate path contexts */
  2179. len = ( ( 1 << log_num_qps ) * hermon->cap.altc_entry_size );
  2180. icm_offset = icm_align ( icm_offset, len );
  2181. MLX_FILL_1 ( init_hca, 24,
  2182. qpc_eec_cqc_eqc_rdb_parameters.altc_base_addr_h,
  2183. ( icm_offset >> 32 ) );
  2184. MLX_FILL_1 ( init_hca, 25,
  2185. qpc_eec_cqc_eqc_rdb_parameters.altc_base_addr_l,
  2186. icm_offset );
  2187. DBGC ( hermon, "Hermon %p ICM ALTC is %d x %#zx at [%08llx,%08llx)\n",
  2188. hermon, ( 1 << log_num_qps ), hermon->cap.altc_entry_size,
  2189. icm_offset, ( icm_offset + len ) );
  2190. icm_offset += len;
  2191. /* Extended auxiliary contexts */
  2192. len = ( ( 1 << log_num_qps ) * hermon->cap.auxc_entry_size );
  2193. icm_offset = icm_align ( icm_offset, len );
  2194. MLX_FILL_1 ( init_hca, 28,
  2195. qpc_eec_cqc_eqc_rdb_parameters.auxc_base_addr_h,
  2196. ( icm_offset >> 32 ) );
  2197. MLX_FILL_1 ( init_hca, 29,
  2198. qpc_eec_cqc_eqc_rdb_parameters.auxc_base_addr_l,
  2199. icm_offset );
  2200. DBGC ( hermon, "Hermon %p ICM AUXC is %d x %#zx at [%08llx,%08llx)\n",
  2201. hermon, ( 1 << log_num_qps ), hermon->cap.auxc_entry_size,
  2202. icm_offset, ( icm_offset + len ) );
  2203. icm_offset += len;
  2204. /* Shared receive queue contexts */
  2205. len = ( ( 1 << log_num_srqs ) * hermon->cap.srqc_entry_size );
  2206. icm_offset = icm_align ( icm_offset, len );
  2207. MLX_FILL_1 ( init_hca, 18,
  2208. qpc_eec_cqc_eqc_rdb_parameters.srqc_base_addr_h,
  2209. ( icm_offset >> 32 ) );
  2210. MLX_FILL_2 ( init_hca, 19,
  2211. qpc_eec_cqc_eqc_rdb_parameters.srqc_base_addr_l,
  2212. ( icm_offset >> 5 ),
  2213. qpc_eec_cqc_eqc_rdb_parameters.log_num_of_srq,
  2214. log_num_srqs );
  2215. DBGC ( hermon, "Hermon %p ICM SRQC is %d x %#zx at [%08llx,%08llx)\n",
  2216. hermon, ( 1 << log_num_srqs ), hermon->cap.srqc_entry_size,
  2217. icm_offset, ( icm_offset + len ) );
  2218. icm_offset += len;
  2219. /* Completion queue contexts */
  2220. len = ( ( 1 << log_num_cqs ) * hermon->cap.cqc_entry_size );
  2221. icm_offset = icm_align ( icm_offset, len );
  2222. MLX_FILL_1 ( init_hca, 20,
  2223. qpc_eec_cqc_eqc_rdb_parameters.cqc_base_addr_h,
  2224. ( icm_offset >> 32 ) );
  2225. MLX_FILL_2 ( init_hca, 21,
  2226. qpc_eec_cqc_eqc_rdb_parameters.cqc_base_addr_l,
  2227. ( icm_offset >> 5 ),
  2228. qpc_eec_cqc_eqc_rdb_parameters.log_num_of_cq,
  2229. log_num_cqs );
  2230. DBGC ( hermon, "Hermon %p ICM CQC is %d x %#zx at [%08llx,%08llx)\n",
  2231. hermon, ( 1 << log_num_cqs ), hermon->cap.cqc_entry_size,
  2232. icm_offset, ( icm_offset + len ) );
  2233. icm_offset += len;
  2234. /* Event queue contexts */
  2235. len = ( ( 1 << log_num_eqs ) * hermon->cap.eqc_entry_size );
  2236. icm_offset = icm_align ( icm_offset, len );
  2237. MLX_FILL_1 ( init_hca, 32,
  2238. qpc_eec_cqc_eqc_rdb_parameters.eqc_base_addr_h,
  2239. ( icm_offset >> 32 ) );
  2240. MLX_FILL_2 ( init_hca, 33,
  2241. qpc_eec_cqc_eqc_rdb_parameters.eqc_base_addr_l,
  2242. ( icm_offset >> 5 ),
  2243. qpc_eec_cqc_eqc_rdb_parameters.log_num_of_eq,
  2244. log_num_eqs );
  2245. DBGC ( hermon, "Hermon %p ICM EQC is %d x %#zx at [%08llx,%08llx)\n",
  2246. hermon, ( 1 << log_num_eqs ), hermon->cap.eqc_entry_size,
  2247. icm_offset, ( icm_offset + len ) );
  2248. icm_offset += len;
  2249. /* Memory translation table */
  2250. len = ( ( 1 << log_num_mtts ) * hermon->cap.mtt_entry_size );
  2251. icm_offset = icm_align ( icm_offset, len );
  2252. MLX_FILL_1 ( init_hca, 64,
  2253. tpt_parameters.mtt_base_addr_h, ( icm_offset >> 32 ) );
  2254. MLX_FILL_1 ( init_hca, 65,
  2255. tpt_parameters.mtt_base_addr_l, icm_offset );
  2256. DBGC ( hermon, "Hermon %p ICM MTT is %d x %#zx at [%08llx,%08llx)\n",
  2257. hermon, ( 1 << log_num_mtts ), hermon->cap.mtt_entry_size,
  2258. icm_offset, ( icm_offset + len ) );
  2259. icm_offset += len;
  2260. /* Memory protection table */
  2261. len = ( ( 1 << log_num_mpts ) * hermon->cap.dmpt_entry_size );
  2262. icm_offset = icm_align ( icm_offset, len );
  2263. MLX_FILL_1 ( init_hca, 60,
  2264. tpt_parameters.dmpt_base_adr_h, ( icm_offset >> 32 ) );
  2265. MLX_FILL_1 ( init_hca, 61,
  2266. tpt_parameters.dmpt_base_adr_l, icm_offset );
  2267. MLX_FILL_1 ( init_hca, 62,
  2268. tpt_parameters.log_dmpt_sz, log_num_mpts );
  2269. DBGC ( hermon, "Hermon %p ICM DMPT is %d x %#zx at [%08llx,%08llx)\n",
  2270. hermon, ( 1 << log_num_mpts ), hermon->cap.dmpt_entry_size,
  2271. icm_offset, ( icm_offset + len ) );
  2272. icm_offset += len;
  2273. /* Multicast table */
  2274. len = ( ( 1 << log_num_mcs ) * sizeof ( struct hermonprm_mcg_entry ) );
  2275. icm_offset = icm_align ( icm_offset, len );
  2276. MLX_FILL_1 ( init_hca, 48,
  2277. multicast_parameters.mc_base_addr_h,
  2278. ( icm_offset >> 32 ) );
  2279. MLX_FILL_1 ( init_hca, 49,
  2280. multicast_parameters.mc_base_addr_l, icm_offset );
  2281. MLX_FILL_1 ( init_hca, 52,
  2282. multicast_parameters.log_mc_table_entry_sz,
  2283. fls ( sizeof ( struct hermonprm_mcg_entry ) - 1 ) );
  2284. MLX_FILL_1 ( init_hca, 53,
  2285. multicast_parameters.log_mc_table_hash_sz, log_num_mcs );
  2286. MLX_FILL_1 ( init_hca, 54,
  2287. multicast_parameters.log_mc_table_sz, log_num_mcs );
  2288. DBGC ( hermon, "Hermon %p ICM MC is %d x %#zx at [%08llx,%08llx)\n",
  2289. hermon, ( 1 << log_num_mcs ),
  2290. sizeof ( struct hermonprm_mcg_entry ),
  2291. icm_offset, ( icm_offset + len ) );
  2292. icm_offset += len;
  2293. hermon->icm_map[HERMON_ICM_OTHER].len =
  2294. ( icm_offset - hermon->icm_map[HERMON_ICM_OTHER].offset );
  2295. /*
  2296. * Allocate and map physical memory for (portions of) ICM
  2297. *
  2298. * Map is:
  2299. * ICM AUX area (aligned to its own size)
  2300. * cMPT areas
  2301. * Other areas
  2302. */
  2303. /* Calculate physical memory required for ICM */
  2304. icm_len = 0;
  2305. for ( i = 0 ; i < HERMON_ICM_NUM_REGIONS ; i++ ) {
  2306. icm_len += hermon->icm_map[i].len;
  2307. }
  2308. /* Get ICM auxiliary area size */
  2309. memset ( &icm_size, 0, sizeof ( icm_size ) );
  2310. MLX_FILL_1 ( &icm_size, 0, value_hi, ( icm_offset >> 32 ) );
  2311. MLX_FILL_1 ( &icm_size, 1, value, icm_offset );
  2312. if ( ( rc = hermon_cmd_set_icm_size ( hermon, &icm_size,
  2313. &icm_aux_size ) ) != 0 ) {
  2314. DBGC ( hermon, "Hermon %p could not set ICM size: %s\n",
  2315. hermon, strerror ( rc ) );
  2316. goto err_set_icm_size;
  2317. }
  2318. icm_aux_len = ( MLX_GET ( &icm_aux_size, value ) * HERMON_PAGE_SIZE );
  2319. /* Allocate ICM data and auxiliary area */
  2320. DBGC ( hermon, "Hermon %p requires %zd kB ICM and %zd kB AUX ICM\n",
  2321. hermon, ( icm_len / 1024 ), ( icm_aux_len / 1024 ) );
  2322. if ( ! hermon->icm ) {
  2323. hermon->icm_len = icm_len;
  2324. hermon->icm_aux_len = icm_aux_len;
  2325. hermon->icm = umalloc ( hermon->icm_aux_len + hermon->icm_len );
  2326. if ( ! hermon->icm ) {
  2327. rc = -ENOMEM;
  2328. goto err_alloc;
  2329. }
  2330. } else {
  2331. assert ( hermon->icm_len == icm_len );
  2332. assert ( hermon->icm_aux_len == icm_aux_len );
  2333. }
  2334. icm_phys = user_to_phys ( hermon->icm, 0 );
  2335. /* Map ICM auxiliary area */
  2336. DBGC ( hermon, "Hermon %p mapping ICM AUX => %08lx\n",
  2337. hermon, icm_phys );
  2338. if ( ( rc = hermon_map_vpm ( hermon, hermon_cmd_map_icm_aux,
  2339. 0, icm_phys, icm_aux_len ) ) != 0 ) {
  2340. DBGC ( hermon, "Hermon %p could not map AUX ICM: %s\n",
  2341. hermon, strerror ( rc ) );
  2342. goto err_map_icm_aux;
  2343. }
  2344. icm_phys += icm_aux_len;
  2345. /* MAP ICM area */
  2346. for ( i = 0 ; i < HERMON_ICM_NUM_REGIONS ; i++ ) {
  2347. DBGC ( hermon, "Hermon %p mapping ICM %llx+%zx => %08lx\n",
  2348. hermon, hermon->icm_map[i].offset,
  2349. hermon->icm_map[i].len, icm_phys );
  2350. if ( ( rc = hermon_map_vpm ( hermon, hermon_cmd_map_icm,
  2351. hermon->icm_map[i].offset,
  2352. icm_phys,
  2353. hermon->icm_map[i].len ) ) != 0 ){
  2354. DBGC ( hermon, "Hermon %p could not map ICM: %s\n",
  2355. hermon, strerror ( rc ) );
  2356. goto err_map_icm;
  2357. }
  2358. icm_phys += hermon->icm_map[i].len;
  2359. }
  2360. return 0;
  2361. err_map_icm:
  2362. assert ( i == 0 ); /* We don't handle partial failure at present */
  2363. err_map_icm_aux:
  2364. hermon_cmd_unmap_icm_aux ( hermon );
  2365. err_alloc:
  2366. err_set_icm_size:
  2367. return rc;
  2368. }
  2369. /**
  2370. * Unmap ICM
  2371. *
  2372. * @v hermon Hermon device
  2373. */
  2374. static void hermon_unmap_icm ( struct hermon *hermon ) {
  2375. struct hermonprm_scalar_parameter unmap_icm;
  2376. int i;
  2377. for ( i = ( HERMON_ICM_NUM_REGIONS - 1 ) ; i >= 0 ; i-- ) {
  2378. memset ( &unmap_icm, 0, sizeof ( unmap_icm ) );
  2379. MLX_FILL_1 ( &unmap_icm, 0, value_hi,
  2380. ( hermon->icm_map[i].offset >> 32 ) );
  2381. MLX_FILL_1 ( &unmap_icm, 1, value,
  2382. hermon->icm_map[i].offset );
  2383. hermon_cmd_unmap_icm ( hermon,
  2384. ( 1 << fls ( ( hermon->icm_map[i].len /
  2385. HERMON_PAGE_SIZE ) - 1)),
  2386. &unmap_icm );
  2387. }
  2388. hermon_cmd_unmap_icm_aux ( hermon );
  2389. }
  2390. /***************************************************************************
  2391. *
  2392. * Initialisation and teardown
  2393. *
  2394. ***************************************************************************
  2395. */
  2396. /**
  2397. * Reset device
  2398. *
  2399. * @v hermon Hermon device
  2400. */
  2401. static void hermon_reset ( struct hermon *hermon ) {
  2402. struct pci_device *pci = hermon->pci;
  2403. struct pci_config_backup backup;
  2404. static const uint8_t backup_exclude[] =
  2405. PCI_CONFIG_BACKUP_EXCLUDE ( 0x58, 0x5c );
  2406. /* Perform device reset and preserve PCI configuration */
  2407. pci_backup ( pci, &backup, backup_exclude );
  2408. writel ( HERMON_RESET_MAGIC,
  2409. ( hermon->config + HERMON_RESET_OFFSET ) );
  2410. mdelay ( HERMON_RESET_WAIT_TIME_MS );
  2411. pci_restore ( pci, &backup, backup_exclude );
  2412. /* Reset command interface toggle */
  2413. hermon->toggle = 0;
  2414. }
  2415. /**
  2416. * Set up memory protection table
  2417. *
  2418. * @v hermon Hermon device
  2419. * @ret rc Return status code
  2420. */
  2421. static int hermon_setup_mpt ( struct hermon *hermon ) {
  2422. struct hermonprm_mpt mpt;
  2423. uint32_t key;
  2424. int rc;
  2425. /* Derive key */
  2426. key = ( hermon->cap.reserved_mrws | HERMON_MKEY_PREFIX );
  2427. hermon->lkey = ( ( key << 8 ) | ( key >> 24 ) );
  2428. /* Initialise memory protection table */
  2429. memset ( &mpt, 0, sizeof ( mpt ) );
  2430. MLX_FILL_7 ( &mpt, 0,
  2431. atomic, 1,
  2432. rw, 1,
  2433. rr, 1,
  2434. lw, 1,
  2435. lr, 1,
  2436. pa, 1,
  2437. r_w, 1 );
  2438. MLX_FILL_1 ( &mpt, 2, mem_key, key );
  2439. MLX_FILL_1 ( &mpt, 3,
  2440. pd, HERMON_GLOBAL_PD );
  2441. MLX_FILL_1 ( &mpt, 10, len64, 1 );
  2442. if ( ( rc = hermon_cmd_sw2hw_mpt ( hermon,
  2443. hermon->cap.reserved_mrws,
  2444. &mpt ) ) != 0 ) {
  2445. DBGC ( hermon, "Hermon %p could not set up MPT: %s\n",
  2446. hermon, strerror ( rc ) );
  2447. return rc;
  2448. }
  2449. return 0;
  2450. }
  2451. /**
  2452. * Configure special queue pairs
  2453. *
  2454. * @v hermon Hermon device
  2455. * @ret rc Return status code
  2456. */
  2457. static int hermon_configure_special_qps ( struct hermon *hermon ) {
  2458. int rc;
  2459. /* Special QP block must be aligned on its own size */
  2460. hermon->special_qpn_base = ( ( hermon->cap.reserved_qps +
  2461. HERMON_NUM_SPECIAL_QPS - 1 )
  2462. & ~( HERMON_NUM_SPECIAL_QPS - 1 ) );
  2463. hermon->qpn_base = ( hermon->special_qpn_base +
  2464. HERMON_NUM_SPECIAL_QPS );
  2465. DBGC ( hermon, "Hermon %p special QPs at [%lx,%lx]\n", hermon,
  2466. hermon->special_qpn_base, ( hermon->qpn_base - 1 ) );
  2467. /* Issue command to configure special QPs */
  2468. if ( ( rc = hermon_cmd_conf_special_qp ( hermon, 0x00,
  2469. hermon->special_qpn_base ) ) != 0 ) {
  2470. DBGC ( hermon, "Hermon %p could not configure special QPs: "
  2471. "%s\n", hermon, strerror ( rc ) );
  2472. return rc;
  2473. }
  2474. return 0;
  2475. }
  2476. /**
  2477. * Start Hermon device
  2478. *
  2479. * @v hermon Hermon device
  2480. * @v running Firmware is already running
  2481. * @ret rc Return status code
  2482. */
  2483. static int hermon_start ( struct hermon *hermon, int running ) {
  2484. struct hermonprm_init_hca init_hca;
  2485. unsigned int i;
  2486. int rc;
  2487. /* Start firmware if not already running */
  2488. if ( ! running ) {
  2489. if ( ( rc = hermon_start_firmware ( hermon ) ) != 0 )
  2490. goto err_start_firmware;
  2491. }
  2492. /* Allocate and map ICM */
  2493. memset ( &init_hca, 0, sizeof ( init_hca ) );
  2494. if ( ( rc = hermon_map_icm ( hermon, &init_hca ) ) != 0 )
  2495. goto err_map_icm;
  2496. /* Initialise HCA */
  2497. MLX_FILL_1 ( &init_hca, 0, version, 0x02 /* "Must be 0x02" */ );
  2498. MLX_FILL_1 ( &init_hca, 5, udp, 1 );
  2499. MLX_FILL_1 ( &init_hca, 74, uar_parameters.log_max_uars, 8 );
  2500. if ( ( rc = hermon_cmd_init_hca ( hermon, &init_hca ) ) != 0 ) {
  2501. DBGC ( hermon, "Hermon %p could not initialise HCA: %s\n",
  2502. hermon, strerror ( rc ) );
  2503. goto err_init_hca;
  2504. }
  2505. /* Set up memory protection */
  2506. if ( ( rc = hermon_setup_mpt ( hermon ) ) != 0 )
  2507. goto err_setup_mpt;
  2508. for ( i = 0 ; i < hermon->cap.num_ports ; i++ )
  2509. hermon->port[i].ibdev->rdma_key = hermon->lkey;
  2510. /* Set up event queue */
  2511. if ( ( rc = hermon_create_eq ( hermon ) ) != 0 )
  2512. goto err_create_eq;
  2513. /* Configure special QPs */
  2514. if ( ( rc = hermon_configure_special_qps ( hermon ) ) != 0 )
  2515. goto err_conf_special_qps;
  2516. return 0;
  2517. err_conf_special_qps:
  2518. hermon_destroy_eq ( hermon );
  2519. err_create_eq:
  2520. err_setup_mpt:
  2521. hermon_cmd_close_hca ( hermon );
  2522. err_init_hca:
  2523. hermon_unmap_icm ( hermon );
  2524. err_map_icm:
  2525. hermon_stop_firmware ( hermon );
  2526. err_start_firmware:
  2527. return rc;
  2528. }
  2529. /**
  2530. * Stop Hermon device
  2531. *
  2532. * @v hermon Hermon device
  2533. */
  2534. static void hermon_stop ( struct hermon *hermon ) {
  2535. hermon_destroy_eq ( hermon );
  2536. hermon_cmd_close_hca ( hermon );
  2537. hermon_unmap_icm ( hermon );
  2538. hermon_stop_firmware ( hermon );
  2539. hermon_reset ( hermon );
  2540. }
  2541. /**
  2542. * Open Hermon device
  2543. *
  2544. * @v hermon Hermon device
  2545. * @ret rc Return status code
  2546. */
  2547. static int hermon_open ( struct hermon *hermon ) {
  2548. int rc;
  2549. /* Start device if applicable */
  2550. if ( hermon->open_count == 0 ) {
  2551. if ( ( rc = hermon_start ( hermon, 0 ) ) != 0 )
  2552. return rc;
  2553. }
  2554. /* Increment open counter */
  2555. hermon->open_count++;
  2556. return 0;
  2557. }
  2558. /**
  2559. * Close Hermon device
  2560. *
  2561. * @v hermon Hermon device
  2562. */
  2563. static void hermon_close ( struct hermon *hermon ) {
  2564. /* Decrement open counter */
  2565. assert ( hermon->open_count != 0 );
  2566. hermon->open_count--;
  2567. /* Stop device if applicable */
  2568. if ( hermon->open_count == 0 )
  2569. hermon_stop ( hermon );
  2570. }
  2571. /***************************************************************************
  2572. *
  2573. * Infiniband link-layer operations
  2574. *
  2575. ***************************************************************************
  2576. */
  2577. /**
  2578. * Initialise Infiniband link
  2579. *
  2580. * @v ibdev Infiniband device
  2581. * @ret rc Return status code
  2582. */
  2583. static int hermon_ib_open ( struct ib_device *ibdev ) {
  2584. struct hermon *hermon = ib_get_drvdata ( ibdev );
  2585. union hermonprm_set_port set_port;
  2586. int rc;
  2587. /* Open hardware */
  2588. if ( ( rc = hermon_open ( hermon ) ) != 0 )
  2589. goto err_open;
  2590. /* Set port parameters */
  2591. memset ( &set_port, 0, sizeof ( set_port ) );
  2592. MLX_FILL_8 ( &set_port.ib, 0,
  2593. mmc, 1,
  2594. mvc, 1,
  2595. mp, 1,
  2596. mg, 1,
  2597. mtu_cap, IB_MTU_2048,
  2598. vl_cap, IB_VL_0,
  2599. rcm, 1,
  2600. lss, 1 );
  2601. MLX_FILL_2 ( &set_port.ib, 10,
  2602. max_pkey, 1,
  2603. max_gid, 1 );
  2604. MLX_FILL_1 ( &set_port.ib, 28,
  2605. link_speed_supported, 1 );
  2606. if ( ( rc = hermon_cmd_set_port ( hermon, 0, ibdev->port,
  2607. &set_port ) ) != 0 ) {
  2608. DBGC ( hermon, "Hermon %p port %d could not set port: %s\n",
  2609. hermon, ibdev->port, strerror ( rc ) );
  2610. goto err_set_port;
  2611. }
  2612. /* Initialise port */
  2613. if ( ( rc = hermon_cmd_init_port ( hermon, ibdev->port ) ) != 0 ) {
  2614. DBGC ( hermon, "Hermon %p port %d could not initialise port: "
  2615. "%s\n", hermon, ibdev->port, strerror ( rc ) );
  2616. goto err_init_port;
  2617. }
  2618. /* Update MAD parameters */
  2619. ib_smc_update ( ibdev, hermon_mad );
  2620. return 0;
  2621. err_init_port:
  2622. err_set_port:
  2623. hermon_close ( hermon );
  2624. err_open:
  2625. return rc;
  2626. }
  2627. /**
  2628. * Close Infiniband link
  2629. *
  2630. * @v ibdev Infiniband device
  2631. */
  2632. static void hermon_ib_close ( struct ib_device *ibdev ) {
  2633. struct hermon *hermon = ib_get_drvdata ( ibdev );
  2634. int rc;
  2635. /* Close port */
  2636. if ( ( rc = hermon_cmd_close_port ( hermon, ibdev->port ) ) != 0 ) {
  2637. DBGC ( hermon, "Hermon %p port %d could not close port: %s\n",
  2638. hermon, ibdev->port, strerror ( rc ) );
  2639. /* Nothing we can do about this */
  2640. }
  2641. /* Close hardware */
  2642. hermon_close ( hermon );
  2643. }
  2644. /**
  2645. * Inform embedded subnet management agent of a received MAD
  2646. *
  2647. * @v ibdev Infiniband device
  2648. * @v mad MAD
  2649. * @ret rc Return status code
  2650. */
  2651. static int hermon_inform_sma ( struct ib_device *ibdev,
  2652. union ib_mad *mad ) {
  2653. int rc;
  2654. /* Send the MAD to the embedded SMA */
  2655. if ( ( rc = hermon_mad ( ibdev, mad ) ) != 0 )
  2656. return rc;
  2657. /* Update parameters held in software */
  2658. ib_smc_update ( ibdev, hermon_mad );
  2659. return 0;
  2660. }
  2661. /***************************************************************************
  2662. *
  2663. * Multicast group operations
  2664. *
  2665. ***************************************************************************
  2666. */
  2667. /**
  2668. * Attach to multicast group
  2669. *
  2670. * @v ibdev Infiniband device
  2671. * @v qp Queue pair
  2672. * @v gid Multicast GID
  2673. * @ret rc Return status code
  2674. */
  2675. static int hermon_mcast_attach ( struct ib_device *ibdev,
  2676. struct ib_queue_pair *qp,
  2677. union ib_gid *gid ) {
  2678. struct hermon *hermon = ib_get_drvdata ( ibdev );
  2679. struct hermonprm_mgm_hash hash;
  2680. struct hermonprm_mcg_entry mcg;
  2681. unsigned int index;
  2682. int rc;
  2683. /* Generate hash table index */
  2684. if ( ( rc = hermon_cmd_mgid_hash ( hermon, gid, &hash ) ) != 0 ) {
  2685. DBGC ( hermon, "Hermon %p could not hash GID: %s\n",
  2686. hermon, strerror ( rc ) );
  2687. return rc;
  2688. }
  2689. index = MLX_GET ( &hash, hash );
  2690. /* Check for existing hash table entry */
  2691. if ( ( rc = hermon_cmd_read_mcg ( hermon, index, &mcg ) ) != 0 ) {
  2692. DBGC ( hermon, "Hermon %p could not read MCG %#x: %s\n",
  2693. hermon, index, strerror ( rc ) );
  2694. return rc;
  2695. }
  2696. if ( MLX_GET ( &mcg, hdr.members_count ) != 0 ) {
  2697. /* FIXME: this implementation allows only a single QP
  2698. * per multicast group, and doesn't handle hash
  2699. * collisions. Sufficient for IPoIB but may need to
  2700. * be extended in future.
  2701. */
  2702. DBGC ( hermon, "Hermon %p MGID index %#x already in use\n",
  2703. hermon, index );
  2704. return -EBUSY;
  2705. }
  2706. /* Update hash table entry */
  2707. MLX_FILL_1 ( &mcg, 1, hdr.members_count, 1 );
  2708. MLX_FILL_1 ( &mcg, 8, qp[0].qpn, qp->qpn );
  2709. memcpy ( &mcg.u.dwords[4], gid, sizeof ( *gid ) );
  2710. if ( ( rc = hermon_cmd_write_mcg ( hermon, index, &mcg ) ) != 0 ) {
  2711. DBGC ( hermon, "Hermon %p could not write MCG %#x: %s\n",
  2712. hermon, index, strerror ( rc ) );
  2713. return rc;
  2714. }
  2715. return 0;
  2716. }
  2717. /**
  2718. * Detach from multicast group
  2719. *
  2720. * @v ibdev Infiniband device
  2721. * @v qp Queue pair
  2722. * @v gid Multicast GID
  2723. */
  2724. static void hermon_mcast_detach ( struct ib_device *ibdev,
  2725. struct ib_queue_pair *qp __unused,
  2726. union ib_gid *gid ) {
  2727. struct hermon *hermon = ib_get_drvdata ( ibdev );
  2728. struct hermonprm_mgm_hash hash;
  2729. struct hermonprm_mcg_entry mcg;
  2730. unsigned int index;
  2731. int rc;
  2732. /* Generate hash table index */
  2733. if ( ( rc = hermon_cmd_mgid_hash ( hermon, gid, &hash ) ) != 0 ) {
  2734. DBGC ( hermon, "Hermon %p could not hash GID: %s\n",
  2735. hermon, strerror ( rc ) );
  2736. return;
  2737. }
  2738. index = MLX_GET ( &hash, hash );
  2739. /* Clear hash table entry */
  2740. memset ( &mcg, 0, sizeof ( mcg ) );
  2741. if ( ( rc = hermon_cmd_write_mcg ( hermon, index, &mcg ) ) != 0 ) {
  2742. DBGC ( hermon, "Hermon %p could not write MCG %#x: %s\n",
  2743. hermon, index, strerror ( rc ) );
  2744. return;
  2745. }
  2746. }
  2747. /** Hermon Infiniband operations */
  2748. static struct ib_device_operations hermon_ib_operations = {
  2749. .create_cq = hermon_create_cq,
  2750. .destroy_cq = hermon_destroy_cq,
  2751. .create_qp = hermon_create_qp,
  2752. .modify_qp = hermon_modify_qp,
  2753. .destroy_qp = hermon_destroy_qp,
  2754. .post_send = hermon_post_send,
  2755. .post_recv = hermon_post_recv,
  2756. .poll_cq = hermon_poll_cq,
  2757. .poll_eq = hermon_poll_eq,
  2758. .open = hermon_ib_open,
  2759. .close = hermon_ib_close,
  2760. .mcast_attach = hermon_mcast_attach,
  2761. .mcast_detach = hermon_mcast_detach,
  2762. .set_port_info = hermon_inform_sma,
  2763. .set_pkey_table = hermon_inform_sma,
  2764. };
  2765. /**
  2766. * Register Hermon Infiniband device
  2767. *
  2768. * @v hermon Hermon device
  2769. * @v port Hermon port
  2770. * @ret rc Return status code
  2771. */
  2772. static int hermon_register_ibdev ( struct hermon *hermon,
  2773. struct hermon_port *port ) {
  2774. struct ib_device *ibdev = port->ibdev;
  2775. int rc;
  2776. /* Initialise parameters using SMC */
  2777. ib_smc_init ( ibdev, hermon_mad );
  2778. /* Register Infiniband device */
  2779. if ( ( rc = register_ibdev ( ibdev ) ) != 0 ) {
  2780. DBGC ( hermon, "Hermon %p port %d could not register IB "
  2781. "device: %s\n", hermon, ibdev->port, strerror ( rc ) );
  2782. return rc;
  2783. }
  2784. return 0;
  2785. }
  2786. /**
  2787. * Handle Hermon Infiniband device port state change
  2788. *
  2789. * @v hermon Hermon device
  2790. * @v port Hermon port
  2791. * @v link_up Link is up
  2792. */
  2793. static void hermon_state_change_ibdev ( struct hermon *hermon __unused,
  2794. struct hermon_port *port,
  2795. int link_up __unused ) {
  2796. struct ib_device *ibdev = port->ibdev;
  2797. /* Update MAD parameters */
  2798. ib_smc_update ( ibdev, hermon_mad );
  2799. }
  2800. /**
  2801. * Unregister Hermon Infiniband device
  2802. *
  2803. * @v hermon Hermon device
  2804. * @v port Hermon port
  2805. */
  2806. static void hermon_unregister_ibdev ( struct hermon *hermon __unused,
  2807. struct hermon_port *port ) {
  2808. struct ib_device *ibdev = port->ibdev;
  2809. unregister_ibdev ( ibdev );
  2810. }
  2811. /** Hermon Infiniband port type */
  2812. static struct hermon_port_type hermon_port_type_ib = {
  2813. .register_dev = hermon_register_ibdev,
  2814. .state_change = hermon_state_change_ibdev,
  2815. .unregister_dev = hermon_unregister_ibdev,
  2816. };
  2817. /***************************************************************************
  2818. *
  2819. * Ethernet operation
  2820. *
  2821. ***************************************************************************
  2822. */
  2823. /** Number of Hermon Ethernet send work queue entries */
  2824. #define HERMON_ETH_NUM_SEND_WQES 2
  2825. /** Number of Hermon Ethernet receive work queue entries */
  2826. #define HERMON_ETH_NUM_RECV_WQES 4
  2827. /** Number of Hermon Ethernet completion entries */
  2828. #define HERMON_ETH_NUM_CQES 8
  2829. /**
  2830. * Transmit packet via Hermon Ethernet device
  2831. *
  2832. * @v netdev Network device
  2833. * @v iobuf I/O buffer
  2834. * @ret rc Return status code
  2835. */
  2836. static int hermon_eth_transmit ( struct net_device *netdev,
  2837. struct io_buffer *iobuf ) {
  2838. struct hermon_port *port = netdev->priv;
  2839. struct ib_device *ibdev = port->ibdev;
  2840. struct hermon *hermon = ib_get_drvdata ( ibdev );
  2841. int rc;
  2842. /* Transmit packet */
  2843. if ( ( rc = ib_post_send ( ibdev, port->eth_qp, NULL,
  2844. iobuf ) ) != 0 ) {
  2845. DBGC ( hermon, "Hermon %p port %d could not transmit: %s\n",
  2846. hermon, ibdev->port, strerror ( rc ) );
  2847. return rc;
  2848. }
  2849. return 0;
  2850. }
  2851. /** Hermon Ethernet queue pair operations */
  2852. static struct ib_queue_pair_operations hermon_eth_qp_op = {
  2853. .alloc_iob = alloc_iob,
  2854. };
  2855. /**
  2856. * Handle Hermon Ethernet device send completion
  2857. *
  2858. * @v ibdev Infiniband device
  2859. * @v qp Queue pair
  2860. * @v iobuf I/O buffer
  2861. * @v rc Completion status code
  2862. */
  2863. static void hermon_eth_complete_send ( struct ib_device *ibdev __unused,
  2864. struct ib_queue_pair *qp,
  2865. struct io_buffer *iobuf, int rc ) {
  2866. struct net_device *netdev = ib_qp_get_ownerdata ( qp );
  2867. netdev_tx_complete_err ( netdev, iobuf, rc );
  2868. }
  2869. /**
  2870. * Handle Hermon Ethernet device receive completion
  2871. *
  2872. * @v ibdev Infiniband device
  2873. * @v qp Queue pair
  2874. * @v dest Destination address vector, or NULL
  2875. * @v source Source address vector, or NULL
  2876. * @v iobuf I/O buffer
  2877. * @v rc Completion status code
  2878. */
  2879. static void hermon_eth_complete_recv ( struct ib_device *ibdev __unused,
  2880. struct ib_queue_pair *qp,
  2881. struct ib_address_vector *dest __unused,
  2882. struct ib_address_vector *source,
  2883. struct io_buffer *iobuf, int rc ) {
  2884. struct net_device *netdev = ib_qp_get_ownerdata ( qp );
  2885. struct net_device *vlan;
  2886. /* Find VLAN device, if applicable */
  2887. if ( source->vlan_present ) {
  2888. if ( ( vlan = vlan_find ( netdev, source->vlan ) ) != NULL ) {
  2889. netdev = vlan;
  2890. } else if ( rc == 0 ) {
  2891. rc = -ENODEV;
  2892. }
  2893. }
  2894. /* Hand off to network layer */
  2895. if ( rc == 0 ) {
  2896. netdev_rx ( netdev, iobuf );
  2897. } else {
  2898. netdev_rx_err ( netdev, iobuf, rc );
  2899. }
  2900. }
  2901. /** Hermon Ethernet device completion operations */
  2902. static struct ib_completion_queue_operations hermon_eth_cq_op = {
  2903. .complete_send = hermon_eth_complete_send,
  2904. .complete_recv = hermon_eth_complete_recv,
  2905. };
  2906. /**
  2907. * Poll Hermon Ethernet device
  2908. *
  2909. * @v netdev Network device
  2910. */
  2911. static void hermon_eth_poll ( struct net_device *netdev ) {
  2912. struct hermon_port *port = netdev->priv;
  2913. struct ib_device *ibdev = port->ibdev;
  2914. ib_poll_eq ( ibdev );
  2915. }
  2916. /**
  2917. * Open Hermon Ethernet device
  2918. *
  2919. * @v netdev Network device
  2920. * @ret rc Return status code
  2921. */
  2922. static int hermon_eth_open ( struct net_device *netdev ) {
  2923. struct hermon_port *port = netdev->priv;
  2924. struct ib_device *ibdev = port->ibdev;
  2925. struct hermon *hermon = ib_get_drvdata ( ibdev );
  2926. union hermonprm_set_port set_port;
  2927. int rc;
  2928. /* Open hardware */
  2929. if ( ( rc = hermon_open ( hermon ) ) != 0 )
  2930. goto err_open;
  2931. /* Allocate completion queue */
  2932. if ( ( rc = ib_create_cq ( ibdev, HERMON_ETH_NUM_CQES,
  2933. &hermon_eth_cq_op, &port->eth_cq ) ) != 0 ) {
  2934. DBGC ( hermon, "Hermon %p port %d could not create completion "
  2935. "queue: %s\n", hermon, ibdev->port, strerror ( rc ) );
  2936. goto err_create_cq;
  2937. }
  2938. /* Allocate queue pair */
  2939. if ( ( rc = ib_create_qp ( ibdev, IB_QPT_ETH, HERMON_ETH_NUM_SEND_WQES,
  2940. port->eth_cq, HERMON_ETH_NUM_RECV_WQES,
  2941. port->eth_cq, &hermon_eth_qp_op,
  2942. netdev->name, &port->eth_qp ) ) != 0 ) {
  2943. DBGC ( hermon, "Hermon %p port %d could not create queue "
  2944. "pair: %s\n", hermon, ibdev->port, strerror ( rc ) );
  2945. goto err_create_qp;
  2946. }
  2947. ib_qp_set_ownerdata ( port->eth_qp, netdev );
  2948. /* Activate queue pair */
  2949. if ( ( rc = ib_modify_qp ( ibdev, port->eth_qp ) ) != 0 ) {
  2950. DBGC ( hermon, "Hermon %p port %d could not modify queue "
  2951. "pair: %s\n", hermon, ibdev->port, strerror ( rc ) );
  2952. goto err_modify_qp;
  2953. }
  2954. /* Fill receive rings */
  2955. ib_refill_recv ( ibdev, port->eth_qp );
  2956. /* Set port general parameters */
  2957. memset ( &set_port, 0, sizeof ( set_port ) );
  2958. MLX_FILL_3 ( &set_port.general, 0,
  2959. v_mtu, 1,
  2960. v_pprx, 1,
  2961. v_pptx, 1 );
  2962. MLX_FILL_1 ( &set_port.general, 1,
  2963. mtu, ( ETH_FRAME_LEN + 40 /* Used by card */ ) );
  2964. MLX_FILL_1 ( &set_port.general, 2,
  2965. pfctx, ( 1 << FCOE_VLAN_PRIORITY ) );
  2966. MLX_FILL_1 ( &set_port.general, 3,
  2967. pfcrx, ( 1 << FCOE_VLAN_PRIORITY ) );
  2968. if ( ( rc = hermon_cmd_set_port ( hermon, 1,
  2969. ( HERMON_SET_PORT_GENERAL_PARAM |
  2970. ibdev->port ),
  2971. &set_port ) ) != 0 ) {
  2972. DBGC ( hermon, "Hermon %p port %d could not set port general "
  2973. "parameters: %s\n",
  2974. hermon, ibdev->port, strerror ( rc ) );
  2975. goto err_set_port_general_params;
  2976. }
  2977. /* Set port receive QP */
  2978. memset ( &set_port, 0, sizeof ( set_port ) );
  2979. MLX_FILL_1 ( &set_port.rqp_calc, 0, base_qpn, port->eth_qp->qpn );
  2980. MLX_FILL_1 ( &set_port.rqp_calc, 2,
  2981. mac_miss_index, 128 /* MAC misses go to promisc QP */ );
  2982. MLX_FILL_2 ( &set_port.rqp_calc, 3,
  2983. vlan_miss_index, 127 /* VLAN misses go to promisc QP */,
  2984. no_vlan_index, 126 /* VLAN-free go to promisc QP */ );
  2985. MLX_FILL_2 ( &set_port.rqp_calc, 5,
  2986. promisc_qpn, port->eth_qp->qpn,
  2987. en_uc_promisc, 1 );
  2988. MLX_FILL_2 ( &set_port.rqp_calc, 6,
  2989. def_mcast_qpn, port->eth_qp->qpn,
  2990. mc_promisc_mode, 2 /* Receive all multicasts */ );
  2991. if ( ( rc = hermon_cmd_set_port ( hermon, 1,
  2992. ( HERMON_SET_PORT_RECEIVE_QP |
  2993. ibdev->port ),
  2994. &set_port ) ) != 0 ) {
  2995. DBGC ( hermon, "Hermon %p port %d could not set port receive "
  2996. "QP: %s\n", hermon, ibdev->port, strerror ( rc ) );
  2997. goto err_set_port_receive_qp;
  2998. }
  2999. /* Initialise port */
  3000. if ( ( rc = hermon_cmd_init_port ( hermon, ibdev->port ) ) != 0 ) {
  3001. DBGC ( hermon, "Hermon %p port %d could not initialise port: "
  3002. "%s\n", hermon, ibdev->port, strerror ( rc ) );
  3003. goto err_init_port;
  3004. }
  3005. return 0;
  3006. err_init_port:
  3007. err_set_port_receive_qp:
  3008. err_set_port_general_params:
  3009. err_modify_qp:
  3010. ib_destroy_qp ( ibdev, port->eth_qp );
  3011. err_create_qp:
  3012. ib_destroy_cq ( ibdev, port->eth_cq );
  3013. err_create_cq:
  3014. hermon_close ( hermon );
  3015. err_open:
  3016. return rc;
  3017. }
  3018. /**
  3019. * Close Hermon Ethernet device
  3020. *
  3021. * @v netdev Network device
  3022. */
  3023. static void hermon_eth_close ( struct net_device *netdev ) {
  3024. struct hermon_port *port = netdev->priv;
  3025. struct ib_device *ibdev = port->ibdev;
  3026. struct hermon *hermon = ib_get_drvdata ( ibdev );
  3027. int rc;
  3028. /* Close port */
  3029. if ( ( rc = hermon_cmd_close_port ( hermon, ibdev->port ) ) != 0 ) {
  3030. DBGC ( hermon, "Hermon %p port %d could not close port: %s\n",
  3031. hermon, ibdev->port, strerror ( rc ) );
  3032. /* Nothing we can do about this */
  3033. }
  3034. /* Tear down the queues */
  3035. ib_destroy_qp ( ibdev, port->eth_qp );
  3036. ib_destroy_cq ( ibdev, port->eth_cq );
  3037. /* Close hardware */
  3038. hermon_close ( hermon );
  3039. }
  3040. /** Hermon Ethernet network device operations */
  3041. static struct net_device_operations hermon_eth_operations = {
  3042. .open = hermon_eth_open,
  3043. .close = hermon_eth_close,
  3044. .transmit = hermon_eth_transmit,
  3045. .poll = hermon_eth_poll,
  3046. };
  3047. /**
  3048. * Register Hermon Ethernet device
  3049. *
  3050. * @v hermon Hermon device
  3051. * @v port Hermon port
  3052. * @ret rc Return status code
  3053. */
  3054. static int hermon_register_netdev ( struct hermon *hermon,
  3055. struct hermon_port *port ) {
  3056. struct net_device *netdev = port->netdev;
  3057. struct ib_device *ibdev = port->ibdev;
  3058. struct hermonprm_query_port_cap query_port;
  3059. union {
  3060. uint8_t bytes[8];
  3061. uint32_t dwords[2];
  3062. } mac;
  3063. int rc;
  3064. /* Retrieve MAC address */
  3065. if ( ( rc = hermon_cmd_query_port ( hermon, ibdev->port,
  3066. &query_port ) ) != 0 ) {
  3067. DBGC ( hermon, "Hermon %p port %d could not query port: %s\n",
  3068. hermon, ibdev->port, strerror ( rc ) );
  3069. goto err_query_port;
  3070. }
  3071. mac.dwords[0] = htonl ( MLX_GET ( &query_port, mac_47_32 ) );
  3072. mac.dwords[1] = htonl ( MLX_GET ( &query_port, mac_31_0 ) );
  3073. memcpy ( netdev->hw_addr,
  3074. &mac.bytes[ sizeof ( mac.bytes ) - ETH_ALEN ], ETH_ALEN );
  3075. /* Register network device */
  3076. if ( ( rc = register_netdev ( netdev ) ) != 0 ) {
  3077. DBGC ( hermon, "Hermon %p port %d could not register network "
  3078. "device: %s\n", hermon, ibdev->port, strerror ( rc ) );
  3079. goto err_register_netdev;
  3080. }
  3081. /* Register non-volatile options */
  3082. if ( ( rc = register_nvo ( &port->nvo,
  3083. netdev_settings ( netdev ) ) ) != 0 ) {
  3084. DBGC ( hermon, "Hermon %p port %d could not register non-"
  3085. "volatile options: %s\n",
  3086. hermon, ibdev->port, strerror ( rc ) );
  3087. goto err_register_nvo;
  3088. }
  3089. return 0;
  3090. unregister_nvo ( &port->nvo );
  3091. err_register_nvo:
  3092. unregister_netdev ( netdev );
  3093. err_register_netdev:
  3094. err_query_port:
  3095. return rc;
  3096. }
  3097. /**
  3098. * Handle Hermon Ethernet device port state change
  3099. *
  3100. * @v hermon Hermon device
  3101. * @v port Hermon port
  3102. * @v link_up Link is up
  3103. */
  3104. static void hermon_state_change_netdev ( struct hermon *hermon __unused,
  3105. struct hermon_port *port,
  3106. int link_up ) {
  3107. struct net_device *netdev = port->netdev;
  3108. if ( link_up ) {
  3109. netdev_link_up ( netdev );
  3110. } else {
  3111. netdev_link_down ( netdev );
  3112. }
  3113. }
  3114. /**
  3115. * Unregister Hermon Ethernet device
  3116. *
  3117. * @v hermon Hermon device
  3118. * @v port Hermon port
  3119. */
  3120. static void hermon_unregister_netdev ( struct hermon *hermon __unused,
  3121. struct hermon_port *port ) {
  3122. struct net_device *netdev = port->netdev;
  3123. unregister_nvo ( &port->nvo );
  3124. unregister_netdev ( netdev );
  3125. }
  3126. /** Hermon Ethernet port type */
  3127. static struct hermon_port_type hermon_port_type_eth = {
  3128. .register_dev = hermon_register_netdev,
  3129. .state_change = hermon_state_change_netdev,
  3130. .unregister_dev = hermon_unregister_netdev,
  3131. };
  3132. /***************************************************************************
  3133. *
  3134. * Port type detection
  3135. *
  3136. ***************************************************************************
  3137. */
  3138. /** Timeout for port sensing */
  3139. #define HERMON_SENSE_PORT_TIMEOUT ( TICKS_PER_SEC / 2 )
  3140. /**
  3141. * Name port type
  3142. *
  3143. * @v port_type Port type
  3144. * @v port_type_name Port type name
  3145. */
  3146. static inline const char * hermon_name_port_type ( unsigned int port_type ) {
  3147. switch ( port_type ) {
  3148. case HERMON_PORT_TYPE_UNKNOWN: return "unknown";
  3149. case HERMON_PORT_TYPE_IB: return "Infiniband";
  3150. case HERMON_PORT_TYPE_ETH: return "Ethernet";
  3151. default: return "INVALID";
  3152. }
  3153. }
  3154. /**
  3155. * Sense port type
  3156. *
  3157. * @v hermon Hermon device
  3158. * @v port Hermon port
  3159. * @ret port_type Port type, or negative error
  3160. */
  3161. static int hermon_sense_port_type ( struct hermon *hermon,
  3162. struct hermon_port *port ) {
  3163. struct ib_device *ibdev = port->ibdev;
  3164. struct hermonprm_sense_port sense_port;
  3165. int port_type;
  3166. int rc;
  3167. /* If DPDP is not supported, always assume Infiniband */
  3168. if ( ! hermon->cap.dpdp ) {
  3169. port_type = HERMON_PORT_TYPE_IB;
  3170. DBGC ( hermon, "Hermon %p port %d does not support DPDP; "
  3171. "assuming an %s network\n", hermon, ibdev->port,
  3172. hermon_name_port_type ( port_type ) );
  3173. return port_type;
  3174. }
  3175. /* Sense the port type */
  3176. if ( ( rc = hermon_cmd_sense_port ( hermon, ibdev->port,
  3177. &sense_port ) ) != 0 ) {
  3178. DBGC ( hermon, "Hermon %p port %d sense failed: %s\n",
  3179. hermon, ibdev->port, strerror ( rc ) );
  3180. return rc;
  3181. }
  3182. port_type = MLX_GET ( &sense_port, port_type );
  3183. DBGC ( hermon, "Hermon %p port %d sensed an %s network\n",
  3184. hermon, ibdev->port, hermon_name_port_type ( port_type ) );
  3185. return port_type;
  3186. }
  3187. /**
  3188. * Set port type
  3189. *
  3190. * @v hermon Hermon device
  3191. * @v port Hermon port
  3192. * @ret rc Return status code
  3193. */
  3194. static int hermon_set_port_type ( struct hermon *hermon,
  3195. struct hermon_port *port ) {
  3196. struct ib_device *ibdev = port->ibdev;
  3197. struct hermonprm_query_port_cap query_port;
  3198. int ib_supported;
  3199. int eth_supported;
  3200. int port_type;
  3201. unsigned long start;
  3202. unsigned long elapsed;
  3203. int rc;
  3204. /* Check to see which types are supported */
  3205. if ( ( rc = hermon_cmd_query_port ( hermon, ibdev->port,
  3206. &query_port ) ) != 0 ) {
  3207. DBGC ( hermon, "Hermon %p port %d could not query port: %s\n",
  3208. hermon, ibdev->port, strerror ( rc ) );
  3209. return rc;
  3210. }
  3211. ib_supported = MLX_GET ( &query_port, ib );
  3212. eth_supported = MLX_GET ( &query_port, eth );
  3213. DBGC ( hermon, "Hermon %p port %d supports%s%s%s\n",
  3214. hermon, ibdev->port, ( ib_supported ? " Infiniband" : "" ),
  3215. ( ( ib_supported && eth_supported ) ? " and" : "" ),
  3216. ( eth_supported ? " Ethernet" : "" ) );
  3217. /* Sense network, if applicable */
  3218. if ( ib_supported && eth_supported ) {
  3219. /* Both types are supported; try sensing network */
  3220. start = currticks();
  3221. do {
  3222. /* Try sensing port */
  3223. port_type = hermon_sense_port_type ( hermon, port );
  3224. if ( port_type < 0 ) {
  3225. rc = port_type;
  3226. return rc;
  3227. }
  3228. } while ( ( port_type == HERMON_PORT_TYPE_UNKNOWN ) &&
  3229. ( ( elapsed = ( currticks() - start ) ) <
  3230. HERMON_SENSE_PORT_TIMEOUT ) );
  3231. /* Set port type based on sensed network, defaulting
  3232. * to Infiniband if nothing was sensed.
  3233. */
  3234. switch ( port_type ) {
  3235. case HERMON_PORT_TYPE_ETH:
  3236. port->type = &hermon_port_type_eth;
  3237. break;
  3238. case HERMON_PORT_TYPE_IB:
  3239. case HERMON_PORT_TYPE_UNKNOWN:
  3240. port->type = &hermon_port_type_ib;
  3241. break;
  3242. default:
  3243. return -EINVAL;
  3244. }
  3245. } else if ( eth_supported ) {
  3246. port->type = &hermon_port_type_eth;
  3247. } else {
  3248. port->type = &hermon_port_type_ib;
  3249. }
  3250. assert ( port->type != NULL );
  3251. return 0;
  3252. }
  3253. /***************************************************************************
  3254. *
  3255. * BOFM interface
  3256. *
  3257. ***************************************************************************
  3258. */
  3259. /**
  3260. * Harvest Ethernet MAC for BOFM
  3261. *
  3262. * @v bofm BOFM device
  3263. * @v mport Multi-port index
  3264. * @v mac MAC to fill in
  3265. * @ret rc Return status code
  3266. */
  3267. static int hermon_bofm_harvest ( struct bofm_device *bofm, unsigned int mport,
  3268. uint8_t *mac ) {
  3269. struct hermon *hermon = container_of ( bofm, struct hermon, bofm );
  3270. struct hermonprm_mod_stat_cfg stat_cfg;
  3271. union {
  3272. uint8_t bytes[8];
  3273. uint32_t dwords[2];
  3274. } buf;
  3275. int rc;
  3276. /* Query static configuration */
  3277. if ( ( rc = hermon_mod_stat_cfg ( hermon, mport,
  3278. HERMON_MOD_STAT_CFG_QUERY,
  3279. HERMON_MOD_STAT_CFG_OFFSET ( mac_m ),
  3280. &stat_cfg ) ) != 0 ) {
  3281. DBGC ( hermon, "Hermon %p port %d could not query "
  3282. "configuration: %s\n", hermon, mport, strerror ( rc ) );
  3283. return rc;
  3284. }
  3285. /* Retrieve MAC address */
  3286. buf.dwords[0] = htonl ( MLX_GET ( &stat_cfg, mac_high ) );
  3287. buf.dwords[1] = htonl ( MLX_GET ( &stat_cfg, mac_low ) );
  3288. memcpy ( mac, &buf.bytes[ sizeof ( buf.bytes ) - ETH_ALEN ],
  3289. ETH_ALEN );
  3290. DBGC ( hermon, "Hermon %p port %d harvested MAC address %s\n",
  3291. hermon, mport, eth_ntoa ( mac ) );
  3292. return 0;
  3293. }
  3294. /**
  3295. * Update Ethernet MAC for BOFM
  3296. *
  3297. * @v bofm BOFM device
  3298. * @v mport Multi-port index
  3299. * @v mac MAC to fill in
  3300. * @ret rc Return status code
  3301. */
  3302. static int hermon_bofm_update ( struct bofm_device *bofm, unsigned int mport,
  3303. const uint8_t *mac ) {
  3304. struct hermon *hermon = container_of ( bofm, struct hermon, bofm );
  3305. struct hermonprm_mod_stat_cfg stat_cfg;
  3306. union {
  3307. uint8_t bytes[8];
  3308. uint32_t dwords[2];
  3309. } buf;
  3310. int rc;
  3311. /* Prepare MAC address */
  3312. memset ( &buf, 0, sizeof ( buf ) );
  3313. memcpy ( &buf.bytes[ sizeof ( buf.bytes ) - ETH_ALEN ], mac,
  3314. ETH_ALEN );
  3315. /* Modify static configuration */
  3316. memset ( &stat_cfg, 0, sizeof ( stat_cfg ) );
  3317. MLX_FILL_2 ( &stat_cfg, 36,
  3318. mac_m, 1,
  3319. mac_high, ntohl ( buf.dwords[0] ) );
  3320. MLX_FILL_1 ( &stat_cfg, 37, mac_low, ntohl ( buf.dwords[1] ) );
  3321. if ( ( rc = hermon_mod_stat_cfg ( hermon, mport,
  3322. HERMON_MOD_STAT_CFG_SET,
  3323. HERMON_MOD_STAT_CFG_OFFSET ( mac_m ),
  3324. &stat_cfg ) ) != 0 ) {
  3325. DBGC ( hermon, "Hermon %p port %d could not modify "
  3326. "configuration: %s\n", hermon, mport, strerror ( rc ) );
  3327. return rc;
  3328. }
  3329. DBGC ( hermon, "Hermon %p port %d updated MAC address to %s\n",
  3330. hermon, mport, eth_ntoa ( mac ) );
  3331. return 0;
  3332. }
  3333. /** Hermon BOFM operations */
  3334. static struct bofm_operations hermon_bofm_operations = {
  3335. .harvest = hermon_bofm_harvest,
  3336. .update = hermon_bofm_update,
  3337. };
  3338. /***************************************************************************
  3339. *
  3340. * PCI interface
  3341. *
  3342. ***************************************************************************
  3343. */
  3344. /**
  3345. * Allocate Hermon device
  3346. *
  3347. * @v pci PCI device
  3348. * @v id PCI ID
  3349. * @ret rc Return status code
  3350. */
  3351. static struct hermon * hermon_alloc ( void ) {
  3352. struct hermon *hermon;
  3353. /* Allocate Hermon device */
  3354. hermon = zalloc ( sizeof ( *hermon ) );
  3355. if ( ! hermon )
  3356. goto err_hermon;
  3357. /* Allocate space for mailboxes */
  3358. hermon->mailbox_in = malloc_dma ( HERMON_MBOX_SIZE,
  3359. HERMON_MBOX_ALIGN );
  3360. if ( ! hermon->mailbox_in )
  3361. goto err_mailbox_in;
  3362. hermon->mailbox_out = malloc_dma ( HERMON_MBOX_SIZE,
  3363. HERMON_MBOX_ALIGN );
  3364. if ( ! hermon->mailbox_out )
  3365. goto err_mailbox_out;
  3366. return hermon;
  3367. free_dma ( hermon->mailbox_out, HERMON_MBOX_SIZE );
  3368. err_mailbox_out:
  3369. free_dma ( hermon->mailbox_in, HERMON_MBOX_SIZE );
  3370. err_mailbox_in:
  3371. free ( hermon );
  3372. err_hermon:
  3373. return NULL;
  3374. }
  3375. /**
  3376. * Free Hermon device
  3377. *
  3378. * @v hermon Hermon device
  3379. */
  3380. static void hermon_free ( struct hermon *hermon ) {
  3381. ufree ( hermon->icm );
  3382. ufree ( hermon->firmware_area );
  3383. free_dma ( hermon->mailbox_out, HERMON_MBOX_SIZE );
  3384. free_dma ( hermon->mailbox_in, HERMON_MBOX_SIZE );
  3385. free ( hermon );
  3386. }
  3387. /**
  3388. * Probe PCI device
  3389. *
  3390. * @v pci PCI device
  3391. * @v id PCI ID
  3392. * @ret rc Return status code
  3393. */
  3394. static int hermon_probe ( struct pci_device *pci ) {
  3395. struct hermon *hermon;
  3396. struct ib_device *ibdev;
  3397. struct net_device *netdev;
  3398. struct hermon_port *port;
  3399. unsigned int i;
  3400. int rc;
  3401. /* Allocate Hermon device */
  3402. hermon = hermon_alloc();
  3403. if ( ! hermon ) {
  3404. rc = -ENOMEM;
  3405. goto err_alloc;
  3406. }
  3407. pci_set_drvdata ( pci, hermon );
  3408. hermon->pci = pci;
  3409. /* Fix up PCI device */
  3410. adjust_pci_device ( pci );
  3411. /* Map PCI BARs */
  3412. hermon->config = ioremap ( pci_bar_start ( pci, HERMON_PCI_CONFIG_BAR ),
  3413. HERMON_PCI_CONFIG_BAR_SIZE );
  3414. hermon->uar = ioremap ( pci_bar_start ( pci, HERMON_PCI_UAR_BAR ),
  3415. HERMON_UAR_NON_EQ_PAGE * HERMON_PAGE_SIZE );
  3416. /* Reset device */
  3417. hermon_reset ( hermon );
  3418. /* Start firmware */
  3419. if ( ( rc = hermon_start_firmware ( hermon ) ) != 0 )
  3420. goto err_start_firmware;
  3421. /* Get device limits */
  3422. if ( ( rc = hermon_get_cap ( hermon ) ) != 0 )
  3423. goto err_get_cap;
  3424. /* Allocate Infiniband devices */
  3425. for ( i = 0 ; i < hermon->cap.num_ports ; i++ ) {
  3426. ibdev = alloc_ibdev ( 0 );
  3427. if ( ! ibdev ) {
  3428. rc = -ENOMEM;
  3429. goto err_alloc_ibdev;
  3430. }
  3431. hermon->port[i].ibdev = ibdev;
  3432. ibdev->op = &hermon_ib_operations;
  3433. ibdev->dev = &pci->dev;
  3434. ibdev->port = ( HERMON_PORT_BASE + i );
  3435. ib_set_drvdata ( ibdev, hermon );
  3436. }
  3437. /* Allocate network devices */
  3438. for ( i = 0 ; i < hermon->cap.num_ports ; i++ ) {
  3439. netdev = alloc_etherdev ( 0 );
  3440. if ( ! netdev ) {
  3441. rc = -ENOMEM;
  3442. goto err_alloc_netdev;
  3443. }
  3444. hermon->port[i].netdev = netdev;
  3445. netdev_init ( netdev, &hermon_eth_operations );
  3446. netdev->dev = &pci->dev;
  3447. netdev->priv = &hermon->port[i];
  3448. }
  3449. /* Start device */
  3450. if ( ( rc = hermon_start ( hermon, 1 ) ) != 0 )
  3451. goto err_start;
  3452. /* Determine port types */
  3453. for ( i = 0 ; i < hermon->cap.num_ports ; i++ ) {
  3454. port = &hermon->port[i];
  3455. if ( ( rc = hermon_set_port_type ( hermon, port ) ) != 0 )
  3456. goto err_set_port_type;
  3457. }
  3458. /* Initialise non-volatile storage */
  3459. nvs_vpd_init ( &hermon->nvsvpd, pci );
  3460. for ( i = 0 ; i < hermon->cap.num_ports ; i++ ) {
  3461. port = &hermon->port[i];
  3462. nvs_vpd_nvo_init ( &hermon->nvsvpd,
  3463. HERMON_VPD_FIELD ( port->ibdev->port ),
  3464. &port->nvo, NULL );
  3465. }
  3466. /* Register devices */
  3467. for ( i = 0 ; i < hermon->cap.num_ports ; i++ ) {
  3468. port = &hermon->port[i];
  3469. if ( ( rc = port->type->register_dev ( hermon, port ) ) != 0 )
  3470. goto err_register;
  3471. }
  3472. /* Leave device quiescent until opened */
  3473. if ( hermon->open_count == 0 )
  3474. hermon_stop ( hermon );
  3475. return 0;
  3476. i = hermon->cap.num_ports;
  3477. err_register:
  3478. for ( i-- ; ( signed int ) i >= 0 ; i-- ) {
  3479. port = &hermon->port[i];
  3480. port->type->unregister_dev ( hermon, port );
  3481. }
  3482. err_set_port_type:
  3483. hermon_stop ( hermon );
  3484. err_start:
  3485. i = hermon->cap.num_ports;
  3486. err_alloc_netdev:
  3487. for ( i-- ; ( signed int ) i >= 0 ; i-- ) {
  3488. netdev_nullify ( hermon->port[i].netdev );
  3489. netdev_put ( hermon->port[i].netdev );
  3490. }
  3491. i = hermon->cap.num_ports;
  3492. err_alloc_ibdev:
  3493. for ( i-- ; ( signed int ) i >= 0 ; i-- )
  3494. ibdev_put ( hermon->port[i].ibdev );
  3495. err_get_cap:
  3496. hermon_stop_firmware ( hermon );
  3497. err_start_firmware:
  3498. iounmap ( hermon->uar );
  3499. iounmap ( hermon->config );
  3500. hermon_free ( hermon );
  3501. err_alloc:
  3502. return rc;
  3503. }
  3504. /**
  3505. * Remove PCI device
  3506. *
  3507. * @v pci PCI device
  3508. */
  3509. static void hermon_remove ( struct pci_device *pci ) {
  3510. struct hermon *hermon = pci_get_drvdata ( pci );
  3511. struct hermon_port *port;
  3512. int i;
  3513. for ( i = ( hermon->cap.num_ports - 1 ) ; i >= 0 ; i-- ) {
  3514. port = &hermon->port[i];
  3515. port->type->unregister_dev ( hermon, port );
  3516. }
  3517. for ( i = ( hermon->cap.num_ports - 1 ) ; i >= 0 ; i-- ) {
  3518. netdev_nullify ( hermon->port[i].netdev );
  3519. netdev_put ( hermon->port[i].netdev );
  3520. }
  3521. for ( i = ( hermon->cap.num_ports - 1 ) ; i >= 0 ; i-- )
  3522. ibdev_put ( hermon->port[i].ibdev );
  3523. iounmap ( hermon->uar );
  3524. iounmap ( hermon->config );
  3525. hermon_free ( hermon );
  3526. }
  3527. /**
  3528. * Probe PCI device for BOFM
  3529. *
  3530. * @v pci PCI device
  3531. * @v id PCI ID
  3532. * @ret rc Return status code
  3533. */
  3534. static int hermon_bofm_probe ( struct pci_device *pci ) {
  3535. struct hermon *hermon;
  3536. int rc;
  3537. /* Allocate Hermon device */
  3538. hermon = hermon_alloc();
  3539. if ( ! hermon ) {
  3540. rc = -ENOMEM;
  3541. goto err_alloc;
  3542. }
  3543. pci_set_drvdata ( pci, hermon );
  3544. hermon->pci = pci;
  3545. /* Fix up PCI device */
  3546. adjust_pci_device ( pci );
  3547. /* Map PCI BAR */
  3548. hermon->config = ioremap ( pci_bar_start ( pci, HERMON_PCI_CONFIG_BAR ),
  3549. HERMON_PCI_CONFIG_BAR_SIZE );
  3550. /* Initialise BOFM device */
  3551. bofm_init ( &hermon->bofm, pci, &hermon_bofm_operations );
  3552. /* Register BOFM device */
  3553. if ( ( rc = bofm_register ( &hermon->bofm ) ) != 0 ) {
  3554. DBGC ( hermon, "Hermon %p could not register BOFM device: "
  3555. "%s\n", hermon, strerror ( rc ) );
  3556. goto err_bofm_register;
  3557. }
  3558. return 0;
  3559. err_bofm_register:
  3560. iounmap ( hermon->config );
  3561. hermon_free ( hermon );
  3562. err_alloc:
  3563. return rc;
  3564. }
  3565. /**
  3566. * Remove PCI device for BOFM
  3567. *
  3568. * @v pci PCI device
  3569. */
  3570. static void hermon_bofm_remove ( struct pci_device *pci ) {
  3571. struct hermon *hermon = pci_get_drvdata ( pci );
  3572. bofm_unregister ( &hermon->bofm );
  3573. iounmap ( hermon->config );
  3574. hermon_free ( hermon );
  3575. }
  3576. static struct pci_device_id hermon_nics[] = {
  3577. PCI_ROM ( 0x15b3, 0x6340, "mt25408", "MT25408 HCA driver", 0 ),
  3578. PCI_ROM ( 0x15b3, 0x634a, "mt25418", "MT25418 HCA driver", 0 ),
  3579. PCI_ROM ( 0x15b3, 0x6732, "mt26418", "MT26418 HCA driver", 0 ),
  3580. PCI_ROM ( 0x15b3, 0x673c, "mt26428", "MT26428 HCA driver", 0 ),
  3581. PCI_ROM ( 0x15b3, 0x6746, "mt26438", "MT26438 HCA driver", 0 ),
  3582. PCI_ROM ( 0x15b3, 0x6778, "mt26488", "MT26488 HCA driver", 0 ),
  3583. PCI_ROM ( 0x15b3, 0x6368, "mt25448", "MT25448 HCA driver", 0 ),
  3584. PCI_ROM ( 0x15b3, 0x6750, "mt26448", "MT26448 HCA driver", 0 ),
  3585. PCI_ROM ( 0x15b3, 0x6372, "mt25458", "MT25458 HCA driver", 0 ),
  3586. PCI_ROM ( 0x15b3, 0x675a, "mt26458", "MT26458 HCA driver", 0 ),
  3587. PCI_ROM ( 0x15b3, 0x6764, "mt26468", "MT26468 HCA driver", 0 ),
  3588. PCI_ROM ( 0x15b3, 0x676e, "mt26478", "MT26478 HCA driver", 0 ),
  3589. };
  3590. struct pci_driver hermon_driver __pci_driver = {
  3591. .ids = hermon_nics,
  3592. .id_count = ( sizeof ( hermon_nics ) / sizeof ( hermon_nics[0] ) ),
  3593. .probe = hermon_probe,
  3594. .remove = hermon_remove,
  3595. };
  3596. struct pci_driver hermon_bofm_driver __bofm_driver = {
  3597. .ids = hermon_nics,
  3598. .id_count = ( sizeof ( hermon_nics ) / sizeof ( hermon_nics[0] ) ),
  3599. .probe = hermon_bofm_probe,
  3600. .remove = hermon_bofm_remove,
  3601. };