GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / net / ethernet / broadcom / bnx2x / bnx2x_init_ops.h
1 /* bnx2x_init_ops.h: Qlogic Everest network driver.
2  *               Static functions needed during the initialization.
3  *               This file is "included" in bnx2x_main.c.
4  *
5  * Copyright (c) 2007-2013 Broadcom Corporation
6  * Copyright (c) 2014 QLogic Corporation
7  All rights reserved
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation.
12  *
13  * Maintained by: Ariel Elior <ariel.elior@qlogic.com>
14  * Written by: Vladislav Zolotarov
15  */
16
17 #ifndef BNX2X_INIT_OPS_H
18 #define BNX2X_INIT_OPS_H
19
20
21 #ifndef BP_ILT
22 #define BP_ILT(bp)      NULL
23 #endif
24
25 #ifndef BP_FUNC
26 #define BP_FUNC(bp)     0
27 #endif
28
29 #ifndef BP_PORT
30 #define BP_PORT(bp)     0
31 #endif
32
33 #ifndef BNX2X_ILT_FREE
34 #define BNX2X_ILT_FREE(x, y, sz)
35 #endif
36
37 #ifndef BNX2X_ILT_ZALLOC
38 #define BNX2X_ILT_ZALLOC(x, y, sz)
39 #endif
40
41 #ifndef ILOG2
42 #define ILOG2(x)        x
43 #endif
44
45 static int bnx2x_gunzip(struct bnx2x *bp, const u8 *zbuf, int len);
46 static void bnx2x_reg_wr_ind(struct bnx2x *bp, u32 addr, u32 val);
47 static void bnx2x_write_dmae_phys_len(struct bnx2x *bp,
48                                       dma_addr_t phys_addr, u32 addr,
49                                       u32 len);
50
51 static void bnx2x_init_str_wr(struct bnx2x *bp, u32 addr,
52                               const u32 *data, u32 len)
53 {
54         u32 i;
55
56         for (i = 0; i < len; i++)
57                 REG_WR(bp, addr + i*4, data[i]);
58 }
59
60 static void bnx2x_init_ind_wr(struct bnx2x *bp, u32 addr,
61                               const u32 *data, u32 len)
62 {
63         u32 i;
64
65         for (i = 0; i < len; i++)
66                 bnx2x_reg_wr_ind(bp, addr + i*4, data[i]);
67 }
68
69 static void bnx2x_write_big_buf(struct bnx2x *bp, u32 addr, u32 len,
70                                 u8 wb)
71 {
72         if (bp->dmae_ready)
73                 bnx2x_write_dmae_phys_len(bp, GUNZIP_PHYS(bp), addr, len);
74
75         /* in E1 chips BIOS initiated ZLR may interrupt widebus writes */
76         else if (wb && CHIP_IS_E1(bp))
77                 bnx2x_init_ind_wr(bp, addr, GUNZIP_BUF(bp), len);
78
79         /* in later chips PXP root complex handles BIOS ZLR w/o interrupting */
80         else
81                 bnx2x_init_str_wr(bp, addr, GUNZIP_BUF(bp), len);
82 }
83
84 static void bnx2x_init_fill(struct bnx2x *bp, u32 addr, int fill,
85                             u32 len, u8 wb)
86 {
87         u32 buf_len = (((len*4) > FW_BUF_SIZE) ? FW_BUF_SIZE : (len*4));
88         u32 buf_len32 = buf_len/4;
89         u32 i;
90
91         memset(GUNZIP_BUF(bp), (u8)fill, buf_len);
92
93         for (i = 0; i < len; i += buf_len32) {
94                 u32 cur_len = min(buf_len32, len - i);
95
96                 bnx2x_write_big_buf(bp, addr + i*4, cur_len, wb);
97         }
98 }
99
100 static void bnx2x_write_big_buf_wb(struct bnx2x *bp, u32 addr, u32 len)
101 {
102         if (bp->dmae_ready)
103                 bnx2x_write_dmae_phys_len(bp, GUNZIP_PHYS(bp), addr, len);
104
105         /* in E1 chips BIOS initiated ZLR may interrupt widebus writes */
106         else if (CHIP_IS_E1(bp))
107                 bnx2x_init_ind_wr(bp, addr, GUNZIP_BUF(bp), len);
108
109         /* in later chips PXP root complex handles BIOS ZLR w/o interrupting */
110         else
111                 bnx2x_init_str_wr(bp, addr, GUNZIP_BUF(bp), len);
112 }
113
114 static void bnx2x_init_wr_64(struct bnx2x *bp, u32 addr,
115                              const u32 *data, u32 len64)
116 {
117         u32 buf_len32 = FW_BUF_SIZE/4;
118         u32 len = len64*2;
119         u64 data64 = 0;
120         u32 i;
121
122         /* 64 bit value is in a blob: first low DWORD, then high DWORD */
123         data64 = HILO_U64((*(data + 1)), (*data));
124
125         len64 = min((u32)(FW_BUF_SIZE/8), len64);
126         for (i = 0; i < len64; i++) {
127                 u64 *pdata = ((u64 *)(GUNZIP_BUF(bp))) + i;
128
129                 *pdata = data64;
130         }
131
132         for (i = 0; i < len; i += buf_len32) {
133                 u32 cur_len = min(buf_len32, len - i);
134
135                 bnx2x_write_big_buf_wb(bp, addr + i*4, cur_len);
136         }
137 }
138
139 /*********************************************************
140    There are different blobs for each PRAM section.
141    In addition, each blob write operation is divided into a few operations
142    in order to decrease the amount of phys. contiguous buffer needed.
143    Thus, when we select a blob the address may be with some offset
144    from the beginning of PRAM section.
145    The same holds for the INT_TABLE sections.
146 **********************************************************/
147 #define IF_IS_INT_TABLE_ADDR(base, addr) \
148                         if (((base) <= (addr)) && ((base) + 0x400 >= (addr)))
149
150 #define IF_IS_PRAM_ADDR(base, addr) \
151                         if (((base) <= (addr)) && ((base) + 0x40000 >= (addr)))
152
153 static const u8 *bnx2x_sel_blob(struct bnx2x *bp, u32 addr,
154                                 const u8 *data)
155 {
156         IF_IS_INT_TABLE_ADDR(TSEM_REG_INT_TABLE, addr)
157                 data = INIT_TSEM_INT_TABLE_DATA(bp);
158         else
159                 IF_IS_INT_TABLE_ADDR(CSEM_REG_INT_TABLE, addr)
160                         data = INIT_CSEM_INT_TABLE_DATA(bp);
161         else
162                 IF_IS_INT_TABLE_ADDR(USEM_REG_INT_TABLE, addr)
163                         data = INIT_USEM_INT_TABLE_DATA(bp);
164         else
165                 IF_IS_INT_TABLE_ADDR(XSEM_REG_INT_TABLE, addr)
166                         data = INIT_XSEM_INT_TABLE_DATA(bp);
167         else
168                 IF_IS_PRAM_ADDR(TSEM_REG_PRAM, addr)
169                         data = INIT_TSEM_PRAM_DATA(bp);
170         else
171                 IF_IS_PRAM_ADDR(CSEM_REG_PRAM, addr)
172                         data = INIT_CSEM_PRAM_DATA(bp);
173         else
174                 IF_IS_PRAM_ADDR(USEM_REG_PRAM, addr)
175                         data = INIT_USEM_PRAM_DATA(bp);
176         else
177                 IF_IS_PRAM_ADDR(XSEM_REG_PRAM, addr)
178                         data = INIT_XSEM_PRAM_DATA(bp);
179
180         return data;
181 }
182
183 extern void bnx2x_init_wr_wb(struct bnx2x *, u32, const u32 *, u32);
184 /*(DEBLOBBED)*/
185
186 static void bnx2x_wr_64(struct bnx2x *bp, u32 reg, u32 val_lo,
187                         u32 val_hi)
188 {
189         u32 wb_write[2];
190
191         wb_write[0] = val_lo;
192         wb_write[1] = val_hi;
193         REG_WR_DMAE_LEN(bp, reg, wb_write, 2);
194 }
195 static void bnx2x_init_wr_zp(struct bnx2x *bp, u32 addr, u32 len,
196                              u32 blob_off)
197 {
198         const u8 *data = NULL;
199         int rc;
200         u32 i;
201
202         data = bnx2x_sel_blob(bp, addr, data) + blob_off*4;
203
204         rc = bnx2x_gunzip(bp, data, len);
205         if (rc)
206                 return;
207
208         /* gunzip_outlen is in dwords */
209         len = GUNZIP_OUTLEN(bp);
210         for (i = 0; i < len; i++)
211                 ((u32 *)GUNZIP_BUF(bp))[i] = (__force u32)
212                                 cpu_to_le32(((u32 *)GUNZIP_BUF(bp))[i]);
213
214         bnx2x_write_big_buf_wb(bp, addr, len);
215 }
216
217 /*(DEBLOBBED)*/
218
219
220 /****************************************************************************
221 * PXP Arbiter
222 ****************************************************************************/
223 /*
224  * This code configures the PCI read/write arbiter
225  * which implements a weighted round robin
226  * between the virtual queues in the chip.
227  *
228  * The values were derived for each PCI max payload and max request size.
229  * since max payload and max request size are only known at run time,
230  * this is done as a separate init stage.
231  */
232
233 #define NUM_WR_Q                        13
234 #define NUM_RD_Q                        29
235 #define MAX_RD_ORD                      3
236 #define MAX_WR_ORD                      2
237
238 /* configuration for one arbiter queue */
239 struct arb_line {
240         int l;
241         int add;
242         int ubound;
243 };
244
245 /* derived configuration for each read queue for each max request size */
246 static const struct arb_line read_arb_data[NUM_RD_Q][MAX_RD_ORD + 1] = {
247 /* 1 */ { {8, 64, 25}, {16, 64, 25}, {32, 64, 25}, {64, 64, 41} },
248         { {4, 8,  4},  {4,  8,  4},  {4,  8,  4},  {4,  8,  4}  },
249         { {4, 3,  3},  {4,  3,  3},  {4,  3,  3},  {4,  3,  3}  },
250         { {8, 3,  6},  {16, 3,  11}, {16, 3,  11}, {16, 3,  11} },
251         { {8, 64, 25}, {16, 64, 25}, {32, 64, 25}, {64, 64, 41} },
252         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {64, 3,  41} },
253         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {64, 3,  41} },
254         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {64, 3,  41} },
255         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {64, 3,  41} },
256 /* 10 */{ {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
257         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
258         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
259         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
260         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
261         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
262         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
263         { {8, 64, 6},  {16, 64, 11}, {32, 64, 21}, {32, 64, 21} },
264         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
265         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
266 /* 20 */{ {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
267         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
268         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
269         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
270         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
271         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
272         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
273         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
274         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
275         { {8, 64, 25}, {16, 64, 41}, {32, 64, 81}, {64, 64, 120} }
276 };
277
278 /* derived configuration for each write queue for each max request size */
279 static const struct arb_line write_arb_data[NUM_WR_Q][MAX_WR_ORD + 1] = {
280 /* 1 */ { {4, 6,  3},  {4,  6,  3},  {4,  6,  3} },
281         { {4, 2,  3},  {4,  2,  3},  {4,  2,  3} },
282         { {8, 2,  6},  {16, 2,  11}, {16, 2,  11} },
283         { {8, 2,  6},  {16, 2,  11}, {32, 2,  21} },
284         { {8, 2,  6},  {16, 2,  11}, {32, 2,  21} },
285         { {8, 2,  6},  {16, 2,  11}, {32, 2,  21} },
286         { {8, 64, 25}, {16, 64, 25}, {32, 64, 25} },
287         { {8, 2,  6},  {16, 2,  11}, {16, 2,  11} },
288         { {8, 2,  6},  {16, 2,  11}, {16, 2,  11} },
289 /* 10 */{ {8, 9,  6},  {16, 9,  11}, {32, 9,  21} },
290         { {8, 47, 19}, {16, 47, 19}, {32, 47, 21} },
291         { {8, 9,  6},  {16, 9,  11}, {16, 9,  11} },
292         { {8, 64, 25}, {16, 64, 41}, {32, 64, 81} }
293 };
294
295 /* register addresses for read queues */
296 static const struct arb_line read_arb_addr[NUM_RD_Q-1] = {
297 /* 1 */ {PXP2_REG_RQ_BW_RD_L0, PXP2_REG_RQ_BW_RD_ADD0,
298                 PXP2_REG_RQ_BW_RD_UBOUND0},
299         {PXP2_REG_PSWRQ_BW_L1, PXP2_REG_PSWRQ_BW_ADD1,
300                 PXP2_REG_PSWRQ_BW_UB1},
301         {PXP2_REG_PSWRQ_BW_L2, PXP2_REG_PSWRQ_BW_ADD2,
302                 PXP2_REG_PSWRQ_BW_UB2},
303         {PXP2_REG_PSWRQ_BW_L3, PXP2_REG_PSWRQ_BW_ADD3,
304                 PXP2_REG_PSWRQ_BW_UB3},
305         {PXP2_REG_RQ_BW_RD_L4, PXP2_REG_RQ_BW_RD_ADD4,
306                 PXP2_REG_RQ_BW_RD_UBOUND4},
307         {PXP2_REG_RQ_BW_RD_L5, PXP2_REG_RQ_BW_RD_ADD5,
308                 PXP2_REG_RQ_BW_RD_UBOUND5},
309         {PXP2_REG_PSWRQ_BW_L6, PXP2_REG_PSWRQ_BW_ADD6,
310                 PXP2_REG_PSWRQ_BW_UB6},
311         {PXP2_REG_PSWRQ_BW_L7, PXP2_REG_PSWRQ_BW_ADD7,
312                 PXP2_REG_PSWRQ_BW_UB7},
313         {PXP2_REG_PSWRQ_BW_L8, PXP2_REG_PSWRQ_BW_ADD8,
314                 PXP2_REG_PSWRQ_BW_UB8},
315 /* 10 */{PXP2_REG_PSWRQ_BW_L9, PXP2_REG_PSWRQ_BW_ADD9,
316                 PXP2_REG_PSWRQ_BW_UB9},
317         {PXP2_REG_PSWRQ_BW_L10, PXP2_REG_PSWRQ_BW_ADD10,
318                 PXP2_REG_PSWRQ_BW_UB10},
319         {PXP2_REG_PSWRQ_BW_L11, PXP2_REG_PSWRQ_BW_ADD11,
320                 PXP2_REG_PSWRQ_BW_UB11},
321         {PXP2_REG_RQ_BW_RD_L12, PXP2_REG_RQ_BW_RD_ADD12,
322                 PXP2_REG_RQ_BW_RD_UBOUND12},
323         {PXP2_REG_RQ_BW_RD_L13, PXP2_REG_RQ_BW_RD_ADD13,
324                 PXP2_REG_RQ_BW_RD_UBOUND13},
325         {PXP2_REG_RQ_BW_RD_L14, PXP2_REG_RQ_BW_RD_ADD14,
326                 PXP2_REG_RQ_BW_RD_UBOUND14},
327         {PXP2_REG_RQ_BW_RD_L15, PXP2_REG_RQ_BW_RD_ADD15,
328                 PXP2_REG_RQ_BW_RD_UBOUND15},
329         {PXP2_REG_RQ_BW_RD_L16, PXP2_REG_RQ_BW_RD_ADD16,
330                 PXP2_REG_RQ_BW_RD_UBOUND16},
331         {PXP2_REG_RQ_BW_RD_L17, PXP2_REG_RQ_BW_RD_ADD17,
332                 PXP2_REG_RQ_BW_RD_UBOUND17},
333         {PXP2_REG_RQ_BW_RD_L18, PXP2_REG_RQ_BW_RD_ADD18,
334                 PXP2_REG_RQ_BW_RD_UBOUND18},
335 /* 20 */{PXP2_REG_RQ_BW_RD_L19, PXP2_REG_RQ_BW_RD_ADD19,
336                 PXP2_REG_RQ_BW_RD_UBOUND19},
337         {PXP2_REG_RQ_BW_RD_L20, PXP2_REG_RQ_BW_RD_ADD20,
338                 PXP2_REG_RQ_BW_RD_UBOUND20},
339         {PXP2_REG_RQ_BW_RD_L22, PXP2_REG_RQ_BW_RD_ADD22,
340                 PXP2_REG_RQ_BW_RD_UBOUND22},
341         {PXP2_REG_RQ_BW_RD_L23, PXP2_REG_RQ_BW_RD_ADD23,
342                 PXP2_REG_RQ_BW_RD_UBOUND23},
343         {PXP2_REG_RQ_BW_RD_L24, PXP2_REG_RQ_BW_RD_ADD24,
344                 PXP2_REG_RQ_BW_RD_UBOUND24},
345         {PXP2_REG_RQ_BW_RD_L25, PXP2_REG_RQ_BW_RD_ADD25,
346                 PXP2_REG_RQ_BW_RD_UBOUND25},
347         {PXP2_REG_RQ_BW_RD_L26, PXP2_REG_RQ_BW_RD_ADD26,
348                 PXP2_REG_RQ_BW_RD_UBOUND26},
349         {PXP2_REG_RQ_BW_RD_L27, PXP2_REG_RQ_BW_RD_ADD27,
350                 PXP2_REG_RQ_BW_RD_UBOUND27},
351         {PXP2_REG_PSWRQ_BW_L28, PXP2_REG_PSWRQ_BW_ADD28,
352                 PXP2_REG_PSWRQ_BW_UB28}
353 };
354
355 /* register addresses for write queues */
356 static const struct arb_line write_arb_addr[NUM_WR_Q-1] = {
357 /* 1 */ {PXP2_REG_PSWRQ_BW_L1, PXP2_REG_PSWRQ_BW_ADD1,
358                 PXP2_REG_PSWRQ_BW_UB1},
359         {PXP2_REG_PSWRQ_BW_L2, PXP2_REG_PSWRQ_BW_ADD2,
360                 PXP2_REG_PSWRQ_BW_UB2},
361         {PXP2_REG_PSWRQ_BW_L3, PXP2_REG_PSWRQ_BW_ADD3,
362                 PXP2_REG_PSWRQ_BW_UB3},
363         {PXP2_REG_PSWRQ_BW_L6, PXP2_REG_PSWRQ_BW_ADD6,
364                 PXP2_REG_PSWRQ_BW_UB6},
365         {PXP2_REG_PSWRQ_BW_L7, PXP2_REG_PSWRQ_BW_ADD7,
366                 PXP2_REG_PSWRQ_BW_UB7},
367         {PXP2_REG_PSWRQ_BW_L8, PXP2_REG_PSWRQ_BW_ADD8,
368                 PXP2_REG_PSWRQ_BW_UB8},
369         {PXP2_REG_PSWRQ_BW_L9, PXP2_REG_PSWRQ_BW_ADD9,
370                 PXP2_REG_PSWRQ_BW_UB9},
371         {PXP2_REG_PSWRQ_BW_L10, PXP2_REG_PSWRQ_BW_ADD10,
372                 PXP2_REG_PSWRQ_BW_UB10},
373         {PXP2_REG_PSWRQ_BW_L11, PXP2_REG_PSWRQ_BW_ADD11,
374                 PXP2_REG_PSWRQ_BW_UB11},
375 /* 10 */{PXP2_REG_PSWRQ_BW_L28, PXP2_REG_PSWRQ_BW_ADD28,
376                 PXP2_REG_PSWRQ_BW_UB28},
377         {PXP2_REG_RQ_BW_WR_L29, PXP2_REG_RQ_BW_WR_ADD29,
378                 PXP2_REG_RQ_BW_WR_UBOUND29},
379         {PXP2_REG_RQ_BW_WR_L30, PXP2_REG_RQ_BW_WR_ADD30,
380                 PXP2_REG_RQ_BW_WR_UBOUND30}
381 };
382
383 static void bnx2x_init_pxp_arb(struct bnx2x *bp, int r_order,
384                                int w_order)
385 {
386         u32 val, i;
387
388         if (r_order > MAX_RD_ORD) {
389                 DP(NETIF_MSG_HW, "read order of %d  order adjusted to %d\n",
390                    r_order, MAX_RD_ORD);
391                 r_order = MAX_RD_ORD;
392         }
393         if (w_order > MAX_WR_ORD) {
394                 DP(NETIF_MSG_HW, "write order of %d  order adjusted to %d\n",
395                    w_order, MAX_WR_ORD);
396                 w_order = MAX_WR_ORD;
397         }
398         if (CHIP_REV_IS_FPGA(bp)) {
399                 DP(NETIF_MSG_HW, "write order adjusted to 1 for FPGA\n");
400                 w_order = 0;
401         }
402         DP(NETIF_MSG_HW, "read order %d  write order %d\n", r_order, w_order);
403
404         for (i = 0; i < NUM_RD_Q-1; i++) {
405                 REG_WR(bp, read_arb_addr[i].l, read_arb_data[i][r_order].l);
406                 REG_WR(bp, read_arb_addr[i].add,
407                        read_arb_data[i][r_order].add);
408                 REG_WR(bp, read_arb_addr[i].ubound,
409                        read_arb_data[i][r_order].ubound);
410         }
411
412         for (i = 0; i < NUM_WR_Q-1; i++) {
413                 if ((write_arb_addr[i].l == PXP2_REG_RQ_BW_WR_L29) ||
414                     (write_arb_addr[i].l == PXP2_REG_RQ_BW_WR_L30)) {
415
416                         REG_WR(bp, write_arb_addr[i].l,
417                                write_arb_data[i][w_order].l);
418
419                         REG_WR(bp, write_arb_addr[i].add,
420                                write_arb_data[i][w_order].add);
421
422                         REG_WR(bp, write_arb_addr[i].ubound,
423                                write_arb_data[i][w_order].ubound);
424                 } else {
425
426                         val = REG_RD(bp, write_arb_addr[i].l);
427                         REG_WR(bp, write_arb_addr[i].l,
428                                val | (write_arb_data[i][w_order].l << 10));
429
430                         val = REG_RD(bp, write_arb_addr[i].add);
431                         REG_WR(bp, write_arb_addr[i].add,
432                                val | (write_arb_data[i][w_order].add << 10));
433
434                         val = REG_RD(bp, write_arb_addr[i].ubound);
435                         REG_WR(bp, write_arb_addr[i].ubound,
436                                val | (write_arb_data[i][w_order].ubound << 7));
437                 }
438         }
439
440         val =  write_arb_data[NUM_WR_Q-1][w_order].add;
441         val += write_arb_data[NUM_WR_Q-1][w_order].ubound << 10;
442         val += write_arb_data[NUM_WR_Q-1][w_order].l << 17;
443         REG_WR(bp, PXP2_REG_PSWRQ_BW_RD, val);
444
445         val =  read_arb_data[NUM_RD_Q-1][r_order].add;
446         val += read_arb_data[NUM_RD_Q-1][r_order].ubound << 10;
447         val += read_arb_data[NUM_RD_Q-1][r_order].l << 17;
448         REG_WR(bp, PXP2_REG_PSWRQ_BW_WR, val);
449
450         REG_WR(bp, PXP2_REG_RQ_WR_MBS0, w_order);
451         REG_WR(bp, PXP2_REG_RQ_WR_MBS1, w_order);
452         REG_WR(bp, PXP2_REG_RQ_RD_MBS0, r_order);
453         REG_WR(bp, PXP2_REG_RQ_RD_MBS1, r_order);
454
455         if ((CHIP_IS_E1(bp) || CHIP_IS_E1H(bp)) && (r_order == MAX_RD_ORD))
456                 REG_WR(bp, PXP2_REG_RQ_PDR_LIMIT, 0xe00);
457
458         if (CHIP_IS_E3(bp))
459                 REG_WR(bp, PXP2_REG_WR_USDMDP_TH, (0x4 << w_order));
460         else if (CHIP_IS_E2(bp))
461                 REG_WR(bp, PXP2_REG_WR_USDMDP_TH, (0x8 << w_order));
462         else
463                 REG_WR(bp, PXP2_REG_WR_USDMDP_TH, (0x18 << w_order));
464
465         if (!CHIP_IS_E1(bp)) {
466                 /*    MPS      w_order     optimal TH      presently TH
467                  *    128         0             0               2
468                  *    256         1             1               3
469                  *    >=512       2             2               3
470                  */
471                 /* DMAE is special */
472                 if (!CHIP_IS_E1H(bp)) {
473                         /* E2 can use optimal TH */
474                         val = w_order;
475                         REG_WR(bp, PXP2_REG_WR_DMAE_MPS, val);
476                 } else {
477                         val = ((w_order == 0) ? 2 : 3);
478                         REG_WR(bp, PXP2_REG_WR_DMAE_MPS, 2);
479                 }
480
481                 REG_WR(bp, PXP2_REG_WR_HC_MPS, val);
482                 REG_WR(bp, PXP2_REG_WR_USDM_MPS, val);
483                 REG_WR(bp, PXP2_REG_WR_CSDM_MPS, val);
484                 REG_WR(bp, PXP2_REG_WR_TSDM_MPS, val);
485                 REG_WR(bp, PXP2_REG_WR_XSDM_MPS, val);
486                 REG_WR(bp, PXP2_REG_WR_QM_MPS, val);
487                 REG_WR(bp, PXP2_REG_WR_TM_MPS, val);
488                 REG_WR(bp, PXP2_REG_WR_SRC_MPS, val);
489                 REG_WR(bp, PXP2_REG_WR_DBG_MPS, val);
490                 REG_WR(bp, PXP2_REG_WR_CDU_MPS, val);
491         }
492
493         /* Validate number of tags suppoted by device */
494 #define PCIE_REG_PCIER_TL_HDR_FC_ST             0x2980
495         val = REG_RD(bp, PCIE_REG_PCIER_TL_HDR_FC_ST);
496         val &= 0xFF;
497         if (val <= 0x20)
498                 REG_WR(bp, PXP2_REG_PGL_TAGS_LIMIT, 0x20);
499 }
500
501 /****************************************************************************
502 * ILT management
503 ****************************************************************************/
504 /*
505  * This codes hides the low level HW interaction for ILT management and
506  * configuration. The API consists of a shadow ILT table which is set by the
507  * driver and a set of routines to use it to configure the HW.
508  *
509  */
510
511 /* ILT HW init operations */
512
513 /* ILT memory management operations */
514 #define ILT_MEMOP_ALLOC         0
515 #define ILT_MEMOP_FREE          1
516
517 /* the phys address is shifted right 12 bits and has an added
518  * 1=valid bit added to the 53rd bit
519  * then since this is a wide register(TM)
520  * we split it into two 32 bit writes
521  */
522 #define ILT_ADDR1(x)            ((u32)(((u64)x >> 12) & 0xFFFFFFFF))
523 #define ILT_ADDR2(x)            ((u32)((1 << 20) | ((u64)x >> 44)))
524 #define ILT_RANGE(f, l)         (((l) << 10) | f)
525
526 static int bnx2x_ilt_line_mem_op(struct bnx2x *bp,
527                                  struct ilt_line *line, u32 size, u8 memop)
528 {
529         if (memop == ILT_MEMOP_FREE) {
530                 BNX2X_ILT_FREE(line->page, line->page_mapping, line->size);
531                 return 0;
532         }
533         BNX2X_ILT_ZALLOC(line->page, &line->page_mapping, size);
534         if (!line->page)
535                 return -1;
536         line->size = size;
537         return 0;
538 }
539
540
541 static int bnx2x_ilt_client_mem_op(struct bnx2x *bp, int cli_num,
542                                    u8 memop)
543 {
544         int i, rc;
545         struct bnx2x_ilt *ilt = BP_ILT(bp);
546         struct ilt_client_info *ilt_cli;
547
548         if (!ilt || !ilt->lines)
549                 return -1;
550
551         ilt_cli = &ilt->clients[cli_num];
552
553         if (ilt_cli->flags & (ILT_CLIENT_SKIP_INIT | ILT_CLIENT_SKIP_MEM))
554                 return 0;
555
556         for (rc = 0, i = ilt_cli->start; i <= ilt_cli->end && !rc; i++) {
557                 rc = bnx2x_ilt_line_mem_op(bp, &ilt->lines[i],
558                                            ilt_cli->page_size, memop);
559         }
560         return rc;
561 }
562
563 static int bnx2x_ilt_mem_op_cnic(struct bnx2x *bp, u8 memop)
564 {
565         int rc = 0;
566
567         if (CONFIGURE_NIC_MODE(bp))
568                 rc = bnx2x_ilt_client_mem_op(bp, ILT_CLIENT_SRC, memop);
569         if (!rc)
570                 rc = bnx2x_ilt_client_mem_op(bp, ILT_CLIENT_TM, memop);
571
572         return rc;
573 }
574
575 static int bnx2x_ilt_mem_op(struct bnx2x *bp, u8 memop)
576 {
577         int rc = bnx2x_ilt_client_mem_op(bp, ILT_CLIENT_CDU, memop);
578         if (!rc)
579                 rc = bnx2x_ilt_client_mem_op(bp, ILT_CLIENT_QM, memop);
580         if (!rc && CNIC_SUPPORT(bp) && !CONFIGURE_NIC_MODE(bp))
581                 rc = bnx2x_ilt_client_mem_op(bp, ILT_CLIENT_SRC, memop);
582
583         return rc;
584 }
585
586 static void bnx2x_ilt_line_wr(struct bnx2x *bp, int abs_idx,
587                               dma_addr_t page_mapping)
588 {
589         u32 reg;
590
591         if (CHIP_IS_E1(bp))
592                 reg = PXP2_REG_RQ_ONCHIP_AT + abs_idx*8;
593         else
594                 reg = PXP2_REG_RQ_ONCHIP_AT_B0 + abs_idx*8;
595
596         bnx2x_wr_64(bp, reg, ILT_ADDR1(page_mapping), ILT_ADDR2(page_mapping));
597 }
598
599 static void bnx2x_ilt_line_init_op(struct bnx2x *bp,
600                                    struct bnx2x_ilt *ilt, int idx, u8 initop)
601 {
602         dma_addr_t      null_mapping;
603         int abs_idx = ilt->start_line + idx;
604
605
606         switch (initop) {
607         case INITOP_INIT:
608                 /* set in the init-value array */
609         case INITOP_SET:
610                 bnx2x_ilt_line_wr(bp, abs_idx, ilt->lines[idx].page_mapping);
611                 break;
612         case INITOP_CLEAR:
613                 null_mapping = 0;
614                 bnx2x_ilt_line_wr(bp, abs_idx, null_mapping);
615                 break;
616         }
617 }
618
619 static void bnx2x_ilt_boundry_init_op(struct bnx2x *bp,
620                                       struct ilt_client_info *ilt_cli,
621                                       u32 ilt_start, u8 initop)
622 {
623         u32 start_reg = 0;
624         u32 end_reg = 0;
625
626         /* The boundary is either SET or INIT,
627            CLEAR => SET and for now SET ~~ INIT */
628
629         /* find the appropriate regs */
630         if (CHIP_IS_E1(bp)) {
631                 switch (ilt_cli->client_num) {
632                 case ILT_CLIENT_CDU:
633                         start_reg = PXP2_REG_PSWRQ_CDU0_L2P;
634                         break;
635                 case ILT_CLIENT_QM:
636                         start_reg = PXP2_REG_PSWRQ_QM0_L2P;
637                         break;
638                 case ILT_CLIENT_SRC:
639                         start_reg = PXP2_REG_PSWRQ_SRC0_L2P;
640                         break;
641                 case ILT_CLIENT_TM:
642                         start_reg = PXP2_REG_PSWRQ_TM0_L2P;
643                         break;
644                 }
645                 REG_WR(bp, start_reg + BP_FUNC(bp)*4,
646                        ILT_RANGE((ilt_start + ilt_cli->start),
647                                  (ilt_start + ilt_cli->end)));
648         } else {
649                 switch (ilt_cli->client_num) {
650                 case ILT_CLIENT_CDU:
651                         start_reg = PXP2_REG_RQ_CDU_FIRST_ILT;
652                         end_reg = PXP2_REG_RQ_CDU_LAST_ILT;
653                         break;
654                 case ILT_CLIENT_QM:
655                         start_reg = PXP2_REG_RQ_QM_FIRST_ILT;
656                         end_reg = PXP2_REG_RQ_QM_LAST_ILT;
657                         break;
658                 case ILT_CLIENT_SRC:
659                         start_reg = PXP2_REG_RQ_SRC_FIRST_ILT;
660                         end_reg = PXP2_REG_RQ_SRC_LAST_ILT;
661                         break;
662                 case ILT_CLIENT_TM:
663                         start_reg = PXP2_REG_RQ_TM_FIRST_ILT;
664                         end_reg = PXP2_REG_RQ_TM_LAST_ILT;
665                         break;
666                 }
667                 REG_WR(bp, start_reg, (ilt_start + ilt_cli->start));
668                 REG_WR(bp, end_reg, (ilt_start + ilt_cli->end));
669         }
670 }
671
672 static void bnx2x_ilt_client_init_op_ilt(struct bnx2x *bp,
673                                          struct bnx2x_ilt *ilt,
674                                          struct ilt_client_info *ilt_cli,
675                                          u8 initop)
676 {
677         int i;
678
679         if (ilt_cli->flags & ILT_CLIENT_SKIP_INIT)
680                 return;
681
682         for (i = ilt_cli->start; i <= ilt_cli->end; i++)
683                 bnx2x_ilt_line_init_op(bp, ilt, i, initop);
684
685         /* init/clear the ILT boundries */
686         bnx2x_ilt_boundry_init_op(bp, ilt_cli, ilt->start_line, initop);
687 }
688
689 static void bnx2x_ilt_client_init_op(struct bnx2x *bp,
690                                      struct ilt_client_info *ilt_cli, u8 initop)
691 {
692         struct bnx2x_ilt *ilt = BP_ILT(bp);
693
694         bnx2x_ilt_client_init_op_ilt(bp, ilt, ilt_cli, initop);
695 }
696
697 static void bnx2x_ilt_client_id_init_op(struct bnx2x *bp,
698                                         int cli_num, u8 initop)
699 {
700         struct bnx2x_ilt *ilt = BP_ILT(bp);
701         struct ilt_client_info *ilt_cli = &ilt->clients[cli_num];
702
703         bnx2x_ilt_client_init_op(bp, ilt_cli, initop);
704 }
705
706 static void bnx2x_ilt_init_op_cnic(struct bnx2x *bp, u8 initop)
707 {
708         if (CONFIGURE_NIC_MODE(bp))
709                 bnx2x_ilt_client_id_init_op(bp, ILT_CLIENT_SRC, initop);
710         bnx2x_ilt_client_id_init_op(bp, ILT_CLIENT_TM, initop);
711 }
712
713 static void bnx2x_ilt_init_op(struct bnx2x *bp, u8 initop)
714 {
715         bnx2x_ilt_client_id_init_op(bp, ILT_CLIENT_CDU, initop);
716         bnx2x_ilt_client_id_init_op(bp, ILT_CLIENT_QM, initop);
717         if (CNIC_SUPPORT(bp) && !CONFIGURE_NIC_MODE(bp))
718                 bnx2x_ilt_client_id_init_op(bp, ILT_CLIENT_SRC, initop);
719 }
720
721 static void bnx2x_ilt_init_client_psz(struct bnx2x *bp, int cli_num,
722                                       u32 psz_reg, u8 initop)
723 {
724         struct bnx2x_ilt *ilt = BP_ILT(bp);
725         struct ilt_client_info *ilt_cli = &ilt->clients[cli_num];
726
727         if (ilt_cli->flags & ILT_CLIENT_SKIP_INIT)
728                 return;
729
730         switch (initop) {
731         case INITOP_INIT:
732                 /* set in the init-value array */
733         case INITOP_SET:
734                 REG_WR(bp, psz_reg, ILOG2(ilt_cli->page_size >> 12));
735                 break;
736         case INITOP_CLEAR:
737                 break;
738         }
739 }
740
741 /*
742  * called during init common stage, ilt clients should be initialized
743  * prioir to calling this function
744  */
745 static void bnx2x_ilt_init_page_size(struct bnx2x *bp, u8 initop)
746 {
747         bnx2x_ilt_init_client_psz(bp, ILT_CLIENT_CDU,
748                                   PXP2_REG_RQ_CDU_P_SIZE, initop);
749         bnx2x_ilt_init_client_psz(bp, ILT_CLIENT_QM,
750                                   PXP2_REG_RQ_QM_P_SIZE, initop);
751         bnx2x_ilt_init_client_psz(bp, ILT_CLIENT_SRC,
752                                   PXP2_REG_RQ_SRC_P_SIZE, initop);
753         bnx2x_ilt_init_client_psz(bp, ILT_CLIENT_TM,
754                                   PXP2_REG_RQ_TM_P_SIZE, initop);
755 }
756
757 /****************************************************************************
758 * QM initializations
759 ****************************************************************************/
760 #define QM_QUEUES_PER_FUNC      16 /* E1 has 32, but only 16 are used */
761 #define QM_INIT_MIN_CID_COUNT   31
762 #define QM_INIT(cid_cnt)        (cid_cnt > QM_INIT_MIN_CID_COUNT)
763
764 /* called during init port stage */
765 static void bnx2x_qm_init_cid_count(struct bnx2x *bp, int qm_cid_count,
766                                     u8 initop)
767 {
768         int port = BP_PORT(bp);
769
770         if (QM_INIT(qm_cid_count)) {
771                 switch (initop) {
772                 case INITOP_INIT:
773                         /* set in the init-value array */
774                 case INITOP_SET:
775                         REG_WR(bp, QM_REG_CONNNUM_0 + port*4,
776                                qm_cid_count/16 - 1);
777                         break;
778                 case INITOP_CLEAR:
779                         break;
780                 }
781         }
782 }
783
784 static void bnx2x_qm_set_ptr_table(struct bnx2x *bp, int qm_cid_count,
785                                    u32 base_reg, u32 reg)
786 {
787         int i;
788         u32 wb_data[2] = {0, 0};
789         for (i = 0; i < 4 * QM_QUEUES_PER_FUNC; i++) {
790                 REG_WR(bp, base_reg + i*4,
791                        qm_cid_count * 4 * (i % QM_QUEUES_PER_FUNC));
792                 bnx2x_init_wr_wb(bp, reg + i*8,  wb_data, 2);
793         }
794 }
795
796 /* called during init common stage */
797 static void bnx2x_qm_init_ptr_table(struct bnx2x *bp, int qm_cid_count,
798                                     u8 initop)
799 {
800         if (!QM_INIT(qm_cid_count))
801                 return;
802
803         switch (initop) {
804         case INITOP_INIT:
805                 /* set in the init-value array */
806         case INITOP_SET:
807                 bnx2x_qm_set_ptr_table(bp, qm_cid_count,
808                                        QM_REG_BASEADDR, QM_REG_PTRTBL);
809                 if (CHIP_IS_E1H(bp))
810                         bnx2x_qm_set_ptr_table(bp, qm_cid_count,
811                                                QM_REG_BASEADDR_EXT_A,
812                                                QM_REG_PTRTBL_EXT_A);
813                 break;
814         case INITOP_CLEAR:
815                 break;
816         }
817 }
818
819 /****************************************************************************
820 * SRC initializations
821 ****************************************************************************/
822 /* called during init func stage */
823 static void bnx2x_src_init_t2(struct bnx2x *bp, struct src_ent *t2,
824                               dma_addr_t t2_mapping, int src_cid_count)
825 {
826         int i;
827         int port = BP_PORT(bp);
828
829         /* Initialize T2 */
830         for (i = 0; i < src_cid_count-1; i++)
831                 t2[i].next = (u64)(t2_mapping +
832                              (i+1)*sizeof(struct src_ent));
833
834         /* tell the searcher where the T2 table is */
835         REG_WR(bp, SRC_REG_COUNTFREE0 + port*4, src_cid_count);
836
837         bnx2x_wr_64(bp, SRC_REG_FIRSTFREE0 + port*16,
838                     U64_LO(t2_mapping), U64_HI(t2_mapping));
839
840         bnx2x_wr_64(bp, SRC_REG_LASTFREE0 + port*16,
841                     U64_LO((u64)t2_mapping +
842                            (src_cid_count-1) * sizeof(struct src_ent)),
843                     U64_HI((u64)t2_mapping +
844                            (src_cid_count-1) * sizeof(struct src_ent)));
845 }
846 #endif /* BNX2X_INIT_OPS_H */