2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
4 * Based in part upon the original driver by Mellanox Technologies
5 * Ltd. Portions may be Copyright (c) Mellanox Technologies Ltd.
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.
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.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include <gpxe/malloc.h>
32 #include <gpxe/umalloc.h>
33 #include <gpxe/iobuf.h>
34 #include <gpxe/netdevice.h>
35 #include <gpxe/infiniband.h>
41 * Mellanox Arbel Infiniband HCA
45 /***************************************************************************
47 * Queue number allocation
49 ***************************************************************************
53 * Allocate queue number
55 * @v q_inuse Queue usage bitmask
56 * @v max_inuse Maximum number of in-use queues
57 * @ret qn_offset Free queue number offset, or negative error
59 static int arbel_alloc_qn_offset ( arbel_bitmask_t
*q_inuse
,
60 unsigned int max_inuse
) {
61 unsigned int qn_offset
= 0;
62 arbel_bitmask_t mask
= 1;
64 while ( qn_offset
< max_inuse
) {
65 if ( ( mask
& *q_inuse
) == 0 ) {
82 * @v q_inuse Queue usage bitmask
83 * @v qn_offset Queue number offset
85 static void arbel_free_qn_offset ( arbel_bitmask_t
*q_inuse
, int qn_offset
) {
88 mask
= ( 1 << ( qn_offset
% ( 8 * sizeof ( mask
) ) ) );
89 q_inuse
+= ( qn_offset
/ ( 8 * sizeof ( mask
) ) );
93 /***************************************************************************
97 ***************************************************************************
101 * Wait for Arbel command completion
103 * @v arbel Arbel device
104 * @ret rc Return status code
106 static int arbel_cmd_wait ( struct arbel
*arbel
,
107 struct arbelprm_hca_command_register
*hcr
) {
110 for ( wait
= ARBEL_HCR_MAX_WAIT_MS
; wait
; wait
-- ) {
112 readl ( arbel
->config
+ ARBEL_HCR_REG ( 6 ) );
113 if ( MLX_GET ( hcr
, go
) == 0 )
123 * @v arbel Arbel device
124 * @v command Command opcode, flags and input/output lengths
125 * @v op_mod Opcode modifier (0 if no modifier applicable)
126 * @v in Input parameters
127 * @v in_mod Input modifier (0 if no modifier applicable)
128 * @v out Output parameters
129 * @ret rc Return status code
131 static int arbel_cmd ( struct arbel
*arbel
, unsigned long command
,
132 unsigned int op_mod
, const void *in
,
133 unsigned int in_mod
, void *out
) {
134 struct arbelprm_hca_command_register hcr
;
135 unsigned int opcode
= ARBEL_HCR_OPCODE ( command
);
136 size_t in_len
= ARBEL_HCR_IN_LEN ( command
);
137 size_t out_len
= ARBEL_HCR_OUT_LEN ( command
);
144 assert ( in_len
<= ARBEL_MBOX_SIZE
);
145 assert ( out_len
<= ARBEL_MBOX_SIZE
);
147 DBGC2 ( arbel
, "Arbel %p command %02x in %zx%s out %zx%s\n",
148 arbel
, opcode
, in_len
,
149 ( ( command
& ARBEL_HCR_IN_MBOX
) ? "(mbox)" : "" ), out_len
,
150 ( ( command
& ARBEL_HCR_OUT_MBOX
) ? "(mbox)" : "" ) );
152 /* Check that HCR is free */
153 if ( ( rc
= arbel_cmd_wait ( arbel
, &hcr
) ) != 0 ) {
154 DBGC ( arbel
, "Arbel %p command interface locked\n", arbel
);
159 memset ( &hcr
, 0, sizeof ( hcr
) );
160 in_buffer
= &hcr
.u
.dwords
[0];
161 if ( in_len
&& ( command
& ARBEL_HCR_IN_MBOX
) ) {
162 in_buffer
= arbel
->mailbox_in
;
163 MLX_FILL_1 ( &hcr
, 1, in_param_l
, virt_to_bus ( in_buffer
) );
165 memcpy ( in_buffer
, in
, in_len
);
166 MLX_FILL_1 ( &hcr
, 2, input_modifier
, in_mod
);
167 out_buffer
= &hcr
.u
.dwords
[3];
168 if ( out_len
&& ( command
& ARBEL_HCR_OUT_MBOX
) ) {
169 out_buffer
= arbel
->mailbox_out
;
170 MLX_FILL_1 ( &hcr
, 4, out_param_l
,
171 virt_to_bus ( out_buffer
) );
173 MLX_FILL_3 ( &hcr
, 6,
175 opcode_modifier
, op_mod
,
177 DBGC2_HD ( arbel
, &hcr
, sizeof ( hcr
) );
179 DBGC2 ( arbel
, "Input:\n" );
180 DBGC2_HD ( arbel
, in
, ( ( in_len
< 512 ) ? in_len
: 512 ) );
184 for ( i
= 0 ; i
< ( sizeof ( hcr
) / sizeof ( hcr
.u
.dwords
[0] ) ) ;
186 writel ( hcr
.u
.dwords
[i
],
187 arbel
->config
+ ARBEL_HCR_REG ( i
) );
191 /* Wait for command completion */
192 if ( ( rc
= arbel_cmd_wait ( arbel
, &hcr
) ) != 0 ) {
193 DBGC ( arbel
, "Arbel %p timed out waiting for command:\n",
195 DBGC_HD ( arbel
, &hcr
, sizeof ( hcr
) );
199 /* Check command status */
200 status
= MLX_GET ( &hcr
, status
);
202 DBGC ( arbel
, "Arbel %p command failed with status %02x:\n",
204 DBGC_HD ( arbel
, &hcr
, sizeof ( hcr
) );
208 /* Read output parameters, if any */
209 hcr
.u
.dwords
[3] = readl ( arbel
->config
+ ARBEL_HCR_REG ( 3 ) );
210 hcr
.u
.dwords
[4] = readl ( arbel
->config
+ ARBEL_HCR_REG ( 4 ) );
211 memcpy ( out
, out_buffer
, out_len
);
213 DBGC2 ( arbel
, "Output:\n" );
214 DBGC2_HD ( arbel
, out
, ( ( out_len
< 512 ) ? out_len
: 512 ) );
221 arbel_cmd_query_dev_lim ( struct arbel
*arbel
,
222 struct arbelprm_query_dev_lim
*dev_lim
) {
223 return arbel_cmd ( arbel
,
224 ARBEL_HCR_OUT_CMD ( ARBEL_HCR_QUERY_DEV_LIM
,
225 1, sizeof ( *dev_lim
) ),
226 0, NULL
, 0, dev_lim
);
230 arbel_cmd_query_fw ( struct arbel
*arbel
, struct arbelprm_query_fw
*fw
) {
231 return arbel_cmd ( arbel
,
232 ARBEL_HCR_OUT_CMD ( ARBEL_HCR_QUERY_FW
,
238 arbel_cmd_init_hca ( struct arbel
*arbel
,
239 const struct arbelprm_init_hca
*init_hca
) {
240 return arbel_cmd ( arbel
,
241 ARBEL_HCR_IN_CMD ( ARBEL_HCR_INIT_HCA
,
242 1, sizeof ( *init_hca
) ),
243 0, init_hca
, 0, NULL
);
247 arbel_cmd_close_hca ( struct arbel
*arbel
) {
248 return arbel_cmd ( arbel
,
249 ARBEL_HCR_VOID_CMD ( ARBEL_HCR_CLOSE_HCA
),
254 arbel_cmd_init_ib ( struct arbel
*arbel
, unsigned int port
,
255 const struct arbelprm_init_ib
*init_ib
) {
256 return arbel_cmd ( arbel
,
257 ARBEL_HCR_IN_CMD ( ARBEL_HCR_INIT_IB
,
258 1, sizeof ( *init_ib
) ),
259 0, init_ib
, port
, NULL
);
263 arbel_cmd_close_ib ( struct arbel
*arbel
, unsigned int port
) {
264 return arbel_cmd ( arbel
,
265 ARBEL_HCR_VOID_CMD ( ARBEL_HCR_CLOSE_IB
),
266 0, NULL
, port
, NULL
);
270 arbel_cmd_sw2hw_mpt ( struct arbel
*arbel
, unsigned int index
,
271 const struct arbelprm_mpt
*mpt
) {
272 return arbel_cmd ( arbel
,
273 ARBEL_HCR_IN_CMD ( ARBEL_HCR_SW2HW_MPT
,
274 1, sizeof ( *mpt
) ),
275 0, mpt
, index
, NULL
);
279 arbel_cmd_map_eq ( struct arbel
*arbel
, unsigned long index_map
,
280 const struct arbelprm_event_mask
*mask
) {
281 return arbel_cmd ( arbel
,
282 ARBEL_HCR_IN_CMD ( ARBEL_HCR_MAP_EQ
,
283 0, sizeof ( *mask
) ),
284 0, mask
, index_map
, NULL
);
288 arbel_cmd_sw2hw_eq ( struct arbel
*arbel
, unsigned int index
,
289 const struct arbelprm_eqc
*eqctx
) {
290 return arbel_cmd ( arbel
,
291 ARBEL_HCR_IN_CMD ( ARBEL_HCR_SW2HW_EQ
,
292 1, sizeof ( *eqctx
) ),
293 0, eqctx
, index
, NULL
);
297 arbel_cmd_hw2sw_eq ( struct arbel
*arbel
, unsigned int index
,
298 struct arbelprm_eqc
*eqctx
) {
299 return arbel_cmd ( arbel
,
300 ARBEL_HCR_OUT_CMD ( ARBEL_HCR_HW2SW_EQ
,
301 1, sizeof ( *eqctx
) ),
302 1, NULL
, index
, eqctx
);
306 arbel_cmd_sw2hw_cq ( struct arbel
*arbel
, unsigned long cqn
,
307 const struct arbelprm_completion_queue_context
*cqctx
) {
308 return arbel_cmd ( arbel
,
309 ARBEL_HCR_IN_CMD ( ARBEL_HCR_SW2HW_CQ
,
310 1, sizeof ( *cqctx
) ),
311 0, cqctx
, cqn
, NULL
);
315 arbel_cmd_hw2sw_cq ( struct arbel
*arbel
, unsigned long cqn
,
316 struct arbelprm_completion_queue_context
*cqctx
) {
317 return arbel_cmd ( arbel
,
318 ARBEL_HCR_OUT_CMD ( ARBEL_HCR_HW2SW_CQ
,
319 1, sizeof ( *cqctx
) ),
320 0, NULL
, cqn
, cqctx
);
324 arbel_cmd_rst2init_qpee ( struct arbel
*arbel
, unsigned long qpn
,
325 const struct arbelprm_qp_ee_state_transitions
*ctx
){
326 return arbel_cmd ( arbel
,
327 ARBEL_HCR_IN_CMD ( ARBEL_HCR_RST2INIT_QPEE
,
328 1, sizeof ( *ctx
) ),
333 arbel_cmd_init2rtr_qpee ( struct arbel
*arbel
, unsigned long qpn
,
334 const struct arbelprm_qp_ee_state_transitions
*ctx
){
335 return arbel_cmd ( arbel
,
336 ARBEL_HCR_IN_CMD ( ARBEL_HCR_INIT2RTR_QPEE
,
337 1, sizeof ( *ctx
) ),
342 arbel_cmd_rtr2rts_qpee ( struct arbel
*arbel
, unsigned long qpn
,
343 const struct arbelprm_qp_ee_state_transitions
*ctx
) {
344 return arbel_cmd ( arbel
,
345 ARBEL_HCR_IN_CMD ( ARBEL_HCR_RTR2RTS_QPEE
,
346 1, sizeof ( *ctx
) ),
351 arbel_cmd_rts2rts_qp ( struct arbel
*arbel
, unsigned long qpn
,
352 const struct arbelprm_qp_ee_state_transitions
*ctx
) {
353 return arbel_cmd ( arbel
,
354 ARBEL_HCR_IN_CMD ( ARBEL_HCR_RTS2RTS_QPEE
,
355 1, sizeof ( *ctx
) ),
360 arbel_cmd_2rst_qpee ( struct arbel
*arbel
, unsigned long qpn
) {
361 return arbel_cmd ( arbel
,
362 ARBEL_HCR_VOID_CMD ( ARBEL_HCR_2RST_QPEE
),
363 0x03, NULL
, qpn
, NULL
);
367 arbel_cmd_mad_ifc ( struct arbel
*arbel
, unsigned int port
,
368 union arbelprm_mad
*mad
) {
369 return arbel_cmd ( arbel
,
370 ARBEL_HCR_INOUT_CMD ( ARBEL_HCR_MAD_IFC
,
372 1, sizeof ( *mad
) ),
373 0x03, mad
, port
, mad
);
377 arbel_cmd_read_mgm ( struct arbel
*arbel
, unsigned int index
,
378 struct arbelprm_mgm_entry
*mgm
) {
379 return arbel_cmd ( arbel
,
380 ARBEL_HCR_OUT_CMD ( ARBEL_HCR_READ_MGM
,
381 1, sizeof ( *mgm
) ),
382 0, NULL
, index
, mgm
);
386 arbel_cmd_write_mgm ( struct arbel
*arbel
, unsigned int index
,
387 const struct arbelprm_mgm_entry
*mgm
) {
388 return arbel_cmd ( arbel
,
389 ARBEL_HCR_IN_CMD ( ARBEL_HCR_WRITE_MGM
,
390 1, sizeof ( *mgm
) ),
391 0, mgm
, index
, NULL
);
395 arbel_cmd_mgid_hash ( struct arbel
*arbel
, const struct ib_gid
*gid
,
396 struct arbelprm_mgm_hash
*hash
) {
397 return arbel_cmd ( arbel
,
398 ARBEL_HCR_INOUT_CMD ( ARBEL_HCR_MGID_HASH
,
400 0, sizeof ( *hash
) ),
405 arbel_cmd_run_fw ( struct arbel
*arbel
) {
406 return arbel_cmd ( arbel
,
407 ARBEL_HCR_VOID_CMD ( ARBEL_HCR_RUN_FW
),
412 arbel_cmd_disable_lam ( struct arbel
*arbel
) {
413 return arbel_cmd ( arbel
,
414 ARBEL_HCR_VOID_CMD ( ARBEL_HCR_DISABLE_LAM
),
419 arbel_cmd_enable_lam ( struct arbel
*arbel
, struct arbelprm_access_lam
*lam
) {
420 return arbel_cmd ( arbel
,
421 ARBEL_HCR_OUT_CMD ( ARBEL_HCR_ENABLE_LAM
,
422 1, sizeof ( *lam
) ),
427 arbel_cmd_unmap_icm ( struct arbel
*arbel
, unsigned int page_count
) {
428 return arbel_cmd ( arbel
,
429 ARBEL_HCR_VOID_CMD ( ARBEL_HCR_UNMAP_ICM
),
430 0, NULL
, page_count
, NULL
);
434 arbel_cmd_map_icm ( struct arbel
*arbel
,
435 const struct arbelprm_virtual_physical_mapping
*map
) {
436 return arbel_cmd ( arbel
,
437 ARBEL_HCR_IN_CMD ( ARBEL_HCR_MAP_ICM
,
438 1, sizeof ( *map
) ),
443 arbel_cmd_unmap_icm_aux ( struct arbel
*arbel
) {
444 return arbel_cmd ( arbel
,
445 ARBEL_HCR_VOID_CMD ( ARBEL_HCR_UNMAP_ICM_AUX
),
450 arbel_cmd_map_icm_aux ( struct arbel
*arbel
,
451 const struct arbelprm_virtual_physical_mapping
*map
) {
452 return arbel_cmd ( arbel
,
453 ARBEL_HCR_IN_CMD ( ARBEL_HCR_MAP_ICM_AUX
,
454 1, sizeof ( *map
) ),
459 arbel_cmd_set_icm_size ( struct arbel
*arbel
,
460 const struct arbelprm_scalar_parameter
*icm_size
,
461 struct arbelprm_scalar_parameter
*icm_aux_size
) {
462 return arbel_cmd ( arbel
,
463 ARBEL_HCR_INOUT_CMD ( ARBEL_HCR_SET_ICM_SIZE
,
464 0, sizeof ( *icm_size
),
465 0, sizeof ( *icm_aux_size
) ),
466 0, icm_size
, 0, icm_aux_size
);
470 arbel_cmd_unmap_fa ( struct arbel
*arbel
) {
471 return arbel_cmd ( arbel
,
472 ARBEL_HCR_VOID_CMD ( ARBEL_HCR_UNMAP_FA
),
477 arbel_cmd_map_fa ( struct arbel
*arbel
,
478 const struct arbelprm_virtual_physical_mapping
*map
) {
479 return arbel_cmd ( arbel
,
480 ARBEL_HCR_IN_CMD ( ARBEL_HCR_MAP_FA
,
481 1, sizeof ( *map
) ),
485 /***************************************************************************
487 * Completion queue operations
489 ***************************************************************************
493 * Create completion queue
495 * @v ibdev Infiniband device
496 * @v cq Completion queue
497 * @ret rc Return status code
499 static int arbel_create_cq ( struct ib_device
*ibdev
,
500 struct ib_completion_queue
*cq
) {
501 struct arbel
*arbel
= ib_get_drvdata ( ibdev
);
502 struct arbel_completion_queue
*arbel_cq
;
503 struct arbelprm_completion_queue_context cqctx
;
504 struct arbelprm_cq_ci_db_record
*ci_db_rec
;
505 struct arbelprm_cq_arm_db_record
*arm_db_rec
;
510 /* Find a free completion queue number */
511 cqn_offset
= arbel_alloc_qn_offset ( arbel
->cq_inuse
, ARBEL_MAX_CQS
);
512 if ( cqn_offset
< 0 ) {
513 DBGC ( arbel
, "Arbel %p out of completion queues\n", arbel
);
517 cq
->cqn
= ( arbel
->limits
.reserved_cqs
+ cqn_offset
);
519 /* Allocate control structures */
520 arbel_cq
= zalloc ( sizeof ( *arbel_cq
) );
525 arbel_cq
->ci_doorbell_idx
= arbel_cq_ci_doorbell_idx ( cqn_offset
);
526 arbel_cq
->arm_doorbell_idx
= arbel_cq_arm_doorbell_idx ( cqn_offset
);
528 /* Allocate completion queue itself */
529 arbel_cq
->cqe_size
= ( cq
->num_cqes
* sizeof ( arbel_cq
->cqe
[0] ) );
530 arbel_cq
->cqe
= malloc_dma ( arbel_cq
->cqe_size
,
531 sizeof ( arbel_cq
->cqe
[0] ) );
532 if ( ! arbel_cq
->cqe
) {
536 memset ( arbel_cq
->cqe
, 0, arbel_cq
->cqe_size
);
537 for ( i
= 0 ; i
< cq
->num_cqes
; i
++ ) {
538 MLX_FILL_1 ( &arbel_cq
->cqe
[i
].normal
, 7, owner
, 1 );
542 /* Initialise doorbell records */
543 ci_db_rec
= &arbel
->db_rec
[arbel_cq
->ci_doorbell_idx
].cq_ci
;
544 MLX_FILL_1 ( ci_db_rec
, 0, counter
, 0 );
545 MLX_FILL_2 ( ci_db_rec
, 1,
546 res
, ARBEL_UAR_RES_CQ_CI
,
547 cq_number
, cq
->cqn
);
548 arm_db_rec
= &arbel
->db_rec
[arbel_cq
->arm_doorbell_idx
].cq_arm
;
549 MLX_FILL_1 ( arm_db_rec
, 0, counter
, 0 );
550 MLX_FILL_2 ( arm_db_rec
, 1,
551 res
, ARBEL_UAR_RES_CQ_ARM
,
552 cq_number
, cq
->cqn
);
554 /* Hand queue over to hardware */
555 memset ( &cqctx
, 0, sizeof ( cqctx
) );
556 MLX_FILL_1 ( &cqctx
, 0, st
, 0xa /* "Event fired" */ );
557 MLX_FILL_1 ( &cqctx
, 2, start_address_l
,
558 virt_to_bus ( arbel_cq
->cqe
) );
559 MLX_FILL_2 ( &cqctx
, 3,
560 usr_page
, arbel
->limits
.reserved_uars
,
561 log_cq_size
, fls ( cq
->num_cqes
- 1 ) );
562 MLX_FILL_1 ( &cqctx
, 5, c_eqn
, ARBEL_NO_EQ
);
563 MLX_FILL_1 ( &cqctx
, 6, pd
, ARBEL_GLOBAL_PD
);
564 MLX_FILL_1 ( &cqctx
, 7, l_key
, arbel
->reserved_lkey
);
565 MLX_FILL_1 ( &cqctx
, 12, cqn
, cq
->cqn
);
566 MLX_FILL_1 ( &cqctx
, 13,
567 cq_ci_db_record
, arbel_cq
->ci_doorbell_idx
);
568 MLX_FILL_1 ( &cqctx
, 14,
569 cq_state_db_record
, arbel_cq
->arm_doorbell_idx
);
570 if ( ( rc
= arbel_cmd_sw2hw_cq ( arbel
, cq
->cqn
, &cqctx
) ) != 0 ) {
571 DBGC ( arbel
, "Arbel %p SW2HW_CQ failed: %s\n",
572 arbel
, strerror ( rc
) );
576 DBGC ( arbel
, "Arbel %p CQN %#lx ring at [%p,%p)\n",
577 arbel
, cq
->cqn
, arbel_cq
->cqe
,
578 ( ( ( void * ) arbel_cq
->cqe
) + arbel_cq
->cqe_size
) );
579 ib_cq_set_drvdata ( cq
, arbel_cq
);
583 MLX_FILL_1 ( ci_db_rec
, 1, res
, ARBEL_UAR_RES_NONE
);
584 MLX_FILL_1 ( arm_db_rec
, 1, res
, ARBEL_UAR_RES_NONE
);
585 free_dma ( arbel_cq
->cqe
, arbel_cq
->cqe_size
);
589 arbel_free_qn_offset ( arbel
->cq_inuse
, cqn_offset
);
595 * Destroy completion queue
597 * @v ibdev Infiniband device
598 * @v cq Completion queue
600 static void arbel_destroy_cq ( struct ib_device
*ibdev
,
601 struct ib_completion_queue
*cq
) {
602 struct arbel
*arbel
= ib_get_drvdata ( ibdev
);
603 struct arbel_completion_queue
*arbel_cq
= ib_cq_get_drvdata ( cq
);
604 struct arbelprm_completion_queue_context cqctx
;
605 struct arbelprm_cq_ci_db_record
*ci_db_rec
;
606 struct arbelprm_cq_arm_db_record
*arm_db_rec
;
610 /* Take ownership back from hardware */
611 if ( ( rc
= arbel_cmd_hw2sw_cq ( arbel
, cq
->cqn
, &cqctx
) ) != 0 ) {
612 DBGC ( arbel
, "Arbel %p FATAL HW2SW_CQ failed on CQN %#lx: "
613 "%s\n", arbel
, cq
->cqn
, strerror ( rc
) );
614 /* Leak memory and return; at least we avoid corruption */
618 /* Clear doorbell records */
619 ci_db_rec
= &arbel
->db_rec
[arbel_cq
->ci_doorbell_idx
].cq_ci
;
620 arm_db_rec
= &arbel
->db_rec
[arbel_cq
->arm_doorbell_idx
].cq_arm
;
621 MLX_FILL_1 ( ci_db_rec
, 1, res
, ARBEL_UAR_RES_NONE
);
622 MLX_FILL_1 ( arm_db_rec
, 1, res
, ARBEL_UAR_RES_NONE
);
625 free_dma ( arbel_cq
->cqe
, arbel_cq
->cqe_size
);
628 /* Mark queue number as free */
629 cqn_offset
= ( cq
->cqn
- arbel
->limits
.reserved_cqs
);
630 arbel_free_qn_offset ( arbel
->cq_inuse
, cqn_offset
);
632 ib_cq_set_drvdata ( cq
, NULL
);
635 /***************************************************************************
637 * Queue pair operations
639 ***************************************************************************
643 * Create send work queue
645 * @v arbel_send_wq Send work queue
646 * @v num_wqes Number of work queue entries
647 * @ret rc Return status code
649 static int arbel_create_send_wq ( struct arbel_send_work_queue
*arbel_send_wq
,
650 unsigned int num_wqes
) {
651 struct arbelprm_ud_send_wqe
*wqe
;
652 struct arbelprm_ud_send_wqe
*next_wqe
;
653 unsigned int wqe_idx_mask
;
656 /* Allocate work queue */
657 arbel_send_wq
->wqe_size
= ( num_wqes
*
658 sizeof ( arbel_send_wq
->wqe
[0] ) );
659 arbel_send_wq
->wqe
= malloc_dma ( arbel_send_wq
->wqe_size
,
660 sizeof ( arbel_send_wq
->wqe
[0] ) );
661 if ( ! arbel_send_wq
->wqe
)
663 memset ( arbel_send_wq
->wqe
, 0, arbel_send_wq
->wqe_size
);
665 /* Link work queue entries */
666 wqe_idx_mask
= ( num_wqes
- 1 );
667 for ( i
= 0 ; i
< num_wqes
; i
++ ) {
668 wqe
= &arbel_send_wq
->wqe
[i
].ud
;
669 next_wqe
= &arbel_send_wq
->wqe
[ ( i
+ 1 ) & wqe_idx_mask
].ud
;
670 MLX_FILL_1 ( &wqe
->next
, 0, nda_31_6
,
671 ( virt_to_bus ( next_wqe
) >> 6 ) );
678 * Create receive work queue
680 * @v arbel_recv_wq Receive work queue
681 * @v num_wqes Number of work queue entries
682 * @ret rc Return status code
684 static int arbel_create_recv_wq ( struct arbel_recv_work_queue
*arbel_recv_wq
,
685 unsigned int num_wqes
) {
686 struct arbelprm_recv_wqe
*wqe
;
687 struct arbelprm_recv_wqe
*next_wqe
;
688 unsigned int wqe_idx_mask
;
693 /* Allocate work queue */
694 arbel_recv_wq
->wqe_size
= ( num_wqes
*
695 sizeof ( arbel_recv_wq
->wqe
[0] ) );
696 arbel_recv_wq
->wqe
= malloc_dma ( arbel_recv_wq
->wqe_size
,
697 sizeof ( arbel_recv_wq
->wqe
[0] ) );
698 if ( ! arbel_recv_wq
->wqe
)
700 memset ( arbel_recv_wq
->wqe
, 0, arbel_recv_wq
->wqe_size
);
702 /* Link work queue entries */
703 wqe_idx_mask
= ( num_wqes
- 1 );
704 nds
= ( ( offsetof ( typeof ( *wqe
), data
) +
705 sizeof ( wqe
->data
[0] ) ) >> 4 );
706 for ( i
= 0 ; i
< num_wqes
; i
++ ) {
707 wqe
= &arbel_recv_wq
->wqe
[i
].recv
;
708 next_wqe
= &arbel_recv_wq
->wqe
[( i
+ 1 ) & wqe_idx_mask
].recv
;
709 MLX_FILL_1 ( &wqe
->next
, 0, nda_31_6
,
710 ( virt_to_bus ( next_wqe
) >> 6 ) );
711 MLX_FILL_1 ( &wqe
->next
, 1, nds
, ( sizeof ( *wqe
) / 16 ) );
712 for ( j
= 0 ; ( ( ( void * ) &wqe
->data
[j
] ) <
713 ( ( void * ) ( wqe
+ 1 ) ) ) ; j
++ ) {
714 MLX_FILL_1 ( &wqe
->data
[j
], 1,
715 l_key
, ARBEL_INVALID_LKEY
);
725 * @v ibdev Infiniband device
727 * @ret rc Return status code
729 static int arbel_create_qp ( struct ib_device
*ibdev
,
730 struct ib_queue_pair
*qp
) {
731 struct arbel
*arbel
= ib_get_drvdata ( ibdev
);
732 struct arbel_queue_pair
*arbel_qp
;
733 struct arbelprm_qp_ee_state_transitions qpctx
;
734 struct arbelprm_qp_db_record
*send_db_rec
;
735 struct arbelprm_qp_db_record
*recv_db_rec
;
739 /* Find a free queue pair number */
740 qpn_offset
= arbel_alloc_qn_offset ( arbel
->qp_inuse
, ARBEL_MAX_QPS
);
741 if ( qpn_offset
< 0 ) {
742 DBGC ( arbel
, "Arbel %p out of queue pairs\n", arbel
);
746 qp
->qpn
= ( ARBEL_QPN_BASE
+ arbel
->limits
.reserved_qps
+ qpn_offset
);
748 /* Allocate control structures */
749 arbel_qp
= zalloc ( sizeof ( *arbel_qp
) );
754 arbel_qp
->send
.doorbell_idx
= arbel_send_doorbell_idx ( qpn_offset
);
755 arbel_qp
->recv
.doorbell_idx
= arbel_recv_doorbell_idx ( qpn_offset
);
757 /* Create send and receive work queues */
758 if ( ( rc
= arbel_create_send_wq ( &arbel_qp
->send
,
759 qp
->send
.num_wqes
) ) != 0 )
760 goto err_create_send_wq
;
761 if ( ( rc
= arbel_create_recv_wq ( &arbel_qp
->recv
,
762 qp
->recv
.num_wqes
) ) != 0 )
763 goto err_create_recv_wq
;
765 /* Initialise doorbell records */
766 send_db_rec
= &arbel
->db_rec
[arbel_qp
->send
.doorbell_idx
].qp
;
767 MLX_FILL_1 ( send_db_rec
, 0, counter
, 0 );
768 MLX_FILL_2 ( send_db_rec
, 1,
769 res
, ARBEL_UAR_RES_SQ
,
770 qp_number
, qp
->qpn
);
771 recv_db_rec
= &arbel
->db_rec
[arbel_qp
->recv
.doorbell_idx
].qp
;
772 MLX_FILL_1 ( recv_db_rec
, 0, counter
, 0 );
773 MLX_FILL_2 ( recv_db_rec
, 1,
774 res
, ARBEL_UAR_RES_RQ
,
775 qp_number
, qp
->qpn
);
777 /* Hand queue over to hardware */
778 memset ( &qpctx
, 0, sizeof ( qpctx
) );
779 MLX_FILL_3 ( &qpctx
, 2,
781 qpc_eec_data
.pm_state
, 0x03 /* Always 0x03 for UD */,
782 qpc_eec_data
.st
, ARBEL_ST_UD
);
783 MLX_FILL_6 ( &qpctx
, 4,
784 qpc_eec_data
.mtu
, ARBEL_MTU_2048
,
785 qpc_eec_data
.msg_max
, 11 /* 2^11 = 2048 */,
786 qpc_eec_data
.log_rq_size
, fls ( qp
->recv
.num_wqes
- 1 ),
787 qpc_eec_data
.log_rq_stride
,
788 ( fls ( sizeof ( arbel_qp
->recv
.wqe
[0] ) - 1 ) - 4 ),
789 qpc_eec_data
.log_sq_size
, fls ( qp
->send
.num_wqes
- 1 ),
790 qpc_eec_data
.log_sq_stride
,
791 ( fls ( sizeof ( arbel_qp
->send
.wqe
[0] ) - 1 ) - 4 ) );
792 MLX_FILL_1 ( &qpctx
, 5,
793 qpc_eec_data
.usr_page
, arbel
->limits
.reserved_uars
);
794 MLX_FILL_1 ( &qpctx
, 10, qpc_eec_data
.primary_address_path
.port_number
,
796 MLX_FILL_1 ( &qpctx
, 27, qpc_eec_data
.pd
, ARBEL_GLOBAL_PD
);
797 MLX_FILL_1 ( &qpctx
, 29, qpc_eec_data
.wqe_lkey
, arbel
->reserved_lkey
);
798 MLX_FILL_1 ( &qpctx
, 30, qpc_eec_data
.ssc
, 1 );
799 MLX_FILL_1 ( &qpctx
, 33, qpc_eec_data
.cqn_snd
, qp
->send
.cq
->cqn
);
800 MLX_FILL_1 ( &qpctx
, 34, qpc_eec_data
.snd_wqe_base_adr_l
,
801 ( virt_to_bus ( arbel_qp
->send
.wqe
) >> 6 ) );
802 MLX_FILL_1 ( &qpctx
, 35, qpc_eec_data
.snd_db_record_index
,
803 arbel_qp
->send
.doorbell_idx
);
804 MLX_FILL_1 ( &qpctx
, 38, qpc_eec_data
.rsc
, 1 );
805 MLX_FILL_1 ( &qpctx
, 41, qpc_eec_data
.cqn_rcv
, qp
->recv
.cq
->cqn
);
806 MLX_FILL_1 ( &qpctx
, 42, qpc_eec_data
.rcv_wqe_base_adr_l
,
807 ( virt_to_bus ( arbel_qp
->recv
.wqe
) >> 6 ) );
808 MLX_FILL_1 ( &qpctx
, 43, qpc_eec_data
.rcv_db_record_index
,
809 arbel_qp
->recv
.doorbell_idx
);
810 MLX_FILL_1 ( &qpctx
, 44, qpc_eec_data
.q_key
, qp
->qkey
);
811 if ( ( rc
= arbel_cmd_rst2init_qpee ( arbel
, qp
->qpn
, &qpctx
)) != 0 ){
812 DBGC ( arbel
, "Arbel %p RST2INIT_QPEE failed: %s\n",
813 arbel
, strerror ( rc
) );
814 goto err_rst2init_qpee
;
816 memset ( &qpctx
, 0, sizeof ( qpctx
) );
817 MLX_FILL_2 ( &qpctx
, 4,
818 qpc_eec_data
.mtu
, ARBEL_MTU_2048
,
819 qpc_eec_data
.msg_max
, 11 /* 2^11 = 2048 */ );
820 if ( ( rc
= arbel_cmd_init2rtr_qpee ( arbel
, qp
->qpn
, &qpctx
)) != 0 ){
821 DBGC ( arbel
, "Arbel %p INIT2RTR_QPEE failed: %s\n",
822 arbel
, strerror ( rc
) );
823 goto err_init2rtr_qpee
;
825 memset ( &qpctx
, 0, sizeof ( qpctx
) );
826 if ( ( rc
= arbel_cmd_rtr2rts_qpee ( arbel
, qp
->qpn
, &qpctx
) ) != 0 ){
827 DBGC ( arbel
, "Arbel %p RTR2RTS_QPEE failed: %s\n",
828 arbel
, strerror ( rc
) );
829 goto err_rtr2rts_qpee
;
832 DBGC ( arbel
, "Arbel %p QPN %#lx send ring at [%p,%p)\n",
833 arbel
, qp
->qpn
, arbel_qp
->send
.wqe
,
834 ( ( (void *) arbel_qp
->send
.wqe
) + arbel_qp
->send
.wqe_size
) );
835 DBGC ( arbel
, "Arbel %p QPN %#lx receive ring at [%p,%p)\n",
836 arbel
, qp
->qpn
, arbel_qp
->recv
.wqe
,
837 ( ( (void *) arbel_qp
->recv
.wqe
) + arbel_qp
->recv
.wqe_size
) );
838 ib_qp_set_drvdata ( qp
, arbel_qp
);
843 arbel_cmd_2rst_qpee ( arbel
, qp
->qpn
);
845 MLX_FILL_1 ( send_db_rec
, 1, res
, ARBEL_UAR_RES_NONE
);
846 MLX_FILL_1 ( recv_db_rec
, 1, res
, ARBEL_UAR_RES_NONE
);
847 free_dma ( arbel_qp
->recv
.wqe
, arbel_qp
->recv
.wqe_size
);
849 free_dma ( arbel_qp
->send
.wqe
, arbel_qp
->send
.wqe_size
);
853 arbel_free_qn_offset ( arbel
->qp_inuse
, qpn_offset
);
861 * @v ibdev Infiniband device
863 * @v mod_list Modification list
864 * @ret rc Return status code
866 static int arbel_modify_qp ( struct ib_device
*ibdev
,
867 struct ib_queue_pair
*qp
,
868 unsigned long mod_list
) {
869 struct arbel
*arbel
= ib_get_drvdata ( ibdev
);
870 struct arbelprm_qp_ee_state_transitions qpctx
;
871 unsigned long optparammask
= 0;
874 /* Construct optparammask */
875 if ( mod_list
& IB_MODIFY_QKEY
)
876 optparammask
|= ARBEL_QPEE_OPT_PARAM_QKEY
;
878 /* Issue RTS2RTS_QP */
879 memset ( &qpctx
, 0, sizeof ( qpctx
) );
880 MLX_FILL_1 ( &qpctx
, 0, opt_param_mask
, optparammask
);
881 MLX_FILL_1 ( &qpctx
, 44, qpc_eec_data
.q_key
, qp
->qkey
);
882 if ( ( rc
= arbel_cmd_rts2rts_qp ( arbel
, qp
->qpn
, &qpctx
) ) != 0 ){
883 DBGC ( arbel
, "Arbel %p RTS2RTS_QP failed: %s\n",
884 arbel
, strerror ( rc
) );
894 * @v ibdev Infiniband device
897 static void arbel_destroy_qp ( struct ib_device
*ibdev
,
898 struct ib_queue_pair
*qp
) {
899 struct arbel
*arbel
= ib_get_drvdata ( ibdev
);
900 struct arbel_queue_pair
*arbel_qp
= ib_qp_get_drvdata ( qp
);
901 struct arbelprm_qp_db_record
*send_db_rec
;
902 struct arbelprm_qp_db_record
*recv_db_rec
;
906 /* Take ownership back from hardware */
907 if ( ( rc
= arbel_cmd_2rst_qpee ( arbel
, qp
->qpn
) ) != 0 ) {
908 DBGC ( arbel
, "Arbel %p FATAL 2RST_QPEE failed on QPN %#lx: "
909 "%s\n", arbel
, qp
->qpn
, strerror ( rc
) );
910 /* Leak memory and return; at least we avoid corruption */
914 /* Clear doorbell records */
915 send_db_rec
= &arbel
->db_rec
[arbel_qp
->send
.doorbell_idx
].qp
;
916 recv_db_rec
= &arbel
->db_rec
[arbel_qp
->recv
.doorbell_idx
].qp
;
917 MLX_FILL_1 ( send_db_rec
, 1, res
, ARBEL_UAR_RES_NONE
);
918 MLX_FILL_1 ( recv_db_rec
, 1, res
, ARBEL_UAR_RES_NONE
);
921 free_dma ( arbel_qp
->send
.wqe
, arbel_qp
->send
.wqe_size
);
922 free_dma ( arbel_qp
->recv
.wqe
, arbel_qp
->recv
.wqe_size
);
925 /* Mark queue number as free */
926 qpn_offset
= ( qp
->qpn
- ARBEL_QPN_BASE
- arbel
->limits
.reserved_qps
);
927 arbel_free_qn_offset ( arbel
->qp_inuse
, qpn_offset
);
929 ib_qp_set_drvdata ( qp
, NULL
);
932 /***************************************************************************
934 * Work request operations
936 ***************************************************************************
940 * Ring doorbell register in UAR
942 * @v arbel Arbel device
943 * @v db_reg Doorbell register structure
944 * @v offset Address of doorbell
946 static void arbel_ring_doorbell ( struct arbel
*arbel
,
947 union arbelprm_doorbell_register
*db_reg
,
948 unsigned int offset
) {
950 DBGC2 ( arbel
, "Arbel %p ringing doorbell %08lx:%08lx at %lx\n",
951 arbel
, db_reg
->dword
[0], db_reg
->dword
[1],
952 virt_to_phys ( arbel
->uar
+ offset
) );
955 writel ( db_reg
->dword
[0], ( arbel
->uar
+ offset
+ 0 ) );
957 writel ( db_reg
->dword
[1], ( arbel
->uar
+ offset
+ 4 ) );
960 /** GID used for GID-less send work queue entries */
961 static const struct ib_gid arbel_no_gid
= {
962 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0 } }
966 * Post send work queue entry
968 * @v ibdev Infiniband device
970 * @v av Address vector
971 * @v iobuf I/O buffer
972 * @ret rc Return status code
974 static int arbel_post_send ( struct ib_device
*ibdev
,
975 struct ib_queue_pair
*qp
,
976 struct ib_address_vector
*av
,
977 struct io_buffer
*iobuf
) {
978 struct arbel
*arbel
= ib_get_drvdata ( ibdev
);
979 struct arbel_queue_pair
*arbel_qp
= ib_qp_get_drvdata ( qp
);
980 struct ib_work_queue
*wq
= &qp
->send
;
981 struct arbel_send_work_queue
*arbel_send_wq
= &arbel_qp
->send
;
982 struct arbelprm_ud_send_wqe
*prev_wqe
;
983 struct arbelprm_ud_send_wqe
*wqe
;
984 struct arbelprm_qp_db_record
*qp_db_rec
;
985 union arbelprm_doorbell_register db_reg
;
986 const struct ib_gid
*gid
;
987 unsigned int wqe_idx_mask
;
990 /* Allocate work queue entry */
991 wqe_idx_mask
= ( wq
->num_wqes
- 1 );
992 if ( wq
->iobufs
[wq
->next_idx
& wqe_idx_mask
] ) {
993 DBGC ( arbel
, "Arbel %p send queue full", arbel
);
996 wq
->iobufs
[wq
->next_idx
& wqe_idx_mask
] = iobuf
;
997 prev_wqe
= &arbel_send_wq
->wqe
[(wq
->next_idx
- 1) & wqe_idx_mask
].ud
;
998 wqe
= &arbel_send_wq
->wqe
[wq
->next_idx
& wqe_idx_mask
].ud
;
1000 /* Construct work queue entry */
1001 MLX_FILL_1 ( &wqe
->next
, 1, always1
, 1 );
1002 memset ( &wqe
->ctrl
, 0, sizeof ( wqe
->ctrl
) );
1003 MLX_FILL_1 ( &wqe
->ctrl
, 0, always1
, 1 );
1004 memset ( &wqe
->ud
, 0, sizeof ( wqe
->ud
) );
1005 MLX_FILL_2 ( &wqe
->ud
, 0,
1006 ud_address_vector
.pd
, ARBEL_GLOBAL_PD
,
1007 ud_address_vector
.port_number
, ibdev
->port
);
1008 MLX_FILL_2 ( &wqe
->ud
, 1,
1009 ud_address_vector
.rlid
, av
->dlid
,
1010 ud_address_vector
.g
, av
->gid_present
);
1011 MLX_FILL_2 ( &wqe
->ud
, 2,
1012 ud_address_vector
.max_stat_rate
,
1013 ( ( av
->rate
>= 3 ) ? 0 : 1 ),
1014 ud_address_vector
.msg
, 3 );
1015 MLX_FILL_1 ( &wqe
->ud
, 3, ud_address_vector
.sl
, av
->sl
);
1016 gid
= ( av
->gid_present
? &av
->gid
: &arbel_no_gid
);
1017 memcpy ( &wqe
->ud
.u
.dwords
[4], gid
, sizeof ( *gid
) );
1018 MLX_FILL_1 ( &wqe
->ud
, 8, destination_qp
, av
->dest_qp
);
1019 MLX_FILL_1 ( &wqe
->ud
, 9, q_key
, av
->qkey
);
1020 MLX_FILL_1 ( &wqe
->data
[0], 0, byte_count
, iob_len ( iobuf
) );
1021 MLX_FILL_1 ( &wqe
->data
[0], 1, l_key
, arbel
->reserved_lkey
);
1022 MLX_FILL_1 ( &wqe
->data
[0], 3,
1023 local_address_l
, virt_to_bus ( iobuf
->data
) );
1025 /* Update previous work queue entry's "next" field */
1026 nds
= ( ( offsetof ( typeof ( *wqe
), data
) +
1027 sizeof ( wqe
->data
[0] ) ) >> 4 );
1028 MLX_SET ( &prev_wqe
->next
, nopcode
, ARBEL_OPCODE_SEND
);
1029 MLX_FILL_3 ( &prev_wqe
->next
, 1,
1034 /* Update doorbell record */
1036 qp_db_rec
= &arbel
->db_rec
[arbel_send_wq
->doorbell_idx
].qp
;
1037 MLX_FILL_1 ( qp_db_rec
, 0,
1038 counter
, ( ( wq
->next_idx
+ 1 ) & 0xffff ) );
1040 /* Ring doorbell register */
1041 MLX_FILL_4 ( &db_reg
.send
, 0,
1042 nopcode
, ARBEL_OPCODE_SEND
,
1044 wqe_counter
, ( wq
->next_idx
& 0xffff ),
1046 MLX_FILL_2 ( &db_reg
.send
, 1,
1049 arbel_ring_doorbell ( arbel
, &db_reg
, ARBEL_DB_POST_SND_OFFSET
);
1051 /* Update work queue's index */
1058 * Post receive work queue entry
1060 * @v ibdev Infiniband device
1062 * @v iobuf I/O buffer
1063 * @ret rc Return status code
1065 static int arbel_post_recv ( struct ib_device
*ibdev
,
1066 struct ib_queue_pair
*qp
,
1067 struct io_buffer
*iobuf
) {
1068 struct arbel
*arbel
= ib_get_drvdata ( ibdev
);
1069 struct arbel_queue_pair
*arbel_qp
= ib_qp_get_drvdata ( qp
);
1070 struct ib_work_queue
*wq
= &qp
->recv
;
1071 struct arbel_recv_work_queue
*arbel_recv_wq
= &arbel_qp
->recv
;
1072 struct arbelprm_recv_wqe
*wqe
;
1073 union arbelprm_doorbell_record
*db_rec
;
1074 unsigned int wqe_idx_mask
;
1076 /* Allocate work queue entry */
1077 wqe_idx_mask
= ( wq
->num_wqes
- 1 );
1078 if ( wq
->iobufs
[wq
->next_idx
& wqe_idx_mask
] ) {
1079 DBGC ( arbel
, "Arbel %p receive queue full", arbel
);
1082 wq
->iobufs
[wq
->next_idx
& wqe_idx_mask
] = iobuf
;
1083 wqe
= &arbel_recv_wq
->wqe
[wq
->next_idx
& wqe_idx_mask
].recv
;
1085 /* Construct work queue entry */
1086 MLX_FILL_1 ( &wqe
->data
[0], 0, byte_count
, iob_tailroom ( iobuf
) );
1087 MLX_FILL_1 ( &wqe
->data
[0], 1, l_key
, arbel
->reserved_lkey
);
1088 MLX_FILL_1 ( &wqe
->data
[0], 3,
1089 local_address_l
, virt_to_bus ( iobuf
->data
) );
1091 /* Update doorbell record */
1093 db_rec
= &arbel
->db_rec
[arbel_recv_wq
->doorbell_idx
];
1094 MLX_FILL_1 ( &db_rec
->qp
, 0,
1095 counter
, ( ( wq
->next_idx
+ 1 ) & 0xffff ) );
1097 /* Update work queue's index */
1106 * @v ibdev Infiniband device
1107 * @v cq Completion queue
1108 * @v cqe Hardware completion queue entry
1109 * @v complete_send Send completion handler
1110 * @v complete_recv Receive completion handler
1111 * @ret rc Return status code
1113 static int arbel_complete ( struct ib_device
*ibdev
,
1114 struct ib_completion_queue
*cq
,
1115 union arbelprm_completion_entry
*cqe
,
1116 ib_completer_t complete_send
,
1117 ib_completer_t complete_recv
) {
1118 struct arbel
*arbel
= ib_get_drvdata ( ibdev
);
1119 struct ib_completion completion
;
1120 struct ib_work_queue
*wq
;
1121 struct ib_queue_pair
*qp
;
1122 struct arbel_queue_pair
*arbel_qp
;
1123 struct arbel_send_work_queue
*arbel_send_wq
;
1124 struct arbel_recv_work_queue
*arbel_recv_wq
;
1125 struct arbelprm_recv_wqe
*recv_wqe
;
1126 struct io_buffer
*iobuf
;
1127 ib_completer_t complete
;
1128 unsigned int opcode
;
1131 unsigned long wqe_adr
;
1132 unsigned int wqe_idx
;
1135 /* Parse completion */
1136 memset ( &completion
, 0, sizeof ( completion
) );
1137 qpn
= MLX_GET ( &cqe
->normal
, my_qpn
);
1138 is_send
= MLX_GET ( &cqe
->normal
, s
);
1139 wqe_adr
= ( MLX_GET ( &cqe
->normal
, wqe_adr
) << 6 );
1140 opcode
= MLX_GET ( &cqe
->normal
, opcode
);
1141 if ( opcode
>= ARBEL_OPCODE_RECV_ERROR
) {
1142 /* "s" field is not valid for error opcodes */
1143 is_send
= ( opcode
== ARBEL_OPCODE_SEND_ERROR
);
1144 completion
.syndrome
= MLX_GET ( &cqe
->error
, syndrome
);
1145 DBGC ( arbel
, "Arbel %p CPN %lx syndrome %x vendor %lx\n",
1146 arbel
, cq
->cqn
, completion
.syndrome
,
1147 MLX_GET ( &cqe
->error
, vendor_code
) );
1149 /* Don't return immediately; propagate error to completer */
1152 /* Identify work queue */
1153 wq
= ib_find_wq ( cq
, qpn
, is_send
);
1155 DBGC ( arbel
, "Arbel %p CQN %lx unknown %s QPN %lx\n",
1156 arbel
, cq
->cqn
, ( is_send
? "send" : "recv" ), qpn
);
1160 arbel_qp
= ib_qp_get_drvdata ( qp
);
1161 arbel_send_wq
= &arbel_qp
->send
;
1162 arbel_recv_wq
= &arbel_qp
->recv
;
1164 /* Identify work queue entry index */
1166 wqe_idx
= ( ( wqe_adr
- virt_to_bus ( arbel_send_wq
->wqe
) ) /
1167 sizeof ( arbel_send_wq
->wqe
[0] ) );
1168 assert ( wqe_idx
< qp
->send
.num_wqes
);
1170 wqe_idx
= ( ( wqe_adr
- virt_to_bus ( arbel_recv_wq
->wqe
) ) /
1171 sizeof ( arbel_recv_wq
->wqe
[0] ) );
1172 assert ( wqe_idx
< qp
->recv
.num_wqes
);
1175 /* Identify I/O buffer */
1176 iobuf
= wq
->iobufs
[wqe_idx
];
1178 DBGC ( arbel
, "Arbel %p CQN %lx QPN %lx empty WQE %x\n",
1179 arbel
, cq
->cqn
, qpn
, wqe_idx
);
1182 wq
->iobufs
[wqe_idx
] = NULL
;
1184 /* Fill in length for received packets */
1186 completion
.len
= MLX_GET ( &cqe
->normal
, byte_cnt
);
1187 recv_wqe
= &arbel_recv_wq
->wqe
[wqe_idx
].recv
;
1188 assert ( MLX_GET ( &recv_wqe
->data
[0], local_address_l
) ==
1189 virt_to_bus ( iobuf
->data
) );
1190 assert ( MLX_GET ( &recv_wqe
->data
[0], byte_count
) ==
1191 iob_tailroom ( iobuf
) );
1192 MLX_FILL_1 ( &recv_wqe
->data
[0], 0, byte_count
, 0 );
1193 MLX_FILL_1 ( &recv_wqe
->data
[0], 1,
1194 l_key
, ARBEL_INVALID_LKEY
);
1195 if ( completion
.len
> iob_tailroom ( iobuf
) ) {
1196 DBGC ( arbel
, "Arbel %p CQN %lx QPN %lx IDX %x "
1197 "overlength received packet length %zd\n",
1198 arbel
, cq
->cqn
, qpn
, wqe_idx
, completion
.len
);
1203 /* Pass off to caller's completion handler */
1204 complete
= ( is_send
? complete_send
: complete_recv
);
1205 complete ( ibdev
, qp
, &completion
, iobuf
);
1211 * Poll completion queue
1213 * @v ibdev Infiniband device
1214 * @v cq Completion queue
1215 * @v complete_send Send completion handler
1216 * @v complete_recv Receive completion handler
1218 static void arbel_poll_cq ( struct ib_device
*ibdev
,
1219 struct ib_completion_queue
*cq
,
1220 ib_completer_t complete_send
,
1221 ib_completer_t complete_recv
) {
1222 struct arbel
*arbel
= ib_get_drvdata ( ibdev
);
1223 struct arbel_completion_queue
*arbel_cq
= ib_cq_get_drvdata ( cq
);
1224 struct arbelprm_cq_ci_db_record
*ci_db_rec
;
1225 union arbelprm_completion_entry
*cqe
;
1226 unsigned int cqe_idx_mask
;
1230 /* Look for completion entry */
1231 cqe_idx_mask
= ( cq
->num_cqes
- 1 );
1232 cqe
= &arbel_cq
->cqe
[cq
->next_idx
& cqe_idx_mask
];
1233 if ( MLX_GET ( &cqe
->normal
, owner
) != 0 ) {
1234 /* Entry still owned by hardware; end of poll */
1238 /* Handle completion */
1239 if ( ( rc
= arbel_complete ( ibdev
, cq
, cqe
, complete_send
,
1240 complete_recv
) ) != 0 ) {
1241 DBGC ( arbel
, "Arbel %p failed to complete: %s\n",
1242 arbel
, strerror ( rc
) );
1243 DBGC_HD ( arbel
, cqe
, sizeof ( *cqe
) );
1246 /* Return ownership to hardware */
1247 MLX_FILL_1 ( &cqe
->normal
, 7, owner
, 1 );
1249 /* Update completion queue's index */
1251 /* Update doorbell record */
1252 ci_db_rec
= &arbel
->db_rec
[arbel_cq
->ci_doorbell_idx
].cq_ci
;
1253 MLX_FILL_1 ( ci_db_rec
, 0,
1254 counter
, ( cq
->next_idx
& 0xffffffffUL
) );
1258 /***************************************************************************
1262 ***************************************************************************
1266 * Create event queue
1268 * @v arbel Arbel device
1269 * @ret rc Return status code
1271 static int arbel_create_eq ( struct arbel
*arbel
) {
1272 struct arbel_event_queue
*arbel_eq
= &arbel
->eq
;
1273 struct arbelprm_eqc eqctx
;
1274 struct arbelprm_event_mask mask
;
1278 /* Select event queue number */
1279 arbel_eq
->eqn
= arbel
->limits
.reserved_eqs
;
1281 /* Calculate doorbell address */
1282 arbel_eq
->doorbell
= ( arbel
->eq_ci_doorbells
+
1283 ARBEL_DB_EQ_OFFSET ( arbel_eq
->eqn
) );
1285 /* Allocate event queue itself */
1286 arbel_eq
->eqe_size
=
1287 ( ARBEL_NUM_EQES
* sizeof ( arbel_eq
->eqe
[0] ) );
1288 arbel_eq
->eqe
= malloc_dma ( arbel_eq
->eqe_size
,
1289 sizeof ( arbel_eq
->eqe
[0] ) );
1290 if ( ! arbel_eq
->eqe
) {
1294 memset ( arbel_eq
->eqe
, 0, arbel_eq
->eqe_size
);
1295 for ( i
= 0 ; i
< ARBEL_NUM_EQES
; i
++ ) {
1296 MLX_FILL_1 ( &arbel_eq
->eqe
[i
].generic
, 7, owner
, 1 );
1300 /* Hand queue over to hardware */
1301 memset ( &eqctx
, 0, sizeof ( eqctx
) );
1302 MLX_FILL_1 ( &eqctx
, 0, st
, 0xa /* "Fired" */ );
1303 MLX_FILL_1 ( &eqctx
, 2,
1304 start_address_l
, virt_to_phys ( arbel_eq
->eqe
) );
1305 MLX_FILL_1 ( &eqctx
, 3, log_eq_size
, fls ( ARBEL_NUM_EQES
- 1 ) );
1306 MLX_FILL_1 ( &eqctx
, 6, pd
, ARBEL_GLOBAL_PD
);
1307 MLX_FILL_1 ( &eqctx
, 7, lkey
, arbel
->reserved_lkey
);
1308 if ( ( rc
= arbel_cmd_sw2hw_eq ( arbel
, arbel_eq
->eqn
,
1310 DBGC ( arbel
, "Arbel %p SW2HW_EQ failed: %s\n",
1311 arbel
, strerror ( rc
) );
1315 /* Map events to this event queue */
1316 memset ( &mask
, 0, sizeof ( mask
) );
1317 MLX_FILL_1 ( &mask
, 1, port_state_change
, 1 );
1318 if ( ( rc
= arbel_cmd_map_eq ( arbel
,
1319 ( ARBEL_MAP_EQ
| arbel_eq
->eqn
),
1321 DBGC ( arbel
, "Arbel %p MAP_EQ failed: %s\n",
1322 arbel
, strerror ( rc
) );
1326 DBGC ( arbel
, "Arbel %p EQN %#lx ring at [%p,%p])\n",
1327 arbel
, arbel_eq
->eqn
, arbel_eq
->eqe
,
1328 ( ( ( void * ) arbel_eq
->eqe
) + arbel_eq
->eqe_size
) );
1332 arbel_cmd_hw2sw_eq ( arbel
, arbel_eq
->eqn
, &eqctx
);
1334 free_dma ( arbel_eq
->eqe
, arbel_eq
->eqe_size
);
1336 memset ( arbel_eq
, 0, sizeof ( *arbel_eq
) );
1341 * Destroy event queue
1343 * @v arbel Arbel device
1345 static void arbel_destroy_eq ( struct arbel
*arbel
) {
1346 struct arbel_event_queue
*arbel_eq
= &arbel
->eq
;
1347 struct arbelprm_eqc eqctx
;
1348 struct arbelprm_event_mask mask
;
1351 /* Unmap events from event queue */
1352 memset ( &mask
, 0, sizeof ( mask
) );
1353 MLX_FILL_1 ( &mask
, 1, port_state_change
, 1 );
1354 if ( ( rc
= arbel_cmd_map_eq ( arbel
,
1355 ( ARBEL_UNMAP_EQ
| arbel_eq
->eqn
),
1357 DBGC ( arbel
, "Arbel %p FATAL MAP_EQ failed to unmap: %s\n",
1358 arbel
, strerror ( rc
) );
1359 /* Continue; HCA may die but system should survive */
1362 /* Take ownership back from hardware */
1363 if ( ( rc
= arbel_cmd_hw2sw_eq ( arbel
, arbel_eq
->eqn
,
1365 DBGC ( arbel
, "Arbel %p FATAL HW2SW_EQ failed: %s\n",
1366 arbel
, strerror ( rc
) );
1367 /* Leak memory and return; at least we avoid corruption */
1372 free_dma ( arbel_eq
->eqe
, arbel_eq
->eqe_size
);
1373 memset ( arbel_eq
, 0, sizeof ( *arbel_eq
) );
1377 * Handle port state event
1379 * @v arbel Arbel device
1380 * @v eqe Port state change event queue entry
1382 static void arbel_event_port_state_change ( struct arbel
*arbel
,
1383 union arbelprm_event_entry
*eqe
){
1387 /* Get port and link status */
1388 port
= ( MLX_GET ( &eqe
->port_state_change
, data
.p
) - 1 );
1389 link_up
= ( MLX_GET ( &eqe
->generic
, event_sub_type
) & 0x04 );
1390 DBGC ( arbel
, "Arbel %p port %d link %s\n", arbel
, ( port
+ 1 ),
1391 ( link_up
? "up" : "down" ) );
1394 if ( port
>= ARBEL_NUM_PORTS
) {
1395 DBGC ( arbel
, "Arbel %p port %d does not exist!\n",
1396 arbel
, ( port
+ 1 ) );
1400 /* Notify Infiniband core of link state change */
1401 ib_link_state_changed ( arbel
->ibdev
[port
] );
1407 * @v ibdev Infiniband device
1409 static void arbel_poll_eq ( struct ib_device
*ibdev
) {
1410 struct arbel
*arbel
= ib_get_drvdata ( ibdev
);
1411 struct arbel_event_queue
*arbel_eq
= &arbel
->eq
;
1412 union arbelprm_event_entry
*eqe
;
1413 union arbelprm_eq_doorbell_register db_reg
;
1414 unsigned int eqe_idx_mask
;
1415 unsigned int event_type
;
1418 /* Look for event entry */
1419 eqe_idx_mask
= ( ARBEL_NUM_EQES
- 1 );
1420 eqe
= &arbel_eq
->eqe
[arbel_eq
->next_idx
& eqe_idx_mask
];
1421 if ( MLX_GET ( &eqe
->generic
, owner
) != 0 ) {
1422 /* Entry still owned by hardware; end of poll */
1425 DBGCP ( arbel
, "Arbel %p event:\n", arbel
);
1426 DBGCP_HD ( arbel
, eqe
, sizeof ( *eqe
) );
1429 event_type
= MLX_GET ( &eqe
->generic
, event_type
);
1430 switch ( event_type
) {
1431 case ARBEL_EV_PORT_STATE_CHANGE
:
1432 arbel_event_port_state_change ( arbel
, eqe
);
1435 DBGC ( arbel
, "Arbel %p unrecognised event type "
1436 "%#x:\n", arbel
, event_type
);
1437 DBGC_HD ( arbel
, eqe
, sizeof ( *eqe
) );
1441 /* Return ownership to hardware */
1442 MLX_FILL_1 ( &eqe
->generic
, 7, owner
, 1 );
1445 /* Update event queue's index */
1446 arbel_eq
->next_idx
++;
1449 MLX_FILL_1 ( &db_reg
.ci
, 0, ci
, arbel_eq
->next_idx
);
1450 DBGCP ( arbel
, "Ringing doorbell %08lx with %08lx\n",
1451 virt_to_phys ( arbel_eq
->doorbell
),
1453 writel ( db_reg
.dword
[0], arbel_eq
->doorbell
);
1457 /***************************************************************************
1459 * Infiniband link-layer operations
1461 ***************************************************************************
1465 * Initialise Infiniband link
1467 * @v ibdev Infiniband device
1468 * @ret rc Return status code
1470 static int arbel_open ( struct ib_device
*ibdev
) {
1471 struct arbel
*arbel
= ib_get_drvdata ( ibdev
);
1472 struct arbelprm_init_ib init_ib
;
1475 memset ( &init_ib
, 0, sizeof ( init_ib
) );
1476 MLX_FILL_3 ( &init_ib
, 0,
1477 mtu_cap
, ARBEL_MTU_2048
,
1480 MLX_FILL_1 ( &init_ib
, 1, max_gid
, 1 );
1481 MLX_FILL_1 ( &init_ib
, 2, max_pkey
, 64 );
1482 if ( ( rc
= arbel_cmd_init_ib ( arbel
, ibdev
->port
,
1483 &init_ib
) ) != 0 ) {
1484 DBGC ( arbel
, "Arbel %p could not intialise IB: %s\n",
1485 arbel
, strerror ( rc
) );
1493 * Close Infiniband link
1495 * @v ibdev Infiniband device
1497 static void arbel_close ( struct ib_device
*ibdev
) {
1498 struct arbel
*arbel
= ib_get_drvdata ( ibdev
);
1501 if ( ( rc
= arbel_cmd_close_ib ( arbel
, ibdev
->port
) ) != 0 ) {
1502 DBGC ( arbel
, "Arbel %p could not close IB: %s\n",
1503 arbel
, strerror ( rc
) );
1504 /* Nothing we can do about this */
1508 /***************************************************************************
1510 * Multicast group operations
1512 ***************************************************************************
1516 * Attach to multicast group
1518 * @v ibdev Infiniband device
1520 * @v gid Multicast GID
1521 * @ret rc Return status code
1523 static int arbel_mcast_attach ( struct ib_device
*ibdev
,
1524 struct ib_queue_pair
*qp
,
1525 struct ib_gid
*gid
) {
1526 struct arbel
*arbel
= ib_get_drvdata ( ibdev
);
1527 struct arbelprm_mgm_hash hash
;
1528 struct arbelprm_mgm_entry mgm
;
1532 /* Generate hash table index */
1533 if ( ( rc
= arbel_cmd_mgid_hash ( arbel
, gid
, &hash
) ) != 0 ) {
1534 DBGC ( arbel
, "Arbel %p could not hash GID: %s\n",
1535 arbel
, strerror ( rc
) );
1538 index
= MLX_GET ( &hash
, hash
);
1540 /* Check for existing hash table entry */
1541 if ( ( rc
= arbel_cmd_read_mgm ( arbel
, index
, &mgm
) ) != 0 ) {
1542 DBGC ( arbel
, "Arbel %p could not read MGM %#x: %s\n",
1543 arbel
, index
, strerror ( rc
) );
1546 if ( MLX_GET ( &mgm
, mgmqp_0
.qi
) != 0 ) {
1547 /* FIXME: this implementation allows only a single QP
1548 * per multicast group, and doesn't handle hash
1549 * collisions. Sufficient for IPoIB but may need to
1550 * be extended in future.
1552 DBGC ( arbel
, "Arbel %p MGID index %#x already in use\n",
1557 /* Update hash table entry */
1558 MLX_FILL_2 ( &mgm
, 8,
1559 mgmqp_0
.qpn_i
, qp
->qpn
,
1561 memcpy ( &mgm
.u
.dwords
[4], gid
, sizeof ( *gid
) );
1562 if ( ( rc
= arbel_cmd_write_mgm ( arbel
, index
, &mgm
) ) != 0 ) {
1563 DBGC ( arbel
, "Arbel %p could not write MGM %#x: %s\n",
1564 arbel
, index
, strerror ( rc
) );
1572 * Detach from multicast group
1574 * @v ibdev Infiniband device
1576 * @v gid Multicast GID
1578 static void arbel_mcast_detach ( struct ib_device
*ibdev
,
1579 struct ib_queue_pair
*qp __unused
,
1580 struct ib_gid
*gid
) {
1581 struct arbel
*arbel
= ib_get_drvdata ( ibdev
);
1582 struct arbelprm_mgm_hash hash
;
1583 struct arbelprm_mgm_entry mgm
;
1587 /* Generate hash table index */
1588 if ( ( rc
= arbel_cmd_mgid_hash ( arbel
, gid
, &hash
) ) != 0 ) {
1589 DBGC ( arbel
, "Arbel %p could not hash GID: %s\n",
1590 arbel
, strerror ( rc
) );
1593 index
= MLX_GET ( &hash
, hash
);
1595 /* Clear hash table entry */
1596 memset ( &mgm
, 0, sizeof ( mgm
) );
1597 if ( ( rc
= arbel_cmd_write_mgm ( arbel
, index
, &mgm
) ) != 0 ) {
1598 DBGC ( arbel
, "Arbel %p could not write MGM %#x: %s\n",
1599 arbel
, index
, strerror ( rc
) );
1604 /***************************************************************************
1608 ***************************************************************************
1612 * Issue management datagram
1614 * @v ibdev Infiniband device
1615 * @v mad Management datagram
1616 * @v len Length of management datagram
1617 * @ret rc Return status code
1619 static int arbel_mad ( struct ib_device
*ibdev
, struct ib_mad_hdr
*mad
,
1621 struct arbel
*arbel
= ib_get_drvdata ( ibdev
);
1622 union arbelprm_mad mad_ifc
;
1625 /* Copy in request packet */
1626 memset ( &mad_ifc
, 0, sizeof ( mad_ifc
) );
1627 assert ( len
<= sizeof ( mad_ifc
.mad
) );
1628 memcpy ( &mad_ifc
.mad
, mad
, len
);
1631 if ( ( rc
= arbel_cmd_mad_ifc ( arbel
, ibdev
->port
,
1632 &mad_ifc
) ) != 0 ) {
1633 DBGC ( arbel
, "Arbel %p could not issue MAD IFC: %s\n",
1634 arbel
, strerror ( rc
) );
1638 /* Copy out reply packet */
1639 memcpy ( mad
, &mad_ifc
.mad
, len
);
1641 if ( mad
->status
!= 0 ) {
1642 DBGC ( arbel
, "Arbel %p MAD IFC status %04x\n",
1643 arbel
, ntohs ( mad
->status
) );
1649 /** Arbel Infiniband operations */
1650 static struct ib_device_operations arbel_ib_operations
= {
1651 .create_cq
= arbel_create_cq
,
1652 .destroy_cq
= arbel_destroy_cq
,
1653 .create_qp
= arbel_create_qp
,
1654 .modify_qp
= arbel_modify_qp
,
1655 .destroy_qp
= arbel_destroy_qp
,
1656 .post_send
= arbel_post_send
,
1657 .post_recv
= arbel_post_recv
,
1658 .poll_cq
= arbel_poll_cq
,
1659 .poll_eq
= arbel_poll_eq
,
1661 .close
= arbel_close
,
1662 .mcast_attach
= arbel_mcast_attach
,
1663 .mcast_detach
= arbel_mcast_detach
,
1667 /***************************************************************************
1671 ***************************************************************************
1675 * Start firmware running
1677 * @v arbel Arbel device
1678 * @ret rc Return status code
1680 static int arbel_start_firmware ( struct arbel
*arbel
) {
1681 struct arbelprm_query_fw fw
;
1682 struct arbelprm_access_lam lam
;
1683 struct arbelprm_virtual_physical_mapping map_fa
;
1684 unsigned int fw_pages
;
1685 unsigned int log2_fw_pages
;
1688 uint64_t eq_set_ci_base_addr
;
1691 /* Get firmware parameters */
1692 if ( ( rc
= arbel_cmd_query_fw ( arbel
, &fw
) ) != 0 ) {
1693 DBGC ( arbel
, "Arbel %p could not query firmware: %s\n",
1694 arbel
, strerror ( rc
) );
1697 DBGC ( arbel
, "Arbel %p firmware version %ld.%ld.%ld\n", arbel
,
1698 MLX_GET ( &fw
, fw_rev_major
), MLX_GET ( &fw
, fw_rev_minor
),
1699 MLX_GET ( &fw
, fw_rev_subminor
) );
1700 fw_pages
= MLX_GET ( &fw
, fw_pages
);
1701 log2_fw_pages
= fls ( fw_pages
- 1 );
1702 fw_pages
= ( 1 << log2_fw_pages
);
1703 DBGC ( arbel
, "Arbel %p requires %d kB for firmware\n",
1704 arbel
, ( fw_pages
* 4 ) );
1705 eq_set_ci_base_addr
=
1706 ( ( (uint64_t) MLX_GET ( &fw
, eq_set_ci_base_addr_h
) << 32 ) |
1707 ( (uint64_t) MLX_GET ( &fw
, eq_set_ci_base_addr_l
) ) );
1708 arbel
->eq_ci_doorbells
= ioremap ( eq_set_ci_base_addr
, 0x200 );
1710 /* Enable locally-attached memory. Ignore failure; there may
1711 * be no attached memory.
1713 arbel_cmd_enable_lam ( arbel
, &lam
);
1715 /* Allocate firmware pages and map firmware area */
1716 fw_size
= ( fw_pages
* 4096 );
1717 arbel
->firmware_area
= umalloc ( fw_size
* 2 );
1718 if ( ! arbel
->firmware_area
) {
1722 fw_base
= ( user_to_phys ( arbel
->firmware_area
, fw_size
) &
1724 DBGC ( arbel
, "Arbel %p firmware area at physical [%lx,%lx)\n",
1725 arbel
, fw_base
, ( fw_base
+ fw_size
) );
1726 memset ( &map_fa
, 0, sizeof ( map_fa
) );
1727 MLX_FILL_2 ( &map_fa
, 3,
1728 log2size
, log2_fw_pages
,
1729 pa_l
, ( fw_base
>> 12 ) );
1730 if ( ( rc
= arbel_cmd_map_fa ( arbel
, &map_fa
) ) != 0 ) {
1731 DBGC ( arbel
, "Arbel %p could not map firmware: %s\n",
1732 arbel
, strerror ( rc
) );
1736 /* Start firmware */
1737 if ( ( rc
= arbel_cmd_run_fw ( arbel
) ) != 0 ) {
1738 DBGC ( arbel
, "Arbel %p could not run firmware: %s\n",
1739 arbel
, strerror ( rc
) );
1743 DBGC ( arbel
, "Arbel %p firmware started\n", arbel
);
1747 arbel_cmd_unmap_fa ( arbel
);
1749 ufree ( arbel
->firmware_area
);
1750 arbel
->firmware_area
= UNULL
;
1757 * Stop firmware running
1759 * @v arbel Arbel device
1761 static void arbel_stop_firmware ( struct arbel
*arbel
) {
1764 if ( ( rc
= arbel_cmd_unmap_fa ( arbel
) ) != 0 ) {
1765 DBGC ( arbel
, "Arbel %p FATAL could not stop firmware: %s\n",
1766 arbel
, strerror ( rc
) );
1767 /* Leak memory and return; at least we avoid corruption */
1770 ufree ( arbel
->firmware_area
);
1771 arbel
->firmware_area
= UNULL
;
1774 /***************************************************************************
1776 * Infinihost Context Memory management
1778 ***************************************************************************
1784 * @v arbel Arbel device
1785 * @ret rc Return status code
1787 static int arbel_get_limits ( struct arbel
*arbel
) {
1788 struct arbelprm_query_dev_lim dev_lim
;
1791 if ( ( rc
= arbel_cmd_query_dev_lim ( arbel
, &dev_lim
) ) != 0 ) {
1792 DBGC ( arbel
, "Arbel %p could not get device limits: %s\n",
1793 arbel
, strerror ( rc
) );
1797 arbel
->limits
.reserved_qps
=
1798 ( 1 << MLX_GET ( &dev_lim
, log2_rsvd_qps
) );
1799 arbel
->limits
.qpc_entry_size
= MLX_GET ( &dev_lim
, qpc_entry_sz
);
1800 arbel
->limits
.eqpc_entry_size
= MLX_GET ( &dev_lim
, eqpc_entry_sz
);
1801 arbel
->limits
.reserved_srqs
=
1802 ( 1 << MLX_GET ( &dev_lim
, log2_rsvd_srqs
) );
1803 arbel
->limits
.srqc_entry_size
= MLX_GET ( &dev_lim
, srq_entry_sz
);
1804 arbel
->limits
.reserved_ees
=
1805 ( 1 << MLX_GET ( &dev_lim
, log2_rsvd_ees
) );
1806 arbel
->limits
.eec_entry_size
= MLX_GET ( &dev_lim
, eec_entry_sz
);
1807 arbel
->limits
.eeec_entry_size
= MLX_GET ( &dev_lim
, eeec_entry_sz
);
1808 arbel
->limits
.reserved_cqs
=
1809 ( 1 << MLX_GET ( &dev_lim
, log2_rsvd_cqs
) );
1810 arbel
->limits
.cqc_entry_size
= MLX_GET ( &dev_lim
, cqc_entry_sz
);
1811 arbel
->limits
.reserved_eqs
= MLX_GET ( &dev_lim
, num_rsvd_eqs
);
1812 arbel
->limits
.reserved_mtts
=
1813 ( 1 << MLX_GET ( &dev_lim
, log2_rsvd_mtts
) );
1814 arbel
->limits
.mtt_entry_size
= MLX_GET ( &dev_lim
, mtt_entry_sz
);
1815 arbel
->limits
.reserved_mrws
=
1816 ( 1 << MLX_GET ( &dev_lim
, log2_rsvd_mrws
) );
1817 arbel
->limits
.mpt_entry_size
= MLX_GET ( &dev_lim
, mpt_entry_sz
);
1818 arbel
->limits
.reserved_rdbs
=
1819 ( 1 << MLX_GET ( &dev_lim
, log2_rsvd_rdbs
) );
1820 arbel
->limits
.eqc_entry_size
= MLX_GET ( &dev_lim
, eqc_entry_sz
);
1821 arbel
->limits
.reserved_uars
= MLX_GET ( &dev_lim
, num_rsvd_uars
);
1829 * @v log_num_entries Log2 of the number of entries
1830 * @v entry_size Entry size
1831 * @ret usage Usage size in ICM
1833 static size_t icm_usage ( unsigned int log_num_entries
, size_t entry_size
) {
1836 usage
= ( ( 1 << log_num_entries
) * entry_size
);
1837 usage
= ( ( usage
+ 4095 ) & ~4095 );
1844 * @v arbel Arbel device
1845 * @v init_hca INIT_HCA structure to fill in
1846 * @ret rc Return status code
1848 static int arbel_alloc_icm ( struct arbel
*arbel
,
1849 struct arbelprm_init_hca
*init_hca
) {
1850 struct arbelprm_scalar_parameter icm_size
;
1851 struct arbelprm_scalar_parameter icm_aux_size
;
1852 struct arbelprm_virtual_physical_mapping map_icm_aux
;
1853 struct arbelprm_virtual_physical_mapping map_icm
;
1854 union arbelprm_doorbell_record
*db_rec
;
1855 size_t icm_offset
= 0;
1856 unsigned int log_num_qps
, log_num_srqs
, log_num_ees
, log_num_cqs
;
1857 unsigned int log_num_mtts
, log_num_mpts
, log_num_rdbs
, log_num_eqs
;
1860 icm_offset
= ( ( arbel
->limits
.reserved_uars
+ 1 ) << 12 );
1862 /* Queue pair contexts */
1863 log_num_qps
= fls ( arbel
->limits
.reserved_qps
+ ARBEL_MAX_QPS
- 1 );
1864 MLX_FILL_2 ( init_hca
, 13,
1865 qpc_eec_cqc_eqc_rdb_parameters
.qpc_base_addr_l
,
1866 ( icm_offset
>> 7 ),
1867 qpc_eec_cqc_eqc_rdb_parameters
.log_num_of_qp
,
1869 DBGC ( arbel
, "Arbel %p ICM QPC base = %zx\n", arbel
, icm_offset
);
1870 icm_offset
+= icm_usage ( log_num_qps
, arbel
->limits
.qpc_entry_size
);
1872 /* Extended queue pair contexts */
1873 MLX_FILL_1 ( init_hca
, 25,
1874 qpc_eec_cqc_eqc_rdb_parameters
.eqpc_base_addr_l
,
1876 DBGC ( arbel
, "Arbel %p ICM EQPC base = %zx\n", arbel
, icm_offset
);
1877 // icm_offset += icm_usage ( log_num_qps, arbel->limits.eqpc_entry_size );
1878 icm_offset
+= icm_usage ( log_num_qps
, arbel
->limits
.qpc_entry_size
);
1880 /* Shared receive queue contexts */
1881 log_num_srqs
= fls ( arbel
->limits
.reserved_srqs
- 1 );
1882 MLX_FILL_2 ( init_hca
, 19,
1883 qpc_eec_cqc_eqc_rdb_parameters
.srqc_base_addr_l
,
1884 ( icm_offset
>> 5 ),
1885 qpc_eec_cqc_eqc_rdb_parameters
.log_num_of_srq
,
1887 DBGC ( arbel
, "Arbel %p ICM SRQC base = %zx\n", arbel
, icm_offset
);
1888 icm_offset
+= icm_usage ( log_num_srqs
, arbel
->limits
.srqc_entry_size
);
1890 /* End-to-end contexts */
1891 log_num_ees
= fls ( arbel
->limits
.reserved_ees
- 1 );
1892 MLX_FILL_2 ( init_hca
, 17,
1893 qpc_eec_cqc_eqc_rdb_parameters
.eec_base_addr_l
,
1894 ( icm_offset
>> 7 ),
1895 qpc_eec_cqc_eqc_rdb_parameters
.log_num_of_ee
,
1897 DBGC ( arbel
, "Arbel %p ICM EEC base = %zx\n", arbel
, icm_offset
);
1898 icm_offset
+= icm_usage ( log_num_ees
, arbel
->limits
.eec_entry_size
);
1900 /* Extended end-to-end contexts */
1901 MLX_FILL_1 ( init_hca
, 29,
1902 qpc_eec_cqc_eqc_rdb_parameters
.eeec_base_addr_l
,
1904 DBGC ( arbel
, "Arbel %p ICM EEEC base = %zx\n", arbel
, icm_offset
);
1905 icm_offset
+= icm_usage ( log_num_ees
, arbel
->limits
.eeec_entry_size
);
1907 /* Completion queue contexts */
1908 log_num_cqs
= fls ( arbel
->limits
.reserved_cqs
+ ARBEL_MAX_CQS
- 1 );
1909 MLX_FILL_2 ( init_hca
, 21,
1910 qpc_eec_cqc_eqc_rdb_parameters
.cqc_base_addr_l
,
1911 ( icm_offset
>> 6 ),
1912 qpc_eec_cqc_eqc_rdb_parameters
.log_num_of_cq
,
1914 DBGC ( arbel
, "Arbel %p ICM CQC base = %zx\n", arbel
, icm_offset
);
1915 icm_offset
+= icm_usage ( log_num_cqs
, arbel
->limits
.cqc_entry_size
);
1917 /* Memory translation table */
1918 log_num_mtts
= fls ( arbel
->limits
.reserved_mtts
- 1 );
1919 MLX_FILL_1 ( init_hca
, 65,
1920 tpt_parameters
.mtt_base_addr_l
, icm_offset
);
1921 DBGC ( arbel
, "Arbel %p ICM MTT base = %zx\n", arbel
, icm_offset
);
1922 icm_offset
+= icm_usage ( log_num_mtts
, arbel
->limits
.mtt_entry_size
);
1924 /* Memory protection table */
1925 log_num_mpts
= fls ( arbel
->limits
.reserved_mrws
+ 1 - 1 );
1926 MLX_FILL_1 ( init_hca
, 61,
1927 tpt_parameters
.mpt_base_adr_l
, icm_offset
);
1928 MLX_FILL_1 ( init_hca
, 62,
1929 tpt_parameters
.log_mpt_sz
, log_num_mpts
);
1930 DBGC ( arbel
, "Arbel %p ICM MTT base = %zx\n", arbel
, icm_offset
);
1931 icm_offset
+= icm_usage ( log_num_mpts
, arbel
->limits
.mpt_entry_size
);
1933 /* RDMA something or other */
1934 log_num_rdbs
= fls ( arbel
->limits
.reserved_rdbs
- 1 );
1935 MLX_FILL_1 ( init_hca
, 37,
1936 qpc_eec_cqc_eqc_rdb_parameters
.rdb_base_addr_l
,
1938 DBGC ( arbel
, "Arbel %p ICM RDB base = %zx\n", arbel
, icm_offset
);
1939 icm_offset
+= icm_usage ( log_num_rdbs
, 32 );
1941 /* Event queue contexts */
1942 log_num_eqs
= fls ( arbel
->limits
.reserved_eqs
+ ARBEL_MAX_EQS
- 1 );
1943 MLX_FILL_2 ( init_hca
, 33,
1944 qpc_eec_cqc_eqc_rdb_parameters
.eqc_base_addr_l
,
1945 ( icm_offset
>> 6 ),
1946 qpc_eec_cqc_eqc_rdb_parameters
.log_num_eq
,
1948 DBGC ( arbel
, "Arbel %p ICM EQ base = %zx\n", arbel
, icm_offset
);
1949 icm_offset
+= ( ( 1 << log_num_eqs
) * arbel
->limits
.eqc_entry_size
);
1951 /* Multicast table */
1952 MLX_FILL_1 ( init_hca
, 49,
1953 multicast_parameters
.mc_base_addr_l
, icm_offset
);
1954 MLX_FILL_1 ( init_hca
, 52,
1955 multicast_parameters
.log_mc_table_entry_sz
,
1956 fls ( sizeof ( struct arbelprm_mgm_entry
) - 1 ) );
1957 MLX_FILL_1 ( init_hca
, 53,
1958 multicast_parameters
.mc_table_hash_sz
, 8 );
1959 MLX_FILL_1 ( init_hca
, 54,
1960 multicast_parameters
.log_mc_table_sz
, 3 );
1961 DBGC ( arbel
, "Arbel %p ICM MC base = %zx\n", arbel
, icm_offset
);
1962 icm_offset
+= ( 8 * sizeof ( struct arbelprm_mgm_entry
) );
1964 arbel
->icm_len
= icm_offset
;
1965 arbel
->icm_len
= ( ( arbel
->icm_len
+ 4095 ) & ~4095 );
1967 /* Get ICM auxiliary area size */
1968 memset ( &icm_size
, 0, sizeof ( icm_size
) );
1969 MLX_FILL_1 ( &icm_size
, 1, value
, arbel
->icm_len
);
1970 if ( ( rc
= arbel_cmd_set_icm_size ( arbel
, &icm_size
,
1971 &icm_aux_size
) ) != 0 ) {
1972 DBGC ( arbel
, "Arbel %p could not set ICM size: %s\n",
1973 arbel
, strerror ( rc
) );
1974 goto err_set_icm_size
;
1976 arbel
->icm_aux_len
= ( MLX_GET ( &icm_aux_size
, value
) * 4096 );
1978 /* Allocate ICM data and auxiliary area */
1979 DBGC ( arbel
, "Arbel %p requires %zd kB ICM and %zd kB AUX ICM\n",
1980 arbel
, ( arbel
->icm_len
/ 1024 ),
1981 ( arbel
->icm_aux_len
/ 1024 ) );
1982 arbel
->icm
= umalloc ( arbel
->icm_len
+ arbel
->icm_aux_len
);
1983 if ( ! arbel
->icm
) {
1988 /* Map ICM auxiliary area */
1989 memset ( &map_icm_aux
, 0, sizeof ( map_icm_aux
) );
1990 MLX_FILL_2 ( &map_icm_aux
, 3,
1991 log2size
, fls ( ( arbel
->icm_aux_len
/ 4096 ) - 1 ),
1993 ( user_to_phys ( arbel
->icm
, arbel
->icm_len
) >> 12 ) );
1994 if ( ( rc
= arbel_cmd_map_icm_aux ( arbel
, &map_icm_aux
) ) != 0 ) {
1995 DBGC ( arbel
, "Arbel %p could not map AUX ICM: %s\n",
1996 arbel
, strerror ( rc
) );
1997 goto err_map_icm_aux
;
2001 memset ( &map_icm
, 0, sizeof ( map_icm
) );
2002 MLX_FILL_2 ( &map_icm
, 3,
2003 log2size
, fls ( ( arbel
->icm_len
/ 4096 ) - 1 ),
2004 pa_l
, ( user_to_phys ( arbel
->icm
, 0 ) >> 12 ) );
2005 if ( ( rc
= arbel_cmd_map_icm ( arbel
, &map_icm
) ) != 0 ) {
2006 DBGC ( arbel
, "Arbel %p could not map ICM: %s\n",
2007 arbel
, strerror ( rc
) );
2011 /* Initialise UAR context */
2012 arbel
->db_rec
= phys_to_virt ( user_to_phys ( arbel
->icm
, 0 ) +
2013 ( arbel
->limits
.reserved_uars
*
2014 ARBEL_PAGE_SIZE
) );
2015 memset ( arbel
->db_rec
, 0, ARBEL_PAGE_SIZE
);
2016 db_rec
= &arbel
->db_rec
[ARBEL_GROUP_SEPARATOR_DOORBELL
];
2017 MLX_FILL_1 ( &db_rec
->qp
, 1, res
, ARBEL_UAR_RES_GROUP_SEP
);
2021 arbel_cmd_unmap_icm ( arbel
, ( arbel
->icm_len
/ 4096 ) );
2023 arbel_cmd_unmap_icm_aux ( arbel
);
2025 ufree ( arbel
->icm
);
2035 * @v arbel Arbel device
2037 static void arbel_free_icm ( struct arbel
*arbel
) {
2038 arbel_cmd_unmap_icm ( arbel
, ( arbel
->icm_len
/ 4096 ) );
2039 arbel_cmd_unmap_icm_aux ( arbel
);
2040 ufree ( arbel
->icm
);
2044 /***************************************************************************
2048 ***************************************************************************
2052 * Set up memory protection table
2054 * @v arbel Arbel device
2055 * @ret rc Return status code
2057 static int arbel_setup_mpt ( struct arbel
*arbel
) {
2058 struct arbelprm_mpt mpt
;
2063 key
= ( arbel
->limits
.reserved_mrws
| ARBEL_MKEY_PREFIX
);
2064 arbel
->reserved_lkey
= ( ( key
<< 8 ) | ( key
>> 24 ) );
2066 /* Initialise memory protection table */
2067 memset ( &mpt
, 0, sizeof ( mpt
) );
2068 MLX_FILL_4 ( &mpt
, 0,
2073 MLX_FILL_1 ( &mpt
, 2, mem_key
, key
);
2074 MLX_FILL_1 ( &mpt
, 3, pd
, ARBEL_GLOBAL_PD
);
2075 MLX_FILL_1 ( &mpt
, 6, reg_wnd_len_h
, 0xffffffffUL
);
2076 MLX_FILL_1 ( &mpt
, 7, reg_wnd_len_l
, 0xffffffffUL
);
2077 if ( ( rc
= arbel_cmd_sw2hw_mpt ( arbel
, arbel
->limits
.reserved_mrws
,
2079 DBGC ( arbel
, "Arbel %p could not set up MPT: %s\n",
2080 arbel
, strerror ( rc
) );
2092 * @ret rc Return status code
2094 static int arbel_probe ( struct pci_device
*pci
,
2095 const struct pci_device_id
*id __unused
) {
2096 struct arbel
*arbel
;
2097 struct ib_device
*ibdev
;
2098 struct arbelprm_init_hca init_hca
;
2102 /* Allocate Arbel device */
2103 arbel
= zalloc ( sizeof ( *arbel
) );
2106 goto err_alloc_arbel
;
2108 pci_set_drvdata ( pci
, arbel
);
2110 /* Allocate Infiniband devices */
2111 for ( i
= 0 ; i
< ARBEL_NUM_PORTS
; i
++ ) {
2112 ibdev
= alloc_ibdev ( 0 );
2115 goto err_alloc_ibdev
;
2117 arbel
->ibdev
[i
] = ibdev
;
2118 ibdev
->op
= &arbel_ib_operations
;
2119 ibdev
->dev
= &pci
->dev
;
2120 ibdev
->port
= ( ARBEL_PORT_BASE
+ i
);
2121 ib_set_drvdata ( ibdev
, arbel
);
2124 /* Fix up PCI device */
2125 adjust_pci_device ( pci
);
2128 arbel
->config
= ioremap ( pci_bar_start ( pci
, ARBEL_PCI_CONFIG_BAR
),
2129 ARBEL_PCI_CONFIG_BAR_SIZE
);
2130 arbel
->uar
= ioremap ( ( pci_bar_start ( pci
, ARBEL_PCI_UAR_BAR
) +
2131 ARBEL_PCI_UAR_IDX
* ARBEL_PCI_UAR_SIZE
),
2132 ARBEL_PCI_UAR_SIZE
);
2134 /* Allocate space for mailboxes */
2135 arbel
->mailbox_in
= malloc_dma ( ARBEL_MBOX_SIZE
, ARBEL_MBOX_ALIGN
);
2136 if ( ! arbel
->mailbox_in
) {
2138 goto err_mailbox_in
;
2140 arbel
->mailbox_out
= malloc_dma ( ARBEL_MBOX_SIZE
, ARBEL_MBOX_ALIGN
);
2141 if ( ! arbel
->mailbox_out
) {
2143 goto err_mailbox_out
;
2146 /* Start firmware */
2147 if ( ( rc
= arbel_start_firmware ( arbel
) ) != 0 )
2148 goto err_start_firmware
;
2150 /* Get device limits */
2151 if ( ( rc
= arbel_get_limits ( arbel
) ) != 0 )
2152 goto err_get_limits
;
2155 memset ( &init_hca
, 0, sizeof ( init_hca
) );
2156 if ( ( rc
= arbel_alloc_icm ( arbel
, &init_hca
) ) != 0 )
2159 /* Initialise HCA */
2160 MLX_FILL_1 ( &init_hca
, 74, uar_parameters
.log_max_uars
, 1 );
2161 if ( ( rc
= arbel_cmd_init_hca ( arbel
, &init_hca
) ) != 0 ) {
2162 DBGC ( arbel
, "Arbel %p could not initialise HCA: %s\n",
2163 arbel
, strerror ( rc
) );
2167 /* Set up memory protection */
2168 if ( ( rc
= arbel_setup_mpt ( arbel
) ) != 0 )
2171 /* Set up event queue */
2172 if ( ( rc
= arbel_create_eq ( arbel
) ) != 0 )
2175 /* Register Infiniband devices */
2176 for ( i
= 0 ; i
< ARBEL_NUM_PORTS
; i
++ ) {
2177 if ( ( rc
= register_ibdev ( arbel
->ibdev
[i
] ) ) != 0 ) {
2178 DBGC ( arbel
, "Arbel %p could not register IB "
2179 "device: %s\n", arbel
, strerror ( rc
) );
2180 goto err_register_ibdev
;
2186 i
= ARBEL_NUM_PORTS
;
2188 for ( i
-- ; i
>= 0 ; i
-- )
2189 unregister_ibdev ( arbel
->ibdev
[i
] );
2190 arbel_destroy_eq ( arbel
);
2193 arbel_cmd_close_hca ( arbel
);
2195 arbel_free_icm ( arbel
);
2198 arbel_stop_firmware ( arbel
);
2200 free_dma ( arbel
->mailbox_out
, ARBEL_MBOX_SIZE
);
2202 free_dma ( arbel
->mailbox_in
, ARBEL_MBOX_SIZE
);
2204 i
= ARBEL_NUM_PORTS
;
2206 for ( i
-- ; i
>= 0 ; i
-- )
2207 ibdev_put ( arbel
->ibdev
[i
] );
2218 static void arbel_remove ( struct pci_device
*pci
) {
2219 struct arbel
*arbel
= pci_get_drvdata ( pci
);
2222 for ( i
= ( ARBEL_NUM_PORTS
- 1 ) ; i
>= 0 ; i
-- )
2223 unregister_ibdev ( arbel
->ibdev
[i
] );
2224 arbel_destroy_eq ( arbel
);
2225 arbel_cmd_close_hca ( arbel
);
2226 arbel_free_icm ( arbel
);
2227 arbel_stop_firmware ( arbel
);
2228 arbel_stop_firmware ( arbel
);
2229 free_dma ( arbel
->mailbox_out
, ARBEL_MBOX_SIZE
);
2230 free_dma ( arbel
->mailbox_in
, ARBEL_MBOX_SIZE
);
2231 for ( i
= ( ARBEL_NUM_PORTS
- 1 ) ; i
>= 0 ; i
-- )
2232 ibdev_put ( arbel
->ibdev
[i
] );
2236 static struct pci_device_id arbel_nics
[] = {
2237 PCI_ROM ( 0x15b3, 0x6282, "mt25218", "MT25218 HCA driver" ),
2238 PCI_ROM ( 0x15b3, 0x6274, "mt25204", "MT25204 HCA driver" ),
2241 struct pci_driver arbel_driver __pci_driver
= {
2243 .id_count
= ( sizeof ( arbel_nics
) / sizeof ( arbel_nics
[0] ) ),
2244 .probe
= arbel_probe
,
2245 .remove
= arbel_remove
,