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 	int err = 0;
155 	u8 prio_tc[MAX_USER_PRIORITY] = {0};
156 	int i;
157 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
158 
159 	/* Fail command if not in CEE mode */
160 	if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_CEE))
161 		return 1;
162 
163 	/* verify there is something to do, if not then exit */
164 	if (!!state != !(adapter->flags & IXGBE_FLAG_DCB_ENABLED))
165 		goto out;
166 
167 	if (state > 0) {
168 		err = ixgbe_setup_tc(netdev, adapter->dcb_cfg.num_tcs.pg_tcs);
169 		ixgbe_dcb_unpack_map(&adapter->dcb_cfg, DCB_TX_CONFIG, prio_tc);
170 	} else {
171 		err = ixgbe_setup_tc(netdev, 0);
172 	}
173 
174 	if (err)
175 		goto out;
176 
177 	for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
178 		netdev_set_prio_tc_map(netdev, i, prio_tc[i]);
179 
180 out:
181 	return err ? 1 : 0;
182 }
183 
184 static void ixgbe_dcbnl_get_perm_hw_addr(struct net_device *netdev,
185 					 u8 *perm_addr)
186 {
187 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
188 	int i, j;
189 
190 	memset(perm_addr, 0xff, MAX_ADDR_LEN);
191 
192 	for (i = 0; i < netdev->addr_len; i++)
193 		perm_addr[i] = adapter->hw.mac.perm_addr[i];
194 
195 	switch (adapter->hw.mac.type) {
196 	case ixgbe_mac_82599EB:
197 	case ixgbe_mac_X540:
198 		for (j = 0; j < netdev->addr_len; j++, i++)
199 			perm_addr[i] = adapter->hw.mac.san_addr[j];
200 		break;
201 	default:
202 		break;
203 	}
204 }
205 
206 static void ixgbe_dcbnl_set_pg_tc_cfg_tx(struct net_device *netdev, int tc,
207                                          u8 prio, u8 bwg_id, u8 bw_pct,
208                                          u8 up_map)
209 {
210 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
211 
212 	if (prio != DCB_ATTR_VALUE_UNDEFINED)
213 		adapter->temp_dcb_cfg.tc_config[tc].path[0].prio_type = prio;
214 	if (bwg_id != DCB_ATTR_VALUE_UNDEFINED)
215 		adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_id = bwg_id;
216 	if (bw_pct != DCB_ATTR_VALUE_UNDEFINED)
217 		adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_percent =
218 			bw_pct;
219 	if (up_map != DCB_ATTR_VALUE_UNDEFINED)
220 		adapter->temp_dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap =
221 			up_map;
222 }
223 
224 static void ixgbe_dcbnl_set_pg_bwg_cfg_tx(struct net_device *netdev, int bwg_id,
225                                           u8 bw_pct)
226 {
227 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
228 
229 	adapter->temp_dcb_cfg.bw_percentage[0][bwg_id] = bw_pct;
230 }
231 
232 static void ixgbe_dcbnl_set_pg_tc_cfg_rx(struct net_device *netdev, int tc,
233                                          u8 prio, u8 bwg_id, u8 bw_pct,
234                                          u8 up_map)
235 {
236 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
237 
238 	if (prio != DCB_ATTR_VALUE_UNDEFINED)
239 		adapter->temp_dcb_cfg.tc_config[tc].path[1].prio_type = prio;
240 	if (bwg_id != DCB_ATTR_VALUE_UNDEFINED)
241 		adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_id = bwg_id;
242 	if (bw_pct != DCB_ATTR_VALUE_UNDEFINED)
243 		adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_percent =
244 			bw_pct;
245 	if (up_map != DCB_ATTR_VALUE_UNDEFINED)
246 		adapter->temp_dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap =
247 			up_map;
248 }
249 
250 static void ixgbe_dcbnl_set_pg_bwg_cfg_rx(struct net_device *netdev, int bwg_id,
251                                           u8 bw_pct)
252 {
253 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
254 
255 	adapter->temp_dcb_cfg.bw_percentage[1][bwg_id] = bw_pct;
256 }
257 
258 static void ixgbe_dcbnl_get_pg_tc_cfg_tx(struct net_device *netdev, int tc,
259                                          u8 *prio, u8 *bwg_id, u8 *bw_pct,
260                                          u8 *up_map)
261 {
262 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
263 
264 	*prio = adapter->dcb_cfg.tc_config[tc].path[0].prio_type;
265 	*bwg_id = adapter->dcb_cfg.tc_config[tc].path[0].bwg_id;
266 	*bw_pct = adapter->dcb_cfg.tc_config[tc].path[0].bwg_percent;
267 	*up_map = adapter->dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap;
268 }
269 
270 static void ixgbe_dcbnl_get_pg_bwg_cfg_tx(struct net_device *netdev, int bwg_id,
271                                           u8 *bw_pct)
272 {
273 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
274 
275 	*bw_pct = adapter->dcb_cfg.bw_percentage[0][bwg_id];
276 }
277 
278 static void ixgbe_dcbnl_get_pg_tc_cfg_rx(struct net_device *netdev, int tc,
279                                          u8 *prio, u8 *bwg_id, u8 *bw_pct,
280                                          u8 *up_map)
281 {
282 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
283 
284 	*prio = adapter->dcb_cfg.tc_config[tc].path[1].prio_type;
285 	*bwg_id = adapter->dcb_cfg.tc_config[tc].path[1].bwg_id;
286 	*bw_pct = adapter->dcb_cfg.tc_config[tc].path[1].bwg_percent;
287 	*up_map = adapter->dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap;
288 }
289 
290 static void ixgbe_dcbnl_get_pg_bwg_cfg_rx(struct net_device *netdev, int bwg_id,
291                                           u8 *bw_pct)
292 {
293 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
294 
295 	*bw_pct = adapter->dcb_cfg.bw_percentage[1][bwg_id];
296 }
297 
298 static void ixgbe_dcbnl_set_pfc_cfg(struct net_device *netdev, int priority,
299                                     u8 setting)
300 {
301 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
302 
303 	adapter->temp_dcb_cfg.tc_config[priority].dcb_pfc = setting;
304 	if (adapter->temp_dcb_cfg.tc_config[priority].dcb_pfc !=
305 	    adapter->dcb_cfg.tc_config[priority].dcb_pfc)
306 		adapter->temp_dcb_cfg.pfc_mode_enable = true;
307 }
308 
309 static void ixgbe_dcbnl_get_pfc_cfg(struct net_device *netdev, int priority,
310                                     u8 *setting)
311 {
312 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
313 
314 	*setting = adapter->dcb_cfg.tc_config[priority].dcb_pfc;
315 }
316 
317 #ifdef IXGBE_FCOE
318 static void ixgbe_dcbnl_devreset(struct net_device *dev)
319 {
320 	struct ixgbe_adapter *adapter = netdev_priv(dev);
321 
322 	while (test_and_set_bit(__IXGBE_RESETTING, &adapter->state))
323 		usleep_range(1000, 2000);
324 
325 	if (netif_running(dev))
326 		dev->netdev_ops->ndo_stop(dev);
327 
328 	ixgbe_clear_interrupt_scheme(adapter);
329 	ixgbe_init_interrupt_scheme(adapter);
330 
331 	if (netif_running(dev))
332 		dev->netdev_ops->ndo_open(dev);
333 
334 	clear_bit(__IXGBE_RESETTING, &adapter->state);
335 }
336 #endif
337 
338 static u8 ixgbe_dcbnl_set_all(struct net_device *netdev)
339 {
340 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
341 	int ret = DCB_NO_HW_CHG;
342 	int i;
343 
344 	/* Fail command if not in CEE mode */
345 	if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_CEE))
346 		return ret;
347 
348 	adapter->dcb_set_bitmap |= ixgbe_copy_dcb_cfg(adapter,
349 						      MAX_TRAFFIC_CLASS);
350 	if (!adapter->dcb_set_bitmap)
351 		return ret;
352 
353 	if (adapter->dcb_cfg.pfc_mode_enable) {
354 		switch (adapter->hw.mac.type) {
355 		case ixgbe_mac_82599EB:
356 		case ixgbe_mac_X540:
357 			if (adapter->hw.fc.current_mode != ixgbe_fc_pfc)
358 				adapter->last_lfc_mode =
359 				                  adapter->hw.fc.current_mode;
360 			break;
361 		default:
362 			break;
363 		}
364 		adapter->hw.fc.requested_mode = ixgbe_fc_pfc;
365 	} else {
366 		switch (adapter->hw.mac.type) {
367 		case ixgbe_mac_82598EB:
368 			adapter->hw.fc.requested_mode = ixgbe_fc_none;
369 			break;
370 		case ixgbe_mac_82599EB:
371 		case ixgbe_mac_X540:
372 			adapter->hw.fc.requested_mode = adapter->last_lfc_mode;
373 			break;
374 		default:
375 			break;
376 		}
377 	}
378 
379 	if (adapter->dcb_set_bitmap & (BIT_PG_TX|BIT_PG_RX)) {
380 		u16 refill[MAX_TRAFFIC_CLASS], max[MAX_TRAFFIC_CLASS];
381 		u8 bwg_id[MAX_TRAFFIC_CLASS], prio_type[MAX_TRAFFIC_CLASS];
382 		/* Priority to TC mapping in CEE case default to 1:1 */
383 		u8 prio_tc[MAX_USER_PRIORITY];
384 		int max_frame = adapter->netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
385 
386 #ifdef IXGBE_FCOE
387 		if (adapter->netdev->features & NETIF_F_FCOE_MTU)
388 			max_frame = max(max_frame, IXGBE_FCOE_JUMBO_FRAME_SIZE);
389 #endif
390 
391 		ixgbe_dcb_calculate_tc_credits(&adapter->hw, &adapter->dcb_cfg,
392 					       max_frame, DCB_TX_CONFIG);
393 		ixgbe_dcb_calculate_tc_credits(&adapter->hw, &adapter->dcb_cfg,
394 					       max_frame, DCB_RX_CONFIG);
395 
396 		ixgbe_dcb_unpack_refill(&adapter->dcb_cfg,
397 					DCB_TX_CONFIG, refill);
398 		ixgbe_dcb_unpack_max(&adapter->dcb_cfg, max);
399 		ixgbe_dcb_unpack_bwgid(&adapter->dcb_cfg,
400 				       DCB_TX_CONFIG, bwg_id);
401 		ixgbe_dcb_unpack_prio(&adapter->dcb_cfg,
402 				      DCB_TX_CONFIG, prio_type);
403 		ixgbe_dcb_unpack_map(&adapter->dcb_cfg,
404 				     DCB_TX_CONFIG, prio_tc);
405 
406 		ixgbe_dcb_hw_ets_config(&adapter->hw, refill, max,
407 					bwg_id, prio_type, prio_tc);
408 
409 		for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
410 			netdev_set_prio_tc_map(netdev, i, prio_tc[i]);
411 
412 		ret = DCB_HW_CHG_RST;
413 	}
414 
415 	if (adapter->dcb_set_bitmap & BIT_PFC) {
416 		u8 pfc_en;
417 		u8 prio_tc[MAX_USER_PRIORITY];
418 
419 		ixgbe_dcb_unpack_map(&adapter->dcb_cfg,
420 				     DCB_TX_CONFIG, prio_tc);
421 		ixgbe_dcb_unpack_pfc(&adapter->dcb_cfg, &pfc_en);
422 		ixgbe_dcb_hw_pfc_config(&adapter->hw, pfc_en, prio_tc);
423 		if (ret != DCB_HW_CHG_RST)
424 			ret = DCB_HW_CHG;
425 	}
426 
427 	if (adapter->dcb_cfg.pfc_mode_enable)
428 		adapter->hw.fc.current_mode = ixgbe_fc_pfc;
429 
430 #ifdef IXGBE_FCOE
431 	/* Reprogam FCoE hardware offloads when the traffic class
432 	 * FCoE is using changes. This happens if the APP info
433 	 * changes or the up2tc mapping is updated.
434 	 */
435 	if (adapter->dcb_set_bitmap & BIT_APP_UPCHG) {
436 		struct dcb_app app = {
437 				      .selector = DCB_APP_IDTYPE_ETHTYPE,
438 				      .protocol = ETH_P_FCOE,
439 				     };
440 		u8 up = dcb_getapp(netdev, &app);
441 
442 		adapter->fcoe.up = ffs(up) - 1;
443 		ixgbe_dcbnl_devreset(netdev);
444 		ret = DCB_HW_CHG_RST;
445 	}
446 #endif
447 
448 	adapter->dcb_set_bitmap = 0x00;
449 	return ret;
450 }
451 
452 static u8 ixgbe_dcbnl_getcap(struct net_device *netdev, int capid, u8 *cap)
453 {
454 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
455 
456 	switch (capid) {
457 	case DCB_CAP_ATTR_PG:
458 		*cap = true;
459 		break;
460 	case DCB_CAP_ATTR_PFC:
461 		*cap = true;
462 		break;
463 	case DCB_CAP_ATTR_UP2TC:
464 		*cap = false;
465 		break;
466 	case DCB_CAP_ATTR_PG_TCS:
467 		*cap = 0x80;
468 		break;
469 	case DCB_CAP_ATTR_PFC_TCS:
470 		*cap = 0x80;
471 		break;
472 	case DCB_CAP_ATTR_GSP:
473 		*cap = true;
474 		break;
475 	case DCB_CAP_ATTR_BCN:
476 		*cap = false;
477 		break;
478 	case DCB_CAP_ATTR_DCBX:
479 		*cap = adapter->dcbx_cap;
480 		break;
481 	default:
482 		*cap = false;
483 		break;
484 	}
485 
486 	return 0;
487 }
488 
489 static int ixgbe_dcbnl_getnumtcs(struct net_device *netdev, int tcid, u8 *num)
490 {
491 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
492 	u8 rval = 0;
493 
494 	if (adapter->flags & IXGBE_FLAG_DCB_ENABLED) {
495 		switch (tcid) {
496 		case DCB_NUMTCS_ATTR_PG:
497 			*num = adapter->dcb_cfg.num_tcs.pg_tcs;
498 			break;
499 		case DCB_NUMTCS_ATTR_PFC:
500 			*num = adapter->dcb_cfg.num_tcs.pfc_tcs;
501 			break;
502 		default:
503 			rval = -EINVAL;
504 			break;
505 		}
506 	} else {
507 		rval = -EINVAL;
508 	}
509 
510 	return rval;
511 }
512 
513 static int ixgbe_dcbnl_setnumtcs(struct net_device *netdev, int tcid, u8 num)
514 {
515 	return -EINVAL;
516 }
517 
518 static u8 ixgbe_dcbnl_getpfcstate(struct net_device *netdev)
519 {
520 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
521 
522 	return adapter->dcb_cfg.pfc_mode_enable;
523 }
524 
525 static void ixgbe_dcbnl_setpfcstate(struct net_device *netdev, u8 state)
526 {
527 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
528 
529 	adapter->temp_dcb_cfg.pfc_mode_enable = state;
530 }
531 
532 /**
533  * ixgbe_dcbnl_getapp - retrieve the DCBX application user priority
534  * @netdev : the corresponding netdev
535  * @idtype : identifies the id as ether type or TCP/UDP port number
536  * @id: id is either ether type or TCP/UDP port number
537  *
538  * Returns : on success, returns a non-zero 802.1p user priority bitmap
539  * otherwise returns 0 as the invalid user priority bitmap to indicate an
540  * error.
541  */
542 static u8 ixgbe_dcbnl_getapp(struct net_device *netdev, u8 idtype, u16 id)
543 {
544 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
545 	struct dcb_app app = {
546 				.selector = idtype,
547 				.protocol = id,
548 			     };
549 
550 	if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_CEE))
551 		return 0;
552 
553 	return dcb_getapp(netdev, &app);
554 }
555 
556 static int ixgbe_dcbnl_ieee_getets(struct net_device *dev,
557 				   struct ieee_ets *ets)
558 {
559 	struct ixgbe_adapter *adapter = netdev_priv(dev);
560 	struct ieee_ets *my_ets = adapter->ixgbe_ieee_ets;
561 
562 	ets->ets_cap = adapter->dcb_cfg.num_tcs.pg_tcs;
563 
564 	/* No IEEE PFC settings available */
565 	if (!my_ets)
566 		return 0;
567 
568 	ets->cbs = my_ets->cbs;
569 	memcpy(ets->tc_tx_bw, my_ets->tc_tx_bw, sizeof(ets->tc_tx_bw));
570 	memcpy(ets->tc_rx_bw, my_ets->tc_rx_bw, sizeof(ets->tc_rx_bw));
571 	memcpy(ets->tc_tsa, my_ets->tc_tsa, sizeof(ets->tc_tsa));
572 	memcpy(ets->prio_tc, my_ets->prio_tc, sizeof(ets->prio_tc));
573 	return 0;
574 }
575 
576 static int ixgbe_dcbnl_ieee_setets(struct net_device *dev,
577 				   struct ieee_ets *ets)
578 {
579 	struct ixgbe_adapter *adapter = netdev_priv(dev);
580 	int max_frame = dev->mtu + ETH_HLEN + ETH_FCS_LEN;
581 	int i, err = 0;
582 	__u8 max_tc = 0;
583 
584 	if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_IEEE))
585 		return -EINVAL;
586 
587 	if (!adapter->ixgbe_ieee_ets) {
588 		adapter->ixgbe_ieee_ets = kmalloc(sizeof(struct ieee_ets),
589 						  GFP_KERNEL);
590 		if (!adapter->ixgbe_ieee_ets)
591 			return -ENOMEM;
592 	}
593 
594 	memcpy(adapter->ixgbe_ieee_ets, ets, sizeof(*adapter->ixgbe_ieee_ets));
595 
596 	for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
597 		if (ets->prio_tc[i] > max_tc)
598 			max_tc = ets->prio_tc[i];
599 	}
600 
601 	if (max_tc)
602 		max_tc++;
603 
604 	if (max_tc > adapter->dcb_cfg.num_tcs.pg_tcs)
605 		return -EINVAL;
606 
607 	if (max_tc != netdev_get_num_tc(dev))
608 		err = ixgbe_setup_tc(dev, max_tc);
609 
610 	if (err)
611 		goto err_out;
612 
613 	for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
614 		netdev_set_prio_tc_map(dev, i, ets->prio_tc[i]);
615 
616 	err = ixgbe_dcb_hw_ets(&adapter->hw, ets, max_frame);
617 err_out:
618 	return err;
619 }
620 
621 static int ixgbe_dcbnl_ieee_getpfc(struct net_device *dev,
622 				   struct ieee_pfc *pfc)
623 {
624 	struct ixgbe_adapter *adapter = netdev_priv(dev);
625 	struct ieee_pfc *my_pfc = adapter->ixgbe_ieee_pfc;
626 	int i;
627 
628 	pfc->pfc_cap = adapter->dcb_cfg.num_tcs.pfc_tcs;
629 
630 	/* No IEEE PFC settings available */
631 	if (!my_pfc)
632 		return 0;
633 
634 	pfc->pfc_en = my_pfc->pfc_en;
635 	pfc->mbc = my_pfc->mbc;
636 	pfc->delay = my_pfc->delay;
637 
638 	for (i = 0; i < MAX_TRAFFIC_CLASS; i++) {
639 		pfc->requests[i] = adapter->stats.pxoffrxc[i];
640 		pfc->indications[i] = adapter->stats.pxofftxc[i];
641 	}
642 
643 	return 0;
644 }
645 
646 static int ixgbe_dcbnl_ieee_setpfc(struct net_device *dev,
647 				   struct ieee_pfc *pfc)
648 {
649 	struct ixgbe_adapter *adapter = netdev_priv(dev);
650 	u8 *prio_tc;
651 
652 	if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_IEEE))
653 		return -EINVAL;
654 
655 	if (!adapter->ixgbe_ieee_pfc) {
656 		adapter->ixgbe_ieee_pfc = kmalloc(sizeof(struct ieee_pfc),
657 						  GFP_KERNEL);
658 		if (!adapter->ixgbe_ieee_pfc)
659 			return -ENOMEM;
660 	}
661 
662 	prio_tc = adapter->ixgbe_ieee_ets->prio_tc;
663 	memcpy(adapter->ixgbe_ieee_pfc, pfc, sizeof(*adapter->ixgbe_ieee_pfc));
664 	return ixgbe_dcb_hw_pfc_config(&adapter->hw, pfc->pfc_en, prio_tc);
665 }
666 
667 static int ixgbe_dcbnl_ieee_setapp(struct net_device *dev,
668 				   struct dcb_app *app)
669 {
670 	struct ixgbe_adapter *adapter = netdev_priv(dev);
671 	int err = -EINVAL;
672 
673 	if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_IEEE))
674 		return err;
675 
676 	err = dcb_ieee_setapp(dev, app);
677 
678 #ifdef IXGBE_FCOE
679 	if (!err && app->selector == IEEE_8021QAZ_APP_SEL_ETHERTYPE &&
680 	    app->protocol == ETH_P_FCOE) {
681 		u8 app_mask = dcb_ieee_getapp_mask(dev, app);
682 
683 		if (app_mask & (1 << adapter->fcoe.up))
684 			return err;
685 
686 		adapter->fcoe.up = app->priority;
687 		ixgbe_dcbnl_devreset(dev);
688 	}
689 #endif
690 	return 0;
691 }
692 
693 static int ixgbe_dcbnl_ieee_delapp(struct net_device *dev,
694 				   struct dcb_app *app)
695 {
696 	struct ixgbe_adapter *adapter = netdev_priv(dev);
697 	int err;
698 
699 	if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_IEEE))
700 		return -EINVAL;
701 
702 	err = dcb_ieee_delapp(dev, app);
703 
704 #ifdef IXGBE_FCOE
705 	if (!err && app->selector == IEEE_8021QAZ_APP_SEL_ETHERTYPE &&
706 	    app->protocol == ETH_P_FCOE) {
707 		u8 app_mask = dcb_ieee_getapp_mask(dev, app);
708 
709 		if (app_mask & (1 << adapter->fcoe.up))
710 			return err;
711 
712 		adapter->fcoe.up = app_mask ?
713 				   ffs(app_mask) - 1 : IXGBE_FCOE_DEFTC;
714 		ixgbe_dcbnl_devreset(dev);
715 	}
716 #endif
717 	return err;
718 }
719 
720 static u8 ixgbe_dcbnl_getdcbx(struct net_device *dev)
721 {
722 	struct ixgbe_adapter *adapter = netdev_priv(dev);
723 	return adapter->dcbx_cap;
724 }
725 
726 static u8 ixgbe_dcbnl_setdcbx(struct net_device *dev, u8 mode)
727 {
728 	struct ixgbe_adapter *adapter = netdev_priv(dev);
729 	struct ieee_ets ets = {0};
730 	struct ieee_pfc pfc = {0};
731 	int err = 0;
732 
733 	/* no support for LLD_MANAGED modes or CEE+IEEE */
734 	if ((mode & DCB_CAP_DCBX_LLD_MANAGED) ||
735 	    ((mode & DCB_CAP_DCBX_VER_IEEE) && (mode & DCB_CAP_DCBX_VER_CEE)) ||
736 	    !(mode & DCB_CAP_DCBX_HOST))
737 		return 1;
738 
739 	if (mode == adapter->dcbx_cap)
740 		return 0;
741 
742 	adapter->dcbx_cap = mode;
743 
744 	/* ETS and PFC defaults */
745 	ets.ets_cap = 8;
746 	pfc.pfc_cap = 8;
747 
748 	if (mode & DCB_CAP_DCBX_VER_IEEE) {
749 		ixgbe_dcbnl_ieee_setets(dev, &ets);
750 		ixgbe_dcbnl_ieee_setpfc(dev, &pfc);
751 	} else if (mode & DCB_CAP_DCBX_VER_CEE) {
752 		u8 mask = BIT_PFC | BIT_PG_TX | BIT_PG_RX | BIT_APP_UPCHG;
753 
754 		adapter->dcb_set_bitmap |= mask;
755 		ixgbe_dcbnl_set_all(dev);
756 	} else {
757 		/* Drop into single TC mode strict priority as this
758 		 * indicates CEE and IEEE versions are disabled
759 		 */
760 		ixgbe_dcbnl_ieee_setets(dev, &ets);
761 		ixgbe_dcbnl_ieee_setpfc(dev, &pfc);
762 		err = ixgbe_setup_tc(dev, 0);
763 	}
764 
765 	return err ? 1 : 0;
766 }
767 
768 const struct dcbnl_rtnl_ops dcbnl_ops = {
769 	.ieee_getets	= ixgbe_dcbnl_ieee_getets,
770 	.ieee_setets	= ixgbe_dcbnl_ieee_setets,
771 	.ieee_getpfc	= ixgbe_dcbnl_ieee_getpfc,
772 	.ieee_setpfc	= ixgbe_dcbnl_ieee_setpfc,
773 	.ieee_setapp	= ixgbe_dcbnl_ieee_setapp,
774 	.ieee_delapp	= ixgbe_dcbnl_ieee_delapp,
775 	.getstate	= ixgbe_dcbnl_get_state,
776 	.setstate	= ixgbe_dcbnl_set_state,
777 	.getpermhwaddr	= ixgbe_dcbnl_get_perm_hw_addr,
778 	.setpgtccfgtx	= ixgbe_dcbnl_set_pg_tc_cfg_tx,
779 	.setpgbwgcfgtx	= ixgbe_dcbnl_set_pg_bwg_cfg_tx,
780 	.setpgtccfgrx	= ixgbe_dcbnl_set_pg_tc_cfg_rx,
781 	.setpgbwgcfgrx	= ixgbe_dcbnl_set_pg_bwg_cfg_rx,
782 	.getpgtccfgtx	= ixgbe_dcbnl_get_pg_tc_cfg_tx,
783 	.getpgbwgcfgtx	= ixgbe_dcbnl_get_pg_bwg_cfg_tx,
784 	.getpgtccfgrx	= ixgbe_dcbnl_get_pg_tc_cfg_rx,
785 	.getpgbwgcfgrx	= ixgbe_dcbnl_get_pg_bwg_cfg_rx,
786 	.setpfccfg	= ixgbe_dcbnl_set_pfc_cfg,
787 	.getpfccfg	= ixgbe_dcbnl_get_pfc_cfg,
788 	.setall		= ixgbe_dcbnl_set_all,
789 	.getcap		= ixgbe_dcbnl_getcap,
790 	.getnumtcs	= ixgbe_dcbnl_getnumtcs,
791 	.setnumtcs	= ixgbe_dcbnl_setnumtcs,
792 	.getpfcstate	= ixgbe_dcbnl_getpfcstate,
793 	.setpfcstate	= ixgbe_dcbnl_setpfcstate,
794 	.getapp		= ixgbe_dcbnl_getapp,
795 	.getdcbx	= ixgbe_dcbnl_getdcbx,
796 	.setdcbx	= ixgbe_dcbnl_setdcbx,
797 };
798