BNX2X: uint*_t spatch
[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 -
1704                                                  help_data->num_of_pg + 1);
1705                         /*
1706                          * If there are still VOQ-s which have no associated PG,
1707                          * then associate these VOQ-s to PG15. These PG-s will
1708                          * be used for SP between priorities on PG15.
1709                          */
1710                         entry += bnx2x_dcbx_cee_fill_strict_pri(bp, cos_data,
1711                                 entry, need_num_of_entries, pg->pg_priority);
1712                 }
1713         }
1714
1715         /* the entry will represent the number of COSes used */
1716         cos_data->num_of_cos = entry;
1717 }
1718 static void bnx2x_dcbx_fill_cos_params(struct bnx2x *bp,
1719                                        struct pg_help_data *help_data,
1720                                        struct dcbx_ets_feature *ets,
1721                                        uint32_t *pg_pri_orginal_spread)
1722 {
1723         struct cos_help_data         cos_data;
1724         uint8_t                    i                           = 0;
1725         uint32_t                   pri_join_mask               = 0;
1726         uint8_t                    num_of_dif_pri              = 0;
1727
1728         memset(&cos_data, 0, sizeof(cos_data));
1729
1730         /* Validate the pg value */
1731         for (i = 0; i < help_data->num_of_pg ; i++) {
1732                 if (DCBX_STRICT_PRIORITY != help_data->data[i].pg &&
1733                     DCBX_MAX_NUM_PG_BW_ENTRIES <= help_data->data[i].pg)
1734                         BNX2X_ERR("Invalid pg[%d] data %x\n", i,
1735                                   help_data->data[i].pg);
1736                 pri_join_mask   |=  help_data->data[i].pg_priority;
1737                 num_of_dif_pri  += help_data->data[i].num_of_dif_pri;
1738         }
1739
1740         /* defaults */
1741         cos_data.num_of_cos = 1;
1742         for (i = 0; i < ARRAY_SIZE(cos_data.data); i++) {
1743                 cos_data.data[i].pri_join_mask = 0;
1744                 cos_data.data[i].pausable = false;
1745                 cos_data.data[i].strict = BNX2X_DCBX_STRICT_INVALID;
1746                 cos_data.data[i].cos_bw = DCBX_INVALID_COS_BW;
1747         }
1748
1749         if (CHIP_IS_E3B0(bp))
1750                 bnx2x_dcbx_cee_fill_cos_params(bp, help_data, ets,
1751                                                &cos_data, pri_join_mask);
1752         else /* E2 + E3A0 */
1753                 bnx2x_dcbx_2cos_limit_cee_fill_cos_params(bp,
1754                                                           help_data, ets,
1755                                                           &cos_data,
1756                                                           pg_pri_orginal_spread,
1757                                                           pri_join_mask,
1758                                                           num_of_dif_pri);
1759
1760         for (i = 0; i < cos_data.num_of_cos ; i++) {
1761                 struct bnx2x_dcbx_cos_params *p =
1762                         &bp->dcbx_port_params.ets.cos_params[i];
1763
1764                 p->strict = cos_data.data[i].strict;
1765                 p->bw_tbl = cos_data.data[i].cos_bw;
1766                 p->pri_bitmask = cos_data.data[i].pri_join_mask;
1767                 p->pauseable = cos_data.data[i].pausable;
1768
1769                 /* sanity */
1770                 if (p->bw_tbl != DCBX_INVALID_COS_BW ||
1771                     p->strict != BNX2X_DCBX_STRICT_INVALID) {
1772                         if (p->pri_bitmask == 0)
1773                                 BNX2X_ERR("Invalid pri_bitmask for %d\n", i);
1774
1775                         if (CHIP_IS_E2(bp) || CHIP_IS_E3A0(bp)) {
1776
1777                                 if (p->pauseable &&
1778                                     DCBX_PFC_PRI_GET_NON_PAUSE(bp,
1779                                                 p->pri_bitmask) != 0)
1780                                         BNX2X_ERR("Inconsistent config for pausable COS %d\n",
1781                                                   i);
1782
1783                                 if (!p->pauseable &&
1784                                     DCBX_PFC_PRI_GET_PAUSE(bp,
1785                                                 p->pri_bitmask) != 0)
1786                                         BNX2X_ERR("Inconsistent config for nonpausable COS %d\n",
1787                                                   i);
1788                         }
1789                 }
1790
1791                 if (p->pauseable)
1792                         DP(BNX2X_MSG_DCB, "COS %d PAUSABLE prijoinmask 0x%x\n",
1793                                   i, cos_data.data[i].pri_join_mask);
1794                 else
1795                         DP(BNX2X_MSG_DCB,
1796                            "COS %d NONPAUSABLE prijoinmask 0x%x\n",
1797                            i, cos_data.data[i].pri_join_mask);
1798         }
1799
1800         bp->dcbx_port_params.ets.num_of_cos = cos_data.num_of_cos ;
1801 }
1802
1803 static void bnx2x_dcbx_get_ets_pri_pg_tbl(struct bnx2x *bp,
1804                                 uint32_t *set_configuration_ets_pg,
1805                                 uint32_t *pri_pg_tbl)
1806 {
1807         int i;
1808
1809         for (i = 0; i < DCBX_MAX_NUM_PRI_PG_ENTRIES; i++) {
1810                 set_configuration_ets_pg[i] = DCBX_PRI_PG_GET(pri_pg_tbl, i);
1811
1812                 DP(BNX2X_MSG_DCB, "set_configuration_ets_pg[%d] = 0x%x\n",
1813                    i, set_configuration_ets_pg[i]);
1814         }
1815 }
1816
1817 static void bnx2x_dcbx_fw_struct(struct bnx2x *bp,
1818                                  struct bnx2x_func_tx_start_params *pfc_fw_cfg)
1819 {
1820         uint16_t pri_bit = 0;
1821         uint8_t cos = 0, pri = 0;
1822         struct priority_cos *tt2cos;
1823         uint32_t *ttp = bp->dcbx_port_params.app.traffic_type_priority;
1824         int mfw_configured = SHMEM2_HAS(bp, drv_flags) &&
1825                              GET_FLAGS(SHMEM2_RD(bp, drv_flags),
1826                                        1 << DRV_FLAGS_DCB_MFW_CONFIGURED);
1827
1828         memset(pfc_fw_cfg, 0, sizeof(*pfc_fw_cfg));
1829
1830         /* to disable DCB - the structure must be zeroed */
1831         if ((bp->dcbx_error & DCBX_REMOTE_MIB_ERROR) && !mfw_configured)
1832                 return;
1833
1834         /*shortcut*/
1835         tt2cos = pfc_fw_cfg->traffic_type_to_priority_cos;
1836
1837         /* Fw version should be incremented each update */
1838         pfc_fw_cfg->dcb_version = ++bp->dcb_version;
1839         pfc_fw_cfg->dcb_enabled = 1;
1840
1841         /* Fill priority parameters */
1842         for (pri = 0; pri < LLFC_DRIVER_TRAFFIC_TYPE_MAX; pri++) {
1843                 tt2cos[pri].priority = ttp[pri];
1844                 pri_bit = 1 << tt2cos[pri].priority;
1845
1846                 /* Fill COS parameters based on COS calculated to
1847                  * make it more general for future use */
1848                 for (cos = 0; cos < bp->dcbx_port_params.ets.num_of_cos; cos++)
1849                         if (bp->dcbx_port_params.ets.cos_params[cos].
1850                                                 pri_bitmask & pri_bit)
1851                                         tt2cos[pri].cos = cos;
1852         }
1853
1854         /* we never want the FW to add a 0 vlan tag */
1855         pfc_fw_cfg->dont_add_pri_0_en = 1;
1856
1857         bnx2x_dcbx_print_cos_params(bp, pfc_fw_cfg);
1858 }
1859
1860 void bnx2x_dcbx_pmf_update(struct bnx2x *bp)
1861 {
1862         /* if we need to synchronize DCBX result from prev PMF
1863          * read it from shmem and update bp and netdev accordingly
1864          */
1865         if (SHMEM2_HAS(bp, drv_flags) &&
1866            GET_FLAGS(SHMEM2_RD(bp, drv_flags), 1 << DRV_FLAGS_DCB_CONFIGURED)) {
1867                 /* Read neg results if dcbx is in the FW */
1868                 if (bnx2x_dcbx_read_shmem_neg_results(bp))
1869                         return;
1870
1871                 bnx2x_dump_dcbx_drv_param(bp, &bp->dcbx_local_feat,
1872                                           bp->dcbx_error);
1873                 bnx2x_get_dcbx_drv_param(bp, &bp->dcbx_local_feat,
1874                                          bp->dcbx_error);
1875 #ifdef BCM_DCBNL
1876                 /*
1877                  * Add new app tlvs to dcbnl
1878                  */
1879                 bnx2x_dcbnl_update_applist(bp, false);
1880                 /*
1881                  * Send a notification for the new negotiated parameters
1882                  */
1883                 dcbnl_cee_notify(bp->dev, RTM_GETDCB, DCB_CMD_CEE_GET, 0, 0);
1884 #endif
1885                 /*
1886                  * reconfigure the netdevice with the results of the new
1887                  * dcbx negotiation.
1888                  */
1889                 bnx2x_dcbx_update_tc_mapping(bp);
1890         }
1891 }
1892
1893 /* DCB netlink */
1894 #ifdef BCM_DCBNL
1895
1896 #define BNX2X_DCBX_CAPS         (DCB_CAP_DCBX_LLD_MANAGED | \
1897                                 DCB_CAP_DCBX_VER_CEE | DCB_CAP_DCBX_STATIC)
1898
1899 static inline bool bnx2x_dcbnl_set_valid(struct bnx2x *bp)
1900 {
1901         /* validate dcbnl call that may change HW state:
1902          * DCB is on and DCBX mode was SUCCESSFULLY set by the user.
1903          */
1904         return bp->dcb_state && bp->dcbx_mode_uset;
1905 }
1906
1907 static uint8_t bnx2x_dcbnl_get_state(struct net_device *netdev)
1908 {
1909         struct bnx2x *bp = netdev_priv(netdev);
1910         DP(BNX2X_MSG_DCB, "state = %d\n", bp->dcb_state);
1911         return bp->dcb_state;
1912 }
1913
1914 static uint8_t bnx2x_dcbnl_set_state(struct net_device *netdev, uint8_t state)
1915 {
1916         struct bnx2x *bp = netdev_priv(netdev);
1917         DP(BNX2X_MSG_DCB, "state = %s\n", state ? "on" : "off");
1918
1919         /* Fail to set state to "enabled" if dcbx is disabled in nvram */
1920         if (state && ((bp->dcbx_enabled == BNX2X_DCBX_ENABLED_OFF) ||
1921                       (bp->dcbx_enabled == BNX2X_DCBX_ENABLED_INVALID))) {
1922                 DP(BNX2X_MSG_DCB, "Can not set dcbx to enabled while it is disabled in nvm\n");
1923                 return 1;
1924         }
1925
1926         bnx2x_dcbx_set_state(bp, (state ? true : false), bp->dcbx_enabled);
1927         return 0;
1928 }
1929
1930 static void bnx2x_dcbnl_get_perm_hw_addr(struct net_device *netdev,
1931                                          uint8_t *perm_addr)
1932 {
1933         struct bnx2x *bp = netdev_priv(netdev);
1934         DP(BNX2X_MSG_DCB, "GET-PERM-ADDR\n");
1935
1936         /* first the HW mac address */
1937         memcpy(perm_addr, netdev->dev_addr, netdev->addr_len);
1938
1939         if (CNIC_LOADED(bp))
1940                 /* second SAN address */
1941                 memcpy(perm_addr+netdev->addr_len, bp->fip_mac,
1942                        netdev->addr_len);
1943 }
1944
1945 static void bnx2x_dcbnl_set_pg_tccfg_tx(struct net_device *netdev, int prio,
1946                                         uint8_t prio_type, uint8_t pgid,
1947                                         uint8_t bw_pct,
1948                                         uint8_t up_map)
1949 {
1950         struct bnx2x *bp = netdev_priv(netdev);
1951
1952         DP(BNX2X_MSG_DCB, "prio[%d] = %d\n", prio, pgid);
1953         if (!bnx2x_dcbnl_set_valid(bp) || prio >= DCBX_MAX_NUM_PRI_PG_ENTRIES)
1954                 return;
1955
1956         /**
1957          * bw_pct ignored -     band-width percentage devision between user
1958          *                      priorities within the same group is not
1959          *                      standard and hence not supported
1960          *
1961          * prio_type ignored -  priority levels within the same group are not
1962          *                      standard and hence are not supported. According
1963          *                      to the standard pgid 15 is dedicated to strict
1964          *                      priority traffic (on the port level).
1965          *
1966          * up_map ignored
1967          */
1968
1969         bp->dcbx_config_params.admin_configuration_ets_pg[prio] = pgid;
1970         bp->dcbx_config_params.admin_ets_configuration_tx_enable = 1;
1971 }
1972
1973 static void bnx2x_dcbnl_set_pg_bwgcfg_tx(struct net_device *netdev,
1974                                          int pgid, uint8_t bw_pct)
1975 {
1976         struct bnx2x *bp = netdev_priv(netdev);
1977         DP(BNX2X_MSG_DCB, "pgid[%d] = %d\n", pgid, bw_pct);
1978
1979         if (!bnx2x_dcbnl_set_valid(bp) || pgid >= DCBX_MAX_NUM_PG_BW_ENTRIES)
1980                 return;
1981
1982         bp->dcbx_config_params.admin_configuration_bw_precentage[pgid] = bw_pct;
1983         bp->dcbx_config_params.admin_ets_configuration_tx_enable = 1;
1984 }
1985
1986 static void bnx2x_dcbnl_set_pg_tccfg_rx(struct net_device *netdev, int prio,
1987                                         uint8_t prio_type, uint8_t pgid,
1988                                         uint8_t bw_pct,
1989                                         uint8_t up_map)
1990 {
1991         struct bnx2x *bp = netdev_priv(netdev);
1992         DP(BNX2X_MSG_DCB, "Nothing to set; No RX support\n");
1993 }
1994
1995 static void bnx2x_dcbnl_set_pg_bwgcfg_rx(struct net_device *netdev,
1996                                          int pgid, uint8_t bw_pct)
1997 {
1998         struct bnx2x *bp = netdev_priv(netdev);
1999         DP(BNX2X_MSG_DCB, "Nothing to set; No RX support\n");
2000 }
2001
2002 static void bnx2x_dcbnl_get_pg_tccfg_tx(struct net_device *netdev, int prio,
2003                                         uint8_t *prio_type, uint8_t *pgid,
2004                                         uint8_t *bw_pct,
2005                                         uint8_t *up_map)
2006 {
2007         struct bnx2x *bp = netdev_priv(netdev);
2008         DP(BNX2X_MSG_DCB, "prio = %d\n", prio);
2009
2010         /**
2011          * bw_pct ignored -     band-width percentage devision between user
2012          *                      priorities within the same group is not
2013          *                      standard and hence not supported
2014          *
2015          * prio_type ignored -  priority levels within the same group are not
2016          *                      standard and hence are not supported. According
2017          *                      to the standard pgid 15 is dedicated to strict
2018          *                      priority traffic (on the port level).
2019          *
2020          * up_map ignored
2021          */
2022         *up_map = *bw_pct = *prio_type = *pgid = 0;
2023
2024         if (!bp->dcb_state || prio >= DCBX_MAX_NUM_PRI_PG_ENTRIES)
2025                 return;
2026
2027         *pgid = DCBX_PRI_PG_GET(bp->dcbx_local_feat.ets.pri_pg_tbl, prio);
2028 }
2029
2030 static void bnx2x_dcbnl_get_pg_bwgcfg_tx(struct net_device *netdev,
2031                                          int pgid, uint8_t *bw_pct)
2032 {
2033         struct bnx2x *bp = netdev_priv(netdev);
2034         DP(BNX2X_MSG_DCB, "pgid = %d\n", pgid);
2035
2036         *bw_pct = 0;
2037
2038         if (!bp->dcb_state || pgid >= DCBX_MAX_NUM_PG_BW_ENTRIES)
2039                 return;
2040
2041         *bw_pct = DCBX_PG_BW_GET(bp->dcbx_local_feat.ets.pg_bw_tbl, pgid);
2042 }
2043
2044 static void bnx2x_dcbnl_get_pg_tccfg_rx(struct net_device *netdev, int prio,
2045                                         uint8_t *prio_type, uint8_t *pgid,
2046                                         uint8_t *bw_pct,
2047                                         uint8_t *up_map)
2048 {
2049         struct bnx2x *bp = netdev_priv(netdev);
2050         DP(BNX2X_MSG_DCB, "Nothing to get; No RX support\n");
2051
2052         *prio_type = *pgid = *bw_pct = *up_map = 0;
2053 }
2054
2055 static void bnx2x_dcbnl_get_pg_bwgcfg_rx(struct net_device *netdev,
2056                                          int pgid, uint8_t *bw_pct)
2057 {
2058         struct bnx2x *bp = netdev_priv(netdev);
2059         DP(BNX2X_MSG_DCB, "Nothing to get; No RX support\n");
2060
2061         *bw_pct = 0;
2062 }
2063
2064 static void bnx2x_dcbnl_set_pfc_cfg(struct net_device *netdev, int prio,
2065                                     uint8_t setting)
2066 {
2067         struct bnx2x *bp = netdev_priv(netdev);
2068         DP(BNX2X_MSG_DCB, "prio[%d] = %d\n", prio, setting);
2069
2070         if (!bnx2x_dcbnl_set_valid(bp) || prio >= MAX_PFC_PRIORITIES)
2071                 return;
2072
2073         if (setting) {
2074                 bp->dcbx_config_params.admin_pfc_bitmap |= (1 << prio);
2075                 bp->dcbx_config_params.admin_pfc_tx_enable = 1;
2076         } else {
2077                 bp->dcbx_config_params.admin_pfc_bitmap &= ~(1 << prio);
2078         }
2079 }
2080
2081 static void bnx2x_dcbnl_get_pfc_cfg(struct net_device *netdev, int prio,
2082                                     uint8_t *setting)
2083 {
2084         struct bnx2x *bp = netdev_priv(netdev);
2085         DP(BNX2X_MSG_DCB, "prio = %d\n", prio);
2086
2087         *setting = 0;
2088
2089         if (!bp->dcb_state || prio >= MAX_PFC_PRIORITIES)
2090                 return;
2091
2092         *setting = (bp->dcbx_local_feat.pfc.pri_en_bitmap >> prio) & 0x1;
2093 }
2094
2095 static uint8_t bnx2x_dcbnl_set_all(struct net_device *netdev)
2096 {
2097         struct bnx2x *bp = netdev_priv(netdev);
2098
2099         DP(BNX2X_MSG_DCB, "SET-ALL\n");
2100
2101         if (!bnx2x_dcbnl_set_valid(bp))
2102                 return 1;
2103
2104         if (bp->recovery_state != BNX2X_RECOVERY_DONE) {
2105                 netdev_err(bp->dev,
2106                            "Handling parity error recovery. Try again later\n");
2107                 return 1;
2108         }
2109         if (netif_running(bp->dev)) {
2110                 bnx2x_update_drv_flags(bp,
2111                                        1 << DRV_FLAGS_DCB_MFW_CONFIGURED,
2112                                        1);
2113                 bnx2x_dcbx_init(bp, true);
2114         }
2115         DP(BNX2X_MSG_DCB, "set_dcbx_params done\n");
2116
2117         return 0;
2118 }
2119
2120 static uint8_t bnx2x_dcbnl_get_cap(struct net_device *netdev, int capid,
2121                                    uint8_t *cap)
2122 {
2123         struct bnx2x *bp = netdev_priv(netdev);
2124         uint8_t rval = 0;
2125
2126         if (bp->dcb_state) {
2127                 switch (capid) {
2128                 case DCB_CAP_ATTR_PG:
2129                         *cap = true;
2130                         break;
2131                 case DCB_CAP_ATTR_PFC:
2132                         *cap = true;
2133                         break;
2134                 case DCB_CAP_ATTR_UP2TC:
2135                         *cap = false;
2136                         break;
2137                 case DCB_CAP_ATTR_PG_TCS:
2138                         *cap = 0x80;    /* 8 priorities for PGs */
2139                         break;
2140                 case DCB_CAP_ATTR_PFC_TCS:
2141                         *cap = 0x80;    /* 8 priorities for PFC */
2142                         break;
2143                 case DCB_CAP_ATTR_GSP:
2144                         *cap = true;
2145                         break;
2146                 case DCB_CAP_ATTR_BCN:
2147                         *cap = false;
2148                         break;
2149                 case DCB_CAP_ATTR_DCBX:
2150                         *cap = BNX2X_DCBX_CAPS;
2151                         break;
2152                 default:
2153                         BNX2X_ERR("Non valid capability ID\n");
2154                         rval = 1;
2155                         break;
2156                 }
2157         } else {
2158                 DP(BNX2X_MSG_DCB, "DCB disabled\n");
2159                 rval = 1;
2160         }
2161
2162         DP(BNX2X_MSG_DCB, "capid %d:%x\n", capid, *cap);
2163         return rval;
2164 }
2165
2166 static int bnx2x_dcbnl_get_numtcs(struct net_device *netdev, int tcid,
2167                                   uint8_t *num)
2168 {
2169         struct bnx2x *bp = netdev_priv(netdev);
2170         uint8_t rval = 0;
2171
2172         DP(BNX2X_MSG_DCB, "tcid %d\n", tcid);
2173
2174         if (bp->dcb_state) {
2175                 switch (tcid) {
2176                 case DCB_NUMTCS_ATTR_PG:
2177                         *num = CHIP_IS_E3B0(bp) ? DCBX_COS_MAX_NUM_E3B0 :
2178                                                   DCBX_COS_MAX_NUM_E2;
2179                         break;
2180                 case DCB_NUMTCS_ATTR_PFC:
2181                         *num = CHIP_IS_E3B0(bp) ? DCBX_COS_MAX_NUM_E3B0 :
2182                                                   DCBX_COS_MAX_NUM_E2;
2183                         break;
2184                 default:
2185                         BNX2X_ERR("Non valid TC-ID\n");
2186                         rval = 1;
2187                         break;
2188                 }
2189         } else {
2190                 DP(BNX2X_MSG_DCB, "DCB disabled\n");
2191                 rval = 1;
2192         }
2193
2194         return rval;
2195 }
2196
2197 static int bnx2x_dcbnl_set_numtcs(struct net_device *netdev, int tcid,
2198                                   uint8_t num)
2199 {
2200         struct bnx2x *bp = netdev_priv(netdev);
2201         DP(BNX2X_MSG_DCB, "num tcs = %d; Not supported\n", num);
2202         return -EINVAL;
2203 }
2204
2205 static uint8_t bnx2x_dcbnl_get_pfc_state(struct net_device *netdev)
2206 {
2207         struct bnx2x *bp = netdev_priv(netdev);
2208         DP(BNX2X_MSG_DCB, "state = %d\n", bp->dcbx_local_feat.pfc.enabled);
2209
2210         if (!bp->dcb_state)
2211                 return 0;
2212
2213         return bp->dcbx_local_feat.pfc.enabled;
2214 }
2215
2216 static void bnx2x_dcbnl_set_pfc_state(struct net_device *netdev,
2217                                       uint8_t state)
2218 {
2219         struct bnx2x *bp = netdev_priv(netdev);
2220         DP(BNX2X_MSG_DCB, "state = %s\n", state ? "on" : "off");
2221
2222         if (!bnx2x_dcbnl_set_valid(bp))
2223                 return;
2224
2225         bp->dcbx_config_params.admin_pfc_tx_enable =
2226         bp->dcbx_config_params.admin_pfc_enable = (state ? 1 : 0);
2227 }
2228
2229 static void bnx2x_admin_app_set_ent(
2230         struct bnx2x_admin_priority_app_table *app_ent,
2231         uint8_t idtype, uint16_t idval, uint8_t up)
2232 {
2233         app_ent->valid = 1;
2234
2235         switch (idtype) {
2236         case DCB_APP_IDTYPE_ETHTYPE:
2237                 app_ent->traffic_type = TRAFFIC_TYPE_ETH;
2238                 break;
2239         case DCB_APP_IDTYPE_PORTNUM:
2240                 app_ent->traffic_type = TRAFFIC_TYPE_PORT;
2241                 break;
2242         default:
2243                 break; /* never gets here */
2244         }
2245         app_ent->app_id = idval;
2246         app_ent->priority = up;
2247 }
2248
2249 static bool bnx2x_admin_app_is_equal(
2250         struct bnx2x_admin_priority_app_table *app_ent,
2251         uint8_t idtype, uint16_t idval)
2252 {
2253         if (!app_ent->valid)
2254                 return false;
2255
2256         switch (idtype) {
2257         case DCB_APP_IDTYPE_ETHTYPE:
2258                 if (app_ent->traffic_type != TRAFFIC_TYPE_ETH)
2259                         return false;
2260                 break;
2261         case DCB_APP_IDTYPE_PORTNUM:
2262                 if (app_ent->traffic_type != TRAFFIC_TYPE_PORT)
2263                         return false;
2264                 break;
2265         default:
2266                 return false;
2267         }
2268         if (app_ent->app_id != idval)
2269                 return false;
2270
2271         return true;
2272 }
2273
2274 static int bnx2x_set_admin_app_up(struct bnx2x *bp, uint8_t idtype,
2275                                   uint16_t idval, uint8_t up)
2276 {
2277         int i, ff;
2278
2279         /* iterate over the app entries looking for idtype and idval */
2280         for (i = 0, ff = -1; i < DCBX_CONFIG_MAX_APP_PROTOCOL; i++) {
2281                 struct bnx2x_admin_priority_app_table *app_ent =
2282                         &bp->dcbx_config_params.admin_priority_app_table[i];
2283                 if (bnx2x_admin_app_is_equal(app_ent, idtype, idval))
2284                         break;
2285
2286                 if (ff < 0 && !app_ent->valid)
2287                         ff = i;
2288         }
2289         if (i < DCBX_CONFIG_MAX_APP_PROTOCOL)
2290                 /* if found overwrite up */
2291                 bp->dcbx_config_params.
2292                         admin_priority_app_table[i].priority = up;
2293         else if (ff >= 0)
2294                 /* not found use first-free */
2295                 bnx2x_admin_app_set_ent(
2296                         &bp->dcbx_config_params.admin_priority_app_table[ff],
2297                         idtype, idval, up);
2298         else {
2299                 /* app table is full */
2300                 BNX2X_ERR("Application table is too large\n");
2301                 return -EBUSY;
2302         }
2303
2304         /* up configured, if not 0 make sure feature is enabled */
2305         if (up)
2306                 bp->dcbx_config_params.admin_application_priority_tx_enable = 1;
2307
2308         return 0;
2309 }
2310
2311 static int bnx2x_dcbnl_set_app_up(struct net_device *netdev, uint8_t idtype,
2312                                   uint16_t idval, uint8_t up)
2313 {
2314         struct bnx2x *bp = netdev_priv(netdev);
2315
2316         DP(BNX2X_MSG_DCB, "app_type %d, app_id %x, prio bitmap %d\n",
2317            idtype, idval, up);
2318
2319         if (!bnx2x_dcbnl_set_valid(bp)) {
2320                 DP(BNX2X_MSG_DCB, "dcbnl call not valid\n");
2321                 return -EINVAL;
2322         }
2323
2324         /* verify idtype */
2325         switch (idtype) {
2326         case DCB_APP_IDTYPE_ETHTYPE:
2327         case DCB_APP_IDTYPE_PORTNUM:
2328                 break;
2329         default:
2330                 DP(BNX2X_MSG_DCB, "Wrong ID type\n");
2331                 return -EINVAL;
2332         }
2333         return bnx2x_set_admin_app_up(bp, idtype, idval, up);
2334 }
2335
2336 static uint8_t bnx2x_dcbnl_get_dcbx(struct net_device *netdev)
2337 {
2338         struct bnx2x *bp = netdev_priv(netdev);
2339         uint8_t state;
2340
2341         state = DCB_CAP_DCBX_LLD_MANAGED | DCB_CAP_DCBX_VER_CEE;
2342
2343         if (bp->dcbx_enabled == BNX2X_DCBX_ENABLED_ON_NEG_OFF)
2344                 state |= DCB_CAP_DCBX_STATIC;
2345
2346         return state;
2347 }
2348
2349 static uint8_t bnx2x_dcbnl_set_dcbx(struct net_device *netdev, uint8_t state)
2350 {
2351         struct bnx2x *bp = netdev_priv(netdev);
2352         DP(BNX2X_MSG_DCB, "state = %02x\n", state);
2353
2354         /* set dcbx mode */
2355
2356         if ((state & BNX2X_DCBX_CAPS) != state) {
2357                 BNX2X_ERR("Requested DCBX mode %x is beyond advertised capabilities\n",
2358                           state);
2359                 return 1;
2360         }
2361
2362         if (bp->dcb_state != BNX2X_DCB_STATE_ON) {
2363                 BNX2X_ERR("DCB turned off, DCBX configuration is invalid\n");
2364                 return 1;
2365         }
2366
2367         if (state & DCB_CAP_DCBX_STATIC)
2368                 bp->dcbx_enabled = BNX2X_DCBX_ENABLED_ON_NEG_OFF;
2369         else
2370                 bp->dcbx_enabled = BNX2X_DCBX_ENABLED_ON_NEG_ON;
2371
2372         bp->dcbx_mode_uset = true;
2373         return 0;
2374 }
2375
2376 static uint8_t bnx2x_dcbnl_get_featcfg(struct net_device *netdev, int featid,
2377                                   uint8_t *flags)
2378 {
2379         struct bnx2x *bp = netdev_priv(netdev);
2380         uint8_t rval = 0;
2381
2382         DP(BNX2X_MSG_DCB, "featid %d\n", featid);
2383
2384         if (bp->dcb_state) {
2385                 *flags = 0;
2386                 switch (featid) {
2387                 case DCB_FEATCFG_ATTR_PG:
2388                         if (bp->dcbx_local_feat.ets.enabled)
2389                                 *flags |= DCB_FEATCFG_ENABLE;
2390                         if (bp->dcbx_error & (DCBX_LOCAL_ETS_ERROR |
2391                                               DCBX_REMOTE_MIB_ERROR))
2392                                 *flags |= DCB_FEATCFG_ERROR;
2393                         break;
2394                 case DCB_FEATCFG_ATTR_PFC:
2395                         if (bp->dcbx_local_feat.pfc.enabled)
2396                                 *flags |= DCB_FEATCFG_ENABLE;
2397                         if (bp->dcbx_error & (DCBX_LOCAL_PFC_ERROR |
2398                                               DCBX_LOCAL_PFC_MISMATCH |
2399                                               DCBX_REMOTE_MIB_ERROR))
2400                                 *flags |= DCB_FEATCFG_ERROR;
2401                         break;
2402                 case DCB_FEATCFG_ATTR_APP:
2403                         if (bp->dcbx_local_feat.app.enabled)
2404                                 *flags |= DCB_FEATCFG_ENABLE;
2405                         if (bp->dcbx_error & (DCBX_LOCAL_APP_ERROR |
2406                                               DCBX_LOCAL_APP_MISMATCH |
2407                                               DCBX_REMOTE_MIB_ERROR))
2408                                 *flags |= DCB_FEATCFG_ERROR;
2409                         break;
2410                 default:
2411                         BNX2X_ERR("Non valid feature-ID\n");
2412                         rval = 1;
2413                         break;
2414                 }
2415         } else {
2416                 DP(BNX2X_MSG_DCB, "DCB disabled\n");
2417                 rval = 1;
2418         }
2419
2420         return rval;
2421 }
2422
2423 static uint8_t bnx2x_dcbnl_set_featcfg(struct net_device *netdev, int featid,
2424                                   uint8_t flags)
2425 {
2426         struct bnx2x *bp = netdev_priv(netdev);
2427         uint8_t rval = 0;
2428
2429         DP(BNX2X_MSG_DCB, "featid = %d flags = %02x\n", featid, flags);
2430
2431         /* ignore the 'advertise' flag */
2432         if (bnx2x_dcbnl_set_valid(bp)) {
2433                 switch (featid) {
2434                 case DCB_FEATCFG_ATTR_PG:
2435                         bp->dcbx_config_params.admin_ets_enable =
2436                                 flags & DCB_FEATCFG_ENABLE ? 1 : 0;
2437                         bp->dcbx_config_params.admin_ets_willing =
2438                                 flags & DCB_FEATCFG_WILLING ? 1 : 0;
2439                         break;
2440                 case DCB_FEATCFG_ATTR_PFC:
2441                         bp->dcbx_config_params.admin_pfc_enable =
2442                                 flags & DCB_FEATCFG_ENABLE ? 1 : 0;
2443                         bp->dcbx_config_params.admin_pfc_willing =
2444                                 flags & DCB_FEATCFG_WILLING ? 1 : 0;
2445                         break;
2446                 case DCB_FEATCFG_ATTR_APP:
2447                         /* ignore enable, always enabled */
2448                         bp->dcbx_config_params.admin_app_priority_willing =
2449                                 flags & DCB_FEATCFG_WILLING ? 1 : 0;
2450                         break;
2451                 default:
2452                         BNX2X_ERR("Non valid feature-ID\n");
2453                         rval = 1;
2454                         break;
2455                 }
2456         } else {
2457                 DP(BNX2X_MSG_DCB, "dcbnl call not valid\n");
2458                 rval = 1;
2459         }
2460
2461         return rval;
2462 }
2463
2464 static int bnx2x_peer_appinfo(struct net_device *netdev,
2465                               struct dcb_peer_app_info *info,
2466                               uint16_t* app_count)
2467 {
2468         int i;
2469         struct bnx2x *bp = netdev_priv(netdev);
2470
2471         DP(BNX2X_MSG_DCB, "APP-INFO\n");
2472
2473         info->willing = (bp->dcbx_remote_flags & DCBX_APP_REM_WILLING) ?: 0;
2474         info->error = (bp->dcbx_remote_flags & DCBX_APP_RX_ERROR) ?: 0;
2475         *app_count = 0;
2476
2477         for (i = 0; i < DCBX_MAX_APP_PROTOCOL; i++)
2478                 if (bp->dcbx_remote_feat.app.app_pri_tbl[i].appBitfield &
2479                     DCBX_APP_ENTRY_VALID)
2480                         (*app_count)++;
2481         return 0;
2482 }
2483
2484 static int bnx2x_peer_apptable(struct net_device *netdev,
2485                                struct dcb_app *table)
2486 {
2487         int i, j;
2488         struct bnx2x *bp = netdev_priv(netdev);
2489
2490         DP(BNX2X_MSG_DCB, "APP-TABLE\n");
2491
2492         for (i = 0, j = 0; i < DCBX_MAX_APP_PROTOCOL; i++) {
2493                 struct dcbx_app_priority_entry *ent =
2494                         &bp->dcbx_remote_feat.app.app_pri_tbl[i];
2495
2496                 if (ent->appBitfield & DCBX_APP_ENTRY_VALID) {
2497                         table[j].selector = bnx2x_dcbx_dcbnl_app_idtype(ent);
2498                         table[j].priority = bnx2x_dcbx_dcbnl_app_up(ent);
2499                         table[j++].protocol = ent->app_id;
2500                 }
2501         }
2502         return 0;
2503 }
2504
2505 static int bnx2x_cee_peer_getpg(struct net_device *netdev, struct cee_pg *pg)
2506 {
2507         int i;
2508         struct bnx2x *bp = netdev_priv(netdev);
2509
2510         pg->willing = (bp->dcbx_remote_flags & DCBX_ETS_REM_WILLING) ?: 0;
2511
2512         for (i = 0; i < CEE_DCBX_MAX_PGS; i++) {
2513                 pg->pg_bw[i] =
2514                         DCBX_PG_BW_GET(bp->dcbx_remote_feat.ets.pg_bw_tbl, i);
2515                 pg->prio_pg[i] =
2516                         DCBX_PRI_PG_GET(bp->dcbx_remote_feat.ets.pri_pg_tbl, i);
2517         }
2518         return 0;
2519 }
2520
2521 static int bnx2x_cee_peer_getpfc(struct net_device *netdev,
2522                                  struct cee_pfc *pfc)
2523 {
2524         struct bnx2x *bp = netdev_priv(netdev);
2525         pfc->tcs_supported = bp->dcbx_remote_feat.pfc.pfc_caps;
2526         pfc->pfc_en = bp->dcbx_remote_feat.pfc.pri_en_bitmap;
2527         return 0;
2528 }
2529
2530 const struct dcbnl_rtnl_ops bnx2x_dcbnl_ops = {
2531         .getstate               = bnx2x_dcbnl_get_state,
2532         .setstate               = bnx2x_dcbnl_set_state,
2533         .getpermhwaddr          = bnx2x_dcbnl_get_perm_hw_addr,
2534         .setpgtccfgtx           = bnx2x_dcbnl_set_pg_tccfg_tx,
2535         .setpgbwgcfgtx          = bnx2x_dcbnl_set_pg_bwgcfg_tx,
2536         .setpgtccfgrx           = bnx2x_dcbnl_set_pg_tccfg_rx,
2537         .setpgbwgcfgrx          = bnx2x_dcbnl_set_pg_bwgcfg_rx,
2538         .getpgtccfgtx           = bnx2x_dcbnl_get_pg_tccfg_tx,
2539         .getpgbwgcfgtx          = bnx2x_dcbnl_get_pg_bwgcfg_tx,
2540         .getpgtccfgrx           = bnx2x_dcbnl_get_pg_tccfg_rx,
2541         .getpgbwgcfgrx          = bnx2x_dcbnl_get_pg_bwgcfg_rx,
2542         .setpfccfg              = bnx2x_dcbnl_set_pfc_cfg,
2543         .getpfccfg              = bnx2x_dcbnl_get_pfc_cfg,
2544         .setall                 = bnx2x_dcbnl_set_all,
2545         .getcap                 = bnx2x_dcbnl_get_cap,
2546         .getnumtcs              = bnx2x_dcbnl_get_numtcs,
2547         .setnumtcs              = bnx2x_dcbnl_set_numtcs,
2548         .getpfcstate            = bnx2x_dcbnl_get_pfc_state,
2549         .setpfcstate            = bnx2x_dcbnl_set_pfc_state,
2550         .setapp                 = bnx2x_dcbnl_set_app_up,
2551         .getdcbx                = bnx2x_dcbnl_get_dcbx,
2552         .setdcbx                = bnx2x_dcbnl_set_dcbx,
2553         .getfeatcfg             = bnx2x_dcbnl_get_featcfg,
2554         .setfeatcfg             = bnx2x_dcbnl_set_featcfg,
2555         .peer_getappinfo        = bnx2x_peer_appinfo,
2556         .peer_getapptable       = bnx2x_peer_apptable,
2557         .cee_peer_getpg         = bnx2x_cee_peer_getpg,
2558         .cee_peer_getpfc        = bnx2x_cee_peer_getpfc,
2559 };
2560
2561 #endif /* BCM_DCBNL */