1 /**************************************************************************
3 Copyright (c) 2007, Chelsio Inc.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
9 1. Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
12 2. Neither the name of the Chelsio Corporation nor the names of its
13 contributors may be used to endorse or promote products derived from
14 this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
28 ***************************************************************************/
30 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: cxgb_mc5.c,v 1.2 2007/12/11 11:25:47 lukem Exp $");
35 __FBSDID("$FreeBSD: src/sys/dev/cxgb/common/cxgb_mc5.c,v 1.5 2007/07/17 06:50:34 kmacy Exp $");
39 #include <common/cxgb_common.h>
40 #include <common/cxgb_regs.h>
43 #include <dev/cxgb/common/cxgb_common.h>
44 #include <dev/cxgb/common/cxgb_regs.h>
47 #include <dev/pci/cxgb_common.h>
48 #include <dev/pci/cxgb_regs.h>
57 /* DBGI command mode */
60 DBGI_MODE_IDT52100
= 5
63 /* IDT 75P52100 commands */
64 #define IDT_CMD_READ 0
65 #define IDT_CMD_WRITE 1
66 #define IDT_CMD_SEARCH 2
67 #define IDT_CMD_LEARN 3
69 /* IDT LAR register address and value for 144-bit mode (low 32 bits) */
70 #define IDT_LAR_ADR0 0x180006
71 #define IDT_LAR_MODE144 0xffff0000
73 /* IDT SCR and SSR addresses (low 32 bits) */
74 #define IDT_SCR_ADR0 0x180000
75 #define IDT_SSR0_ADR0 0x180002
76 #define IDT_SSR1_ADR0 0x180004
78 /* IDT GMR base address (low 32 bits) */
79 #define IDT_GMR_BASE_ADR0 0x180020
81 /* IDT data and mask array base addresses (low 32 bits) */
82 #define IDT_DATARY_BASE_ADR0 0
83 #define IDT_MSKARY_BASE_ADR0 0x80000
85 /* IDT 75N43102 commands */
86 #define IDT4_CMD_SEARCH144 3
87 #define IDT4_CMD_WRITE 4
88 #define IDT4_CMD_READ 5
90 /* IDT 75N43102 SCR address (low 32 bits) */
91 #define IDT4_SCR_ADR0 0x3
93 /* IDT 75N43102 GMR base addresses (low 32 bits) */
94 #define IDT4_GMR_BASE0 0x10
95 #define IDT4_GMR_BASE1 0x20
96 #define IDT4_GMR_BASE2 0x30
98 /* IDT 75N43102 data and mask array base addresses (low 32 bits) */
99 #define IDT4_DATARY_BASE_ADR0 0x1000000
100 #define IDT4_MSKARY_BASE_ADR0 0x2000000
102 #define MAX_WRITE_ATTEMPTS 5
104 #define MAX_ROUTES 2048
107 * Issue a command to the TCAM and wait for its completion. The address and
108 * any data required by the command must have been setup by the caller.
110 static int mc5_cmd_write(adapter_t
*adapter
, u32 cmd
)
112 t3_write_reg(adapter
, A_MC5_DB_DBGI_REQ_CMD
, cmd
);
113 return t3_wait_op_done(adapter
, A_MC5_DB_DBGI_RSP_STATUS
,
114 F_DBGIRSPVALID
, 1, MAX_WRITE_ATTEMPTS
, 1);
117 static inline void dbgi_wr_addr3(adapter_t
*adapter
, u32 v1
, u32 v2
, u32 v3
)
119 t3_write_reg(adapter
, A_MC5_DB_DBGI_REQ_ADDR0
, v1
);
120 t3_write_reg(adapter
, A_MC5_DB_DBGI_REQ_ADDR1
, v2
);
121 t3_write_reg(adapter
, A_MC5_DB_DBGI_REQ_ADDR2
, v3
);
124 static inline void dbgi_wr_data3(adapter_t
*adapter
, u32 v1
, u32 v2
, u32 v3
)
126 t3_write_reg(adapter
, A_MC5_DB_DBGI_REQ_DATA0
, v1
);
127 t3_write_reg(adapter
, A_MC5_DB_DBGI_REQ_DATA1
, v2
);
128 t3_write_reg(adapter
, A_MC5_DB_DBGI_REQ_DATA2
, v3
);
131 static inline void dbgi_rd_rsp3(adapter_t
*adapter
, u32
*v1
, u32
*v2
, u32
*v3
)
133 *v1
= t3_read_reg(adapter
, A_MC5_DB_DBGI_RSP_DATA0
);
134 *v2
= t3_read_reg(adapter
, A_MC5_DB_DBGI_RSP_DATA1
);
135 *v3
= t3_read_reg(adapter
, A_MC5_DB_DBGI_RSP_DATA2
);
139 * Write data to the TCAM register at address (0, 0, addr_lo) using the TCAM
140 * command cmd. The data to be written must have been set up by the caller.
141 * Returns -1 on failure, 0 on success.
143 static int mc5_write(adapter_t
*adapter
, u32 addr_lo
, u32 cmd
)
145 t3_write_reg(adapter
, A_MC5_DB_DBGI_REQ_ADDR0
, addr_lo
);
146 if (mc5_cmd_write(adapter
, cmd
) == 0)
148 CH_ERR(adapter
, "MC5 timeout writing to TCAM address 0x%x\n", addr_lo
);
152 static int init_mask_data_array(struct mc5
*mc5
, u32 mask_array_base
,
153 u32 data_array_base
, u32 write_cmd
,
157 adapter_t
*adap
= mc5
->adapter
;
160 * We need the size of the TCAM data and mask arrays in terms of
163 unsigned int size72
= mc5
->tcam_size
;
164 unsigned int server_base
= t3_read_reg(adap
, A_MC5_DB_SERVER_INDEX
);
166 if (mc5
->mode
== MC5_MODE_144_BIT
) {
167 size72
*= 2; /* 1 144-bit entry is 2 72-bit entries */
171 /* Clear the data array */
172 dbgi_wr_data3(adap
, 0, 0, 0);
173 for (i
= 0; i
< size72
; i
++)
174 if (mc5_write(adap
, data_array_base
+ (i
<< addr_shift
),
178 /* Initialize the mask array. */
179 for (i
= 0; i
< server_base
; i
++) {
180 dbgi_wr_data3(adap
, 0x3fffffff, 0xfff80000, 0xff);
181 if (mc5_write(adap
, mask_array_base
+ (i
<< addr_shift
),
185 dbgi_wr_data3(adap
, 0xffffffff, 0xffffffff, 0xff);
186 if (mc5_write(adap
, mask_array_base
+ (i
<< addr_shift
),
192 mc5
->mode
== MC5_MODE_144_BIT
? 0xfffffff9 : 0xfffffffd,
194 for (; i
< size72
; i
++)
195 if (mc5_write(adap
, mask_array_base
+ (i
<< addr_shift
),
202 static int init_idt52100(struct mc5
*mc5
)
205 adapter_t
*adap
= mc5
->adapter
;
207 t3_write_reg(adap
, A_MC5_DB_RSP_LATENCY
,
208 V_RDLAT(0x15) | V_LRNLAT(0x15) | V_SRCHLAT(0x15));
209 t3_write_reg(adap
, A_MC5_DB_PART_ID_INDEX
, 2);
212 * Use GMRs 14-15 for ELOOKUP, GMRs 12-13 for SYN lookups, and
213 * GMRs 8-9 for ACK- and AOPEN searches.
215 t3_write_reg(adap
, A_MC5_DB_POPEN_DATA_WR_CMD
, IDT_CMD_WRITE
);
216 t3_write_reg(adap
, A_MC5_DB_POPEN_MASK_WR_CMD
, IDT_CMD_WRITE
);
217 t3_write_reg(adap
, A_MC5_DB_AOPEN_SRCH_CMD
, IDT_CMD_SEARCH
);
218 t3_write_reg(adap
, A_MC5_DB_AOPEN_LRN_CMD
, IDT_CMD_LEARN
);
219 t3_write_reg(adap
, A_MC5_DB_SYN_SRCH_CMD
, IDT_CMD_SEARCH
| 0x6000);
220 t3_write_reg(adap
, A_MC5_DB_SYN_LRN_CMD
, IDT_CMD_LEARN
);
221 t3_write_reg(adap
, A_MC5_DB_ACK_SRCH_CMD
, IDT_CMD_SEARCH
);
222 t3_write_reg(adap
, A_MC5_DB_ACK_LRN_CMD
, IDT_CMD_LEARN
);
223 t3_write_reg(adap
, A_MC5_DB_ILOOKUP_CMD
, IDT_CMD_SEARCH
);
224 t3_write_reg(adap
, A_MC5_DB_ELOOKUP_CMD
, IDT_CMD_SEARCH
| 0x7000);
225 t3_write_reg(adap
, A_MC5_DB_DATA_WRITE_CMD
, IDT_CMD_WRITE
);
226 t3_write_reg(adap
, A_MC5_DB_DATA_READ_CMD
, IDT_CMD_READ
);
228 /* Set DBGI command mode for IDT TCAM. */
229 t3_write_reg(adap
, A_MC5_DB_DBGI_CONFIG
, DBGI_MODE_IDT52100
);
232 dbgi_wr_data3(adap
, IDT_LAR_MODE144
, 0, 0);
233 if (mc5_write(adap
, IDT_LAR_ADR0
, IDT_CMD_WRITE
))
237 dbgi_wr_data3(adap
, 0xffffffff, 0xffffffff, 0);
238 if (mc5_write(adap
, IDT_SSR0_ADR0
, IDT_CMD_WRITE
) ||
239 mc5_write(adap
, IDT_SSR1_ADR0
, IDT_CMD_WRITE
))
243 for (i
= 0; i
< 32; ++i
) {
244 if (i
>= 12 && i
< 15)
245 dbgi_wr_data3(adap
, 0xfffffff9, 0xffffffff, 0xff);
247 dbgi_wr_data3(adap
, 0xfffffff9, 0xffff8007, 0xff);
249 dbgi_wr_data3(adap
, 0xffffffff, 0xffffffff, 0xff);
251 if (mc5_write(adap
, IDT_GMR_BASE_ADR0
+ i
, IDT_CMD_WRITE
))
256 dbgi_wr_data3(adap
, 1, 0, 0);
257 if (mc5_write(adap
, IDT_SCR_ADR0
, IDT_CMD_WRITE
))
260 return init_mask_data_array(mc5
, IDT_MSKARY_BASE_ADR0
,
261 IDT_DATARY_BASE_ADR0
, IDT_CMD_WRITE
, 0);
266 static int init_idt43102(struct mc5
*mc5
)
269 adapter_t
*adap
= mc5
->adapter
;
271 t3_write_reg(adap
, A_MC5_DB_RSP_LATENCY
,
272 adap
->params
.rev
== 0 ? V_RDLAT(0xd) | V_SRCHLAT(0x11) :
273 V_RDLAT(0xd) | V_SRCHLAT(0x12));
276 * Use GMRs 24-25 for ELOOKUP, GMRs 20-21 for SYN lookups, and no mask
277 * for ACK- and AOPEN searches.
279 t3_write_reg(adap
, A_MC5_DB_POPEN_DATA_WR_CMD
, IDT4_CMD_WRITE
);
280 t3_write_reg(adap
, A_MC5_DB_POPEN_MASK_WR_CMD
, IDT4_CMD_WRITE
);
281 t3_write_reg(adap
, A_MC5_DB_AOPEN_SRCH_CMD
,
282 IDT4_CMD_SEARCH144
| 0x3800);
283 t3_write_reg(adap
, A_MC5_DB_SYN_SRCH_CMD
, IDT4_CMD_SEARCH144
);
284 t3_write_reg(adap
, A_MC5_DB_ACK_SRCH_CMD
, IDT4_CMD_SEARCH144
| 0x3800);
285 t3_write_reg(adap
, A_MC5_DB_ILOOKUP_CMD
, IDT4_CMD_SEARCH144
| 0x3800);
286 t3_write_reg(adap
, A_MC5_DB_ELOOKUP_CMD
, IDT4_CMD_SEARCH144
| 0x800);
287 t3_write_reg(adap
, A_MC5_DB_DATA_WRITE_CMD
, IDT4_CMD_WRITE
);
288 t3_write_reg(adap
, A_MC5_DB_DATA_READ_CMD
, IDT4_CMD_READ
);
290 t3_write_reg(adap
, A_MC5_DB_PART_ID_INDEX
, 3);
292 /* Set DBGI command mode for IDT TCAM. */
293 t3_write_reg(adap
, A_MC5_DB_DBGI_CONFIG
, DBGI_MODE_IDT52100
);
296 dbgi_wr_data3(adap
, 0xffffffff, 0xffffffff, 0xff);
297 for (i
= 0; i
< 7; ++i
)
298 if (mc5_write(adap
, IDT4_GMR_BASE0
+ i
, IDT4_CMD_WRITE
))
301 for (i
= 0; i
< 4; ++i
)
302 if (mc5_write(adap
, IDT4_GMR_BASE2
+ i
, IDT4_CMD_WRITE
))
305 dbgi_wr_data3(adap
, 0xfffffff9, 0xffffffff, 0xff);
306 if (mc5_write(adap
, IDT4_GMR_BASE1
, IDT4_CMD_WRITE
) ||
307 mc5_write(adap
, IDT4_GMR_BASE1
+ 1, IDT4_CMD_WRITE
) ||
308 mc5_write(adap
, IDT4_GMR_BASE1
+ 4, IDT4_CMD_WRITE
))
311 dbgi_wr_data3(adap
, 0xfffffff9, 0xffff8007, 0xff);
312 if (mc5_write(adap
, IDT4_GMR_BASE1
+ 5, IDT4_CMD_WRITE
))
316 dbgi_wr_data3(adap
, 0xf0000000, 0, 0);
317 if (mc5_write(adap
, IDT4_SCR_ADR0
, IDT4_CMD_WRITE
))
320 return init_mask_data_array(mc5
, IDT4_MSKARY_BASE_ADR0
,
321 IDT4_DATARY_BASE_ADR0
, IDT4_CMD_WRITE
, 1);
326 /* Put MC5 in DBGI mode. */
327 static inline void mc5_dbgi_mode_enable(const struct mc5
*mc5
)
329 t3_set_reg_field(mc5
->adapter
, A_MC5_DB_CONFIG
, F_PRTYEN
| F_MBUSEN
,
333 /* Put MC5 in M-Bus mode. */
334 static void mc5_dbgi_mode_disable(const struct mc5
*mc5
)
336 t3_set_reg_field(mc5
->adapter
, A_MC5_DB_CONFIG
, F_DBGIEN
,
337 V_PRTYEN(mc5
->parity_enabled
) | F_MBUSEN
);
341 * Initialization that requires the OS and protocol layers to already
342 * be intialized goes here.
344 int t3_mc5_init(struct mc5
*mc5
, unsigned int nservers
, unsigned int nfilters
,
345 unsigned int nroutes
)
348 unsigned int tcam_size
= mc5
->tcam_size
;
349 unsigned int mode72
= mc5
->mode
== MC5_MODE_72_BIT
;
350 adapter_t
*adap
= mc5
->adapter
;
355 if (nroutes
> MAX_ROUTES
|| nroutes
+ nservers
+ nfilters
> tcam_size
)
358 if (nfilters
&& adap
->params
.rev
< T3_REV_C
)
359 mc5
->parity_enabled
= 0;
362 t3_set_reg_field(adap
, A_MC5_DB_CONFIG
, F_TMMODE
| F_COMPEN
,
363 V_COMPEN(mode72
) | V_TMMODE(mode72
) | F_TMRST
);
364 if (t3_wait_op_done(adap
, A_MC5_DB_CONFIG
, F_TMRDY
, 1, 500, 0)) {
365 CH_ERR(adap
, "TCAM reset timed out\n");
369 t3_write_reg(adap
, A_MC5_DB_ROUTING_TABLE_INDEX
, tcam_size
- nroutes
);
370 t3_write_reg(adap
, A_MC5_DB_FILTER_TABLE
,
371 tcam_size
- nroutes
- nfilters
);
372 t3_write_reg(adap
, A_MC5_DB_SERVER_INDEX
,
373 tcam_size
- nroutes
- nfilters
- nservers
);
375 /* All the TCAM addresses we access have only the low 32 bits non 0 */
376 t3_write_reg(adap
, A_MC5_DB_DBGI_REQ_ADDR1
, 0);
377 t3_write_reg(adap
, A_MC5_DB_DBGI_REQ_ADDR2
, 0);
379 mc5_dbgi_mode_enable(mc5
);
381 switch (mc5
->part_type
) {
383 err
= init_idt52100(mc5
);
386 err
= init_idt43102(mc5
);
389 CH_ERR(adap
, "Unsupported TCAM type %d\n", mc5
->part_type
);
394 mc5_dbgi_mode_disable(mc5
);
399 * read_mc5_range - dump a part of the memory managed by MC5
400 * @mc5: the MC5 handle
401 * @start: the start address for the dump
402 * @n: number of 72-bit words to read
403 * @buf: result buffer
405 * Read n 72-bit words from MC5 memory from the given start location.
407 int t3_read_mc5_range(const struct mc5
*mc5
, unsigned int start
,
408 unsigned int n
, u32
*buf
)
412 adapter_t
*adap
= mc5
->adapter
;
414 if (mc5
->part_type
== IDT75P52100
)
415 read_cmd
= IDT_CMD_READ
;
416 else if (mc5
->part_type
== IDT75N43102
)
417 read_cmd
= IDT4_CMD_READ
;
421 mc5_dbgi_mode_enable(mc5
);
424 t3_write_reg(adap
, A_MC5_DB_DBGI_REQ_ADDR0
, start
++);
425 if (mc5_cmd_write(adap
, read_cmd
)) {
429 dbgi_rd_rsp3(adap
, buf
+ 2, buf
+ 1, buf
);
433 mc5_dbgi_mode_disable(mc5
);
437 #define MC5_INT_FATAL (F_PARITYERR | F_REQQPARERR | F_DISPQPARERR)
440 * MC5 interrupt handler
442 void t3_mc5_intr_handler(struct mc5
*mc5
)
444 adapter_t
*adap
= mc5
->adapter
;
445 u32 cause
= t3_read_reg(adap
, A_MC5_DB_INT_CAUSE
);
447 if ((cause
& F_PARITYERR
) && mc5
->parity_enabled
) {
448 CH_ALERT(adap
, "MC5 parity error\n");
449 mc5
->stats
.parity_err
++;
452 if (cause
& F_REQQPARERR
) {
453 CH_ALERT(adap
, "MC5 request queue parity error\n");
454 mc5
->stats
.reqq_parity_err
++;
457 if (cause
& F_DISPQPARERR
) {
458 CH_ALERT(adap
, "MC5 dispatch queue parity error\n");
459 mc5
->stats
.dispq_parity_err
++;
462 if (cause
& F_ACTRGNFULL
)
463 mc5
->stats
.active_rgn_full
++;
464 if (cause
& F_NFASRCHFAIL
)
465 mc5
->stats
.nfa_srch_err
++;
466 if (cause
& F_UNKNOWNCMD
)
467 mc5
->stats
.unknown_cmd
++;
468 if (cause
& F_DELACTEMPTY
)
469 mc5
->stats
.del_act_empty
++;
470 if (cause
& MC5_INT_FATAL
)
473 t3_write_reg(adap
, A_MC5_DB_INT_CAUSE
, cause
);
476 void __devinit
t3_mc5_prep(adapter_t
*adapter
, struct mc5
*mc5
, int mode
)
480 static unsigned int tcam_part_size
[] = { /* in K 72-bit entries */
481 64 K
, 128 K
, 256 K
, 32 K
486 u32 cfg
= t3_read_reg(adapter
, A_MC5_DB_CONFIG
);
488 mc5
->adapter
= adapter
;
489 mc5
->parity_enabled
= 1;
490 mc5
->mode
= (unsigned char) mode
;
491 mc5
->part_type
= (unsigned char) G_TMTYPE(cfg
);
492 if (cfg
& F_TMTYPEHI
)
495 mc5
->tcam_size
= tcam_part_size
[G_TMPARTSIZE(cfg
)];
496 if (mode
== MC5_MODE_144_BIT
)