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