2 * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 FILE_LICENCE ( GPL2_OR_LATER );
28 #include <gpxe/infiniband.h>
30 #include <gpxe/bitbash.h>
31 #include <gpxe/malloc.h>
32 #include <gpxe/iobuf.h>
38 * QLogic Linda Infiniband HCA
42 /** A Linda send work queue */
43 struct linda_send_work_queue {
44 /** Send buffer usage */
52 /** A Linda receive work queue */
53 struct linda_recv_work_queue {
54 /** Receive header ring */
56 /** Receive header producer offset (written by hardware) */
57 struct QIB_7220_scalar header_prod;
58 /** Receive header consumer offset */
59 unsigned int header_cons;
60 /** Offset within register space of the eager array */
61 unsigned long eager_array;
62 /** Number of entries in eager array */
63 unsigned int eager_entries;
64 /** Eager array producer index */
65 unsigned int eager_prod;
66 /** Eager array consumer index */
67 unsigned int eager_cons;
75 /** In-use contexts */
76 uint8_t used_ctx[LINDA_NUM_CONTEXTS];
77 /** Send work queues */
78 struct linda_send_work_queue send_wq[LINDA_NUM_CONTEXTS];
79 /** Receive work queues */
80 struct linda_recv_work_queue recv_wq[LINDA_NUM_CONTEXTS];
82 /** Offset within register space of the first send buffer */
83 unsigned long send_buffer_base;
84 /** Send buffer availability (reported by hardware) */
85 struct QIB_7220_SendBufAvail *sendbufavail;
86 /** Send buffer availability (maintained by software) */
87 uint8_t send_buf[LINDA_MAX_SEND_BUFS];
88 /** Send buffer availability producer counter */
89 unsigned int send_buf_prod;
90 /** Send buffer availability consumer counter */
91 unsigned int send_buf_cons;
92 /** Number of reserved send buffers (across all QPs) */
93 unsigned int reserved_send_bufs;
95 /** I2C bit-bashing interface */
96 struct i2c_bit_basher i2c;
97 /** I2C serial EEPROM */
98 struct i2c_device eeprom;
101 /***************************************************************************
103 * Linda register access
105 ***************************************************************************
107 * This card requires atomic 64-bit accesses. Strange things happen
108 * if you try to use 32-bit accesses; sometimes they work, sometimes
109 * they don't, sometimes you get random data.
111 * These accessors use the "movq" MMX instruction, and so won't work
112 * on really old Pentiums (which won't have PCIe anyway, so this is
113 * something of a moot point).
117 * Read Linda qword register
119 * @v linda Linda device
120 * @v dwords Register buffer to read into
121 * @v offset Register offset
123 static void linda_readq ( struct linda *linda, uint32_t *dwords,
124 unsigned long offset ) {
125 void *addr = ( linda->regs + offset );
127 __asm__ __volatile__ ( "movq (%1), %%mm0\n\t"
128 "movq %%mm0, (%0)\n\t"
129 : : "r" ( dwords ), "r" ( addr ) : "memory" );
131 DBGIO ( "[%08lx] => %08x%08x\n",
132 virt_to_phys ( addr ), dwords[1], dwords[0] );
134 #define linda_readq( _linda, _ptr, _offset ) \
135 linda_readq ( (_linda), (_ptr)->u.dwords, (_offset) )
136 #define linda_readq_array8b( _linda, _ptr, _offset, _idx ) \
137 linda_readq ( (_linda), (_ptr), ( (_offset) + ( (_idx) * 8 ) ) )
138 #define linda_readq_array64k( _linda, _ptr, _offset, _idx ) \
139 linda_readq ( (_linda), (_ptr), ( (_offset) + ( (_idx) * 65536 ) ) )
142 * Write Linda qword register
144 * @v linda Linda device
145 * @v dwords Register buffer to write
146 * @v offset Register offset
148 static void linda_writeq ( struct linda *linda, const uint32_t *dwords,
149 unsigned long offset ) {
150 void *addr = ( linda->regs + offset );
152 DBGIO ( "[%08lx] <= %08x%08x\n",
153 virt_to_phys ( addr ), dwords[1], dwords[0] );
155 __asm__ __volatile__ ( "movq (%0), %%mm0\n\t"
156 "movq %%mm0, (%1)\n\t"
157 : : "r" ( dwords ), "r" ( addr ) : "memory" );
159 #define linda_writeq( _linda, _ptr, _offset ) \
160 linda_writeq ( (_linda), (_ptr)->u.dwords, (_offset) )
161 #define linda_writeq_array8b( _linda, _ptr, _offset, _idx ) \
162 linda_writeq ( (_linda), (_ptr), ( (_offset) + ( (_idx) * 8 ) ) )
163 #define linda_writeq_array64k( _linda, _ptr, _offset, _idx ) \
164 linda_writeq ( (_linda), (_ptr), ( (_offset) + ( (_idx) * 65536 ) ) )
167 * Write Linda dword register
169 * @v linda Linda device
170 * @v dword Value to write
171 * @v offset Register offset
173 static void linda_writel ( struct linda *linda, uint32_t dword,
174 unsigned long offset ) {
175 writel ( dword, ( linda->regs + offset ) );
178 /***************************************************************************
180 * Link state management
182 ***************************************************************************
186 * Textual representation of link state
188 * @v link_state Link state
189 * @ret link_text Link state text
191 static const char * linda_link_state_text ( unsigned int link_state ) {
192 switch ( link_state ) {
193 case LINDA_LINK_STATE_DOWN: return "DOWN";
194 case LINDA_LINK_STATE_INIT: return "INIT";
195 case LINDA_LINK_STATE_ARM: return "ARM";
196 case LINDA_LINK_STATE_ACTIVE: return "ACTIVE";
197 case LINDA_LINK_STATE_ACT_DEFER:return "ACT_DEFER";
198 default: return "UNKNOWN";
203 * Handle link state change
205 * @v linda Linda device
207 static void linda_link_state_changed ( struct ib_device *ibdev ) {
208 struct linda *linda = ib_get_drvdata ( ibdev );
209 struct QIB_7220_IBCStatus ibcstatus;
210 struct QIB_7220_EXTCtrl extctrl;
211 unsigned int link_state;
212 unsigned int link_width;
213 unsigned int link_speed;
215 /* Read link state */
216 linda_readq ( linda, &ibcstatus, QIB_7220_IBCStatus_offset );
217 link_state = BIT_GET ( &ibcstatus, LinkState );
218 link_width = BIT_GET ( &ibcstatus, LinkWidthActive );
219 link_speed = BIT_GET ( &ibcstatus, LinkSpeedActive );
220 DBGC ( linda, "Linda %p link state %s (%s %s)\n", linda,
221 linda_link_state_text ( link_state ),
222 ( link_speed ? "DDR" : "SDR" ), ( link_width ? "x4" : "x1" ) );
224 /* Set LEDs according to link state */
225 linda_readq ( linda, &extctrl, QIB_7220_EXTCtrl_offset );
226 BIT_SET ( &extctrl, LEDPriPortGreenOn,
227 ( ( link_state >= LINDA_LINK_STATE_INIT ) ? 1 : 0 ) );
228 BIT_SET ( &extctrl, LEDPriPortYellowOn,
229 ( ( link_state >= LINDA_LINK_STATE_ACTIVE ) ? 1 : 0 ) );
230 linda_writeq ( linda, &extctrl, QIB_7220_EXTCtrl_offset );
232 /* Notify Infiniband core of link state change */
233 ibdev->port_state = ( link_state + 1 );
234 ibdev->link_width_active =
235 ( link_width ? IB_LINK_WIDTH_4X : IB_LINK_WIDTH_1X );
236 ibdev->link_speed_active =
237 ( link_speed ? IB_LINK_SPEED_DDR : IB_LINK_SPEED_SDR );
238 ib_link_state_changed ( ibdev );
242 * Set port information
244 * @v ibdev Infiniband device
245 * @v port_info New port information
247 static int linda_set_port_info ( struct ib_device *ibdev,
248 const struct ib_port_info *port_info ) {
249 struct linda *linda = ib_get_drvdata ( ibdev );
250 struct QIB_7220_IBCCtrl ibcctrl;
251 unsigned int port_state;
252 unsigned int link_state;
254 /* Set new link state */
255 port_state = ( port_info->link_speed_supported__port_state & 0xf );
257 link_state = ( port_state - 1 );
258 DBGC ( linda, "Linda %p set link state to %s (%x)\n", linda,
259 linda_link_state_text ( link_state ), link_state );
260 linda_readq ( linda, &ibcctrl, QIB_7220_IBCCtrl_offset );
261 BIT_SET ( &ibcctrl, LinkCmd, link_state );
262 linda_writeq ( linda, &ibcctrl, QIB_7220_IBCCtrl_offset );
265 /* Detect and report link state change */
266 linda_link_state_changed ( ibdev );
271 /***************************************************************************
275 ***************************************************************************
279 * Map context number to QPN
281 * @v ctx Context index
282 * @ret qpn Queue pair number
284 static int linda_ctx_to_qpn ( unsigned int ctx ) {
285 /* This mapping is fixed by hardware */
290 * Map QPN to context number
292 * @v qpn Queue pair number
293 * @ret ctx Context index
295 static int linda_qpn_to_ctx ( unsigned int qpn ) {
296 /* This mapping is fixed by hardware */
303 * @v linda Linda device
304 * @ret ctx Context index, or negative error
306 static int linda_alloc_ctx ( struct linda *linda ) {
309 for ( ctx = 0 ; ctx < LINDA_NUM_CONTEXTS ; ctx++ ) {
311 if ( ! linda->used_ctx[ctx] ) {
312 linda->used_ctx[ctx ] = 1;
313 DBGC2 ( linda, "Linda %p CTX %d allocated\n",
319 DBGC ( linda, "Linda %p out of available contexts\n", linda );
326 * @v linda Linda device
327 * @v ctx Context index
329 static void linda_free_ctx ( struct linda *linda, unsigned int ctx ) {
331 linda->used_ctx[ctx] = 0;
332 DBGC2 ( linda, "Linda %p CTX %d freed\n", linda, ctx );
335 /***************************************************************************
339 ***************************************************************************
342 /** Send buffer toggle bit
344 * We encode send buffers as 7 bits of send buffer index plus a single
345 * bit which should match the "check" bit in the SendBufAvail array.
347 #define LINDA_SEND_BUF_TOGGLE 0x80
350 * Allocate a send buffer
352 * @v linda Linda device
353 * @ret send_buf Send buffer
355 * You must guarantee that a send buffer is available. This is done
356 * by refusing to allocate more TX WQEs in total than the number of
357 * available send buffers.
359 static unsigned int linda_alloc_send_buf ( struct linda *linda ) {
360 unsigned int send_buf;
362 send_buf = linda->send_buf[linda->send_buf_cons];
363 send_buf ^= LINDA_SEND_BUF_TOGGLE;
364 linda->send_buf_cons = ( ( linda->send_buf_cons + 1 ) %
365 LINDA_MAX_SEND_BUFS );
372 * @v linda Linda device
373 * @v send_buf Send buffer
375 static void linda_free_send_buf ( struct linda *linda,
376 unsigned int send_buf ) {
377 linda->send_buf[linda->send_buf_prod] = send_buf;
378 linda->send_buf_prod = ( ( linda->send_buf_prod + 1 ) %
379 LINDA_MAX_SEND_BUFS );
383 * Check to see if send buffer is in use
385 * @v linda Linda device
386 * @v send_buf Send buffer
387 * @ret in_use Send buffer is in use
389 static int linda_send_buf_in_use ( struct linda *linda,
390 unsigned int send_buf ) {
391 unsigned int send_idx;
392 unsigned int send_check;
393 unsigned int inusecheck;
397 send_idx = ( send_buf & ~LINDA_SEND_BUF_TOGGLE );
398 send_check = ( !! ( send_buf & LINDA_SEND_BUF_TOGGLE ) );
399 inusecheck = BIT_GET ( linda->sendbufavail, InUseCheck[send_idx] );
400 inuse = ( !! ( inusecheck & 0x02 ) );
401 check = ( !! ( inusecheck & 0x01 ) );
402 return ( inuse || ( check != send_check ) );
406 * Calculate starting offset for send buffer
408 * @v linda Linda device
409 * @v send_buf Send buffer
410 * @ret offset Starting offset
412 static unsigned long linda_send_buffer_offset ( struct linda *linda,
413 unsigned int send_buf ) {
414 return ( linda->send_buffer_base +
415 ( ( send_buf & ~LINDA_SEND_BUF_TOGGLE ) *
416 LINDA_SEND_BUF_SIZE ) );
420 * Create send work queue
422 * @v linda Linda device
425 static int linda_create_send_wq ( struct linda *linda,
426 struct ib_queue_pair *qp ) {
427 struct ib_work_queue *wq = &qp->send;
428 struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
431 /* Reserve send buffers */
432 if ( ( linda->reserved_send_bufs + qp->send.num_wqes ) >
433 LINDA_MAX_SEND_BUFS ) {
434 DBGC ( linda, "Linda %p out of send buffers (have %d, used "
435 "%d, need %d)\n", linda, LINDA_MAX_SEND_BUFS,
436 linda->reserved_send_bufs, qp->send.num_wqes );
438 goto err_reserve_bufs;
440 linda->reserved_send_bufs += qp->send.num_wqes;
442 /* Reset work queue */
446 /* Allocate space for send buffer uasge list */
447 linda_wq->send_buf = zalloc ( qp->send.num_wqes *
448 sizeof ( linda_wq->send_buf[0] ) );
449 if ( ! linda_wq->send_buf ) {
451 goto err_alloc_send_buf;
456 free ( linda_wq->send_buf );
458 linda->reserved_send_bufs -= qp->send.num_wqes;
464 * Destroy send work queue
466 * @v linda Linda device
469 static void linda_destroy_send_wq ( struct linda *linda,
470 struct ib_queue_pair *qp ) {
471 struct ib_work_queue *wq = &qp->send;
472 struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
474 free ( linda_wq->send_buf );
475 linda->reserved_send_bufs -= qp->send.num_wqes;
479 * Initialise send datapath
481 * @v linda Linda device
482 * @ret rc Return status code
484 static int linda_init_send ( struct linda *linda ) {
485 struct QIB_7220_SendBufBase sendbufbase;
486 struct QIB_7220_SendBufAvailAddr sendbufavailaddr;
487 struct QIB_7220_SendCtrl sendctrl;
491 /* Retrieve SendBufBase */
492 linda_readq ( linda, &sendbufbase, QIB_7220_SendBufBase_offset );
493 linda->send_buffer_base = BIT_GET ( &sendbufbase,
495 DBGC ( linda, "Linda %p send buffers at %lx\n",
496 linda, linda->send_buffer_base );
498 /* Initialise the send_buf[] array */
499 for ( i = 0 ; i < LINDA_MAX_SEND_BUFS ; i++ )
500 linda->send_buf[i] = i;
502 /* Allocate space for the SendBufAvail array */
503 linda->sendbufavail = malloc_dma ( sizeof ( *linda->sendbufavail ),
504 LINDA_SENDBUFAVAIL_ALIGN );
505 if ( ! linda->sendbufavail ) {
507 goto err_alloc_sendbufavail;
509 memset ( linda->sendbufavail, 0, sizeof ( linda->sendbufavail ) );
511 /* Program SendBufAvailAddr into the hardware */
512 memset ( &sendbufavailaddr, 0, sizeof ( sendbufavailaddr ) );
513 BIT_FILL_1 ( &sendbufavailaddr, SendBufAvailAddr,
514 ( virt_to_bus ( linda->sendbufavail ) >> 6 ) );
515 linda_writeq ( linda, &sendbufavailaddr,
516 QIB_7220_SendBufAvailAddr_offset );
518 /* Enable sending and DMA of SendBufAvail */
519 memset ( &sendctrl, 0, sizeof ( sendctrl ) );
520 BIT_FILL_2 ( &sendctrl,
523 linda_writeq ( linda, &sendctrl, QIB_7220_SendCtrl_offset );
527 free_dma ( linda->sendbufavail, sizeof ( *linda->sendbufavail ) );
528 err_alloc_sendbufavail:
533 * Shut down send datapath
535 * @v linda Linda device
537 static void linda_fini_send ( struct linda *linda ) {
538 struct QIB_7220_SendCtrl sendctrl;
540 /* Disable sending and DMA of SendBufAvail */
541 memset ( &sendctrl, 0, sizeof ( sendctrl ) );
542 linda_writeq ( linda, &sendctrl, QIB_7220_SendCtrl_offset );
545 /* Ensure hardware has seen this disable */
546 linda_readq ( linda, &sendctrl, QIB_7220_SendCtrl_offset );
548 free_dma ( linda->sendbufavail, sizeof ( *linda->sendbufavail ) );
551 /***************************************************************************
555 ***************************************************************************
559 * Create receive work queue
561 * @v linda Linda device
563 * @ret rc Return status code
565 static int linda_create_recv_wq ( struct linda *linda,
566 struct ib_queue_pair *qp ) {
567 struct ib_work_queue *wq = &qp->recv;
568 struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
569 struct QIB_7220_RcvHdrAddr0 rcvhdraddr;
570 struct QIB_7220_RcvHdrTailAddr0 rcvhdrtailaddr;
571 struct QIB_7220_RcvHdrHead0 rcvhdrhead;
572 struct QIB_7220_scalar rcvegrindexhead;
573 struct QIB_7220_RcvCtrl rcvctrl;
574 unsigned int ctx = linda_qpn_to_ctx ( qp->qpn );
577 /* Reset context information */
578 memset ( &linda_wq->header_prod, 0,
579 sizeof ( linda_wq->header_prod ) );
580 linda_wq->header_cons = 0;
581 linda_wq->eager_prod = 0;
582 linda_wq->eager_cons = 0;
584 /* Allocate receive header buffer */
585 linda_wq->header = malloc_dma ( LINDA_RECV_HEADERS_SIZE,
586 LINDA_RECV_HEADERS_ALIGN );
587 if ( ! linda_wq->header ) {
589 goto err_alloc_header;
592 /* Enable context in hardware */
593 memset ( &rcvhdraddr, 0, sizeof ( rcvhdraddr ) );
594 BIT_FILL_1 ( &rcvhdraddr, RcvHdrAddr0,
595 ( virt_to_bus ( linda_wq->header ) >> 2 ) );
596 linda_writeq_array8b ( linda, &rcvhdraddr,
597 QIB_7220_RcvHdrAddr0_offset, ctx );
598 memset ( &rcvhdrtailaddr, 0, sizeof ( rcvhdrtailaddr ) );
599 BIT_FILL_1 ( &rcvhdrtailaddr, RcvHdrTailAddr0,
600 ( virt_to_bus ( &linda_wq->header_prod ) >> 2 ) );
601 linda_writeq_array8b ( linda, &rcvhdrtailaddr,
602 QIB_7220_RcvHdrTailAddr0_offset, ctx );
603 memset ( &rcvhdrhead, 0, sizeof ( rcvhdrhead ) );
604 BIT_FILL_1 ( &rcvhdrhead, counter, 1 );
605 linda_writeq_array64k ( linda, &rcvhdrhead,
606 QIB_7220_RcvHdrHead0_offset, ctx );
607 memset ( &rcvegrindexhead, 0, sizeof ( rcvegrindexhead ) );
608 BIT_FILL_1 ( &rcvegrindexhead, Value, 1 );
609 linda_writeq_array64k ( linda, &rcvegrindexhead,
610 QIB_7220_RcvEgrIndexHead0_offset, ctx );
611 linda_readq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
612 BIT_SET ( &rcvctrl, PortEnable[ctx], 1 );
613 BIT_SET ( &rcvctrl, IntrAvail[ctx], 1 );
614 linda_writeq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
616 DBGC ( linda, "Linda %p QPN %ld CTX %d hdrs [%lx,%lx) prod %lx\n",
617 linda, qp->qpn, ctx, virt_to_bus ( linda_wq->header ),
618 ( virt_to_bus ( linda_wq->header ) + LINDA_RECV_HEADERS_SIZE ),
619 virt_to_bus ( &linda_wq->header_prod ) );
622 free_dma ( linda_wq->header, LINDA_RECV_HEADERS_SIZE );
628 * Destroy receive work queue
630 * @v linda Linda device
633 static void linda_destroy_recv_wq ( struct linda *linda,
634 struct ib_queue_pair *qp ) {
635 struct ib_work_queue *wq = &qp->recv;
636 struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
637 struct QIB_7220_RcvCtrl rcvctrl;
638 unsigned int ctx = linda_qpn_to_ctx ( qp->qpn );
640 /* Disable context in hardware */
641 linda_readq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
642 BIT_SET ( &rcvctrl, PortEnable[ctx], 0 );
643 BIT_SET ( &rcvctrl, IntrAvail[ctx], 0 );
644 linda_writeq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
646 /* Make sure the hardware has seen that the context is disabled */
647 linda_readq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
650 /* Free headers ring */
651 free_dma ( linda_wq->header, LINDA_RECV_HEADERS_SIZE );
654 linda_free_ctx ( linda, ctx );
658 * Initialise receive datapath
660 * @v linda Linda device
661 * @ret rc Return status code
663 static int linda_init_recv ( struct linda *linda ) {
664 struct QIB_7220_RcvCtrl rcvctrl;
665 struct QIB_7220_scalar rcvegrbase;
666 struct QIB_7220_scalar rcvhdrentsize;
667 struct QIB_7220_scalar rcvhdrcnt;
668 struct QIB_7220_RcvBTHQP rcvbthqp;
669 unsigned int portcfg;
670 unsigned long egrbase;
671 unsigned int eager_array_size_0;
672 unsigned int eager_array_size_other;
675 /* Select configuration based on number of contexts */
676 switch ( LINDA_NUM_CONTEXTS ) {
678 portcfg = LINDA_PORTCFG_5CTX;
679 eager_array_size_0 = LINDA_EAGER_ARRAY_SIZE_5CTX_0;
680 eager_array_size_other = LINDA_EAGER_ARRAY_SIZE_5CTX_OTHER;
683 portcfg = LINDA_PORTCFG_9CTX;
684 eager_array_size_0 = LINDA_EAGER_ARRAY_SIZE_9CTX_0;
685 eager_array_size_other = LINDA_EAGER_ARRAY_SIZE_9CTX_OTHER;
688 portcfg = LINDA_PORTCFG_17CTX;
689 eager_array_size_0 = LINDA_EAGER_ARRAY_SIZE_17CTX_0;
690 eager_array_size_other = LINDA_EAGER_ARRAY_SIZE_17CTX_OTHER;
693 linker_assert ( 0, invalid_LINDA_NUM_CONTEXTS );
697 /* Configure number of contexts */
698 memset ( &rcvctrl, 0, sizeof ( rcvctrl ) );
699 BIT_FILL_3 ( &rcvctrl,
703 linda_writeq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
705 /* Configure receive header buffer sizes */
706 memset ( &rcvhdrcnt, 0, sizeof ( rcvhdrcnt ) );
707 BIT_FILL_1 ( &rcvhdrcnt, Value, LINDA_RECV_HEADER_COUNT );
708 linda_writeq ( linda, &rcvhdrcnt, QIB_7220_RcvHdrCnt_offset );
709 memset ( &rcvhdrentsize, 0, sizeof ( rcvhdrentsize ) );
710 BIT_FILL_1 ( &rcvhdrentsize, Value, ( LINDA_RECV_HEADER_SIZE >> 2 ) );
711 linda_writeq ( linda, &rcvhdrentsize, QIB_7220_RcvHdrEntSize_offset );
713 /* Calculate eager array start addresses for each context */
714 linda_readq ( linda, &rcvegrbase, QIB_7220_RcvEgrBase_offset );
715 egrbase = BIT_GET ( &rcvegrbase, Value );
716 linda->recv_wq[0].eager_array = egrbase;
717 linda->recv_wq[0].eager_entries = eager_array_size_0;
718 egrbase += ( eager_array_size_0 * sizeof ( struct QIB_7220_RcvEgr ) );
719 for ( ctx = 1 ; ctx < LINDA_NUM_CONTEXTS ; ctx++ ) {
720 linda->recv_wq[ctx].eager_array = egrbase;
721 linda->recv_wq[ctx].eager_entries = eager_array_size_other;
722 egrbase += ( eager_array_size_other *
723 sizeof ( struct QIB_7220_RcvEgr ) );
725 for ( ctx = 0 ; ctx < LINDA_NUM_CONTEXTS ; ctx++ ) {
726 DBGC ( linda, "Linda %p CTX %d eager array at %lx (%d "
727 "entries)\n", linda, ctx,
728 linda->recv_wq[ctx].eager_array,
729 linda->recv_wq[ctx].eager_entries );
732 /* Set the BTH QP for Infinipath packets to an unused value */
733 memset ( &rcvbthqp, 0, sizeof ( rcvbthqp ) );
734 BIT_FILL_1 ( &rcvbthqp, RcvBTHQP, LINDA_QP_IDETH );
735 linda_writeq ( linda, &rcvbthqp, QIB_7220_RcvBTHQP_offset );
741 * Shut down receive datapath
743 * @v linda Linda device
745 static void linda_fini_recv ( struct linda *linda __unused ) {
746 /* Nothing to do; all contexts were already disabled when the
747 * queue pairs were destroyed
751 /***************************************************************************
753 * Completion queue operations
755 ***************************************************************************
759 * Create completion queue
761 * @v ibdev Infiniband device
762 * @v cq Completion queue
763 * @ret rc Return status code
765 static int linda_create_cq ( struct ib_device *ibdev,
766 struct ib_completion_queue *cq ) {
767 struct linda *linda = ib_get_drvdata ( ibdev );
770 /* The hardware has no concept of completion queues. We
771 * simply use the association between CQs and WQs (already
772 * handled by the IB core) to decide which WQs to poll.
774 * We do set a CQN, just to avoid confusing debug messages
778 DBGC ( linda, "Linda %p CQN %ld created\n", linda, cq->cqn );
784 * Destroy completion queue
786 * @v ibdev Infiniband device
787 * @v cq Completion queue
789 static void linda_destroy_cq ( struct ib_device *ibdev,
790 struct ib_completion_queue *cq ) {
791 struct linda *linda = ib_get_drvdata ( ibdev );
794 DBGC ( linda, "Linda %p CQN %ld destroyed\n", linda, cq->cqn );
797 /***************************************************************************
799 * Queue pair operations
801 ***************************************************************************
807 * @v ibdev Infiniband device
809 * @ret rc Return status code
811 static int linda_create_qp ( struct ib_device *ibdev,
812 struct ib_queue_pair *qp ) {
813 struct linda *linda = ib_get_drvdata ( ibdev );
817 /* Locate an available context */
818 ctx = linda_alloc_ctx ( linda );
824 /* Set queue pair number based on context index */
825 qp->qpn = linda_ctx_to_qpn ( ctx );
827 /* Set work-queue private data pointers */
828 ib_wq_set_drvdata ( &qp->send, &linda->send_wq[ctx] );
829 ib_wq_set_drvdata ( &qp->recv, &linda->recv_wq[ctx] );
831 /* Create receive work queue */
832 if ( ( rc = linda_create_recv_wq ( linda, qp ) ) != 0 )
833 goto err_create_recv_wq;
835 /* Create send work queue */
836 if ( ( rc = linda_create_send_wq ( linda, qp ) ) != 0 )
837 goto err_create_send_wq;
841 linda_destroy_send_wq ( linda, qp );
843 linda_destroy_recv_wq ( linda, qp );
845 linda_free_ctx ( linda, ctx );
853 * @v ibdev Infiniband device
855 * @v mod_list Modification list
856 * @ret rc Return status code
858 static int linda_modify_qp ( struct ib_device *ibdev,
859 struct ib_queue_pair *qp,
860 unsigned long mod_list __unused ) {
861 struct linda *linda = ib_get_drvdata ( ibdev );
863 /* Nothing to do; the hardware doesn't have a notion of queue
866 DBGC ( linda, "Linda %p QPN %ld modified\n", linda, qp->qpn );
873 * @v ibdev Infiniband device
876 static void linda_destroy_qp ( struct ib_device *ibdev,
877 struct ib_queue_pair *qp ) {
878 struct linda *linda = ib_get_drvdata ( ibdev );
880 linda_destroy_send_wq ( linda, qp );
881 linda_destroy_recv_wq ( linda, qp );
884 /***************************************************************************
886 * Work request operations
888 ***************************************************************************
892 * Post send work queue entry
894 * @v ibdev Infiniband device
896 * @v av Address vector
897 * @v iobuf I/O buffer
898 * @ret rc Return status code
900 static int linda_post_send ( struct ib_device *ibdev,
901 struct ib_queue_pair *qp,
902 struct ib_address_vector *av,
903 struct io_buffer *iobuf ) {
904 struct linda *linda = ib_get_drvdata ( ibdev );
905 struct ib_work_queue *wq = &qp->send;
906 struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
907 struct QIB_7220_SendPbc sendpbc;
908 uint8_t header_buf[IB_MAX_HEADER_SIZE];
909 struct io_buffer headers;
910 unsigned int send_buf;
911 unsigned long start_offset;
912 unsigned long offset;
917 /* Allocate send buffer and calculate offset */
918 send_buf = linda_alloc_send_buf ( linda );
919 start_offset = offset = linda_send_buffer_offset ( linda, send_buf );
921 /* Store I/O buffer and send buffer index */
922 assert ( wq->iobufs[linda_wq->prod] == NULL );
923 wq->iobufs[linda_wq->prod] = iobuf;
924 linda_wq->send_buf[linda_wq->prod] = send_buf;
926 /* Construct headers */
927 iob_populate ( &headers, header_buf, 0, sizeof ( header_buf ) );
928 iob_reserve ( &headers, sizeof ( header_buf ) );
929 ib_push ( ibdev, &headers, qp, iob_len ( iobuf ), av );
931 /* Calculate packet length */
932 len = ( ( sizeof ( sendpbc ) + iob_len ( &headers ) +
933 iob_len ( iobuf ) + 3 ) & ~3 );
935 /* Construct send per-buffer control word */
936 memset ( &sendpbc, 0, sizeof ( sendpbc ) );
937 BIT_FILL_2 ( &sendpbc,
938 LengthP1_toibc, ( ( len >> 2 ) - 1 ),
942 DBG_DISABLE ( DBGLVL_IO );
943 linda_writeq ( linda, &sendpbc, offset );
944 offset += sizeof ( sendpbc );
947 for ( data = headers.data, frag_len = iob_len ( &headers ) ;
948 frag_len > 0 ; data++, offset += 4, frag_len -= 4 ) {
949 linda_writel ( linda, *data, offset );
953 for ( data = iobuf->data, frag_len = iob_len ( iobuf ) ;
954 frag_len > 0 ; data++, offset += 4, frag_len -= 4 ) {
955 linda_writel ( linda, *data, offset );
957 DBG_ENABLE ( DBGLVL_IO );
959 assert ( ( start_offset + len ) == offset );
960 DBGC2 ( linda, "Linda %p QPN %ld TX %d(%d) posted [%lx,%lx)\n",
961 linda, qp->qpn, send_buf, linda_wq->prod,
962 start_offset, offset );
964 /* Increment producer counter */
965 linda_wq->prod = ( ( linda_wq->prod + 1 ) & ( wq->num_wqes - 1 ) );
971 * Complete send work queue entry
973 * @v ibdev Infiniband device
975 * @v wqe_idx Work queue entry index
977 static void linda_complete_send ( struct ib_device *ibdev,
978 struct ib_queue_pair *qp,
979 unsigned int wqe_idx ) {
980 struct linda *linda = ib_get_drvdata ( ibdev );
981 struct ib_work_queue *wq = &qp->send;
982 struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
983 struct io_buffer *iobuf;
984 unsigned int send_buf;
986 /* Parse completion */
987 send_buf = linda_wq->send_buf[wqe_idx];
988 DBGC2 ( linda, "Linda %p QPN %ld TX %d(%d) complete\n",
989 linda, qp->qpn, send_buf, wqe_idx );
991 /* Complete work queue entry */
992 iobuf = wq->iobufs[wqe_idx];
993 assert ( iobuf != NULL );
994 ib_complete_send ( ibdev, qp, iobuf, 0 );
995 wq->iobufs[wqe_idx] = NULL;
997 /* Free send buffer */
998 linda_free_send_buf ( linda, send_buf );
1002 * Poll send work queue
1004 * @v ibdev Infiniband device
1007 static void linda_poll_send_wq ( struct ib_device *ibdev,
1008 struct ib_queue_pair *qp ) {
1009 struct linda *linda = ib_get_drvdata ( ibdev );
1010 struct ib_work_queue *wq = &qp->send;
1011 struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
1012 unsigned int send_buf;
1014 /* Look for completions */
1015 while ( wq->fill ) {
1017 /* Check to see if send buffer has completed */
1018 send_buf = linda_wq->send_buf[linda_wq->cons];
1019 if ( linda_send_buf_in_use ( linda, send_buf ) )
1022 /* Complete this buffer */
1023 linda_complete_send ( ibdev, qp, linda_wq->cons );
1025 /* Increment consumer counter */
1026 linda_wq->cons = ( ( linda_wq->cons + 1 ) &
1027 ( wq->num_wqes - 1 ) );
1032 * Post receive work queue entry
1034 * @v ibdev Infiniband device
1036 * @v iobuf I/O buffer
1037 * @ret rc Return status code
1039 static int linda_post_recv ( struct ib_device *ibdev,
1040 struct ib_queue_pair *qp,
1041 struct io_buffer *iobuf ) {
1042 struct linda *linda = ib_get_drvdata ( ibdev );
1043 struct ib_work_queue *wq = &qp->recv;
1044 struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
1045 struct QIB_7220_RcvEgr rcvegr;
1046 struct QIB_7220_scalar rcvegrindexhead;
1047 unsigned int ctx = linda_qpn_to_ctx ( qp->qpn );
1050 unsigned int wqe_idx;
1051 unsigned int bufsize;
1054 addr = virt_to_bus ( iobuf->data );
1055 len = iob_tailroom ( iobuf );
1056 if ( addr & ( LINDA_EAGER_BUFFER_ALIGN - 1 ) ) {
1057 DBGC ( linda, "Linda %p QPN %ld misaligned RX buffer "
1058 "(%08lx)\n", linda, qp->qpn, addr );
1061 if ( len != LINDA_RECV_PAYLOAD_SIZE ) {
1062 DBGC ( linda, "Linda %p QPN %ld wrong RX buffer size (%zd)\n",
1063 linda, qp->qpn, len );
1067 /* Calculate eager producer index and WQE index */
1068 wqe_idx = ( linda_wq->eager_prod & ( wq->num_wqes - 1 ) );
1069 assert ( wq->iobufs[wqe_idx] == NULL );
1071 /* Store I/O buffer */
1072 wq->iobufs[wqe_idx] = iobuf;
1074 /* Calculate buffer size */
1075 switch ( LINDA_RECV_PAYLOAD_SIZE ) {
1076 case 2048: bufsize = LINDA_EAGER_BUFFER_2K; break;
1077 case 4096: bufsize = LINDA_EAGER_BUFFER_4K; break;
1078 case 8192: bufsize = LINDA_EAGER_BUFFER_8K; break;
1079 case 16384: bufsize = LINDA_EAGER_BUFFER_16K; break;
1080 case 32768: bufsize = LINDA_EAGER_BUFFER_32K; break;
1081 case 65536: bufsize = LINDA_EAGER_BUFFER_64K; break;
1082 default: linker_assert ( 0, invalid_rx_payload_size );
1083 bufsize = LINDA_EAGER_BUFFER_NONE;
1086 /* Post eager buffer */
1087 memset ( &rcvegr, 0, sizeof ( rcvegr ) );
1088 BIT_FILL_2 ( &rcvegr,
1089 Addr, ( addr >> 11 ),
1091 linda_writeq_array8b ( linda, &rcvegr,
1092 linda_wq->eager_array, linda_wq->eager_prod );
1093 DBGC2 ( linda, "Linda %p QPN %ld RX egr %d(%d) posted [%lx,%lx)\n",
1094 linda, qp->qpn, linda_wq->eager_prod, wqe_idx,
1095 addr, ( addr + len ) );
1097 /* Increment producer index */
1098 linda_wq->eager_prod = ( ( linda_wq->eager_prod + 1 ) &
1099 ( linda_wq->eager_entries - 1 ) );
1101 /* Update head index */
1102 memset ( &rcvegrindexhead, 0, sizeof ( rcvegrindexhead ) );
1103 BIT_FILL_1 ( &rcvegrindexhead,
1104 Value, ( ( linda_wq->eager_prod + 1 ) &
1105 ( linda_wq->eager_entries - 1 ) ) );
1106 linda_writeq_array64k ( linda, &rcvegrindexhead,
1107 QIB_7220_RcvEgrIndexHead0_offset, ctx );
1113 * Complete receive work queue entry
1115 * @v ibdev Infiniband device
1117 * @v header_offs Header offset
1119 static void linda_complete_recv ( struct ib_device *ibdev,
1120 struct ib_queue_pair *qp,
1121 unsigned int header_offs ) {
1122 struct linda *linda = ib_get_drvdata ( ibdev );
1123 struct ib_work_queue *wq = &qp->recv;
1124 struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
1125 struct QIB_7220_RcvHdrFlags *rcvhdrflags;
1126 struct QIB_7220_RcvEgr rcvegr;
1127 struct io_buffer headers;
1128 struct io_buffer *iobuf;
1129 struct ib_queue_pair *intended_qp;
1130 struct ib_address_vector av;
1131 unsigned int rcvtype;
1132 unsigned int pktlen;
1133 unsigned int egrindex;
1134 unsigned int useegrbfr;
1135 unsigned int iberr, mkerr, tiderr, khdrerr, mtuerr;
1136 unsigned int lenerr, parityerr, vcrcerr, icrcerr;
1138 unsigned int hdrqoffset;
1139 unsigned int header_len;
1140 unsigned int padded_payload_len;
1141 unsigned int wqe_idx;
1146 /* RcvHdrFlags are at the end of the header entry */
1147 rcvhdrflags = ( linda_wq->header + header_offs +
1148 LINDA_RECV_HEADER_SIZE - sizeof ( *rcvhdrflags ) );
1149 rcvtype = BIT_GET ( rcvhdrflags, RcvType );
1150 pktlen = ( BIT_GET ( rcvhdrflags, PktLen ) << 2 );
1151 egrindex = BIT_GET ( rcvhdrflags, EgrIndex );
1152 useegrbfr = BIT_GET ( rcvhdrflags, UseEgrBfr );
1153 hdrqoffset = ( BIT_GET ( rcvhdrflags, HdrqOffset ) << 2 );
1154 iberr = BIT_GET ( rcvhdrflags, IBErr );
1155 mkerr = BIT_GET ( rcvhdrflags, MKErr );
1156 tiderr = BIT_GET ( rcvhdrflags, TIDErr );
1157 khdrerr = BIT_GET ( rcvhdrflags, KHdrErr );
1158 mtuerr = BIT_GET ( rcvhdrflags, MTUErr );
1159 lenerr = BIT_GET ( rcvhdrflags, LenErr );
1160 parityerr = BIT_GET ( rcvhdrflags, ParityErr );
1161 vcrcerr = BIT_GET ( rcvhdrflags, VCRCErr );
1162 icrcerr = BIT_GET ( rcvhdrflags, ICRCErr );
1163 header_len = ( LINDA_RECV_HEADER_SIZE - hdrqoffset -
1164 sizeof ( *rcvhdrflags ) );
1165 padded_payload_len = ( pktlen - header_len - 4 /* ICRC */ );
1166 err = ( iberr | mkerr | tiderr | khdrerr | mtuerr |
1167 lenerr | parityerr | vcrcerr | icrcerr );
1168 /* IB header is placed immediately before RcvHdrFlags */
1169 iob_populate ( &headers, ( ( ( void * ) rcvhdrflags ) - header_len ),
1170 header_len, header_len );
1172 /* Dump diagnostic information */
1173 if ( err || ( ! useegrbfr ) ) {
1174 DBGC ( linda, "Linda %p QPN %ld RX egr %d%s hdr %d type %d "
1175 "len %d(%d+%d+4)%s%s%s%s%s%s%s%s%s%s%s\n", linda,
1176 qp->qpn, egrindex, ( useegrbfr ? "" : "(unused)" ),
1177 ( header_offs / LINDA_RECV_HEADER_SIZE ), rcvtype,
1178 pktlen, header_len, padded_payload_len,
1179 ( err ? " [Err" : "" ), ( iberr ? " IB" : "" ),
1180 ( mkerr ? " MK" : "" ), ( tiderr ? " TID" : "" ),
1181 ( khdrerr ? " KHdr" : "" ), ( mtuerr ? " MTU" : "" ),
1182 ( lenerr ? " Len" : "" ), ( parityerr ? " Parity" : ""),
1183 ( vcrcerr ? " VCRC" : "" ), ( icrcerr ? " ICRC" : "" ),
1184 ( err ? "]" : "" ) );
1186 DBGC2 ( linda, "Linda %p QPN %ld RX egr %d hdr %d type %d "
1187 "len %d(%d+%d+4)\n", linda, qp->qpn, egrindex,
1188 ( header_offs / LINDA_RECV_HEADER_SIZE ), rcvtype,
1189 pktlen, header_len, padded_payload_len );
1191 DBGCP_HDA ( linda, hdrqoffset, headers.data,
1192 ( header_len + sizeof ( *rcvhdrflags ) ) );
1194 /* Parse header to generate address vector */
1195 qp0 = ( qp->qpn == 0 );
1197 if ( ( rc = ib_pull ( ibdev, &headers, ( qp0 ? &intended_qp : NULL ),
1198 &payload_len, &av ) ) != 0 ) {
1199 DBGC ( linda, "Linda %p could not parse headers: %s\n",
1200 linda, strerror ( rc ) );
1203 if ( ! intended_qp )
1206 /* Complete this buffer and any skipped buffers. Note that
1207 * when the hardware runs out of buffers, it will repeatedly
1208 * report the same buffer (the tail) as a TID error, and that
1209 * it also has a habit of sometimes skipping over several
1214 /* If we have caught up to the producer counter, stop.
1215 * This will happen when the hardware first runs out
1216 * of buffers and starts reporting TID errors against
1217 * the eager buffer it wants to use next.
1219 if ( linda_wq->eager_cons == linda_wq->eager_prod )
1222 /* If we have caught up to where we should be after
1223 * completing this egrindex, stop. We phrase the test
1224 * this way to avoid completing the entire ring when
1225 * we receive the same egrindex twice in a row.
1227 if ( ( linda_wq->eager_cons ==
1228 ( ( egrindex + 1 ) & ( linda_wq->eager_entries - 1 ) )))
1231 /* Identify work queue entry and corresponding I/O
1234 wqe_idx = ( linda_wq->eager_cons & ( wq->num_wqes - 1 ) );
1235 iobuf = wq->iobufs[wqe_idx];
1236 assert ( iobuf != NULL );
1237 wq->iobufs[wqe_idx] = NULL;
1239 /* Complete the eager buffer */
1240 if ( linda_wq->eager_cons == egrindex ) {
1241 /* Completing the eager buffer described in
1242 * this header entry.
1244 iob_put ( iobuf, payload_len );
1245 rc = ( err ? -EIO : ( useegrbfr ? 0 : -ECANCELED ) );
1246 /* Redirect to target QP if necessary */
1247 if ( qp != intended_qp ) {
1248 DBGC ( linda, "Linda %p redirecting QPN %ld "
1250 linda, qp->qpn, intended_qp->qpn );
1251 /* Compensate for incorrect fill levels */
1253 intended_qp->recv.fill++;
1255 ib_complete_recv ( ibdev, intended_qp, &av, iobuf, rc);
1257 /* Completing on a skipped-over eager buffer */
1258 ib_complete_recv ( ibdev, qp, &av, iobuf, -ECANCELED );
1261 /* Clear eager buffer */
1262 memset ( &rcvegr, 0, sizeof ( rcvegr ) );
1263 linda_writeq_array8b ( linda, &rcvegr, linda_wq->eager_array,
1264 linda_wq->eager_cons );
1266 /* Increment consumer index */
1267 linda_wq->eager_cons = ( ( linda_wq->eager_cons + 1 ) &
1268 ( linda_wq->eager_entries - 1 ) );
1273 * Poll receive work queue
1275 * @v ibdev Infiniband device
1278 static void linda_poll_recv_wq ( struct ib_device *ibdev,
1279 struct ib_queue_pair *qp ) {
1280 struct linda *linda = ib_get_drvdata ( ibdev );
1281 struct ib_work_queue *wq = &qp->recv;
1282 struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
1283 struct QIB_7220_RcvHdrHead0 rcvhdrhead;
1284 unsigned int ctx = linda_qpn_to_ctx ( qp->qpn );
1285 unsigned int header_prod;
1287 /* Check for received packets */
1288 header_prod = ( BIT_GET ( &linda_wq->header_prod, Value ) << 2 );
1289 if ( header_prod == linda_wq->header_cons )
1292 /* Process all received packets */
1293 while ( linda_wq->header_cons != header_prod ) {
1295 /* Complete the receive */
1296 linda_complete_recv ( ibdev, qp, linda_wq->header_cons );
1298 /* Increment the consumer offset */
1299 linda_wq->header_cons += LINDA_RECV_HEADER_SIZE;
1300 linda_wq->header_cons %= LINDA_RECV_HEADERS_SIZE;
1303 /* Update consumer offset */
1304 memset ( &rcvhdrhead, 0, sizeof ( rcvhdrhead ) );
1305 BIT_FILL_2 ( &rcvhdrhead,
1306 RcvHeadPointer, ( linda_wq->header_cons >> 2 ),
1308 linda_writeq_array64k ( linda, &rcvhdrhead,
1309 QIB_7220_RcvHdrHead0_offset, ctx );
1313 * Poll completion queue
1315 * @v ibdev Infiniband device
1316 * @v cq Completion queue
1318 static void linda_poll_cq ( struct ib_device *ibdev,
1319 struct ib_completion_queue *cq ) {
1320 struct ib_work_queue *wq;
1322 /* Poll associated send and receive queues */
1323 list_for_each_entry ( wq, &cq->work_queues, list ) {
1324 if ( wq->is_send ) {
1325 linda_poll_send_wq ( ibdev, wq->qp );
1327 linda_poll_recv_wq ( ibdev, wq->qp );
1332 /***************************************************************************
1336 ***************************************************************************
1342 * @v ibdev Infiniband device
1344 static void linda_poll_eq ( struct ib_device *ibdev ) {
1345 struct linda *linda = ib_get_drvdata ( ibdev );
1346 struct QIB_7220_ErrStatus errstatus;
1347 struct QIB_7220_ErrClear errclear;
1349 /* Check for link status changes */
1350 DBG_DISABLE ( DBGLVL_IO );
1351 linda_readq ( linda, &errstatus, QIB_7220_ErrStatus_offset );
1352 DBG_ENABLE ( DBGLVL_IO );
1353 if ( BIT_GET ( &errstatus, IBStatusChanged ) ) {
1354 linda_link_state_changed ( ibdev );
1355 memset ( &errclear, 0, sizeof ( errclear ) );
1356 BIT_FILL_1 ( &errclear, IBStatusChangedClear, 1 );
1357 linda_writeq ( linda, &errclear, QIB_7220_ErrClear_offset );
1361 /***************************************************************************
1363 * Infiniband link-layer operations
1365 ***************************************************************************
1369 * Initialise Infiniband link
1371 * @v ibdev Infiniband device
1372 * @ret rc Return status code
1374 static int linda_open ( struct ib_device *ibdev ) {
1375 struct linda *linda = ib_get_drvdata ( ibdev );
1376 struct QIB_7220_Control control;
1379 linda_readq ( linda, &control, QIB_7220_Control_offset );
1380 BIT_SET ( &control, LinkEn, 1 );
1381 linda_writeq ( linda, &control, QIB_7220_Control_offset );
1386 * Close Infiniband link
1388 * @v ibdev Infiniband device
1390 static void linda_close ( struct ib_device *ibdev ) {
1391 struct linda *linda = ib_get_drvdata ( ibdev );
1392 struct QIB_7220_Control control;
1395 linda_readq ( linda, &control, QIB_7220_Control_offset );
1396 BIT_SET ( &control, LinkEn, 0 );
1397 linda_writeq ( linda, &control, QIB_7220_Control_offset );
1400 /***************************************************************************
1402 * Multicast group operations
1404 ***************************************************************************
1408 * Attach to multicast group
1410 * @v ibdev Infiniband device
1412 * @v gid Multicast GID
1413 * @ret rc Return status code
1415 static int linda_mcast_attach ( struct ib_device *ibdev,
1416 struct ib_queue_pair *qp,
1417 struct ib_gid *gid ) {
1418 struct linda *linda = ib_get_drvdata ( ibdev );
1427 * Detach from multicast group
1429 * @v ibdev Infiniband device
1431 * @v gid Multicast GID
1433 static void linda_mcast_detach ( struct ib_device *ibdev,
1434 struct ib_queue_pair *qp,
1435 struct ib_gid *gid ) {
1436 struct linda *linda = ib_get_drvdata ( ibdev );
1443 /** Linda Infiniband operations */
1444 static struct ib_device_operations linda_ib_operations = {
1445 .create_cq = linda_create_cq,
1446 .destroy_cq = linda_destroy_cq,
1447 .create_qp = linda_create_qp,
1448 .modify_qp = linda_modify_qp,
1449 .destroy_qp = linda_destroy_qp,
1450 .post_send = linda_post_send,
1451 .post_recv = linda_post_recv,
1452 .poll_cq = linda_poll_cq,
1453 .poll_eq = linda_poll_eq,
1455 .close = linda_close,
1456 .mcast_attach = linda_mcast_attach,
1457 .mcast_detach = linda_mcast_detach,
1458 .set_port_info = linda_set_port_info,
1461 /***************************************************************************
1463 * I2C bus operations
1465 ***************************************************************************
1468 /** Linda I2C bit to GPIO mappings */
1469 static unsigned int linda_i2c_bits[] = {
1470 [I2C_BIT_SCL] = ( 1 << LINDA_GPIO_SCL ),
1471 [I2C_BIT_SDA] = ( 1 << LINDA_GPIO_SDA ),
1475 * Read Linda I2C line status
1477 * @v basher Bit-bashing interface
1478 * @v bit_id Bit number
1479 * @ret zero Input is a logic 0
1480 * @ret non-zero Input is a logic 1
1482 static int linda_i2c_read_bit ( struct bit_basher *basher,
1483 unsigned int bit_id ) {
1484 struct linda *linda =
1485 container_of ( basher, struct linda, i2c.basher );
1486 struct QIB_7220_EXTStatus extstatus;
1487 unsigned int status;
1489 DBG_DISABLE ( DBGLVL_IO );
1491 linda_readq ( linda, &extstatus, QIB_7220_EXTStatus_offset );
1492 status = ( BIT_GET ( &extstatus, GPIOIn ) & linda_i2c_bits[bit_id] );
1494 DBG_ENABLE ( DBGLVL_IO );
1500 * Write Linda I2C line status
1502 * @v basher Bit-bashing interface
1503 * @v bit_id Bit number
1504 * @v data Value to write
1506 static void linda_i2c_write_bit ( struct bit_basher *basher,
1507 unsigned int bit_id, unsigned long data ) {
1508 struct linda *linda =
1509 container_of ( basher, struct linda, i2c.basher );
1510 struct QIB_7220_EXTCtrl extctrl;
1511 struct QIB_7220_GPIO gpioout;
1512 unsigned int bit = linda_i2c_bits[bit_id];
1513 unsigned int outputs = 0;
1514 unsigned int output_enables = 0;
1516 DBG_DISABLE ( DBGLVL_IO );
1518 /* Read current GPIO mask and outputs */
1519 linda_readq ( linda, &extctrl, QIB_7220_EXTCtrl_offset );
1520 linda_readq ( linda, &gpioout, QIB_7220_GPIOOut_offset );
1522 /* Update outputs and output enables. I2C lines are tied
1523 * high, so we always set the output to 0 and use the output
1524 * enable to control the line.
1526 output_enables = BIT_GET ( &extctrl, GPIOOe );
1527 output_enables = ( ( output_enables & ~bit ) | ( ~data & bit ) );
1528 outputs = BIT_GET ( &gpioout, GPIO );
1529 outputs = ( outputs & ~bit );
1530 BIT_SET ( &extctrl, GPIOOe, output_enables );
1531 BIT_SET ( &gpioout, GPIO, outputs );
1533 /* Write the output enable first; that way we avoid logic
1536 linda_writeq ( linda, &extctrl, QIB_7220_EXTCtrl_offset );
1537 linda_writeq ( linda, &gpioout, QIB_7220_GPIOOut_offset );
1540 DBG_ENABLE ( DBGLVL_IO );
1543 /** Linda I2C bit-bashing interface operations */
1544 static struct bit_basher_operations linda_i2c_basher_ops = {
1545 .read = linda_i2c_read_bit,
1546 .write = linda_i2c_write_bit,
1550 * Initialise Linda I2C subsystem
1552 * @v linda Linda device
1553 * @ret rc Return status code
1555 static int linda_init_i2c ( struct linda *linda ) {
1556 static int try_eeprom_address[] = { 0x51, 0x50 };
1560 /* Initialise bus */
1561 if ( ( rc = init_i2c_bit_basher ( &linda->i2c,
1562 &linda_i2c_basher_ops ) ) != 0 ) {
1563 DBGC ( linda, "Linda %p could not initialise I2C bus: %s\n",
1564 linda, strerror ( rc ) );
1568 /* Probe for devices */
1569 for ( i = 0 ; i < ( sizeof ( try_eeprom_address ) /
1570 sizeof ( try_eeprom_address[0] ) ) ; i++ ) {
1571 init_i2c_eeprom ( &linda->eeprom, try_eeprom_address[i] );
1572 if ( ( rc = i2c_check_presence ( &linda->i2c.i2c,
1573 &linda->eeprom ) ) == 0 ) {
1574 DBGC2 ( linda, "Linda %p found EEPROM at %02x\n",
1575 linda, try_eeprom_address[i] );
1580 DBGC ( linda, "Linda %p could not find EEPROM\n", linda );
1585 * Read EEPROM parameters
1587 * @v linda Linda device
1588 * @v guid GUID to fill in
1589 * @ret rc Return status code
1591 static int linda_read_eeprom ( struct linda *linda,
1592 struct ib_gid_half *guid ) {
1593 struct i2c_interface *i2c = &linda->i2c.i2c;
1597 if ( ( rc = i2c->read ( i2c, &linda->eeprom, LINDA_EEPROM_GUID_OFFSET,
1598 guid->bytes, sizeof ( *guid ) ) ) != 0 ) {
1599 DBGC ( linda, "Linda %p could not read GUID: %s\n",
1600 linda, strerror ( rc ) );
1603 DBGC2 ( linda, "Linda %p has GUID %02x:%02x:%02x:%02x:%02x:%02x:"
1604 "%02x:%02x\n", linda, guid->bytes[0], guid->bytes[1],
1605 guid->bytes[2], guid->bytes[3], guid->bytes[4],
1606 guid->bytes[5], guid->bytes[6], guid->bytes[7] );
1608 /* Read serial number (debug only) */
1610 uint8_t serial[LINDA_EEPROM_SERIAL_SIZE + 1];
1612 serial[ sizeof ( serial ) - 1 ] = '\0';
1613 if ( ( rc = i2c->read ( i2c, &linda->eeprom,
1614 LINDA_EEPROM_SERIAL_OFFSET, serial,
1615 ( sizeof ( serial ) - 1 ) ) ) != 0 ) {
1616 DBGC ( linda, "Linda %p could not read serial: %s\n",
1617 linda, strerror ( rc ) );
1620 DBGC2 ( linda, "Linda %p has serial number \"%s\"\n",
1627 /***************************************************************************
1629 * External parallel bus access
1631 ***************************************************************************
1635 * Request ownership of the IB external parallel bus
1637 * @v linda Linda device
1638 * @ret rc Return status code
1640 static int linda_ib_epb_request ( struct linda *linda ) {
1641 struct QIB_7220_ibsd_epb_access_ctrl access;
1644 /* Request ownership */
1645 memset ( &access, 0, sizeof ( access ) );
1646 BIT_FILL_1 ( &access, sw_ib_epb_req, 1 );
1647 linda_writeq ( linda, &access, QIB_7220_ibsd_epb_access_ctrl_offset );
1649 /* Wait for ownership to be granted */
1650 for ( i = 0 ; i < LINDA_EPB_REQUEST_MAX_WAIT_US ; i++ ) {
1651 linda_readq ( linda, &access,
1652 QIB_7220_ibsd_epb_access_ctrl_offset );
1653 if ( BIT_GET ( &access, sw_ib_epb_req_granted ) )
1658 DBGC ( linda, "Linda %p timed out waiting for IB EPB request\n",
1664 * Wait for IB external parallel bus transaction to complete
1666 * @v linda Linda device
1667 * @v xact Buffer to hold transaction result
1668 * @ret rc Return status code
1670 static int linda_ib_epb_wait ( struct linda *linda,
1671 struct QIB_7220_ibsd_epb_transaction_reg *xact ) {
1674 /* Discard first read to allow for signals crossing clock domains */
1675 linda_readq ( linda, xact, QIB_7220_ibsd_epb_transaction_reg_offset );
1677 for ( i = 0 ; i < LINDA_EPB_XACT_MAX_WAIT_US ; i++ ) {
1678 linda_readq ( linda, xact,
1679 QIB_7220_ibsd_epb_transaction_reg_offset );
1680 if ( BIT_GET ( xact, ib_epb_rdy ) ) {
1681 if ( BIT_GET ( xact, ib_epb_req_error ) ) {
1682 DBGC ( linda, "Linda %p EPB transaction "
1683 "failed\n", linda );
1692 DBGC ( linda, "Linda %p timed out waiting for IB EPB transaction\n",
1698 * Release ownership of the IB external parallel bus
1700 * @v linda Linda device
1702 static void linda_ib_epb_release ( struct linda *linda ) {
1703 struct QIB_7220_ibsd_epb_access_ctrl access;
1705 memset ( &access, 0, sizeof ( access ) );
1706 BIT_FILL_1 ( &access, sw_ib_epb_req, 0 );
1707 linda_writeq ( linda, &access, QIB_7220_ibsd_epb_access_ctrl_offset );
1711 * Read data via IB external parallel bus
1713 * @v linda Linda device
1714 * @v location EPB location
1715 * @ret data Data read, or negative error
1717 * You must have already acquired ownership of the IB external
1720 static int linda_ib_epb_read ( struct linda *linda, unsigned int location ) {
1721 struct QIB_7220_ibsd_epb_transaction_reg xact;
1725 /* Ensure no transaction is currently in progress */
1726 if ( ( rc = linda_ib_epb_wait ( linda, &xact ) ) != 0 )
1730 memset ( &xact, 0, sizeof ( xact ) );
1732 ib_epb_address, LINDA_EPB_LOC_ADDRESS ( location ),
1733 ib_epb_read_write, LINDA_EPB_READ,
1734 ib_epb_cs, LINDA_EPB_LOC_CS ( location ) );
1735 linda_writeq ( linda, &xact,
1736 QIB_7220_ibsd_epb_transaction_reg_offset );
1738 /* Wait for transaction to complete */
1739 if ( ( rc = linda_ib_epb_wait ( linda, &xact ) ) != 0 )
1742 data = BIT_GET ( &xact, ib_epb_data );
1747 * Write data via IB external parallel bus
1749 * @v linda Linda device
1750 * @v location EPB location
1751 * @v data Data to write
1752 * @ret rc Return status code
1754 * You must have already acquired ownership of the IB external
1757 static int linda_ib_epb_write ( struct linda *linda, unsigned int location,
1758 unsigned int data ) {
1759 struct QIB_7220_ibsd_epb_transaction_reg xact;
1762 /* Ensure no transaction is currently in progress */
1763 if ( ( rc = linda_ib_epb_wait ( linda, &xact ) ) != 0 )
1767 memset ( &xact, 0, sizeof ( xact ) );
1770 ib_epb_address, LINDA_EPB_LOC_ADDRESS ( location ),
1771 ib_epb_read_write, LINDA_EPB_WRITE,
1772 ib_epb_cs, LINDA_EPB_LOC_CS ( location ) );
1773 linda_writeq ( linda, &xact,
1774 QIB_7220_ibsd_epb_transaction_reg_offset );
1776 /* Wait for transaction to complete */
1777 if ( ( rc = linda_ib_epb_wait ( linda, &xact ) ) != 0 )
1784 * Read/modify/write EPB register
1786 * @v linda Linda device
1788 * @v channel Channel
1789 * @v element Element
1791 * @v value Value to set
1792 * @v mask Mask to apply to old value
1793 * @ret rc Return status code
1795 static int linda_ib_epb_mod_reg ( struct linda *linda, unsigned int cs,
1796 unsigned int channel, unsigned int element,
1797 unsigned int reg, unsigned int value,
1798 unsigned int mask ) {
1799 unsigned int location;
1803 DBG_DISABLE ( DBGLVL_IO );
1806 assert ( ( value & mask ) == value );
1808 /* Acquire bus ownership */
1809 if ( ( rc = linda_ib_epb_request ( linda ) ) != 0 )
1812 /* Read existing value, if necessary */
1813 location = LINDA_EPB_LOC ( cs, channel, element, reg );
1814 if ( (~mask) & 0xff ) {
1815 old_value = linda_ib_epb_read ( linda, location );
1816 if ( old_value < 0 ) {
1825 value = ( ( old_value & ~mask ) | value );
1826 DBGCP ( linda, "Linda %p CS %d EPB(%d,%d,%#02x) %#02x => %#02x\n",
1827 linda, cs, channel, element, reg, old_value, value );
1828 if ( ( rc = linda_ib_epb_write ( linda, location, value ) ) != 0 )
1833 linda_ib_epb_release ( linda );
1835 DBG_ENABLE ( DBGLVL_IO );
1840 * Transfer data to/from microcontroller RAM
1842 * @v linda Linda device
1843 * @v address Starting address
1844 * @v write Data to write, or NULL
1845 * @v read Data to read, or NULL
1846 * @v len Length of data
1847 * @ret rc Return status code
1849 static int linda_ib_epb_ram_xfer ( struct linda *linda, unsigned int address,
1850 const void *write, void *read,
1852 unsigned int control;
1853 unsigned int address_hi;
1854 unsigned int address_lo;
1858 DBG_DISABLE ( DBGLVL_IO );
1860 assert ( ! ( write && read ) );
1861 assert ( ( address % LINDA_EPB_UC_CHUNK_SIZE ) == 0 );
1862 assert ( ( len % LINDA_EPB_UC_CHUNK_SIZE ) == 0 );
1864 /* Acquire bus ownership */
1865 if ( ( rc = linda_ib_epb_request ( linda ) ) != 0 )
1871 /* Reset the address for each new chunk */
1872 if ( ( address % LINDA_EPB_UC_CHUNK_SIZE ) == 0 ) {
1874 /* Write the control register */
1875 control = ( read ? LINDA_EPB_UC_CTL_READ :
1876 LINDA_EPB_UC_CTL_WRITE );
1877 if ( ( rc = linda_ib_epb_write ( linda,
1882 /* Write the address registers */
1883 address_hi = ( address >> 8 );
1884 if ( ( rc = linda_ib_epb_write ( linda,
1885 LINDA_EPB_UC_ADDR_HI,
1886 address_hi ) ) != 0 )
1888 address_lo = ( address & 0xff );
1889 if ( ( rc = linda_ib_epb_write ( linda,
1890 LINDA_EPB_UC_ADDR_LO,
1891 address_lo ) ) != 0 )
1895 /* Read or write the data */
1897 data = linda_ib_epb_read ( linda, LINDA_EPB_UC_DATA );
1902 *( ( uint8_t * ) read++ ) = data;
1904 data = *( ( uint8_t * ) write++ );
1905 if ( ( rc = linda_ib_epb_write ( linda,
1913 /* Reset the control byte after each chunk */
1914 if ( ( address % LINDA_EPB_UC_CHUNK_SIZE ) == 0 ) {
1915 if ( ( rc = linda_ib_epb_write ( linda,
1923 linda_ib_epb_release ( linda );
1926 DBG_ENABLE ( DBGLVL_IO );
1930 /***************************************************************************
1932 * Infiniband SerDes initialisation
1934 ***************************************************************************
1937 /** A Linda SerDes parameter */
1938 struct linda_serdes_param {
1939 /** EPB address as constructed by LINDA_EPB_ADDRESS() */
1943 /** Mask to apply to old value */
1947 /** Magic "all channels" channel number */
1948 #define LINDA_EPB_ALL_CHANNELS 31
1950 /** End of SerDes parameter list marker */
1951 #define LINDA_SERDES_PARAM_END { 0, 0, 0 }
1954 * Program IB SerDes register(s)
1956 * @v linda Linda device
1957 * @v param SerDes parameter
1958 * @ret rc Return status code
1960 static int linda_set_serdes_param ( struct linda *linda,
1961 struct linda_serdes_param *param ) {
1962 unsigned int channel;
1963 unsigned int channel_start;
1964 unsigned int channel_end;
1965 unsigned int element;
1969 /* Break down the EPB address and determine channels */
1970 channel = LINDA_EPB_ADDRESS_CHANNEL ( param->address );
1971 element = LINDA_EPB_ADDRESS_ELEMENT ( param->address );
1972 reg = LINDA_EPB_ADDRESS_REG ( param->address );
1973 if ( channel == LINDA_EPB_ALL_CHANNELS ) {
1977 channel_start = channel_end = channel;
1980 /* Modify register for each specified channel */
1981 for ( channel = channel_start ; channel <= channel_end ; channel++ ) {
1982 if ( ( rc = linda_ib_epb_mod_reg ( linda, LINDA_EPB_CS_SERDES,
1983 channel, element, reg,
1985 param->mask ) ) != 0 )
1993 * Program IB SerDes registers
1995 * @v linda Linda device
1996 * @v param SerDes parameters
1997 * @v count Number of parameters
1998 * @ret rc Return status code
2000 static int linda_set_serdes_params ( struct linda *linda,
2001 struct linda_serdes_param *params ) {
2004 for ( ; params->mask != 0 ; params++ ){
2005 if ( ( rc = linda_set_serdes_param ( linda,
2013 #define LINDA_DDS_VAL( amp_d, main_d, ipst_d, ipre_d, \
2014 amp_s, main_s, ipst_s, ipre_s ) \
2015 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x00 ), \
2016 ( ( ( amp_d & 0x1f ) << 1 ) | 1 ), 0xff }, \
2017 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x01 ), \
2018 ( ( ( amp_s & 0x1f ) << 1 ) | 1 ), 0xff }, \
2019 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x09 ), \
2020 ( ( main_d << 3 ) | 4 | ( ipre_d >> 2 ) ), 0xff }, \
2021 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x0a ), \
2022 ( ( main_s << 3 ) | 4 | ( ipre_s >> 2 ) ), 0xff }, \
2023 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x06 ), \
2024 ( ( ( ipst_d & 0xf ) << 1 ) | \
2025 ( ( ipre_d & 3 ) << 6 ) | 0x21 ), 0xff }, \
2026 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x07 ), \
2027 ( ( ( ipst_s & 0xf ) << 1 ) | \
2028 ( ( ipre_s & 3 ) << 6) | 0x21 ), 0xff }
2031 * Linda SerDes default parameters
2033 * These magic start-of-day values are taken from the Linux driver.
2035 static struct linda_serdes_param linda_serdes_defaults1[] = {
2037 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x00 ), 0xd4, 0xff },
2039 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x05 ), 0x2d, 0xff },
2041 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x08 ), 0x03, 0x0f },
2043 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x27 ), 0x10, 0xff },
2045 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x28 ), 0x30, 0xff },
2047 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x0e ), 0x40, 0xff },
2049 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x06 ), 0x04, 0xff },
2051 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x0f ), 0x04, 0xff },
2052 /* End of this block */
2053 LINDA_SERDES_PARAM_END
2055 static struct linda_serdes_param linda_serdes_defaults2[] = {
2057 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x06 ), 0x00, 0xff },
2059 LINDA_DDS_VAL ( 31, 19, 12, 0, 29, 22, 9, 0 ),
2060 /* Set Rcv Eq. to Preset node */
2061 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x27 ), 0x10, 0xff },
2063 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x08 ), 0x00, 0xff },
2065 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x21 ), 0x00, 0xff },
2067 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x09 ), 0x02, 0xff },
2069 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x23 ), 0x02, 0xff },
2071 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x1b ), 0x0c, 0xff },
2073 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x1c ), 0x0c, 0xff },
2075 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x1e ), 0x10, 0xff },
2077 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x1f ), 0x10, 0xff },
2078 /* VCDL_CTRL0 toggle */
2079 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x06 ), 0x20, 0xff },
2080 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x06 ), 0x00, 0xff },
2082 { LINDA_EPB_ADDRESS ( 7, 0, 0x15 ), 0x80, 0xff },
2083 /* End of this block */
2084 LINDA_SERDES_PARAM_END
2086 static struct linda_serdes_param linda_serdes_defaults3[] = {
2088 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x27 ), 0x00, 0x38 },
2089 /* End of this block */
2090 LINDA_SERDES_PARAM_END
2094 * Program the microcontroller RAM
2096 * @v linda Linda device
2097 * @ret rc Return status code
2099 static int linda_program_uc_ram ( struct linda *linda ) {
2102 if ( ( rc = linda_ib_epb_ram_xfer ( linda, 0, linda_ib_fw, NULL,
2103 sizeof ( linda_ib_fw ) ) ) != 0 ){
2104 DBGC ( linda, "Linda %p could not load IB firmware: %s\n",
2105 linda, strerror ( rc ) );
2113 * Verify the microcontroller RAM
2115 * @v linda Linda device
2116 * @ret rc Return status code
2118 static int linda_verify_uc_ram ( struct linda *linda ) {
2119 uint8_t verify[LINDA_EPB_UC_CHUNK_SIZE];
2120 unsigned int offset;
2123 for ( offset = 0 ; offset < sizeof ( linda_ib_fw );
2124 offset += sizeof ( verify ) ) {
2125 if ( ( rc = linda_ib_epb_ram_xfer ( linda, offset,
2127 sizeof (verify) )) != 0 ){
2128 DBGC ( linda, "Linda %p could not read back IB "
2129 "firmware: %s\n", linda, strerror ( rc ) );
2132 if ( memcmp ( ( linda_ib_fw + offset ), verify,
2133 sizeof ( verify ) ) != 0 ) {
2134 DBGC ( linda, "Linda %p firmware verification failed "
2135 "at offset %#x\n", linda, offset );
2136 DBGC_HDA ( linda, offset, ( linda_ib_fw + offset ),
2137 sizeof ( verify ) );
2138 DBGC_HDA ( linda, offset, verify, sizeof ( verify ) );
2143 DBGC2 ( linda, "Linda %p firmware verified ok\n", linda );
2148 * Use the microcontroller to trim the IB link
2150 * @v linda Linda device
2151 * @ret rc Return status code
2153 static int linda_trim_ib ( struct linda *linda ) {
2154 struct QIB_7220_IBSerDesCtrl ctrl;
2155 struct QIB_7220_IntStatus intstatus;
2159 /* Bring the microcontroller out of reset */
2160 linda_readq ( linda, &ctrl, QIB_7220_IBSerDesCtrl_offset );
2161 BIT_SET ( &ctrl, ResetIB_uC_Core, 0 );
2162 linda_writeq ( linda, &ctrl, QIB_7220_IBSerDesCtrl_offset );
2164 /* Wait for the "trim done" signal */
2165 for ( i = 0 ; i < LINDA_TRIM_DONE_MAX_WAIT_MS ; i++ ) {
2166 linda_readq ( linda, &intstatus, QIB_7220_IntStatus_offset );
2167 if ( BIT_GET ( &intstatus, IBSerdesTrimDone ) ) {
2174 DBGC ( linda, "Linda %p timed out waiting for trim done\n", linda );
2177 /* Put the microcontroller back into reset */
2178 BIT_SET ( &ctrl, ResetIB_uC_Core, 1 );
2179 linda_writeq ( linda, &ctrl, QIB_7220_IBSerDesCtrl_offset );
2185 * Initialise the IB SerDes
2187 * @v linda Linda device
2188 * @ret rc Return status code
2190 static int linda_init_ib_serdes ( struct linda *linda ) {
2191 struct QIB_7220_Control control;
2192 struct QIB_7220_IBCCtrl ibcctrl;
2193 struct QIB_7220_IBCDDRCtrl ibcddrctrl;
2194 struct QIB_7220_XGXSCfg xgxscfg;
2198 linda_readq ( linda, &control, QIB_7220_Control_offset );
2199 BIT_SET ( &control, LinkEn, 0 );
2200 linda_writeq ( linda, &control, QIB_7220_Control_offset );
2202 /* Configure sensible defaults for IBC */
2203 memset ( &ibcctrl, 0, sizeof ( ibcctrl ) );
2204 BIT_FILL_6 ( &ibcctrl, /* Tuning values taken from Linux driver */
2205 FlowCtrlPeriod, 0x03,
2206 FlowCtrlWaterMark, 0x05,
2207 MaxPktLen, ( ( LINDA_RECV_HEADER_SIZE +
2208 LINDA_RECV_PAYLOAD_SIZE +
2209 4 /* ICRC */ ) >> 2 ),
2210 PhyerrThreshold, 0xf,
2211 OverrunThreshold, 0xf,
2213 linda_writeq ( linda, &ibcctrl, QIB_7220_IBCCtrl_offset );
2215 /* Force SDR only to avoid needing all the DDR tuning,
2216 * Mellanox compatibility hacks etc. SDR is plenty for
2217 * boot-time operation.
2219 linda_readq ( linda, &ibcddrctrl, QIB_7220_IBCDDRCtrl_offset );
2220 BIT_SET ( &ibcddrctrl, IB_ENHANCED_MODE, 0 );
2221 BIT_SET ( &ibcddrctrl, SD_SPEED_SDR, 1 );
2222 BIT_SET ( &ibcddrctrl, SD_SPEED_DDR, 0 );
2223 BIT_SET ( &ibcddrctrl, SD_SPEED_QDR, 0 );
2224 BIT_SET ( &ibcddrctrl, HRTBT_ENB, 0 );
2225 BIT_SET ( &ibcddrctrl, HRTBT_AUTO, 0 );
2226 linda_writeq ( linda, &ibcddrctrl, QIB_7220_IBCDDRCtrl_offset );
2228 /* Set default SerDes parameters */
2229 if ( ( rc = linda_set_serdes_params ( linda,
2230 linda_serdes_defaults1 ) ) != 0 )
2232 udelay ( 415 ); /* Magic delay while SerDes sorts itself out */
2233 if ( ( rc = linda_set_serdes_params ( linda,
2234 linda_serdes_defaults2 ) ) != 0 )
2237 /* Program the microcontroller RAM */
2238 if ( ( rc = linda_program_uc_ram ( linda ) ) != 0 )
2241 /* Verify the microcontroller RAM contents */
2243 if ( ( rc = linda_verify_uc_ram ( linda ) ) != 0 )
2247 /* More SerDes tuning */
2248 if ( ( rc = linda_set_serdes_params ( linda,
2249 linda_serdes_defaults3 ) ) != 0 )
2252 /* Use the microcontroller to trim the IB link */
2253 if ( ( rc = linda_trim_ib ( linda ) ) != 0 )
2256 /* Bring XGXS out of reset */
2257 linda_readq ( linda, &xgxscfg, QIB_7220_XGXSCfg_offset );
2258 BIT_SET ( &xgxscfg, tx_rx_reset, 0 );
2259 BIT_SET ( &xgxscfg, xcv_reset, 0 );
2260 linda_writeq ( linda, &xgxscfg, QIB_7220_XGXSCfg_offset );
2265 /***************************************************************************
2267 * PCI layer interface
2269 ***************************************************************************
2277 * @ret rc Return status code
2279 static int linda_probe ( struct pci_device *pci,
2280 const struct pci_device_id *id __unused ) {
2281 struct ib_device *ibdev;
2282 struct linda *linda;
2283 struct QIB_7220_Revision revision;
2286 /* Allocate Infiniband device */
2287 ibdev = alloc_ibdev ( sizeof ( *linda ) );
2290 goto err_alloc_ibdev;
2292 pci_set_drvdata ( pci, ibdev );
2293 linda = ib_get_drvdata ( ibdev );
2294 ibdev->op = &linda_ib_operations;
2295 ibdev->dev = &pci->dev;
2298 /* Fix up PCI device */
2299 adjust_pci_device ( pci );
2302 linda->regs = ioremap ( pci->membase, LINDA_BAR0_SIZE );
2303 DBGC2 ( linda, "Linda %p has BAR at %08lx\n", linda, pci->membase );
2305 /* Print some general data */
2306 linda_readq ( linda, &revision, QIB_7220_Revision_offset );
2307 DBGC2 ( linda, "Linda %p board %02lx v%ld.%ld.%ld.%ld\n", linda,
2308 BIT_GET ( &revision, BoardID ),
2309 BIT_GET ( &revision, R_SW ),
2310 BIT_GET ( &revision, R_Arch ),
2311 BIT_GET ( &revision, R_ChipRevMajor ),
2312 BIT_GET ( &revision, R_ChipRevMinor ) );
2314 /* Record link capabilities. Note that we force SDR only to
2315 * avoid having to carry extra code for DDR tuning etc.
2317 ibdev->link_width_enabled = ibdev->link_width_supported =
2318 ( IB_LINK_WIDTH_4X | IB_LINK_WIDTH_1X );
2319 ibdev->link_speed_enabled = ibdev->link_speed_supported =
2322 /* Initialise I2C subsystem */
2323 if ( ( rc = linda_init_i2c ( linda ) ) != 0 )
2326 /* Read EEPROM parameters */
2327 if ( ( rc = linda_read_eeprom ( linda, &ibdev->gid.u.half[1] ) ) != 0 )
2328 goto err_read_eeprom;
2330 /* Initialise send datapath */
2331 if ( ( rc = linda_init_send ( linda ) ) != 0 )
2334 /* Initialise receive datapath */
2335 if ( ( rc = linda_init_recv ( linda ) ) != 0 )
2338 /* Initialise the IB SerDes */
2339 if ( ( rc = linda_init_ib_serdes ( linda ) ) != 0 )
2340 goto err_init_ib_serdes;
2342 /* Register Infiniband device */
2343 if ( ( rc = register_ibdev ( ibdev ) ) != 0 ) {
2344 DBGC ( linda, "Linda %p could not register IB "
2345 "device: %s\n", linda, strerror ( rc ) );
2346 goto err_register_ibdev;
2351 unregister_ibdev ( ibdev );
2353 linda_fini_recv ( linda );
2355 linda_fini_send ( linda );
2360 ibdev_put ( ibdev );
2370 static void linda_remove ( struct pci_device *pci ) {
2371 struct ib_device *ibdev = pci_get_drvdata ( pci );
2372 struct linda *linda = ib_get_drvdata ( ibdev );
2374 unregister_ibdev ( ibdev );
2375 linda_fini_recv ( linda );
2376 linda_fini_send ( linda );
2377 ibdev_put ( ibdev );
2380 static struct pci_device_id linda_nics[] = {
2381 PCI_ROM ( 0x1077, 0x7220, "iba7220", "QLE7240/7280 HCA driver", 0 ),
2384 struct pci_driver linda_driver __pci_driver = {
2386 .id_count = ( sizeof ( linda_nics ) / sizeof ( linda_nics[0] ) ),
2387 .probe = linda_probe,
2388 .remove = linda_remove,