您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

arbel.c 66KB

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