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