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