]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/net/ethernet/intel/ice/ice_dcb_lib.c
x86/mce/amd: Fix kobject lifetime
[linux.git] / drivers / net / ethernet / intel / ice / ice_dcb_lib.c
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_dcbx_cfg *local_dcbx_cfg, *desired_dcbx_cfg, *prev_cfg;
319         struct ice_aqc_port_ets_elem buf = { 0 };
320         struct device *dev = ice_pf_to_dev(pf);
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         local_dcbx_cfg = &pf->hw.port_info->local_dcbx_cfg;
334         desired_dcbx_cfg = &pf->hw.port_info->desired_dcbx_cfg;
335
336         /* Save current willing state and force FW to unwilling */
337         local_dcbx_cfg->etscfg.willing = 0x0;
338         local_dcbx_cfg->pfc.willing = 0x0;
339         local_dcbx_cfg->app_mode = ICE_DCBX_APPS_NON_WILLING;
340
341         ice_cfg_etsrec_defaults(pf->hw.port_info);
342         ret = ice_set_dcb_cfg(pf->hw.port_info);
343         if (ret) {
344                 dev_err(dev, "Failed to set DCB to unwilling\n");
345                 goto dcb_error;
346         }
347
348         /* Retrieve DCB config and ensure same as current in SW */
349         prev_cfg = kmemdup(local_dcbx_cfg, sizeof(*prev_cfg), GFP_KERNEL);
350         if (!prev_cfg)
351                 goto dcb_error;
352
353         ice_init_dcb(&pf->hw, true);
354         if (pf->hw.port_info->dcbx_status == ICE_DCBX_STATUS_DIS)
355                 pf->hw.port_info->is_sw_lldp = true;
356         else
357                 pf->hw.port_info->is_sw_lldp = false;
358
359         if (ice_dcb_need_recfg(pf, prev_cfg, local_dcbx_cfg)) {
360                 /* difference in cfg detected - disable DCB till next MIB */
361                 dev_err(dev, "Set local MIB not accurate\n");
362                 kfree(prev_cfg);
363                 goto dcb_error;
364         }
365
366         /* fetched config congruent to previous configuration */
367         kfree(prev_cfg);
368
369         /* Set the local desired config */
370         if (local_dcbx_cfg->dcbx_mode == ICE_DCBX_MODE_CEE)
371                 memcpy(local_dcbx_cfg, desired_dcbx_cfg,
372                        sizeof(*local_dcbx_cfg));
373
374         ice_cfg_etsrec_defaults(pf->hw.port_info);
375         ret = ice_set_dcb_cfg(pf->hw.port_info);
376         if (ret) {
377                 dev_err(dev, "Failed to set desired config\n");
378                 goto dcb_error;
379         }
380         dev_info(dev, "DCB restored after reset\n");
381         ret = ice_query_port_ets(pf->hw.port_info, &buf, sizeof(buf), NULL);
382         if (ret) {
383                 dev_err(dev, "Query Port ETS failed\n");
384                 goto dcb_error;
385         }
386
387         return;
388
389 dcb_error:
390         dev_err(dev, "Disabling DCB until new settings occur\n");
391         prev_cfg = kzalloc(sizeof(*prev_cfg), GFP_KERNEL);
392         if (!prev_cfg)
393                 return;
394
395         prev_cfg->etscfg.willing = true;
396         prev_cfg->etscfg.tcbwtable[0] = ICE_TC_MAX_BW;
397         prev_cfg->etscfg.tsatable[0] = ICE_IEEE_TSA_ETS;
398         memcpy(&prev_cfg->etsrec, &prev_cfg->etscfg, sizeof(prev_cfg->etsrec));
399         /* Coverity warns the return code of ice_pf_dcb_cfg() is not checked
400          * here as is done for other calls to that function. That check is
401          * not necessary since this is in this function's error cleanup path.
402          * Suppress the Coverity warning with the following comment...
403          */
404         /* coverity[check_return] */
405         ice_pf_dcb_cfg(pf, prev_cfg, false);
406         kfree(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  */
414 static 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 = kmemdup(&pi->local_dcbx_cfg, sizeof(*newcfg), GFP_KERNEL);
422         if (!newcfg)
423                 return -ENOMEM;
424
425         memset(&pi->local_dcbx_cfg, 0, sizeof(*newcfg));
426
427         dev_info(ice_pf_to_dev(pf), "Configuring initial DCB values\n");
428         if (ice_pf_dcb_cfg(pf, newcfg, locked))
429                 ret = -EINVAL;
430
431         kfree(newcfg);
432
433         return ret;
434 }
435
436 /**
437  * ice_dcb_sw_default_config - Apply a default DCB config
438  * @pf: PF to apply config to
439  * @ets_willing: configure ets willing
440  * @locked: was this function called with RTNL held
441  */
442 static int ice_dcb_sw_dflt_cfg(struct ice_pf *pf, bool ets_willing, 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 = kzalloc(sizeof(*dcbcfg), GFP_KERNEL);
453         if (!dcbcfg)
454                 return -ENOMEM;
455
456         memset(&pi->local_dcbx_cfg, 0, sizeof(*dcbcfg));
457
458         dcbcfg->etscfg.willing = ets_willing ? 1 : 0;
459         dcbcfg->etscfg.maxtcs = hw->func_caps.common_cap.maxtc;
460         dcbcfg->etscfg.tcbwtable[0] = 100;
461         dcbcfg->etscfg.tsatable[0] = ICE_IEEE_TSA_ETS;
462
463         memcpy(&dcbcfg->etsrec, &dcbcfg->etscfg,
464                sizeof(dcbcfg->etsrec));
465         dcbcfg->etsrec.willing = 0;
466
467         dcbcfg->pfc.willing = 1;
468         dcbcfg->pfc.pfccap = hw->func_caps.common_cap.maxtc;
469
470         dcbcfg->numapps = 1;
471         dcbcfg->app[0].selector = ICE_APP_SEL_ETHTYPE;
472         dcbcfg->app[0].priority = 3;
473         dcbcfg->app[0].prot_id = ICE_APP_PROT_ID_FCOE;
474
475         ret = ice_pf_dcb_cfg(pf, dcbcfg, locked);
476         kfree(dcbcfg);
477         if (ret)
478                 return ret;
479
480         return ice_query_port_ets(pi, &buf, sizeof(buf), NULL);
481 }
482
483 /**
484  * ice_dcb_tc_contig - Check that TCs are contiguous
485  * @prio_table: pointer to priority table
486  *
487  * Check if TCs begin with TC0 and are contiguous
488  */
489 static bool ice_dcb_tc_contig(u8 *prio_table)
490 {
491         u8 max_tc = 0;
492         int i;
493
494         for (i = 0; i < CEE_DCBX_MAX_PRIO; i++) {
495                 u8 cur_tc = prio_table[i];
496
497                 if (cur_tc > max_tc)
498                         return false;
499                 else if (cur_tc == max_tc)
500                         max_tc++;
501         }
502
503         return true;
504 }
505
506 /**
507  * ice_dcb_noncontig_cfg - Configure DCB for non-contiguous TCs
508  * @pf: pointer to the PF struct
509  *
510  * If non-contiguous TCs, then configure SW DCB with TC0 and ETS non-willing
511  */
512 static int ice_dcb_noncontig_cfg(struct ice_pf *pf)
513 {
514         struct ice_dcbx_cfg *dcbcfg = &pf->hw.port_info->local_dcbx_cfg;
515         struct device *dev = ice_pf_to_dev(pf);
516         int ret;
517
518         /* Configure SW DCB default with ETS non-willing */
519         ret = ice_dcb_sw_dflt_cfg(pf, false, true);
520         if (ret) {
521                 dev_err(dev, "Failed to set local DCB config %d\n", ret);
522                 return ret;
523         }
524
525         /* Reconfigure with ETS willing so that FW will send LLDP MIB event */
526         dcbcfg->etscfg.willing = 1;
527         ret = ice_set_dcb_cfg(pf->hw.port_info);
528         if (ret)
529                 dev_err(dev, "Failed to set DCB to unwilling\n");
530
531         return ret;
532 }
533
534 /**
535  * ice_pf_dcb_recfg - Reconfigure all VEBs and VSIs
536  * @pf: pointer to the PF struct
537  *
538  * Assumed caller has already disabled all VSIs before
539  * calling this function. Reconfiguring DCB based on
540  * local_dcbx_cfg.
541  */
542 void ice_pf_dcb_recfg(struct ice_pf *pf)
543 {
544         struct ice_dcbx_cfg *dcbcfg = &pf->hw.port_info->local_dcbx_cfg;
545         u8 tc_map = 0;
546         int v, ret;
547
548         /* Update each VSI */
549         ice_for_each_vsi(pf, v) {
550                 struct ice_vsi *vsi = pf->vsi[v];
551
552                 if (!vsi)
553                         continue;
554
555                 if (vsi->type == ICE_VSI_PF) {
556                         tc_map = ice_dcb_get_ena_tc(dcbcfg);
557
558                         /* If DCBX request non-contiguous TC, then configure
559                          * default TC
560                          */
561                         if (!ice_dcb_tc_contig(dcbcfg->etscfg.prio_table)) {
562                                 tc_map = ICE_DFLT_TRAFFIC_CLASS;
563                                 ice_dcb_noncontig_cfg(pf);
564                         }
565                 } else {
566                         tc_map = ICE_DFLT_TRAFFIC_CLASS;
567                 }
568
569                 ret = ice_vsi_cfg_tc(vsi, tc_map);
570                 if (ret) {
571                         dev_err(ice_pf_to_dev(pf), "Failed to config TC for VSI index: %d\n",
572                                 vsi->idx);
573                         continue;
574                 }
575
576                 ice_vsi_map_rings_to_vectors(vsi);
577                 if (vsi->type == ICE_VSI_PF)
578                         ice_dcbnl_set_all(vsi);
579         }
580 }
581
582 /**
583  * ice_init_pf_dcb - initialize DCB for a PF
584  * @pf: PF to initialize DCB for
585  * @locked: Was function called with RTNL held
586  */
587 int ice_init_pf_dcb(struct ice_pf *pf, bool locked)
588 {
589         struct device *dev = ice_pf_to_dev(pf);
590         struct ice_port_info *port_info;
591         struct ice_hw *hw = &pf->hw;
592         int err;
593
594         port_info = hw->port_info;
595
596         err = ice_init_dcb(hw, false);
597         if (err && !port_info->is_sw_lldp) {
598                 dev_err(dev, "Error initializing DCB %d\n", err);
599                 goto dcb_init_err;
600         }
601
602         dev_info(dev,
603                  "DCB is enabled in the hardware, max number of TCs supported on this port are %d\n",
604                  pf->hw.func_caps.common_cap.maxtc);
605         if (err) {
606                 struct ice_vsi *pf_vsi;
607
608                 /* FW LLDP is disabled, activate SW DCBX/LLDP mode */
609                 dev_info(dev, "FW LLDP is disabled, DCBx/LLDP in SW mode.\n");
610                 clear_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags);
611                 err = ice_dcb_sw_dflt_cfg(pf, true, locked);
612                 if (err) {
613                         dev_err(dev,
614                                 "Failed to set local DCB config %d\n", err);
615                         err = -EIO;
616                         goto dcb_init_err;
617                 }
618
619                 /* If the FW DCBX engine is not running then Rx LLDP packets
620                  * need to be redirected up the stack.
621                  */
622                 pf_vsi = ice_get_main_vsi(pf);
623                 if (!pf_vsi) {
624                         dev_err(dev, "Failed to set local DCB config\n");
625                         err = -EIO;
626                         goto dcb_init_err;
627                 }
628
629                 ice_cfg_sw_lldp(pf_vsi, false, true);
630
631                 pf->dcbx_cap = DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_IEEE;
632                 return 0;
633         }
634
635         set_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags);
636
637         /* DCBX in FW and LLDP enabled in FW */
638         pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED | DCB_CAP_DCBX_VER_IEEE;
639
640         err = ice_dcb_init_cfg(pf, locked);
641         if (err)
642                 goto dcb_init_err;
643
644         return err;
645
646 dcb_init_err:
647         dev_err(dev, "DCB init failed\n");
648         return err;
649 }
650
651 /**
652  * ice_update_dcb_stats - Update DCB stats counters
653  * @pf: PF whose stats needs to be updated
654  */
655 void ice_update_dcb_stats(struct ice_pf *pf)
656 {
657         struct ice_hw_port_stats *prev_ps, *cur_ps;
658         struct ice_hw *hw = &pf->hw;
659         u8 port;
660         int i;
661
662         port = hw->port_info->lport;
663         prev_ps = &pf->stats_prev;
664         cur_ps = &pf->stats;
665
666         for (i = 0; i < 8; i++) {
667                 ice_stat_update32(hw, GLPRT_PXOFFRXC(port, i),
668                                   pf->stat_prev_loaded,
669                                   &prev_ps->priority_xoff_rx[i],
670                                   &cur_ps->priority_xoff_rx[i]);
671                 ice_stat_update32(hw, GLPRT_PXONRXC(port, i),
672                                   pf->stat_prev_loaded,
673                                   &prev_ps->priority_xon_rx[i],
674                                   &cur_ps->priority_xon_rx[i]);
675                 ice_stat_update32(hw, GLPRT_PXONTXC(port, i),
676                                   pf->stat_prev_loaded,
677                                   &prev_ps->priority_xon_tx[i],
678                                   &cur_ps->priority_xon_tx[i]);
679                 ice_stat_update32(hw, GLPRT_PXOFFTXC(port, i),
680                                   pf->stat_prev_loaded,
681                                   &prev_ps->priority_xoff_tx[i],
682                                   &cur_ps->priority_xoff_tx[i]);
683                 ice_stat_update32(hw, GLPRT_RXON2OFFCNT(port, i),
684                                   pf->stat_prev_loaded,
685                                   &prev_ps->priority_xon_2_xoff[i],
686                                   &cur_ps->priority_xon_2_xoff[i]);
687         }
688 }
689
690 /**
691  * ice_tx_prepare_vlan_flags_dcb - prepare VLAN tagging for DCB
692  * @tx_ring: ring to send buffer on
693  * @first: pointer to struct ice_tx_buf
694  */
695 int
696 ice_tx_prepare_vlan_flags_dcb(struct ice_ring *tx_ring,
697                               struct ice_tx_buf *first)
698 {
699         struct sk_buff *skb = first->skb;
700
701         if (!test_bit(ICE_FLAG_DCB_ENA, tx_ring->vsi->back->flags))
702                 return 0;
703
704         /* Insert 802.1p priority into VLAN header */
705         if ((first->tx_flags & (ICE_TX_FLAGS_HW_VLAN | ICE_TX_FLAGS_SW_VLAN)) ||
706             skb->priority != TC_PRIO_CONTROL) {
707                 first->tx_flags &= ~ICE_TX_FLAGS_VLAN_PR_M;
708                 /* Mask the lower 3 bits to set the 802.1p priority */
709                 first->tx_flags |= (skb->priority & 0x7) <<
710                                    ICE_TX_FLAGS_VLAN_PR_S;
711                 if (first->tx_flags & ICE_TX_FLAGS_SW_VLAN) {
712                         struct vlan_ethhdr *vhdr;
713                         int rc;
714
715                         rc = skb_cow_head(skb, 0);
716                         if (rc < 0)
717                                 return rc;
718                         vhdr = (struct vlan_ethhdr *)skb->data;
719                         vhdr->h_vlan_TCI = htons(first->tx_flags >>
720                                                  ICE_TX_FLAGS_VLAN_S);
721                 } else {
722                         first->tx_flags |= ICE_TX_FLAGS_HW_VLAN;
723                 }
724         }
725
726         return 0;
727 }
728
729 /**
730  * ice_dcb_process_lldp_set_mib_change - Process MIB change
731  * @pf: ptr to ice_pf
732  * @event: pointer to the admin queue receive event
733  */
734 void
735 ice_dcb_process_lldp_set_mib_change(struct ice_pf *pf,
736                                     struct ice_rq_event_info *event)
737 {
738         struct ice_aqc_port_ets_elem buf = { 0 };
739         struct device *dev = ice_pf_to_dev(pf);
740         struct ice_aqc_lldp_get_mib *mib;
741         struct ice_dcbx_cfg tmp_dcbx_cfg;
742         bool need_reconfig = false;
743         struct ice_port_info *pi;
744         struct ice_vsi *pf_vsi;
745         u8 type;
746         int ret;
747
748         /* Not DCB capable or capability disabled */
749         if (!(test_bit(ICE_FLAG_DCB_CAPABLE, pf->flags)))
750                 return;
751
752         if (pf->dcbx_cap & DCB_CAP_DCBX_HOST) {
753                 dev_dbg(dev, "MIB Change Event in HOST mode\n");
754                 return;
755         }
756
757         pi = pf->hw.port_info;
758         mib = (struct ice_aqc_lldp_get_mib *)&event->desc.params.raw;
759         /* Ignore if event is not for Nearest Bridge */
760         type = ((mib->type >> ICE_AQ_LLDP_BRID_TYPE_S) &
761                 ICE_AQ_LLDP_BRID_TYPE_M);
762         dev_dbg(dev, "LLDP event MIB bridge type 0x%x\n", type);
763         if (type != ICE_AQ_LLDP_BRID_TYPE_NEAREST_BRID)
764                 return;
765
766         /* Check MIB Type and return if event for Remote MIB update */
767         type = mib->type & ICE_AQ_LLDP_MIB_TYPE_M;
768         dev_dbg(dev, "LLDP event mib type %s\n", type ? "remote" : "local");
769         if (type == ICE_AQ_LLDP_MIB_REMOTE) {
770                 /* Update the remote cached instance and return */
771                 ret = ice_aq_get_dcb_cfg(pi->hw, ICE_AQ_LLDP_MIB_REMOTE,
772                                          ICE_AQ_LLDP_BRID_TYPE_NEAREST_BRID,
773                                          &pi->remote_dcbx_cfg);
774                 if (ret) {
775                         dev_err(dev, "Failed to get remote DCB config\n");
776                         return;
777                 }
778         }
779
780         /* store the old configuration */
781         tmp_dcbx_cfg = pf->hw.port_info->local_dcbx_cfg;
782
783         /* Reset the old DCBX configuration data */
784         memset(&pi->local_dcbx_cfg, 0, sizeof(pi->local_dcbx_cfg));
785
786         /* Get updated DCBX data from firmware */
787         ret = ice_get_dcb_cfg(pf->hw.port_info);
788         if (ret) {
789                 dev_err(dev, "Failed to get DCB config\n");
790                 return;
791         }
792
793         /* No change detected in DCBX configs */
794         if (!memcmp(&tmp_dcbx_cfg, &pi->local_dcbx_cfg, sizeof(tmp_dcbx_cfg))) {
795                 dev_dbg(dev, "No change detected in DCBX configuration.\n");
796                 return;
797         }
798
799         need_reconfig = ice_dcb_need_recfg(pf, &tmp_dcbx_cfg,
800                                            &pi->local_dcbx_cfg);
801         ice_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &pi->local_dcbx_cfg);
802         if (!need_reconfig)
803                 return;
804
805         /* Enable DCB tagging only when more than one TC */
806         if (ice_dcb_get_num_tc(&pi->local_dcbx_cfg) > 1) {
807                 dev_dbg(dev, "DCB tagging enabled (num TC > 1)\n");
808                 set_bit(ICE_FLAG_DCB_ENA, pf->flags);
809         } else {
810                 dev_dbg(dev, "DCB tagging disabled (num TC = 1)\n");
811                 clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
812         }
813
814         pf_vsi = ice_get_main_vsi(pf);
815         if (!pf_vsi) {
816                 dev_dbg(dev, "PF VSI doesn't exist\n");
817                 return;
818         }
819
820         rtnl_lock();
821         ice_dis_vsi(pf_vsi, true);
822
823         ret = ice_query_port_ets(pf->hw.port_info, &buf, sizeof(buf), NULL);
824         if (ret) {
825                 dev_err(dev, "Query Port ETS failed\n");
826                 rtnl_unlock();
827                 return;
828         }
829
830         /* changes in configuration update VSI */
831         ice_pf_dcb_recfg(pf);
832
833         ice_ena_vsi(pf_vsi, true);
834         rtnl_unlock();
835 }