Linux-libre 4.14.132-gnu
[librecmc/linux-libre.git] / drivers / net / ethernet / broadcom / bnx2x / bnx2x_dcb.c
1 /* bnx2x_dcb.c: QLogic Everest network driver.
2  *
3  * Copyright 2009-2013 Broadcom Corporation
4  * Copyright 2014 QLogic Corporation
5  * All rights reserved
6  *
7  * Unless you and QLogic execute a separate written software license
8  * agreement governing use of this software, this software is licensed to you
9  * under the terms of the GNU General Public License version 2, available
10  * at http://www.gnu.org/licenses/old-licenses/gpl-2.0.html (the "GPL").
11  *
12  * Notwithstanding the above, under no circumstances may you combine this
13  * software in any way with any other QLogic software provided under a
14  * license other than the GPL, without QLogic's express prior written
15  * consent.
16  *
17  * Maintained by: Ariel Elior <ariel.elior@qlogic.com>
18  * Written by: Dmitry Kravkov
19  *
20  */
21
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
24 #include <linux/netdevice.h>
25 #include <linux/types.h>
26 #include <linux/errno.h>
27 #include <linux/rtnetlink.h>
28 #include <net/dcbnl.h>
29
30 #include "bnx2x.h"
31 #include "bnx2x_cmn.h"
32 #include "bnx2x_dcb.h"
33
34 /* forward declarations of dcbx related functions */
35 static void bnx2x_pfc_set_pfc(struct bnx2x *bp);
36 static void bnx2x_dcbx_update_ets_params(struct bnx2x *bp);
37 static void bnx2x_dcbx_get_ets_pri_pg_tbl(struct bnx2x *bp,
38                                           u32 *set_configuration_ets_pg,
39                                           u32 *pri_pg_tbl);
40 static void bnx2x_dcbx_get_num_pg_traf_type(struct bnx2x *bp,
41                                             u32 *pg_pri_orginal_spread,
42                                             struct pg_help_data *help_data);
43 static void bnx2x_dcbx_fill_cos_params(struct bnx2x *bp,
44                                        struct pg_help_data *help_data,
45                                        struct dcbx_ets_feature *ets,
46                                        u32 *pg_pri_orginal_spread);
47 static void bnx2x_dcbx_separate_pauseable_from_non(struct bnx2x *bp,
48                                 struct cos_help_data *cos_data,
49                                 u32 *pg_pri_orginal_spread,
50                                 struct dcbx_ets_feature *ets);
51 static void bnx2x_dcbx_fw_struct(struct bnx2x *bp,
52                                  struct bnx2x_func_tx_start_params*);
53
54 /* helpers: read/write len bytes from addr into buff by REG_RD/REG_WR */
55 static void bnx2x_read_data(struct bnx2x *bp, u32 *buff,
56                                    u32 addr, u32 len)
57 {
58         int i;
59         for (i = 0; i < len; i += 4, buff++)
60                 *buff = REG_RD(bp, addr + i);
61 }
62
63 static void bnx2x_write_data(struct bnx2x *bp, u32 *buff,
64                                     u32 addr, u32 len)
65 {
66         int i;
67         for (i = 0; i < len; i += 4, buff++)
68                 REG_WR(bp, addr + i, *buff);
69 }
70
71 static void bnx2x_pfc_set(struct bnx2x *bp)
72 {
73         struct bnx2x_nig_brb_pfc_port_params pfc_params = {0};
74         u32 pri_bit, val = 0;
75         int i;
76
77         pfc_params.num_of_rx_cos_priority_mask =
78                                         bp->dcbx_port_params.ets.num_of_cos;
79
80         /* Tx COS configuration */
81         for (i = 0; i < bp->dcbx_port_params.ets.num_of_cos; i++)
82                 /*
83                  * We configure only the pauseable bits (non pauseable aren't
84                  * configured at all) it's done to avoid false pauses from
85                  * network
86                  */
87                 pfc_params.rx_cos_priority_mask[i] =
88                         bp->dcbx_port_params.ets.cos_params[i].pri_bitmask
89                                 & DCBX_PFC_PRI_PAUSE_MASK(bp);
90
91         /*
92          * Rx COS configuration
93          * Changing PFC RX configuration .
94          * In RX COS0 will always be configured to lossless and COS1 to lossy
95          */
96         for (i = 0 ; i < MAX_PFC_PRIORITIES ; i++) {
97                 pri_bit = 1 << i;
98
99                 if (!(pri_bit & DCBX_PFC_PRI_PAUSE_MASK(bp)))
100                         val |= 1 << (i * 4);
101         }
102
103         pfc_params.pkt_priority_to_cos = val;
104
105         /* RX COS0 */
106         pfc_params.llfc_low_priority_classes = DCBX_PFC_PRI_PAUSE_MASK(bp);
107         /* RX COS1 */
108         pfc_params.llfc_high_priority_classes = 0;
109
110         bnx2x_acquire_phy_lock(bp);
111         bp->link_params.feature_config_flags |= FEATURE_CONFIG_PFC_ENABLED;
112         bnx2x_update_pfc(&bp->link_params, &bp->link_vars, &pfc_params);
113         bnx2x_release_phy_lock(bp);
114 }
115
116 static void bnx2x_pfc_clear(struct bnx2x *bp)
117 {
118         struct bnx2x_nig_brb_pfc_port_params nig_params = {0};
119         nig_params.pause_enable = 1;
120         bnx2x_acquire_phy_lock(bp);
121         bp->link_params.feature_config_flags &= ~FEATURE_CONFIG_PFC_ENABLED;
122         bnx2x_update_pfc(&bp->link_params, &bp->link_vars, &nig_params);
123         bnx2x_release_phy_lock(bp);
124 }
125
126 static void  bnx2x_dump_dcbx_drv_param(struct bnx2x *bp,
127                                        struct dcbx_features *features,
128                                        u32 error)
129 {
130         u8 i = 0;
131         DP(NETIF_MSG_LINK, "local_mib.error %x\n", error);
132
133         /* PG */
134         DP(NETIF_MSG_LINK,
135            "local_mib.features.ets.enabled %x\n", features->ets.enabled);
136         for (i = 0; i < DCBX_MAX_NUM_PG_BW_ENTRIES; i++)
137                 DP(NETIF_MSG_LINK,
138                    "local_mib.features.ets.pg_bw_tbl[%d] %d\n", i,
139                    DCBX_PG_BW_GET(features->ets.pg_bw_tbl, i));
140         for (i = 0; i < DCBX_MAX_NUM_PRI_PG_ENTRIES; i++)
141                 DP(NETIF_MSG_LINK,
142                    "local_mib.features.ets.pri_pg_tbl[%d] %d\n", i,
143                    DCBX_PRI_PG_GET(features->ets.pri_pg_tbl, i));
144
145         /* pfc */
146         DP(BNX2X_MSG_DCB, "dcbx_features.pfc.pri_en_bitmap %x\n",
147                                         features->pfc.pri_en_bitmap);
148         DP(BNX2X_MSG_DCB, "dcbx_features.pfc.pfc_caps %x\n",
149                                         features->pfc.pfc_caps);
150         DP(BNX2X_MSG_DCB, "dcbx_features.pfc.enabled %x\n",
151                                         features->pfc.enabled);
152
153         DP(BNX2X_MSG_DCB, "dcbx_features.app.default_pri %x\n",
154                                         features->app.default_pri);
155         DP(BNX2X_MSG_DCB, "dcbx_features.app.tc_supported %x\n",
156                                         features->app.tc_supported);
157         DP(BNX2X_MSG_DCB, "dcbx_features.app.enabled %x\n",
158                                         features->app.enabled);
159         for (i = 0; i < DCBX_MAX_APP_PROTOCOL; i++) {
160                 DP(BNX2X_MSG_DCB,
161                    "dcbx_features.app.app_pri_tbl[%x].app_id %x\n",
162                    i, features->app.app_pri_tbl[i].app_id);
163                 DP(BNX2X_MSG_DCB,
164                    "dcbx_features.app.app_pri_tbl[%x].pri_bitmap %x\n",
165                    i, features->app.app_pri_tbl[i].pri_bitmap);
166                 DP(BNX2X_MSG_DCB,
167                    "dcbx_features.app.app_pri_tbl[%x].appBitfield %x\n",
168                    i, features->app.app_pri_tbl[i].appBitfield);
169         }
170 }
171
172 static void bnx2x_dcbx_get_ap_priority(struct bnx2x *bp,
173                                        u8 pri_bitmap,
174                                        u8 llfc_traf_type)
175 {
176         u32 pri = MAX_PFC_PRIORITIES;
177         u32 index = MAX_PFC_PRIORITIES - 1;
178         u32 pri_mask;
179         u32 *ttp = bp->dcbx_port_params.app.traffic_type_priority;
180
181         /* Choose the highest priority */
182         while ((MAX_PFC_PRIORITIES == pri) && (0 != index)) {
183                 pri_mask = 1 << index;
184                 if (GET_FLAGS(pri_bitmap, pri_mask))
185                         pri = index ;
186                 index--;
187         }
188
189         if (pri < MAX_PFC_PRIORITIES)
190                 ttp[llfc_traf_type] = max_t(u32, ttp[llfc_traf_type], pri);
191 }
192
193 static void bnx2x_dcbx_get_ap_feature(struct bnx2x *bp,
194                                    struct dcbx_app_priority_feature *app,
195                                    u32 error) {
196         u8 index;
197         u32 *ttp = bp->dcbx_port_params.app.traffic_type_priority;
198         u8 iscsi_pri_found = 0, fcoe_pri_found = 0;
199
200         if (GET_FLAGS(error, DCBX_LOCAL_APP_ERROR))
201                 DP(BNX2X_MSG_DCB, "DCBX_LOCAL_APP_ERROR\n");
202
203         if (GET_FLAGS(error, DCBX_LOCAL_APP_MISMATCH))
204                 DP(BNX2X_MSG_DCB, "DCBX_LOCAL_APP_MISMATCH\n");
205
206         if (GET_FLAGS(error, DCBX_REMOTE_APP_TLV_NOT_FOUND))
207                 DP(BNX2X_MSG_DCB, "DCBX_REMOTE_APP_TLV_NOT_FOUND\n");
208         if (app->enabled &&
209             !GET_FLAGS(error, DCBX_LOCAL_APP_ERROR | DCBX_LOCAL_APP_MISMATCH |
210                               DCBX_REMOTE_APP_TLV_NOT_FOUND)) {
211
212                 bp->dcbx_port_params.app.enabled = true;
213
214                 /* Use 0 as the default application priority for all. */
215                 for (index = 0 ; index < LLFC_DRIVER_TRAFFIC_TYPE_MAX; index++)
216                         ttp[index] = 0;
217
218                 for (index = 0 ; index < DCBX_MAX_APP_PROTOCOL; index++) {
219                         struct dcbx_app_priority_entry *entry =
220                                                         app->app_pri_tbl;
221                         enum traffic_type type = MAX_TRAFFIC_TYPE;
222
223                         if (GET_FLAGS(entry[index].appBitfield,
224                                       DCBX_APP_SF_DEFAULT) &&
225                             GET_FLAGS(entry[index].appBitfield,
226                                       DCBX_APP_SF_ETH_TYPE)) {
227                                 type = LLFC_TRAFFIC_TYPE_NW;
228                         } else if (GET_FLAGS(entry[index].appBitfield,
229                                              DCBX_APP_SF_PORT) &&
230                                    TCP_PORT_ISCSI == entry[index].app_id) {
231                                 type = LLFC_TRAFFIC_TYPE_ISCSI;
232                                 iscsi_pri_found = 1;
233                         } else if (GET_FLAGS(entry[index].appBitfield,
234                                              DCBX_APP_SF_ETH_TYPE) &&
235                                    ETH_TYPE_FCOE == entry[index].app_id) {
236                                 type = LLFC_TRAFFIC_TYPE_FCOE;
237                                 fcoe_pri_found = 1;
238                         }
239
240                         if (type == MAX_TRAFFIC_TYPE)
241                                 continue;
242
243                         bnx2x_dcbx_get_ap_priority(bp,
244                                                    entry[index].pri_bitmap,
245                                                    type);
246                 }
247
248                 /* If we have received a non-zero default application
249                  * priority, then use that for applications which are
250                  * not configured with any priority.
251                  */
252                 if (ttp[LLFC_TRAFFIC_TYPE_NW] != 0) {
253                         if (!iscsi_pri_found) {
254                                 ttp[LLFC_TRAFFIC_TYPE_ISCSI] =
255                                         ttp[LLFC_TRAFFIC_TYPE_NW];
256                                 DP(BNX2X_MSG_DCB,
257                                    "ISCSI is using default priority.\n");
258                         }
259                         if (!fcoe_pri_found) {
260                                 ttp[LLFC_TRAFFIC_TYPE_FCOE] =
261                                         ttp[LLFC_TRAFFIC_TYPE_NW];
262                                 DP(BNX2X_MSG_DCB,
263                                    "FCoE is using default priority.\n");
264                         }
265                 }
266         } else {
267                 DP(BNX2X_MSG_DCB, "DCBX_LOCAL_APP_DISABLED\n");
268                 bp->dcbx_port_params.app.enabled = false;
269                 for (index = 0 ; index < LLFC_DRIVER_TRAFFIC_TYPE_MAX; index++)
270                         ttp[index] = INVALID_TRAFFIC_TYPE_PRIORITY;
271         }
272 }
273
274 static void bnx2x_dcbx_get_ets_feature(struct bnx2x *bp,
275                                        struct dcbx_ets_feature *ets,
276                                        u32 error) {
277         int i = 0;
278         u32 pg_pri_orginal_spread[DCBX_MAX_NUM_PG_BW_ENTRIES] = {0};
279         struct pg_help_data pg_help_data;
280         struct bnx2x_dcbx_cos_params *cos_params =
281                         bp->dcbx_port_params.ets.cos_params;
282
283         memset(&pg_help_data, 0, sizeof(struct pg_help_data));
284
285         if (GET_FLAGS(error, DCBX_LOCAL_ETS_ERROR))
286                 DP(BNX2X_MSG_DCB, "DCBX_LOCAL_ETS_ERROR\n");
287
288         if (GET_FLAGS(error, DCBX_REMOTE_ETS_TLV_NOT_FOUND))
289                 DP(BNX2X_MSG_DCB, "DCBX_REMOTE_ETS_TLV_NOT_FOUND\n");
290
291         /* Clean up old settings of ets on COS */
292         for (i = 0; i < ARRAY_SIZE(bp->dcbx_port_params.ets.cos_params) ; i++) {
293                 cos_params[i].pauseable = false;
294                 cos_params[i].strict = BNX2X_DCBX_STRICT_INVALID;
295                 cos_params[i].bw_tbl = DCBX_INVALID_COS_BW;
296                 cos_params[i].pri_bitmask = 0;
297         }
298
299         if (bp->dcbx_port_params.app.enabled && ets->enabled &&
300            !GET_FLAGS(error,
301                       DCBX_LOCAL_ETS_ERROR | DCBX_REMOTE_ETS_TLV_NOT_FOUND)) {
302                 DP(BNX2X_MSG_DCB, "DCBX_LOCAL_ETS_ENABLE\n");
303                 bp->dcbx_port_params.ets.enabled = true;
304
305                 bnx2x_dcbx_get_ets_pri_pg_tbl(bp,
306                                               pg_pri_orginal_spread,
307                                               ets->pri_pg_tbl);
308
309                 bnx2x_dcbx_get_num_pg_traf_type(bp,
310                                                 pg_pri_orginal_spread,
311                                                 &pg_help_data);
312
313                 bnx2x_dcbx_fill_cos_params(bp, &pg_help_data,
314                                            ets, pg_pri_orginal_spread);
315
316         } else {
317                 DP(BNX2X_MSG_DCB, "DCBX_LOCAL_ETS_DISABLED\n");
318                 bp->dcbx_port_params.ets.enabled = false;
319                 ets->pri_pg_tbl[0] = 0;
320
321                 for (i = 0; i < DCBX_MAX_NUM_PRI_PG_ENTRIES ; i++)
322                         DCBX_PG_BW_SET(ets->pg_bw_tbl, i, 1);
323         }
324 }
325
326 static void  bnx2x_dcbx_get_pfc_feature(struct bnx2x *bp,
327                                         struct dcbx_pfc_feature *pfc, u32 error)
328 {
329         if (GET_FLAGS(error, DCBX_LOCAL_PFC_ERROR))
330                 DP(BNX2X_MSG_DCB, "DCBX_LOCAL_PFC_ERROR\n");
331
332         if (GET_FLAGS(error, DCBX_REMOTE_PFC_TLV_NOT_FOUND))
333                 DP(BNX2X_MSG_DCB, "DCBX_REMOTE_PFC_TLV_NOT_FOUND\n");
334         if (bp->dcbx_port_params.app.enabled && pfc->enabled &&
335            !GET_FLAGS(error, DCBX_LOCAL_PFC_ERROR | DCBX_LOCAL_PFC_MISMATCH |
336                              DCBX_REMOTE_PFC_TLV_NOT_FOUND)) {
337                 bp->dcbx_port_params.pfc.enabled = true;
338                 bp->dcbx_port_params.pfc.priority_non_pauseable_mask =
339                         ~(pfc->pri_en_bitmap);
340         } else {
341                 DP(BNX2X_MSG_DCB, "DCBX_LOCAL_PFC_DISABLED\n");
342                 bp->dcbx_port_params.pfc.enabled = false;
343                 bp->dcbx_port_params.pfc.priority_non_pauseable_mask = 0;
344         }
345 }
346
347 /* maps unmapped priorities to to the same COS as L2 */
348 static void bnx2x_dcbx_map_nw(struct bnx2x *bp)
349 {
350         int i;
351         u32 unmapped = (1 << MAX_PFC_PRIORITIES) - 1; /* all ones */
352         u32 *ttp = bp->dcbx_port_params.app.traffic_type_priority;
353         u32 nw_prio = 1 << ttp[LLFC_TRAFFIC_TYPE_NW];
354         struct bnx2x_dcbx_cos_params *cos_params =
355                         bp->dcbx_port_params.ets.cos_params;
356
357         /* get unmapped priorities by clearing mapped bits */
358         for (i = 0; i < LLFC_DRIVER_TRAFFIC_TYPE_MAX; i++)
359                 unmapped &= ~(1 << ttp[i]);
360
361         /* find cos for nw prio and extend it with unmapped */
362         for (i = 0; i < ARRAY_SIZE(bp->dcbx_port_params.ets.cos_params); i++) {
363                 if (cos_params[i].pri_bitmask & nw_prio) {
364                         /* extend the bitmask with unmapped */
365                         DP(BNX2X_MSG_DCB,
366                            "cos %d extended with 0x%08x\n", i, unmapped);
367                         cos_params[i].pri_bitmask |= unmapped;
368                         break;
369                 }
370         }
371 }
372
373 static void bnx2x_get_dcbx_drv_param(struct bnx2x *bp,
374                                      struct dcbx_features *features,
375                                      u32 error)
376 {
377         bnx2x_dcbx_get_ap_feature(bp, &features->app, error);
378
379         bnx2x_dcbx_get_pfc_feature(bp, &features->pfc, error);
380
381         bnx2x_dcbx_get_ets_feature(bp, &features->ets, error);
382
383         bnx2x_dcbx_map_nw(bp);
384 }
385
386 #define DCBX_LOCAL_MIB_MAX_TRY_READ             (100)
387 static int bnx2x_dcbx_read_mib(struct bnx2x *bp,
388                                u32 *base_mib_addr,
389                                u32 offset,
390                                int read_mib_type)
391 {
392         int max_try_read = 0;
393         u32 mib_size, prefix_seq_num, suffix_seq_num;
394         struct lldp_remote_mib *remote_mib ;
395         struct lldp_local_mib  *local_mib;
396
397         switch (read_mib_type) {
398         case DCBX_READ_LOCAL_MIB:
399                 mib_size = sizeof(struct lldp_local_mib);
400                 break;
401         case DCBX_READ_REMOTE_MIB:
402                 mib_size = sizeof(struct lldp_remote_mib);
403                 break;
404         default:
405                 return 1; /*error*/
406         }
407
408         offset += BP_PORT(bp) * mib_size;
409
410         do {
411                 bnx2x_read_data(bp, base_mib_addr, offset, mib_size);
412
413                 max_try_read++;
414
415                 switch (read_mib_type) {
416                 case DCBX_READ_LOCAL_MIB:
417                         local_mib = (struct lldp_local_mib *) base_mib_addr;
418                         prefix_seq_num = local_mib->prefix_seq_num;
419                         suffix_seq_num = local_mib->suffix_seq_num;
420                         break;
421                 case DCBX_READ_REMOTE_MIB:
422                         remote_mib = (struct lldp_remote_mib *) base_mib_addr;
423                         prefix_seq_num = remote_mib->prefix_seq_num;
424                         suffix_seq_num = remote_mib->suffix_seq_num;
425                         break;
426                 default:
427                         return 1; /*error*/
428                 }
429         } while ((prefix_seq_num != suffix_seq_num) &&
430                (max_try_read < DCBX_LOCAL_MIB_MAX_TRY_READ));
431
432         if (max_try_read >= DCBX_LOCAL_MIB_MAX_TRY_READ) {
433                 BNX2X_ERR("MIB could not be read\n");
434                 return 1;
435         }
436
437         return 0;
438 }
439
440 static void bnx2x_pfc_set_pfc(struct bnx2x *bp)
441 {
442         int mfw_configured = SHMEM2_HAS(bp, drv_flags) &&
443                              GET_FLAGS(SHMEM2_RD(bp, drv_flags),
444                                        1 << DRV_FLAGS_DCB_MFW_CONFIGURED);
445
446         if (bp->dcbx_port_params.pfc.enabled &&
447             (!(bp->dcbx_error & DCBX_REMOTE_MIB_ERROR) || mfw_configured))
448                 /*
449                  * 1. Fills up common PFC structures if required
450                  * 2. Configure NIG, MAC and BRB via the elink
451                  */
452                 bnx2x_pfc_set(bp);
453         else
454                 bnx2x_pfc_clear(bp);
455 }
456
457 int bnx2x_dcbx_stop_hw_tx(struct bnx2x *bp)
458 {
459         struct bnx2x_func_state_params func_params = {NULL};
460         int rc;
461
462         func_params.f_obj = &bp->func_obj;
463         func_params.cmd = BNX2X_F_CMD_TX_STOP;
464
465         __set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
466         __set_bit(RAMROD_RETRY, &func_params.ramrod_flags);
467
468         DP(BNX2X_MSG_DCB, "STOP TRAFFIC\n");
469
470         rc = bnx2x_func_state_change(bp, &func_params);
471         if (rc) {
472                 BNX2X_ERR("Unable to hold traffic for HW configuration\n");
473                 bnx2x_panic();
474         }
475
476         return rc;
477 }
478
479 int bnx2x_dcbx_resume_hw_tx(struct bnx2x *bp)
480 {
481         struct bnx2x_func_state_params func_params = {NULL};
482         struct bnx2x_func_tx_start_params *tx_params =
483                 &func_params.params.tx_start;
484         int rc;
485
486         func_params.f_obj = &bp->func_obj;
487         func_params.cmd = BNX2X_F_CMD_TX_START;
488
489         __set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
490         __set_bit(RAMROD_RETRY, &func_params.ramrod_flags);
491
492         bnx2x_dcbx_fw_struct(bp, tx_params);
493
494         DP(BNX2X_MSG_DCB, "START TRAFFIC\n");
495
496         rc = bnx2x_func_state_change(bp, &func_params);
497         if (rc) {
498                 BNX2X_ERR("Unable to resume traffic after HW configuration\n");
499                 bnx2x_panic();
500         }
501
502         return rc;
503 }
504
505 static void bnx2x_dcbx_2cos_limit_update_ets_config(struct bnx2x *bp)
506 {
507         struct bnx2x_dcbx_pg_params *ets = &(bp->dcbx_port_params.ets);
508         int rc = 0;
509
510         if (ets->num_of_cos == 0 || ets->num_of_cos > DCBX_COS_MAX_NUM_E2) {
511                 BNX2X_ERR("Illegal number of COSes %d\n", ets->num_of_cos);
512                 return;
513         }
514
515         /* valid COS entries */
516         if (ets->num_of_cos == 1)   /* no ETS */
517                 return;
518
519         /* sanity */
520         if (((BNX2X_DCBX_STRICT_INVALID == ets->cos_params[0].strict) &&
521              (DCBX_INVALID_COS_BW == ets->cos_params[0].bw_tbl)) ||
522             ((BNX2X_DCBX_STRICT_INVALID == ets->cos_params[1].strict) &&
523              (DCBX_INVALID_COS_BW == ets->cos_params[1].bw_tbl))) {
524                 BNX2X_ERR("all COS should have at least bw_limit or strict"
525                             "ets->cos_params[0].strict= %x"
526                             "ets->cos_params[0].bw_tbl= %x"
527                             "ets->cos_params[1].strict= %x"
528                             "ets->cos_params[1].bw_tbl= %x",
529                           ets->cos_params[0].strict,
530                           ets->cos_params[0].bw_tbl,
531                           ets->cos_params[1].strict,
532                           ets->cos_params[1].bw_tbl);
533                 return;
534         }
535         /* If we join a group and there is bw_tbl and strict then bw rules */
536         if ((DCBX_INVALID_COS_BW != ets->cos_params[0].bw_tbl) &&
537             (DCBX_INVALID_COS_BW != ets->cos_params[1].bw_tbl)) {
538                 u32 bw_tbl_0 = ets->cos_params[0].bw_tbl;
539                 u32 bw_tbl_1 = ets->cos_params[1].bw_tbl;
540                 /* Do not allow 0-100 configuration
541                  * since PBF does not support it
542                  * force 1-99 instead
543                  */
544                 if (bw_tbl_0 == 0) {
545                         bw_tbl_0 = 1;
546                         bw_tbl_1 = 99;
547                 } else if (bw_tbl_1 == 0) {
548                         bw_tbl_1 = 1;
549                         bw_tbl_0 = 99;
550                 }
551
552                 bnx2x_ets_bw_limit(&bp->link_params, bw_tbl_0, bw_tbl_1);
553         } else {
554                 if (ets->cos_params[0].strict == BNX2X_DCBX_STRICT_COS_HIGHEST)
555                         rc = bnx2x_ets_strict(&bp->link_params, 0);
556                 else if (ets->cos_params[1].strict
557                                         == BNX2X_DCBX_STRICT_COS_HIGHEST)
558                         rc = bnx2x_ets_strict(&bp->link_params, 1);
559                 if (rc)
560                         BNX2X_ERR("update_ets_params failed\n");
561         }
562 }
563
564 /*
565  * In E3B0 the configuration may have more than 2 COS.
566  */
567 static void bnx2x_dcbx_update_ets_config(struct bnx2x *bp)
568 {
569         struct bnx2x_dcbx_pg_params *ets = &(bp->dcbx_port_params.ets);
570         struct bnx2x_ets_params ets_params = { 0 };
571         u8 i;
572
573         ets_params.num_of_cos = ets->num_of_cos;
574
575         for (i = 0; i < ets->num_of_cos; i++) {
576                 /* COS is SP */
577                 if (ets->cos_params[i].strict != BNX2X_DCBX_STRICT_INVALID) {
578                         if (ets->cos_params[i].bw_tbl != DCBX_INVALID_COS_BW) {
579                                 BNX2X_ERR("COS can't be not BW and not SP\n");
580                                 return;
581                         }
582
583                         ets_params.cos[i].state = bnx2x_cos_state_strict;
584                         ets_params.cos[i].params.sp_params.pri =
585                                                 ets->cos_params[i].strict;
586                 } else { /* COS is BW */
587                         if (ets->cos_params[i].bw_tbl == DCBX_INVALID_COS_BW) {
588                                 BNX2X_ERR("COS can't be not BW and not SP\n");
589                                 return;
590                         }
591                         ets_params.cos[i].state = bnx2x_cos_state_bw;
592                         ets_params.cos[i].params.bw_params.bw =
593                                                 (u8)ets->cos_params[i].bw_tbl;
594                 }
595         }
596
597         /* Configure the ETS in HW */
598         if (bnx2x_ets_e3b0_config(&bp->link_params, &bp->link_vars,
599                                   &ets_params)) {
600                 BNX2X_ERR("bnx2x_ets_e3b0_config failed\n");
601                 bnx2x_ets_disabled(&bp->link_params, &bp->link_vars);
602         }
603 }
604
605 static void bnx2x_dcbx_update_ets_params(struct bnx2x *bp)
606 {
607         int mfw_configured = SHMEM2_HAS(bp, drv_flags) &&
608                              GET_FLAGS(SHMEM2_RD(bp, drv_flags),
609                                        1 << DRV_FLAGS_DCB_MFW_CONFIGURED);
610
611         bnx2x_ets_disabled(&bp->link_params, &bp->link_vars);
612
613         if (!bp->dcbx_port_params.ets.enabled ||
614             ((bp->dcbx_error & DCBX_REMOTE_MIB_ERROR) && !mfw_configured))
615                 return;
616
617         if (CHIP_IS_E3B0(bp))
618                 bnx2x_dcbx_update_ets_config(bp);
619         else
620                 bnx2x_dcbx_2cos_limit_update_ets_config(bp);
621 }
622
623 #ifdef BCM_DCBNL
624 static int bnx2x_dcbx_read_shmem_remote_mib(struct bnx2x *bp)
625 {
626         struct lldp_remote_mib remote_mib = {0};
627         u32 dcbx_remote_mib_offset = SHMEM2_RD(bp, dcbx_remote_mib_offset);
628         int rc;
629
630         DP(BNX2X_MSG_DCB, "dcbx_remote_mib_offset 0x%x\n",
631            dcbx_remote_mib_offset);
632
633         if (SHMEM_DCBX_REMOTE_MIB_NONE == dcbx_remote_mib_offset) {
634                 BNX2X_ERR("FW doesn't support dcbx_remote_mib_offset\n");
635                 return -EINVAL;
636         }
637
638         rc = bnx2x_dcbx_read_mib(bp, (u32 *)&remote_mib, dcbx_remote_mib_offset,
639                                  DCBX_READ_REMOTE_MIB);
640
641         if (rc) {
642                 BNX2X_ERR("Failed to read remote mib from FW\n");
643                 return rc;
644         }
645
646         /* save features and flags */
647         bp->dcbx_remote_feat = remote_mib.features;
648         bp->dcbx_remote_flags = remote_mib.flags;
649         return 0;
650 }
651 #endif
652
653 static int bnx2x_dcbx_read_shmem_neg_results(struct bnx2x *bp)
654 {
655         struct lldp_local_mib local_mib = {0};
656         u32 dcbx_neg_res_offset = SHMEM2_RD(bp, dcbx_neg_res_offset);
657         int rc;
658
659         DP(BNX2X_MSG_DCB, "dcbx_neg_res_offset 0x%x\n", dcbx_neg_res_offset);
660
661         if (SHMEM_DCBX_NEG_RES_NONE == dcbx_neg_res_offset) {
662                 BNX2X_ERR("FW doesn't support dcbx_neg_res_offset\n");
663                 return -EINVAL;
664         }
665
666         rc = bnx2x_dcbx_read_mib(bp, (u32 *)&local_mib, dcbx_neg_res_offset,
667                                  DCBX_READ_LOCAL_MIB);
668
669         if (rc) {
670                 BNX2X_ERR("Failed to read local mib from FW\n");
671                 return rc;
672         }
673
674         /* save features and error */
675         bp->dcbx_local_feat = local_mib.features;
676         bp->dcbx_error = local_mib.error;
677         return 0;
678 }
679
680 #ifdef BCM_DCBNL
681 static inline
682 u8 bnx2x_dcbx_dcbnl_app_up(struct dcbx_app_priority_entry *ent)
683 {
684         u8 pri;
685
686         /* Choose the highest priority */
687         for (pri = MAX_PFC_PRIORITIES - 1; pri > 0; pri--)
688                 if (ent->pri_bitmap & (1 << pri))
689                         break;
690         return pri;
691 }
692
693 static inline
694 u8 bnx2x_dcbx_dcbnl_app_idtype(struct dcbx_app_priority_entry *ent)
695 {
696         return ((ent->appBitfield & DCBX_APP_ENTRY_SF_MASK) ==
697                 DCBX_APP_SF_PORT) ? DCB_APP_IDTYPE_PORTNUM :
698                 DCB_APP_IDTYPE_ETHTYPE;
699 }
700
701 int bnx2x_dcbnl_update_applist(struct bnx2x *bp, bool delall)
702 {
703         int i, err = 0;
704
705         for (i = 0; i < DCBX_MAX_APP_PROTOCOL && err == 0; i++) {
706                 struct dcbx_app_priority_entry *ent =
707                         &bp->dcbx_local_feat.app.app_pri_tbl[i];
708
709                 if (ent->appBitfield & DCBX_APP_ENTRY_VALID) {
710                         u8 up = bnx2x_dcbx_dcbnl_app_up(ent);
711
712                         /* avoid invalid user-priority */
713                         if (up) {
714                                 struct dcb_app app;
715                                 app.selector = bnx2x_dcbx_dcbnl_app_idtype(ent);
716                                 app.protocol = ent->app_id;
717                                 app.priority = delall ? 0 : up;
718                                 err = dcb_setapp(bp->dev, &app);
719                         }
720                 }
721         }
722         return err;
723 }
724 #endif
725
726 static inline void bnx2x_dcbx_update_tc_mapping(struct bnx2x *bp)
727 {
728         u8 prio, cos;
729         for (cos = 0; cos < bp->dcbx_port_params.ets.num_of_cos; cos++) {
730                 for (prio = 0; prio < BNX2X_MAX_PRIORITY; prio++) {
731                         if (bp->dcbx_port_params.ets.cos_params[cos].pri_bitmask
732                             & (1 << prio)) {
733                                 bp->prio_to_cos[prio] = cos;
734                                 DP(BNX2X_MSG_DCB,
735                                    "tx_mapping %d --> %d\n", prio, cos);
736                         }
737                 }
738         }
739
740         /* setup tc must be called under rtnl lock, but we can't take it here
741          * as we are handling an attention on a work queue which must be
742          * flushed at some rtnl-locked contexts (e.g. if down)
743          */
744         bnx2x_schedule_sp_rtnl(bp, BNX2X_SP_RTNL_SETUP_TC, 0);
745 }
746
747 void bnx2x_dcbx_set_params(struct bnx2x *bp, u32 state)
748 {
749         switch (state) {
750         case BNX2X_DCBX_STATE_NEG_RECEIVED:
751                 {
752                         DP(BNX2X_MSG_DCB, "BNX2X_DCBX_STATE_NEG_RECEIVED\n");
753 #ifdef BCM_DCBNL
754                         /**
755                          * Delete app tlvs from dcbnl before reading new
756                          * negotiation results
757                          */
758                         bnx2x_dcbnl_update_applist(bp, true);
759
760                         /* Read remote mib if dcbx is in the FW */
761                         if (bnx2x_dcbx_read_shmem_remote_mib(bp))
762                                 return;
763 #endif
764                         /* Read neg results if dcbx is in the FW */
765                         if (bnx2x_dcbx_read_shmem_neg_results(bp))
766                                 return;
767
768                         bnx2x_dump_dcbx_drv_param(bp, &bp->dcbx_local_feat,
769                                                   bp->dcbx_error);
770
771                         bnx2x_get_dcbx_drv_param(bp, &bp->dcbx_local_feat,
772                                                  bp->dcbx_error);
773
774                         /* mark DCBX result for PMF migration */
775                         bnx2x_update_drv_flags(bp,
776                                                1 << DRV_FLAGS_DCB_CONFIGURED,
777                                                1);
778 #ifdef BCM_DCBNL
779                         /*
780                          * Add new app tlvs to dcbnl
781                          */
782                         bnx2x_dcbnl_update_applist(bp, false);
783 #endif
784                         /*
785                          * reconfigure the netdevice with the results of the new
786                          * dcbx negotiation.
787                          */
788                         bnx2x_dcbx_update_tc_mapping(bp);
789
790                         /*
791                          * allow other functions to update their netdevices
792                          * accordingly
793                          */
794                         if (IS_MF(bp))
795                                 bnx2x_link_sync_notify(bp);
796
797                         bnx2x_schedule_sp_rtnl(bp, BNX2X_SP_RTNL_TX_STOP, 0);
798                         return;
799                 }
800         case BNX2X_DCBX_STATE_TX_PAUSED:
801                 DP(BNX2X_MSG_DCB, "BNX2X_DCBX_STATE_TX_PAUSED\n");
802                 bnx2x_pfc_set_pfc(bp);
803
804                 bnx2x_dcbx_update_ets_params(bp);
805
806                 /* ets may affect cmng configuration: reinit it in hw */
807                 bnx2x_set_local_cmng(bp);
808                 return;
809         case BNX2X_DCBX_STATE_TX_RELEASED:
810                 DP(BNX2X_MSG_DCB, "BNX2X_DCBX_STATE_TX_RELEASED\n");
811                 bnx2x_fw_command(bp, DRV_MSG_CODE_DCBX_PMF_DRV_OK, 0);
812 #ifdef BCM_DCBNL
813                 /*
814                  * Send a notification for the new negotiated parameters
815                  */
816                 dcbnl_cee_notify(bp->dev, RTM_GETDCB, DCB_CMD_CEE_GET, 0, 0);
817 #endif
818                 return;
819         default:
820                 BNX2X_ERR("Unknown DCBX_STATE\n");
821         }
822 }
823
824 #define LLDP_ADMIN_MIB_OFFSET(bp)       (PORT_MAX*sizeof(struct lldp_params) + \
825                                       BP_PORT(bp)*sizeof(struct lldp_admin_mib))
826
827 static void bnx2x_dcbx_admin_mib_updated_params(struct bnx2x *bp,
828                                 u32 dcbx_lldp_params_offset)
829 {
830         struct lldp_admin_mib admin_mib;
831         u32 i, other_traf_type = PREDEFINED_APP_IDX_MAX, traf_type = 0;
832         u32 offset = dcbx_lldp_params_offset + LLDP_ADMIN_MIB_OFFSET(bp);
833
834         /*shortcuts*/
835         struct dcbx_features *af = &admin_mib.features;
836         struct bnx2x_config_dcbx_params *dp = &bp->dcbx_config_params;
837
838         memset(&admin_mib, 0, sizeof(struct lldp_admin_mib));
839
840         /* Read the data first */
841         bnx2x_read_data(bp, (u32 *)&admin_mib, offset,
842                         sizeof(struct lldp_admin_mib));
843
844         if (bp->dcbx_enabled == BNX2X_DCBX_ENABLED_ON_NEG_ON)
845                 SET_FLAGS(admin_mib.ver_cfg_flags, DCBX_DCBX_ENABLED);
846         else
847                 RESET_FLAGS(admin_mib.ver_cfg_flags, DCBX_DCBX_ENABLED);
848
849         if (dp->overwrite_settings == BNX2X_DCBX_OVERWRITE_SETTINGS_ENABLE) {
850
851                 RESET_FLAGS(admin_mib.ver_cfg_flags, DCBX_CEE_VERSION_MASK);
852                 admin_mib.ver_cfg_flags |=
853                         (dp->admin_dcbx_version << DCBX_CEE_VERSION_SHIFT) &
854                          DCBX_CEE_VERSION_MASK;
855
856                 af->ets.enabled = (u8)dp->admin_ets_enable;
857
858                 af->pfc.enabled = (u8)dp->admin_pfc_enable;
859
860                 /* FOR IEEE dp->admin_tc_supported_tx_enable */
861                 if (dp->admin_ets_configuration_tx_enable)
862                         SET_FLAGS(admin_mib.ver_cfg_flags,
863                                   DCBX_ETS_CONFIG_TX_ENABLED);
864                 else
865                         RESET_FLAGS(admin_mib.ver_cfg_flags,
866                                     DCBX_ETS_CONFIG_TX_ENABLED);
867                 /* For IEEE admin_ets_recommendation_tx_enable */
868                 if (dp->admin_pfc_tx_enable)
869                         SET_FLAGS(admin_mib.ver_cfg_flags,
870                                   DCBX_PFC_CONFIG_TX_ENABLED);
871                 else
872                         RESET_FLAGS(admin_mib.ver_cfg_flags,
873                                   DCBX_PFC_CONFIG_TX_ENABLED);
874
875                 if (dp->admin_application_priority_tx_enable)
876                         SET_FLAGS(admin_mib.ver_cfg_flags,
877                                   DCBX_APP_CONFIG_TX_ENABLED);
878                 else
879                         RESET_FLAGS(admin_mib.ver_cfg_flags,
880                                   DCBX_APP_CONFIG_TX_ENABLED);
881
882                 if (dp->admin_ets_willing)
883                         SET_FLAGS(admin_mib.ver_cfg_flags, DCBX_ETS_WILLING);
884                 else
885                         RESET_FLAGS(admin_mib.ver_cfg_flags, DCBX_ETS_WILLING);
886                 /* For IEEE admin_ets_reco_valid */
887                 if (dp->admin_pfc_willing)
888                         SET_FLAGS(admin_mib.ver_cfg_flags, DCBX_PFC_WILLING);
889                 else
890                         RESET_FLAGS(admin_mib.ver_cfg_flags, DCBX_PFC_WILLING);
891
892                 if (dp->admin_app_priority_willing)
893                         SET_FLAGS(admin_mib.ver_cfg_flags, DCBX_APP_WILLING);
894                 else
895                         RESET_FLAGS(admin_mib.ver_cfg_flags, DCBX_APP_WILLING);
896
897                 for (i = 0 ; i < DCBX_MAX_NUM_PG_BW_ENTRIES; i++) {
898                         DCBX_PG_BW_SET(af->ets.pg_bw_tbl, i,
899                                 (u8)dp->admin_configuration_bw_precentage[i]);
900
901                         DP(BNX2X_MSG_DCB, "pg_bw_tbl[%d] = %02x\n",
902                            i, DCBX_PG_BW_GET(af->ets.pg_bw_tbl, i));
903                 }
904
905                 for (i = 0; i < DCBX_MAX_NUM_PRI_PG_ENTRIES; i++) {
906                         DCBX_PRI_PG_SET(af->ets.pri_pg_tbl, i,
907                                         (u8)dp->admin_configuration_ets_pg[i]);
908
909                         DP(BNX2X_MSG_DCB, "pri_pg_tbl[%d] = %02x\n",
910                            i, DCBX_PRI_PG_GET(af->ets.pri_pg_tbl, i));
911                 }
912
913                 /*For IEEE admin_recommendation_bw_percentage
914                  *For IEEE admin_recommendation_ets_pg */
915                 af->pfc.pri_en_bitmap = (u8)dp->admin_pfc_bitmap;
916                 for (i = 0; i < DCBX_CONFIG_MAX_APP_PROTOCOL; i++) {
917                         if (dp->admin_priority_app_table[i].valid) {
918                                 struct bnx2x_admin_priority_app_table *table =
919                                         dp->admin_priority_app_table;
920                                 if ((ETH_TYPE_FCOE == table[i].app_id) &&
921                                    (TRAFFIC_TYPE_ETH == table[i].traffic_type))
922                                         traf_type = FCOE_APP_IDX;
923                                 else if ((TCP_PORT_ISCSI == table[i].app_id) &&
924                                    (TRAFFIC_TYPE_PORT == table[i].traffic_type))
925                                         traf_type = ISCSI_APP_IDX;
926                                 else
927                                         traf_type = other_traf_type++;
928
929                                 af->app.app_pri_tbl[traf_type].app_id =
930                                         table[i].app_id;
931
932                                 af->app.app_pri_tbl[traf_type].pri_bitmap =
933                                         (u8)(1 << table[i].priority);
934
935                                 af->app.app_pri_tbl[traf_type].appBitfield =
936                                     (DCBX_APP_ENTRY_VALID);
937
938                                 af->app.app_pri_tbl[traf_type].appBitfield |=
939                                    (TRAFFIC_TYPE_ETH == table[i].traffic_type) ?
940                                         DCBX_APP_SF_ETH_TYPE : DCBX_APP_SF_PORT;
941                         }
942                 }
943
944                 af->app.default_pri = (u8)dp->admin_default_priority;
945         }
946
947         /* Write the data. */
948         bnx2x_write_data(bp, (u32 *)&admin_mib, offset,
949                          sizeof(struct lldp_admin_mib));
950 }
951
952 void bnx2x_dcbx_set_state(struct bnx2x *bp, bool dcb_on, u32 dcbx_enabled)
953 {
954         if (!CHIP_IS_E1x(bp)) {
955                 bp->dcb_state = dcb_on;
956                 bp->dcbx_enabled = dcbx_enabled;
957         } else {
958                 bp->dcb_state = false;
959                 bp->dcbx_enabled = BNX2X_DCBX_ENABLED_INVALID;
960         }
961         DP(BNX2X_MSG_DCB, "DCB state [%s:%s]\n",
962            dcb_on ? "ON" : "OFF",
963            dcbx_enabled == BNX2X_DCBX_ENABLED_OFF ? "user-mode" :
964            dcbx_enabled == BNX2X_DCBX_ENABLED_ON_NEG_OFF ? "on-chip static" :
965            dcbx_enabled == BNX2X_DCBX_ENABLED_ON_NEG_ON ?
966            "on-chip with negotiation" : "invalid");
967 }
968
969 void bnx2x_dcbx_init_params(struct bnx2x *bp)
970 {
971         bp->dcbx_config_params.admin_dcbx_version = 0x0; /* 0 - CEE; 1 - IEEE */
972         bp->dcbx_config_params.admin_ets_willing = 1;
973         bp->dcbx_config_params.admin_pfc_willing = 1;
974         bp->dcbx_config_params.overwrite_settings = 1;
975         bp->dcbx_config_params.admin_ets_enable = 1;
976         bp->dcbx_config_params.admin_pfc_enable = 1;
977         bp->dcbx_config_params.admin_tc_supported_tx_enable = 1;
978         bp->dcbx_config_params.admin_ets_configuration_tx_enable = 1;
979         bp->dcbx_config_params.admin_pfc_tx_enable = 1;
980         bp->dcbx_config_params.admin_application_priority_tx_enable = 1;
981         bp->dcbx_config_params.admin_ets_reco_valid = 1;
982         bp->dcbx_config_params.admin_app_priority_willing = 1;
983         bp->dcbx_config_params.admin_configuration_bw_precentage[0] = 100;
984         bp->dcbx_config_params.admin_configuration_bw_precentage[1] = 0;
985         bp->dcbx_config_params.admin_configuration_bw_precentage[2] = 0;
986         bp->dcbx_config_params.admin_configuration_bw_precentage[3] = 0;
987         bp->dcbx_config_params.admin_configuration_bw_precentage[4] = 0;
988         bp->dcbx_config_params.admin_configuration_bw_precentage[5] = 0;
989         bp->dcbx_config_params.admin_configuration_bw_precentage[6] = 0;
990         bp->dcbx_config_params.admin_configuration_bw_precentage[7] = 0;
991         bp->dcbx_config_params.admin_configuration_ets_pg[0] = 0;
992         bp->dcbx_config_params.admin_configuration_ets_pg[1] = 0;
993         bp->dcbx_config_params.admin_configuration_ets_pg[2] = 0;
994         bp->dcbx_config_params.admin_configuration_ets_pg[3] = 0;
995         bp->dcbx_config_params.admin_configuration_ets_pg[4] = 0;
996         bp->dcbx_config_params.admin_configuration_ets_pg[5] = 0;
997         bp->dcbx_config_params.admin_configuration_ets_pg[6] = 0;
998         bp->dcbx_config_params.admin_configuration_ets_pg[7] = 0;
999         bp->dcbx_config_params.admin_recommendation_bw_precentage[0] = 100;
1000         bp->dcbx_config_params.admin_recommendation_bw_precentage[1] = 0;
1001         bp->dcbx_config_params.admin_recommendation_bw_precentage[2] = 0;
1002         bp->dcbx_config_params.admin_recommendation_bw_precentage[3] = 0;
1003         bp->dcbx_config_params.admin_recommendation_bw_precentage[4] = 0;
1004         bp->dcbx_config_params.admin_recommendation_bw_precentage[5] = 0;
1005         bp->dcbx_config_params.admin_recommendation_bw_precentage[6] = 0;
1006         bp->dcbx_config_params.admin_recommendation_bw_precentage[7] = 0;
1007         bp->dcbx_config_params.admin_recommendation_ets_pg[0] = 0;
1008         bp->dcbx_config_params.admin_recommendation_ets_pg[1] = 1;
1009         bp->dcbx_config_params.admin_recommendation_ets_pg[2] = 2;
1010         bp->dcbx_config_params.admin_recommendation_ets_pg[3] = 3;
1011         bp->dcbx_config_params.admin_recommendation_ets_pg[4] = 4;
1012         bp->dcbx_config_params.admin_recommendation_ets_pg[5] = 5;
1013         bp->dcbx_config_params.admin_recommendation_ets_pg[6] = 6;
1014         bp->dcbx_config_params.admin_recommendation_ets_pg[7] = 7;
1015         bp->dcbx_config_params.admin_pfc_bitmap = 0x0;
1016         bp->dcbx_config_params.admin_priority_app_table[0].valid = 0;
1017         bp->dcbx_config_params.admin_priority_app_table[1].valid = 0;
1018         bp->dcbx_config_params.admin_priority_app_table[2].valid = 0;
1019         bp->dcbx_config_params.admin_priority_app_table[3].valid = 0;
1020         bp->dcbx_config_params.admin_default_priority = 0;
1021 }
1022
1023 void bnx2x_dcbx_init(struct bnx2x *bp, bool update_shmem)
1024 {
1025         u32 dcbx_lldp_params_offset = SHMEM_LLDP_DCBX_PARAMS_NONE;
1026
1027         /* only PMF can send ADMIN msg to MFW in old MFW versions */
1028         if ((!bp->port.pmf) && (!(bp->flags & BC_SUPPORTS_DCBX_MSG_NON_PMF)))
1029                 return;
1030
1031         if (bp->dcbx_enabled <= 0)
1032                 return;
1033
1034         /* validate:
1035          * chip of good for dcbx version,
1036          * dcb is wanted
1037          * shmem2 contains DCBX support fields
1038          */
1039         DP(BNX2X_MSG_DCB, "dcb_state %d bp->port.pmf %d\n",
1040            bp->dcb_state, bp->port.pmf);
1041
1042         if (bp->dcb_state == BNX2X_DCB_STATE_ON &&
1043             SHMEM2_HAS(bp, dcbx_lldp_params_offset)) {
1044                 dcbx_lldp_params_offset =
1045                         SHMEM2_RD(bp, dcbx_lldp_params_offset);
1046
1047                 DP(BNX2X_MSG_DCB, "dcbx_lldp_params_offset 0x%x\n",
1048                    dcbx_lldp_params_offset);
1049
1050                 bnx2x_update_drv_flags(bp, 1 << DRV_FLAGS_DCB_CONFIGURED, 0);
1051
1052                 if (SHMEM_LLDP_DCBX_PARAMS_NONE != dcbx_lldp_params_offset) {
1053                         /* need HW lock to avoid scenario of two drivers
1054                          * writing in parallel to shmem
1055                          */
1056                         bnx2x_acquire_hw_lock(bp,
1057                                               HW_LOCK_RESOURCE_DCBX_ADMIN_MIB);
1058                         if (update_shmem)
1059                                 bnx2x_dcbx_admin_mib_updated_params(bp,
1060                                         dcbx_lldp_params_offset);
1061
1062                         /* Let HW start negotiation */
1063                         bnx2x_fw_command(bp,
1064                                          DRV_MSG_CODE_DCBX_ADMIN_PMF_MSG, 0);
1065                         /* release HW lock only after MFW acks that it finished
1066                          * reading values from shmem
1067                          */
1068                         bnx2x_release_hw_lock(bp,
1069                                               HW_LOCK_RESOURCE_DCBX_ADMIN_MIB);
1070                 }
1071         }
1072 }
1073 static void
1074 bnx2x_dcbx_print_cos_params(struct bnx2x *bp,
1075                             struct bnx2x_func_tx_start_params *pfc_fw_cfg)
1076 {
1077         u8 pri = 0;
1078         u8 cos = 0;
1079
1080         DP(BNX2X_MSG_DCB,
1081            "pfc_fw_cfg->dcb_version %x\n", pfc_fw_cfg->dcb_version);
1082         DP(BNX2X_MSG_DCB,
1083            "pdev->params.dcbx_port_params.pfc.priority_non_pauseable_mask %x\n",
1084            bp->dcbx_port_params.pfc.priority_non_pauseable_mask);
1085
1086         for (cos = 0 ; cos < bp->dcbx_port_params.ets.num_of_cos ; cos++) {
1087                 DP(BNX2X_MSG_DCB,
1088                    "pdev->params.dcbx_port_params.ets.cos_params[%d].pri_bitmask %x\n",
1089                    cos, bp->dcbx_port_params.ets.cos_params[cos].pri_bitmask);
1090
1091                 DP(BNX2X_MSG_DCB,
1092                    "pdev->params.dcbx_port_params.ets.cos_params[%d].bw_tbl %x\n",
1093                    cos, bp->dcbx_port_params.ets.cos_params[cos].bw_tbl);
1094
1095                 DP(BNX2X_MSG_DCB,
1096                    "pdev->params.dcbx_port_params.ets.cos_params[%d].strict %x\n",
1097                    cos, bp->dcbx_port_params.ets.cos_params[cos].strict);
1098
1099                 DP(BNX2X_MSG_DCB,
1100                    "pdev->params.dcbx_port_params.ets.cos_params[%d].pauseable %x\n",
1101                    cos, bp->dcbx_port_params.ets.cos_params[cos].pauseable);
1102         }
1103
1104         for (pri = 0; pri < LLFC_DRIVER_TRAFFIC_TYPE_MAX; pri++) {
1105                 DP(BNX2X_MSG_DCB,
1106                    "pfc_fw_cfg->traffic_type_to_priority_cos[%d].priority %x\n",
1107                    pri, pfc_fw_cfg->traffic_type_to_priority_cos[pri].priority);
1108
1109                 DP(BNX2X_MSG_DCB,
1110                    "pfc_fw_cfg->traffic_type_to_priority_cos[%d].cos %x\n",
1111                    pri, pfc_fw_cfg->traffic_type_to_priority_cos[pri].cos);
1112         }
1113 }
1114
1115 /* fills help_data according to pg_info */
1116 static void bnx2x_dcbx_get_num_pg_traf_type(struct bnx2x *bp,
1117                                             u32 *pg_pri_orginal_spread,
1118                                             struct pg_help_data *help_data)
1119 {
1120         bool pg_found  = false;
1121         u32 i, traf_type, add_traf_type, add_pg;
1122         u32 *ttp = bp->dcbx_port_params.app.traffic_type_priority;
1123         struct pg_entry_help_data *data = help_data->data; /*shortcut*/
1124
1125         /* Set to invalid */
1126         for (i = 0; i < LLFC_DRIVER_TRAFFIC_TYPE_MAX; i++)
1127                 data[i].pg = DCBX_ILLEGAL_PG;
1128
1129         for (add_traf_type = 0;
1130              add_traf_type < LLFC_DRIVER_TRAFFIC_TYPE_MAX; add_traf_type++) {
1131                 pg_found = false;
1132                 if (ttp[add_traf_type] < MAX_PFC_PRIORITIES) {
1133                         add_pg = (u8)pg_pri_orginal_spread[ttp[add_traf_type]];
1134                         for (traf_type = 0;
1135                              traf_type < LLFC_DRIVER_TRAFFIC_TYPE_MAX;
1136                              traf_type++) {
1137                                 if (data[traf_type].pg == add_pg) {
1138                                         if (!(data[traf_type].pg_priority &
1139                                              (1 << ttp[add_traf_type])))
1140                                                 data[traf_type].
1141                                                         num_of_dif_pri++;
1142                                         data[traf_type].pg_priority |=
1143                                                 (1 << ttp[add_traf_type]);
1144                                         pg_found = true;
1145                                         break;
1146                                 }
1147                         }
1148                         if (false == pg_found) {
1149                                 data[help_data->num_of_pg].pg = add_pg;
1150                                 data[help_data->num_of_pg].pg_priority =
1151                                                 (1 << ttp[add_traf_type]);
1152                                 data[help_data->num_of_pg].num_of_dif_pri = 1;
1153                                 help_data->num_of_pg++;
1154                         }
1155                 }
1156                 DP(BNX2X_MSG_DCB,
1157                    "add_traf_type %d pg_found %s num_of_pg %d\n",
1158                    add_traf_type, (false == pg_found) ? "NO" : "YES",
1159                    help_data->num_of_pg);
1160         }
1161 }
1162
1163 static void bnx2x_dcbx_ets_disabled_entry_data(struct bnx2x *bp,
1164                                                struct cos_help_data *cos_data,
1165                                                u32 pri_join_mask)
1166 {
1167         /* Only one priority than only one COS */
1168         cos_data->data[0].pausable =
1169                 IS_DCBX_PFC_PRI_ONLY_PAUSE(bp, pri_join_mask);
1170         cos_data->data[0].pri_join_mask = pri_join_mask;
1171         cos_data->data[0].cos_bw = 100;
1172         cos_data->num_of_cos = 1;
1173 }
1174
1175 static inline void bnx2x_dcbx_add_to_cos_bw(struct bnx2x *bp,
1176                                             struct cos_entry_help_data *data,
1177                                             u8 pg_bw)
1178 {
1179         if (data->cos_bw == DCBX_INVALID_COS_BW)
1180                 data->cos_bw = pg_bw;
1181         else
1182                 data->cos_bw += pg_bw;
1183 }
1184
1185 static void bnx2x_dcbx_separate_pauseable_from_non(struct bnx2x *bp,
1186                         struct cos_help_data *cos_data,
1187                         u32 *pg_pri_orginal_spread,
1188                         struct dcbx_ets_feature *ets)
1189 {
1190         u32     pri_tested      = 0;
1191         u8      i               = 0;
1192         u8      entry           = 0;
1193         u8      pg_entry        = 0;
1194         u8      num_of_pri      = LLFC_DRIVER_TRAFFIC_TYPE_MAX;
1195
1196         cos_data->data[0].pausable = true;
1197         cos_data->data[1].pausable = false;
1198         cos_data->data[0].pri_join_mask = cos_data->data[1].pri_join_mask = 0;
1199
1200         for (i = 0 ; i < num_of_pri ; i++) {
1201                 pri_tested = 1 << bp->dcbx_port_params.
1202                                         app.traffic_type_priority[i];
1203
1204                 if (pri_tested & DCBX_PFC_PRI_NON_PAUSE_MASK(bp)) {
1205                         cos_data->data[1].pri_join_mask |= pri_tested;
1206                         entry = 1;
1207                 } else {
1208                         cos_data->data[0].pri_join_mask |= pri_tested;
1209                         entry = 0;
1210                 }
1211                 pg_entry = (u8)pg_pri_orginal_spread[bp->dcbx_port_params.
1212                                                 app.traffic_type_priority[i]];
1213                 /* There can be only one strict pg */
1214                 if (pg_entry < DCBX_MAX_NUM_PG_BW_ENTRIES)
1215                         bnx2x_dcbx_add_to_cos_bw(bp, &cos_data->data[entry],
1216                                 DCBX_PG_BW_GET(ets->pg_bw_tbl, pg_entry));
1217                 else
1218                         /* If we join a group and one is strict
1219                          * than the bw rules
1220                          */
1221                         cos_data->data[entry].strict =
1222                                                 BNX2X_DCBX_STRICT_COS_HIGHEST;
1223         }
1224         if ((0 == cos_data->data[0].pri_join_mask) &&
1225             (0 == cos_data->data[1].pri_join_mask))
1226                 BNX2X_ERR("dcbx error: Both groups must have priorities\n");
1227 }
1228
1229 #ifndef POWER_OF_2
1230 #define POWER_OF_2(x)   ((0 != x) && (0 == (x & (x-1))))
1231 #endif
1232
1233 static void bnx2x_dcbx_2cos_limit_cee_single_pg_to_cos_params(struct bnx2x *bp,
1234                                               struct pg_help_data *pg_help_data,
1235                                               struct cos_help_data *cos_data,
1236                                               u32 pri_join_mask,
1237                                               u8 num_of_dif_pri)
1238 {
1239         u8 i = 0;
1240         u32 pri_tested = 0;
1241         u32 pri_mask_without_pri = 0;
1242         u32 *ttp = bp->dcbx_port_params.app.traffic_type_priority;
1243         /*debug*/
1244         if (num_of_dif_pri == 1) {
1245                 bnx2x_dcbx_ets_disabled_entry_data(bp, cos_data, pri_join_mask);
1246                 return;
1247         }
1248         /* single priority group */
1249         if (pg_help_data->data[0].pg < DCBX_MAX_NUM_PG_BW_ENTRIES) {
1250                 /* If there are both pauseable and non-pauseable priorities,
1251                  * the pauseable priorities go to the first queue and
1252                  * the non-pauseable priorities go to the second queue.
1253                  */
1254                 if (IS_DCBX_PFC_PRI_MIX_PAUSE(bp, pri_join_mask)) {
1255                         /* Pauseable */
1256                         cos_data->data[0].pausable = true;
1257                         /* Non pauseable.*/
1258                         cos_data->data[1].pausable = false;
1259
1260                         if (2 == num_of_dif_pri) {
1261                                 cos_data->data[0].cos_bw = 50;
1262                                 cos_data->data[1].cos_bw = 50;
1263                         }
1264
1265                         if (3 == num_of_dif_pri) {
1266                                 if (POWER_OF_2(DCBX_PFC_PRI_GET_PAUSE(bp,
1267                                                         pri_join_mask))) {
1268                                         cos_data->data[0].cos_bw = 33;
1269                                         cos_data->data[1].cos_bw = 67;
1270                                 } else {
1271                                         cos_data->data[0].cos_bw = 67;
1272                                         cos_data->data[1].cos_bw = 33;
1273                                 }
1274                         }
1275
1276                 } else if (IS_DCBX_PFC_PRI_ONLY_PAUSE(bp, pri_join_mask)) {
1277                         /* If there are only pauseable priorities,
1278                          * then one/two priorities go to the first queue
1279                          * and one priority goes to the second queue.
1280                          */
1281                         if (2 == num_of_dif_pri) {
1282                                 cos_data->data[0].cos_bw = 50;
1283                                 cos_data->data[1].cos_bw = 50;
1284                         } else {
1285                                 cos_data->data[0].cos_bw = 67;
1286                                 cos_data->data[1].cos_bw = 33;
1287                         }
1288                         cos_data->data[1].pausable = true;
1289                         cos_data->data[0].pausable = true;
1290                         /* All priorities except FCOE */
1291                         cos_data->data[0].pri_join_mask = (pri_join_mask &
1292                                 ((u8)~(1 << ttp[LLFC_TRAFFIC_TYPE_FCOE])));
1293                         /* Only FCOE priority.*/
1294                         cos_data->data[1].pri_join_mask =
1295                                 (1 << ttp[LLFC_TRAFFIC_TYPE_FCOE]);
1296                 } else
1297                         /* If there are only non-pauseable priorities,
1298                          * they will all go to the same queue.
1299                          */
1300                         bnx2x_dcbx_ets_disabled_entry_data(bp,
1301                                                 cos_data, pri_join_mask);
1302         } else {
1303                 /* priority group which is not BW limited (PG#15):*/
1304                 if (IS_DCBX_PFC_PRI_MIX_PAUSE(bp, pri_join_mask)) {
1305                         /* If there are both pauseable and non-pauseable
1306                          * priorities, the pauseable priorities go to the first
1307                          * queue and the non-pauseable priorities
1308                          * go to the second queue.
1309                          */
1310                         if (DCBX_PFC_PRI_GET_PAUSE(bp, pri_join_mask) >
1311                             DCBX_PFC_PRI_GET_NON_PAUSE(bp, pri_join_mask)) {
1312                                 cos_data->data[0].strict =
1313                                         BNX2X_DCBX_STRICT_COS_HIGHEST;
1314                                 cos_data->data[1].strict =
1315                                         BNX2X_DCBX_STRICT_COS_NEXT_LOWER_PRI(
1316                                                 BNX2X_DCBX_STRICT_COS_HIGHEST);
1317                         } else {
1318                                 cos_data->data[0].strict =
1319                                         BNX2X_DCBX_STRICT_COS_NEXT_LOWER_PRI(
1320                                                 BNX2X_DCBX_STRICT_COS_HIGHEST);
1321                                 cos_data->data[1].strict =
1322                                         BNX2X_DCBX_STRICT_COS_HIGHEST;
1323                         }
1324                         /* Pauseable */
1325                         cos_data->data[0].pausable = true;
1326                         /* Non pause-able.*/
1327                         cos_data->data[1].pausable = false;
1328                 } else {
1329                         /* If there are only pauseable priorities or
1330                          * only non-pauseable,* the lower priorities go
1331                          * to the first queue and the higher priorities go
1332                          * to the second queue.
1333                          */
1334                         cos_data->data[0].pausable =
1335                                 cos_data->data[1].pausable =
1336                                 IS_DCBX_PFC_PRI_ONLY_PAUSE(bp, pri_join_mask);
1337
1338                         for (i = 0 ; i < LLFC_DRIVER_TRAFFIC_TYPE_MAX; i++) {
1339                                 pri_tested = 1 << bp->dcbx_port_params.
1340                                         app.traffic_type_priority[i];
1341                                 /* Remove priority tested */
1342                                 pri_mask_without_pri =
1343                                         (pri_join_mask & ((u8)(~pri_tested)));
1344                                 if (pri_mask_without_pri < pri_tested)
1345                                         break;
1346                         }
1347
1348                         if (i == LLFC_DRIVER_TRAFFIC_TYPE_MAX)
1349                                 BNX2X_ERR("Invalid value for pri_join_mask - could not find a priority\n");
1350
1351                         cos_data->data[0].pri_join_mask = pri_mask_without_pri;
1352                         cos_data->data[1].pri_join_mask = pri_tested;
1353                         /* Both queues are strict priority,
1354                          * and that with the highest priority
1355                          * gets the highest strict priority in the arbiter.
1356                          */
1357                         cos_data->data[0].strict =
1358                                         BNX2X_DCBX_STRICT_COS_NEXT_LOWER_PRI(
1359                                                 BNX2X_DCBX_STRICT_COS_HIGHEST);
1360                         cos_data->data[1].strict =
1361                                         BNX2X_DCBX_STRICT_COS_HIGHEST;
1362                 }
1363         }
1364 }
1365
1366 static void bnx2x_dcbx_2cos_limit_cee_two_pg_to_cos_params(
1367                             struct bnx2x                *bp,
1368                             struct  pg_help_data        *pg_help_data,
1369                             struct dcbx_ets_feature     *ets,
1370                             struct cos_help_data        *cos_data,
1371                             u32                 *pg_pri_orginal_spread,
1372                             u32                         pri_join_mask,
1373                             u8                          num_of_dif_pri)
1374 {
1375         u8 i = 0;
1376         u8 pg[DCBX_COS_MAX_NUM_E2] = { 0 };
1377
1378         /* If there are both pauseable and non-pauseable priorities,
1379          * the pauseable priorities go to the first queue and
1380          * the non-pauseable priorities go to the second queue.
1381          */
1382         if (IS_DCBX_PFC_PRI_MIX_PAUSE(bp, pri_join_mask)) {
1383                 if (IS_DCBX_PFC_PRI_MIX_PAUSE(bp,
1384                                          pg_help_data->data[0].pg_priority) ||
1385                     IS_DCBX_PFC_PRI_MIX_PAUSE(bp,
1386                                          pg_help_data->data[1].pg_priority)) {
1387                         /* If one PG contains both pauseable and
1388                          * non-pauseable priorities then ETS is disabled.
1389                          */
1390                         bnx2x_dcbx_separate_pauseable_from_non(bp, cos_data,
1391                                         pg_pri_orginal_spread, ets);
1392                         bp->dcbx_port_params.ets.enabled = false;
1393                         return;
1394                 }
1395
1396                 /* Pauseable */
1397                 cos_data->data[0].pausable = true;
1398                 /* Non pauseable. */
1399                 cos_data->data[1].pausable = false;
1400                 if (IS_DCBX_PFC_PRI_ONLY_PAUSE(bp,
1401                                 pg_help_data->data[0].pg_priority)) {
1402                         /* 0 is pauseable */
1403                         cos_data->data[0].pri_join_mask =
1404                                 pg_help_data->data[0].pg_priority;
1405                         pg[0] = pg_help_data->data[0].pg;
1406                         cos_data->data[1].pri_join_mask =
1407                                 pg_help_data->data[1].pg_priority;
1408                         pg[1] = pg_help_data->data[1].pg;
1409                 } else {/* 1 is pauseable */
1410                         cos_data->data[0].pri_join_mask =
1411                                 pg_help_data->data[1].pg_priority;
1412                         pg[0] = pg_help_data->data[1].pg;
1413                         cos_data->data[1].pri_join_mask =
1414                                 pg_help_data->data[0].pg_priority;
1415                         pg[1] = pg_help_data->data[0].pg;
1416                 }
1417         } else {
1418                 /* If there are only pauseable priorities or
1419                  * only non-pauseable, each PG goes to a queue.
1420                  */
1421                 cos_data->data[0].pausable = cos_data->data[1].pausable =
1422                         IS_DCBX_PFC_PRI_ONLY_PAUSE(bp, pri_join_mask);
1423                 cos_data->data[0].pri_join_mask =
1424                         pg_help_data->data[0].pg_priority;
1425                 pg[0] = pg_help_data->data[0].pg;
1426                 cos_data->data[1].pri_join_mask =
1427                         pg_help_data->data[1].pg_priority;
1428                 pg[1] = pg_help_data->data[1].pg;
1429         }
1430
1431         /* There can be only one strict pg */
1432         for (i = 0 ; i < ARRAY_SIZE(pg); i++) {
1433                 if (pg[i] < DCBX_MAX_NUM_PG_BW_ENTRIES)
1434                         cos_data->data[i].cos_bw =
1435                                 DCBX_PG_BW_GET(ets->pg_bw_tbl, pg[i]);
1436                 else
1437                         cos_data->data[i].strict =
1438                                                 BNX2X_DCBX_STRICT_COS_HIGHEST;
1439         }
1440 }
1441
1442 static int bnx2x_dcbx_join_pgs(
1443                               struct bnx2x            *bp,
1444                               struct dcbx_ets_feature *ets,
1445                               struct pg_help_data     *pg_help_data,
1446                               u8                      required_num_of_pg)
1447 {
1448         u8 entry_joined    = pg_help_data->num_of_pg - 1;
1449         u8 entry_removed   = entry_joined + 1;
1450         u8 pg_joined       = 0;
1451
1452         if (required_num_of_pg == 0 || ARRAY_SIZE(pg_help_data->data)
1453                                                 <= pg_help_data->num_of_pg) {
1454
1455                 BNX2X_ERR("required_num_of_pg can't be zero\n");
1456                 return -EINVAL;
1457         }
1458
1459         while (required_num_of_pg < pg_help_data->num_of_pg) {
1460                 entry_joined = pg_help_data->num_of_pg - 2;
1461                 entry_removed = entry_joined + 1;
1462                 /* protect index */
1463                 entry_removed %= ARRAY_SIZE(pg_help_data->data);
1464
1465                 pg_help_data->data[entry_joined].pg_priority |=
1466                         pg_help_data->data[entry_removed].pg_priority;
1467
1468                 pg_help_data->data[entry_joined].num_of_dif_pri +=
1469                         pg_help_data->data[entry_removed].num_of_dif_pri;
1470
1471                 if (pg_help_data->data[entry_joined].pg == DCBX_STRICT_PRI_PG ||
1472                     pg_help_data->data[entry_removed].pg == DCBX_STRICT_PRI_PG)
1473                         /* Entries joined strict priority rules */
1474                         pg_help_data->data[entry_joined].pg =
1475                                                         DCBX_STRICT_PRI_PG;
1476                 else {
1477                         /* Entries can be joined join BW */
1478                         pg_joined = DCBX_PG_BW_GET(ets->pg_bw_tbl,
1479                                         pg_help_data->data[entry_joined].pg) +
1480                                     DCBX_PG_BW_GET(ets->pg_bw_tbl,
1481                                         pg_help_data->data[entry_removed].pg);
1482
1483                         DCBX_PG_BW_SET(ets->pg_bw_tbl,
1484                                 pg_help_data->data[entry_joined].pg, pg_joined);
1485                 }
1486                 /* Joined the entries */
1487                 pg_help_data->num_of_pg--;
1488         }
1489
1490         return 0;
1491 }
1492
1493 static void bnx2x_dcbx_2cos_limit_cee_three_pg_to_cos_params(
1494                               struct bnx2x              *bp,
1495                               struct pg_help_data       *pg_help_data,
1496                               struct dcbx_ets_feature   *ets,
1497                               struct cos_help_data      *cos_data,
1498                               u32                       *pg_pri_orginal_spread,
1499                               u32                       pri_join_mask,
1500                               u8                        num_of_dif_pri)
1501 {
1502         u8 i = 0;
1503         u32 pri_tested = 0;
1504         u8 entry = 0;
1505         u8 pg_entry = 0;
1506         bool b_found_strict = false;
1507         u8 num_of_pri = LLFC_DRIVER_TRAFFIC_TYPE_MAX;
1508
1509         cos_data->data[0].pri_join_mask = cos_data->data[1].pri_join_mask = 0;
1510         /* If there are both pauseable and non-pauseable priorities,
1511          * the pauseable priorities go to the first queue and the
1512          * non-pauseable priorities go to the second queue.
1513          */
1514         if (IS_DCBX_PFC_PRI_MIX_PAUSE(bp, pri_join_mask))
1515                 bnx2x_dcbx_separate_pauseable_from_non(bp,
1516                                 cos_data, pg_pri_orginal_spread, ets);
1517         else {
1518                 /* If two BW-limited PG-s were combined to one queue,
1519                  * the BW is their sum.
1520                  *
1521                  * If there are only pauseable priorities or only non-pauseable,
1522                  * and there are both BW-limited and non-BW-limited PG-s,
1523                  * the BW-limited PG/s go to one queue and the non-BW-limited
1524                  * PG/s go to the second queue.
1525                  *
1526                  * If there are only pauseable priorities or only non-pauseable
1527                  * and all are BW limited, then two priorities go to the first
1528                  * queue and one priority goes to the second queue.
1529                  *
1530                  * We will join this two cases:
1531                  * if one is BW limited it will go to the second queue
1532                  * otherwise the last priority will get it
1533                  */
1534
1535                 cos_data->data[0].pausable = cos_data->data[1].pausable =
1536                         IS_DCBX_PFC_PRI_ONLY_PAUSE(bp, pri_join_mask);
1537
1538                 for (i = 0 ; i < num_of_pri; i++) {
1539                         pri_tested = 1 << bp->dcbx_port_params.
1540                                 app.traffic_type_priority[i];
1541                         pg_entry = (u8)pg_pri_orginal_spread[bp->
1542                                 dcbx_port_params.app.traffic_type_priority[i]];
1543
1544                         if (pg_entry < DCBX_MAX_NUM_PG_BW_ENTRIES) {
1545                                 entry = 0;
1546
1547                                 if (i == (num_of_pri-1) &&
1548                                     false == b_found_strict)
1549                                         /* last entry will be handled separately
1550                                          * If no priority is strict than last
1551                                          * entry goes to last queue.
1552                                          */
1553                                         entry = 1;
1554                                 cos_data->data[entry].pri_join_mask |=
1555                                                                 pri_tested;
1556                                 bnx2x_dcbx_add_to_cos_bw(bp,
1557                                         &cos_data->data[entry],
1558                                         DCBX_PG_BW_GET(ets->pg_bw_tbl,
1559                                                        pg_entry));
1560                         } else {
1561                                 b_found_strict = true;
1562                                 cos_data->data[1].pri_join_mask |= pri_tested;
1563                                 /* If we join a group and one is strict
1564                                  * than the bw rules
1565                                  */
1566                                 cos_data->data[1].strict =
1567                                         BNX2X_DCBX_STRICT_COS_HIGHEST;
1568                         }
1569                 }
1570         }
1571 }
1572
1573 static void bnx2x_dcbx_2cos_limit_cee_fill_cos_params(struct bnx2x *bp,
1574                                        struct pg_help_data *help_data,
1575                                        struct dcbx_ets_feature *ets,
1576                                        struct cos_help_data *cos_data,
1577                                        u32 *pg_pri_orginal_spread,
1578                                        u32 pri_join_mask,
1579                                        u8 num_of_dif_pri)
1580 {
1581         /* default E2 settings */
1582         cos_data->num_of_cos = DCBX_COS_MAX_NUM_E2;
1583
1584         switch (help_data->num_of_pg) {
1585         case 1:
1586                 bnx2x_dcbx_2cos_limit_cee_single_pg_to_cos_params(
1587                                                bp,
1588                                                help_data,
1589                                                cos_data,
1590                                                pri_join_mask,
1591                                                num_of_dif_pri);
1592                 break;
1593         case 2:
1594                 bnx2x_dcbx_2cos_limit_cee_two_pg_to_cos_params(
1595                                             bp,
1596                                             help_data,
1597                                             ets,
1598                                             cos_data,
1599                                             pg_pri_orginal_spread,
1600                                             pri_join_mask,
1601                                             num_of_dif_pri);
1602                 break;
1603
1604         case 3:
1605                 bnx2x_dcbx_2cos_limit_cee_three_pg_to_cos_params(
1606                                               bp,
1607                                               help_data,
1608                                               ets,
1609                                               cos_data,
1610                                               pg_pri_orginal_spread,
1611                                               pri_join_mask,
1612                                               num_of_dif_pri);
1613                 break;
1614         default:
1615                 BNX2X_ERR("Wrong pg_help_data.num_of_pg\n");
1616                 bnx2x_dcbx_ets_disabled_entry_data(bp,
1617                                                    cos_data, pri_join_mask);
1618         }
1619 }
1620
1621 static int bnx2x_dcbx_spread_strict_pri(struct bnx2x *bp,
1622                                         struct cos_help_data *cos_data,
1623                                         u8 entry,
1624                                         u8 num_spread_of_entries,
1625                                         u8 strict_app_pris)
1626 {
1627         u8 strict_pri = BNX2X_DCBX_STRICT_COS_HIGHEST;
1628         u8 num_of_app_pri = MAX_PFC_PRIORITIES;
1629         u8 app_pri_bit = 0;
1630
1631         while (num_spread_of_entries && num_of_app_pri > 0) {
1632                 app_pri_bit = 1 << (num_of_app_pri - 1);
1633                 if (app_pri_bit & strict_app_pris) {
1634                         struct cos_entry_help_data *data = &cos_data->
1635                                                                 data[entry];
1636                         num_spread_of_entries--;
1637                         if (num_spread_of_entries == 0) {
1638                                 /* last entry needed put all the entries left */
1639                                 data->cos_bw = DCBX_INVALID_COS_BW;
1640                                 data->strict = strict_pri;
1641                                 data->pri_join_mask = strict_app_pris;
1642                                 data->pausable = DCBX_IS_PFC_PRI_SOME_PAUSE(bp,
1643                                                         data->pri_join_mask);
1644                         } else {
1645                                 strict_app_pris &= ~app_pri_bit;
1646
1647                                 data->cos_bw = DCBX_INVALID_COS_BW;
1648                                 data->strict = strict_pri;
1649                                 data->pri_join_mask = app_pri_bit;
1650                                 data->pausable = DCBX_IS_PFC_PRI_SOME_PAUSE(bp,
1651                                                         data->pri_join_mask);
1652                         }
1653
1654                         strict_pri =
1655                             BNX2X_DCBX_STRICT_COS_NEXT_LOWER_PRI(strict_pri);
1656                         entry++;
1657                 }
1658
1659                 num_of_app_pri--;
1660         }
1661
1662         if (num_spread_of_entries) {
1663                 BNX2X_ERR("Didn't succeed to spread strict priorities\n");
1664                 return -EINVAL;
1665         }
1666
1667         return 0;
1668 }
1669
1670 static u8 bnx2x_dcbx_cee_fill_strict_pri(struct bnx2x *bp,
1671                                          struct cos_help_data *cos_data,
1672                                          u8 entry,
1673                                          u8 num_spread_of_entries,
1674                                          u8 strict_app_pris)
1675 {
1676         if (bnx2x_dcbx_spread_strict_pri(bp, cos_data, entry,
1677                                          num_spread_of_entries,
1678                                          strict_app_pris)) {
1679                 struct cos_entry_help_data *data = &cos_data->
1680                                                     data[entry];
1681                 /* Fill BW entry */
1682                 data->cos_bw = DCBX_INVALID_COS_BW;
1683                 data->strict = BNX2X_DCBX_STRICT_COS_HIGHEST;
1684                 data->pri_join_mask = strict_app_pris;
1685                 data->pausable = DCBX_IS_PFC_PRI_SOME_PAUSE(bp,
1686                                  data->pri_join_mask);
1687                 return 1;
1688         }
1689
1690         return num_spread_of_entries;
1691 }
1692
1693 static void bnx2x_dcbx_cee_fill_cos_params(struct bnx2x *bp,
1694                                            struct pg_help_data *help_data,
1695                                            struct dcbx_ets_feature *ets,
1696                                            struct cos_help_data *cos_data,
1697                                            u32 pri_join_mask)
1698
1699 {
1700         u8 need_num_of_entries = 0;
1701         u8 i = 0;
1702         u8 entry = 0;
1703
1704         /*
1705          * if the number of requested PG-s in CEE is greater than 3
1706          * then the results are not determined since this is a violation
1707          * of the standard.
1708          */
1709         if (help_data->num_of_pg > DCBX_COS_MAX_NUM_E3B0) {
1710                 if (bnx2x_dcbx_join_pgs(bp, ets, help_data,
1711                                         DCBX_COS_MAX_NUM_E3B0)) {
1712                         BNX2X_ERR("Unable to reduce the number of PGs - we will disables ETS\n");
1713                         bnx2x_dcbx_ets_disabled_entry_data(bp, cos_data,
1714                                                            pri_join_mask);
1715                         return;
1716                 }
1717         }
1718
1719         for (i = 0 ; i < help_data->num_of_pg; i++) {
1720                 struct pg_entry_help_data *pg =  &help_data->data[i];
1721                 if (pg->pg < DCBX_MAX_NUM_PG_BW_ENTRIES) {
1722                         struct cos_entry_help_data *data = &cos_data->
1723                                                             data[entry];
1724                         /* Fill BW entry */
1725                         data->cos_bw = DCBX_PG_BW_GET(ets->pg_bw_tbl, pg->pg);
1726                         data->strict = BNX2X_DCBX_STRICT_INVALID;
1727                         data->pri_join_mask = pg->pg_priority;
1728                         data->pausable = DCBX_IS_PFC_PRI_SOME_PAUSE(bp,
1729                                                 data->pri_join_mask);
1730
1731                         entry++;
1732                 } else {
1733                         need_num_of_entries =  min_t(u8,
1734                                 (u8)pg->num_of_dif_pri,
1735                                 (u8)DCBX_COS_MAX_NUM_E3B0 -
1736                                                  help_data->num_of_pg + 1);
1737                         /*
1738                          * If there are still VOQ-s which have no associated PG,
1739                          * then associate these VOQ-s to PG15. These PG-s will
1740                          * be used for SP between priorities on PG15.
1741                          */
1742                         entry += bnx2x_dcbx_cee_fill_strict_pri(bp, cos_data,
1743                                 entry, need_num_of_entries, pg->pg_priority);
1744                 }
1745         }
1746
1747         /* the entry will represent the number of COSes used */
1748         cos_data->num_of_cos = entry;
1749 }
1750 static void bnx2x_dcbx_fill_cos_params(struct bnx2x *bp,
1751                                        struct pg_help_data *help_data,
1752                                        struct dcbx_ets_feature *ets,
1753                                        u32 *pg_pri_orginal_spread)
1754 {
1755         struct cos_help_data         cos_data;
1756         u8                    i                           = 0;
1757         u32                   pri_join_mask               = 0;
1758         u8                    num_of_dif_pri              = 0;
1759
1760         memset(&cos_data, 0, sizeof(cos_data));
1761
1762         /* Validate the pg value */
1763         for (i = 0; i < help_data->num_of_pg ; i++) {
1764                 if (DCBX_STRICT_PRIORITY != help_data->data[i].pg &&
1765                     DCBX_MAX_NUM_PG_BW_ENTRIES <= help_data->data[i].pg)
1766                         BNX2X_ERR("Invalid pg[%d] data %x\n", i,
1767                                   help_data->data[i].pg);
1768                 pri_join_mask   |=  help_data->data[i].pg_priority;
1769                 num_of_dif_pri  += help_data->data[i].num_of_dif_pri;
1770         }
1771
1772         /* defaults */
1773         cos_data.num_of_cos = 1;
1774         for (i = 0; i < ARRAY_SIZE(cos_data.data); i++) {
1775                 cos_data.data[i].pri_join_mask = 0;
1776                 cos_data.data[i].pausable = false;
1777                 cos_data.data[i].strict = BNX2X_DCBX_STRICT_INVALID;
1778                 cos_data.data[i].cos_bw = DCBX_INVALID_COS_BW;
1779         }
1780
1781         if (CHIP_IS_E3B0(bp))
1782                 bnx2x_dcbx_cee_fill_cos_params(bp, help_data, ets,
1783                                                &cos_data, pri_join_mask);
1784         else /* E2 + E3A0 */
1785                 bnx2x_dcbx_2cos_limit_cee_fill_cos_params(bp,
1786                                                           help_data, ets,
1787                                                           &cos_data,
1788                                                           pg_pri_orginal_spread,
1789                                                           pri_join_mask,
1790                                                           num_of_dif_pri);
1791
1792         for (i = 0; i < cos_data.num_of_cos ; i++) {
1793                 struct bnx2x_dcbx_cos_params *p =
1794                         &bp->dcbx_port_params.ets.cos_params[i];
1795
1796                 p->strict = cos_data.data[i].strict;
1797                 p->bw_tbl = cos_data.data[i].cos_bw;
1798                 p->pri_bitmask = cos_data.data[i].pri_join_mask;
1799                 p->pauseable = cos_data.data[i].pausable;
1800
1801                 /* sanity */
1802                 if (p->bw_tbl != DCBX_INVALID_COS_BW ||
1803                     p->strict != BNX2X_DCBX_STRICT_INVALID) {
1804                         if (p->pri_bitmask == 0)
1805                                 BNX2X_ERR("Invalid pri_bitmask for %d\n", i);
1806
1807                         if (CHIP_IS_E2(bp) || CHIP_IS_E3A0(bp)) {
1808
1809                                 if (p->pauseable &&
1810                                     DCBX_PFC_PRI_GET_NON_PAUSE(bp,
1811                                                 p->pri_bitmask) != 0)
1812                                         BNX2X_ERR("Inconsistent config for pausable COS %d\n",
1813                                                   i);
1814
1815                                 if (!p->pauseable &&
1816                                     DCBX_PFC_PRI_GET_PAUSE(bp,
1817                                                 p->pri_bitmask) != 0)
1818                                         BNX2X_ERR("Inconsistent config for nonpausable COS %d\n",
1819                                                   i);
1820                         }
1821                 }
1822
1823                 if (p->pauseable)
1824                         DP(BNX2X_MSG_DCB, "COS %d PAUSABLE prijoinmask 0x%x\n",
1825                                   i, cos_data.data[i].pri_join_mask);
1826                 else
1827                         DP(BNX2X_MSG_DCB,
1828                            "COS %d NONPAUSABLE prijoinmask 0x%x\n",
1829                            i, cos_data.data[i].pri_join_mask);
1830         }
1831
1832         bp->dcbx_port_params.ets.num_of_cos = cos_data.num_of_cos ;
1833 }
1834
1835 static void bnx2x_dcbx_get_ets_pri_pg_tbl(struct bnx2x *bp,
1836                                 u32 *set_configuration_ets_pg,
1837                                 u32 *pri_pg_tbl)
1838 {
1839         int i;
1840
1841         for (i = 0; i < DCBX_MAX_NUM_PRI_PG_ENTRIES; i++) {
1842                 set_configuration_ets_pg[i] = DCBX_PRI_PG_GET(pri_pg_tbl, i);
1843
1844                 DP(BNX2X_MSG_DCB, "set_configuration_ets_pg[%d] = 0x%x\n",
1845                    i, set_configuration_ets_pg[i]);
1846         }
1847 }
1848
1849 static void bnx2x_dcbx_fw_struct(struct bnx2x *bp,
1850                                  struct bnx2x_func_tx_start_params *pfc_fw_cfg)
1851 {
1852         u16 pri_bit = 0;
1853         u8 cos = 0, pri = 0;
1854         struct priority_cos *tt2cos;
1855         u32 *ttp = bp->dcbx_port_params.app.traffic_type_priority;
1856         int mfw_configured = SHMEM2_HAS(bp, drv_flags) &&
1857                              GET_FLAGS(SHMEM2_RD(bp, drv_flags),
1858                                        1 << DRV_FLAGS_DCB_MFW_CONFIGURED);
1859
1860         memset(pfc_fw_cfg, 0, sizeof(*pfc_fw_cfg));
1861
1862         /* to disable DCB - the structure must be zeroed */
1863         if ((bp->dcbx_error & DCBX_REMOTE_MIB_ERROR) && !mfw_configured)
1864                 return;
1865
1866         /*shortcut*/
1867         tt2cos = pfc_fw_cfg->traffic_type_to_priority_cos;
1868
1869         /* Fw version should be incremented each update */
1870         pfc_fw_cfg->dcb_version = ++bp->dcb_version;
1871         pfc_fw_cfg->dcb_enabled = 1;
1872
1873         /* Fill priority parameters */
1874         for (pri = 0; pri < LLFC_DRIVER_TRAFFIC_TYPE_MAX; pri++) {
1875                 tt2cos[pri].priority = ttp[pri];
1876                 pri_bit = 1 << tt2cos[pri].priority;
1877
1878                 /* Fill COS parameters based on COS calculated to
1879                  * make it more general for future use */
1880                 for (cos = 0; cos < bp->dcbx_port_params.ets.num_of_cos; cos++)
1881                         if (bp->dcbx_port_params.ets.cos_params[cos].
1882                                                 pri_bitmask & pri_bit)
1883                                         tt2cos[pri].cos = cos;
1884
1885                 pfc_fw_cfg->dcb_outer_pri[pri]  = ttp[pri];
1886         }
1887
1888         /* we never want the FW to add a 0 vlan tag */
1889         pfc_fw_cfg->dont_add_pri_0_en = 1;
1890
1891         bnx2x_dcbx_print_cos_params(bp, pfc_fw_cfg);
1892 }
1893
1894 void bnx2x_dcbx_pmf_update(struct bnx2x *bp)
1895 {
1896         /* if we need to synchronize DCBX result from prev PMF
1897          * read it from shmem and update bp and netdev accordingly
1898          */
1899         if (SHMEM2_HAS(bp, drv_flags) &&
1900            GET_FLAGS(SHMEM2_RD(bp, drv_flags), 1 << DRV_FLAGS_DCB_CONFIGURED)) {
1901                 /* Read neg results if dcbx is in the FW */
1902                 if (bnx2x_dcbx_read_shmem_neg_results(bp))
1903                         return;
1904
1905                 bnx2x_dump_dcbx_drv_param(bp, &bp->dcbx_local_feat,
1906                                           bp->dcbx_error);
1907                 bnx2x_get_dcbx_drv_param(bp, &bp->dcbx_local_feat,
1908                                          bp->dcbx_error);
1909 #ifdef BCM_DCBNL
1910                 /*
1911                  * Add new app tlvs to dcbnl
1912                  */
1913                 bnx2x_dcbnl_update_applist(bp, false);
1914                 /*
1915                  * Send a notification for the new negotiated parameters
1916                  */
1917                 dcbnl_cee_notify(bp->dev, RTM_GETDCB, DCB_CMD_CEE_GET, 0, 0);
1918 #endif
1919                 /*
1920                  * reconfigure the netdevice with the results of the new
1921                  * dcbx negotiation.
1922                  */
1923                 bnx2x_dcbx_update_tc_mapping(bp);
1924         }
1925 }
1926
1927 /* DCB netlink */
1928 #ifdef BCM_DCBNL
1929
1930 #define BNX2X_DCBX_CAPS         (DCB_CAP_DCBX_LLD_MANAGED | \
1931                                 DCB_CAP_DCBX_VER_CEE | DCB_CAP_DCBX_STATIC)
1932
1933 static inline bool bnx2x_dcbnl_set_valid(struct bnx2x *bp)
1934 {
1935         /* validate dcbnl call that may change HW state:
1936          * DCB is on and DCBX mode was SUCCESSFULLY set by the user.
1937          */
1938         return bp->dcb_state && bp->dcbx_mode_uset;
1939 }
1940
1941 static u8 bnx2x_dcbnl_get_state(struct net_device *netdev)
1942 {
1943         struct bnx2x *bp = netdev_priv(netdev);
1944         DP(BNX2X_MSG_DCB, "state = %d\n", bp->dcb_state);
1945         return bp->dcb_state;
1946 }
1947
1948 static u8 bnx2x_dcbnl_set_state(struct net_device *netdev, u8 state)
1949 {
1950         struct bnx2x *bp = netdev_priv(netdev);
1951         DP(BNX2X_MSG_DCB, "state = %s\n", state ? "on" : "off");
1952
1953         /* Fail to set state to "enabled" if dcbx is disabled in nvram */
1954         if (state && ((bp->dcbx_enabled == BNX2X_DCBX_ENABLED_OFF) ||
1955                       (bp->dcbx_enabled == BNX2X_DCBX_ENABLED_INVALID))) {
1956                 DP(BNX2X_MSG_DCB, "Can not set dcbx to enabled while it is disabled in nvm\n");
1957                 return 1;
1958         }
1959
1960         bnx2x_dcbx_set_state(bp, (state ? true : false), bp->dcbx_enabled);
1961         return 0;
1962 }
1963
1964 static void bnx2x_dcbnl_get_perm_hw_addr(struct net_device *netdev,
1965                                          u8 *perm_addr)
1966 {
1967         struct bnx2x *bp = netdev_priv(netdev);
1968         DP(BNX2X_MSG_DCB, "GET-PERM-ADDR\n");
1969
1970         /* first the HW mac address */
1971         memcpy(perm_addr, netdev->dev_addr, netdev->addr_len);
1972
1973         if (CNIC_LOADED(bp))
1974                 /* second SAN address */
1975                 memcpy(perm_addr+netdev->addr_len, bp->fip_mac,
1976                        netdev->addr_len);
1977 }
1978
1979 static void bnx2x_dcbnl_set_pg_tccfg_tx(struct net_device *netdev, int prio,
1980                                         u8 prio_type, u8 pgid, u8 bw_pct,
1981                                         u8 up_map)
1982 {
1983         struct bnx2x *bp = netdev_priv(netdev);
1984
1985         DP(BNX2X_MSG_DCB, "prio[%d] = %d\n", prio, pgid);
1986         if (!bnx2x_dcbnl_set_valid(bp) || prio >= DCBX_MAX_NUM_PRI_PG_ENTRIES)
1987                 return;
1988
1989         /**
1990          * bw_pct ignored -     band-width percentage devision between user
1991          *                      priorities within the same group is not
1992          *                      standard and hence not supported
1993          *
1994          * prio_type ignored -  priority levels within the same group are not
1995          *                      standard and hence are not supported. According
1996          *                      to the standard pgid 15 is dedicated to strict
1997          *                      priority traffic (on the port level).
1998          *
1999          * up_map ignored
2000          */
2001
2002         bp->dcbx_config_params.admin_configuration_ets_pg[prio] = pgid;
2003         bp->dcbx_config_params.admin_ets_configuration_tx_enable = 1;
2004 }
2005
2006 static void bnx2x_dcbnl_set_pg_bwgcfg_tx(struct net_device *netdev,
2007                                          int pgid, u8 bw_pct)
2008 {
2009         struct bnx2x *bp = netdev_priv(netdev);
2010         DP(BNX2X_MSG_DCB, "pgid[%d] = %d\n", pgid, bw_pct);
2011
2012         if (!bnx2x_dcbnl_set_valid(bp) || pgid >= DCBX_MAX_NUM_PG_BW_ENTRIES)
2013                 return;
2014
2015         bp->dcbx_config_params.admin_configuration_bw_precentage[pgid] = bw_pct;
2016         bp->dcbx_config_params.admin_ets_configuration_tx_enable = 1;
2017 }
2018
2019 static void bnx2x_dcbnl_set_pg_tccfg_rx(struct net_device *netdev, int prio,
2020                                         u8 prio_type, u8 pgid, u8 bw_pct,
2021                                         u8 up_map)
2022 {
2023         struct bnx2x *bp = netdev_priv(netdev);
2024         DP(BNX2X_MSG_DCB, "Nothing to set; No RX support\n");
2025 }
2026
2027 static void bnx2x_dcbnl_set_pg_bwgcfg_rx(struct net_device *netdev,
2028                                          int pgid, u8 bw_pct)
2029 {
2030         struct bnx2x *bp = netdev_priv(netdev);
2031         DP(BNX2X_MSG_DCB, "Nothing to set; No RX support\n");
2032 }
2033
2034 static void bnx2x_dcbnl_get_pg_tccfg_tx(struct net_device *netdev, int prio,
2035                                         u8 *prio_type, u8 *pgid, u8 *bw_pct,
2036                                         u8 *up_map)
2037 {
2038         struct bnx2x *bp = netdev_priv(netdev);
2039         DP(BNX2X_MSG_DCB, "prio = %d\n", prio);
2040
2041         /**
2042          * bw_pct ignored -     band-width percentage devision between user
2043          *                      priorities within the same group is not
2044          *                      standard and hence not supported
2045          *
2046          * prio_type ignored -  priority levels within the same group are not
2047          *                      standard and hence are not supported. According
2048          *                      to the standard pgid 15 is dedicated to strict
2049          *                      priority traffic (on the port level).
2050          *
2051          * up_map ignored
2052          */
2053         *up_map = *bw_pct = *prio_type = *pgid = 0;
2054
2055         if (!bp->dcb_state || prio >= DCBX_MAX_NUM_PRI_PG_ENTRIES)
2056                 return;
2057
2058         *pgid = DCBX_PRI_PG_GET(bp->dcbx_local_feat.ets.pri_pg_tbl, prio);
2059 }
2060
2061 static void bnx2x_dcbnl_get_pg_bwgcfg_tx(struct net_device *netdev,
2062                                          int pgid, u8 *bw_pct)
2063 {
2064         struct bnx2x *bp = netdev_priv(netdev);
2065         DP(BNX2X_MSG_DCB, "pgid = %d\n", pgid);
2066
2067         *bw_pct = 0;
2068
2069         if (!bp->dcb_state || pgid >= DCBX_MAX_NUM_PG_BW_ENTRIES)
2070                 return;
2071
2072         *bw_pct = DCBX_PG_BW_GET(bp->dcbx_local_feat.ets.pg_bw_tbl, pgid);
2073 }
2074
2075 static void bnx2x_dcbnl_get_pg_tccfg_rx(struct net_device *netdev, int prio,
2076                                         u8 *prio_type, u8 *pgid, u8 *bw_pct,
2077                                         u8 *up_map)
2078 {
2079         struct bnx2x *bp = netdev_priv(netdev);
2080         DP(BNX2X_MSG_DCB, "Nothing to get; No RX support\n");
2081
2082         *prio_type = *pgid = *bw_pct = *up_map = 0;
2083 }
2084
2085 static void bnx2x_dcbnl_get_pg_bwgcfg_rx(struct net_device *netdev,
2086                                          int pgid, u8 *bw_pct)
2087 {
2088         struct bnx2x *bp = netdev_priv(netdev);
2089         DP(BNX2X_MSG_DCB, "Nothing to get; No RX support\n");
2090
2091         *bw_pct = 0;
2092 }
2093
2094 static void bnx2x_dcbnl_set_pfc_cfg(struct net_device *netdev, int prio,
2095                                     u8 setting)
2096 {
2097         struct bnx2x *bp = netdev_priv(netdev);
2098         DP(BNX2X_MSG_DCB, "prio[%d] = %d\n", prio, setting);
2099
2100         if (!bnx2x_dcbnl_set_valid(bp) || prio >= MAX_PFC_PRIORITIES)
2101                 return;
2102
2103         if (setting) {
2104                 bp->dcbx_config_params.admin_pfc_bitmap |= (1 << prio);
2105                 bp->dcbx_config_params.admin_pfc_tx_enable = 1;
2106         } else {
2107                 bp->dcbx_config_params.admin_pfc_bitmap &= ~(1 << prio);
2108         }
2109 }
2110
2111 static void bnx2x_dcbnl_get_pfc_cfg(struct net_device *netdev, int prio,
2112                                     u8 *setting)
2113 {
2114         struct bnx2x *bp = netdev_priv(netdev);
2115         DP(BNX2X_MSG_DCB, "prio = %d\n", prio);
2116
2117         *setting = 0;
2118
2119         if (!bp->dcb_state || prio >= MAX_PFC_PRIORITIES)
2120                 return;
2121
2122         *setting = (bp->dcbx_local_feat.pfc.pri_en_bitmap >> prio) & 0x1;
2123 }
2124
2125 static u8 bnx2x_dcbnl_set_all(struct net_device *netdev)
2126 {
2127         struct bnx2x *bp = netdev_priv(netdev);
2128
2129         DP(BNX2X_MSG_DCB, "SET-ALL\n");
2130
2131         if (!bnx2x_dcbnl_set_valid(bp))
2132                 return 1;
2133
2134         if (bp->recovery_state != BNX2X_RECOVERY_DONE) {
2135                 netdev_err(bp->dev,
2136                            "Handling parity error recovery. Try again later\n");
2137                 return 1;
2138         }
2139         if (netif_running(bp->dev)) {
2140                 bnx2x_update_drv_flags(bp,
2141                                        1 << DRV_FLAGS_DCB_MFW_CONFIGURED,
2142                                        1);
2143                 bnx2x_dcbx_init(bp, true);
2144         }
2145         DP(BNX2X_MSG_DCB, "set_dcbx_params done\n");
2146
2147         return 0;
2148 }
2149
2150 static u8 bnx2x_dcbnl_get_cap(struct net_device *netdev, int capid, u8 *cap)
2151 {
2152         struct bnx2x *bp = netdev_priv(netdev);
2153         u8 rval = 0;
2154
2155         if (bp->dcb_state) {
2156                 switch (capid) {
2157                 case DCB_CAP_ATTR_PG:
2158                         *cap = true;
2159                         break;
2160                 case DCB_CAP_ATTR_PFC:
2161                         *cap = true;
2162                         break;
2163                 case DCB_CAP_ATTR_UP2TC:
2164                         *cap = false;
2165                         break;
2166                 case DCB_CAP_ATTR_PG_TCS:
2167                         *cap = 0x80;    /* 8 priorities for PGs */
2168                         break;
2169                 case DCB_CAP_ATTR_PFC_TCS:
2170                         *cap = 0x80;    /* 8 priorities for PFC */
2171                         break;
2172                 case DCB_CAP_ATTR_GSP:
2173                         *cap = true;
2174                         break;
2175                 case DCB_CAP_ATTR_BCN:
2176                         *cap = false;
2177                         break;
2178                 case DCB_CAP_ATTR_DCBX:
2179                         *cap = BNX2X_DCBX_CAPS;
2180                         break;
2181                 default:
2182                         BNX2X_ERR("Non valid capability ID\n");
2183                         rval = 1;
2184                         break;
2185                 }
2186         } else {
2187                 DP(BNX2X_MSG_DCB, "DCB disabled\n");
2188                 rval = 1;
2189         }
2190
2191         DP(BNX2X_MSG_DCB, "capid %d:%x\n", capid, *cap);
2192         return rval;
2193 }
2194
2195 static int bnx2x_dcbnl_get_numtcs(struct net_device *netdev, int tcid, u8 *num)
2196 {
2197         struct bnx2x *bp = netdev_priv(netdev);
2198         u8 rval = 0;
2199
2200         DP(BNX2X_MSG_DCB, "tcid %d\n", tcid);
2201
2202         if (bp->dcb_state) {
2203                 switch (tcid) {
2204                 case DCB_NUMTCS_ATTR_PG:
2205                         *num = CHIP_IS_E3B0(bp) ? DCBX_COS_MAX_NUM_E3B0 :
2206                                                   DCBX_COS_MAX_NUM_E2;
2207                         break;
2208                 case DCB_NUMTCS_ATTR_PFC:
2209                         *num = CHIP_IS_E3B0(bp) ? DCBX_COS_MAX_NUM_E3B0 :
2210                                                   DCBX_COS_MAX_NUM_E2;
2211                         break;
2212                 default:
2213                         BNX2X_ERR("Non valid TC-ID\n");
2214                         rval = 1;
2215                         break;
2216                 }
2217         } else {
2218                 DP(BNX2X_MSG_DCB, "DCB disabled\n");
2219                 rval = 1;
2220         }
2221
2222         return rval;
2223 }
2224
2225 static int bnx2x_dcbnl_set_numtcs(struct net_device *netdev, int tcid, u8 num)
2226 {
2227         struct bnx2x *bp = netdev_priv(netdev);
2228         DP(BNX2X_MSG_DCB, "num tcs = %d; Not supported\n", num);
2229         return -EINVAL;
2230 }
2231
2232 static u8 bnx2x_dcbnl_get_pfc_state(struct net_device *netdev)
2233 {
2234         struct bnx2x *bp = netdev_priv(netdev);
2235         DP(BNX2X_MSG_DCB, "state = %d\n", bp->dcbx_local_feat.pfc.enabled);
2236
2237         if (!bp->dcb_state)
2238                 return 0;
2239
2240         return bp->dcbx_local_feat.pfc.enabled;
2241 }
2242
2243 static void bnx2x_dcbnl_set_pfc_state(struct net_device *netdev, u8 state)
2244 {
2245         struct bnx2x *bp = netdev_priv(netdev);
2246         DP(BNX2X_MSG_DCB, "state = %s\n", state ? "on" : "off");
2247
2248         if (!bnx2x_dcbnl_set_valid(bp))
2249                 return;
2250
2251         bp->dcbx_config_params.admin_pfc_tx_enable =
2252         bp->dcbx_config_params.admin_pfc_enable = (state ? 1 : 0);
2253 }
2254
2255 static void bnx2x_admin_app_set_ent(
2256         struct bnx2x_admin_priority_app_table *app_ent,
2257         u8 idtype, u16 idval, u8 up)
2258 {
2259         app_ent->valid = 1;
2260
2261         switch (idtype) {
2262         case DCB_APP_IDTYPE_ETHTYPE:
2263                 app_ent->traffic_type = TRAFFIC_TYPE_ETH;
2264                 break;
2265         case DCB_APP_IDTYPE_PORTNUM:
2266                 app_ent->traffic_type = TRAFFIC_TYPE_PORT;
2267                 break;
2268         default:
2269                 break; /* never gets here */
2270         }
2271         app_ent->app_id = idval;
2272         app_ent->priority = up;
2273 }
2274
2275 static bool bnx2x_admin_app_is_equal(
2276         struct bnx2x_admin_priority_app_table *app_ent,
2277         u8 idtype, u16 idval)
2278 {
2279         if (!app_ent->valid)
2280                 return false;
2281
2282         switch (idtype) {
2283         case DCB_APP_IDTYPE_ETHTYPE:
2284                 if (app_ent->traffic_type != TRAFFIC_TYPE_ETH)
2285                         return false;
2286                 break;
2287         case DCB_APP_IDTYPE_PORTNUM:
2288                 if (app_ent->traffic_type != TRAFFIC_TYPE_PORT)
2289                         return false;
2290                 break;
2291         default:
2292                 return false;
2293         }
2294         if (app_ent->app_id != idval)
2295                 return false;
2296
2297         return true;
2298 }
2299
2300 static int bnx2x_set_admin_app_up(struct bnx2x *bp, u8 idtype, u16 idval, u8 up)
2301 {
2302         int i, ff;
2303
2304         /* iterate over the app entries looking for idtype and idval */
2305         for (i = 0, ff = -1; i < DCBX_CONFIG_MAX_APP_PROTOCOL; i++) {
2306                 struct bnx2x_admin_priority_app_table *app_ent =
2307                         &bp->dcbx_config_params.admin_priority_app_table[i];
2308                 if (bnx2x_admin_app_is_equal(app_ent, idtype, idval))
2309                         break;
2310
2311                 if (ff < 0 && !app_ent->valid)
2312                         ff = i;
2313         }
2314         if (i < DCBX_CONFIG_MAX_APP_PROTOCOL)
2315                 /* if found overwrite up */
2316                 bp->dcbx_config_params.
2317                         admin_priority_app_table[i].priority = up;
2318         else if (ff >= 0)
2319                 /* not found use first-free */
2320                 bnx2x_admin_app_set_ent(
2321                         &bp->dcbx_config_params.admin_priority_app_table[ff],
2322                         idtype, idval, up);
2323         else {
2324                 /* app table is full */
2325                 BNX2X_ERR("Application table is too large\n");
2326                 return -EBUSY;
2327         }
2328
2329         /* up configured, if not 0 make sure feature is enabled */
2330         if (up)
2331                 bp->dcbx_config_params.admin_application_priority_tx_enable = 1;
2332
2333         return 0;
2334 }
2335
2336 static int bnx2x_dcbnl_set_app_up(struct net_device *netdev, u8 idtype,
2337                                   u16 idval, u8 up)
2338 {
2339         struct bnx2x *bp = netdev_priv(netdev);
2340
2341         DP(BNX2X_MSG_DCB, "app_type %d, app_id %x, prio bitmap %d\n",
2342            idtype, idval, up);
2343
2344         if (!bnx2x_dcbnl_set_valid(bp)) {
2345                 DP(BNX2X_MSG_DCB, "dcbnl call not valid\n");
2346                 return -EINVAL;
2347         }
2348
2349         /* verify idtype */
2350         switch (idtype) {
2351         case DCB_APP_IDTYPE_ETHTYPE:
2352         case DCB_APP_IDTYPE_PORTNUM:
2353                 break;
2354         default:
2355                 DP(BNX2X_MSG_DCB, "Wrong ID type\n");
2356                 return -EINVAL;
2357         }
2358         return bnx2x_set_admin_app_up(bp, idtype, idval, up);
2359 }
2360
2361 static u8 bnx2x_dcbnl_get_dcbx(struct net_device *netdev)
2362 {
2363         struct bnx2x *bp = netdev_priv(netdev);
2364         u8 state;
2365
2366         state = DCB_CAP_DCBX_LLD_MANAGED | DCB_CAP_DCBX_VER_CEE;
2367
2368         if (bp->dcbx_enabled == BNX2X_DCBX_ENABLED_ON_NEG_OFF)
2369                 state |= DCB_CAP_DCBX_STATIC;
2370
2371         return state;
2372 }
2373
2374 static u8 bnx2x_dcbnl_set_dcbx(struct net_device *netdev, u8 state)
2375 {
2376         struct bnx2x *bp = netdev_priv(netdev);
2377         DP(BNX2X_MSG_DCB, "state = %02x\n", state);
2378
2379         /* set dcbx mode */
2380
2381         if ((state & BNX2X_DCBX_CAPS) != state) {
2382                 BNX2X_ERR("Requested DCBX mode %x is beyond advertised capabilities\n",
2383                           state);
2384                 return 1;
2385         }
2386
2387         if (bp->dcb_state != BNX2X_DCB_STATE_ON) {
2388                 BNX2X_ERR("DCB turned off, DCBX configuration is invalid\n");
2389                 return 1;
2390         }
2391
2392         if (state & DCB_CAP_DCBX_STATIC)
2393                 bp->dcbx_enabled = BNX2X_DCBX_ENABLED_ON_NEG_OFF;
2394         else
2395                 bp->dcbx_enabled = BNX2X_DCBX_ENABLED_ON_NEG_ON;
2396
2397         bp->dcbx_mode_uset = true;
2398         return 0;
2399 }
2400
2401 static u8 bnx2x_dcbnl_get_featcfg(struct net_device *netdev, int featid,
2402                                   u8 *flags)
2403 {
2404         struct bnx2x *bp = netdev_priv(netdev);
2405         u8 rval = 0;
2406
2407         DP(BNX2X_MSG_DCB, "featid %d\n", featid);
2408
2409         if (bp->dcb_state) {
2410                 *flags = 0;
2411                 switch (featid) {
2412                 case DCB_FEATCFG_ATTR_PG:
2413                         if (bp->dcbx_local_feat.ets.enabled)
2414                                 *flags |= DCB_FEATCFG_ENABLE;
2415                         if (bp->dcbx_error & (DCBX_LOCAL_ETS_ERROR |
2416                                               DCBX_REMOTE_MIB_ERROR))
2417                                 *flags |= DCB_FEATCFG_ERROR;
2418                         break;
2419                 case DCB_FEATCFG_ATTR_PFC:
2420                         if (bp->dcbx_local_feat.pfc.enabled)
2421                                 *flags |= DCB_FEATCFG_ENABLE;
2422                         if (bp->dcbx_error & (DCBX_LOCAL_PFC_ERROR |
2423                                               DCBX_LOCAL_PFC_MISMATCH |
2424                                               DCBX_REMOTE_MIB_ERROR))
2425                                 *flags |= DCB_FEATCFG_ERROR;
2426                         break;
2427                 case DCB_FEATCFG_ATTR_APP:
2428                         if (bp->dcbx_local_feat.app.enabled)
2429                                 *flags |= DCB_FEATCFG_ENABLE;
2430                         if (bp->dcbx_error & (DCBX_LOCAL_APP_ERROR |
2431                                               DCBX_LOCAL_APP_MISMATCH |
2432                                               DCBX_REMOTE_MIB_ERROR))
2433                                 *flags |= DCB_FEATCFG_ERROR;
2434                         break;
2435                 default:
2436                         BNX2X_ERR("Non valid feature-ID\n");
2437                         rval = 1;
2438                         break;
2439                 }
2440         } else {
2441                 DP(BNX2X_MSG_DCB, "DCB disabled\n");
2442                 rval = 1;
2443         }
2444
2445         return rval;
2446 }
2447
2448 static u8 bnx2x_dcbnl_set_featcfg(struct net_device *netdev, int featid,
2449                                   u8 flags)
2450 {
2451         struct bnx2x *bp = netdev_priv(netdev);
2452         u8 rval = 0;
2453
2454         DP(BNX2X_MSG_DCB, "featid = %d flags = %02x\n", featid, flags);
2455
2456         /* ignore the 'advertise' flag */
2457         if (bnx2x_dcbnl_set_valid(bp)) {
2458                 switch (featid) {
2459                 case DCB_FEATCFG_ATTR_PG:
2460                         bp->dcbx_config_params.admin_ets_enable =
2461                                 flags & DCB_FEATCFG_ENABLE ? 1 : 0;
2462                         bp->dcbx_config_params.admin_ets_willing =
2463                                 flags & DCB_FEATCFG_WILLING ? 1 : 0;
2464                         break;
2465                 case DCB_FEATCFG_ATTR_PFC:
2466                         bp->dcbx_config_params.admin_pfc_enable =
2467                                 flags & DCB_FEATCFG_ENABLE ? 1 : 0;
2468                         bp->dcbx_config_params.admin_pfc_willing =
2469                                 flags & DCB_FEATCFG_WILLING ? 1 : 0;
2470                         break;
2471                 case DCB_FEATCFG_ATTR_APP:
2472                         /* ignore enable, always enabled */
2473                         bp->dcbx_config_params.admin_app_priority_willing =
2474                                 flags & DCB_FEATCFG_WILLING ? 1 : 0;
2475                         break;
2476                 default:
2477                         BNX2X_ERR("Non valid feature-ID\n");
2478                         rval = 1;
2479                         break;
2480                 }
2481         } else {
2482                 DP(BNX2X_MSG_DCB, "dcbnl call not valid\n");
2483                 rval = 1;
2484         }
2485
2486         return rval;
2487 }
2488
2489 static int bnx2x_peer_appinfo(struct net_device *netdev,
2490                               struct dcb_peer_app_info *info, u16* app_count)
2491 {
2492         int i;
2493         struct bnx2x *bp = netdev_priv(netdev);
2494
2495         DP(BNX2X_MSG_DCB, "APP-INFO\n");
2496
2497         info->willing = (bp->dcbx_remote_flags & DCBX_APP_REM_WILLING) ?: 0;
2498         info->error = (bp->dcbx_remote_flags & DCBX_APP_RX_ERROR) ?: 0;
2499         *app_count = 0;
2500
2501         for (i = 0; i < DCBX_MAX_APP_PROTOCOL; i++)
2502                 if (bp->dcbx_remote_feat.app.app_pri_tbl[i].appBitfield &
2503                     DCBX_APP_ENTRY_VALID)
2504                         (*app_count)++;
2505         return 0;
2506 }
2507
2508 static int bnx2x_peer_apptable(struct net_device *netdev,
2509                                struct dcb_app *table)
2510 {
2511         int i, j;
2512         struct bnx2x *bp = netdev_priv(netdev);
2513
2514         DP(BNX2X_MSG_DCB, "APP-TABLE\n");
2515
2516         for (i = 0, j = 0; i < DCBX_MAX_APP_PROTOCOL; i++) {
2517                 struct dcbx_app_priority_entry *ent =
2518                         &bp->dcbx_remote_feat.app.app_pri_tbl[i];
2519
2520                 if (ent->appBitfield & DCBX_APP_ENTRY_VALID) {
2521                         table[j].selector = bnx2x_dcbx_dcbnl_app_idtype(ent);
2522                         table[j].priority = bnx2x_dcbx_dcbnl_app_up(ent);
2523                         table[j++].protocol = ent->app_id;
2524                 }
2525         }
2526         return 0;
2527 }
2528
2529 static int bnx2x_cee_peer_getpg(struct net_device *netdev, struct cee_pg *pg)
2530 {
2531         int i;
2532         struct bnx2x *bp = netdev_priv(netdev);
2533
2534         pg->willing = (bp->dcbx_remote_flags & DCBX_ETS_REM_WILLING) ?: 0;
2535
2536         for (i = 0; i < CEE_DCBX_MAX_PGS; i++) {
2537                 pg->pg_bw[i] =
2538                         DCBX_PG_BW_GET(bp->dcbx_remote_feat.ets.pg_bw_tbl, i);
2539                 pg->prio_pg[i] =
2540                         DCBX_PRI_PG_GET(bp->dcbx_remote_feat.ets.pri_pg_tbl, i);
2541         }
2542         return 0;
2543 }
2544
2545 static int bnx2x_cee_peer_getpfc(struct net_device *netdev,
2546                                  struct cee_pfc *pfc)
2547 {
2548         struct bnx2x *bp = netdev_priv(netdev);
2549         pfc->tcs_supported = bp->dcbx_remote_feat.pfc.pfc_caps;
2550         pfc->pfc_en = bp->dcbx_remote_feat.pfc.pri_en_bitmap;
2551         return 0;
2552 }
2553
2554 const struct dcbnl_rtnl_ops bnx2x_dcbnl_ops = {
2555         .getstate               = bnx2x_dcbnl_get_state,
2556         .setstate               = bnx2x_dcbnl_set_state,
2557         .getpermhwaddr          = bnx2x_dcbnl_get_perm_hw_addr,
2558         .setpgtccfgtx           = bnx2x_dcbnl_set_pg_tccfg_tx,
2559         .setpgbwgcfgtx          = bnx2x_dcbnl_set_pg_bwgcfg_tx,
2560         .setpgtccfgrx           = bnx2x_dcbnl_set_pg_tccfg_rx,
2561         .setpgbwgcfgrx          = bnx2x_dcbnl_set_pg_bwgcfg_rx,
2562         .getpgtccfgtx           = bnx2x_dcbnl_get_pg_tccfg_tx,
2563         .getpgbwgcfgtx          = bnx2x_dcbnl_get_pg_bwgcfg_tx,
2564         .getpgtccfgrx           = bnx2x_dcbnl_get_pg_tccfg_rx,
2565         .getpgbwgcfgrx          = bnx2x_dcbnl_get_pg_bwgcfg_rx,
2566         .setpfccfg              = bnx2x_dcbnl_set_pfc_cfg,
2567         .getpfccfg              = bnx2x_dcbnl_get_pfc_cfg,
2568         .setall                 = bnx2x_dcbnl_set_all,
2569         .getcap                 = bnx2x_dcbnl_get_cap,
2570         .getnumtcs              = bnx2x_dcbnl_get_numtcs,
2571         .setnumtcs              = bnx2x_dcbnl_set_numtcs,
2572         .getpfcstate            = bnx2x_dcbnl_get_pfc_state,
2573         .setpfcstate            = bnx2x_dcbnl_set_pfc_state,
2574         .setapp                 = bnx2x_dcbnl_set_app_up,
2575         .getdcbx                = bnx2x_dcbnl_get_dcbx,
2576         .setdcbx                = bnx2x_dcbnl_set_dcbx,
2577         .getfeatcfg             = bnx2x_dcbnl_get_featcfg,
2578         .setfeatcfg             = bnx2x_dcbnl_set_featcfg,
2579         .peer_getappinfo        = bnx2x_peer_appinfo,
2580         .peer_getapptable       = bnx2x_peer_apptable,
2581         .cee_peer_getpg         = bnx2x_cee_peer_getpg,
2582         .cee_peer_getpfc        = bnx2x_cee_peer_getpfc,
2583 };
2584
2585 #endif /* BCM_DCBNL */