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