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 
ixgbe_copy_dcb_cfg(struct ixgbe_dcb_config * src_dcb_cfg,struct ixgbe_dcb_config * dst_dcb_cfg,int tc_max)47 int ixgbe_copy_dcb_cfg(struct ixgbe_dcb_config *src_dcb_cfg,
48                        struct ixgbe_dcb_config *dst_dcb_cfg, int tc_max)
49 {
50 	struct tc_configuration *src_tc_cfg = NULL;
51 	struct tc_configuration *dst_tc_cfg = NULL;
52 	int i;
53 
54 	if (!src_dcb_cfg || !dst_dcb_cfg)
55 		return -EINVAL;
56 
57 	for (i = DCB_PG_ATTR_TC_0; i < tc_max + DCB_PG_ATTR_TC_0; i++) {
58 		src_tc_cfg = &src_dcb_cfg->tc_config[i - DCB_PG_ATTR_TC_0];
59 		dst_tc_cfg = &dst_dcb_cfg->tc_config[i - DCB_PG_ATTR_TC_0];
60 
61 		dst_tc_cfg->path[DCB_TX_CONFIG].prio_type =
62 				src_tc_cfg->path[DCB_TX_CONFIG].prio_type;
63 
64 		dst_tc_cfg->path[DCB_TX_CONFIG].bwg_id =
65 				src_tc_cfg->path[DCB_TX_CONFIG].bwg_id;
66 
67 		dst_tc_cfg->path[DCB_TX_CONFIG].bwg_percent =
68 				src_tc_cfg->path[DCB_TX_CONFIG].bwg_percent;
69 
70 		dst_tc_cfg->path[DCB_TX_CONFIG].up_to_tc_bitmap =
71 				src_tc_cfg->path[DCB_TX_CONFIG].up_to_tc_bitmap;
72 
73 		dst_tc_cfg->path[DCB_RX_CONFIG].prio_type =
74 				src_tc_cfg->path[DCB_RX_CONFIG].prio_type;
75 
76 		dst_tc_cfg->path[DCB_RX_CONFIG].bwg_id =
77 				src_tc_cfg->path[DCB_RX_CONFIG].bwg_id;
78 
79 		dst_tc_cfg->path[DCB_RX_CONFIG].bwg_percent =
80 				src_tc_cfg->path[DCB_RX_CONFIG].bwg_percent;
81 
82 		dst_tc_cfg->path[DCB_RX_CONFIG].up_to_tc_bitmap =
83 				src_tc_cfg->path[DCB_RX_CONFIG].up_to_tc_bitmap;
84 	}
85 
86 	for (i = DCB_PG_ATTR_BW_ID_0; i < DCB_PG_ATTR_BW_ID_MAX; i++) {
87 		dst_dcb_cfg->bw_percentage[DCB_TX_CONFIG]
88 			[i-DCB_PG_ATTR_BW_ID_0] = src_dcb_cfg->bw_percentage
89 				[DCB_TX_CONFIG][i-DCB_PG_ATTR_BW_ID_0];
90 		dst_dcb_cfg->bw_percentage[DCB_RX_CONFIG]
91 			[i-DCB_PG_ATTR_BW_ID_0] = src_dcb_cfg->bw_percentage
92 				[DCB_RX_CONFIG][i-DCB_PG_ATTR_BW_ID_0];
93 	}
94 
95 	for (i = DCB_PFC_UP_ATTR_0; i < DCB_PFC_UP_ATTR_MAX; i++) {
96 		dst_dcb_cfg->tc_config[i - DCB_PFC_UP_ATTR_0].dcb_pfc =
97 			src_dcb_cfg->tc_config[i - DCB_PFC_UP_ATTR_0].dcb_pfc;
98 	}
99 
100 	dst_dcb_cfg->pfc_mode_enable = src_dcb_cfg->pfc_mode_enable;
101 
102 	return 0;
103 }
104 
ixgbe_dcbnl_get_state(struct net_device * netdev)105 static u8 ixgbe_dcbnl_get_state(struct net_device *netdev)
106 {
107 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
108 
109 	return !!(adapter->flags & IXGBE_FLAG_DCB_ENABLED);
110 }
111 
ixgbe_dcbnl_set_state(struct net_device * netdev,u8 state)112 static u8 ixgbe_dcbnl_set_state(struct net_device *netdev, u8 state)
113 {
114 	u8 err = 0;
115 	u8 prio_tc[MAX_USER_PRIORITY] = {0};
116 	int i;
117 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
118 
119 	/* Fail command if not in CEE mode */
120 	if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_CEE))
121 		return 1;
122 
123 	/* verify there is something to do, if not then exit */
124 	if (!!state != !(adapter->flags & IXGBE_FLAG_DCB_ENABLED))
125 		return err;
126 
127 	if (state > 0) {
128 		err = ixgbe_setup_tc(netdev, adapter->dcb_cfg.num_tcs.pg_tcs);
129 		ixgbe_dcb_unpack_map(&adapter->dcb_cfg, DCB_TX_CONFIG, prio_tc);
130 	} else {
131 		err = ixgbe_setup_tc(netdev, 0);
132 	}
133 
134 	for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
135 		netdev_set_prio_tc_map(netdev, i, prio_tc[i]);
136 
137 	return err;
138 }
139 
ixgbe_dcbnl_get_perm_hw_addr(struct net_device * netdev,u8 * perm_addr)140 static void ixgbe_dcbnl_get_perm_hw_addr(struct net_device *netdev,
141 					 u8 *perm_addr)
142 {
143 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
144 	int i, j;
145 
146 	memset(perm_addr, 0xff, MAX_ADDR_LEN);
147 
148 	for (i = 0; i < netdev->addr_len; i++)
149 		perm_addr[i] = adapter->hw.mac.perm_addr[i];
150 
151 	switch (adapter->hw.mac.type) {
152 	case ixgbe_mac_82599EB:
153 	case ixgbe_mac_X540:
154 		for (j = 0; j < netdev->addr_len; j++, i++)
155 			perm_addr[i] = adapter->hw.mac.san_addr[j];
156 		break;
157 	default:
158 		break;
159 	}
160 }
161 
ixgbe_dcbnl_set_pg_tc_cfg_tx(struct net_device * netdev,int tc,u8 prio,u8 bwg_id,u8 bw_pct,u8 up_map)162 static void ixgbe_dcbnl_set_pg_tc_cfg_tx(struct net_device *netdev, int tc,
163                                          u8 prio, u8 bwg_id, u8 bw_pct,
164                                          u8 up_map)
165 {
166 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
167 
168 	if (prio != DCB_ATTR_VALUE_UNDEFINED)
169 		adapter->temp_dcb_cfg.tc_config[tc].path[0].prio_type = prio;
170 	if (bwg_id != DCB_ATTR_VALUE_UNDEFINED)
171 		adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_id = bwg_id;
172 	if (bw_pct != DCB_ATTR_VALUE_UNDEFINED)
173 		adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_percent =
174 			bw_pct;
175 	if (up_map != DCB_ATTR_VALUE_UNDEFINED)
176 		adapter->temp_dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap =
177 			up_map;
178 
179 	if ((adapter->temp_dcb_cfg.tc_config[tc].path[0].prio_type !=
180 	     adapter->dcb_cfg.tc_config[tc].path[0].prio_type) ||
181 	    (adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_id !=
182 	     adapter->dcb_cfg.tc_config[tc].path[0].bwg_id) ||
183 	    (adapter->temp_dcb_cfg.tc_config[tc].path[0].bwg_percent !=
184 	     adapter->dcb_cfg.tc_config[tc].path[0].bwg_percent) ||
185 	    (adapter->temp_dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap !=
186 	     adapter->dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap))
187 		adapter->dcb_set_bitmap |= BIT_PG_TX;
188 
189 	if (adapter->temp_dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap !=
190 	     adapter->dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap)
191 		adapter->dcb_set_bitmap |= BIT_PFC | BIT_APP_UPCHG;
192 }
193 
ixgbe_dcbnl_set_pg_bwg_cfg_tx(struct net_device * netdev,int bwg_id,u8 bw_pct)194 static void ixgbe_dcbnl_set_pg_bwg_cfg_tx(struct net_device *netdev, int bwg_id,
195                                           u8 bw_pct)
196 {
197 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
198 
199 	adapter->temp_dcb_cfg.bw_percentage[0][bwg_id] = bw_pct;
200 
201 	if (adapter->temp_dcb_cfg.bw_percentage[0][bwg_id] !=
202 	    adapter->dcb_cfg.bw_percentage[0][bwg_id])
203 		adapter->dcb_set_bitmap |= BIT_PG_TX;
204 }
205 
ixgbe_dcbnl_set_pg_tc_cfg_rx(struct net_device * netdev,int tc,u8 prio,u8 bwg_id,u8 bw_pct,u8 up_map)206 static void ixgbe_dcbnl_set_pg_tc_cfg_rx(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[1].prio_type = prio;
214 	if (bwg_id != DCB_ATTR_VALUE_UNDEFINED)
215 		adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_id = bwg_id;
216 	if (bw_pct != DCB_ATTR_VALUE_UNDEFINED)
217 		adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_percent =
218 			bw_pct;
219 	if (up_map != DCB_ATTR_VALUE_UNDEFINED)
220 		adapter->temp_dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap =
221 			up_map;
222 
223 	if ((adapter->temp_dcb_cfg.tc_config[tc].path[1].prio_type !=
224 	     adapter->dcb_cfg.tc_config[tc].path[1].prio_type) ||
225 	    (adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_id !=
226 	     adapter->dcb_cfg.tc_config[tc].path[1].bwg_id) ||
227 	    (adapter->temp_dcb_cfg.tc_config[tc].path[1].bwg_percent !=
228 	     adapter->dcb_cfg.tc_config[tc].path[1].bwg_percent) ||
229 	    (adapter->temp_dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap !=
230 	     adapter->dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap))
231 		adapter->dcb_set_bitmap |= BIT_PG_RX;
232 
233 	if (adapter->temp_dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap !=
234 	     adapter->dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap)
235 		adapter->dcb_set_bitmap |= BIT_PFC;
236 }
237 
ixgbe_dcbnl_set_pg_bwg_cfg_rx(struct net_device * netdev,int bwg_id,u8 bw_pct)238 static void ixgbe_dcbnl_set_pg_bwg_cfg_rx(struct net_device *netdev, int bwg_id,
239                                           u8 bw_pct)
240 {
241 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
242 
243 	adapter->temp_dcb_cfg.bw_percentage[1][bwg_id] = bw_pct;
244 
245 	if (adapter->temp_dcb_cfg.bw_percentage[1][bwg_id] !=
246 	    adapter->dcb_cfg.bw_percentage[1][bwg_id])
247 		adapter->dcb_set_bitmap |= BIT_PG_RX;
248 }
249 
ixgbe_dcbnl_get_pg_tc_cfg_tx(struct net_device * netdev,int tc,u8 * prio,u8 * bwg_id,u8 * bw_pct,u8 * up_map)250 static void ixgbe_dcbnl_get_pg_tc_cfg_tx(struct net_device *netdev, int tc,
251                                          u8 *prio, u8 *bwg_id, u8 *bw_pct,
252                                          u8 *up_map)
253 {
254 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
255 
256 	*prio = adapter->dcb_cfg.tc_config[tc].path[0].prio_type;
257 	*bwg_id = adapter->dcb_cfg.tc_config[tc].path[0].bwg_id;
258 	*bw_pct = adapter->dcb_cfg.tc_config[tc].path[0].bwg_percent;
259 	*up_map = adapter->dcb_cfg.tc_config[tc].path[0].up_to_tc_bitmap;
260 }
261 
ixgbe_dcbnl_get_pg_bwg_cfg_tx(struct net_device * netdev,int bwg_id,u8 * bw_pct)262 static void ixgbe_dcbnl_get_pg_bwg_cfg_tx(struct net_device *netdev, int bwg_id,
263                                           u8 *bw_pct)
264 {
265 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
266 
267 	*bw_pct = adapter->dcb_cfg.bw_percentage[0][bwg_id];
268 }
269 
ixgbe_dcbnl_get_pg_tc_cfg_rx(struct net_device * netdev,int tc,u8 * prio,u8 * bwg_id,u8 * bw_pct,u8 * up_map)270 static void ixgbe_dcbnl_get_pg_tc_cfg_rx(struct net_device *netdev, int tc,
271                                          u8 *prio, u8 *bwg_id, u8 *bw_pct,
272                                          u8 *up_map)
273 {
274 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
275 
276 	*prio = adapter->dcb_cfg.tc_config[tc].path[1].prio_type;
277 	*bwg_id = adapter->dcb_cfg.tc_config[tc].path[1].bwg_id;
278 	*bw_pct = adapter->dcb_cfg.tc_config[tc].path[1].bwg_percent;
279 	*up_map = adapter->dcb_cfg.tc_config[tc].path[1].up_to_tc_bitmap;
280 }
281 
ixgbe_dcbnl_get_pg_bwg_cfg_rx(struct net_device * netdev,int bwg_id,u8 * bw_pct)282 static void ixgbe_dcbnl_get_pg_bwg_cfg_rx(struct net_device *netdev, int bwg_id,
283                                           u8 *bw_pct)
284 {
285 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
286 
287 	*bw_pct = adapter->dcb_cfg.bw_percentage[1][bwg_id];
288 }
289 
ixgbe_dcbnl_set_pfc_cfg(struct net_device * netdev,int priority,u8 setting)290 static void ixgbe_dcbnl_set_pfc_cfg(struct net_device *netdev, int priority,
291                                     u8 setting)
292 {
293 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
294 
295 	adapter->temp_dcb_cfg.tc_config[priority].dcb_pfc = setting;
296 	if (adapter->temp_dcb_cfg.tc_config[priority].dcb_pfc !=
297 	    adapter->dcb_cfg.tc_config[priority].dcb_pfc) {
298 		adapter->dcb_set_bitmap |= BIT_PFC;
299 		adapter->temp_dcb_cfg.pfc_mode_enable = true;
300 	}
301 }
302 
ixgbe_dcbnl_get_pfc_cfg(struct net_device * netdev,int priority,u8 * setting)303 static void ixgbe_dcbnl_get_pfc_cfg(struct net_device *netdev, int priority,
304                                     u8 *setting)
305 {
306 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
307 
308 	*setting = adapter->dcb_cfg.tc_config[priority].dcb_pfc;
309 }
310 
311 #ifdef IXGBE_FCOE
ixgbe_dcbnl_devreset(struct net_device * dev)312 static void ixgbe_dcbnl_devreset(struct net_device *dev)
313 {
314 	struct ixgbe_adapter *adapter = netdev_priv(dev);
315 
316 	while (test_and_set_bit(__IXGBE_RESETTING, &adapter->state))
317 		usleep_range(1000, 2000);
318 
319 	if (netif_running(dev))
320 		dev->netdev_ops->ndo_stop(dev);
321 
322 	ixgbe_clear_interrupt_scheme(adapter);
323 	ixgbe_init_interrupt_scheme(adapter);
324 
325 	if (netif_running(dev))
326 		dev->netdev_ops->ndo_open(dev);
327 
328 	clear_bit(__IXGBE_RESETTING, &adapter->state);
329 }
330 #endif
331 
ixgbe_dcbnl_set_all(struct net_device * netdev)332 static u8 ixgbe_dcbnl_set_all(struct net_device *netdev)
333 {
334 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
335 	int ret, i;
336 #ifdef IXGBE_FCOE
337 	struct dcb_app app = {
338 			      .selector = DCB_APP_IDTYPE_ETHTYPE,
339 			      .protocol = ETH_P_FCOE,
340 			     };
341 	u8 up;
342 
343 	/* In IEEE mode, use the IEEE Ethertype selector value */
344 	if (adapter->dcbx_cap & DCB_CAP_DCBX_VER_IEEE) {
345 		app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
346 		up = dcb_ieee_getapp_mask(netdev, &app);
347 	} else {
348 		up = dcb_getapp(netdev, &app);
349 	}
350 #endif
351 
352 	/* Fail command if not in CEE mode */
353 	if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_CEE))
354 		return 1;
355 
356 	ret = ixgbe_copy_dcb_cfg(&adapter->temp_dcb_cfg, &adapter->dcb_cfg,
357 				 MAX_TRAFFIC_CLASS);
358 	if (ret)
359 		return DCB_NO_HW_CHG;
360 
361 	if (adapter->dcb_cfg.pfc_mode_enable) {
362 		switch (adapter->hw.mac.type) {
363 		case ixgbe_mac_82599EB:
364 		case ixgbe_mac_X540:
365 			if (adapter->hw.fc.current_mode != ixgbe_fc_pfc)
366 				adapter->last_lfc_mode =
367 				                  adapter->hw.fc.current_mode;
368 			break;
369 		default:
370 			break;
371 		}
372 		adapter->hw.fc.requested_mode = ixgbe_fc_pfc;
373 	} else {
374 		switch (adapter->hw.mac.type) {
375 		case ixgbe_mac_82598EB:
376 			adapter->hw.fc.requested_mode = ixgbe_fc_none;
377 			break;
378 		case ixgbe_mac_82599EB:
379 		case ixgbe_mac_X540:
380 			adapter->hw.fc.requested_mode = adapter->last_lfc_mode;
381 			break;
382 		default:
383 			break;
384 		}
385 	}
386 
387 	if (adapter->dcb_set_bitmap & (BIT_PG_TX|BIT_PG_RX)) {
388 		u16 refill[MAX_TRAFFIC_CLASS], max[MAX_TRAFFIC_CLASS];
389 		u8 bwg_id[MAX_TRAFFIC_CLASS], prio_type[MAX_TRAFFIC_CLASS];
390 		/* Priority to TC mapping in CEE case default to 1:1 */
391 		u8 prio_tc[MAX_USER_PRIORITY];
392 		int max_frame = adapter->netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
393 
394 #ifdef IXGBE_FCOE
395 		if (adapter->netdev->features & NETIF_F_FCOE_MTU)
396 			max_frame = max(max_frame, IXGBE_FCOE_JUMBO_FRAME_SIZE);
397 #endif
398 
399 		ixgbe_dcb_calculate_tc_credits(&adapter->hw, &adapter->dcb_cfg,
400 					       max_frame, DCB_TX_CONFIG);
401 		ixgbe_dcb_calculate_tc_credits(&adapter->hw, &adapter->dcb_cfg,
402 					       max_frame, DCB_RX_CONFIG);
403 
404 		ixgbe_dcb_unpack_refill(&adapter->dcb_cfg,
405 					DCB_TX_CONFIG, refill);
406 		ixgbe_dcb_unpack_max(&adapter->dcb_cfg, max);
407 		ixgbe_dcb_unpack_bwgid(&adapter->dcb_cfg,
408 				       DCB_TX_CONFIG, bwg_id);
409 		ixgbe_dcb_unpack_prio(&adapter->dcb_cfg,
410 				      DCB_TX_CONFIG, prio_type);
411 		ixgbe_dcb_unpack_map(&adapter->dcb_cfg,
412 				     DCB_TX_CONFIG, prio_tc);
413 
414 		ixgbe_dcb_hw_ets_config(&adapter->hw, refill, max,
415 					bwg_id, prio_type, prio_tc);
416 
417 		for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
418 			netdev_set_prio_tc_map(netdev, i, prio_tc[i]);
419 	}
420 
421 	if (adapter->dcb_set_bitmap & BIT_PFC) {
422 		u8 pfc_en;
423 		u8 prio_tc[MAX_USER_PRIORITY];
424 
425 		ixgbe_dcb_unpack_map(&adapter->dcb_cfg,
426 				     DCB_TX_CONFIG, prio_tc);
427 		ixgbe_dcb_unpack_pfc(&adapter->dcb_cfg, &pfc_en);
428 		ixgbe_dcb_hw_pfc_config(&adapter->hw, pfc_en, prio_tc);
429 		ret = DCB_HW_CHG;
430 	}
431 
432 	if (adapter->dcb_cfg.pfc_mode_enable)
433 		adapter->hw.fc.current_mode = ixgbe_fc_pfc;
434 
435 #ifdef IXGBE_FCOE
436 	/* Reprogam FCoE hardware offloads when the traffic class
437 	 * FCoE is using changes. This happens if the APP info
438 	 * changes or the up2tc mapping is updated.
439 	 */
440 	if ((up && !(up & (1 << adapter->fcoe.up))) ||
441 	    (adapter->dcb_set_bitmap & BIT_APP_UPCHG)) {
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 
ixgbe_dcbnl_getcap(struct net_device * netdev,int capid,u8 * cap)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 
ixgbe_dcbnl_getnumtcs(struct net_device * netdev,int tcid,u8 * num)489 static u8 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 
ixgbe_dcbnl_setnumtcs(struct net_device * netdev,int tcid,u8 num)513 static u8 ixgbe_dcbnl_setnumtcs(struct net_device *netdev, int tcid, u8 num)
514 {
515 	return -EINVAL;
516 }
517 
ixgbe_dcbnl_getpfcstate(struct net_device * netdev)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 
ixgbe_dcbnl_setpfcstate(struct net_device * netdev,u8 state)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 	if (adapter->temp_dcb_cfg.pfc_mode_enable !=
531 		adapter->dcb_cfg.pfc_mode_enable)
532 		adapter->dcb_set_bitmap |= BIT_PFC;
533 }
534 
535 /**
536  * ixgbe_dcbnl_getapp - retrieve the DCBX application user priority
537  * @netdev : the corresponding netdev
538  * @idtype : identifies the id as ether type or TCP/UDP port number
539  * @id: id is either ether type or TCP/UDP port number
540  *
541  * Returns : on success, returns a non-zero 802.1p user priority bitmap
542  * otherwise returns 0 as the invalid user priority bitmap to indicate an
543  * error.
544  */
ixgbe_dcbnl_getapp(struct net_device * netdev,u8 idtype,u16 id)545 static u8 ixgbe_dcbnl_getapp(struct net_device *netdev, u8 idtype, u16 id)
546 {
547 	struct ixgbe_adapter *adapter = netdev_priv(netdev);
548 	struct dcb_app app = {
549 				.selector = idtype,
550 				.protocol = id,
551 			     };
552 
553 	if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_CEE))
554 		return 0;
555 
556 	return dcb_getapp(netdev, &app);
557 }
558 
ixgbe_dcbnl_ieee_getets(struct net_device * dev,struct ieee_ets * ets)559 static int ixgbe_dcbnl_ieee_getets(struct net_device *dev,
560 				   struct ieee_ets *ets)
561 {
562 	struct ixgbe_adapter *adapter = netdev_priv(dev);
563 	struct ieee_ets *my_ets = adapter->ixgbe_ieee_ets;
564 
565 	ets->ets_cap = adapter->dcb_cfg.num_tcs.pg_tcs;
566 
567 	/* No IEEE PFC settings available */
568 	if (!my_ets)
569 		return 0;
570 
571 	ets->cbs = my_ets->cbs;
572 	memcpy(ets->tc_tx_bw, my_ets->tc_tx_bw, sizeof(ets->tc_tx_bw));
573 	memcpy(ets->tc_rx_bw, my_ets->tc_rx_bw, sizeof(ets->tc_rx_bw));
574 	memcpy(ets->tc_tsa, my_ets->tc_tsa, sizeof(ets->tc_tsa));
575 	memcpy(ets->prio_tc, my_ets->prio_tc, sizeof(ets->prio_tc));
576 	return 0;
577 }
578 
ixgbe_dcbnl_ieee_setets(struct net_device * dev,struct ieee_ets * ets)579 static int ixgbe_dcbnl_ieee_setets(struct net_device *dev,
580 				   struct ieee_ets *ets)
581 {
582 	struct ixgbe_adapter *adapter = netdev_priv(dev);
583 	int max_frame = dev->mtu + ETH_HLEN + ETH_FCS_LEN;
584 	int i;
585 	__u8 max_tc = 0;
586 
587 	if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_IEEE))
588 		return -EINVAL;
589 
590 	if (!adapter->ixgbe_ieee_ets) {
591 		adapter->ixgbe_ieee_ets = kmalloc(sizeof(struct ieee_ets),
592 						  GFP_KERNEL);
593 		if (!adapter->ixgbe_ieee_ets)
594 			return -ENOMEM;
595 	}
596 
597 	memcpy(adapter->ixgbe_ieee_ets, ets, sizeof(*adapter->ixgbe_ieee_ets));
598 
599 	for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
600 		if (ets->prio_tc[i] > max_tc)
601 			max_tc = ets->prio_tc[i];
602 	}
603 
604 	if (max_tc)
605 		max_tc++;
606 
607 	if (max_tc > adapter->dcb_cfg.num_tcs.pg_tcs)
608 		return -EINVAL;
609 
610 	if (max_tc != netdev_get_num_tc(dev))
611 		ixgbe_setup_tc(dev, max_tc);
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 	return ixgbe_dcb_hw_ets(&adapter->hw, ets, max_frame);
617 }
618 
ixgbe_dcbnl_ieee_getpfc(struct net_device * dev,struct ieee_pfc * pfc)619 static int ixgbe_dcbnl_ieee_getpfc(struct net_device *dev,
620 				   struct ieee_pfc *pfc)
621 {
622 	struct ixgbe_adapter *adapter = netdev_priv(dev);
623 	struct ieee_pfc *my_pfc = adapter->ixgbe_ieee_pfc;
624 	int i;
625 
626 	pfc->pfc_cap = adapter->dcb_cfg.num_tcs.pfc_tcs;
627 
628 	/* No IEEE PFC settings available */
629 	if (!my_pfc)
630 		return 0;
631 
632 	pfc->pfc_en = my_pfc->pfc_en;
633 	pfc->mbc = my_pfc->mbc;
634 	pfc->delay = my_pfc->delay;
635 
636 	for (i = 0; i < MAX_TRAFFIC_CLASS; i++) {
637 		pfc->requests[i] = adapter->stats.pxoffrxc[i];
638 		pfc->indications[i] = adapter->stats.pxofftxc[i];
639 	}
640 
641 	return 0;
642 }
643 
ixgbe_dcbnl_ieee_setpfc(struct net_device * dev,struct ieee_pfc * pfc)644 static int ixgbe_dcbnl_ieee_setpfc(struct net_device *dev,
645 				   struct ieee_pfc *pfc)
646 {
647 	struct ixgbe_adapter *adapter = netdev_priv(dev);
648 	u8 *prio_tc;
649 
650 	if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_IEEE))
651 		return -EINVAL;
652 
653 	if (!adapter->ixgbe_ieee_pfc) {
654 		adapter->ixgbe_ieee_pfc = kmalloc(sizeof(struct ieee_pfc),
655 						  GFP_KERNEL);
656 		if (!adapter->ixgbe_ieee_pfc)
657 			return -ENOMEM;
658 	}
659 
660 	prio_tc = adapter->ixgbe_ieee_ets->prio_tc;
661 	memcpy(adapter->ixgbe_ieee_pfc, pfc, sizeof(*adapter->ixgbe_ieee_pfc));
662 	return ixgbe_dcb_hw_pfc_config(&adapter->hw, pfc->pfc_en, prio_tc);
663 }
664 
ixgbe_dcbnl_ieee_setapp(struct net_device * dev,struct dcb_app * app)665 static int ixgbe_dcbnl_ieee_setapp(struct net_device *dev,
666 				   struct dcb_app *app)
667 {
668 	struct ixgbe_adapter *adapter = netdev_priv(dev);
669 	int err = -EINVAL;
670 
671 	if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_IEEE))
672 		return err;
673 
674 	err = dcb_ieee_setapp(dev, app);
675 
676 #ifdef IXGBE_FCOE
677 	if (!err && app->selector == IEEE_8021QAZ_APP_SEL_ETHERTYPE &&
678 	    app->protocol == ETH_P_FCOE) {
679 		u8 app_mask = dcb_ieee_getapp_mask(dev, app);
680 
681 		if (app_mask & (1 << adapter->fcoe.up))
682 			return err;
683 
684 		adapter->fcoe.up = app->priority;
685 		ixgbe_dcbnl_devreset(dev);
686 	}
687 #endif
688 	return 0;
689 }
690 
ixgbe_dcbnl_ieee_delapp(struct net_device * dev,struct dcb_app * app)691 static int ixgbe_dcbnl_ieee_delapp(struct net_device *dev,
692 				   struct dcb_app *app)
693 {
694 	struct ixgbe_adapter *adapter = netdev_priv(dev);
695 	int err;
696 
697 	if (!(adapter->dcbx_cap & DCB_CAP_DCBX_VER_IEEE))
698 		return -EINVAL;
699 
700 	err = dcb_ieee_delapp(dev, app);
701 
702 #ifdef IXGBE_FCOE
703 	if (!err && app->selector == IEEE_8021QAZ_APP_SEL_ETHERTYPE &&
704 	    app->protocol == ETH_P_FCOE) {
705 		u8 app_mask = dcb_ieee_getapp_mask(dev, app);
706 
707 		if (app_mask & (1 << adapter->fcoe.up))
708 			return err;
709 
710 		adapter->fcoe.up = app_mask ?
711 				   ffs(app_mask) - 1 : IXGBE_FCOE_DEFTC;
712 		ixgbe_dcbnl_devreset(dev);
713 	}
714 #endif
715 	return err;
716 }
717 
ixgbe_dcbnl_getdcbx(struct net_device * dev)718 static u8 ixgbe_dcbnl_getdcbx(struct net_device *dev)
719 {
720 	struct ixgbe_adapter *adapter = netdev_priv(dev);
721 	return adapter->dcbx_cap;
722 }
723 
ixgbe_dcbnl_setdcbx(struct net_device * dev,u8 mode)724 static u8 ixgbe_dcbnl_setdcbx(struct net_device *dev, u8 mode)
725 {
726 	struct ixgbe_adapter *adapter = netdev_priv(dev);
727 	struct ieee_ets ets = {0};
728 	struct ieee_pfc pfc = {0};
729 
730 	/* no support for LLD_MANAGED modes or CEE+IEEE */
731 	if ((mode & DCB_CAP_DCBX_LLD_MANAGED) ||
732 	    ((mode & DCB_CAP_DCBX_VER_IEEE) && (mode & DCB_CAP_DCBX_VER_CEE)) ||
733 	    !(mode & DCB_CAP_DCBX_HOST))
734 		return 1;
735 
736 	if (mode == adapter->dcbx_cap)
737 		return 0;
738 
739 	adapter->dcbx_cap = mode;
740 
741 	/* ETS and PFC defaults */
742 	ets.ets_cap = 8;
743 	pfc.pfc_cap = 8;
744 
745 	if (mode & DCB_CAP_DCBX_VER_IEEE) {
746 		ixgbe_dcbnl_ieee_setets(dev, &ets);
747 		ixgbe_dcbnl_ieee_setpfc(dev, &pfc);
748 	} else if (mode & DCB_CAP_DCBX_VER_CEE) {
749 		u8 mask = BIT_PFC | BIT_PG_TX | BIT_PG_RX | BIT_APP_UPCHG;
750 
751 		adapter->dcb_set_bitmap |= mask;
752 		ixgbe_dcbnl_set_all(dev);
753 	} else {
754 		/* Drop into single TC mode strict priority as this
755 		 * indicates CEE and IEEE versions are disabled
756 		 */
757 		ixgbe_dcbnl_ieee_setets(dev, &ets);
758 		ixgbe_dcbnl_ieee_setpfc(dev, &pfc);
759 		ixgbe_setup_tc(dev, 0);
760 	}
761 
762 	return 0;
763 }
764 
765 const struct dcbnl_rtnl_ops dcbnl_ops = {
766 	.ieee_getets	= ixgbe_dcbnl_ieee_getets,
767 	.ieee_setets	= ixgbe_dcbnl_ieee_setets,
768 	.ieee_getpfc	= ixgbe_dcbnl_ieee_getpfc,
769 	.ieee_setpfc	= ixgbe_dcbnl_ieee_setpfc,
770 	.ieee_setapp	= ixgbe_dcbnl_ieee_setapp,
771 	.ieee_delapp	= ixgbe_dcbnl_ieee_delapp,
772 	.getstate	= ixgbe_dcbnl_get_state,
773 	.setstate	= ixgbe_dcbnl_set_state,
774 	.getpermhwaddr	= ixgbe_dcbnl_get_perm_hw_addr,
775 	.setpgtccfgtx	= ixgbe_dcbnl_set_pg_tc_cfg_tx,
776 	.setpgbwgcfgtx	= ixgbe_dcbnl_set_pg_bwg_cfg_tx,
777 	.setpgtccfgrx	= ixgbe_dcbnl_set_pg_tc_cfg_rx,
778 	.setpgbwgcfgrx	= ixgbe_dcbnl_set_pg_bwg_cfg_rx,
779 	.getpgtccfgtx	= ixgbe_dcbnl_get_pg_tc_cfg_tx,
780 	.getpgbwgcfgtx	= ixgbe_dcbnl_get_pg_bwg_cfg_tx,
781 	.getpgtccfgrx	= ixgbe_dcbnl_get_pg_tc_cfg_rx,
782 	.getpgbwgcfgrx	= ixgbe_dcbnl_get_pg_bwg_cfg_rx,
783 	.setpfccfg	= ixgbe_dcbnl_set_pfc_cfg,
784 	.getpfccfg	= ixgbe_dcbnl_get_pfc_cfg,
785 	.setall		= ixgbe_dcbnl_set_all,
786 	.getcap		= ixgbe_dcbnl_getcap,
787 	.getnumtcs	= ixgbe_dcbnl_getnumtcs,
788 	.setnumtcs	= ixgbe_dcbnl_setnumtcs,
789 	.getpfcstate	= ixgbe_dcbnl_getpfcstate,
790 	.setpfcstate	= ixgbe_dcbnl_setpfcstate,
791 	.getapp		= ixgbe_dcbnl_getapp,
792 	.getdcbx	= ixgbe_dcbnl_getdcbx,
793 	.setdcbx	= ixgbe_dcbnl_setdcbx,
794 };
795