arbel.c 63KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <stdint.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <strings.h>
  26. #include <unistd.h>
  27. #include <errno.h>
  28. #include <timer.h>
  29. #include <byteswap.h>
  30. #include <gpxe/pci.h>
  31. #include <gpxe/malloc.h>
  32. #include <gpxe/umalloc.h>
  33. #include <gpxe/iobuf.h>
  34. #include <gpxe/netdevice.h>
  35. #include <gpxe/infiniband.h>
  36. #include <gpxe/ipoib.h>
  37. #include "arbel.h"
  38. /**
  39. * @file
  40. *
  41. * Mellanox Arbel Infiniband HCA
  42. *
  43. */
  44. /* Port to use */
  45. #define PXE_IB_PORT 1
  46. /***************************************************************************
  47. *
  48. * Queue number allocation
  49. *
  50. ***************************************************************************
  51. */
  52. /**
  53. * Allocate queue number
  54. *
  55. * @v q_inuse Queue usage bitmask
  56. * @v max_inuse Maximum number of in-use queues
  57. * @ret qn_offset Free queue number offset, or negative error
  58. */
  59. static int arbel_alloc_qn_offset ( arbel_bitmask_t *q_inuse,
  60. unsigned int max_inuse ) {
  61. unsigned int qn_offset = 0;
  62. arbel_bitmask_t mask = 1;
  63. while ( qn_offset < max_inuse ) {
  64. if ( ( mask & *q_inuse ) == 0 ) {
  65. *q_inuse |= mask;
  66. return qn_offset;
  67. }
  68. qn_offset++;
  69. mask <<= 1;
  70. if ( ! mask ) {
  71. mask = 1;
  72. q_inuse++;
  73. }
  74. }
  75. return -ENFILE;
  76. }
  77. /**
  78. * Free queue number
  79. *
  80. * @v q_inuse Queue usage bitmask
  81. * @v qn_offset Queue number offset
  82. */
  83. static void arbel_free_qn_offset ( arbel_bitmask_t *q_inuse, int qn_offset ) {
  84. arbel_bitmask_t mask;
  85. mask = ( 1 << ( qn_offset % ( 8 * sizeof ( mask ) ) ) );
  86. q_inuse += ( qn_offset / ( 8 * sizeof ( mask ) ) );
  87. *q_inuse &= ~mask;
  88. }
  89. /***************************************************************************
  90. *
  91. * HCA commands
  92. *
  93. ***************************************************************************
  94. */
  95. /**
  96. * Wait for Arbel command completion
  97. *
  98. * @v arbel Arbel device
  99. * @ret rc Return status code
  100. */
  101. static int arbel_cmd_wait ( struct arbel *arbel,
  102. struct arbelprm_hca_command_register *hcr ) {
  103. unsigned int wait;
  104. for ( wait = ARBEL_HCR_MAX_WAIT_MS ; wait ; wait-- ) {
  105. hcr->u.dwords[6] =
  106. readl ( arbel->config + ARBEL_HCR_REG ( 6 ) );
  107. if ( MLX_GET ( hcr, go ) == 0 )
  108. return 0;
  109. mdelay ( 1 );
  110. }
  111. return -EBUSY;
  112. }
  113. /**
  114. * Issue HCA command
  115. *
  116. * @v arbel Arbel device
  117. * @v command Command opcode, flags and input/output lengths
  118. * @v op_mod Opcode modifier (0 if no modifier applicable)
  119. * @v in Input parameters
  120. * @v in_mod Input modifier (0 if no modifier applicable)
  121. * @v out Output parameters
  122. * @ret rc Return status code
  123. */
  124. static int arbel_cmd ( struct arbel *arbel, unsigned long command,
  125. unsigned int op_mod, const void *in,
  126. unsigned int in_mod, void *out ) {
  127. struct arbelprm_hca_command_register hcr;
  128. unsigned int opcode = ARBEL_HCR_OPCODE ( command );
  129. size_t in_len = ARBEL_HCR_IN_LEN ( command );
  130. size_t out_len = ARBEL_HCR_OUT_LEN ( command );
  131. void *in_buffer;
  132. void *out_buffer;
  133. unsigned int status;
  134. unsigned int i;
  135. int rc;
  136. assert ( in_len <= ARBEL_MBOX_SIZE );
  137. assert ( out_len <= ARBEL_MBOX_SIZE );
  138. DBGC2 ( arbel, "Arbel %p command %02x in %zx%s out %zx%s\n",
  139. arbel, opcode, in_len,
  140. ( ( command & ARBEL_HCR_IN_MBOX ) ? "(mbox)" : "" ), out_len,
  141. ( ( command & ARBEL_HCR_OUT_MBOX ) ? "(mbox)" : "" ) );
  142. /* Check that HCR is free */
  143. if ( ( rc = arbel_cmd_wait ( arbel, &hcr ) ) != 0 ) {
  144. DBGC ( arbel, "Arbel %p command interface locked\n", arbel );
  145. return rc;
  146. }
  147. /* Prepare HCR */
  148. memset ( &hcr, 0, sizeof ( hcr ) );
  149. in_buffer = &hcr.u.dwords[0];
  150. if ( in_len && ( command & ARBEL_HCR_IN_MBOX ) ) {
  151. in_buffer = arbel->mailbox_in;
  152. MLX_FILL_1 ( &hcr, 1, in_param_l, virt_to_bus ( in_buffer ) );
  153. }
  154. memcpy ( in_buffer, in, in_len );
  155. MLX_FILL_1 ( &hcr, 2, input_modifier, in_mod );
  156. out_buffer = &hcr.u.dwords[3];
  157. if ( out_len && ( command & ARBEL_HCR_OUT_MBOX ) ) {
  158. out_buffer = arbel->mailbox_out;
  159. MLX_FILL_1 ( &hcr, 4, out_param_l,
  160. virt_to_bus ( out_buffer ) );
  161. }
  162. MLX_FILL_3 ( &hcr, 6,
  163. opcode, opcode,
  164. opcode_modifier, op_mod,
  165. go, 1 );
  166. DBGC2_HD ( arbel, &hcr, sizeof ( hcr ) );
  167. if ( in_len ) {
  168. DBGC2 ( arbel, "Input:\n" );
  169. DBGC2_HD ( arbel, in, ( ( in_len < 512 ) ? in_len : 512 ) );
  170. }
  171. /* Issue command */
  172. for ( i = 0 ; i < ( sizeof ( hcr ) / sizeof ( hcr.u.dwords[0] ) ) ;
  173. i++ ) {
  174. writel ( hcr.u.dwords[i],
  175. arbel->config + ARBEL_HCR_REG ( i ) );
  176. barrier();
  177. }
  178. /* Wait for command completion */
  179. if ( ( rc = arbel_cmd_wait ( arbel, &hcr ) ) != 0 ) {
  180. DBGC ( arbel, "Arbel %p timed out waiting for command:\n",
  181. arbel );
  182. DBGC_HD ( arbel, &hcr, sizeof ( hcr ) );
  183. return rc;
  184. }
  185. /* Check command status */
  186. status = MLX_GET ( &hcr, status );
  187. if ( status != 0 ) {
  188. DBGC ( arbel, "Arbel %p command failed with status %02x:\n",
  189. arbel, status );
  190. DBGC_HD ( arbel, &hcr, sizeof ( hcr ) );
  191. return -EIO;
  192. }
  193. /* Read output parameters, if any */
  194. hcr.u.dwords[3] = readl ( arbel->config + ARBEL_HCR_REG ( 3 ) );
  195. hcr.u.dwords[4] = readl ( arbel->config + ARBEL_HCR_REG ( 4 ) );
  196. memcpy ( out, out_buffer, out_len );
  197. if ( out_len ) {
  198. DBGC2 ( arbel, "Output:\n" );
  199. DBGC2_HD ( arbel, out, ( ( out_len < 512 ) ? out_len : 512 ) );
  200. }
  201. return 0;
  202. }
  203. static inline int
  204. arbel_cmd_query_dev_lim ( struct arbel *arbel,
  205. struct arbelprm_query_dev_lim *dev_lim ) {
  206. return arbel_cmd ( arbel,
  207. ARBEL_HCR_OUT_CMD ( ARBEL_HCR_QUERY_DEV_LIM,
  208. 1, sizeof ( *dev_lim ) ),
  209. 0, NULL, 0, dev_lim );
  210. }
  211. static inline int
  212. arbel_cmd_query_fw ( struct arbel *arbel, struct arbelprm_query_fw *fw ) {
  213. return arbel_cmd ( arbel,
  214. ARBEL_HCR_OUT_CMD ( ARBEL_HCR_QUERY_FW,
  215. 1, sizeof ( *fw ) ),
  216. 0, NULL, 0, fw );
  217. }
  218. static inline int
  219. arbel_cmd_init_hca ( struct arbel *arbel,
  220. const struct arbelprm_init_hca *init_hca ) {
  221. return arbel_cmd ( arbel,
  222. ARBEL_HCR_IN_CMD ( ARBEL_HCR_INIT_HCA,
  223. 1, sizeof ( *init_hca ) ),
  224. 0, init_hca, 0, NULL );
  225. }
  226. static inline int
  227. arbel_cmd_close_hca ( struct arbel *arbel ) {
  228. return arbel_cmd ( arbel,
  229. ARBEL_HCR_VOID_CMD ( ARBEL_HCR_CLOSE_HCA ),
  230. 0, NULL, 0, NULL );
  231. }
  232. static inline int
  233. arbel_cmd_init_ib ( struct arbel *arbel, unsigned int port,
  234. const struct arbelprm_init_ib *init_ib ) {
  235. return arbel_cmd ( arbel,
  236. ARBEL_HCR_IN_CMD ( ARBEL_HCR_INIT_IB,
  237. 1, sizeof ( *init_ib ) ),
  238. 0, init_ib, port, NULL );
  239. }
  240. static inline int
  241. arbel_cmd_close_ib ( struct arbel *arbel, unsigned int port ) {
  242. return arbel_cmd ( arbel,
  243. ARBEL_HCR_VOID_CMD ( ARBEL_HCR_CLOSE_IB ),
  244. 0, NULL, port, NULL );
  245. }
  246. static inline int
  247. arbel_cmd_sw2hw_mpt ( struct arbel *arbel, unsigned int index,
  248. const struct arbelprm_mpt *mpt ) {
  249. return arbel_cmd ( arbel,
  250. ARBEL_HCR_IN_CMD ( ARBEL_HCR_SW2HW_MPT,
  251. 1, sizeof ( *mpt ) ),
  252. 0, mpt, index, NULL );
  253. }
  254. static inline int
  255. arbel_cmd_sw2hw_eq ( struct arbel *arbel, unsigned int index,
  256. const struct arbelprm_eqc *eqc ) {
  257. return arbel_cmd ( arbel,
  258. ARBEL_HCR_IN_CMD ( ARBEL_HCR_SW2HW_EQ,
  259. 1, sizeof ( *eqc ) ),
  260. 0, eqc, index, NULL );
  261. }
  262. static inline int
  263. arbel_cmd_hw2sw_eq ( struct arbel *arbel, unsigned int index ) {
  264. return arbel_cmd ( arbel,
  265. ARBEL_HCR_VOID_CMD ( ARBEL_HCR_HW2SW_EQ ),
  266. 1, NULL, index, NULL );
  267. }
  268. static inline int
  269. arbel_cmd_sw2hw_cq ( struct arbel *arbel, unsigned long cqn,
  270. const struct arbelprm_completion_queue_context *cqctx ) {
  271. return arbel_cmd ( arbel,
  272. ARBEL_HCR_IN_CMD ( ARBEL_HCR_SW2HW_CQ,
  273. 1, sizeof ( *cqctx ) ),
  274. 0, cqctx, cqn, NULL );
  275. }
  276. static inline int
  277. arbel_cmd_hw2sw_cq ( struct arbel *arbel, unsigned long cqn,
  278. struct arbelprm_completion_queue_context *cqctx) {
  279. return arbel_cmd ( arbel,
  280. ARBEL_HCR_OUT_CMD ( ARBEL_HCR_HW2SW_CQ,
  281. 1, sizeof ( *cqctx ) ),
  282. 0, NULL, cqn, cqctx );
  283. }
  284. static inline int
  285. arbel_cmd_rst2init_qpee ( struct arbel *arbel, unsigned long qpn,
  286. const struct arbelprm_qp_ee_state_transitions *ctx ){
  287. return arbel_cmd ( arbel,
  288. ARBEL_HCR_IN_CMD ( ARBEL_HCR_RST2INIT_QPEE,
  289. 1, sizeof ( *ctx ) ),
  290. 0, ctx, qpn, NULL );
  291. }
  292. static inline int
  293. arbel_cmd_init2rtr_qpee ( struct arbel *arbel, unsigned long qpn,
  294. const struct arbelprm_qp_ee_state_transitions *ctx ){
  295. return arbel_cmd ( arbel,
  296. ARBEL_HCR_IN_CMD ( ARBEL_HCR_INIT2RTR_QPEE,
  297. 1, sizeof ( *ctx ) ),
  298. 0, ctx, qpn, NULL );
  299. }
  300. static inline int
  301. arbel_cmd_rtr2rts_qpee ( struct arbel *arbel, unsigned long qpn,
  302. const struct arbelprm_qp_ee_state_transitions *ctx ) {
  303. return arbel_cmd ( arbel,
  304. ARBEL_HCR_IN_CMD ( ARBEL_HCR_RTR2RTS_QPEE,
  305. 1, sizeof ( *ctx ) ),
  306. 0, ctx, qpn, NULL );
  307. }
  308. static inline int
  309. arbel_cmd_2rst_qpee ( struct arbel *arbel, unsigned long qpn ) {
  310. return arbel_cmd ( arbel,
  311. ARBEL_HCR_VOID_CMD ( ARBEL_HCR_2RST_QPEE ),
  312. 0x03, NULL, qpn, NULL );
  313. }
  314. static inline int
  315. arbel_cmd_mad_ifc ( struct arbel *arbel, union arbelprm_mad *mad ) {
  316. return arbel_cmd ( arbel,
  317. ARBEL_HCR_INOUT_CMD ( ARBEL_HCR_MAD_IFC,
  318. 1, sizeof ( *mad ),
  319. 1, sizeof ( *mad ) ),
  320. 0x03, mad, PXE_IB_PORT, mad );
  321. }
  322. static inline int
  323. arbel_cmd_read_mgm ( struct arbel *arbel, unsigned int index,
  324. struct arbelprm_mgm_entry *mgm ) {
  325. return arbel_cmd ( arbel,
  326. ARBEL_HCR_OUT_CMD ( ARBEL_HCR_READ_MGM,
  327. 1, sizeof ( *mgm ) ),
  328. 0, NULL, index, mgm );
  329. }
  330. static inline int
  331. arbel_cmd_write_mgm ( struct arbel *arbel, unsigned int index,
  332. const struct arbelprm_mgm_entry *mgm ) {
  333. return arbel_cmd ( arbel,
  334. ARBEL_HCR_IN_CMD ( ARBEL_HCR_WRITE_MGM,
  335. 1, sizeof ( *mgm ) ),
  336. 0, mgm, index, NULL );
  337. }
  338. static inline int
  339. arbel_cmd_mgid_hash ( struct arbel *arbel, const struct ib_gid *gid,
  340. struct arbelprm_mgm_hash *hash ) {
  341. return arbel_cmd ( arbel,
  342. ARBEL_HCR_INOUT_CMD ( ARBEL_HCR_MGID_HASH,
  343. 1, sizeof ( *gid ),
  344. 0, sizeof ( *hash ) ),
  345. 0, gid, 0, hash );
  346. }
  347. static inline int
  348. arbel_cmd_run_fw ( struct arbel *arbel ) {
  349. return arbel_cmd ( arbel,
  350. ARBEL_HCR_VOID_CMD ( ARBEL_HCR_RUN_FW ),
  351. 0, NULL, 0, NULL );
  352. }
  353. static inline int
  354. arbel_cmd_disable_lam ( struct arbel *arbel ) {
  355. return arbel_cmd ( arbel,
  356. ARBEL_HCR_VOID_CMD ( ARBEL_HCR_DISABLE_LAM ),
  357. 0, NULL, 0, NULL );
  358. }
  359. static inline int
  360. arbel_cmd_enable_lam ( struct arbel *arbel, struct arbelprm_access_lam *lam ) {
  361. return arbel_cmd ( arbel,
  362. ARBEL_HCR_OUT_CMD ( ARBEL_HCR_ENABLE_LAM,
  363. 1, sizeof ( *lam ) ),
  364. 1, NULL, 0, lam );
  365. }
  366. static inline int
  367. arbel_cmd_unmap_icm ( struct arbel *arbel, unsigned int page_count ) {
  368. return arbel_cmd ( arbel,
  369. ARBEL_HCR_VOID_CMD ( ARBEL_HCR_UNMAP_ICM ),
  370. 0, NULL, page_count, NULL );
  371. }
  372. static inline int
  373. arbel_cmd_map_icm ( struct arbel *arbel,
  374. const struct arbelprm_virtual_physical_mapping *map ) {
  375. return arbel_cmd ( arbel,
  376. ARBEL_HCR_IN_CMD ( ARBEL_HCR_MAP_ICM,
  377. 1, sizeof ( *map ) ),
  378. 0, map, 1, NULL );
  379. }
  380. static inline int
  381. arbel_cmd_unmap_icm_aux ( struct arbel *arbel ) {
  382. return arbel_cmd ( arbel,
  383. ARBEL_HCR_VOID_CMD ( ARBEL_HCR_UNMAP_ICM_AUX ),
  384. 0, NULL, 0, NULL );
  385. }
  386. static inline int
  387. arbel_cmd_map_icm_aux ( struct arbel *arbel,
  388. const struct arbelprm_virtual_physical_mapping *map ) {
  389. return arbel_cmd ( arbel,
  390. ARBEL_HCR_IN_CMD ( ARBEL_HCR_MAP_ICM_AUX,
  391. 1, sizeof ( *map ) ),
  392. 0, map, 1, NULL );
  393. }
  394. static inline int
  395. arbel_cmd_set_icm_size ( struct arbel *arbel,
  396. const struct arbelprm_scalar_parameter *icm_size,
  397. struct arbelprm_scalar_parameter *icm_aux_size ) {
  398. return arbel_cmd ( arbel,
  399. ARBEL_HCR_INOUT_CMD ( ARBEL_HCR_SET_ICM_SIZE,
  400. 0, sizeof ( *icm_size ),
  401. 0, sizeof ( *icm_aux_size ) ),
  402. 0, icm_size, 0, icm_aux_size );
  403. }
  404. static inline int
  405. arbel_cmd_unmap_fa ( struct arbel *arbel ) {
  406. return arbel_cmd ( arbel,
  407. ARBEL_HCR_VOID_CMD ( ARBEL_HCR_UNMAP_FA ),
  408. 0, NULL, 0, NULL );
  409. }
  410. static inline int
  411. arbel_cmd_map_fa ( struct arbel *arbel,
  412. const struct arbelprm_virtual_physical_mapping *map ) {
  413. return arbel_cmd ( arbel,
  414. ARBEL_HCR_IN_CMD ( ARBEL_HCR_MAP_FA,
  415. 1, sizeof ( *map ) ),
  416. 0, map, 1, NULL );
  417. }
  418. /***************************************************************************
  419. *
  420. * Completion queue operations
  421. *
  422. ***************************************************************************
  423. */
  424. /**
  425. * Create completion queue
  426. *
  427. * @v ibdev Infiniband device
  428. * @v cq Completion queue
  429. * @ret rc Return status code
  430. */
  431. static int arbel_create_cq ( struct ib_device *ibdev,
  432. struct ib_completion_queue *cq ) {
  433. struct arbel *arbel = ibdev->dev_priv;
  434. struct arbel_completion_queue *arbel_cq;
  435. struct arbelprm_completion_queue_context cqctx;
  436. struct arbelprm_cq_ci_db_record *ci_db_rec;
  437. struct arbelprm_cq_arm_db_record *arm_db_rec;
  438. int cqn_offset;
  439. unsigned int i;
  440. int rc;
  441. /* Find a free completion queue number */
  442. cqn_offset = arbel_alloc_qn_offset ( arbel->cq_inuse, ARBEL_MAX_CQS );
  443. if ( cqn_offset < 0 ) {
  444. DBGC ( arbel, "Arbel %p out of completion queues\n", arbel );
  445. rc = cqn_offset;
  446. goto err_cqn_offset;
  447. }
  448. cq->cqn = ( arbel->limits.reserved_cqs + cqn_offset );
  449. /* Allocate control structures */
  450. arbel_cq = zalloc ( sizeof ( *arbel_cq ) );
  451. if ( ! arbel_cq ) {
  452. rc = -ENOMEM;
  453. goto err_arbel_cq;
  454. }
  455. arbel_cq->ci_doorbell_idx = arbel_cq_ci_doorbell_idx ( cqn_offset );
  456. arbel_cq->arm_doorbell_idx = arbel_cq_arm_doorbell_idx ( cqn_offset );
  457. /* Allocate completion queue itself */
  458. arbel_cq->cqe_size = ( cq->num_cqes * sizeof ( arbel_cq->cqe[0] ) );
  459. arbel_cq->cqe = malloc_dma ( arbel_cq->cqe_size,
  460. sizeof ( arbel_cq->cqe[0] ) );
  461. if ( ! arbel_cq->cqe ) {
  462. rc = -ENOMEM;
  463. goto err_cqe;
  464. }
  465. memset ( arbel_cq->cqe, 0, arbel_cq->cqe_size );
  466. for ( i = 0 ; i < cq->num_cqes ; i++ ) {
  467. MLX_FILL_1 ( &arbel_cq->cqe[i].normal, 7, owner, 1 );
  468. }
  469. barrier();
  470. /* Initialise doorbell records */
  471. ci_db_rec = &arbel->db_rec[arbel_cq->ci_doorbell_idx].cq_ci;
  472. MLX_FILL_1 ( ci_db_rec, 0, counter, 0 );
  473. MLX_FILL_2 ( ci_db_rec, 1,
  474. res, ARBEL_UAR_RES_CQ_CI,
  475. cq_number, cq->cqn );
  476. arm_db_rec = &arbel->db_rec[arbel_cq->arm_doorbell_idx].cq_arm;
  477. MLX_FILL_1 ( arm_db_rec, 0, counter, 0 );
  478. MLX_FILL_2 ( arm_db_rec, 1,
  479. res, ARBEL_UAR_RES_CQ_ARM,
  480. cq_number, cq->cqn );
  481. /* Hand queue over to hardware */
  482. memset ( &cqctx, 0, sizeof ( cqctx ) );
  483. MLX_FILL_1 ( &cqctx, 0, st, 0xa /* "Event fired" */ );
  484. MLX_FILL_1 ( &cqctx, 2, start_address_l,
  485. virt_to_bus ( arbel_cq->cqe ) );
  486. MLX_FILL_2 ( &cqctx, 3,
  487. usr_page, arbel->limits.reserved_uars,
  488. log_cq_size, fls ( cq->num_cqes - 1 ) );
  489. MLX_FILL_1 ( &cqctx, 5, c_eqn, ARBEL_NO_EQ );
  490. MLX_FILL_1 ( &cqctx, 6, pd, ARBEL_GLOBAL_PD );
  491. MLX_FILL_1 ( &cqctx, 7, l_key, arbel->reserved_lkey );
  492. MLX_FILL_1 ( &cqctx, 12, cqn, cq->cqn );
  493. MLX_FILL_1 ( &cqctx, 13,
  494. cq_ci_db_record, arbel_cq->ci_doorbell_idx );
  495. MLX_FILL_1 ( &cqctx, 14,
  496. cq_state_db_record, arbel_cq->arm_doorbell_idx );
  497. if ( ( rc = arbel_cmd_sw2hw_cq ( arbel, cq->cqn, &cqctx ) ) != 0 ) {
  498. DBGC ( arbel, "Arbel %p SW2HW_CQ failed: %s\n",
  499. arbel, strerror ( rc ) );
  500. goto err_sw2hw_cq;
  501. }
  502. DBGC ( arbel, "Arbel %p CQN %#lx ring at [%p,%p)\n",
  503. arbel, cq->cqn, arbel_cq->cqe,
  504. ( ( ( void * ) arbel_cq->cqe ) + arbel_cq->cqe_size ) );
  505. cq->dev_priv = arbel_cq;
  506. return 0;
  507. err_sw2hw_cq:
  508. MLX_FILL_1 ( ci_db_rec, 1, res, ARBEL_UAR_RES_NONE );
  509. MLX_FILL_1 ( arm_db_rec, 1, res, ARBEL_UAR_RES_NONE );
  510. free_dma ( arbel_cq->cqe, arbel_cq->cqe_size );
  511. err_cqe:
  512. free ( arbel_cq );
  513. err_arbel_cq:
  514. arbel_free_qn_offset ( arbel->cq_inuse, cqn_offset );
  515. err_cqn_offset:
  516. return rc;
  517. }
  518. /**
  519. * Destroy completion queue
  520. *
  521. * @v ibdev Infiniband device
  522. * @v cq Completion queue
  523. */
  524. static void arbel_destroy_cq ( struct ib_device *ibdev,
  525. struct ib_completion_queue *cq ) {
  526. struct arbel *arbel = ibdev->dev_priv;
  527. struct arbel_completion_queue *arbel_cq = cq->dev_priv;
  528. struct arbelprm_completion_queue_context cqctx;
  529. struct arbelprm_cq_ci_db_record *ci_db_rec;
  530. struct arbelprm_cq_arm_db_record *arm_db_rec;
  531. int cqn_offset;
  532. int rc;
  533. /* Take ownership back from hardware */
  534. if ( ( rc = arbel_cmd_hw2sw_cq ( arbel, cq->cqn, &cqctx ) ) != 0 ) {
  535. DBGC ( arbel, "Arbel %p FATAL HW2SW_CQ failed on CQN %#lx: "
  536. "%s\n", arbel, cq->cqn, strerror ( rc ) );
  537. /* Leak memory and return; at least we avoid corruption */
  538. return;
  539. }
  540. /* Clear doorbell records */
  541. ci_db_rec = &arbel->db_rec[arbel_cq->ci_doorbell_idx].cq_ci;
  542. arm_db_rec = &arbel->db_rec[arbel_cq->arm_doorbell_idx].cq_arm;
  543. MLX_FILL_1 ( ci_db_rec, 1, res, ARBEL_UAR_RES_NONE );
  544. MLX_FILL_1 ( arm_db_rec, 1, res, ARBEL_UAR_RES_NONE );
  545. /* Free memory */
  546. free_dma ( arbel_cq->cqe, arbel_cq->cqe_size );
  547. free ( arbel_cq );
  548. /* Mark queue number as free */
  549. cqn_offset = ( cq->cqn - arbel->limits.reserved_cqs );
  550. arbel_free_qn_offset ( arbel->cq_inuse, cqn_offset );
  551. cq->dev_priv = NULL;
  552. }
  553. /***************************************************************************
  554. *
  555. * Queue pair operations
  556. *
  557. ***************************************************************************
  558. */
  559. /**
  560. * Create send work queue
  561. *
  562. * @v arbel_send_wq Send work queue
  563. * @v num_wqes Number of work queue entries
  564. * @ret rc Return status code
  565. */
  566. static int arbel_create_send_wq ( struct arbel_send_work_queue *arbel_send_wq,
  567. unsigned int num_wqes ) {
  568. struct arbelprm_ud_send_wqe *wqe;
  569. struct arbelprm_ud_send_wqe *next_wqe;
  570. unsigned int wqe_idx_mask;
  571. unsigned int i;
  572. /* Allocate work queue */
  573. arbel_send_wq->wqe_size = ( num_wqes *
  574. sizeof ( arbel_send_wq->wqe[0] ) );
  575. arbel_send_wq->wqe = malloc_dma ( arbel_send_wq->wqe_size,
  576. sizeof ( arbel_send_wq->wqe[0] ) );
  577. if ( ! arbel_send_wq->wqe )
  578. return -ENOMEM;
  579. memset ( arbel_send_wq->wqe, 0, arbel_send_wq->wqe_size );
  580. /* Link work queue entries */
  581. wqe_idx_mask = ( num_wqes - 1 );
  582. for ( i = 0 ; i < num_wqes ; i++ ) {
  583. wqe = &arbel_send_wq->wqe[i].ud;
  584. next_wqe = &arbel_send_wq->wqe[ ( i + 1 ) & wqe_idx_mask ].ud;
  585. MLX_FILL_1 ( &wqe->next, 0, nda_31_6,
  586. ( virt_to_bus ( next_wqe ) >> 6 ) );
  587. }
  588. return 0;
  589. }
  590. /**
  591. * Create receive work queue
  592. *
  593. * @v arbel_recv_wq Receive work queue
  594. * @v num_wqes Number of work queue entries
  595. * @ret rc Return status code
  596. */
  597. static int arbel_create_recv_wq ( struct arbel_recv_work_queue *arbel_recv_wq,
  598. unsigned int num_wqes ) {
  599. struct arbelprm_recv_wqe *wqe;
  600. struct arbelprm_recv_wqe *next_wqe;
  601. unsigned int wqe_idx_mask;
  602. size_t nds;
  603. unsigned int i;
  604. unsigned int j;
  605. /* Allocate work queue */
  606. arbel_recv_wq->wqe_size = ( num_wqes *
  607. sizeof ( arbel_recv_wq->wqe[0] ) );
  608. arbel_recv_wq->wqe = malloc_dma ( arbel_recv_wq->wqe_size,
  609. sizeof ( arbel_recv_wq->wqe[0] ) );
  610. if ( ! arbel_recv_wq->wqe )
  611. return -ENOMEM;
  612. memset ( arbel_recv_wq->wqe, 0, arbel_recv_wq->wqe_size );
  613. /* Link work queue entries */
  614. wqe_idx_mask = ( num_wqes - 1 );
  615. nds = ( ( offsetof ( typeof ( *wqe ), data ) +
  616. sizeof ( wqe->data[0] ) ) >> 4 );
  617. for ( i = 0 ; i < num_wqes ; i++ ) {
  618. wqe = &arbel_recv_wq->wqe[i].recv;
  619. next_wqe = &arbel_recv_wq->wqe[( i + 1 ) & wqe_idx_mask].recv;
  620. MLX_FILL_1 ( &wqe->next, 0, nda_31_6,
  621. ( virt_to_bus ( next_wqe ) >> 6 ) );
  622. MLX_FILL_1 ( &wqe->next, 1, nds, ( sizeof ( *wqe ) / 16 ) );
  623. for ( j = 0 ; ( ( ( void * ) &wqe->data[j] ) <
  624. ( ( void * ) ( wqe + 1 ) ) ) ; j++ ) {
  625. MLX_FILL_1 ( &wqe->data[j], 1,
  626. l_key, ARBEL_INVALID_LKEY );
  627. }
  628. }
  629. return 0;
  630. }
  631. /**
  632. * Create queue pair
  633. *
  634. * @v ibdev Infiniband device
  635. * @v qp Queue pair
  636. * @ret rc Return status code
  637. */
  638. static int arbel_create_qp ( struct ib_device *ibdev,
  639. struct ib_queue_pair *qp ) {
  640. struct arbel *arbel = ibdev->dev_priv;
  641. struct arbel_queue_pair *arbel_qp;
  642. struct arbelprm_qp_ee_state_transitions qpctx;
  643. struct arbelprm_qp_db_record *send_db_rec;
  644. struct arbelprm_qp_db_record *recv_db_rec;
  645. int qpn_offset;
  646. int rc;
  647. /* Find a free queue pair number */
  648. qpn_offset = arbel_alloc_qn_offset ( arbel->qp_inuse, ARBEL_MAX_QPS );
  649. if ( qpn_offset < 0 ) {
  650. DBGC ( arbel, "Arbel %p out of queue pairs\n", arbel );
  651. rc = qpn_offset;
  652. goto err_qpn_offset;
  653. }
  654. qp->qpn = ( ARBEL_QPN_BASE + arbel->limits.reserved_qps + qpn_offset );
  655. /* Allocate control structures */
  656. arbel_qp = zalloc ( sizeof ( *arbel_qp ) );
  657. if ( ! arbel_qp ) {
  658. rc = -ENOMEM;
  659. goto err_arbel_qp;
  660. }
  661. arbel_qp->send.doorbell_idx = arbel_send_doorbell_idx ( qpn_offset );
  662. arbel_qp->recv.doorbell_idx = arbel_recv_doorbell_idx ( qpn_offset );
  663. /* Create send and receive work queues */
  664. if ( ( rc = arbel_create_send_wq ( &arbel_qp->send,
  665. qp->send.num_wqes ) ) != 0 )
  666. goto err_create_send_wq;
  667. if ( ( rc = arbel_create_recv_wq ( &arbel_qp->recv,
  668. qp->recv.num_wqes ) ) != 0 )
  669. goto err_create_recv_wq;
  670. /* Initialise doorbell records */
  671. send_db_rec = &arbel->db_rec[arbel_qp->send.doorbell_idx].qp;
  672. MLX_FILL_1 ( send_db_rec, 0, counter, 0 );
  673. MLX_FILL_2 ( send_db_rec, 1,
  674. res, ARBEL_UAR_RES_SQ,
  675. qp_number, qp->qpn );
  676. recv_db_rec = &arbel->db_rec[arbel_qp->recv.doorbell_idx].qp;
  677. MLX_FILL_1 ( recv_db_rec, 0, counter, 0 );
  678. MLX_FILL_2 ( recv_db_rec, 1,
  679. res, ARBEL_UAR_RES_RQ,
  680. qp_number, qp->qpn );
  681. /* Hand queue over to hardware */
  682. memset ( &qpctx, 0, sizeof ( qpctx ) );
  683. MLX_FILL_3 ( &qpctx, 2,
  684. qpc_eec_data.de, 1,
  685. qpc_eec_data.pm_state, 0x03 /* Always 0x03 for UD */,
  686. qpc_eec_data.st, ARBEL_ST_UD );
  687. MLX_FILL_6 ( &qpctx, 4,
  688. qpc_eec_data.mtu, ARBEL_MTU_2048,
  689. qpc_eec_data.msg_max, 11 /* 2^11 = 2048 */,
  690. qpc_eec_data.log_rq_size, fls ( qp->recv.num_wqes - 1 ),
  691. qpc_eec_data.log_rq_stride,
  692. ( fls ( sizeof ( arbel_qp->recv.wqe[0] ) - 1 ) - 4 ),
  693. qpc_eec_data.log_sq_size, fls ( qp->send.num_wqes - 1 ),
  694. qpc_eec_data.log_sq_stride,
  695. ( fls ( sizeof ( arbel_qp->send.wqe[0] ) - 1 ) - 4 ) );
  696. MLX_FILL_1 ( &qpctx, 5,
  697. qpc_eec_data.usr_page, arbel->limits.reserved_uars );
  698. MLX_FILL_1 ( &qpctx, 10, qpc_eec_data.primary_address_path.port_number,
  699. PXE_IB_PORT );
  700. MLX_FILL_1 ( &qpctx, 27, qpc_eec_data.pd, ARBEL_GLOBAL_PD );
  701. MLX_FILL_1 ( &qpctx, 29, qpc_eec_data.wqe_lkey, arbel->reserved_lkey );
  702. MLX_FILL_1 ( &qpctx, 30, qpc_eec_data.ssc, 1 );
  703. MLX_FILL_1 ( &qpctx, 33, qpc_eec_data.cqn_snd, qp->send.cq->cqn );
  704. MLX_FILL_1 ( &qpctx, 34, qpc_eec_data.snd_wqe_base_adr_l,
  705. ( virt_to_bus ( arbel_qp->send.wqe ) >> 6 ) );
  706. MLX_FILL_1 ( &qpctx, 35, qpc_eec_data.snd_db_record_index,
  707. arbel_qp->send.doorbell_idx );
  708. MLX_FILL_1 ( &qpctx, 38, qpc_eec_data.rsc, 1 );
  709. MLX_FILL_1 ( &qpctx, 41, qpc_eec_data.cqn_rcv, qp->recv.cq->cqn );
  710. MLX_FILL_1 ( &qpctx, 42, qpc_eec_data.rcv_wqe_base_adr_l,
  711. ( virt_to_bus ( arbel_qp->recv.wqe ) >> 6 ) );
  712. MLX_FILL_1 ( &qpctx, 43, qpc_eec_data.rcv_db_record_index,
  713. arbel_qp->recv.doorbell_idx );
  714. MLX_FILL_1 ( &qpctx, 44, qpc_eec_data.q_key, qp->qkey );
  715. if ( ( rc = arbel_cmd_rst2init_qpee ( arbel, qp->qpn, &qpctx )) != 0 ){
  716. DBGC ( arbel, "Arbel %p RST2INIT_QPEE failed: %s\n",
  717. arbel, strerror ( rc ) );
  718. goto err_rst2init_qpee;
  719. }
  720. memset ( &qpctx, 0, sizeof ( qpctx ) );
  721. MLX_FILL_2 ( &qpctx, 4,
  722. qpc_eec_data.mtu, ARBEL_MTU_2048,
  723. qpc_eec_data.msg_max, 11 /* 2^11 = 2048 */ );
  724. if ( ( rc = arbel_cmd_init2rtr_qpee ( arbel, qp->qpn, &qpctx )) != 0 ){
  725. DBGC ( arbel, "Arbel %p INIT2RTR_QPEE failed: %s\n",
  726. arbel, strerror ( rc ) );
  727. goto err_init2rtr_qpee;
  728. }
  729. memset ( &qpctx, 0, sizeof ( qpctx ) );
  730. if ( ( rc = arbel_cmd_rtr2rts_qpee ( arbel, qp->qpn, &qpctx ) ) != 0 ){
  731. DBGC ( arbel, "Arbel %p RTR2RTS_QPEE failed: %s\n",
  732. arbel, strerror ( rc ) );
  733. goto err_rtr2rts_qpee;
  734. }
  735. DBGC ( arbel, "Arbel %p QPN %#lx send ring at [%p,%p)\n",
  736. arbel, qp->qpn, arbel_qp->send.wqe,
  737. ( ( (void *) arbel_qp->send.wqe ) + arbel_qp->send.wqe_size ) );
  738. DBGC ( arbel, "Arbel %p QPN %#lx receive ring at [%p,%p)\n",
  739. arbel, qp->qpn, arbel_qp->recv.wqe,
  740. ( ( (void *) arbel_qp->recv.wqe ) + arbel_qp->recv.wqe_size ) );
  741. qp->dev_priv = arbel_qp;
  742. return 0;
  743. err_rtr2rts_qpee:
  744. err_init2rtr_qpee:
  745. arbel_cmd_2rst_qpee ( arbel, qp->qpn );
  746. err_rst2init_qpee:
  747. MLX_FILL_1 ( send_db_rec, 1, res, ARBEL_UAR_RES_NONE );
  748. MLX_FILL_1 ( recv_db_rec, 1, res, ARBEL_UAR_RES_NONE );
  749. free_dma ( arbel_qp->recv.wqe, arbel_qp->recv.wqe_size );
  750. err_create_recv_wq:
  751. free_dma ( arbel_qp->send.wqe, arbel_qp->send.wqe_size );
  752. err_create_send_wq:
  753. free ( arbel_qp );
  754. err_arbel_qp:
  755. arbel_free_qn_offset ( arbel->qp_inuse, qpn_offset );
  756. err_qpn_offset:
  757. return rc;
  758. }
  759. /**
  760. * Destroy queue pair
  761. *
  762. * @v ibdev Infiniband device
  763. * @v qp Queue pair
  764. */
  765. static void arbel_destroy_qp ( struct ib_device *ibdev,
  766. struct ib_queue_pair *qp ) {
  767. struct arbel *arbel = ibdev->dev_priv;
  768. struct arbel_queue_pair *arbel_qp = qp->dev_priv;
  769. struct arbelprm_qp_db_record *send_db_rec;
  770. struct arbelprm_qp_db_record *recv_db_rec;
  771. int qpn_offset;
  772. int rc;
  773. /* Take ownership back from hardware */
  774. if ( ( rc = arbel_cmd_2rst_qpee ( arbel, qp->qpn ) ) != 0 ) {
  775. DBGC ( arbel, "Arbel %p FATAL 2RST_QPEE failed on QPN %#lx: "
  776. "%s\n", arbel, qp->qpn, strerror ( rc ) );
  777. /* Leak memory and return; at least we avoid corruption */
  778. return;
  779. }
  780. /* Clear doorbell records */
  781. send_db_rec = &arbel->db_rec[arbel_qp->send.doorbell_idx].qp;
  782. recv_db_rec = &arbel->db_rec[arbel_qp->recv.doorbell_idx].qp;
  783. MLX_FILL_1 ( send_db_rec, 1, res, ARBEL_UAR_RES_NONE );
  784. MLX_FILL_1 ( recv_db_rec, 1, res, ARBEL_UAR_RES_NONE );
  785. /* Free memory */
  786. free_dma ( arbel_qp->send.wqe, arbel_qp->send.wqe_size );
  787. free_dma ( arbel_qp->recv.wqe, arbel_qp->recv.wqe_size );
  788. free ( arbel_qp );
  789. /* Mark queue number as free */
  790. qpn_offset = ( qp->qpn - ARBEL_QPN_BASE - arbel->limits.reserved_qps );
  791. arbel_free_qn_offset ( arbel->qp_inuse, qpn_offset );
  792. qp->dev_priv = NULL;
  793. }
  794. /***************************************************************************
  795. *
  796. * Work request operations
  797. *
  798. ***************************************************************************
  799. */
  800. /**
  801. * Ring doorbell register in UAR
  802. *
  803. * @v arbel Arbel device
  804. * @v db_reg Doorbell register structure
  805. * @v offset Address of doorbell
  806. */
  807. static void arbel_ring_doorbell ( struct arbel *arbel,
  808. union arbelprm_doorbell_register *db_reg,
  809. unsigned int offset ) {
  810. DBGC2 ( arbel, "Arbel %p ringing doorbell %08lx:%08lx at %lx\n",
  811. arbel, db_reg->dword[0], db_reg->dword[1],
  812. virt_to_phys ( arbel->uar + offset ) );
  813. barrier();
  814. writel ( db_reg->dword[0], ( arbel->uar + offset + 0 ) );
  815. barrier();
  816. writel ( db_reg->dword[1], ( arbel->uar + offset + 4 ) );
  817. }
  818. /** GID used for GID-less send work queue entries */
  819. static const struct ib_gid arbel_no_gid = {
  820. { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0 } }
  821. };
  822. /**
  823. * Post send work queue entry
  824. *
  825. * @v ibdev Infiniband device
  826. * @v qp Queue pair
  827. * @v av Address vector
  828. * @v iobuf I/O buffer
  829. * @ret rc Return status code
  830. */
  831. static int arbel_post_send ( struct ib_device *ibdev,
  832. struct ib_queue_pair *qp,
  833. struct ib_address_vector *av,
  834. struct io_buffer *iobuf ) {
  835. struct arbel *arbel = ibdev->dev_priv;
  836. struct arbel_queue_pair *arbel_qp = qp->dev_priv;
  837. struct ib_work_queue *wq = &qp->send;
  838. struct arbel_send_work_queue *arbel_send_wq = &arbel_qp->send;
  839. struct arbelprm_ud_send_wqe *prev_wqe;
  840. struct arbelprm_ud_send_wqe *wqe;
  841. struct arbelprm_qp_db_record *qp_db_rec;
  842. union arbelprm_doorbell_register db_reg;
  843. const struct ib_gid *gid;
  844. unsigned int wqe_idx_mask;
  845. size_t nds;
  846. /* Allocate work queue entry */
  847. wqe_idx_mask = ( wq->num_wqes - 1 );
  848. if ( wq->iobufs[wq->next_idx & wqe_idx_mask] ) {
  849. DBGC ( arbel, "Arbel %p send queue full", arbel );
  850. return -ENOBUFS;
  851. }
  852. wq->iobufs[wq->next_idx & wqe_idx_mask] = iobuf;
  853. prev_wqe = &arbel_send_wq->wqe[(wq->next_idx - 1) & wqe_idx_mask].ud;
  854. wqe = &arbel_send_wq->wqe[wq->next_idx & wqe_idx_mask].ud;
  855. /* Construct work queue entry */
  856. MLX_FILL_1 ( &wqe->next, 1, always1, 1 );
  857. memset ( &wqe->ctrl, 0, sizeof ( wqe->ctrl ) );
  858. MLX_FILL_1 ( &wqe->ctrl, 0, always1, 1 );
  859. memset ( &wqe->ud, 0, sizeof ( wqe->ud ) );
  860. MLX_FILL_2 ( &wqe->ud, 0,
  861. ud_address_vector.pd, ARBEL_GLOBAL_PD,
  862. ud_address_vector.port_number, PXE_IB_PORT );
  863. MLX_FILL_2 ( &wqe->ud, 1,
  864. ud_address_vector.rlid, av->dlid,
  865. ud_address_vector.g, av->gid_present );
  866. MLX_FILL_2 ( &wqe->ud, 2,
  867. ud_address_vector.max_stat_rate,
  868. ( ( av->rate >= 3 ) ? 0 : 1 ),
  869. ud_address_vector.msg, 3 );
  870. MLX_FILL_1 ( &wqe->ud, 3, ud_address_vector.sl, av->sl );
  871. gid = ( av->gid_present ? &av->gid : &arbel_no_gid );
  872. memcpy ( &wqe->ud.u.dwords[4], gid, sizeof ( *gid ) );
  873. MLX_FILL_1 ( &wqe->ud, 8, destination_qp, av->dest_qp );
  874. MLX_FILL_1 ( &wqe->ud, 9, q_key, av->qkey );
  875. MLX_FILL_1 ( &wqe->data[0], 0, byte_count, iob_len ( iobuf ) );
  876. MLX_FILL_1 ( &wqe->data[0], 1, l_key, arbel->reserved_lkey );
  877. MLX_FILL_1 ( &wqe->data[0], 3,
  878. local_address_l, virt_to_bus ( iobuf->data ) );
  879. /* Update previous work queue entry's "next" field */
  880. nds = ( ( offsetof ( typeof ( *wqe ), data ) +
  881. sizeof ( wqe->data[0] ) ) >> 4 );
  882. MLX_SET ( &prev_wqe->next, nopcode, ARBEL_OPCODE_SEND );
  883. MLX_FILL_3 ( &prev_wqe->next, 1,
  884. nds, nds,
  885. f, 1,
  886. always1, 1 );
  887. /* Update doorbell record */
  888. barrier();
  889. qp_db_rec = &arbel->db_rec[arbel_send_wq->doorbell_idx].qp;
  890. MLX_FILL_1 ( qp_db_rec, 0,
  891. counter, ( ( wq->next_idx + 1 ) & 0xffff ) );
  892. /* Ring doorbell register */
  893. MLX_FILL_4 ( &db_reg.send, 0,
  894. nopcode, ARBEL_OPCODE_SEND,
  895. f, 1,
  896. wqe_counter, ( wq->next_idx & 0xffff ),
  897. wqe_cnt, 1 );
  898. MLX_FILL_2 ( &db_reg.send, 1,
  899. nds, nds,
  900. qpn, qp->qpn );
  901. arbel_ring_doorbell ( arbel, &db_reg, ARBEL_DB_POST_SND_OFFSET );
  902. /* Update work queue's index */
  903. wq->next_idx++;
  904. return 0;
  905. }
  906. /**
  907. * Post receive work queue entry
  908. *
  909. * @v ibdev Infiniband device
  910. * @v qp Queue pair
  911. * @v iobuf I/O buffer
  912. * @ret rc Return status code
  913. */
  914. static int arbel_post_recv ( struct ib_device *ibdev,
  915. struct ib_queue_pair *qp,
  916. struct io_buffer *iobuf ) {
  917. struct arbel *arbel = ibdev->dev_priv;
  918. struct arbel_queue_pair *arbel_qp = qp->dev_priv;
  919. struct ib_work_queue *wq = &qp->recv;
  920. struct arbel_recv_work_queue *arbel_recv_wq = &arbel_qp->recv;
  921. struct arbelprm_recv_wqe *wqe;
  922. union arbelprm_doorbell_record *db_rec;
  923. unsigned int wqe_idx_mask;
  924. /* Allocate work queue entry */
  925. wqe_idx_mask = ( wq->num_wqes - 1 );
  926. if ( wq->iobufs[wq->next_idx & wqe_idx_mask] ) {
  927. DBGC ( arbel, "Arbel %p receive queue full", arbel );
  928. return -ENOBUFS;
  929. }
  930. wq->iobufs[wq->next_idx & wqe_idx_mask] = iobuf;
  931. wqe = &arbel_recv_wq->wqe[wq->next_idx & wqe_idx_mask].recv;
  932. /* Construct work queue entry */
  933. MLX_FILL_1 ( &wqe->data[0], 0, byte_count, iob_tailroom ( iobuf ) );
  934. MLX_FILL_1 ( &wqe->data[0], 1, l_key, arbel->reserved_lkey );
  935. MLX_FILL_1 ( &wqe->data[0], 3,
  936. local_address_l, virt_to_bus ( iobuf->data ) );
  937. /* Update doorbell record */
  938. barrier();
  939. db_rec = &arbel->db_rec[arbel_recv_wq->doorbell_idx];
  940. MLX_FILL_1 ( &db_rec->qp, 0,
  941. counter, ( ( wq->next_idx + 1 ) & 0xffff ) );
  942. /* Update work queue's index */
  943. wq->next_idx++;
  944. return 0;
  945. }
  946. /**
  947. * Handle completion
  948. *
  949. * @v ibdev Infiniband device
  950. * @v cq Completion queue
  951. * @v cqe Hardware completion queue entry
  952. * @v complete_send Send completion handler
  953. * @v complete_recv Receive completion handler
  954. * @ret rc Return status code
  955. */
  956. static int arbel_complete ( struct ib_device *ibdev,
  957. struct ib_completion_queue *cq,
  958. union arbelprm_completion_entry *cqe,
  959. ib_completer_t complete_send,
  960. ib_completer_t complete_recv ) {
  961. struct arbel *arbel = ibdev->dev_priv;
  962. struct ib_completion completion;
  963. struct ib_work_queue *wq;
  964. struct ib_queue_pair *qp;
  965. struct arbel_queue_pair *arbel_qp;
  966. struct arbel_send_work_queue *arbel_send_wq;
  967. struct arbel_recv_work_queue *arbel_recv_wq;
  968. struct arbelprm_recv_wqe *recv_wqe;
  969. struct io_buffer *iobuf;
  970. ib_completer_t complete;
  971. unsigned int opcode;
  972. unsigned long qpn;
  973. int is_send;
  974. unsigned long wqe_adr;
  975. unsigned int wqe_idx;
  976. int rc = 0;
  977. /* Parse completion */
  978. memset ( &completion, 0, sizeof ( completion ) );
  979. qpn = MLX_GET ( &cqe->normal, my_qpn );
  980. is_send = MLX_GET ( &cqe->normal, s );
  981. wqe_adr = ( MLX_GET ( &cqe->normal, wqe_adr ) << 6 );
  982. opcode = MLX_GET ( &cqe->normal, opcode );
  983. if ( opcode >= ARBEL_OPCODE_RECV_ERROR ) {
  984. /* "s" field is not valid for error opcodes */
  985. is_send = ( opcode == ARBEL_OPCODE_SEND_ERROR );
  986. completion.syndrome = MLX_GET ( &cqe->error, syndrome );
  987. DBGC ( arbel, "Arbel %p CPN %lx syndrome %x vendor %lx\n",
  988. arbel, cq->cqn, completion.syndrome,
  989. MLX_GET ( &cqe->error, vendor_code ) );
  990. rc = -EIO;
  991. /* Don't return immediately; propagate error to completer */
  992. }
  993. /* Identify work queue */
  994. wq = ib_find_wq ( cq, qpn, is_send );
  995. if ( ! wq ) {
  996. DBGC ( arbel, "Arbel %p CQN %lx unknown %s QPN %lx\n",
  997. arbel, cq->cqn, ( is_send ? "send" : "recv" ), qpn );
  998. return -EIO;
  999. }
  1000. qp = wq->qp;
  1001. arbel_qp = qp->dev_priv;
  1002. arbel_send_wq = &arbel_qp->send;
  1003. arbel_recv_wq = &arbel_qp->recv;
  1004. /* Identify work queue entry index */
  1005. if ( is_send ) {
  1006. wqe_idx = ( ( wqe_adr - virt_to_bus ( arbel_send_wq->wqe ) ) /
  1007. sizeof ( arbel_send_wq->wqe[0] ) );
  1008. assert ( wqe_idx < qp->send.num_wqes );
  1009. } else {
  1010. wqe_idx = ( ( wqe_adr - virt_to_bus ( arbel_recv_wq->wqe ) ) /
  1011. sizeof ( arbel_recv_wq->wqe[0] ) );
  1012. assert ( wqe_idx < qp->recv.num_wqes );
  1013. }
  1014. /* Identify I/O buffer */
  1015. iobuf = wq->iobufs[wqe_idx];
  1016. if ( ! iobuf ) {
  1017. DBGC ( arbel, "Arbel %p CQN %lx QPN %lx empty WQE %x\n",
  1018. arbel, cq->cqn, qpn, wqe_idx );
  1019. return -EIO;
  1020. }
  1021. wq->iobufs[wqe_idx] = NULL;
  1022. /* Fill in length for received packets */
  1023. if ( ! is_send ) {
  1024. completion.len = MLX_GET ( &cqe->normal, byte_cnt );
  1025. recv_wqe = &arbel_recv_wq->wqe[wqe_idx].recv;
  1026. assert ( MLX_GET ( &recv_wqe->data[0], local_address_l ) ==
  1027. virt_to_bus ( iobuf->data ) );
  1028. assert ( MLX_GET ( &recv_wqe->data[0], byte_count ) ==
  1029. iob_tailroom ( iobuf ) );
  1030. MLX_FILL_1 ( &recv_wqe->data[0], 0, byte_count, 0 );
  1031. MLX_FILL_1 ( &recv_wqe->data[0], 1,
  1032. l_key, ARBEL_INVALID_LKEY );
  1033. if ( completion.len > iob_tailroom ( iobuf ) ) {
  1034. DBGC ( arbel, "Arbel %p CQN %lx QPN %lx IDX %x "
  1035. "overlength received packet length %zd\n",
  1036. arbel, cq->cqn, qpn, wqe_idx, completion.len );
  1037. return -EIO;
  1038. }
  1039. }
  1040. /* Pass off to caller's completion handler */
  1041. complete = ( is_send ? complete_send : complete_recv );
  1042. complete ( ibdev, qp, &completion, iobuf );
  1043. return rc;
  1044. }
  1045. /**
  1046. * Poll completion queue
  1047. *
  1048. * @v ibdev Infiniband device
  1049. * @v cq Completion queue
  1050. * @v complete_send Send completion handler
  1051. * @v complete_recv Receive completion handler
  1052. */
  1053. static void arbel_poll_cq ( struct ib_device *ibdev,
  1054. struct ib_completion_queue *cq,
  1055. ib_completer_t complete_send,
  1056. ib_completer_t complete_recv ) {
  1057. struct arbel *arbel = ibdev->dev_priv;
  1058. struct arbel_completion_queue *arbel_cq = cq->dev_priv;
  1059. struct arbelprm_cq_ci_db_record *ci_db_rec;
  1060. union arbelprm_completion_entry *cqe;
  1061. unsigned int cqe_idx_mask;
  1062. int rc;
  1063. while ( 1 ) {
  1064. /* Look for completion entry */
  1065. cqe_idx_mask = ( cq->num_cqes - 1 );
  1066. cqe = &arbel_cq->cqe[cq->next_idx & cqe_idx_mask];
  1067. if ( MLX_GET ( &cqe->normal, owner ) != 0 ) {
  1068. /* Entry still owned by hardware; end of poll */
  1069. break;
  1070. }
  1071. /* Handle completion */
  1072. if ( ( rc = arbel_complete ( ibdev, cq, cqe, complete_send,
  1073. complete_recv ) ) != 0 ) {
  1074. DBGC ( arbel, "Arbel %p failed to complete: %s\n",
  1075. arbel, strerror ( rc ) );
  1076. DBGC_HD ( arbel, cqe, sizeof ( *cqe ) );
  1077. }
  1078. /* Return ownership to hardware */
  1079. MLX_FILL_1 ( &cqe->normal, 7, owner, 1 );
  1080. barrier();
  1081. /* Update completion queue's index */
  1082. cq->next_idx++;
  1083. /* Update doorbell record */
  1084. ci_db_rec = &arbel->db_rec[arbel_cq->ci_doorbell_idx].cq_ci;
  1085. MLX_FILL_1 ( ci_db_rec, 0,
  1086. counter, ( cq->next_idx & 0xffffffffUL ) );
  1087. }
  1088. }
  1089. /***************************************************************************
  1090. *
  1091. * Multicast group operations
  1092. *
  1093. ***************************************************************************
  1094. */
  1095. /**
  1096. * Attach to multicast group
  1097. *
  1098. * @v ibdev Infiniband device
  1099. * @v qp Queue pair
  1100. * @v gid Multicast GID
  1101. * @ret rc Return status code
  1102. */
  1103. static int arbel_mcast_attach ( struct ib_device *ibdev,
  1104. struct ib_queue_pair *qp,
  1105. struct ib_gid *gid ) {
  1106. struct arbel *arbel = ibdev->dev_priv;
  1107. struct arbelprm_mgm_hash hash;
  1108. struct arbelprm_mgm_entry mgm;
  1109. unsigned int index;
  1110. int rc;
  1111. /* Generate hash table index */
  1112. if ( ( rc = arbel_cmd_mgid_hash ( arbel, gid, &hash ) ) != 0 ) {
  1113. DBGC ( arbel, "Arbel %p could not hash GID: %s\n",
  1114. arbel, strerror ( rc ) );
  1115. return rc;
  1116. }
  1117. index = MLX_GET ( &hash, hash );
  1118. /* Check for existing hash table entry */
  1119. if ( ( rc = arbel_cmd_read_mgm ( arbel, index, &mgm ) ) != 0 ) {
  1120. DBGC ( arbel, "Arbel %p could not read MGM %#x: %s\n",
  1121. arbel, index, strerror ( rc ) );
  1122. return rc;
  1123. }
  1124. if ( MLX_GET ( &mgm, mgmqp_0.qi ) != 0 ) {
  1125. /* FIXME: this implementation allows only a single QP
  1126. * per multicast group, and doesn't handle hash
  1127. * collisions. Sufficient for IPoIB but may need to
  1128. * be extended in future.
  1129. */
  1130. DBGC ( arbel, "Arbel %p MGID index %#x already in use\n",
  1131. arbel, index );
  1132. return -EBUSY;
  1133. }
  1134. /* Update hash table entry */
  1135. MLX_FILL_2 ( &mgm, 8,
  1136. mgmqp_0.qpn_i, qp->qpn,
  1137. mgmqp_0.qi, 1 );
  1138. memcpy ( &mgm.u.dwords[4], gid, sizeof ( *gid ) );
  1139. if ( ( rc = arbel_cmd_write_mgm ( arbel, index, &mgm ) ) != 0 ) {
  1140. DBGC ( arbel, "Arbel %p could not write MGM %#x: %s\n",
  1141. arbel, index, strerror ( rc ) );
  1142. return rc;
  1143. }
  1144. return 0;
  1145. }
  1146. /**
  1147. * Detach from multicast group
  1148. *
  1149. * @v ibdev Infiniband device
  1150. * @v qp Queue pair
  1151. * @v gid Multicast GID
  1152. */
  1153. static void arbel_mcast_detach ( struct ib_device *ibdev,
  1154. struct ib_queue_pair *qp __unused,
  1155. struct ib_gid *gid ) {
  1156. struct arbel *arbel = ibdev->dev_priv;
  1157. struct arbelprm_mgm_hash hash;
  1158. struct arbelprm_mgm_entry mgm;
  1159. unsigned int index;
  1160. int rc;
  1161. /* Generate hash table index */
  1162. if ( ( rc = arbel_cmd_mgid_hash ( arbel, gid, &hash ) ) != 0 ) {
  1163. DBGC ( arbel, "Arbel %p could not hash GID: %s\n",
  1164. arbel, strerror ( rc ) );
  1165. return;
  1166. }
  1167. index = MLX_GET ( &hash, hash );
  1168. /* Clear hash table entry */
  1169. memset ( &mgm, 0, sizeof ( mgm ) );
  1170. if ( ( rc = arbel_cmd_write_mgm ( arbel, index, &mgm ) ) != 0 ) {
  1171. DBGC ( arbel, "Arbel %p could not write MGM %#x: %s\n",
  1172. arbel, index, strerror ( rc ) );
  1173. return;
  1174. }
  1175. }
  1176. /** Arbel Infiniband operations */
  1177. static struct ib_device_operations arbel_ib_operations = {
  1178. .create_cq = arbel_create_cq,
  1179. .destroy_cq = arbel_destroy_cq,
  1180. .create_qp = arbel_create_qp,
  1181. .destroy_qp = arbel_destroy_qp,
  1182. .post_send = arbel_post_send,
  1183. .post_recv = arbel_post_recv,
  1184. .poll_cq = arbel_poll_cq,
  1185. .mcast_attach = arbel_mcast_attach,
  1186. .mcast_detach = arbel_mcast_detach,
  1187. };
  1188. /***************************************************************************
  1189. *
  1190. * MAD IFC operations
  1191. *
  1192. ***************************************************************************
  1193. */
  1194. static int arbel_mad_ifc ( struct arbel *arbel,
  1195. union arbelprm_mad *mad ) {
  1196. struct ib_mad_hdr *hdr = &mad->mad.mad_hdr;
  1197. int rc;
  1198. hdr->base_version = IB_MGMT_BASE_VERSION;
  1199. if ( ( rc = arbel_cmd_mad_ifc ( arbel, mad ) ) != 0 ) {
  1200. DBGC ( arbel, "Arbel %p could not issue MAD IFC: %s\n",
  1201. arbel, strerror ( rc ) );
  1202. return rc;
  1203. }
  1204. if ( hdr->status != 0 ) {
  1205. DBGC ( arbel, "Arbel %p MAD IFC status %04x\n",
  1206. arbel, ntohs ( hdr->status ) );
  1207. return -EIO;
  1208. }
  1209. return 0;
  1210. }
  1211. static int arbel_get_port_info ( struct arbel *arbel,
  1212. struct ib_mad_port_info *port_info ) {
  1213. union arbelprm_mad mad;
  1214. struct ib_mad_hdr *hdr = &mad.mad.mad_hdr;
  1215. int rc;
  1216. memset ( &mad, 0, sizeof ( mad ) );
  1217. hdr->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  1218. hdr->class_version = 1;
  1219. hdr->method = IB_MGMT_METHOD_GET;
  1220. hdr->attr_id = htons ( IB_SMP_ATTR_PORT_INFO );
  1221. hdr->attr_mod = htonl ( PXE_IB_PORT );
  1222. if ( ( rc = arbel_mad_ifc ( arbel, &mad ) ) != 0 ) {
  1223. DBGC ( arbel, "Arbel %p could not get port info: %s\n",
  1224. arbel, strerror ( rc ) );
  1225. return rc;
  1226. }
  1227. memcpy ( port_info, &mad.mad.port_info, sizeof ( *port_info ) );
  1228. return 0;
  1229. }
  1230. static int arbel_get_guid_info ( struct arbel *arbel,
  1231. struct ib_mad_guid_info *guid_info ) {
  1232. union arbelprm_mad mad;
  1233. struct ib_mad_hdr *hdr = &mad.mad.mad_hdr;
  1234. int rc;
  1235. memset ( &mad, 0, sizeof ( mad ) );
  1236. hdr->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  1237. hdr->class_version = 1;
  1238. hdr->method = IB_MGMT_METHOD_GET;
  1239. hdr->attr_id = htons ( IB_SMP_ATTR_GUID_INFO );
  1240. if ( ( rc = arbel_mad_ifc ( arbel, &mad ) ) != 0 ) {
  1241. DBGC ( arbel, "Arbel %p could not get GUID info: %s\n",
  1242. arbel, strerror ( rc ) );
  1243. return rc;
  1244. }
  1245. memcpy ( guid_info, &mad.mad.guid_info, sizeof ( *guid_info ) );
  1246. return 0;
  1247. }
  1248. static int arbel_get_pkey_table ( struct arbel *arbel,
  1249. struct ib_mad_pkey_table *pkey_table ) {
  1250. union arbelprm_mad mad;
  1251. struct ib_mad_hdr *hdr = &mad.mad.mad_hdr;
  1252. int rc;
  1253. memset ( &mad, 0, sizeof ( mad ) );
  1254. hdr->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
  1255. hdr->class_version = 1;
  1256. hdr->method = IB_MGMT_METHOD_GET;
  1257. hdr->attr_id = htons ( IB_SMP_ATTR_PKEY_TABLE );
  1258. if ( ( rc = arbel_mad_ifc ( arbel, &mad ) ) != 0 ) {
  1259. DBGC ( arbel, "Arbel %p could not get pkey table: %s\n",
  1260. arbel, strerror ( rc ) );
  1261. return rc;
  1262. }
  1263. memcpy ( pkey_table, &mad.mad.pkey_table, sizeof ( *pkey_table ) );
  1264. return 0;
  1265. }
  1266. static int arbel_get_port_gid ( struct arbel *arbel,
  1267. struct ib_gid *port_gid ) {
  1268. union {
  1269. /* This union exists just to save stack space */
  1270. struct ib_mad_port_info port_info;
  1271. struct ib_mad_guid_info guid_info;
  1272. } u;
  1273. int rc;
  1274. /* Port info gives us the first half of the port GID */
  1275. if ( ( rc = arbel_get_port_info ( arbel, &u.port_info ) ) != 0 )
  1276. return rc;
  1277. memcpy ( &port_gid->u.bytes[0], u.port_info.gid_prefix, 8 );
  1278. /* GUID info gives us the second half of the port GID */
  1279. if ( ( rc = arbel_get_guid_info ( arbel, &u.guid_info ) ) != 0 )
  1280. return rc;
  1281. memcpy ( &port_gid->u.bytes[8], u.guid_info.gid_local, 8 );
  1282. return 0;
  1283. }
  1284. static int arbel_get_sm_lid ( struct arbel *arbel,
  1285. unsigned long *sm_lid ) {
  1286. struct ib_mad_port_info port_info;
  1287. int rc;
  1288. if ( ( rc = arbel_get_port_info ( arbel, &port_info ) ) != 0 )
  1289. return rc;
  1290. *sm_lid = ntohs ( port_info.mastersm_lid );
  1291. return 0;
  1292. }
  1293. static int arbel_get_pkey ( struct arbel *arbel, unsigned int *pkey ) {
  1294. struct ib_mad_pkey_table pkey_table;
  1295. int rc;
  1296. if ( ( rc = arbel_get_pkey_table ( arbel, &pkey_table ) ) != 0 )
  1297. return rc;
  1298. *pkey = ntohs ( pkey_table.pkey[0][0] );
  1299. return 0;
  1300. }
  1301. /**
  1302. * Wait for link up
  1303. *
  1304. * @v arbel Arbel device
  1305. * @ret rc Return status code
  1306. *
  1307. * This function shouldn't really exist. Unfortunately, IB links take
  1308. * a long time to come up, and we can't get various key parameters
  1309. * e.g. our own IPoIB MAC address without information from the subnet
  1310. * manager). We should eventually make link-up an asynchronous event.
  1311. */
  1312. static int arbel_wait_for_link ( struct arbel *arbel ) {
  1313. struct ib_mad_port_info port_info;
  1314. unsigned int retries;
  1315. int rc;
  1316. printf ( "Waiting for Infiniband link-up..." );
  1317. for ( retries = 20 ; retries ; retries-- ) {
  1318. if ( ( rc = arbel_get_port_info ( arbel, &port_info ) ) != 0 )
  1319. continue;
  1320. if ( ( ( port_info.port_state__link_speed_supported ) & 0xf )
  1321. == 4 ) {
  1322. printf ( "ok\n" );
  1323. return 0;
  1324. }
  1325. printf ( "." );
  1326. sleep ( 1 );
  1327. }
  1328. printf ( "failed\n" );
  1329. return -ENODEV;
  1330. };
  1331. /**
  1332. * Get MAD parameters
  1333. *
  1334. * @v arbel Arbel device
  1335. * @ret rc Return status code
  1336. */
  1337. static int arbel_get_mad_params ( struct ib_device *ibdev ) {
  1338. struct arbel *arbel = ibdev->dev_priv;
  1339. int rc;
  1340. /* Get subnet manager LID */
  1341. if ( ( rc = arbel_get_sm_lid ( arbel, &ibdev->sm_lid ) ) != 0 ) {
  1342. DBGC ( arbel, "Arbel %p could not determine subnet manager "
  1343. "LID: %s\n", arbel, strerror ( rc ) );
  1344. return rc;
  1345. }
  1346. /* Get port GID */
  1347. if ( ( rc = arbel_get_port_gid ( arbel, &ibdev->port_gid ) ) != 0 ) {
  1348. DBGC ( arbel, "Arbel %p could not determine port GID: %s\n",
  1349. arbel, strerror ( rc ) );
  1350. return rc;
  1351. }
  1352. /* Get partition key */
  1353. if ( ( rc = arbel_get_pkey ( arbel, &ibdev->pkey ) ) != 0 ) {
  1354. DBGC ( arbel, "Arbel %p could not determine partition key: "
  1355. "%s\n", arbel, strerror ( rc ) );
  1356. return rc;
  1357. }
  1358. return 0;
  1359. }
  1360. /***************************************************************************
  1361. *
  1362. * Firmware control
  1363. *
  1364. ***************************************************************************
  1365. */
  1366. /**
  1367. * Start firmware running
  1368. *
  1369. * @v arbel Arbel device
  1370. * @ret rc Return status code
  1371. */
  1372. static int arbel_start_firmware ( struct arbel *arbel ) {
  1373. struct arbelprm_query_fw fw;
  1374. struct arbelprm_access_lam lam;
  1375. struct arbelprm_virtual_physical_mapping map_fa;
  1376. unsigned int fw_pages;
  1377. unsigned int log2_fw_pages;
  1378. size_t fw_size;
  1379. physaddr_t fw_base;
  1380. int rc;
  1381. /* Get firmware parameters */
  1382. if ( ( rc = arbel_cmd_query_fw ( arbel, &fw ) ) != 0 ) {
  1383. DBGC ( arbel, "Arbel %p could not query firmware: %s\n",
  1384. arbel, strerror ( rc ) );
  1385. goto err_query_fw;
  1386. }
  1387. DBGC ( arbel, "Arbel %p firmware version %ld.%ld.%ld\n", arbel,
  1388. MLX_GET ( &fw, fw_rev_major ), MLX_GET ( &fw, fw_rev_minor ),
  1389. MLX_GET ( &fw, fw_rev_subminor ) );
  1390. fw_pages = MLX_GET ( &fw, fw_pages );
  1391. log2_fw_pages = fls ( fw_pages - 1 );
  1392. fw_pages = ( 1 << log2_fw_pages );
  1393. DBGC ( arbel, "Arbel %p requires %d kB for firmware\n",
  1394. arbel, ( fw_pages * 4 ) );
  1395. /* Enable locally-attached memory. Ignore failure; there may
  1396. * be no attached memory.
  1397. */
  1398. arbel_cmd_enable_lam ( arbel, &lam );
  1399. /* Allocate firmware pages and map firmware area */
  1400. fw_size = ( fw_pages * 4096 );
  1401. arbel->firmware_area = umalloc ( fw_size );
  1402. if ( ! arbel->firmware_area ) {
  1403. rc = -ENOMEM;
  1404. goto err_alloc_fa;
  1405. }
  1406. fw_base = ( user_to_phys ( arbel->firmware_area, fw_size ) &
  1407. ~( fw_size - 1 ) );
  1408. DBGC ( arbel, "Arbel %p firmware area at physical [%lx,%lx)\n",
  1409. arbel, fw_base, ( fw_base + fw_size ) );
  1410. memset ( &map_fa, 0, sizeof ( map_fa ) );
  1411. MLX_FILL_2 ( &map_fa, 3,
  1412. log2size, log2_fw_pages,
  1413. pa_l, ( fw_base >> 12 ) );
  1414. if ( ( rc = arbel_cmd_map_fa ( arbel, &map_fa ) ) != 0 ) {
  1415. DBGC ( arbel, "Arbel %p could not map firmware: %s\n",
  1416. arbel, strerror ( rc ) );
  1417. goto err_map_fa;
  1418. }
  1419. /* Start firmware */
  1420. if ( ( rc = arbel_cmd_run_fw ( arbel ) ) != 0 ) {
  1421. DBGC ( arbel, "Arbel %p could not run firmware: %s\n",
  1422. arbel, strerror ( rc ) );
  1423. goto err_run_fw;
  1424. }
  1425. DBGC ( arbel, "Arbel %p firmware started\n", arbel );
  1426. return 0;
  1427. err_run_fw:
  1428. arbel_cmd_unmap_fa ( arbel );
  1429. err_map_fa:
  1430. ufree ( arbel->firmware_area );
  1431. arbel->firmware_area = UNULL;
  1432. err_alloc_fa:
  1433. err_query_fw:
  1434. return rc;
  1435. }
  1436. /**
  1437. * Stop firmware running
  1438. *
  1439. * @v arbel Arbel device
  1440. */
  1441. static void arbel_stop_firmware ( struct arbel *arbel ) {
  1442. int rc;
  1443. if ( ( rc = arbel_cmd_unmap_fa ( arbel ) ) != 0 ) {
  1444. DBGC ( arbel, "Arbel %p FATAL could not stop firmware: %s\n",
  1445. arbel, strerror ( rc ) );
  1446. /* Leak memory and return; at least we avoid corruption */
  1447. return;
  1448. }
  1449. ufree ( arbel->firmware_area );
  1450. arbel->firmware_area = UNULL;
  1451. }
  1452. /***************************************************************************
  1453. *
  1454. * Infinihost Context Memory management
  1455. *
  1456. ***************************************************************************
  1457. */
  1458. /**
  1459. * Get device limits
  1460. *
  1461. * @v arbel Arbel device
  1462. * @ret rc Return status code
  1463. */
  1464. static int arbel_get_limits ( struct arbel *arbel ) {
  1465. struct arbelprm_query_dev_lim dev_lim;
  1466. int rc;
  1467. if ( ( rc = arbel_cmd_query_dev_lim ( arbel, &dev_lim ) ) != 0 ) {
  1468. DBGC ( arbel, "Arbel %p could not get device limits: %s\n",
  1469. arbel, strerror ( rc ) );
  1470. return rc;
  1471. }
  1472. arbel->limits.reserved_qps =
  1473. ( 1 << MLX_GET ( &dev_lim, log2_rsvd_qps ) );
  1474. arbel->limits.qpc_entry_size = MLX_GET ( &dev_lim, qpc_entry_sz );
  1475. arbel->limits.eqpc_entry_size = MLX_GET ( &dev_lim, eqpc_entry_sz );
  1476. arbel->limits.reserved_srqs =
  1477. ( 1 << MLX_GET ( &dev_lim, log2_rsvd_srqs ) );
  1478. arbel->limits.srqc_entry_size = MLX_GET ( &dev_lim, srq_entry_sz );
  1479. arbel->limits.reserved_ees =
  1480. ( 1 << MLX_GET ( &dev_lim, log2_rsvd_ees ) );
  1481. arbel->limits.eec_entry_size = MLX_GET ( &dev_lim, eec_entry_sz );
  1482. arbel->limits.eeec_entry_size = MLX_GET ( &dev_lim, eeec_entry_sz );
  1483. arbel->limits.reserved_cqs =
  1484. ( 1 << MLX_GET ( &dev_lim, log2_rsvd_cqs ) );
  1485. arbel->limits.cqc_entry_size = MLX_GET ( &dev_lim, cqc_entry_sz );
  1486. arbel->limits.reserved_mtts =
  1487. ( 1 << MLX_GET ( &dev_lim, log2_rsvd_mtts ) );
  1488. arbel->limits.mtt_entry_size = MLX_GET ( &dev_lim, mtt_entry_sz );
  1489. arbel->limits.reserved_mrws =
  1490. ( 1 << MLX_GET ( &dev_lim, log2_rsvd_mrws ) );
  1491. arbel->limits.mpt_entry_size = MLX_GET ( &dev_lim, mpt_entry_sz );
  1492. arbel->limits.reserved_rdbs =
  1493. ( 1 << MLX_GET ( &dev_lim, log2_rsvd_rdbs ) );
  1494. arbel->limits.eqc_entry_size = MLX_GET ( &dev_lim, eqc_entry_sz );
  1495. arbel->limits.reserved_uars = MLX_GET ( &dev_lim, num_rsvd_uars );
  1496. return 0;
  1497. }
  1498. /**
  1499. * Get ICM usage
  1500. *
  1501. * @v log_num_entries Log2 of the number of entries
  1502. * @v entry_size Entry size
  1503. * @ret usage Usage size in ICM
  1504. */
  1505. static size_t icm_usage ( unsigned int log_num_entries, size_t entry_size ) {
  1506. size_t usage;
  1507. usage = ( ( 1 << log_num_entries ) * entry_size );
  1508. usage = ( ( usage + 4095 ) & ~4095 );
  1509. return usage;
  1510. }
  1511. /**
  1512. * Allocate ICM
  1513. *
  1514. * @v arbel Arbel device
  1515. * @v init_hca INIT_HCA structure to fill in
  1516. * @ret rc Return status code
  1517. */
  1518. static int arbel_alloc_icm ( struct arbel *arbel,
  1519. struct arbelprm_init_hca *init_hca ) {
  1520. struct arbelprm_scalar_parameter icm_size;
  1521. struct arbelprm_scalar_parameter icm_aux_size;
  1522. struct arbelprm_virtual_physical_mapping map_icm_aux;
  1523. struct arbelprm_virtual_physical_mapping map_icm;
  1524. size_t icm_offset = 0;
  1525. unsigned int log_num_qps, log_num_srqs, log_num_ees, log_num_cqs;
  1526. unsigned int log_num_mtts, log_num_mpts, log_num_rdbs, log_num_eqs;
  1527. int rc;
  1528. icm_offset = ( ( arbel->limits.reserved_uars + 1 ) << 12 );
  1529. /* Queue pair contexts */
  1530. log_num_qps = fls ( arbel->limits.reserved_qps + ARBEL_MAX_QPS - 1 );
  1531. MLX_FILL_2 ( init_hca, 13,
  1532. qpc_eec_cqc_eqc_rdb_parameters.qpc_base_addr_l,
  1533. ( icm_offset >> 7 ),
  1534. qpc_eec_cqc_eqc_rdb_parameters.log_num_of_qp,
  1535. log_num_qps );
  1536. DBGC ( arbel, "Arbel %p ICM QPC base = %zx\n", arbel, icm_offset );
  1537. icm_offset += icm_usage ( log_num_qps, arbel->limits.qpc_entry_size );
  1538. /* Extended queue pair contexts */
  1539. MLX_FILL_1 ( init_hca, 25,
  1540. qpc_eec_cqc_eqc_rdb_parameters.eqpc_base_addr_l,
  1541. icm_offset );
  1542. DBGC ( arbel, "Arbel %p ICM EQPC base = %zx\n", arbel, icm_offset );
  1543. // icm_offset += icm_usage ( log_num_qps, arbel->limits.eqpc_entry_size );
  1544. icm_offset += icm_usage ( log_num_qps, arbel->limits.qpc_entry_size );
  1545. /* Shared receive queue contexts */
  1546. log_num_srqs = fls ( arbel->limits.reserved_srqs - 1 );
  1547. MLX_FILL_2 ( init_hca, 19,
  1548. qpc_eec_cqc_eqc_rdb_parameters.srqc_base_addr_l,
  1549. ( icm_offset >> 5 ),
  1550. qpc_eec_cqc_eqc_rdb_parameters.log_num_of_srq,
  1551. log_num_srqs );
  1552. DBGC ( arbel, "Arbel %p ICM SRQC base = %zx\n", arbel, icm_offset );
  1553. icm_offset += icm_usage ( log_num_srqs, arbel->limits.srqc_entry_size );
  1554. /* End-to-end contexts */
  1555. log_num_ees = fls ( arbel->limits.reserved_ees - 1 );
  1556. MLX_FILL_2 ( init_hca, 17,
  1557. qpc_eec_cqc_eqc_rdb_parameters.eec_base_addr_l,
  1558. ( icm_offset >> 7 ),
  1559. qpc_eec_cqc_eqc_rdb_parameters.log_num_of_ee,
  1560. log_num_ees );
  1561. DBGC ( arbel, "Arbel %p ICM EEC base = %zx\n", arbel, icm_offset );
  1562. icm_offset += icm_usage ( log_num_ees, arbel->limits.eec_entry_size );
  1563. /* Extended end-to-end contexts */
  1564. MLX_FILL_1 ( init_hca, 29,
  1565. qpc_eec_cqc_eqc_rdb_parameters.eeec_base_addr_l,
  1566. icm_offset );
  1567. DBGC ( arbel, "Arbel %p ICM EEEC base = %zx\n", arbel, icm_offset );
  1568. icm_offset += icm_usage ( log_num_ees, arbel->limits.eeec_entry_size );
  1569. /* Completion queue contexts */
  1570. log_num_cqs = fls ( arbel->limits.reserved_cqs + ARBEL_MAX_CQS - 1 );
  1571. MLX_FILL_2 ( init_hca, 21,
  1572. qpc_eec_cqc_eqc_rdb_parameters.cqc_base_addr_l,
  1573. ( icm_offset >> 6 ),
  1574. qpc_eec_cqc_eqc_rdb_parameters.log_num_of_cq,
  1575. log_num_cqs );
  1576. DBGC ( arbel, "Arbel %p ICM CQC base = %zx\n", arbel, icm_offset );
  1577. icm_offset += icm_usage ( log_num_cqs, arbel->limits.cqc_entry_size );
  1578. /* Memory translation table */
  1579. log_num_mtts = fls ( arbel->limits.reserved_mtts - 1 );
  1580. MLX_FILL_1 ( init_hca, 65,
  1581. tpt_parameters.mtt_base_addr_l, icm_offset );
  1582. DBGC ( arbel, "Arbel %p ICM MTT base = %zx\n", arbel, icm_offset );
  1583. icm_offset += icm_usage ( log_num_mtts, arbel->limits.mtt_entry_size );
  1584. /* Memory protection table */
  1585. log_num_mpts = fls ( arbel->limits.reserved_mrws + 1 - 1 );
  1586. MLX_FILL_1 ( init_hca, 61,
  1587. tpt_parameters.mpt_base_adr_l, icm_offset );
  1588. MLX_FILL_1 ( init_hca, 62,
  1589. tpt_parameters.log_mpt_sz, log_num_mpts );
  1590. DBGC ( arbel, "Arbel %p ICM MTT base = %zx\n", arbel, icm_offset );
  1591. icm_offset += icm_usage ( log_num_mpts, arbel->limits.mpt_entry_size );
  1592. /* RDMA something or other */
  1593. log_num_rdbs = fls ( arbel->limits.reserved_rdbs - 1 );
  1594. MLX_FILL_1 ( init_hca, 37,
  1595. qpc_eec_cqc_eqc_rdb_parameters.rdb_base_addr_l,
  1596. icm_offset );
  1597. DBGC ( arbel, "Arbel %p ICM RDB base = %zx\n", arbel, icm_offset );
  1598. icm_offset += icm_usage ( log_num_rdbs, 32 );
  1599. /* Event queue contexts */
  1600. log_num_eqs = 6;
  1601. MLX_FILL_2 ( init_hca, 33,
  1602. qpc_eec_cqc_eqc_rdb_parameters.eqc_base_addr_l,
  1603. ( icm_offset >> 6 ),
  1604. qpc_eec_cqc_eqc_rdb_parameters.log_num_eq,
  1605. log_num_eqs );
  1606. DBGC ( arbel, "Arbel %p ICM EQ base = %zx\n", arbel, icm_offset );
  1607. icm_offset += ( ( 1 << log_num_eqs ) * arbel->limits.eqc_entry_size );
  1608. /* Multicast table */
  1609. MLX_FILL_1 ( init_hca, 49,
  1610. multicast_parameters.mc_base_addr_l, icm_offset );
  1611. MLX_FILL_1 ( init_hca, 52,
  1612. multicast_parameters.log_mc_table_entry_sz,
  1613. fls ( sizeof ( struct arbelprm_mgm_entry ) - 1 ) );
  1614. MLX_FILL_1 ( init_hca, 53,
  1615. multicast_parameters.mc_table_hash_sz, 8 );
  1616. MLX_FILL_1 ( init_hca, 54,
  1617. multicast_parameters.log_mc_table_sz, 3 );
  1618. DBGC ( arbel, "Arbel %p ICM MC base = %zx\n", arbel, icm_offset );
  1619. icm_offset += ( 8 * sizeof ( struct arbelprm_mgm_entry ) );
  1620. arbel->icm_len = icm_offset;
  1621. arbel->icm_len = ( ( arbel->icm_len + 4095 ) & ~4095 );
  1622. /* Get ICM auxiliary area size */
  1623. memset ( &icm_size, 0, sizeof ( icm_size ) );
  1624. MLX_FILL_1 ( &icm_size, 1, value, arbel->icm_len );
  1625. if ( ( rc = arbel_cmd_set_icm_size ( arbel, &icm_size,
  1626. &icm_aux_size ) ) != 0 ) {
  1627. DBGC ( arbel, "Arbel %p could not set ICM size: %s\n",
  1628. arbel, strerror ( rc ) );
  1629. goto err_set_icm_size;
  1630. }
  1631. arbel->icm_aux_len = ( MLX_GET ( &icm_aux_size, value ) * 4096 );
  1632. /* Allocate ICM data and auxiliary area */
  1633. DBGC ( arbel, "Arbel %p requires %zd kB ICM and %zd kB AUX ICM\n",
  1634. arbel, ( arbel->icm_len / 1024 ),
  1635. ( arbel->icm_aux_len / 1024 ) );
  1636. arbel->icm = umalloc ( arbel->icm_len + arbel->icm_aux_len );
  1637. if ( ! arbel->icm ) {
  1638. rc = -ENOMEM;
  1639. goto err_alloc;
  1640. }
  1641. /* Map ICM auxiliary area */
  1642. memset ( &map_icm_aux, 0, sizeof ( map_icm_aux ) );
  1643. MLX_FILL_2 ( &map_icm_aux, 3,
  1644. log2size, fls ( ( arbel->icm_aux_len / 4096 ) - 1 ),
  1645. pa_l,
  1646. ( user_to_phys ( arbel->icm, arbel->icm_len ) >> 12 ) );
  1647. if ( ( rc = arbel_cmd_map_icm_aux ( arbel, &map_icm_aux ) ) != 0 ) {
  1648. DBGC ( arbel, "Arbel %p could not map AUX ICM: %s\n",
  1649. arbel, strerror ( rc ) );
  1650. goto err_map_icm_aux;
  1651. }
  1652. /* MAP ICM area */
  1653. memset ( &map_icm, 0, sizeof ( map_icm ) );
  1654. MLX_FILL_2 ( &map_icm, 3,
  1655. log2size, fls ( ( arbel->icm_len / 4096 ) - 1 ),
  1656. pa_l, ( user_to_phys ( arbel->icm, 0 ) >> 12 ) );
  1657. if ( ( rc = arbel_cmd_map_icm ( arbel, &map_icm ) ) != 0 ) {
  1658. DBGC ( arbel, "Arbel %p could not map ICM: %s\n",
  1659. arbel, strerror ( rc ) );
  1660. goto err_map_icm;
  1661. }
  1662. return 0;
  1663. arbel_cmd_unmap_icm ( arbel, ( arbel->icm_len / 4096 ) );
  1664. err_map_icm:
  1665. arbel_cmd_unmap_icm_aux ( arbel );
  1666. err_map_icm_aux:
  1667. ufree ( arbel->icm );
  1668. arbel->icm = UNULL;
  1669. err_alloc:
  1670. err_set_icm_size:
  1671. return rc;
  1672. }
  1673. /**
  1674. * Free ICM
  1675. *
  1676. * @v arbel Arbel device
  1677. */
  1678. static void arbel_free_icm ( struct arbel *arbel ) {
  1679. arbel_cmd_unmap_icm ( arbel, ( arbel->icm_len / 4096 ) );
  1680. arbel_cmd_unmap_icm_aux ( arbel );
  1681. ufree ( arbel->icm );
  1682. arbel->icm = UNULL;
  1683. }
  1684. /***************************************************************************
  1685. *
  1686. * Infiniband link-layer operations
  1687. *
  1688. ***************************************************************************
  1689. */
  1690. /**
  1691. * Initialise Infiniband link
  1692. *
  1693. * @v arbel Arbel device
  1694. * @ret rc Return status code
  1695. */
  1696. static int arbel_init_ib ( struct arbel *arbel ) {
  1697. struct arbelprm_init_ib init_ib;
  1698. int rc;
  1699. memset ( &init_ib, 0, sizeof ( init_ib ) );
  1700. MLX_FILL_3 ( &init_ib, 0,
  1701. mtu_cap, ARBEL_MTU_2048,
  1702. port_width_cap, 3,
  1703. vl_cap, 1 );
  1704. MLX_FILL_1 ( &init_ib, 1, max_gid, 1 );
  1705. MLX_FILL_1 ( &init_ib, 2, max_pkey, 64 );
  1706. if ( ( rc = arbel_cmd_init_ib ( arbel, PXE_IB_PORT,
  1707. &init_ib ) ) != 0 ) {
  1708. DBGC ( arbel, "Arbel %p could not intialise IB: %s\n",
  1709. arbel, strerror ( rc ) );
  1710. return rc;
  1711. }
  1712. return 0;
  1713. }
  1714. /**
  1715. * Close Infiniband link
  1716. *
  1717. * @v arbel Arbel device
  1718. */
  1719. static void arbel_close_ib ( struct arbel *arbel ) {
  1720. int rc;
  1721. if ( ( rc = arbel_cmd_close_ib ( arbel, PXE_IB_PORT ) ) != 0 ) {
  1722. DBGC ( arbel, "Arbel %p could not close IB: %s\n",
  1723. arbel, strerror ( rc ) );
  1724. /* Nothing we can do about this */
  1725. }
  1726. }
  1727. /***************************************************************************
  1728. *
  1729. * PCI interface
  1730. *
  1731. ***************************************************************************
  1732. */
  1733. /**
  1734. * Set up memory protection table
  1735. *
  1736. * @v arbel Arbel device
  1737. * @ret rc Return status code
  1738. */
  1739. static int arbel_setup_mpt ( struct arbel *arbel ) {
  1740. struct arbelprm_mpt mpt;
  1741. uint32_t key;
  1742. int rc;
  1743. /* Derive key */
  1744. key = ( arbel->limits.reserved_mrws | ARBEL_MKEY_PREFIX );
  1745. arbel->reserved_lkey = ( ( key << 8 ) | ( key >> 24 ) );
  1746. /* Initialise memory protection table */
  1747. memset ( &mpt, 0, sizeof ( mpt ) );
  1748. MLX_FILL_4 ( &mpt, 0,
  1749. r_w, 1,
  1750. pa, 1,
  1751. lr, 1,
  1752. lw, 1 );
  1753. MLX_FILL_1 ( &mpt, 2, mem_key, key );
  1754. MLX_FILL_1 ( &mpt, 3, pd, ARBEL_GLOBAL_PD );
  1755. MLX_FILL_1 ( &mpt, 6, reg_wnd_len_h, 0xffffffffUL );
  1756. MLX_FILL_1 ( &mpt, 7, reg_wnd_len_l, 0xffffffffUL );
  1757. if ( ( rc = arbel_cmd_sw2hw_mpt ( arbel, arbel->limits.reserved_mrws,
  1758. &mpt ) ) != 0 ) {
  1759. DBGC ( arbel, "Arbel %p could not set up MPT: %s\n",
  1760. arbel, strerror ( rc ) );
  1761. return rc;
  1762. }
  1763. return 0;
  1764. }
  1765. /**
  1766. * Probe PCI device
  1767. *
  1768. * @v pci PCI device
  1769. * @v id PCI ID
  1770. * @ret rc Return status code
  1771. */
  1772. static int arbel_probe ( struct pci_device *pci,
  1773. const struct pci_device_id *id __unused ) {
  1774. struct ib_device *ibdev;
  1775. struct arbel *arbel;
  1776. struct arbelprm_init_hca init_hca;
  1777. int rc;
  1778. /* Allocate Infiniband device */
  1779. ibdev = alloc_ibdev ( sizeof ( *arbel ) );
  1780. if ( ! ibdev ) {
  1781. rc = -ENOMEM;
  1782. goto err_ibdev;
  1783. }
  1784. ibdev->op = &arbel_ib_operations;
  1785. pci_set_drvdata ( pci, ibdev );
  1786. ibdev->dev = &pci->dev;
  1787. arbel = ibdev->dev_priv;
  1788. memset ( arbel, 0, sizeof ( *arbel ) );
  1789. /* Fix up PCI device */
  1790. adjust_pci_device ( pci );
  1791. /* Get PCI BARs */
  1792. arbel->config = ioremap ( pci_bar_start ( pci, ARBEL_PCI_CONFIG_BAR ),
  1793. ARBEL_PCI_CONFIG_BAR_SIZE );
  1794. arbel->uar = ioremap ( ( pci_bar_start ( pci, ARBEL_PCI_UAR_BAR ) +
  1795. ARBEL_PCI_UAR_IDX * ARBEL_PCI_UAR_SIZE ),
  1796. ARBEL_PCI_UAR_SIZE );
  1797. /* Allocate space for mailboxes */
  1798. arbel->mailbox_in = malloc_dma ( ARBEL_MBOX_SIZE, ARBEL_MBOX_ALIGN );
  1799. if ( ! arbel->mailbox_in ) {
  1800. rc = -ENOMEM;
  1801. goto err_mailbox_in;
  1802. }
  1803. arbel->mailbox_out = malloc_dma ( ARBEL_MBOX_SIZE, ARBEL_MBOX_ALIGN );
  1804. if ( ! arbel->mailbox_out ) {
  1805. rc = -ENOMEM;
  1806. goto err_mailbox_out;
  1807. }
  1808. /* Start firmware */
  1809. if ( ( rc = arbel_start_firmware ( arbel ) ) != 0 )
  1810. goto err_start_firmware;
  1811. /* Get device limits */
  1812. if ( ( rc = arbel_get_limits ( arbel ) ) != 0 )
  1813. goto err_get_limits;
  1814. /* Allocate ICM */
  1815. memset ( &init_hca, 0, sizeof ( init_hca ) );
  1816. if ( ( rc = arbel_alloc_icm ( arbel, &init_hca ) ) != 0 )
  1817. goto err_alloc_icm;
  1818. unsigned long uar_offset = ( arbel->limits.reserved_uars * 4096 );
  1819. arbel->db_rec = phys_to_virt ( user_to_phys ( arbel->icm,
  1820. uar_offset ) );
  1821. memset ( arbel->db_rec, 0, 4096 );
  1822. union arbelprm_doorbell_record *db_rec;
  1823. db_rec = &arbel->db_rec[ARBEL_GROUP_SEPARATOR_DOORBELL];
  1824. MLX_FILL_1 ( &db_rec->qp, 1, res, ARBEL_UAR_RES_GROUP_SEP );
  1825. /* Initialise HCA */
  1826. MLX_FILL_1 ( &init_hca, 74, uar_parameters.log_max_uars, 1 );
  1827. if ( ( rc = arbel_cmd_init_hca ( arbel, &init_hca ) ) != 0 ) {
  1828. DBGC ( arbel, "Arbel %p could not initialise HCA: %s\n",
  1829. arbel, strerror ( rc ) );
  1830. goto err_init_hca;
  1831. }
  1832. /* Set up memory protection */
  1833. if ( ( rc = arbel_setup_mpt ( arbel ) ) != 0 )
  1834. goto err_setup_mpt;
  1835. /* Bring up IB layer */
  1836. if ( ( rc = arbel_init_ib ( arbel ) ) != 0 )
  1837. goto err_init_ib;
  1838. /* Wait for link */
  1839. if ( ( rc = arbel_wait_for_link ( arbel ) ) != 0 )
  1840. goto err_wait_for_link;
  1841. /* Get MAD parameters */
  1842. if ( ( rc = arbel_get_mad_params ( ibdev ) ) != 0 )
  1843. goto err_get_mad_params;
  1844. DBGC ( arbel, "Arbel %p port GID is %08lx:%08lx:%08lx:%08lx\n", arbel,
  1845. htonl ( ibdev->port_gid.u.dwords[0] ),
  1846. htonl ( ibdev->port_gid.u.dwords[1] ),
  1847. htonl ( ibdev->port_gid.u.dwords[2] ),
  1848. htonl ( ibdev->port_gid.u.dwords[3] ) );
  1849. /* Add IPoIB device */
  1850. if ( ( rc = ipoib_probe ( ibdev ) ) != 0 ) {
  1851. DBGC ( arbel, "Arbel %p could not add IPoIB device: %s\n",
  1852. arbel, strerror ( rc ) );
  1853. goto err_ipoib_probe;
  1854. }
  1855. return 0;
  1856. err_ipoib_probe:
  1857. err_get_mad_params:
  1858. err_wait_for_link:
  1859. arbel_close_ib ( arbel );
  1860. err_init_ib:
  1861. err_setup_mpt:
  1862. arbel_cmd_close_hca ( arbel );
  1863. err_init_hca:
  1864. arbel_free_icm ( arbel );
  1865. err_alloc_icm:
  1866. err_get_limits:
  1867. arbel_stop_firmware ( arbel );
  1868. err_start_firmware:
  1869. free_dma ( arbel->mailbox_out, ARBEL_MBOX_SIZE );
  1870. err_mailbox_out:
  1871. free_dma ( arbel->mailbox_in, ARBEL_MBOX_SIZE );
  1872. err_mailbox_in:
  1873. free_ibdev ( ibdev );
  1874. err_ibdev:
  1875. return rc;
  1876. }
  1877. /**
  1878. * Remove PCI device
  1879. *
  1880. * @v pci PCI device
  1881. */
  1882. static void arbel_remove ( struct pci_device *pci ) {
  1883. struct ib_device *ibdev = pci_get_drvdata ( pci );
  1884. struct arbel *arbel = ibdev->dev_priv;
  1885. ipoib_remove ( ibdev );
  1886. arbel_close_ib ( arbel );
  1887. arbel_cmd_close_hca ( arbel );
  1888. arbel_free_icm ( arbel );
  1889. arbel_stop_firmware ( arbel );
  1890. arbel_stop_firmware ( arbel );
  1891. free_dma ( arbel->mailbox_out, ARBEL_MBOX_SIZE );
  1892. free_dma ( arbel->mailbox_in, ARBEL_MBOX_SIZE );
  1893. free_ibdev ( ibdev );
  1894. }
  1895. static struct pci_device_id arbel_nics[] = {
  1896. PCI_ROM ( 0x15b3, 0x6282, "MT25218", "MT25218 HCA driver" ),
  1897. PCI_ROM ( 0x15b3, 0x6274, "MT25204", "MT25204 HCA driver" ),
  1898. };
  1899. struct pci_driver arbel_driver __pci_driver = {
  1900. .ids = arbel_nics,
  1901. .id_count = ( sizeof ( arbel_nics ) / sizeof ( arbel_nics[0] ) ),
  1902. .probe = arbel_probe,
  1903. .remove = arbel_remove,
  1904. };