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  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <assert.h>
31 #include <ipxe/io.h>
32 #include <ipxe/pci.h>
33 #include <ipxe/infiniband.h>
34 #include <ipxe/i2c.h>
35 #include <ipxe/bitbash.h>
36 #include <ipxe/malloc.h>
37 #include <ipxe/iobuf.h>
38 #include "linda.h"
39 
40 /**
41  * @file
42  *
43  * QLogic Linda Infiniband HCA
44  *
45  */
46 
47 /** A Linda send work queue */
48 struct linda_send_work_queue {
49 	/** Send buffer usage */
50 	uint8_t *send_buf;
51 	/** Producer index */
52 	unsigned int prod;
53 	/** Consumer index */
54 	unsigned int cons;
55 };
56 
57 /** A Linda receive work queue */
58 struct linda_recv_work_queue {
59 	/** Receive header ring */
60 	void *header;
61 	/** Receive header producer offset (written by hardware) */
62 	struct QIB_7220_scalar header_prod;
63 	/** Receive header consumer offset */
64 	unsigned int header_cons;
65 	/** Offset within register space of the eager array */
66 	unsigned long eager_array;
67 	/** Number of entries in eager array */
68 	unsigned int eager_entries;
69 	/** Eager array producer index */
70 	unsigned int eager_prod;
71 	/** Eager array consumer index */
72 	unsigned int eager_cons;
73 };
74 
75 /** A Linda HCA */
76 struct linda {
77 	/** Registers */
78 	void *regs;
79 
80 	/** In-use contexts */
81 	uint8_t used_ctx[LINDA_NUM_CONTEXTS];
82 	/** Send work queues */
83 	struct linda_send_work_queue send_wq[LINDA_NUM_CONTEXTS];
84 	/** Receive work queues */
85 	struct linda_recv_work_queue recv_wq[LINDA_NUM_CONTEXTS];
86 
87 	/** Offset within register space of the first send buffer */
88 	unsigned long send_buffer_base;
89 	/** Send buffer availability (reported by hardware) */
90 	struct QIB_7220_SendBufAvail *sendbufavail;
91 	/** Send buffer availability (maintained by software) */
92 	uint8_t send_buf[LINDA_MAX_SEND_BUFS];
93 	/** Send buffer availability producer counter */
94 	unsigned int send_buf_prod;
95 	/** Send buffer availability consumer counter */
96 	unsigned int send_buf_cons;
97 	/** Number of reserved send buffers (across all QPs) */
98 	unsigned int reserved_send_bufs;
99 
100 	/** I2C bit-bashing interface */
101 	struct i2c_bit_basher i2c;
102 	/** I2C serial EEPROM */
103 	struct i2c_device eeprom;
104 };
105 
106 /***************************************************************************
107  *
108  * Linda register access
109  *
110  ***************************************************************************
111  *
112  * This card requires atomic 64-bit accesses.  Strange things happen
113  * if you try to use 32-bit accesses; sometimes they work, sometimes
114  * they don't, sometimes you get random data.
115  */
116 
117 /**
118  * Read Linda qword register
119  *
120  * @v linda		Linda device
121  * @v qword		Register buffer to read into
122  * @v offset		Register offset
123  */
linda_readq(struct linda * linda,uint64_t * qword,unsigned long offset)124 static void linda_readq ( struct linda *linda, uint64_t *qword,
125 			  unsigned long offset ) {
126 	*qword = readq ( linda->regs + offset );
127 }
128 #define linda_readq( _linda, _ptr, _offset ) \
129 	linda_readq ( (_linda), (_ptr)->u.qwords, (_offset) )
130 #define linda_readq_array8b( _linda, _ptr, _offset, _idx ) \
131 	linda_readq ( (_linda), (_ptr), ( (_offset) + ( (_idx) * 8 ) ) )
132 #define linda_readq_array64k( _linda, _ptr, _offset, _idx ) \
133 	linda_readq ( (_linda), (_ptr), ( (_offset) + ( (_idx) * 65536 ) ) )
134 
135 /**
136  * Write Linda qword register
137  *
138  * @v linda		Linda device
139  * @v qword		Register buffer to write
140  * @v offset		Register offset
141  */
linda_writeq(struct linda * linda,const uint64_t * qword,unsigned long offset)142 static void linda_writeq ( struct linda *linda, const uint64_t *qword,
143 			   unsigned long offset ) {
144 	writeq ( *qword, ( linda->regs + offset ) );
145 }
146 #define linda_writeq( _linda, _ptr, _offset ) \
147 	linda_writeq ( (_linda), (_ptr)->u.qwords, (_offset) )
148 #define linda_writeq_array8b( _linda, _ptr, _offset, _idx ) \
149 	linda_writeq ( (_linda), (_ptr), ( (_offset) + ( (_idx) * 8 ) ) )
150 #define linda_writeq_array64k( _linda, _ptr, _offset, _idx ) \
151 	linda_writeq ( (_linda), (_ptr), ( (_offset) + ( (_idx) * 65536 ) ) )
152 
153 /**
154  * Write Linda dword register
155  *
156  * @v linda		Linda device
157  * @v dword		Value to write
158  * @v offset		Register offset
159  */
linda_writel(struct linda * linda,uint32_t dword,unsigned long offset)160 static void linda_writel ( struct linda *linda, uint32_t dword,
161 			   unsigned long offset ) {
162 	writel ( dword, ( linda->regs + offset ) );
163 }
164 
165 /***************************************************************************
166  *
167  * Link state management
168  *
169  ***************************************************************************
170  */
171 
172 /**
173  * Textual representation of link state
174  *
175  * @v link_state	Link state
176  * @ret link_text	Link state text
177  */
linda_link_state_text(unsigned int link_state)178 static const char * linda_link_state_text ( unsigned int link_state ) {
179 	switch ( link_state ) {
180 	case LINDA_LINK_STATE_DOWN:	return "DOWN";
181 	case LINDA_LINK_STATE_INIT:	return "INIT";
182 	case LINDA_LINK_STATE_ARM:	return "ARM";
183 	case LINDA_LINK_STATE_ACTIVE:	return "ACTIVE";
184 	case LINDA_LINK_STATE_ACT_DEFER:return "ACT_DEFER";
185 	default:			return "UNKNOWN";
186 	}
187 }
188 
189 /**
190  * Handle link state change
191  *
192  * @v linda		Linda device
193  */
linda_link_state_changed(struct ib_device * ibdev)194 static void linda_link_state_changed ( struct ib_device *ibdev ) {
195 	struct linda *linda = ib_get_drvdata ( ibdev );
196 	struct QIB_7220_IBCStatus ibcstatus;
197 	struct QIB_7220_EXTCtrl extctrl;
198 	unsigned int link_state;
199 	unsigned int link_width;
200 	unsigned int link_speed;
201 
202 	/* Read link state */
203 	linda_readq ( linda, &ibcstatus, QIB_7220_IBCStatus_offset );
204 	link_state = BIT_GET ( &ibcstatus, LinkState );
205 	link_width = BIT_GET ( &ibcstatus, LinkWidthActive );
206 	link_speed = BIT_GET ( &ibcstatus, LinkSpeedActive );
207 	DBGC ( linda, "Linda %p link state %s (%s %s)\n", linda,
208 	       linda_link_state_text ( link_state ),
209 	       ( link_speed ? "DDR" : "SDR" ), ( link_width ? "x4" : "x1" ) );
210 
211 	/* Set LEDs according to link state */
212 	linda_readq ( linda, &extctrl, QIB_7220_EXTCtrl_offset );
213 	BIT_SET ( &extctrl, LEDPriPortGreenOn,
214 		  ( ( link_state >= LINDA_LINK_STATE_INIT ) ? 1 : 0 ) );
215 	BIT_SET ( &extctrl, LEDPriPortYellowOn,
216 		  ( ( link_state >= LINDA_LINK_STATE_ACTIVE ) ? 1 : 0 ) );
217 	linda_writeq ( linda, &extctrl, QIB_7220_EXTCtrl_offset );
218 
219 	/* Notify Infiniband core of link state change */
220 	ibdev->port_state = ( link_state + 1 );
221 	ibdev->link_width_active =
222 		( link_width ? IB_LINK_WIDTH_4X : IB_LINK_WIDTH_1X );
223 	ibdev->link_speed_active =
224 		( link_speed ? IB_LINK_SPEED_DDR : IB_LINK_SPEED_SDR );
225 	ib_link_state_changed ( ibdev );
226 }
227 
228 /**
229  * Wait for link state change to take effect
230  *
231  * @v linda		Linda device
232  * @v new_link_state	Expected link state
233  * @ret rc		Return status code
234  */
linda_link_state_check(struct linda * linda,unsigned int new_link_state)235 static int linda_link_state_check ( struct linda *linda,
236 				    unsigned int new_link_state ) {
237 	struct QIB_7220_IBCStatus ibcstatus;
238 	unsigned int link_state;
239 	unsigned int i;
240 
241 	for ( i = 0 ; i < LINDA_LINK_STATE_MAX_WAIT_US ; i++ ) {
242 		linda_readq ( linda, &ibcstatus, QIB_7220_IBCStatus_offset );
243 		link_state = BIT_GET ( &ibcstatus, LinkState );
244 		if ( link_state == new_link_state )
245 			return 0;
246 		udelay ( 1 );
247 	}
248 
249 	DBGC ( linda, "Linda %p timed out waiting for link state %s\n",
250 	       linda, linda_link_state_text ( link_state ) );
251 	return -ETIMEDOUT;
252 }
253 
254 /**
255  * Set port information
256  *
257  * @v ibdev		Infiniband device
258  * @v mad		Set port information MAD
259  */
linda_set_port_info(struct ib_device * ibdev,union ib_mad * mad)260 static int linda_set_port_info ( struct ib_device *ibdev, union ib_mad *mad ) {
261 	struct linda *linda = ib_get_drvdata ( ibdev );
262 	struct ib_port_info *port_info = &mad->smp.smp_data.port_info;
263 	struct QIB_7220_IBCCtrl ibcctrl;
264 	unsigned int port_state;
265 	unsigned int link_state;
266 
267 	/* Set new link state */
268 	port_state = ( port_info->link_speed_supported__port_state & 0xf );
269 	if ( port_state ) {
270 		link_state = ( port_state - 1 );
271 		DBGC ( linda, "Linda %p set link state to %s (%x)\n", linda,
272 		       linda_link_state_text ( link_state ), link_state );
273 		linda_readq ( linda, &ibcctrl, QIB_7220_IBCCtrl_offset );
274 		BIT_SET ( &ibcctrl, LinkCmd, link_state );
275 		linda_writeq ( linda, &ibcctrl, QIB_7220_IBCCtrl_offset );
276 
277 		/* Wait for link state change to take effect.  Ignore
278 		 * errors; the current link state will be returned via
279 		 * the GetResponse MAD.
280 		 */
281 		linda_link_state_check ( linda, link_state );
282 	}
283 
284 	/* Detect and report link state change */
285 	linda_link_state_changed ( ibdev );
286 
287 	return 0;
288 }
289 
290 /**
291  * Set partition key table
292  *
293  * @v ibdev		Infiniband device
294  * @v mad		Set partition key table MAD
295  */
linda_set_pkey_table(struct ib_device * ibdev __unused,union ib_mad * mad __unused)296 static int linda_set_pkey_table ( struct ib_device *ibdev __unused,
297 				  union ib_mad *mad __unused ) {
298 	/* Nothing to do */
299 	return 0;
300 }
301 
302 /***************************************************************************
303  *
304  * Context allocation
305  *
306  ***************************************************************************
307  */
308 
309 /**
310  * Map context number to QPN
311  *
312  * @v ctx		Context index
313  * @ret qpn		Queue pair number
314  */
linda_ctx_to_qpn(unsigned int ctx)315 static int linda_ctx_to_qpn ( unsigned int ctx ) {
316 	/* This mapping is fixed by hardware */
317 	return ( ctx * 2 );
318 }
319 
320 /**
321  * Map QPN to context number
322  *
323  * @v qpn		Queue pair number
324  * @ret ctx		Context index
325  */
linda_qpn_to_ctx(unsigned int qpn)326 static int linda_qpn_to_ctx ( unsigned int qpn ) {
327 	/* This mapping is fixed by hardware */
328 	return ( qpn / 2 );
329 }
330 
331 /**
332  * Allocate a context
333  *
334  * @v linda		Linda device
335  * @ret ctx		Context index, or negative error
336  */
linda_alloc_ctx(struct linda * linda)337 static int linda_alloc_ctx ( struct linda *linda ) {
338 	unsigned int ctx;
339 
340 	for ( ctx = 0 ; ctx < LINDA_NUM_CONTEXTS ; ctx++ ) {
341 
342 		if ( ! linda->used_ctx[ctx] ) {
343 			linda->used_ctx[ctx ] = 1;
344 			DBGC2 ( linda, "Linda %p CTX %d allocated\n",
345 				linda, ctx );
346 			return ctx;
347 		}
348 	}
349 
350 	DBGC ( linda, "Linda %p out of available contexts\n", linda );
351 	return -ENOENT;
352 }
353 
354 /**
355  * Free a context
356  *
357  * @v linda		Linda device
358  * @v ctx		Context index
359  */
linda_free_ctx(struct linda * linda,unsigned int ctx)360 static void linda_free_ctx ( struct linda *linda, unsigned int ctx ) {
361 
362 	linda->used_ctx[ctx] = 0;
363 	DBGC2 ( linda, "Linda %p CTX %d freed\n", linda, ctx );
364 }
365 
366 /***************************************************************************
367  *
368  * Send datapath
369  *
370  ***************************************************************************
371  */
372 
373 /** Send buffer toggle bit
374  *
375  * We encode send buffers as 7 bits of send buffer index plus a single
376  * bit which should match the "check" bit in the SendBufAvail array.
377  */
378 #define LINDA_SEND_BUF_TOGGLE 0x80
379 
380 /**
381  * Allocate a send buffer
382  *
383  * @v linda		Linda device
384  * @ret send_buf	Send buffer
385  *
386  * You must guarantee that a send buffer is available.  This is done
387  * by refusing to allocate more TX WQEs in total than the number of
388  * available send buffers.
389  */
linda_alloc_send_buf(struct linda * linda)390 static unsigned int linda_alloc_send_buf ( struct linda *linda ) {
391 	unsigned int send_buf;
392 
393 	send_buf = linda->send_buf[linda->send_buf_cons];
394 	send_buf ^= LINDA_SEND_BUF_TOGGLE;
395 	linda->send_buf_cons = ( ( linda->send_buf_cons + 1 ) %
396 				 LINDA_MAX_SEND_BUFS );
397 	return send_buf;
398 }
399 
400 /**
401  * Free a send buffer
402  *
403  * @v linda		Linda device
404  * @v send_buf		Send buffer
405  */
linda_free_send_buf(struct linda * linda,unsigned int send_buf)406 static void linda_free_send_buf ( struct linda *linda,
407 				  unsigned int send_buf ) {
408 	linda->send_buf[linda->send_buf_prod] = send_buf;
409 	linda->send_buf_prod = ( ( linda->send_buf_prod + 1 ) %
410 				 LINDA_MAX_SEND_BUFS );
411 }
412 
413 /**
414  * Check to see if send buffer is in use
415  *
416  * @v linda		Linda device
417  * @v send_buf		Send buffer
418  * @ret in_use		Send buffer is in use
419  */
linda_send_buf_in_use(struct linda * linda,unsigned int send_buf)420 static int linda_send_buf_in_use ( struct linda *linda,
421 				   unsigned int send_buf ) {
422 	unsigned int send_idx;
423 	unsigned int send_check;
424 	unsigned int inusecheck;
425 	unsigned int inuse;
426 	unsigned int check;
427 
428 	send_idx = ( send_buf & ~LINDA_SEND_BUF_TOGGLE );
429 	send_check = ( !! ( send_buf & LINDA_SEND_BUF_TOGGLE ) );
430 	inusecheck = BIT_GET ( linda->sendbufavail, InUseCheck[send_idx] );
431 	inuse = ( !! ( inusecheck & 0x02 ) );
432 	check = ( !! ( inusecheck & 0x01 ) );
433 	return ( inuse || ( check != send_check ) );
434 }
435 
436 /**
437  * Calculate starting offset for send buffer
438  *
439  * @v linda		Linda device
440  * @v send_buf		Send buffer
441  * @ret offset		Starting offset
442  */
linda_send_buffer_offset(struct linda * linda,unsigned int send_buf)443 static unsigned long linda_send_buffer_offset ( struct linda *linda,
444 						unsigned int send_buf ) {
445 	return ( linda->send_buffer_base +
446 		 ( ( send_buf & ~LINDA_SEND_BUF_TOGGLE ) *
447 		   LINDA_SEND_BUF_SIZE ) );
448 }
449 
450 /**
451  * Create send work queue
452  *
453  * @v linda		Linda device
454  * @v qp		Queue pair
455  */
linda_create_send_wq(struct linda * linda,struct ib_queue_pair * qp)456 static int linda_create_send_wq ( struct linda *linda,
457 				  struct ib_queue_pair *qp ) {
458 	struct ib_work_queue *wq = &qp->send;
459 	struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
460 	int rc;
461 
462 	/* Reserve send buffers */
463 	if ( ( linda->reserved_send_bufs + qp->send.num_wqes ) >
464 	     LINDA_MAX_SEND_BUFS ) {
465 		DBGC ( linda, "Linda %p out of send buffers (have %d, used "
466 		       "%d, need %d)\n", linda, LINDA_MAX_SEND_BUFS,
467 		       linda->reserved_send_bufs, qp->send.num_wqes );
468 		rc = -ENOBUFS;
469 		goto err_reserve_bufs;
470 	}
471 	linda->reserved_send_bufs += qp->send.num_wqes;
472 
473 	/* Reset work queue */
474 	linda_wq->prod = 0;
475 	linda_wq->cons = 0;
476 
477 	/* Allocate space for send buffer uasge list */
478 	linda_wq->send_buf = zalloc ( qp->send.num_wqes *
479 				      sizeof ( linda_wq->send_buf[0] ) );
480 	if ( ! linda_wq->send_buf ) {
481 		rc = -ENOBUFS;
482 		goto err_alloc_send_buf;
483 	}
484 
485 	return 0;
486 
487 	free ( linda_wq->send_buf );
488  err_alloc_send_buf:
489 	linda->reserved_send_bufs -= qp->send.num_wqes;
490  err_reserve_bufs:
491 	return rc;
492 }
493 
494 /**
495  * Destroy send work queue
496  *
497  * @v linda		Linda device
498  * @v qp		Queue pair
499  */
linda_destroy_send_wq(struct linda * linda,struct ib_queue_pair * qp)500 static void linda_destroy_send_wq ( struct linda *linda,
501 				    struct ib_queue_pair *qp ) {
502 	struct ib_work_queue *wq = &qp->send;
503 	struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
504 
505 	free ( linda_wq->send_buf );
506 	linda->reserved_send_bufs -= qp->send.num_wqes;
507 }
508 
509 /**
510  * Initialise send datapath
511  *
512  * @v linda		Linda device
513  * @ret rc		Return status code
514  */
linda_init_send(struct linda * linda)515 static int linda_init_send ( struct linda *linda ) {
516 	struct QIB_7220_SendBufBase sendbufbase;
517 	struct QIB_7220_SendBufAvailAddr sendbufavailaddr;
518 	struct QIB_7220_SendCtrl sendctrl;
519 	unsigned int i;
520 	int rc;
521 
522 	/* Retrieve SendBufBase */
523 	linda_readq ( linda, &sendbufbase, QIB_7220_SendBufBase_offset );
524 	linda->send_buffer_base = BIT_GET ( &sendbufbase,
525 					    BaseAddr_SmallPIO );
526 	DBGC ( linda, "Linda %p send buffers at %lx\n",
527 	       linda, linda->send_buffer_base );
528 
529 	/* Initialise the send_buf[] array */
530 	for ( i = 0 ; i < LINDA_MAX_SEND_BUFS ; i++ )
531 		linda->send_buf[i] = i;
532 
533 	/* Allocate space for the SendBufAvail array */
534 	linda->sendbufavail = malloc_dma ( sizeof ( *linda->sendbufavail ),
535 					   LINDA_SENDBUFAVAIL_ALIGN );
536 	if ( ! linda->sendbufavail ) {
537 		rc = -ENOMEM;
538 		goto err_alloc_sendbufavail;
539 	}
540 	memset ( linda->sendbufavail, 0, sizeof ( *linda->sendbufavail ) );
541 
542 	/* Program SendBufAvailAddr into the hardware */
543 	memset ( &sendbufavailaddr, 0, sizeof ( sendbufavailaddr ) );
544 	BIT_FILL_1 ( &sendbufavailaddr, SendBufAvailAddr,
545 		     ( virt_to_bus ( linda->sendbufavail ) >> 6 ) );
546 	linda_writeq ( linda, &sendbufavailaddr,
547 		       QIB_7220_SendBufAvailAddr_offset );
548 
549 	/* Enable sending and DMA of SendBufAvail */
550 	memset ( &sendctrl, 0, sizeof ( sendctrl ) );
551 	BIT_FILL_2 ( &sendctrl,
552 		     SendBufAvailUpd, 1,
553 		     SPioEnable, 1 );
554 	linda_writeq ( linda, &sendctrl, QIB_7220_SendCtrl_offset );
555 
556 	return 0;
557 
558 	free_dma ( linda->sendbufavail, sizeof ( *linda->sendbufavail ) );
559  err_alloc_sendbufavail:
560 	return rc;
561 }
562 
563 /**
564  * Shut down send datapath
565  *
566  * @v linda		Linda device
567  */
linda_fini_send(struct linda * linda)568 static void linda_fini_send ( struct linda *linda ) {
569 	struct QIB_7220_SendCtrl sendctrl;
570 
571 	/* Disable sending and DMA of SendBufAvail */
572 	memset ( &sendctrl, 0, sizeof ( sendctrl ) );
573 	linda_writeq ( linda, &sendctrl, QIB_7220_SendCtrl_offset );
574 	mb();
575 
576 	/* Ensure hardware has seen this disable */
577 	linda_readq ( linda, &sendctrl, QIB_7220_SendCtrl_offset );
578 
579 	free_dma ( linda->sendbufavail, sizeof ( *linda->sendbufavail ) );
580 }
581 
582 /***************************************************************************
583  *
584  * Receive datapath
585  *
586  ***************************************************************************
587  */
588 
589 /**
590  * Create receive work queue
591  *
592  * @v linda		Linda device
593  * @v qp		Queue pair
594  * @ret rc		Return status code
595  */
linda_create_recv_wq(struct linda * linda,struct ib_queue_pair * qp)596 static int linda_create_recv_wq ( struct linda *linda,
597 				  struct ib_queue_pair *qp ) {
598 	struct ib_work_queue *wq = &qp->recv;
599 	struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
600 	struct QIB_7220_RcvHdrAddr0 rcvhdraddr;
601 	struct QIB_7220_RcvHdrTailAddr0 rcvhdrtailaddr;
602 	struct QIB_7220_RcvHdrHead0 rcvhdrhead;
603 	struct QIB_7220_scalar rcvegrindexhead;
604 	struct QIB_7220_RcvCtrl rcvctrl;
605 	unsigned int ctx = linda_qpn_to_ctx ( qp->qpn );
606 	int rc;
607 
608 	/* Reset context information */
609 	memset ( &linda_wq->header_prod, 0,
610 		 sizeof ( linda_wq->header_prod ) );
611 	linda_wq->header_cons = 0;
612 	linda_wq->eager_prod = 0;
613 	linda_wq->eager_cons = 0;
614 
615 	/* Allocate receive header buffer */
616 	linda_wq->header = malloc_dma ( LINDA_RECV_HEADERS_SIZE,
617 					LINDA_RECV_HEADERS_ALIGN );
618 	if ( ! linda_wq->header ) {
619 		rc = -ENOMEM;
620 		goto err_alloc_header;
621 	}
622 
623 	/* Enable context in hardware */
624 	memset ( &rcvhdraddr, 0, sizeof ( rcvhdraddr ) );
625 	BIT_FILL_1 ( &rcvhdraddr, RcvHdrAddr0,
626 		     ( virt_to_bus ( linda_wq->header ) >> 2 ) );
627 	linda_writeq_array8b ( linda, &rcvhdraddr,
628 			       QIB_7220_RcvHdrAddr0_offset, ctx );
629 	memset ( &rcvhdrtailaddr, 0, sizeof ( rcvhdrtailaddr ) );
630 	BIT_FILL_1 ( &rcvhdrtailaddr, RcvHdrTailAddr0,
631 		     ( virt_to_bus ( &linda_wq->header_prod ) >> 2 ) );
632 	linda_writeq_array8b ( linda, &rcvhdrtailaddr,
633 			       QIB_7220_RcvHdrTailAddr0_offset, ctx );
634 	memset ( &rcvhdrhead, 0, sizeof ( rcvhdrhead ) );
635 	BIT_FILL_1 ( &rcvhdrhead, counter, 1 );
636 	linda_writeq_array64k ( linda, &rcvhdrhead,
637 				QIB_7220_RcvHdrHead0_offset, ctx );
638 	memset ( &rcvegrindexhead, 0, sizeof ( rcvegrindexhead ) );
639 	BIT_FILL_1 ( &rcvegrindexhead, Value, 1 );
640 	linda_writeq_array64k ( linda, &rcvegrindexhead,
641 				QIB_7220_RcvEgrIndexHead0_offset, ctx );
642 	linda_readq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
643 	BIT_SET ( &rcvctrl, PortEnable[ctx], 1 );
644 	BIT_SET ( &rcvctrl, IntrAvail[ctx], 1 );
645 	linda_writeq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
646 
647 	DBGC ( linda, "Linda %p QPN %ld CTX %d hdrs [%lx,%lx) prod %lx\n",
648 	       linda, qp->qpn, ctx, virt_to_bus ( linda_wq->header ),
649 	       ( virt_to_bus ( linda_wq->header ) + LINDA_RECV_HEADERS_SIZE ),
650 	       virt_to_bus ( &linda_wq->header_prod ) );
651 	return 0;
652 
653 	free_dma ( linda_wq->header, LINDA_RECV_HEADERS_SIZE );
654  err_alloc_header:
655 	return rc;
656 }
657 
658 /**
659  * Destroy receive work queue
660  *
661  * @v linda		Linda device
662  * @v qp		Queue pair
663  */
linda_destroy_recv_wq(struct linda * linda,struct ib_queue_pair * qp)664 static void linda_destroy_recv_wq ( struct linda *linda,
665 				    struct ib_queue_pair *qp ) {
666 	struct ib_work_queue *wq = &qp->recv;
667 	struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
668 	struct QIB_7220_RcvCtrl rcvctrl;
669 	unsigned int ctx = linda_qpn_to_ctx ( qp->qpn );
670 
671 	/* Disable context in hardware */
672 	linda_readq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
673 	BIT_SET ( &rcvctrl, PortEnable[ctx], 0 );
674 	BIT_SET ( &rcvctrl, IntrAvail[ctx], 0 );
675 	linda_writeq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
676 
677 	/* Make sure the hardware has seen that the context is disabled */
678 	linda_readq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
679 	mb();
680 
681 	/* Free headers ring */
682 	free_dma ( linda_wq->header, LINDA_RECV_HEADERS_SIZE );
683 
684 	/* Free context */
685 	linda_free_ctx ( linda, ctx );
686 }
687 
688 /**
689  * Initialise receive datapath
690  *
691  * @v linda		Linda device
692  * @ret rc		Return status code
693  */
linda_init_recv(struct linda * linda)694 static int linda_init_recv ( struct linda *linda ) {
695 	struct QIB_7220_RcvCtrl rcvctrl;
696 	struct QIB_7220_scalar rcvegrbase;
697 	struct QIB_7220_scalar rcvhdrentsize;
698 	struct QIB_7220_scalar rcvhdrcnt;
699 	struct QIB_7220_RcvBTHQP rcvbthqp;
700 	unsigned int portcfg;
701 	unsigned long egrbase;
702 	unsigned int eager_array_size_0;
703 	unsigned int eager_array_size_other;
704 	unsigned int ctx;
705 
706 	/* Select configuration based on number of contexts */
707 	switch ( LINDA_NUM_CONTEXTS ) {
708 	case 5:
709 		portcfg = LINDA_PORTCFG_5CTX;
710 		eager_array_size_0 = LINDA_EAGER_ARRAY_SIZE_5CTX_0;
711 		eager_array_size_other = LINDA_EAGER_ARRAY_SIZE_5CTX_OTHER;
712 		break;
713 	case 9:
714 		portcfg = LINDA_PORTCFG_9CTX;
715 		eager_array_size_0 = LINDA_EAGER_ARRAY_SIZE_9CTX_0;
716 		eager_array_size_other = LINDA_EAGER_ARRAY_SIZE_9CTX_OTHER;
717 		break;
718 	case 17:
719 		portcfg = LINDA_PORTCFG_17CTX;
720 		eager_array_size_0 = LINDA_EAGER_ARRAY_SIZE_17CTX_0;
721 		eager_array_size_other = LINDA_EAGER_ARRAY_SIZE_17CTX_OTHER;
722 		break;
723 	default:
724 		linker_assert ( 0, invalid_LINDA_NUM_CONTEXTS );
725 		return -EINVAL;
726 	}
727 
728 	/* Configure number of contexts */
729 	memset ( &rcvctrl, 0, sizeof ( rcvctrl ) );
730 	BIT_FILL_3 ( &rcvctrl,
731 		     TailUpd, 1,
732 		     PortCfg, portcfg,
733 		     RcvQPMapEnable, 1 );
734 	linda_writeq ( linda, &rcvctrl, QIB_7220_RcvCtrl_offset );
735 
736 	/* Configure receive header buffer sizes */
737 	memset ( &rcvhdrcnt, 0, sizeof ( rcvhdrcnt ) );
738 	BIT_FILL_1 ( &rcvhdrcnt, Value, LINDA_RECV_HEADER_COUNT );
739 	linda_writeq ( linda, &rcvhdrcnt, QIB_7220_RcvHdrCnt_offset );
740 	memset ( &rcvhdrentsize, 0, sizeof ( rcvhdrentsize ) );
741 	BIT_FILL_1 ( &rcvhdrentsize, Value, ( LINDA_RECV_HEADER_SIZE >> 2 ) );
742 	linda_writeq ( linda, &rcvhdrentsize, QIB_7220_RcvHdrEntSize_offset );
743 
744 	/* Calculate eager array start addresses for each context */
745 	linda_readq ( linda, &rcvegrbase, QIB_7220_RcvEgrBase_offset );
746 	egrbase = BIT_GET ( &rcvegrbase, Value );
747 	linda->recv_wq[0].eager_array = egrbase;
748 	linda->recv_wq[0].eager_entries = eager_array_size_0;
749 	egrbase += ( eager_array_size_0 * sizeof ( struct QIB_7220_RcvEgr ) );
750 	for ( ctx = 1 ; ctx < LINDA_NUM_CONTEXTS ; ctx++ ) {
751 		linda->recv_wq[ctx].eager_array = egrbase;
752 		linda->recv_wq[ctx].eager_entries = eager_array_size_other;
753 		egrbase += ( eager_array_size_other *
754 			     sizeof ( struct QIB_7220_RcvEgr ) );
755 	}
756 	for ( ctx = 0 ; ctx < LINDA_NUM_CONTEXTS ; ctx++ ) {
757 		DBGC ( linda, "Linda %p CTX %d eager array at %lx (%d "
758 		       "entries)\n", linda, ctx,
759 		       linda->recv_wq[ctx].eager_array,
760 		       linda->recv_wq[ctx].eager_entries );
761 	}
762 
763 	/* Set the BTH QP for Infinipath packets to an unused value */
764 	memset ( &rcvbthqp, 0, sizeof ( rcvbthqp ) );
765 	BIT_FILL_1 ( &rcvbthqp, RcvBTHQP, LINDA_QP_IDETH );
766 	linda_writeq ( linda, &rcvbthqp, QIB_7220_RcvBTHQP_offset );
767 
768 	return 0;
769 }
770 
771 /**
772  * Shut down receive datapath
773  *
774  * @v linda		Linda device
775  */
linda_fini_recv(struct linda * linda __unused)776 static void linda_fini_recv ( struct linda *linda __unused ) {
777 	/* Nothing to do; all contexts were already disabled when the
778 	 * queue pairs were destroyed
779 	 */
780 }
781 
782 /***************************************************************************
783  *
784  * Completion queue operations
785  *
786  ***************************************************************************
787  */
788 
789 /**
790  * Create completion queue
791  *
792  * @v ibdev		Infiniband device
793  * @v cq		Completion queue
794  * @ret rc		Return status code
795  */
linda_create_cq(struct ib_device * ibdev,struct ib_completion_queue * cq)796 static int linda_create_cq ( struct ib_device *ibdev,
797 			     struct ib_completion_queue *cq ) {
798 	struct linda *linda = ib_get_drvdata ( ibdev );
799 	static int cqn;
800 
801 	/* The hardware has no concept of completion queues.  We
802 	 * simply use the association between CQs and WQs (already
803 	 * handled by the IB core) to decide which WQs to poll.
804 	 *
805 	 * We do set a CQN, just to avoid confusing debug messages
806 	 * from the IB core.
807 	 */
808 	cq->cqn = ++cqn;
809 	DBGC ( linda, "Linda %p CQN %ld created\n", linda, cq->cqn );
810 
811 	return 0;
812 }
813 
814 /**
815  * Destroy completion queue
816  *
817  * @v ibdev		Infiniband device
818  * @v cq		Completion queue
819  */
linda_destroy_cq(struct ib_device * ibdev,struct ib_completion_queue * cq)820 static void linda_destroy_cq ( struct ib_device *ibdev,
821 			       struct ib_completion_queue *cq ) {
822 	struct linda *linda = ib_get_drvdata ( ibdev );
823 
824 	/* Nothing to do */
825 	DBGC ( linda, "Linda %p CQN %ld destroyed\n", linda, cq->cqn );
826 }
827 
828 /***************************************************************************
829  *
830  * Queue pair operations
831  *
832  ***************************************************************************
833  */
834 
835 /**
836  * Create queue pair
837  *
838  * @v ibdev		Infiniband device
839  * @v qp		Queue pair
840  * @ret rc		Return status code
841  */
linda_create_qp(struct ib_device * ibdev,struct ib_queue_pair * qp)842 static int linda_create_qp ( struct ib_device *ibdev,
843 			     struct ib_queue_pair *qp ) {
844 	struct linda *linda = ib_get_drvdata ( ibdev );
845 	int ctx;
846 	int rc;
847 
848 	/* Locate an available context */
849 	ctx = linda_alloc_ctx ( linda );
850 	if ( ctx < 0 ) {
851 		rc = ctx;
852 		goto err_alloc_ctx;
853 	}
854 
855 	/* Set queue pair number based on context index */
856 	qp->qpn = linda_ctx_to_qpn ( ctx );
857 
858 	/* Set work-queue private data pointers */
859 	ib_wq_set_drvdata ( &qp->send, &linda->send_wq[ctx] );
860 	ib_wq_set_drvdata ( &qp->recv, &linda->recv_wq[ctx] );
861 
862 	/* Create receive work queue */
863 	if ( ( rc = linda_create_recv_wq ( linda, qp ) ) != 0 )
864 		goto err_create_recv_wq;
865 
866 	/* Create send work queue */
867 	if ( ( rc = linda_create_send_wq ( linda, qp ) ) != 0 )
868 		goto err_create_send_wq;
869 
870 	return 0;
871 
872 	linda_destroy_send_wq ( linda, qp );
873  err_create_send_wq:
874 	linda_destroy_recv_wq ( linda, qp );
875  err_create_recv_wq:
876 	linda_free_ctx ( linda, ctx );
877  err_alloc_ctx:
878 	return rc;
879 }
880 
881 /**
882  * Modify queue pair
883  *
884  * @v ibdev		Infiniband device
885  * @v qp		Queue pair
886  * @ret rc		Return status code
887  */
linda_modify_qp(struct ib_device * ibdev,struct ib_queue_pair * qp)888 static int linda_modify_qp ( struct ib_device *ibdev,
889 			     struct ib_queue_pair *qp ) {
890 	struct linda *linda = ib_get_drvdata ( ibdev );
891 
892 	/* Nothing to do; the hardware doesn't have a notion of queue
893 	 * keys
894 	 */
895 	DBGC ( linda, "Linda %p QPN %ld modified\n", linda, qp->qpn );
896 	return 0;
897 }
898 
899 /**
900  * Destroy queue pair
901  *
902  * @v ibdev		Infiniband device
903  * @v qp		Queue pair
904  */
linda_destroy_qp(struct ib_device * ibdev,struct ib_queue_pair * qp)905 static void linda_destroy_qp ( struct ib_device *ibdev,
906 			       struct ib_queue_pair *qp ) {
907 	struct linda *linda = ib_get_drvdata ( ibdev );
908 
909 	linda_destroy_send_wq ( linda, qp );
910 	linda_destroy_recv_wq ( linda, qp );
911 }
912 
913 /***************************************************************************
914  *
915  * Work request operations
916  *
917  ***************************************************************************
918  */
919 
920 /**
921  * Post send work queue entry
922  *
923  * @v ibdev		Infiniband device
924  * @v qp		Queue pair
925  * @v dest		Destination address vector
926  * @v iobuf		I/O buffer
927  * @ret rc		Return status code
928  */
linda_post_send(struct ib_device * ibdev,struct ib_queue_pair * qp,struct ib_address_vector * dest,struct io_buffer * iobuf)929 static int linda_post_send ( struct ib_device *ibdev,
930 			     struct ib_queue_pair *qp,
931 			     struct ib_address_vector *dest,
932 			     struct io_buffer *iobuf ) {
933 	struct linda *linda = ib_get_drvdata ( ibdev );
934 	struct ib_work_queue *wq = &qp->send;
935 	struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
936 	struct QIB_7220_SendPbc sendpbc;
937 	uint8_t header_buf[IB_MAX_HEADER_SIZE];
938 	struct io_buffer headers;
939 	unsigned int send_buf;
940 	unsigned long start_offset;
941 	unsigned long offset;
942 	size_t len;
943 	ssize_t frag_len;
944 	uint32_t *data;
945 
946 	/* Allocate send buffer and calculate offset */
947 	send_buf = linda_alloc_send_buf ( linda );
948 	start_offset = offset = linda_send_buffer_offset ( linda, send_buf );
949 
950 	/* Store I/O buffer and send buffer index */
951 	assert ( wq->iobufs[linda_wq->prod] == NULL );
952 	wq->iobufs[linda_wq->prod] = iobuf;
953 	linda_wq->send_buf[linda_wq->prod] = send_buf;
954 
955 	/* Construct headers */
956 	iob_populate ( &headers, header_buf, 0, sizeof ( header_buf ) );
957 	iob_reserve ( &headers, sizeof ( header_buf ) );
958 	ib_push ( ibdev, &headers, qp, iob_len ( iobuf ), dest );
959 
960 	/* Calculate packet length */
961 	len = ( ( sizeof ( sendpbc ) + iob_len ( &headers ) +
962 		  iob_len ( iobuf ) + 3 ) & ~3 );
963 
964 	/* Construct send per-buffer control word */
965 	memset ( &sendpbc, 0, sizeof ( sendpbc ) );
966 	BIT_FILL_2 ( &sendpbc,
967 		     LengthP1_toibc, ( ( len >> 2 ) - 1 ),
968 		     VL15, 1 );
969 
970 	/* Write SendPbc */
971 	DBG_DISABLE ( DBGLVL_IO );
972 	linda_writeq ( linda, &sendpbc, offset );
973 	offset += sizeof ( sendpbc );
974 
975 	/* Write headers */
976 	for ( data = headers.data, frag_len = iob_len ( &headers ) ;
977 	      frag_len > 0 ; data++, offset += 4, frag_len -= 4 ) {
978 		linda_writel ( linda, *data, offset );
979 	}
980 
981 	/* Write data */
982 	for ( data = iobuf->data, frag_len = iob_len ( iobuf ) ;
983 	      frag_len > 0 ; data++, offset += 4, frag_len -= 4 ) {
984 		linda_writel ( linda, *data, offset );
985 	}
986 	DBG_ENABLE ( DBGLVL_IO );
987 
988 	assert ( ( start_offset + len ) == offset );
989 	DBGC2 ( linda, "Linda %p QPN %ld TX %d(%d) posted [%lx,%lx)\n",
990 		linda, qp->qpn, send_buf, linda_wq->prod,
991 		start_offset, offset );
992 
993 	/* Increment producer counter */
994 	linda_wq->prod = ( ( linda_wq->prod + 1 ) & ( wq->num_wqes - 1 ) );
995 
996 	return 0;
997 }
998 
999 /**
1000  * Complete send work queue entry
1001  *
1002  * @v ibdev		Infiniband device
1003  * @v qp		Queue pair
1004  * @v wqe_idx		Work queue entry index
1005  */
linda_complete_send(struct ib_device * ibdev,struct ib_queue_pair * qp,unsigned int wqe_idx)1006 static void linda_complete_send ( struct ib_device *ibdev,
1007 				  struct ib_queue_pair *qp,
1008 				  unsigned int wqe_idx ) {
1009 	struct linda *linda = ib_get_drvdata ( ibdev );
1010 	struct ib_work_queue *wq = &qp->send;
1011 	struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
1012 	struct io_buffer *iobuf;
1013 	unsigned int send_buf;
1014 
1015 	/* Parse completion */
1016 	send_buf = linda_wq->send_buf[wqe_idx];
1017 	DBGC2 ( linda, "Linda %p QPN %ld TX %d(%d) complete\n",
1018 		linda, qp->qpn, send_buf, wqe_idx );
1019 
1020 	/* Complete work queue entry */
1021 	iobuf = wq->iobufs[wqe_idx];
1022 	assert ( iobuf != NULL );
1023 	ib_complete_send ( ibdev, qp, iobuf, 0 );
1024 	wq->iobufs[wqe_idx] = NULL;
1025 
1026 	/* Free send buffer */
1027 	linda_free_send_buf ( linda, send_buf );
1028 }
1029 
1030 /**
1031  * Poll send work queue
1032  *
1033  * @v ibdev		Infiniband device
1034  * @v qp		Queue pair
1035  */
linda_poll_send_wq(struct ib_device * ibdev,struct ib_queue_pair * qp)1036 static void linda_poll_send_wq ( struct ib_device *ibdev,
1037 				 struct ib_queue_pair *qp ) {
1038 	struct linda *linda = ib_get_drvdata ( ibdev );
1039 	struct ib_work_queue *wq = &qp->send;
1040 	struct linda_send_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
1041 	unsigned int send_buf;
1042 
1043 	/* Look for completions */
1044 	while ( wq->fill ) {
1045 
1046 		/* Check to see if send buffer has completed */
1047 		send_buf = linda_wq->send_buf[linda_wq->cons];
1048 		if ( linda_send_buf_in_use ( linda, send_buf ) )
1049 			break;
1050 
1051 		/* Complete this buffer */
1052 		linda_complete_send ( ibdev, qp, linda_wq->cons );
1053 
1054 		/* Increment consumer counter */
1055 		linda_wq->cons = ( ( linda_wq->cons + 1 ) &
1056 				   ( wq->num_wqes - 1 ) );
1057 	}
1058 }
1059 
1060 /**
1061  * Post receive work queue entry
1062  *
1063  * @v ibdev		Infiniband device
1064  * @v qp		Queue pair
1065  * @v iobuf		I/O buffer
1066  * @ret rc		Return status code
1067  */
linda_post_recv(struct ib_device * ibdev,struct ib_queue_pair * qp,struct io_buffer * iobuf)1068 static int linda_post_recv ( struct ib_device *ibdev,
1069 			     struct ib_queue_pair *qp,
1070 			     struct io_buffer *iobuf ) {
1071 	struct linda *linda = ib_get_drvdata ( ibdev );
1072 	struct ib_work_queue *wq = &qp->recv;
1073 	struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
1074 	struct QIB_7220_RcvEgr rcvegr;
1075 	struct QIB_7220_scalar rcvegrindexhead;
1076 	unsigned int ctx = linda_qpn_to_ctx ( qp->qpn );
1077 	physaddr_t addr;
1078 	size_t len;
1079 	unsigned int wqe_idx;
1080 	unsigned int bufsize;
1081 
1082 	/* Sanity checks */
1083 	addr = virt_to_bus ( iobuf->data );
1084 	len = iob_tailroom ( iobuf );
1085 	if ( addr & ( LINDA_EAGER_BUFFER_ALIGN - 1 ) ) {
1086 		DBGC ( linda, "Linda %p QPN %ld misaligned RX buffer "
1087 		       "(%08lx)\n", linda, qp->qpn, addr );
1088 		return -EINVAL;
1089 	}
1090 	if ( len != LINDA_RECV_PAYLOAD_SIZE ) {
1091 		DBGC ( linda, "Linda %p QPN %ld wrong RX buffer size (%zd)\n",
1092 		       linda, qp->qpn, len );
1093 		return -EINVAL;
1094 	}
1095 
1096 	/* Calculate eager producer index and WQE index */
1097 	wqe_idx = ( linda_wq->eager_prod & ( wq->num_wqes - 1 ) );
1098 	assert ( wq->iobufs[wqe_idx] == NULL );
1099 
1100 	/* Store I/O buffer */
1101 	wq->iobufs[wqe_idx] = iobuf;
1102 
1103 	/* Calculate buffer size */
1104 	switch ( LINDA_RECV_PAYLOAD_SIZE ) {
1105 	case 2048:  bufsize = LINDA_EAGER_BUFFER_2K;  break;
1106 	case 4096:  bufsize = LINDA_EAGER_BUFFER_4K;  break;
1107 	case 8192:  bufsize = LINDA_EAGER_BUFFER_8K;  break;
1108 	case 16384: bufsize = LINDA_EAGER_BUFFER_16K; break;
1109 	case 32768: bufsize = LINDA_EAGER_BUFFER_32K; break;
1110 	case 65536: bufsize = LINDA_EAGER_BUFFER_64K; break;
1111 	default:    linker_assert ( 0, invalid_rx_payload_size );
1112 		    bufsize = LINDA_EAGER_BUFFER_NONE;
1113 	}
1114 
1115 	/* Post eager buffer */
1116 	memset ( &rcvegr, 0, sizeof ( rcvegr ) );
1117 	BIT_FILL_2 ( &rcvegr,
1118 		     Addr, ( addr >> 11 ),
1119 		     BufSize, bufsize );
1120 	linda_writeq_array8b ( linda, &rcvegr,
1121 			       linda_wq->eager_array, linda_wq->eager_prod );
1122 	DBGC2 ( linda, "Linda %p QPN %ld RX egr %d(%d) posted [%lx,%lx)\n",
1123 		linda, qp->qpn, linda_wq->eager_prod, wqe_idx,
1124 		addr, ( addr + len ) );
1125 
1126 	/* Increment producer index */
1127 	linda_wq->eager_prod = ( ( linda_wq->eager_prod + 1 ) &
1128 				 ( linda_wq->eager_entries - 1 ) );
1129 
1130 	/* Update head index */
1131 	memset ( &rcvegrindexhead, 0, sizeof ( rcvegrindexhead ) );
1132 	BIT_FILL_1 ( &rcvegrindexhead,
1133 		     Value, ( ( linda_wq->eager_prod + 1 ) &
1134 			      ( linda_wq->eager_entries - 1 ) ) );
1135 	linda_writeq_array64k ( linda, &rcvegrindexhead,
1136 				QIB_7220_RcvEgrIndexHead0_offset, ctx );
1137 
1138 	return 0;
1139 }
1140 
1141 /**
1142  * Complete receive work queue entry
1143  *
1144  * @v ibdev		Infiniband device
1145  * @v qp		Queue pair
1146  * @v header_offs	Header offset
1147  */
linda_complete_recv(struct ib_device * ibdev,struct ib_queue_pair * qp,unsigned int header_offs)1148 static void linda_complete_recv ( struct ib_device *ibdev,
1149 				  struct ib_queue_pair *qp,
1150 				  unsigned int header_offs ) {
1151 	struct linda *linda = ib_get_drvdata ( ibdev );
1152 	struct ib_work_queue *wq = &qp->recv;
1153 	struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
1154 	struct QIB_7220_RcvHdrFlags *rcvhdrflags;
1155 	struct QIB_7220_RcvEgr rcvegr;
1156 	struct io_buffer headers;
1157 	struct io_buffer *iobuf;
1158 	struct ib_queue_pair *intended_qp;
1159 	struct ib_address_vector dest;
1160 	struct ib_address_vector source;
1161 	unsigned int rcvtype;
1162 	unsigned int pktlen;
1163 	unsigned int egrindex;
1164 	unsigned int useegrbfr;
1165 	unsigned int iberr, mkerr, tiderr, khdrerr, mtuerr;
1166 	unsigned int lenerr, parityerr, vcrcerr, icrcerr;
1167 	unsigned int err;
1168 	unsigned int hdrqoffset;
1169 	unsigned int header_len;
1170 	unsigned int padded_payload_len;
1171 	unsigned int wqe_idx;
1172 	size_t payload_len;
1173 	int qp0;
1174 	int rc;
1175 
1176 	/* RcvHdrFlags are at the end of the header entry */
1177 	rcvhdrflags = ( linda_wq->header + header_offs +
1178 			LINDA_RECV_HEADER_SIZE - sizeof ( *rcvhdrflags ) );
1179 	rcvtype = BIT_GET ( rcvhdrflags, RcvType );
1180 	pktlen = ( BIT_GET ( rcvhdrflags, PktLen ) << 2 );
1181 	egrindex = BIT_GET ( rcvhdrflags, EgrIndex );
1182 	useegrbfr = BIT_GET ( rcvhdrflags, UseEgrBfr );
1183 	hdrqoffset = ( BIT_GET ( rcvhdrflags, HdrqOffset ) << 2 );
1184 	iberr = BIT_GET ( rcvhdrflags, IBErr );
1185 	mkerr = BIT_GET ( rcvhdrflags, MKErr );
1186 	tiderr = BIT_GET ( rcvhdrflags, TIDErr );
1187 	khdrerr = BIT_GET ( rcvhdrflags, KHdrErr );
1188 	mtuerr = BIT_GET ( rcvhdrflags, MTUErr );
1189 	lenerr = BIT_GET ( rcvhdrflags, LenErr );
1190 	parityerr = BIT_GET ( rcvhdrflags, ParityErr );
1191 	vcrcerr = BIT_GET ( rcvhdrflags, VCRCErr );
1192 	icrcerr = BIT_GET ( rcvhdrflags, ICRCErr );
1193 	header_len = ( LINDA_RECV_HEADER_SIZE - hdrqoffset -
1194 		       sizeof ( *rcvhdrflags ) );
1195 	padded_payload_len = ( pktlen - header_len - 4 /* ICRC */ );
1196 	err = ( iberr | mkerr | tiderr | khdrerr | mtuerr |
1197 		lenerr | parityerr | vcrcerr | icrcerr );
1198 	/* IB header is placed immediately before RcvHdrFlags */
1199 	iob_populate ( &headers, ( ( ( void * ) rcvhdrflags ) - header_len ),
1200 		       header_len, header_len );
1201 
1202 	/* Dump diagnostic information */
1203 	if ( err || ( ! useegrbfr ) ) {
1204 		DBGC ( linda, "Linda %p QPN %ld RX egr %d%s hdr %d type %d "
1205 		       "len %d(%d+%d+4)%s%s%s%s%s%s%s%s%s%s%s\n", linda,
1206 		       qp->qpn, egrindex, ( useegrbfr ? "" : "(unused)" ),
1207 		       ( header_offs / LINDA_RECV_HEADER_SIZE ), rcvtype,
1208 		       pktlen, header_len, padded_payload_len,
1209 		       ( err ? " [Err" : "" ), ( iberr ? " IB" : "" ),
1210 		       ( mkerr ? " MK" : "" ), ( tiderr ? " TID" : "" ),
1211 		       ( khdrerr ? " KHdr" : "" ), ( mtuerr ? " MTU" : "" ),
1212 		       ( lenerr ? " Len" : "" ), ( parityerr ? " Parity" : ""),
1213 		       ( vcrcerr ? " VCRC" : "" ), ( icrcerr ? " ICRC" : "" ),
1214 		       ( err ? "]" : "" ) );
1215 	} else {
1216 		DBGC2 ( linda, "Linda %p QPN %ld RX egr %d hdr %d type %d "
1217 			"len %d(%d+%d+4)\n", linda, qp->qpn, egrindex,
1218 			( header_offs / LINDA_RECV_HEADER_SIZE ), rcvtype,
1219 			pktlen, header_len, padded_payload_len );
1220 	}
1221 	DBGCP_HDA ( linda, hdrqoffset, headers.data,
1222 		    ( header_len + sizeof ( *rcvhdrflags ) ) );
1223 
1224 	/* Parse header to generate address vector */
1225 	qp0 = ( qp->qpn == 0 );
1226 	intended_qp = NULL;
1227 	if ( ( rc = ib_pull ( ibdev, &headers, ( qp0 ? &intended_qp : NULL ),
1228 			      &payload_len, &dest, &source ) ) != 0 ) {
1229 		DBGC ( linda, "Linda %p could not parse headers: %s\n",
1230 		       linda, strerror ( rc ) );
1231 		err = 1;
1232 	}
1233 	if ( ! intended_qp )
1234 		intended_qp = qp;
1235 
1236 	/* Complete this buffer and any skipped buffers.  Note that
1237 	 * when the hardware runs out of buffers, it will repeatedly
1238 	 * report the same buffer (the tail) as a TID error, and that
1239 	 * it also has a habit of sometimes skipping over several
1240 	 * buffers at once.
1241 	 */
1242 	while ( 1 ) {
1243 
1244 		/* If we have caught up to the producer counter, stop.
1245 		 * This will happen when the hardware first runs out
1246 		 * of buffers and starts reporting TID errors against
1247 		 * the eager buffer it wants to use next.
1248 		 */
1249 		if ( linda_wq->eager_cons == linda_wq->eager_prod )
1250 			break;
1251 
1252 		/* If we have caught up to where we should be after
1253 		 * completing this egrindex, stop.  We phrase the test
1254 		 * this way to avoid completing the entire ring when
1255 		 * we receive the same egrindex twice in a row.
1256 		 */
1257 		if ( ( linda_wq->eager_cons ==
1258 		       ( ( egrindex + 1 ) & ( linda_wq->eager_entries - 1 ) )))
1259 			break;
1260 
1261 		/* Identify work queue entry and corresponding I/O
1262 		 * buffer.
1263 		 */
1264 		wqe_idx = ( linda_wq->eager_cons & ( wq->num_wqes - 1 ) );
1265 		iobuf = wq->iobufs[wqe_idx];
1266 		assert ( iobuf != NULL );
1267 		wq->iobufs[wqe_idx] = NULL;
1268 
1269 		/* Complete the eager buffer */
1270 		if ( linda_wq->eager_cons == egrindex ) {
1271 			/* Completing the eager buffer described in
1272 			 * this header entry.
1273 			 */
1274 			if ( payload_len <= iob_tailroom ( iobuf ) ) {
1275 				iob_put ( iobuf, payload_len );
1276 				rc = ( err ?
1277 				       -EIO : ( useegrbfr ? 0 : -ECANCELED ) );
1278 			} else {
1279 				DBGC ( linda, "Linda %p bad payload len %zd\n",
1280 				       linda, payload_len );
1281 				rc = -EPROTO;
1282 			}
1283 			/* Redirect to target QP if necessary */
1284 			if ( qp != intended_qp ) {
1285 				DBGC ( linda, "Linda %p redirecting QPN %ld "
1286 				       "=> %ld\n",
1287 				       linda, qp->qpn, intended_qp->qpn );
1288 				/* Compensate for incorrect fill levels */
1289 				qp->recv.fill--;
1290 				intended_qp->recv.fill++;
1291 			}
1292 			ib_complete_recv ( ibdev, intended_qp, &dest, &source,
1293 					   iobuf, rc );
1294 		} else {
1295 			/* Completing on a skipped-over eager buffer */
1296 			ib_complete_recv ( ibdev, qp, &dest, &source, iobuf,
1297 					   -ECANCELED );
1298 		}
1299 
1300 		/* Clear eager buffer */
1301 		memset ( &rcvegr, 0, sizeof ( rcvegr ) );
1302 		linda_writeq_array8b ( linda, &rcvegr, linda_wq->eager_array,
1303 				       linda_wq->eager_cons );
1304 
1305 		/* Increment consumer index */
1306 		linda_wq->eager_cons = ( ( linda_wq->eager_cons + 1 ) &
1307 					 ( linda_wq->eager_entries - 1 ) );
1308 	}
1309 }
1310 
1311 /**
1312  * Poll receive work queue
1313  *
1314  * @v ibdev		Infiniband device
1315  * @v qp		Queue pair
1316  */
linda_poll_recv_wq(struct ib_device * ibdev,struct ib_queue_pair * qp)1317 static void linda_poll_recv_wq ( struct ib_device *ibdev,
1318 				 struct ib_queue_pair *qp ) {
1319 	struct linda *linda = ib_get_drvdata ( ibdev );
1320 	struct ib_work_queue *wq = &qp->recv;
1321 	struct linda_recv_work_queue *linda_wq = ib_wq_get_drvdata ( wq );
1322 	struct QIB_7220_RcvHdrHead0 rcvhdrhead;
1323 	unsigned int ctx = linda_qpn_to_ctx ( qp->qpn );
1324 	unsigned int header_prod;
1325 
1326 	/* Check for received packets */
1327 	header_prod = ( BIT_GET ( &linda_wq->header_prod, Value ) << 2 );
1328 	if ( header_prod == linda_wq->header_cons )
1329 		return;
1330 
1331 	/* Process all received packets */
1332 	while ( linda_wq->header_cons != header_prod ) {
1333 
1334 		/* Complete the receive */
1335 		linda_complete_recv ( ibdev, qp, linda_wq->header_cons );
1336 
1337 		/* Increment the consumer offset */
1338 		linda_wq->header_cons += LINDA_RECV_HEADER_SIZE;
1339 		linda_wq->header_cons %= LINDA_RECV_HEADERS_SIZE;
1340 	}
1341 
1342 	/* Update consumer offset */
1343 	memset ( &rcvhdrhead, 0, sizeof ( rcvhdrhead ) );
1344 	BIT_FILL_2 ( &rcvhdrhead,
1345 		     RcvHeadPointer, ( linda_wq->header_cons >> 2 ),
1346 		     counter, 1 );
1347 	linda_writeq_array64k ( linda, &rcvhdrhead,
1348 				QIB_7220_RcvHdrHead0_offset, ctx );
1349 }
1350 
1351 /**
1352  * Poll completion queue
1353  *
1354  * @v ibdev		Infiniband device
1355  * @v cq		Completion queue
1356  */
linda_poll_cq(struct ib_device * ibdev,struct ib_completion_queue * cq)1357 static void linda_poll_cq ( struct ib_device *ibdev,
1358 			    struct ib_completion_queue *cq ) {
1359 	struct ib_work_queue *wq;
1360 
1361 	/* Poll associated send and receive queues */
1362 	list_for_each_entry ( wq, &cq->work_queues, list ) {
1363 		if ( wq->is_send ) {
1364 			linda_poll_send_wq ( ibdev, wq->qp );
1365 		} else {
1366 			linda_poll_recv_wq ( ibdev, wq->qp );
1367 		}
1368 	}
1369 }
1370 
1371 /***************************************************************************
1372  *
1373  * Event queues
1374  *
1375  ***************************************************************************
1376  */
1377 
1378 /**
1379  * Poll event queue
1380  *
1381  * @v ibdev		Infiniband device
1382  */
linda_poll_eq(struct ib_device * ibdev)1383 static void linda_poll_eq ( struct ib_device *ibdev ) {
1384 	struct linda *linda = ib_get_drvdata ( ibdev );
1385 	struct QIB_7220_ErrStatus errstatus;
1386 	struct QIB_7220_ErrClear errclear;
1387 
1388 	/* Check for link status changes */
1389 	DBG_DISABLE ( DBGLVL_IO );
1390 	linda_readq ( linda, &errstatus, QIB_7220_ErrStatus_offset );
1391 	DBG_ENABLE ( DBGLVL_IO );
1392 	if ( BIT_GET ( &errstatus, IBStatusChanged ) ) {
1393 		linda_link_state_changed ( ibdev );
1394 		memset ( &errclear, 0, sizeof ( errclear ) );
1395 		BIT_FILL_1 ( &errclear, IBStatusChangedClear, 1 );
1396 		linda_writeq ( linda, &errclear, QIB_7220_ErrClear_offset );
1397 	}
1398 }
1399 
1400 /***************************************************************************
1401  *
1402  * Infiniband link-layer operations
1403  *
1404  ***************************************************************************
1405  */
1406 
1407 /**
1408  * Initialise Infiniband link
1409  *
1410  * @v ibdev		Infiniband device
1411  * @ret rc		Return status code
1412  */
linda_open(struct ib_device * ibdev)1413 static int linda_open ( struct ib_device *ibdev ) {
1414 	struct linda *linda = ib_get_drvdata ( ibdev );
1415 	struct QIB_7220_Control control;
1416 
1417 	/* Disable link */
1418 	linda_readq ( linda, &control, QIB_7220_Control_offset );
1419 	BIT_SET ( &control, LinkEn, 1 );
1420 	linda_writeq ( linda, &control, QIB_7220_Control_offset );
1421 	return 0;
1422 }
1423 
1424 /**
1425  * Close Infiniband link
1426  *
1427  * @v ibdev		Infiniband device
1428  */
linda_close(struct ib_device * ibdev)1429 static void linda_close ( struct ib_device *ibdev ) {
1430 	struct linda *linda = ib_get_drvdata ( ibdev );
1431 	struct QIB_7220_Control control;
1432 
1433 	/* Disable link */
1434 	linda_readq ( linda, &control, QIB_7220_Control_offset );
1435 	BIT_SET ( &control, LinkEn, 0 );
1436 	linda_writeq ( linda, &control, QIB_7220_Control_offset );
1437 }
1438 
1439 /***************************************************************************
1440  *
1441  * Multicast group operations
1442  *
1443  ***************************************************************************
1444  */
1445 
1446 /**
1447  * Attach to multicast group
1448  *
1449  * @v ibdev		Infiniband device
1450  * @v qp		Queue pair
1451  * @v gid		Multicast GID
1452  * @ret rc		Return status code
1453  */
linda_mcast_attach(struct ib_device * ibdev,struct ib_queue_pair * qp,union ib_gid * gid)1454 static int linda_mcast_attach ( struct ib_device *ibdev,
1455 				struct ib_queue_pair *qp,
1456 				union ib_gid *gid ) {
1457 	struct linda *linda = ib_get_drvdata ( ibdev );
1458 
1459 	( void ) linda;
1460 	( void ) qp;
1461 	( void ) gid;
1462 	return 0;
1463 }
1464 
1465 /**
1466  * Detach from multicast group
1467  *
1468  * @v ibdev		Infiniband device
1469  * @v qp		Queue pair
1470  * @v gid		Multicast GID
1471  */
linda_mcast_detach(struct ib_device * ibdev,struct ib_queue_pair * qp,union ib_gid * gid)1472 static void linda_mcast_detach ( struct ib_device *ibdev,
1473 				 struct ib_queue_pair *qp,
1474 				 union ib_gid *gid ) {
1475 	struct linda *linda = ib_get_drvdata ( ibdev );
1476 
1477 	( void ) linda;
1478 	( void ) qp;
1479 	( void ) gid;
1480 }
1481 
1482 /** Linda Infiniband operations */
1483 static struct ib_device_operations linda_ib_operations = {
1484 	.create_cq	= linda_create_cq,
1485 	.destroy_cq	= linda_destroy_cq,
1486 	.create_qp	= linda_create_qp,
1487 	.modify_qp	= linda_modify_qp,
1488 	.destroy_qp	= linda_destroy_qp,
1489 	.post_send	= linda_post_send,
1490 	.post_recv	= linda_post_recv,
1491 	.poll_cq	= linda_poll_cq,
1492 	.poll_eq	= linda_poll_eq,
1493 	.open		= linda_open,
1494 	.close		= linda_close,
1495 	.mcast_attach	= linda_mcast_attach,
1496 	.mcast_detach	= linda_mcast_detach,
1497 	.set_port_info	= linda_set_port_info,
1498 	.set_pkey_table	= linda_set_pkey_table,
1499 };
1500 
1501 /***************************************************************************
1502  *
1503  * I2C bus operations
1504  *
1505  ***************************************************************************
1506  */
1507 
1508 /** Linda I2C bit to GPIO mappings */
1509 static unsigned int linda_i2c_bits[] = {
1510 	[I2C_BIT_SCL] = ( 1 << LINDA_GPIO_SCL ),
1511 	[I2C_BIT_SDA] = ( 1 << LINDA_GPIO_SDA ),
1512 };
1513 
1514 /**
1515  * Read Linda I2C line status
1516  *
1517  * @v basher		Bit-bashing interface
1518  * @v bit_id		Bit number
1519  * @ret zero		Input is a logic 0
1520  * @ret non-zero	Input is a logic 1
1521  */
linda_i2c_read_bit(struct bit_basher * basher,unsigned int bit_id)1522 static int linda_i2c_read_bit ( struct bit_basher *basher,
1523 				unsigned int bit_id ) {
1524 	struct linda *linda =
1525 		container_of ( basher, struct linda, i2c.basher );
1526 	struct QIB_7220_EXTStatus extstatus;
1527 	unsigned int status;
1528 
1529 	DBG_DISABLE ( DBGLVL_IO );
1530 
1531 	linda_readq ( linda, &extstatus, QIB_7220_EXTStatus_offset );
1532 	status = ( BIT_GET ( &extstatus, GPIOIn ) & linda_i2c_bits[bit_id] );
1533 
1534 	DBG_ENABLE ( DBGLVL_IO );
1535 
1536 	return status;
1537 }
1538 
1539 /**
1540  * Write Linda I2C line status
1541  *
1542  * @v basher		Bit-bashing interface
1543  * @v bit_id		Bit number
1544  * @v data		Value to write
1545  */
linda_i2c_write_bit(struct bit_basher * basher,unsigned int bit_id,unsigned long data)1546 static void linda_i2c_write_bit ( struct bit_basher *basher,
1547 				  unsigned int bit_id, unsigned long data ) {
1548 	struct linda *linda =
1549 		container_of ( basher, struct linda, i2c.basher );
1550 	struct QIB_7220_EXTCtrl extctrl;
1551 	struct QIB_7220_GPIO gpioout;
1552 	unsigned int bit = linda_i2c_bits[bit_id];
1553 	unsigned int outputs = 0;
1554 	unsigned int output_enables = 0;
1555 
1556 	DBG_DISABLE ( DBGLVL_IO );
1557 
1558 	/* Read current GPIO mask and outputs */
1559 	linda_readq ( linda, &extctrl, QIB_7220_EXTCtrl_offset );
1560 	linda_readq ( linda, &gpioout, QIB_7220_GPIOOut_offset );
1561 
1562 	/* Update outputs and output enables.  I2C lines are tied
1563 	 * high, so we always set the output to 0 and use the output
1564 	 * enable to control the line.
1565 	 */
1566 	output_enables = BIT_GET ( &extctrl, GPIOOe );
1567 	output_enables = ( ( output_enables & ~bit ) | ( ~data & bit ) );
1568 	outputs = BIT_GET ( &gpioout, GPIO );
1569 	outputs = ( outputs & ~bit );
1570 	BIT_SET ( &extctrl, GPIOOe, output_enables );
1571 	BIT_SET ( &gpioout, GPIO, outputs );
1572 
1573 	/* Write the output enable first; that way we avoid logic
1574 	 * hazards.
1575 	 */
1576 	linda_writeq ( linda, &extctrl, QIB_7220_EXTCtrl_offset );
1577 	linda_writeq ( linda, &gpioout, QIB_7220_GPIOOut_offset );
1578 	mb();
1579 
1580 	DBG_ENABLE ( DBGLVL_IO );
1581 }
1582 
1583 /** Linda I2C bit-bashing interface operations */
1584 static struct bit_basher_operations linda_i2c_basher_ops = {
1585 	.read	= linda_i2c_read_bit,
1586 	.write	= linda_i2c_write_bit,
1587 };
1588 
1589 /**
1590  * Initialise Linda I2C subsystem
1591  *
1592  * @v linda		Linda device
1593  * @ret rc		Return status code
1594  */
linda_init_i2c(struct linda * linda)1595 static int linda_init_i2c ( struct linda *linda ) {
1596 	static int try_eeprom_address[] = { 0x51, 0x50 };
1597 	unsigned int i;
1598 	int rc;
1599 
1600 	/* Initialise bus */
1601 	if ( ( rc = init_i2c_bit_basher ( &linda->i2c,
1602 					  &linda_i2c_basher_ops ) ) != 0 ) {
1603 		DBGC ( linda, "Linda %p could not initialise I2C bus: %s\n",
1604 		       linda, strerror ( rc ) );
1605 		return rc;
1606 	}
1607 
1608 	/* Probe for devices */
1609 	for ( i = 0 ; i < ( sizeof ( try_eeprom_address ) /
1610 			    sizeof ( try_eeprom_address[0] ) ) ; i++ ) {
1611 		init_i2c_eeprom ( &linda->eeprom, try_eeprom_address[i] );
1612 		if ( ( rc = i2c_check_presence ( &linda->i2c.i2c,
1613 						 &linda->eeprom ) ) == 0 ) {
1614 			DBGC2 ( linda, "Linda %p found EEPROM at %02x\n",
1615 				linda, try_eeprom_address[i] );
1616 			return 0;
1617 		}
1618 	}
1619 
1620 	DBGC ( linda, "Linda %p could not find EEPROM\n", linda );
1621 	return -ENODEV;
1622 }
1623 
1624 /**
1625  * Read EEPROM parameters
1626  *
1627  * @v linda		Linda device
1628  * @v guid		GUID to fill in
1629  * @ret rc		Return status code
1630  */
linda_read_eeprom(struct linda * linda,union ib_guid * guid)1631 static int linda_read_eeprom ( struct linda *linda, union ib_guid *guid ) {
1632 	struct i2c_interface *i2c = &linda->i2c.i2c;
1633 	int rc;
1634 
1635 	/* Read GUID */
1636 	if ( ( rc = i2c->read ( i2c, &linda->eeprom, LINDA_EEPROM_GUID_OFFSET,
1637 				guid->bytes, sizeof ( *guid ) ) ) != 0 ) {
1638 		DBGC ( linda, "Linda %p could not read GUID: %s\n",
1639 		       linda, strerror ( rc ) );
1640 		return rc;
1641 	}
1642 	DBGC2 ( linda, "Linda %p has GUID " IB_GUID_FMT "\n",
1643 		linda, IB_GUID_ARGS ( guid ) );
1644 
1645 	/* Read serial number (debug only) */
1646 	if ( DBG_LOG ) {
1647 		uint8_t serial[LINDA_EEPROM_SERIAL_SIZE + 1];
1648 
1649 		serial[ sizeof ( serial ) - 1 ] = '\0';
1650 		if ( ( rc = i2c->read ( i2c, &linda->eeprom,
1651 					LINDA_EEPROM_SERIAL_OFFSET, serial,
1652 					( sizeof ( serial ) - 1 ) ) ) != 0 ) {
1653 			DBGC ( linda, "Linda %p could not read serial: %s\n",
1654 			       linda, strerror ( rc ) );
1655 			return rc;
1656 		}
1657 		DBGC2 ( linda, "Linda %p has serial number \"%s\"\n",
1658 			linda, serial );
1659 	}
1660 
1661 	return 0;
1662 }
1663 
1664 /***************************************************************************
1665  *
1666  * External parallel bus access
1667  *
1668  ***************************************************************************
1669  */
1670 
1671 /**
1672  * Request ownership of the IB external parallel bus
1673  *
1674  * @v linda		Linda device
1675  * @ret rc		Return status code
1676  */
linda_ib_epb_request(struct linda * linda)1677 static int linda_ib_epb_request ( struct linda *linda ) {
1678 	struct QIB_7220_ibsd_epb_access_ctrl access;
1679 	unsigned int i;
1680 
1681 	/* Request ownership */
1682 	memset ( &access, 0, sizeof ( access ) );
1683 	BIT_FILL_1 ( &access, sw_ib_epb_req, 1 );
1684 	linda_writeq ( linda, &access, QIB_7220_ibsd_epb_access_ctrl_offset );
1685 
1686 	/* Wait for ownership to be granted */
1687 	for ( i = 0 ; i < LINDA_EPB_REQUEST_MAX_WAIT_US ; i++ ) {
1688 		linda_readq ( linda, &access,
1689 			      QIB_7220_ibsd_epb_access_ctrl_offset );
1690 		if ( BIT_GET ( &access, sw_ib_epb_req_granted ) )
1691 			return 0;
1692 		udelay ( 1 );
1693 	}
1694 
1695 	DBGC ( linda, "Linda %p timed out waiting for IB EPB request\n",
1696 	       linda );
1697 	return -ETIMEDOUT;
1698 }
1699 
1700 /**
1701  * Wait for IB external parallel bus transaction to complete
1702  *
1703  * @v linda		Linda device
1704  * @v xact		Buffer to hold transaction result
1705  * @ret rc		Return status code
1706  */
linda_ib_epb_wait(struct linda * linda,struct QIB_7220_ibsd_epb_transaction_reg * xact)1707 static int linda_ib_epb_wait ( struct linda *linda,
1708 			    struct QIB_7220_ibsd_epb_transaction_reg *xact ) {
1709 	unsigned int i;
1710 
1711 	/* Discard first read to allow for signals crossing clock domains */
1712 	linda_readq ( linda, xact, QIB_7220_ibsd_epb_transaction_reg_offset );
1713 
1714 	for ( i = 0 ; i < LINDA_EPB_XACT_MAX_WAIT_US ; i++ ) {
1715 		linda_readq ( linda, xact,
1716 			      QIB_7220_ibsd_epb_transaction_reg_offset );
1717 		if ( BIT_GET ( xact, ib_epb_rdy ) ) {
1718 			if ( BIT_GET ( xact, ib_epb_req_error ) ) {
1719 				DBGC ( linda, "Linda %p EPB transaction "
1720 				       "failed\n", linda );
1721 				return -EIO;
1722 			} else {
1723 				return 0;
1724 			}
1725 		}
1726 		udelay ( 1 );
1727 	}
1728 
1729 	DBGC ( linda, "Linda %p timed out waiting for IB EPB transaction\n",
1730 	       linda );
1731 	return -ETIMEDOUT;
1732 }
1733 
1734 /**
1735  * Release ownership of the IB external parallel bus
1736  *
1737  * @v linda		Linda device
1738  */
linda_ib_epb_release(struct linda * linda)1739 static void linda_ib_epb_release ( struct linda *linda ) {
1740 	struct QIB_7220_ibsd_epb_access_ctrl access;
1741 
1742 	memset ( &access, 0, sizeof ( access ) );
1743 	BIT_FILL_1 ( &access, sw_ib_epb_req, 0 );
1744 	linda_writeq ( linda, &access, QIB_7220_ibsd_epb_access_ctrl_offset );
1745 }
1746 
1747 /**
1748  * Read data via IB external parallel bus
1749  *
1750  * @v linda		Linda device
1751  * @v location		EPB location
1752  * @ret data		Data read, or negative error
1753  *
1754  * You must have already acquired ownership of the IB external
1755  * parallel bus.
1756  */
linda_ib_epb_read(struct linda * linda,unsigned int location)1757 static int linda_ib_epb_read ( struct linda *linda, unsigned int location ) {
1758 	struct QIB_7220_ibsd_epb_transaction_reg xact;
1759 	unsigned int data;
1760 	int rc;
1761 
1762 	/* Ensure no transaction is currently in progress */
1763 	if ( ( rc = linda_ib_epb_wait ( linda, &xact ) ) != 0 )
1764 		return rc;
1765 
1766 	/* Process data */
1767 	memset ( &xact, 0, sizeof ( xact ) );
1768 	BIT_FILL_3 ( &xact,
1769 		     ib_epb_address, LINDA_EPB_LOC_ADDRESS ( location ),
1770 		     ib_epb_read_write, LINDA_EPB_READ,
1771 		     ib_epb_cs, LINDA_EPB_LOC_CS ( location ) );
1772 	linda_writeq ( linda, &xact,
1773 		       QIB_7220_ibsd_epb_transaction_reg_offset );
1774 
1775 	/* Wait for transaction to complete */
1776 	if ( ( rc = linda_ib_epb_wait ( linda, &xact ) ) != 0 )
1777 		return rc;
1778 
1779 	data = BIT_GET ( &xact, ib_epb_data );
1780 	return data;
1781 }
1782 
1783 /**
1784  * Write data via IB external parallel bus
1785  *
1786  * @v linda		Linda device
1787  * @v location		EPB location
1788  * @v data		Data to write
1789  * @ret rc		Return status code
1790  *
1791  * You must have already acquired ownership of the IB external
1792  * parallel bus.
1793  */
linda_ib_epb_write(struct linda * linda,unsigned int location,unsigned int data)1794 static int linda_ib_epb_write ( struct linda *linda, unsigned int location,
1795 				unsigned int data ) {
1796 	struct QIB_7220_ibsd_epb_transaction_reg xact;
1797 	int rc;
1798 
1799 	/* Ensure no transaction is currently in progress */
1800 	if ( ( rc = linda_ib_epb_wait ( linda, &xact ) ) != 0 )
1801 		return rc;
1802 
1803 	/* Process data */
1804 	memset ( &xact, 0, sizeof ( xact ) );
1805 	BIT_FILL_4 ( &xact,
1806 		     ib_epb_data, data,
1807 		     ib_epb_address, LINDA_EPB_LOC_ADDRESS ( location ),
1808 		     ib_epb_read_write, LINDA_EPB_WRITE,
1809 		     ib_epb_cs, LINDA_EPB_LOC_CS ( location ) );
1810 	linda_writeq ( linda, &xact,
1811 		       QIB_7220_ibsd_epb_transaction_reg_offset );
1812 
1813 	/* Wait for transaction to complete */
1814 	if ( ( rc = linda_ib_epb_wait ( linda, &xact ) ) != 0 )
1815 		return rc;
1816 
1817 	return 0;
1818 }
1819 
1820 /**
1821  * Read/modify/write EPB register
1822  *
1823  * @v linda		Linda device
1824  * @v cs		Chip select
1825  * @v channel		Channel
1826  * @v element		Element
1827  * @v reg		Register
1828  * @v value		Value to set
1829  * @v mask		Mask to apply to old value
1830  * @ret rc		Return status code
1831  */
linda_ib_epb_mod_reg(struct linda * linda,unsigned int cs,unsigned int channel,unsigned int element,unsigned int reg,unsigned int value,unsigned int mask)1832 static int linda_ib_epb_mod_reg ( struct linda *linda, unsigned int cs,
1833 				  unsigned int channel, unsigned int element,
1834 				  unsigned int reg, unsigned int value,
1835 				  unsigned int mask ) {
1836 	unsigned int location;
1837 	int old_value;
1838 	int rc;
1839 
1840 	DBG_DISABLE ( DBGLVL_IO );
1841 
1842 	/* Sanity check */
1843 	assert ( ( value & mask ) == value );
1844 
1845 	/* Acquire bus ownership */
1846 	if ( ( rc = linda_ib_epb_request ( linda ) ) != 0 )
1847 		goto out;
1848 
1849 	/* Read existing value, if necessary */
1850 	location = LINDA_EPB_LOC ( cs, channel, element, reg );
1851 	if ( (~mask) & 0xff ) {
1852 		old_value = linda_ib_epb_read ( linda, location );
1853 		if ( old_value < 0 ) {
1854 			rc = old_value;
1855 			goto out_release;
1856 		}
1857 	} else {
1858 		old_value = 0;
1859 	}
1860 
1861 	/* Update value */
1862 	value = ( ( old_value & ~mask ) | value );
1863 	DBGCP ( linda, "Linda %p CS %d EPB(%d,%d,%#02x) %#02x => %#02x\n",
1864 		linda, cs, channel, element, reg, old_value, value );
1865 	if ( ( rc = linda_ib_epb_write ( linda, location, value ) ) != 0 )
1866 		goto out_release;
1867 
1868  out_release:
1869 	/* Release bus */
1870 	linda_ib_epb_release ( linda );
1871  out:
1872 	DBG_ENABLE ( DBGLVL_IO );
1873 	return rc;
1874 }
1875 
1876 /**
1877  * Transfer data to/from microcontroller RAM
1878  *
1879  * @v linda		Linda device
1880  * @v address		Starting address
1881  * @v write		Data to write, or NULL
1882  * @v read		Data to read, or NULL
1883  * @v len		Length of data
1884  * @ret rc		Return status code
1885  */
linda_ib_epb_ram_xfer(struct linda * linda,unsigned int address,const void * write,void * read,size_t len)1886 static int linda_ib_epb_ram_xfer ( struct linda *linda, unsigned int address,
1887 				   const void *write, void *read,
1888 				   size_t len ) {
1889 	unsigned int control;
1890 	unsigned int address_hi;
1891 	unsigned int address_lo;
1892 	int data;
1893 	int rc;
1894 
1895 	DBG_DISABLE ( DBGLVL_IO );
1896 
1897 	assert ( ! ( write && read ) );
1898 	assert ( ( address % LINDA_EPB_UC_CHUNK_SIZE ) == 0 );
1899 	assert ( ( len % LINDA_EPB_UC_CHUNK_SIZE ) == 0 );
1900 
1901 	/* Acquire bus ownership */
1902 	if ( ( rc = linda_ib_epb_request ( linda ) ) != 0 )
1903 		goto out;
1904 
1905 	/* Process data */
1906 	while ( len ) {
1907 
1908 		/* Reset the address for each new chunk */
1909 		if ( ( address % LINDA_EPB_UC_CHUNK_SIZE ) == 0 ) {
1910 
1911 			/* Write the control register */
1912 			control = ( read ? LINDA_EPB_UC_CTL_READ :
1913 				    LINDA_EPB_UC_CTL_WRITE );
1914 			if ( ( rc = linda_ib_epb_write ( linda,
1915 							 LINDA_EPB_UC_CTL,
1916 							 control ) ) != 0 )
1917 				break;
1918 
1919 			/* Write the address registers */
1920 			address_hi = ( address >> 8 );
1921 			if ( ( rc = linda_ib_epb_write ( linda,
1922 							 LINDA_EPB_UC_ADDR_HI,
1923 							 address_hi ) ) != 0 )
1924 				break;
1925 			address_lo = ( address & 0xff );
1926 			if ( ( rc = linda_ib_epb_write ( linda,
1927 							 LINDA_EPB_UC_ADDR_LO,
1928 							 address_lo ) ) != 0 )
1929 				break;
1930 		}
1931 
1932 		/* Read or write the data */
1933 		if ( read ) {
1934 			data = linda_ib_epb_read ( linda, LINDA_EPB_UC_DATA );
1935 			if ( data < 0 ) {
1936 				rc = data;
1937 				break;
1938 			}
1939 			*( ( uint8_t * ) read++ ) = data;
1940 		} else {
1941 			data = *( ( uint8_t * ) write++ );
1942 			if ( ( rc = linda_ib_epb_write ( linda,
1943 							 LINDA_EPB_UC_DATA,
1944 							 data ) ) != 0 )
1945 				break;
1946 		}
1947 		address++;
1948 		len--;
1949 
1950 		/* Reset the control byte after each chunk */
1951 		if ( ( address % LINDA_EPB_UC_CHUNK_SIZE ) == 0 ) {
1952 			if ( ( rc = linda_ib_epb_write ( linda,
1953 							 LINDA_EPB_UC_CTL,
1954 							 0 ) ) != 0 )
1955 				break;
1956 		}
1957 	}
1958 
1959 	/* Release bus */
1960 	linda_ib_epb_release ( linda );
1961 
1962  out:
1963 	DBG_ENABLE ( DBGLVL_IO );
1964 	return rc;
1965 }
1966 
1967 /***************************************************************************
1968  *
1969  * Infiniband SerDes initialisation
1970  *
1971  ***************************************************************************
1972  */
1973 
1974 /** A Linda SerDes parameter */
1975 struct linda_serdes_param {
1976 	/** EPB address as constructed by LINDA_EPB_ADDRESS() */
1977 	uint16_t address;
1978 	/** Value to set */
1979 	uint8_t value;
1980 	/** Mask to apply to old value */
1981 	uint8_t mask;
1982 } __packed;
1983 
1984 /** Magic "all channels" channel number */
1985 #define LINDA_EPB_ALL_CHANNELS 31
1986 
1987 /** End of SerDes parameter list marker */
1988 #define LINDA_SERDES_PARAM_END { 0, 0, 0 }
1989 
1990 /**
1991  * Program IB SerDes register(s)
1992  *
1993  * @v linda		Linda device
1994  * @v param		SerDes parameter
1995  * @ret rc		Return status code
1996  */
linda_set_serdes_param(struct linda * linda,struct linda_serdes_param * param)1997 static int linda_set_serdes_param ( struct linda *linda,
1998 				    struct linda_serdes_param *param ) {
1999 	unsigned int channel;
2000 	unsigned int channel_start;
2001 	unsigned int channel_end;
2002 	unsigned int element;
2003 	unsigned int reg;
2004 	int rc;
2005 
2006 	/* Break down the EPB address and determine channels */
2007 	channel = LINDA_EPB_ADDRESS_CHANNEL ( param->address );
2008 	element = LINDA_EPB_ADDRESS_ELEMENT ( param->address );
2009 	reg = LINDA_EPB_ADDRESS_REG ( param->address );
2010 	if ( channel == LINDA_EPB_ALL_CHANNELS ) {
2011 		channel_start = 0;
2012 		channel_end = 3;
2013 	} else {
2014 		channel_start = channel_end = channel;
2015 	}
2016 
2017 	/* Modify register for each specified channel */
2018 	for ( channel = channel_start ; channel <= channel_end ; channel++ ) {
2019 		if ( ( rc = linda_ib_epb_mod_reg ( linda, LINDA_EPB_CS_SERDES,
2020 						   channel, element, reg,
2021 						   param->value,
2022 						   param->mask ) ) != 0 )
2023 			return rc;
2024 	}
2025 
2026 	return 0;
2027 }
2028 
2029 /**
2030  * Program IB SerDes registers
2031  *
2032  * @v linda		Linda device
2033  * @v param		SerDes parameters
2034  * @v count		Number of parameters
2035  * @ret rc		Return status code
2036  */
linda_set_serdes_params(struct linda * linda,struct linda_serdes_param * params)2037 static int linda_set_serdes_params ( struct linda *linda,
2038 				     struct linda_serdes_param *params ) {
2039 	int rc;
2040 
2041 	for ( ; params->mask != 0 ; params++ ){
2042 		if ( ( rc = linda_set_serdes_param ( linda,
2043 							 params ) ) != 0 )
2044 			return rc;
2045 	}
2046 
2047 	return 0;
2048 }
2049 
2050 #define LINDA_DDS_VAL( amp_d, main_d, ipst_d, ipre_d,			\
2051 		       amp_s, main_s, ipst_s, ipre_s )			\
2052 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x00 ),	\
2053 	  ( ( ( amp_d & 0x1f ) << 1 ) | 1 ), 0xff },			\
2054 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x01 ),	\
2055 	  ( ( ( amp_s & 0x1f ) << 1 ) | 1 ), 0xff },			\
2056 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x09 ),	\
2057 	  ( ( main_d << 3 ) | 4 | ( ipre_d >> 2 ) ), 0xff },		\
2058 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x0a ),	\
2059 	  ( ( main_s << 3 ) | 4 | ( ipre_s >> 2 ) ), 0xff },		\
2060 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x06 ),	\
2061 	  ( ( ( ipst_d & 0xf ) << 1 ) |					\
2062 	    ( ( ipre_d & 3 ) << 6 ) | 0x21 ), 0xff },			\
2063 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 9, 0x07 ),	\
2064 	  ( ( ( ipst_s & 0xf ) << 1 ) |					\
2065 	    ( ( ipre_s & 3 ) << 6) | 0x21 ), 0xff }
2066 
2067 /**
2068  * Linda SerDes default parameters
2069  *
2070  * These magic start-of-day values are taken from the Linux driver.
2071  */
2072 static struct linda_serdes_param linda_serdes_defaults1[] = {
2073 	/* RXHSCTRL0 */
2074 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x00 ), 0xd4, 0xff },
2075 	/* VCDL_DAC2 */
2076 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x05 ), 0x2d, 0xff },
2077 	/* VCDL_CTRL2 */
2078 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x08 ), 0x03, 0x0f },
2079 	/* START_EQ1 */
2080 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x27 ), 0x10, 0xff },
2081 	/* START_EQ2 */
2082 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x28 ), 0x30, 0xff },
2083 	/* BACTRL */
2084 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x0e ), 0x40, 0xff },
2085 	/* LDOUTCTRL1 */
2086 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x06 ), 0x04, 0xff },
2087 	/* RXHSSTATUS */
2088 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x0f ), 0x04, 0xff },
2089 	/* End of this block */
2090 	LINDA_SERDES_PARAM_END
2091 };
2092 static struct linda_serdes_param linda_serdes_defaults2[] = {
2093 	/* LDOUTCTRL1 */
2094 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x06 ), 0x00, 0xff },
2095 	/* DDS values */
2096 	LINDA_DDS_VAL ( 31, 19, 12, 0, 29, 22, 9, 0 ),
2097 	/* Set Rcv Eq. to Preset node */
2098 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x27 ), 0x10, 0xff },
2099 	/* DFELTHFDR */
2100 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x08 ), 0x00, 0xff },
2101 	/* DFELTHHDR */
2102 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x21 ), 0x00, 0xff },
2103 	/* TLTHFDR */
2104 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x09 ), 0x02, 0xff },
2105 	/* TLTHHDR */
2106 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x23 ), 0x02, 0xff },
2107 	/* ZFR */
2108 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x1b ), 0x0c, 0xff },
2109 	/* ZCNT) */
2110 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x1c ), 0x0c, 0xff },
2111 	/* GFR */
2112 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x1e ), 0x10, 0xff },
2113 	/* GHR */
2114 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x1f ), 0x10, 0xff },
2115 	/* VCDL_CTRL0 toggle */
2116 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x06 ), 0x20, 0xff },
2117 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 6, 0x06 ), 0x00, 0xff },
2118 	/* CMUCTRL5 */
2119 	{ LINDA_EPB_ADDRESS (			   7, 0, 0x15 ), 0x80, 0xff },
2120 	/* End of this block */
2121 	LINDA_SERDES_PARAM_END
2122 };
2123 static struct linda_serdes_param linda_serdes_defaults3[] = {
2124 	/* START_EQ1 */
2125 	{ LINDA_EPB_ADDRESS ( LINDA_EPB_ALL_CHANNELS, 7, 0x27 ), 0x00, 0x38 },
2126 	/* End of this block */
2127 	LINDA_SERDES_PARAM_END
2128 };
2129 
2130 /**
2131  * Program the microcontroller RAM
2132  *
2133  * @v linda		Linda device
2134  * @ret rc		Return status code
2135  */
linda_program_uc_ram(struct linda * linda)2136 static int linda_program_uc_ram ( struct linda *linda ) {
2137 	int rc;
2138 
2139 	if ( ( rc = linda_ib_epb_ram_xfer ( linda, 0, linda_ib_fw, NULL,
2140 					    sizeof ( linda_ib_fw ) ) ) != 0 ){
2141 		DBGC ( linda, "Linda %p could not load IB firmware: %s\n",
2142 		       linda, strerror ( rc ) );
2143 		return rc;
2144 	}
2145 
2146 	return 0;
2147 }
2148 
2149 /**
2150  * Verify the microcontroller RAM
2151  *
2152  * @v linda		Linda device
2153  * @ret rc		Return status code
2154  */
linda_verify_uc_ram(struct linda * linda)2155 static int linda_verify_uc_ram ( struct linda *linda ) {
2156 	uint8_t verify[LINDA_EPB_UC_CHUNK_SIZE];
2157 	unsigned int offset;
2158 	int rc;
2159 
2160 	for ( offset = 0 ; offset < sizeof ( linda_ib_fw );
2161 	      offset += sizeof ( verify ) ) {
2162 		if ( ( rc = linda_ib_epb_ram_xfer ( linda, offset,
2163 						    NULL, verify,
2164 						    sizeof (verify) )) != 0 ){
2165 			DBGC ( linda, "Linda %p could not read back IB "
2166 			       "firmware: %s\n", linda, strerror ( rc ) );
2167 			return rc;
2168 		}
2169 		if ( memcmp ( ( linda_ib_fw + offset ), verify,
2170 			      sizeof ( verify ) ) != 0 ) {
2171 			DBGC ( linda, "Linda %p firmware verification failed "
2172 			       "at offset %#x\n", linda, offset );
2173 			DBGC_HDA ( linda, offset, ( linda_ib_fw + offset ),
2174 				   sizeof ( verify ) );
2175 			DBGC_HDA ( linda, offset, verify, sizeof ( verify ) );
2176 			return -EIO;
2177 		}
2178 	}
2179 
2180 	DBGC2 ( linda, "Linda %p firmware verified ok\n", linda );
2181 	return 0;
2182 }
2183 
2184 /**
2185  * Use the microcontroller to trim the IB link
2186  *
2187  * @v linda		Linda device
2188  * @ret rc		Return status code
2189  */
linda_trim_ib(struct linda * linda)2190 static int linda_trim_ib ( struct linda *linda ) {
2191 	struct QIB_7220_IBSerDesCtrl ctrl;
2192 	struct QIB_7220_IntStatus intstatus;
2193 	unsigned int i;
2194 	int rc;
2195 
2196 	/* Bring the microcontroller out of reset */
2197 	linda_readq ( linda, &ctrl, QIB_7220_IBSerDesCtrl_offset );
2198 	BIT_SET ( &ctrl, ResetIB_uC_Core, 0 );
2199 	linda_writeq ( linda, &ctrl, QIB_7220_IBSerDesCtrl_offset );
2200 
2201 	/* Wait for the "trim done" signal */
2202 	for ( i = 0 ; i < LINDA_TRIM_DONE_MAX_WAIT_MS ; i++ ) {
2203 		linda_readq ( linda, &intstatus, QIB_7220_IntStatus_offset );
2204 		if ( BIT_GET ( &intstatus, IBSerdesTrimDone ) ) {
2205 			rc = 0;
2206 			goto out_reset;
2207 		}
2208 		mdelay ( 1 );
2209 	}
2210 
2211 	DBGC ( linda, "Linda %p timed out waiting for trim done\n", linda );
2212 	rc = -ETIMEDOUT;
2213  out_reset:
2214 	/* Put the microcontroller back into reset */
2215 	BIT_SET ( &ctrl, ResetIB_uC_Core, 1 );
2216 	linda_writeq ( linda, &ctrl, QIB_7220_IBSerDesCtrl_offset );
2217 
2218 	return rc;
2219 }
2220 
2221 /**
2222  * Initialise the IB SerDes
2223  *
2224  * @v linda		Linda device
2225  * @ret rc		Return status code
2226  */
linda_init_ib_serdes(struct linda * linda)2227 static int linda_init_ib_serdes ( struct linda *linda ) {
2228 	struct QIB_7220_Control control;
2229 	struct QIB_7220_IBCCtrl ibcctrl;
2230 	struct QIB_7220_IBCDDRCtrl ibcddrctrl;
2231 	struct QIB_7220_XGXSCfg xgxscfg;
2232 	int rc;
2233 
2234 	/* Disable link */
2235 	linda_readq ( linda, &control, QIB_7220_Control_offset );
2236 	BIT_SET ( &control, LinkEn, 0 );
2237 	linda_writeq ( linda, &control, QIB_7220_Control_offset );
2238 
2239 	/* Configure sensible defaults for IBC */
2240 	memset ( &ibcctrl, 0, sizeof ( ibcctrl ) );
2241 	BIT_FILL_6 ( &ibcctrl, /* Tuning values taken from Linux driver */
2242 		     FlowCtrlPeriod, 0x03,
2243 		     FlowCtrlWaterMark, 0x05,
2244 		     MaxPktLen, ( ( LINDA_RECV_HEADER_SIZE +
2245 				    LINDA_RECV_PAYLOAD_SIZE +
2246 				    4 /* ICRC */ ) >> 2 ),
2247 		     PhyerrThreshold, 0xf,
2248 		     OverrunThreshold, 0xf,
2249 		     CreditScale, 0x4 );
2250 	linda_writeq ( linda, &ibcctrl, QIB_7220_IBCCtrl_offset );
2251 
2252 	/* Force SDR only to avoid needing all the DDR tuning,
2253 	 * Mellanox compatibility hacks etc.  SDR is plenty for
2254 	 * boot-time operation.
2255 	 */
2256 	linda_readq ( linda, &ibcddrctrl, QIB_7220_IBCDDRCtrl_offset );
2257 	BIT_SET ( &ibcddrctrl, IB_ENHANCED_MODE, 0 );
2258 	BIT_SET ( &ibcddrctrl, SD_SPEED_SDR, 1 );
2259 	BIT_SET ( &ibcddrctrl, SD_SPEED_DDR, 0 );
2260 	BIT_SET ( &ibcddrctrl, SD_SPEED_QDR, 0 );
2261 	BIT_SET ( &ibcddrctrl, HRTBT_ENB, 0 );
2262 	BIT_SET ( &ibcddrctrl, HRTBT_AUTO, 0 );
2263 	linda_writeq ( linda, &ibcddrctrl, QIB_7220_IBCDDRCtrl_offset );
2264 
2265 	/* Set default SerDes parameters */
2266 	if ( ( rc = linda_set_serdes_params ( linda,
2267 					      linda_serdes_defaults1 ) ) != 0 )
2268 		return rc;
2269 	udelay ( 415 ); /* Magic delay while SerDes sorts itself out */
2270 	if ( ( rc = linda_set_serdes_params ( linda,
2271 					      linda_serdes_defaults2 ) ) != 0 )
2272 		return rc;
2273 
2274 	/* Program the microcontroller RAM */
2275 	if ( ( rc = linda_program_uc_ram ( linda ) ) != 0 )
2276 		return rc;
2277 
2278 	/* Verify the microcontroller RAM contents */
2279 	if ( DBGLVL_LOG ) {
2280 		if ( ( rc = linda_verify_uc_ram ( linda ) ) != 0 )
2281 			return rc;
2282 	}
2283 
2284 	/* More SerDes tuning */
2285 	if ( ( rc = linda_set_serdes_params ( linda,
2286 					      linda_serdes_defaults3 ) ) != 0 )
2287 		return rc;
2288 
2289 	/* Use the microcontroller to trim the IB link */
2290 	if ( ( rc = linda_trim_ib ( linda ) ) != 0 )
2291 		return rc;
2292 
2293 	/* Bring XGXS out of reset */
2294 	linda_readq ( linda, &xgxscfg, QIB_7220_XGXSCfg_offset );
2295 	BIT_SET ( &xgxscfg, tx_rx_reset, 0 );
2296 	BIT_SET ( &xgxscfg, xcv_reset, 0 );
2297 	linda_writeq ( linda, &xgxscfg, QIB_7220_XGXSCfg_offset );
2298 
2299 	return rc;
2300 }
2301 
2302 /***************************************************************************
2303  *
2304  * PCI layer interface
2305  *
2306  ***************************************************************************
2307  */
2308 
2309 /**
2310  * Probe PCI device
2311  *
2312  * @v pci		PCI device
2313  * @v id		PCI ID
2314  * @ret rc		Return status code
2315  */
linda_probe(struct pci_device * pci)2316 static int linda_probe ( struct pci_device *pci ) {
2317 	struct ib_device *ibdev;
2318 	struct linda *linda;
2319 	struct QIB_7220_Revision revision;
2320 	int rc;
2321 
2322 	/* Allocate Infiniband device */
2323 	ibdev = alloc_ibdev ( sizeof ( *linda ) );
2324 	if ( ! ibdev ) {
2325 		rc = -ENOMEM;
2326 		goto err_alloc_ibdev;
2327 	}
2328 	pci_set_drvdata ( pci, ibdev );
2329 	linda = ib_get_drvdata ( ibdev );
2330 	ibdev->op = &linda_ib_operations;
2331 	ibdev->dev = &pci->dev;
2332 	ibdev->port = 1;
2333 
2334 	/* Fix up PCI device */
2335 	adjust_pci_device ( pci );
2336 
2337 	/* Map PCI BARs */
2338 	linda->regs = ioremap ( pci->membase, LINDA_BAR0_SIZE );
2339 	DBGC2 ( linda, "Linda %p has BAR at %08lx\n", linda, pci->membase );
2340 
2341 	/* Print some general data */
2342 	linda_readq ( linda, &revision, QIB_7220_Revision_offset );
2343 	DBGC2 ( linda, "Linda %p board %02lx v%ld.%ld.%ld.%ld\n", linda,
2344 		BIT_GET ( &revision, BoardID ),
2345 		BIT_GET ( &revision, R_SW ),
2346 		BIT_GET ( &revision, R_Arch ),
2347 		BIT_GET ( &revision, R_ChipRevMajor ),
2348 		BIT_GET ( &revision, R_ChipRevMinor ) );
2349 
2350 	/* Record link capabilities.  Note that we force SDR only to
2351 	 * avoid having to carry extra code for DDR tuning etc.
2352 	 */
2353 	ibdev->link_width_enabled = ibdev->link_width_supported =
2354 		( IB_LINK_WIDTH_4X | IB_LINK_WIDTH_1X );
2355 	ibdev->link_speed_enabled = ibdev->link_speed_supported =
2356 		IB_LINK_SPEED_SDR;
2357 
2358 	/* Initialise I2C subsystem */
2359 	if ( ( rc = linda_init_i2c ( linda ) ) != 0 )
2360 		goto err_init_i2c;
2361 
2362 	/* Read EEPROM parameters */
2363 	if ( ( rc = linda_read_eeprom ( linda, &ibdev->node_guid ) ) != 0 )
2364 		goto err_read_eeprom;
2365 	memcpy ( &ibdev->gid.s.guid, &ibdev->node_guid,
2366 		 sizeof ( ibdev->gid.s.guid ) );
2367 
2368 	/* Initialise send datapath */
2369 	if ( ( rc = linda_init_send ( linda ) ) != 0 )
2370 		goto err_init_send;
2371 
2372 	/* Initialise receive datapath */
2373 	if ( ( rc = linda_init_recv ( linda ) ) != 0 )
2374 		goto err_init_recv;
2375 
2376 	/* Initialise the IB SerDes */
2377 	if ( ( rc = linda_init_ib_serdes ( linda ) ) != 0 )
2378 		goto err_init_ib_serdes;
2379 
2380 	/* Register Infiniband device */
2381 	if ( ( rc = register_ibdev ( ibdev ) ) != 0 ) {
2382 		DBGC ( linda, "Linda %p could not register IB "
2383 		       "device: %s\n", linda, strerror ( rc ) );
2384 		goto err_register_ibdev;
2385 	}
2386 
2387 	return 0;
2388 
2389 	unregister_ibdev ( ibdev );
2390  err_register_ibdev:
2391 	linda_fini_recv ( linda );
2392  err_init_recv:
2393 	linda_fini_send ( linda );
2394  err_init_send:
2395  err_init_ib_serdes:
2396  err_read_eeprom:
2397  err_init_i2c:
2398 	iounmap ( linda->regs );
2399 	ibdev_put ( ibdev );
2400  err_alloc_ibdev:
2401 	return rc;
2402 }
2403 
2404 /**
2405  * Remove PCI device
2406  *
2407  * @v pci		PCI device
2408  */
linda_remove(struct pci_device * pci)2409 static void linda_remove ( struct pci_device *pci ) {
2410 	struct ib_device *ibdev = pci_get_drvdata ( pci );
2411 	struct linda *linda = ib_get_drvdata ( ibdev );
2412 
2413 	unregister_ibdev ( ibdev );
2414 	linda_fini_recv ( linda );
2415 	linda_fini_send ( linda );
2416 	iounmap ( linda->regs );
2417 	ibdev_put ( ibdev );
2418 }
2419 
2420 static struct pci_device_id linda_nics[] = {
2421 	PCI_ROM ( 0x1077, 0x7220, "iba7220", "QLE7240/7280 HCA driver", 0 ),
2422 };
2423 
2424 struct pci_driver linda_driver __pci_driver = {
2425 	.ids = linda_nics,
2426 	.id_count = ( sizeof ( linda_nics ) / sizeof ( linda_nics[0] ) ),
2427 	.probe = linda_probe,
2428 	.remove = linda_remove,
2429 };
2430