xref: /openbmc/linux/drivers/net/ethernet/intel/ice/ice_dcb_lib.c (revision a89aa749ece9c6fee7932163472d2ee0efd6ddd3)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019, Intel Corporation. */
3 
4 #include "ice_dcb_lib.h"
5 #include "ice_dcb_nl.h"
6 
7 /**
8  * ice_vsi_cfg_netdev_tc - Setup the netdev TC configuration
9  * @vsi: the VSI being configured
10  * @ena_tc: TC map to be enabled
11  */
12 void ice_vsi_cfg_netdev_tc(struct ice_vsi *vsi, u8 ena_tc)
13 {
14 	struct net_device *netdev = vsi->netdev;
15 	struct ice_pf *pf = vsi->back;
16 	struct ice_dcbx_cfg *dcbcfg;
17 	u8 netdev_tc;
18 	int i;
19 
20 	if (!netdev)
21 		return;
22 
23 	if (!ena_tc) {
24 		netdev_reset_tc(netdev);
25 		return;
26 	}
27 
28 	if (netdev_set_num_tc(netdev, vsi->tc_cfg.numtc))
29 		return;
30 
31 	dcbcfg = &pf->hw.port_info->local_dcbx_cfg;
32 
33 	ice_for_each_traffic_class(i)
34 		if (vsi->tc_cfg.ena_tc & BIT(i))
35 			netdev_set_tc_queue(netdev,
36 					    vsi->tc_cfg.tc_info[i].netdev_tc,
37 					    vsi->tc_cfg.tc_info[i].qcount_tx,
38 					    vsi->tc_cfg.tc_info[i].qoffset);
39 
40 	for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
41 		u8 ets_tc = dcbcfg->etscfg.prio_table[i];
42 
43 		/* Get the mapped netdev TC# for the UP */
44 		netdev_tc = vsi->tc_cfg.tc_info[ets_tc].netdev_tc;
45 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
46 	}
47 }
48 
49 /**
50  * ice_dcb_get_ena_tc - return bitmap of enabled TCs
51  * @dcbcfg: DCB config to evaluate for enabled TCs
52  */
53 u8 ice_dcb_get_ena_tc(struct ice_dcbx_cfg *dcbcfg)
54 {
55 	u8 i, num_tc, ena_tc = 1;
56 
57 	num_tc = ice_dcb_get_num_tc(dcbcfg);
58 
59 	for (i = 0; i < num_tc; i++)
60 		ena_tc |= BIT(i);
61 
62 	return ena_tc;
63 }
64 
65 /**
66  * ice_dcb_get_mode - gets the DCB mode
67  * @port_info: pointer to port info structure
68  * @host: if set it's HOST if not it's MANAGED
69  */
70 static u8 ice_dcb_get_mode(struct ice_port_info *port_info, bool host)
71 {
72 	u8 mode;
73 
74 	if (host)
75 		mode = DCB_CAP_DCBX_HOST;
76 	else
77 		mode = DCB_CAP_DCBX_LLD_MANAGED;
78 
79 	if (port_info->local_dcbx_cfg.dcbx_mode & ICE_DCBX_MODE_CEE)
80 		return mode | DCB_CAP_DCBX_VER_CEE;
81 	else
82 		return mode | DCB_CAP_DCBX_VER_IEEE;
83 }
84 
85 /**
86  * ice_dcb_get_num_tc - Get the number of TCs from DCBX config
87  * @dcbcfg: config to retrieve number of TCs from
88  */
89 u8 ice_dcb_get_num_tc(struct ice_dcbx_cfg *dcbcfg)
90 {
91 	bool tc_unused = false;
92 	u8 num_tc = 0;
93 	u8 ret = 0;
94 	int i;
95 
96 	/* Scan the ETS Config Priority Table to find traffic classes
97 	 * enabled and create a bitmask of enabled TCs
98 	 */
99 	for (i = 0; i < CEE_DCBX_MAX_PRIO; i++)
100 		num_tc |= BIT(dcbcfg->etscfg.prio_table[i]);
101 
102 	/* Scan bitmask for contiguous TCs starting with TC0 */
103 	for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
104 		if (num_tc & BIT(i)) {
105 			if (!tc_unused) {
106 				ret++;
107 			} else {
108 				pr_err("Non-contiguous TCs - Disabling DCB\n");
109 				return 1;
110 			}
111 		} else {
112 			tc_unused = true;
113 		}
114 	}
115 
116 	/* There is always at least 1 TC */
117 	if (!ret)
118 		ret = 1;
119 
120 	return ret;
121 }
122 
123 /**
124  * ice_dcb_get_tc - Get the TC associated with the queue
125  * @vsi: ptr to the VSI
126  * @queue_index: queue number associated with VSI
127  */
128 u8 ice_dcb_get_tc(struct ice_vsi *vsi, int queue_index)
129 {
130 	return vsi->tx_rings[queue_index]->dcb_tc;
131 }
132 
133 /**
134  * ice_vsi_cfg_dcb_rings - Update rings to reflect DCB TC
135  * @vsi: VSI owner of rings being updated
136  */
137 void ice_vsi_cfg_dcb_rings(struct ice_vsi *vsi)
138 {
139 	struct ice_ring *tx_ring, *rx_ring;
140 	u16 qoffset, qcount;
141 	int i, n;
142 
143 	if (!test_bit(ICE_FLAG_DCB_ENA, vsi->back->flags)) {
144 		/* Reset the TC information */
145 		for (i = 0; i < vsi->num_txq; i++) {
146 			tx_ring = vsi->tx_rings[i];
147 			tx_ring->dcb_tc = 0;
148 		}
149 		for (i = 0; i < vsi->num_rxq; i++) {
150 			rx_ring = vsi->rx_rings[i];
151 			rx_ring->dcb_tc = 0;
152 		}
153 		return;
154 	}
155 
156 	ice_for_each_traffic_class(n) {
157 		if (!(vsi->tc_cfg.ena_tc & BIT(n)))
158 			break;
159 
160 		qoffset = vsi->tc_cfg.tc_info[n].qoffset;
161 		qcount = vsi->tc_cfg.tc_info[n].qcount_tx;
162 		for (i = qoffset; i < (qoffset + qcount); i++) {
163 			tx_ring = vsi->tx_rings[i];
164 			rx_ring = vsi->rx_rings[i];
165 			tx_ring->dcb_tc = n;
166 			rx_ring->dcb_tc = n;
167 		}
168 	}
169 }
170 
171 /**
172  * ice_dcb_bwchk - check if ETS bandwidth input parameters are correct
173  * @pf: pointer to the PF struct
174  * @dcbcfg: pointer to DCB config structure
175  */
176 int ice_dcb_bwchk(struct ice_pf *pf, struct ice_dcbx_cfg *dcbcfg)
177 {
178 	struct ice_dcb_ets_cfg *etscfg = &dcbcfg->etscfg;
179 	u8 num_tc, total_bw = 0;
180 	int i;
181 
182 	/* returns number of contigous TCs and 1 TC for non-contigous TCs,
183 	 * since at least 1 TC has to be configured
184 	 */
185 	num_tc = ice_dcb_get_num_tc(dcbcfg);
186 
187 	/* no bandwidth checks required if there's only one TC, so assign
188 	 * all bandwidth to TC0 and return
189 	 */
190 	if (num_tc == 1) {
191 		etscfg->tcbwtable[0] = ICE_TC_MAX_BW;
192 		return 0;
193 	}
194 
195 	for (i = 0; i < num_tc; i++)
196 		total_bw += etscfg->tcbwtable[i];
197 
198 	if (!total_bw) {
199 		etscfg->tcbwtable[0] = ICE_TC_MAX_BW;
200 	} else if (total_bw != ICE_TC_MAX_BW) {
201 		dev_err(ice_pf_to_dev(pf), "Invalid config, total bandwidth must equal 100\n");
202 		return -EINVAL;
203 	}
204 
205 	return 0;
206 }
207 
208 /**
209  * ice_pf_dcb_cfg - Apply new DCB configuration
210  * @pf: pointer to the PF struct
211  * @new_cfg: DCBX config to apply
212  * @locked: is the RTNL held
213  */
214 int ice_pf_dcb_cfg(struct ice_pf *pf, struct ice_dcbx_cfg *new_cfg, bool locked)
215 {
216 	struct ice_aqc_port_ets_elem buf = { 0 };
217 	struct ice_dcbx_cfg *old_cfg, *curr_cfg;
218 	struct device *dev = ice_pf_to_dev(pf);
219 	int ret = ICE_DCB_NO_HW_CHG;
220 	struct ice_vsi *pf_vsi;
221 
222 	curr_cfg = &pf->hw.port_info->local_dcbx_cfg;
223 
224 	/* FW does not care if change happened */
225 	if (!pf->hw.port_info->is_sw_lldp)
226 		ret = ICE_DCB_HW_CHG_RST;
227 
228 	/* Enable DCB tagging only when more than one TC */
229 	if (ice_dcb_get_num_tc(new_cfg) > 1) {
230 		dev_dbg(dev, "DCB tagging enabled (num TC > 1)\n");
231 		set_bit(ICE_FLAG_DCB_ENA, pf->flags);
232 	} else {
233 		dev_dbg(dev, "DCB tagging disabled (num TC = 1)\n");
234 		clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
235 	}
236 
237 	if (!memcmp(new_cfg, curr_cfg, sizeof(*new_cfg))) {
238 		dev_dbg(dev, "No change in DCB config required\n");
239 		return ret;
240 	}
241 
242 	if (ice_dcb_bwchk(pf, new_cfg))
243 		return -EINVAL;
244 
245 	/* Store old config in case FW config fails */
246 	old_cfg = kmemdup(curr_cfg, sizeof(*old_cfg), GFP_KERNEL);
247 	if (!old_cfg)
248 		return -ENOMEM;
249 
250 	dev_info(dev, "Commit DCB Configuration to the hardware\n");
251 	pf_vsi = ice_get_main_vsi(pf);
252 	if (!pf_vsi) {
253 		dev_dbg(dev, "PF VSI doesn't exist\n");
254 		ret = -EINVAL;
255 		goto free_cfg;
256 	}
257 
258 	/* avoid race conditions by holding the lock while disabling and
259 	 * re-enabling the VSI
260 	 */
261 	if (!locked)
262 		rtnl_lock();
263 	ice_dis_vsi(pf_vsi, true);
264 
265 	memcpy(curr_cfg, new_cfg, sizeof(*curr_cfg));
266 	memcpy(&curr_cfg->etsrec, &curr_cfg->etscfg, sizeof(curr_cfg->etsrec));
267 	memcpy(&new_cfg->etsrec, &curr_cfg->etscfg, sizeof(curr_cfg->etsrec));
268 
269 	/* Only send new config to HW if we are in SW LLDP mode. Otherwise,
270 	 * the new config came from the HW in the first place.
271 	 */
272 	if (pf->hw.port_info->is_sw_lldp) {
273 		ret = ice_set_dcb_cfg(pf->hw.port_info);
274 		if (ret) {
275 			dev_err(dev, "Set DCB Config failed\n");
276 			/* Restore previous settings to local config */
277 			memcpy(curr_cfg, old_cfg, sizeof(*curr_cfg));
278 			goto out;
279 		}
280 	}
281 
282 	ret = ice_query_port_ets(pf->hw.port_info, &buf, sizeof(buf), NULL);
283 	if (ret) {
284 		dev_err(dev, "Query Port ETS failed\n");
285 		goto out;
286 	}
287 
288 	ice_pf_dcb_recfg(pf);
289 
290 out:
291 	ice_ena_vsi(pf_vsi, true);
292 	if (!locked)
293 		rtnl_unlock();
294 free_cfg:
295 	kfree(old_cfg);
296 	return ret;
297 }
298 
299 /**
300  * ice_cfg_etsrec_defaults - Set default ETS recommended DCB config
301  * @pi: port information structure
302  */
303 static void ice_cfg_etsrec_defaults(struct ice_port_info *pi)
304 {
305 	struct ice_dcbx_cfg *dcbcfg = &pi->local_dcbx_cfg;
306 	u8 i;
307 
308 	/* Ensure ETS recommended DCB configuration is not already set */
309 	if (dcbcfg->etsrec.maxtcs)
310 		return;
311 
312 	/* In CEE mode, set the default to 1 TC */
313 	dcbcfg->etsrec.maxtcs = 1;
314 	for (i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++) {
315 		dcbcfg->etsrec.tcbwtable[i] = i ? 0 : 100;
316 		dcbcfg->etsrec.tsatable[i] = i ? ICE_IEEE_TSA_STRICT :
317 						 ICE_IEEE_TSA_ETS;
318 	}
319 }
320 
321 /**
322  * ice_dcb_need_recfg - Check if DCB needs reconfig
323  * @pf: board private structure
324  * @old_cfg: current DCB config
325  * @new_cfg: new DCB config
326  */
327 static bool
328 ice_dcb_need_recfg(struct ice_pf *pf, struct ice_dcbx_cfg *old_cfg,
329 		   struct ice_dcbx_cfg *new_cfg)
330 {
331 	struct device *dev = ice_pf_to_dev(pf);
332 	bool need_reconfig = false;
333 
334 	/* Check if ETS configuration has changed */
335 	if (memcmp(&new_cfg->etscfg, &old_cfg->etscfg,
336 		   sizeof(new_cfg->etscfg))) {
337 		/* If Priority Table has changed reconfig is needed */
338 		if (memcmp(&new_cfg->etscfg.prio_table,
339 			   &old_cfg->etscfg.prio_table,
340 			   sizeof(new_cfg->etscfg.prio_table))) {
341 			need_reconfig = true;
342 			dev_dbg(dev, "ETS UP2TC changed.\n");
343 		}
344 
345 		if (memcmp(&new_cfg->etscfg.tcbwtable,
346 			   &old_cfg->etscfg.tcbwtable,
347 			   sizeof(new_cfg->etscfg.tcbwtable)))
348 			dev_dbg(dev, "ETS TC BW Table changed.\n");
349 
350 		if (memcmp(&new_cfg->etscfg.tsatable,
351 			   &old_cfg->etscfg.tsatable,
352 			   sizeof(new_cfg->etscfg.tsatable)))
353 			dev_dbg(dev, "ETS TSA Table changed.\n");
354 	}
355 
356 	/* Check if PFC configuration has changed */
357 	if (memcmp(&new_cfg->pfc, &old_cfg->pfc, sizeof(new_cfg->pfc))) {
358 		need_reconfig = true;
359 		dev_dbg(dev, "PFC config change detected.\n");
360 	}
361 
362 	/* Check if APP Table has changed */
363 	if (memcmp(&new_cfg->app, &old_cfg->app, sizeof(new_cfg->app))) {
364 		need_reconfig = true;
365 		dev_dbg(dev, "APP Table change detected.\n");
366 	}
367 
368 	dev_dbg(dev, "dcb need_reconfig=%d\n", need_reconfig);
369 	return need_reconfig;
370 }
371 
372 /**
373  * ice_dcb_rebuild - rebuild DCB post reset
374  * @pf: physical function instance
375  */
376 void ice_dcb_rebuild(struct ice_pf *pf)
377 {
378 	struct ice_aqc_port_ets_elem buf = { 0 };
379 	struct device *dev = ice_pf_to_dev(pf);
380 	struct ice_dcbx_cfg *err_cfg;
381 	enum ice_status ret;
382 
383 	ret = ice_query_port_ets(pf->hw.port_info, &buf, sizeof(buf), NULL);
384 	if (ret) {
385 		dev_err(dev, "Query Port ETS failed\n");
386 		goto dcb_error;
387 	}
388 
389 	/* If DCB was not enabled previously, we are done */
390 	if (!test_bit(ICE_FLAG_DCB_ENA, pf->flags))
391 		return;
392 
393 	mutex_lock(&pf->tc_mutex);
394 
395 	if (!pf->hw.port_info->is_sw_lldp)
396 		ice_cfg_etsrec_defaults(pf->hw.port_info);
397 
398 	ret = ice_set_dcb_cfg(pf->hw.port_info);
399 	if (ret) {
400 		dev_err(dev, "Failed to set DCB config in rebuild\n");
401 		goto dcb_error;
402 	}
403 
404 	if (!pf->hw.port_info->is_sw_lldp) {
405 		ret = ice_cfg_lldp_mib_change(&pf->hw, true);
406 		if (ret && !pf->hw.port_info->is_sw_lldp) {
407 			dev_err(dev, "Failed to register for MIB changes\n");
408 			goto dcb_error;
409 		}
410 	}
411 
412 	dev_info(dev, "DCB restored after reset\n");
413 	ret = ice_query_port_ets(pf->hw.port_info, &buf, sizeof(buf), NULL);
414 	if (ret) {
415 		dev_err(dev, "Query Port ETS failed\n");
416 		goto dcb_error;
417 	}
418 
419 	mutex_unlock(&pf->tc_mutex);
420 
421 	return;
422 
423 dcb_error:
424 	dev_err(dev, "Disabling DCB until new settings occur\n");
425 	err_cfg = kzalloc(sizeof(*err_cfg), GFP_KERNEL);
426 	if (!err_cfg) {
427 		mutex_unlock(&pf->tc_mutex);
428 		return;
429 	}
430 
431 	err_cfg->etscfg.willing = true;
432 	err_cfg->etscfg.tcbwtable[0] = ICE_TC_MAX_BW;
433 	err_cfg->etscfg.tsatable[0] = ICE_IEEE_TSA_ETS;
434 	memcpy(&err_cfg->etsrec, &err_cfg->etscfg, sizeof(err_cfg->etsrec));
435 	/* Coverity warns the return code of ice_pf_dcb_cfg() is not checked
436 	 * here as is done for other calls to that function. That check is
437 	 * not necessary since this is in this function's error cleanup path.
438 	 * Suppress the Coverity warning with the following comment...
439 	 */
440 	/* coverity[check_return] */
441 	ice_pf_dcb_cfg(pf, err_cfg, false);
442 	kfree(err_cfg);
443 
444 	mutex_unlock(&pf->tc_mutex);
445 }
446 
447 /**
448  * ice_dcb_init_cfg - set the initial DCB config in SW
449  * @pf: PF to apply config to
450  * @locked: Is the RTNL held
451  */
452 static int ice_dcb_init_cfg(struct ice_pf *pf, bool locked)
453 {
454 	struct ice_dcbx_cfg *newcfg;
455 	struct ice_port_info *pi;
456 	int ret = 0;
457 
458 	pi = pf->hw.port_info;
459 	newcfg = kmemdup(&pi->local_dcbx_cfg, sizeof(*newcfg), GFP_KERNEL);
460 	if (!newcfg)
461 		return -ENOMEM;
462 
463 	memset(&pi->local_dcbx_cfg, 0, sizeof(*newcfg));
464 
465 	dev_info(ice_pf_to_dev(pf), "Configuring initial DCB values\n");
466 	if (ice_pf_dcb_cfg(pf, newcfg, locked))
467 		ret = -EINVAL;
468 
469 	kfree(newcfg);
470 
471 	return ret;
472 }
473 
474 /**
475  * ice_dcb_sw_dflt_cfg - Apply a default DCB config
476  * @pf: PF to apply config to
477  * @ets_willing: configure ETS willing
478  * @locked: was this function called with RTNL held
479  */
480 static int ice_dcb_sw_dflt_cfg(struct ice_pf *pf, bool ets_willing, bool locked)
481 {
482 	struct ice_aqc_port_ets_elem buf = { 0 };
483 	struct ice_dcbx_cfg *dcbcfg;
484 	struct ice_port_info *pi;
485 	struct ice_hw *hw;
486 	int ret;
487 
488 	hw = &pf->hw;
489 	pi = hw->port_info;
490 	dcbcfg = kzalloc(sizeof(*dcbcfg), GFP_KERNEL);
491 	if (!dcbcfg)
492 		return -ENOMEM;
493 
494 	memset(&pi->local_dcbx_cfg, 0, sizeof(*dcbcfg));
495 
496 	dcbcfg->etscfg.willing = ets_willing ? 1 : 0;
497 	dcbcfg->etscfg.maxtcs = hw->func_caps.common_cap.maxtc;
498 	dcbcfg->etscfg.tcbwtable[0] = 100;
499 	dcbcfg->etscfg.tsatable[0] = ICE_IEEE_TSA_ETS;
500 
501 	memcpy(&dcbcfg->etsrec, &dcbcfg->etscfg,
502 	       sizeof(dcbcfg->etsrec));
503 	dcbcfg->etsrec.willing = 0;
504 
505 	dcbcfg->pfc.willing = 1;
506 	dcbcfg->pfc.pfccap = hw->func_caps.common_cap.maxtc;
507 
508 	dcbcfg->numapps = 1;
509 	dcbcfg->app[0].selector = ICE_APP_SEL_ETHTYPE;
510 	dcbcfg->app[0].priority = 3;
511 	dcbcfg->app[0].prot_id = ICE_APP_PROT_ID_FCOE;
512 
513 	ret = ice_pf_dcb_cfg(pf, dcbcfg, locked);
514 	kfree(dcbcfg);
515 	if (ret)
516 		return ret;
517 
518 	return ice_query_port_ets(pi, &buf, sizeof(buf), NULL);
519 }
520 
521 /**
522  * ice_dcb_tc_contig - Check that TCs are contiguous
523  * @prio_table: pointer to priority table
524  *
525  * Check if TCs begin with TC0 and are contiguous
526  */
527 static bool ice_dcb_tc_contig(u8 *prio_table)
528 {
529 	u8 max_tc = 0;
530 	int i;
531 
532 	for (i = 0; i < CEE_DCBX_MAX_PRIO; i++) {
533 		u8 cur_tc = prio_table[i];
534 
535 		if (cur_tc > max_tc)
536 			return false;
537 		else if (cur_tc == max_tc)
538 			max_tc++;
539 	}
540 
541 	return true;
542 }
543 
544 /**
545  * ice_dcb_noncontig_cfg - Configure DCB for non-contiguous TCs
546  * @pf: pointer to the PF struct
547  *
548  * If non-contiguous TCs, then configure SW DCB with TC0 and ETS non-willing
549  */
550 static int ice_dcb_noncontig_cfg(struct ice_pf *pf)
551 {
552 	struct ice_dcbx_cfg *dcbcfg = &pf->hw.port_info->local_dcbx_cfg;
553 	struct device *dev = ice_pf_to_dev(pf);
554 	int ret;
555 
556 	/* Configure SW DCB default with ETS non-willing */
557 	ret = ice_dcb_sw_dflt_cfg(pf, false, true);
558 	if (ret) {
559 		dev_err(dev, "Failed to set local DCB config %d\n", ret);
560 		return ret;
561 	}
562 
563 	/* Reconfigure with ETS willing so that FW will send LLDP MIB event */
564 	dcbcfg->etscfg.willing = 1;
565 	ret = ice_set_dcb_cfg(pf->hw.port_info);
566 	if (ret)
567 		dev_err(dev, "Failed to set DCB to unwilling\n");
568 
569 	return ret;
570 }
571 
572 /**
573  * ice_pf_dcb_recfg - Reconfigure all VEBs and VSIs
574  * @pf: pointer to the PF struct
575  *
576  * Assumed caller has already disabled all VSIs before
577  * calling this function. Reconfiguring DCB based on
578  * local_dcbx_cfg.
579  */
580 void ice_pf_dcb_recfg(struct ice_pf *pf)
581 {
582 	struct ice_dcbx_cfg *dcbcfg = &pf->hw.port_info->local_dcbx_cfg;
583 	u8 tc_map = 0;
584 	int v, ret;
585 
586 	/* Update each VSI */
587 	ice_for_each_vsi(pf, v) {
588 		struct ice_vsi *vsi = pf->vsi[v];
589 
590 		if (!vsi)
591 			continue;
592 
593 		if (vsi->type == ICE_VSI_PF) {
594 			tc_map = ice_dcb_get_ena_tc(dcbcfg);
595 
596 			/* If DCBX request non-contiguous TC, then configure
597 			 * default TC
598 			 */
599 			if (!ice_dcb_tc_contig(dcbcfg->etscfg.prio_table)) {
600 				tc_map = ICE_DFLT_TRAFFIC_CLASS;
601 				ice_dcb_noncontig_cfg(pf);
602 			}
603 		} else {
604 			tc_map = ICE_DFLT_TRAFFIC_CLASS;
605 		}
606 
607 		ret = ice_vsi_cfg_tc(vsi, tc_map);
608 		if (ret) {
609 			dev_err(ice_pf_to_dev(pf), "Failed to config TC for VSI index: %d\n",
610 				vsi->idx);
611 			continue;
612 		}
613 
614 		ice_vsi_map_rings_to_vectors(vsi);
615 		if (vsi->type == ICE_VSI_PF)
616 			ice_dcbnl_set_all(vsi);
617 	}
618 }
619 
620 /**
621  * ice_init_pf_dcb - initialize DCB for a PF
622  * @pf: PF to initialize DCB for
623  * @locked: Was function called with RTNL held
624  */
625 int ice_init_pf_dcb(struct ice_pf *pf, bool locked)
626 {
627 	struct device *dev = ice_pf_to_dev(pf);
628 	struct ice_port_info *port_info;
629 	struct ice_hw *hw = &pf->hw;
630 	int err;
631 
632 	port_info = hw->port_info;
633 
634 	err = ice_init_dcb(hw, false);
635 	if (err && !port_info->is_sw_lldp) {
636 		dev_err(dev, "Error initializing DCB %d\n", err);
637 		goto dcb_init_err;
638 	}
639 
640 	dev_info(dev, "DCB is enabled in the hardware, max number of TCs supported on this port are %d\n",
641 		 pf->hw.func_caps.common_cap.maxtc);
642 	if (err) {
643 		struct ice_vsi *pf_vsi;
644 
645 		/* FW LLDP is disabled, activate SW DCBX/LLDP mode */
646 		dev_info(dev, "FW LLDP is disabled, DCBx/LLDP in SW mode.\n");
647 		clear_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags);
648 		err = ice_dcb_sw_dflt_cfg(pf, true, locked);
649 		if (err) {
650 			dev_err(dev, "Failed to set local DCB config %d\n",
651 				err);
652 			err = -EIO;
653 			goto dcb_init_err;
654 		}
655 
656 		/* If the FW DCBX engine is not running then Rx LLDP packets
657 		 * need to be redirected up the stack.
658 		 */
659 		pf_vsi = ice_get_main_vsi(pf);
660 		if (!pf_vsi) {
661 			dev_err(dev, "Failed to set local DCB config\n");
662 			err = -EIO;
663 			goto dcb_init_err;
664 		}
665 
666 		ice_cfg_sw_lldp(pf_vsi, false, true);
667 
668 		pf->dcbx_cap = ice_dcb_get_mode(port_info, true);
669 		return 0;
670 	}
671 
672 	set_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags);
673 
674 	/* DCBX/LLDP enabled in FW, set DCBNL mode advertisement */
675 	pf->dcbx_cap = ice_dcb_get_mode(port_info, false);
676 
677 	err = ice_dcb_init_cfg(pf, locked);
678 	if (err)
679 		goto dcb_init_err;
680 
681 	return err;
682 
683 dcb_init_err:
684 	dev_err(dev, "DCB init failed\n");
685 	return err;
686 }
687 
688 /**
689  * ice_update_dcb_stats - Update DCB stats counters
690  * @pf: PF whose stats needs to be updated
691  */
692 void ice_update_dcb_stats(struct ice_pf *pf)
693 {
694 	struct ice_hw_port_stats *prev_ps, *cur_ps;
695 	struct ice_hw *hw = &pf->hw;
696 	u8 port;
697 	int i;
698 
699 	port = hw->port_info->lport;
700 	prev_ps = &pf->stats_prev;
701 	cur_ps = &pf->stats;
702 
703 	for (i = 0; i < 8; i++) {
704 		ice_stat_update32(hw, GLPRT_PXOFFRXC(port, i),
705 				  pf->stat_prev_loaded,
706 				  &prev_ps->priority_xoff_rx[i],
707 				  &cur_ps->priority_xoff_rx[i]);
708 		ice_stat_update32(hw, GLPRT_PXONRXC(port, i),
709 				  pf->stat_prev_loaded,
710 				  &prev_ps->priority_xon_rx[i],
711 				  &cur_ps->priority_xon_rx[i]);
712 		ice_stat_update32(hw, GLPRT_PXONTXC(port, i),
713 				  pf->stat_prev_loaded,
714 				  &prev_ps->priority_xon_tx[i],
715 				  &cur_ps->priority_xon_tx[i]);
716 		ice_stat_update32(hw, GLPRT_PXOFFTXC(port, i),
717 				  pf->stat_prev_loaded,
718 				  &prev_ps->priority_xoff_tx[i],
719 				  &cur_ps->priority_xoff_tx[i]);
720 		ice_stat_update32(hw, GLPRT_RXON2OFFCNT(port, i),
721 				  pf->stat_prev_loaded,
722 				  &prev_ps->priority_xon_2_xoff[i],
723 				  &cur_ps->priority_xon_2_xoff[i]);
724 	}
725 }
726 
727 /**
728  * ice_tx_prepare_vlan_flags_dcb - prepare VLAN tagging for DCB
729  * @tx_ring: ring to send buffer on
730  * @first: pointer to struct ice_tx_buf
731  */
732 int
733 ice_tx_prepare_vlan_flags_dcb(struct ice_ring *tx_ring,
734 			      struct ice_tx_buf *first)
735 {
736 	struct sk_buff *skb = first->skb;
737 
738 	if (!test_bit(ICE_FLAG_DCB_ENA, tx_ring->vsi->back->flags))
739 		return 0;
740 
741 	/* Insert 802.1p priority into VLAN header */
742 	if ((first->tx_flags & (ICE_TX_FLAGS_HW_VLAN | ICE_TX_FLAGS_SW_VLAN)) ||
743 	    skb->priority != TC_PRIO_CONTROL) {
744 		first->tx_flags &= ~ICE_TX_FLAGS_VLAN_PR_M;
745 		/* Mask the lower 3 bits to set the 802.1p priority */
746 		first->tx_flags |= (skb->priority & 0x7) <<
747 				   ICE_TX_FLAGS_VLAN_PR_S;
748 		if (first->tx_flags & ICE_TX_FLAGS_SW_VLAN) {
749 			struct vlan_ethhdr *vhdr;
750 			int rc;
751 
752 			rc = skb_cow_head(skb, 0);
753 			if (rc < 0)
754 				return rc;
755 			vhdr = (struct vlan_ethhdr *)skb->data;
756 			vhdr->h_vlan_TCI = htons(first->tx_flags >>
757 						 ICE_TX_FLAGS_VLAN_S);
758 		} else {
759 			first->tx_flags |= ICE_TX_FLAGS_HW_VLAN;
760 		}
761 	}
762 
763 	return 0;
764 }
765 
766 /**
767  * ice_dcb_process_lldp_set_mib_change - Process MIB change
768  * @pf: ptr to ice_pf
769  * @event: pointer to the admin queue receive event
770  */
771 void
772 ice_dcb_process_lldp_set_mib_change(struct ice_pf *pf,
773 				    struct ice_rq_event_info *event)
774 {
775 	struct ice_aqc_port_ets_elem buf = { 0 };
776 	struct device *dev = ice_pf_to_dev(pf);
777 	struct ice_aqc_lldp_get_mib *mib;
778 	struct ice_dcbx_cfg tmp_dcbx_cfg;
779 	bool need_reconfig = false;
780 	struct ice_port_info *pi;
781 	struct ice_vsi *pf_vsi;
782 	u8 mib_type;
783 	int ret;
784 
785 	/* Not DCB capable or capability disabled */
786 	if (!(test_bit(ICE_FLAG_DCB_CAPABLE, pf->flags)))
787 		return;
788 
789 	if (pf->dcbx_cap & DCB_CAP_DCBX_HOST) {
790 		dev_dbg(dev, "MIB Change Event in HOST mode\n");
791 		return;
792 	}
793 
794 	pi = pf->hw.port_info;
795 	mib = (struct ice_aqc_lldp_get_mib *)&event->desc.params.raw;
796 	/* Ignore if event is not for Nearest Bridge */
797 	mib_type = ((mib->type >> ICE_AQ_LLDP_BRID_TYPE_S) &
798 		    ICE_AQ_LLDP_BRID_TYPE_M);
799 	dev_dbg(dev, "LLDP event MIB bridge type 0x%x\n", mib_type);
800 	if (mib_type != ICE_AQ_LLDP_BRID_TYPE_NEAREST_BRID)
801 		return;
802 
803 	/* Check MIB Type and return if event for Remote MIB update */
804 	mib_type = mib->type & ICE_AQ_LLDP_MIB_TYPE_M;
805 	dev_dbg(dev, "LLDP event mib type %s\n", mib_type ? "remote" : "local");
806 	if (mib_type == ICE_AQ_LLDP_MIB_REMOTE) {
807 		/* Update the remote cached instance and return */
808 		ret = ice_aq_get_dcb_cfg(pi->hw, ICE_AQ_LLDP_MIB_REMOTE,
809 					 ICE_AQ_LLDP_BRID_TYPE_NEAREST_BRID,
810 					 &pi->remote_dcbx_cfg);
811 		if (ret) {
812 			dev_err(dev, "Failed to get remote DCB config\n");
813 			return;
814 		}
815 	}
816 
817 	mutex_lock(&pf->tc_mutex);
818 
819 	/* store the old configuration */
820 	tmp_dcbx_cfg = pf->hw.port_info->local_dcbx_cfg;
821 
822 	/* Reset the old DCBX configuration data */
823 	memset(&pi->local_dcbx_cfg, 0, sizeof(pi->local_dcbx_cfg));
824 
825 	/* Get updated DCBX data from firmware */
826 	ret = ice_get_dcb_cfg(pf->hw.port_info);
827 	if (ret) {
828 		dev_err(dev, "Failed to get DCB config\n");
829 		goto out;
830 	}
831 
832 	/* No change detected in DCBX configs */
833 	if (!memcmp(&tmp_dcbx_cfg, &pi->local_dcbx_cfg, sizeof(tmp_dcbx_cfg))) {
834 		dev_dbg(dev, "No change detected in DCBX configuration.\n");
835 		goto out;
836 	}
837 
838 	pf->dcbx_cap = ice_dcb_get_mode(pi, false);
839 
840 	need_reconfig = ice_dcb_need_recfg(pf, &tmp_dcbx_cfg,
841 					   &pi->local_dcbx_cfg);
842 	ice_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &pi->local_dcbx_cfg);
843 	if (!need_reconfig)
844 		goto out;
845 
846 	/* Enable DCB tagging only when more than one TC */
847 	if (ice_dcb_get_num_tc(&pi->local_dcbx_cfg) > 1) {
848 		dev_dbg(dev, "DCB tagging enabled (num TC > 1)\n");
849 		set_bit(ICE_FLAG_DCB_ENA, pf->flags);
850 	} else {
851 		dev_dbg(dev, "DCB tagging disabled (num TC = 1)\n");
852 		clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
853 	}
854 
855 	pf_vsi = ice_get_main_vsi(pf);
856 	if (!pf_vsi) {
857 		dev_dbg(dev, "PF VSI doesn't exist\n");
858 		goto out;
859 	}
860 
861 	rtnl_lock();
862 	ice_dis_vsi(pf_vsi, true);
863 
864 	ret = ice_query_port_ets(pf->hw.port_info, &buf, sizeof(buf), NULL);
865 	if (ret) {
866 		dev_err(dev, "Query Port ETS failed\n");
867 		goto unlock_rtnl;
868 	}
869 
870 	/* changes in configuration update VSI */
871 	ice_pf_dcb_recfg(pf);
872 
873 	ice_ena_vsi(pf_vsi, true);
874 unlock_rtnl:
875 	rtnl_unlock();
876 out:
877 	mutex_unlock(&pf->tc_mutex);
878 }
879