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., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
33 #include <ipxe/infiniband.h>
35 #include <ipxe/bitbash.h>
36 #include <ipxe/malloc.h>
37 #include <ipxe/iobuf.h>
43 * QLogic Linda Infiniband HCA
47 /** A Linda send work queue */
48 struct linda_send_work_queue {
49 /** Send buffer usage */
57 /** A Linda receive work queue */
58 struct linda_recv_work_queue {
59 /** Receive header ring */
61 /** Receive header producer offset (written by hardware) */
62 struct QIB_7220_scalar header_prod;
63 /** Receive header consumer offset */
64 unsigned int header_cons;
65 /** Offset within register space of the eager array */
66 unsigned long eager_array;
67 /** Number of entries in eager array */
68 unsigned int eager_entries;
69 /** Eager array producer index */
70 unsigned int eager_prod;
71 /** Eager array consumer index */
72 unsigned int eager_cons;
80 /** In-use contexts */
81 uint8_t used_ctx[LINDA_NUM_CONTEXTS];
82 /** Send work queues */
83 struct linda_send_work_queue send_wq[LINDA_NUM_CONTEXTS];
84 /** Receive work queues */
85 struct linda_recv_work_queue recv_wq[LINDA_NUM_CONTEXTS];
87 /** Offset within register space of the first send buffer */
88 unsigned long send_buffer_base;
89 /** Send buffer availability (reported by hardware) */
90 struct QIB_7220_SendBufAvail *sendbufavail;
91 /** Send buffer availability (maintained by software) */
92 uint8_t send_buf[LINDA_MAX_SEND_BUFS];
93 /** Send buffer availability producer counter */
94 unsigned int send_buf_prod;
95 /** Send buffer availability consumer counter */
96 unsigned int send_buf_cons;
97 /** Number of reserved send buffers (across all QPs) */
98 unsigned int reserved_send_bufs;
100 /** I2C bit-bashing interface */
101 struct i2c_bit_basher i2c;
102 /** I2C serial EEPROM */
103 struct i2c_device eeprom;
106 /***************************************************************************
108 * Linda register access
110 ***************************************************************************
112 * This card requires atomic 64-bit accesses. Strange things happen
113 * if you try to use 32-bit accesses; sometimes they work, sometimes
114 * they don't, sometimes you get random data.
116 * These accessors use the "movq" MMX instruction, and so won't work
117 * on really old Pentiums (which won't have PCIe anyway, so this is
118 * something of a moot point).
122 * Read Linda qword register
124 * @v linda Linda device
125 * @v dwords Register buffer to read into
126 * @v offset Register offset
128 static void linda_readq ( struct linda *linda, uint32_t *dwords,
129 unsigned long offset ) {
130 void *addr = ( linda->regs + offset );
132 __asm__ __volatile__ ( "movq (%1), %%mm0\n\t"
133 "movq %%mm0, (%0)\n\t"
134 : : "r" ( dwords ), "r" ( addr ) : "memory" );
136 DBGIO ( "[%08lx] => %08x%08x\n",
137 virt_to_phys ( addr ), dwords[1], dwords[0] );
139 #define linda_readq( _linda, _ptr, _offset ) \
140 linda_readq ( (_linda), (_ptr)->u.dwords, (_offset) )
141 #define linda_readq_array8b( _linda, _ptr, _offset, _idx ) \
142 linda_readq ( (_linda), (_ptr), ( (_offset) + ( (_idx) * 8 ) ) )
143 #define linda_readq_array64k( _linda, _ptr, _offset, _idx ) \
144 linda_readq ( (_linda), (_ptr), ( (_offset) + ( (_idx) * 65536 ) ) )
147 * Write Linda qword register
149 * @v linda Linda device
150 * @v dwords Register buffer to write
151 * @v offset Register offset
153 static void linda_writeq ( struct linda *linda, const uint32_t *dwords,
154 unsigned long offset ) {
155 void *addr = ( linda->regs + offset );
157 DBGIO ( "[%08lx] <= %08x%08x\n",
158 virt_to_phys ( addr ), dwords[1], dwords[0] );
160 __asm__ __volatile__ ( "movq (%0), %%mm0\n\t"
161 "movq %%mm0, (%1)\n\t"
162 : : "r" ( dwords ), "r" ( addr ) : "memory" );
164 #define linda_writeq( _linda, _ptr, _offset ) \
165 linda_writeq ( (_linda), (_ptr)->u.dwords, (_offset) )
166 #define linda_writeq_array8b( _linda, _ptr, _offset, _idx ) \
167 linda_writeq ( (_linda), (_ptr), ( (_offset) + ( (_idx) * 8 ) ) )
168 #define linda_writeq_array64k( _linda, _ptr, _offset, _idx ) \
169 linda_writeq ( (_linda), (_ptr), ( (_offset) + ( (_idx) * 65536 ) ) )
172 * Write Linda dword register
174 * @v linda Linda device
175 * @v dword Value to write
176 * @v offset Register offset
178 static void linda_writel ( struct linda *linda, uint32_t dword,
179 unsigned long offset ) {
180 writel ( dword, ( linda->regs + offset ) );
183 /***************************************************************************
185 * Link state management
187 ***************************************************************************
191 * Textual representation of link state
193 * @v link_state Link state
194 * @ret link_text Link state text
196 static const char * linda_link_state_text ( unsigned int link_state ) {
197 switch ( link_state ) {
198 case LINDA_LINK_STATE_DOWN: return "DOWN";
199 case LINDA_LINK_STATE_INIT: return "INIT";
200 case LINDA_LINK_STATE_ARM: return "ARM";
201 case LINDA_LINK_STATE_ACTIVE: return "ACTIVE";
202 case LINDA_LINK_STATE_ACT_DEFER:return "ACT_DEFER";
203 default: return "UNKNOWN";
208 * Handle link state change
210 * @v linda Linda device
212 static void linda_link_state_changed ( struct ib_device *ibdev ) {
213 struct linda *linda = ib_get_drvdata ( ibdev );
214 struct QIB_7220_IBCStatus ibcstatus;
215 struct QIB_7220_EXTCtrl extctrl;
216 unsigned int link_state;
217 unsigned int link_width;
218 unsigned int link_speed;
220 /* Read link state */
221 linda_readq ( linda, &ibcstatus, QIB_7220_IBCStatus_offset );
222 link_state = BIT_GET ( &ibcstatus, LinkState );
223 link_width = BIT_GET ( &ibcstatus, LinkWidthActive );
224 link_speed = BIT_GET ( &ibcstatus, LinkSpeedActive );
225 DBGC ( linda, "Linda %p link state %s (%s %s)\n", linda,
226 linda_link_state_text ( link_state ),
227 ( link_speed ? "DDR" : "SDR" ), ( link_width ? "x4" : "x1" ) );
229 /* Set LEDs according to link state */
230 linda_readq ( linda, &extctrl, QIB_7220_EXTCtrl_offset );
231 BIT_SET ( &extctrl, LEDPriPortGreenOn,
232 ( ( link_state >= LINDA_LINK_STATE_INIT ) ? 1 : 0 ) );
233 BIT_SET ( &extctrl, LEDPriPortYellowOn,
234 ( ( link_state >= LINDA_LINK_STATE_ACTIVE ) ? 1 : 0 ) );
235 linda_writeq ( linda, &extctrl, QIB_7220_EXTCtrl_offset );
237 /* Notify Infiniband core of link state change */
238 ibdev->port_state = ( link_state + 1 );
239 ibdev->link_width_active =
240 ( link_width ? IB_LINK_WIDTH_4X : IB_LINK_WIDTH_1X );
241 ibdev->link_speed_active =
242 ( link_speed ? IB_LINK_SPEED_DDR : IB_LINK_SPEED_SDR );
243 ib_link_state_changed ( ibdev );
247 * Wait for link state change to take effect
249 * @v linda Linda device
250 * @v new_link_state Expected link state
251 * @ret rc Return status code
253 static int linda_link_state_check ( struct linda *linda,
254 unsigned int new_link_state ) {
255 struct QIB_7220_IBCStatus ibcstatus;
256 unsigned int link_state;
259 for ( i = 0 ; i < LINDA_LINK_STATE_MAX_WAIT_US ; i++ ) {
260 linda_readq ( linda, &ibcstatus, QIB_7220_IBCStatus_offset );
261 link_state = BIT_GET ( &ibcstatus, LinkState );
262 if ( link_state == new_link_state )
267 DBGC ( linda, "Linda %p timed out waiting for link state %s\n",
268 linda, linda_link_state_text ( link_state ) );
273 * Set port information
275 * @v ibdev Infiniband device
276 * @v mad Set port information MAD
278 static int linda_set_port_info ( struct ib_device *ibdev, union ib_mad *mad ) {
279 struct linda *linda = ib_get_drvdata ( ibdev );
280 struct ib_port_info *port_info = &mad->smp.smp_data.port_info;
281 struct QIB_7220_IBCCtrl ibcctrl;
282 unsigned int port_state;
283 unsigned int link_state;
285 /* Set new link state */
286 port_state = ( port_info->link_speed_supported__port_state & 0xf );
288 link_state = ( port_state - 1 );
289 DBGC ( linda, "Linda %p set link state to %s (%x)\n", linda,
290 linda_link_state_text ( link_state ), link_state );
291 linda_readq ( linda, &ibcctrl, QIB_7220_IBCCtrl_offset );
292 BIT_SET ( &ibcctrl, LinkCmd, link_state );
293 linda_writeq ( linda, &ibcctrl, QIB_7220_IBCCtrl_offset );
295 /* Wait for link state change to take effect. Ignore
296 * errors; the current link state will be returned via
297 * the GetResponse MAD.
299 linda_link_state_check ( linda, link_state );
302 /* Detect and report link state change */
303 linda_link_state_changed ( ibdev );
309 * Set partition key table
311 * @v ibdev Infiniband device
312 * @v mad Set partition key table MAD
314 static int linda_set_pkey_table ( struct ib_device *ibdev __unused,
315 union ib_mad *mad __unused ) {
320 /***************************************************************************
324 ***************************************************************************
328 * Map context number to QPN
330 * @v ctx Context index
331 * @ret qpn Queue pair number
333 static int linda_ctx_to_qpn ( unsigned int ctx ) {
334 /* This mapping is fixed by hardware */
339 * Map QPN to context number
341 * @v qpn Queue pair number
342 * @ret ctx Context index
344 static int linda_qpn_to_ctx ( unsigned int qpn ) {
345 /* This mapping is fixed by hardware */
352 * @v linda Linda device
353 * @ret ctx Context index, or negative error
355 static int linda_alloc_ctx ( struct linda *linda ) {
358 for ( ctx = 0 ; ctx < LINDA_NUM_CONTEXTS ; ctx++ ) {
360 if ( ! linda->used_ctx[ctx] ) {
361 linda->used_ctx[ctx ] = 1;
362 DBGC2 ( linda, "Linda %p CTX %d allocated\n",
368 DBGC ( linda, "Linda %p out of available contexts\n", linda );
375 * @v linda Linda device
376 * @v ctx Context index
378 static void linda_free_ctx ( struct linda *linda, unsigned int ctx ) {
380 linda->used_ctx[ctx] = 0;
381 DBGC2 ( linda, "Linda %p CTX %d freed\n", linda, ctx );
384 /***************************************************************************
388 ***************************************************************************
391 /** Send buffer toggle bit
393 * We encode send buffers as 7 bits of send buffer index plus a single
394 * bit which should match the "check" bit in the SendBufAvail array.
396 #define LINDA_SEND_BUF_TOGGLE 0x80
399 * Allocate a send buffer
401 * @v linda Linda device
402 * @ret send_buf Send buffer
404 * You must guarantee that a send buffer is available. This is done
405 * by refusing to allocate more TX WQEs in total than the number of
406 * available send buffers.
408 static unsigned int linda_alloc_send_buf ( struct linda *linda ) {
409 unsigned int send_buf;
411 send_buf = linda->send_buf[linda->send_buf_cons];
412 send_buf ^= LINDA_SEND_BUF_TOGGLE;
413 linda->send_buf_cons = ( ( linda->send_buf_cons + 1 ) %
414 LINDA_MAX_SEND_BUFS );
421 * @v linda Linda device
422 * @v send_buf Send buffer
424 static void linda_free_send_buf ( struct linda *linda,
425 unsigned int send_buf ) {
426 linda->send_buf[linda->send_buf_prod] = send_buf;
427 linda->send_buf_prod = ( ( linda->send_buf_prod + 1 ) %
428 LINDA_MAX_SEND_BUFS );
432 * Check to see if send buffer is in use
434 * @v linda Linda device
435 * @v send_buf Send buffer
436 * @ret in_use Send buffer is in use
438 static int linda_send_buf_in_use ( struct linda *linda,
439 unsigned int send_buf ) {
440 unsigned int send_idx;
441 unsigned int send_check;
442 unsigned int inusecheck;
446 send_idx = ( send_buf & ~LINDA_SEND_BUF_TOGGLE );
447 send_check = ( !! ( send_buf & LINDA_SEND_BUF_TOGGLE ) );
448 inusecheck = BIT_GET ( linda->sendbufavail, InUseCheck[send_idx] );
449 inuse = ( !! ( inusecheck & 0x02 ) );
450 check = ( !! ( inusecheck & 0x01 ) );
451 return ( inuse || ( check != send_check ) );
455 * Calculate starting offset for send buffer
457 * @v linda Linda device
458 * @v send_buf Send buffer
459 * @ret offset Starting offset
461 static unsigned long linda_send_buffer_offset ( struct linda *linda,
462 unsigned int send_buf ) {
463 return ( linda->send_buffer_base +
464 ( ( send_buf & ~LINDA_SEND_BUF_TOGGLE ) *
465 LINDA_SEND_BUF_SIZE ) );
469 * Create send work queue
471 * @v linda Linda device
474 static int linda_create_send_wq ( struct linda *linda,
475 struct ib_queue_pair *qp ) {
476 struct ib_work_queue *wq = &qp->send;
477 struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
480 /* Reserve send buffers */
481 if ( ( linda->reserved_send_bufs + qp->send.num_wqes ) >
482 LINDA_MAX_SEND_BUFS ) {
483 DBGC ( linda, "Linda %p out of send buffers (have %d, used "
484 "%d, need %d)\n", linda, LINDA_MAX_SEND_BUFS,
485 linda->reserved_send_bufs, qp->send.num_wqes );
487 goto err_reserve_bufs;
489 linda->reserved_send_bufs += qp->send.num_wqes;
491 /* Reset work queue */
495 /* Allocate space for send buffer uasge list */
496 linda_wq->send_buf = zalloc ( qp->send.num_wqes *
497 sizeof ( linda_wq->send_buf[0] ) );
498 if ( ! linda_wq->send_buf ) {
500 goto err_alloc_send_buf;
505 free ( linda_wq->send_buf );
507 linda->reserved_send_bufs -= qp->send.num_wqes;
513 * Destroy send work queue
515 * @v linda Linda device
518 static void linda_destroy_send_wq ( struct linda *linda,
519 struct ib_queue_pair *qp ) {
520 struct ib_work_queue *wq = &qp->send;
521 struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
523 free ( linda_wq->send_buf );
524 linda->reserved_send_bufs -= qp->send.num_wqes;
528 * Initialise send datapath
530 * @v linda Linda device
531 * @ret rc Return status code
533 static int linda_init_send ( struct linda *linda ) {
534 struct QIB_7220_SendBufBase sendbufbase;
535 struct QIB_7220_SendBufAvailAddr sendbufavailaddr;
536 struct QIB_7220_SendCtrl sendctrl;
540 /* Retrieve SendBufBase */
541 linda_readq ( linda, &sendbufbase, QIB_7220_SendBufBase_offset );
542 linda->send_buffer_base = BIT_GET ( &sendbufbase,
544 DBGC ( linda, "Linda %p send buffers at %lx\n",
545 linda, linda->send_buffer_base );
547 /* Initialise the send_buf[] array */
548 for ( i = 0 ; i < LINDA_MAX_SEND_BUFS ; i++ )
549 linda->send_buf[i] = i;
551 /* Allocate space for the SendBufAvail array */
552 linda->sendbufavail = malloc_dma ( sizeof ( *linda->sendbufavail ),
553 LINDA_SENDBUFAVAIL_ALIGN );
554 if ( ! linda->sendbufavail ) {
556 goto err_alloc_sendbufavail;
558 memset ( linda->sendbufavail, 0, sizeof ( linda->sendbufavail ) );
560 /* Program SendBufAvailAddr into the hardware */
561 memset ( &sendbufavailaddr, 0, sizeof ( sendbufavailaddr ) );
562 BIT_FILL_1 ( &sendbufavailaddr, SendBufAvailAddr,
563 ( virt_to_bus ( linda->sendbufavail ) >> 6 ) );
564 linda_writeq ( linda, &sendbufavailaddr,
565 QIB_7220_SendBufAvailAddr_offset );
567 /* Enable sending and DMA of SendBufAvail */
568 memset ( &sendctrl, 0, sizeof ( sendctrl ) );
569 BIT_FILL_2 ( &sendctrl,
572 linda_writeq ( linda, &sendctrl, QIB_7220_SendCtrl_offset );
576 free_dma ( linda->sendbufavail, sizeof ( *linda->sendbufavail ) );
577 err_alloc_sendbufavail:
582 * Shut down send datapath
584 * @v linda Linda device
586 static void linda_fini_send ( struct linda *linda ) {
587 struct QIB_7220_SendCtrl sendctrl;
589 /* Disable sending and DMA of SendBufAvail */
590 memset ( &sendctrl, 0, sizeof ( sendctrl ) );
591 linda_writeq ( linda, &sendctrl, QIB_7220_SendCtrl_offset );
594 /* Ensure hardware has seen this disable */
595 linda_readq ( linda, &sendctrl, QIB_7220_SendCtrl_offset );
597 free_dma ( linda->sendbufavail, sizeof ( *linda->sendbufavail ) );
600 /***************************************************************************
604 ***************************************************************************
608 * Create receive work queue
610 * @v linda Linda device
612 * @ret rc Return status code
614 static int linda_create_recv_wq ( struct linda *linda,
615 struct ib_queue_pair *qp ) {
616 struct ib_work_queue *wq = &qp->recv;
617 struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
618 struct QIB_7220_RcvHdrAddr0 rcvhdraddr;
619 struct QIB_7220_RcvHdrTailAddr0 rcvhdrtailaddr;
620 struct QIB_7220_RcvHdrHead0 rcvhdrhead;
621 struct QIB_7220_scalar rcvegrindexhead;
622 struct QIB_7220_RcvCtrl rcvctrl;
623 unsigned int ctx = linda_qpn_to_ctx ( qp->qpn );
626 /* Reset context information */
627 memset ( &linda_wq->header_prod, 0,
628 sizeof ( linda_wq->header_prod ) );
629 linda_wq->header_cons = 0;
630 linda_wq->eager_prod = 0;
631 linda_wq->eager_cons = 0;
633 /* Allocate receive header buffer */
634 linda_wq->header = malloc_dma ( LINDA_RECV_HEADERS_SIZE,
635 LINDA_RECV_HEADERS_ALIGN );
636 if ( ! linda_wq->header ) {
638 goto err_alloc_header;
641 /* Enable context in hardware */
642 memset ( &rcvhdraddr, 0, sizeof ( rcvhdraddr ) );
643 BIT_FILL_1 ( &rcvhdraddr, RcvHdrAddr0,
644 ( virt_to_bus ( linda_wq->header ) >> 2 ) );
645 linda_writeq_array8b ( linda, &rcvhdraddr,
646 QIB_7220_RcvHdrAddr0_offset, ctx );
647 memset ( &rcvhdrtailaddr, 0, sizeof ( rcvhdrtailaddr ) );
648 BIT_FILL_1 ( &rcvhdrtailaddr, RcvHdrTailAddr0,
649 ( virt_to_bus ( &linda_wq->header_prod ) >> 2 ) );
650 linda_writeq_array8b ( linda, &rcvhdrtailaddr,
651 QIB_7220_RcvHdrTailAddr0_offset, ctx );
652 memset ( &rcvhdrhead, 0, sizeof ( rcvhdrhead ) );
653 BIT_FILL_1 ( &rcvhdrhead, counter, 1 );
654 linda_writeq_array64k ( linda, &rcvhdrhead,
655 QIB_7220_RcvHdrHead0_offset, ctx );
656 memset ( &rcvegrindexhead, 0, sizeof ( rcvegrindexhead ) );
657 BIT_FILL_1 ( &rcvegrindexhead, Value, 1 );
658 linda_writeq_array64k ( linda, &rcvegrindexhead,
659 QIB_7220_RcvEgrIndexHead0_offset, ctx );
660 linda_readq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
661 BIT_SET ( &rcvctrl, PortEnable[ctx], 1 );
662 BIT_SET ( &rcvctrl, IntrAvail[ctx], 1 );
663 linda_writeq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
665 DBGC ( linda, "Linda %p QPN %ld CTX %d hdrs [%lx,%lx) prod %lx\n",
666 linda, qp->qpn, ctx, virt_to_bus ( linda_wq->header ),
667 ( virt_to_bus ( linda_wq->header ) + LINDA_RECV_HEADERS_SIZE ),
668 virt_to_bus ( &linda_wq->header_prod ) );
671 free_dma ( linda_wq->header, LINDA_RECV_HEADERS_SIZE );
677 * Destroy receive work queue
679 * @v linda Linda device
682 static void linda_destroy_recv_wq ( struct linda *linda,
683 struct ib_queue_pair *qp ) {
684 struct ib_work_queue *wq = &qp->recv;
685 struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
686 struct QIB_7220_RcvCtrl rcvctrl;
687 unsigned int ctx = linda_qpn_to_ctx ( qp->qpn );
689 /* Disable context in hardware */
690 linda_readq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
691 BIT_SET ( &rcvctrl, PortEnable[ctx], 0 );
692 BIT_SET ( &rcvctrl, IntrAvail[ctx], 0 );
693 linda_writeq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
695 /* Make sure the hardware has seen that the context is disabled */
696 linda_readq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
699 /* Free headers ring */
700 free_dma ( linda_wq->header, LINDA_RECV_HEADERS_SIZE );
703 linda_free_ctx ( linda, ctx );
707 * Initialise receive datapath
709 * @v linda Linda device
710 * @ret rc Return status code
712 static int linda_init_recv ( struct linda *linda ) {
713 struct QIB_7220_RcvCtrl rcvctrl;
714 struct QIB_7220_scalar rcvegrbase;
715 struct QIB_7220_scalar rcvhdrentsize;
716 struct QIB_7220_scalar rcvhdrcnt;
717 struct QIB_7220_RcvBTHQP rcvbthqp;
718 unsigned int portcfg;
719 unsigned long egrbase;
720 unsigned int eager_array_size_0;
721 unsigned int eager_array_size_other;
724 /* Select configuration based on number of contexts */
725 switch ( LINDA_NUM_CONTEXTS ) {
727 portcfg = LINDA_PORTCFG_5CTX;
728 eager_array_size_0 = LINDA_EAGER_ARRAY_SIZE_5CTX_0;
729 eager_array_size_other = LINDA_EAGER_ARRAY_SIZE_5CTX_OTHER;
732 portcfg = LINDA_PORTCFG_9CTX;
733 eager_array_size_0 = LINDA_EAGER_ARRAY_SIZE_9CTX_0;
734 eager_array_size_other = LINDA_EAGER_ARRAY_SIZE_9CTX_OTHER;
737 portcfg = LINDA_PORTCFG_17CTX;
738 eager_array_size_0 = LINDA_EAGER_ARRAY_SIZE_17CTX_0;
739 eager_array_size_other = LINDA_EAGER_ARRAY_SIZE_17CTX_OTHER;
742 linker_assert ( 0, invalid_LINDA_NUM_CONTEXTS );
746 /* Configure number of contexts */
747 memset ( &rcvctrl, 0, sizeof ( rcvctrl ) );
748 BIT_FILL_3 ( &rcvctrl,
752 linda_writeq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
754 /* Configure receive header buffer sizes */
755 memset ( &rcvhdrcnt, 0, sizeof ( rcvhdrcnt ) );
756 BIT_FILL_1 ( &rcvhdrcnt, Value, LINDA_RECV_HEADER_COUNT );
757 linda_writeq ( linda, &rcvhdrcnt, QIB_7220_RcvHdrCnt_offset );
758 memset ( &rcvhdrentsize, 0, sizeof ( rcvhdrentsize ) );
759 BIT_FILL_1 ( &rcvhdrentsize, Value, ( LINDA_RECV_HEADER_SIZE >> 2 ) );
760 linda_writeq ( linda, &rcvhdrentsize, QIB_7220_RcvHdrEntSize_offset );
762 /* Calculate eager array start addresses for each context */
763 linda_readq ( linda, &rcvegrbase, QIB_7220_RcvEgrBase_offset );
764 egrbase = BIT_GET ( &rcvegrbase, Value );
765 linda->recv_wq[0].eager_array = egrbase;
766 linda->recv_wq[0].eager_entries = eager_array_size_0;
767 egrbase += ( eager_array_size_0 * sizeof ( struct QIB_7220_RcvEgr ) );
768 for ( ctx = 1 ; ctx < LINDA_NUM_CONTEXTS ; ctx++ ) {
769 linda->recv_wq[ctx].eager_array = egrbase;
770 linda->recv_wq[ctx].eager_entries = eager_array_size_other;
771 egrbase += ( eager_array_size_other *
772 sizeof ( struct QIB_7220_RcvEgr ) );
774 for ( ctx = 0 ; ctx < LINDA_NUM_CONTEXTS ; ctx++ ) {
775 DBGC ( linda, "Linda %p CTX %d eager array at %lx (%d "
776 "entries)\n", linda, ctx,
777 linda->recv_wq[ctx].eager_array,
778 linda->recv_wq[ctx].eager_entries );
781 /* Set the BTH QP for Infinipath packets to an unused value */
782 memset ( &rcvbthqp, 0, sizeof ( rcvbthqp ) );
783 BIT_FILL_1 ( &rcvbthqp, RcvBTHQP, LINDA_QP_IDETH );
784 linda_writeq ( linda, &rcvbthqp, QIB_7220_RcvBTHQP_offset );
790 * Shut down receive datapath
792 * @v linda Linda device
794 static void linda_fini_recv ( struct linda *linda __unused ) {
795 /* Nothing to do; all contexts were already disabled when the
796 * queue pairs were destroyed
800 /***************************************************************************
802 * Completion queue operations
804 ***************************************************************************
808 * Create completion queue
810 * @v ibdev Infiniband device
811 * @v cq Completion queue
812 * @ret rc Return status code
814 static int linda_create_cq ( struct ib_device *ibdev,
815 struct ib_completion_queue *cq ) {
816 struct linda *linda = ib_get_drvdata ( ibdev );
819 /* The hardware has no concept of completion queues. We
820 * simply use the association between CQs and WQs (already
821 * handled by the IB core) to decide which WQs to poll.
823 * We do set a CQN, just to avoid confusing debug messages
827 DBGC ( linda, "Linda %p CQN %ld created\n", linda, cq->cqn );
833 * Destroy completion queue
835 * @v ibdev Infiniband device
836 * @v cq Completion queue
838 static void linda_destroy_cq ( struct ib_device *ibdev,
839 struct ib_completion_queue *cq ) {
840 struct linda *linda = ib_get_drvdata ( ibdev );
843 DBGC ( linda, "Linda %p CQN %ld destroyed\n", linda, cq->cqn );
846 /***************************************************************************
848 * Queue pair operations
850 ***************************************************************************
856 * @v ibdev Infiniband device
858 * @ret rc Return status code
860 static int linda_create_qp ( struct ib_device *ibdev,
861 struct ib_queue_pair *qp ) {
862 struct linda *linda = ib_get_drvdata ( ibdev );
866 /* Locate an available context */
867 ctx = linda_alloc_ctx ( linda );
873 /* Set queue pair number based on context index */
874 qp->qpn = linda_ctx_to_qpn ( ctx );
876 /* Set work-queue private data pointers */
877 ib_wq_set_drvdata ( &qp->send, &linda->send_wq[ctx] );
878 ib_wq_set_drvdata ( &qp->recv, &linda->recv_wq[ctx] );
880 /* Create receive work queue */
881 if ( ( rc = linda_create_recv_wq ( linda, qp ) ) != 0 )
882 goto err_create_recv_wq;
884 /* Create send work queue */
885 if ( ( rc = linda_create_send_wq ( linda, qp ) ) != 0 )
886 goto err_create_send_wq;
890 linda_destroy_send_wq ( linda, qp );
892 linda_destroy_recv_wq ( linda, qp );
894 linda_free_ctx ( linda, ctx );
902 * @v ibdev Infiniband device
904 * @ret rc Return status code
906 static int linda_modify_qp ( struct ib_device *ibdev,
907 struct ib_queue_pair *qp ) {
908 struct linda *linda = ib_get_drvdata ( ibdev );
910 /* Nothing to do; the hardware doesn't have a notion of queue
913 DBGC ( linda, "Linda %p QPN %ld modified\n", linda, qp->qpn );
920 * @v ibdev Infiniband device
923 static void linda_destroy_qp ( struct ib_device *ibdev,
924 struct ib_queue_pair *qp ) {
925 struct linda *linda = ib_get_drvdata ( ibdev );
927 linda_destroy_send_wq ( linda, qp );
928 linda_destroy_recv_wq ( linda, qp );
931 /***************************************************************************
933 * Work request operations
935 ***************************************************************************
939 * Post send work queue entry
941 * @v ibdev Infiniband device
943 * @v dest Destination address vector
944 * @v iobuf I/O buffer
945 * @ret rc Return status code
947 static int linda_post_send ( struct ib_device *ibdev,
948 struct ib_queue_pair *qp,
949 struct ib_address_vector *dest,
950 struct io_buffer *iobuf ) {
951 struct linda *linda = ib_get_drvdata ( ibdev );
952 struct ib_work_queue *wq = &qp->send;
953 struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
954 struct QIB_7220_SendPbc sendpbc;
955 uint8_t header_buf[IB_MAX_HEADER_SIZE];
956 struct io_buffer headers;
957 unsigned int send_buf;
958 unsigned long start_offset;
959 unsigned long offset;
964 /* Allocate send buffer and calculate offset */
965 send_buf = linda_alloc_send_buf ( linda );
966 start_offset = offset = linda_send_buffer_offset ( linda, send_buf );
968 /* Store I/O buffer and send buffer index */
969 assert ( wq->iobufs[linda_wq->prod] == NULL );
970 wq->iobufs[linda_wq->prod] = iobuf;
971 linda_wq->send_buf[linda_wq->prod] = send_buf;
973 /* Construct headers */
974 iob_populate ( &headers, header_buf, 0, sizeof ( header_buf ) );
975 iob_reserve ( &headers, sizeof ( header_buf ) );
976 ib_push ( ibdev, &headers, qp, iob_len ( iobuf ), dest );
978 /* Calculate packet length */
979 len = ( ( sizeof ( sendpbc ) + iob_len ( &headers ) +
980 iob_len ( iobuf ) + 3 ) & ~3 );
982 /* Construct send per-buffer control word */
983 memset ( &sendpbc, 0, sizeof ( sendpbc ) );
984 BIT_FILL_2 ( &sendpbc,
985 LengthP1_toibc, ( ( len >> 2 ) - 1 ),
989 DBG_DISABLE ( DBGLVL_IO );
990 linda_writeq ( linda, &sendpbc, offset );
991 offset += sizeof ( sendpbc );
994 for ( data = headers.data, frag_len = iob_len ( &headers ) ;
995 frag_len > 0 ; data++, offset += 4, frag_len -= 4 ) {
996 linda_writel ( linda, *data, offset );
1000 for ( data = iobuf->data, frag_len = iob_len ( iobuf ) ;
1001 frag_len > 0 ; data++, offset += 4, frag_len -= 4 ) {
1002 linda_writel ( linda, *data, offset );
1004 DBG_ENABLE ( DBGLVL_IO );
1006 assert ( ( start_offset + len ) == offset );
1007 DBGC2 ( linda, "Linda %p QPN %ld TX %d(%d) posted [%lx,%lx)\n",
1008 linda, qp->qpn, send_buf, linda_wq->prod,
1009 start_offset, offset );
1011 /* Increment producer counter */
1012 linda_wq->prod = ( ( linda_wq->prod + 1 ) & ( wq->num_wqes - 1 ) );
1018 * Complete send work queue entry
1020 * @v ibdev Infiniband device
1022 * @v wqe_idx Work queue entry index
1024 static void linda_complete_send ( struct ib_device *ibdev,
1025 struct ib_queue_pair *qp,
1026 unsigned int wqe_idx ) {
1027 struct linda *linda = ib_get_drvdata ( ibdev );
1028 struct ib_work_queue *wq = &qp->send;
1029 struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
1030 struct io_buffer *iobuf;
1031 unsigned int send_buf;
1033 /* Parse completion */
1034 send_buf = linda_wq->send_buf[wqe_idx];
1035 DBGC2 ( linda, "Linda %p QPN %ld TX %d(%d) complete\n",
1036 linda, qp->qpn, send_buf, wqe_idx );
1038 /* Complete work queue entry */
1039 iobuf = wq->iobufs[wqe_idx];
1040 assert ( iobuf != NULL );
1041 ib_complete_send ( ibdev, qp, iobuf, 0 );
1042 wq->iobufs[wqe_idx] = NULL;
1044 /* Free send buffer */
1045 linda_free_send_buf ( linda, send_buf );
1049 * Poll send work queue
1051 * @v ibdev Infiniband device
1054 static void linda_poll_send_wq ( struct ib_device *ibdev,
1055 struct ib_queue_pair *qp ) {
1056 struct linda *linda = ib_get_drvdata ( ibdev );
1057 struct ib_work_queue *wq = &qp->send;
1058 struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
1059 unsigned int send_buf;
1061 /* Look for completions */
1062 while ( wq->fill ) {
1064 /* Check to see if send buffer has completed */
1065 send_buf = linda_wq->send_buf[linda_wq->cons];
1066 if ( linda_send_buf_in_use ( linda, send_buf ) )
1069 /* Complete this buffer */
1070 linda_complete_send ( ibdev, qp, linda_wq->cons );
1072 /* Increment consumer counter */
1073 linda_wq->cons = ( ( linda_wq->cons + 1 ) &
1074 ( wq->num_wqes - 1 ) );
1079 * Post receive work queue entry
1081 * @v ibdev Infiniband device
1083 * @v iobuf I/O buffer
1084 * @ret rc Return status code
1086 static int linda_post_recv ( struct ib_device *ibdev,
1087 struct ib_queue_pair *qp,
1088 struct io_buffer *iobuf ) {
1089 struct linda *linda = ib_get_drvdata ( ibdev );
1090 struct ib_work_queue *wq = &qp->recv;
1091 struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
1092 struct QIB_7220_RcvEgr rcvegr;
1093 struct QIB_7220_scalar rcvegrindexhead;
1094 unsigned int ctx = linda_qpn_to_ctx ( qp->qpn );
1097 unsigned int wqe_idx;
1098 unsigned int bufsize;
1101 addr = virt_to_bus ( iobuf->data );
1102 len = iob_tailroom ( iobuf );
1103 if ( addr & ( LINDA_EAGER_BUFFER_ALIGN - 1 ) ) {
1104 DBGC ( linda, "Linda %p QPN %ld misaligned RX buffer "
1105 "(%08lx)\n", linda, qp->qpn, addr );
1108 if ( len != LINDA_RECV_PAYLOAD_SIZE ) {
1109 DBGC ( linda, "Linda %p QPN %ld wrong RX buffer size (%zd)\n",
1110 linda, qp->qpn, len );
1114 /* Calculate eager producer index and WQE index */
1115 wqe_idx = ( linda_wq->eager_prod & ( wq->num_wqes - 1 ) );
1116 assert ( wq->iobufs[wqe_idx] == NULL );
1118 /* Store I/O buffer */
1119 wq->iobufs[wqe_idx] = iobuf;
1121 /* Calculate buffer size */
1122 switch ( LINDA_RECV_PAYLOAD_SIZE ) {
1123 case 2048: bufsize = LINDA_EAGER_BUFFER_2K; break;
1124 case 4096: bufsize = LINDA_EAGER_BUFFER_4K; break;
1125 case 8192: bufsize = LINDA_EAGER_BUFFER_8K; break;
1126 case 16384: bufsize = LINDA_EAGER_BUFFER_16K; break;
1127 case 32768: bufsize = LINDA_EAGER_BUFFER_32K; break;
1128 case 65536: bufsize = LINDA_EAGER_BUFFER_64K; break;
1129 default: linker_assert ( 0, invalid_rx_payload_size );
1130 bufsize = LINDA_EAGER_BUFFER_NONE;
1133 /* Post eager buffer */
1134 memset ( &rcvegr, 0, sizeof ( rcvegr ) );
1135 BIT_FILL_2 ( &rcvegr,
1136 Addr, ( addr >> 11 ),
1138 linda_writeq_array8b ( linda, &rcvegr,
1139 linda_wq->eager_array, linda_wq->eager_prod );
1140 DBGC2 ( linda, "Linda %p QPN %ld RX egr %d(%d) posted [%lx,%lx)\n",
1141 linda, qp->qpn, linda_wq->eager_prod, wqe_idx,
1142 addr, ( addr + len ) );
1144 /* Increment producer index */
1145 linda_wq->eager_prod = ( ( linda_wq->eager_prod + 1 ) &
1146 ( linda_wq->eager_entries - 1 ) );
1148 /* Update head index */
1149 memset ( &rcvegrindexhead, 0, sizeof ( rcvegrindexhead ) );
1150 BIT_FILL_1 ( &rcvegrindexhead,
1151 Value, ( ( linda_wq->eager_prod + 1 ) &
1152 ( linda_wq->eager_entries - 1 ) ) );
1153 linda_writeq_array64k ( linda, &rcvegrindexhead,
1154 QIB_7220_RcvEgrIndexHead0_offset, ctx );
1160 * Complete receive work queue entry
1162 * @v ibdev Infiniband device
1164 * @v header_offs Header offset
1166 static void linda_complete_recv ( struct ib_device *ibdev,
1167 struct ib_queue_pair *qp,
1168 unsigned int header_offs ) {
1169 struct linda *linda = ib_get_drvdata ( ibdev );
1170 struct ib_work_queue *wq = &qp->recv;
1171 struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
1172 struct QIB_7220_RcvHdrFlags *rcvhdrflags;
1173 struct QIB_7220_RcvEgr rcvegr;
1174 struct io_buffer headers;
1175 struct io_buffer *iobuf;
1176 struct ib_queue_pair *intended_qp;
1177 struct ib_address_vector dest;
1178 struct ib_address_vector source;
1179 unsigned int rcvtype;
1180 unsigned int pktlen;
1181 unsigned int egrindex;
1182 unsigned int useegrbfr;
1183 unsigned int iberr, mkerr, tiderr, khdrerr, mtuerr;
1184 unsigned int lenerr, parityerr, vcrcerr, icrcerr;
1186 unsigned int hdrqoffset;
1187 unsigned int header_len;
1188 unsigned int padded_payload_len;
1189 unsigned int wqe_idx;
1194 /* RcvHdrFlags are at the end of the header entry */
1195 rcvhdrflags = ( linda_wq->header + header_offs +
1196 LINDA_RECV_HEADER_SIZE - sizeof ( *rcvhdrflags ) );
1197 rcvtype = BIT_GET ( rcvhdrflags, RcvType );
1198 pktlen = ( BIT_GET ( rcvhdrflags, PktLen ) << 2 );
1199 egrindex = BIT_GET ( rcvhdrflags, EgrIndex );
1200 useegrbfr = BIT_GET ( rcvhdrflags, UseEgrBfr );
1201 hdrqoffset = ( BIT_GET ( rcvhdrflags, HdrqOffset ) << 2 );
1202 iberr = BIT_GET ( rcvhdrflags, IBErr );
1203 mkerr = BIT_GET ( rcvhdrflags, MKErr );
1204 tiderr = BIT_GET ( rcvhdrflags, TIDErr );
1205 khdrerr = BIT_GET ( rcvhdrflags, KHdrErr );
1206 mtuerr = BIT_GET ( rcvhdrflags, MTUErr );
1207 lenerr = BIT_GET ( rcvhdrflags, LenErr );
1208 parityerr = BIT_GET ( rcvhdrflags, ParityErr );
1209 vcrcerr = BIT_GET ( rcvhdrflags, VCRCErr );
1210 icrcerr = BIT_GET ( rcvhdrflags, ICRCErr );
1211 header_len = ( LINDA_RECV_HEADER_SIZE - hdrqoffset -
1212 sizeof ( *rcvhdrflags ) );
1213 padded_payload_len = ( pktlen - header_len - 4 /* ICRC */ );
1214 err = ( iberr | mkerr | tiderr | khdrerr | mtuerr |
1215 lenerr | parityerr | vcrcerr | icrcerr );
1216 /* IB header is placed immediately before RcvHdrFlags */
1217 iob_populate ( &headers, ( ( ( void * ) rcvhdrflags ) - header_len ),
1218 header_len, header_len );
1220 /* Dump diagnostic information */
1221 if ( err || ( ! useegrbfr ) ) {
1222 DBGC ( linda, "Linda %p QPN %ld RX egr %d%s hdr %d type %d "
1223 "len %d(%d+%d+4)%s%s%s%s%s%s%s%s%s%s%s\n", linda,
1224 qp->qpn, egrindex, ( useegrbfr ? "" : "(unused)" ),
1225 ( header_offs / LINDA_RECV_HEADER_SIZE ), rcvtype,
1226 pktlen, header_len, padded_payload_len,
1227 ( err ? " [Err" : "" ), ( iberr ? " IB" : "" ),
1228 ( mkerr ? " MK" : "" ), ( tiderr ? " TID" : "" ),
1229 ( khdrerr ? " KHdr" : "" ), ( mtuerr ? " MTU" : "" ),
1230 ( lenerr ? " Len" : "" ), ( parityerr ? " Parity" : ""),
1231 ( vcrcerr ? " VCRC" : "" ), ( icrcerr ? " ICRC" : "" ),
1232 ( err ? "]" : "" ) );
1234 DBGC2 ( linda, "Linda %p QPN %ld RX egr %d hdr %d type %d "
1235 "len %d(%d+%d+4)\n", linda, qp->qpn, egrindex,
1236 ( header_offs / LINDA_RECV_HEADER_SIZE ), rcvtype,
1237 pktlen, header_len, padded_payload_len );
1239 DBGCP_HDA ( linda, hdrqoffset, headers.data,
1240 ( header_len + sizeof ( *rcvhdrflags ) ) );
1242 /* Parse header to generate address vector */
1243 qp0 = ( qp->qpn == 0 );
1245 if ( ( rc = ib_pull ( ibdev, &headers, ( qp0 ? &intended_qp : NULL ),
1246 &payload_len, &dest, &source ) ) != 0 ) {
1247 DBGC ( linda, "Linda %p could not parse headers: %s\n",
1248 linda, strerror ( rc ) );
1251 if ( ! intended_qp )
1254 /* Complete this buffer and any skipped buffers. Note that
1255 * when the hardware runs out of buffers, it will repeatedly
1256 * report the same buffer (the tail) as a TID error, and that
1257 * it also has a habit of sometimes skipping over several
1262 /* If we have caught up to the producer counter, stop.
1263 * This will happen when the hardware first runs out
1264 * of buffers and starts reporting TID errors against
1265 * the eager buffer it wants to use next.
1267 if ( linda_wq->eager_cons == linda_wq->eager_prod )
1270 /* If we have caught up to where we should be after
1271 * completing this egrindex, stop. We phrase the test
1272 * this way to avoid completing the entire ring when
1273 * we receive the same egrindex twice in a row.
1275 if ( ( linda_wq->eager_cons ==
1276 ( ( egrindex + 1 ) & ( linda_wq->eager_entries - 1 ) )))
1279 /* Identify work queue entry and corresponding I/O
1282 wqe_idx = ( linda_wq->eager_cons & ( wq->num_wqes - 1 ) );
1283 iobuf = wq->iobufs[wqe_idx];
1284 assert ( iobuf != NULL );
1285 wq->iobufs[wqe_idx] = NULL;
1287 /* Complete the eager buffer */
1288 if ( linda_wq->eager_cons == egrindex ) {
1289 /* Completing the eager buffer described in
1290 * this header entry.
1292 iob_put ( iobuf, payload_len );
1293 rc = ( err ? -EIO : ( useegrbfr ? 0 : -ECANCELED ) );
1294 /* Redirect to target QP if necessary */
1295 if ( qp != intended_qp ) {
1296 DBGC ( linda, "Linda %p redirecting QPN %ld "
1298 linda, qp->qpn, intended_qp->qpn );
1299 /* Compensate for incorrect fill levels */
1301 intended_qp->recv.fill++;
1303 ib_complete_recv ( ibdev, intended_qp, &dest, &source,
1306 /* Completing on a skipped-over eager buffer */
1307 ib_complete_recv ( ibdev, qp, &dest, &source, iobuf,
1311 /* Clear eager buffer */
1312 memset ( &rcvegr, 0, sizeof ( rcvegr ) );
1313 linda_writeq_array8b ( linda, &rcvegr, linda_wq->eager_array,
1314 linda_wq->eager_cons );
1316 /* Increment consumer index */
1317 linda_wq->eager_cons = ( ( linda_wq->eager_cons + 1 ) &
1318 ( linda_wq->eager_entries - 1 ) );
1323 * Poll receive work queue
1325 * @v ibdev Infiniband device
1328 static void linda_poll_recv_wq ( struct ib_device *ibdev,
1329 struct ib_queue_pair *qp ) {
1330 struct linda *linda = ib_get_drvdata ( ibdev );
1331 struct ib_work_queue *wq = &qp->recv;
1332 struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
1333 struct QIB_7220_RcvHdrHead0 rcvhdrhead;
1334 unsigned int ctx = linda_qpn_to_ctx ( qp->qpn );
1335 unsigned int header_prod;
1337 /* Check for received packets */
1338 header_prod = ( BIT_GET ( &linda_wq->header_prod, Value ) << 2 );
1339 if ( header_prod == linda_wq->header_cons )
1342 /* Process all received packets */
1343 while ( linda_wq->header_cons != header_prod ) {
1345 /* Complete the receive */
1346 linda_complete_recv ( ibdev, qp, linda_wq->header_cons );
1348 /* Increment the consumer offset */
1349 linda_wq->header_cons += LINDA_RECV_HEADER_SIZE;
1350 linda_wq->header_cons %= LINDA_RECV_HEADERS_SIZE;
1353 /* Update consumer offset */
1354 memset ( &rcvhdrhead, 0, sizeof ( rcvhdrhead ) );
1355 BIT_FILL_2 ( &rcvhdrhead,
1356 RcvHeadPointer, ( linda_wq->header_cons >> 2 ),
1358 linda_writeq_array64k ( linda, &rcvhdrhead,
1359 QIB_7220_RcvHdrHead0_offset, ctx );
1363 * Poll completion queue
1365 * @v ibdev Infiniband device
1366 * @v cq Completion queue
1368 static void linda_poll_cq ( struct ib_device *ibdev,
1369 struct ib_completion_queue *cq ) {
1370 struct ib_work_queue *wq;
1372 /* Poll associated send and receive queues */
1373 list_for_each_entry ( wq, &cq->work_queues, list ) {
1374 if ( wq->is_send ) {
1375 linda_poll_send_wq ( ibdev, wq->qp );
1377 linda_poll_recv_wq ( ibdev, wq->qp );
1382 /***************************************************************************
1386 ***************************************************************************
1392 * @v ibdev Infiniband device
1394 static void linda_poll_eq ( struct ib_device *ibdev ) {
1395 struct linda *linda = ib_get_drvdata ( ibdev );
1396 struct QIB_7220_ErrStatus errstatus;
1397 struct QIB_7220_ErrClear errclear;
1399 /* Check for link status changes */
1400 DBG_DISABLE ( DBGLVL_IO );
1401 linda_readq ( linda, &errstatus, QIB_7220_ErrStatus_offset );
1402 DBG_ENABLE ( DBGLVL_IO );
1403 if ( BIT_GET ( &errstatus, IBStatusChanged ) ) {
1404 linda_link_state_changed ( ibdev );
1405 memset ( &errclear, 0, sizeof ( errclear ) );
1406 BIT_FILL_1 ( &errclear, IBStatusChangedClear, 1 );
1407 linda_writeq ( linda, &errclear, QIB_7220_ErrClear_offset );
1411 /***************************************************************************
1413 * Infiniband link-layer operations
1415 ***************************************************************************
1419 * Initialise Infiniband link
1421 * @v ibdev Infiniband device
1422 * @ret rc Return status code
1424 static int linda_open ( struct ib_device *ibdev ) {
1425 struct linda *linda = ib_get_drvdata ( ibdev );
1426 struct QIB_7220_Control control;
1429 linda_readq ( linda, &control, QIB_7220_Control_offset );
1430 BIT_SET ( &control, LinkEn, 1 );
1431 linda_writeq ( linda, &control, QIB_7220_Control_offset );
1436 * Close Infiniband link
1438 * @v ibdev Infiniband device
1440 static void linda_close ( struct ib_device *ibdev ) {
1441 struct linda *linda = ib_get_drvdata ( ibdev );
1442 struct QIB_7220_Control control;
1445 linda_readq ( linda, &control, QIB_7220_Control_offset );
1446 BIT_SET ( &control, LinkEn, 0 );
1447 linda_writeq ( linda, &control, QIB_7220_Control_offset );
1450 /***************************************************************************
1452 * Multicast group operations
1454 ***************************************************************************
1458 * Attach to multicast group
1460 * @v ibdev Infiniband device
1462 * @v gid Multicast GID
1463 * @ret rc Return status code
1465 static int linda_mcast_attach ( struct ib_device *ibdev,
1466 struct ib_queue_pair *qp,
1467 union ib_gid *gid ) {
1468 struct linda *linda = ib_get_drvdata ( ibdev );
1477 * Detach from multicast group
1479 * @v ibdev Infiniband device
1481 * @v gid Multicast GID
1483 static void linda_mcast_detach ( struct ib_device *ibdev,
1484 struct ib_queue_pair *qp,
1485 union ib_gid *gid ) {
1486 struct linda *linda = ib_get_drvdata ( ibdev );
1493 /** Linda Infiniband operations */
1494 static struct ib_device_operations linda_ib_operations = {
1495 .create_cq = linda_create_cq,
1496 .destroy_cq = linda_destroy_cq,
1497 .create_qp = linda_create_qp,
1498 .modify_qp = linda_modify_qp,
1499 .destroy_qp = linda_destroy_qp,
1500 .post_send = linda_post_send,
1501 .post_recv = linda_post_recv,
1502 .poll_cq = linda_poll_cq,
1503 .poll_eq = linda_poll_eq,
1505 .close = linda_close,
1506 .mcast_attach = linda_mcast_attach,
1507 .mcast_detach = linda_mcast_detach,
1508 .set_port_info = linda_set_port_info,
1509 .set_pkey_table = linda_set_pkey_table,
1512 /***************************************************************************
1514 * I2C bus operations
1516 ***************************************************************************
1519 /** Linda I2C bit to GPIO mappings */
1520 static unsigned int linda_i2c_bits[] = {
1521 [I2C_BIT_SCL] = ( 1 << LINDA_GPIO_SCL ),
1522 [I2C_BIT_SDA] = ( 1 << LINDA_GPIO_SDA ),
1526 * Read Linda I2C line status
1528 * @v basher Bit-bashing interface
1529 * @v bit_id Bit number
1530 * @ret zero Input is a logic 0
1531 * @ret non-zero Input is a logic 1
1533 static int linda_i2c_read_bit ( struct bit_basher *basher,
1534 unsigned int bit_id ) {
1535 struct linda *linda =
1536 container_of ( basher, struct linda, i2c.basher );
1537 struct QIB_7220_EXTStatus extstatus;
1538 unsigned int status;
1540 DBG_DISABLE ( DBGLVL_IO );
1542 linda_readq ( linda, &extstatus, QIB_7220_EXTStatus_offset );
1543 status = ( BIT_GET ( &extstatus, GPIOIn ) & linda_i2c_bits[bit_id] );
1545 DBG_ENABLE ( DBGLVL_IO );
1551 * Write Linda I2C line status
1553 * @v basher Bit-bashing interface
1554 * @v bit_id Bit number
1555 * @v data Value to write
1557 static void linda_i2c_write_bit ( struct bit_basher *basher,
1558 unsigned int bit_id, unsigned long data ) {
1559 struct linda *linda =
1560 container_of ( basher, struct linda, i2c.basher );
1561 struct QIB_7220_EXTCtrl extctrl;
1562 struct QIB_7220_GPIO gpioout;
1563 unsigned int bit = linda_i2c_bits[bit_id];
1564 unsigned int outputs = 0;
1565 unsigned int output_enables = 0;
1567 DBG_DISABLE ( DBGLVL_IO );
1569 /* Read current GPIO mask and outputs */
1570 linda_readq ( linda, &extctrl, QIB_7220_EXTCtrl_offset );
1571 linda_readq ( linda, &gpioout, QIB_7220_GPIOOut_offset );
1573 /* Update outputs and output enables. I2C lines are tied
1574 * high, so we always set the output to 0 and use the output
1575 * enable to control the line.
1577 output_enables = BIT_GET ( &extctrl, GPIOOe );
1578 output_enables = ( ( output_enables & ~bit ) | ( ~data & bit ) );
1579 outputs = BIT_GET ( &gpioout, GPIO );
1580 outputs = ( outputs & ~bit );
1581 BIT_SET ( &extctrl, GPIOOe, output_enables );
1582 BIT_SET ( &gpioout, GPIO, outputs );
1584 /* Write the output enable first; that way we avoid logic
1587 linda_writeq ( linda, &extctrl, QIB_7220_EXTCtrl_offset );
1588 linda_writeq ( linda, &gpioout, QIB_7220_GPIOOut_offset );
1591 DBG_ENABLE ( DBGLVL_IO );
1594 /** Linda I2C bit-bashing interface operations */
1595 static struct bit_basher_operations linda_i2c_basher_ops = {
1596 .read = linda_i2c_read_bit,
1597 .write = linda_i2c_write_bit,
1601 * Initialise Linda I2C subsystem
1603 * @v linda Linda device
1604 * @ret rc Return status code
1606 static int linda_init_i2c ( struct linda *linda ) {
1607 static int try_eeprom_address[] = { 0x51, 0x50 };
1611 /* Initialise bus */
1612 if ( ( rc = init_i2c_bit_basher ( &linda->i2c,
1613 &linda_i2c_basher_ops ) ) != 0 ) {
1614 DBGC ( linda, "Linda %p could not initialise I2C bus: %s\n",
1615 linda, strerror ( rc ) );
1619 /* Probe for devices */
1620 for ( i = 0 ; i < ( sizeof ( try_eeprom_address ) /
1621 sizeof ( try_eeprom_address[0] ) ) ; i++ ) {
1622 init_i2c_eeprom ( &linda->eeprom, try_eeprom_address[i] );
1623 if ( ( rc = i2c_check_presence ( &linda->i2c.i2c,
1624 &linda->eeprom ) ) == 0 ) {
1625 DBGC2 ( linda, "Linda %p found EEPROM at %02x\n",
1626 linda, try_eeprom_address[i] );
1631 DBGC ( linda, "Linda %p could not find EEPROM\n", linda );
1636 * Read EEPROM parameters
1638 * @v linda Linda device
1639 * @v guid GUID to fill in
1640 * @ret rc Return status code
1642 static int linda_read_eeprom ( struct linda *linda, union ib_guid *guid ) {
1643 struct i2c_interface *i2c = &linda->i2c.i2c;
1647 if ( ( rc = i2c->read ( i2c, &linda->eeprom, LINDA_EEPROM_GUID_OFFSET,
1648 guid->bytes, sizeof ( *guid ) ) ) != 0 ) {
1649 DBGC ( linda, "Linda %p could not read GUID: %s\n",
1650 linda, strerror ( rc ) );
1653 DBGC2 ( linda, "Linda %p has GUID " IB_GUID_FMT "\n",
1654 linda, IB_GUID_ARGS ( guid ) );
1656 /* Read serial number (debug only) */
1658 uint8_t serial[LINDA_EEPROM_SERIAL_SIZE + 1];
1660 serial[ sizeof ( serial ) - 1 ] = '\0';
1661 if ( ( rc = i2c->read ( i2c, &linda->eeprom,
1662 LINDA_EEPROM_SERIAL_OFFSET, serial,
1663 ( sizeof ( serial ) - 1 ) ) ) != 0 ) {
1664 DBGC ( linda, "Linda %p could not read serial: %s\n",
1665 linda, strerror ( rc ) );
1668 DBGC2 ( linda, "Linda %p has serial number \"%s\"\n",
1675 /***************************************************************************
1677 * External parallel bus access
1679 ***************************************************************************
1683 * Request ownership of the IB external parallel bus
1685 * @v linda Linda device
1686 * @ret rc Return status code
1688 static int linda_ib_epb_request ( struct linda *linda ) {
1689 struct QIB_7220_ibsd_epb_access_ctrl access;
1692 /* Request ownership */
1693 memset ( &access, 0, sizeof ( access ) );
1694 BIT_FILL_1 ( &access, sw_ib_epb_req, 1 );
1695 linda_writeq ( linda, &access, QIB_7220_ibsd_epb_access_ctrl_offset );
1697 /* Wait for ownership to be granted */
1698 for ( i = 0 ; i < LINDA_EPB_REQUEST_MAX_WAIT_US ; i++ ) {
1699 linda_readq ( linda, &access,
1700 QIB_7220_ibsd_epb_access_ctrl_offset );
1701 if ( BIT_GET ( &access, sw_ib_epb_req_granted ) )
1706 DBGC ( linda, "Linda %p timed out waiting for IB EPB request\n",
1712 * Wait for IB external parallel bus transaction to complete
1714 * @v linda Linda device
1715 * @v xact Buffer to hold transaction result
1716 * @ret rc Return status code
1718 static int linda_ib_epb_wait ( struct linda *linda,
1719 struct QIB_7220_ibsd_epb_transaction_reg *xact ) {
1722 /* Discard first read to allow for signals crossing clock domains */
1723 linda_readq ( linda, xact, QIB_7220_ibsd_epb_transaction_reg_offset );
1725 for ( i = 0 ; i < LINDA_EPB_XACT_MAX_WAIT_US ; i++ ) {
1726 linda_readq ( linda, xact,
1727 QIB_7220_ibsd_epb_transaction_reg_offset );
1728 if ( BIT_GET ( xact, ib_epb_rdy ) ) {
1729 if ( BIT_GET ( xact, ib_epb_req_error ) ) {
1730 DBGC ( linda, "Linda %p EPB transaction "
1731 "failed\n", linda );
1740 DBGC ( linda, "Linda %p timed out waiting for IB EPB transaction\n",
1746 * Release ownership of the IB external parallel bus
1748 * @v linda Linda device
1750 static void linda_ib_epb_release ( struct linda *linda ) {
1751 struct QIB_7220_ibsd_epb_access_ctrl access;
1753 memset ( &access, 0, sizeof ( access ) );
1754 BIT_FILL_1 ( &access, sw_ib_epb_req, 0 );
1755 linda_writeq ( linda, &access, QIB_7220_ibsd_epb_access_ctrl_offset );
1759 * Read data via IB external parallel bus
1761 * @v linda Linda device
1762 * @v location EPB location
1763 * @ret data Data read, or negative error
1765 * You must have already acquired ownership of the IB external
1768 static int linda_ib_epb_read ( struct linda *linda, unsigned int location ) {
1769 struct QIB_7220_ibsd_epb_transaction_reg xact;
1773 /* Ensure no transaction is currently in progress */
1774 if ( ( rc = linda_ib_epb_wait ( linda, &xact ) ) != 0 )
1778 memset ( &xact, 0, sizeof ( xact ) );
1780 ib_epb_address, LINDA_EPB_LOC_ADDRESS ( location ),
1781 ib_epb_read_write, LINDA_EPB_READ,
1782 ib_epb_cs, LINDA_EPB_LOC_CS ( location ) );
1783 linda_writeq ( linda, &xact,
1784 QIB_7220_ibsd_epb_transaction_reg_offset );
1786 /* Wait for transaction to complete */
1787 if ( ( rc = linda_ib_epb_wait ( linda, &xact ) ) != 0 )
1790 data = BIT_GET ( &xact, ib_epb_data );
1795 * Write data via IB external parallel bus
1797 * @v linda Linda device
1798 * @v location EPB location
1799 * @v data Data to write
1800 * @ret rc Return status code
1802 * You must have already acquired ownership of the IB external
1805 static int linda_ib_epb_write ( struct linda *linda, unsigned int location,
1806 unsigned int data ) {
1807 struct QIB_7220_ibsd_epb_transaction_reg xact;
1810 /* Ensure no transaction is currently in progress */
1811 if ( ( rc = linda_ib_epb_wait ( linda, &xact ) ) != 0 )
1815 memset ( &xact, 0, sizeof ( xact ) );
1818 ib_epb_address, LINDA_EPB_LOC_ADDRESS ( location ),
1819 ib_epb_read_write, LINDA_EPB_WRITE,
1820 ib_epb_cs, LINDA_EPB_LOC_CS ( location ) );
1821 linda_writeq ( linda, &xact,
1822 QIB_7220_ibsd_epb_transaction_reg_offset );
1824 /* Wait for transaction to complete */
1825 if ( ( rc = linda_ib_epb_wait ( linda, &xact ) ) != 0 )
1832 * Read/modify/write EPB register
1834 * @v linda Linda device
1836 * @v channel Channel
1837 * @v element Element
1839 * @v value Value to set
1840 * @v mask Mask to apply to old value
1841 * @ret rc Return status code
1843 static int linda_ib_epb_mod_reg ( struct linda *linda, unsigned int cs,
1844 unsigned int channel, unsigned int element,
1845 unsigned int reg, unsigned int value,
1846 unsigned int mask ) {
1847 unsigned int location;
1851 DBG_DISABLE ( DBGLVL_IO );
1854 assert ( ( value & mask ) == value );
1856 /* Acquire bus ownership */
1857 if ( ( rc = linda_ib_epb_request ( linda ) ) != 0 )
1860 /* Read existing value, if necessary */
1861 location = LINDA_EPB_LOC ( cs, channel, element, reg );
1862 if ( (~mask) & 0xff ) {
1863 old_value = linda_ib_epb_read ( linda, location );
1864 if ( old_value < 0 ) {
1873 value = ( ( old_value & ~mask ) | value );
1874 DBGCP ( linda, "Linda %p CS %d EPB(%d,%d,%#02x) %#02x => %#02x\n",
1875 linda, cs, channel, element, reg, old_value, value );
1876 if ( ( rc = linda_ib_epb_write ( linda, location, value ) ) != 0 )
1881 linda_ib_epb_release ( linda );
1883 DBG_ENABLE ( DBGLVL_IO );
1888 * Transfer data to/from microcontroller RAM
1890 * @v linda Linda device
1891 * @v address Starting address
1892 * @v write Data to write, or NULL
1893 * @v read Data to read, or NULL
1894 * @v len Length of data
1895 * @ret rc Return status code
1897 static int linda_ib_epb_ram_xfer ( struct linda *linda, unsigned int address,
1898 const void *write, void *read,
1900 unsigned int control;
1901 unsigned int address_hi;
1902 unsigned int address_lo;
1906 DBG_DISABLE ( DBGLVL_IO );
1908 assert ( ! ( write && read ) );
1909 assert ( ( address % LINDA_EPB_UC_CHUNK_SIZE ) == 0 );
1910 assert ( ( len % LINDA_EPB_UC_CHUNK_SIZE ) == 0 );
1912 /* Acquire bus ownership */
1913 if ( ( rc = linda_ib_epb_request ( linda ) ) != 0 )
1919 /* Reset the address for each new chunk */
1920 if ( ( address % LINDA_EPB_UC_CHUNK_SIZE ) == 0 ) {
1922 /* Write the control register */
1923 control = ( read ? LINDA_EPB_UC_CTL_READ :
1924 LINDA_EPB_UC_CTL_WRITE );
1925 if ( ( rc = linda_ib_epb_write ( linda,
1930 /* Write the address registers */
1931 address_hi = ( address >> 8 );
1932 if ( ( rc = linda_ib_epb_write ( linda,
1933 LINDA_EPB_UC_ADDR_HI,
1934 address_hi ) ) != 0 )
1936 address_lo = ( address & 0xff );
1937 if ( ( rc = linda_ib_epb_write ( linda,
1938 LINDA_EPB_UC_ADDR_LO,
1939 address_lo ) ) != 0 )
1943 /* Read or write the data */
1945 data = linda_ib_epb_read ( linda, LINDA_EPB_UC_DATA );
1950 *( ( uint8_t * ) read++ ) = data;
1952 data = *( ( uint8_t * ) write++ );
1953 if ( ( rc = linda_ib_epb_write ( linda,
1961 /* Reset the control byte after each chunk */
1962 if ( ( address % LINDA_EPB_UC_CHUNK_SIZE ) == 0 ) {
1963 if ( ( rc = linda_ib_epb_write ( linda,
1971 linda_ib_epb_release ( linda );
1974 DBG_ENABLE ( DBGLVL_IO );
1978 /***************************************************************************
1980 * Infiniband SerDes initialisation
1982 ***************************************************************************
1985 /** A Linda SerDes parameter */
1986 struct linda_serdes_param {
1987 /** EPB address as constructed by LINDA_EPB_ADDRESS() */
1991 /** Mask to apply to old value */
1995 /** Magic "all channels" channel number */
1996 #define LINDA_EPB_ALL_CHANNELS 31
1998 /** End of SerDes parameter list marker */
1999 #define LINDA_SERDES_PARAM_END { 0, 0, 0 }
2002 * Program IB SerDes register(s)
2004 * @v linda Linda device
2005 * @v param SerDes parameter
2006 * @ret rc Return status code
2008 static int linda_set_serdes_param ( struct linda *linda,
2009 struct linda_serdes_param *param ) {
2010 unsigned int channel;
2011 unsigned int channel_start;
2012 unsigned int channel_end;
2013 unsigned int element;
2017 /* Break down the EPB address and determine channels */
2018 channel = LINDA_EPB_ADDRESS_CHANNEL ( param->address );
2019 element = LINDA_EPB_ADDRESS_ELEMENT ( param->address );
2020 reg = LINDA_EPB_ADDRESS_REG ( param->address );
2021 if ( channel == LINDA_EPB_ALL_CHANNELS ) {
2025 channel_start = channel_end = channel;
2028 /* Modify register for each specified channel */
2029 for ( channel = channel_start ; channel <= channel_end ; channel++ ) {
2030 if ( ( rc = linda_ib_epb_mod_reg ( linda, LINDA_EPB_CS_SERDES,
2031 channel, element, reg,
2033 param->mask ) ) != 0 )
2041 * Program IB SerDes registers
2043 * @v linda Linda device
2044 * @v param SerDes parameters
2045 * @v count Number of parameters
2046 * @ret rc Return status code
2048 static int linda_set_serdes_params ( struct linda *linda,
2049 struct linda_serdes_param *params ) {
2052 for ( ; params->mask != 0 ; params++ ){
2053 if ( ( rc = linda_set_serdes_param ( linda,
2061 #define LINDA_DDS_VAL( amp_d, main_d, ipst_d, ipre_d, \
2062 amp_s, main_s, ipst_s, ipre_s ) \
2063 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x00 ), \
2064 ( ( ( amp_d & 0x1f ) << 1 ) | 1 ), 0xff }, \
2065 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x01 ), \
2066 ( ( ( amp_s & 0x1f ) << 1 ) | 1 ), 0xff }, \
2067 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x09 ), \
2068 ( ( main_d << 3 ) | 4 | ( ipre_d >> 2 ) ), 0xff }, \
2069 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x0a ), \
2070 ( ( main_s << 3 ) | 4 | ( ipre_s >> 2 ) ), 0xff }, \
2071 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x06 ), \
2072 ( ( ( ipst_d & 0xf ) << 1 ) | \
2073 ( ( ipre_d & 3 ) << 6 ) | 0x21 ), 0xff }, \
2074 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x07 ), \
2075 ( ( ( ipst_s & 0xf ) << 1 ) | \
2076 ( ( ipre_s & 3 ) << 6) | 0x21 ), 0xff }
2079 * Linda SerDes default parameters
2081 * These magic start-of-day values are taken from the Linux driver.
2083 static struct linda_serdes_param linda_serdes_defaults1[] = {
2085 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x00 ), 0xd4, 0xff },
2087 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x05 ), 0x2d, 0xff },
2089 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x08 ), 0x03, 0x0f },
2091 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x27 ), 0x10, 0xff },
2093 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x28 ), 0x30, 0xff },
2095 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x0e ), 0x40, 0xff },
2097 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x06 ), 0x04, 0xff },
2099 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x0f ), 0x04, 0xff },
2100 /* End of this block */
2101 LINDA_SERDES_PARAM_END
2103 static struct linda_serdes_param linda_serdes_defaults2[] = {
2105 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x06 ), 0x00, 0xff },
2107 LINDA_DDS_VAL ( 31, 19, 12, 0, 29, 22, 9, 0 ),
2108 /* Set Rcv Eq. to Preset node */
2109 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x27 ), 0x10, 0xff },
2111 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x08 ), 0x00, 0xff },
2113 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x21 ), 0x00, 0xff },
2115 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x09 ), 0x02, 0xff },
2117 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x23 ), 0x02, 0xff },
2119 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x1b ), 0x0c, 0xff },
2121 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x1c ), 0x0c, 0xff },
2123 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x1e ), 0x10, 0xff },
2125 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x1f ), 0x10, 0xff },
2126 /* VCDL_CTRL0 toggle */
2127 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x06 ), 0x20, 0xff },
2128 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x06 ), 0x00, 0xff },
2130 { LINDA_EPB_ADDRESS ( 7, 0, 0x15 ), 0x80, 0xff },
2131 /* End of this block */
2132 LINDA_SERDES_PARAM_END
2134 static struct linda_serdes_param linda_serdes_defaults3[] = {
2136 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x27 ), 0x00, 0x38 },
2137 /* End of this block */
2138 LINDA_SERDES_PARAM_END
2142 * Program the microcontroller RAM
2144 * @v linda Linda device
2145 * @ret rc Return status code
2147 static int linda_program_uc_ram ( struct linda *linda ) {
2150 if ( ( rc = linda_ib_epb_ram_xfer ( linda, 0, linda_ib_fw, NULL,
2151 sizeof ( linda_ib_fw ) ) ) != 0 ){
2152 DBGC ( linda, "Linda %p could not load IB firmware: %s\n",
2153 linda, strerror ( rc ) );
2161 * Verify the microcontroller RAM
2163 * @v linda Linda device
2164 * @ret rc Return status code
2166 static int linda_verify_uc_ram ( struct linda *linda ) {
2167 uint8_t verify[LINDA_EPB_UC_CHUNK_SIZE];
2168 unsigned int offset;
2171 for ( offset = 0 ; offset < sizeof ( linda_ib_fw );
2172 offset += sizeof ( verify ) ) {
2173 if ( ( rc = linda_ib_epb_ram_xfer ( linda, offset,
2175 sizeof (verify) )) != 0 ){
2176 DBGC ( linda, "Linda %p could not read back IB "
2177 "firmware: %s\n", linda, strerror ( rc ) );
2180 if ( memcmp ( ( linda_ib_fw + offset ), verify,
2181 sizeof ( verify ) ) != 0 ) {
2182 DBGC ( linda, "Linda %p firmware verification failed "
2183 "at offset %#x\n", linda, offset );
2184 DBGC_HDA ( linda, offset, ( linda_ib_fw + offset ),
2185 sizeof ( verify ) );
2186 DBGC_HDA ( linda, offset, verify, sizeof ( verify ) );
2191 DBGC2 ( linda, "Linda %p firmware verified ok\n", linda );
2196 * Use the microcontroller to trim the IB link
2198 * @v linda Linda device
2199 * @ret rc Return status code
2201 static int linda_trim_ib ( struct linda *linda ) {
2202 struct QIB_7220_IBSerDesCtrl ctrl;
2203 struct QIB_7220_IntStatus intstatus;
2207 /* Bring the microcontroller out of reset */
2208 linda_readq ( linda, &ctrl, QIB_7220_IBSerDesCtrl_offset );
2209 BIT_SET ( &ctrl, ResetIB_uC_Core, 0 );
2210 linda_writeq ( linda, &ctrl, QIB_7220_IBSerDesCtrl_offset );
2212 /* Wait for the "trim done" signal */
2213 for ( i = 0 ; i < LINDA_TRIM_DONE_MAX_WAIT_MS ; i++ ) {
2214 linda_readq ( linda, &intstatus, QIB_7220_IntStatus_offset );
2215 if ( BIT_GET ( &intstatus, IBSerdesTrimDone ) ) {
2222 DBGC ( linda, "Linda %p timed out waiting for trim done\n", linda );
2225 /* Put the microcontroller back into reset */
2226 BIT_SET ( &ctrl, ResetIB_uC_Core, 1 );
2227 linda_writeq ( linda, &ctrl, QIB_7220_IBSerDesCtrl_offset );
2233 * Initialise the IB SerDes
2235 * @v linda Linda device
2236 * @ret rc Return status code
2238 static int linda_init_ib_serdes ( struct linda *linda ) {
2239 struct QIB_7220_Control control;
2240 struct QIB_7220_IBCCtrl ibcctrl;
2241 struct QIB_7220_IBCDDRCtrl ibcddrctrl;
2242 struct QIB_7220_XGXSCfg xgxscfg;
2246 linda_readq ( linda, &control, QIB_7220_Control_offset );
2247 BIT_SET ( &control, LinkEn, 0 );
2248 linda_writeq ( linda, &control, QIB_7220_Control_offset );
2250 /* Configure sensible defaults for IBC */
2251 memset ( &ibcctrl, 0, sizeof ( ibcctrl ) );
2252 BIT_FILL_6 ( &ibcctrl, /* Tuning values taken from Linux driver */
2253 FlowCtrlPeriod, 0x03,
2254 FlowCtrlWaterMark, 0x05,
2255 MaxPktLen, ( ( LINDA_RECV_HEADER_SIZE +
2256 LINDA_RECV_PAYLOAD_SIZE +
2257 4 /* ICRC */ ) >> 2 ),
2258 PhyerrThreshold, 0xf,
2259 OverrunThreshold, 0xf,
2261 linda_writeq ( linda, &ibcctrl, QIB_7220_IBCCtrl_offset );
2263 /* Force SDR only to avoid needing all the DDR tuning,
2264 * Mellanox compatibility hacks etc. SDR is plenty for
2265 * boot-time operation.
2267 linda_readq ( linda, &ibcddrctrl, QIB_7220_IBCDDRCtrl_offset );
2268 BIT_SET ( &ibcddrctrl, IB_ENHANCED_MODE, 0 );
2269 BIT_SET ( &ibcddrctrl, SD_SPEED_SDR, 1 );
2270 BIT_SET ( &ibcddrctrl, SD_SPEED_DDR, 0 );
2271 BIT_SET ( &ibcddrctrl, SD_SPEED_QDR, 0 );
2272 BIT_SET ( &ibcddrctrl, HRTBT_ENB, 0 );
2273 BIT_SET ( &ibcddrctrl, HRTBT_AUTO, 0 );
2274 linda_writeq ( linda, &ibcddrctrl, QIB_7220_IBCDDRCtrl_offset );
2276 /* Set default SerDes parameters */
2277 if ( ( rc = linda_set_serdes_params ( linda,
2278 linda_serdes_defaults1 ) ) != 0 )
2280 udelay ( 415 ); /* Magic delay while SerDes sorts itself out */
2281 if ( ( rc = linda_set_serdes_params ( linda,
2282 linda_serdes_defaults2 ) ) != 0 )
2285 /* Program the microcontroller RAM */
2286 if ( ( rc = linda_program_uc_ram ( linda ) ) != 0 )
2289 /* Verify the microcontroller RAM contents */
2291 if ( ( rc = linda_verify_uc_ram ( linda ) ) != 0 )
2295 /* More SerDes tuning */
2296 if ( ( rc = linda_set_serdes_params ( linda,
2297 linda_serdes_defaults3 ) ) != 0 )
2300 /* Use the microcontroller to trim the IB link */
2301 if ( ( rc = linda_trim_ib ( linda ) ) != 0 )
2304 /* Bring XGXS out of reset */
2305 linda_readq ( linda, &xgxscfg, QIB_7220_XGXSCfg_offset );
2306 BIT_SET ( &xgxscfg, tx_rx_reset, 0 );
2307 BIT_SET ( &xgxscfg, xcv_reset, 0 );
2308 linda_writeq ( linda, &xgxscfg, QIB_7220_XGXSCfg_offset );
2313 /***************************************************************************
2315 * PCI layer interface
2317 ***************************************************************************
2325 * @ret rc Return status code
2327 static int linda_probe ( struct pci_device *pci ) {
2328 struct ib_device *ibdev;
2329 struct linda *linda;
2330 struct QIB_7220_Revision revision;
2333 /* Allocate Infiniband device */
2334 ibdev = alloc_ibdev ( sizeof ( *linda ) );
2337 goto err_alloc_ibdev;
2339 pci_set_drvdata ( pci, ibdev );
2340 linda = ib_get_drvdata ( ibdev );
2341 ibdev->op = &linda_ib_operations;
2342 ibdev->dev = &pci->dev;
2345 /* Fix up PCI device */
2346 adjust_pci_device ( pci );
2349 linda->regs = ioremap ( pci->membase, LINDA_BAR0_SIZE );
2350 DBGC2 ( linda, "Linda %p has BAR at %08lx\n", linda, pci->membase );
2352 /* Print some general data */
2353 linda_readq ( linda, &revision, QIB_7220_Revision_offset );
2354 DBGC2 ( linda, "Linda %p board %02lx v%ld.%ld.%ld.%ld\n", linda,
2355 BIT_GET ( &revision, BoardID ),
2356 BIT_GET ( &revision, R_SW ),
2357 BIT_GET ( &revision, R_Arch ),
2358 BIT_GET ( &revision, R_ChipRevMajor ),
2359 BIT_GET ( &revision, R_ChipRevMinor ) );
2361 /* Record link capabilities. Note that we force SDR only to
2362 * avoid having to carry extra code for DDR tuning etc.
2364 ibdev->link_width_enabled = ibdev->link_width_supported =
2365 ( IB_LINK_WIDTH_4X | IB_LINK_WIDTH_1X );
2366 ibdev->link_speed_enabled = ibdev->link_speed_supported =
2369 /* Initialise I2C subsystem */
2370 if ( ( rc = linda_init_i2c ( linda ) ) != 0 )
2373 /* Read EEPROM parameters */
2374 if ( ( rc = linda_read_eeprom ( linda, &ibdev->node_guid ) ) != 0 )
2375 goto err_read_eeprom;
2376 memcpy ( &ibdev->gid.s.guid, &ibdev->node_guid,
2377 sizeof ( ibdev->gid.s.guid ) );
2379 /* Initialise send datapath */
2380 if ( ( rc = linda_init_send ( linda ) ) != 0 )
2383 /* Initialise receive datapath */
2384 if ( ( rc = linda_init_recv ( linda ) ) != 0 )
2387 /* Initialise the IB SerDes */
2388 if ( ( rc = linda_init_ib_serdes ( linda ) ) != 0 )
2389 goto err_init_ib_serdes;
2391 /* Register Infiniband device */
2392 if ( ( rc = register_ibdev ( ibdev ) ) != 0 ) {
2393 DBGC ( linda, "Linda %p could not register IB "
2394 "device: %s\n", linda, strerror ( rc ) );
2395 goto err_register_ibdev;
2400 unregister_ibdev ( ibdev );
2402 linda_fini_recv ( linda );
2404 linda_fini_send ( linda );
2409 ibdev_put ( ibdev );
2419 static void linda_remove ( struct pci_device *pci ) {
2420 struct ib_device *ibdev = pci_get_drvdata ( pci );
2421 struct linda *linda = ib_get_drvdata ( ibdev );
2423 unregister_ibdev ( ibdev );
2424 linda_fini_recv ( linda );
2425 linda_fini_send ( linda );
2426 ibdev_put ( ibdev );
2429 static struct pci_device_id linda_nics[] = {
2430 PCI_ROM ( 0x1077, 0x7220, "iba7220", "QLE7240/7280 HCA driver", 0 ),
2433 struct pci_driver linda_driver __pci_driver = {
2435 .id_count = ( sizeof ( linda_nics ) / sizeof ( linda_nics[0] ) ),
2436 .probe = linda_probe,
2437 .remove = linda_remove,