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
20 FILE_LICENCE ( GPL2_OR_LATER );
29 #include <ipxe/infiniband.h>
31 #include <ipxe/bitbash.h>
32 #include <ipxe/malloc.h>
33 #include <ipxe/iobuf.h>
39 * QLogic Linda Infiniband HCA
43 /** A Linda send work queue */
44 struct linda_send_work_queue {
45 /** Send buffer usage */
53 /** A Linda receive work queue */
54 struct linda_recv_work_queue {
55 /** Receive header ring */
57 /** Receive header producer offset (written by hardware) */
58 struct QIB_7220_scalar header_prod;
59 /** Receive header consumer offset */
60 unsigned int header_cons;
61 /** Offset within register space of the eager array */
62 unsigned long eager_array;
63 /** Number of entries in eager array */
64 unsigned int eager_entries;
65 /** Eager array producer index */
66 unsigned int eager_prod;
67 /** Eager array consumer index */
68 unsigned int eager_cons;
76 /** In-use contexts */
77 uint8_t used_ctx[LINDA_NUM_CONTEXTS];
78 /** Send work queues */
79 struct linda_send_work_queue send_wq[LINDA_NUM_CONTEXTS];
80 /** Receive work queues */
81 struct linda_recv_work_queue recv_wq[LINDA_NUM_CONTEXTS];
83 /** Offset within register space of the first send buffer */
84 unsigned long send_buffer_base;
85 /** Send buffer availability (reported by hardware) */
86 struct QIB_7220_SendBufAvail *sendbufavail;
87 /** Send buffer availability (maintained by software) */
88 uint8_t send_buf[LINDA_MAX_SEND_BUFS];
89 /** Send buffer availability producer counter */
90 unsigned int send_buf_prod;
91 /** Send buffer availability consumer counter */
92 unsigned int send_buf_cons;
93 /** Number of reserved send buffers (across all QPs) */
94 unsigned int reserved_send_bufs;
96 /** I2C bit-bashing interface */
97 struct i2c_bit_basher i2c;
98 /** I2C serial EEPROM */
99 struct i2c_device eeprom;
102 /***************************************************************************
104 * Linda register access
106 ***************************************************************************
108 * This card requires atomic 64-bit accesses. Strange things happen
109 * if you try to use 32-bit accesses; sometimes they work, sometimes
110 * they don't, sometimes you get random data.
112 * These accessors use the "movq" MMX instruction, and so won't work
113 * on really old Pentiums (which won't have PCIe anyway, so this is
114 * something of a moot point).
118 * Read Linda qword register
120 * @v linda Linda device
121 * @v dwords Register buffer to read into
122 * @v offset Register offset
124 static void linda_readq ( struct linda *linda, uint32_t *dwords,
125 unsigned long offset ) {
126 void *addr = ( linda->regs + offset );
128 __asm__ __volatile__ ( "movq (%1), %%mm0\n\t"
129 "movq %%mm0, (%0)\n\t"
130 : : "r" ( dwords ), "r" ( addr ) : "memory" );
132 DBGIO ( "[%08lx] => %08x%08x\n",
133 virt_to_phys ( addr ), dwords[1], dwords[0] );
135 #define linda_readq( _linda, _ptr, _offset ) \
136 linda_readq ( (_linda), (_ptr)->u.dwords, (_offset) )
137 #define linda_readq_array8b( _linda, _ptr, _offset, _idx ) \
138 linda_readq ( (_linda), (_ptr), ( (_offset) + ( (_idx) * 8 ) ) )
139 #define linda_readq_array64k( _linda, _ptr, _offset, _idx ) \
140 linda_readq ( (_linda), (_ptr), ( (_offset) + ( (_idx) * 65536 ) ) )
143 * Write Linda qword register
145 * @v linda Linda device
146 * @v dwords Register buffer to write
147 * @v offset Register offset
149 static void linda_writeq ( struct linda *linda, const uint32_t *dwords,
150 unsigned long offset ) {
151 void *addr = ( linda->regs + offset );
153 DBGIO ( "[%08lx] <= %08x%08x\n",
154 virt_to_phys ( addr ), dwords[1], dwords[0] );
156 __asm__ __volatile__ ( "movq (%0), %%mm0\n\t"
157 "movq %%mm0, (%1)\n\t"
158 : : "r" ( dwords ), "r" ( addr ) : "memory" );
160 #define linda_writeq( _linda, _ptr, _offset ) \
161 linda_writeq ( (_linda), (_ptr)->u.dwords, (_offset) )
162 #define linda_writeq_array8b( _linda, _ptr, _offset, _idx ) \
163 linda_writeq ( (_linda), (_ptr), ( (_offset) + ( (_idx) * 8 ) ) )
164 #define linda_writeq_array64k( _linda, _ptr, _offset, _idx ) \
165 linda_writeq ( (_linda), (_ptr), ( (_offset) + ( (_idx) * 65536 ) ) )
168 * Write Linda dword register
170 * @v linda Linda device
171 * @v dword Value to write
172 * @v offset Register offset
174 static void linda_writel ( struct linda *linda, uint32_t dword,
175 unsigned long offset ) {
176 writel ( dword, ( linda->regs + offset ) );
179 /***************************************************************************
181 * Link state management
183 ***************************************************************************
187 * Textual representation of link state
189 * @v link_state Link state
190 * @ret link_text Link state text
192 static const char * linda_link_state_text ( unsigned int link_state ) {
193 switch ( link_state ) {
194 case LINDA_LINK_STATE_DOWN: return "DOWN";
195 case LINDA_LINK_STATE_INIT: return "INIT";
196 case LINDA_LINK_STATE_ARM: return "ARM";
197 case LINDA_LINK_STATE_ACTIVE: return "ACTIVE";
198 case LINDA_LINK_STATE_ACT_DEFER:return "ACT_DEFER";
199 default: return "UNKNOWN";
204 * Handle link state change
206 * @v linda Linda device
208 static void linda_link_state_changed ( struct ib_device *ibdev ) {
209 struct linda *linda = ib_get_drvdata ( ibdev );
210 struct QIB_7220_IBCStatus ibcstatus;
211 struct QIB_7220_EXTCtrl extctrl;
212 unsigned int link_state;
213 unsigned int link_width;
214 unsigned int link_speed;
216 /* Read link state */
217 linda_readq ( linda, &ibcstatus, QIB_7220_IBCStatus_offset );
218 link_state = BIT_GET ( &ibcstatus, LinkState );
219 link_width = BIT_GET ( &ibcstatus, LinkWidthActive );
220 link_speed = BIT_GET ( &ibcstatus, LinkSpeedActive );
221 DBGC ( linda, "Linda %p link state %s (%s %s)\n", linda,
222 linda_link_state_text ( link_state ),
223 ( link_speed ? "DDR" : "SDR" ), ( link_width ? "x4" : "x1" ) );
225 /* Set LEDs according to link state */
226 linda_readq ( linda, &extctrl, QIB_7220_EXTCtrl_offset );
227 BIT_SET ( &extctrl, LEDPriPortGreenOn,
228 ( ( link_state >= LINDA_LINK_STATE_INIT ) ? 1 : 0 ) );
229 BIT_SET ( &extctrl, LEDPriPortYellowOn,
230 ( ( link_state >= LINDA_LINK_STATE_ACTIVE ) ? 1 : 0 ) );
231 linda_writeq ( linda, &extctrl, QIB_7220_EXTCtrl_offset );
233 /* Notify Infiniband core of link state change */
234 ibdev->port_state = ( link_state + 1 );
235 ibdev->link_width_active =
236 ( link_width ? IB_LINK_WIDTH_4X : IB_LINK_WIDTH_1X );
237 ibdev->link_speed_active =
238 ( link_speed ? IB_LINK_SPEED_DDR : IB_LINK_SPEED_SDR );
239 ib_link_state_changed ( ibdev );
243 * Wait for link state change to take effect
245 * @v linda Linda device
246 * @v new_link_state Expected link state
247 * @ret rc Return status code
249 static int linda_link_state_check ( struct linda *linda,
250 unsigned int new_link_state ) {
251 struct QIB_7220_IBCStatus ibcstatus;
252 unsigned int link_state;
255 for ( i = 0 ; i < LINDA_LINK_STATE_MAX_WAIT_US ; i++ ) {
256 linda_readq ( linda, &ibcstatus, QIB_7220_IBCStatus_offset );
257 link_state = BIT_GET ( &ibcstatus, LinkState );
258 if ( link_state == new_link_state )
263 DBGC ( linda, "Linda %p timed out waiting for link state %s\n",
264 linda, linda_link_state_text ( link_state ) );
269 * Set port information
271 * @v ibdev Infiniband device
272 * @v mad Set port information MAD
274 static int linda_set_port_info ( struct ib_device *ibdev, union ib_mad *mad ) {
275 struct linda *linda = ib_get_drvdata ( ibdev );
276 struct ib_port_info *port_info = &mad->smp.smp_data.port_info;
277 struct QIB_7220_IBCCtrl ibcctrl;
278 unsigned int port_state;
279 unsigned int link_state;
281 /* Set new link state */
282 port_state = ( port_info->link_speed_supported__port_state & 0xf );
284 link_state = ( port_state - 1 );
285 DBGC ( linda, "Linda %p set link state to %s (%x)\n", linda,
286 linda_link_state_text ( link_state ), link_state );
287 linda_readq ( linda, &ibcctrl, QIB_7220_IBCCtrl_offset );
288 BIT_SET ( &ibcctrl, LinkCmd, link_state );
289 linda_writeq ( linda, &ibcctrl, QIB_7220_IBCCtrl_offset );
291 /* Wait for link state change to take effect. Ignore
292 * errors; the current link state will be returned via
293 * the GetResponse MAD.
295 linda_link_state_check ( linda, link_state );
298 /* Detect and report link state change */
299 linda_link_state_changed ( ibdev );
305 * Set partition key table
307 * @v ibdev Infiniband device
308 * @v mad Set partition key table MAD
310 static int linda_set_pkey_table ( struct ib_device *ibdev __unused,
311 union ib_mad *mad __unused ) {
316 /***************************************************************************
320 ***************************************************************************
324 * Map context number to QPN
326 * @v ctx Context index
327 * @ret qpn Queue pair number
329 static int linda_ctx_to_qpn ( unsigned int ctx ) {
330 /* This mapping is fixed by hardware */
335 * Map QPN to context number
337 * @v qpn Queue pair number
338 * @ret ctx Context index
340 static int linda_qpn_to_ctx ( unsigned int qpn ) {
341 /* This mapping is fixed by hardware */
348 * @v linda Linda device
349 * @ret ctx Context index, or negative error
351 static int linda_alloc_ctx ( struct linda *linda ) {
354 for ( ctx = 0 ; ctx < LINDA_NUM_CONTEXTS ; ctx++ ) {
356 if ( ! linda->used_ctx[ctx] ) {
357 linda->used_ctx[ctx ] = 1;
358 DBGC2 ( linda, "Linda %p CTX %d allocated\n",
364 DBGC ( linda, "Linda %p out of available contexts\n", linda );
371 * @v linda Linda device
372 * @v ctx Context index
374 static void linda_free_ctx ( struct linda *linda, unsigned int ctx ) {
376 linda->used_ctx[ctx] = 0;
377 DBGC2 ( linda, "Linda %p CTX %d freed\n", linda, ctx );
380 /***************************************************************************
384 ***************************************************************************
387 /** Send buffer toggle bit
389 * We encode send buffers as 7 bits of send buffer index plus a single
390 * bit which should match the "check" bit in the SendBufAvail array.
392 #define LINDA_SEND_BUF_TOGGLE 0x80
395 * Allocate a send buffer
397 * @v linda Linda device
398 * @ret send_buf Send buffer
400 * You must guarantee that a send buffer is available. This is done
401 * by refusing to allocate more TX WQEs in total than the number of
402 * available send buffers.
404 static unsigned int linda_alloc_send_buf ( struct linda *linda ) {
405 unsigned int send_buf;
407 send_buf = linda->send_buf[linda->send_buf_cons];
408 send_buf ^= LINDA_SEND_BUF_TOGGLE;
409 linda->send_buf_cons = ( ( linda->send_buf_cons + 1 ) %
410 LINDA_MAX_SEND_BUFS );
417 * @v linda Linda device
418 * @v send_buf Send buffer
420 static void linda_free_send_buf ( struct linda *linda,
421 unsigned int send_buf ) {
422 linda->send_buf[linda->send_buf_prod] = send_buf;
423 linda->send_buf_prod = ( ( linda->send_buf_prod + 1 ) %
424 LINDA_MAX_SEND_BUFS );
428 * Check to see if send buffer is in use
430 * @v linda Linda device
431 * @v send_buf Send buffer
432 * @ret in_use Send buffer is in use
434 static int linda_send_buf_in_use ( struct linda *linda,
435 unsigned int send_buf ) {
436 unsigned int send_idx;
437 unsigned int send_check;
438 unsigned int inusecheck;
442 send_idx = ( send_buf & ~LINDA_SEND_BUF_TOGGLE );
443 send_check = ( !! ( send_buf & LINDA_SEND_BUF_TOGGLE ) );
444 inusecheck = BIT_GET ( linda->sendbufavail, InUseCheck[send_idx] );
445 inuse = ( !! ( inusecheck & 0x02 ) );
446 check = ( !! ( inusecheck & 0x01 ) );
447 return ( inuse || ( check != send_check ) );
451 * Calculate starting offset for send buffer
453 * @v linda Linda device
454 * @v send_buf Send buffer
455 * @ret offset Starting offset
457 static unsigned long linda_send_buffer_offset ( struct linda *linda,
458 unsigned int send_buf ) {
459 return ( linda->send_buffer_base +
460 ( ( send_buf & ~LINDA_SEND_BUF_TOGGLE ) *
461 LINDA_SEND_BUF_SIZE ) );
465 * Create send work queue
467 * @v linda Linda device
470 static int linda_create_send_wq ( struct linda *linda,
471 struct ib_queue_pair *qp ) {
472 struct ib_work_queue *wq = &qp->send;
473 struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
476 /* Reserve send buffers */
477 if ( ( linda->reserved_send_bufs + qp->send.num_wqes ) >
478 LINDA_MAX_SEND_BUFS ) {
479 DBGC ( linda, "Linda %p out of send buffers (have %d, used "
480 "%d, need %d)\n", linda, LINDA_MAX_SEND_BUFS,
481 linda->reserved_send_bufs, qp->send.num_wqes );
483 goto err_reserve_bufs;
485 linda->reserved_send_bufs += qp->send.num_wqes;
487 /* Reset work queue */
491 /* Allocate space for send buffer uasge list */
492 linda_wq->send_buf = zalloc ( qp->send.num_wqes *
493 sizeof ( linda_wq->send_buf[0] ) );
494 if ( ! linda_wq->send_buf ) {
496 goto err_alloc_send_buf;
501 free ( linda_wq->send_buf );
503 linda->reserved_send_bufs -= qp->send.num_wqes;
509 * Destroy send work queue
511 * @v linda Linda device
514 static void linda_destroy_send_wq ( struct linda *linda,
515 struct ib_queue_pair *qp ) {
516 struct ib_work_queue *wq = &qp->send;
517 struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
519 free ( linda_wq->send_buf );
520 linda->reserved_send_bufs -= qp->send.num_wqes;
524 * Initialise send datapath
526 * @v linda Linda device
527 * @ret rc Return status code
529 static int linda_init_send ( struct linda *linda ) {
530 struct QIB_7220_SendBufBase sendbufbase;
531 struct QIB_7220_SendBufAvailAddr sendbufavailaddr;
532 struct QIB_7220_SendCtrl sendctrl;
536 /* Retrieve SendBufBase */
537 linda_readq ( linda, &sendbufbase, QIB_7220_SendBufBase_offset );
538 linda->send_buffer_base = BIT_GET ( &sendbufbase,
540 DBGC ( linda, "Linda %p send buffers at %lx\n",
541 linda, linda->send_buffer_base );
543 /* Initialise the send_buf[] array */
544 for ( i = 0 ; i < LINDA_MAX_SEND_BUFS ; i++ )
545 linda->send_buf[i] = i;
547 /* Allocate space for the SendBufAvail array */
548 linda->sendbufavail = malloc_dma ( sizeof ( *linda->sendbufavail ),
549 LINDA_SENDBUFAVAIL_ALIGN );
550 if ( ! linda->sendbufavail ) {
552 goto err_alloc_sendbufavail;
554 memset ( linda->sendbufavail, 0, sizeof ( linda->sendbufavail ) );
556 /* Program SendBufAvailAddr into the hardware */
557 memset ( &sendbufavailaddr, 0, sizeof ( sendbufavailaddr ) );
558 BIT_FILL_1 ( &sendbufavailaddr, SendBufAvailAddr,
559 ( virt_to_bus ( linda->sendbufavail ) >> 6 ) );
560 linda_writeq ( linda, &sendbufavailaddr,
561 QIB_7220_SendBufAvailAddr_offset );
563 /* Enable sending and DMA of SendBufAvail */
564 memset ( &sendctrl, 0, sizeof ( sendctrl ) );
565 BIT_FILL_2 ( &sendctrl,
568 linda_writeq ( linda, &sendctrl, QIB_7220_SendCtrl_offset );
572 free_dma ( linda->sendbufavail, sizeof ( *linda->sendbufavail ) );
573 err_alloc_sendbufavail:
578 * Shut down send datapath
580 * @v linda Linda device
582 static void linda_fini_send ( struct linda *linda ) {
583 struct QIB_7220_SendCtrl sendctrl;
585 /* Disable sending and DMA of SendBufAvail */
586 memset ( &sendctrl, 0, sizeof ( sendctrl ) );
587 linda_writeq ( linda, &sendctrl, QIB_7220_SendCtrl_offset );
590 /* Ensure hardware has seen this disable */
591 linda_readq ( linda, &sendctrl, QIB_7220_SendCtrl_offset );
593 free_dma ( linda->sendbufavail, sizeof ( *linda->sendbufavail ) );
596 /***************************************************************************
600 ***************************************************************************
604 * Create receive work queue
606 * @v linda Linda device
608 * @ret rc Return status code
610 static int linda_create_recv_wq ( struct linda *linda,
611 struct ib_queue_pair *qp ) {
612 struct ib_work_queue *wq = &qp->recv;
613 struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
614 struct QIB_7220_RcvHdrAddr0 rcvhdraddr;
615 struct QIB_7220_RcvHdrTailAddr0 rcvhdrtailaddr;
616 struct QIB_7220_RcvHdrHead0 rcvhdrhead;
617 struct QIB_7220_scalar rcvegrindexhead;
618 struct QIB_7220_RcvCtrl rcvctrl;
619 unsigned int ctx = linda_qpn_to_ctx ( qp->qpn );
622 /* Reset context information */
623 memset ( &linda_wq->header_prod, 0,
624 sizeof ( linda_wq->header_prod ) );
625 linda_wq->header_cons = 0;
626 linda_wq->eager_prod = 0;
627 linda_wq->eager_cons = 0;
629 /* Allocate receive header buffer */
630 linda_wq->header = malloc_dma ( LINDA_RECV_HEADERS_SIZE,
631 LINDA_RECV_HEADERS_ALIGN );
632 if ( ! linda_wq->header ) {
634 goto err_alloc_header;
637 /* Enable context in hardware */
638 memset ( &rcvhdraddr, 0, sizeof ( rcvhdraddr ) );
639 BIT_FILL_1 ( &rcvhdraddr, RcvHdrAddr0,
640 ( virt_to_bus ( linda_wq->header ) >> 2 ) );
641 linda_writeq_array8b ( linda, &rcvhdraddr,
642 QIB_7220_RcvHdrAddr0_offset, ctx );
643 memset ( &rcvhdrtailaddr, 0, sizeof ( rcvhdrtailaddr ) );
644 BIT_FILL_1 ( &rcvhdrtailaddr, RcvHdrTailAddr0,
645 ( virt_to_bus ( &linda_wq->header_prod ) >> 2 ) );
646 linda_writeq_array8b ( linda, &rcvhdrtailaddr,
647 QIB_7220_RcvHdrTailAddr0_offset, ctx );
648 memset ( &rcvhdrhead, 0, sizeof ( rcvhdrhead ) );
649 BIT_FILL_1 ( &rcvhdrhead, counter, 1 );
650 linda_writeq_array64k ( linda, &rcvhdrhead,
651 QIB_7220_RcvHdrHead0_offset, ctx );
652 memset ( &rcvegrindexhead, 0, sizeof ( rcvegrindexhead ) );
653 BIT_FILL_1 ( &rcvegrindexhead, Value, 1 );
654 linda_writeq_array64k ( linda, &rcvegrindexhead,
655 QIB_7220_RcvEgrIndexHead0_offset, ctx );
656 linda_readq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
657 BIT_SET ( &rcvctrl, PortEnable[ctx], 1 );
658 BIT_SET ( &rcvctrl, IntrAvail[ctx], 1 );
659 linda_writeq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
661 DBGC ( linda, "Linda %p QPN %ld CTX %d hdrs [%lx,%lx) prod %lx\n",
662 linda, qp->qpn, ctx, virt_to_bus ( linda_wq->header ),
663 ( virt_to_bus ( linda_wq->header ) + LINDA_RECV_HEADERS_SIZE ),
664 virt_to_bus ( &linda_wq->header_prod ) );
667 free_dma ( linda_wq->header, LINDA_RECV_HEADERS_SIZE );
673 * Destroy receive work queue
675 * @v linda Linda device
678 static void linda_destroy_recv_wq ( struct linda *linda,
679 struct ib_queue_pair *qp ) {
680 struct ib_work_queue *wq = &qp->recv;
681 struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
682 struct QIB_7220_RcvCtrl rcvctrl;
683 unsigned int ctx = linda_qpn_to_ctx ( qp->qpn );
685 /* Disable context in hardware */
686 linda_readq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
687 BIT_SET ( &rcvctrl, PortEnable[ctx], 0 );
688 BIT_SET ( &rcvctrl, IntrAvail[ctx], 0 );
689 linda_writeq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
691 /* Make sure the hardware has seen that the context is disabled */
692 linda_readq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
695 /* Free headers ring */
696 free_dma ( linda_wq->header, LINDA_RECV_HEADERS_SIZE );
699 linda_free_ctx ( linda, ctx );
703 * Initialise receive datapath
705 * @v linda Linda device
706 * @ret rc Return status code
708 static int linda_init_recv ( struct linda *linda ) {
709 struct QIB_7220_RcvCtrl rcvctrl;
710 struct QIB_7220_scalar rcvegrbase;
711 struct QIB_7220_scalar rcvhdrentsize;
712 struct QIB_7220_scalar rcvhdrcnt;
713 struct QIB_7220_RcvBTHQP rcvbthqp;
714 unsigned int portcfg;
715 unsigned long egrbase;
716 unsigned int eager_array_size_0;
717 unsigned int eager_array_size_other;
720 /* Select configuration based on number of contexts */
721 switch ( LINDA_NUM_CONTEXTS ) {
723 portcfg = LINDA_PORTCFG_5CTX;
724 eager_array_size_0 = LINDA_EAGER_ARRAY_SIZE_5CTX_0;
725 eager_array_size_other = LINDA_EAGER_ARRAY_SIZE_5CTX_OTHER;
728 portcfg = LINDA_PORTCFG_9CTX;
729 eager_array_size_0 = LINDA_EAGER_ARRAY_SIZE_9CTX_0;
730 eager_array_size_other = LINDA_EAGER_ARRAY_SIZE_9CTX_OTHER;
733 portcfg = LINDA_PORTCFG_17CTX;
734 eager_array_size_0 = LINDA_EAGER_ARRAY_SIZE_17CTX_0;
735 eager_array_size_other = LINDA_EAGER_ARRAY_SIZE_17CTX_OTHER;
738 linker_assert ( 0, invalid_LINDA_NUM_CONTEXTS );
742 /* Configure number of contexts */
743 memset ( &rcvctrl, 0, sizeof ( rcvctrl ) );
744 BIT_FILL_3 ( &rcvctrl,
748 linda_writeq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
750 /* Configure receive header buffer sizes */
751 memset ( &rcvhdrcnt, 0, sizeof ( rcvhdrcnt ) );
752 BIT_FILL_1 ( &rcvhdrcnt, Value, LINDA_RECV_HEADER_COUNT );
753 linda_writeq ( linda, &rcvhdrcnt, QIB_7220_RcvHdrCnt_offset );
754 memset ( &rcvhdrentsize, 0, sizeof ( rcvhdrentsize ) );
755 BIT_FILL_1 ( &rcvhdrentsize, Value, ( LINDA_RECV_HEADER_SIZE >> 2 ) );
756 linda_writeq ( linda, &rcvhdrentsize, QIB_7220_RcvHdrEntSize_offset );
758 /* Calculate eager array start addresses for each context */
759 linda_readq ( linda, &rcvegrbase, QIB_7220_RcvEgrBase_offset );
760 egrbase = BIT_GET ( &rcvegrbase, Value );
761 linda->recv_wq[0].eager_array = egrbase;
762 linda->recv_wq[0].eager_entries = eager_array_size_0;
763 egrbase += ( eager_array_size_0 * sizeof ( struct QIB_7220_RcvEgr ) );
764 for ( ctx = 1 ; ctx < LINDA_NUM_CONTEXTS ; ctx++ ) {
765 linda->recv_wq[ctx].eager_array = egrbase;
766 linda->recv_wq[ctx].eager_entries = eager_array_size_other;
767 egrbase += ( eager_array_size_other *
768 sizeof ( struct QIB_7220_RcvEgr ) );
770 for ( ctx = 0 ; ctx < LINDA_NUM_CONTEXTS ; ctx++ ) {
771 DBGC ( linda, "Linda %p CTX %d eager array at %lx (%d "
772 "entries)\n", linda, ctx,
773 linda->recv_wq[ctx].eager_array,
774 linda->recv_wq[ctx].eager_entries );
777 /* Set the BTH QP for Infinipath packets to an unused value */
778 memset ( &rcvbthqp, 0, sizeof ( rcvbthqp ) );
779 BIT_FILL_1 ( &rcvbthqp, RcvBTHQP, LINDA_QP_IDETH );
780 linda_writeq ( linda, &rcvbthqp, QIB_7220_RcvBTHQP_offset );
786 * Shut down receive datapath
788 * @v linda Linda device
790 static void linda_fini_recv ( struct linda *linda __unused ) {
791 /* Nothing to do; all contexts were already disabled when the
792 * queue pairs were destroyed
796 /***************************************************************************
798 * Completion queue operations
800 ***************************************************************************
804 * Create completion queue
806 * @v ibdev Infiniband device
807 * @v cq Completion queue
808 * @ret rc Return status code
810 static int linda_create_cq ( struct ib_device *ibdev,
811 struct ib_completion_queue *cq ) {
812 struct linda *linda = ib_get_drvdata ( ibdev );
815 /* The hardware has no concept of completion queues. We
816 * simply use the association between CQs and WQs (already
817 * handled by the IB core) to decide which WQs to poll.
819 * We do set a CQN, just to avoid confusing debug messages
823 DBGC ( linda, "Linda %p CQN %ld created\n", linda, cq->cqn );
829 * Destroy completion queue
831 * @v ibdev Infiniband device
832 * @v cq Completion queue
834 static void linda_destroy_cq ( struct ib_device *ibdev,
835 struct ib_completion_queue *cq ) {
836 struct linda *linda = ib_get_drvdata ( ibdev );
839 DBGC ( linda, "Linda %p CQN %ld destroyed\n", linda, cq->cqn );
842 /***************************************************************************
844 * Queue pair operations
846 ***************************************************************************
852 * @v ibdev Infiniband device
854 * @ret rc Return status code
856 static int linda_create_qp ( struct ib_device *ibdev,
857 struct ib_queue_pair *qp ) {
858 struct linda *linda = ib_get_drvdata ( ibdev );
862 /* Locate an available context */
863 ctx = linda_alloc_ctx ( linda );
869 /* Set queue pair number based on context index */
870 qp->qpn = linda_ctx_to_qpn ( ctx );
872 /* Set work-queue private data pointers */
873 ib_wq_set_drvdata ( &qp->send, &linda->send_wq[ctx] );
874 ib_wq_set_drvdata ( &qp->recv, &linda->recv_wq[ctx] );
876 /* Create receive work queue */
877 if ( ( rc = linda_create_recv_wq ( linda, qp ) ) != 0 )
878 goto err_create_recv_wq;
880 /* Create send work queue */
881 if ( ( rc = linda_create_send_wq ( linda, qp ) ) != 0 )
882 goto err_create_send_wq;
886 linda_destroy_send_wq ( linda, qp );
888 linda_destroy_recv_wq ( linda, qp );
890 linda_free_ctx ( linda, ctx );
898 * @v ibdev Infiniband device
900 * @ret rc Return status code
902 static int linda_modify_qp ( struct ib_device *ibdev,
903 struct ib_queue_pair *qp ) {
904 struct linda *linda = ib_get_drvdata ( ibdev );
906 /* Nothing to do; the hardware doesn't have a notion of queue
909 DBGC ( linda, "Linda %p QPN %ld modified\n", linda, qp->qpn );
916 * @v ibdev Infiniband device
919 static void linda_destroy_qp ( struct ib_device *ibdev,
920 struct ib_queue_pair *qp ) {
921 struct linda *linda = ib_get_drvdata ( ibdev );
923 linda_destroy_send_wq ( linda, qp );
924 linda_destroy_recv_wq ( linda, qp );
927 /***************************************************************************
929 * Work request operations
931 ***************************************************************************
935 * Post send work queue entry
937 * @v ibdev Infiniband device
939 * @v dest Destination address vector
940 * @v iobuf I/O buffer
941 * @ret rc Return status code
943 static int linda_post_send ( struct ib_device *ibdev,
944 struct ib_queue_pair *qp,
945 struct ib_address_vector *dest,
946 struct io_buffer *iobuf ) {
947 struct linda *linda = ib_get_drvdata ( ibdev );
948 struct ib_work_queue *wq = &qp->send;
949 struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
950 struct QIB_7220_SendPbc sendpbc;
951 uint8_t header_buf[IB_MAX_HEADER_SIZE];
952 struct io_buffer headers;
953 unsigned int send_buf;
954 unsigned long start_offset;
955 unsigned long offset;
960 /* Allocate send buffer and calculate offset */
961 send_buf = linda_alloc_send_buf ( linda );
962 start_offset = offset = linda_send_buffer_offset ( linda, send_buf );
964 /* Store I/O buffer and send buffer index */
965 assert ( wq->iobufs[linda_wq->prod] == NULL );
966 wq->iobufs[linda_wq->prod] = iobuf;
967 linda_wq->send_buf[linda_wq->prod] = send_buf;
969 /* Construct headers */
970 iob_populate ( &headers, header_buf, 0, sizeof ( header_buf ) );
971 iob_reserve ( &headers, sizeof ( header_buf ) );
972 ib_push ( ibdev, &headers, qp, iob_len ( iobuf ), dest );
974 /* Calculate packet length */
975 len = ( ( sizeof ( sendpbc ) + iob_len ( &headers ) +
976 iob_len ( iobuf ) + 3 ) & ~3 );
978 /* Construct send per-buffer control word */
979 memset ( &sendpbc, 0, sizeof ( sendpbc ) );
980 BIT_FILL_2 ( &sendpbc,
981 LengthP1_toibc, ( ( len >> 2 ) - 1 ),
985 DBG_DISABLE ( DBGLVL_IO );
986 linda_writeq ( linda, &sendpbc, offset );
987 offset += sizeof ( sendpbc );
990 for ( data = headers.data, frag_len = iob_len ( &headers ) ;
991 frag_len > 0 ; data++, offset += 4, frag_len -= 4 ) {
992 linda_writel ( linda, *data, offset );
996 for ( data = iobuf->data, frag_len = iob_len ( iobuf ) ;
997 frag_len > 0 ; data++, offset += 4, frag_len -= 4 ) {
998 linda_writel ( linda, *data, offset );
1000 DBG_ENABLE ( DBGLVL_IO );
1002 assert ( ( start_offset + len ) == offset );
1003 DBGC2 ( linda, "Linda %p QPN %ld TX %d(%d) posted [%lx,%lx)\n",
1004 linda, qp->qpn, send_buf, linda_wq->prod,
1005 start_offset, offset );
1007 /* Increment producer counter */
1008 linda_wq->prod = ( ( linda_wq->prod + 1 ) & ( wq->num_wqes - 1 ) );
1014 * Complete send work queue entry
1016 * @v ibdev Infiniband device
1018 * @v wqe_idx Work queue entry index
1020 static void linda_complete_send ( struct ib_device *ibdev,
1021 struct ib_queue_pair *qp,
1022 unsigned int wqe_idx ) {
1023 struct linda *linda = ib_get_drvdata ( ibdev );
1024 struct ib_work_queue *wq = &qp->send;
1025 struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
1026 struct io_buffer *iobuf;
1027 unsigned int send_buf;
1029 /* Parse completion */
1030 send_buf = linda_wq->send_buf[wqe_idx];
1031 DBGC2 ( linda, "Linda %p QPN %ld TX %d(%d) complete\n",
1032 linda, qp->qpn, send_buf, wqe_idx );
1034 /* Complete work queue entry */
1035 iobuf = wq->iobufs[wqe_idx];
1036 assert ( iobuf != NULL );
1037 ib_complete_send ( ibdev, qp, iobuf, 0 );
1038 wq->iobufs[wqe_idx] = NULL;
1040 /* Free send buffer */
1041 linda_free_send_buf ( linda, send_buf );
1045 * Poll send work queue
1047 * @v ibdev Infiniband device
1050 static void linda_poll_send_wq ( struct ib_device *ibdev,
1051 struct ib_queue_pair *qp ) {
1052 struct linda *linda = ib_get_drvdata ( ibdev );
1053 struct ib_work_queue *wq = &qp->send;
1054 struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
1055 unsigned int send_buf;
1057 /* Look for completions */
1058 while ( wq->fill ) {
1060 /* Check to see if send buffer has completed */
1061 send_buf = linda_wq->send_buf[linda_wq->cons];
1062 if ( linda_send_buf_in_use ( linda, send_buf ) )
1065 /* Complete this buffer */
1066 linda_complete_send ( ibdev, qp, linda_wq->cons );
1068 /* Increment consumer counter */
1069 linda_wq->cons = ( ( linda_wq->cons + 1 ) &
1070 ( wq->num_wqes - 1 ) );
1075 * Post receive work queue entry
1077 * @v ibdev Infiniband device
1079 * @v iobuf I/O buffer
1080 * @ret rc Return status code
1082 static int linda_post_recv ( struct ib_device *ibdev,
1083 struct ib_queue_pair *qp,
1084 struct io_buffer *iobuf ) {
1085 struct linda *linda = ib_get_drvdata ( ibdev );
1086 struct ib_work_queue *wq = &qp->recv;
1087 struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
1088 struct QIB_7220_RcvEgr rcvegr;
1089 struct QIB_7220_scalar rcvegrindexhead;
1090 unsigned int ctx = linda_qpn_to_ctx ( qp->qpn );
1093 unsigned int wqe_idx;
1094 unsigned int bufsize;
1097 addr = virt_to_bus ( iobuf->data );
1098 len = iob_tailroom ( iobuf );
1099 if ( addr & ( LINDA_EAGER_BUFFER_ALIGN - 1 ) ) {
1100 DBGC ( linda, "Linda %p QPN %ld misaligned RX buffer "
1101 "(%08lx)\n", linda, qp->qpn, addr );
1104 if ( len != LINDA_RECV_PAYLOAD_SIZE ) {
1105 DBGC ( linda, "Linda %p QPN %ld wrong RX buffer size (%zd)\n",
1106 linda, qp->qpn, len );
1110 /* Calculate eager producer index and WQE index */
1111 wqe_idx = ( linda_wq->eager_prod & ( wq->num_wqes - 1 ) );
1112 assert ( wq->iobufs[wqe_idx] == NULL );
1114 /* Store I/O buffer */
1115 wq->iobufs[wqe_idx] = iobuf;
1117 /* Calculate buffer size */
1118 switch ( LINDA_RECV_PAYLOAD_SIZE ) {
1119 case 2048: bufsize = LINDA_EAGER_BUFFER_2K; break;
1120 case 4096: bufsize = LINDA_EAGER_BUFFER_4K; break;
1121 case 8192: bufsize = LINDA_EAGER_BUFFER_8K; break;
1122 case 16384: bufsize = LINDA_EAGER_BUFFER_16K; break;
1123 case 32768: bufsize = LINDA_EAGER_BUFFER_32K; break;
1124 case 65536: bufsize = LINDA_EAGER_BUFFER_64K; break;
1125 default: linker_assert ( 0, invalid_rx_payload_size );
1126 bufsize = LINDA_EAGER_BUFFER_NONE;
1129 /* Post eager buffer */
1130 memset ( &rcvegr, 0, sizeof ( rcvegr ) );
1131 BIT_FILL_2 ( &rcvegr,
1132 Addr, ( addr >> 11 ),
1134 linda_writeq_array8b ( linda, &rcvegr,
1135 linda_wq->eager_array, linda_wq->eager_prod );
1136 DBGC2 ( linda, "Linda %p QPN %ld RX egr %d(%d) posted [%lx,%lx)\n",
1137 linda, qp->qpn, linda_wq->eager_prod, wqe_idx,
1138 addr, ( addr + len ) );
1140 /* Increment producer index */
1141 linda_wq->eager_prod = ( ( linda_wq->eager_prod + 1 ) &
1142 ( linda_wq->eager_entries - 1 ) );
1144 /* Update head index */
1145 memset ( &rcvegrindexhead, 0, sizeof ( rcvegrindexhead ) );
1146 BIT_FILL_1 ( &rcvegrindexhead,
1147 Value, ( ( linda_wq->eager_prod + 1 ) &
1148 ( linda_wq->eager_entries - 1 ) ) );
1149 linda_writeq_array64k ( linda, &rcvegrindexhead,
1150 QIB_7220_RcvEgrIndexHead0_offset, ctx );
1156 * Complete receive work queue entry
1158 * @v ibdev Infiniband device
1160 * @v header_offs Header offset
1162 static void linda_complete_recv ( struct ib_device *ibdev,
1163 struct ib_queue_pair *qp,
1164 unsigned int header_offs ) {
1165 struct linda *linda = ib_get_drvdata ( ibdev );
1166 struct ib_work_queue *wq = &qp->recv;
1167 struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
1168 struct QIB_7220_RcvHdrFlags *rcvhdrflags;
1169 struct QIB_7220_RcvEgr rcvegr;
1170 struct io_buffer headers;
1171 struct io_buffer *iobuf;
1172 struct ib_queue_pair *intended_qp;
1173 struct ib_address_vector dest;
1174 struct ib_address_vector source;
1175 unsigned int rcvtype;
1176 unsigned int pktlen;
1177 unsigned int egrindex;
1178 unsigned int useegrbfr;
1179 unsigned int iberr, mkerr, tiderr, khdrerr, mtuerr;
1180 unsigned int lenerr, parityerr, vcrcerr, icrcerr;
1182 unsigned int hdrqoffset;
1183 unsigned int header_len;
1184 unsigned int padded_payload_len;
1185 unsigned int wqe_idx;
1190 /* RcvHdrFlags are at the end of the header entry */
1191 rcvhdrflags = ( linda_wq->header + header_offs +
1192 LINDA_RECV_HEADER_SIZE - sizeof ( *rcvhdrflags ) );
1193 rcvtype = BIT_GET ( rcvhdrflags, RcvType );
1194 pktlen = ( BIT_GET ( rcvhdrflags, PktLen ) << 2 );
1195 egrindex = BIT_GET ( rcvhdrflags, EgrIndex );
1196 useegrbfr = BIT_GET ( rcvhdrflags, UseEgrBfr );
1197 hdrqoffset = ( BIT_GET ( rcvhdrflags, HdrqOffset ) << 2 );
1198 iberr = BIT_GET ( rcvhdrflags, IBErr );
1199 mkerr = BIT_GET ( rcvhdrflags, MKErr );
1200 tiderr = BIT_GET ( rcvhdrflags, TIDErr );
1201 khdrerr = BIT_GET ( rcvhdrflags, KHdrErr );
1202 mtuerr = BIT_GET ( rcvhdrflags, MTUErr );
1203 lenerr = BIT_GET ( rcvhdrflags, LenErr );
1204 parityerr = BIT_GET ( rcvhdrflags, ParityErr );
1205 vcrcerr = BIT_GET ( rcvhdrflags, VCRCErr );
1206 icrcerr = BIT_GET ( rcvhdrflags, ICRCErr );
1207 header_len = ( LINDA_RECV_HEADER_SIZE - hdrqoffset -
1208 sizeof ( *rcvhdrflags ) );
1209 padded_payload_len = ( pktlen - header_len - 4 /* ICRC */ );
1210 err = ( iberr | mkerr | tiderr | khdrerr | mtuerr |
1211 lenerr | parityerr | vcrcerr | icrcerr );
1212 /* IB header is placed immediately before RcvHdrFlags */
1213 iob_populate ( &headers, ( ( ( void * ) rcvhdrflags ) - header_len ),
1214 header_len, header_len );
1216 /* Dump diagnostic information */
1217 if ( err || ( ! useegrbfr ) ) {
1218 DBGC ( linda, "Linda %p QPN %ld RX egr %d%s hdr %d type %d "
1219 "len %d(%d+%d+4)%s%s%s%s%s%s%s%s%s%s%s\n", linda,
1220 qp->qpn, egrindex, ( useegrbfr ? "" : "(unused)" ),
1221 ( header_offs / LINDA_RECV_HEADER_SIZE ), rcvtype,
1222 pktlen, header_len, padded_payload_len,
1223 ( err ? " [Err" : "" ), ( iberr ? " IB" : "" ),
1224 ( mkerr ? " MK" : "" ), ( tiderr ? " TID" : "" ),
1225 ( khdrerr ? " KHdr" : "" ), ( mtuerr ? " MTU" : "" ),
1226 ( lenerr ? " Len" : "" ), ( parityerr ? " Parity" : ""),
1227 ( vcrcerr ? " VCRC" : "" ), ( icrcerr ? " ICRC" : "" ),
1228 ( err ? "]" : "" ) );
1230 DBGC2 ( linda, "Linda %p QPN %ld RX egr %d hdr %d type %d "
1231 "len %d(%d+%d+4)\n", linda, qp->qpn, egrindex,
1232 ( header_offs / LINDA_RECV_HEADER_SIZE ), rcvtype,
1233 pktlen, header_len, padded_payload_len );
1235 DBGCP_HDA ( linda, hdrqoffset, headers.data,
1236 ( header_len + sizeof ( *rcvhdrflags ) ) );
1238 /* Parse header to generate address vector */
1239 qp0 = ( qp->qpn == 0 );
1241 if ( ( rc = ib_pull ( ibdev, &headers, ( qp0 ? &intended_qp : NULL ),
1242 &payload_len, &dest, &source ) ) != 0 ) {
1243 DBGC ( linda, "Linda %p could not parse headers: %s\n",
1244 linda, strerror ( rc ) );
1247 if ( ! intended_qp )
1250 /* Complete this buffer and any skipped buffers. Note that
1251 * when the hardware runs out of buffers, it will repeatedly
1252 * report the same buffer (the tail) as a TID error, and that
1253 * it also has a habit of sometimes skipping over several
1258 /* If we have caught up to the producer counter, stop.
1259 * This will happen when the hardware first runs out
1260 * of buffers and starts reporting TID errors against
1261 * the eager buffer it wants to use next.
1263 if ( linda_wq->eager_cons == linda_wq->eager_prod )
1266 /* If we have caught up to where we should be after
1267 * completing this egrindex, stop. We phrase the test
1268 * this way to avoid completing the entire ring when
1269 * we receive the same egrindex twice in a row.
1271 if ( ( linda_wq->eager_cons ==
1272 ( ( egrindex + 1 ) & ( linda_wq->eager_entries - 1 ) )))
1275 /* Identify work queue entry and corresponding I/O
1278 wqe_idx = ( linda_wq->eager_cons & ( wq->num_wqes - 1 ) );
1279 iobuf = wq->iobufs[wqe_idx];
1280 assert ( iobuf != NULL );
1281 wq->iobufs[wqe_idx] = NULL;
1283 /* Complete the eager buffer */
1284 if ( linda_wq->eager_cons == egrindex ) {
1285 /* Completing the eager buffer described in
1286 * this header entry.
1288 iob_put ( iobuf, payload_len );
1289 rc = ( err ? -EIO : ( useegrbfr ? 0 : -ECANCELED ) );
1290 /* Redirect to target QP if necessary */
1291 if ( qp != intended_qp ) {
1292 DBGC ( linda, "Linda %p redirecting QPN %ld "
1294 linda, qp->qpn, intended_qp->qpn );
1295 /* Compensate for incorrect fill levels */
1297 intended_qp->recv.fill++;
1299 ib_complete_recv ( ibdev, intended_qp, &dest, &source,
1302 /* Completing on a skipped-over eager buffer */
1303 ib_complete_recv ( ibdev, qp, &dest, &source, iobuf,
1307 /* Clear eager buffer */
1308 memset ( &rcvegr, 0, sizeof ( rcvegr ) );
1309 linda_writeq_array8b ( linda, &rcvegr, linda_wq->eager_array,
1310 linda_wq->eager_cons );
1312 /* Increment consumer index */
1313 linda_wq->eager_cons = ( ( linda_wq->eager_cons + 1 ) &
1314 ( linda_wq->eager_entries - 1 ) );
1319 * Poll receive work queue
1321 * @v ibdev Infiniband device
1324 static void linda_poll_recv_wq ( struct ib_device *ibdev,
1325 struct ib_queue_pair *qp ) {
1326 struct linda *linda = ib_get_drvdata ( ibdev );
1327 struct ib_work_queue *wq = &qp->recv;
1328 struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
1329 struct QIB_7220_RcvHdrHead0 rcvhdrhead;
1330 unsigned int ctx = linda_qpn_to_ctx ( qp->qpn );
1331 unsigned int header_prod;
1333 /* Check for received packets */
1334 header_prod = ( BIT_GET ( &linda_wq->header_prod, Value ) << 2 );
1335 if ( header_prod == linda_wq->header_cons )
1338 /* Process all received packets */
1339 while ( linda_wq->header_cons != header_prod ) {
1341 /* Complete the receive */
1342 linda_complete_recv ( ibdev, qp, linda_wq->header_cons );
1344 /* Increment the consumer offset */
1345 linda_wq->header_cons += LINDA_RECV_HEADER_SIZE;
1346 linda_wq->header_cons %= LINDA_RECV_HEADERS_SIZE;
1349 /* Update consumer offset */
1350 memset ( &rcvhdrhead, 0, sizeof ( rcvhdrhead ) );
1351 BIT_FILL_2 ( &rcvhdrhead,
1352 RcvHeadPointer, ( linda_wq->header_cons >> 2 ),
1354 linda_writeq_array64k ( linda, &rcvhdrhead,
1355 QIB_7220_RcvHdrHead0_offset, ctx );
1359 * Poll completion queue
1361 * @v ibdev Infiniband device
1362 * @v cq Completion queue
1364 static void linda_poll_cq ( struct ib_device *ibdev,
1365 struct ib_completion_queue *cq ) {
1366 struct ib_work_queue *wq;
1368 /* Poll associated send and receive queues */
1369 list_for_each_entry ( wq, &cq->work_queues, list ) {
1370 if ( wq->is_send ) {
1371 linda_poll_send_wq ( ibdev, wq->qp );
1373 linda_poll_recv_wq ( ibdev, wq->qp );
1378 /***************************************************************************
1382 ***************************************************************************
1388 * @v ibdev Infiniband device
1390 static void linda_poll_eq ( struct ib_device *ibdev ) {
1391 struct linda *linda = ib_get_drvdata ( ibdev );
1392 struct QIB_7220_ErrStatus errstatus;
1393 struct QIB_7220_ErrClear errclear;
1395 /* Check for link status changes */
1396 DBG_DISABLE ( DBGLVL_IO );
1397 linda_readq ( linda, &errstatus, QIB_7220_ErrStatus_offset );
1398 DBG_ENABLE ( DBGLVL_IO );
1399 if ( BIT_GET ( &errstatus, IBStatusChanged ) ) {
1400 linda_link_state_changed ( ibdev );
1401 memset ( &errclear, 0, sizeof ( errclear ) );
1402 BIT_FILL_1 ( &errclear, IBStatusChangedClear, 1 );
1403 linda_writeq ( linda, &errclear, QIB_7220_ErrClear_offset );
1407 /***************************************************************************
1409 * Infiniband link-layer operations
1411 ***************************************************************************
1415 * Initialise Infiniband link
1417 * @v ibdev Infiniband device
1418 * @ret rc Return status code
1420 static int linda_open ( struct ib_device *ibdev ) {
1421 struct linda *linda = ib_get_drvdata ( ibdev );
1422 struct QIB_7220_Control control;
1425 linda_readq ( linda, &control, QIB_7220_Control_offset );
1426 BIT_SET ( &control, LinkEn, 1 );
1427 linda_writeq ( linda, &control, QIB_7220_Control_offset );
1432 * Close Infiniband link
1434 * @v ibdev Infiniband device
1436 static void linda_close ( struct ib_device *ibdev ) {
1437 struct linda *linda = ib_get_drvdata ( ibdev );
1438 struct QIB_7220_Control control;
1441 linda_readq ( linda, &control, QIB_7220_Control_offset );
1442 BIT_SET ( &control, LinkEn, 0 );
1443 linda_writeq ( linda, &control, QIB_7220_Control_offset );
1446 /***************************************************************************
1448 * Multicast group operations
1450 ***************************************************************************
1454 * Attach to multicast group
1456 * @v ibdev Infiniband device
1458 * @v gid Multicast GID
1459 * @ret rc Return status code
1461 static int linda_mcast_attach ( struct ib_device *ibdev,
1462 struct ib_queue_pair *qp,
1463 union ib_gid *gid ) {
1464 struct linda *linda = ib_get_drvdata ( ibdev );
1473 * Detach from multicast group
1475 * @v ibdev Infiniband device
1477 * @v gid Multicast GID
1479 static void linda_mcast_detach ( struct ib_device *ibdev,
1480 struct ib_queue_pair *qp,
1481 union ib_gid *gid ) {
1482 struct linda *linda = ib_get_drvdata ( ibdev );
1489 /** Linda Infiniband operations */
1490 static struct ib_device_operations linda_ib_operations = {
1491 .create_cq = linda_create_cq,
1492 .destroy_cq = linda_destroy_cq,
1493 .create_qp = linda_create_qp,
1494 .modify_qp = linda_modify_qp,
1495 .destroy_qp = linda_destroy_qp,
1496 .post_send = linda_post_send,
1497 .post_recv = linda_post_recv,
1498 .poll_cq = linda_poll_cq,
1499 .poll_eq = linda_poll_eq,
1501 .close = linda_close,
1502 .mcast_attach = linda_mcast_attach,
1503 .mcast_detach = linda_mcast_detach,
1504 .set_port_info = linda_set_port_info,
1505 .set_pkey_table = linda_set_pkey_table,
1508 /***************************************************************************
1510 * I2C bus operations
1512 ***************************************************************************
1515 /** Linda I2C bit to GPIO mappings */
1516 static unsigned int linda_i2c_bits[] = {
1517 [I2C_BIT_SCL] = ( 1 << LINDA_GPIO_SCL ),
1518 [I2C_BIT_SDA] = ( 1 << LINDA_GPIO_SDA ),
1522 * Read Linda I2C line status
1524 * @v basher Bit-bashing interface
1525 * @v bit_id Bit number
1526 * @ret zero Input is a logic 0
1527 * @ret non-zero Input is a logic 1
1529 static int linda_i2c_read_bit ( struct bit_basher *basher,
1530 unsigned int bit_id ) {
1531 struct linda *linda =
1532 container_of ( basher, struct linda, i2c.basher );
1533 struct QIB_7220_EXTStatus extstatus;
1534 unsigned int status;
1536 DBG_DISABLE ( DBGLVL_IO );
1538 linda_readq ( linda, &extstatus, QIB_7220_EXTStatus_offset );
1539 status = ( BIT_GET ( &extstatus, GPIOIn ) & linda_i2c_bits[bit_id] );
1541 DBG_ENABLE ( DBGLVL_IO );
1547 * Write Linda I2C line status
1549 * @v basher Bit-bashing interface
1550 * @v bit_id Bit number
1551 * @v data Value to write
1553 static void linda_i2c_write_bit ( struct bit_basher *basher,
1554 unsigned int bit_id, unsigned long data ) {
1555 struct linda *linda =
1556 container_of ( basher, struct linda, i2c.basher );
1557 struct QIB_7220_EXTCtrl extctrl;
1558 struct QIB_7220_GPIO gpioout;
1559 unsigned int bit = linda_i2c_bits[bit_id];
1560 unsigned int outputs = 0;
1561 unsigned int output_enables = 0;
1563 DBG_DISABLE ( DBGLVL_IO );
1565 /* Read current GPIO mask and outputs */
1566 linda_readq ( linda, &extctrl, QIB_7220_EXTCtrl_offset );
1567 linda_readq ( linda, &gpioout, QIB_7220_GPIOOut_offset );
1569 /* Update outputs and output enables. I2C lines are tied
1570 * high, so we always set the output to 0 and use the output
1571 * enable to control the line.
1573 output_enables = BIT_GET ( &extctrl, GPIOOe );
1574 output_enables = ( ( output_enables & ~bit ) | ( ~data & bit ) );
1575 outputs = BIT_GET ( &gpioout, GPIO );
1576 outputs = ( outputs & ~bit );
1577 BIT_SET ( &extctrl, GPIOOe, output_enables );
1578 BIT_SET ( &gpioout, GPIO, outputs );
1580 /* Write the output enable first; that way we avoid logic
1583 linda_writeq ( linda, &extctrl, QIB_7220_EXTCtrl_offset );
1584 linda_writeq ( linda, &gpioout, QIB_7220_GPIOOut_offset );
1587 DBG_ENABLE ( DBGLVL_IO );
1590 /** Linda I2C bit-bashing interface operations */
1591 static struct bit_basher_operations linda_i2c_basher_ops = {
1592 .read = linda_i2c_read_bit,
1593 .write = linda_i2c_write_bit,
1597 * Initialise Linda I2C subsystem
1599 * @v linda Linda device
1600 * @ret rc Return status code
1602 static int linda_init_i2c ( struct linda *linda ) {
1603 static int try_eeprom_address[] = { 0x51, 0x50 };
1607 /* Initialise bus */
1608 if ( ( rc = init_i2c_bit_basher ( &linda->i2c,
1609 &linda_i2c_basher_ops ) ) != 0 ) {
1610 DBGC ( linda, "Linda %p could not initialise I2C bus: %s\n",
1611 linda, strerror ( rc ) );
1615 /* Probe for devices */
1616 for ( i = 0 ; i < ( sizeof ( try_eeprom_address ) /
1617 sizeof ( try_eeprom_address[0] ) ) ; i++ ) {
1618 init_i2c_eeprom ( &linda->eeprom, try_eeprom_address[i] );
1619 if ( ( rc = i2c_check_presence ( &linda->i2c.i2c,
1620 &linda->eeprom ) ) == 0 ) {
1621 DBGC2 ( linda, "Linda %p found EEPROM at %02x\n",
1622 linda, try_eeprom_address[i] );
1627 DBGC ( linda, "Linda %p could not find EEPROM\n", linda );
1632 * Read EEPROM parameters
1634 * @v linda Linda device
1635 * @v guid GUID to fill in
1636 * @ret rc Return status code
1638 static int linda_read_eeprom ( struct linda *linda, union ib_guid *guid ) {
1639 struct i2c_interface *i2c = &linda->i2c.i2c;
1643 if ( ( rc = i2c->read ( i2c, &linda->eeprom, LINDA_EEPROM_GUID_OFFSET,
1644 guid->bytes, sizeof ( *guid ) ) ) != 0 ) {
1645 DBGC ( linda, "Linda %p could not read GUID: %s\n",
1646 linda, strerror ( rc ) );
1649 DBGC2 ( linda, "Linda %p has GUID " IB_GUID_FMT "\n",
1650 linda, IB_GUID_ARGS ( guid ) );
1652 /* Read serial number (debug only) */
1654 uint8_t serial[LINDA_EEPROM_SERIAL_SIZE + 1];
1656 serial[ sizeof ( serial ) - 1 ] = '\0';
1657 if ( ( rc = i2c->read ( i2c, &linda->eeprom,
1658 LINDA_EEPROM_SERIAL_OFFSET, serial,
1659 ( sizeof ( serial ) - 1 ) ) ) != 0 ) {
1660 DBGC ( linda, "Linda %p could not read serial: %s\n",
1661 linda, strerror ( rc ) );
1664 DBGC2 ( linda, "Linda %p has serial number \"%s\"\n",
1671 /***************************************************************************
1673 * External parallel bus access
1675 ***************************************************************************
1679 * Request ownership of the IB external parallel bus
1681 * @v linda Linda device
1682 * @ret rc Return status code
1684 static int linda_ib_epb_request ( struct linda *linda ) {
1685 struct QIB_7220_ibsd_epb_access_ctrl access;
1688 /* Request ownership */
1689 memset ( &access, 0, sizeof ( access ) );
1690 BIT_FILL_1 ( &access, sw_ib_epb_req, 1 );
1691 linda_writeq ( linda, &access, QIB_7220_ibsd_epb_access_ctrl_offset );
1693 /* Wait for ownership to be granted */
1694 for ( i = 0 ; i < LINDA_EPB_REQUEST_MAX_WAIT_US ; i++ ) {
1695 linda_readq ( linda, &access,
1696 QIB_7220_ibsd_epb_access_ctrl_offset );
1697 if ( BIT_GET ( &access, sw_ib_epb_req_granted ) )
1702 DBGC ( linda, "Linda %p timed out waiting for IB EPB request\n",
1708 * Wait for IB external parallel bus transaction to complete
1710 * @v linda Linda device
1711 * @v xact Buffer to hold transaction result
1712 * @ret rc Return status code
1714 static int linda_ib_epb_wait ( struct linda *linda,
1715 struct QIB_7220_ibsd_epb_transaction_reg *xact ) {
1718 /* Discard first read to allow for signals crossing clock domains */
1719 linda_readq ( linda, xact, QIB_7220_ibsd_epb_transaction_reg_offset );
1721 for ( i = 0 ; i < LINDA_EPB_XACT_MAX_WAIT_US ; i++ ) {
1722 linda_readq ( linda, xact,
1723 QIB_7220_ibsd_epb_transaction_reg_offset );
1724 if ( BIT_GET ( xact, ib_epb_rdy ) ) {
1725 if ( BIT_GET ( xact, ib_epb_req_error ) ) {
1726 DBGC ( linda, "Linda %p EPB transaction "
1727 "failed\n", linda );
1736 DBGC ( linda, "Linda %p timed out waiting for IB EPB transaction\n",
1742 * Release ownership of the IB external parallel bus
1744 * @v linda Linda device
1746 static void linda_ib_epb_release ( struct linda *linda ) {
1747 struct QIB_7220_ibsd_epb_access_ctrl access;
1749 memset ( &access, 0, sizeof ( access ) );
1750 BIT_FILL_1 ( &access, sw_ib_epb_req, 0 );
1751 linda_writeq ( linda, &access, QIB_7220_ibsd_epb_access_ctrl_offset );
1755 * Read data via IB external parallel bus
1757 * @v linda Linda device
1758 * @v location EPB location
1759 * @ret data Data read, or negative error
1761 * You must have already acquired ownership of the IB external
1764 static int linda_ib_epb_read ( struct linda *linda, unsigned int location ) {
1765 struct QIB_7220_ibsd_epb_transaction_reg xact;
1769 /* Ensure no transaction is currently in progress */
1770 if ( ( rc = linda_ib_epb_wait ( linda, &xact ) ) != 0 )
1774 memset ( &xact, 0, sizeof ( xact ) );
1776 ib_epb_address, LINDA_EPB_LOC_ADDRESS ( location ),
1777 ib_epb_read_write, LINDA_EPB_READ,
1778 ib_epb_cs, LINDA_EPB_LOC_CS ( location ) );
1779 linda_writeq ( linda, &xact,
1780 QIB_7220_ibsd_epb_transaction_reg_offset );
1782 /* Wait for transaction to complete */
1783 if ( ( rc = linda_ib_epb_wait ( linda, &xact ) ) != 0 )
1786 data = BIT_GET ( &xact, ib_epb_data );
1791 * Write data via IB external parallel bus
1793 * @v linda Linda device
1794 * @v location EPB location
1795 * @v data Data to write
1796 * @ret rc Return status code
1798 * You must have already acquired ownership of the IB external
1801 static int linda_ib_epb_write ( struct linda *linda, unsigned int location,
1802 unsigned int data ) {
1803 struct QIB_7220_ibsd_epb_transaction_reg xact;
1806 /* Ensure no transaction is currently in progress */
1807 if ( ( rc = linda_ib_epb_wait ( linda, &xact ) ) != 0 )
1811 memset ( &xact, 0, sizeof ( xact ) );
1814 ib_epb_address, LINDA_EPB_LOC_ADDRESS ( location ),
1815 ib_epb_read_write, LINDA_EPB_WRITE,
1816 ib_epb_cs, LINDA_EPB_LOC_CS ( location ) );
1817 linda_writeq ( linda, &xact,
1818 QIB_7220_ibsd_epb_transaction_reg_offset );
1820 /* Wait for transaction to complete */
1821 if ( ( rc = linda_ib_epb_wait ( linda, &xact ) ) != 0 )
1828 * Read/modify/write EPB register
1830 * @v linda Linda device
1832 * @v channel Channel
1833 * @v element Element
1835 * @v value Value to set
1836 * @v mask Mask to apply to old value
1837 * @ret rc Return status code
1839 static int linda_ib_epb_mod_reg ( struct linda *linda, unsigned int cs,
1840 unsigned int channel, unsigned int element,
1841 unsigned int reg, unsigned int value,
1842 unsigned int mask ) {
1843 unsigned int location;
1847 DBG_DISABLE ( DBGLVL_IO );
1850 assert ( ( value & mask ) == value );
1852 /* Acquire bus ownership */
1853 if ( ( rc = linda_ib_epb_request ( linda ) ) != 0 )
1856 /* Read existing value, if necessary */
1857 location = LINDA_EPB_LOC ( cs, channel, element, reg );
1858 if ( (~mask) & 0xff ) {
1859 old_value = linda_ib_epb_read ( linda, location );
1860 if ( old_value < 0 ) {
1869 value = ( ( old_value & ~mask ) | value );
1870 DBGCP ( linda, "Linda %p CS %d EPB(%d,%d,%#02x) %#02x => %#02x\n",
1871 linda, cs, channel, element, reg, old_value, value );
1872 if ( ( rc = linda_ib_epb_write ( linda, location, value ) ) != 0 )
1877 linda_ib_epb_release ( linda );
1879 DBG_ENABLE ( DBGLVL_IO );
1884 * Transfer data to/from microcontroller RAM
1886 * @v linda Linda device
1887 * @v address Starting address
1888 * @v write Data to write, or NULL
1889 * @v read Data to read, or NULL
1890 * @v len Length of data
1891 * @ret rc Return status code
1893 static int linda_ib_epb_ram_xfer ( struct linda *linda, unsigned int address,
1894 const void *write, void *read,
1896 unsigned int control;
1897 unsigned int address_hi;
1898 unsigned int address_lo;
1902 DBG_DISABLE ( DBGLVL_IO );
1904 assert ( ! ( write && read ) );
1905 assert ( ( address % LINDA_EPB_UC_CHUNK_SIZE ) == 0 );
1906 assert ( ( len % LINDA_EPB_UC_CHUNK_SIZE ) == 0 );
1908 /* Acquire bus ownership */
1909 if ( ( rc = linda_ib_epb_request ( linda ) ) != 0 )
1915 /* Reset the address for each new chunk */
1916 if ( ( address % LINDA_EPB_UC_CHUNK_SIZE ) == 0 ) {
1918 /* Write the control register */
1919 control = ( read ? LINDA_EPB_UC_CTL_READ :
1920 LINDA_EPB_UC_CTL_WRITE );
1921 if ( ( rc = linda_ib_epb_write ( linda,
1926 /* Write the address registers */
1927 address_hi = ( address >> 8 );
1928 if ( ( rc = linda_ib_epb_write ( linda,
1929 LINDA_EPB_UC_ADDR_HI,
1930 address_hi ) ) != 0 )
1932 address_lo = ( address & 0xff );
1933 if ( ( rc = linda_ib_epb_write ( linda,
1934 LINDA_EPB_UC_ADDR_LO,
1935 address_lo ) ) != 0 )
1939 /* Read or write the data */
1941 data = linda_ib_epb_read ( linda, LINDA_EPB_UC_DATA );
1946 *( ( uint8_t * ) read++ ) = data;
1948 data = *( ( uint8_t * ) write++ );
1949 if ( ( rc = linda_ib_epb_write ( linda,
1957 /* Reset the control byte after each chunk */
1958 if ( ( address % LINDA_EPB_UC_CHUNK_SIZE ) == 0 ) {
1959 if ( ( rc = linda_ib_epb_write ( linda,
1967 linda_ib_epb_release ( linda );
1970 DBG_ENABLE ( DBGLVL_IO );
1974 /***************************************************************************
1976 * Infiniband SerDes initialisation
1978 ***************************************************************************
1981 /** A Linda SerDes parameter */
1982 struct linda_serdes_param {
1983 /** EPB address as constructed by LINDA_EPB_ADDRESS() */
1987 /** Mask to apply to old value */
1991 /** Magic "all channels" channel number */
1992 #define LINDA_EPB_ALL_CHANNELS 31
1994 /** End of SerDes parameter list marker */
1995 #define LINDA_SERDES_PARAM_END { 0, 0, 0 }
1998 * Program IB SerDes register(s)
2000 * @v linda Linda device
2001 * @v param SerDes parameter
2002 * @ret rc Return status code
2004 static int linda_set_serdes_param ( struct linda *linda,
2005 struct linda_serdes_param *param ) {
2006 unsigned int channel;
2007 unsigned int channel_start;
2008 unsigned int channel_end;
2009 unsigned int element;
2013 /* Break down the EPB address and determine channels */
2014 channel = LINDA_EPB_ADDRESS_CHANNEL ( param->address );
2015 element = LINDA_EPB_ADDRESS_ELEMENT ( param->address );
2016 reg = LINDA_EPB_ADDRESS_REG ( param->address );
2017 if ( channel == LINDA_EPB_ALL_CHANNELS ) {
2021 channel_start = channel_end = channel;
2024 /* Modify register for each specified channel */
2025 for ( channel = channel_start ; channel <= channel_end ; channel++ ) {
2026 if ( ( rc = linda_ib_epb_mod_reg ( linda, LINDA_EPB_CS_SERDES,
2027 channel, element, reg,
2029 param->mask ) ) != 0 )
2037 * Program IB SerDes registers
2039 * @v linda Linda device
2040 * @v param SerDes parameters
2041 * @v count Number of parameters
2042 * @ret rc Return status code
2044 static int linda_set_serdes_params ( struct linda *linda,
2045 struct linda_serdes_param *params ) {
2048 for ( ; params->mask != 0 ; params++ ){
2049 if ( ( rc = linda_set_serdes_param ( linda,
2057 #define LINDA_DDS_VAL( amp_d, main_d, ipst_d, ipre_d, \
2058 amp_s, main_s, ipst_s, ipre_s ) \
2059 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x00 ), \
2060 ( ( ( amp_d & 0x1f ) << 1 ) | 1 ), 0xff }, \
2061 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x01 ), \
2062 ( ( ( amp_s & 0x1f ) << 1 ) | 1 ), 0xff }, \
2063 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x09 ), \
2064 ( ( main_d << 3 ) | 4 | ( ipre_d >> 2 ) ), 0xff }, \
2065 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x0a ), \
2066 ( ( main_s << 3 ) | 4 | ( ipre_s >> 2 ) ), 0xff }, \
2067 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x06 ), \
2068 ( ( ( ipst_d & 0xf ) << 1 ) | \
2069 ( ( ipre_d & 3 ) << 6 ) | 0x21 ), 0xff }, \
2070 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x07 ), \
2071 ( ( ( ipst_s & 0xf ) << 1 ) | \
2072 ( ( ipre_s & 3 ) << 6) | 0x21 ), 0xff }
2075 * Linda SerDes default parameters
2077 * These magic start-of-day values are taken from the Linux driver.
2079 static struct linda_serdes_param linda_serdes_defaults1[] = {
2081 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x00 ), 0xd4, 0xff },
2083 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x05 ), 0x2d, 0xff },
2085 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x08 ), 0x03, 0x0f },
2087 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x27 ), 0x10, 0xff },
2089 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x28 ), 0x30, 0xff },
2091 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x0e ), 0x40, 0xff },
2093 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x06 ), 0x04, 0xff },
2095 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x0f ), 0x04, 0xff },
2096 /* End of this block */
2097 LINDA_SERDES_PARAM_END
2099 static struct linda_serdes_param linda_serdes_defaults2[] = {
2101 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x06 ), 0x00, 0xff },
2103 LINDA_DDS_VAL ( 31, 19, 12, 0, 29, 22, 9, 0 ),
2104 /* Set Rcv Eq. to Preset node */
2105 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x27 ), 0x10, 0xff },
2107 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x08 ), 0x00, 0xff },
2109 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x21 ), 0x00, 0xff },
2111 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x09 ), 0x02, 0xff },
2113 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x23 ), 0x02, 0xff },
2115 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x1b ), 0x0c, 0xff },
2117 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x1c ), 0x0c, 0xff },
2119 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x1e ), 0x10, 0xff },
2121 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x1f ), 0x10, 0xff },
2122 /* VCDL_CTRL0 toggle */
2123 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x06 ), 0x20, 0xff },
2124 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x06 ), 0x00, 0xff },
2126 { LINDA_EPB_ADDRESS ( 7, 0, 0x15 ), 0x80, 0xff },
2127 /* End of this block */
2128 LINDA_SERDES_PARAM_END
2130 static struct linda_serdes_param linda_serdes_defaults3[] = {
2132 { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x27 ), 0x00, 0x38 },
2133 /* End of this block */
2134 LINDA_SERDES_PARAM_END
2138 * Program the microcontroller RAM
2140 * @v linda Linda device
2141 * @ret rc Return status code
2143 static int linda_program_uc_ram ( struct linda *linda ) {
2146 if ( ( rc = linda_ib_epb_ram_xfer ( linda, 0, linda_ib_fw, NULL,
2147 sizeof ( linda_ib_fw ) ) ) != 0 ){
2148 DBGC ( linda, "Linda %p could not load IB firmware: %s\n",
2149 linda, strerror ( rc ) );
2157 * Verify the microcontroller RAM
2159 * @v linda Linda device
2160 * @ret rc Return status code
2162 static int linda_verify_uc_ram ( struct linda *linda ) {
2163 uint8_t verify[LINDA_EPB_UC_CHUNK_SIZE];
2164 unsigned int offset;
2167 for ( offset = 0 ; offset < sizeof ( linda_ib_fw );
2168 offset += sizeof ( verify ) ) {
2169 if ( ( rc = linda_ib_epb_ram_xfer ( linda, offset,
2171 sizeof (verify) )) != 0 ){
2172 DBGC ( linda, "Linda %p could not read back IB "
2173 "firmware: %s\n", linda, strerror ( rc ) );
2176 if ( memcmp ( ( linda_ib_fw + offset ), verify,
2177 sizeof ( verify ) ) != 0 ) {
2178 DBGC ( linda, "Linda %p firmware verification failed "
2179 "at offset %#x\n", linda, offset );
2180 DBGC_HDA ( linda, offset, ( linda_ib_fw + offset ),
2181 sizeof ( verify ) );
2182 DBGC_HDA ( linda, offset, verify, sizeof ( verify ) );
2187 DBGC2 ( linda, "Linda %p firmware verified ok\n", linda );
2192 * Use the microcontroller to trim the IB link
2194 * @v linda Linda device
2195 * @ret rc Return status code
2197 static int linda_trim_ib ( struct linda *linda ) {
2198 struct QIB_7220_IBSerDesCtrl ctrl;
2199 struct QIB_7220_IntStatus intstatus;
2203 /* Bring the microcontroller out of reset */
2204 linda_readq ( linda, &ctrl, QIB_7220_IBSerDesCtrl_offset );
2205 BIT_SET ( &ctrl, ResetIB_uC_Core, 0 );
2206 linda_writeq ( linda, &ctrl, QIB_7220_IBSerDesCtrl_offset );
2208 /* Wait for the "trim done" signal */
2209 for ( i = 0 ; i < LINDA_TRIM_DONE_MAX_WAIT_MS ; i++ ) {
2210 linda_readq ( linda, &intstatus, QIB_7220_IntStatus_offset );
2211 if ( BIT_GET ( &intstatus, IBSerdesTrimDone ) ) {
2218 DBGC ( linda, "Linda %p timed out waiting for trim done\n", linda );
2221 /* Put the microcontroller back into reset */
2222 BIT_SET ( &ctrl, ResetIB_uC_Core, 1 );
2223 linda_writeq ( linda, &ctrl, QIB_7220_IBSerDesCtrl_offset );
2229 * Initialise the IB SerDes
2231 * @v linda Linda device
2232 * @ret rc Return status code
2234 static int linda_init_ib_serdes ( struct linda *linda ) {
2235 struct QIB_7220_Control control;
2236 struct QIB_7220_IBCCtrl ibcctrl;
2237 struct QIB_7220_IBCDDRCtrl ibcddrctrl;
2238 struct QIB_7220_XGXSCfg xgxscfg;
2242 linda_readq ( linda, &control, QIB_7220_Control_offset );
2243 BIT_SET ( &control, LinkEn, 0 );
2244 linda_writeq ( linda, &control, QIB_7220_Control_offset );
2246 /* Configure sensible defaults for IBC */
2247 memset ( &ibcctrl, 0, sizeof ( ibcctrl ) );
2248 BIT_FILL_6 ( &ibcctrl, /* Tuning values taken from Linux driver */
2249 FlowCtrlPeriod, 0x03,
2250 FlowCtrlWaterMark, 0x05,
2251 MaxPktLen, ( ( LINDA_RECV_HEADER_SIZE +
2252 LINDA_RECV_PAYLOAD_SIZE +
2253 4 /* ICRC */ ) >> 2 ),
2254 PhyerrThreshold, 0xf,
2255 OverrunThreshold, 0xf,
2257 linda_writeq ( linda, &ibcctrl, QIB_7220_IBCCtrl_offset );
2259 /* Force SDR only to avoid needing all the DDR tuning,
2260 * Mellanox compatibility hacks etc. SDR is plenty for
2261 * boot-time operation.
2263 linda_readq ( linda, &ibcddrctrl, QIB_7220_IBCDDRCtrl_offset );
2264 BIT_SET ( &ibcddrctrl, IB_ENHANCED_MODE, 0 );
2265 BIT_SET ( &ibcddrctrl, SD_SPEED_SDR, 1 );
2266 BIT_SET ( &ibcddrctrl, SD_SPEED_DDR, 0 );
2267 BIT_SET ( &ibcddrctrl, SD_SPEED_QDR, 0 );
2268 BIT_SET ( &ibcddrctrl, HRTBT_ENB, 0 );
2269 BIT_SET ( &ibcddrctrl, HRTBT_AUTO, 0 );
2270 linda_writeq ( linda, &ibcddrctrl, QIB_7220_IBCDDRCtrl_offset );
2272 /* Set default SerDes parameters */
2273 if ( ( rc = linda_set_serdes_params ( linda,
2274 linda_serdes_defaults1 ) ) != 0 )
2276 udelay ( 415 ); /* Magic delay while SerDes sorts itself out */
2277 if ( ( rc = linda_set_serdes_params ( linda,
2278 linda_serdes_defaults2 ) ) != 0 )
2281 /* Program the microcontroller RAM */
2282 if ( ( rc = linda_program_uc_ram ( linda ) ) != 0 )
2285 /* Verify the microcontroller RAM contents */
2287 if ( ( rc = linda_verify_uc_ram ( linda ) ) != 0 )
2291 /* More SerDes tuning */
2292 if ( ( rc = linda_set_serdes_params ( linda,
2293 linda_serdes_defaults3 ) ) != 0 )
2296 /* Use the microcontroller to trim the IB link */
2297 if ( ( rc = linda_trim_ib ( linda ) ) != 0 )
2300 /* Bring XGXS out of reset */
2301 linda_readq ( linda, &xgxscfg, QIB_7220_XGXSCfg_offset );
2302 BIT_SET ( &xgxscfg, tx_rx_reset, 0 );
2303 BIT_SET ( &xgxscfg, xcv_reset, 0 );
2304 linda_writeq ( linda, &xgxscfg, QIB_7220_XGXSCfg_offset );
2309 /***************************************************************************
2311 * PCI layer interface
2313 ***************************************************************************
2321 * @ret rc Return status code
2323 static int linda_probe ( struct pci_device *pci ) {
2324 struct ib_device *ibdev;
2325 struct linda *linda;
2326 struct QIB_7220_Revision revision;
2329 /* Allocate Infiniband device */
2330 ibdev = alloc_ibdev ( sizeof ( *linda ) );
2333 goto err_alloc_ibdev;
2335 pci_set_drvdata ( pci, ibdev );
2336 linda = ib_get_drvdata ( ibdev );
2337 ibdev->op = &linda_ib_operations;
2338 ibdev->dev = &pci->dev;
2341 /* Fix up PCI device */
2342 adjust_pci_device ( pci );
2345 linda->regs = ioremap ( pci->membase, LINDA_BAR0_SIZE );
2346 DBGC2 ( linda, "Linda %p has BAR at %08lx\n", linda, pci->membase );
2348 /* Print some general data */
2349 linda_readq ( linda, &revision, QIB_7220_Revision_offset );
2350 DBGC2 ( linda, "Linda %p board %02lx v%ld.%ld.%ld.%ld\n", linda,
2351 BIT_GET ( &revision, BoardID ),
2352 BIT_GET ( &revision, R_SW ),
2353 BIT_GET ( &revision, R_Arch ),
2354 BIT_GET ( &revision, R_ChipRevMajor ),
2355 BIT_GET ( &revision, R_ChipRevMinor ) );
2357 /* Record link capabilities. Note that we force SDR only to
2358 * avoid having to carry extra code for DDR tuning etc.
2360 ibdev->link_width_enabled = ibdev->link_width_supported =
2361 ( IB_LINK_WIDTH_4X | IB_LINK_WIDTH_1X );
2362 ibdev->link_speed_enabled = ibdev->link_speed_supported =
2365 /* Initialise I2C subsystem */
2366 if ( ( rc = linda_init_i2c ( linda ) ) != 0 )
2369 /* Read EEPROM parameters */
2370 if ( ( rc = linda_read_eeprom ( linda, &ibdev->node_guid ) ) != 0 )
2371 goto err_read_eeprom;
2372 memcpy ( &ibdev->gid.s.guid, &ibdev->node_guid,
2373 sizeof ( ibdev->gid.s.guid ) );
2375 /* Initialise send datapath */
2376 if ( ( rc = linda_init_send ( linda ) ) != 0 )
2379 /* Initialise receive datapath */
2380 if ( ( rc = linda_init_recv ( linda ) ) != 0 )
2383 /* Initialise the IB SerDes */
2384 if ( ( rc = linda_init_ib_serdes ( linda ) ) != 0 )
2385 goto err_init_ib_serdes;
2387 /* Register Infiniband device */
2388 if ( ( rc = register_ibdev ( ibdev ) ) != 0 ) {
2389 DBGC ( linda, "Linda %p could not register IB "
2390 "device: %s\n", linda, strerror ( rc ) );
2391 goto err_register_ibdev;
2396 unregister_ibdev ( ibdev );
2398 linda_fini_recv ( linda );
2400 linda_fini_send ( linda );
2405 ibdev_put ( ibdev );
2415 static void linda_remove ( struct pci_device *pci ) {
2416 struct ib_device *ibdev = pci_get_drvdata ( pci );
2417 struct linda *linda = ib_get_drvdata ( ibdev );
2419 unregister_ibdev ( ibdev );
2420 linda_fini_recv ( linda );
2421 linda_fini_send ( linda );
2422 ibdev_put ( ibdev );
2425 static struct pci_device_id linda_nics[] = {
2426 PCI_ROM ( 0x1077, 0x7220, "iba7220", "QLE7240/7280 HCA driver", 0 ),
2429 struct pci_driver linda_driver __pci_driver = {
2431 .id_count = ( sizeof ( linda_nics ) / sizeof ( linda_nics[0] ) ),
2432 .probe = linda_probe,
2433 .remove = linda_remove,