Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / drivers / infiniband / linda.c
1 /*
2  * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <assert.h>
27 #include <ipxe/io.h>
28 #include <ipxe/pci.h>
29 #include <ipxe/infiniband.h>
30 #include <ipxe/i2c.h>
31 #include <ipxe/bitbash.h>
32 #include <ipxe/malloc.h>
33 #include <ipxe/iobuf.h>
34 #include "linda.h"
35
36 /**
37  * @file
38  *
39  * QLogic Linda Infiniband HCA
40  *
41  */
42
43 /** A Linda send work queue */
44 struct linda_send_work_queue {
45         /** Send buffer usage */
46         uint8_t *send_buf;
47         /** Producer index */
48         unsigned int prod;
49         /** Consumer index */
50         unsigned int cons;
51 };
52
53 /** A Linda receive work queue */
54 struct linda_recv_work_queue {
55         /** Receive header ring */
56         void *header;
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;
69 };
70
71 /** A Linda HCA */
72 struct linda {
73         /** Registers */
74         void *regs;
75
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];
82
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;
95
96         /** I2C bit-bashing interface */
97         struct i2c_bit_basher i2c;
98         /** I2C serial EEPROM */
99         struct i2c_device eeprom;
100 };
101
102 /***************************************************************************
103  *
104  * Linda register access
105  *
106  ***************************************************************************
107  *
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.
111  *
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).
115  */
116
117 /**
118  * Read Linda qword register
119  *
120  * @v linda             Linda device
121  * @v dwords            Register buffer to read into
122  * @v offset            Register offset
123  */
124 static void linda_readq ( struct linda *linda, uint32_t *dwords,
125                           unsigned long offset ) {
126         void *addr = ( linda->regs + offset );
127
128         __asm__ __volatile__ ( "movq (%1), %%mm0\n\t"
129                                "movq %%mm0, (%0)\n\t"
130                                : : "r" ( dwords ), "r" ( addr ) : "memory" );
131
132         DBGIO ( "[%08lx] => %08x%08x\n",
133                 virt_to_phys ( addr ), dwords[1], dwords[0] );
134 }
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 ) ) )
141
142 /**
143  * Write Linda qword register
144  *
145  * @v linda             Linda device
146  * @v dwords            Register buffer to write
147  * @v offset            Register offset
148  */
149 static void linda_writeq ( struct linda *linda, const uint32_t *dwords,
150                            unsigned long offset ) {
151         void *addr = ( linda->regs + offset );
152
153         DBGIO ( "[%08lx] <= %08x%08x\n",
154                 virt_to_phys ( addr ), dwords[1], dwords[0] );
155
156         __asm__ __volatile__ ( "movq (%0), %%mm0\n\t"
157                                "movq %%mm0, (%1)\n\t"
158                                : : "r" ( dwords ), "r" ( addr ) : "memory" );
159 }
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 ) ) )
166
167 /**
168  * Write Linda dword register
169  *
170  * @v linda             Linda device
171  * @v dword             Value to write
172  * @v offset            Register offset
173  */
174 static void linda_writel ( struct linda *linda, uint32_t dword,
175                            unsigned long offset ) {
176         writel ( dword, ( linda->regs + offset ) );
177 }
178
179 /***************************************************************************
180  *
181  * Link state management
182  *
183  ***************************************************************************
184  */
185
186 /**
187  * Textual representation of link state
188  *
189  * @v link_state        Link state
190  * @ret link_text       Link state text
191  */
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";
200         }
201 }
202
203 /**
204  * Handle link state change
205  *
206  * @v linda             Linda device
207  */
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;
215
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" ) );
224
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 );
232
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 );
240 }
241
242 /**
243  * Wait for link state change to take effect
244  *
245  * @v linda             Linda device
246  * @v new_link_state    Expected link state
247  * @ret rc              Return status code
248  */
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;
253         unsigned int i;
254
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 )
259                         return 0;
260                 udelay ( 1 );
261         }
262
263         DBGC ( linda, "Linda %p timed out waiting for link state %s\n",
264                linda, linda_link_state_text ( link_state ) );
265         return -ETIMEDOUT;
266 }
267
268 /**
269  * Set port information
270  *
271  * @v ibdev             Infiniband device
272  * @v mad               Set port information MAD
273  */
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;
280
281         /* Set new link state */
282         port_state = ( port_info->link_speed_supported__port_state & 0xf );
283         if ( port_state ) {
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 );
290
291                 /* Wait for link state change to take effect.  Ignore
292                  * errors; the current link state will be returned via
293                  * the GetResponse MAD.
294                  */
295                 linda_link_state_check ( linda, link_state );
296         }
297
298         /* Detect and report link state change */
299         linda_link_state_changed ( ibdev );
300
301         return 0;
302 }
303
304 /**
305  * Set partition key table
306  *
307  * @v ibdev             Infiniband device
308  * @v mad               Set partition key table MAD
309  */
310 static int linda_set_pkey_table ( struct ib_device *ibdev __unused,
311                                   union ib_mad *mad __unused ) {
312         /* Nothing to do */
313         return 0;
314 }
315
316 /***************************************************************************
317  *
318  * Context allocation
319  *
320  ***************************************************************************
321  */
322
323 /**
324  * Map context number to QPN
325  *
326  * @v ctx               Context index
327  * @ret qpn             Queue pair number
328  */
329 static int linda_ctx_to_qpn ( unsigned int ctx ) {
330         /* This mapping is fixed by hardware */
331         return ( ctx * 2 );
332 }
333
334 /**
335  * Map QPN to context number
336  *
337  * @v qpn               Queue pair number
338  * @ret ctx             Context index
339  */
340 static int linda_qpn_to_ctx ( unsigned int qpn ) {
341         /* This mapping is fixed by hardware */
342         return ( qpn / 2 );
343 }
344
345 /**
346  * Allocate a context
347  *
348  * @v linda             Linda device
349  * @ret ctx             Context index, or negative error
350  */
351 static int linda_alloc_ctx ( struct linda *linda ) {
352         unsigned int ctx;
353
354         for ( ctx = 0 ; ctx < LINDA_NUM_CONTEXTS ; ctx++ ) {
355
356                 if ( ! linda->used_ctx[ctx] ) {
357                         linda->used_ctx[ctx ] = 1;
358                         DBGC2 ( linda, "Linda %p CTX %d allocated\n",
359                                 linda, ctx );
360                         return ctx;
361                 }
362         }
363
364         DBGC ( linda, "Linda %p out of available contexts\n", linda );
365         return -ENOENT;
366 }
367
368 /**
369  * Free a context
370  *
371  * @v linda             Linda device
372  * @v ctx               Context index
373  */
374 static void linda_free_ctx ( struct linda *linda, unsigned int ctx ) {
375
376         linda->used_ctx[ctx] = 0;
377         DBGC2 ( linda, "Linda %p CTX %d freed\n", linda, ctx );
378 }
379
380 /***************************************************************************
381  *
382  * Send datapath
383  *
384  ***************************************************************************
385  */
386
387 /** Send buffer toggle bit
388  *
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.
391  */
392 #define LINDA_SEND_BUF_TOGGLE 0x80
393
394 /**
395  * Allocate a send buffer
396  *
397  * @v linda             Linda device
398  * @ret send_buf        Send buffer
399  *
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.
403  */
404 static unsigned int linda_alloc_send_buf ( struct linda *linda ) {
405         unsigned int send_buf;
406
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 );
411         return send_buf;
412 }
413
414 /**
415  * Free a send buffer
416  *
417  * @v linda             Linda device
418  * @v send_buf          Send buffer
419  */
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 );
425 }
426
427 /**
428  * Check to see if send buffer is in use
429  *
430  * @v linda             Linda device
431  * @v send_buf          Send buffer
432  * @ret in_use          Send buffer is in use
433  */
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;
439         unsigned int inuse;
440         unsigned int check;
441
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 ) );
448 }
449
450 /**
451  * Calculate starting offset for send buffer
452  *
453  * @v linda             Linda device
454  * @v send_buf          Send buffer
455  * @ret offset          Starting offset
456  */
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 ) );
462 }
463
464 /**
465  * Create send work queue
466  *
467  * @v linda             Linda device
468  * @v qp                Queue pair
469  */
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 );
474         int rc;
475
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 );
482                 rc = -ENOBUFS;
483                 goto err_reserve_bufs;
484         }
485         linda->reserved_send_bufs += qp->send.num_wqes;
486
487         /* Reset work queue */
488         linda_wq->prod = 0;
489         linda_wq->cons = 0;
490
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 ) {
495                 rc = -ENOBUFS;
496                 goto err_alloc_send_buf;
497         }
498
499         return 0;
500
501         free ( linda_wq->send_buf );
502  err_alloc_send_buf:
503         linda->reserved_send_bufs -= qp->send.num_wqes;
504  err_reserve_bufs:
505         return rc;
506 }
507
508 /**
509  * Destroy send work queue
510  *
511  * @v linda             Linda device
512  * @v qp                Queue pair
513  */
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 );
518
519         free ( linda_wq->send_buf );
520         linda->reserved_send_bufs -= qp->send.num_wqes;
521 }
522
523 /**
524  * Initialise send datapath
525  *
526  * @v linda             Linda device
527  * @ret rc              Return status code
528  */
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;
533         unsigned int i;
534         int rc;
535
536         /* Retrieve SendBufBase */
537         linda_readq ( linda, &sendbufbase, QIB_7220_SendBufBase_offset );
538         linda->send_buffer_base = BIT_GET ( &sendbufbase,
539                                             BaseAddr_SmallPIO );
540         DBGC ( linda, "Linda %p send buffers at %lx\n",
541                linda, linda->send_buffer_base );
542
543         /* Initialise the send_buf[] array */
544         for ( i = 0 ; i < LINDA_MAX_SEND_BUFS ; i++ )
545                 linda->send_buf[i] = i;
546
547         /* Allocate space for the SendBufAvail array */
548         linda->sendbufavail = malloc_dma ( sizeof ( *linda->sendbufavail ),
549                                            LINDA_SENDBUFAVAIL_ALIGN );
550         if ( ! linda->sendbufavail ) {
551                 rc = -ENOMEM;
552                 goto err_alloc_sendbufavail;
553         }
554         memset ( linda->sendbufavail, 0, sizeof ( linda->sendbufavail ) );
555
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 );
562
563         /* Enable sending and DMA of SendBufAvail */
564         memset ( &sendctrl, 0, sizeof ( sendctrl ) );
565         BIT_FILL_2 ( &sendctrl,
566                      SendBufAvailUpd, 1,
567                      SPioEnable, 1 );
568         linda_writeq ( linda, &sendctrl, QIB_7220_SendCtrl_offset );
569
570         return 0;
571
572         free_dma ( linda->sendbufavail, sizeof ( *linda->sendbufavail ) );
573  err_alloc_sendbufavail:
574         return rc;
575 }
576
577 /**
578  * Shut down send datapath
579  *
580  * @v linda             Linda device
581  */
582 static void linda_fini_send ( struct linda *linda ) {
583         struct QIB_7220_SendCtrl sendctrl;
584
585         /* Disable sending and DMA of SendBufAvail */
586         memset ( &sendctrl, 0, sizeof ( sendctrl ) );
587         linda_writeq ( linda, &sendctrl, QIB_7220_SendCtrl_offset );
588         mb();
589
590         /* Ensure hardware has seen this disable */
591         linda_readq ( linda, &sendctrl, QIB_7220_SendCtrl_offset );
592
593         free_dma ( linda->sendbufavail, sizeof ( *linda->sendbufavail ) );
594 }
595
596 /***************************************************************************
597  *
598  * Receive datapath
599  *
600  ***************************************************************************
601  */
602
603 /**
604  * Create receive work queue
605  *
606  * @v linda             Linda device
607  * @v qp                Queue pair
608  * @ret rc              Return status code
609  */
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 );
620         int rc;
621
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;
628
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 ) {
633                 rc = -ENOMEM;
634                 goto err_alloc_header;
635         }
636
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 );
660
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 ) );
665         return 0;
666
667         free_dma ( linda_wq->header, LINDA_RECV_HEADERS_SIZE );
668  err_alloc_header:
669         return rc;
670 }
671
672 /**
673  * Destroy receive work queue
674  *
675  * @v linda             Linda device
676  * @v qp                Queue pair
677  */
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 );
684
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 );
690
691         /* Make sure the hardware has seen that the context is disabled */
692         linda_readq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
693         mb();
694
695         /* Free headers ring */
696         free_dma ( linda_wq->header, LINDA_RECV_HEADERS_SIZE );
697
698         /* Free context */
699         linda_free_ctx ( linda, ctx );
700 }
701
702 /**
703  * Initialise receive datapath
704  *
705  * @v linda             Linda device
706  * @ret rc              Return status code
707  */
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;
718         unsigned int ctx;
719
720         /* Select configuration based on number of contexts */
721         switch ( LINDA_NUM_CONTEXTS ) {
722         case 5:
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;
726                 break;
727         case 9:
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;
731                 break;
732         case 17:
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;
736                 break;
737         default:
738                 linker_assert ( 0, invalid_LINDA_NUM_CONTEXTS );
739                 return -EINVAL;
740         }
741
742         /* Configure number of contexts */
743         memset ( &rcvctrl, 0, sizeof ( rcvctrl ) );
744         BIT_FILL_3 ( &rcvctrl,
745                      TailUpd, 1,
746                      PortCfg, portcfg,
747                      RcvQPMapEnable, 1 );
748         linda_writeq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
749
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 );
757
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 ) );
769         }
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 );
775         }
776
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 );
781
782         return 0;
783 }
784
785 /**
786  * Shut down receive datapath
787  *
788  * @v linda             Linda device
789  */
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
793          */
794 }
795
796 /***************************************************************************
797  *
798  * Completion queue operations
799  *
800  ***************************************************************************
801  */
802
803 /**
804  * Create completion queue
805  *
806  * @v ibdev             Infiniband device
807  * @v cq                Completion queue
808  * @ret rc              Return status code
809  */
810 static int linda_create_cq ( struct ib_device *ibdev,
811                              struct ib_completion_queue *cq ) {
812         struct linda *linda = ib_get_drvdata ( ibdev );
813         static int cqn;
814
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.
818          *
819          * We do set a CQN, just to avoid confusing debug messages
820          * from the IB core.
821          */
822         cq->cqn = ++cqn;
823         DBGC ( linda, "Linda %p CQN %ld created\n", linda, cq->cqn );
824
825         return 0;
826 }
827
828 /**
829  * Destroy completion queue
830  *
831  * @v ibdev             Infiniband device
832  * @v cq                Completion queue
833  */
834 static void linda_destroy_cq ( struct ib_device *ibdev,
835                                struct ib_completion_queue *cq ) {
836         struct linda *linda = ib_get_drvdata ( ibdev );
837
838         /* Nothing to do */
839         DBGC ( linda, "Linda %p CQN %ld destroyed\n", linda, cq->cqn );
840 }
841
842 /***************************************************************************
843  *
844  * Queue pair operations
845  *
846  ***************************************************************************
847  */
848
849 /**
850  * Create queue pair
851  *
852  * @v ibdev             Infiniband device
853  * @v qp                Queue pair
854  * @ret rc              Return status code
855  */
856 static int linda_create_qp ( struct ib_device *ibdev,
857                              struct ib_queue_pair *qp ) {
858         struct linda *linda = ib_get_drvdata ( ibdev );
859         int ctx;
860         int rc;
861
862         /* Locate an available context */
863         ctx = linda_alloc_ctx ( linda );
864         if ( ctx < 0 ) {
865                 rc = ctx;
866                 goto err_alloc_ctx;
867         }
868
869         /* Set queue pair number based on context index */
870         qp->qpn = linda_ctx_to_qpn ( ctx );
871
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] );
875
876         /* Create receive work queue */
877         if ( ( rc = linda_create_recv_wq ( linda, qp ) ) != 0 )
878                 goto err_create_recv_wq;
879
880         /* Create send work queue */
881         if ( ( rc = linda_create_send_wq ( linda, qp ) ) != 0 )
882                 goto err_create_send_wq;
883
884         return 0;
885
886         linda_destroy_send_wq ( linda, qp );
887  err_create_send_wq:
888         linda_destroy_recv_wq ( linda, qp );
889  err_create_recv_wq:
890         linda_free_ctx ( linda, ctx );
891  err_alloc_ctx:
892         return rc;
893 }
894
895 /**
896  * Modify queue pair
897  *
898  * @v ibdev             Infiniband device
899  * @v qp                Queue pair
900  * @ret rc              Return status code
901  */
902 static int linda_modify_qp ( struct ib_device *ibdev,
903                              struct ib_queue_pair *qp ) {
904         struct linda *linda = ib_get_drvdata ( ibdev );
905
906         /* Nothing to do; the hardware doesn't have a notion of queue
907          * keys
908          */
909         DBGC ( linda, "Linda %p QPN %ld modified\n", linda, qp->qpn );
910         return 0;
911 }
912
913 /**
914  * Destroy queue pair
915  *
916  * @v ibdev             Infiniband device
917  * @v qp                Queue pair
918  */
919 static void linda_destroy_qp ( struct ib_device *ibdev,
920                                struct ib_queue_pair *qp ) {
921         struct linda *linda = ib_get_drvdata ( ibdev );
922
923         linda_destroy_send_wq ( linda, qp );
924         linda_destroy_recv_wq ( linda, qp );
925 }
926
927 /***************************************************************************
928  *
929  * Work request operations
930  *
931  ***************************************************************************
932  */
933
934 /**
935  * Post send work queue entry
936  *
937  * @v ibdev             Infiniband device
938  * @v qp                Queue pair
939  * @v dest              Destination address vector
940  * @v iobuf             I/O buffer
941  * @ret rc              Return status code
942  */
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;
956         size_t len;
957         ssize_t frag_len;
958         uint32_t *data;
959
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 );
963
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;
968
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 );
973
974         /* Calculate packet length */
975         len = ( ( sizeof ( sendpbc ) + iob_len ( &headers ) +
976                   iob_len ( iobuf ) + 3 ) & ~3 );
977
978         /* Construct send per-buffer control word */
979         memset ( &sendpbc, 0, sizeof ( sendpbc ) );
980         BIT_FILL_2 ( &sendpbc,
981                      LengthP1_toibc, ( ( len >> 2 ) - 1 ),
982                      VL15, 1 );
983
984         /* Write SendPbc */
985         DBG_DISABLE ( DBGLVL_IO );
986         linda_writeq ( linda, &sendpbc, offset );
987         offset += sizeof ( sendpbc );
988
989         /* Write headers */
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 );
993         }
994
995         /* Write data */
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 );
999         }
1000         DBG_ENABLE ( DBGLVL_IO );
1001
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 );
1006
1007         /* Increment producer counter */
1008         linda_wq->prod = ( ( linda_wq->prod + 1 ) & ( wq->num_wqes - 1 ) );
1009
1010         return 0;
1011 }
1012
1013 /**
1014  * Complete send work queue entry
1015  *
1016  * @v ibdev             Infiniband device
1017  * @v qp                Queue pair
1018  * @v wqe_idx           Work queue entry index
1019  */
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;
1028
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 );
1033
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;
1039
1040         /* Free send buffer */
1041         linda_free_send_buf ( linda, send_buf );
1042 }
1043
1044 /**
1045  * Poll send work queue
1046  *
1047  * @v ibdev             Infiniband device
1048  * @v qp                Queue pair
1049  */
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;
1056
1057         /* Look for completions */
1058         while ( wq->fill ) {
1059
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 ) )
1063                         break;
1064
1065                 /* Complete this buffer */
1066                 linda_complete_send ( ibdev, qp, linda_wq->cons );
1067
1068                 /* Increment consumer counter */
1069                 linda_wq->cons = ( ( linda_wq->cons + 1 ) &
1070                                    ( wq->num_wqes - 1 ) );
1071         }
1072 }
1073
1074 /**
1075  * Post receive work queue entry
1076  *
1077  * @v ibdev             Infiniband device
1078  * @v qp                Queue pair
1079  * @v iobuf             I/O buffer
1080  * @ret rc              Return status code
1081  */
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 );
1091         physaddr_t addr;
1092         size_t len;
1093         unsigned int wqe_idx;
1094         unsigned int bufsize;
1095
1096         /* Sanity checks */
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 );
1102                 return -EINVAL;
1103         }
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 );
1107                 return -EINVAL;
1108         }
1109
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 );
1113
1114         /* Store I/O buffer */
1115         wq->iobufs[wqe_idx] = iobuf;
1116
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;
1127         }
1128
1129         /* Post eager buffer */
1130         memset ( &rcvegr, 0, sizeof ( rcvegr ) );
1131         BIT_FILL_2 ( &rcvegr,
1132                      Addr, ( addr >> 11 ),
1133                      BufSize, bufsize );
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 ) );
1139
1140         /* Increment producer index */
1141         linda_wq->eager_prod = ( ( linda_wq->eager_prod + 1 ) &
1142                                  ( linda_wq->eager_entries - 1 ) );
1143
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 );
1151
1152         return 0;
1153 }
1154
1155 /**
1156  * Complete receive work queue entry
1157  *
1158  * @v ibdev             Infiniband device
1159  * @v qp                Queue pair
1160  * @v header_offs       Header offset
1161  */
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;
1181         unsigned int err;
1182         unsigned int hdrqoffset;
1183         unsigned int header_len;
1184         unsigned int padded_payload_len;
1185         unsigned int wqe_idx;
1186         size_t payload_len;
1187         int qp0;
1188         int rc;
1189
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 );
1215
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 ? "]" : "" ) );
1229         } else {
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 );
1234         }
1235         DBGCP_HDA ( linda, hdrqoffset, headers.data,
1236                     ( header_len + sizeof ( *rcvhdrflags ) ) );
1237
1238         /* Parse header to generate address vector */
1239         qp0 = ( qp->qpn == 0 );
1240         intended_qp = NULL;
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 ) );
1245                 err = 1;
1246         }
1247         if ( ! intended_qp )
1248                 intended_qp = qp;
1249
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
1254          * buffers at once.
1255          */
1256         while ( 1 ) {
1257
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.
1262                  */
1263                 if ( linda_wq->eager_cons == linda_wq->eager_prod )
1264                         break;
1265
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.
1270                  */
1271                 if ( ( linda_wq->eager_cons ==
1272                        ( ( egrindex + 1 ) & ( linda_wq->eager_entries - 1 ) )))
1273                         break;
1274
1275                 /* Identify work queue entry and corresponding I/O
1276                  * buffer.
1277                  */
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;
1282
1283                 /* Complete the eager buffer */
1284                 if ( linda_wq->eager_cons == egrindex ) {
1285                         /* Completing the eager buffer described in
1286                          * this header entry.
1287                          */
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 "
1293                                        "=> %ld\n",
1294                                        linda, qp->qpn, intended_qp->qpn );
1295                                 /* Compensate for incorrect fill levels */
1296                                 qp->recv.fill--;
1297                                 intended_qp->recv.fill++;
1298                         }
1299                         ib_complete_recv ( ibdev, intended_qp, &dest, &source,
1300                                            iobuf, rc);
1301                 } else {
1302                         /* Completing on a skipped-over eager buffer */
1303                         ib_complete_recv ( ibdev, qp, &dest, &source, iobuf,
1304                                            -ECANCELED );
1305                 }
1306
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 );
1311
1312                 /* Increment consumer index */
1313                 linda_wq->eager_cons = ( ( linda_wq->eager_cons + 1 ) &
1314                                          ( linda_wq->eager_entries - 1 ) );
1315         }
1316 }
1317
1318 /**
1319  * Poll receive work queue
1320  *
1321  * @v ibdev             Infiniband device
1322  * @v qp                Queue pair
1323  */
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;
1332
1333         /* Check for received packets */
1334         header_prod = ( BIT_GET ( &linda_wq->header_prod, Value ) << 2 );
1335         if ( header_prod == linda_wq->header_cons )
1336                 return;
1337
1338         /* Process all received packets */
1339         while ( linda_wq->header_cons != header_prod ) {
1340
1341                 /* Complete the receive */
1342                 linda_complete_recv ( ibdev, qp, linda_wq->header_cons );
1343
1344                 /* Increment the consumer offset */
1345                 linda_wq->header_cons += LINDA_RECV_HEADER_SIZE;
1346                 linda_wq->header_cons %= LINDA_RECV_HEADERS_SIZE;
1347         }
1348
1349         /* Update consumer offset */
1350         memset ( &rcvhdrhead, 0, sizeof ( rcvhdrhead ) );
1351         BIT_FILL_2 ( &rcvhdrhead,
1352                      RcvHeadPointer, ( linda_wq->header_cons >> 2 ),
1353                      counter, 1 );
1354         linda_writeq_array64k ( linda, &rcvhdrhead,
1355                                 QIB_7220_RcvHdrHead0_offset, ctx );
1356 }
1357
1358 /**
1359  * Poll completion queue
1360  *
1361  * @v ibdev             Infiniband device
1362  * @v cq                Completion queue
1363  */
1364 static void linda_poll_cq ( struct ib_device *ibdev,
1365                             struct ib_completion_queue *cq ) {
1366         struct ib_work_queue *wq;
1367
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 );
1372                 } else {
1373                         linda_poll_recv_wq ( ibdev, wq->qp );
1374                 }
1375         }
1376 }
1377
1378 /***************************************************************************
1379  *
1380  * Event queues
1381  *
1382  ***************************************************************************
1383  */
1384
1385 /**
1386  * Poll event queue
1387  *
1388  * @v ibdev             Infiniband device
1389  */
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;
1394
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 );
1404         }
1405 }
1406
1407 /***************************************************************************
1408  *
1409  * Infiniband link-layer operations
1410  *
1411  ***************************************************************************
1412  */
1413
1414 /**
1415  * Initialise Infiniband link
1416  *
1417  * @v ibdev             Infiniband device
1418  * @ret rc              Return status code
1419  */
1420 static int linda_open ( struct ib_device *ibdev ) {
1421         struct linda *linda = ib_get_drvdata ( ibdev );
1422         struct QIB_7220_Control control;
1423
1424         /* Disable link */
1425         linda_readq ( linda, &control, QIB_7220_Control_offset );
1426         BIT_SET ( &control, LinkEn, 1 );
1427         linda_writeq ( linda, &control, QIB_7220_Control_offset );
1428         return 0;
1429 }
1430
1431 /**
1432  * Close Infiniband link
1433  *
1434  * @v ibdev             Infiniband device
1435  */
1436 static void linda_close ( struct ib_device *ibdev ) {
1437         struct linda *linda = ib_get_drvdata ( ibdev );
1438         struct QIB_7220_Control control;
1439
1440         /* Disable link */
1441         linda_readq ( linda, &control, QIB_7220_Control_offset );
1442         BIT_SET ( &control, LinkEn, 0 );
1443         linda_writeq ( linda, &control, QIB_7220_Control_offset );
1444 }
1445
1446 /***************************************************************************
1447  *
1448  * Multicast group operations
1449  *
1450  ***************************************************************************
1451  */
1452
1453 /**
1454  * Attach to multicast group
1455  *
1456  * @v ibdev             Infiniband device
1457  * @v qp                Queue pair
1458  * @v gid               Multicast GID
1459  * @ret rc              Return status code
1460  */
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 );
1465
1466         ( void ) linda;
1467         ( void ) qp;
1468         ( void ) gid;
1469         return 0;
1470 }
1471
1472 /**
1473  * Detach from multicast group
1474  *
1475  * @v ibdev             Infiniband device
1476  * @v qp                Queue pair
1477  * @v gid               Multicast GID
1478  */
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 );
1483
1484         ( void ) linda;
1485         ( void ) qp;
1486         ( void ) gid;
1487 }
1488
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,
1500         .open           = linda_open,
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,
1506 };
1507
1508 /***************************************************************************
1509  *
1510  * I2C bus operations
1511  *
1512  ***************************************************************************
1513  */
1514
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 ),
1519 };
1520
1521 /**
1522  * Read Linda I2C line status
1523  *
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
1528  */
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;
1535
1536         DBG_DISABLE ( DBGLVL_IO );
1537
1538         linda_readq ( linda, &extstatus, QIB_7220_EXTStatus_offset );
1539         status = ( BIT_GET ( &extstatus, GPIOIn ) & linda_i2c_bits[bit_id] );
1540
1541         DBG_ENABLE ( DBGLVL_IO );
1542
1543         return status;
1544 }
1545
1546 /**
1547  * Write Linda I2C line status
1548  *
1549  * @v basher            Bit-bashing interface
1550  * @v bit_id            Bit number
1551  * @v data              Value to write
1552  */
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;
1562
1563         DBG_DISABLE ( DBGLVL_IO );
1564
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 );
1568
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.
1572          */
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 );
1579
1580         /* Write the output enable first; that way we avoid logic
1581          * hazards.
1582          */
1583         linda_writeq ( linda, &extctrl, QIB_7220_EXTCtrl_offset );
1584         linda_writeq ( linda, &gpioout, QIB_7220_GPIOOut_offset );
1585         mb();
1586
1587         DBG_ENABLE ( DBGLVL_IO );
1588 }
1589
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,
1594 };
1595
1596 /**
1597  * Initialise Linda I2C subsystem
1598  *
1599  * @v linda             Linda device
1600  * @ret rc              Return status code
1601  */
1602 static int linda_init_i2c ( struct linda *linda ) {
1603         static int try_eeprom_address[] = { 0x51, 0x50 };
1604         unsigned int i;
1605         int rc;
1606
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 ) );
1612                 return rc;
1613         }
1614
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] );
1623                         return 0;
1624                 }
1625         }
1626
1627         DBGC ( linda, "Linda %p could not find EEPROM\n", linda );
1628         return -ENODEV;
1629 }
1630
1631 /**
1632  * Read EEPROM parameters
1633  *
1634  * @v linda             Linda device
1635  * @v guid              GUID to fill in
1636  * @ret rc              Return status code
1637  */
1638 static int linda_read_eeprom ( struct linda *linda, union ib_guid *guid ) {
1639         struct i2c_interface *i2c = &linda->i2c.i2c;
1640         int rc;
1641
1642         /* Read GUID */
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 ) );
1647                 return rc;
1648         }
1649         DBGC2 ( linda, "Linda %p has GUID " IB_GUID_FMT "\n",
1650                 linda, IB_GUID_ARGS ( guid ) );
1651
1652         /* Read serial number (debug only) */
1653         if ( DBG_LOG ) {
1654                 uint8_t serial[LINDA_EEPROM_SERIAL_SIZE + 1];
1655
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 ) );
1662                         return rc;
1663                 }
1664                 DBGC2 ( linda, "Linda %p has serial number \"%s\"\n",
1665                         linda, serial );
1666         }
1667
1668         return 0;
1669 }
1670
1671 /***************************************************************************
1672  *
1673  * External parallel bus access
1674  *
1675  ***************************************************************************
1676  */
1677
1678 /**
1679  * Request ownership of the IB external parallel bus
1680  *
1681  * @v linda             Linda device
1682  * @ret rc              Return status code
1683  */
1684 static int linda_ib_epb_request ( struct linda *linda ) {
1685         struct QIB_7220_ibsd_epb_access_ctrl access;
1686         unsigned int i;
1687
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 );
1692
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 ) )
1698                         return 0;
1699                 udelay ( 1 );
1700         }
1701
1702         DBGC ( linda, "Linda %p timed out waiting for IB EPB request\n",
1703                linda );
1704         return -ETIMEDOUT;
1705 }
1706
1707 /**
1708  * Wait for IB external parallel bus transaction to complete
1709  *
1710  * @v linda             Linda device
1711  * @v xact              Buffer to hold transaction result
1712  * @ret rc              Return status code
1713  */
1714 static int linda_ib_epb_wait ( struct linda *linda,
1715                             struct QIB_7220_ibsd_epb_transaction_reg *xact ) {
1716         unsigned int i;
1717
1718         /* Discard first read to allow for signals crossing clock domains */
1719         linda_readq ( linda, xact, QIB_7220_ibsd_epb_transaction_reg_offset );
1720
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 );
1728                                 return -EIO;
1729                         } else {
1730                                 return 0;
1731                         }
1732                 }
1733                 udelay ( 1 );
1734         }
1735
1736         DBGC ( linda, "Linda %p timed out waiting for IB EPB transaction\n",
1737                linda );
1738         return -ETIMEDOUT;
1739 }
1740
1741 /**
1742  * Release ownership of the IB external parallel bus
1743  *
1744  * @v linda             Linda device
1745  */
1746 static void linda_ib_epb_release ( struct linda *linda ) {
1747         struct QIB_7220_ibsd_epb_access_ctrl access;
1748
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 );
1752 }
1753
1754 /**
1755  * Read data via IB external parallel bus
1756  *
1757  * @v linda             Linda device
1758  * @v location          EPB location
1759  * @ret data            Data read, or negative error
1760  *
1761  * You must have already acquired ownership of the IB external
1762  * parallel bus.
1763  */
1764 static int linda_ib_epb_read ( struct linda *linda, unsigned int location ) {
1765         struct QIB_7220_ibsd_epb_transaction_reg xact;
1766         unsigned int data;
1767         int rc;
1768
1769         /* Ensure no transaction is currently in progress */
1770         if ( ( rc = linda_ib_epb_wait ( linda, &xact ) ) != 0 )
1771                 return rc;
1772
1773         /* Process data */
1774         memset ( &xact, 0, sizeof ( xact ) );
1775         BIT_FILL_3 ( &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 );
1781
1782         /* Wait for transaction to complete */
1783         if ( ( rc = linda_ib_epb_wait ( linda, &xact ) ) != 0 )
1784                 return rc;
1785
1786         data = BIT_GET ( &xact, ib_epb_data );
1787         return data;
1788 }
1789
1790 /**
1791  * Write data via IB external parallel bus
1792  *
1793  * @v linda             Linda device
1794  * @v location          EPB location
1795  * @v data              Data to write
1796  * @ret rc              Return status code
1797  *
1798  * You must have already acquired ownership of the IB external
1799  * parallel bus.
1800  */
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;
1804         int rc;
1805
1806         /* Ensure no transaction is currently in progress */
1807         if ( ( rc = linda_ib_epb_wait ( linda, &xact ) ) != 0 )
1808                 return rc;
1809
1810         /* Process data */
1811         memset ( &xact, 0, sizeof ( xact ) );
1812         BIT_FILL_4 ( &xact,
1813                      ib_epb_data, data,
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 );
1819
1820         /* Wait for transaction to complete */
1821         if ( ( rc = linda_ib_epb_wait ( linda, &xact ) ) != 0 )
1822                 return rc;
1823
1824         return 0;
1825 }
1826
1827 /**
1828  * Read/modify/write EPB register
1829  *
1830  * @v linda             Linda device
1831  * @v cs                Chip select
1832  * @v channel           Channel
1833  * @v element           Element
1834  * @v reg               Register
1835  * @v value             Value to set
1836  * @v mask              Mask to apply to old value
1837  * @ret rc              Return status code
1838  */
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;
1844         int old_value;
1845         int rc;
1846
1847         DBG_DISABLE ( DBGLVL_IO );
1848
1849         /* Sanity check */
1850         assert ( ( value & mask ) == value );
1851
1852         /* Acquire bus ownership */
1853         if ( ( rc = linda_ib_epb_request ( linda ) ) != 0 )
1854                 goto out;
1855
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 ) {
1861                         rc = old_value;
1862                         goto out_release;
1863                 }
1864         } else {
1865                 old_value = 0;
1866         }
1867
1868         /* Update value */
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 )
1873                 goto out_release;
1874
1875  out_release:
1876         /* Release bus */
1877         linda_ib_epb_release ( linda );
1878  out:
1879         DBG_ENABLE ( DBGLVL_IO );
1880         return rc;
1881 }
1882
1883 /**
1884  * Transfer data to/from microcontroller RAM
1885  *
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
1892  */
1893 static int linda_ib_epb_ram_xfer ( struct linda *linda, unsigned int address,
1894                                    const void *write, void *read,
1895                                    size_t len ) {
1896         unsigned int control;
1897         unsigned int address_hi;
1898         unsigned int address_lo;
1899         int data;
1900         int rc;
1901
1902         DBG_DISABLE ( DBGLVL_IO );
1903
1904         assert ( ! ( write && read ) );
1905         assert ( ( address % LINDA_EPB_UC_CHUNK_SIZE ) == 0 );
1906         assert ( ( len % LINDA_EPB_UC_CHUNK_SIZE ) == 0 );
1907
1908         /* Acquire bus ownership */
1909         if ( ( rc = linda_ib_epb_request ( linda ) ) != 0 )
1910                 goto out;
1911
1912         /* Process data */
1913         while ( len ) {
1914
1915                 /* Reset the address for each new chunk */
1916                 if ( ( address % LINDA_EPB_UC_CHUNK_SIZE ) == 0 ) {
1917
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,
1922                                                          LINDA_EPB_UC_CTL,
1923                                                          control ) ) != 0 )
1924                                 break;
1925
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 )
1931                                 break;
1932                         address_lo = ( address & 0xff );
1933                         if ( ( rc = linda_ib_epb_write ( linda,
1934                                                          LINDA_EPB_UC_ADDR_LO,
1935                                                          address_lo ) ) != 0 )
1936                                 break;
1937                 }
1938
1939                 /* Read or write the data */
1940                 if ( read ) {
1941                         data = linda_ib_epb_read ( linda, LINDA_EPB_UC_DATA );
1942                         if ( data < 0 ) {
1943                                 rc = data;
1944                                 break;
1945                         }
1946                         *( ( uint8_t * ) read++ ) = data;
1947                 } else {
1948                         data = *( ( uint8_t * ) write++ );
1949                         if ( ( rc = linda_ib_epb_write ( linda,
1950                                                          LINDA_EPB_UC_DATA,
1951                                                          data ) ) != 0 )
1952                                 break;
1953                 }
1954                 address++;
1955                 len--;
1956
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,
1960                                                          LINDA_EPB_UC_CTL,
1961                                                          0 ) ) != 0 )
1962                                 break;
1963                 }
1964         }
1965
1966         /* Release bus */
1967         linda_ib_epb_release ( linda );
1968
1969  out:
1970         DBG_ENABLE ( DBGLVL_IO );
1971         return rc;
1972 }
1973
1974 /***************************************************************************
1975  *
1976  * Infiniband SerDes initialisation
1977  *
1978  ***************************************************************************
1979  */
1980
1981 /** A Linda SerDes parameter */
1982 struct linda_serdes_param {
1983         /** EPB address as constructed by LINDA_EPB_ADDRESS() */
1984         uint16_t address;
1985         /** Value to set */
1986         uint8_t value;
1987         /** Mask to apply to old value */
1988         uint8_t mask;
1989 } __packed;
1990
1991 /** Magic "all channels" channel number */
1992 #define LINDA_EPB_ALL_CHANNELS 31
1993
1994 /** End of SerDes parameter list marker */
1995 #define LINDA_SERDES_PARAM_END { 0, 0, 0 }
1996
1997 /**
1998  * Program IB SerDes register(s)
1999  *
2000  * @v linda             Linda device
2001  * @v param             SerDes parameter
2002  * @ret rc              Return status code
2003  */
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;
2010         unsigned int reg;
2011         int rc;
2012
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 ) {
2018                 channel_start = 0;
2019                 channel_end = 3;
2020         } else {
2021                 channel_start = channel_end = channel;
2022         }
2023
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,
2028                                                    param->value,
2029                                                    param->mask ) ) != 0 )
2030                         return rc;
2031         }
2032
2033         return 0;
2034 }
2035
2036 /**
2037  * Program IB SerDes registers
2038  *
2039  * @v linda             Linda device
2040  * @v param             SerDes parameters
2041  * @v count             Number of parameters
2042  * @ret rc              Return status code
2043  */
2044 static int linda_set_serdes_params ( struct linda *linda,
2045                                      struct linda_serdes_param *params ) {
2046         int rc;
2047
2048         for ( ; params->mask != 0 ; params++ ){
2049                 if ( ( rc = linda_set_serdes_param ( linda,
2050                                                          params ) ) != 0 )
2051                         return rc;
2052         }
2053
2054         return 0;
2055 }
2056
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 }
2073
2074 /**
2075  * Linda SerDes default parameters
2076  *
2077  * These magic start-of-day values are taken from the Linux driver.
2078  */
2079 static struct linda_serdes_param linda_serdes_defaults1[] = {
2080         /* RXHSCTRL0 */
2081         { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x00 ), 0xd4, 0xff },
2082         /* VCDL_DAC2 */
2083         { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x05 ), 0x2d, 0xff },
2084         /* VCDL_CTRL2 */
2085         { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x08 ), 0x03, 0x0f },
2086         /* START_EQ1 */
2087         { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x27 ), 0x10, 0xff },
2088         /* START_EQ2 */
2089         { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x28 ), 0x30, 0xff },
2090         /* BACTRL */
2091         { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x0e ), 0x40, 0xff },
2092         /* LDOUTCTRL1 */
2093         { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x06 ), 0x04, 0xff },
2094         /* RXHSSTATUS */
2095         { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x0f ), 0x04, 0xff },
2096         /* End of this block */
2097         LINDA_SERDES_PARAM_END
2098 };
2099 static struct linda_serdes_param linda_serdes_defaults2[] = {
2100         /* LDOUTCTRL1 */
2101         { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x06 ), 0x00, 0xff },
2102         /* DDS values */
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 },
2106         /* DFELTHFDR */
2107         { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x08 ), 0x00, 0xff },
2108         /* DFELTHHDR */
2109         { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x21 ), 0x00, 0xff },
2110         /* TLTHFDR */
2111         { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x09 ), 0x02, 0xff },
2112         /* TLTHHDR */
2113         { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x23 ), 0x02, 0xff },
2114         /* ZFR */
2115         { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x1b ), 0x0c, 0xff },
2116         /* ZCNT) */
2117         { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x1c ), 0x0c, 0xff },
2118         /* GFR */
2119         { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x1e ), 0x10, 0xff },
2120         /* GHR */
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 },
2125         /* CMUCTRL5 */
2126         { LINDA_EPB_ADDRESS (                      7, 0, 0x15 ), 0x80, 0xff },
2127         /* End of this block */
2128         LINDA_SERDES_PARAM_END
2129 };
2130 static struct linda_serdes_param linda_serdes_defaults3[] = {
2131         /* START_EQ1 */
2132         { LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x27 ), 0x00, 0x38 },
2133         /* End of this block */
2134         LINDA_SERDES_PARAM_END
2135 };
2136
2137 /**
2138  * Program the microcontroller RAM
2139  *
2140  * @v linda             Linda device
2141  * @ret rc              Return status code
2142  */
2143 static int linda_program_uc_ram ( struct linda *linda ) {
2144         int rc;
2145
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 ) );
2150                 return rc;
2151         }
2152
2153         return 0;
2154 }
2155
2156 /**
2157  * Verify the microcontroller RAM
2158  *
2159  * @v linda             Linda device
2160  * @ret rc              Return status code
2161  */
2162 static int linda_verify_uc_ram ( struct linda *linda ) {
2163         uint8_t verify[LINDA_EPB_UC_CHUNK_SIZE];
2164         unsigned int offset;
2165         int rc;
2166
2167         for ( offset = 0 ; offset < sizeof ( linda_ib_fw );
2168               offset += sizeof ( verify ) ) {
2169                 if ( ( rc = linda_ib_epb_ram_xfer ( linda, offset,
2170                                                     NULL, verify,
2171                                                     sizeof (verify) )) != 0 ){
2172                         DBGC ( linda, "Linda %p could not read back IB "
2173                                "firmware: %s\n", linda, strerror ( rc ) );
2174                         return rc;
2175                 }
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 ) );
2183                         return -EIO;
2184                 }
2185         }
2186
2187         DBGC2 ( linda, "Linda %p firmware verified ok\n", linda );
2188         return 0;
2189 }
2190
2191 /**
2192  * Use the microcontroller to trim the IB link
2193  *
2194  * @v linda             Linda device
2195  * @ret rc              Return status code
2196  */
2197 static int linda_trim_ib ( struct linda *linda ) {
2198         struct QIB_7220_IBSerDesCtrl ctrl;
2199         struct QIB_7220_IntStatus intstatus;
2200         unsigned int i;
2201         int rc;
2202
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 );
2207
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 ) ) {
2212                         rc = 0;
2213                         goto out_reset;
2214                 }
2215                 mdelay ( 1 );
2216         }
2217
2218         DBGC ( linda, "Linda %p timed out waiting for trim done\n", linda );
2219         rc = -ETIMEDOUT;
2220  out_reset:
2221         /* Put the microcontroller back into reset */
2222         BIT_SET ( &ctrl, ResetIB_uC_Core, 1 );
2223         linda_writeq ( linda, &ctrl, QIB_7220_IBSerDesCtrl_offset );
2224
2225         return rc;
2226 }
2227
2228 /**
2229  * Initialise the IB SerDes
2230  *
2231  * @v linda             Linda device
2232  * @ret rc              Return status code
2233  */
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;
2239         int rc;
2240
2241         /* Disable link */
2242         linda_readq ( linda, &control, QIB_7220_Control_offset );
2243         BIT_SET ( &control, LinkEn, 0 );
2244         linda_writeq ( linda, &control, QIB_7220_Control_offset );
2245
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,
2256                      CreditScale, 0x4 );
2257         linda_writeq ( linda, &ibcctrl, QIB_7220_IBCCtrl_offset );
2258
2259         /* Force SDR only to avoid needing all the DDR tuning,
2260          * Mellanox compatibility hacks etc.  SDR is plenty for
2261          * boot-time operation.
2262          */
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 );
2271
2272         /* Set default SerDes parameters */
2273         if ( ( rc = linda_set_serdes_params ( linda,
2274                                               linda_serdes_defaults1 ) ) != 0 )
2275                 return rc;
2276         udelay ( 415 ); /* Magic delay while SerDes sorts itself out */
2277         if ( ( rc = linda_set_serdes_params ( linda,
2278                                               linda_serdes_defaults2 ) ) != 0 )
2279                 return rc;
2280
2281         /* Program the microcontroller RAM */
2282         if ( ( rc = linda_program_uc_ram ( linda ) ) != 0 )
2283                 return rc;
2284
2285         /* Verify the microcontroller RAM contents */
2286         if ( DBGLVL_LOG ) {
2287                 if ( ( rc = linda_verify_uc_ram ( linda ) ) != 0 )
2288                         return rc;
2289         }
2290
2291         /* More SerDes tuning */
2292         if ( ( rc = linda_set_serdes_params ( linda,
2293                                               linda_serdes_defaults3 ) ) != 0 )
2294                 return rc;
2295
2296         /* Use the microcontroller to trim the IB link */
2297         if ( ( rc = linda_trim_ib ( linda ) ) != 0 )
2298                 return rc;
2299
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 );
2305
2306         return rc;
2307 }
2308
2309 /***************************************************************************
2310  *
2311  * PCI layer interface
2312  *
2313  ***************************************************************************
2314  */
2315
2316 /**
2317  * Probe PCI device
2318  *
2319  * @v pci               PCI device
2320  * @v id                PCI ID
2321  * @ret rc              Return status code
2322  */
2323 static int linda_probe ( struct pci_device *pci ) {
2324         struct ib_device *ibdev;
2325         struct linda *linda;
2326         struct QIB_7220_Revision revision;
2327         int rc;
2328
2329         /* Allocate Infiniband device */
2330         ibdev = alloc_ibdev ( sizeof ( *linda ) );
2331         if ( ! ibdev ) {
2332                 rc = -ENOMEM;
2333                 goto err_alloc_ibdev;
2334         }
2335         pci_set_drvdata ( pci, ibdev );
2336         linda = ib_get_drvdata ( ibdev );
2337         ibdev->op = &linda_ib_operations;
2338         ibdev->dev = &pci->dev;
2339         ibdev->port = 1;
2340
2341         /* Fix up PCI device */
2342         adjust_pci_device ( pci );
2343
2344         /* Get PCI BARs */
2345         linda->regs = ioremap ( pci->membase, LINDA_BAR0_SIZE );
2346         DBGC2 ( linda, "Linda %p has BAR at %08lx\n", linda, pci->membase );
2347
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 ) );
2356
2357         /* Record link capabilities.  Note that we force SDR only to
2358          * avoid having to carry extra code for DDR tuning etc.
2359          */
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 =
2363                 IB_LINK_SPEED_SDR;
2364
2365         /* Initialise I2C subsystem */
2366         if ( ( rc = linda_init_i2c ( linda ) ) != 0 )
2367                 goto err_init_i2c;
2368
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 ) );
2374
2375         /* Initialise send datapath */
2376         if ( ( rc = linda_init_send ( linda ) ) != 0 )
2377                 goto err_init_send;
2378
2379         /* Initialise receive datapath */
2380         if ( ( rc = linda_init_recv ( linda ) ) != 0 )
2381                 goto err_init_recv;
2382
2383         /* Initialise the IB SerDes */
2384         if ( ( rc = linda_init_ib_serdes ( linda ) ) != 0 )
2385                 goto err_init_ib_serdes;
2386
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;
2392         }
2393
2394         return 0;
2395
2396         unregister_ibdev ( ibdev );
2397  err_register_ibdev:
2398         linda_fini_recv ( linda );
2399  err_init_recv:
2400         linda_fini_send ( linda );
2401  err_init_send:
2402  err_init_ib_serdes:
2403  err_read_eeprom:
2404  err_init_i2c:
2405         ibdev_put ( ibdev );
2406  err_alloc_ibdev:
2407         return rc;
2408 }
2409
2410 /**
2411  * Remove PCI device
2412  *
2413  * @v pci               PCI device
2414  */
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 );
2418
2419         unregister_ibdev ( ibdev );
2420         linda_fini_recv ( linda );
2421         linda_fini_send ( linda );
2422         ibdev_put ( ibdev );
2423 }
2424
2425 static struct pci_device_id linda_nics[] = {
2426         PCI_ROM ( 0x1077, 0x7220, "iba7220", "QLE7240/7280 HCA driver", 0 ),
2427 };
2428
2429 struct pci_driver linda_driver __pci_driver = {
2430         .ids = linda_nics,
2431         .id_count = ( sizeof ( linda_nics ) / sizeof ( linda_nics[0] ) ),
2432         .probe = linda_probe,
2433         .remove = linda_remove,
2434 };