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.
32 #include <gpxe/malloc.h>
33 #include <gpxe/umalloc.h>
34 #include <gpxe/iobuf.h>
35 #include <gpxe/netdevice.h>
36 #include <gpxe/infiniband.h>
42 * Mellanox Arbel Infiniband HCA
46 /***************************************************************************
48 * Queue number allocation
50 ***************************************************************************
54 * Allocate queue number
56 * @v q_inuse Queue usage bitmask
57 * @v max_inuse Maximum number of in-use queues
58 * @ret qn_offset Free queue number offset, or negative error
60 static int arbel_alloc_qn_offset ( arbel_bitmask_t *q_inuse,
61 unsigned int max_inuse ) {
62 unsigned int qn_offset = 0;
63 arbel_bitmask_t mask = 1;
65 while ( qn_offset < max_inuse ) {
66 if ( ( mask & *q_inuse ) == 0 ) {
83 * @v q_inuse Queue usage bitmask
84 * @v qn_offset Queue number offset
86 static void arbel_free_qn_offset ( arbel_bitmask_t *q_inuse, int qn_offset ) {
89 mask = ( 1 << ( qn_offset % ( 8 * sizeof ( mask ) ) ) );
90 q_inuse += ( qn_offset / ( 8 * sizeof ( mask ) ) );
94 /***************************************************************************
98 ***************************************************************************
102 * Wait for Arbel command completion
104 * @v arbel Arbel device
105 * @ret rc Return status code
107 static int arbel_cmd_wait ( struct arbel *arbel,
108 struct arbelprm_hca_command_register *hcr ) {
111 for ( wait = ARBEL_HCR_MAX_WAIT_MS ; wait ; wait-- ) {
113 readl ( arbel->config + ARBEL_HCR_REG ( 6 ) );
114 if ( MLX_GET ( hcr, go ) == 0 )
124 * @v arbel Arbel device
125 * @v command Command opcode, flags and input/output lengths
126 * @v op_mod Opcode modifier (0 if no modifier applicable)
127 * @v in Input parameters
128 * @v in_mod Input modifier (0 if no modifier applicable)
129 * @v out Output parameters
130 * @ret rc Return status code
132 static int arbel_cmd ( struct arbel *arbel, unsigned long command,
133 unsigned int op_mod, const void *in,
134 unsigned int in_mod, void *out ) {
135 struct arbelprm_hca_command_register hcr;
136 unsigned int opcode = ARBEL_HCR_OPCODE ( command );
137 size_t in_len = ARBEL_HCR_IN_LEN ( command );
138 size_t out_len = ARBEL_HCR_OUT_LEN ( command );
145 assert ( in_len <= ARBEL_MBOX_SIZE );
146 assert ( out_len <= ARBEL_MBOX_SIZE );
148 DBGC2 ( arbel, "Arbel %p command %02x in %zx%s out %zx%s\n",
149 arbel, opcode, in_len,
150 ( ( command & ARBEL_HCR_IN_MBOX ) ? "(mbox)" : "" ), out_len,
151 ( ( command & ARBEL_HCR_OUT_MBOX ) ? "(mbox)" : "" ) );
153 /* Check that HCR is free */
154 if ( ( rc = arbel_cmd_wait ( arbel, &hcr ) ) != 0 ) {
155 DBGC ( arbel, "Arbel %p command interface locked\n", arbel );
160 memset ( &hcr, 0, sizeof ( hcr ) );
161 in_buffer = &hcr.u.dwords[0];
162 if ( in_len && ( command & ARBEL_HCR_IN_MBOX ) ) {
163 in_buffer = arbel->mailbox_in;
164 MLX_FILL_1 ( &hcr, 1, in_param_l, virt_to_bus ( in_buffer ) );
166 memcpy ( in_buffer, in, in_len );
167 MLX_FILL_1 ( &hcr, 2, input_modifier, in_mod );
168 out_buffer = &hcr.u.dwords[3];
169 if ( out_len && ( command & ARBEL_HCR_OUT_MBOX ) ) {
170 out_buffer = arbel->mailbox_out;
171 MLX_FILL_1 ( &hcr, 4, out_param_l,
172 virt_to_bus ( out_buffer ) );
174 MLX_FILL_3 ( &hcr, 6,
176 opcode_modifier, op_mod,
178 DBGC2_HD ( arbel, &hcr, sizeof ( hcr ) );
180 DBGC2 ( arbel, "Input:\n" );
181 DBGC2_HD ( arbel, in, ( ( in_len < 512 ) ? in_len : 512 ) );
185 for ( i = 0 ; i < ( sizeof ( hcr ) / sizeof ( hcr.u.dwords[0] ) ) ;
187 writel ( hcr.u.dwords[i],
188 arbel->config + ARBEL_HCR_REG ( i ) );
192 /* Wait for command completion */
193 if ( ( rc = arbel_cmd_wait ( arbel, &hcr ) ) != 0 ) {
194 DBGC ( arbel, "Arbel %p timed out waiting for command:\n",
196 DBGC_HD ( arbel, &hcr, sizeof ( hcr ) );
200 /* Check command status */
201 status = MLX_GET ( &hcr, status );
203 DBGC ( arbel, "Arbel %p command failed with status %02x:\n",
205 DBGC_HD ( arbel, &hcr, sizeof ( hcr ) );
209 /* Read output parameters, if any */
210 hcr.u.dwords[3] = readl ( arbel->config + ARBEL_HCR_REG ( 3 ) );
211 hcr.u.dwords[4] = readl ( arbel->config + ARBEL_HCR_REG ( 4 ) );
212 memcpy ( out, out_buffer, out_len );
214 DBGC2 ( arbel, "Output:\n" );
215 DBGC2_HD ( arbel, out, ( ( out_len < 512 ) ? out_len : 512 ) );
222 arbel_cmd_query_dev_lim ( struct arbel *arbel,
223 struct arbelprm_query_dev_lim *dev_lim ) {
224 return arbel_cmd ( arbel,
225 ARBEL_HCR_OUT_CMD ( ARBEL_HCR_QUERY_DEV_LIM,
226 1, sizeof ( *dev_lim ) ),
227 0, NULL, 0, dev_lim );
231 arbel_cmd_query_fw ( struct arbel *arbel, struct arbelprm_query_fw *fw ) {
232 return arbel_cmd ( arbel,
233 ARBEL_HCR_OUT_CMD ( ARBEL_HCR_QUERY_FW,
239 arbel_cmd_init_hca ( struct arbel *arbel,
240 const struct arbelprm_init_hca *init_hca ) {
241 return arbel_cmd ( arbel,
242 ARBEL_HCR_IN_CMD ( ARBEL_HCR_INIT_HCA,
243 1, sizeof ( *init_hca ) ),
244 0, init_hca, 0, NULL );
248 arbel_cmd_close_hca ( struct arbel *arbel ) {
249 return arbel_cmd ( arbel,
250 ARBEL_HCR_VOID_CMD ( ARBEL_HCR_CLOSE_HCA ),
255 arbel_cmd_init_ib ( struct arbel *arbel, unsigned int port,
256 const struct arbelprm_init_ib *init_ib ) {
257 return arbel_cmd ( arbel,
258 ARBEL_HCR_IN_CMD ( ARBEL_HCR_INIT_IB,
259 1, sizeof ( *init_ib ) ),
260 0, init_ib, port, NULL );
264 arbel_cmd_close_ib ( struct arbel *arbel, unsigned int port ) {
265 return arbel_cmd ( arbel,
266 ARBEL_HCR_VOID_CMD ( ARBEL_HCR_CLOSE_IB ),
267 0, NULL, port, NULL );
271 arbel_cmd_sw2hw_mpt ( struct arbel *arbel, unsigned int index,
272 const struct arbelprm_mpt *mpt ) {
273 return arbel_cmd ( arbel,
274 ARBEL_HCR_IN_CMD ( ARBEL_HCR_SW2HW_MPT,
275 1, sizeof ( *mpt ) ),
276 0, mpt, index, NULL );
280 arbel_cmd_sw2hw_eq ( struct arbel *arbel, unsigned int index,
281 const struct arbelprm_eqc *eqc ) {
282 return arbel_cmd ( arbel,
283 ARBEL_HCR_IN_CMD ( ARBEL_HCR_SW2HW_EQ,
284 1, sizeof ( *eqc ) ),
285 0, eqc, index, NULL );
289 arbel_cmd_hw2sw_eq ( struct arbel *arbel, unsigned int index ) {
290 return arbel_cmd ( arbel,
291 ARBEL_HCR_VOID_CMD ( ARBEL_HCR_HW2SW_EQ ),
292 1, NULL, index, NULL );
296 arbel_cmd_sw2hw_cq ( struct arbel *arbel, unsigned long cqn,
297 const struct arbelprm_completion_queue_context *cqctx ) {
298 return arbel_cmd ( arbel,
299 ARBEL_HCR_IN_CMD ( ARBEL_HCR_SW2HW_CQ,
300 1, sizeof ( *cqctx ) ),
301 0, cqctx, cqn, NULL );
305 arbel_cmd_hw2sw_cq ( struct arbel *arbel, unsigned long cqn,
306 struct arbelprm_completion_queue_context *cqctx) {
307 return arbel_cmd ( arbel,
308 ARBEL_HCR_OUT_CMD ( ARBEL_HCR_HW2SW_CQ,
309 1, sizeof ( *cqctx ) ),
310 0, NULL, cqn, cqctx );
314 arbel_cmd_rst2init_qpee ( struct arbel *arbel, unsigned long qpn,
315 const struct arbelprm_qp_ee_state_transitions *ctx ){
316 return arbel_cmd ( arbel,
317 ARBEL_HCR_IN_CMD ( ARBEL_HCR_RST2INIT_QPEE,
318 1, sizeof ( *ctx ) ),
323 arbel_cmd_init2rtr_qpee ( struct arbel *arbel, unsigned long qpn,
324 const struct arbelprm_qp_ee_state_transitions *ctx ){
325 return arbel_cmd ( arbel,
326 ARBEL_HCR_IN_CMD ( ARBEL_HCR_INIT2RTR_QPEE,
327 1, sizeof ( *ctx ) ),
332 arbel_cmd_rtr2rts_qpee ( struct arbel *arbel, unsigned long qpn,
333 const struct arbelprm_qp_ee_state_transitions *ctx ) {
334 return arbel_cmd ( arbel,
335 ARBEL_HCR_IN_CMD ( ARBEL_HCR_RTR2RTS_QPEE,
336 1, sizeof ( *ctx ) ),
341 arbel_cmd_2rst_qpee ( struct arbel *arbel, unsigned long qpn ) {
342 return arbel_cmd ( arbel,
343 ARBEL_HCR_VOID_CMD ( ARBEL_HCR_2RST_QPEE ),
344 0x03, NULL, qpn, NULL );
348 arbel_cmd_mad_ifc ( struct arbel *arbel, unsigned int port,
349 union arbelprm_mad *mad ) {
350 return arbel_cmd ( arbel,
351 ARBEL_HCR_INOUT_CMD ( ARBEL_HCR_MAD_IFC,
353 1, sizeof ( *mad ) ),
354 0x03, mad, port, mad );
358 arbel_cmd_read_mgm ( struct arbel *arbel, unsigned int index,
359 struct arbelprm_mgm_entry *mgm ) {
360 return arbel_cmd ( arbel,
361 ARBEL_HCR_OUT_CMD ( ARBEL_HCR_READ_MGM,
362 1, sizeof ( *mgm ) ),
363 0, NULL, index, mgm );
367 arbel_cmd_write_mgm ( struct arbel *arbel, unsigned int index,
368 const struct arbelprm_mgm_entry *mgm ) {
369 return arbel_cmd ( arbel,
370 ARBEL_HCR_IN_CMD ( ARBEL_HCR_WRITE_MGM,
371 1, sizeof ( *mgm ) ),
372 0, mgm, index, NULL );
376 arbel_cmd_mgid_hash ( struct arbel *arbel, const struct ib_gid *gid,
377 struct arbelprm_mgm_hash *hash ) {
378 return arbel_cmd ( arbel,
379 ARBEL_HCR_INOUT_CMD ( ARBEL_HCR_MGID_HASH,
381 0, sizeof ( *hash ) ),
386 arbel_cmd_run_fw ( struct arbel *arbel ) {
387 return arbel_cmd ( arbel,
388 ARBEL_HCR_VOID_CMD ( ARBEL_HCR_RUN_FW ),
393 arbel_cmd_disable_lam ( struct arbel *arbel ) {
394 return arbel_cmd ( arbel,
395 ARBEL_HCR_VOID_CMD ( ARBEL_HCR_DISABLE_LAM ),
400 arbel_cmd_enable_lam ( struct arbel *arbel, struct arbelprm_access_lam *lam ) {
401 return arbel_cmd ( arbel,
402 ARBEL_HCR_OUT_CMD ( ARBEL_HCR_ENABLE_LAM,
403 1, sizeof ( *lam ) ),
408 arbel_cmd_unmap_icm ( struct arbel *arbel, unsigned int page_count ) {
409 return arbel_cmd ( arbel,
410 ARBEL_HCR_VOID_CMD ( ARBEL_HCR_UNMAP_ICM ),
411 0, NULL, page_count, NULL );
415 arbel_cmd_map_icm ( struct arbel *arbel,
416 const struct arbelprm_virtual_physical_mapping *map ) {
417 return arbel_cmd ( arbel,
418 ARBEL_HCR_IN_CMD ( ARBEL_HCR_MAP_ICM,
419 1, sizeof ( *map ) ),
424 arbel_cmd_unmap_icm_aux ( struct arbel *arbel ) {
425 return arbel_cmd ( arbel,
426 ARBEL_HCR_VOID_CMD ( ARBEL_HCR_UNMAP_ICM_AUX ),
431 arbel_cmd_map_icm_aux ( struct arbel *arbel,
432 const struct arbelprm_virtual_physical_mapping *map ) {
433 return arbel_cmd ( arbel,
434 ARBEL_HCR_IN_CMD ( ARBEL_HCR_MAP_ICM_AUX,
435 1, sizeof ( *map ) ),
440 arbel_cmd_set_icm_size ( struct arbel *arbel,
441 const struct arbelprm_scalar_parameter *icm_size,
442 struct arbelprm_scalar_parameter *icm_aux_size ) {
443 return arbel_cmd ( arbel,
444 ARBEL_HCR_INOUT_CMD ( ARBEL_HCR_SET_ICM_SIZE,
445 0, sizeof ( *icm_size ),
446 0, sizeof ( *icm_aux_size ) ),
447 0, icm_size, 0, icm_aux_size );
451 arbel_cmd_unmap_fa ( struct arbel *arbel ) {
452 return arbel_cmd ( arbel,
453 ARBEL_HCR_VOID_CMD ( ARBEL_HCR_UNMAP_FA ),
458 arbel_cmd_map_fa ( struct arbel *arbel,
459 const struct arbelprm_virtual_physical_mapping *map ) {
460 return arbel_cmd ( arbel,
461 ARBEL_HCR_IN_CMD ( ARBEL_HCR_MAP_FA,
462 1, sizeof ( *map ) ),
466 /***************************************************************************
468 * Completion queue operations
470 ***************************************************************************
474 * Create completion queue
476 * @v ibdev Infiniband device
477 * @v cq Completion queue
478 * @ret rc Return status code
480 static int arbel_create_cq ( struct ib_device *ibdev,
481 struct ib_completion_queue *cq ) {
482 struct arbel *arbel = ib_get_drvdata ( ibdev );
483 struct arbel_completion_queue *arbel_cq;
484 struct arbelprm_completion_queue_context cqctx;
485 struct arbelprm_cq_ci_db_record *ci_db_rec;
486 struct arbelprm_cq_arm_db_record *arm_db_rec;
491 /* Find a free completion queue number */
492 cqn_offset = arbel_alloc_qn_offset ( arbel->cq_inuse, ARBEL_MAX_CQS );
493 if ( cqn_offset < 0 ) {
494 DBGC ( arbel, "Arbel %p out of completion queues\n", arbel );
498 cq->cqn = ( arbel->limits.reserved_cqs + cqn_offset );
500 /* Allocate control structures */
501 arbel_cq = zalloc ( sizeof ( *arbel_cq ) );
506 arbel_cq->ci_doorbell_idx = arbel_cq_ci_doorbell_idx ( cqn_offset );
507 arbel_cq->arm_doorbell_idx = arbel_cq_arm_doorbell_idx ( cqn_offset );
509 /* Allocate completion queue itself */
510 arbel_cq->cqe_size = ( cq->num_cqes * sizeof ( arbel_cq->cqe[0] ) );
511 arbel_cq->cqe = malloc_dma ( arbel_cq->cqe_size,
512 sizeof ( arbel_cq->cqe[0] ) );
513 if ( ! arbel_cq->cqe ) {
517 memset ( arbel_cq->cqe, 0, arbel_cq->cqe_size );
518 for ( i = 0 ; i < cq->num_cqes ; i++ ) {
519 MLX_FILL_1 ( &arbel_cq->cqe[i].normal, 7, owner, 1 );
523 /* Initialise doorbell records */
524 ci_db_rec = &arbel->db_rec[arbel_cq->ci_doorbell_idx].cq_ci;
525 MLX_FILL_1 ( ci_db_rec, 0, counter, 0 );
526 MLX_FILL_2 ( ci_db_rec, 1,
527 res, ARBEL_UAR_RES_CQ_CI,
528 cq_number, cq->cqn );
529 arm_db_rec = &arbel->db_rec[arbel_cq->arm_doorbell_idx].cq_arm;
530 MLX_FILL_1 ( arm_db_rec, 0, counter, 0 );
531 MLX_FILL_2 ( arm_db_rec, 1,
532 res, ARBEL_UAR_RES_CQ_ARM,
533 cq_number, cq->cqn );
535 /* Hand queue over to hardware */
536 memset ( &cqctx, 0, sizeof ( cqctx ) );
537 MLX_FILL_1 ( &cqctx, 0, st, 0xa /* "Event fired" */ );
538 MLX_FILL_1 ( &cqctx, 2, start_address_l,
539 virt_to_bus ( arbel_cq->cqe ) );
540 MLX_FILL_2 ( &cqctx, 3,
541 usr_page, arbel->limits.reserved_uars,
542 log_cq_size, fls ( cq->num_cqes - 1 ) );
543 MLX_FILL_1 ( &cqctx, 5, c_eqn, ARBEL_NO_EQ );
544 MLX_FILL_1 ( &cqctx, 6, pd, ARBEL_GLOBAL_PD );
545 MLX_FILL_1 ( &cqctx, 7, l_key, arbel->reserved_lkey );
546 MLX_FILL_1 ( &cqctx, 12, cqn, cq->cqn );
547 MLX_FILL_1 ( &cqctx, 13,
548 cq_ci_db_record, arbel_cq->ci_doorbell_idx );
549 MLX_FILL_1 ( &cqctx, 14,
550 cq_state_db_record, arbel_cq->arm_doorbell_idx );
551 if ( ( rc = arbel_cmd_sw2hw_cq ( arbel, cq->cqn, &cqctx ) ) != 0 ) {
552 DBGC ( arbel, "Arbel %p SW2HW_CQ failed: %s\n",
553 arbel, strerror ( rc ) );
557 DBGC ( arbel, "Arbel %p CQN %#lx ring at [%p,%p)\n",
558 arbel, cq->cqn, arbel_cq->cqe,
559 ( ( ( void * ) arbel_cq->cqe ) + arbel_cq->cqe_size ) );
560 ib_cq_set_drvdata ( cq, arbel_cq );
564 MLX_FILL_1 ( ci_db_rec, 1, res, ARBEL_UAR_RES_NONE );
565 MLX_FILL_1 ( arm_db_rec, 1, res, ARBEL_UAR_RES_NONE );
566 free_dma ( arbel_cq->cqe, arbel_cq->cqe_size );
570 arbel_free_qn_offset ( arbel->cq_inuse, cqn_offset );
576 * Destroy completion queue
578 * @v ibdev Infiniband device
579 * @v cq Completion queue
581 static void arbel_destroy_cq ( struct ib_device *ibdev,
582 struct ib_completion_queue *cq ) {
583 struct arbel *arbel = ib_get_drvdata ( ibdev );
584 struct arbel_completion_queue *arbel_cq = ib_cq_get_drvdata ( cq );
585 struct arbelprm_completion_queue_context cqctx;
586 struct arbelprm_cq_ci_db_record *ci_db_rec;
587 struct arbelprm_cq_arm_db_record *arm_db_rec;
591 /* Take ownership back from hardware */
592 if ( ( rc = arbel_cmd_hw2sw_cq ( arbel, cq->cqn, &cqctx ) ) != 0 ) {
593 DBGC ( arbel, "Arbel %p FATAL HW2SW_CQ failed on CQN %#lx: "
594 "%s\n", arbel, cq->cqn, strerror ( rc ) );
595 /* Leak memory and return; at least we avoid corruption */
599 /* Clear doorbell records */
600 ci_db_rec = &arbel->db_rec[arbel_cq->ci_doorbell_idx].cq_ci;
601 arm_db_rec = &arbel->db_rec[arbel_cq->arm_doorbell_idx].cq_arm;
602 MLX_FILL_1 ( ci_db_rec, 1, res, ARBEL_UAR_RES_NONE );
603 MLX_FILL_1 ( arm_db_rec, 1, res, ARBEL_UAR_RES_NONE );
606 free_dma ( arbel_cq->cqe, arbel_cq->cqe_size );
609 /* Mark queue number as free */
610 cqn_offset = ( cq->cqn - arbel->limits.reserved_cqs );
611 arbel_free_qn_offset ( arbel->cq_inuse, cqn_offset );
613 ib_cq_set_drvdata ( cq, NULL );
616 /***************************************************************************
618 * Queue pair operations
620 ***************************************************************************
624 * Create send work queue
626 * @v arbel_send_wq Send work queue
627 * @v num_wqes Number of work queue entries
628 * @ret rc Return status code
630 static int arbel_create_send_wq ( struct arbel_send_work_queue *arbel_send_wq,
631 unsigned int num_wqes ) {
632 struct arbelprm_ud_send_wqe *wqe;
633 struct arbelprm_ud_send_wqe *next_wqe;
634 unsigned int wqe_idx_mask;
637 /* Allocate work queue */
638 arbel_send_wq->wqe_size = ( num_wqes *
639 sizeof ( arbel_send_wq->wqe[0] ) );
640 arbel_send_wq->wqe = malloc_dma ( arbel_send_wq->wqe_size,
641 sizeof ( arbel_send_wq->wqe[0] ) );
642 if ( ! arbel_send_wq->wqe )
644 memset ( arbel_send_wq->wqe, 0, arbel_send_wq->wqe_size );
646 /* Link work queue entries */
647 wqe_idx_mask = ( num_wqes - 1 );
648 for ( i = 0 ; i < num_wqes ; i++ ) {
649 wqe = &arbel_send_wq->wqe[i].ud;
650 next_wqe = &arbel_send_wq->wqe[ ( i + 1 ) & wqe_idx_mask ].ud;
651 MLX_FILL_1 ( &wqe->next, 0, nda_31_6,
652 ( virt_to_bus ( next_wqe ) >> 6 ) );
659 * Create receive work queue
661 * @v arbel_recv_wq Receive work queue
662 * @v num_wqes Number of work queue entries
663 * @ret rc Return status code
665 static int arbel_create_recv_wq ( struct arbel_recv_work_queue *arbel_recv_wq,
666 unsigned int num_wqes ) {
667 struct arbelprm_recv_wqe *wqe;
668 struct arbelprm_recv_wqe *next_wqe;
669 unsigned int wqe_idx_mask;
674 /* Allocate work queue */
675 arbel_recv_wq->wqe_size = ( num_wqes *
676 sizeof ( arbel_recv_wq->wqe[0] ) );
677 arbel_recv_wq->wqe = malloc_dma ( arbel_recv_wq->wqe_size,
678 sizeof ( arbel_recv_wq->wqe[0] ) );
679 if ( ! arbel_recv_wq->wqe )
681 memset ( arbel_recv_wq->wqe, 0, arbel_recv_wq->wqe_size );
683 /* Link work queue entries */
684 wqe_idx_mask = ( num_wqes - 1 );
685 nds = ( ( offsetof ( typeof ( *wqe ), data ) +
686 sizeof ( wqe->data[0] ) ) >> 4 );
687 for ( i = 0 ; i < num_wqes ; i++ ) {
688 wqe = &arbel_recv_wq->wqe[i].recv;
689 next_wqe = &arbel_recv_wq->wqe[( i + 1 ) & wqe_idx_mask].recv;
690 MLX_FILL_1 ( &wqe->next, 0, nda_31_6,
691 ( virt_to_bus ( next_wqe ) >> 6 ) );
692 MLX_FILL_1 ( &wqe->next, 1, nds, ( sizeof ( *wqe ) / 16 ) );
693 for ( j = 0 ; ( ( ( void * ) &wqe->data[j] ) <
694 ( ( void * ) ( wqe + 1 ) ) ) ; j++ ) {
695 MLX_FILL_1 ( &wqe->data[j], 1,
696 l_key, ARBEL_INVALID_LKEY );
706 * @v ibdev Infiniband device
708 * @ret rc Return status code
710 static int arbel_create_qp ( struct ib_device *ibdev,
711 struct ib_queue_pair *qp ) {
712 struct arbel *arbel = ib_get_drvdata ( ibdev );
713 struct arbel_queue_pair *arbel_qp;
714 struct arbelprm_qp_ee_state_transitions qpctx;
715 struct arbelprm_qp_db_record *send_db_rec;
716 struct arbelprm_qp_db_record *recv_db_rec;
720 /* Find a free queue pair number */
721 qpn_offset = arbel_alloc_qn_offset ( arbel->qp_inuse, ARBEL_MAX_QPS );
722 if ( qpn_offset < 0 ) {
723 DBGC ( arbel, "Arbel %p out of queue pairs\n", arbel );
727 qp->qpn = ( ARBEL_QPN_BASE + arbel->limits.reserved_qps + qpn_offset );
729 /* Allocate control structures */
730 arbel_qp = zalloc ( sizeof ( *arbel_qp ) );
735 arbel_qp->send.doorbell_idx = arbel_send_doorbell_idx ( qpn_offset );
736 arbel_qp->recv.doorbell_idx = arbel_recv_doorbell_idx ( qpn_offset );
738 /* Create send and receive work queues */
739 if ( ( rc = arbel_create_send_wq ( &arbel_qp->send,
740 qp->send.num_wqes ) ) != 0 )
741 goto err_create_send_wq;
742 if ( ( rc = arbel_create_recv_wq ( &arbel_qp->recv,
743 qp->recv.num_wqes ) ) != 0 )
744 goto err_create_recv_wq;
746 /* Initialise doorbell records */
747 send_db_rec = &arbel->db_rec[arbel_qp->send.doorbell_idx].qp;
748 MLX_FILL_1 ( send_db_rec, 0, counter, 0 );
749 MLX_FILL_2 ( send_db_rec, 1,
750 res, ARBEL_UAR_RES_SQ,
751 qp_number, qp->qpn );
752 recv_db_rec = &arbel->db_rec[arbel_qp->recv.doorbell_idx].qp;
753 MLX_FILL_1 ( recv_db_rec, 0, counter, 0 );
754 MLX_FILL_2 ( recv_db_rec, 1,
755 res, ARBEL_UAR_RES_RQ,
756 qp_number, qp->qpn );
758 /* Hand queue over to hardware */
759 memset ( &qpctx, 0, sizeof ( qpctx ) );
760 MLX_FILL_3 ( &qpctx, 2,
762 qpc_eec_data.pm_state, 0x03 /* Always 0x03 for UD */,
763 qpc_eec_data.st, ARBEL_ST_UD );
764 MLX_FILL_6 ( &qpctx, 4,
765 qpc_eec_data.mtu, ARBEL_MTU_2048,
766 qpc_eec_data.msg_max, 11 /* 2^11 = 2048 */,
767 qpc_eec_data.log_rq_size, fls ( qp->recv.num_wqes - 1 ),
768 qpc_eec_data.log_rq_stride,
769 ( fls ( sizeof ( arbel_qp->recv.wqe[0] ) - 1 ) - 4 ),
770 qpc_eec_data.log_sq_size, fls ( qp->send.num_wqes - 1 ),
771 qpc_eec_data.log_sq_stride,
772 ( fls ( sizeof ( arbel_qp->send.wqe[0] ) - 1 ) - 4 ) );
773 MLX_FILL_1 ( &qpctx, 5,
774 qpc_eec_data.usr_page, arbel->limits.reserved_uars );
775 MLX_FILL_1 ( &qpctx, 10, qpc_eec_data.primary_address_path.port_number,
777 MLX_FILL_1 ( &qpctx, 27, qpc_eec_data.pd, ARBEL_GLOBAL_PD );
778 MLX_FILL_1 ( &qpctx, 29, qpc_eec_data.wqe_lkey, arbel->reserved_lkey );
779 MLX_FILL_1 ( &qpctx, 30, qpc_eec_data.ssc, 1 );
780 MLX_FILL_1 ( &qpctx, 33, qpc_eec_data.cqn_snd, qp->send.cq->cqn );
781 MLX_FILL_1 ( &qpctx, 34, qpc_eec_data.snd_wqe_base_adr_l,
782 ( virt_to_bus ( arbel_qp->send.wqe ) >> 6 ) );
783 MLX_FILL_1 ( &qpctx, 35, qpc_eec_data.snd_db_record_index,
784 arbel_qp->send.doorbell_idx );
785 MLX_FILL_1 ( &qpctx, 38, qpc_eec_data.rsc, 1 );
786 MLX_FILL_1 ( &qpctx, 41, qpc_eec_data.cqn_rcv, qp->recv.cq->cqn );
787 MLX_FILL_1 ( &qpctx, 42, qpc_eec_data.rcv_wqe_base_adr_l,
788 ( virt_to_bus ( arbel_qp->recv.wqe ) >> 6 ) );
789 MLX_FILL_1 ( &qpctx, 43, qpc_eec_data.rcv_db_record_index,
790 arbel_qp->recv.doorbell_idx );
791 MLX_FILL_1 ( &qpctx, 44, qpc_eec_data.q_key, qp->qkey );
792 if ( ( rc = arbel_cmd_rst2init_qpee ( arbel, qp->qpn, &qpctx )) != 0 ){
793 DBGC ( arbel, "Arbel %p RST2INIT_QPEE failed: %s\n",
794 arbel, strerror ( rc ) );
795 goto err_rst2init_qpee;
797 memset ( &qpctx, 0, sizeof ( qpctx ) );
798 MLX_FILL_2 ( &qpctx, 4,
799 qpc_eec_data.mtu, ARBEL_MTU_2048,
800 qpc_eec_data.msg_max, 11 /* 2^11 = 2048 */ );
801 if ( ( rc = arbel_cmd_init2rtr_qpee ( arbel, qp->qpn, &qpctx )) != 0 ){
802 DBGC ( arbel, "Arbel %p INIT2RTR_QPEE failed: %s\n",
803 arbel, strerror ( rc ) );
804 goto err_init2rtr_qpee;
806 memset ( &qpctx, 0, sizeof ( qpctx ) );
807 if ( ( rc = arbel_cmd_rtr2rts_qpee ( arbel, qp->qpn, &qpctx ) ) != 0 ){
808 DBGC ( arbel, "Arbel %p RTR2RTS_QPEE failed: %s\n",
809 arbel, strerror ( rc ) );
810 goto err_rtr2rts_qpee;
813 DBGC ( arbel, "Arbel %p QPN %#lx send ring at [%p,%p)\n",
814 arbel, qp->qpn, arbel_qp->send.wqe,
815 ( ( (void *) arbel_qp->send.wqe ) + arbel_qp->send.wqe_size ) );
816 DBGC ( arbel, "Arbel %p QPN %#lx receive ring at [%p,%p)\n",
817 arbel, qp->qpn, arbel_qp->recv.wqe,
818 ( ( (void *) arbel_qp->recv.wqe ) + arbel_qp->recv.wqe_size ) );
819 ib_qp_set_drvdata ( qp, arbel_qp );
824 arbel_cmd_2rst_qpee ( arbel, qp->qpn );
826 MLX_FILL_1 ( send_db_rec, 1, res, ARBEL_UAR_RES_NONE );
827 MLX_FILL_1 ( recv_db_rec, 1, res, ARBEL_UAR_RES_NONE );
828 free_dma ( arbel_qp->recv.wqe, arbel_qp->recv.wqe_size );
830 free_dma ( arbel_qp->send.wqe, arbel_qp->send.wqe_size );
834 arbel_free_qn_offset ( arbel->qp_inuse, qpn_offset );
842 * @v ibdev Infiniband device
845 static void arbel_destroy_qp ( struct ib_device *ibdev,
846 struct ib_queue_pair *qp ) {
847 struct arbel *arbel = ib_get_drvdata ( ibdev );
848 struct arbel_queue_pair *arbel_qp = ib_qp_get_drvdata ( qp );
849 struct arbelprm_qp_db_record *send_db_rec;
850 struct arbelprm_qp_db_record *recv_db_rec;
854 /* Take ownership back from hardware */
855 if ( ( rc = arbel_cmd_2rst_qpee ( arbel, qp->qpn ) ) != 0 ) {
856 DBGC ( arbel, "Arbel %p FATAL 2RST_QPEE failed on QPN %#lx: "
857 "%s\n", arbel, qp->qpn, strerror ( rc ) );
858 /* Leak memory and return; at least we avoid corruption */
862 /* Clear doorbell records */
863 send_db_rec = &arbel->db_rec[arbel_qp->send.doorbell_idx].qp;
864 recv_db_rec = &arbel->db_rec[arbel_qp->recv.doorbell_idx].qp;
865 MLX_FILL_1 ( send_db_rec, 1, res, ARBEL_UAR_RES_NONE );
866 MLX_FILL_1 ( recv_db_rec, 1, res, ARBEL_UAR_RES_NONE );
869 free_dma ( arbel_qp->send.wqe, arbel_qp->send.wqe_size );
870 free_dma ( arbel_qp->recv.wqe, arbel_qp->recv.wqe_size );
873 /* Mark queue number as free */
874 qpn_offset = ( qp->qpn - ARBEL_QPN_BASE - arbel->limits.reserved_qps );
875 arbel_free_qn_offset ( arbel->qp_inuse, qpn_offset );
877 ib_qp_set_drvdata ( qp, NULL );
880 /***************************************************************************
882 * Work request operations
884 ***************************************************************************
888 * Ring doorbell register in UAR
890 * @v arbel Arbel device
891 * @v db_reg Doorbell register structure
892 * @v offset Address of doorbell
894 static void arbel_ring_doorbell ( struct arbel *arbel,
895 union arbelprm_doorbell_register *db_reg,
896 unsigned int offset ) {
898 DBGC2 ( arbel, "Arbel %p ringing doorbell %08lx:%08lx at %lx\n",
899 arbel, db_reg->dword[0], db_reg->dword[1],
900 virt_to_phys ( arbel->uar + offset ) );
903 writel ( db_reg->dword[0], ( arbel->uar + offset + 0 ) );
905 writel ( db_reg->dword[1], ( arbel->uar + offset + 4 ) );
908 /** GID used for GID-less send work queue entries */
909 static const struct ib_gid arbel_no_gid = {
910 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0 } }
914 * Post send work queue entry
916 * @v ibdev Infiniband device
918 * @v av Address vector
919 * @v iobuf I/O buffer
920 * @ret rc Return status code
922 static int arbel_post_send ( struct ib_device *ibdev,
923 struct ib_queue_pair *qp,
924 struct ib_address_vector *av,
925 struct io_buffer *iobuf ) {
926 struct arbel *arbel = ib_get_drvdata ( ibdev );
927 struct arbel_queue_pair *arbel_qp = ib_qp_get_drvdata ( qp );
928 struct ib_work_queue *wq = &qp->send;
929 struct arbel_send_work_queue *arbel_send_wq = &arbel_qp->send;
930 struct arbelprm_ud_send_wqe *prev_wqe;
931 struct arbelprm_ud_send_wqe *wqe;
932 struct arbelprm_qp_db_record *qp_db_rec;
933 union arbelprm_doorbell_register db_reg;
934 const struct ib_gid *gid;
935 unsigned int wqe_idx_mask;
938 /* Allocate work queue entry */
939 wqe_idx_mask = ( wq->num_wqes - 1 );
940 if ( wq->iobufs[wq->next_idx & wqe_idx_mask] ) {
941 DBGC ( arbel, "Arbel %p send queue full", arbel );
944 wq->iobufs[wq->next_idx & wqe_idx_mask] = iobuf;
945 prev_wqe = &arbel_send_wq->wqe[(wq->next_idx - 1) & wqe_idx_mask].ud;
946 wqe = &arbel_send_wq->wqe[wq->next_idx & wqe_idx_mask].ud;
948 /* Construct work queue entry */
949 MLX_FILL_1 ( &wqe->next, 1, always1, 1 );
950 memset ( &wqe->ctrl, 0, sizeof ( wqe->ctrl ) );
951 MLX_FILL_1 ( &wqe->ctrl, 0, always1, 1 );
952 memset ( &wqe->ud, 0, sizeof ( wqe->ud ) );
953 MLX_FILL_2 ( &wqe->ud, 0,
954 ud_address_vector.pd, ARBEL_GLOBAL_PD,
955 ud_address_vector.port_number, ibdev->port );
956 MLX_FILL_2 ( &wqe->ud, 1,
957 ud_address_vector.rlid, av->dlid,
958 ud_address_vector.g, av->gid_present );
959 MLX_FILL_2 ( &wqe->ud, 2,
960 ud_address_vector.max_stat_rate,
961 ( ( av->rate >= 3 ) ? 0 : 1 ),
962 ud_address_vector.msg, 3 );
963 MLX_FILL_1 ( &wqe->ud, 3, ud_address_vector.sl, av->sl );
964 gid = ( av->gid_present ? &av->gid : &arbel_no_gid );
965 memcpy ( &wqe->ud.u.dwords[4], gid, sizeof ( *gid ) );
966 MLX_FILL_1 ( &wqe->ud, 8, destination_qp, av->dest_qp );
967 MLX_FILL_1 ( &wqe->ud, 9, q_key, av->qkey );
968 MLX_FILL_1 ( &wqe->data[0], 0, byte_count, iob_len ( iobuf ) );
969 MLX_FILL_1 ( &wqe->data[0], 1, l_key, arbel->reserved_lkey );
970 MLX_FILL_1 ( &wqe->data[0], 3,
971 local_address_l, virt_to_bus ( iobuf->data ) );
973 /* Update previous work queue entry's "next" field */
974 nds = ( ( offsetof ( typeof ( *wqe ), data ) +
975 sizeof ( wqe->data[0] ) ) >> 4 );
976 MLX_SET ( &prev_wqe->next, nopcode, ARBEL_OPCODE_SEND );
977 MLX_FILL_3 ( &prev_wqe->next, 1,
982 /* Update doorbell record */
984 qp_db_rec = &arbel->db_rec[arbel_send_wq->doorbell_idx].qp;
985 MLX_FILL_1 ( qp_db_rec, 0,
986 counter, ( ( wq->next_idx + 1 ) & 0xffff ) );
988 /* Ring doorbell register */
989 MLX_FILL_4 ( &db_reg.send, 0,
990 nopcode, ARBEL_OPCODE_SEND,
992 wqe_counter, ( wq->next_idx & 0xffff ),
994 MLX_FILL_2 ( &db_reg.send, 1,
997 arbel_ring_doorbell ( arbel, &db_reg, ARBEL_DB_POST_SND_OFFSET );
999 /* Update work queue's index */
1006 * Post receive work queue entry
1008 * @v ibdev Infiniband device
1010 * @v iobuf I/O buffer
1011 * @ret rc Return status code
1013 static int arbel_post_recv ( struct ib_device *ibdev,
1014 struct ib_queue_pair *qp,
1015 struct io_buffer *iobuf ) {
1016 struct arbel *arbel = ib_get_drvdata ( ibdev );
1017 struct arbel_queue_pair *arbel_qp = ib_qp_get_drvdata ( qp );
1018 struct ib_work_queue *wq = &qp->recv;
1019 struct arbel_recv_work_queue *arbel_recv_wq = &arbel_qp->recv;
1020 struct arbelprm_recv_wqe *wqe;
1021 union arbelprm_doorbell_record *db_rec;
1022 unsigned int wqe_idx_mask;
1024 /* Allocate work queue entry */
1025 wqe_idx_mask = ( wq->num_wqes - 1 );
1026 if ( wq->iobufs[wq->next_idx & wqe_idx_mask] ) {
1027 DBGC ( arbel, "Arbel %p receive queue full", arbel );
1030 wq->iobufs[wq->next_idx & wqe_idx_mask] = iobuf;
1031 wqe = &arbel_recv_wq->wqe[wq->next_idx & wqe_idx_mask].recv;
1033 /* Construct work queue entry */
1034 MLX_FILL_1 ( &wqe->data[0], 0, byte_count, iob_tailroom ( iobuf ) );
1035 MLX_FILL_1 ( &wqe->data[0], 1, l_key, arbel->reserved_lkey );
1036 MLX_FILL_1 ( &wqe->data[0], 3,
1037 local_address_l, virt_to_bus ( iobuf->data ) );
1039 /* Update doorbell record */
1041 db_rec = &arbel->db_rec[arbel_recv_wq->doorbell_idx];
1042 MLX_FILL_1 ( &db_rec->qp, 0,
1043 counter, ( ( wq->next_idx + 1 ) & 0xffff ) );
1045 /* Update work queue's index */
1054 * @v ibdev Infiniband device
1055 * @v cq Completion queue
1056 * @v cqe Hardware completion queue entry
1057 * @v complete_send Send completion handler
1058 * @v complete_recv Receive completion handler
1059 * @ret rc Return status code
1061 static int arbel_complete ( struct ib_device *ibdev,
1062 struct ib_completion_queue *cq,
1063 union arbelprm_completion_entry *cqe,
1064 ib_completer_t complete_send,
1065 ib_completer_t complete_recv ) {
1066 struct arbel *arbel = ib_get_drvdata ( ibdev );
1067 struct ib_completion completion;
1068 struct ib_work_queue *wq;
1069 struct ib_queue_pair *qp;
1070 struct arbel_queue_pair *arbel_qp;
1071 struct arbel_send_work_queue *arbel_send_wq;
1072 struct arbel_recv_work_queue *arbel_recv_wq;
1073 struct arbelprm_recv_wqe *recv_wqe;
1074 struct io_buffer *iobuf;
1075 ib_completer_t complete;
1076 unsigned int opcode;
1079 unsigned long wqe_adr;
1080 unsigned int wqe_idx;
1083 /* Parse completion */
1084 memset ( &completion, 0, sizeof ( completion ) );
1085 qpn = MLX_GET ( &cqe->normal, my_qpn );
1086 is_send = MLX_GET ( &cqe->normal, s );
1087 wqe_adr = ( MLX_GET ( &cqe->normal, wqe_adr ) << 6 );
1088 opcode = MLX_GET ( &cqe->normal, opcode );
1089 if ( opcode >= ARBEL_OPCODE_RECV_ERROR ) {
1090 /* "s" field is not valid for error opcodes */
1091 is_send = ( opcode == ARBEL_OPCODE_SEND_ERROR );
1092 completion.syndrome = MLX_GET ( &cqe->error, syndrome );
1093 DBGC ( arbel, "Arbel %p CPN %lx syndrome %x vendor %lx\n",
1094 arbel, cq->cqn, completion.syndrome,
1095 MLX_GET ( &cqe->error, vendor_code ) );
1097 /* Don't return immediately; propagate error to completer */
1100 /* Identify work queue */
1101 wq = ib_find_wq ( cq, qpn, is_send );
1103 DBGC ( arbel, "Arbel %p CQN %lx unknown %s QPN %lx\n",
1104 arbel, cq->cqn, ( is_send ? "send" : "recv" ), qpn );
1108 arbel_qp = ib_qp_get_drvdata ( qp );
1109 arbel_send_wq = &arbel_qp->send;
1110 arbel_recv_wq = &arbel_qp->recv;
1112 /* Identify work queue entry index */
1114 wqe_idx = ( ( wqe_adr - virt_to_bus ( arbel_send_wq->wqe ) ) /
1115 sizeof ( arbel_send_wq->wqe[0] ) );
1116 assert ( wqe_idx < qp->send.num_wqes );
1118 wqe_idx = ( ( wqe_adr - virt_to_bus ( arbel_recv_wq->wqe ) ) /
1119 sizeof ( arbel_recv_wq->wqe[0] ) );
1120 assert ( wqe_idx < qp->recv.num_wqes );
1123 /* Identify I/O buffer */
1124 iobuf = wq->iobufs[wqe_idx];
1126 DBGC ( arbel, "Arbel %p CQN %lx QPN %lx empty WQE %x\n",
1127 arbel, cq->cqn, qpn, wqe_idx );
1130 wq->iobufs[wqe_idx] = NULL;
1132 /* Fill in length for received packets */
1134 completion.len = MLX_GET ( &cqe->normal, byte_cnt );
1135 recv_wqe = &arbel_recv_wq->wqe[wqe_idx].recv;
1136 assert ( MLX_GET ( &recv_wqe->data[0], local_address_l ) ==
1137 virt_to_bus ( iobuf->data ) );
1138 assert ( MLX_GET ( &recv_wqe->data[0], byte_count ) ==
1139 iob_tailroom ( iobuf ) );
1140 MLX_FILL_1 ( &recv_wqe->data[0], 0, byte_count, 0 );
1141 MLX_FILL_1 ( &recv_wqe->data[0], 1,
1142 l_key, ARBEL_INVALID_LKEY );
1143 if ( completion.len > iob_tailroom ( iobuf ) ) {
1144 DBGC ( arbel, "Arbel %p CQN %lx QPN %lx IDX %x "
1145 "overlength received packet length %zd\n",
1146 arbel, cq->cqn, qpn, wqe_idx, completion.len );
1151 /* Pass off to caller's completion handler */
1152 complete = ( is_send ? complete_send : complete_recv );
1153 complete ( ibdev, qp, &completion, iobuf );
1159 * Poll completion queue
1161 * @v ibdev Infiniband device
1162 * @v cq Completion queue
1163 * @v complete_send Send completion handler
1164 * @v complete_recv Receive completion handler
1166 static void arbel_poll_cq ( struct ib_device *ibdev,
1167 struct ib_completion_queue *cq,
1168 ib_completer_t complete_send,
1169 ib_completer_t complete_recv ) {
1170 struct arbel *arbel = ib_get_drvdata ( ibdev );
1171 struct arbel_completion_queue *arbel_cq = ib_cq_get_drvdata ( cq );
1172 struct arbelprm_cq_ci_db_record *ci_db_rec;
1173 union arbelprm_completion_entry *cqe;
1174 unsigned int cqe_idx_mask;
1178 /* Look for completion entry */
1179 cqe_idx_mask = ( cq->num_cqes - 1 );
1180 cqe = &arbel_cq->cqe[cq->next_idx & cqe_idx_mask];
1181 if ( MLX_GET ( &cqe->normal, owner ) != 0 ) {
1182 /* Entry still owned by hardware; end of poll */
1186 /* Handle completion */
1187 if ( ( rc = arbel_complete ( ibdev, cq, cqe, complete_send,
1188 complete_recv ) ) != 0 ) {
1189 DBGC ( arbel, "Arbel %p failed to complete: %s\n",
1190 arbel, strerror ( rc ) );
1191 DBGC_HD ( arbel, cqe, sizeof ( *cqe ) );
1194 /* Return ownership to hardware */
1195 MLX_FILL_1 ( &cqe->normal, 7, owner, 1 );
1197 /* Update completion queue's index */
1199 /* Update doorbell record */
1200 ci_db_rec = &arbel->db_rec[arbel_cq->ci_doorbell_idx].cq_ci;
1201 MLX_FILL_1 ( ci_db_rec, 0,
1202 counter, ( cq->next_idx & 0xffffffffUL ) );
1206 /***************************************************************************
1208 * Infiniband link-layer operations
1210 ***************************************************************************
1214 * Initialise Infiniband link
1216 * @v ibdev Infiniband device
1217 * @ret rc Return status code
1219 static int arbel_open ( struct ib_device *ibdev ) {
1220 struct arbel *arbel = ib_get_drvdata ( ibdev );
1221 struct arbelprm_init_ib init_ib;
1224 memset ( &init_ib, 0, sizeof ( init_ib ) );
1225 MLX_FILL_3 ( &init_ib, 0,
1226 mtu_cap, ARBEL_MTU_2048,
1229 MLX_FILL_1 ( &init_ib, 1, max_gid, 1 );
1230 MLX_FILL_1 ( &init_ib, 2, max_pkey, 64 );
1231 if ( ( rc = arbel_cmd_init_ib ( arbel, ibdev->port,
1232 &init_ib ) ) != 0 ) {
1233 DBGC ( arbel, "Arbel %p could not intialise IB: %s\n",
1234 arbel, strerror ( rc ) );
1242 * Close Infiniband link
1244 * @v ibdev Infiniband device
1246 static void arbel_close ( struct ib_device *ibdev ) {
1247 struct arbel *arbel = ib_get_drvdata ( ibdev );
1250 if ( ( rc = arbel_cmd_close_ib ( arbel, ibdev->port ) ) != 0 ) {
1251 DBGC ( arbel, "Arbel %p could not close IB: %s\n",
1252 arbel, strerror ( rc ) );
1253 /* Nothing we can do about this */
1257 /***************************************************************************
1259 * Multicast group operations
1261 ***************************************************************************
1265 * Attach to multicast group
1267 * @v ibdev Infiniband device
1269 * @v gid Multicast GID
1270 * @ret rc Return status code
1272 static int arbel_mcast_attach ( struct ib_device *ibdev,
1273 struct ib_queue_pair *qp,
1274 struct ib_gid *gid ) {
1275 struct arbel *arbel = ib_get_drvdata ( ibdev );
1276 struct arbelprm_mgm_hash hash;
1277 struct arbelprm_mgm_entry mgm;
1281 /* Generate hash table index */
1282 if ( ( rc = arbel_cmd_mgid_hash ( arbel, gid, &hash ) ) != 0 ) {
1283 DBGC ( arbel, "Arbel %p could not hash GID: %s\n",
1284 arbel, strerror ( rc ) );
1287 index = MLX_GET ( &hash, hash );
1289 /* Check for existing hash table entry */
1290 if ( ( rc = arbel_cmd_read_mgm ( arbel, index, &mgm ) ) != 0 ) {
1291 DBGC ( arbel, "Arbel %p could not read MGM %#x: %s\n",
1292 arbel, index, strerror ( rc ) );
1295 if ( MLX_GET ( &mgm, mgmqp_0.qi ) != 0 ) {
1296 /* FIXME: this implementation allows only a single QP
1297 * per multicast group, and doesn't handle hash
1298 * collisions. Sufficient for IPoIB but may need to
1299 * be extended in future.
1301 DBGC ( arbel, "Arbel %p MGID index %#x already in use\n",
1306 /* Update hash table entry */
1307 MLX_FILL_2 ( &mgm, 8,
1308 mgmqp_0.qpn_i, qp->qpn,
1310 memcpy ( &mgm.u.dwords[4], gid, sizeof ( *gid ) );
1311 if ( ( rc = arbel_cmd_write_mgm ( arbel, index, &mgm ) ) != 0 ) {
1312 DBGC ( arbel, "Arbel %p could not write MGM %#x: %s\n",
1313 arbel, index, strerror ( rc ) );
1321 * Detach from multicast group
1323 * @v ibdev Infiniband device
1325 * @v gid Multicast GID
1327 static void arbel_mcast_detach ( struct ib_device *ibdev,
1328 struct ib_queue_pair *qp __unused,
1329 struct ib_gid *gid ) {
1330 struct arbel *arbel = ib_get_drvdata ( ibdev );
1331 struct arbelprm_mgm_hash hash;
1332 struct arbelprm_mgm_entry mgm;
1336 /* Generate hash table index */
1337 if ( ( rc = arbel_cmd_mgid_hash ( arbel, gid, &hash ) ) != 0 ) {
1338 DBGC ( arbel, "Arbel %p could not hash GID: %s\n",
1339 arbel, strerror ( rc ) );
1342 index = MLX_GET ( &hash, hash );
1344 /* Clear hash table entry */
1345 memset ( &mgm, 0, sizeof ( mgm ) );
1346 if ( ( rc = arbel_cmd_write_mgm ( arbel, index, &mgm ) ) != 0 ) {
1347 DBGC ( arbel, "Arbel %p could not write MGM %#x: %s\n",
1348 arbel, index, strerror ( rc ) );
1353 /***************************************************************************
1357 ***************************************************************************
1361 * Issue management datagram
1363 * @v ibdev Infiniband device
1364 * @v mad Management datagram
1365 * @v len Length of management datagram
1366 * @ret rc Return status code
1368 static int arbel_mad ( struct ib_device *ibdev, struct ib_mad_hdr *mad,
1370 struct arbel *arbel = ib_get_drvdata ( ibdev );
1371 union arbelprm_mad mad_ifc;
1374 /* Copy in request packet */
1375 memset ( &mad_ifc, 0, sizeof ( mad_ifc ) );
1376 assert ( len <= sizeof ( mad_ifc.mad ) );
1377 memcpy ( &mad_ifc.mad, mad, len );
1380 if ( ( rc = arbel_cmd_mad_ifc ( arbel, ibdev->port,
1381 &mad_ifc ) ) != 0 ) {
1382 DBGC ( arbel, "Arbel %p could not issue MAD IFC: %s\n",
1383 arbel, strerror ( rc ) );
1387 /* Copy out reply packet */
1388 memcpy ( mad, &mad_ifc.mad, len );
1390 if ( mad->status != 0 ) {
1391 DBGC ( arbel, "Arbel %p MAD IFC status %04x\n",
1392 arbel, ntohs ( mad->status ) );
1398 /** Arbel Infiniband operations */
1399 static struct ib_device_operations arbel_ib_operations = {
1400 .create_cq = arbel_create_cq,
1401 .destroy_cq = arbel_destroy_cq,
1402 .create_qp = arbel_create_qp,
1403 .destroy_qp = arbel_destroy_qp,
1404 .post_send = arbel_post_send,
1405 .post_recv = arbel_post_recv,
1406 .poll_cq = arbel_poll_cq,
1408 .close = arbel_close,
1409 .mcast_attach = arbel_mcast_attach,
1410 .mcast_detach = arbel_mcast_detach,
1414 /***************************************************************************
1418 ***************************************************************************
1422 * Start firmware running
1424 * @v arbel Arbel device
1425 * @ret rc Return status code
1427 static int arbel_start_firmware ( struct arbel *arbel ) {
1428 struct arbelprm_query_fw fw;
1429 struct arbelprm_access_lam lam;
1430 struct arbelprm_virtual_physical_mapping map_fa;
1431 unsigned int fw_pages;
1432 unsigned int log2_fw_pages;
1437 /* Get firmware parameters */
1438 if ( ( rc = arbel_cmd_query_fw ( arbel, &fw ) ) != 0 ) {
1439 DBGC ( arbel, "Arbel %p could not query firmware: %s\n",
1440 arbel, strerror ( rc ) );
1443 DBGC ( arbel, "Arbel %p firmware version %ld.%ld.%ld\n", arbel,
1444 MLX_GET ( &fw, fw_rev_major ), MLX_GET ( &fw, fw_rev_minor ),
1445 MLX_GET ( &fw, fw_rev_subminor ) );
1446 fw_pages = MLX_GET ( &fw, fw_pages );
1447 log2_fw_pages = fls ( fw_pages - 1 );
1448 fw_pages = ( 1 << log2_fw_pages );
1449 DBGC ( arbel, "Arbel %p requires %d kB for firmware\n",
1450 arbel, ( fw_pages * 4 ) );
1452 /* Enable locally-attached memory. Ignore failure; there may
1453 * be no attached memory.
1455 arbel_cmd_enable_lam ( arbel, &lam );
1457 /* Allocate firmware pages and map firmware area */
1458 fw_size = ( fw_pages * 4096 );
1459 arbel->firmware_area = umalloc ( fw_size );
1460 if ( ! arbel->firmware_area ) {
1464 fw_base = ( user_to_phys ( arbel->firmware_area, fw_size ) &
1466 DBGC ( arbel, "Arbel %p firmware area at physical [%lx,%lx)\n",
1467 arbel, fw_base, ( fw_base + fw_size ) );
1468 memset ( &map_fa, 0, sizeof ( map_fa ) );
1469 MLX_FILL_2 ( &map_fa, 3,
1470 log2size, log2_fw_pages,
1471 pa_l, ( fw_base >> 12 ) );
1472 if ( ( rc = arbel_cmd_map_fa ( arbel, &map_fa ) ) != 0 ) {
1473 DBGC ( arbel, "Arbel %p could not map firmware: %s\n",
1474 arbel, strerror ( rc ) );
1478 /* Start firmware */
1479 if ( ( rc = arbel_cmd_run_fw ( arbel ) ) != 0 ) {
1480 DBGC ( arbel, "Arbel %p could not run firmware: %s\n",
1481 arbel, strerror ( rc ) );
1485 DBGC ( arbel, "Arbel %p firmware started\n", arbel );
1489 arbel_cmd_unmap_fa ( arbel );
1491 ufree ( arbel->firmware_area );
1492 arbel->firmware_area = UNULL;
1499 * Stop firmware running
1501 * @v arbel Arbel device
1503 static void arbel_stop_firmware ( struct arbel *arbel ) {
1506 if ( ( rc = arbel_cmd_unmap_fa ( arbel ) ) != 0 ) {
1507 DBGC ( arbel, "Arbel %p FATAL could not stop firmware: %s\n",
1508 arbel, strerror ( rc ) );
1509 /* Leak memory and return; at least we avoid corruption */
1512 ufree ( arbel->firmware_area );
1513 arbel->firmware_area = UNULL;
1516 /***************************************************************************
1518 * Infinihost Context Memory management
1520 ***************************************************************************
1526 * @v arbel Arbel device
1527 * @ret rc Return status code
1529 static int arbel_get_limits ( struct arbel *arbel ) {
1530 struct arbelprm_query_dev_lim dev_lim;
1533 if ( ( rc = arbel_cmd_query_dev_lim ( arbel, &dev_lim ) ) != 0 ) {
1534 DBGC ( arbel, "Arbel %p could not get device limits: %s\n",
1535 arbel, strerror ( rc ) );
1539 arbel->limits.reserved_qps =
1540 ( 1 << MLX_GET ( &dev_lim, log2_rsvd_qps ) );
1541 arbel->limits.qpc_entry_size = MLX_GET ( &dev_lim, qpc_entry_sz );
1542 arbel->limits.eqpc_entry_size = MLX_GET ( &dev_lim, eqpc_entry_sz );
1543 arbel->limits.reserved_srqs =
1544 ( 1 << MLX_GET ( &dev_lim, log2_rsvd_srqs ) );
1545 arbel->limits.srqc_entry_size = MLX_GET ( &dev_lim, srq_entry_sz );
1546 arbel->limits.reserved_ees =
1547 ( 1 << MLX_GET ( &dev_lim, log2_rsvd_ees ) );
1548 arbel->limits.eec_entry_size = MLX_GET ( &dev_lim, eec_entry_sz );
1549 arbel->limits.eeec_entry_size = MLX_GET ( &dev_lim, eeec_entry_sz );
1550 arbel->limits.reserved_cqs =
1551 ( 1 << MLX_GET ( &dev_lim, log2_rsvd_cqs ) );
1552 arbel->limits.cqc_entry_size = MLX_GET ( &dev_lim, cqc_entry_sz );
1553 arbel->limits.reserved_mtts =
1554 ( 1 << MLX_GET ( &dev_lim, log2_rsvd_mtts ) );
1555 arbel->limits.mtt_entry_size = MLX_GET ( &dev_lim, mtt_entry_sz );
1556 arbel->limits.reserved_mrws =
1557 ( 1 << MLX_GET ( &dev_lim, log2_rsvd_mrws ) );
1558 arbel->limits.mpt_entry_size = MLX_GET ( &dev_lim, mpt_entry_sz );
1559 arbel->limits.reserved_rdbs =
1560 ( 1 << MLX_GET ( &dev_lim, log2_rsvd_rdbs ) );
1561 arbel->limits.eqc_entry_size = MLX_GET ( &dev_lim, eqc_entry_sz );
1562 arbel->limits.reserved_uars = MLX_GET ( &dev_lim, num_rsvd_uars );
1570 * @v log_num_entries Log2 of the number of entries
1571 * @v entry_size Entry size
1572 * @ret usage Usage size in ICM
1574 static size_t icm_usage ( unsigned int log_num_entries, size_t entry_size ) {
1577 usage = ( ( 1 << log_num_entries ) * entry_size );
1578 usage = ( ( usage + 4095 ) & ~4095 );
1585 * @v arbel Arbel device
1586 * @v init_hca INIT_HCA structure to fill in
1587 * @ret rc Return status code
1589 static int arbel_alloc_icm ( struct arbel *arbel,
1590 struct arbelprm_init_hca *init_hca ) {
1591 struct arbelprm_scalar_parameter icm_size;
1592 struct arbelprm_scalar_parameter icm_aux_size;
1593 struct arbelprm_virtual_physical_mapping map_icm_aux;
1594 struct arbelprm_virtual_physical_mapping map_icm;
1595 union arbelprm_doorbell_record *db_rec;
1596 size_t icm_offset = 0;
1597 unsigned int log_num_qps, log_num_srqs, log_num_ees, log_num_cqs;
1598 unsigned int log_num_mtts, log_num_mpts, log_num_rdbs, log_num_eqs;
1601 icm_offset = ( ( arbel->limits.reserved_uars + 1 ) << 12 );
1603 /* Queue pair contexts */
1604 log_num_qps = fls ( arbel->limits.reserved_qps + ARBEL_MAX_QPS - 1 );
1605 MLX_FILL_2 ( init_hca, 13,
1606 qpc_eec_cqc_eqc_rdb_parameters.qpc_base_addr_l,
1607 ( icm_offset >> 7 ),
1608 qpc_eec_cqc_eqc_rdb_parameters.log_num_of_qp,
1610 DBGC ( arbel, "Arbel %p ICM QPC base = %zx\n", arbel, icm_offset );
1611 icm_offset += icm_usage ( log_num_qps, arbel->limits.qpc_entry_size );
1613 /* Extended queue pair contexts */
1614 MLX_FILL_1 ( init_hca, 25,
1615 qpc_eec_cqc_eqc_rdb_parameters.eqpc_base_addr_l,
1617 DBGC ( arbel, "Arbel %p ICM EQPC base = %zx\n", arbel, icm_offset );
1618 // icm_offset += icm_usage ( log_num_qps, arbel->limits.eqpc_entry_size );
1619 icm_offset += icm_usage ( log_num_qps, arbel->limits.qpc_entry_size );
1621 /* Shared receive queue contexts */
1622 log_num_srqs = fls ( arbel->limits.reserved_srqs - 1 );
1623 MLX_FILL_2 ( init_hca, 19,
1624 qpc_eec_cqc_eqc_rdb_parameters.srqc_base_addr_l,
1625 ( icm_offset >> 5 ),
1626 qpc_eec_cqc_eqc_rdb_parameters.log_num_of_srq,
1628 DBGC ( arbel, "Arbel %p ICM SRQC base = %zx\n", arbel, icm_offset );
1629 icm_offset += icm_usage ( log_num_srqs, arbel->limits.srqc_entry_size );
1631 /* End-to-end contexts */
1632 log_num_ees = fls ( arbel->limits.reserved_ees - 1 );
1633 MLX_FILL_2 ( init_hca, 17,
1634 qpc_eec_cqc_eqc_rdb_parameters.eec_base_addr_l,
1635 ( icm_offset >> 7 ),
1636 qpc_eec_cqc_eqc_rdb_parameters.log_num_of_ee,
1638 DBGC ( arbel, "Arbel %p ICM EEC base = %zx\n", arbel, icm_offset );
1639 icm_offset += icm_usage ( log_num_ees, arbel->limits.eec_entry_size );
1641 /* Extended end-to-end contexts */
1642 MLX_FILL_1 ( init_hca, 29,
1643 qpc_eec_cqc_eqc_rdb_parameters.eeec_base_addr_l,
1645 DBGC ( arbel, "Arbel %p ICM EEEC base = %zx\n", arbel, icm_offset );
1646 icm_offset += icm_usage ( log_num_ees, arbel->limits.eeec_entry_size );
1648 /* Completion queue contexts */
1649 log_num_cqs = fls ( arbel->limits.reserved_cqs + ARBEL_MAX_CQS - 1 );
1650 MLX_FILL_2 ( init_hca, 21,
1651 qpc_eec_cqc_eqc_rdb_parameters.cqc_base_addr_l,
1652 ( icm_offset >> 6 ),
1653 qpc_eec_cqc_eqc_rdb_parameters.log_num_of_cq,
1655 DBGC ( arbel, "Arbel %p ICM CQC base = %zx\n", arbel, icm_offset );
1656 icm_offset += icm_usage ( log_num_cqs, arbel->limits.cqc_entry_size );
1658 /* Memory translation table */
1659 log_num_mtts = fls ( arbel->limits.reserved_mtts - 1 );
1660 MLX_FILL_1 ( init_hca, 65,
1661 tpt_parameters.mtt_base_addr_l, icm_offset );
1662 DBGC ( arbel, "Arbel %p ICM MTT base = %zx\n", arbel, icm_offset );
1663 icm_offset += icm_usage ( log_num_mtts, arbel->limits.mtt_entry_size );
1665 /* Memory protection table */
1666 log_num_mpts = fls ( arbel->limits.reserved_mrws + 1 - 1 );
1667 MLX_FILL_1 ( init_hca, 61,
1668 tpt_parameters.mpt_base_adr_l, icm_offset );
1669 MLX_FILL_1 ( init_hca, 62,
1670 tpt_parameters.log_mpt_sz, log_num_mpts );
1671 DBGC ( arbel, "Arbel %p ICM MTT base = %zx\n", arbel, icm_offset );
1672 icm_offset += icm_usage ( log_num_mpts, arbel->limits.mpt_entry_size );
1674 /* RDMA something or other */
1675 log_num_rdbs = fls ( arbel->limits.reserved_rdbs - 1 );
1676 MLX_FILL_1 ( init_hca, 37,
1677 qpc_eec_cqc_eqc_rdb_parameters.rdb_base_addr_l,
1679 DBGC ( arbel, "Arbel %p ICM RDB base = %zx\n", arbel, icm_offset );
1680 icm_offset += icm_usage ( log_num_rdbs, 32 );
1682 /* Event queue contexts */
1684 MLX_FILL_2 ( init_hca, 33,
1685 qpc_eec_cqc_eqc_rdb_parameters.eqc_base_addr_l,
1686 ( icm_offset >> 6 ),
1687 qpc_eec_cqc_eqc_rdb_parameters.log_num_eq,
1689 DBGC ( arbel, "Arbel %p ICM EQ base = %zx\n", arbel, icm_offset );
1690 icm_offset += ( ( 1 << log_num_eqs ) * arbel->limits.eqc_entry_size );
1692 /* Multicast table */
1693 MLX_FILL_1 ( init_hca, 49,
1694 multicast_parameters.mc_base_addr_l, icm_offset );
1695 MLX_FILL_1 ( init_hca, 52,
1696 multicast_parameters.log_mc_table_entry_sz,
1697 fls ( sizeof ( struct arbelprm_mgm_entry ) - 1 ) );
1698 MLX_FILL_1 ( init_hca, 53,
1699 multicast_parameters.mc_table_hash_sz, 8 );
1700 MLX_FILL_1 ( init_hca, 54,
1701 multicast_parameters.log_mc_table_sz, 3 );
1702 DBGC ( arbel, "Arbel %p ICM MC base = %zx\n", arbel, icm_offset );
1703 icm_offset += ( 8 * sizeof ( struct arbelprm_mgm_entry ) );
1705 arbel->icm_len = icm_offset;
1706 arbel->icm_len = ( ( arbel->icm_len + 4095 ) & ~4095 );
1708 /* Get ICM auxiliary area size */
1709 memset ( &icm_size, 0, sizeof ( icm_size ) );
1710 MLX_FILL_1 ( &icm_size, 1, value, arbel->icm_len );
1711 if ( ( rc = arbel_cmd_set_icm_size ( arbel, &icm_size,
1712 &icm_aux_size ) ) != 0 ) {
1713 DBGC ( arbel, "Arbel %p could not set ICM size: %s\n",
1714 arbel, strerror ( rc ) );
1715 goto err_set_icm_size;
1717 arbel->icm_aux_len = ( MLX_GET ( &icm_aux_size, value ) * 4096 );
1719 /* Allocate ICM data and auxiliary area */
1720 DBGC ( arbel, "Arbel %p requires %zd kB ICM and %zd kB AUX ICM\n",
1721 arbel, ( arbel->icm_len / 1024 ),
1722 ( arbel->icm_aux_len / 1024 ) );
1723 arbel->icm = umalloc ( arbel->icm_len + arbel->icm_aux_len );
1724 if ( ! arbel->icm ) {
1729 /* Map ICM auxiliary area */
1730 memset ( &map_icm_aux, 0, sizeof ( map_icm_aux ) );
1731 MLX_FILL_2 ( &map_icm_aux, 3,
1732 log2size, fls ( ( arbel->icm_aux_len / 4096 ) - 1 ),
1734 ( user_to_phys ( arbel->icm, arbel->icm_len ) >> 12 ) );
1735 if ( ( rc = arbel_cmd_map_icm_aux ( arbel, &map_icm_aux ) ) != 0 ) {
1736 DBGC ( arbel, "Arbel %p could not map AUX ICM: %s\n",
1737 arbel, strerror ( rc ) );
1738 goto err_map_icm_aux;
1742 memset ( &map_icm, 0, sizeof ( map_icm ) );
1743 MLX_FILL_2 ( &map_icm, 3,
1744 log2size, fls ( ( arbel->icm_len / 4096 ) - 1 ),
1745 pa_l, ( user_to_phys ( arbel->icm, 0 ) >> 12 ) );
1746 if ( ( rc = arbel_cmd_map_icm ( arbel, &map_icm ) ) != 0 ) {
1747 DBGC ( arbel, "Arbel %p could not map ICM: %s\n",
1748 arbel, strerror ( rc ) );
1752 /* Initialise UAR context */
1753 arbel->db_rec = phys_to_virt ( user_to_phys ( arbel->icm, 0 ) +
1754 ( arbel->limits.reserved_uars *
1755 ARBEL_PAGE_SIZE ) );
1756 memset ( arbel->db_rec, 0, ARBEL_PAGE_SIZE );
1757 db_rec = &arbel->db_rec[ARBEL_GROUP_SEPARATOR_DOORBELL];
1758 MLX_FILL_1 ( &db_rec->qp, 1, res, ARBEL_UAR_RES_GROUP_SEP );
1762 arbel_cmd_unmap_icm ( arbel, ( arbel->icm_len / 4096 ) );
1764 arbel_cmd_unmap_icm_aux ( arbel );
1766 ufree ( arbel->icm );
1776 * @v arbel Arbel device
1778 static void arbel_free_icm ( struct arbel *arbel ) {
1779 arbel_cmd_unmap_icm ( arbel, ( arbel->icm_len / 4096 ) );
1780 arbel_cmd_unmap_icm_aux ( arbel );
1781 ufree ( arbel->icm );
1785 /***************************************************************************
1789 ***************************************************************************
1793 * Set up memory protection table
1795 * @v arbel Arbel device
1796 * @ret rc Return status code
1798 static int arbel_setup_mpt ( struct arbel *arbel ) {
1799 struct arbelprm_mpt mpt;
1804 key = ( arbel->limits.reserved_mrws | ARBEL_MKEY_PREFIX );
1805 arbel->reserved_lkey = ( ( key << 8 ) | ( key >> 24 ) );
1807 /* Initialise memory protection table */
1808 memset ( &mpt, 0, sizeof ( mpt ) );
1809 MLX_FILL_4 ( &mpt, 0,
1814 MLX_FILL_1 ( &mpt, 2, mem_key, key );
1815 MLX_FILL_1 ( &mpt, 3, pd, ARBEL_GLOBAL_PD );
1816 MLX_FILL_1 ( &mpt, 6, reg_wnd_len_h, 0xffffffffUL );
1817 MLX_FILL_1 ( &mpt, 7, reg_wnd_len_l, 0xffffffffUL );
1818 if ( ( rc = arbel_cmd_sw2hw_mpt ( arbel, arbel->limits.reserved_mrws,
1820 DBGC ( arbel, "Arbel %p could not set up MPT: %s\n",
1821 arbel, strerror ( rc ) );
1833 * @ret rc Return status code
1835 static int arbel_probe ( struct pci_device *pci,
1836 const struct pci_device_id *id __unused ) {
1837 struct arbel *arbel;
1838 struct ib_device *ibdev;
1839 struct arbelprm_init_hca init_hca;
1843 /* Allocate Arbel device */
1844 arbel = zalloc ( sizeof ( *arbel ) );
1847 goto err_alloc_arbel;
1849 pci_set_drvdata ( pci, arbel );
1851 /* Allocate Infiniband devices */
1852 for ( i = 0 ; i < ARBEL_NUM_PORTS ; i++ ) {
1853 ibdev = alloc_ibdev ( 0 );
1856 goto err_alloc_ibdev;
1858 arbel->ibdev[i] = ibdev;
1859 ibdev->op = &arbel_ib_operations;
1860 ibdev->dev = &pci->dev;
1861 ibdev->port = ( ARBEL_PORT_BASE + i );
1862 ib_set_drvdata ( ibdev, arbel );
1865 /* Fix up PCI device */
1866 adjust_pci_device ( pci );
1869 arbel->config = ioremap ( pci_bar_start ( pci, ARBEL_PCI_CONFIG_BAR ),
1870 ARBEL_PCI_CONFIG_BAR_SIZE );
1871 arbel->uar = ioremap ( ( pci_bar_start ( pci, ARBEL_PCI_UAR_BAR ) +
1872 ARBEL_PCI_UAR_IDX * ARBEL_PCI_UAR_SIZE ),
1873 ARBEL_PCI_UAR_SIZE );
1875 /* Allocate space for mailboxes */
1876 arbel->mailbox_in = malloc_dma ( ARBEL_MBOX_SIZE, ARBEL_MBOX_ALIGN );
1877 if ( ! arbel->mailbox_in ) {
1879 goto err_mailbox_in;
1881 arbel->mailbox_out = malloc_dma ( ARBEL_MBOX_SIZE, ARBEL_MBOX_ALIGN );
1882 if ( ! arbel->mailbox_out ) {
1884 goto err_mailbox_out;
1887 /* Start firmware */
1888 if ( ( rc = arbel_start_firmware ( arbel ) ) != 0 )
1889 goto err_start_firmware;
1891 /* Get device limits */
1892 if ( ( rc = arbel_get_limits ( arbel ) ) != 0 )
1893 goto err_get_limits;
1896 memset ( &init_hca, 0, sizeof ( init_hca ) );
1897 if ( ( rc = arbel_alloc_icm ( arbel, &init_hca ) ) != 0 )
1900 /* Initialise HCA */
1901 MLX_FILL_1 ( &init_hca, 74, uar_parameters.log_max_uars, 1 );
1902 if ( ( rc = arbel_cmd_init_hca ( arbel, &init_hca ) ) != 0 ) {
1903 DBGC ( arbel, "Arbel %p could not initialise HCA: %s\n",
1904 arbel, strerror ( rc ) );
1908 /* Set up memory protection */
1909 if ( ( rc = arbel_setup_mpt ( arbel ) ) != 0 )
1912 /* Register Infiniband devices */
1913 for ( i = 0 ; i < ARBEL_NUM_PORTS ; i++ ) {
1914 if ( ( rc = register_ibdev ( arbel->ibdev[i] ) ) != 0 ) {
1915 DBGC ( arbel, "Arbel %p could not register IB "
1916 "device: %s\n", arbel, strerror ( rc ) );
1917 goto err_register_ibdev;
1923 i = ( ARBEL_NUM_PORTS - 1 );
1925 for ( ; i >= 0 ; i-- )
1926 unregister_ibdev ( arbel->ibdev[i] );
1928 arbel_cmd_close_hca ( arbel );
1930 arbel_free_icm ( arbel );
1933 arbel_stop_firmware ( arbel );
1935 free_dma ( arbel->mailbox_out, ARBEL_MBOX_SIZE );
1937 free_dma ( arbel->mailbox_in, ARBEL_MBOX_SIZE );
1939 i = ( ARBEL_NUM_PORTS - 1 );
1941 for ( ; i >= 0 ; i-- )
1942 free_ibdev ( arbel->ibdev[i] );
1953 static void arbel_remove ( struct pci_device *pci ) {
1954 struct arbel *arbel = pci_get_drvdata ( pci );
1957 for ( i = ( ARBEL_NUM_PORTS - 1 ) ; i >= 0 ; i-- )
1958 unregister_ibdev ( arbel->ibdev[i] );
1959 arbel_cmd_close_hca ( arbel );
1960 arbel_free_icm ( arbel );
1961 arbel_stop_firmware ( arbel );
1962 arbel_stop_firmware ( arbel );
1963 free_dma ( arbel->mailbox_out, ARBEL_MBOX_SIZE );
1964 free_dma ( arbel->mailbox_in, ARBEL_MBOX_SIZE );
1965 for ( i = ( ARBEL_NUM_PORTS - 1 ) ; i >= 0 ; i-- )
1966 free_ibdev ( arbel->ibdev[i] );
1970 static struct pci_device_id arbel_nics[] = {
1971 PCI_ROM ( 0x15b3, 0x6282, "mt25218", "MT25218 HCA driver" ),
1972 PCI_ROM ( 0x15b3, 0x6274, "mt25204", "MT25204 HCA driver" ),
1975 struct pci_driver arbel_driver __pci_driver = {
1977 .id_count = ( sizeof ( arbel_nics ) / sizeof ( arbel_nics[0] ) ),
1978 .probe = arbel_probe,
1979 .remove = arbel_remove,