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>
20 /* to get some global routines like printf */
21 #include "etherboot.h"
22 /* to get the interface to the body of the program */
25 #include "mt25218_imp.c"
30 static const struct ib_gid arbel_no_gid = {
31 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2 }
35 #define MLX_RX_MAX_FILL NUM_IPOIB_RCV_WQES
38 /** Queue pair handle */
40 /** Broadcast Address Vector */
42 /** Send completion queue */
44 /** Receive completion queue */
52 static struct io_buffer *static_ipoib_tx_ring[NUM_IPOIB_SND_WQES];
53 static struct io_buffer *static_ipoib_rx_ring[NUM_IPOIB_RCV_WQES];
55 static struct arbel static_arbel;
57 static struct arbel_completion_queue static_arbel_ipoib_send_cq = {
58 .ci_doorbell_idx = IPOIB_SND_CQ_CI_DB_IDX,
60 static struct ib_completion_queue static_ipoib_send_cq = {
61 .cqn = 1234, /* Only used for debug messages */
62 .num_cqes = NUM_IPOIB_SND_CQES,
63 .work_queues = LIST_HEAD_INIT ( static_ipoib_send_cq.work_queues ),
64 .dev_priv = &static_arbel_ipoib_send_cq,
67 static struct arbel_completion_queue static_arbel_ipoib_recv_cq = {
68 .ci_doorbell_idx = IPOIB_RCV_CQ_CI_DB_IDX,
70 static struct ib_completion_queue static_ipoib_recv_cq = {
71 .cqn = 2345, /* Only used for debug messages */
72 .num_cqes = NUM_IPOIB_RCV_CQES,
73 .work_queues = LIST_HEAD_INIT ( static_ipoib_recv_cq.work_queues ),
74 .dev_priv = &static_arbel_ipoib_recv_cq,
77 static struct arbel_queue_pair static_arbel_ipoib_qp = {
79 .doorbell_idx = IPOIB_SND_QP_DB_IDX,
82 .doorbell_idx = IPOIB_RCV_QP_DB_IDX,
85 static struct ib_queue_pair static_ipoib_qp = {
87 .qp = &static_ipoib_qp,
89 .cq = &static_ipoib_send_cq,
90 .num_wqes = NUM_IPOIB_SND_WQES,
91 .iobufs = static_ipoib_tx_ring,
92 .list = LIST_HEAD_INIT (static_ipoib_qp.send.list),
93 .dev_priv = &static_arbel_ipoib_qp.send,
96 .qp = &static_ipoib_qp,
98 .cq = &static_ipoib_recv_cq,
99 .num_wqes = NUM_IPOIB_RCV_WQES,
100 .iobufs = static_ipoib_rx_ring,
101 .list = LIST_HEAD_INIT (static_ipoib_qp.recv.list),
102 .dev_priv = &static_arbel_ipoib_qp.recv,
104 .dev_priv = &static_arbel_ipoib_qp,
108 static struct ib_device static_ibdev = {
109 .dev_priv = &static_arbel,
114 * Open network device
116 * @v netdev Network device
117 * @ret rc Return status code
119 static int mlx_open ( struct net_device *netdev ) {
127 * Close network device
129 * @v netdev Network device
131 static void mlx_close ( struct net_device *netdev ) {
137 static int arbel_post_send ( struct ib_device *ibdev,
138 struct ib_queue_pair *qp,
139 struct ib_address_vector *av,
140 struct io_buffer *iobuf );
142 static int mlx_transmit_direct ( struct net_device *netdev,
143 struct io_buffer *iobuf ) {
144 struct mlx_nic *mlx = netdev->priv;
147 struct ud_av_st *bcast_av = mlx->bcast_av;
148 struct arbelprm_ud_address_vector *bav =
149 ( struct arbelprm_ud_address_vector * ) &bcast_av->av;
150 struct ib_address_vector av = {
151 .dest_qp = bcast_av->dest_qp,
152 .qkey = bcast_av->qkey,
153 .dlid = MLX_GET ( bav, rlid ),
154 .rate = ( MLX_GET ( bav, max_stat_rate ) ? 1 : 4 ),
155 .sl = MLX_GET ( bav, sl ),
158 memcpy ( &av.gid, ( ( void * ) bav ) + 16, 16 );
160 rc = arbel_post_send ( &static_ibdev, &static_ipoib_qp, &av, iobuf );
165 static void arbel_poll_cq ( struct ib_device *ibdev,
166 struct ib_completion_queue *cq,
167 ib_completer_t complete_send,
168 ib_completer_t complete_recv );
170 static void temp_complete_send ( struct ib_device *ibdev __unused,
171 struct ib_queue_pair *qp,
172 struct ib_completion *completion,
173 struct io_buffer *iobuf ) {
174 struct net_device *netdev = qp->owner_priv;
176 DBG ( "Wahey! TX completion\n" );
177 netdev_tx_complete_err ( netdev, iobuf,
178 ( completion->syndrome ? -EIO : 0 ) );
181 static void temp_complete_recv ( struct ib_device *ibdev __unused,
182 struct ib_queue_pair *qp,
183 struct ib_completion *completion,
184 struct io_buffer *iobuf ) {
185 struct net_device *netdev = qp->owner_priv;
186 struct mlx_nic *mlx = netdev->priv;
188 DBG ( "Yay! RX completion on %p len %zx:\n", iobuf, completion->len );
189 if ( completion->syndrome ) {
190 netdev_rx_err ( netdev, iobuf, -EIO );
192 iob_put ( iobuf, completion->len );
193 iob_pull ( iobuf, sizeof ( struct ib_global_route_header ) );
194 netdev_rx ( netdev, iobuf );
200 static int arbel_post_recv ( struct ib_device *ibdev,
201 struct ib_queue_pair *qp,
202 struct io_buffer *iobuf );
204 static void mlx_refill_rx ( struct net_device *netdev ) {
205 struct mlx_nic *mlx = netdev->priv;
206 struct io_buffer *iobuf;
209 while ( mlx->rx_fill < MLX_RX_MAX_FILL ) {
210 iobuf = alloc_iob ( 2048 );
213 DBG ( "Posting RX buffer %p:\n", iobuf );
214 if ( ( rc = arbel_post_recv ( &static_ibdev,
225 * Poll for completed and received packets
227 * @v netdev Network device
229 static void mlx_poll ( struct net_device *netdev ) {
230 struct mlx_nic *mlx = netdev->priv;
233 if ( ( rc = poll_error_buf() ) != 0 ) {
234 DBG ( "poll_error_buf() failed: %s\n", strerror ( rc ) );
238 /* Drain event queue. We can ignore events, since we're going
239 * to just poll all completion queues anyway.
241 if ( ( rc = drain_eq() ) != 0 ) {
242 DBG ( "drain_eq() failed: %s\n", strerror ( rc ) );
246 /* Poll completion queues */
247 arbel_poll_cq ( &static_ibdev, &static_ipoib_send_cq,
248 temp_complete_send, temp_complete_recv );
249 arbel_poll_cq ( &static_ibdev, &static_ipoib_recv_cq,
250 temp_complete_send, temp_complete_recv );
252 mlx_refill_rx ( netdev );
256 * Enable or disable interrupts
258 * @v netdev Network device
259 * @v enable Interrupts should be enabled
261 static void mlx_irq ( struct net_device *netdev, int enable ) {
268 static struct net_device_operations mlx_operations = {
271 .transmit = mlx_transmit_direct,
279 /***************************************************************************
281 * Queue number allocation
283 ***************************************************************************
287 * Allocate queue number
289 * @v q_inuse Queue usage bitmask
290 * @v max_inuse Maximum number of in-use queues
291 * @ret qn_offset Free queue number offset, or negative error
293 static int arbel_alloc_qn_offset ( arbel_bitmask_t *q_inuse,
294 unsigned int max_inuse ) {
295 unsigned int qn_offset = 0;
296 arbel_bitmask_t mask = 1;
298 while ( qn_offset < max_inuse ) {
299 if ( ( mask & *q_inuse ) == 0 ) {
316 * @v q_inuse Queue usage bitmask
317 * @v qn_offset Queue number offset
319 static void arbel_free_qn_offset ( arbel_bitmask_t *q_inuse, int qn_offset ) {
320 arbel_bitmask_t mask;
322 mask = ( 1 << ( qn_offset % ( 8 * sizeof ( mask ) ) ) );
323 q_inuse += ( qn_offset / ( 8 * sizeof ( mask ) ) );
327 /***************************************************************************
331 ***************************************************************************
335 * Wait for Arbel command completion
337 * @v arbel Arbel device
338 * @ret rc Return status code
340 static int arbel_cmd_wait ( struct arbel *arbel,
341 struct arbelprm_hca_command_register *hcr ) {
344 for ( wait = ARBEL_HCR_MAX_WAIT_MS ; wait ; wait-- ) {
346 readl ( arbel->config + ARBEL_HCR_REG ( 6 ) );
347 if ( MLX_GET ( hcr, go ) == 0 )
357 * @v arbel Arbel device
358 * @v command Command opcode, flags and input/output lengths
359 * @v op_mod Opcode modifier (0 if no modifier applicable)
360 * @v in Input parameters
361 * @v in_mod Input modifier (0 if no modifier applicable)
362 * @v out Output parameters
363 * @ret rc Return status code
365 static int arbel_cmd ( struct arbel *arbel, unsigned long command,
366 unsigned int op_mod, const void *in,
367 unsigned int in_mod, void *out ) {
368 struct arbelprm_hca_command_register hcr;
369 unsigned int opcode = ARBEL_HCR_OPCODE ( command );
370 size_t in_len = ARBEL_HCR_IN_LEN ( command );
371 size_t out_len = ARBEL_HCR_OUT_LEN ( command );
378 DBGC ( arbel, "Arbel %p command %02x in %zx%s out %zx%s\n",
379 arbel, opcode, in_len,
380 ( ( command & ARBEL_HCR_IN_MBOX ) ? "(mbox)" : "" ), out_len,
381 ( ( command & ARBEL_HCR_OUT_MBOX ) ? "(mbox)" : "" ) );
383 /* Check that HCR is free */
384 if ( ( rc = arbel_cmd_wait ( arbel, &hcr ) ) != 0 ) {
385 DBGC ( arbel, "Arbel %p command interface locked\n", arbel );
390 memset ( &hcr, 0, sizeof ( hcr ) );
391 in_buffer = &hcr.u.dwords[0];
392 if ( in_len && ( command & ARBEL_HCR_IN_MBOX ) ) {
393 in_buffer = arbel->mailbox_in;
394 MLX_FILL_1 ( &hcr, 1, in_param_l, virt_to_bus ( in_buffer ) );
396 memcpy ( in_buffer, in, in_len );
397 MLX_FILL_1 ( &hcr, 2, input_modifier, in_mod );
398 out_buffer = &hcr.u.dwords[3];
399 if ( out_len && ( command & ARBEL_HCR_OUT_MBOX ) ) {
400 out_buffer = arbel->mailbox_out;
401 MLX_FILL_1 ( &hcr, 4, out_param_l,
402 virt_to_bus ( out_buffer ) );
404 MLX_FILL_3 ( &hcr, 6,
406 opcode_modifier, op_mod,
410 for ( i = 0 ; i < ( sizeof ( hcr ) / sizeof ( hcr.u.dwords[0] ) ) ;
412 writel ( hcr.u.dwords[i],
413 arbel->config + ARBEL_HCR_REG ( i ) );
417 /* Wait for command completion */
418 if ( ( rc = arbel_cmd_wait ( arbel, &hcr ) ) != 0 ) {
419 DBGC ( arbel, "Arbel %p timed out waiting for command:\n",
421 DBGC_HD ( arbel, &hcr, sizeof ( hcr ) );
425 /* Check command status */
426 status = MLX_GET ( &hcr, status );
428 DBGC ( arbel, "Arbel %p command failed with status %02x:\n",
430 DBGC_HD ( arbel, &hcr, sizeof ( hcr ) );
434 /* Read output parameters, if any */
435 hcr.u.dwords[3] = readl ( arbel->config + ARBEL_HCR_REG ( 3 ) );
436 hcr.u.dwords[4] = readl ( arbel->config + ARBEL_HCR_REG ( 4 ) );
437 memcpy ( out, out_buffer, out_len );
443 arbel_cmd_query_dev_lim ( struct arbel *arbel,
444 struct arbelprm_query_dev_lim *dev_lim ) {
445 return arbel_cmd ( arbel,
446 ARBEL_HCR_OUT_CMD ( ARBEL_HCR_QUERY_DEV_LIM,
447 1, sizeof ( *dev_lim ) ),
448 0, NULL, 0, dev_lim );
452 arbel_cmd_sw2hw_cq ( struct arbel *arbel, unsigned long cqn,
453 const struct arbelprm_completion_queue_context *cqctx ) {
454 return arbel_cmd ( arbel,
455 ARBEL_HCR_IN_CMD ( ARBEL_HCR_SW2HW_CQ,
456 1, sizeof ( *cqctx ) ),
457 0, cqctx, cqn, NULL );
461 arbel_cmd_hw2sw_cq ( struct arbel *arbel, unsigned long cqn ) {
462 return arbel_cmd ( arbel,
463 ARBEL_HCR_VOID_CMD ( ARBEL_HCR_HW2SW_CQ ),
464 1, NULL, cqn, NULL );
468 arbel_cmd_rst2init_qpee ( struct arbel *arbel, unsigned long qpn,
469 struct arbelprm_queue_pair_ee_context_entry *ctx ) {
470 return arbel_cmd ( arbel,
471 ARBEL_HCR_IN_CMD ( ARBEL_HCR_RST2INIT_QPEE,
472 1, sizeof ( *ctx ) ),
477 arbel_cmd_init2rtr_qpee ( struct arbel *arbel, unsigned long qpn,
478 struct arbelprm_queue_pair_ee_context_entry *ctx ) {
479 return arbel_cmd ( arbel,
480 ARBEL_HCR_IN_CMD ( ARBEL_HCR_INIT2RTR_QPEE,
481 1, sizeof ( *ctx ) ),
486 arbel_cmd_rtr2rts_qpee ( struct arbel *arbel, unsigned long qpn,
487 struct arbelprm_queue_pair_ee_context_entry *ctx ) {
488 return arbel_cmd ( arbel,
489 ARBEL_HCR_IN_CMD ( ARBEL_HCR_RTR2RTS_QPEE,
490 1, sizeof ( *ctx ) ),
495 arbel_cmd_2rst_qpee ( struct arbel *arbel, unsigned long qpn ) {
496 return arbel_cmd ( arbel,
497 ARBEL_HCR_VOID_CMD ( ARBEL_HCR_2RST_QPEE ),
498 0x03, NULL, qpn, NULL );
501 /***************************************************************************
503 * Completion queue operations
505 ***************************************************************************
509 * Create completion queue
511 * @v ibdev Infiniband device
512 * @v cq Completion queue
513 * @ret rc Return status code
515 static int arbel_create_cq ( struct ib_device *ibdev,
516 struct ib_completion_queue *cq ) {
517 struct arbel *arbel = ibdev->dev_priv;
518 struct arbel_completion_queue *arbel_cq;
519 struct arbelprm_completion_queue_context cqctx;
520 struct arbelprm_cq_ci_db_record *ci_db_rec;
521 struct arbelprm_cq_arm_db_record *arm_db_rec;
526 /* Find a free completion queue number */
527 cqn_offset = arbel_alloc_qn_offset ( arbel->cq_inuse, ARBEL_MAX_CQS );
528 if ( cqn_offset < 0 ) {
529 DBGC ( arbel, "Arbel %p out of completion queues\n", arbel );
533 cq->cqn = ( arbel->limits.reserved_cqs + cqn_offset );
535 /* Allocate control structures */
536 arbel_cq = zalloc ( sizeof ( *arbel_cq ) );
541 arbel_cq->ci_doorbell_idx = arbel_cq_ci_doorbell_idx ( cqn_offset );
542 arbel_cq->arm_doorbell_idx = arbel_cq_arm_doorbell_idx ( cqn_offset );
544 /* Allocate completion queue itself */
545 arbel_cq->cqe_size = ( cq->num_cqes * sizeof ( arbel_cq->cqe[0] ) );
546 arbel_cq->cqe = malloc_dma ( arbel_cq->cqe_size,
547 sizeof ( arbel_cq->cqe[0] ) );
548 if ( ! arbel_cq->cqe ) {
552 memset ( arbel_cq->cqe, 0, arbel_cq->cqe_size );
553 for ( i = 0 ; i < cq->num_cqes ; i++ ) {
554 MLX_FILL_1 ( &arbel_cq->cqe[i].normal, 7, owner, 1 );
558 /* Initialise doorbell records */
559 ci_db_rec = &arbel->db_rec[arbel_cq->ci_doorbell_idx].cq_ci;
560 MLX_FILL_1 ( ci_db_rec, 0, counter, 0 );
561 MLX_FILL_2 ( ci_db_rec, 1,
562 res, ARBEL_UAR_RES_CQ_CI,
563 cq_number, cq->cqn );
564 arm_db_rec = &arbel->db_rec[arbel_cq->arm_doorbell_idx].cq_arm;
565 MLX_FILL_1 ( arm_db_rec, 0, counter, 0 );
566 MLX_FILL_2 ( arm_db_rec, 1,
567 res, ARBEL_UAR_RES_CQ_ARM,
568 cq_number, cq->cqn );
570 /* Hand queue over to hardware */
571 memset ( &cqctx, 0, sizeof ( cqctx ) );
572 MLX_FILL_1 ( &cqctx, 0, st, 0xa /* "Event fired" */ );
573 MLX_FILL_1 ( &cqctx, 2, start_address_l,
574 virt_to_bus ( arbel_cq->cqe ) );
575 MLX_FILL_2 ( &cqctx, 3,
576 usr_page, arbel->limits.reserved_uars,
577 log_cq_size, ( fls ( cq->num_cqes ) - 1 ) );
578 MLX_FILL_1 ( &cqctx, 5, c_eqn, arbel->eqn );
579 MLX_FILL_1 ( &cqctx, 6, pd, ARBEL_GLOBAL_PD );
580 MLX_FILL_1 ( &cqctx, 7, l_key, arbel->reserved_lkey );
581 MLX_FILL_1 ( &cqctx, 12, cqn, cq->cqn );
582 MLX_FILL_1 ( &cqctx, 13,
583 cq_ci_db_record, arbel_cq->ci_doorbell_idx );
584 MLX_FILL_1 ( &cqctx, 14,
585 cq_state_db_record, arbel_cq->arm_doorbell_idx );
586 if ( ( rc = arbel_cmd_sw2hw_cq ( arbel, cq->cqn, &cqctx ) ) != 0 ) {
587 DBGC ( arbel, "Arbel %p SW2HW_CQ failed: %s\n",
588 arbel, strerror ( rc ) );
592 cq->dev_priv = arbel_cq;
596 MLX_FILL_1 ( ci_db_rec, 1, res, ARBEL_UAR_RES_NONE );
597 MLX_FILL_1 ( arm_db_rec, 1, res, ARBEL_UAR_RES_NONE );
598 free_dma ( arbel_cq->cqe, arbel_cq->cqe_size );
602 arbel_free_qn_offset ( arbel->cq_inuse, cqn_offset );
608 * Destroy completion queue
610 * @v ibdev Infiniband device
611 * @v cq Completion queue
613 static void arbel_destroy_cq ( struct ib_device *ibdev,
614 struct ib_completion_queue *cq ) {
615 struct arbel *arbel = ibdev->dev_priv;
616 struct arbel_completion_queue *arbel_cq = cq->dev_priv;
617 struct arbelprm_cq_ci_db_record *ci_db_rec;
618 struct arbelprm_cq_arm_db_record *arm_db_rec;
622 /* Take ownership back from hardware */
623 if ( ( rc = arbel_cmd_hw2sw_cq ( arbel, cq->cqn ) ) != 0 ) {
624 DBGC ( arbel, "Arbel %p FATAL HW2SW_CQ failed on CQN %#lx: "
625 "%s\n", arbel, cq->cqn, strerror ( rc ) );
626 /* Leak memory and return; at least we avoid corruption */
630 /* Clear doorbell records */
631 ci_db_rec = &arbel->db_rec[arbel_cq->ci_doorbell_idx].cq_ci;
632 arm_db_rec = &arbel->db_rec[arbel_cq->arm_doorbell_idx].cq_arm;
633 MLX_FILL_1 ( ci_db_rec, 1, res, ARBEL_UAR_RES_NONE );
634 MLX_FILL_1 ( arm_db_rec, 1, res, ARBEL_UAR_RES_NONE );
637 free_dma ( arbel_cq->cqe, arbel_cq->cqe_size );
640 /* Mark queue number as free */
641 cqn_offset = ( cq->cqn - arbel->limits.reserved_cqs );
642 arbel_free_qn_offset ( arbel->cq_inuse, cqn_offset );
647 /***************************************************************************
649 * Queue pair operations
651 ***************************************************************************
654 static int arbel_create_send_wq ( struct arbel_send_work_queue *arbel_send_wq,
655 unsigned int num_wqes ) {
657 arbel_send_wq->wqe_size = ( num_wqes *
658 sizeof ( arbel_send_wq->wqe[0] ) );
659 arbel_send_wq->wqe = malloc_dma ( arbel_send_wq->wqe_size,
660 sizeof ( arbel_send_wq->wqe[0] ) );
661 if ( ! arbel_send_wq->wqe )
664 // initialise (prelink?)
667 static int arbel_create_recv_wq ( struct arbel_recv_work_queue *arbel_recv_wq,
668 unsigned int num_wqes ) {
670 arbel_recv_wq->wqe_size = ( num_wqes *
671 sizeof ( arbel_recv_wq->wqe[0] ) );
672 arbel_recv_wq->wqe = malloc_dma ( arbel_recv_wq->wqe_size,
673 sizeof ( arbel_recv_wq->wqe[0] ) );
674 if ( ! arbel_recv_wq->wqe )
677 // initialise (prelink?)
686 * @v ibdev Infiniband device
688 * @ret rc Return status code
690 static int arbel_create_qp ( struct ib_device *ibdev,
691 struct ib_queue_pair *qp ) {
692 struct arbel *arbel = ibdev->dev_priv;
693 struct arbel_queue_pair *arbel_qp;
694 struct arbelprm_queue_pair_ee_context_entry qpctx;
695 struct arbelprm_qp_db_record *send_db_rec;
696 struct arbelprm_qp_db_record *recv_db_rec;
700 /* Find a free queue pair number */
701 qpn_offset = arbel_alloc_qn_offset ( arbel->qp_inuse, ARBEL_MAX_QPS );
702 if ( qpn_offset < 0 ) {
703 DBGC ( arbel, "Arbel %p out of queue pairs\n", arbel );
707 qp->qpn = ( ARBEL_QPN_BASE + arbel->limits.reserved_qps + qpn_offset );
709 /* Allocate control structures */
710 arbel_qp = zalloc ( sizeof ( *arbel_qp ) );
715 arbel_qp->send.doorbell_idx = arbel_send_doorbell_idx ( qpn_offset );
716 arbel_qp->recv.doorbell_idx = arbel_recv_doorbell_idx ( qpn_offset );
718 /* Create send and receive work queues */
719 if ( ( rc = arbel_create_send_wq ( &arbel_qp->send,
720 qp->send.num_wqes ) ) != 0 )
721 goto err_create_send_wq;
722 if ( ( rc = arbel_create_recv_wq ( &arbel_qp->recv,
723 qp->recv.num_wqes ) ) != 0 )
724 goto err_create_recv_wq;
726 /* Initialise doorbell records */
727 send_db_rec = &arbel->db_rec[arbel_qp->send.doorbell_idx].qp;
728 MLX_FILL_1 ( send_db_rec, 0, counter, 0 );
729 MLX_FILL_2 ( send_db_rec, 1,
730 res, ARBEL_UAR_RES_SQ,
731 qp_number, qp->qpn );
732 recv_db_rec = &arbel->db_rec[arbel_qp->recv.doorbell_idx].qp;
733 MLX_FILL_1 ( recv_db_rec, 0, counter, 0 );
734 MLX_FILL_2 ( recv_db_rec, 1,
735 res, ARBEL_UAR_RES_RQ,
736 qp_number, qp->qpn );
738 /* Hand queue over to hardware */
739 memset ( &qpctx, 0, sizeof ( qpctx ) );
740 // ... fill in context
741 if ( ( rc = arbel_cmd_rst2init_qpee ( arbel, qp->qpn, &qpctx )) != 0 ){
742 DBGC ( arbel, "Arbel %p RST2INIT_QPEE failed: %s\n",
743 arbel, strerror ( rc ) );
744 goto err_rst2init_qpee;
746 if ( ( rc = arbel_cmd_init2rtr_qpee ( arbel, qp->qpn, &qpctx )) != 0 ){
747 DBGC ( arbel, "Arbel %p INIT2RTR_QPEE failed: %s\n",
748 arbel, strerror ( rc ) );
749 goto err_init2rtr_qpee;
751 if ( ( rc = arbel_cmd_rtr2rts_qpee ( arbel, qp->qpn, &qpctx ) ) != 0 ){
752 DBGC ( arbel, "Arbel %p RTR2RTS_QPEE failed: %s\n",
753 arbel, strerror ( rc ) );
754 goto err_rtr2rts_qpee;
757 qp->dev_priv = arbel_qp;
762 arbel_cmd_2rst_qpee ( arbel, qp->qpn );
764 MLX_FILL_1 ( send_db_rec, 1, res, ARBEL_UAR_RES_NONE );
765 MLX_FILL_1 ( recv_db_rec, 1, res, ARBEL_UAR_RES_NONE );
766 free_dma ( arbel_qp->recv.wqe, arbel_qp->recv.wqe_size );
768 free_dma ( arbel_qp->send.wqe, arbel_qp->send.wqe_size );
772 arbel_free_qn_offset ( arbel->qp_inuse, qpn_offset );
780 * @v ibdev Infiniband device
783 static void arbel_destroy_qp ( struct ib_device *ibdev,
784 struct ib_queue_pair *qp ) {
785 struct arbel *arbel = ibdev->dev_priv;
786 struct arbel_queue_pair *arbel_qp = qp->dev_priv;
787 struct arbelprm_qp_db_record *send_db_rec;
788 struct arbelprm_qp_db_record *recv_db_rec;
792 /* Take ownership back from hardware */
793 if ( ( rc = arbel_cmd_2rst_qpee ( arbel, qp->qpn ) ) != 0 ) {
794 DBGC ( arbel, "Arbel %p FATAL 2RST_QPEE failed on QPN %#lx: "
795 "%s\n", arbel, qp->qpn, strerror ( rc ) );
796 /* Leak memory and return; at least we avoid corruption */
800 /* Clear doorbell records */
801 send_db_rec = &arbel->db_rec[arbel_qp->send.doorbell_idx].qp;
802 recv_db_rec = &arbel->db_rec[arbel_qp->recv.doorbell_idx].qp;
803 MLX_FILL_1 ( send_db_rec, 1, res, ARBEL_UAR_RES_NONE );
804 MLX_FILL_1 ( recv_db_rec, 1, res, ARBEL_UAR_RES_NONE );
807 free_dma ( arbel_qp->send.wqe, arbel_qp->send.wqe_size );
808 free_dma ( arbel_qp->recv.wqe, arbel_qp->recv.wqe_size );
811 /* Mark queue number as free */
812 qpn_offset = ( qp->qpn - ARBEL_QPN_BASE - arbel->limits.reserved_qps );
813 arbel_free_qn_offset ( arbel->qp_inuse, qpn_offset );
818 /***************************************************************************
820 * Work request operations
822 ***************************************************************************
826 * Ring doorbell register in UAR
828 * @v arbel Arbel device
829 * @v db_reg Doorbell register structure
830 * @v offset Address of doorbell
832 static void arbel_ring_doorbell ( struct arbel *arbel,
833 union arbelprm_doorbell_register *db_reg,
834 unsigned int offset ) {
836 DBG ( "arbel_ring_doorbell %08lx:%08lx to %lx\n",
837 db_reg->dword[0], db_reg->dword[1],
838 virt_to_phys ( arbel->uar + offset ) );
841 writel ( db_reg->dword[0], ( arbel->uar + offset + 0 ) );
843 writel ( db_reg->dword[1], ( arbel->uar + offset + 4 ) );
847 * Post send work queue entry
849 * @v ibdev Infiniband device
851 * @v av Address vector
852 * @v iobuf I/O buffer
853 * @ret rc Return status code
855 static int arbel_post_send ( struct ib_device *ibdev,
856 struct ib_queue_pair *qp,
857 struct ib_address_vector *av,
858 struct io_buffer *iobuf ) {
859 struct arbel *arbel = ibdev->dev_priv;
860 struct arbel_queue_pair *arbel_qp = qp->dev_priv;
861 struct ib_work_queue *wq = &qp->send;
862 struct arbel_send_work_queue *arbel_send_wq = &arbel_qp->send;
863 struct arbelprm_ud_send_wqe *prev_wqe;
864 struct arbelprm_ud_send_wqe *wqe;
865 struct arbelprm_qp_db_record *qp_db_rec;
866 union arbelprm_doorbell_register db_reg;
867 const struct ib_gid *gid;
868 unsigned int wqe_idx_mask;
871 /* Allocate work queue entry */
872 wqe_idx_mask = ( wq->num_wqes - 1 );
873 if ( wq->iobufs[wq->next_idx & wqe_idx_mask] ) {
874 DBGC ( arbel, "Arbel %p send queue full", arbel );
877 wq->iobufs[wq->next_idx & wqe_idx_mask] = iobuf;
878 prev_wqe = &arbel_send_wq->wqe[(wq->next_idx - 1) & wqe_idx_mask].ud;
879 wqe = &arbel_send_wq->wqe[wq->next_idx & wqe_idx_mask].ud;
881 /* Construct work queue entry */
882 MLX_FILL_1 ( &wqe->next, 1, always1, 1 );
883 memset ( &wqe->ctrl, 0, sizeof ( wqe->ctrl ) );
884 MLX_FILL_1 ( &wqe->ctrl, 0, always1, 1 );
885 memset ( &wqe->ud, 0, sizeof ( wqe->ud ) );
886 MLX_FILL_2 ( &wqe->ud, 0,
887 ud_address_vector.pd, ARBEL_GLOBAL_PD,
888 ud_address_vector.port_number, PXE_IB_PORT );
889 MLX_FILL_2 ( &wqe->ud, 1,
890 ud_address_vector.rlid, av->dlid,
891 ud_address_vector.g, av->gid_present );
892 MLX_FILL_2 ( &wqe->ud, 2,
893 ud_address_vector.max_stat_rate,
894 ( ( av->rate >= 3 ) ? 0 : 1 ),
895 ud_address_vector.msg, 3 );
896 MLX_FILL_1 ( &wqe->ud, 3, ud_address_vector.sl, av->sl );
897 gid = ( av->gid_present ? &av->gid : &arbel_no_gid );
898 memcpy ( &wqe->ud.u.dwords[4], gid, sizeof ( *gid ) );
899 MLX_FILL_1 ( &wqe->ud, 8, destination_qp, av->dest_qp );
900 MLX_FILL_1 ( &wqe->ud, 9, q_key, av->qkey );
901 MLX_FILL_1 ( &wqe->data[0], 0, byte_count, iob_len ( iobuf ) );
902 MLX_FILL_1 ( &wqe->data[0], 3,
903 local_address_l, virt_to_bus ( iobuf->data ) );
905 /* Update previous work queue entry's "next" field */
906 nds = ( ( offsetof ( typeof ( *wqe ), data ) +
907 sizeof ( wqe->data[0] ) ) >> 4 );
908 MLX_SET ( &prev_wqe->next, nopcode, ARBEL_OPCODE_SEND );
909 MLX_FILL_3 ( &prev_wqe->next, 1,
914 /* Update doorbell record */
916 qp_db_rec = &arbel->db_rec[arbel_send_wq->doorbell_idx].qp;
917 MLX_FILL_1 ( qp_db_rec, 0,
918 counter, ( ( wq->next_idx + 1 ) & 0xffff ) );
920 /* Ring doorbell register */
921 MLX_FILL_4 ( &db_reg.send, 0,
922 nopcode, ARBEL_OPCODE_SEND,
924 wqe_counter, ( wq->next_idx & 0xffff ),
926 MLX_FILL_2 ( &db_reg.send, 1,
929 arbel_ring_doorbell ( arbel, &db_reg, POST_SND_OFFSET );
931 /* Update work queue's index */
938 * Post receive work queue entry
940 * @v ibdev Infiniband device
942 * @v iobuf I/O buffer
943 * @ret rc Return status code
945 static int arbel_post_recv ( struct ib_device *ibdev,
946 struct ib_queue_pair *qp,
947 struct io_buffer *iobuf ) {
948 struct arbel *arbel = ibdev->dev_priv;
949 struct arbel_queue_pair *arbel_qp = qp->dev_priv;
950 struct ib_work_queue *wq = &qp->recv;
951 struct arbel_recv_work_queue *arbel_recv_wq = &arbel_qp->recv;
952 struct arbelprm_recv_wqe *wqe;
953 union arbelprm_doorbell_record *db_rec;
954 unsigned int wqe_idx_mask;
956 /* Allocate work queue entry */
957 wqe_idx_mask = ( wq->num_wqes - 1 );
958 if ( wq->iobufs[wq->next_idx & wqe_idx_mask] ) {
959 DBGC ( arbel, "Arbel %p receive queue full", arbel );
962 wq->iobufs[wq->next_idx & wqe_idx_mask] = iobuf;
963 wqe = &arbel_recv_wq->wqe[wq->next_idx & wqe_idx_mask].recv;
965 /* Construct work queue entry */
966 MLX_FILL_1 ( &wqe->data[0], 0, byte_count, iob_tailroom ( iobuf ) );
967 MLX_FILL_1 ( &wqe->data[0], 1, l_key, arbel->reserved_lkey );
968 MLX_FILL_1 ( &wqe->data[0], 3,
969 local_address_l, virt_to_bus ( iobuf->data ) );
971 /* Update doorbell record */
973 db_rec = &arbel->db_rec[arbel_recv_wq->doorbell_idx];
974 MLX_FILL_1 ( &db_rec->qp, 0,
975 counter, ( ( wq->next_idx + 1 ) & 0xffff ) );
977 /* Update work queue's index */
986 * @v ibdev Infiniband device
987 * @v cq Completion queue
988 * @v cqe Hardware completion queue entry
989 * @v complete_send Send completion handler
990 * @v complete_recv Receive completion handler
991 * @ret rc Return status code
993 static int arbel_complete ( struct ib_device *ibdev,
994 struct ib_completion_queue *cq,
995 union arbelprm_completion_entry *cqe,
996 ib_completer_t complete_send,
997 ib_completer_t complete_recv ) {
998 struct arbel *arbel = ibdev->dev_priv;
999 struct ib_completion completion;
1000 struct ib_work_queue *wq;
1001 struct ib_queue_pair *qp;
1002 struct arbel_queue_pair *arbel_qp;
1003 struct arbel_send_work_queue *arbel_send_wq;
1004 struct arbel_recv_work_queue *arbel_recv_wq;
1005 struct io_buffer *iobuf;
1006 ib_completer_t complete;
1007 unsigned int opcode;
1010 unsigned long wqe_adr;
1011 unsigned int wqe_idx;
1014 /* Parse completion */
1015 memset ( &completion, 0, sizeof ( completion ) );
1016 completion.len = MLX_GET ( &cqe->normal, byte_cnt );
1017 qpn = MLX_GET ( &cqe->normal, my_qpn );
1018 is_send = MLX_GET ( &cqe->normal, s );
1019 wqe_adr = ( MLX_GET ( &cqe->normal, wqe_adr ) << 6 );
1020 opcode = MLX_GET ( &cqe->normal, opcode );
1021 if ( opcode >= ARBEL_OPCODE_RECV_ERROR ) {
1022 /* "s" field is not valid for error opcodes */
1023 is_send = ( opcode == ARBEL_OPCODE_SEND_ERROR );
1024 completion.syndrome = MLX_GET ( &cqe->error, syndrome );
1025 DBGC ( arbel, "Arbel %p CPN %lx syndrome %x vendor %lx\n",
1026 arbel, cq->cqn, completion.syndrome,
1027 MLX_GET ( &cqe->error, vendor_code ) );
1029 /* Don't return immediately; propagate error to completer */
1032 /* Identify work queue */
1033 wq = ib_find_wq ( cq, qpn, is_send );
1035 DBGC ( arbel, "Arbel %p CQN %lx unknown %s QPN %lx\n",
1036 arbel, cq->cqn, ( is_send ? "send" : "recv" ), qpn );
1040 arbel_qp = qp->dev_priv;
1042 /* Identify work queue entry index */
1044 arbel_send_wq = &arbel_qp->send;
1045 wqe_idx = ( ( wqe_adr - virt_to_bus ( arbel_send_wq->wqe ) ) /
1046 sizeof ( arbel_send_wq->wqe[0] ) );
1048 arbel_recv_wq = &arbel_qp->recv;
1049 wqe_idx = ( ( wqe_adr - virt_to_bus ( arbel_recv_wq->wqe ) ) /
1050 sizeof ( arbel_recv_wq->wqe[0] ) );
1053 /* Identify I/O buffer */
1054 iobuf = wq->iobufs[wqe_idx];
1056 DBGC ( arbel, "Arbel %p CQN %lx QPN %lx empty WQE %x\n",
1057 arbel, cq->cqn, qpn, wqe_idx );
1060 wq->iobufs[wqe_idx] = NULL;
1062 /* Pass off to caller's completion handler */
1063 complete = ( is_send ? complete_send : complete_recv );
1064 complete ( ibdev, qp, &completion, iobuf );
1070 * Poll completion queue
1072 * @v ibdev Infiniband device
1073 * @v cq Completion queue
1074 * @v complete_send Send completion handler
1075 * @v complete_recv Receive completion handler
1077 static void arbel_poll_cq ( struct ib_device *ibdev,
1078 struct ib_completion_queue *cq,
1079 ib_completer_t complete_send,
1080 ib_completer_t complete_recv ) {
1081 struct arbel *arbel = ibdev->dev_priv;
1082 struct arbel_completion_queue *arbel_cq = cq->dev_priv;
1083 struct arbelprm_cq_ci_db_record *ci_db_rec;
1084 union arbelprm_completion_entry *cqe;
1085 unsigned int cqe_idx_mask;
1089 /* Look for completion entry */
1090 cqe_idx_mask = ( cq->num_cqes - 1 );
1091 cqe = &arbel_cq->cqe[cq->next_idx & cqe_idx_mask];
1092 if ( MLX_GET ( &cqe->normal, owner ) != 0 ) {
1093 /* Entry still owned by hardware; end of poll */
1097 /* Handle completion */
1098 if ( ( rc = arbel_complete ( ibdev, cq, cqe, complete_send,
1099 complete_recv ) ) != 0 ) {
1100 DBGC ( arbel, "Arbel %p failed to complete: %s\n",
1101 arbel, strerror ( rc ) );
1102 DBGC_HD ( arbel, cqe, sizeof ( *cqe ) );
1105 /* Return ownership to hardware */
1106 MLX_FILL_1 ( &cqe->normal, 7, owner, 1 );
1108 /* Update completion queue's index */
1110 /* Update doorbell record */
1111 ci_db_rec = &arbel->db_rec[arbel_cq->ci_doorbell_idx].cq_ci;
1112 MLX_FILL_1 ( ci_db_rec, 0,
1113 counter, ( cq->next_idx & 0xffffffffUL ) );
1117 /** Arbel Infiniband operations */
1118 static struct ib_device_operations arbel_ib_operations = {
1119 .create_cq = arbel_create_cq,
1120 .destroy_cq = arbel_destroy_cq,
1121 .create_qp = arbel_create_qp,
1122 .destroy_qp = arbel_destroy_qp,
1123 .post_send = arbel_post_send,
1124 .post_recv = arbel_post_recv,
1125 .poll_cq = arbel_poll_cq,
1133 static void arbel_remove ( struct pci_device *pci ) {
1134 struct net_device *netdev = pci_get_drvdata ( pci );
1136 unregister_netdev ( netdev );
1137 ib_driver_close ( 0 );
1138 netdev_nullify ( netdev );
1139 netdev_put ( netdev );
1147 * @ret rc Return status code
1149 static int arbel_probe ( struct pci_device *pci,
1150 const struct pci_device_id *id __unused ) {
1151 struct net_device *netdev;
1152 struct arbelprm_query_dev_lim dev_lim;
1153 struct arbel *arbel = &static_arbel;
1154 struct mlx_nic *mlx;
1159 /* Allocate net device */
1160 netdev = alloc_ibdev ( sizeof ( *mlx ) );
1163 netdev_init ( netdev, &mlx_operations );
1165 pci_set_drvdata ( pci, netdev );
1166 netdev->dev = &pci->dev;
1167 memset ( mlx, 0, sizeof ( *mlx ) );
1169 /* Fix up PCI device */
1170 adjust_pci_device ( pci );
1172 /* Initialise hardware */
1173 if ( ( rc = ib_driver_init ( pci, &qph ) ) != 0 )
1174 goto err_ipoib_init;
1175 mlx->ipoib_qph = qph;
1176 mlx->bcast_av = ib_data.bcast_av;
1177 mlx->snd_cqh = ib_data.ipoib_snd_cq;
1178 mlx->rcv_cqh = ib_data.ipoib_rcv_cq;
1179 mac = ( ( struct ib_mac * ) netdev->ll_addr );
1180 mac->qpn = htonl ( ib_get_qpn ( mlx->ipoib_qph ) );
1181 memcpy ( &mac->gid, ib_data.port_gid.raw, sizeof ( mac->gid ) );
1183 /* Hack up IB structures */
1184 arbel->config = memfree_pci_dev.cr_space;
1185 arbel->mailbox_in = dev_buffers_p->inprm_buf;
1186 arbel->mailbox_out = dev_buffers_p->outprm_buf;
1187 arbel->uar = memfree_pci_dev.uar;
1188 arbel->db_rec = dev_ib_data.uar_context_base;
1189 arbel->reserved_lkey = dev_ib_data.mkey;
1190 arbel->eqn = dev_ib_data.eq.eqn;
1191 static_arbel_ipoib_qp.send.wqe =
1192 ( ( struct udqp_st * ) qph )->snd_wq;
1193 static_arbel_ipoib_qp.recv.wqe =
1194 ( ( struct udqp_st * ) qph )->rcv_wq;
1195 static_arbel_ipoib_send_cq.cqe =
1196 ( ( struct cq_st * ) ib_data.ipoib_snd_cq )->cq_buf;
1197 static_arbel_ipoib_recv_cq.cqe =
1198 ( ( struct cq_st * ) ib_data.ipoib_rcv_cq )->cq_buf;
1199 static_ipoib_qp.qpn = ib_get_qpn ( qph );
1200 static_ipoib_qp.owner_priv = netdev;
1201 list_add ( &static_ipoib_qp.send.list,
1202 &static_ipoib_send_cq.work_queues );
1203 list_add ( &static_ipoib_qp.recv.list,
1204 &static_ipoib_recv_cq.work_queues );
1205 static_ibdev.op = &arbel_ib_operations;
1207 /* Get device limits */
1208 if ( ( rc = arbel_cmd_query_dev_lim ( arbel, &dev_lim ) ) != 0 ) {
1209 DBGC ( arbel, "Arbel %p could not get device limits: %s\n",
1210 arbel, strerror ( rc ) );
1211 goto err_query_dev_lim;
1213 arbel->limits.reserved_uars = MLX_GET ( &dev_lim, num_rsvd_uars );
1214 arbel->limits.reserved_cqs =
1215 ( 1 << MLX_GET ( &dev_lim, log2_rsvd_cqs ) );
1216 arbel->limits.reserved_qps =
1217 ( 1 << MLX_GET ( &dev_lim, log2_rsvd_qps ) );
1218 DBG ( "Device limits:\n ");
1219 DBG_HD ( &dev_lim, sizeof ( dev_lim ) );
1221 /* Register network device */
1222 if ( ( rc = register_netdev ( netdev ) ) != 0 )
1223 goto err_register_netdev;
1228 err_register_netdev:
1230 ib_driver_close ( 0 );
1231 netdev_nullify ( netdev );
1232 netdev_put ( netdev );
1236 static struct pci_device_id arbel_nics[] = {
1237 PCI_ROM ( 0x15b3, 0x6282, "MT25218", "MT25218 HCA driver" ),
1238 PCI_ROM ( 0x15b3, 0x6274, "MT25204", "MT25204 HCA driver" ),
1241 struct pci_driver arbel_driver __pci_driver = {
1243 .id_count = ( sizeof ( arbel_nics ) / sizeof ( arbel_nics[0] ) ),
1244 .probe = arbel_probe,
1245 .remove = arbel_remove,