1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019, Intel Corporation. */
3 
4 #include "ice.h"
5 #include "ice_dcb.h"
6 #include "ice_dcb_lib.h"
7 #include "ice_dcb_nl.h"
8 #include <net/dcbnl.h>
9 
10 #define ICE_APP_PROT_ID_ROCE	0x8915
11 
12 /**
13  * ice_dcbnl_devreset - perform enough of a ifdown/ifup to sync DCBNL info
14  * @netdev: device associated with interface that needs reset
15  */
16 static void ice_dcbnl_devreset(struct net_device *netdev)
17 {
18 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
19 
20 	while (ice_is_reset_in_progress(pf->state))
21 		usleep_range(1000, 2000);
22 
23 	set_bit(__ICE_DCBNL_DEVRESET, pf->state);
24 	dev_close(netdev);
25 	netdev_state_change(netdev);
26 	dev_open(netdev, NULL);
27 	netdev_state_change(netdev);
28 	clear_bit(__ICE_DCBNL_DEVRESET, pf->state);
29 }
30 
31 /**
32  * ice_dcbnl_getets - retrieve local ETS configuration
33  * @netdev: the relevant netdev
34  * @ets: struct to hold ETS configuration
35  */
36 static int ice_dcbnl_getets(struct net_device *netdev, struct ieee_ets *ets)
37 {
38 	struct ice_dcbx_cfg *dcbxcfg;
39 	struct ice_port_info *pi;
40 	struct ice_pf *pf;
41 
42 	pf = ice_netdev_to_pf(netdev);
43 	pi = pf->hw.port_info;
44 	dcbxcfg = &pi->local_dcbx_cfg;
45 
46 	ets->willing = dcbxcfg->etscfg.willing;
47 	ets->ets_cap = dcbxcfg->etscfg.maxtcs;
48 	ets->cbs = dcbxcfg->etscfg.cbs;
49 	memcpy(ets->tc_tx_bw, dcbxcfg->etscfg.tcbwtable, sizeof(ets->tc_tx_bw));
50 	memcpy(ets->tc_rx_bw, dcbxcfg->etscfg.tcbwtable, sizeof(ets->tc_rx_bw));
51 	memcpy(ets->tc_tsa, dcbxcfg->etscfg.tsatable, sizeof(ets->tc_tsa));
52 	memcpy(ets->prio_tc, dcbxcfg->etscfg.prio_table, sizeof(ets->prio_tc));
53 	memcpy(ets->tc_reco_bw, dcbxcfg->etsrec.tcbwtable,
54 	       sizeof(ets->tc_reco_bw));
55 	memcpy(ets->tc_reco_tsa, dcbxcfg->etsrec.tsatable,
56 	       sizeof(ets->tc_reco_tsa));
57 	memcpy(ets->reco_prio_tc, dcbxcfg->etscfg.prio_table,
58 	       sizeof(ets->reco_prio_tc));
59 
60 	return 0;
61 }
62 
63 /**
64  * ice_dcbnl_setets - set IEEE ETS configuration
65  * @netdev: pointer to relevant netdev
66  * @ets: struct to hold ETS configuration
67  */
68 static int ice_dcbnl_setets(struct net_device *netdev, struct ieee_ets *ets)
69 {
70 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
71 	struct ice_dcbx_cfg *new_cfg;
72 	int bwcfg = 0, bwrec = 0;
73 	int err, i, max_tc = 0;
74 
75 	if ((pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED) ||
76 	    !(pf->dcbx_cap & DCB_CAP_DCBX_VER_IEEE))
77 		return -EINVAL;
78 
79 	new_cfg = &pf->hw.port_info->desired_dcbx_cfg;
80 
81 	mutex_lock(&pf->tc_mutex);
82 
83 	new_cfg->etscfg.willing = ets->willing;
84 	new_cfg->etscfg.cbs = ets->cbs;
85 	ice_for_each_traffic_class(i) {
86 		new_cfg->etscfg.tcbwtable[i] = ets->tc_tx_bw[i];
87 		bwcfg += ets->tc_tx_bw[i];
88 		new_cfg->etscfg.tsatable[i] = ets->tc_tsa[i];
89 		new_cfg->etscfg.prio_table[i] = ets->prio_tc[i];
90 		if (ets->prio_tc[i] > max_tc)
91 			max_tc = ets->prio_tc[i];
92 		new_cfg->etsrec.tcbwtable[i] = ets->tc_reco_bw[i];
93 		bwrec += ets->tc_reco_bw[i];
94 		new_cfg->etsrec.tsatable[i] = ets->tc_reco_tsa[i];
95 		new_cfg->etsrec.prio_table[i] = ets->reco_prio_tc[i];
96 	}
97 
98 	/* max_tc is a 1-8 value count of number of TC's, not a 0-7 value
99 	 * for the TC's index number.  Add one to value if not zero, and
100 	 * for zero set it to the FW's default value
101 	 */
102 	if (max_tc)
103 		max_tc++;
104 	else
105 		max_tc = IEEE_8021QAZ_MAX_TCS;
106 
107 	new_cfg->etscfg.maxtcs = max_tc;
108 
109 	if (!bwcfg)
110 		new_cfg->etscfg.tcbwtable[0] = 100;
111 
112 	if (!bwrec)
113 		new_cfg->etsrec.tcbwtable[0] = 100;
114 
115 	err = ice_pf_dcb_cfg(pf, new_cfg, true);
116 	/* return of zero indicates new cfg applied */
117 	if (err == ICE_DCB_HW_CHG_RST)
118 		ice_dcbnl_devreset(netdev);
119 	if (err == ICE_DCB_NO_HW_CHG)
120 		err = ICE_DCB_HW_CHG_RST;
121 
122 	mutex_unlock(&pf->tc_mutex);
123 	return err;
124 }
125 
126 /**
127  * ice_dcbnl_getnumtcs - Get max number of traffic classes supported
128  * @dev: pointer to netdev struct
129  * @tcid: TC ID
130  * @num: total number of TCs supported by the adapter
131  *
132  * Return the total number of TCs supported
133  */
134 static int
135 ice_dcbnl_getnumtcs(struct net_device *dev, int __always_unused tcid, u8 *num)
136 {
137 	struct ice_pf *pf = ice_netdev_to_pf(dev);
138 
139 	if (!test_bit(ICE_FLAG_DCB_CAPABLE, pf->flags))
140 		return -EINVAL;
141 
142 	*num = IEEE_8021QAZ_MAX_TCS;
143 	return 0;
144 }
145 
146 /**
147  * ice_dcbnl_getdcbx - retrieve current DCBX capability
148  * @netdev: pointer to the netdev struct
149  */
150 static u8 ice_dcbnl_getdcbx(struct net_device *netdev)
151 {
152 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
153 
154 	return pf->dcbx_cap;
155 }
156 
157 /**
158  * ice_dcbnl_setdcbx - set required DCBX capability
159  * @netdev: the corresponding netdev
160  * @mode: required mode
161  */
162 static u8 ice_dcbnl_setdcbx(struct net_device *netdev, u8 mode)
163 {
164 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
165 
166 	/* No support for LLD_MANAGED modes or CEE+IEEE */
167 	if ((mode & DCB_CAP_DCBX_LLD_MANAGED) ||
168 	    ((mode & DCB_CAP_DCBX_VER_IEEE) && (mode & DCB_CAP_DCBX_VER_CEE)) ||
169 	    !(mode & DCB_CAP_DCBX_HOST))
170 		return ICE_DCB_NO_HW_CHG;
171 
172 	/* Already set to the given mode no change */
173 	if (mode == pf->dcbx_cap)
174 		return ICE_DCB_NO_HW_CHG;
175 
176 	pf->dcbx_cap = mode;
177 	if (mode & DCB_CAP_DCBX_VER_CEE)
178 		pf->hw.port_info->local_dcbx_cfg.dcbx_mode = ICE_DCBX_MODE_CEE;
179 	else
180 		pf->hw.port_info->local_dcbx_cfg.dcbx_mode = ICE_DCBX_MODE_IEEE;
181 
182 	dev_info(ice_pf_to_dev(pf), "DCBx mode = 0x%x\n", mode);
183 	return ICE_DCB_HW_CHG_RST;
184 }
185 
186 /**
187  * ice_dcbnl_get_perm_hw_addr - MAC address used by DCBX
188  * @netdev: pointer to netdev struct
189  * @perm_addr: buffer to return permanent MAC address
190  */
191 static void ice_dcbnl_get_perm_hw_addr(struct net_device *netdev, u8 *perm_addr)
192 {
193 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
194 	struct ice_port_info *pi = pf->hw.port_info;
195 	int i, j;
196 
197 	memset(perm_addr, 0xff, MAX_ADDR_LEN);
198 
199 	for (i = 0; i < netdev->addr_len; i++)
200 		perm_addr[i] = pi->mac.perm_addr[i];
201 
202 	for (j = 0; j < netdev->addr_len; j++, i++)
203 		perm_addr[i] = pi->mac.perm_addr[j];
204 }
205 
206 /**
207  * ice_get_pfc_delay - Retrieve PFC Link Delay
208  * @hw: pointer to HW struct
209  * @delay: holds the PFC Link Delay value
210  */
211 static void ice_get_pfc_delay(struct ice_hw *hw, u16 *delay)
212 {
213 	u32 val;
214 
215 	val = rd32(hw, PRTDCB_GENC);
216 	*delay = (u16)((val & PRTDCB_GENC_PFCLDA_M) >> PRTDCB_GENC_PFCLDA_S);
217 }
218 
219 /**
220  * ice_dcbnl_getpfc - retrieve local IEEE PFC config
221  * @netdev: pointer to netdev struct
222  * @pfc: struct to hold PFC info
223  */
224 static int ice_dcbnl_getpfc(struct net_device *netdev, struct ieee_pfc *pfc)
225 {
226 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
227 	struct ice_port_info *pi = pf->hw.port_info;
228 	struct ice_dcbx_cfg *dcbxcfg;
229 	int i;
230 
231 	dcbxcfg = &pi->local_dcbx_cfg;
232 	pfc->pfc_cap = dcbxcfg->pfc.pfccap;
233 	pfc->pfc_en = dcbxcfg->pfc.pfcena;
234 	pfc->mbc = dcbxcfg->pfc.mbc;
235 	ice_get_pfc_delay(&pf->hw, &pfc->delay);
236 
237 	ice_for_each_traffic_class(i) {
238 		pfc->requests[i] = pf->stats.priority_xoff_tx[i];
239 		pfc->indications[i] = pf->stats.priority_xoff_rx[i];
240 	}
241 
242 	return 0;
243 }
244 
245 /**
246  * ice_dcbnl_setpfc - set local IEEE PFC config
247  * @netdev: pointer to relevant netdev
248  * @pfc: pointer to struct holding PFC config
249  */
250 static int ice_dcbnl_setpfc(struct net_device *netdev, struct ieee_pfc *pfc)
251 {
252 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
253 	struct ice_dcbx_cfg *new_cfg;
254 	int err;
255 
256 	if ((pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED) ||
257 	    !(pf->dcbx_cap & DCB_CAP_DCBX_VER_IEEE))
258 		return -EINVAL;
259 
260 	mutex_lock(&pf->tc_mutex);
261 
262 	new_cfg = &pf->hw.port_info->desired_dcbx_cfg;
263 
264 	if (pfc->pfc_cap)
265 		new_cfg->pfc.pfccap = pfc->pfc_cap;
266 	else
267 		new_cfg->pfc.pfccap = pf->hw.func_caps.common_cap.maxtc;
268 
269 	new_cfg->pfc.pfcena = pfc->pfc_en;
270 
271 	err = ice_pf_dcb_cfg(pf, new_cfg, true);
272 	if (err == ICE_DCB_HW_CHG_RST)
273 		ice_dcbnl_devreset(netdev);
274 	if (err == ICE_DCB_NO_HW_CHG)
275 		err = ICE_DCB_HW_CHG_RST;
276 	mutex_unlock(&pf->tc_mutex);
277 	return err;
278 }
279 
280 /**
281  * ice_dcbnl_get_pfc_cfg - Get CEE PFC config
282  * @netdev: pointer to netdev struct
283  * @prio: corresponding user priority
284  * @setting: the PFC setting for given priority
285  */
286 static void
287 ice_dcbnl_get_pfc_cfg(struct net_device *netdev, int prio, u8 *setting)
288 {
289 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
290 	struct ice_port_info *pi = pf->hw.port_info;
291 
292 	if ((pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED) ||
293 	    !(pf->dcbx_cap & DCB_CAP_DCBX_VER_CEE))
294 		return;
295 
296 	if (prio >= ICE_MAX_USER_PRIORITY)
297 		return;
298 
299 	*setting = (pi->local_dcbx_cfg.pfc.pfcena >> prio) & 0x1;
300 	dev_dbg(ice_pf_to_dev(pf),
301 		"Get PFC Config up=%d, setting=%d, pfcenable=0x%x\n",
302 		prio, *setting, pi->local_dcbx_cfg.pfc.pfcena);
303 }
304 
305 /**
306  * ice_dcbnl_set_pfc_cfg - Set CEE PFC config
307  * @netdev: the corresponding netdev
308  * @prio: User Priority
309  * @set: PFC setting to apply
310  */
311 static void ice_dcbnl_set_pfc_cfg(struct net_device *netdev, int prio, u8 set)
312 {
313 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
314 	struct ice_dcbx_cfg *new_cfg;
315 
316 	if ((pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED) ||
317 	    !(pf->dcbx_cap & DCB_CAP_DCBX_VER_CEE))
318 		return;
319 
320 	if (prio >= ICE_MAX_USER_PRIORITY)
321 		return;
322 
323 	new_cfg = &pf->hw.port_info->desired_dcbx_cfg;
324 
325 	new_cfg->pfc.pfccap = pf->hw.func_caps.common_cap.maxtc;
326 	if (set)
327 		new_cfg->pfc.pfcena |= BIT(prio);
328 	else
329 		new_cfg->pfc.pfcena &= ~BIT(prio);
330 
331 	dev_dbg(ice_pf_to_dev(pf), "Set PFC config UP:%d set:%d pfcena:0x%x\n",
332 		prio, set, new_cfg->pfc.pfcena);
333 }
334 
335 /**
336  * ice_dcbnl_getpfcstate - get CEE PFC mode
337  * @netdev: pointer to netdev struct
338  */
339 static u8 ice_dcbnl_getpfcstate(struct net_device *netdev)
340 {
341 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
342 	struct ice_port_info *pi = pf->hw.port_info;
343 
344 	/* Return enabled if any UP enabled for PFC */
345 	if (pi->local_dcbx_cfg.pfc.pfcena)
346 		return 1;
347 
348 	return 0;
349 }
350 
351 /**
352  * ice_dcbnl_getstate - get DCB enabled state
353  * @netdev: pointer to netdev struct
354  */
355 static u8 ice_dcbnl_getstate(struct net_device *netdev)
356 {
357 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
358 	u8 state = 0;
359 
360 	state = test_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
361 
362 	dev_dbg(ice_pf_to_dev(pf), "DCB enabled state = %d\n", state);
363 	return state;
364 }
365 
366 /**
367  * ice_dcbnl_setstate - Set CEE DCB state
368  * @netdev: pointer to relevant netdev
369  * @state: state value to set
370  */
371 static u8 ice_dcbnl_setstate(struct net_device *netdev, u8 state)
372 {
373 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
374 
375 	if ((pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED) ||
376 	    !(pf->dcbx_cap & DCB_CAP_DCBX_VER_CEE))
377 		return ICE_DCB_NO_HW_CHG;
378 
379 	/* Nothing to do */
380 	if (!!state == test_bit(ICE_FLAG_DCB_ENA, pf->flags))
381 		return ICE_DCB_NO_HW_CHG;
382 
383 	if (state) {
384 		set_bit(ICE_FLAG_DCB_ENA, pf->flags);
385 		memcpy(&pf->hw.port_info->desired_dcbx_cfg,
386 		       &pf->hw.port_info->local_dcbx_cfg,
387 		       sizeof(struct ice_dcbx_cfg));
388 	} else {
389 		clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
390 	}
391 
392 	return ICE_DCB_HW_CHG;
393 }
394 
395 /**
396  * ice_dcbnl_get_pg_tc_cfg_tx - get CEE PG Tx config
397  * @netdev: pointer to netdev struct
398  * @prio: the corresponding user priority
399  * @prio_type: traffic priority type
400  * @pgid: the BW group ID the traffic class belongs to
401  * @bw_pct: BW percentage for the corresponding BWG
402  * @up_map: prio mapped to corresponding TC
403  */
404 static void
405 ice_dcbnl_get_pg_tc_cfg_tx(struct net_device *netdev, int prio,
406 			   u8 __always_unused *prio_type, u8 *pgid,
407 			   u8 __always_unused *bw_pct,
408 			   u8 __always_unused *up_map)
409 {
410 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
411 	struct ice_port_info *pi = pf->hw.port_info;
412 
413 	if ((pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED) ||
414 	    !(pf->dcbx_cap & DCB_CAP_DCBX_VER_CEE))
415 		return;
416 
417 	if (prio >= ICE_MAX_USER_PRIORITY)
418 		return;
419 
420 	*pgid = pi->local_dcbx_cfg.etscfg.prio_table[prio];
421 	dev_dbg(ice_pf_to_dev(pf),
422 		"Get PG config prio=%d tc=%d\n", prio, *pgid);
423 }
424 
425 /**
426  * ice_dcbnl_set_pg_tc_cfg_tx - set CEE PG Tx config
427  * @netdev: pointer to relevant netdev
428  * @tc: the corresponding traffic class
429  * @prio_type: the traffic priority type
430  * @bwg_id: the BW group ID the TC belongs to
431  * @bw_pct: the BW perventage for the BWG
432  * @up_map: prio mapped to corresponding TC
433  */
434 static void
435 ice_dcbnl_set_pg_tc_cfg_tx(struct net_device *netdev, int tc,
436 			   u8 __always_unused prio_type,
437 			   u8 __always_unused bwg_id,
438 			   u8 __always_unused bw_pct, u8 up_map)
439 {
440 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
441 	struct ice_dcbx_cfg *new_cfg;
442 	int i;
443 
444 	if ((pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED) ||
445 	    !(pf->dcbx_cap & DCB_CAP_DCBX_VER_CEE))
446 		return;
447 
448 	if (tc >= ICE_MAX_TRAFFIC_CLASS)
449 		return;
450 
451 	new_cfg = &pf->hw.port_info->desired_dcbx_cfg;
452 
453 	/* prio_type, bwg_id and bw_pct per UP are not supported */
454 
455 	ice_for_each_traffic_class(i) {
456 		if (up_map & BIT(i))
457 			new_cfg->etscfg.prio_table[i] = tc;
458 	}
459 	new_cfg->etscfg.tsatable[tc] = ICE_IEEE_TSA_ETS;
460 }
461 
462 /**
463  * ice_dcbnl_get_pg_bwg_cfg_tx - Get CEE PGBW config
464  * @netdev: pointer to the netdev struct
465  * @pgid: corresponding traffic class
466  * @bw_pct: the BW percentage for the corresponding TC
467  */
468 static void
469 ice_dcbnl_get_pg_bwg_cfg_tx(struct net_device *netdev, int pgid, u8 *bw_pct)
470 {
471 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
472 	struct ice_port_info *pi = pf->hw.port_info;
473 
474 	if ((pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED) ||
475 	    !(pf->dcbx_cap & DCB_CAP_DCBX_VER_CEE))
476 		return;
477 
478 	if (pgid >= ICE_MAX_TRAFFIC_CLASS)
479 		return;
480 
481 	*bw_pct = pi->local_dcbx_cfg.etscfg.tcbwtable[pgid];
482 	dev_dbg(ice_pf_to_dev(pf), "Get PG BW config tc=%d bw_pct=%d\n",
483 		pgid, *bw_pct);
484 }
485 
486 /**
487  * ice_dcbnl_set_pg_bwg_cfg_tx - set CEE PG Tx BW config
488  * @netdev: the corresponding netdev
489  * @pgid: Correspongind traffic class
490  * @bw_pct: the BW percentage for the specified TC
491  */
492 static void
493 ice_dcbnl_set_pg_bwg_cfg_tx(struct net_device *netdev, int pgid, u8 bw_pct)
494 {
495 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
496 	struct ice_dcbx_cfg *new_cfg;
497 
498 	if ((pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED) ||
499 	    !(pf->dcbx_cap & DCB_CAP_DCBX_VER_CEE))
500 		return;
501 
502 	if (pgid >= ICE_MAX_TRAFFIC_CLASS)
503 		return;
504 
505 	new_cfg = &pf->hw.port_info->desired_dcbx_cfg;
506 
507 	new_cfg->etscfg.tcbwtable[pgid] = bw_pct;
508 }
509 
510 /**
511  * ice_dcbnl_get_pg_tc_cfg_rx - Get CEE PG Rx config
512  * @netdev: pointer to netdev struct
513  * @prio: the corresponding user priority
514  * @prio_type: the traffic priority type
515  * @pgid: the PG ID
516  * @bw_pct: the BW percentage for the corresponding BWG
517  * @up_map: prio mapped to corresponding TC
518  */
519 static void
520 ice_dcbnl_get_pg_tc_cfg_rx(struct net_device *netdev, int prio,
521 			   u8 __always_unused *prio_type, u8 *pgid,
522 			   u8 __always_unused *bw_pct,
523 			   u8 __always_unused *up_map)
524 {
525 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
526 	struct ice_port_info *pi = pf->hw.port_info;
527 
528 	if ((pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED) ||
529 	    !(pf->dcbx_cap & DCB_CAP_DCBX_VER_CEE))
530 		return;
531 
532 	if (prio >= ICE_MAX_USER_PRIORITY)
533 		return;
534 
535 	*pgid = pi->local_dcbx_cfg.etscfg.prio_table[prio];
536 }
537 
538 /**
539  * ice_dcbnl_get_pg_bwg_cfg_rx - Get CEE PG BW Rx config
540  * @netdev: pointer to netdev struct
541  * @pgid: the corresponding traffic class
542  * @bw_pct: the BW percentage for the corresponding TC
543  */
544 static void
545 ice_dcbnl_get_pg_bwg_cfg_rx(struct net_device *netdev, int __always_unused pgid,
546 			    u8 *bw_pct)
547 {
548 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
549 
550 	if ((pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED) ||
551 	    !(pf->dcbx_cap & DCB_CAP_DCBX_VER_CEE))
552 		return;
553 
554 	*bw_pct = 0;
555 }
556 
557 /**
558  * ice_dcbnl_get_cap - Get DCBX capabilities of adapter
559  * @netdev: pointer to netdev struct
560  * @capid: the capability type
561  * @cap: the capability value
562  */
563 static u8 ice_dcbnl_get_cap(struct net_device *netdev, int capid, u8 *cap)
564 {
565 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
566 
567 	if (!(test_bit(ICE_FLAG_DCB_CAPABLE, pf->flags)))
568 		return ICE_DCB_NO_HW_CHG;
569 
570 	switch (capid) {
571 	case DCB_CAP_ATTR_PG:
572 		*cap = true;
573 		break;
574 	case DCB_CAP_ATTR_PFC:
575 		*cap = true;
576 		break;
577 	case DCB_CAP_ATTR_UP2TC:
578 		*cap = false;
579 		break;
580 	case DCB_CAP_ATTR_PG_TCS:
581 		*cap = 0x80;
582 		break;
583 	case DCB_CAP_ATTR_PFC_TCS:
584 		*cap = 0x80;
585 		break;
586 	case DCB_CAP_ATTR_GSP:
587 		*cap = false;
588 		break;
589 	case DCB_CAP_ATTR_BCN:
590 		*cap = false;
591 		break;
592 	case DCB_CAP_ATTR_DCBX:
593 		*cap = pf->dcbx_cap;
594 		break;
595 	default:
596 		*cap = false;
597 		break;
598 	}
599 
600 	dev_dbg(ice_pf_to_dev(pf), "DCBX Get Capability cap=%d capval=0x%x\n",
601 		capid, *cap);
602 	return 0;
603 }
604 
605 /**
606  * ice_dcbnl_getapp - get CEE APP
607  * @netdev: pointer to netdev struct
608  * @idtype: the App selector
609  * @id: the App ethtype or port number
610  */
611 static int ice_dcbnl_getapp(struct net_device *netdev, u8 idtype, u16 id)
612 {
613 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
614 	struct dcb_app app = {
615 				.selector = idtype,
616 				.protocol = id,
617 			     };
618 
619 	if ((pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED) ||
620 	    !(pf->dcbx_cap & DCB_CAP_DCBX_VER_CEE))
621 		return -EINVAL;
622 
623 	return dcb_getapp(netdev, &app);
624 }
625 
626 /**
627  * ice_dcbnl_find_app - Search for APP in given DCB config
628  * @cfg: struct to hold DCBX config
629  * @app: struct to hold app data to look for
630  */
631 static bool
632 ice_dcbnl_find_app(struct ice_dcbx_cfg *cfg,
633 		   struct ice_dcb_app_priority_table *app)
634 {
635 	int i;
636 
637 	for (i = 0; i < cfg->numapps; i++) {
638 		if (app->selector == cfg->app[i].selector &&
639 		    app->prot_id == cfg->app[i].prot_id &&
640 		    app->priority == cfg->app[i].priority)
641 			return true;
642 	}
643 
644 	return false;
645 }
646 
647 /**
648  * ice_dcbnl_setapp - set local IEEE App config
649  * @netdev: relevant netdev struct
650  * @app: struct to hold app config info
651  */
652 static int ice_dcbnl_setapp(struct net_device *netdev, struct dcb_app *app)
653 {
654 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
655 	struct ice_dcb_app_priority_table new_app;
656 	struct ice_dcbx_cfg *old_cfg, *new_cfg;
657 	int ret;
658 
659 	if ((pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED) ||
660 	    !(pf->dcbx_cap & DCB_CAP_DCBX_VER_IEEE))
661 		return -EINVAL;
662 
663 	mutex_lock(&pf->tc_mutex);
664 
665 	new_cfg = &pf->hw.port_info->desired_dcbx_cfg;
666 
667 	old_cfg = &pf->hw.port_info->local_dcbx_cfg;
668 
669 	if (old_cfg->numapps == ICE_DCBX_MAX_APPS) {
670 		ret = -EINVAL;
671 		goto setapp_out;
672 	}
673 
674 	ret = dcb_ieee_setapp(netdev, app);
675 	if (ret)
676 		goto setapp_out;
677 
678 	new_app.selector = app->selector;
679 	new_app.prot_id = app->protocol;
680 	new_app.priority = app->priority;
681 	if (ice_dcbnl_find_app(old_cfg, &new_app)) {
682 		ret = 0;
683 		goto setapp_out;
684 	}
685 
686 	new_cfg->app[new_cfg->numapps++] = new_app;
687 	ret = ice_pf_dcb_cfg(pf, new_cfg, true);
688 	/* return of zero indicates new cfg applied */
689 	if (ret == ICE_DCB_HW_CHG_RST)
690 		ice_dcbnl_devreset(netdev);
691 	if (ret == ICE_DCB_NO_HW_CHG)
692 		ret = ICE_DCB_HW_CHG_RST;
693 
694 setapp_out:
695 	mutex_unlock(&pf->tc_mutex);
696 	return ret;
697 }
698 
699 /**
700  * ice_dcbnl_delapp - Delete local IEEE App config
701  * @netdev: relevant netdev
702  * @app: struct to hold app too delete
703  *
704  * Will not delete first application required by the FW
705  */
706 static int ice_dcbnl_delapp(struct net_device *netdev, struct dcb_app *app)
707 {
708 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
709 	struct ice_dcbx_cfg *old_cfg, *new_cfg;
710 	int i, j, ret = 0;
711 
712 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
713 		return -EINVAL;
714 
715 	mutex_lock(&pf->tc_mutex);
716 	ret = dcb_ieee_delapp(netdev, app);
717 	if (ret)
718 		goto delapp_out;
719 
720 	old_cfg = &pf->hw.port_info->local_dcbx_cfg;
721 
722 	if (old_cfg->numapps == 1)
723 		goto delapp_out;
724 
725 	new_cfg = &pf->hw.port_info->desired_dcbx_cfg;
726 
727 	for (i = 1; i < new_cfg->numapps; i++) {
728 		if (app->selector == new_cfg->app[i].selector &&
729 		    app->protocol == new_cfg->app[i].prot_id &&
730 		    app->priority == new_cfg->app[i].priority) {
731 			new_cfg->app[i].selector = 0;
732 			new_cfg->app[i].prot_id = 0;
733 			new_cfg->app[i].priority = 0;
734 			break;
735 		}
736 	}
737 
738 	/* Did not find DCB App */
739 	if (i == new_cfg->numapps) {
740 		ret = -EINVAL;
741 		goto delapp_out;
742 	}
743 
744 	new_cfg->numapps--;
745 
746 	for (j = i; j < new_cfg->numapps; j++) {
747 		new_cfg->app[i].selector = old_cfg->app[j + 1].selector;
748 		new_cfg->app[i].prot_id = old_cfg->app[j + 1].prot_id;
749 		new_cfg->app[i].priority = old_cfg->app[j + 1].priority;
750 	}
751 
752 	ret = ice_pf_dcb_cfg(pf, new_cfg, true);
753 	/* return of zero indicates new cfg applied */
754 	if (ret == ICE_DCB_HW_CHG_RST)
755 		ice_dcbnl_devreset(netdev);
756 	if (ret == ICE_DCB_NO_HW_CHG)
757 		ret = ICE_DCB_HW_CHG_RST;
758 
759 delapp_out:
760 	mutex_unlock(&pf->tc_mutex);
761 	return ret;
762 }
763 
764 /**
765  * ice_dcbnl_cee_set_all - Commit CEE DCB settings to HW
766  * @netdev: the corresponding netdev
767  */
768 static u8 ice_dcbnl_cee_set_all(struct net_device *netdev)
769 {
770 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
771 	struct ice_dcbx_cfg *new_cfg;
772 	int err;
773 
774 	if ((pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED) ||
775 	    !(pf->dcbx_cap & DCB_CAP_DCBX_VER_CEE))
776 		return ICE_DCB_NO_HW_CHG;
777 
778 	new_cfg = &pf->hw.port_info->desired_dcbx_cfg;
779 
780 	mutex_lock(&pf->tc_mutex);
781 
782 	err = ice_pf_dcb_cfg(pf, new_cfg, true);
783 
784 	mutex_unlock(&pf->tc_mutex);
785 	return (err != ICE_DCB_HW_CHG_RST) ? ICE_DCB_NO_HW_CHG : err;
786 }
787 
788 static const struct dcbnl_rtnl_ops dcbnl_ops = {
789 	/* IEEE 802.1Qaz std */
790 	.ieee_getets = ice_dcbnl_getets,
791 	.ieee_setets = ice_dcbnl_setets,
792 	.ieee_getpfc = ice_dcbnl_getpfc,
793 	.ieee_setpfc = ice_dcbnl_setpfc,
794 	.ieee_setapp = ice_dcbnl_setapp,
795 	.ieee_delapp = ice_dcbnl_delapp,
796 
797 	/* CEE std */
798 	.getstate = ice_dcbnl_getstate,
799 	.setstate = ice_dcbnl_setstate,
800 	.getpermhwaddr = ice_dcbnl_get_perm_hw_addr,
801 	.setpgtccfgtx = ice_dcbnl_set_pg_tc_cfg_tx,
802 	.setpgbwgcfgtx = ice_dcbnl_set_pg_bwg_cfg_tx,
803 	.getpgtccfgtx = ice_dcbnl_get_pg_tc_cfg_tx,
804 	.getpgbwgcfgtx = ice_dcbnl_get_pg_bwg_cfg_tx,
805 	.getpgtccfgrx = ice_dcbnl_get_pg_tc_cfg_rx,
806 	.getpgbwgcfgrx = ice_dcbnl_get_pg_bwg_cfg_rx,
807 	.setpfccfg = ice_dcbnl_set_pfc_cfg,
808 	.getpfccfg = ice_dcbnl_get_pfc_cfg,
809 	.setall = ice_dcbnl_cee_set_all,
810 	.getcap = ice_dcbnl_get_cap,
811 	.getnumtcs = ice_dcbnl_getnumtcs,
812 	.getpfcstate = ice_dcbnl_getpfcstate,
813 	.getapp = ice_dcbnl_getapp,
814 
815 	/* DCBX configuration */
816 	.getdcbx = ice_dcbnl_getdcbx,
817 	.setdcbx = ice_dcbnl_setdcbx,
818 };
819 
820 /**
821  * ice_dcbnl_set_all - set all the apps and ieee data from DCBX config
822  * @vsi: pointer to VSI struct
823  */
824 void ice_dcbnl_set_all(struct ice_vsi *vsi)
825 {
826 	struct net_device *netdev = vsi->netdev;
827 	struct ice_dcbx_cfg *dcbxcfg;
828 	struct ice_port_info *pi;
829 	struct dcb_app sapp;
830 	struct ice_pf *pf;
831 	int i;
832 
833 	if (!netdev)
834 		return;
835 
836 	pf = ice_netdev_to_pf(netdev);
837 	pi = pf->hw.port_info;
838 
839 	/* SW DCB taken care of by SW Default Config */
840 	if (pf->dcbx_cap & DCB_CAP_DCBX_HOST)
841 		return;
842 
843 	/* DCB not enabled */
844 	if (!test_bit(ICE_FLAG_DCB_ENA, pf->flags))
845 		return;
846 
847 	dcbxcfg = &pi->local_dcbx_cfg;
848 
849 	for (i = 0; i < dcbxcfg->numapps; i++) {
850 		u8 prio, tc_map;
851 
852 		prio = dcbxcfg->app[i].priority;
853 		tc_map = BIT(dcbxcfg->etscfg.prio_table[prio]);
854 
855 		/* Add APP only if the TC is enabled for this VSI */
856 		if (tc_map & vsi->tc_cfg.ena_tc) {
857 			sapp.selector = dcbxcfg->app[i].selector;
858 			sapp.protocol = dcbxcfg->app[i].prot_id;
859 			sapp.priority = prio;
860 			dcb_ieee_setapp(netdev, &sapp);
861 		}
862 	}
863 	/* Notify user-space of the changes */
864 	dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_SET, 0, 0);
865 }
866 
867 /**
868  * ice_dcbnl_vsi_del_app - Delete APP on all VSIs
869  * @vsi: pointer to the main VSI
870  * @app: APP to delete
871  *
872  * Delete given APP from all the VSIs for given PF
873  */
874 static void
875 ice_dcbnl_vsi_del_app(struct ice_vsi *vsi,
876 		      struct ice_dcb_app_priority_table *app)
877 {
878 	struct dcb_app sapp;
879 	int err;
880 
881 	sapp.selector = app->selector;
882 	sapp.protocol = app->prot_id;
883 	sapp.priority = app->priority;
884 	err = ice_dcbnl_delapp(vsi->netdev, &sapp);
885 	dev_dbg(&vsi->back->pdev->dev,
886 		"Deleting app for VSI idx=%d err=%d sel=%d proto=0x%x, prio=%d\n",
887 		vsi->idx, err, app->selector, app->prot_id, app->priority);
888 }
889 
890 /**
891  * ice_dcbnl_flush_apps - Delete all removed APPs
892  * @pf: the corresponding PF
893  * @old_cfg: old DCBX configuration data
894  * @new_cfg: new DCBX configuration data
895  *
896  * Find and delete all APPS that are not present in the passed
897  * DCB configuration
898  */
899 void
900 ice_dcbnl_flush_apps(struct ice_pf *pf, struct ice_dcbx_cfg *old_cfg,
901 		     struct ice_dcbx_cfg *new_cfg)
902 {
903 	struct ice_vsi *main_vsi = ice_get_main_vsi(pf);
904 	int i;
905 
906 	if (!main_vsi)
907 		return;
908 
909 	for (i = 0; i < old_cfg->numapps; i++) {
910 		struct ice_dcb_app_priority_table app = old_cfg->app[i];
911 
912 		/* The APP is not available anymore delete it */
913 		if (!ice_dcbnl_find_app(new_cfg, &app))
914 			ice_dcbnl_vsi_del_app(main_vsi, &app);
915 	}
916 }
917 
918 /**
919  * ice_dcbnl_setup - setup DCBNL
920  * @vsi: VSI to get associated netdev from
921  */
922 void ice_dcbnl_setup(struct ice_vsi *vsi)
923 {
924 	struct net_device *netdev = vsi->netdev;
925 	struct ice_pf *pf;
926 
927 	pf = ice_netdev_to_pf(netdev);
928 	if (!test_bit(ICE_FLAG_DCB_CAPABLE, pf->flags))
929 		return;
930 
931 	netdev->dcbnl_ops = &dcbnl_ops;
932 	ice_dcbnl_set_all(vsi);
933 }
934