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