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