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 114KB

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