1 /**************************************************************************
2 Etherboot - BOOTP/TFTP Bootstrap Program
3 Skeleton NIC driver for Etherboot
4 ***************************************************************************/
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, or (at
10 * your option) any later version.
15 #include <gpxe/malloc.h>
16 #include <gpxe/iobuf.h>
17 #include <gpxe/netdevice.h>
18 #include <gpxe/infiniband.h>
19 #include <gpxe/ipoib.h>
21 /* to get some global routines like printf */
22 #include "etherboot.h"
23 /* to get the interface to the body of the program */
28 #include "mt25218_imp.c"
37 /***************************************************************************
39 * Queue number allocation
41 ***************************************************************************
45 * Allocate queue number
47 * @v q_inuse Queue usage bitmask
48 * @v max_inuse Maximum number of in-use queues
49 * @ret qn_offset Free queue number offset, or negative error
51 static int arbel_alloc_qn_offset ( arbel_bitmask_t *q_inuse,
52 unsigned int max_inuse ) {
53 unsigned int qn_offset = 0;
54 arbel_bitmask_t mask = 1;
56 while ( qn_offset < max_inuse ) {
57 if ( ( mask & *q_inuse ) == 0 ) {
74 * @v q_inuse Queue usage bitmask
75 * @v qn_offset Queue number offset
77 static void arbel_free_qn_offset ( arbel_bitmask_t *q_inuse, int qn_offset ) {
80 mask = ( 1 << ( qn_offset % ( 8 * sizeof ( mask ) ) ) );
81 q_inuse += ( qn_offset / ( 8 * sizeof ( mask ) ) );
85 /***************************************************************************
89 ***************************************************************************
93 * Wait for Arbel command completion
95 * @v arbel Arbel device
96 * @ret rc Return status code
98 static int arbel_cmd_wait ( struct arbel *arbel,
99 struct arbelprm_hca_command_register *hcr ) {
102 for ( wait = ARBEL_HCR_MAX_WAIT_MS ; wait ; wait-- ) {
104 readl ( arbel->config + ARBEL_HCR_REG ( 6 ) );
105 if ( MLX_GET ( hcr, go ) == 0 )
115 * @v arbel Arbel device
116 * @v command Command opcode, flags and input/output lengths
117 * @v op_mod Opcode modifier (0 if no modifier applicable)
118 * @v in Input parameters
119 * @v in_mod Input modifier (0 if no modifier applicable)
120 * @v out Output parameters
121 * @ret rc Return status code
123 static int arbel_cmd ( struct arbel *arbel, unsigned long command,
124 unsigned int op_mod, const void *in,
125 unsigned int in_mod, void *out ) {
126 struct arbelprm_hca_command_register hcr;
127 unsigned int opcode = ARBEL_HCR_OPCODE ( command );
128 size_t in_len = ARBEL_HCR_IN_LEN ( command );
129 size_t out_len = ARBEL_HCR_OUT_LEN ( command );
136 assert ( in_len <= ARBEL_MBOX_SIZE );
137 assert ( out_len <= ARBEL_MBOX_SIZE );
139 DBGC2 ( arbel, "Arbel %p command %02x in %zx%s out %zx%s\n",
140 arbel, opcode, in_len,
141 ( ( command & ARBEL_HCR_IN_MBOX ) ? "(mbox)" : "" ), out_len,
142 ( ( command & ARBEL_HCR_OUT_MBOX ) ? "(mbox)" : "" ) );
144 /* Check that HCR is free */
145 if ( ( rc = arbel_cmd_wait ( arbel, &hcr ) ) != 0 ) {
146 DBGC ( arbel, "Arbel %p command interface locked\n", arbel );
151 memset ( &hcr, 0, sizeof ( hcr ) );
152 in_buffer = &hcr.u.dwords[0];
153 if ( in_len && ( command & ARBEL_HCR_IN_MBOX ) ) {
154 in_buffer = arbel->mailbox_in;
155 MLX_FILL_1 ( &hcr, 1, in_param_l, virt_to_bus ( in_buffer ) );
157 memcpy ( in_buffer, in, in_len );
158 MLX_FILL_1 ( &hcr, 2, input_modifier, in_mod );
159 out_buffer = &hcr.u.dwords[3];
160 if ( out_len && ( command & ARBEL_HCR_OUT_MBOX ) ) {
161 out_buffer = arbel->mailbox_out;
162 MLX_FILL_1 ( &hcr, 4, out_param_l,
163 virt_to_bus ( out_buffer ) );
165 MLX_FILL_3 ( &hcr, 6,
167 opcode_modifier, op_mod,
169 DBGC2_HD ( arbel, &hcr, sizeof ( hcr ) );
171 DBGC2 ( arbel, "Input:\n" );
172 DBGC2_HD ( arbel, in, ( ( in_len < 256 ) ? in_len : 256 ) );
176 for ( i = 0 ; i < ( sizeof ( hcr ) / sizeof ( hcr.u.dwords[0] ) ) ;
178 writel ( hcr.u.dwords[i],
179 arbel->config + ARBEL_HCR_REG ( i ) );
183 /* Wait for command completion */
184 if ( ( rc = arbel_cmd_wait ( arbel, &hcr ) ) != 0 ) {
185 DBGC ( arbel, "Arbel %p timed out waiting for command:\n",
187 DBGC_HD ( arbel, &hcr, sizeof ( hcr ) );
191 /* Check command status */
192 status = MLX_GET ( &hcr, status );
194 DBGC ( arbel, "Arbel %p command failed with status %02x:\n",
196 DBGC_HD ( arbel, &hcr, sizeof ( hcr ) );
200 /* Read output parameters, if any */
201 hcr.u.dwords[3] = readl ( arbel->config + ARBEL_HCR_REG ( 3 ) );
202 hcr.u.dwords[4] = readl ( arbel->config + ARBEL_HCR_REG ( 4 ) );
203 memcpy ( out, out_buffer, out_len );
205 DBGC2 ( arbel, "Output:\n" );
206 DBGC2_HD ( arbel, out, ( ( out_len < 256 ) ? out_len : 256 ) );
213 arbel_cmd_query_dev_lim ( struct arbel *arbel,
214 struct arbelprm_query_dev_lim *dev_lim ) {
215 return arbel_cmd ( arbel,
216 ARBEL_HCR_OUT_CMD ( ARBEL_HCR_QUERY_DEV_LIM,
217 1, sizeof ( *dev_lim ) ),
218 0, NULL, 0, dev_lim );
222 arbel_cmd_query_fw ( struct arbel *arbel, struct arbelprm_query_fw *fw ) {
223 return arbel_cmd ( arbel,
224 ARBEL_HCR_OUT_CMD ( ARBEL_HCR_QUERY_FW,
230 arbel_cmd_sw2hw_cq ( struct arbel *arbel, unsigned long cqn,
231 const struct arbelprm_completion_queue_context *cqctx ) {
232 return arbel_cmd ( arbel,
233 ARBEL_HCR_IN_CMD ( ARBEL_HCR_SW2HW_CQ,
234 1, sizeof ( *cqctx ) ),
235 0, cqctx, cqn, NULL );
239 arbel_cmd_hw2sw_cq ( struct arbel *arbel, unsigned long cqn,
240 struct arbelprm_completion_queue_context *cqctx) {
241 return arbel_cmd ( arbel,
242 ARBEL_HCR_OUT_CMD ( ARBEL_HCR_HW2SW_CQ,
243 1, sizeof ( *cqctx ) ),
244 0, NULL, cqn, cqctx );
248 arbel_cmd_rst2init_qpee ( struct arbel *arbel, unsigned long qpn,
249 const struct arbelprm_qp_ee_state_transitions *ctx ){
250 return arbel_cmd ( arbel,
251 ARBEL_HCR_IN_CMD ( ARBEL_HCR_RST2INIT_QPEE,
252 1, sizeof ( *ctx ) ),
257 arbel_cmd_init2rtr_qpee ( struct arbel *arbel, unsigned long qpn,
258 const struct arbelprm_qp_ee_state_transitions *ctx ){
259 return arbel_cmd ( arbel,
260 ARBEL_HCR_IN_CMD ( ARBEL_HCR_INIT2RTR_QPEE,
261 1, sizeof ( *ctx ) ),
266 arbel_cmd_rtr2rts_qpee ( struct arbel *arbel, unsigned long qpn,
267 const struct arbelprm_qp_ee_state_transitions *ctx ) {
268 return arbel_cmd ( arbel,
269 ARBEL_HCR_IN_CMD ( ARBEL_HCR_RTR2RTS_QPEE,
270 1, sizeof ( *ctx ) ),
275 arbel_cmd_2rst_qpee ( struct arbel *arbel, unsigned long qpn ) {
276 return arbel_cmd ( arbel,
277 ARBEL_HCR_VOID_CMD ( ARBEL_HCR_2RST_QPEE ),
278 0x03, NULL, qpn, NULL );
282 arbel_cmd_mad_ifc ( struct arbel *arbel, union arbelprm_mad *mad ) {
283 return arbel_cmd ( arbel,
284 ARBEL_HCR_INOUT_CMD ( ARBEL_HCR_MAD_IFC,
286 1, sizeof ( *mad ) ),
287 0x03, mad, PXE_IB_PORT, mad );
291 arbel_cmd_read_mgm ( struct arbel *arbel, unsigned int index,
292 struct arbelprm_mgm_entry *mgm ) {
293 return arbel_cmd ( arbel,
294 ARBEL_HCR_OUT_CMD ( ARBEL_HCR_READ_MGM,
295 1, sizeof ( *mgm ) ),
296 0, NULL, index, mgm );
300 arbel_cmd_write_mgm ( struct arbel *arbel, unsigned int index,
301 const struct arbelprm_mgm_entry *mgm ) {
302 return arbel_cmd ( arbel,
303 ARBEL_HCR_IN_CMD ( ARBEL_HCR_WRITE_MGM,
304 1, sizeof ( *mgm ) ),
305 0, mgm, index, NULL );
309 arbel_cmd_mgid_hash ( struct arbel *arbel, const struct ib_gid *gid,
310 struct arbelprm_mgm_hash *hash ) {
311 return arbel_cmd ( arbel,
312 ARBEL_HCR_INOUT_CMD ( ARBEL_HCR_MGID_HASH,
314 0, sizeof ( *hash ) ),
318 /***************************************************************************
320 * Completion queue operations
322 ***************************************************************************
326 * Create completion queue
328 * @v ibdev Infiniband device
329 * @v cq Completion queue
330 * @ret rc Return status code
332 static int arbel_create_cq ( struct ib_device *ibdev,
333 struct ib_completion_queue *cq ) {
334 struct arbel *arbel = ibdev->dev_priv;
335 struct arbel_completion_queue *arbel_cq;
336 struct arbelprm_completion_queue_context cqctx;
337 struct arbelprm_cq_ci_db_record *ci_db_rec;
338 struct arbelprm_cq_arm_db_record *arm_db_rec;
343 /* Find a free completion queue number */
344 cqn_offset = arbel_alloc_qn_offset ( arbel->cq_inuse, ARBEL_MAX_CQS );
345 if ( cqn_offset < 0 ) {
346 DBGC ( arbel, "Arbel %p out of completion queues\n", arbel );
350 cq->cqn = ( arbel->limits.reserved_cqs + cqn_offset );
352 /* Allocate control structures */
353 arbel_cq = zalloc ( sizeof ( *arbel_cq ) );
358 arbel_cq->ci_doorbell_idx = arbel_cq_ci_doorbell_idx ( cqn_offset );
359 arbel_cq->arm_doorbell_idx = arbel_cq_arm_doorbell_idx ( cqn_offset );
361 /* Allocate completion queue itself */
362 arbel_cq->cqe_size = ( cq->num_cqes * sizeof ( arbel_cq->cqe[0] ) );
363 arbel_cq->cqe = malloc_dma ( arbel_cq->cqe_size,
364 sizeof ( arbel_cq->cqe[0] ) );
365 if ( ! arbel_cq->cqe ) {
369 memset ( arbel_cq->cqe, 0, arbel_cq->cqe_size );
370 for ( i = 0 ; i < cq->num_cqes ; i++ ) {
371 MLX_FILL_1 ( &arbel_cq->cqe[i].normal, 7, owner, 1 );
375 /* Initialise doorbell records */
376 ci_db_rec = &arbel->db_rec[arbel_cq->ci_doorbell_idx].cq_ci;
377 MLX_FILL_1 ( ci_db_rec, 0, counter, 0 );
378 MLX_FILL_2 ( ci_db_rec, 1,
379 res, ARBEL_UAR_RES_CQ_CI,
380 cq_number, cq->cqn );
381 arm_db_rec = &arbel->db_rec[arbel_cq->arm_doorbell_idx].cq_arm;
382 MLX_FILL_1 ( arm_db_rec, 0, counter, 0 );
383 MLX_FILL_2 ( arm_db_rec, 1,
384 res, ARBEL_UAR_RES_CQ_ARM,
385 cq_number, cq->cqn );
387 /* Hand queue over to hardware */
388 memset ( &cqctx, 0, sizeof ( cqctx ) );
389 MLX_FILL_1 ( &cqctx, 0, st, 0xa /* "Event fired" */ );
390 MLX_FILL_1 ( &cqctx, 2, start_address_l,
391 virt_to_bus ( arbel_cq->cqe ) );
392 MLX_FILL_2 ( &cqctx, 3,
393 usr_page, arbel->limits.reserved_uars,
394 log_cq_size, fls ( cq->num_cqes - 1 ) );
395 MLX_FILL_1 ( &cqctx, 5, c_eqn, arbel->eqn );
396 MLX_FILL_1 ( &cqctx, 6, pd, ARBEL_GLOBAL_PD );
397 MLX_FILL_1 ( &cqctx, 7, l_key, arbel->reserved_lkey );
398 MLX_FILL_1 ( &cqctx, 12, cqn, cq->cqn );
399 MLX_FILL_1 ( &cqctx, 13,
400 cq_ci_db_record, arbel_cq->ci_doorbell_idx );
401 MLX_FILL_1 ( &cqctx, 14,
402 cq_state_db_record, arbel_cq->arm_doorbell_idx );
403 if ( ( rc = arbel_cmd_sw2hw_cq ( arbel, cq->cqn, &cqctx ) ) != 0 ) {
404 DBGC ( arbel, "Arbel %p SW2HW_CQ failed: %s\n",
405 arbel, strerror ( rc ) );
409 DBGC ( arbel, "Arbel %p CQN %#lx ring at [%p,%p)\n",
410 arbel, cq->cqn, arbel_cq->cqe,
411 ( ( ( void * ) arbel_cq->cqe ) + arbel_cq->cqe_size ) );
412 cq->dev_priv = arbel_cq;
416 MLX_FILL_1 ( ci_db_rec, 1, res, ARBEL_UAR_RES_NONE );
417 MLX_FILL_1 ( arm_db_rec, 1, res, ARBEL_UAR_RES_NONE );
418 free_dma ( arbel_cq->cqe, arbel_cq->cqe_size );
422 arbel_free_qn_offset ( arbel->cq_inuse, cqn_offset );
428 * Destroy completion queue
430 * @v ibdev Infiniband device
431 * @v cq Completion queue
433 static void arbel_destroy_cq ( struct ib_device *ibdev,
434 struct ib_completion_queue *cq ) {
435 struct arbel *arbel = ibdev->dev_priv;
436 struct arbel_completion_queue *arbel_cq = cq->dev_priv;
437 struct arbelprm_completion_queue_context cqctx;
438 struct arbelprm_cq_ci_db_record *ci_db_rec;
439 struct arbelprm_cq_arm_db_record *arm_db_rec;
443 /* Take ownership back from hardware */
444 if ( ( rc = arbel_cmd_hw2sw_cq ( arbel, cq->cqn, &cqctx ) ) != 0 ) {
445 DBGC ( arbel, "Arbel %p FATAL HW2SW_CQ failed on CQN %#lx: "
446 "%s\n", arbel, cq->cqn, strerror ( rc ) );
447 /* Leak memory and return; at least we avoid corruption */
451 /* Clear doorbell records */
452 ci_db_rec = &arbel->db_rec[arbel_cq->ci_doorbell_idx].cq_ci;
453 arm_db_rec = &arbel->db_rec[arbel_cq->arm_doorbell_idx].cq_arm;
454 MLX_FILL_1 ( ci_db_rec, 1, res, ARBEL_UAR_RES_NONE );
455 MLX_FILL_1 ( arm_db_rec, 1, res, ARBEL_UAR_RES_NONE );
458 free_dma ( arbel_cq->cqe, arbel_cq->cqe_size );
461 /* Mark queue number as free */
462 cqn_offset = ( cq->cqn - arbel->limits.reserved_cqs );
463 arbel_free_qn_offset ( arbel->cq_inuse, cqn_offset );
468 /***************************************************************************
470 * Queue pair operations
472 ***************************************************************************
476 * Create send work queue
478 * @v arbel_send_wq Send work queue
479 * @v num_wqes Number of work queue entries
480 * @ret rc Return status code
482 static int arbel_create_send_wq ( struct arbel_send_work_queue *arbel_send_wq,
483 unsigned int num_wqes ) {
484 struct arbelprm_ud_send_wqe *wqe;
485 struct arbelprm_ud_send_wqe *next_wqe;
486 unsigned int wqe_idx_mask;
489 /* Allocate work queue */
490 arbel_send_wq->wqe_size = ( num_wqes *
491 sizeof ( arbel_send_wq->wqe[0] ) );
492 arbel_send_wq->wqe = malloc_dma ( arbel_send_wq->wqe_size,
493 sizeof ( arbel_send_wq->wqe[0] ) );
494 if ( ! arbel_send_wq->wqe )
496 memset ( arbel_send_wq->wqe, 0, arbel_send_wq->wqe_size );
498 /* Link work queue entries */
499 wqe_idx_mask = ( num_wqes - 1 );
500 for ( i = 0 ; i < num_wqes ; i++ ) {
501 wqe = &arbel_send_wq->wqe[i].ud;
502 next_wqe = &arbel_send_wq->wqe[ ( i + 1 ) & wqe_idx_mask ].ud;
503 MLX_FILL_1 ( &wqe->next, 0, nda_31_6,
504 ( virt_to_bus ( next_wqe ) >> 6 ) );
511 * Create receive work queue
513 * @v arbel_recv_wq Receive work queue
514 * @v num_wqes Number of work queue entries
515 * @ret rc Return status code
517 static int arbel_create_recv_wq ( struct arbel_recv_work_queue *arbel_recv_wq,
518 unsigned int num_wqes ) {
519 struct arbelprm_recv_wqe *wqe;
520 struct arbelprm_recv_wqe *next_wqe;
521 unsigned int wqe_idx_mask;
526 /* Allocate work queue */
527 arbel_recv_wq->wqe_size = ( num_wqes *
528 sizeof ( arbel_recv_wq->wqe[0] ) );
529 arbel_recv_wq->wqe = malloc_dma ( arbel_recv_wq->wqe_size,
530 sizeof ( arbel_recv_wq->wqe[0] ) );
531 if ( ! arbel_recv_wq->wqe )
533 memset ( arbel_recv_wq->wqe, 0, arbel_recv_wq->wqe_size );
535 /* Link work queue entries */
536 wqe_idx_mask = ( num_wqes - 1 );
537 nds = ( ( offsetof ( typeof ( *wqe ), data ) +
538 sizeof ( wqe->data[0] ) ) >> 4 );
539 for ( i = 0 ; i < num_wqes ; i++ ) {
540 wqe = &arbel_recv_wq->wqe[i].recv;
541 next_wqe = &arbel_recv_wq->wqe[( i + 1 ) & wqe_idx_mask].recv;
542 MLX_FILL_1 ( &wqe->next, 0, nda_31_6,
543 ( virt_to_bus ( next_wqe ) >> 6 ) );
544 MLX_FILL_1 ( &wqe->next, 1, nds, ( sizeof ( *wqe ) / 16 ) );
545 for ( j = 0 ; ( ( ( void * ) &wqe->data[j] ) <
546 ( ( void * ) ( wqe + 1 ) ) ) ; j++ ) {
547 MLX_FILL_1 ( &wqe->data[j], 1,
548 l_key, ARBEL_INVALID_LKEY );
558 * @v ibdev Infiniband device
560 * @ret rc Return status code
562 static int arbel_create_qp ( struct ib_device *ibdev,
563 struct ib_queue_pair *qp ) {
564 struct arbel *arbel = ibdev->dev_priv;
565 struct arbel_queue_pair *arbel_qp;
566 struct arbelprm_qp_ee_state_transitions qpctx;
567 struct arbelprm_qp_db_record *send_db_rec;
568 struct arbelprm_qp_db_record *recv_db_rec;
572 /* Find a free queue pair number */
573 qpn_offset = arbel_alloc_qn_offset ( arbel->qp_inuse, ARBEL_MAX_QPS );
574 if ( qpn_offset < 0 ) {
575 DBGC ( arbel, "Arbel %p out of queue pairs\n", arbel );
579 qp->qpn = ( ARBEL_QPN_BASE + arbel->limits.reserved_qps + qpn_offset );
581 /* Allocate control structures */
582 arbel_qp = zalloc ( sizeof ( *arbel_qp ) );
587 arbel_qp->send.doorbell_idx = arbel_send_doorbell_idx ( qpn_offset );
588 arbel_qp->recv.doorbell_idx = arbel_recv_doorbell_idx ( qpn_offset );
590 /* Create send and receive work queues */
591 if ( ( rc = arbel_create_send_wq ( &arbel_qp->send,
592 qp->send.num_wqes ) ) != 0 )
593 goto err_create_send_wq;
594 if ( ( rc = arbel_create_recv_wq ( &arbel_qp->recv,
595 qp->recv.num_wqes ) ) != 0 )
596 goto err_create_recv_wq;
598 /* Initialise doorbell records */
599 send_db_rec = &arbel->db_rec[arbel_qp->send.doorbell_idx].qp;
600 MLX_FILL_1 ( send_db_rec, 0, counter, 0 );
601 MLX_FILL_2 ( send_db_rec, 1,
602 res, ARBEL_UAR_RES_SQ,
603 qp_number, qp->qpn );
604 recv_db_rec = &arbel->db_rec[arbel_qp->recv.doorbell_idx].qp;
605 MLX_FILL_1 ( recv_db_rec, 0, counter, 0 );
606 MLX_FILL_2 ( recv_db_rec, 1,
607 res, ARBEL_UAR_RES_RQ,
608 qp_number, qp->qpn );
610 /* Hand queue over to hardware */
611 memset ( &qpctx, 0, sizeof ( qpctx ) );
612 MLX_FILL_3 ( &qpctx, 2,
614 qpc_eec_data.pm_state, 0x03 /* Always 0x03 for UD */,
615 qpc_eec_data.st, ARBEL_ST_UD );
616 MLX_FILL_6 ( &qpctx, 4,
617 qpc_eec_data.mtu, ARBEL_MTU_2048,
618 qpc_eec_data.msg_max, 11 /* 2^11 = 2048 */,
619 qpc_eec_data.log_rq_size, fls ( qp->recv.num_wqes - 1 ),
620 qpc_eec_data.log_rq_stride,
621 ( fls ( sizeof ( arbel_qp->recv.wqe[0] ) - 1 ) - 4 ),
622 qpc_eec_data.log_sq_size, fls ( qp->send.num_wqes - 1 ),
623 qpc_eec_data.log_sq_stride,
624 ( fls ( sizeof ( arbel_qp->send.wqe[0] ) - 1 ) - 4 ) );
625 MLX_FILL_1 ( &qpctx, 5,
626 qpc_eec_data.usr_page, arbel->limits.reserved_uars );
627 MLX_FILL_1 ( &qpctx, 10, qpc_eec_data.primary_address_path.port_number,
629 MLX_FILL_1 ( &qpctx, 27, qpc_eec_data.pd, ARBEL_GLOBAL_PD );
630 MLX_FILL_1 ( &qpctx, 29, qpc_eec_data.wqe_lkey, arbel->reserved_lkey );
631 MLX_FILL_1 ( &qpctx, 30, qpc_eec_data.ssc, 1 );
632 MLX_FILL_1 ( &qpctx, 33, qpc_eec_data.cqn_snd, qp->send.cq->cqn );
633 MLX_FILL_1 ( &qpctx, 34, qpc_eec_data.snd_wqe_base_adr_l,
634 ( virt_to_bus ( arbel_qp->send.wqe ) >> 6 ) );
635 MLX_FILL_1 ( &qpctx, 35, qpc_eec_data.snd_db_record_index,
636 arbel_qp->send.doorbell_idx );
637 MLX_FILL_1 ( &qpctx, 38, qpc_eec_data.rsc, 1 );
638 MLX_FILL_1 ( &qpctx, 41, qpc_eec_data.cqn_rcv, qp->recv.cq->cqn );
639 MLX_FILL_1 ( &qpctx, 42, qpc_eec_data.rcv_wqe_base_adr_l,
640 ( virt_to_bus ( arbel_qp->recv.wqe ) >> 6 ) );
641 MLX_FILL_1 ( &qpctx, 43, qpc_eec_data.rcv_db_record_index,
642 arbel_qp->recv.doorbell_idx );
643 MLX_FILL_1 ( &qpctx, 44, qpc_eec_data.q_key, qp->qkey );
644 if ( ( rc = arbel_cmd_rst2init_qpee ( arbel, qp->qpn, &qpctx )) != 0 ){
645 DBGC ( arbel, "Arbel %p RST2INIT_QPEE failed: %s\n",
646 arbel, strerror ( rc ) );
647 goto err_rst2init_qpee;
649 memset ( &qpctx, 0, sizeof ( qpctx ) );
650 MLX_FILL_2 ( &qpctx, 4,
651 qpc_eec_data.mtu, ARBEL_MTU_2048,
652 qpc_eec_data.msg_max, 11 /* 2^11 = 2048 */ );
653 if ( ( rc = arbel_cmd_init2rtr_qpee ( arbel, qp->qpn, &qpctx )) != 0 ){
654 DBGC ( arbel, "Arbel %p INIT2RTR_QPEE failed: %s\n",
655 arbel, strerror ( rc ) );
656 goto err_init2rtr_qpee;
658 memset ( &qpctx, 0, sizeof ( qpctx ) );
659 if ( ( rc = arbel_cmd_rtr2rts_qpee ( arbel, qp->qpn, &qpctx ) ) != 0 ){
660 DBGC ( arbel, "Arbel %p RTR2RTS_QPEE failed: %s\n",
661 arbel, strerror ( rc ) );
662 goto err_rtr2rts_qpee;
665 DBGC ( arbel, "Arbel %p QPN %#lx send ring at [%p,%p)\n",
666 arbel, qp->qpn, arbel_qp->send.wqe,
667 ( ( (void *) arbel_qp->send.wqe ) + arbel_qp->send.wqe_size ) );
668 DBGC ( arbel, "Arbel %p QPN %#lx receive ring at [%p,%p)\n",
669 arbel, qp->qpn, arbel_qp->recv.wqe,
670 ( ( (void *) arbel_qp->recv.wqe ) + arbel_qp->recv.wqe_size ) );
671 qp->dev_priv = arbel_qp;
676 arbel_cmd_2rst_qpee ( arbel, qp->qpn );
678 MLX_FILL_1 ( send_db_rec, 1, res, ARBEL_UAR_RES_NONE );
679 MLX_FILL_1 ( recv_db_rec, 1, res, ARBEL_UAR_RES_NONE );
680 free_dma ( arbel_qp->recv.wqe, arbel_qp->recv.wqe_size );
682 free_dma ( arbel_qp->send.wqe, arbel_qp->send.wqe_size );
686 arbel_free_qn_offset ( arbel->qp_inuse, qpn_offset );
694 * @v ibdev Infiniband device
697 static void arbel_destroy_qp ( struct ib_device *ibdev,
698 struct ib_queue_pair *qp ) {
699 struct arbel *arbel = ibdev->dev_priv;
700 struct arbel_queue_pair *arbel_qp = qp->dev_priv;
701 struct arbelprm_qp_db_record *send_db_rec;
702 struct arbelprm_qp_db_record *recv_db_rec;
706 /* Take ownership back from hardware */
707 if ( ( rc = arbel_cmd_2rst_qpee ( arbel, qp->qpn ) ) != 0 ) {
708 DBGC ( arbel, "Arbel %p FATAL 2RST_QPEE failed on QPN %#lx: "
709 "%s\n", arbel, qp->qpn, strerror ( rc ) );
710 /* Leak memory and return; at least we avoid corruption */
714 /* Clear doorbell records */
715 send_db_rec = &arbel->db_rec[arbel_qp->send.doorbell_idx].qp;
716 recv_db_rec = &arbel->db_rec[arbel_qp->recv.doorbell_idx].qp;
717 MLX_FILL_1 ( send_db_rec, 1, res, ARBEL_UAR_RES_NONE );
718 MLX_FILL_1 ( recv_db_rec, 1, res, ARBEL_UAR_RES_NONE );
721 free_dma ( arbel_qp->send.wqe, arbel_qp->send.wqe_size );
722 free_dma ( arbel_qp->recv.wqe, arbel_qp->recv.wqe_size );
725 /* Mark queue number as free */
726 qpn_offset = ( qp->qpn - ARBEL_QPN_BASE - arbel->limits.reserved_qps );
727 arbel_free_qn_offset ( arbel->qp_inuse, qpn_offset );
732 /***************************************************************************
734 * Work request operations
736 ***************************************************************************
740 * Ring doorbell register in UAR
742 * @v arbel Arbel device
743 * @v db_reg Doorbell register structure
744 * @v offset Address of doorbell
746 static void arbel_ring_doorbell ( struct arbel *arbel,
747 union arbelprm_doorbell_register *db_reg,
748 unsigned int offset ) {
750 DBGC2 ( arbel, "Arbel %p ringing doorbell %08lx:%08lx at %lx\n",
751 arbel, db_reg->dword[0], db_reg->dword[1],
752 virt_to_phys ( arbel->uar + offset ) );
755 writel ( db_reg->dword[0], ( arbel->uar + offset + 0 ) );
757 writel ( db_reg->dword[1], ( arbel->uar + offset + 4 ) );
760 /** GID used for GID-less send work queue entries */
761 static const struct ib_gid arbel_no_gid = {
762 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0 } }
766 * Post send work queue entry
768 * @v ibdev Infiniband device
770 * @v av Address vector
771 * @v iobuf I/O buffer
772 * @ret rc Return status code
774 static int arbel_post_send ( struct ib_device *ibdev,
775 struct ib_queue_pair *qp,
776 struct ib_address_vector *av,
777 struct io_buffer *iobuf ) {
778 struct arbel *arbel = ibdev->dev_priv;
779 struct arbel_queue_pair *arbel_qp = qp->dev_priv;
780 struct ib_work_queue *wq = &qp->send;
781 struct arbel_send_work_queue *arbel_send_wq = &arbel_qp->send;
782 struct arbelprm_ud_send_wqe *prev_wqe;
783 struct arbelprm_ud_send_wqe *wqe;
784 struct arbelprm_qp_db_record *qp_db_rec;
785 union arbelprm_doorbell_register db_reg;
786 const struct ib_gid *gid;
787 unsigned int wqe_idx_mask;
790 /* Allocate work queue entry */
791 wqe_idx_mask = ( wq->num_wqes - 1 );
792 if ( wq->iobufs[wq->next_idx & wqe_idx_mask] ) {
793 DBGC ( arbel, "Arbel %p send queue full", arbel );
796 wq->iobufs[wq->next_idx & wqe_idx_mask] = iobuf;
797 prev_wqe = &arbel_send_wq->wqe[(wq->next_idx - 1) & wqe_idx_mask].ud;
798 wqe = &arbel_send_wq->wqe[wq->next_idx & wqe_idx_mask].ud;
800 /* Construct work queue entry */
801 MLX_FILL_1 ( &wqe->next, 1, always1, 1 );
802 memset ( &wqe->ctrl, 0, sizeof ( wqe->ctrl ) );
803 MLX_FILL_1 ( &wqe->ctrl, 0, always1, 1 );
804 memset ( &wqe->ud, 0, sizeof ( wqe->ud ) );
805 MLX_FILL_2 ( &wqe->ud, 0,
806 ud_address_vector.pd, ARBEL_GLOBAL_PD,
807 ud_address_vector.port_number, PXE_IB_PORT );
808 MLX_FILL_2 ( &wqe->ud, 1,
809 ud_address_vector.rlid, av->dlid,
810 ud_address_vector.g, av->gid_present );
811 MLX_FILL_2 ( &wqe->ud, 2,
812 ud_address_vector.max_stat_rate,
813 ( ( av->rate >= 3 ) ? 0 : 1 ),
814 ud_address_vector.msg, 3 );
815 MLX_FILL_1 ( &wqe->ud, 3, ud_address_vector.sl, av->sl );
816 gid = ( av->gid_present ? &av->gid : &arbel_no_gid );
817 memcpy ( &wqe->ud.u.dwords[4], gid, sizeof ( *gid ) );
818 MLX_FILL_1 ( &wqe->ud, 8, destination_qp, av->dest_qp );
819 MLX_FILL_1 ( &wqe->ud, 9, q_key, av->qkey );
820 MLX_FILL_1 ( &wqe->data[0], 0, byte_count, iob_len ( iobuf ) );
821 MLX_FILL_1 ( &wqe->data[0], 1, l_key, arbel->reserved_lkey );
822 MLX_FILL_1 ( &wqe->data[0], 3,
823 local_address_l, virt_to_bus ( iobuf->data ) );
825 /* Update previous work queue entry's "next" field */
826 nds = ( ( offsetof ( typeof ( *wqe ), data ) +
827 sizeof ( wqe->data[0] ) ) >> 4 );
828 MLX_SET ( &prev_wqe->next, nopcode, ARBEL_OPCODE_SEND );
829 MLX_FILL_3 ( &prev_wqe->next, 1,
834 /* Update doorbell record */
836 qp_db_rec = &arbel->db_rec[arbel_send_wq->doorbell_idx].qp;
837 MLX_FILL_1 ( qp_db_rec, 0,
838 counter, ( ( wq->next_idx + 1 ) & 0xffff ) );
840 /* Ring doorbell register */
841 MLX_FILL_4 ( &db_reg.send, 0,
842 nopcode, ARBEL_OPCODE_SEND,
844 wqe_counter, ( wq->next_idx & 0xffff ),
846 MLX_FILL_2 ( &db_reg.send, 1,
849 arbel_ring_doorbell ( arbel, &db_reg, POST_SND_OFFSET );
851 /* Update work queue's index */
858 * Post receive work queue entry
860 * @v ibdev Infiniband device
862 * @v iobuf I/O buffer
863 * @ret rc Return status code
865 static int arbel_post_recv ( struct ib_device *ibdev,
866 struct ib_queue_pair *qp,
867 struct io_buffer *iobuf ) {
868 struct arbel *arbel = ibdev->dev_priv;
869 struct arbel_queue_pair *arbel_qp = qp->dev_priv;
870 struct ib_work_queue *wq = &qp->recv;
871 struct arbel_recv_work_queue *arbel_recv_wq = &arbel_qp->recv;
872 struct arbelprm_recv_wqe *wqe;
873 union arbelprm_doorbell_record *db_rec;
874 unsigned int wqe_idx_mask;
876 /* Allocate work queue entry */
877 wqe_idx_mask = ( wq->num_wqes - 1 );
878 if ( wq->iobufs[wq->next_idx & wqe_idx_mask] ) {
879 DBGC ( arbel, "Arbel %p receive queue full", arbel );
882 wq->iobufs[wq->next_idx & wqe_idx_mask] = iobuf;
883 wqe = &arbel_recv_wq->wqe[wq->next_idx & wqe_idx_mask].recv;
885 /* Construct work queue entry */
886 MLX_FILL_1 ( &wqe->data[0], 0, byte_count, iob_tailroom ( iobuf ) );
887 MLX_FILL_1 ( &wqe->data[0], 1, l_key, arbel->reserved_lkey );
888 MLX_FILL_1 ( &wqe->data[0], 3,
889 local_address_l, virt_to_bus ( iobuf->data ) );
891 /* Update doorbell record */
893 db_rec = &arbel->db_rec[arbel_recv_wq->doorbell_idx];
894 MLX_FILL_1 ( &db_rec->qp, 0,
895 counter, ( ( wq->next_idx + 1 ) & 0xffff ) );
897 /* Update work queue's index */
906 * @v ibdev Infiniband device
907 * @v cq Completion queue
908 * @v cqe Hardware completion queue entry
909 * @v complete_send Send completion handler
910 * @v complete_recv Receive completion handler
911 * @ret rc Return status code
913 static int arbel_complete ( struct ib_device *ibdev,
914 struct ib_completion_queue *cq,
915 union arbelprm_completion_entry *cqe,
916 ib_completer_t complete_send,
917 ib_completer_t complete_recv ) {
918 struct arbel *arbel = ibdev->dev_priv;
919 struct ib_completion completion;
920 struct ib_work_queue *wq;
921 struct ib_queue_pair *qp;
922 struct arbel_queue_pair *arbel_qp;
923 struct arbel_send_work_queue *arbel_send_wq;
924 struct arbel_recv_work_queue *arbel_recv_wq;
925 struct arbelprm_recv_wqe *recv_wqe;
926 struct io_buffer *iobuf;
927 ib_completer_t complete;
931 unsigned long wqe_adr;
932 unsigned int wqe_idx;
935 /* Parse completion */
936 memset ( &completion, 0, sizeof ( completion ) );
937 qpn = MLX_GET ( &cqe->normal, my_qpn );
938 is_send = MLX_GET ( &cqe->normal, s );
939 wqe_adr = ( MLX_GET ( &cqe->normal, wqe_adr ) << 6 );
940 opcode = MLX_GET ( &cqe->normal, opcode );
941 if ( opcode >= ARBEL_OPCODE_RECV_ERROR ) {
942 /* "s" field is not valid for error opcodes */
943 is_send = ( opcode == ARBEL_OPCODE_SEND_ERROR );
944 completion.syndrome = MLX_GET ( &cqe->error, syndrome );
945 DBGC ( arbel, "Arbel %p CPN %lx syndrome %x vendor %lx\n",
946 arbel, cq->cqn, completion.syndrome,
947 MLX_GET ( &cqe->error, vendor_code ) );
949 /* Don't return immediately; propagate error to completer */
952 /* Identify work queue */
953 wq = ib_find_wq ( cq, qpn, is_send );
955 DBGC ( arbel, "Arbel %p CQN %lx unknown %s QPN %lx\n",
956 arbel, cq->cqn, ( is_send ? "send" : "recv" ), qpn );
960 arbel_qp = qp->dev_priv;
962 /* Identify work queue entry index */
964 arbel_send_wq = &arbel_qp->send;
965 wqe_idx = ( ( wqe_adr - virt_to_bus ( arbel_send_wq->wqe ) ) /
966 sizeof ( arbel_send_wq->wqe[0] ) );
967 assert ( wqe_idx < qp->send.num_wqes );
969 arbel_recv_wq = &arbel_qp->recv;
970 wqe_idx = ( ( wqe_adr - virt_to_bus ( arbel_recv_wq->wqe ) ) /
971 sizeof ( arbel_recv_wq->wqe[0] ) );
972 assert ( wqe_idx < qp->recv.num_wqes );
975 /* Identify I/O buffer */
976 iobuf = wq->iobufs[wqe_idx];
978 DBGC ( arbel, "Arbel %p CQN %lx QPN %lx empty WQE %x\n",
979 arbel, cq->cqn, qpn, wqe_idx );
982 wq->iobufs[wqe_idx] = NULL;
984 /* Fill in length for received packets */
986 completion.len = MLX_GET ( &cqe->normal, byte_cnt );
987 recv_wqe = &arbel_recv_wq->wqe[wqe_idx].recv;
988 assert ( MLX_GET ( &recv_wqe->data[0], local_address_l ) ==
989 virt_to_bus ( iobuf->data ) );
990 assert ( MLX_GET ( &recv_wqe->data[0], byte_count ) ==
991 iob_tailroom ( iobuf ) );
992 MLX_FILL_1 ( &recv_wqe->data[0], 0, byte_count, 0 );
993 MLX_FILL_1 ( &recv_wqe->data[0], 1,
994 l_key, ARBEL_INVALID_LKEY );
995 if ( completion.len > iob_tailroom ( iobuf ) ) {
996 DBGC ( arbel, "Arbel %p CQN %lx QPN %lx IDX %x "
997 "overlength received packet length %zd\n",
998 arbel, cq->cqn, qpn, wqe_idx, completion.len );
1003 /* Pass off to caller's completion handler */
1004 complete = ( is_send ? complete_send : complete_recv );
1005 complete ( ibdev, qp, &completion, iobuf );
1013 * @v arbel Arbel device
1015 static void arbel_drain_eq ( struct arbel *arbel ) {
1016 #warning "drain the event queue"
1021 * Poll completion queue
1023 * @v ibdev Infiniband device
1024 * @v cq Completion queue
1025 * @v complete_send Send completion handler
1026 * @v complete_recv Receive completion handler
1028 static void arbel_poll_cq ( struct ib_device *ibdev,
1029 struct ib_completion_queue *cq,
1030 ib_completer_t complete_send,
1031 ib_completer_t complete_recv ) {
1032 struct arbel *arbel = ibdev->dev_priv;
1033 struct arbel_completion_queue *arbel_cq = cq->dev_priv;
1034 struct arbelprm_cq_ci_db_record *ci_db_rec;
1035 union arbelprm_completion_entry *cqe;
1036 unsigned int cqe_idx_mask;
1039 /* Drain the event queue */
1040 arbel_drain_eq ( arbel );
1043 /* Look for completion entry */
1044 cqe_idx_mask = ( cq->num_cqes - 1 );
1045 cqe = &arbel_cq->cqe[cq->next_idx & cqe_idx_mask];
1046 if ( MLX_GET ( &cqe->normal, owner ) != 0 ) {
1047 /* Entry still owned by hardware; end of poll */
1051 /* Handle completion */
1052 if ( ( rc = arbel_complete ( ibdev, cq, cqe, complete_send,
1053 complete_recv ) ) != 0 ) {
1054 DBGC ( arbel, "Arbel %p failed to complete: %s\n",
1055 arbel, strerror ( rc ) );
1056 DBGC_HD ( arbel, cqe, sizeof ( *cqe ) );
1059 /* Return ownership to hardware */
1060 MLX_FILL_1 ( &cqe->normal, 7, owner, 1 );
1062 /* Update completion queue's index */
1064 /* Update doorbell record */
1065 ci_db_rec = &arbel->db_rec[arbel_cq->ci_doorbell_idx].cq_ci;
1066 MLX_FILL_1 ( ci_db_rec, 0,
1067 counter, ( cq->next_idx & 0xffffffffUL ) );
1071 /***************************************************************************
1073 * Multicast group operations
1075 ***************************************************************************
1079 * Attach to multicast group
1081 * @v ibdev Infiniband device
1083 * @v gid Multicast GID
1084 * @ret rc Return status code
1086 static int arbel_mcast_attach ( struct ib_device *ibdev,
1087 struct ib_queue_pair *qp,
1088 struct ib_gid *gid ) {
1089 struct arbel *arbel = ibdev->dev_priv;
1090 struct arbelprm_mgm_hash hash;
1091 struct arbelprm_mgm_entry mgm;
1095 /* Generate hash table index */
1096 if ( ( rc = arbel_cmd_mgid_hash ( arbel, gid, &hash ) ) != 0 ) {
1097 DBGC ( arbel, "Arbel %p could not hash GID: %s\n",
1098 arbel, strerror ( rc ) );
1101 index = MLX_GET ( &hash, hash );
1103 /* Check for existing hash table entry */
1104 if ( ( rc = arbel_cmd_read_mgm ( arbel, index, &mgm ) ) != 0 ) {
1105 DBGC ( arbel, "Arbel %p could not read MGM %#x: %s\n",
1106 arbel, index, strerror ( rc ) );
1109 if ( MLX_GET ( &mgm, mgmqp_0.qi ) != 0 ) {
1110 /* FIXME: this implementation allows only a single QP
1111 * per multicast group, and doesn't handle hash
1112 * collisions. Sufficient for IPoIB but may need to
1113 * be extended in future.
1115 DBGC ( arbel, "Arbel %p MGID index %#x already in use\n",
1120 /* Update hash table entry */
1121 MLX_FILL_2 ( &mgm, 8,
1122 mgmqp_0.qpn_i, qp->qpn,
1124 memcpy ( &mgm.u.dwords[4], gid, sizeof ( *gid ) );
1125 if ( ( rc = arbel_cmd_write_mgm ( arbel, index, &mgm ) ) != 0 ) {
1126 DBGC ( arbel, "Arbel %p could not write MGM %#x: %s\n",
1127 arbel, index, strerror ( rc ) );
1135 * Detach from multicast group
1137 * @v ibdev Infiniband device
1139 * @v gid Multicast GID
1141 static void arbel_mcast_detach ( struct ib_device *ibdev,
1142 struct ib_queue_pair *qp __unused,
1143 struct ib_gid *gid ) {
1144 struct arbel *arbel = ibdev->dev_priv;
1145 struct arbelprm_mgm_hash hash;
1146 struct arbelprm_mgm_entry mgm;
1150 /* Generate hash table index */
1151 if ( ( rc = arbel_cmd_mgid_hash ( arbel, gid, &hash ) ) != 0 ) {
1152 DBGC ( arbel, "Arbel %p could not hash GID: %s\n",
1153 arbel, strerror ( rc ) );
1156 index = MLX_GET ( &hash, hash );
1158 /* Clear hash table entry */
1159 memset ( &mgm, 0, sizeof ( mgm ) );
1160 if ( ( rc = arbel_cmd_write_mgm ( arbel, index, &mgm ) ) != 0 ) {
1161 DBGC ( arbel, "Arbel %p could not write MGM %#x: %s\n",
1162 arbel, index, strerror ( rc ) );
1167 /** Arbel Infiniband operations */
1168 static struct ib_device_operations arbel_ib_operations = {
1169 .create_cq = arbel_create_cq,
1170 .destroy_cq = arbel_destroy_cq,
1171 .create_qp = arbel_create_qp,
1172 .destroy_qp = arbel_destroy_qp,
1173 .post_send = arbel_post_send,
1174 .post_recv = arbel_post_recv,
1175 .poll_cq = arbel_poll_cq,
1176 .mcast_attach = arbel_mcast_attach,
1177 .mcast_detach = arbel_mcast_detach,
1181 static int arbel_mad_ifc ( struct arbel *arbel,
1182 union arbelprm_mad *mad ) {
1183 struct ib_mad_hdr *hdr = &mad->mad.mad_hdr;
1186 hdr->base_version = IB_MGMT_BASE_VERSION;
1187 if ( ( rc = arbel_cmd_mad_ifc ( arbel, mad ) ) != 0 ) {
1188 DBGC ( arbel, "Arbel %p could not issue MAD IFC: %s\n",
1189 arbel, strerror ( rc ) );
1192 if ( hdr->status != 0 ) {
1193 DBGC ( arbel, "Arbel %p MAD IFC status %04x\n",
1194 arbel, ntohs ( hdr->status ) );
1200 static int arbel_get_port_info ( struct arbel *arbel,
1201 struct ib_mad_port_info *port_info ) {
1202 union arbelprm_mad mad;
1203 struct ib_mad_hdr *hdr = &mad.mad.mad_hdr;
1206 memset ( &mad, 0, sizeof ( mad ) );
1207 hdr->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
1208 hdr->class_version = 1;
1209 hdr->method = IB_MGMT_METHOD_GET;
1210 hdr->attr_id = htons ( IB_SMP_ATTR_PORT_INFO );
1211 hdr->attr_mod = htonl ( PXE_IB_PORT );
1212 if ( ( rc = arbel_mad_ifc ( arbel, &mad ) ) != 0 ) {
1213 DBGC ( arbel, "Arbel %p could not get port info: %s\n",
1214 arbel, strerror ( rc ) );
1217 memcpy ( port_info, &mad.mad.port_info, sizeof ( *port_info ) );
1221 static int arbel_get_guid_info ( struct arbel *arbel,
1222 struct ib_mad_guid_info *guid_info ) {
1223 union arbelprm_mad mad;
1224 struct ib_mad_hdr *hdr = &mad.mad.mad_hdr;
1227 memset ( &mad, 0, sizeof ( mad ) );
1228 hdr->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
1229 hdr->class_version = 1;
1230 hdr->method = IB_MGMT_METHOD_GET;
1231 hdr->attr_id = htons ( IB_SMP_ATTR_GUID_INFO );
1232 if ( ( rc = arbel_mad_ifc ( arbel, &mad ) ) != 0 ) {
1233 DBGC ( arbel, "Arbel %p could not get GUID info: %s\n",
1234 arbel, strerror ( rc ) );
1237 memcpy ( guid_info, &mad.mad.guid_info, sizeof ( *guid_info ) );
1241 static int arbel_get_pkey_table ( struct arbel *arbel,
1242 struct ib_mad_pkey_table *pkey_table ) {
1243 union arbelprm_mad mad;
1244 struct ib_mad_hdr *hdr = &mad.mad.mad_hdr;
1247 memset ( &mad, 0, sizeof ( mad ) );
1248 hdr->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
1249 hdr->class_version = 1;
1250 hdr->method = IB_MGMT_METHOD_GET;
1251 hdr->attr_id = htons ( IB_SMP_ATTR_PKEY_TABLE );
1252 if ( ( rc = arbel_mad_ifc ( arbel, &mad ) ) != 0 ) {
1253 DBGC ( arbel, "Arbel %p could not get pkey table: %s\n",
1254 arbel, strerror ( rc ) );
1257 memcpy ( pkey_table, &mad.mad.pkey_table, sizeof ( *pkey_table ) );
1261 static int arbel_get_port_gid ( struct arbel *arbel,
1262 struct ib_gid *port_gid ) {
1264 /* This union exists just to save stack space */
1265 struct ib_mad_port_info port_info;
1266 struct ib_mad_guid_info guid_info;
1270 /* Port info gives us the first half of the port GID */
1271 if ( ( rc = arbel_get_port_info ( arbel, &u.port_info ) ) != 0 )
1273 memcpy ( &port_gid->u.bytes[0], u.port_info.gid_prefix, 8 );
1275 /* GUID info gives us the second half of the port GID */
1276 if ( ( rc = arbel_get_guid_info ( arbel, &u.guid_info ) ) != 0 )
1278 memcpy ( &port_gid->u.bytes[8], u.guid_info.gid_local, 8 );
1283 static int arbel_get_sm_lid ( struct arbel *arbel,
1284 unsigned long *sm_lid ) {
1285 struct ib_mad_port_info port_info;
1288 if ( ( rc = arbel_get_port_info ( arbel, &port_info ) ) != 0 )
1290 *sm_lid = ntohs ( port_info.mastersm_lid );
1294 static int arbel_get_pkey ( struct arbel *arbel, unsigned int *pkey ) {
1295 struct ib_mad_pkey_table pkey_table;
1298 if ( ( rc = arbel_get_pkey_table ( arbel, &pkey_table ) ) != 0 )
1300 *pkey = ntohs ( pkey_table.pkey[0][0] );
1309 * @ret rc Return status code
1311 static int arbel_probe ( struct pci_device *pci,
1312 const struct pci_device_id *id __unused ) {
1313 struct ib_device *ibdev;
1314 struct arbelprm_query_fw fw;
1315 struct arbelprm_query_dev_lim dev_lim;
1316 struct arbel *arbel;
1320 /* Allocate Infiniband device */
1321 ibdev = alloc_ibdev ( sizeof ( *arbel ) );
1326 ibdev->op = &arbel_ib_operations;
1327 pci_set_drvdata ( pci, ibdev );
1328 ibdev->dev = &pci->dev;
1329 arbel = ibdev->dev_priv;
1330 memset ( arbel, 0, sizeof ( *arbel ) );
1332 /* Allocate space for mailboxes */
1333 arbel->mailbox_in = malloc_dma ( ARBEL_MBOX_SIZE, ARBEL_MBOX_ALIGN );
1334 if ( ! arbel->mailbox_in ) {
1336 goto err_mailbox_in;
1338 arbel->mailbox_out = malloc_dma ( ARBEL_MBOX_SIZE, ARBEL_MBOX_ALIGN );
1339 if ( ! arbel->mailbox_out ) {
1341 goto err_mailbox_out;
1344 /* Fix up PCI device */
1345 adjust_pci_device ( pci );
1348 arbel->config = ioremap ( pci_bar_start ( pci, ARBEL_PCI_CONFIG_BAR ),
1349 ARBEL_PCI_CONFIG_BAR_SIZE );
1350 arbel->uar = ioremap ( ( pci_bar_start ( pci, ARBEL_PCI_UAR_BAR ) +
1351 ARBEL_PCI_UAR_IDX * ARBEL_PCI_UAR_SIZE ),
1352 ARBEL_PCI_UAR_SIZE );
1354 /* Initialise firmware */
1355 if ( ( rc = arbel_cmd_query_fw ( arbel, &fw ) ) != 0 ) {
1356 DBGC ( arbel, "Arbel %p could not query firmware: %s\n",
1357 arbel, strerror ( rc ) );
1360 DBGC ( arbel, "Arbel %p firmware version %ld.%ld.%ld\n", arbel,
1361 MLX_GET ( &fw, fw_rev_major ), MLX_GET ( &fw, fw_rev_minor ),
1362 MLX_GET ( &fw, fw_rev_subminor ) );
1364 /* Initialise hardware */
1365 if ( ( rc = ib_driver_init ( pci, &qph ) ) != 0 )
1366 goto err_ib_driver_init;
1368 /* Hack up IB structures */
1370 arbel->config = memfree_pci_dev.cr_space;
1371 arbel->uar = memfree_pci_dev.uar;
1372 arbel->mailbox_in = dev_buffers_p->inprm_buf;
1373 arbel->mailbox_out = dev_buffers_p->outprm_buf;
1375 arbel->db_rec = dev_ib_data.uar_context_base;
1376 arbel->reserved_lkey = dev_ib_data.mkey;
1377 arbel->eqn = dev_ib_data.eq.eqn;
1379 /* Get device limits */
1380 if ( ( rc = arbel_cmd_query_dev_lim ( arbel, &dev_lim ) ) != 0 ) {
1381 DBGC ( arbel, "Arbel %p could not get device limits: %s\n",
1382 arbel, strerror ( rc ) );
1383 goto err_query_dev_lim;
1385 arbel->limits.reserved_uars = MLX_GET ( &dev_lim, num_rsvd_uars );
1386 arbel->limits.reserved_cqs =
1387 ( 1 << MLX_GET ( &dev_lim, log2_rsvd_cqs ) );
1388 arbel->limits.reserved_qps =
1389 ( 1 << MLX_GET ( &dev_lim, log2_rsvd_qps ) );
1391 /* Get subnet manager LID */
1392 if ( ( rc = arbel_get_sm_lid ( arbel, &ibdev->sm_lid ) ) != 0 ) {
1393 DBGC ( arbel, "Arbel %p could not determine subnet manager "
1394 "LID: %s\n", arbel, strerror ( rc ) );
1395 goto err_get_sm_lid;
1399 if ( ( rc = arbel_get_port_gid ( arbel, &ibdev->port_gid ) ) != 0 ) {
1400 DBGC ( arbel, "Arbel %p could not determine port GID: %s\n",
1401 arbel, strerror ( rc ) );
1402 goto err_get_port_gid;
1405 /* Get partition key */
1406 if ( ( rc = arbel_get_pkey ( arbel, &ibdev->pkey ) ) != 0 ) {
1407 DBGC ( arbel, "Arbel %p could not determine partition key: "
1408 "%s\n", arbel, strerror ( rc ) );
1412 /* Add IPoIB device */
1413 if ( ( rc = ipoib_probe ( ibdev ) ) != 0 ) {
1414 DBGC ( arbel, "Arbel %p could not add IPoIB device: %s\n",
1415 arbel, strerror ( rc ) );
1416 goto err_ipoib_probe;
1426 ib_driver_close ( 0 );
1429 free_dma ( arbel->mailbox_out, ARBEL_MBOX_SIZE );
1431 free_dma ( arbel->mailbox_in, ARBEL_MBOX_SIZE );
1433 free_ibdev ( ibdev );
1443 static void arbel_remove ( struct pci_device *pci ) {
1444 struct ib_device *ibdev = pci_get_drvdata ( pci );
1445 struct arbel *arbel = ibdev->dev_priv;
1447 ipoib_remove ( ibdev );
1448 ib_driver_close ( 0 );
1449 free_dma ( arbel->mailbox_out, ARBEL_MBOX_SIZE );
1450 free_dma ( arbel->mailbox_in, ARBEL_MBOX_SIZE );
1451 free_ibdev ( ibdev );
1454 static struct pci_device_id arbel_nics[] = {
1455 PCI_ROM ( 0x15b3, 0x6282, "MT25218", "MT25218 HCA driver" ),
1456 PCI_ROM ( 0x15b3, 0x6274, "MT25204", "MT25204 HCA driver" ),
1459 struct pci_driver arbel_driver __pci_driver = {
1461 .id_count = ( sizeof ( arbel_nics ) / sizeof ( arbel_nics[0] ) ),
1462 .probe = arbel_probe,
1463 .remove = arbel_remove,