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.

arbel.c 94KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * Based in part upon the original driver by Mellanox Technologies
  5. * Ltd. Portions may be Copyright (c) Mellanox Technologies Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of the
  10. * License, or any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. * 02110-1301, USA.
  21. *
  22. * You can also choose to distribute this program under the terms of
  23. * the Unmodified Binary Distribution Licence (as given in the file
  24. * COPYING.UBDL), provided that you have satisfied its requirements.
  25. */
  26. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  27. #include <stdint.h>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <strings.h>
  32. #include <unistd.h>
  33. #include <errno.h>
  34. #include <byteswap.h>
  35. #include <ipxe/io.h>
  36. #include <ipxe/pci.h>
  37. #include <ipxe/pcibackup.h>
  38. #include <ipxe/malloc.h>
  39. #include <ipxe/umalloc.h>
  40. #include <ipxe/iobuf.h>
  41. #include <ipxe/netdevice.h>
  42. #include <ipxe/infiniband.h>
  43. #include <ipxe/ib_smc.h>
  44. #include "arbel.h"
  45. /**
  46. * @file
  47. *
  48. * Mellanox Arbel Infiniband HCA
  49. *
  50. */
  51. /***************************************************************************
  52. *
  53. * Queue number allocation
  54. *
  55. ***************************************************************************
  56. */
  57. /**
  58. * Allocate offset within usage bitmask
  59. *
  60. * @v bits Usage bitmask
  61. * @v bits_len Length of usage bitmask
  62. * @ret bit First free bit within bitmask, or negative error
  63. */
  64. static int arbel_bitmask_alloc ( arbel_bitmask_t *bits,
  65. unsigned int bits_len ) {
  66. unsigned int bit = 0;
  67. arbel_bitmask_t mask = 1;
  68. while ( bit < bits_len ) {
  69. if ( ( mask & *bits ) == 0 ) {
  70. *bits |= mask;
  71. return bit;
  72. }
  73. bit++;
  74. mask = ( mask << 1 ) | ( mask >> ( 8 * sizeof ( mask ) - 1 ) );
  75. if ( mask == 1 )
  76. bits++;
  77. }
  78. return -ENFILE;
  79. }
  80. /**
  81. * Free offset within usage bitmask
  82. *
  83. * @v bits Usage bitmask
  84. * @v bit Bit within bitmask
  85. */
  86. static void arbel_bitmask_free ( arbel_bitmask_t *bits, int bit ) {
  87. arbel_bitmask_t mask;
  88. mask = ( 1 << ( bit % ( 8 * sizeof ( mask ) ) ) );
  89. bits += ( bit / ( 8 * sizeof ( mask ) ) );
  90. *bits &= ~mask;
  91. }
  92. /***************************************************************************
  93. *
  94. * HCA commands
  95. *
  96. ***************************************************************************
  97. */
  98. /**
  99. * Wait for Arbel command completion
  100. *
  101. * @v arbel Arbel device
  102. * @ret rc Return status code
  103. */
  104. static int arbel_cmd_wait ( struct arbel *arbel,
  105. struct arbelprm_hca_command_register *hcr ) {
  106. unsigned int wait;
  107. for ( wait = ARBEL_HCR_MAX_WAIT_MS ; wait ; wait-- ) {
  108. hcr->u.dwords[6] =
  109. readl ( arbel->config + ARBEL_HCR_REG ( 6 ) );
  110. if ( MLX_GET ( hcr, go ) == 0 )
  111. return 0;
  112. mdelay ( 1 );
  113. }
  114. return -EBUSY;
  115. }
  116. /**
  117. * Issue HCA command
  118. *
  119. * @v arbel Arbel device
  120. * @v command Command opcode, flags and input/output lengths
  121. * @v op_mod Opcode modifier (0 if no modifier applicable)
  122. * @v in Input parameters
  123. * @v in_mod Input modifier (0 if no modifier applicable)
  124. * @v out Output parameters
  125. * @ret rc Return status code
  126. */
  127. static int arbel_cmd ( struct arbel *arbel, unsigned long command,
  128. unsigned int op_mod, const void *in,
  129. unsigned int in_mod, void *out ) {
  130. struct arbelprm_hca_command_register hcr;
  131. unsigned int opcode = ARBEL_HCR_OPCODE ( command );
  132. size_t in_len = ARBEL_HCR_IN_LEN ( command );
  133. size_t out_len = ARBEL_HCR_OUT_LEN ( command );
  134. void *in_buffer;
  135. void *out_buffer;
  136. unsigned int status;
  137. unsigned int i;
  138. int rc;
  139. assert ( in_len <= ARBEL_MBOX_SIZE );
  140. assert ( out_len <= ARBEL_MBOX_SIZE );
  141. DBGC2 ( arbel, "Arbel %p command %02x in %zx%s out %zx%s\n",
  142. arbel, opcode, in_len,
  143. ( ( command & ARBEL_HCR_IN_MBOX ) ? "(mbox)" : "" ), out_len,
  144. ( ( command & ARBEL_HCR_OUT_MBOX ) ? "(mbox)" : "" ) );
  145. /* Check that HCR is free */
  146. if ( ( rc = arbel_cmd_wait ( arbel, &hcr ) ) != 0 ) {
  147. DBGC ( arbel, "Arbel %p command interface locked\n", arbel );
  148. return rc;
  149. }
  150. /* Prepare HCR */
  151. memset ( &hcr, 0, sizeof ( hcr ) );
  152. in_buffer = &hcr.u.dwords[0];
  153. if ( in_len && ( command & ARBEL_HCR_IN_MBOX ) ) {
  154. in_buffer = arbel->mailbox_in;
  155. MLX_FILL_H ( &hcr, 0, in_param_h, virt_to_bus ( in_buffer ) );
  156. MLX_FILL_1 ( &hcr, 1, in_param_l, virt_to_bus ( in_buffer ) );
  157. }
  158. memcpy ( in_buffer, in, in_len );
  159. MLX_FILL_1 ( &hcr, 2, input_modifier, in_mod );
  160. out_buffer = &hcr.u.dwords[3];
  161. if ( out_len && ( command & ARBEL_HCR_OUT_MBOX ) ) {
  162. out_buffer = arbel->mailbox_out;
  163. MLX_FILL_H ( &hcr, 3, out_param_h,
  164. virt_to_bus ( out_buffer ) );
  165. MLX_FILL_1 ( &hcr, 4, out_param_l,
  166. virt_to_bus ( out_buffer ) );
  167. }
  168. MLX_FILL_3 ( &hcr, 6,
  169. opcode, opcode,
  170. opcode_modifier, op_mod,
  171. go, 1 );
  172. DBGC ( arbel, "Arbel %p issuing command %04x\n", arbel, opcode );
  173. DBGC2_HDA ( arbel, virt_to_phys ( arbel->config + ARBEL_HCR_BASE ),
  174. &hcr, sizeof ( hcr ) );
  175. if ( in_len && ( command & ARBEL_HCR_IN_MBOX ) ) {
  176. DBGC2 ( arbel, "Input mailbox:\n" );
  177. DBGC2_HDA ( arbel, virt_to_phys ( in_buffer ), in_buffer,
  178. ( ( in_len < 512 ) ? in_len : 512 ) );
  179. }
  180. /* Issue command */
  181. for ( i = 0 ; i < ( sizeof ( hcr ) / sizeof ( hcr.u.dwords[0] ) ) ;
  182. i++ ) {
  183. writel ( hcr.u.dwords[i],
  184. arbel->config + ARBEL_HCR_REG ( i ) );
  185. barrier();
  186. }
  187. /* Wait for command completion */
  188. if ( ( rc = arbel_cmd_wait ( arbel, &hcr ) ) != 0 ) {
  189. DBGC ( arbel, "Arbel %p timed out waiting for command:\n",
  190. arbel );
  191. DBGC_HD ( arbel, &hcr, sizeof ( hcr ) );
  192. return rc;
  193. }
  194. /* Check command status */
  195. status = MLX_GET ( &hcr, status );
  196. if ( status != 0 ) {
  197. DBGC ( arbel, "Arbel %p command failed with status %02x:\n",
  198. arbel, status );
  199. DBGC_HD ( arbel, &hcr, sizeof ( hcr ) );
  200. return -EIO;
  201. }
  202. /* Read output parameters, if any */
  203. hcr.u.dwords[3] = readl ( arbel->config + ARBEL_HCR_REG ( 3 ) );
  204. hcr.u.dwords[4] = readl ( arbel->config + ARBEL_HCR_REG ( 4 ) );
  205. memcpy ( out, out_buffer, out_len );
  206. if ( out_len ) {
  207. DBGC2 ( arbel, "Output%s:\n",
  208. ( command & ARBEL_HCR_OUT_MBOX ) ? " mailbox" : "" );
  209. DBGC2_HDA ( arbel, virt_to_phys ( out_buffer ), out_buffer,
  210. ( ( out_len < 512 ) ? out_len : 512 ) );
  211. }
  212. return 0;
  213. }
  214. static inline int
  215. arbel_cmd_query_dev_lim ( struct arbel *arbel,
  216. struct arbelprm_query_dev_lim *dev_lim ) {
  217. return arbel_cmd ( arbel,
  218. ARBEL_HCR_OUT_CMD ( ARBEL_HCR_QUERY_DEV_LIM,
  219. 1, sizeof ( *dev_lim ) ),
  220. 0, NULL, 0, dev_lim );
  221. }
  222. static inline int
  223. arbel_cmd_query_fw ( struct arbel *arbel, struct arbelprm_query_fw *fw ) {
  224. return arbel_cmd ( arbel,
  225. ARBEL_HCR_OUT_CMD ( ARBEL_HCR_QUERY_FW,
  226. 1, sizeof ( *fw ) ),
  227. 0, NULL, 0, fw );
  228. }
  229. static inline int
  230. arbel_cmd_init_hca ( struct arbel *arbel,
  231. const struct arbelprm_init_hca *init_hca ) {
  232. return arbel_cmd ( arbel,
  233. ARBEL_HCR_IN_CMD ( ARBEL_HCR_INIT_HCA,
  234. 1, sizeof ( *init_hca ) ),
  235. 0, init_hca, 0, NULL );
  236. }
  237. static inline int
  238. arbel_cmd_close_hca ( struct arbel *arbel ) {
  239. return arbel_cmd ( arbel,
  240. ARBEL_HCR_VOID_CMD ( ARBEL_HCR_CLOSE_HCA ),
  241. 0, NULL, 0, NULL );
  242. }
  243. static inline int
  244. arbel_cmd_init_ib ( struct arbel *arbel, unsigned int port,
  245. const struct arbelprm_init_ib *init_ib ) {
  246. return arbel_cmd ( arbel,
  247. ARBEL_HCR_IN_CMD ( ARBEL_HCR_INIT_IB,
  248. 1, sizeof ( *init_ib ) ),
  249. 0, init_ib, port, NULL );
  250. }
  251. static inline int
  252. arbel_cmd_close_ib ( struct arbel *arbel, unsigned int port ) {
  253. return arbel_cmd ( arbel,
  254. ARBEL_HCR_VOID_CMD ( ARBEL_HCR_CLOSE_IB ),
  255. 0, NULL, port, NULL );
  256. }
  257. static inline int
  258. arbel_cmd_sw2hw_mpt ( struct arbel *arbel, unsigned int index,
  259. const struct arbelprm_mpt *mpt ) {
  260. return arbel_cmd ( arbel,
  261. ARBEL_HCR_IN_CMD ( ARBEL_HCR_SW2HW_MPT,
  262. 1, sizeof ( *mpt ) ),
  263. 0, mpt, index, NULL );
  264. }
  265. static inline int
  266. arbel_cmd_map_eq ( struct arbel *arbel, unsigned long index_map,
  267. const struct arbelprm_event_mask *mask ) {
  268. return arbel_cmd ( arbel,
  269. ARBEL_HCR_IN_CMD ( ARBEL_HCR_MAP_EQ,
  270. 0, sizeof ( *mask ) ),
  271. 0, mask, index_map, NULL );
  272. }
  273. static inline int
  274. arbel_cmd_sw2hw_eq ( struct arbel *arbel, unsigned int index,
  275. const struct arbelprm_eqc *eqctx ) {
  276. return arbel_cmd ( arbel,
  277. ARBEL_HCR_IN_CMD ( ARBEL_HCR_SW2HW_EQ,
  278. 1, sizeof ( *eqctx ) ),
  279. 0, eqctx, index, NULL );
  280. }
  281. static inline int
  282. arbel_cmd_hw2sw_eq ( struct arbel *arbel, unsigned int index,
  283. struct arbelprm_eqc *eqctx ) {
  284. return arbel_cmd ( arbel,
  285. ARBEL_HCR_OUT_CMD ( ARBEL_HCR_HW2SW_EQ,
  286. 1, sizeof ( *eqctx ) ),
  287. 1, NULL, index, eqctx );
  288. }
  289. static inline int
  290. arbel_cmd_sw2hw_cq ( struct arbel *arbel, unsigned long cqn,
  291. const struct arbelprm_completion_queue_context *cqctx ) {
  292. return arbel_cmd ( arbel,
  293. ARBEL_HCR_IN_CMD ( ARBEL_HCR_SW2HW_CQ,
  294. 1, sizeof ( *cqctx ) ),
  295. 0, cqctx, cqn, NULL );
  296. }
  297. static inline int
  298. arbel_cmd_hw2sw_cq ( struct arbel *arbel, unsigned long cqn,
  299. struct arbelprm_completion_queue_context *cqctx) {
  300. return arbel_cmd ( arbel,
  301. ARBEL_HCR_OUT_CMD ( ARBEL_HCR_HW2SW_CQ,
  302. 1, sizeof ( *cqctx ) ),
  303. 0, NULL, cqn, cqctx );
  304. }
  305. static inline int
  306. arbel_cmd_query_cq ( struct arbel *arbel, unsigned long cqn,
  307. struct arbelprm_completion_queue_context *cqctx ) {
  308. return arbel_cmd ( arbel,
  309. ARBEL_HCR_OUT_CMD ( ARBEL_HCR_QUERY_CQ,
  310. 1, sizeof ( *cqctx ) ),
  311. 0, NULL, cqn, cqctx );
  312. }
  313. static inline int
  314. arbel_cmd_rst2init_qpee ( struct arbel *arbel, unsigned long qpn,
  315. const struct arbelprm_qp_ee_state_transitions *ctx ){
  316. return arbel_cmd ( arbel,
  317. ARBEL_HCR_IN_CMD ( ARBEL_HCR_RST2INIT_QPEE,
  318. 1, sizeof ( *ctx ) ),
  319. 0, ctx, qpn, NULL );
  320. }
  321. static inline int
  322. arbel_cmd_init2rtr_qpee ( struct arbel *arbel, unsigned long qpn,
  323. const struct arbelprm_qp_ee_state_transitions *ctx ){
  324. return arbel_cmd ( arbel,
  325. ARBEL_HCR_IN_CMD ( ARBEL_HCR_INIT2RTR_QPEE,
  326. 1, sizeof ( *ctx ) ),
  327. 0, ctx, qpn, NULL );
  328. }
  329. static inline int
  330. arbel_cmd_rtr2rts_qpee ( struct arbel *arbel, unsigned long qpn,
  331. const struct arbelprm_qp_ee_state_transitions *ctx ) {
  332. return arbel_cmd ( arbel,
  333. ARBEL_HCR_IN_CMD ( ARBEL_HCR_RTR2RTS_QPEE,
  334. 1, sizeof ( *ctx ) ),
  335. 0, ctx, qpn, NULL );
  336. }
  337. static inline int
  338. arbel_cmd_rts2rts_qpee ( struct arbel *arbel, unsigned long qpn,
  339. const struct arbelprm_qp_ee_state_transitions *ctx ) {
  340. return arbel_cmd ( arbel,
  341. ARBEL_HCR_IN_CMD ( ARBEL_HCR_RTS2RTS_QPEE,
  342. 1, sizeof ( *ctx ) ),
  343. 0, ctx, qpn, NULL );
  344. }
  345. static inline int
  346. arbel_cmd_2rst_qpee ( struct arbel *arbel, unsigned long qpn ) {
  347. return arbel_cmd ( arbel,
  348. ARBEL_HCR_VOID_CMD ( ARBEL_HCR_2RST_QPEE ),
  349. 0x03, NULL, qpn, NULL );
  350. }
  351. static inline int
  352. arbel_cmd_query_qpee ( struct arbel *arbel, unsigned long qpn,
  353. struct arbelprm_qp_ee_state_transitions *ctx ) {
  354. return arbel_cmd ( arbel,
  355. ARBEL_HCR_OUT_CMD ( ARBEL_HCR_QUERY_QPEE,
  356. 1, sizeof ( *ctx ) ),
  357. 0, NULL, qpn, ctx );
  358. }
  359. static inline int
  360. arbel_cmd_conf_special_qp ( struct arbel *arbel, unsigned int qp_type,
  361. unsigned long base_qpn ) {
  362. return arbel_cmd ( arbel,
  363. ARBEL_HCR_VOID_CMD ( ARBEL_HCR_CONF_SPECIAL_QP ),
  364. qp_type, NULL, base_qpn, NULL );
  365. }
  366. static inline int
  367. arbel_cmd_mad_ifc ( struct arbel *arbel, unsigned int port,
  368. union arbelprm_mad *mad ) {
  369. return arbel_cmd ( arbel,
  370. ARBEL_HCR_INOUT_CMD ( ARBEL_HCR_MAD_IFC,
  371. 1, sizeof ( *mad ),
  372. 1, sizeof ( *mad ) ),
  373. 0x03, mad, port, mad );
  374. }
  375. static inline int
  376. arbel_cmd_read_mgm ( struct arbel *arbel, unsigned int index,
  377. struct arbelprm_mgm_entry *mgm ) {
  378. return arbel_cmd ( arbel,
  379. ARBEL_HCR_OUT_CMD ( ARBEL_HCR_READ_MGM,
  380. 1, sizeof ( *mgm ) ),
  381. 0, NULL, index, mgm );
  382. }
  383. static inline int
  384. arbel_cmd_write_mgm ( struct arbel *arbel, unsigned int index,
  385. const struct arbelprm_mgm_entry *mgm ) {
  386. return arbel_cmd ( arbel,
  387. ARBEL_HCR_IN_CMD ( ARBEL_HCR_WRITE_MGM,
  388. 1, sizeof ( *mgm ) ),
  389. 0, mgm, index, NULL );
  390. }
  391. static inline int
  392. arbel_cmd_mgid_hash ( struct arbel *arbel, const union ib_gid *gid,
  393. struct arbelprm_mgm_hash *hash ) {
  394. return arbel_cmd ( arbel,
  395. ARBEL_HCR_INOUT_CMD ( ARBEL_HCR_MGID_HASH,
  396. 1, sizeof ( *gid ),
  397. 0, sizeof ( *hash ) ),
  398. 0, gid, 0, hash );
  399. }
  400. static inline int
  401. arbel_cmd_run_fw ( struct arbel *arbel ) {
  402. return arbel_cmd ( arbel,
  403. ARBEL_HCR_VOID_CMD ( ARBEL_HCR_RUN_FW ),
  404. 0, NULL, 0, NULL );
  405. }
  406. static inline int
  407. arbel_cmd_disable_lam ( struct arbel *arbel ) {
  408. return arbel_cmd ( arbel,
  409. ARBEL_HCR_VOID_CMD ( ARBEL_HCR_DISABLE_LAM ),
  410. 0, NULL, 0, NULL );
  411. }
  412. static inline int
  413. arbel_cmd_enable_lam ( struct arbel *arbel, struct arbelprm_access_lam *lam ) {
  414. return arbel_cmd ( arbel,
  415. ARBEL_HCR_OUT_CMD ( ARBEL_HCR_ENABLE_LAM,
  416. 1, sizeof ( *lam ) ),
  417. 1, NULL, 0, lam );
  418. }
  419. static inline int
  420. arbel_cmd_unmap_icm ( struct arbel *arbel, unsigned int page_count,
  421. const struct arbelprm_scalar_parameter *offset ) {
  422. return arbel_cmd ( arbel,
  423. ARBEL_HCR_IN_CMD ( ARBEL_HCR_UNMAP_ICM, 0,
  424. sizeof ( *offset ) ),
  425. 0, offset, page_count, NULL );
  426. }
  427. static inline int
  428. arbel_cmd_map_icm ( struct arbel *arbel,
  429. const struct arbelprm_virtual_physical_mapping *map ) {
  430. return arbel_cmd ( arbel,
  431. ARBEL_HCR_IN_CMD ( ARBEL_HCR_MAP_ICM,
  432. 1, sizeof ( *map ) ),
  433. 0, map, 1, NULL );
  434. }
  435. static inline int
  436. arbel_cmd_unmap_icm_aux ( struct arbel *arbel ) {
  437. return arbel_cmd ( arbel,
  438. ARBEL_HCR_VOID_CMD ( ARBEL_HCR_UNMAP_ICM_AUX ),
  439. 0, NULL, 0, NULL );
  440. }
  441. static inline int
  442. arbel_cmd_map_icm_aux ( struct arbel *arbel,
  443. const struct arbelprm_virtual_physical_mapping *map ) {
  444. return arbel_cmd ( arbel,
  445. ARBEL_HCR_IN_CMD ( ARBEL_HCR_MAP_ICM_AUX,
  446. 1, sizeof ( *map ) ),
  447. 0, map, 1, NULL );
  448. }
  449. static inline int
  450. arbel_cmd_set_icm_size ( struct arbel *arbel,
  451. const struct arbelprm_scalar_parameter *icm_size,
  452. struct arbelprm_scalar_parameter *icm_aux_size ) {
  453. return arbel_cmd ( arbel,
  454. ARBEL_HCR_INOUT_CMD ( ARBEL_HCR_SET_ICM_SIZE,
  455. 0, sizeof ( *icm_size ),
  456. 0, sizeof ( *icm_aux_size ) ),
  457. 0, icm_size, 0, icm_aux_size );
  458. }
  459. static inline int
  460. arbel_cmd_unmap_fa ( struct arbel *arbel ) {
  461. return arbel_cmd ( arbel,
  462. ARBEL_HCR_VOID_CMD ( ARBEL_HCR_UNMAP_FA ),
  463. 0, NULL, 0, NULL );
  464. }
  465. static inline int
  466. arbel_cmd_map_fa ( struct arbel *arbel,
  467. const struct arbelprm_virtual_physical_mapping *map ) {
  468. return arbel_cmd ( arbel,
  469. ARBEL_HCR_IN_CMD ( ARBEL_HCR_MAP_FA,
  470. 1, sizeof ( *map ) ),
  471. 0, map, 1, NULL );
  472. }
  473. /***************************************************************************
  474. *
  475. * MAD operations
  476. *
  477. ***************************************************************************
  478. */
  479. /**
  480. * Issue management datagram
  481. *
  482. * @v ibdev Infiniband device
  483. * @v mad Management datagram
  484. * @ret rc Return status code
  485. */
  486. static int arbel_mad ( struct ib_device *ibdev, union ib_mad *mad ) {
  487. struct arbel *arbel = ib_get_drvdata ( ibdev );
  488. union arbelprm_mad mad_ifc;
  489. int rc;
  490. linker_assert ( sizeof ( *mad ) == sizeof ( mad_ifc.mad ),
  491. mad_size_mismatch );
  492. /* Copy in request packet */
  493. memcpy ( &mad_ifc.mad, mad, sizeof ( mad_ifc.mad ) );
  494. /* Issue MAD */
  495. if ( ( rc = arbel_cmd_mad_ifc ( arbel, ibdev->port,
  496. &mad_ifc ) ) != 0 ) {
  497. DBGC ( arbel, "Arbel %p port %d could not issue MAD IFC: %s\n",
  498. arbel, ibdev->port, strerror ( rc ) );
  499. return rc;
  500. }
  501. /* Copy out reply packet */
  502. memcpy ( mad, &mad_ifc.mad, sizeof ( *mad ) );
  503. if ( mad->hdr.status != 0 ) {
  504. DBGC ( arbel, "Arbel %p port %d MAD IFC status %04x\n",
  505. arbel, ibdev->port, ntohs ( mad->hdr.status ) );
  506. return -EIO;
  507. }
  508. return 0;
  509. }
  510. /***************************************************************************
  511. *
  512. * Completion queue operations
  513. *
  514. ***************************************************************************
  515. */
  516. /**
  517. * Dump completion queue context (for debugging only)
  518. *
  519. * @v arbel Arbel device
  520. * @v cq Completion queue
  521. * @ret rc Return status code
  522. */
  523. static __attribute__ (( unused )) int
  524. arbel_dump_cqctx ( struct arbel *arbel, struct ib_completion_queue *cq ) {
  525. struct arbelprm_completion_queue_context cqctx;
  526. int rc;
  527. memset ( &cqctx, 0, sizeof ( cqctx ) );
  528. if ( ( rc = arbel_cmd_query_cq ( arbel, cq->cqn, &cqctx ) ) != 0 ) {
  529. DBGC ( arbel, "Arbel %p CQN %#lx QUERY_CQ failed: %s\n",
  530. arbel, cq->cqn, strerror ( rc ) );
  531. return rc;
  532. }
  533. DBGC ( arbel, "Arbel %p CQN %#lx context:\n", arbel, cq->cqn );
  534. DBGC_HDA ( arbel, 0, &cqctx, sizeof ( cqctx ) );
  535. return 0;
  536. }
  537. /**
  538. * Create completion queue
  539. *
  540. * @v ibdev Infiniband device
  541. * @v cq Completion queue
  542. * @ret rc Return status code
  543. */
  544. static int arbel_create_cq ( struct ib_device *ibdev,
  545. struct ib_completion_queue *cq ) {
  546. struct arbel *arbel = ib_get_drvdata ( ibdev );
  547. struct arbel_completion_queue *arbel_cq;
  548. struct arbelprm_completion_queue_context cqctx;
  549. struct arbelprm_cq_ci_db_record *ci_db_rec;
  550. struct arbelprm_cq_arm_db_record *arm_db_rec;
  551. int cqn_offset;
  552. unsigned int i;
  553. int rc;
  554. /* Find a free completion queue number */
  555. cqn_offset = arbel_bitmask_alloc ( arbel->cq_inuse, ARBEL_MAX_CQS );
  556. if ( cqn_offset < 0 ) {
  557. DBGC ( arbel, "Arbel %p out of completion queues\n", arbel );
  558. rc = cqn_offset;
  559. goto err_cqn_offset;
  560. }
  561. cq->cqn = ( arbel->limits.reserved_cqs + cqn_offset );
  562. /* Allocate control structures */
  563. arbel_cq = zalloc ( sizeof ( *arbel_cq ) );
  564. if ( ! arbel_cq ) {
  565. rc = -ENOMEM;
  566. goto err_arbel_cq;
  567. }
  568. arbel_cq->ci_doorbell_idx = arbel_cq_ci_doorbell_idx ( arbel, cq );
  569. arbel_cq->arm_doorbell_idx = arbel_cq_arm_doorbell_idx ( arbel, cq );
  570. /* Allocate completion queue itself */
  571. arbel_cq->cqe_size = ( cq->num_cqes * sizeof ( arbel_cq->cqe[0] ) );
  572. arbel_cq->cqe = malloc_dma ( arbel_cq->cqe_size,
  573. sizeof ( arbel_cq->cqe[0] ) );
  574. if ( ! arbel_cq->cqe ) {
  575. rc = -ENOMEM;
  576. goto err_cqe;
  577. }
  578. memset ( arbel_cq->cqe, 0, arbel_cq->cqe_size );
  579. for ( i = 0 ; i < cq->num_cqes ; i++ ) {
  580. MLX_FILL_1 ( &arbel_cq->cqe[i].normal, 7, owner, 1 );
  581. }
  582. barrier();
  583. /* Initialise doorbell records */
  584. ci_db_rec = &arbel->db_rec[arbel_cq->ci_doorbell_idx].cq_ci;
  585. MLX_FILL_1 ( ci_db_rec, 0, counter, 0 );
  586. MLX_FILL_2 ( ci_db_rec, 1,
  587. res, ARBEL_UAR_RES_CQ_CI,
  588. cq_number, cq->cqn );
  589. arm_db_rec = &arbel->db_rec[arbel_cq->arm_doorbell_idx].cq_arm;
  590. MLX_FILL_1 ( arm_db_rec, 0, counter, 0 );
  591. MLX_FILL_2 ( arm_db_rec, 1,
  592. res, ARBEL_UAR_RES_CQ_ARM,
  593. cq_number, cq->cqn );
  594. /* Hand queue over to hardware */
  595. memset ( &cqctx, 0, sizeof ( cqctx ) );
  596. MLX_FILL_1 ( &cqctx, 0, st, 0xa /* "Event fired" */ );
  597. MLX_FILL_H ( &cqctx, 1, start_address_h,
  598. virt_to_bus ( arbel_cq->cqe ) );
  599. MLX_FILL_1 ( &cqctx, 2, start_address_l,
  600. virt_to_bus ( arbel_cq->cqe ) );
  601. MLX_FILL_2 ( &cqctx, 3,
  602. usr_page, arbel->limits.reserved_uars,
  603. log_cq_size, fls ( cq->num_cqes - 1 ) );
  604. MLX_FILL_1 ( &cqctx, 5, c_eqn, arbel->eq.eqn );
  605. MLX_FILL_1 ( &cqctx, 6, pd, ARBEL_GLOBAL_PD );
  606. MLX_FILL_1 ( &cqctx, 7, l_key, arbel->lkey );
  607. MLX_FILL_1 ( &cqctx, 12, cqn, cq->cqn );
  608. MLX_FILL_1 ( &cqctx, 13,
  609. cq_ci_db_record, arbel_cq->ci_doorbell_idx );
  610. MLX_FILL_1 ( &cqctx, 14,
  611. cq_state_db_record, arbel_cq->arm_doorbell_idx );
  612. if ( ( rc = arbel_cmd_sw2hw_cq ( arbel, cq->cqn, &cqctx ) ) != 0 ) {
  613. DBGC ( arbel, "Arbel %p CQN %#lx SW2HW_CQ failed: %s\n",
  614. arbel, cq->cqn, strerror ( rc ) );
  615. goto err_sw2hw_cq;
  616. }
  617. DBGC ( arbel, "Arbel %p CQN %#lx ring [%08lx,%08lx), doorbell %08lx\n",
  618. arbel, cq->cqn, virt_to_phys ( arbel_cq->cqe ),
  619. ( virt_to_phys ( arbel_cq->cqe ) + arbel_cq->cqe_size ),
  620. virt_to_phys ( ci_db_rec ) );
  621. ib_cq_set_drvdata ( cq, arbel_cq );
  622. return 0;
  623. err_sw2hw_cq:
  624. MLX_FILL_1 ( ci_db_rec, 1, res, ARBEL_UAR_RES_NONE );
  625. MLX_FILL_1 ( arm_db_rec, 1, res, ARBEL_UAR_RES_NONE );
  626. free_dma ( arbel_cq->cqe, arbel_cq->cqe_size );
  627. err_cqe:
  628. free ( arbel_cq );
  629. err_arbel_cq:
  630. arbel_bitmask_free ( arbel->cq_inuse, cqn_offset );
  631. err_cqn_offset:
  632. return rc;
  633. }
  634. /**
  635. * Destroy completion queue
  636. *
  637. * @v ibdev Infiniband device
  638. * @v cq Completion queue
  639. */
  640. static void arbel_destroy_cq ( struct ib_device *ibdev,
  641. struct ib_completion_queue *cq ) {
  642. struct arbel *arbel = ib_get_drvdata ( ibdev );
  643. struct arbel_completion_queue *arbel_cq = ib_cq_get_drvdata ( cq );
  644. struct arbelprm_completion_queue_context cqctx;
  645. struct arbelprm_cq_ci_db_record *ci_db_rec;
  646. struct arbelprm_cq_arm_db_record *arm_db_rec;
  647. int cqn_offset;
  648. int rc;
  649. /* Take ownership back from hardware */
  650. if ( ( rc = arbel_cmd_hw2sw_cq ( arbel, cq->cqn, &cqctx ) ) != 0 ) {
  651. DBGC ( arbel, "Arbel %p CQN %#lx FATAL HW2SW_CQ failed: "
  652. "%s\n", arbel, cq->cqn, strerror ( rc ) );
  653. /* Leak memory and return; at least we avoid corruption */
  654. return;
  655. }
  656. /* Clear doorbell records */
  657. ci_db_rec = &arbel->db_rec[arbel_cq->ci_doorbell_idx].cq_ci;
  658. arm_db_rec = &arbel->db_rec[arbel_cq->arm_doorbell_idx].cq_arm;
  659. MLX_FILL_1 ( ci_db_rec, 1, res, ARBEL_UAR_RES_NONE );
  660. MLX_FILL_1 ( arm_db_rec, 1, res, ARBEL_UAR_RES_NONE );
  661. /* Free memory */
  662. free_dma ( arbel_cq->cqe, arbel_cq->cqe_size );
  663. free ( arbel_cq );
  664. /* Mark queue number as free */
  665. cqn_offset = ( cq->cqn - arbel->limits.reserved_cqs );
  666. arbel_bitmask_free ( arbel->cq_inuse, cqn_offset );
  667. ib_cq_set_drvdata ( cq, NULL );
  668. }
  669. /***************************************************************************
  670. *
  671. * Queue pair operations
  672. *
  673. ***************************************************************************
  674. */
  675. /**
  676. * Assign queue pair number
  677. *
  678. * @v ibdev Infiniband device
  679. * @v qp Queue pair
  680. * @ret rc Return status code
  681. */
  682. static int arbel_alloc_qpn ( struct ib_device *ibdev,
  683. struct ib_queue_pair *qp ) {
  684. struct arbel *arbel = ib_get_drvdata ( ibdev );
  685. unsigned int port_offset;
  686. int qpn_offset;
  687. /* Calculate queue pair number */
  688. port_offset = ( ibdev->port - ARBEL_PORT_BASE );
  689. switch ( qp->type ) {
  690. case IB_QPT_SMI:
  691. qp->qpn = ( arbel->special_qpn_base + port_offset );
  692. return 0;
  693. case IB_QPT_GSI:
  694. qp->qpn = ( arbel->special_qpn_base + 2 + port_offset );
  695. return 0;
  696. case IB_QPT_UD:
  697. case IB_QPT_RC:
  698. /* Find a free queue pair number */
  699. qpn_offset = arbel_bitmask_alloc ( arbel->qp_inuse,
  700. ARBEL_MAX_QPS );
  701. if ( qpn_offset < 0 ) {
  702. DBGC ( arbel, "Arbel %p out of queue pairs\n",
  703. arbel );
  704. return qpn_offset;
  705. }
  706. qp->qpn = ( ( random() & ARBEL_QPN_RANDOM_MASK ) |
  707. ( arbel->qpn_base + qpn_offset ) );
  708. return 0;
  709. default:
  710. DBGC ( arbel, "Arbel %p unsupported QP type %d\n",
  711. arbel, qp->type );
  712. return -ENOTSUP;
  713. }
  714. }
  715. /**
  716. * Free queue pair number
  717. *
  718. * @v ibdev Infiniband device
  719. * @v qp Queue pair
  720. */
  721. static void arbel_free_qpn ( struct ib_device *ibdev,
  722. struct ib_queue_pair *qp ) {
  723. struct arbel *arbel = ib_get_drvdata ( ibdev );
  724. int qpn_offset;
  725. qpn_offset = ( ( qp->qpn & ~ARBEL_QPN_RANDOM_MASK ) - arbel->qpn_base );
  726. if ( qpn_offset >= 0 )
  727. arbel_bitmask_free ( arbel->qp_inuse, qpn_offset );
  728. }
  729. /**
  730. * Calculate transmission rate
  731. *
  732. * @v av Address vector
  733. * @ret arbel_rate Arbel rate
  734. */
  735. static unsigned int arbel_rate ( struct ib_address_vector *av ) {
  736. return ( ( ( av->rate >= IB_RATE_2_5 ) && ( av->rate <= IB_RATE_120 ) )
  737. ? ( av->rate + 5 ) : 0 );
  738. }
  739. /** Queue pair transport service type map */
  740. static uint8_t arbel_qp_st[] = {
  741. [IB_QPT_SMI] = ARBEL_ST_MLX,
  742. [IB_QPT_GSI] = ARBEL_ST_MLX,
  743. [IB_QPT_UD] = ARBEL_ST_UD,
  744. [IB_QPT_RC] = ARBEL_ST_RC,
  745. };
  746. /**
  747. * Dump queue pair context (for debugging only)
  748. *
  749. * @v arbel Arbel device
  750. * @v qp Queue pair
  751. * @ret rc Return status code
  752. */
  753. static __attribute__ (( unused )) int
  754. arbel_dump_qpctx ( struct arbel *arbel, struct ib_queue_pair *qp ) {
  755. struct arbelprm_qp_ee_state_transitions qpctx;
  756. int rc;
  757. memset ( &qpctx, 0, sizeof ( qpctx ) );
  758. if ( ( rc = arbel_cmd_query_qpee ( arbel, qp->qpn, &qpctx ) ) != 0 ) {
  759. DBGC ( arbel, "Arbel %p QPN %#lx QUERY_QPEE failed: %s\n",
  760. arbel, qp->qpn, strerror ( rc ) );
  761. return rc;
  762. }
  763. DBGC ( arbel, "Arbel %p QPN %#lx context:\n", arbel, qp->qpn );
  764. DBGC_HDA ( arbel, 0, &qpctx.u.dwords[2], ( sizeof ( qpctx ) - 8 ) );
  765. return 0;
  766. }
  767. /**
  768. * Create send work queue
  769. *
  770. * @v arbel_send_wq Send work queue
  771. * @v num_wqes Number of work queue entries
  772. * @ret rc Return status code
  773. */
  774. static int arbel_create_send_wq ( struct arbel_send_work_queue *arbel_send_wq,
  775. unsigned int num_wqes ) {
  776. union arbel_send_wqe *wqe;
  777. union arbel_send_wqe *next_wqe;
  778. unsigned int wqe_idx_mask;
  779. unsigned int i;
  780. /* Allocate work queue */
  781. arbel_send_wq->wqe_size = ( num_wqes *
  782. sizeof ( arbel_send_wq->wqe[0] ) );
  783. arbel_send_wq->wqe = malloc_dma ( arbel_send_wq->wqe_size,
  784. sizeof ( arbel_send_wq->wqe[0] ) );
  785. if ( ! arbel_send_wq->wqe )
  786. return -ENOMEM;
  787. memset ( arbel_send_wq->wqe, 0, arbel_send_wq->wqe_size );
  788. /* Link work queue entries */
  789. wqe_idx_mask = ( num_wqes - 1 );
  790. for ( i = 0 ; i < num_wqes ; i++ ) {
  791. wqe = &arbel_send_wq->wqe[i];
  792. next_wqe = &arbel_send_wq->wqe[ ( i + 1 ) & wqe_idx_mask ];
  793. MLX_FILL_1 ( &wqe->next, 0, nda_31_6,
  794. ( virt_to_bus ( next_wqe ) >> 6 ) );
  795. MLX_FILL_1 ( &wqe->next, 1, always1, 1 );
  796. }
  797. return 0;
  798. }
  799. /**
  800. * Create receive work queue
  801. *
  802. * @v arbel_recv_wq Receive work queue
  803. * @v num_wqes Number of work queue entries
  804. * @v type Queue pair type
  805. * @ret rc Return status code
  806. */
  807. static int arbel_create_recv_wq ( struct arbel_recv_work_queue *arbel_recv_wq,
  808. unsigned int num_wqes,
  809. enum ib_queue_pair_type type ) {
  810. struct arbelprm_recv_wqe *wqe;
  811. struct arbelprm_recv_wqe *next_wqe;
  812. unsigned int wqe_idx_mask;
  813. size_t nds;
  814. unsigned int i;
  815. unsigned int j;
  816. int rc;
  817. /* Allocate work queue */
  818. arbel_recv_wq->wqe_size = ( num_wqes *
  819. sizeof ( arbel_recv_wq->wqe[0] ) );
  820. arbel_recv_wq->wqe = malloc_dma ( arbel_recv_wq->wqe_size,
  821. sizeof ( arbel_recv_wq->wqe[0] ) );
  822. if ( ! arbel_recv_wq->wqe ) {
  823. rc = -ENOMEM;
  824. goto err_alloc_wqe;
  825. }
  826. memset ( arbel_recv_wq->wqe, 0, arbel_recv_wq->wqe_size );
  827. /* Allocate GRH entries, if needed */
  828. if ( ( type == IB_QPT_SMI ) || ( type == IB_QPT_GSI ) ||
  829. ( type == IB_QPT_UD ) ) {
  830. arbel_recv_wq->grh_size = ( num_wqes *
  831. sizeof ( arbel_recv_wq->grh[0] ) );
  832. arbel_recv_wq->grh = malloc_dma ( arbel_recv_wq->grh_size,
  833. sizeof ( void * ) );
  834. if ( ! arbel_recv_wq->grh ) {
  835. rc = -ENOMEM;
  836. goto err_alloc_grh;
  837. }
  838. }
  839. /* Link work queue entries */
  840. wqe_idx_mask = ( num_wqes - 1 );
  841. nds = ( ( offsetof ( typeof ( *wqe ), data ) +
  842. sizeof ( wqe->data[0] ) ) >> 4 );
  843. for ( i = 0 ; i < num_wqes ; i++ ) {
  844. wqe = &arbel_recv_wq->wqe[i].recv;
  845. next_wqe = &arbel_recv_wq->wqe[( i + 1 ) & wqe_idx_mask].recv;
  846. MLX_FILL_1 ( &wqe->next, 0, nda_31_6,
  847. ( virt_to_bus ( next_wqe ) >> 6 ) );
  848. MLX_FILL_1 ( &wqe->next, 1, nds, nds );
  849. for ( j = 0 ; ( ( ( void * ) &wqe->data[j] ) <
  850. ( ( void * ) ( wqe + 1 ) ) ) ; j++ ) {
  851. MLX_FILL_1 ( &wqe->data[j], 1,
  852. l_key, ARBEL_INVALID_LKEY );
  853. }
  854. }
  855. return 0;
  856. free_dma ( arbel_recv_wq->grh, arbel_recv_wq->grh_size );
  857. err_alloc_grh:
  858. free_dma ( arbel_recv_wq->wqe, arbel_recv_wq->wqe_size );
  859. err_alloc_wqe:
  860. return rc;
  861. }
  862. /**
  863. * Create queue pair
  864. *
  865. * @v ibdev Infiniband device
  866. * @v qp Queue pair
  867. * @ret rc Return status code
  868. */
  869. static int arbel_create_qp ( struct ib_device *ibdev,
  870. struct ib_queue_pair *qp ) {
  871. struct arbel *arbel = ib_get_drvdata ( ibdev );
  872. struct arbel_queue_pair *arbel_qp;
  873. struct arbelprm_qp_ee_state_transitions qpctx;
  874. struct arbelprm_qp_db_record *send_db_rec;
  875. struct arbelprm_qp_db_record *recv_db_rec;
  876. physaddr_t send_wqe_base_adr;
  877. physaddr_t recv_wqe_base_adr;
  878. physaddr_t wqe_base_adr;
  879. int rc;
  880. /* Warn about dysfunctional code
  881. *
  882. * Arbel seems to crash the system as soon as the first send
  883. * WQE completes on an RC queue pair. (NOPs complete
  884. * successfully, so this is a problem specific to the work
  885. * queue rather than the completion queue.) The cause of this
  886. * problem has remained unknown for over a year. Patches to
  887. * fix this are welcome.
  888. */
  889. if ( qp->type == IB_QPT_RC )
  890. DBG ( "*** WARNING: Arbel RC support is non-functional ***\n" );
  891. /* Calculate queue pair number */
  892. if ( ( rc = arbel_alloc_qpn ( ibdev, qp ) ) != 0 )
  893. goto err_alloc_qpn;
  894. /* Allocate control structures */
  895. arbel_qp = zalloc ( sizeof ( *arbel_qp ) );
  896. if ( ! arbel_qp ) {
  897. rc = -ENOMEM;
  898. goto err_arbel_qp;
  899. }
  900. arbel_qp->send.doorbell_idx = arbel_send_doorbell_idx ( arbel, qp );
  901. arbel_qp->recv.doorbell_idx = arbel_recv_doorbell_idx ( arbel, qp );
  902. /* Create send and receive work queues */
  903. if ( ( rc = arbel_create_send_wq ( &arbel_qp->send,
  904. qp->send.num_wqes ) ) != 0 )
  905. goto err_create_send_wq;
  906. if ( ( rc = arbel_create_recv_wq ( &arbel_qp->recv, qp->recv.num_wqes,
  907. qp->type ) ) != 0 )
  908. goto err_create_recv_wq;
  909. /* Send and receive work queue entries must be within the same 4GB */
  910. send_wqe_base_adr = virt_to_bus ( arbel_qp->send.wqe );
  911. recv_wqe_base_adr = virt_to_bus ( arbel_qp->recv.wqe );
  912. if ( ( sizeof ( physaddr_t ) > sizeof ( uint32_t ) ) &&
  913. ( ( ( ( uint64_t ) send_wqe_base_adr ) >> 32 ) !=
  914. ( ( ( uint64_t ) recv_wqe_base_adr ) >> 32 ) ) ) {
  915. DBGC ( arbel, "Arbel %p QPN %#lx cannot support send %08lx "
  916. "recv %08lx\n", arbel, qp->qpn,
  917. send_wqe_base_adr, recv_wqe_base_adr );
  918. rc = -ENOTSUP;
  919. goto err_unsupported_address_split;
  920. }
  921. wqe_base_adr = send_wqe_base_adr;
  922. /* Initialise doorbell records */
  923. send_db_rec = &arbel->db_rec[arbel_qp->send.doorbell_idx].qp;
  924. MLX_FILL_1 ( send_db_rec, 0, counter, 0 );
  925. MLX_FILL_2 ( send_db_rec, 1,
  926. res, ARBEL_UAR_RES_SQ,
  927. qp_number, qp->qpn );
  928. recv_db_rec = &arbel->db_rec[arbel_qp->recv.doorbell_idx].qp;
  929. MLX_FILL_1 ( recv_db_rec, 0, counter, 0 );
  930. MLX_FILL_2 ( recv_db_rec, 1,
  931. res, ARBEL_UAR_RES_RQ,
  932. qp_number, qp->qpn );
  933. /* Transition queue to INIT state */
  934. memset ( &qpctx, 0, sizeof ( qpctx ) );
  935. MLX_FILL_3 ( &qpctx, 2,
  936. qpc_eec_data.de, 1,
  937. qpc_eec_data.pm_state, ARBEL_PM_STATE_MIGRATED,
  938. qpc_eec_data.st, arbel_qp_st[qp->type] );
  939. MLX_FILL_4 ( &qpctx, 4,
  940. qpc_eec_data.log_rq_size, fls ( qp->recv.num_wqes - 1 ),
  941. qpc_eec_data.log_rq_stride,
  942. ( fls ( sizeof ( arbel_qp->recv.wqe[0] ) - 1 ) - 4 ),
  943. qpc_eec_data.log_sq_size, fls ( qp->send.num_wqes - 1 ),
  944. qpc_eec_data.log_sq_stride,
  945. ( fls ( sizeof ( arbel_qp->send.wqe[0] ) - 1 ) - 4 ) );
  946. MLX_FILL_1 ( &qpctx, 5,
  947. qpc_eec_data.usr_page, arbel->limits.reserved_uars );
  948. MLX_FILL_1 ( &qpctx, 10, qpc_eec_data.primary_address_path.port_number,
  949. ibdev->port );
  950. MLX_FILL_1 ( &qpctx, 27, qpc_eec_data.pd, ARBEL_GLOBAL_PD );
  951. MLX_FILL_H ( &qpctx, 28, qpc_eec_data.wqe_base_adr_h, wqe_base_adr );
  952. MLX_FILL_1 ( &qpctx, 29, qpc_eec_data.wqe_lkey, arbel->lkey );
  953. MLX_FILL_1 ( &qpctx, 30, qpc_eec_data.ssc, 1 );
  954. MLX_FILL_1 ( &qpctx, 33, qpc_eec_data.cqn_snd, qp->send.cq->cqn );
  955. MLX_FILL_1 ( &qpctx, 34, qpc_eec_data.snd_wqe_base_adr_l,
  956. ( send_wqe_base_adr >> 6 ) );
  957. MLX_FILL_1 ( &qpctx, 35, qpc_eec_data.snd_db_record_index,
  958. arbel_qp->send.doorbell_idx );
  959. MLX_FILL_4 ( &qpctx, 38,
  960. qpc_eec_data.rre, 1,
  961. qpc_eec_data.rwe, 1,
  962. qpc_eec_data.rae, 1,
  963. qpc_eec_data.rsc, 1 );
  964. MLX_FILL_1 ( &qpctx, 41, qpc_eec_data.cqn_rcv, qp->recv.cq->cqn );
  965. MLX_FILL_1 ( &qpctx, 42, qpc_eec_data.rcv_wqe_base_adr_l,
  966. ( recv_wqe_base_adr >> 6 ) );
  967. MLX_FILL_1 ( &qpctx, 43, qpc_eec_data.rcv_db_record_index,
  968. arbel_qp->recv.doorbell_idx );
  969. if ( ( rc = arbel_cmd_rst2init_qpee ( arbel, qp->qpn, &qpctx )) != 0 ){
  970. DBGC ( arbel, "Arbel %p QPN %#lx RST2INIT_QPEE failed: %s\n",
  971. arbel, qp->qpn, strerror ( rc ) );
  972. goto err_rst2init_qpee;
  973. }
  974. arbel_qp->state = ARBEL_QP_ST_INIT;
  975. DBGC ( arbel, "Arbel %p QPN %#lx send ring [%08lx,%08lx), doorbell "
  976. "%08lx\n", arbel, qp->qpn, virt_to_phys ( arbel_qp->send.wqe ),
  977. ( virt_to_phys ( arbel_qp->send.wqe ) +
  978. arbel_qp->send.wqe_size ),
  979. virt_to_phys ( send_db_rec ) );
  980. DBGC ( arbel, "Arbel %p QPN %#lx receive ring [%08lx,%08lx), doorbell "
  981. "%08lx\n", arbel, qp->qpn, virt_to_phys ( arbel_qp->recv.wqe ),
  982. ( virt_to_phys ( arbel_qp->recv.wqe ) +
  983. arbel_qp->recv.wqe_size ),
  984. virt_to_phys ( recv_db_rec ) );
  985. DBGC ( arbel, "Arbel %p QPN %#lx send CQN %#lx receive CQN %#lx\n",
  986. arbel, qp->qpn, qp->send.cq->cqn, qp->recv.cq->cqn );
  987. ib_qp_set_drvdata ( qp, arbel_qp );
  988. return 0;
  989. arbel_cmd_2rst_qpee ( arbel, qp->qpn );
  990. err_rst2init_qpee:
  991. MLX_FILL_1 ( send_db_rec, 1, res, ARBEL_UAR_RES_NONE );
  992. MLX_FILL_1 ( recv_db_rec, 1, res, ARBEL_UAR_RES_NONE );
  993. err_unsupported_address_split:
  994. free_dma ( arbel_qp->recv.grh, arbel_qp->recv.grh_size );
  995. free_dma ( arbel_qp->recv.wqe, arbel_qp->recv.wqe_size );
  996. err_create_recv_wq:
  997. free_dma ( arbel_qp->send.wqe, arbel_qp->send.wqe_size );
  998. err_create_send_wq:
  999. free ( arbel_qp );
  1000. err_arbel_qp:
  1001. arbel_free_qpn ( ibdev, qp );
  1002. err_alloc_qpn:
  1003. return rc;
  1004. }
  1005. /**
  1006. * Modify queue pair
  1007. *
  1008. * @v ibdev Infiniband device
  1009. * @v qp Queue pair
  1010. * @ret rc Return status code
  1011. */
  1012. static int arbel_modify_qp ( struct ib_device *ibdev,
  1013. struct ib_queue_pair *qp ) {
  1014. struct arbel *arbel = ib_get_drvdata ( ibdev );
  1015. struct arbel_queue_pair *arbel_qp = ib_qp_get_drvdata ( qp );
  1016. struct arbelprm_qp_ee_state_transitions qpctx;
  1017. int rc;
  1018. /* Transition queue to RTR state, if applicable */
  1019. if ( arbel_qp->state < ARBEL_QP_ST_RTR ) {
  1020. memset ( &qpctx, 0, sizeof ( qpctx ) );
  1021. MLX_FILL_2 ( &qpctx, 4,
  1022. qpc_eec_data.mtu, ARBEL_MTU_2048,
  1023. qpc_eec_data.msg_max, 31 );
  1024. MLX_FILL_1 ( &qpctx, 7,
  1025. qpc_eec_data.remote_qpn_een, qp->av.qpn );
  1026. MLX_FILL_2 ( &qpctx, 11,
  1027. qpc_eec_data.primary_address_path.rnr_retry,
  1028. ARBEL_RETRY_MAX,
  1029. qpc_eec_data.primary_address_path.rlid,
  1030. qp->av.lid );
  1031. MLX_FILL_2 ( &qpctx, 12,
  1032. qpc_eec_data.primary_address_path.ack_timeout,
  1033. 14 /* 4.096us * 2^(14) = 67ms */,
  1034. qpc_eec_data.primary_address_path.max_stat_rate,
  1035. arbel_rate ( &qp->av ) );
  1036. memcpy ( &qpctx.u.dwords[14], &qp->av.gid,
  1037. sizeof ( qp->av.gid ) );
  1038. MLX_FILL_1 ( &qpctx, 30,
  1039. qpc_eec_data.retry_count, ARBEL_RETRY_MAX );
  1040. MLX_FILL_1 ( &qpctx, 39,
  1041. qpc_eec_data.next_rcv_psn, qp->recv.psn );
  1042. MLX_FILL_1 ( &qpctx, 40,
  1043. qpc_eec_data.ra_buff_indx,
  1044. ( arbel->limits.reserved_rdbs +
  1045. ( ( qp->qpn & ~ARBEL_QPN_RANDOM_MASK ) -
  1046. arbel->special_qpn_base ) ) );
  1047. if ( ( rc = arbel_cmd_init2rtr_qpee ( arbel, qp->qpn,
  1048. &qpctx ) ) != 0 ) {
  1049. DBGC ( arbel, "Arbel %p QPN %#lx INIT2RTR_QPEE failed:"
  1050. " %s\n", arbel, qp->qpn, strerror ( rc ) );
  1051. return rc;
  1052. }
  1053. arbel_qp->state = ARBEL_QP_ST_RTR;
  1054. }
  1055. /* Transition queue to RTS state, if applicable */
  1056. if ( arbel_qp->state < ARBEL_QP_ST_RTS ) {
  1057. memset ( &qpctx, 0, sizeof ( qpctx ) );
  1058. MLX_FILL_1 ( &qpctx, 11,
  1059. qpc_eec_data.primary_address_path.rnr_retry,
  1060. ARBEL_RETRY_MAX );
  1061. MLX_FILL_1 ( &qpctx, 12,
  1062. qpc_eec_data.primary_address_path.ack_timeout,
  1063. 14 /* 4.096us * 2^(14) = 67ms */ );
  1064. MLX_FILL_2 ( &qpctx, 30,
  1065. qpc_eec_data.retry_count, ARBEL_RETRY_MAX,
  1066. qpc_eec_data.sic, 1 );
  1067. MLX_FILL_1 ( &qpctx, 32,
  1068. qpc_eec_data.next_send_psn, qp->send.psn );
  1069. if ( ( rc = arbel_cmd_rtr2rts_qpee ( arbel, qp->qpn,
  1070. &qpctx ) ) != 0 ) {
  1071. DBGC ( arbel, "Arbel %p QPN %#lx RTR2RTS_QPEE failed: "
  1072. "%s\n", arbel, qp->qpn, strerror ( rc ) );
  1073. return rc;
  1074. }
  1075. arbel_qp->state = ARBEL_QP_ST_RTS;
  1076. }
  1077. /* Update parameters in RTS state */
  1078. memset ( &qpctx, 0, sizeof ( qpctx ) );
  1079. MLX_FILL_1 ( &qpctx, 0, opt_param_mask, ARBEL_QPEE_OPT_PARAM_QKEY );
  1080. MLX_FILL_1 ( &qpctx, 44, qpc_eec_data.q_key, qp->qkey );
  1081. if ( ( rc = arbel_cmd_rts2rts_qpee ( arbel, qp->qpn, &qpctx ) ) != 0 ){
  1082. DBGC ( arbel, "Arbel %p QPN %#lx RTS2RTS_QPEE failed: %s\n",
  1083. arbel, qp->qpn, strerror ( rc ) );
  1084. return rc;
  1085. }
  1086. return 0;
  1087. }
  1088. /**
  1089. * Destroy queue pair
  1090. *
  1091. * @v ibdev Infiniband device
  1092. * @v qp Queue pair
  1093. */
  1094. static void arbel_destroy_qp ( struct ib_device *ibdev,
  1095. struct ib_queue_pair *qp ) {
  1096. struct arbel *arbel = ib_get_drvdata ( ibdev );
  1097. struct arbel_queue_pair *arbel_qp = ib_qp_get_drvdata ( qp );
  1098. struct arbelprm_qp_db_record *send_db_rec;
  1099. struct arbelprm_qp_db_record *recv_db_rec;
  1100. int rc;
  1101. /* Take ownership back from hardware */
  1102. if ( ( rc = arbel_cmd_2rst_qpee ( arbel, qp->qpn ) ) != 0 ) {
  1103. DBGC ( arbel, "Arbel %p QPN %#lx FATAL 2RST_QPEE failed: "
  1104. "%s\n", arbel, qp->qpn, strerror ( rc ) );
  1105. /* Leak memory and return; at least we avoid corruption */
  1106. return;
  1107. }
  1108. /* Clear doorbell records */
  1109. send_db_rec = &arbel->db_rec[arbel_qp->send.doorbell_idx].qp;
  1110. recv_db_rec = &arbel->db_rec[arbel_qp->recv.doorbell_idx].qp;
  1111. MLX_FILL_1 ( send_db_rec, 1, res, ARBEL_UAR_RES_NONE );
  1112. MLX_FILL_1 ( recv_db_rec, 1, res, ARBEL_UAR_RES_NONE );
  1113. /* Free memory */
  1114. free_dma ( arbel_qp->recv.grh, arbel_qp->recv.grh_size );
  1115. free_dma ( arbel_qp->recv.wqe, arbel_qp->recv.wqe_size );
  1116. free_dma ( arbel_qp->send.wqe, arbel_qp->send.wqe_size );
  1117. free ( arbel_qp );
  1118. /* Mark queue number as free */
  1119. arbel_free_qpn ( ibdev, qp );
  1120. ib_qp_set_drvdata ( qp, NULL );
  1121. }
  1122. /***************************************************************************
  1123. *
  1124. * Work request operations
  1125. *
  1126. ***************************************************************************
  1127. */
  1128. /**
  1129. * Ring doorbell register in UAR
  1130. *
  1131. * @v arbel Arbel device
  1132. * @v db_reg Doorbell register structure
  1133. * @v offset Address of doorbell
  1134. */
  1135. static void arbel_ring_doorbell ( struct arbel *arbel,
  1136. union arbelprm_doorbell_register *db_reg,
  1137. unsigned int offset ) {
  1138. DBGC2 ( arbel, "Arbel %p ringing doorbell %08x:%08x at %lx\n",
  1139. arbel, ntohl ( db_reg->dword[0] ), ntohl ( db_reg->dword[1] ),
  1140. virt_to_phys ( arbel->uar + offset ) );
  1141. barrier();
  1142. writel ( db_reg->dword[0], ( arbel->uar + offset + 0 ) );
  1143. barrier();
  1144. writel ( db_reg->dword[1], ( arbel->uar + offset + 4 ) );
  1145. }
  1146. /** GID used for GID-less send work queue entries */
  1147. static const union ib_gid arbel_no_gid = {
  1148. .bytes = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0 },
  1149. };
  1150. /**
  1151. * Construct UD send work queue entry
  1152. *
  1153. * @v ibdev Infiniband device
  1154. * @v qp Queue pair
  1155. * @v dest Destination address vector
  1156. * @v iobuf I/O buffer
  1157. * @v wqe Send work queue entry
  1158. * @ret nds Work queue entry size
  1159. */
  1160. static size_t arbel_fill_ud_send_wqe ( struct ib_device *ibdev,
  1161. struct ib_queue_pair *qp __unused,
  1162. struct ib_address_vector *dest,
  1163. struct io_buffer *iobuf,
  1164. union arbel_send_wqe *wqe ) {
  1165. struct arbel *arbel = ib_get_drvdata ( ibdev );
  1166. const union ib_gid *gid;
  1167. /* Construct this work queue entry */
  1168. MLX_FILL_1 ( &wqe->ud.ctrl, 0, always1, 1 );
  1169. MLX_FILL_2 ( &wqe->ud.ud, 0,
  1170. ud_address_vector.pd, ARBEL_GLOBAL_PD,
  1171. ud_address_vector.port_number, ibdev->port );
  1172. MLX_FILL_2 ( &wqe->ud.ud, 1,
  1173. ud_address_vector.rlid, dest->lid,
  1174. ud_address_vector.g, dest->gid_present );
  1175. MLX_FILL_2 ( &wqe->ud.ud, 2,
  1176. ud_address_vector.max_stat_rate, arbel_rate ( dest ),
  1177. ud_address_vector.msg, 3 );
  1178. MLX_FILL_1 ( &wqe->ud.ud, 3, ud_address_vector.sl, dest->sl );
  1179. gid = ( dest->gid_present ? &dest->gid : &arbel_no_gid );
  1180. memcpy ( &wqe->ud.ud.u.dwords[4], gid, sizeof ( *gid ) );
  1181. MLX_FILL_1 ( &wqe->ud.ud, 8, destination_qp, dest->qpn );
  1182. MLX_FILL_1 ( &wqe->ud.ud, 9, q_key, dest->qkey );
  1183. MLX_FILL_1 ( &wqe->ud.data[0], 0, byte_count, iob_len ( iobuf ) );
  1184. MLX_FILL_1 ( &wqe->ud.data[0], 1, l_key, arbel->lkey );
  1185. MLX_FILL_H ( &wqe->ud.data[0], 2,
  1186. local_address_h, virt_to_bus ( iobuf->data ) );
  1187. MLX_FILL_1 ( &wqe->ud.data[0], 3,
  1188. local_address_l, virt_to_bus ( iobuf->data ) );
  1189. return ( offsetof ( typeof ( wqe->ud ), data[1] ) >> 4 );
  1190. }
  1191. /**
  1192. * Construct MLX send work queue entry
  1193. *
  1194. * @v ibdev Infiniband device
  1195. * @v qp Queue pair
  1196. * @v dest Destination address vector
  1197. * @v iobuf I/O buffer
  1198. * @v wqe Send work queue entry
  1199. * @ret nds Work queue entry size
  1200. */
  1201. static size_t arbel_fill_mlx_send_wqe ( struct ib_device *ibdev,
  1202. struct ib_queue_pair *qp,
  1203. struct ib_address_vector *dest,
  1204. struct io_buffer *iobuf,
  1205. union arbel_send_wqe *wqe ) {
  1206. struct arbel *arbel = ib_get_drvdata ( ibdev );
  1207. struct io_buffer headers;
  1208. /* Construct IB headers */
  1209. iob_populate ( &headers, &wqe->mlx.headers, 0,
  1210. sizeof ( wqe->mlx.headers ) );
  1211. iob_reserve ( &headers, sizeof ( wqe->mlx.headers ) );
  1212. ib_push ( ibdev, &headers, qp, iob_len ( iobuf ), dest );
  1213. /* Construct this work queue entry */
  1214. MLX_FILL_5 ( &wqe->mlx.ctrl, 0,
  1215. c, 1 /* generate completion */,
  1216. icrc, 0 /* generate ICRC */,
  1217. max_statrate, arbel_rate ( dest ),
  1218. slr, 0,
  1219. v15, ( ( qp->ext_qpn == IB_QPN_SMI ) ? 1 : 0 ) );
  1220. MLX_FILL_1 ( &wqe->mlx.ctrl, 1, rlid, dest->lid );
  1221. MLX_FILL_1 ( &wqe->mlx.data[0], 0,
  1222. byte_count, iob_len ( &headers ) );
  1223. MLX_FILL_1 ( &wqe->mlx.data[0], 1, l_key, arbel->lkey );
  1224. MLX_FILL_H ( &wqe->mlx.data[0], 2,
  1225. local_address_h, virt_to_bus ( headers.data ) );
  1226. MLX_FILL_1 ( &wqe->mlx.data[0], 3,
  1227. local_address_l, virt_to_bus ( headers.data ) );
  1228. MLX_FILL_1 ( &wqe->mlx.data[1], 0,
  1229. byte_count, ( iob_len ( iobuf ) + 4 /* ICRC */ ) );
  1230. MLX_FILL_1 ( &wqe->mlx.data[1], 1, l_key, arbel->lkey );
  1231. MLX_FILL_H ( &wqe->mlx.data[1], 2,
  1232. local_address_h, virt_to_bus ( iobuf->data ) );
  1233. MLX_FILL_1 ( &wqe->mlx.data[1], 3,
  1234. local_address_l, virt_to_bus ( iobuf->data ) );
  1235. return ( offsetof ( typeof ( wqe->mlx ), data[2] ) >> 4 );
  1236. }
  1237. /**
  1238. * Construct RC send work queue entry
  1239. *
  1240. * @v ibdev Infiniband device
  1241. * @v qp Queue pair
  1242. * @v dest Destination address vector
  1243. * @v iobuf I/O buffer
  1244. * @v wqe Send work queue entry
  1245. * @ret nds Work queue entry size
  1246. */
  1247. static size_t arbel_fill_rc_send_wqe ( struct ib_device *ibdev,
  1248. struct ib_queue_pair *qp __unused,
  1249. struct ib_address_vector *dest __unused,
  1250. struct io_buffer *iobuf,
  1251. union arbel_send_wqe *wqe ) {
  1252. struct arbel *arbel = ib_get_drvdata ( ibdev );
  1253. /* Construct this work queue entry */
  1254. MLX_FILL_1 ( &wqe->rc.ctrl, 0, always1, 1 );
  1255. MLX_FILL_1 ( &wqe->rc.data[0], 0, byte_count, iob_len ( iobuf ) );
  1256. MLX_FILL_1 ( &wqe->rc.data[0], 1, l_key, arbel->lkey );
  1257. MLX_FILL_H ( &wqe->rc.data[0], 2,
  1258. local_address_h, virt_to_bus ( iobuf->data ) );
  1259. MLX_FILL_1 ( &wqe->rc.data[0], 3,
  1260. local_address_l, virt_to_bus ( iobuf->data ) );
  1261. return ( offsetof ( typeof ( wqe->rc ), data[1] ) >> 4 );
  1262. }
  1263. /** Work queue entry constructors */
  1264. static size_t
  1265. ( * arbel_fill_send_wqe[] ) ( struct ib_device *ibdev,
  1266. struct ib_queue_pair *qp,
  1267. struct ib_address_vector *dest,
  1268. struct io_buffer *iobuf,
  1269. union arbel_send_wqe *wqe ) = {
  1270. [IB_QPT_SMI] = arbel_fill_mlx_send_wqe,
  1271. [IB_QPT_GSI] = arbel_fill_mlx_send_wqe,
  1272. [IB_QPT_UD] = arbel_fill_ud_send_wqe,
  1273. [IB_QPT_RC] = arbel_fill_rc_send_wqe,
  1274. };
  1275. /**
  1276. * Post send work queue entry
  1277. *
  1278. * @v ibdev Infiniband device
  1279. * @v qp Queue pair
  1280. * @v dest Destination address vector
  1281. * @v iobuf I/O buffer
  1282. * @ret rc Return status code
  1283. */
  1284. static int arbel_post_send ( struct ib_device *ibdev,
  1285. struct ib_queue_pair *qp,
  1286. struct ib_address_vector *dest,
  1287. struct io_buffer *iobuf ) {
  1288. struct arbel *arbel = ib_get_drvdata ( ibdev );
  1289. struct arbel_queue_pair *arbel_qp = ib_qp_get_drvdata ( qp );
  1290. struct ib_work_queue *wq = &qp->send;
  1291. struct arbel_send_work_queue *arbel_send_wq = &arbel_qp->send;
  1292. union arbel_send_wqe *prev_wqe;
  1293. union arbel_send_wqe *wqe;
  1294. struct arbelprm_qp_db_record *qp_db_rec;
  1295. union arbelprm_doorbell_register db_reg;
  1296. unsigned long wqe_idx_mask;
  1297. size_t nds;
  1298. /* Allocate work queue entry */
  1299. wqe_idx_mask = ( wq->num_wqes - 1 );
  1300. if ( wq->iobufs[wq->next_idx & wqe_idx_mask] ) {
  1301. DBGC ( arbel, "Arbel %p QPN %#lx send queue full",
  1302. arbel, qp->qpn );
  1303. return -ENOBUFS;
  1304. }
  1305. wq->iobufs[wq->next_idx & wqe_idx_mask] = iobuf;
  1306. prev_wqe = &arbel_send_wq->wqe[(wq->next_idx - 1) & wqe_idx_mask];
  1307. wqe = &arbel_send_wq->wqe[wq->next_idx & wqe_idx_mask];
  1308. /* Construct work queue entry */
  1309. memset ( ( ( ( void * ) wqe ) + sizeof ( wqe->next ) ), 0,
  1310. ( sizeof ( *wqe ) - sizeof ( wqe->next ) ) );
  1311. assert ( qp->type < ( sizeof ( arbel_fill_send_wqe ) /
  1312. sizeof ( arbel_fill_send_wqe[0] ) ) );
  1313. assert ( arbel_fill_send_wqe[qp->type] != NULL );
  1314. nds = arbel_fill_send_wqe[qp->type] ( ibdev, qp, dest, iobuf, wqe );
  1315. DBGCP ( arbel, "Arbel %p QPN %#lx posting send WQE %#lx:\n",
  1316. arbel, qp->qpn, ( wq->next_idx & wqe_idx_mask ) );
  1317. DBGCP_HDA ( arbel, virt_to_phys ( wqe ), wqe, sizeof ( *wqe ) );
  1318. /* Update previous work queue entry's "next" field */
  1319. MLX_SET ( &prev_wqe->next, nopcode, ARBEL_OPCODE_SEND );
  1320. MLX_FILL_3 ( &prev_wqe->next, 1,
  1321. nds, nds,
  1322. f, 0,
  1323. always1, 1 );
  1324. /* Update doorbell record */
  1325. barrier();
  1326. qp_db_rec = &arbel->db_rec[arbel_send_wq->doorbell_idx].qp;
  1327. MLX_FILL_1 ( qp_db_rec, 0,
  1328. counter, ( ( wq->next_idx + 1 ) & 0xffff ) );
  1329. /* Ring doorbell register */
  1330. MLX_FILL_4 ( &db_reg.send, 0,
  1331. nopcode, ARBEL_OPCODE_SEND,
  1332. f, 0,
  1333. wqe_counter, ( wq->next_idx & 0xffff ),
  1334. wqe_cnt, 1 );
  1335. MLX_FILL_2 ( &db_reg.send, 1,
  1336. nds, nds,
  1337. qpn, qp->qpn );
  1338. arbel_ring_doorbell ( arbel, &db_reg, ARBEL_DB_POST_SND_OFFSET );
  1339. /* Update work queue's index */
  1340. wq->next_idx++;
  1341. return 0;
  1342. }
  1343. /**
  1344. * Post receive work queue entry
  1345. *
  1346. * @v ibdev Infiniband device
  1347. * @v qp Queue pair
  1348. * @v iobuf I/O buffer
  1349. * @ret rc Return status code
  1350. */
  1351. static int arbel_post_recv ( struct ib_device *ibdev,
  1352. struct ib_queue_pair *qp,
  1353. struct io_buffer *iobuf ) {
  1354. struct arbel *arbel = ib_get_drvdata ( ibdev );
  1355. struct arbel_queue_pair *arbel_qp = ib_qp_get_drvdata ( qp );
  1356. struct ib_work_queue *wq = &qp->recv;
  1357. struct arbel_recv_work_queue *arbel_recv_wq = &arbel_qp->recv;
  1358. struct arbelprm_recv_wqe *wqe;
  1359. struct arbelprm_wqe_segment_data_ptr *data;
  1360. struct ib_global_route_header *grh;
  1361. union arbelprm_doorbell_record *db_rec;
  1362. unsigned int wqe_idx_mask;
  1363. /* Allocate work queue entry */
  1364. wqe_idx_mask = ( wq->num_wqes - 1 );
  1365. if ( wq->iobufs[wq->next_idx & wqe_idx_mask] ) {
  1366. DBGC ( arbel, "Arbel %p QPN %#lx receive queue full\n",
  1367. arbel, qp->qpn );
  1368. return -ENOBUFS;
  1369. }
  1370. wq->iobufs[wq->next_idx & wqe_idx_mask] = iobuf;
  1371. wqe = &arbel_recv_wq->wqe[wq->next_idx & wqe_idx_mask].recv;
  1372. /* Construct work queue entry */
  1373. data = &wqe->data[0];
  1374. if ( arbel_recv_wq->grh ) {
  1375. grh = &arbel_recv_wq->grh[wq->next_idx & wqe_idx_mask];
  1376. MLX_FILL_1 ( data, 0, byte_count, sizeof ( *grh ) );
  1377. MLX_FILL_1 ( data, 1, l_key, arbel->lkey );
  1378. MLX_FILL_H ( data, 2, local_address_h, virt_to_bus ( grh ) );
  1379. MLX_FILL_1 ( data, 3, local_address_l, virt_to_bus ( grh ) );
  1380. data++;
  1381. }
  1382. MLX_FILL_1 ( data, 0, byte_count, iob_tailroom ( iobuf ) );
  1383. MLX_FILL_1 ( data, 1, l_key, arbel->lkey );
  1384. MLX_FILL_H ( data, 2, local_address_h, virt_to_bus ( iobuf->data ) );
  1385. MLX_FILL_1 ( data, 3, local_address_l, virt_to_bus ( iobuf->data ) );
  1386. /* Update doorbell record */
  1387. barrier();
  1388. db_rec = &arbel->db_rec[arbel_recv_wq->doorbell_idx];
  1389. MLX_FILL_1 ( &db_rec->qp, 0,
  1390. counter, ( ( wq->next_idx + 1 ) & 0xffff ) );
  1391. /* Update work queue's index */
  1392. wq->next_idx++;
  1393. return 0;
  1394. }
  1395. /**
  1396. * Handle completion
  1397. *
  1398. * @v ibdev Infiniband device
  1399. * @v cq Completion queue
  1400. * @v cqe Hardware completion queue entry
  1401. * @ret rc Return status code
  1402. */
  1403. static int arbel_complete ( struct ib_device *ibdev,
  1404. struct ib_completion_queue *cq,
  1405. union arbelprm_completion_entry *cqe ) {
  1406. struct arbel *arbel = ib_get_drvdata ( ibdev );
  1407. struct ib_work_queue *wq;
  1408. struct ib_queue_pair *qp;
  1409. struct arbel_queue_pair *arbel_qp;
  1410. struct arbel_send_work_queue *arbel_send_wq;
  1411. struct arbel_recv_work_queue *arbel_recv_wq;
  1412. struct arbelprm_recv_wqe *recv_wqe;
  1413. struct io_buffer *iobuf;
  1414. struct ib_address_vector recv_dest;
  1415. struct ib_address_vector recv_source;
  1416. struct ib_global_route_header *grh;
  1417. struct ib_address_vector *source;
  1418. unsigned int opcode;
  1419. unsigned long qpn;
  1420. int is_send;
  1421. unsigned long wqe_adr;
  1422. unsigned long wqe_idx;
  1423. size_t len;
  1424. int rc = 0;
  1425. /* Parse completion */
  1426. qpn = MLX_GET ( &cqe->normal, my_qpn );
  1427. is_send = MLX_GET ( &cqe->normal, s );
  1428. wqe_adr = ( MLX_GET ( &cqe->normal, wqe_adr ) << 6 );
  1429. opcode = MLX_GET ( &cqe->normal, opcode );
  1430. if ( opcode >= ARBEL_OPCODE_RECV_ERROR ) {
  1431. /* "s" field is not valid for error opcodes */
  1432. is_send = ( opcode == ARBEL_OPCODE_SEND_ERROR );
  1433. DBGC ( arbel, "Arbel %p CQN %#lx %s QPN %#lx syndrome %#x "
  1434. "vendor %#x\n", arbel, cq->cqn,
  1435. ( is_send ? "send" : "recv" ), qpn,
  1436. MLX_GET ( &cqe->error, syndrome ),
  1437. MLX_GET ( &cqe->error, vendor_code ) );
  1438. DBGC_HDA ( arbel, virt_to_phys ( cqe ), cqe, sizeof ( *cqe ) );
  1439. rc = -EIO;
  1440. /* Don't return immediately; propagate error to completer */
  1441. }
  1442. /* Identify work queue */
  1443. wq = ib_find_wq ( cq, qpn, is_send );
  1444. if ( ! wq ) {
  1445. DBGC ( arbel, "Arbel %p CQN %#lx unknown %s QPN %#lx\n",
  1446. arbel, cq->cqn, ( is_send ? "send" : "recv" ), qpn );
  1447. return -EIO;
  1448. }
  1449. qp = wq->qp;
  1450. arbel_qp = ib_qp_get_drvdata ( qp );
  1451. arbel_send_wq = &arbel_qp->send;
  1452. arbel_recv_wq = &arbel_qp->recv;
  1453. /* Identify work queue entry index */
  1454. if ( is_send ) {
  1455. wqe_idx = ( ( wqe_adr - virt_to_bus ( arbel_send_wq->wqe ) ) /
  1456. sizeof ( arbel_send_wq->wqe[0] ) );
  1457. assert ( wqe_idx < qp->send.num_wqes );
  1458. } else {
  1459. wqe_idx = ( ( wqe_adr - virt_to_bus ( arbel_recv_wq->wqe ) ) /
  1460. sizeof ( arbel_recv_wq->wqe[0] ) );
  1461. assert ( wqe_idx < qp->recv.num_wqes );
  1462. }
  1463. DBGCP ( arbel, "Arbel %p CQN %#lx QPN %#lx %s WQE %#lx completed:\n",
  1464. arbel, cq->cqn, qp->qpn, ( is_send ? "send" : "recv" ),
  1465. wqe_idx );
  1466. DBGCP_HDA ( arbel, virt_to_phys ( cqe ), cqe, sizeof ( *cqe ) );
  1467. /* Identify I/O buffer */
  1468. iobuf = wq->iobufs[wqe_idx];
  1469. if ( ! iobuf ) {
  1470. DBGC ( arbel, "Arbel %p CQN %#lx QPN %#lx empty %s WQE %#lx\n",
  1471. arbel, cq->cqn, qp->qpn, ( is_send ? "send" : "recv" ),
  1472. wqe_idx );
  1473. return -EIO;
  1474. }
  1475. wq->iobufs[wqe_idx] = NULL;
  1476. if ( is_send ) {
  1477. /* Hand off to completion handler */
  1478. ib_complete_send ( ibdev, qp, iobuf, rc );
  1479. } else {
  1480. /* Set received length */
  1481. len = MLX_GET ( &cqe->normal, byte_cnt );
  1482. recv_wqe = &arbel_recv_wq->wqe[wqe_idx].recv;
  1483. assert ( MLX_GET ( &recv_wqe->data[0], local_address_l ) ==
  1484. virt_to_bus ( iobuf->data ) );
  1485. assert ( MLX_GET ( &recv_wqe->data[0], byte_count ) ==
  1486. iob_tailroom ( iobuf ) );
  1487. MLX_FILL_1 ( &recv_wqe->data[0], 0, byte_count, 0 );
  1488. MLX_FILL_1 ( &recv_wqe->data[0], 1,
  1489. l_key, ARBEL_INVALID_LKEY );
  1490. memset ( &recv_dest, 0, sizeof ( recv_dest ) );
  1491. recv_dest.qpn = qpn;
  1492. switch ( qp->type ) {
  1493. case IB_QPT_SMI:
  1494. case IB_QPT_GSI:
  1495. case IB_QPT_UD:
  1496. /* Locate corresponding GRH */
  1497. assert ( arbel_recv_wq->grh != NULL );
  1498. grh = &arbel_recv_wq->grh[wqe_idx];
  1499. len -= sizeof ( *grh );
  1500. /* Construct address vector */
  1501. source = &recv_source;
  1502. memset ( source, 0, sizeof ( *source ) );
  1503. source->qpn = MLX_GET ( &cqe->normal, rqpn );
  1504. source->lid = MLX_GET ( &cqe->normal, rlid );
  1505. source->sl = MLX_GET ( &cqe->normal, sl );
  1506. recv_dest.gid_present = source->gid_present =
  1507. MLX_GET ( &cqe->normal, g );
  1508. memcpy ( &recv_dest.gid, &grh->dgid,
  1509. sizeof ( recv_dest.gid ) );
  1510. memcpy ( &source->gid, &grh->sgid,
  1511. sizeof ( source->gid ) );
  1512. break;
  1513. case IB_QPT_RC:
  1514. source = &qp->av;
  1515. break;
  1516. default:
  1517. assert ( 0 );
  1518. return -EINVAL;
  1519. }
  1520. assert ( len <= iob_tailroom ( iobuf ) );
  1521. iob_put ( iobuf, len );
  1522. /* Hand off to completion handler */
  1523. ib_complete_recv ( ibdev, qp, &recv_dest, source, iobuf, rc );
  1524. }
  1525. return rc;
  1526. }
  1527. /**
  1528. * Poll completion queue
  1529. *
  1530. * @v ibdev Infiniband device
  1531. * @v cq Completion queue
  1532. */
  1533. static void arbel_poll_cq ( struct ib_device *ibdev,
  1534. struct ib_completion_queue *cq ) {
  1535. struct arbel *arbel = ib_get_drvdata ( ibdev );
  1536. struct arbel_completion_queue *arbel_cq = ib_cq_get_drvdata ( cq );
  1537. struct arbelprm_cq_ci_db_record *ci_db_rec;
  1538. union arbelprm_completion_entry *cqe;
  1539. unsigned int cqe_idx_mask;
  1540. int rc;
  1541. while ( 1 ) {
  1542. /* Look for completion entry */
  1543. cqe_idx_mask = ( cq->num_cqes - 1 );
  1544. cqe = &arbel_cq->cqe[cq->next_idx & cqe_idx_mask];
  1545. if ( MLX_GET ( &cqe->normal, owner ) != 0 ) {
  1546. /* Entry still owned by hardware; end of poll */
  1547. break;
  1548. }
  1549. /* Handle completion */
  1550. if ( ( rc = arbel_complete ( ibdev, cq, cqe ) ) != 0 ) {
  1551. DBGC ( arbel, "Arbel %p CQN %#lx failed to complete: "
  1552. "%s\n", arbel, cq->cqn, strerror ( rc ) );
  1553. DBGC_HD ( arbel, cqe, sizeof ( *cqe ) );
  1554. }
  1555. /* Return ownership to hardware */
  1556. MLX_FILL_1 ( &cqe->normal, 7, owner, 1 );
  1557. barrier();
  1558. /* Update completion queue's index */
  1559. cq->next_idx++;
  1560. /* Update doorbell record */
  1561. ci_db_rec = &arbel->db_rec[arbel_cq->ci_doorbell_idx].cq_ci;
  1562. MLX_FILL_1 ( ci_db_rec, 0,
  1563. counter, ( cq->next_idx & 0xffffffffUL ) );
  1564. }
  1565. }
  1566. /***************************************************************************
  1567. *
  1568. * Event queues
  1569. *
  1570. ***************************************************************************
  1571. */
  1572. /**
  1573. * Create event queue
  1574. *
  1575. * @v arbel Arbel device
  1576. * @ret rc Return status code
  1577. */
  1578. static int arbel_create_eq ( struct arbel *arbel ) {
  1579. struct arbel_event_queue *arbel_eq = &arbel->eq;
  1580. struct arbelprm_eqc eqctx;
  1581. struct arbelprm_event_mask mask;
  1582. unsigned int i;
  1583. int rc;
  1584. /* Select event queue number */
  1585. arbel_eq->eqn = arbel->limits.reserved_eqs;
  1586. /* Calculate doorbell address */
  1587. arbel_eq->doorbell = ( arbel->eq_ci_doorbells +
  1588. ARBEL_DB_EQ_OFFSET ( arbel_eq->eqn ) );
  1589. /* Allocate event queue itself */
  1590. arbel_eq->eqe_size =
  1591. ( ARBEL_NUM_EQES * sizeof ( arbel_eq->eqe[0] ) );
  1592. arbel_eq->eqe = malloc_dma ( arbel_eq->eqe_size,
  1593. sizeof ( arbel_eq->eqe[0] ) );
  1594. if ( ! arbel_eq->eqe ) {
  1595. rc = -ENOMEM;
  1596. goto err_eqe;
  1597. }
  1598. memset ( arbel_eq->eqe, 0, arbel_eq->eqe_size );
  1599. for ( i = 0 ; i < ARBEL_NUM_EQES ; i++ ) {
  1600. MLX_FILL_1 ( &arbel_eq->eqe[i].generic, 7, owner, 1 );
  1601. }
  1602. barrier();
  1603. /* Hand queue over to hardware */
  1604. memset ( &eqctx, 0, sizeof ( eqctx ) );
  1605. MLX_FILL_1 ( &eqctx, 0, st, 0xa /* "Fired" */ );
  1606. MLX_FILL_H ( &eqctx, 1,
  1607. start_address_h, virt_to_phys ( arbel_eq->eqe ) );
  1608. MLX_FILL_1 ( &eqctx, 2,
  1609. start_address_l, virt_to_phys ( arbel_eq->eqe ) );
  1610. MLX_FILL_1 ( &eqctx, 3, log_eq_size, fls ( ARBEL_NUM_EQES - 1 ) );
  1611. MLX_FILL_1 ( &eqctx, 6, pd, ARBEL_GLOBAL_PD );
  1612. MLX_FILL_1 ( &eqctx, 7, lkey, arbel->lkey );
  1613. if ( ( rc = arbel_cmd_sw2hw_eq ( arbel, arbel_eq->eqn,
  1614. &eqctx ) ) != 0 ) {
  1615. DBGC ( arbel, "Arbel %p EQN %#lx SW2HW_EQ failed: %s\n",
  1616. arbel, arbel_eq->eqn, strerror ( rc ) );
  1617. goto err_sw2hw_eq;
  1618. }
  1619. /* Map events to this event queue */
  1620. memset ( &mask, 0xff, sizeof ( mask ) );
  1621. if ( ( rc = arbel_cmd_map_eq ( arbel,
  1622. ( ARBEL_MAP_EQ | arbel_eq->eqn ),
  1623. &mask ) ) != 0 ) {
  1624. DBGC ( arbel, "Arbel %p EQN %#lx MAP_EQ failed: %s\n",
  1625. arbel, arbel_eq->eqn, strerror ( rc ) );
  1626. goto err_map_eq;
  1627. }
  1628. DBGC ( arbel, "Arbel %p EQN %#lx ring [%08lx,%08lx), doorbell %08lx\n",
  1629. arbel, arbel_eq->eqn, virt_to_phys ( arbel_eq->eqe ),
  1630. ( virt_to_phys ( arbel_eq->eqe ) + arbel_eq->eqe_size ),
  1631. virt_to_phys ( arbel_eq->doorbell ) );
  1632. return 0;
  1633. err_map_eq:
  1634. arbel_cmd_hw2sw_eq ( arbel, arbel_eq->eqn, &eqctx );
  1635. err_sw2hw_eq:
  1636. free_dma ( arbel_eq->eqe, arbel_eq->eqe_size );
  1637. err_eqe:
  1638. memset ( arbel_eq, 0, sizeof ( *arbel_eq ) );
  1639. return rc;
  1640. }
  1641. /**
  1642. * Destroy event queue
  1643. *
  1644. * @v arbel Arbel device
  1645. */
  1646. static void arbel_destroy_eq ( struct arbel *arbel ) {
  1647. struct arbel_event_queue *arbel_eq = &arbel->eq;
  1648. struct arbelprm_eqc eqctx;
  1649. struct arbelprm_event_mask mask;
  1650. int rc;
  1651. /* Unmap events from event queue */
  1652. memset ( &mask, 0, sizeof ( mask ) );
  1653. MLX_FILL_1 ( &mask, 1, port_state_change, 1 );
  1654. if ( ( rc = arbel_cmd_map_eq ( arbel,
  1655. ( ARBEL_UNMAP_EQ | arbel_eq->eqn ),
  1656. &mask ) ) != 0 ) {
  1657. DBGC ( arbel, "Arbel %p EQN %#lx FATAL MAP_EQ failed to "
  1658. "unmap: %s\n", arbel, arbel_eq->eqn, strerror ( rc ) );
  1659. /* Continue; HCA may die but system should survive */
  1660. }
  1661. /* Take ownership back from hardware */
  1662. if ( ( rc = arbel_cmd_hw2sw_eq ( arbel, arbel_eq->eqn,
  1663. &eqctx ) ) != 0 ) {
  1664. DBGC ( arbel, "Arbel %p EQN %#lx FATAL HW2SW_EQ failed: %s\n",
  1665. arbel, arbel_eq->eqn, strerror ( rc ) );
  1666. /* Leak memory and return; at least we avoid corruption */
  1667. return;
  1668. }
  1669. /* Free memory */
  1670. free_dma ( arbel_eq->eqe, arbel_eq->eqe_size );
  1671. memset ( arbel_eq, 0, sizeof ( *arbel_eq ) );
  1672. }
  1673. /**
  1674. * Handle port state event
  1675. *
  1676. * @v arbel Arbel device
  1677. * @v eqe Port state change event queue entry
  1678. */
  1679. static void arbel_event_port_state_change ( struct arbel *arbel,
  1680. union arbelprm_event_entry *eqe){
  1681. unsigned int port;
  1682. int link_up;
  1683. /* Get port and link status */
  1684. port = ( MLX_GET ( &eqe->port_state_change, data.p ) - 1 );
  1685. link_up = ( MLX_GET ( &eqe->generic, event_sub_type ) & 0x04 );
  1686. DBGC ( arbel, "Arbel %p port %d link %s\n", arbel, ( port + 1 ),
  1687. ( link_up ? "up" : "down" ) );
  1688. /* Sanity check */
  1689. if ( port >= ARBEL_NUM_PORTS ) {
  1690. DBGC ( arbel, "Arbel %p port %d does not exist!\n",
  1691. arbel, ( port + 1 ) );
  1692. return;
  1693. }
  1694. /* Update MAD parameters */
  1695. ib_smc_update ( arbel->ibdev[port], arbel_mad );
  1696. }
  1697. /**
  1698. * Poll event queue
  1699. *
  1700. * @v ibdev Infiniband device
  1701. */
  1702. static void arbel_poll_eq ( struct ib_device *ibdev ) {
  1703. struct arbel *arbel = ib_get_drvdata ( ibdev );
  1704. struct arbel_event_queue *arbel_eq = &arbel->eq;
  1705. union arbelprm_event_entry *eqe;
  1706. union arbelprm_eq_doorbell_register db_reg;
  1707. unsigned int eqe_idx_mask;
  1708. unsigned int event_type;
  1709. /* No event is generated upon reaching INIT, so we must poll
  1710. * separately for link state changes while we remain DOWN.
  1711. */
  1712. if ( ib_is_open ( ibdev ) &&
  1713. ( ibdev->port_state == IB_PORT_STATE_DOWN ) ) {
  1714. ib_smc_update ( ibdev, arbel_mad );
  1715. }
  1716. /* Poll event queue */
  1717. while ( 1 ) {
  1718. /* Look for event entry */
  1719. eqe_idx_mask = ( ARBEL_NUM_EQES - 1 );
  1720. eqe = &arbel_eq->eqe[arbel_eq->next_idx & eqe_idx_mask];
  1721. if ( MLX_GET ( &eqe->generic, owner ) != 0 ) {
  1722. /* Entry still owned by hardware; end of poll */
  1723. break;
  1724. }
  1725. DBGCP ( arbel, "Arbel %p EQN %#lx event:\n",
  1726. arbel, arbel_eq->eqn );
  1727. DBGCP_HDA ( arbel, virt_to_phys ( eqe ),
  1728. eqe, sizeof ( *eqe ) );
  1729. /* Handle event */
  1730. event_type = MLX_GET ( &eqe->generic, event_type );
  1731. switch ( event_type ) {
  1732. case ARBEL_EV_PORT_STATE_CHANGE:
  1733. arbel_event_port_state_change ( arbel, eqe );
  1734. break;
  1735. default:
  1736. DBGC ( arbel, "Arbel %p EQN %#lx unrecognised event "
  1737. "type %#x:\n",
  1738. arbel, arbel_eq->eqn, event_type );
  1739. DBGC_HDA ( arbel, virt_to_phys ( eqe ),
  1740. eqe, sizeof ( *eqe ) );
  1741. break;
  1742. }
  1743. /* Return ownership to hardware */
  1744. MLX_FILL_1 ( &eqe->generic, 7, owner, 1 );
  1745. barrier();
  1746. /* Update event queue's index */
  1747. arbel_eq->next_idx++;
  1748. /* Ring doorbell */
  1749. MLX_FILL_1 ( &db_reg.ci, 0, ci, arbel_eq->next_idx );
  1750. writel ( db_reg.dword[0], arbel_eq->doorbell );
  1751. }
  1752. }
  1753. /***************************************************************************
  1754. *
  1755. * Firmware control
  1756. *
  1757. ***************************************************************************
  1758. */
  1759. /**
  1760. * Map virtual to physical address for firmware usage
  1761. *
  1762. * @v arbel Arbel device
  1763. * @v map Mapping function
  1764. * @v va Virtual address
  1765. * @v pa Physical address
  1766. * @v len Length of region
  1767. * @ret rc Return status code
  1768. */
  1769. static int arbel_map_vpm ( struct arbel *arbel,
  1770. int ( *map ) ( struct arbel *arbel,
  1771. const struct arbelprm_virtual_physical_mapping* ),
  1772. uint64_t va, physaddr_t pa, size_t len ) {
  1773. struct arbelprm_virtual_physical_mapping mapping;
  1774. physaddr_t start;
  1775. physaddr_t low;
  1776. physaddr_t high;
  1777. physaddr_t end;
  1778. size_t size;
  1779. int rc;
  1780. /* Sanity checks */
  1781. assert ( ( va & ( ARBEL_PAGE_SIZE - 1 ) ) == 0 );
  1782. assert ( ( pa & ( ARBEL_PAGE_SIZE - 1 ) ) == 0 );
  1783. assert ( ( len & ( ARBEL_PAGE_SIZE - 1 ) ) == 0 );
  1784. assert ( len != 0 );
  1785. /* Calculate starting points */
  1786. start = pa;
  1787. end = ( start + len );
  1788. size = ( 1UL << ( fls ( start ^ end ) - 1 ) );
  1789. low = high = ( end & ~( size - 1 ) );
  1790. assert ( start < low );
  1791. assert ( high <= end );
  1792. /* These mappings tend to generate huge volumes of
  1793. * uninteresting debug data, which basically makes it
  1794. * impossible to use debugging otherwise.
  1795. */
  1796. DBG_DISABLE ( DBGLVL_LOG | DBGLVL_EXTRA );
  1797. /* Map blocks in descending order of size */
  1798. while ( size >= ARBEL_PAGE_SIZE ) {
  1799. /* Find the next candidate block */
  1800. if ( ( low - size ) >= start ) {
  1801. low -= size;
  1802. pa = low;
  1803. } else if ( high <= ( end - size ) ) {
  1804. pa = high;
  1805. high += size;
  1806. } else {
  1807. size >>= 1;
  1808. continue;
  1809. }
  1810. assert ( ( va & ( size - 1 ) ) == 0 );
  1811. assert ( ( pa & ( size - 1 ) ) == 0 );
  1812. /* Map this block */
  1813. memset ( &mapping, 0, sizeof ( mapping ) );
  1814. MLX_FILL_1 ( &mapping, 0, va_h, ( va >> 32 ) );
  1815. MLX_FILL_1 ( &mapping, 1, va_l, ( va >> 12 ) );
  1816. MLX_FILL_H ( &mapping, 2, pa_h, pa );
  1817. MLX_FILL_2 ( &mapping, 3,
  1818. log2size, ( ( fls ( size ) - 1 ) - 12 ),
  1819. pa_l, ( pa >> 12 ) );
  1820. if ( ( rc = map ( arbel, &mapping ) ) != 0 ) {
  1821. DBG_ENABLE ( DBGLVL_LOG | DBGLVL_EXTRA );
  1822. DBGC ( arbel, "Arbel %p could not map %08llx+%zx to "
  1823. "%08lx: %s\n",
  1824. arbel, va, size, pa, strerror ( rc ) );
  1825. return rc;
  1826. }
  1827. va += size;
  1828. }
  1829. assert ( low == start );
  1830. assert ( high == end );
  1831. DBG_ENABLE ( DBGLVL_LOG | DBGLVL_EXTRA );
  1832. return 0;
  1833. }
  1834. /**
  1835. * Start firmware running
  1836. *
  1837. * @v arbel Arbel device
  1838. * @ret rc Return status code
  1839. */
  1840. static int arbel_start_firmware ( struct arbel *arbel ) {
  1841. struct arbelprm_query_fw fw;
  1842. struct arbelprm_access_lam lam;
  1843. unsigned int fw_pages;
  1844. size_t fw_len;
  1845. physaddr_t fw_base;
  1846. uint64_t eq_set_ci_base_addr;
  1847. int rc;
  1848. /* Get firmware parameters */
  1849. if ( ( rc = arbel_cmd_query_fw ( arbel, &fw ) ) != 0 ) {
  1850. DBGC ( arbel, "Arbel %p could not query firmware: %s\n",
  1851. arbel, strerror ( rc ) );
  1852. goto err_query_fw;
  1853. }
  1854. DBGC ( arbel, "Arbel %p firmware version %d.%d.%d\n", arbel,
  1855. MLX_GET ( &fw, fw_rev_major ), MLX_GET ( &fw, fw_rev_minor ),
  1856. MLX_GET ( &fw, fw_rev_subminor ) );
  1857. fw_pages = MLX_GET ( &fw, fw_pages );
  1858. DBGC ( arbel, "Arbel %p requires %d kB for firmware\n",
  1859. arbel, ( fw_pages * 4 ) );
  1860. eq_set_ci_base_addr =
  1861. ( ( (uint64_t) MLX_GET ( &fw, eq_set_ci_base_addr_h ) << 32 ) |
  1862. ( (uint64_t) MLX_GET ( &fw, eq_set_ci_base_addr_l ) ) );
  1863. arbel->eq_ci_doorbells = ioremap ( eq_set_ci_base_addr, 0x200 );
  1864. /* Enable locally-attached memory. Ignore failure; there may
  1865. * be no attached memory.
  1866. */
  1867. arbel_cmd_enable_lam ( arbel, &lam );
  1868. /* Allocate firmware pages and map firmware area */
  1869. fw_len = ( fw_pages * ARBEL_PAGE_SIZE );
  1870. if ( ! arbel->firmware_area ) {
  1871. arbel->firmware_len = fw_len;
  1872. arbel->firmware_area = umalloc ( arbel->firmware_len );
  1873. if ( ! arbel->firmware_area ) {
  1874. rc = -ENOMEM;
  1875. goto err_alloc_fa;
  1876. }
  1877. } else {
  1878. assert ( arbel->firmware_len == fw_len );
  1879. }
  1880. fw_base = user_to_phys ( arbel->firmware_area, 0 );
  1881. DBGC ( arbel, "Arbel %p firmware area at [%08lx,%08lx)\n",
  1882. arbel, fw_base, ( fw_base + fw_len ) );
  1883. if ( ( rc = arbel_map_vpm ( arbel, arbel_cmd_map_fa,
  1884. 0, fw_base, fw_len ) ) != 0 ) {
  1885. DBGC ( arbel, "Arbel %p could not map firmware: %s\n",
  1886. arbel, strerror ( rc ) );
  1887. goto err_map_fa;
  1888. }
  1889. /* Start firmware */
  1890. if ( ( rc = arbel_cmd_run_fw ( arbel ) ) != 0 ) {
  1891. DBGC ( arbel, "Arbel %p could not run firmware: %s\n",
  1892. arbel, strerror ( rc ) );
  1893. goto err_run_fw;
  1894. }
  1895. DBGC ( arbel, "Arbel %p firmware started\n", arbel );
  1896. return 0;
  1897. err_run_fw:
  1898. arbel_cmd_unmap_fa ( arbel );
  1899. err_map_fa:
  1900. err_alloc_fa:
  1901. err_query_fw:
  1902. return rc;
  1903. }
  1904. /**
  1905. * Stop firmware running
  1906. *
  1907. * @v arbel Arbel device
  1908. */
  1909. static void arbel_stop_firmware ( struct arbel *arbel ) {
  1910. int rc;
  1911. if ( ( rc = arbel_cmd_unmap_fa ( arbel ) ) != 0 ) {
  1912. DBGC ( arbel, "Arbel %p FATAL could not stop firmware: %s\n",
  1913. arbel, strerror ( rc ) );
  1914. /* Leak memory and return; at least we avoid corruption */
  1915. arbel->firmware_area = UNULL;
  1916. return;
  1917. }
  1918. }
  1919. /***************************************************************************
  1920. *
  1921. * Infinihost Context Memory management
  1922. *
  1923. ***************************************************************************
  1924. */
  1925. /**
  1926. * Get device limits
  1927. *
  1928. * @v arbel Arbel device
  1929. * @ret rc Return status code
  1930. */
  1931. static int arbel_get_limits ( struct arbel *arbel ) {
  1932. struct arbelprm_query_dev_lim dev_lim;
  1933. int rc;
  1934. if ( ( rc = arbel_cmd_query_dev_lim ( arbel, &dev_lim ) ) != 0 ) {
  1935. DBGC ( arbel, "Arbel %p could not get device limits: %s\n",
  1936. arbel, strerror ( rc ) );
  1937. return rc;
  1938. }
  1939. arbel->limits.reserved_qps =
  1940. ( 1 << MLX_GET ( &dev_lim, log2_rsvd_qps ) );
  1941. arbel->limits.qpc_entry_size = MLX_GET ( &dev_lim, qpc_entry_sz );
  1942. arbel->limits.eqpc_entry_size = MLX_GET ( &dev_lim, eqpc_entry_sz );
  1943. arbel->limits.reserved_srqs =
  1944. ( 1 << MLX_GET ( &dev_lim, log2_rsvd_srqs ) );
  1945. arbel->limits.srqc_entry_size = MLX_GET ( &dev_lim, srq_entry_sz );
  1946. arbel->limits.reserved_ees =
  1947. ( 1 << MLX_GET ( &dev_lim, log2_rsvd_ees ) );
  1948. arbel->limits.eec_entry_size = MLX_GET ( &dev_lim, eec_entry_sz );
  1949. arbel->limits.eeec_entry_size = MLX_GET ( &dev_lim, eeec_entry_sz );
  1950. arbel->limits.reserved_cqs =
  1951. ( 1 << MLX_GET ( &dev_lim, log2_rsvd_cqs ) );
  1952. arbel->limits.cqc_entry_size = MLX_GET ( &dev_lim, cqc_entry_sz );
  1953. arbel->limits.reserved_mtts =
  1954. ( 1 << MLX_GET ( &dev_lim, log2_rsvd_mtts ) );
  1955. arbel->limits.mtt_entry_size = MLX_GET ( &dev_lim, mtt_entry_sz );
  1956. arbel->limits.reserved_mrws =
  1957. ( 1 << MLX_GET ( &dev_lim, log2_rsvd_mrws ) );
  1958. arbel->limits.mpt_entry_size = MLX_GET ( &dev_lim, mpt_entry_sz );
  1959. arbel->limits.reserved_rdbs =
  1960. ( 1 << MLX_GET ( &dev_lim, log2_rsvd_rdbs ) );
  1961. arbel->limits.reserved_eqs = MLX_GET ( &dev_lim, num_rsvd_eqs );
  1962. arbel->limits.eqc_entry_size = MLX_GET ( &dev_lim, eqc_entry_sz );
  1963. arbel->limits.reserved_uars = MLX_GET ( &dev_lim, num_rsvd_uars );
  1964. arbel->limits.uar_scratch_entry_size =
  1965. MLX_GET ( &dev_lim, uar_scratch_entry_sz );
  1966. DBGC ( arbel, "Arbel %p reserves %d x %#zx QPC, %d x %#zx EQPC, "
  1967. "%d x %#zx SRQC\n", arbel,
  1968. arbel->limits.reserved_qps, arbel->limits.qpc_entry_size,
  1969. arbel->limits.reserved_qps, arbel->limits.eqpc_entry_size,
  1970. arbel->limits.reserved_srqs, arbel->limits.srqc_entry_size );
  1971. DBGC ( arbel, "Arbel %p reserves %d x %#zx EEC, %d x %#zx EEEC, "
  1972. "%d x %#zx CQC\n", arbel,
  1973. arbel->limits.reserved_ees, arbel->limits.eec_entry_size,
  1974. arbel->limits.reserved_ees, arbel->limits.eeec_entry_size,
  1975. arbel->limits.reserved_cqs, arbel->limits.cqc_entry_size );
  1976. DBGC ( arbel, "Arbel %p reserves %d x %#zx EQC, %d x %#zx MTT, "
  1977. "%d x %#zx MPT\n", arbel,
  1978. arbel->limits.reserved_eqs, arbel->limits.eqc_entry_size,
  1979. arbel->limits.reserved_mtts, arbel->limits.mtt_entry_size,
  1980. arbel->limits.reserved_mrws, arbel->limits.mpt_entry_size );
  1981. DBGC ( arbel, "Arbel %p reserves %d x %#zx RDB, %d x %#zx UAR, "
  1982. "%d x %#zx UAR scratchpad\n", arbel,
  1983. arbel->limits.reserved_rdbs, ARBEL_RDB_ENTRY_SIZE,
  1984. arbel->limits.reserved_uars, ARBEL_PAGE_SIZE,
  1985. arbel->limits.reserved_uars,
  1986. arbel->limits.uar_scratch_entry_size );
  1987. return 0;
  1988. }
  1989. /**
  1990. * Align ICM table
  1991. *
  1992. * @v icm_offset Current ICM offset
  1993. * @v len ICM table length
  1994. * @ret icm_offset ICM offset
  1995. */
  1996. static size_t icm_align ( size_t icm_offset, size_t len ) {
  1997. /* Round up to a multiple of the table size */
  1998. assert ( len == ( 1UL << ( fls ( len ) - 1 ) ) );
  1999. return ( ( icm_offset + len - 1 ) & ~( len - 1 ) );
  2000. }
  2001. /**
  2002. * Allocate ICM
  2003. *
  2004. * @v arbel Arbel device
  2005. * @v init_hca INIT_HCA structure to fill in
  2006. * @ret rc Return status code
  2007. */
  2008. static int arbel_alloc_icm ( struct arbel *arbel,
  2009. struct arbelprm_init_hca *init_hca ) {
  2010. struct arbelprm_scalar_parameter icm_size;
  2011. struct arbelprm_scalar_parameter icm_aux_size;
  2012. struct arbelprm_scalar_parameter unmap_icm;
  2013. union arbelprm_doorbell_record *db_rec;
  2014. size_t icm_offset = 0;
  2015. unsigned int log_num_uars, log_num_qps, log_num_srqs, log_num_ees;
  2016. unsigned int log_num_cqs, log_num_mtts, log_num_mpts, log_num_rdbs;
  2017. unsigned int log_num_eqs, log_num_mcs;
  2018. size_t icm_len, icm_aux_len;
  2019. size_t len;
  2020. physaddr_t icm_phys;
  2021. int rc;
  2022. /* Calculate number of each object type within ICM */
  2023. log_num_qps = fls ( arbel->limits.reserved_qps +
  2024. ARBEL_RSVD_SPECIAL_QPS + ARBEL_MAX_QPS - 1 );
  2025. log_num_srqs = fls ( arbel->limits.reserved_srqs - 1 );
  2026. log_num_ees = fls ( arbel->limits.reserved_ees - 1 );
  2027. log_num_cqs = fls ( arbel->limits.reserved_cqs + ARBEL_MAX_CQS - 1 );
  2028. log_num_eqs = fls ( arbel->limits.reserved_eqs + ARBEL_MAX_EQS - 1 );
  2029. log_num_mtts = fls ( arbel->limits.reserved_mtts - 1 );
  2030. log_num_mpts = fls ( arbel->limits.reserved_mrws + 1 - 1 );
  2031. log_num_rdbs = fls ( arbel->limits.reserved_rdbs +
  2032. ARBEL_RSVD_SPECIAL_QPS + ARBEL_MAX_QPS - 1 );
  2033. log_num_uars = fls ( arbel->limits.reserved_uars +
  2034. 1 /* single UAR used */ - 1 );
  2035. log_num_mcs = ARBEL_LOG_MULTICAST_HASH_SIZE;
  2036. /* Queue pair contexts */
  2037. len = ( ( 1 << log_num_qps ) * arbel->limits.qpc_entry_size );
  2038. icm_offset = icm_align ( icm_offset, len );
  2039. MLX_FILL_2 ( init_hca, 13,
  2040. qpc_eec_cqc_eqc_rdb_parameters.qpc_base_addr_l,
  2041. ( icm_offset >> 7 ),
  2042. qpc_eec_cqc_eqc_rdb_parameters.log_num_of_qp,
  2043. log_num_qps );
  2044. DBGC ( arbel, "Arbel %p ICM QPC is %d x %#zx at [%zx,%zx)\n",
  2045. arbel, ( 1 << log_num_qps ), arbel->limits.qpc_entry_size,
  2046. icm_offset, ( icm_offset + len ) );
  2047. icm_offset += len;
  2048. /* Extended queue pair contexts */
  2049. len = ( ( 1 << log_num_qps ) * arbel->limits.eqpc_entry_size );
  2050. icm_offset = icm_align ( icm_offset, len );
  2051. MLX_FILL_1 ( init_hca, 25,
  2052. qpc_eec_cqc_eqc_rdb_parameters.eqpc_base_addr_l,
  2053. icm_offset );
  2054. DBGC ( arbel, "Arbel %p ICM EQPC is %d x %#zx at [%zx,%zx)\n",
  2055. arbel, ( 1 << log_num_qps ), arbel->limits.eqpc_entry_size,
  2056. icm_offset, ( icm_offset + len ) );
  2057. icm_offset += len;
  2058. /* Completion queue contexts */
  2059. len = ( ( 1 << log_num_cqs ) * arbel->limits.cqc_entry_size );
  2060. icm_offset = icm_align ( icm_offset, len );
  2061. MLX_FILL_2 ( init_hca, 21,
  2062. qpc_eec_cqc_eqc_rdb_parameters.cqc_base_addr_l,
  2063. ( icm_offset >> 6 ),
  2064. qpc_eec_cqc_eqc_rdb_parameters.log_num_of_cq,
  2065. log_num_cqs );
  2066. DBGC ( arbel, "Arbel %p ICM CQC is %d x %#zx at [%zx,%zx)\n",
  2067. arbel, ( 1 << log_num_cqs ), arbel->limits.cqc_entry_size,
  2068. icm_offset, ( icm_offset + len ) );
  2069. icm_offset += len;
  2070. /* Event queue contexts */
  2071. len = ( ( 1 << log_num_eqs ) * arbel->limits.eqc_entry_size );
  2072. icm_offset = icm_align ( icm_offset, len );
  2073. MLX_FILL_2 ( init_hca, 33,
  2074. qpc_eec_cqc_eqc_rdb_parameters.eqc_base_addr_l,
  2075. ( icm_offset >> 6 ),
  2076. qpc_eec_cqc_eqc_rdb_parameters.log_num_eq,
  2077. log_num_eqs );
  2078. DBGC ( arbel, "Arbel %p ICM EQC is %d x %#zx at [%zx,%zx)\n",
  2079. arbel, ( 1 << log_num_eqs ), arbel->limits.eqc_entry_size,
  2080. icm_offset, ( icm_offset + len ) );
  2081. icm_offset += len;
  2082. /* End-to-end contexts */
  2083. len = ( ( 1 << log_num_ees ) * arbel->limits.eec_entry_size );
  2084. icm_offset = icm_align ( icm_offset, len );
  2085. MLX_FILL_2 ( init_hca, 17,
  2086. qpc_eec_cqc_eqc_rdb_parameters.eec_base_addr_l,
  2087. ( icm_offset >> 7 ),
  2088. qpc_eec_cqc_eqc_rdb_parameters.log_num_of_ee,
  2089. log_num_ees );
  2090. DBGC ( arbel, "Arbel %p ICM EEC is %d x %#zx at [%zx,%zx)\n",
  2091. arbel, ( 1 << log_num_ees ), arbel->limits.eec_entry_size,
  2092. icm_offset, ( icm_offset + len ) );
  2093. icm_offset += len;
  2094. /* Shared receive queue contexts */
  2095. len = ( ( 1 << log_num_srqs ) * arbel->limits.srqc_entry_size );
  2096. icm_offset = icm_align ( icm_offset, len );
  2097. MLX_FILL_2 ( init_hca, 19,
  2098. qpc_eec_cqc_eqc_rdb_parameters.srqc_base_addr_l,
  2099. ( icm_offset >> 5 ),
  2100. qpc_eec_cqc_eqc_rdb_parameters.log_num_of_srq,
  2101. log_num_srqs );
  2102. DBGC ( arbel, "Arbel %p ICM SRQC is %d x %#zx at [%zx,%zx)\n",
  2103. arbel, ( 1 << log_num_srqs ), arbel->limits.srqc_entry_size,
  2104. icm_offset, ( icm_offset + len ) );
  2105. icm_offset += len;
  2106. /* Memory protection table */
  2107. len = ( ( 1 << log_num_mpts ) * arbel->limits.mpt_entry_size );
  2108. icm_offset = icm_align ( icm_offset, len );
  2109. MLX_FILL_1 ( init_hca, 61,
  2110. tpt_parameters.mpt_base_adr_l, icm_offset );
  2111. MLX_FILL_1 ( init_hca, 62,
  2112. tpt_parameters.log_mpt_sz, log_num_mpts );
  2113. DBGC ( arbel, "Arbel %p ICM MPT is %d x %#zx at [%zx,%zx)\n",
  2114. arbel, ( 1 << log_num_mpts ), arbel->limits.mpt_entry_size,
  2115. icm_offset, ( icm_offset + len ) );
  2116. icm_offset += len;
  2117. /* Remote read data base table */
  2118. len = ( ( 1 << log_num_rdbs ) * ARBEL_RDB_ENTRY_SIZE );
  2119. icm_offset = icm_align ( icm_offset, len );
  2120. MLX_FILL_1 ( init_hca, 37,
  2121. qpc_eec_cqc_eqc_rdb_parameters.rdb_base_addr_l,
  2122. icm_offset );
  2123. DBGC ( arbel, "Arbel %p ICM RDB is %d x %#zx at [%zx,%zx)\n",
  2124. arbel, ( 1 << log_num_rdbs ), ARBEL_RDB_ENTRY_SIZE,
  2125. icm_offset, ( icm_offset + len ) );
  2126. icm_offset += len;
  2127. /* Extended end-to-end contexts */
  2128. len = ( ( 1 << log_num_ees ) * arbel->limits.eeec_entry_size );
  2129. icm_offset = icm_align ( icm_offset, len );
  2130. MLX_FILL_1 ( init_hca, 29,
  2131. qpc_eec_cqc_eqc_rdb_parameters.eeec_base_addr_l,
  2132. icm_offset );
  2133. DBGC ( arbel, "Arbel %p ICM EEEC is %d x %#zx at [%zx,%zx)\n",
  2134. arbel, ( 1 << log_num_ees ), arbel->limits.eeec_entry_size,
  2135. icm_offset, ( icm_offset + len ) );
  2136. icm_offset += len;
  2137. /* Multicast table */
  2138. len = ( ( 1 << log_num_mcs ) * sizeof ( struct arbelprm_mgm_entry ) );
  2139. icm_offset = icm_align ( icm_offset, len );
  2140. MLX_FILL_1 ( init_hca, 49,
  2141. multicast_parameters.mc_base_addr_l, icm_offset );
  2142. MLX_FILL_1 ( init_hca, 52,
  2143. multicast_parameters.log_mc_table_entry_sz,
  2144. fls ( sizeof ( struct arbelprm_mgm_entry ) - 1 ) );
  2145. MLX_FILL_1 ( init_hca, 53,
  2146. multicast_parameters.mc_table_hash_sz,
  2147. ( 1 << log_num_mcs ) );
  2148. MLX_FILL_1 ( init_hca, 54,
  2149. multicast_parameters.log_mc_table_sz,
  2150. log_num_mcs /* Only one entry per hash */ );
  2151. DBGC ( arbel, "Arbel %p ICM MC is %d x %#zx at [%zx,%zx)\n", arbel,
  2152. ( 1 << log_num_mcs ), sizeof ( struct arbelprm_mgm_entry ),
  2153. icm_offset, ( icm_offset + len ) );
  2154. icm_offset += len;
  2155. /* Memory translation table */
  2156. len = ( ( 1 << log_num_mtts ) * arbel->limits.mtt_entry_size );
  2157. icm_offset = icm_align ( icm_offset, len );
  2158. MLX_FILL_1 ( init_hca, 65,
  2159. tpt_parameters.mtt_base_addr_l, icm_offset );
  2160. DBGC ( arbel, "Arbel %p ICM MTT is %d x %#zx at [%zx,%zx)\n",
  2161. arbel, ( 1 << log_num_mtts ), arbel->limits.mtt_entry_size,
  2162. icm_offset, ( icm_offset + len ) );
  2163. icm_offset += len;
  2164. /* User access region scratchpads */
  2165. len = ( ( 1 << log_num_uars ) * arbel->limits.uar_scratch_entry_size );
  2166. icm_offset = icm_align ( icm_offset, len );
  2167. MLX_FILL_1 ( init_hca, 77,
  2168. uar_parameters.uar_scratch_base_addr_l, icm_offset );
  2169. DBGC ( arbel, "Arbel %p UAR scratchpad is %d x %#zx at [%zx,%zx)\n",
  2170. arbel, ( 1 << log_num_uars ),
  2171. arbel->limits.uar_scratch_entry_size,
  2172. icm_offset, ( icm_offset + len ) );
  2173. icm_offset += len;
  2174. /* Record amount of ICM to be allocated */
  2175. icm_offset = icm_align ( icm_offset, ARBEL_PAGE_SIZE );
  2176. icm_len = icm_offset;
  2177. /* User access region contexts
  2178. *
  2179. * The reserved UAR(s) do not need to be backed by physical
  2180. * memory, and our UAR is allocated separately; neither are
  2181. * part of the umalloc()ed ICM block, but both contribute to
  2182. * the total length of ICM virtual address space.
  2183. */
  2184. len = ( ( 1 << log_num_uars ) * ARBEL_PAGE_SIZE );
  2185. icm_offset = icm_align ( icm_offset, len );
  2186. MLX_FILL_1 ( init_hca, 74, uar_parameters.log_max_uars, log_num_uars );
  2187. MLX_FILL_1 ( init_hca, 79,
  2188. uar_parameters.uar_context_base_addr_l, icm_offset );
  2189. arbel->db_rec_offset =
  2190. ( icm_offset +
  2191. ( arbel->limits.reserved_uars * ARBEL_PAGE_SIZE ) );
  2192. DBGC ( arbel, "Arbel %p UAR is %d x %#zx at [%zx,%zx), doorbells "
  2193. "[%zx,%zx)\n", arbel, ( 1 << log_num_uars ), ARBEL_PAGE_SIZE,
  2194. icm_offset, ( icm_offset + len ), arbel->db_rec_offset,
  2195. ( arbel->db_rec_offset + ARBEL_PAGE_SIZE ) );
  2196. icm_offset += len;
  2197. /* Get ICM auxiliary area size */
  2198. memset ( &icm_size, 0, sizeof ( icm_size ) );
  2199. MLX_FILL_1 ( &icm_size, 1, value, icm_len );
  2200. if ( ( rc = arbel_cmd_set_icm_size ( arbel, &icm_size,
  2201. &icm_aux_size ) ) != 0 ) {
  2202. DBGC ( arbel, "Arbel %p could not set ICM size: %s\n",
  2203. arbel, strerror ( rc ) );
  2204. goto err_set_icm_size;
  2205. }
  2206. icm_aux_len = ( MLX_GET ( &icm_aux_size, value ) * ARBEL_PAGE_SIZE );
  2207. /* Allocate ICM data and auxiliary area */
  2208. DBGC ( arbel, "Arbel %p requires %zd kB ICM and %zd kB AUX ICM\n",
  2209. arbel, ( icm_len / 1024 ), ( icm_aux_len / 1024 ) );
  2210. if ( ! arbel->icm ) {
  2211. arbel->icm_len = icm_len;
  2212. arbel->icm_aux_len = icm_aux_len;
  2213. arbel->icm = umalloc ( arbel->icm_len + arbel->icm_aux_len );
  2214. if ( ! arbel->icm ) {
  2215. rc = -ENOMEM;
  2216. goto err_alloc_icm;
  2217. }
  2218. } else {
  2219. assert ( arbel->icm_len == icm_len );
  2220. assert ( arbel->icm_aux_len == icm_aux_len );
  2221. }
  2222. icm_phys = user_to_phys ( arbel->icm, 0 );
  2223. /* Allocate doorbell UAR */
  2224. arbel->db_rec = malloc_dma ( ARBEL_PAGE_SIZE, ARBEL_PAGE_SIZE );
  2225. if ( ! arbel->db_rec ) {
  2226. rc = -ENOMEM;
  2227. goto err_alloc_doorbell;
  2228. }
  2229. /* Map ICM auxiliary area */
  2230. DBGC ( arbel, "Arbel %p ICM AUX at [%08lx,%08lx)\n",
  2231. arbel, icm_phys, ( icm_phys + arbel->icm_aux_len ) );
  2232. if ( ( rc = arbel_map_vpm ( arbel, arbel_cmd_map_icm_aux,
  2233. 0, icm_phys, arbel->icm_aux_len ) ) != 0 ){
  2234. DBGC ( arbel, "Arbel %p could not map AUX ICM: %s\n",
  2235. arbel, strerror ( rc ) );
  2236. goto err_map_icm_aux;
  2237. }
  2238. icm_phys += arbel->icm_aux_len;
  2239. /* Map ICM area */
  2240. DBGC ( arbel, "Arbel %p ICM at [%08lx,%08lx)\n",
  2241. arbel, icm_phys, ( icm_phys + arbel->icm_len ) );
  2242. if ( ( rc = arbel_map_vpm ( arbel, arbel_cmd_map_icm,
  2243. 0, icm_phys, arbel->icm_len ) ) != 0 ) {
  2244. DBGC ( arbel, "Arbel %p could not map ICM: %s\n",
  2245. arbel, strerror ( rc ) );
  2246. goto err_map_icm;
  2247. }
  2248. icm_phys += arbel->icm_len;
  2249. /* Map doorbell UAR */
  2250. DBGC ( arbel, "Arbel %p UAR at [%08lx,%08lx)\n",
  2251. arbel, virt_to_phys ( arbel->db_rec ),
  2252. ( virt_to_phys ( arbel->db_rec ) + ARBEL_PAGE_SIZE ) );
  2253. if ( ( rc = arbel_map_vpm ( arbel, arbel_cmd_map_icm,
  2254. arbel->db_rec_offset,
  2255. virt_to_phys ( arbel->db_rec ),
  2256. ARBEL_PAGE_SIZE ) ) != 0 ) {
  2257. DBGC ( arbel, "Arbel %p could not map doorbell UAR: %s\n",
  2258. arbel, strerror ( rc ) );
  2259. goto err_map_doorbell;
  2260. }
  2261. /* Initialise doorbell records */
  2262. memset ( arbel->db_rec, 0, ARBEL_PAGE_SIZE );
  2263. db_rec = &arbel->db_rec[ARBEL_GROUP_SEPARATOR_DOORBELL];
  2264. MLX_FILL_1 ( &db_rec->qp, 1, res, ARBEL_UAR_RES_GROUP_SEP );
  2265. return 0;
  2266. memset ( &unmap_icm, 0, sizeof ( unmap_icm ) );
  2267. MLX_FILL_1 ( &unmap_icm, 1, value, arbel->db_rec_offset );
  2268. arbel_cmd_unmap_icm ( arbel, 1, &unmap_icm );
  2269. err_map_doorbell:
  2270. memset ( &unmap_icm, 0, sizeof ( unmap_icm ) );
  2271. arbel_cmd_unmap_icm ( arbel, ( arbel->icm_len / ARBEL_PAGE_SIZE ),
  2272. &unmap_icm );
  2273. err_map_icm:
  2274. arbel_cmd_unmap_icm_aux ( arbel );
  2275. err_map_icm_aux:
  2276. free_dma ( arbel->db_rec, ARBEL_PAGE_SIZE );
  2277. arbel->db_rec= NULL;
  2278. err_alloc_doorbell:
  2279. err_alloc_icm:
  2280. err_set_icm_size:
  2281. return rc;
  2282. }
  2283. /**
  2284. * Free ICM
  2285. *
  2286. * @v arbel Arbel device
  2287. */
  2288. static void arbel_free_icm ( struct arbel *arbel ) {
  2289. struct arbelprm_scalar_parameter unmap_icm;
  2290. memset ( &unmap_icm, 0, sizeof ( unmap_icm ) );
  2291. MLX_FILL_1 ( &unmap_icm, 1, value, arbel->db_rec_offset );
  2292. arbel_cmd_unmap_icm ( arbel, 1, &unmap_icm );
  2293. memset ( &unmap_icm, 0, sizeof ( unmap_icm ) );
  2294. arbel_cmd_unmap_icm ( arbel, ( arbel->icm_len / ARBEL_PAGE_SIZE ),
  2295. &unmap_icm );
  2296. arbel_cmd_unmap_icm_aux ( arbel );
  2297. free_dma ( arbel->db_rec, ARBEL_PAGE_SIZE );
  2298. arbel->db_rec = NULL;
  2299. }
  2300. /***************************************************************************
  2301. *
  2302. * Initialisation and teardown
  2303. *
  2304. ***************************************************************************
  2305. */
  2306. /**
  2307. * Reset device
  2308. *
  2309. * @v arbel Arbel device
  2310. */
  2311. static void arbel_reset ( struct arbel *arbel ) {
  2312. struct pci_device *pci = arbel->pci;
  2313. struct pci_config_backup backup;
  2314. static const uint8_t backup_exclude[] =
  2315. PCI_CONFIG_BACKUP_EXCLUDE ( 0x58, 0x5c );
  2316. uint16_t vendor;
  2317. unsigned int i;
  2318. /* Perform device reset and preserve PCI configuration */
  2319. pci_backup ( pci, &backup, backup_exclude );
  2320. writel ( ARBEL_RESET_MAGIC,
  2321. ( arbel->config + ARBEL_RESET_OFFSET ) );
  2322. for ( i = 0 ; i < ARBEL_RESET_WAIT_TIME_MS ; i++ ) {
  2323. mdelay ( 1 );
  2324. pci_read_config_word ( pci, PCI_VENDOR_ID, &vendor );
  2325. if ( vendor != 0xffff )
  2326. break;
  2327. }
  2328. pci_restore ( pci, &backup, backup_exclude );
  2329. }
  2330. /**
  2331. * Set up memory protection table
  2332. *
  2333. * @v arbel Arbel device
  2334. * @ret rc Return status code
  2335. */
  2336. static int arbel_setup_mpt ( struct arbel *arbel ) {
  2337. struct arbelprm_mpt mpt;
  2338. uint32_t key;
  2339. int rc;
  2340. /* Derive key */
  2341. key = ( arbel->limits.reserved_mrws | ARBEL_MKEY_PREFIX );
  2342. arbel->lkey = ( ( key << 8 ) | ( key >> 24 ) );
  2343. /* Initialise memory protection table */
  2344. memset ( &mpt, 0, sizeof ( mpt ) );
  2345. MLX_FILL_7 ( &mpt, 0,
  2346. a, 1,
  2347. rw, 1,
  2348. rr, 1,
  2349. lw, 1,
  2350. lr, 1,
  2351. pa, 1,
  2352. r_w, 1 );
  2353. MLX_FILL_1 ( &mpt, 2, mem_key, key );
  2354. MLX_FILL_2 ( &mpt, 3,
  2355. pd, ARBEL_GLOBAL_PD,
  2356. rae, 1 );
  2357. MLX_FILL_1 ( &mpt, 6, reg_wnd_len_h, 0xffffffffUL );
  2358. MLX_FILL_1 ( &mpt, 7, reg_wnd_len_l, 0xffffffffUL );
  2359. if ( ( rc = arbel_cmd_sw2hw_mpt ( arbel, arbel->limits.reserved_mrws,
  2360. &mpt ) ) != 0 ) {
  2361. DBGC ( arbel, "Arbel %p could not set up MPT: %s\n",
  2362. arbel, strerror ( rc ) );
  2363. return rc;
  2364. }
  2365. return 0;
  2366. }
  2367. /**
  2368. * Configure special queue pairs
  2369. *
  2370. * @v arbel Arbel device
  2371. * @ret rc Return status code
  2372. */
  2373. static int arbel_configure_special_qps ( struct arbel *arbel ) {
  2374. unsigned int smi_qpn_base;
  2375. unsigned int gsi_qpn_base;
  2376. int rc;
  2377. /* Special QP block must be aligned on an even number */
  2378. arbel->special_qpn_base = ( ( arbel->limits.reserved_qps + 1 ) & ~1 );
  2379. arbel->qpn_base = ( arbel->special_qpn_base +
  2380. ARBEL_NUM_SPECIAL_QPS );
  2381. DBGC ( arbel, "Arbel %p special QPs at [%lx,%lx]\n", arbel,
  2382. arbel->special_qpn_base, ( arbel->qpn_base - 1 ) );
  2383. smi_qpn_base = arbel->special_qpn_base;
  2384. gsi_qpn_base = ( smi_qpn_base + 2 );
  2385. /* Issue commands to configure special QPs */
  2386. if ( ( rc = arbel_cmd_conf_special_qp ( arbel, 0,
  2387. smi_qpn_base ) ) != 0 ) {
  2388. DBGC ( arbel, "Arbel %p could not configure SMI QPs: %s\n",
  2389. arbel, strerror ( rc ) );
  2390. return rc;
  2391. }
  2392. if ( ( rc = arbel_cmd_conf_special_qp ( arbel, 1,
  2393. gsi_qpn_base ) ) != 0 ) {
  2394. DBGC ( arbel, "Arbel %p could not configure GSI QPs: %s\n",
  2395. arbel, strerror ( rc ) );
  2396. return rc;
  2397. }
  2398. return 0;
  2399. }
  2400. /**
  2401. * Start Arbel device
  2402. *
  2403. * @v arbel Arbel device
  2404. * @v running Firmware is already running
  2405. * @ret rc Return status code
  2406. */
  2407. static int arbel_start ( struct arbel *arbel, int running ) {
  2408. struct arbelprm_init_hca init_hca;
  2409. unsigned int i;
  2410. int rc;
  2411. /* Start firmware if not already running */
  2412. if ( ! running ) {
  2413. if ( ( rc = arbel_start_firmware ( arbel ) ) != 0 )
  2414. goto err_start_firmware;
  2415. }
  2416. /* Allocate ICM */
  2417. memset ( &init_hca, 0, sizeof ( init_hca ) );
  2418. if ( ( rc = arbel_alloc_icm ( arbel, &init_hca ) ) != 0 )
  2419. goto err_alloc_icm;
  2420. /* Initialise HCA */
  2421. if ( ( rc = arbel_cmd_init_hca ( arbel, &init_hca ) ) != 0 ) {
  2422. DBGC ( arbel, "Arbel %p could not initialise HCA: %s\n",
  2423. arbel, strerror ( rc ) );
  2424. goto err_init_hca;
  2425. }
  2426. /* Set up memory protection */
  2427. if ( ( rc = arbel_setup_mpt ( arbel ) ) != 0 )
  2428. goto err_setup_mpt;
  2429. for ( i = 0 ; i < ARBEL_NUM_PORTS ; i++ )
  2430. arbel->ibdev[i]->rdma_key = arbel->lkey;
  2431. /* Set up event queue */
  2432. if ( ( rc = arbel_create_eq ( arbel ) ) != 0 )
  2433. goto err_create_eq;
  2434. /* Configure special QPs */
  2435. if ( ( rc = arbel_configure_special_qps ( arbel ) ) != 0 )
  2436. goto err_conf_special_qps;
  2437. return 0;
  2438. err_conf_special_qps:
  2439. arbel_destroy_eq ( arbel );
  2440. err_create_eq:
  2441. err_setup_mpt:
  2442. arbel_cmd_close_hca ( arbel );
  2443. err_init_hca:
  2444. arbel_free_icm ( arbel );
  2445. err_alloc_icm:
  2446. arbel_stop_firmware ( arbel );
  2447. err_start_firmware:
  2448. return rc;
  2449. }
  2450. /**
  2451. * Stop Arbel device
  2452. *
  2453. * @v arbel Arbel device
  2454. */
  2455. static void arbel_stop ( struct arbel *arbel ) {
  2456. arbel_destroy_eq ( arbel );
  2457. arbel_cmd_close_hca ( arbel );
  2458. arbel_free_icm ( arbel );
  2459. arbel_stop_firmware ( arbel );
  2460. arbel_reset ( arbel );
  2461. }
  2462. /**
  2463. * Open Arbel device
  2464. *
  2465. * @v arbel Arbel device
  2466. * @ret rc Return status code
  2467. */
  2468. static int arbel_open ( struct arbel *arbel ) {
  2469. int rc;
  2470. /* Start device if applicable */
  2471. if ( arbel->open_count == 0 ) {
  2472. if ( ( rc = arbel_start ( arbel, 0 ) ) != 0 )
  2473. return rc;
  2474. }
  2475. /* Increment open counter */
  2476. arbel->open_count++;
  2477. return 0;
  2478. }
  2479. /**
  2480. * Close Arbel device
  2481. *
  2482. * @v arbel Arbel device
  2483. */
  2484. static void arbel_close ( struct arbel *arbel ) {
  2485. /* Decrement open counter */
  2486. assert ( arbel->open_count != 0 );
  2487. arbel->open_count--;
  2488. /* Stop device if applicable */
  2489. if ( arbel->open_count == 0 )
  2490. arbel_stop ( arbel );
  2491. }
  2492. /***************************************************************************
  2493. *
  2494. * Infiniband link-layer operations
  2495. *
  2496. ***************************************************************************
  2497. */
  2498. /**
  2499. * Initialise Infiniband link
  2500. *
  2501. * @v ibdev Infiniband device
  2502. * @ret rc Return status code
  2503. */
  2504. static int arbel_ib_open ( struct ib_device *ibdev ) {
  2505. struct arbel *arbel = ib_get_drvdata ( ibdev );
  2506. struct arbelprm_init_ib init_ib;
  2507. int rc;
  2508. /* Open hardware */
  2509. if ( ( rc = arbel_open ( arbel ) ) != 0 )
  2510. goto err_open;
  2511. /* Initialise IB */
  2512. memset ( &init_ib, 0, sizeof ( init_ib ) );
  2513. MLX_FILL_3 ( &init_ib, 0,
  2514. mtu_cap, ARBEL_MTU_2048,
  2515. port_width_cap, 3,
  2516. vl_cap, 1 );
  2517. MLX_FILL_1 ( &init_ib, 1, max_gid, 1 );
  2518. MLX_FILL_1 ( &init_ib, 2, max_pkey, 64 );
  2519. if ( ( rc = arbel_cmd_init_ib ( arbel, ibdev->port,
  2520. &init_ib ) ) != 0 ) {
  2521. DBGC ( arbel, "Arbel %p port %d could not intialise IB: %s\n",
  2522. arbel, ibdev->port, strerror ( rc ) );
  2523. goto err_init_ib;
  2524. }
  2525. /* Update MAD parameters */
  2526. ib_smc_update ( ibdev, arbel_mad );
  2527. return 0;
  2528. err_init_ib:
  2529. arbel_close ( arbel );
  2530. err_open:
  2531. return rc;
  2532. }
  2533. /**
  2534. * Close Infiniband link
  2535. *
  2536. * @v ibdev Infiniband device
  2537. */
  2538. static void arbel_ib_close ( struct ib_device *ibdev ) {
  2539. struct arbel *arbel = ib_get_drvdata ( ibdev );
  2540. int rc;
  2541. /* Close IB */
  2542. if ( ( rc = arbel_cmd_close_ib ( arbel, ibdev->port ) ) != 0 ) {
  2543. DBGC ( arbel, "Arbel %p port %d could not close IB: %s\n",
  2544. arbel, ibdev->port, strerror ( rc ) );
  2545. /* Nothing we can do about this */
  2546. }
  2547. /* Close hardware */
  2548. arbel_close ( arbel );
  2549. }
  2550. /**
  2551. * Inform embedded subnet management agent of a received MAD
  2552. *
  2553. * @v ibdev Infiniband device
  2554. * @v mad MAD
  2555. * @ret rc Return status code
  2556. */
  2557. static int arbel_inform_sma ( struct ib_device *ibdev, union ib_mad *mad ) {
  2558. int rc;
  2559. /* Send the MAD to the embedded SMA */
  2560. if ( ( rc = arbel_mad ( ibdev, mad ) ) != 0 )
  2561. return rc;
  2562. /* Update parameters held in software */
  2563. ib_smc_update ( ibdev, arbel_mad );
  2564. return 0;
  2565. }
  2566. /***************************************************************************
  2567. *
  2568. * Multicast group operations
  2569. *
  2570. ***************************************************************************
  2571. */
  2572. /**
  2573. * Attach to multicast group
  2574. *
  2575. * @v ibdev Infiniband device
  2576. * @v qp Queue pair
  2577. * @v gid Multicast GID
  2578. * @ret rc Return status code
  2579. */
  2580. static int arbel_mcast_attach ( struct ib_device *ibdev,
  2581. struct ib_queue_pair *qp,
  2582. union ib_gid *gid ) {
  2583. struct arbel *arbel = ib_get_drvdata ( ibdev );
  2584. struct arbelprm_mgm_hash hash;
  2585. struct arbelprm_mgm_entry mgm;
  2586. unsigned int index;
  2587. int rc;
  2588. /* Generate hash table index */
  2589. if ( ( rc = arbel_cmd_mgid_hash ( arbel, gid, &hash ) ) != 0 ) {
  2590. DBGC ( arbel, "Arbel %p could not hash GID: %s\n",
  2591. arbel, strerror ( rc ) );
  2592. return rc;
  2593. }
  2594. index = MLX_GET ( &hash, hash );
  2595. /* Check for existing hash table entry */
  2596. if ( ( rc = arbel_cmd_read_mgm ( arbel, index, &mgm ) ) != 0 ) {
  2597. DBGC ( arbel, "Arbel %p could not read MGM %#x: %s\n",
  2598. arbel, index, strerror ( rc ) );
  2599. return rc;
  2600. }
  2601. if ( MLX_GET ( &mgm, mgmqp_0.qi ) != 0 ) {
  2602. /* FIXME: this implementation allows only a single QP
  2603. * per multicast group, and doesn't handle hash
  2604. * collisions. Sufficient for IPoIB but may need to
  2605. * be extended in future.
  2606. */
  2607. DBGC ( arbel, "Arbel %p MGID index %#x already in use\n",
  2608. arbel, index );
  2609. return -EBUSY;
  2610. }
  2611. /* Update hash table entry */
  2612. MLX_FILL_2 ( &mgm, 8,
  2613. mgmqp_0.qpn_i, qp->qpn,
  2614. mgmqp_0.qi, 1 );
  2615. memcpy ( &mgm.u.dwords[4], gid, sizeof ( *gid ) );
  2616. if ( ( rc = arbel_cmd_write_mgm ( arbel, index, &mgm ) ) != 0 ) {
  2617. DBGC ( arbel, "Arbel %p could not write MGM %#x: %s\n",
  2618. arbel, index, strerror ( rc ) );
  2619. return rc;
  2620. }
  2621. return 0;
  2622. }
  2623. /**
  2624. * Detach from multicast group
  2625. *
  2626. * @v ibdev Infiniband device
  2627. * @v qp Queue pair
  2628. * @v gid Multicast GID
  2629. */
  2630. static void arbel_mcast_detach ( struct ib_device *ibdev,
  2631. struct ib_queue_pair *qp __unused,
  2632. union ib_gid *gid ) {
  2633. struct arbel *arbel = ib_get_drvdata ( ibdev );
  2634. struct arbelprm_mgm_hash hash;
  2635. struct arbelprm_mgm_entry mgm;
  2636. unsigned int index;
  2637. int rc;
  2638. /* Generate hash table index */
  2639. if ( ( rc = arbel_cmd_mgid_hash ( arbel, gid, &hash ) ) != 0 ) {
  2640. DBGC ( arbel, "Arbel %p could not hash GID: %s\n",
  2641. arbel, strerror ( rc ) );
  2642. return;
  2643. }
  2644. index = MLX_GET ( &hash, hash );
  2645. /* Clear hash table entry */
  2646. memset ( &mgm, 0, sizeof ( mgm ) );
  2647. if ( ( rc = arbel_cmd_write_mgm ( arbel, index, &mgm ) ) != 0 ) {
  2648. DBGC ( arbel, "Arbel %p could not write MGM %#x: %s\n",
  2649. arbel, index, strerror ( rc ) );
  2650. return;
  2651. }
  2652. }
  2653. /** Arbel Infiniband operations */
  2654. static struct ib_device_operations arbel_ib_operations = {
  2655. .create_cq = arbel_create_cq,
  2656. .destroy_cq = arbel_destroy_cq,
  2657. .create_qp = arbel_create_qp,
  2658. .modify_qp = arbel_modify_qp,
  2659. .destroy_qp = arbel_destroy_qp,
  2660. .post_send = arbel_post_send,
  2661. .post_recv = arbel_post_recv,
  2662. .poll_cq = arbel_poll_cq,
  2663. .poll_eq = arbel_poll_eq,
  2664. .open = arbel_ib_open,
  2665. .close = arbel_ib_close,
  2666. .mcast_attach = arbel_mcast_attach,
  2667. .mcast_detach = arbel_mcast_detach,
  2668. .set_port_info = arbel_inform_sma,
  2669. .set_pkey_table = arbel_inform_sma,
  2670. };
  2671. /***************************************************************************
  2672. *
  2673. * PCI interface
  2674. *
  2675. ***************************************************************************
  2676. */
  2677. /**
  2678. * Allocate Arbel device
  2679. *
  2680. * @ret arbel Arbel device
  2681. */
  2682. static struct arbel * arbel_alloc ( void ) {
  2683. struct arbel *arbel;
  2684. /* Allocate Arbel device */
  2685. arbel = zalloc ( sizeof ( *arbel ) );
  2686. if ( ! arbel )
  2687. goto err_arbel;
  2688. /* Allocate space for mailboxes */
  2689. arbel->mailbox_in = malloc_dma ( ARBEL_MBOX_SIZE, ARBEL_MBOX_ALIGN );
  2690. if ( ! arbel->mailbox_in )
  2691. goto err_mailbox_in;
  2692. arbel->mailbox_out = malloc_dma ( ARBEL_MBOX_SIZE, ARBEL_MBOX_ALIGN );
  2693. if ( ! arbel->mailbox_out )
  2694. goto err_mailbox_out;
  2695. return arbel;
  2696. free_dma ( arbel->mailbox_out, ARBEL_MBOX_SIZE );
  2697. err_mailbox_out:
  2698. free_dma ( arbel->mailbox_in, ARBEL_MBOX_SIZE );
  2699. err_mailbox_in:
  2700. free ( arbel );
  2701. err_arbel:
  2702. return NULL;
  2703. }
  2704. /**
  2705. * Free Arbel device
  2706. *
  2707. * @v arbel Arbel device
  2708. */
  2709. static void arbel_free ( struct arbel *arbel ) {
  2710. ufree ( arbel->icm );
  2711. ufree ( arbel->firmware_area );
  2712. free_dma ( arbel->mailbox_out, ARBEL_MBOX_SIZE );
  2713. free_dma ( arbel->mailbox_in, ARBEL_MBOX_SIZE );
  2714. free ( arbel );
  2715. }
  2716. /**
  2717. * Probe PCI device
  2718. *
  2719. * @v pci PCI device
  2720. * @v id PCI ID
  2721. * @ret rc Return status code
  2722. */
  2723. static int arbel_probe ( struct pci_device *pci ) {
  2724. struct arbel *arbel;
  2725. struct ib_device *ibdev;
  2726. int i;
  2727. int rc;
  2728. /* Allocate Arbel device */
  2729. arbel = arbel_alloc();
  2730. if ( ! arbel ) {
  2731. rc = -ENOMEM;
  2732. goto err_alloc;
  2733. }
  2734. pci_set_drvdata ( pci, arbel );
  2735. arbel->pci = pci;
  2736. /* Fix up PCI device */
  2737. adjust_pci_device ( pci );
  2738. /* Map PCI BARs */
  2739. arbel->config = ioremap ( pci_bar_start ( pci, ARBEL_PCI_CONFIG_BAR ),
  2740. ARBEL_PCI_CONFIG_BAR_SIZE );
  2741. arbel->uar = ioremap ( ( pci_bar_start ( pci, ARBEL_PCI_UAR_BAR ) +
  2742. ARBEL_PCI_UAR_IDX * ARBEL_PCI_UAR_SIZE ),
  2743. ARBEL_PCI_UAR_SIZE );
  2744. /* Allocate Infiniband devices */
  2745. for ( i = 0 ; i < ARBEL_NUM_PORTS ; i++ ) {
  2746. ibdev = alloc_ibdev ( 0 );
  2747. if ( ! ibdev ) {
  2748. rc = -ENOMEM;
  2749. goto err_alloc_ibdev;
  2750. }
  2751. arbel->ibdev[i] = ibdev;
  2752. ibdev->op = &arbel_ib_operations;
  2753. ibdev->dev = &pci->dev;
  2754. ibdev->port = ( ARBEL_PORT_BASE + i );
  2755. ib_set_drvdata ( ibdev, arbel );
  2756. }
  2757. /* Reset device */
  2758. arbel_reset ( arbel );
  2759. /* Start firmware */
  2760. if ( ( rc = arbel_start_firmware ( arbel ) ) != 0 )
  2761. goto err_start_firmware;
  2762. /* Get device limits */
  2763. if ( ( rc = arbel_get_limits ( arbel ) ) != 0 )
  2764. goto err_get_limits;
  2765. /* Start device */
  2766. if ( ( rc = arbel_start ( arbel, 1 ) ) != 0 )
  2767. goto err_start;
  2768. /* Initialise parameters using SMC */
  2769. for ( i = 0 ; i < ARBEL_NUM_PORTS ; i++ )
  2770. ib_smc_init ( arbel->ibdev[i], arbel_mad );
  2771. /* Register Infiniband devices */
  2772. for ( i = 0 ; i < ARBEL_NUM_PORTS ; i++ ) {
  2773. if ( ( rc = register_ibdev ( arbel->ibdev[i] ) ) != 0 ) {
  2774. DBGC ( arbel, "Arbel %p port %d could not register IB "
  2775. "device: %s\n", arbel,
  2776. arbel->ibdev[i]->port, strerror ( rc ) );
  2777. goto err_register_ibdev;
  2778. }
  2779. }
  2780. /* Leave device quiescent until opened */
  2781. if ( arbel->open_count == 0 )
  2782. arbel_stop ( arbel );
  2783. return 0;
  2784. i = ARBEL_NUM_PORTS;
  2785. err_register_ibdev:
  2786. for ( i-- ; i >= 0 ; i-- )
  2787. unregister_ibdev ( arbel->ibdev[i] );
  2788. arbel_stop ( arbel );
  2789. err_start:
  2790. err_get_limits:
  2791. arbel_stop_firmware ( arbel );
  2792. err_start_firmware:
  2793. i = ARBEL_NUM_PORTS;
  2794. err_alloc_ibdev:
  2795. for ( i-- ; i >= 0 ; i-- )
  2796. ibdev_put ( arbel->ibdev[i] );
  2797. iounmap ( arbel->uar );
  2798. iounmap ( arbel->config );
  2799. arbel_free ( arbel );
  2800. err_alloc:
  2801. return rc;
  2802. }
  2803. /**
  2804. * Remove PCI device
  2805. *
  2806. * @v pci PCI device
  2807. */
  2808. static void arbel_remove ( struct pci_device *pci ) {
  2809. struct arbel *arbel = pci_get_drvdata ( pci );
  2810. int i;
  2811. for ( i = ( ARBEL_NUM_PORTS - 1 ) ; i >= 0 ; i-- )
  2812. unregister_ibdev ( arbel->ibdev[i] );
  2813. for ( i = ( ARBEL_NUM_PORTS - 1 ) ; i >= 0 ; i-- )
  2814. ibdev_put ( arbel->ibdev[i] );
  2815. iounmap ( arbel->uar );
  2816. iounmap ( arbel->config );
  2817. arbel_free ( arbel );
  2818. }
  2819. static struct pci_device_id arbel_nics[] = {
  2820. PCI_ROM ( 0x15b3, 0x6282, "mt25218", "MT25218 HCA driver", 0 ),
  2821. PCI_ROM ( 0x15b3, 0x6274, "mt25204", "MT25204 HCA driver", 0 ),
  2822. };
  2823. struct pci_driver arbel_driver __pci_driver = {
  2824. .ids = arbel_nics,
  2825. .id_count = ( sizeof ( arbel_nics ) / sizeof ( arbel_nics[0] ) ),
  2826. .probe = arbel_probe,
  2827. .remove = arbel_remove,
  2828. };