xref: /linux/drivers/net/ethernet/intel/ice/ice_dcb_lib.c (revision d53b8e36925256097a08d7cb749198d85cbf9b2b)
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 #include "devlink/devlink.h"
7 
8 /**
9  * ice_dcb_get_ena_tc - return bitmap of enabled TCs
10  * @dcbcfg: DCB config to evaluate for enabled TCs
11  */
12 static u8 ice_dcb_get_ena_tc(struct ice_dcbx_cfg *dcbcfg)
13 {
14 	u8 i, num_tc, ena_tc = 1;
15 
16 	num_tc = ice_dcb_get_num_tc(dcbcfg);
17 
18 	for (i = 0; i < num_tc; i++)
19 		ena_tc |= BIT(i);
20 
21 	return ena_tc;
22 }
23 
24 /**
25  * ice_is_pfc_causing_hung_q
26  * @pf: pointer to PF structure
27  * @txqueue: Tx queue which is supposedly hung queue
28  *
29  * find if PFC is causing the hung queue, if yes return true else false
30  */
31 bool ice_is_pfc_causing_hung_q(struct ice_pf *pf, unsigned int txqueue)
32 {
33 	u8 num_tcs = 0, i, tc, up_mapped_tc, up_in_tc = 0;
34 	u64 ref_prio_xoff[ICE_MAX_UP];
35 	struct ice_vsi *vsi;
36 	u32 up2tc;
37 
38 	vsi = ice_get_main_vsi(pf);
39 	if (!vsi)
40 		return false;
41 
42 	ice_for_each_traffic_class(i)
43 		if (vsi->tc_cfg.ena_tc & BIT(i))
44 			num_tcs++;
45 
46 	/* first find out the TC to which the hung queue belongs to */
47 	for (tc = 0; tc < num_tcs - 1; tc++)
48 		if (ice_find_q_in_range(vsi->tc_cfg.tc_info[tc].qoffset,
49 					vsi->tc_cfg.tc_info[tc + 1].qoffset,
50 					txqueue))
51 			break;
52 
53 	/* Build a bit map of all UPs associated to the suspect hung queue TC,
54 	 * so that we check for its counter increment.
55 	 */
56 	up2tc = rd32(&pf->hw, PRTDCB_TUP2TC);
57 	for (i = 0; i < ICE_MAX_UP; i++) {
58 		up_mapped_tc = (up2tc >> (i * 3)) & 0x7;
59 		if (up_mapped_tc == tc)
60 			up_in_tc |= BIT(i);
61 	}
62 
63 	/* Now that we figured out that hung queue is PFC enabled, still the
64 	 * Tx timeout can be legitimate. So to make sure Tx timeout is
65 	 * absolutely caused by PFC storm, check if the counters are
66 	 * incrementing.
67 	 */
68 	for (i = 0; i < ICE_MAX_UP; i++)
69 		if (up_in_tc & BIT(i))
70 			ref_prio_xoff[i] = pf->stats.priority_xoff_rx[i];
71 
72 	ice_update_dcb_stats(pf);
73 
74 	for (i = 0; i < ICE_MAX_UP; i++)
75 		if (up_in_tc & BIT(i))
76 			if (pf->stats.priority_xoff_rx[i] > ref_prio_xoff[i])
77 				return true;
78 
79 	return false;
80 }
81 
82 /**
83  * ice_dcb_get_mode - gets the DCB mode
84  * @port_info: pointer to port info structure
85  * @host: if set it's HOST if not it's MANAGED
86  */
87 static u8 ice_dcb_get_mode(struct ice_port_info *port_info, bool host)
88 {
89 	u8 mode;
90 
91 	if (host)
92 		mode = DCB_CAP_DCBX_HOST;
93 	else
94 		mode = DCB_CAP_DCBX_LLD_MANAGED;
95 
96 	if (port_info->qos_cfg.local_dcbx_cfg.dcbx_mode & ICE_DCBX_MODE_CEE)
97 		return mode | DCB_CAP_DCBX_VER_CEE;
98 	else
99 		return mode | DCB_CAP_DCBX_VER_IEEE;
100 }
101 
102 /**
103  * ice_dcb_get_num_tc - Get the number of TCs from DCBX config
104  * @dcbcfg: config to retrieve number of TCs from
105  */
106 u8 ice_dcb_get_num_tc(struct ice_dcbx_cfg *dcbcfg)
107 {
108 	bool tc_unused = false;
109 	u8 num_tc = 0;
110 	u8 ret = 0;
111 	int i;
112 
113 	/* Scan the ETS Config Priority Table to find traffic classes
114 	 * enabled and create a bitmask of enabled TCs
115 	 */
116 	for (i = 0; i < CEE_DCBX_MAX_PRIO; i++)
117 		num_tc |= BIT(dcbcfg->etscfg.prio_table[i]);
118 
119 	/* Scan bitmask for contiguous TCs starting with TC0 */
120 	for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
121 		if (num_tc & BIT(i)) {
122 			if (!tc_unused) {
123 				ret++;
124 			} else {
125 				pr_err("Non-contiguous TCs - Disabling DCB\n");
126 				return 1;
127 			}
128 		} else {
129 			tc_unused = true;
130 		}
131 	}
132 
133 	/* There is always at least 1 TC */
134 	if (!ret)
135 		ret = 1;
136 
137 	return ret;
138 }
139 
140 /**
141  * ice_get_first_droptc - returns number of first droptc
142  * @vsi: used to find the first droptc
143  *
144  * This function returns the value of first_droptc.
145  * When DCB is enabled, first droptc information is derived from enabled_tc
146  * and PFC enabled bits. otherwise this function returns 0 as there is one
147  * TC without DCB (tc0)
148  */
149 static u8 ice_get_first_droptc(struct ice_vsi *vsi)
150 {
151 	struct ice_dcbx_cfg *cfg = &vsi->port_info->qos_cfg.local_dcbx_cfg;
152 	struct device *dev = ice_pf_to_dev(vsi->back);
153 	u8 num_tc, ena_tc_map, pfc_ena_map;
154 	u8 i;
155 
156 	num_tc = ice_dcb_get_num_tc(cfg);
157 
158 	/* get bitmap of enabled TCs */
159 	ena_tc_map = ice_dcb_get_ena_tc(cfg);
160 
161 	/* get bitmap of PFC enabled TCs */
162 	pfc_ena_map = cfg->pfc.pfcena;
163 
164 	/* get first TC that is not PFC enabled */
165 	for (i = 0; i < num_tc; i++) {
166 		if ((ena_tc_map & BIT(i)) && (!(pfc_ena_map & BIT(i)))) {
167 			dev_dbg(dev, "first drop tc = %d\n", i);
168 			return i;
169 		}
170 	}
171 
172 	dev_dbg(dev, "first drop tc = 0\n");
173 	return 0;
174 }
175 
176 /**
177  * ice_vsi_set_dcb_tc_cfg - Set VSI's TC based on DCB configuration
178  * @vsi: pointer to the VSI instance
179  */
180 void ice_vsi_set_dcb_tc_cfg(struct ice_vsi *vsi)
181 {
182 	struct ice_dcbx_cfg *cfg = &vsi->port_info->qos_cfg.local_dcbx_cfg;
183 
184 	switch (vsi->type) {
185 	case ICE_VSI_PF:
186 		vsi->tc_cfg.ena_tc = ice_dcb_get_ena_tc(cfg);
187 		vsi->tc_cfg.numtc = ice_dcb_get_num_tc(cfg);
188 		break;
189 	case ICE_VSI_CHNL:
190 		vsi->tc_cfg.ena_tc = BIT(ice_get_first_droptc(vsi));
191 		vsi->tc_cfg.numtc = 1;
192 		break;
193 	case ICE_VSI_CTRL:
194 	case ICE_VSI_LB:
195 	default:
196 		vsi->tc_cfg.ena_tc = ICE_DFLT_TRAFFIC_CLASS;
197 		vsi->tc_cfg.numtc = 1;
198 	}
199 }
200 
201 /**
202  * ice_dcb_get_tc - Get the TC associated with the queue
203  * @vsi: ptr to the VSI
204  * @queue_index: queue number associated with VSI
205  */
206 u8 ice_dcb_get_tc(struct ice_vsi *vsi, int queue_index)
207 {
208 	return vsi->tx_rings[queue_index]->dcb_tc;
209 }
210 
211 /**
212  * ice_vsi_cfg_dcb_rings - Update rings to reflect DCB TC
213  * @vsi: VSI owner of rings being updated
214  */
215 void ice_vsi_cfg_dcb_rings(struct ice_vsi *vsi)
216 {
217 	struct ice_tx_ring *tx_ring;
218 	struct ice_rx_ring *rx_ring;
219 	u16 qoffset, qcount;
220 	int i, n;
221 
222 	if (!test_bit(ICE_FLAG_DCB_ENA, vsi->back->flags)) {
223 		/* Reset the TC information */
224 		ice_for_each_txq(vsi, i) {
225 			tx_ring = vsi->tx_rings[i];
226 			tx_ring->dcb_tc = 0;
227 		}
228 		ice_for_each_rxq(vsi, i) {
229 			rx_ring = vsi->rx_rings[i];
230 			rx_ring->dcb_tc = 0;
231 		}
232 		return;
233 	}
234 
235 	ice_for_each_traffic_class(n) {
236 		if (!(vsi->tc_cfg.ena_tc & BIT(n)))
237 			break;
238 
239 		qoffset = vsi->tc_cfg.tc_info[n].qoffset;
240 		qcount = vsi->tc_cfg.tc_info[n].qcount_tx;
241 		for (i = qoffset; i < (qoffset + qcount); i++)
242 			vsi->tx_rings[i]->dcb_tc = n;
243 
244 		qcount = vsi->tc_cfg.tc_info[n].qcount_rx;
245 		for (i = qoffset; i < (qoffset + qcount); i++)
246 			vsi->rx_rings[i]->dcb_tc = n;
247 	}
248 	/* applicable only if "all_enatc" is set, which will be set from
249 	 * setup_tc method as part of configuring channels
250 	 */
251 	if (vsi->all_enatc) {
252 		u8 first_droptc = ice_get_first_droptc(vsi);
253 
254 		/* When DCB is configured, TC for ADQ queues (which are really
255 		 * PF queues) should be the first drop TC of the main VSI
256 		 */
257 		ice_for_each_chnl_tc(n) {
258 			if (!(vsi->all_enatc & BIT(n)))
259 				break;
260 
261 			qoffset = vsi->mqprio_qopt.qopt.offset[n];
262 			qcount = vsi->mqprio_qopt.qopt.count[n];
263 			for (i = qoffset; i < (qoffset + qcount); i++) {
264 				vsi->tx_rings[i]->dcb_tc = first_droptc;
265 				vsi->rx_rings[i]->dcb_tc = first_droptc;
266 			}
267 		}
268 	}
269 }
270 
271 /**
272  * ice_dcb_ena_dis_vsi - disable certain VSIs for DCB config/reconfig
273  * @pf: pointer to the PF instance
274  * @ena: true to enable VSIs, false to disable
275  * @locked: true if caller holds RTNL lock, false otherwise
276  *
277  * Before a new DCB configuration can be applied, VSIs of type PF, SWITCHDEV
278  * and CHNL need to be brought down. Following completion of DCB configuration
279  * the VSIs that were downed need to be brought up again. This helper function
280  * does both.
281  */
282 static void ice_dcb_ena_dis_vsi(struct ice_pf *pf, bool ena, bool locked)
283 {
284 	int i;
285 
286 	ice_for_each_vsi(pf, i) {
287 		struct ice_vsi *vsi = pf->vsi[i];
288 
289 		if (!vsi)
290 			continue;
291 
292 		switch (vsi->type) {
293 		case ICE_VSI_CHNL:
294 		case ICE_VSI_PF:
295 			if (ena)
296 				ice_ena_vsi(vsi, locked);
297 			else
298 				ice_dis_vsi(vsi, locked);
299 			break;
300 		default:
301 			continue;
302 		}
303 	}
304 }
305 
306 /**
307  * ice_dcb_bwchk - check if ETS bandwidth input parameters are correct
308  * @pf: pointer to the PF struct
309  * @dcbcfg: pointer to DCB config structure
310  */
311 int ice_dcb_bwchk(struct ice_pf *pf, struct ice_dcbx_cfg *dcbcfg)
312 {
313 	struct ice_dcb_ets_cfg *etscfg = &dcbcfg->etscfg;
314 	u8 num_tc, total_bw = 0;
315 	int i;
316 
317 	/* returns number of contigous TCs and 1 TC for non-contigous TCs,
318 	 * since at least 1 TC has to be configured
319 	 */
320 	num_tc = ice_dcb_get_num_tc(dcbcfg);
321 
322 	/* no bandwidth checks required if there's only one TC, so assign
323 	 * all bandwidth to TC0 and return
324 	 */
325 	if (num_tc == 1) {
326 		etscfg->tcbwtable[0] = ICE_TC_MAX_BW;
327 		return 0;
328 	}
329 
330 	for (i = 0; i < num_tc; i++)
331 		total_bw += etscfg->tcbwtable[i];
332 
333 	if (!total_bw) {
334 		etscfg->tcbwtable[0] = ICE_TC_MAX_BW;
335 	} else if (total_bw != ICE_TC_MAX_BW) {
336 		dev_err(ice_pf_to_dev(pf), "Invalid config, total bandwidth must equal 100\n");
337 		return -EINVAL;
338 	}
339 
340 	return 0;
341 }
342 
343 /**
344  * ice_pf_dcb_cfg - Apply new DCB configuration
345  * @pf: pointer to the PF struct
346  * @new_cfg: DCBX config to apply
347  * @locked: is the RTNL held
348  */
349 int ice_pf_dcb_cfg(struct ice_pf *pf, struct ice_dcbx_cfg *new_cfg, bool locked)
350 {
351 	struct ice_aqc_port_ets_elem buf = { 0 };
352 	struct ice_dcbx_cfg *old_cfg, *curr_cfg;
353 	struct device *dev = ice_pf_to_dev(pf);
354 	int ret = ICE_DCB_NO_HW_CHG;
355 	struct iidc_event *event;
356 	struct ice_vsi *pf_vsi;
357 
358 	curr_cfg = &pf->hw.port_info->qos_cfg.local_dcbx_cfg;
359 
360 	/* FW does not care if change happened */
361 	if (!pf->hw.port_info->qos_cfg.is_sw_lldp)
362 		ret = ICE_DCB_HW_CHG_RST;
363 
364 	/* Enable DCB tagging only when more than one TC */
365 	if (ice_dcb_get_num_tc(new_cfg) > 1) {
366 		dev_dbg(dev, "DCB tagging enabled (num TC > 1)\n");
367 		if (pf->hw.port_info->is_custom_tx_enabled) {
368 			dev_err(dev, "Custom Tx scheduler feature enabled, can't configure DCB\n");
369 			return -EBUSY;
370 		}
371 		ice_tear_down_devlink_rate_tree(pf);
372 
373 		set_bit(ICE_FLAG_DCB_ENA, pf->flags);
374 	} else {
375 		dev_dbg(dev, "DCB tagging disabled (num TC = 1)\n");
376 		clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
377 	}
378 
379 	if (!memcmp(new_cfg, curr_cfg, sizeof(*new_cfg))) {
380 		dev_dbg(dev, "No change in DCB config required\n");
381 		return ret;
382 	}
383 
384 	if (ice_dcb_bwchk(pf, new_cfg))
385 		return -EINVAL;
386 
387 	/* Store old config in case FW config fails */
388 	old_cfg = kmemdup(curr_cfg, sizeof(*old_cfg), GFP_KERNEL);
389 	if (!old_cfg)
390 		return -ENOMEM;
391 
392 	dev_info(dev, "Commit DCB Configuration to the hardware\n");
393 	pf_vsi = ice_get_main_vsi(pf);
394 	if (!pf_vsi) {
395 		dev_dbg(dev, "PF VSI doesn't exist\n");
396 		ret = -EINVAL;
397 		goto free_cfg;
398 	}
399 
400 	/* Notify AUX drivers about impending change to TCs */
401 	event = kzalloc(sizeof(*event), GFP_KERNEL);
402 	if (!event) {
403 		ret = -ENOMEM;
404 		goto free_cfg;
405 	}
406 
407 	set_bit(IIDC_EVENT_BEFORE_TC_CHANGE, event->type);
408 	ice_send_event_to_aux(pf, event);
409 	kfree(event);
410 
411 	/* avoid race conditions by holding the lock while disabling and
412 	 * re-enabling the VSI
413 	 */
414 	if (!locked)
415 		rtnl_lock();
416 
417 	/* disable VSIs affected by DCB changes */
418 	ice_dcb_ena_dis_vsi(pf, false, true);
419 
420 	memcpy(curr_cfg, new_cfg, sizeof(*curr_cfg));
421 	memcpy(&curr_cfg->etsrec, &curr_cfg->etscfg, sizeof(curr_cfg->etsrec));
422 	memcpy(&new_cfg->etsrec, &curr_cfg->etscfg, sizeof(curr_cfg->etsrec));
423 
424 	/* Only send new config to HW if we are in SW LLDP mode. Otherwise,
425 	 * the new config came from the HW in the first place.
426 	 */
427 	if (pf->hw.port_info->qos_cfg.is_sw_lldp) {
428 		ret = ice_set_dcb_cfg(pf->hw.port_info);
429 		if (ret) {
430 			dev_err(dev, "Set DCB Config failed\n");
431 			/* Restore previous settings to local config */
432 			memcpy(curr_cfg, old_cfg, sizeof(*curr_cfg));
433 			goto out;
434 		}
435 	}
436 
437 	ret = ice_query_port_ets(pf->hw.port_info, &buf, sizeof(buf), NULL);
438 	if (ret) {
439 		dev_err(dev, "Query Port ETS failed\n");
440 		goto out;
441 	}
442 
443 	ice_pf_dcb_recfg(pf, false);
444 
445 out:
446 	/* enable previously downed VSIs */
447 	ice_dcb_ena_dis_vsi(pf, true, true);
448 	if (!locked)
449 		rtnl_unlock();
450 free_cfg:
451 	kfree(old_cfg);
452 	return ret;
453 }
454 
455 /**
456  * ice_cfg_etsrec_defaults - Set default ETS recommended DCB config
457  * @pi: port information structure
458  */
459 static void ice_cfg_etsrec_defaults(struct ice_port_info *pi)
460 {
461 	struct ice_dcbx_cfg *dcbcfg = &pi->qos_cfg.local_dcbx_cfg;
462 	u8 i;
463 
464 	/* Ensure ETS recommended DCB configuration is not already set */
465 	if (dcbcfg->etsrec.maxtcs)
466 		return;
467 
468 	/* In CEE mode, set the default to 1 TC */
469 	dcbcfg->etsrec.maxtcs = 1;
470 	for (i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++) {
471 		dcbcfg->etsrec.tcbwtable[i] = i ? 0 : 100;
472 		dcbcfg->etsrec.tsatable[i] = i ? ICE_IEEE_TSA_STRICT :
473 						 ICE_IEEE_TSA_ETS;
474 	}
475 }
476 
477 /**
478  * ice_dcb_need_recfg - Check if DCB needs reconfig
479  * @pf: board private structure
480  * @old_cfg: current DCB config
481  * @new_cfg: new DCB config
482  */
483 static bool
484 ice_dcb_need_recfg(struct ice_pf *pf, struct ice_dcbx_cfg *old_cfg,
485 		   struct ice_dcbx_cfg *new_cfg)
486 {
487 	struct device *dev = ice_pf_to_dev(pf);
488 	bool need_reconfig = false;
489 
490 	/* Check if ETS configuration has changed */
491 	if (memcmp(&new_cfg->etscfg, &old_cfg->etscfg,
492 		   sizeof(new_cfg->etscfg))) {
493 		/* If Priority Table has changed reconfig is needed */
494 		if (memcmp(&new_cfg->etscfg.prio_table,
495 			   &old_cfg->etscfg.prio_table,
496 			   sizeof(new_cfg->etscfg.prio_table))) {
497 			need_reconfig = true;
498 			dev_dbg(dev, "ETS UP2TC changed.\n");
499 		}
500 
501 		if (memcmp(&new_cfg->etscfg.tcbwtable,
502 			   &old_cfg->etscfg.tcbwtable,
503 			   sizeof(new_cfg->etscfg.tcbwtable)))
504 			dev_dbg(dev, "ETS TC BW Table changed.\n");
505 
506 		if (memcmp(&new_cfg->etscfg.tsatable,
507 			   &old_cfg->etscfg.tsatable,
508 			   sizeof(new_cfg->etscfg.tsatable)))
509 			dev_dbg(dev, "ETS TSA Table changed.\n");
510 	}
511 
512 	/* Check if PFC configuration has changed */
513 	if (memcmp(&new_cfg->pfc, &old_cfg->pfc, sizeof(new_cfg->pfc))) {
514 		need_reconfig = true;
515 		dev_dbg(dev, "PFC config change detected.\n");
516 	}
517 
518 	/* Check if APP Table has changed */
519 	if (memcmp(&new_cfg->app, &old_cfg->app, sizeof(new_cfg->app))) {
520 		need_reconfig = true;
521 		dev_dbg(dev, "APP Table change detected.\n");
522 	}
523 
524 	dev_dbg(dev, "dcb need_reconfig=%d\n", need_reconfig);
525 	return need_reconfig;
526 }
527 
528 /**
529  * ice_dcb_rebuild - rebuild DCB post reset
530  * @pf: physical function instance
531  */
532 void ice_dcb_rebuild(struct ice_pf *pf)
533 {
534 	struct ice_aqc_port_ets_elem buf = { 0 };
535 	struct device *dev = ice_pf_to_dev(pf);
536 	struct ice_dcbx_cfg *err_cfg;
537 	int ret;
538 
539 	ret = ice_query_port_ets(pf->hw.port_info, &buf, sizeof(buf), NULL);
540 	if (ret) {
541 		dev_err(dev, "Query Port ETS failed\n");
542 		goto dcb_error;
543 	}
544 
545 	mutex_lock(&pf->tc_mutex);
546 
547 	if (!pf->hw.port_info->qos_cfg.is_sw_lldp)
548 		ice_cfg_etsrec_defaults(pf->hw.port_info);
549 
550 	ret = ice_set_dcb_cfg(pf->hw.port_info);
551 	if (ret) {
552 		dev_err(dev, "Failed to set DCB config in rebuild\n");
553 		goto dcb_error;
554 	}
555 
556 	if (!pf->hw.port_info->qos_cfg.is_sw_lldp) {
557 		ret = ice_cfg_lldp_mib_change(&pf->hw, true);
558 		if (ret && !pf->hw.port_info->qos_cfg.is_sw_lldp) {
559 			dev_err(dev, "Failed to register for MIB changes\n");
560 			goto dcb_error;
561 		}
562 	}
563 
564 	dev_info(dev, "DCB info restored\n");
565 	ret = ice_query_port_ets(pf->hw.port_info, &buf, sizeof(buf), NULL);
566 	if (ret) {
567 		dev_err(dev, "Query Port ETS failed\n");
568 		goto dcb_error;
569 	}
570 
571 	mutex_unlock(&pf->tc_mutex);
572 
573 	return;
574 
575 dcb_error:
576 	dev_err(dev, "Disabling DCB until new settings occur\n");
577 	err_cfg = kzalloc(sizeof(*err_cfg), GFP_KERNEL);
578 	if (!err_cfg) {
579 		mutex_unlock(&pf->tc_mutex);
580 		return;
581 	}
582 
583 	err_cfg->etscfg.willing = true;
584 	err_cfg->etscfg.tcbwtable[0] = ICE_TC_MAX_BW;
585 	err_cfg->etscfg.tsatable[0] = ICE_IEEE_TSA_ETS;
586 	memcpy(&err_cfg->etsrec, &err_cfg->etscfg, sizeof(err_cfg->etsrec));
587 	/* Coverity warns the return code of ice_pf_dcb_cfg() is not checked
588 	 * here as is done for other calls to that function. That check is
589 	 * not necessary since this is in this function's error cleanup path.
590 	 * Suppress the Coverity warning with the following comment...
591 	 */
592 	/* coverity[check_return] */
593 	ice_pf_dcb_cfg(pf, err_cfg, false);
594 	kfree(err_cfg);
595 
596 	mutex_unlock(&pf->tc_mutex);
597 }
598 
599 /**
600  * ice_dcb_init_cfg - set the initial DCB config in SW
601  * @pf: PF to apply config to
602  * @locked: Is the RTNL held
603  */
604 static int ice_dcb_init_cfg(struct ice_pf *pf, bool locked)
605 {
606 	struct ice_dcbx_cfg *newcfg;
607 	struct ice_port_info *pi;
608 	int ret = 0;
609 
610 	pi = pf->hw.port_info;
611 	newcfg = kmemdup(&pi->qos_cfg.local_dcbx_cfg, sizeof(*newcfg),
612 			 GFP_KERNEL);
613 	if (!newcfg)
614 		return -ENOMEM;
615 
616 	memset(&pi->qos_cfg.local_dcbx_cfg, 0, sizeof(*newcfg));
617 
618 	dev_info(ice_pf_to_dev(pf), "Configuring initial DCB values\n");
619 	if (ice_pf_dcb_cfg(pf, newcfg, locked))
620 		ret = -EINVAL;
621 
622 	kfree(newcfg);
623 
624 	return ret;
625 }
626 
627 /**
628  * ice_dcb_sw_dflt_cfg - Apply a default DCB config
629  * @pf: PF to apply config to
630  * @ets_willing: configure ETS willing
631  * @locked: was this function called with RTNL held
632  */
633 int ice_dcb_sw_dflt_cfg(struct ice_pf *pf, bool ets_willing, bool locked)
634 {
635 	struct ice_aqc_port_ets_elem buf = { 0 };
636 	struct ice_dcbx_cfg *dcbcfg;
637 	struct ice_port_info *pi;
638 	struct ice_hw *hw;
639 	int ret;
640 
641 	hw = &pf->hw;
642 	pi = hw->port_info;
643 	dcbcfg = kzalloc(sizeof(*dcbcfg), GFP_KERNEL);
644 	if (!dcbcfg)
645 		return -ENOMEM;
646 
647 	memset(&pi->qos_cfg.local_dcbx_cfg, 0, sizeof(*dcbcfg));
648 
649 	dcbcfg->etscfg.willing = ets_willing ? 1 : 0;
650 	dcbcfg->etscfg.maxtcs = hw->func_caps.common_cap.maxtc;
651 	dcbcfg->etscfg.tcbwtable[0] = 100;
652 	dcbcfg->etscfg.tsatable[0] = ICE_IEEE_TSA_ETS;
653 
654 	memcpy(&dcbcfg->etsrec, &dcbcfg->etscfg,
655 	       sizeof(dcbcfg->etsrec));
656 	dcbcfg->etsrec.willing = 0;
657 
658 	dcbcfg->pfc.willing = 1;
659 	dcbcfg->pfc.pfccap = hw->func_caps.common_cap.maxtc;
660 
661 	dcbcfg->numapps = 1;
662 	dcbcfg->app[0].selector = ICE_APP_SEL_ETHTYPE;
663 	dcbcfg->app[0].priority = 3;
664 	dcbcfg->app[0].prot_id = ETH_P_FCOE;
665 
666 	ret = ice_pf_dcb_cfg(pf, dcbcfg, locked);
667 	kfree(dcbcfg);
668 	if (ret)
669 		return ret;
670 
671 	return ice_query_port_ets(pi, &buf, sizeof(buf), NULL);
672 }
673 
674 /**
675  * ice_dcb_tc_contig - Check that TCs are contiguous
676  * @prio_table: pointer to priority table
677  *
678  * Check if TCs begin with TC0 and are contiguous
679  */
680 static bool ice_dcb_tc_contig(u8 *prio_table)
681 {
682 	bool found_empty = false;
683 	u8 used_tc = 0;
684 	int i;
685 
686 	/* Create a bitmap of used TCs */
687 	for (i = 0; i < CEE_DCBX_MAX_PRIO; i++)
688 		used_tc |= BIT(prio_table[i]);
689 
690 	for (i = 0; i < CEE_DCBX_MAX_PRIO; i++) {
691 		if (used_tc & BIT(i)) {
692 			if (found_empty)
693 				return false;
694 		} else {
695 			found_empty = true;
696 		}
697 	}
698 
699 	return true;
700 }
701 
702 /**
703  * ice_dcb_noncontig_cfg - Configure DCB for non-contiguous TCs
704  * @pf: pointer to the PF struct
705  *
706  * If non-contiguous TCs, then configure SW DCB with TC0 and ETS non-willing
707  */
708 static int ice_dcb_noncontig_cfg(struct ice_pf *pf)
709 {
710 	struct ice_dcbx_cfg *dcbcfg = &pf->hw.port_info->qos_cfg.local_dcbx_cfg;
711 	struct device *dev = ice_pf_to_dev(pf);
712 	int ret;
713 
714 	/* Configure SW DCB default with ETS non-willing */
715 	ret = ice_dcb_sw_dflt_cfg(pf, false, true);
716 	if (ret) {
717 		dev_err(dev, "Failed to set local DCB config %d\n", ret);
718 		return ret;
719 	}
720 
721 	/* Reconfigure with ETS willing so that FW will send LLDP MIB event */
722 	dcbcfg->etscfg.willing = 1;
723 	ret = ice_set_dcb_cfg(pf->hw.port_info);
724 	if (ret)
725 		dev_err(dev, "Failed to set DCB to unwilling\n");
726 
727 	return ret;
728 }
729 
730 /**
731  * ice_pf_dcb_recfg - Reconfigure all VEBs and VSIs
732  * @pf: pointer to the PF struct
733  * @locked: is adev device lock held
734  *
735  * Assumed caller has already disabled all VSIs before
736  * calling this function. Reconfiguring DCB based on
737  * local_dcbx_cfg.
738  */
739 void ice_pf_dcb_recfg(struct ice_pf *pf, bool locked)
740 {
741 	struct ice_dcbx_cfg *dcbcfg = &pf->hw.port_info->qos_cfg.local_dcbx_cfg;
742 	struct iidc_event *event;
743 	u8 tc_map = 0;
744 	int v, ret;
745 
746 	/* Update each VSI */
747 	ice_for_each_vsi(pf, v) {
748 		struct ice_vsi *vsi = pf->vsi[v];
749 
750 		if (!vsi)
751 			continue;
752 
753 		if (vsi->type == ICE_VSI_PF) {
754 			tc_map = ice_dcb_get_ena_tc(dcbcfg);
755 
756 			/* If DCBX request non-contiguous TC, then configure
757 			 * default TC
758 			 */
759 			if (!ice_dcb_tc_contig(dcbcfg->etscfg.prio_table)) {
760 				tc_map = ICE_DFLT_TRAFFIC_CLASS;
761 				ice_dcb_noncontig_cfg(pf);
762 			}
763 		} else if (vsi->type == ICE_VSI_CHNL) {
764 			tc_map = BIT(ice_get_first_droptc(vsi));
765 		} else {
766 			tc_map = ICE_DFLT_TRAFFIC_CLASS;
767 		}
768 
769 		ret = ice_vsi_cfg_tc(vsi, tc_map);
770 		if (ret) {
771 			dev_err(ice_pf_to_dev(pf), "Failed to config TC for VSI index: %d\n",
772 				vsi->idx);
773 			continue;
774 		}
775 		/* no need to proceed with remaining cfg if it is CHNL
776 		 * or switchdev VSI
777 		 */
778 		if (vsi->type == ICE_VSI_CHNL)
779 			continue;
780 
781 		ice_vsi_map_rings_to_vectors(vsi);
782 		if (vsi->type == ICE_VSI_PF)
783 			ice_dcbnl_set_all(vsi);
784 	}
785 	if (!locked) {
786 		/* Notify the AUX drivers that TC change is finished */
787 		event = kzalloc(sizeof(*event), GFP_KERNEL);
788 		if (!event)
789 			return;
790 
791 		set_bit(IIDC_EVENT_AFTER_TC_CHANGE, event->type);
792 		ice_send_event_to_aux(pf, event);
793 		kfree(event);
794 	}
795 }
796 
797 /**
798  * ice_init_pf_dcb - initialize DCB for a PF
799  * @pf: PF to initialize DCB for
800  * @locked: Was function called with RTNL held
801  */
802 int ice_init_pf_dcb(struct ice_pf *pf, bool locked)
803 {
804 	struct device *dev = ice_pf_to_dev(pf);
805 	struct ice_port_info *port_info;
806 	struct ice_hw *hw = &pf->hw;
807 	int err;
808 
809 	port_info = hw->port_info;
810 
811 	err = ice_init_dcb(hw, false);
812 	if (err && !port_info->qos_cfg.is_sw_lldp) {
813 		dev_err(dev, "Error initializing DCB %d\n", err);
814 		goto dcb_init_err;
815 	}
816 
817 	dev_info(dev, "DCB is enabled in the hardware, max number of TCs supported on this port are %d\n",
818 		 pf->hw.func_caps.common_cap.maxtc);
819 	if (err) {
820 		struct ice_vsi *pf_vsi;
821 
822 		/* FW LLDP is disabled, activate SW DCBX/LLDP mode */
823 		dev_info(dev, "FW LLDP is disabled, DCBx/LLDP in SW mode.\n");
824 		clear_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags);
825 		err = ice_aq_set_pfc_mode(&pf->hw, ICE_AQC_PFC_VLAN_BASED_PFC,
826 					  NULL);
827 		if (err)
828 			dev_info(dev, "Failed to set VLAN PFC mode\n");
829 
830 		err = ice_dcb_sw_dflt_cfg(pf, true, locked);
831 		if (err) {
832 			dev_err(dev, "Failed to set local DCB config %d\n",
833 				err);
834 			err = -EIO;
835 			goto dcb_init_err;
836 		}
837 
838 		/* If the FW DCBX engine is not running then Rx LLDP packets
839 		 * need to be redirected up the stack.
840 		 */
841 		pf_vsi = ice_get_main_vsi(pf);
842 		if (!pf_vsi) {
843 			dev_err(dev, "Failed to set local DCB config\n");
844 			err = -EIO;
845 			goto dcb_init_err;
846 		}
847 
848 		ice_cfg_sw_lldp(pf_vsi, false, true);
849 
850 		pf->dcbx_cap = ice_dcb_get_mode(port_info, true);
851 		return 0;
852 	}
853 
854 	set_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags);
855 
856 	/* DCBX/LLDP enabled in FW, set DCBNL mode advertisement */
857 	pf->dcbx_cap = ice_dcb_get_mode(port_info, false);
858 
859 	err = ice_dcb_init_cfg(pf, locked);
860 	if (err)
861 		goto dcb_init_err;
862 
863 	return 0;
864 
865 dcb_init_err:
866 	dev_err(dev, "DCB init failed\n");
867 	return err;
868 }
869 
870 /**
871  * ice_update_dcb_stats - Update DCB stats counters
872  * @pf: PF whose stats needs to be updated
873  */
874 void ice_update_dcb_stats(struct ice_pf *pf)
875 {
876 	struct ice_hw_port_stats *prev_ps, *cur_ps;
877 	struct ice_hw *hw = &pf->hw;
878 	u8 port;
879 	int i;
880 
881 	port = hw->port_info->lport;
882 	prev_ps = &pf->stats_prev;
883 	cur_ps = &pf->stats;
884 
885 	if (ice_is_reset_in_progress(pf->state))
886 		pf->stat_prev_loaded = false;
887 
888 	for (i = 0; i < 8; i++) {
889 		ice_stat_update32(hw, GLPRT_PXOFFRXC(port, i),
890 				  pf->stat_prev_loaded,
891 				  &prev_ps->priority_xoff_rx[i],
892 				  &cur_ps->priority_xoff_rx[i]);
893 		ice_stat_update32(hw, GLPRT_PXONRXC(port, i),
894 				  pf->stat_prev_loaded,
895 				  &prev_ps->priority_xon_rx[i],
896 				  &cur_ps->priority_xon_rx[i]);
897 		ice_stat_update32(hw, GLPRT_PXONTXC(port, i),
898 				  pf->stat_prev_loaded,
899 				  &prev_ps->priority_xon_tx[i],
900 				  &cur_ps->priority_xon_tx[i]);
901 		ice_stat_update32(hw, GLPRT_PXOFFTXC(port, i),
902 				  pf->stat_prev_loaded,
903 				  &prev_ps->priority_xoff_tx[i],
904 				  &cur_ps->priority_xoff_tx[i]);
905 		ice_stat_update32(hw, GLPRT_RXON2OFFCNT(port, i),
906 				  pf->stat_prev_loaded,
907 				  &prev_ps->priority_xon_2_xoff[i],
908 				  &cur_ps->priority_xon_2_xoff[i]);
909 	}
910 }
911 
912 /**
913  * ice_tx_prepare_vlan_flags_dcb - prepare VLAN tagging for DCB
914  * @tx_ring: ring to send buffer on
915  * @first: pointer to struct ice_tx_buf
916  *
917  * This should not be called if the outer VLAN is software offloaded as the VLAN
918  * tag will already be configured with the correct ID and priority bits
919  */
920 void
921 ice_tx_prepare_vlan_flags_dcb(struct ice_tx_ring *tx_ring,
922 			      struct ice_tx_buf *first)
923 {
924 	struct sk_buff *skb = first->skb;
925 
926 	if (!test_bit(ICE_FLAG_DCB_ENA, tx_ring->vsi->back->flags))
927 		return;
928 
929 	/* Insert 802.1p priority into VLAN header */
930 	if ((first->tx_flags & ICE_TX_FLAGS_HW_VLAN ||
931 	     first->tx_flags & ICE_TX_FLAGS_HW_OUTER_SINGLE_VLAN) ||
932 	    skb->priority != TC_PRIO_CONTROL) {
933 		first->vid &= ~VLAN_PRIO_MASK;
934 		/* Mask the lower 3 bits to set the 802.1p priority */
935 		first->vid |= FIELD_PREP(VLAN_PRIO_MASK, skb->priority);
936 		/* if this is not already set it means a VLAN 0 + priority needs
937 		 * to be offloaded
938 		 */
939 		if (tx_ring->flags & ICE_TX_FLAGS_RING_VLAN_L2TAG2)
940 			first->tx_flags |= ICE_TX_FLAGS_HW_OUTER_SINGLE_VLAN;
941 		else
942 			first->tx_flags |= ICE_TX_FLAGS_HW_VLAN;
943 	}
944 }
945 
946 /**
947  * ice_dcb_is_mib_change_pending - Check if MIB change is pending
948  * @state: MIB change state
949  */
950 static bool ice_dcb_is_mib_change_pending(u8 state)
951 {
952 	return ICE_AQ_LLDP_MIB_CHANGE_PENDING ==
953 		FIELD_GET(ICE_AQ_LLDP_MIB_CHANGE_STATE_M, state);
954 }
955 
956 /**
957  * ice_dcb_process_lldp_set_mib_change - Process MIB change
958  * @pf: ptr to ice_pf
959  * @event: pointer to the admin queue receive event
960  */
961 void
962 ice_dcb_process_lldp_set_mib_change(struct ice_pf *pf,
963 				    struct ice_rq_event_info *event)
964 {
965 	struct ice_aqc_port_ets_elem buf = { 0 };
966 	struct device *dev = ice_pf_to_dev(pf);
967 	struct ice_aqc_lldp_get_mib *mib;
968 	struct ice_dcbx_cfg tmp_dcbx_cfg;
969 	bool pending_handled = true;
970 	bool need_reconfig = false;
971 	struct ice_port_info *pi;
972 	u8 mib_type;
973 	int ret;
974 
975 	/* Not DCB capable or capability disabled */
976 	if (!(test_bit(ICE_FLAG_DCB_CAPABLE, pf->flags)))
977 		return;
978 
979 	if (pf->dcbx_cap & DCB_CAP_DCBX_HOST) {
980 		dev_dbg(dev, "MIB Change Event in HOST mode\n");
981 		return;
982 	}
983 
984 	pi = pf->hw.port_info;
985 	mib = (struct ice_aqc_lldp_get_mib *)&event->desc.params.raw;
986 
987 	/* Ignore if event is not for Nearest Bridge */
988 	mib_type = FIELD_GET(ICE_AQ_LLDP_BRID_TYPE_M, mib->type);
989 	dev_dbg(dev, "LLDP event MIB bridge type 0x%x\n", mib_type);
990 	if (mib_type != ICE_AQ_LLDP_BRID_TYPE_NEAREST_BRID)
991 		return;
992 
993 	/* A pending change event contains accurate config information, and
994 	 * the FW setting has not been updaed yet, so detect if change is
995 	 * pending to determine where to pull config information from
996 	 * (FW vs event)
997 	 */
998 	if (ice_dcb_is_mib_change_pending(mib->state))
999 		pending_handled = false;
1000 
1001 	/* Check MIB Type and return if event for Remote MIB update */
1002 	mib_type = FIELD_GET(ICE_AQ_LLDP_MIB_TYPE_M, mib->type);
1003 	dev_dbg(dev, "LLDP event mib type %s\n", mib_type ? "remote" : "local");
1004 	if (mib_type == ICE_AQ_LLDP_MIB_REMOTE) {
1005 		/* Update the remote cached instance and return */
1006 		if (!pending_handled) {
1007 			ice_get_dcb_cfg_from_mib_change(pi, event);
1008 		} else {
1009 			ret =
1010 			  ice_aq_get_dcb_cfg(pi->hw, ICE_AQ_LLDP_MIB_REMOTE,
1011 					     ICE_AQ_LLDP_BRID_TYPE_NEAREST_BRID,
1012 					     &pi->qos_cfg.remote_dcbx_cfg);
1013 			if (ret)
1014 				dev_dbg(dev, "Failed to get remote DCB config\n");
1015 		}
1016 		return;
1017 	}
1018 
1019 	/* That a DCB change has happened is now determined */
1020 	mutex_lock(&pf->tc_mutex);
1021 
1022 	/* store the old configuration */
1023 	tmp_dcbx_cfg = pi->qos_cfg.local_dcbx_cfg;
1024 
1025 	/* Reset the old DCBX configuration data */
1026 	memset(&pi->qos_cfg.local_dcbx_cfg, 0,
1027 	       sizeof(pi->qos_cfg.local_dcbx_cfg));
1028 
1029 	/* Get updated DCBX data from firmware */
1030 	if (!pending_handled) {
1031 		ice_get_dcb_cfg_from_mib_change(pi, event);
1032 	} else {
1033 		ret = ice_get_dcb_cfg(pi);
1034 		if (ret) {
1035 			dev_err(dev, "Failed to get DCB config\n");
1036 			goto out;
1037 		}
1038 	}
1039 
1040 	/* No change detected in DCBX configs */
1041 	if (!memcmp(&tmp_dcbx_cfg, &pi->qos_cfg.local_dcbx_cfg,
1042 		    sizeof(tmp_dcbx_cfg))) {
1043 		dev_dbg(dev, "No change detected in DCBX configuration.\n");
1044 		goto out;
1045 	}
1046 
1047 	pf->dcbx_cap = ice_dcb_get_mode(pi, false);
1048 
1049 	need_reconfig = ice_dcb_need_recfg(pf, &tmp_dcbx_cfg,
1050 					   &pi->qos_cfg.local_dcbx_cfg);
1051 	ice_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &pi->qos_cfg.local_dcbx_cfg);
1052 	if (!need_reconfig)
1053 		goto out;
1054 
1055 	/* Enable DCB tagging only when more than one TC */
1056 	if (ice_dcb_get_num_tc(&pi->qos_cfg.local_dcbx_cfg) > 1) {
1057 		dev_dbg(dev, "DCB tagging enabled (num TC > 1)\n");
1058 		set_bit(ICE_FLAG_DCB_ENA, pf->flags);
1059 	} else {
1060 		dev_dbg(dev, "DCB tagging disabled (num TC = 1)\n");
1061 		clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
1062 	}
1063 
1064 	/* Send Execute Pending MIB Change event if it is a Pending event */
1065 	if (!pending_handled) {
1066 		ice_lldp_execute_pending_mib(&pf->hw);
1067 		pending_handled = true;
1068 	}
1069 
1070 	rtnl_lock();
1071 	/* disable VSIs affected by DCB changes */
1072 	ice_dcb_ena_dis_vsi(pf, false, true);
1073 
1074 	ret = ice_query_port_ets(pi, &buf, sizeof(buf), NULL);
1075 	if (ret) {
1076 		dev_err(dev, "Query Port ETS failed\n");
1077 		goto unlock_rtnl;
1078 	}
1079 
1080 	/* changes in configuration update VSI */
1081 	ice_pf_dcb_recfg(pf, false);
1082 
1083 	/* enable previously downed VSIs */
1084 	ice_dcb_ena_dis_vsi(pf, true, true);
1085 unlock_rtnl:
1086 	rtnl_unlock();
1087 out:
1088 	mutex_unlock(&pf->tc_mutex);
1089 
1090 	/* Send Execute Pending MIB Change event if it is a Pending event */
1091 	if (!pending_handled)
1092 		ice_lldp_execute_pending_mib(&pf->hw);
1093 }
1094