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