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