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