1 /*******************************************************************************
2 
3   Intel 10 Gigabit PCI Express Linux driver
4   Copyright(c) 1999 - 2012 Intel Corporation.
5 
6   This program is free software; you can redistribute it and/or modify it
7   under the terms and conditions of the GNU General Public License,
8   version 2, as published by the Free Software Foundation.
9 
10   This program is distributed in the hope it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13   more details.
14 
15   You should have received a copy of the GNU General Public License along with
16   this program; if not, write to the Free Software Foundation, Inc.,
17   51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 
19   The full GNU General Public License is included in this distribution in
20   the file called "COPYING".
21 
22   Contact Information:
23   Linux NICS <linux.nics@intel.com>
24   e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
25   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26 
27 *******************************************************************************/
28 
29 #include "ixgbe.h"
30 #include <linux/dcbnl.h>
31 #include "ixgbe_dcb_82598.h"
32 #include "ixgbe_dcb_82599.h"
33 
34 /* Callbacks for DCB netlink in the kernel */
35 #define BIT_DCB_MODE	0x01
36 #define BIT_PFC		0x02
37 #define BIT_PG_RX	0x04
38 #define BIT_PG_TX	0x08
39 #define BIT_APP_UPCHG	0x10
40 #define BIT_LINKSPEED   0x80
41 
42 /* Responses for the DCB_C_SET_ALL command */
43 #define DCB_HW_CHG_RST  0  /* DCB configuration changed with reset */
44 #define DCB_NO_HW_CHG   1  /* DCB configuration did not change */
45 #define DCB_HW_CHG      2  /* DCB configuration changed, no reset */
46 
47 static int ixgbe_copy_dcb_cfg(struct ixgbe_adapter *adapter, int tc_max)
48 {
49 	struct ixgbe_dcb_config *scfg = &adapter->temp_dcb_cfg;
50 	struct ixgbe_dcb_config *dcfg = &adapter->dcb_cfg;
51 	struct tc_configuration *src = NULL;
52 	struct tc_configuration *dst = NULL;
53 	int i, j;
54 	int tx = DCB_TX_CONFIG;
55 	int rx = DCB_RX_CONFIG;
56 	int changes = 0;
57 #ifdef IXGBE_FCOE
58 	struct dcb_app app = {
59 			      .selector = DCB_APP_IDTYPE_ETHTYPE,
60 			      .protocol = ETH_P_FCOE,
61 			     };
62 	u8 up = dcb_getapp(adapter->netdev, &app);
63 
64 	if (up && !(up & (1 << adapter->fcoe.up)))
65 		changes |= BIT_APP_UPCHG;
66 #endif
67 
68 	for (i = DCB_PG_ATTR_TC_0; i < tc_max + DCB_PG_ATTR_TC_0; i++) {
69 		src = &scfg->tc_config[i - DCB_PG_ATTR_TC_0];
70 		dst = &dcfg->tc_config[i - DCB_PG_ATTR_TC_0];
71 
72 		if (dst->path[tx].prio_type != src->path[tx].prio_type) {
73 			dst->path[tx].prio_type = src->path[tx].prio_type;
74 			changes |= BIT_PG_TX;
75 		}
76 
77 		if (dst->path[tx].bwg_id != src->path[tx].bwg_id) {
78 			dst->path[tx].bwg_id = src->path[tx].bwg_id;
79 			changes |= BIT_PG_TX;
80 		}
81 
82 		if (dst->path[tx].bwg_percent != src->path[tx].bwg_percent) {
83 			dst->path[tx].bwg_percent = src->path[tx].bwg_percent;
84 			changes |= BIT_PG_TX;
85 		}
86 
87 		if (dst->path[tx].up_to_tc_bitmap !=
88 				src->path[tx].up_to_tc_bitmap) {
89 			dst->path[tx].up_to_tc_bitmap =
90 				src->path[tx].up_to_tc_bitmap;
91 			changes |= (BIT_PG_TX | BIT_PFC | BIT_APP_UPCHG);
92 		}
93 
94 		if (dst->path[rx].prio_type != src->path[rx].prio_type) {
95 			dst->path[rx].prio_type = src->path[rx].prio_type;
96 			changes |= BIT_PG_RX;
97 		}
98 
99 		if (dst->path[rx].bwg_id != src->path[rx].bwg_id) {
100 			dst->path[rx].bwg_id = src->path[rx].bwg_id;
101 			changes |= BIT_PG_RX;
102 		}
103 
104 		if (dst->path[rx].bwg_percent != src->path[rx].bwg_percent) {
105 			dst->path[rx].bwg_percent = src->path[rx].bwg_percent;
106 			changes |= BIT_PG_RX;
107 		}
108 
109 		if (dst->path[rx].up_to_tc_bitmap !=
110 				src->path[rx].up_to_tc_bitmap) {
111 			dst->path[rx].up_to_tc_bitmap =
112 				src->path[rx].up_to_tc_bitmap;
113 			changes |= (BIT_PG_RX | BIT_PFC | BIT_APP_UPCHG);
114 		}
115 	}
116 
117 	for (i = DCB_PG_ATTR_BW_ID_0; i < DCB_PG_ATTR_BW_ID_MAX; i++) {
118 		j = i - DCB_PG_ATTR_BW_ID_0;
119 		if (dcfg->bw_percentage[tx][j] != scfg->bw_percentage[tx][j]) {
120 			dcfg->bw_percentage[tx][j] = scfg->bw_percentage[tx][j];
121 			changes |= BIT_PG_TX;
122 		}
123 		if (dcfg->bw_percentage[rx][j] != scfg->bw_percentage[rx][j]) {
124 			dcfg->bw_percentage[rx][j] = scfg->bw_percentage[rx][j];
125 			changes |= BIT_PG_RX;
126 		}
127 	}
128 
129 	for (i = DCB_PFC_UP_ATTR_0; i < DCB_PFC_UP_ATTR_MAX; i++) {
130 		j = i - DCB_PFC_UP_ATTR_0;
131 		if (dcfg->tc_config[j].dcb_pfc != scfg->tc_config[j].dcb_pfc) {
132 			dcfg->tc_config[j].dcb_pfc = scfg->tc_config[j].dcb_pfc;
133 			changes |= BIT_PFC;
134 		}
135 	}
136 
137 	if (dcfg->pfc_mode_enable != scfg->pfc_mode_enable) {
138 		dcfg->pfc_mode_enable = scfg->pfc_mode_enable;
139 		changes |= BIT_PFC;
140 	}
141 
142 	return changes;
143 }
144 
145 static u8 ixgbe_dcbnl_get_state(struct net_device *netdev)
146 {
147 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
148 
149 	return !!(adapter->flags & IXGBE_FLAG_DCB_ENABLED);
150 }
151 
152 static u8 ixgbe_dcbnl_set_state(struct net_device *netdev, u8 state)
153 {
154 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
155 	int err = 0;
156 
157 	/* Fail command if not in CEE mode */
158 	if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_CEE))
159 		return 1;
160 
161 	/* verify there is something to do, if not then exit */
162 	if (!state == !(adapter->flags & IXGBE_FLAG_DCB_ENABLED))
163 		goto out;
164 
165 	err = ixgbe_setup_tc(netdev,
166 			     state ? adapter->dcb_cfg.num_tcs.pg_tcs : 0);
167 out:
168 	return !!err;
169 }
170 
171 static void ixgbe_dcbnl_get_perm_hw_addr(struct net_device *netdev,
172 					 u8 *perm_addr)
173 {
174 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
175 	int i, j;
176 
177 	memset(perm_addr, 0xff, MAX_ADDR_LEN);
178 
179 	for (i = 0; i < netdev->addr_len; i++)
180 		perm_addr[i] = adapter->hw.mac.perm_addr[i];
181 
182 	switch (adapter->hw.mac.type) {
183 	case ixgbe_mac_82599EB:
184 	case ixgbe_mac_X540:
185 		for (j = 0; j < netdev->addr_len; j++, i++)
186 			perm_addr[i] = adapter->hw.mac.san_addr[j];
187 		break;
188 	default:
189 		break;
190 	}
191 }
192 
193 static void ixgbe_dcbnl_set_pg_tc_cfg_tx(struct net_device *netdev, int tc,
194                                          u8 prio, u8 bwg_id, u8 bw_pct,
195                                          u8 up_map)
196 {
197 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
198 
199 	if (prio != DCB_ATTR_VALUE_UNDEFINED)
200 		adapter->temp_dcb_cfg.tc_config[tc].path[0].prio_type = prio;
201 	if (bwg_id != DCB_ATTR_VALUE_UNDEFINED)
202 		adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_id = bwg_id;
203 	if (bw_pct != DCB_ATTR_VALUE_UNDEFINED)
204 		adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_percent =
205 			bw_pct;
206 	if (up_map != DCB_ATTR_VALUE_UNDEFINED)
207 		adapter->temp_dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap =
208 			up_map;
209 }
210 
211 static void ixgbe_dcbnl_set_pg_bwg_cfg_tx(struct net_device *netdev, int bwg_id,
212                                           u8 bw_pct)
213 {
214 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
215 
216 	adapter->temp_dcb_cfg.bw_percentage[0][bwg_id] = bw_pct;
217 }
218 
219 static void ixgbe_dcbnl_set_pg_tc_cfg_rx(struct net_device *netdev, int tc,
220                                          u8 prio, u8 bwg_id, u8 bw_pct,
221                                          u8 up_map)
222 {
223 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
224 
225 	if (prio != DCB_ATTR_VALUE_UNDEFINED)
226 		adapter->temp_dcb_cfg.tc_config[tc].path[1].prio_type = prio;
227 	if (bwg_id != DCB_ATTR_VALUE_UNDEFINED)
228 		adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_id = bwg_id;
229 	if (bw_pct != DCB_ATTR_VALUE_UNDEFINED)
230 		adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_percent =
231 			bw_pct;
232 	if (up_map != DCB_ATTR_VALUE_UNDEFINED)
233 		adapter->temp_dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap =
234 			up_map;
235 }
236 
237 static void ixgbe_dcbnl_set_pg_bwg_cfg_rx(struct net_device *netdev, int bwg_id,
238                                           u8 bw_pct)
239 {
240 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
241 
242 	adapter->temp_dcb_cfg.bw_percentage[1][bwg_id] = bw_pct;
243 }
244 
245 static void ixgbe_dcbnl_get_pg_tc_cfg_tx(struct net_device *netdev, int tc,
246                                          u8 *prio, u8 *bwg_id, u8 *bw_pct,
247                                          u8 *up_map)
248 {
249 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
250 
251 	*prio = adapter->dcb_cfg.tc_config[tc].path[0].prio_type;
252 	*bwg_id = adapter->dcb_cfg.tc_config[tc].path[0].bwg_id;
253 	*bw_pct = adapter->dcb_cfg.tc_config[tc].path[0].bwg_percent;
254 	*up_map = adapter->dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap;
255 }
256 
257 static void ixgbe_dcbnl_get_pg_bwg_cfg_tx(struct net_device *netdev, int bwg_id,
258                                           u8 *bw_pct)
259 {
260 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
261 
262 	*bw_pct = adapter->dcb_cfg.bw_percentage[0][bwg_id];
263 }
264 
265 static void ixgbe_dcbnl_get_pg_tc_cfg_rx(struct net_device *netdev, int tc,
266                                          u8 *prio, u8 *bwg_id, u8 *bw_pct,
267                                          u8 *up_map)
268 {
269 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
270 
271 	*prio = adapter->dcb_cfg.tc_config[tc].path[1].prio_type;
272 	*bwg_id = adapter->dcb_cfg.tc_config[tc].path[1].bwg_id;
273 	*bw_pct = adapter->dcb_cfg.tc_config[tc].path[1].bwg_percent;
274 	*up_map = adapter->dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap;
275 }
276 
277 static void ixgbe_dcbnl_get_pg_bwg_cfg_rx(struct net_device *netdev, int bwg_id,
278                                           u8 *bw_pct)
279 {
280 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
281 
282 	*bw_pct = adapter->dcb_cfg.bw_percentage[1][bwg_id];
283 }
284 
285 static void ixgbe_dcbnl_set_pfc_cfg(struct net_device *netdev, int priority,
286                                     u8 setting)
287 {
288 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
289 
290 	adapter->temp_dcb_cfg.tc_config[priority].dcb_pfc = setting;
291 	if (adapter->temp_dcb_cfg.tc_config[priority].dcb_pfc !=
292 	    adapter->dcb_cfg.tc_config[priority].dcb_pfc)
293 		adapter->temp_dcb_cfg.pfc_mode_enable = true;
294 }
295 
296 static void ixgbe_dcbnl_get_pfc_cfg(struct net_device *netdev, int priority,
297                                     u8 *setting)
298 {
299 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
300 
301 	*setting = adapter->dcb_cfg.tc_config[priority].dcb_pfc;
302 }
303 
304 #ifdef IXGBE_FCOE
305 static void ixgbe_dcbnl_devreset(struct net_device *dev)
306 {
307 	struct ixgbe_adapter *adapter = netdev_priv(dev);
308 
309 	while (test_and_set_bit(__IXGBE_RESETTING, &adapter->state))
310 		usleep_range(1000, 2000);
311 
312 	if (netif_running(dev))
313 		dev->netdev_ops->ndo_stop(dev);
314 
315 	ixgbe_clear_interrupt_scheme(adapter);
316 	ixgbe_init_interrupt_scheme(adapter);
317 
318 	if (netif_running(dev))
319 		dev->netdev_ops->ndo_open(dev);
320 
321 	clear_bit(__IXGBE_RESETTING, &adapter->state);
322 }
323 #endif
324 
325 static u8 ixgbe_dcbnl_set_all(struct net_device *netdev)
326 {
327 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
328 	struct ixgbe_dcb_config *dcb_cfg = &adapter->dcb_cfg;
329 	struct ixgbe_hw *hw = &adapter->hw;
330 	int ret = DCB_NO_HW_CHG;
331 	int i;
332 
333 	/* Fail command if not in CEE mode */
334 	if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_CEE))
335 		return ret;
336 
337 	adapter->dcb_set_bitmap |= ixgbe_copy_dcb_cfg(adapter,
338 						      MAX_TRAFFIC_CLASS);
339 	if (!adapter->dcb_set_bitmap)
340 		return ret;
341 
342 	if (adapter->dcb_set_bitmap & (BIT_PG_TX|BIT_PG_RX)) {
343 		u16 refill[MAX_TRAFFIC_CLASS], max[MAX_TRAFFIC_CLASS];
344 		u8 bwg_id[MAX_TRAFFIC_CLASS], prio_type[MAX_TRAFFIC_CLASS];
345 		/* Priority to TC mapping in CEE case default to 1:1 */
346 		u8 prio_tc[MAX_USER_PRIORITY];
347 		int max_frame = adapter->netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
348 
349 #ifdef IXGBE_FCOE
350 		if (adapter->netdev->features & NETIF_F_FCOE_MTU)
351 			max_frame = max(max_frame, IXGBE_FCOE_JUMBO_FRAME_SIZE);
352 #endif
353 
354 		ixgbe_dcb_calculate_tc_credits(hw, dcb_cfg, max_frame,
355 					       DCB_TX_CONFIG);
356 		ixgbe_dcb_calculate_tc_credits(hw, dcb_cfg, max_frame,
357 					       DCB_RX_CONFIG);
358 
359 		ixgbe_dcb_unpack_refill(dcb_cfg, DCB_TX_CONFIG, refill);
360 		ixgbe_dcb_unpack_max(dcb_cfg, max);
361 		ixgbe_dcb_unpack_bwgid(dcb_cfg, DCB_TX_CONFIG, bwg_id);
362 		ixgbe_dcb_unpack_prio(dcb_cfg, DCB_TX_CONFIG, prio_type);
363 		ixgbe_dcb_unpack_map(dcb_cfg, DCB_TX_CONFIG, prio_tc);
364 
365 		ixgbe_dcb_hw_ets_config(hw, refill, max, bwg_id,
366 					prio_type, prio_tc);
367 
368 		for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
369 			netdev_set_prio_tc_map(netdev, i, prio_tc[i]);
370 
371 		ret = DCB_HW_CHG_RST;
372 	}
373 
374 	if (adapter->dcb_set_bitmap & BIT_PFC) {
375 		if (dcb_cfg->pfc_mode_enable) {
376 			u8 pfc_en;
377 			u8 prio_tc[MAX_USER_PRIORITY];
378 
379 			ixgbe_dcb_unpack_map(dcb_cfg, DCB_TX_CONFIG, prio_tc);
380 			ixgbe_dcb_unpack_pfc(dcb_cfg, &pfc_en);
381 			ixgbe_dcb_hw_pfc_config(hw, pfc_en, prio_tc);
382 		} else {
383 			hw->mac.ops.fc_enable(hw);
384 		}
385 
386 		ixgbe_set_rx_drop_en(adapter);
387 
388 		ret = DCB_HW_CHG;
389 	}
390 
391 #ifdef IXGBE_FCOE
392 	/* Reprogam FCoE hardware offloads when the traffic class
393 	 * FCoE is using changes. This happens if the APP info
394 	 * changes or the up2tc mapping is updated.
395 	 */
396 	if (adapter->dcb_set_bitmap & BIT_APP_UPCHG) {
397 		struct dcb_app app = {
398 				      .selector = DCB_APP_IDTYPE_ETHTYPE,
399 				      .protocol = ETH_P_FCOE,
400 				     };
401 		u8 up = dcb_getapp(netdev, &app);
402 
403 		adapter->fcoe.up = ffs(up) - 1;
404 		ixgbe_dcbnl_devreset(netdev);
405 		ret = DCB_HW_CHG_RST;
406 	}
407 #endif
408 
409 	adapter->dcb_set_bitmap = 0x00;
410 	return ret;
411 }
412 
413 static u8 ixgbe_dcbnl_getcap(struct net_device *netdev, int capid, u8 *cap)
414 {
415 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
416 
417 	switch (capid) {
418 	case DCB_CAP_ATTR_PG:
419 		*cap = true;
420 		break;
421 	case DCB_CAP_ATTR_PFC:
422 		*cap = true;
423 		break;
424 	case DCB_CAP_ATTR_UP2TC:
425 		*cap = false;
426 		break;
427 	case DCB_CAP_ATTR_PG_TCS:
428 		*cap = 0x80;
429 		break;
430 	case DCB_CAP_ATTR_PFC_TCS:
431 		*cap = 0x80;
432 		break;
433 	case DCB_CAP_ATTR_GSP:
434 		*cap = true;
435 		break;
436 	case DCB_CAP_ATTR_BCN:
437 		*cap = false;
438 		break;
439 	case DCB_CAP_ATTR_DCBX:
440 		*cap = adapter->dcbx_cap;
441 		break;
442 	default:
443 		*cap = false;
444 		break;
445 	}
446 
447 	return 0;
448 }
449 
450 static int ixgbe_dcbnl_getnumtcs(struct net_device *netdev, int tcid, u8 *num)
451 {
452 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
453 	u8 rval = 0;
454 
455 	if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
456 		switch (tcid) {
457 		case DCB_NUMTCS_ATTR_PG:
458 			*num = adapter->dcb_cfg.num_tcs.pg_tcs;
459 			break;
460 		case DCB_NUMTCS_ATTR_PFC:
461 			*num = adapter->dcb_cfg.num_tcs.pfc_tcs;
462 			break;
463 		default:
464 			rval = -EINVAL;
465 			break;
466 		}
467 	} else {
468 		rval = -EINVAL;
469 	}
470 
471 	return rval;
472 }
473 
474 static int ixgbe_dcbnl_setnumtcs(struct net_device *netdev, int tcid, u8 num)
475 {
476 	return -EINVAL;
477 }
478 
479 static u8 ixgbe_dcbnl_getpfcstate(struct net_device *netdev)
480 {
481 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
482 
483 	return adapter->dcb_cfg.pfc_mode_enable;
484 }
485 
486 static void ixgbe_dcbnl_setpfcstate(struct net_device *netdev, u8 state)
487 {
488 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
489 
490 	adapter->temp_dcb_cfg.pfc_mode_enable = state;
491 }
492 
493 /**
494  * ixgbe_dcbnl_getapp - retrieve the DCBX application user priority
495  * @netdev : the corresponding netdev
496  * @idtype : identifies the id as ether type or TCP/UDP port number
497  * @id: id is either ether type or TCP/UDP port number
498  *
499  * Returns : on success, returns a non-zero 802.1p user priority bitmap
500  * otherwise returns 0 as the invalid user priority bitmap to indicate an
501  * error.
502  */
503 static u8 ixgbe_dcbnl_getapp(struct net_device *netdev, u8 idtype, u16 id)
504 {
505 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
506 	struct dcb_app app = {
507 				.selector = idtype,
508 				.protocol = id,
509 			     };
510 
511 	if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_CEE))
512 		return 0;
513 
514 	return dcb_getapp(netdev, &app);
515 }
516 
517 static int ixgbe_dcbnl_ieee_getets(struct net_device *dev,
518 				   struct ieee_ets *ets)
519 {
520 	struct ixgbe_adapter *adapter = netdev_priv(dev);
521 	struct ieee_ets *my_ets = adapter->ixgbe_ieee_ets;
522 
523 	ets->ets_cap = adapter->dcb_cfg.num_tcs.pg_tcs;
524 
525 	/* No IEEE PFC settings available */
526 	if (!my_ets)
527 		return 0;
528 
529 	ets->cbs = my_ets->cbs;
530 	memcpy(ets->tc_tx_bw, my_ets->tc_tx_bw, sizeof(ets->tc_tx_bw));
531 	memcpy(ets->tc_rx_bw, my_ets->tc_rx_bw, sizeof(ets->tc_rx_bw));
532 	memcpy(ets->tc_tsa, my_ets->tc_tsa, sizeof(ets->tc_tsa));
533 	memcpy(ets->prio_tc, my_ets->prio_tc, sizeof(ets->prio_tc));
534 	return 0;
535 }
536 
537 static int ixgbe_dcbnl_ieee_setets(struct net_device *dev,
538 				   struct ieee_ets *ets)
539 {
540 	struct ixgbe_adapter *adapter = netdev_priv(dev);
541 	int max_frame = dev->mtu + ETH_HLEN + ETH_FCS_LEN;
542 	int i, err = 0;
543 	__u8 max_tc = 0;
544 
545 	if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_IEEE))
546 		return -EINVAL;
547 
548 	if (!adapter->ixgbe_ieee_ets) {
549 		adapter->ixgbe_ieee_ets = kmalloc(sizeof(struct ieee_ets),
550 						  GFP_KERNEL);
551 		if (!adapter->ixgbe_ieee_ets)
552 			return -ENOMEM;
553 	}
554 
555 	memcpy(adapter->ixgbe_ieee_ets, ets, sizeof(*adapter->ixgbe_ieee_ets));
556 
557 	for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
558 		if (ets->prio_tc[i] > max_tc)
559 			max_tc = ets->prio_tc[i];
560 	}
561 
562 	if (max_tc)
563 		max_tc++;
564 
565 	if (max_tc > adapter->dcb_cfg.num_tcs.pg_tcs)
566 		return -EINVAL;
567 
568 	if (max_tc != netdev_get_num_tc(dev))
569 		err = ixgbe_setup_tc(dev, max_tc);
570 
571 	if (err)
572 		goto err_out;
573 
574 	err = ixgbe_dcb_hw_ets(&adapter->hw, ets, max_frame);
575 err_out:
576 	return err;
577 }
578 
579 static int ixgbe_dcbnl_ieee_getpfc(struct net_device *dev,
580 				   struct ieee_pfc *pfc)
581 {
582 	struct ixgbe_adapter *adapter = netdev_priv(dev);
583 	struct ieee_pfc *my_pfc = adapter->ixgbe_ieee_pfc;
584 	int i;
585 
586 	pfc->pfc_cap = adapter->dcb_cfg.num_tcs.pfc_tcs;
587 
588 	/* No IEEE PFC settings available */
589 	if (!my_pfc)
590 		return 0;
591 
592 	pfc->pfc_en = my_pfc->pfc_en;
593 	pfc->mbc = my_pfc->mbc;
594 	pfc->delay = my_pfc->delay;
595 
596 	for (i = 0; i < MAX_TRAFFIC_CLASS; i++) {
597 		pfc->requests[i] = adapter->stats.pxoffrxc[i];
598 		pfc->indications[i] = adapter->stats.pxofftxc[i];
599 	}
600 
601 	return 0;
602 }
603 
604 static int ixgbe_dcbnl_ieee_setpfc(struct net_device *dev,
605 				   struct ieee_pfc *pfc)
606 {
607 	struct ixgbe_adapter *adapter = netdev_priv(dev);
608 	struct ixgbe_hw *hw = &adapter->hw;
609 	u8 *prio_tc;
610 	int err;
611 
612 	if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_IEEE))
613 		return -EINVAL;
614 
615 	if (!adapter->ixgbe_ieee_pfc) {
616 		adapter->ixgbe_ieee_pfc = kmalloc(sizeof(struct ieee_pfc),
617 						  GFP_KERNEL);
618 		if (!adapter->ixgbe_ieee_pfc)
619 			return -ENOMEM;
620 	}
621 
622 	prio_tc = adapter->ixgbe_ieee_ets->prio_tc;
623 	memcpy(adapter->ixgbe_ieee_pfc, pfc, sizeof(*adapter->ixgbe_ieee_pfc));
624 
625 	/* Enable link flow control parameters if PFC is disabled */
626 	if (pfc->pfc_en)
627 		err = ixgbe_dcb_hw_pfc_config(hw, pfc->pfc_en, prio_tc);
628 	else
629 		err = hw->mac.ops.fc_enable(hw);
630 
631 	ixgbe_set_rx_drop_en(adapter);
632 
633 	return err;
634 }
635 
636 static int ixgbe_dcbnl_ieee_setapp(struct net_device *dev,
637 				   struct dcb_app *app)
638 {
639 	struct ixgbe_adapter *adapter = netdev_priv(dev);
640 	int err = -EINVAL;
641 
642 	if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_IEEE))
643 		return err;
644 
645 	err = dcb_ieee_setapp(dev, app);
646 
647 #ifdef IXGBE_FCOE
648 	if (!err && app->selector == IEEE_8021QAZ_APP_SEL_ETHERTYPE &&
649 	    app->protocol == ETH_P_FCOE) {
650 		u8 app_mask = dcb_ieee_getapp_mask(dev, app);
651 
652 		if (app_mask & (1 << adapter->fcoe.up))
653 			return err;
654 
655 		adapter->fcoe.up = app->priority;
656 		ixgbe_dcbnl_devreset(dev);
657 	}
658 #endif
659 	return 0;
660 }
661 
662 static int ixgbe_dcbnl_ieee_delapp(struct net_device *dev,
663 				   struct dcb_app *app)
664 {
665 	struct ixgbe_adapter *adapter = netdev_priv(dev);
666 	int err;
667 
668 	if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_IEEE))
669 		return -EINVAL;
670 
671 	err = dcb_ieee_delapp(dev, app);
672 
673 #ifdef IXGBE_FCOE
674 	if (!err && app->selector == IEEE_8021QAZ_APP_SEL_ETHERTYPE &&
675 	    app->protocol == ETH_P_FCOE) {
676 		u8 app_mask = dcb_ieee_getapp_mask(dev, app);
677 
678 		if (app_mask & (1 << adapter->fcoe.up))
679 			return err;
680 
681 		adapter->fcoe.up = app_mask ?
682 				   ffs(app_mask) - 1 : IXGBE_FCOE_DEFTC;
683 		ixgbe_dcbnl_devreset(dev);
684 	}
685 #endif
686 	return err;
687 }
688 
689 static u8 ixgbe_dcbnl_getdcbx(struct net_device *dev)
690 {
691 	struct ixgbe_adapter *adapter = netdev_priv(dev);
692 	return adapter->dcbx_cap;
693 }
694 
695 static u8 ixgbe_dcbnl_setdcbx(struct net_device *dev, u8 mode)
696 {
697 	struct ixgbe_adapter *adapter = netdev_priv(dev);
698 	struct ieee_ets ets = {0};
699 	struct ieee_pfc pfc = {0};
700 	int err = 0;
701 
702 	/* no support for LLD_MANAGED modes or CEE+IEEE */
703 	if ((mode & DCB_CAP_DCBX_LLD_MANAGED) ||
704 	    ((mode & DCB_CAP_DCBX_VER_IEEE) && (mode & DCB_CAP_DCBX_VER_CEE)) ||
705 	    !(mode & DCB_CAP_DCBX_HOST))
706 		return 1;
707 
708 	if (mode == adapter->dcbx_cap)
709 		return 0;
710 
711 	adapter->dcbx_cap = mode;
712 
713 	/* ETS and PFC defaults */
714 	ets.ets_cap = 8;
715 	pfc.pfc_cap = 8;
716 
717 	if (mode & DCB_CAP_DCBX_VER_IEEE) {
718 		ixgbe_dcbnl_ieee_setets(dev, &ets);
719 		ixgbe_dcbnl_ieee_setpfc(dev, &pfc);
720 	} else if (mode & DCB_CAP_DCBX_VER_CEE) {
721 		u8 mask = BIT_PFC | BIT_PG_TX | BIT_PG_RX | BIT_APP_UPCHG;
722 
723 		adapter->dcb_set_bitmap |= mask;
724 		ixgbe_dcbnl_set_all(dev);
725 	} else {
726 		/* Drop into single TC mode strict priority as this
727 		 * indicates CEE and IEEE versions are disabled
728 		 */
729 		ixgbe_dcbnl_ieee_setets(dev, &ets);
730 		ixgbe_dcbnl_ieee_setpfc(dev, &pfc);
731 		err = ixgbe_setup_tc(dev, 0);
732 	}
733 
734 	return err ? 1 : 0;
735 }
736 
737 const struct dcbnl_rtnl_ops dcbnl_ops = {
738 	.ieee_getets	= ixgbe_dcbnl_ieee_getets,
739 	.ieee_setets	= ixgbe_dcbnl_ieee_setets,
740 	.ieee_getpfc	= ixgbe_dcbnl_ieee_getpfc,
741 	.ieee_setpfc	= ixgbe_dcbnl_ieee_setpfc,
742 	.ieee_setapp	= ixgbe_dcbnl_ieee_setapp,
743 	.ieee_delapp	= ixgbe_dcbnl_ieee_delapp,
744 	.getstate	= ixgbe_dcbnl_get_state,
745 	.setstate	= ixgbe_dcbnl_set_state,
746 	.getpermhwaddr	= ixgbe_dcbnl_get_perm_hw_addr,
747 	.setpgtccfgtx	= ixgbe_dcbnl_set_pg_tc_cfg_tx,
748 	.setpgbwgcfgtx	= ixgbe_dcbnl_set_pg_bwg_cfg_tx,
749 	.setpgtccfgrx	= ixgbe_dcbnl_set_pg_tc_cfg_rx,
750 	.setpgbwgcfgrx	= ixgbe_dcbnl_set_pg_bwg_cfg_rx,
751 	.getpgtccfgtx	= ixgbe_dcbnl_get_pg_tc_cfg_tx,
752 	.getpgbwgcfgtx	= ixgbe_dcbnl_get_pg_bwg_cfg_tx,
753 	.getpgtccfgrx	= ixgbe_dcbnl_get_pg_tc_cfg_rx,
754 	.getpgbwgcfgrx	= ixgbe_dcbnl_get_pg_bwg_cfg_rx,
755 	.setpfccfg	= ixgbe_dcbnl_set_pfc_cfg,
756 	.getpfccfg	= ixgbe_dcbnl_get_pfc_cfg,
757 	.setall		= ixgbe_dcbnl_set_all,
758 	.getcap		= ixgbe_dcbnl_getcap,
759 	.getnumtcs	= ixgbe_dcbnl_getnumtcs,
760 	.setnumtcs	= ixgbe_dcbnl_setnumtcs,
761 	.getpfcstate	= ixgbe_dcbnl_getpfcstate,
762 	.setpfcstate	= ixgbe_dcbnl_setpfcstate,
763 	.getapp		= ixgbe_dcbnl_getapp,
764 	.getdcbx	= ixgbe_dcbnl_getdcbx,
765 	.setdcbx	= ixgbe_dcbnl_setdcbx,
766 };
767