net/ice/base: whitelist register for NVM access
[dpdk.git] / drivers / net / ice / base / ice_dcb.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2001-2019
3  */
4
5 #include "ice_common.h"
6 #include "ice_sched.h"
7 #include "ice_dcb.h"
8
9 /**
10  * ice_aq_get_lldp_mib
11  * @hw: pointer to the HW struct
12  * @bridge_type: type of bridge requested
13  * @mib_type: Local, Remote or both Local and Remote MIBs
14  * @buf: pointer to the caller-supplied buffer to store the MIB block
15  * @buf_size: size of the buffer (in bytes)
16  * @local_len: length of the returned Local LLDP MIB
17  * @remote_len: length of the returned Remote LLDP MIB
18  * @cd: pointer to command details structure or NULL
19  *
20  * Requests the complete LLDP MIB (entire packet). (0x0A00)
21  */
22 enum ice_status
23 ice_aq_get_lldp_mib(struct ice_hw *hw, u8 bridge_type, u8 mib_type, void *buf,
24                     u16 buf_size, u16 *local_len, u16 *remote_len,
25                     struct ice_sq_cd *cd)
26 {
27         struct ice_aqc_lldp_get_mib *cmd;
28         struct ice_aq_desc desc;
29         enum ice_status status;
30
31         cmd = &desc.params.lldp_get_mib;
32
33         if (buf_size == 0 || !buf)
34                 return ICE_ERR_PARAM;
35
36         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_lldp_get_mib);
37
38         cmd->type = mib_type & ICE_AQ_LLDP_MIB_TYPE_M;
39         cmd->type |= (bridge_type << ICE_AQ_LLDP_BRID_TYPE_S) &
40                 ICE_AQ_LLDP_BRID_TYPE_M;
41
42         desc.datalen = CPU_TO_LE16(buf_size);
43
44         status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
45         if (!status) {
46                 if (local_len)
47                         *local_len = LE16_TO_CPU(cmd->local_len);
48                 if (remote_len)
49                         *remote_len = LE16_TO_CPU(cmd->remote_len);
50         }
51
52         return status;
53 }
54
55 /**
56  * ice_aq_cfg_lldp_mib_change
57  * @hw: pointer to the HW struct
58  * @ena_update: Enable or Disable event posting
59  * @cd: pointer to command details structure or NULL
60  *
61  * Enable or Disable posting of an event on ARQ when LLDP MIB
62  * associated with the interface changes (0x0A01)
63  */
64 enum ice_status
65 ice_aq_cfg_lldp_mib_change(struct ice_hw *hw, bool ena_update,
66                            struct ice_sq_cd *cd)
67 {
68         struct ice_aqc_lldp_set_mib_change *cmd;
69         struct ice_aq_desc desc;
70
71         cmd = &desc.params.lldp_set_event;
72
73         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_lldp_set_mib_change);
74
75         if (!ena_update)
76                 cmd->command |= ICE_AQ_LLDP_MIB_UPDATE_DIS;
77
78         return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
79 }
80
81 /**
82  * ice_aq_stop_lldp
83  * @hw: pointer to the HW struct
84  * @shutdown_lldp_agent: True if LLDP Agent needs to be Shutdown
85  *                       False if LLDP Agent needs to be Stopped
86  * @persist: True if Stop/Shutdown of LLDP Agent needs to be persistent across
87  *           reboots
88  * @cd: pointer to command details structure or NULL
89  *
90  * Stop or Shutdown the embedded LLDP Agent (0x0A05)
91  */
92 enum ice_status
93 ice_aq_stop_lldp(struct ice_hw *hw, bool shutdown_lldp_agent, bool persist,
94                  struct ice_sq_cd *cd)
95 {
96         struct ice_aqc_lldp_stop *cmd;
97         struct ice_aq_desc desc;
98
99         cmd = &desc.params.lldp_stop;
100
101         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_lldp_stop);
102
103         if (shutdown_lldp_agent)
104                 cmd->command |= ICE_AQ_LLDP_AGENT_SHUTDOWN;
105
106         if (persist)
107                 cmd->command |= ICE_AQ_LLDP_AGENT_PERSIST_DIS;
108
109         return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
110 }
111
112 /**
113  * ice_aq_start_lldp
114  * @hw: pointer to the HW struct
115  * @persist: True if Start of LLDP Agent needs to be persistent across reboots
116  * @cd: pointer to command details structure or NULL
117  *
118  * Start the embedded LLDP Agent on all ports. (0x0A06)
119  */
120 enum ice_status
121 ice_aq_start_lldp(struct ice_hw *hw, bool persist, struct ice_sq_cd *cd)
122 {
123         struct ice_aqc_lldp_start *cmd;
124         struct ice_aq_desc desc;
125
126         cmd = &desc.params.lldp_start;
127
128         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_lldp_start);
129
130         cmd->command = ICE_AQ_LLDP_AGENT_START;
131
132         if (persist)
133                 cmd->command |= ICE_AQ_LLDP_AGENT_PERSIST_ENA;
134
135         return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
136 }
137
138 /**
139  * ice_aq_set_lldp_mib - Set the LLDP MIB
140  * @hw: pointer to the HW struct
141  * @mib_type: Local, Remote or both Local and Remote MIBs
142  * @buf: pointer to the caller-supplied buffer to store the MIB block
143  * @buf_size: size of the buffer (in bytes)
144  * @cd: pointer to command details structure or NULL
145  *
146  * Set the LLDP MIB. (0x0A08)
147  */
148 enum ice_status
149 ice_aq_set_lldp_mib(struct ice_hw *hw, u8 mib_type, void *buf, u16 buf_size,
150                     struct ice_sq_cd *cd)
151 {
152         struct ice_aqc_lldp_set_local_mib *cmd;
153         struct ice_aq_desc desc;
154
155         cmd = &desc.params.lldp_set_mib;
156
157         if (buf_size == 0 || !buf)
158                 return ICE_ERR_PARAM;
159
160         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_lldp_set_local_mib);
161
162         desc.flags |= CPU_TO_LE16((u16)ICE_AQ_FLAG_RD);
163         desc.datalen = CPU_TO_LE16(buf_size);
164
165         cmd->type = mib_type;
166         cmd->length = CPU_TO_LE16(buf_size);
167
168         return ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
169 }
170
171 /**
172  * ice_get_dcbx_status
173  * @hw: pointer to the HW struct
174  *
175  * Get the DCBX status from the Firmware
176  */
177 u8 ice_get_dcbx_status(struct ice_hw *hw)
178 {
179         u32 reg;
180
181         reg = rd32(hw, PRTDCB_GENS);
182         return (u8)((reg & PRTDCB_GENS_DCBX_STATUS_M) >>
183                     PRTDCB_GENS_DCBX_STATUS_S);
184 }
185
186 /**
187  * ice_parse_ieee_ets_common_tlv
188  * @buf: Data buffer to be parsed for ETS CFG/REC data
189  * @ets_cfg: Container to store parsed data
190  *
191  * Parses the common data of IEEE 802.1Qaz ETS CFG/REC TLV
192  */
193 static void
194 ice_parse_ieee_ets_common_tlv(u8 *buf, struct ice_dcb_ets_cfg *ets_cfg)
195 {
196         u8 offset = 0;
197         int i;
198
199         /* Priority Assignment Table (4 octets)
200          * Octets:|    1    |    2    |    3    |    4    |
201          *        -----------------------------------------
202          *        |pri0|pri1|pri2|pri3|pri4|pri5|pri6|pri7|
203          *        -----------------------------------------
204          *   Bits:|7  4|3  0|7  4|3  0|7  4|3  0|7  4|3  0|
205          *        -----------------------------------------
206          */
207         for (i = 0; i < 4; i++) {
208                 ets_cfg->prio_table[i * 2] =
209                         ((buf[offset] & ICE_IEEE_ETS_PRIO_1_M) >>
210                          ICE_IEEE_ETS_PRIO_1_S);
211                 ets_cfg->prio_table[i * 2 + 1] =
212                         ((buf[offset] & ICE_IEEE_ETS_PRIO_0_M) >>
213                          ICE_IEEE_ETS_PRIO_0_S);
214                 offset++;
215         }
216
217         /* TC Bandwidth Table (8 octets)
218          * Octets:| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
219          *        ---------------------------------
220          *        |tc0|tc1|tc2|tc3|tc4|tc5|tc6|tc7|
221          *        ---------------------------------
222          *
223          * TSA Assignment Table (8 octets)
224          * Octets:| 9 | 10| 11| 12| 13| 14| 15| 16|
225          *        ---------------------------------
226          *        |tc0|tc1|tc2|tc3|tc4|tc5|tc6|tc7|
227          *        ---------------------------------
228          */
229         ice_for_each_traffic_class(i) {
230                 ets_cfg->tcbwtable[i] = buf[offset];
231                 ets_cfg->tsatable[i] = buf[ICE_MAX_TRAFFIC_CLASS + offset++];
232         }
233 }
234
235 /**
236  * ice_parse_ieee_etscfg_tlv
237  * @tlv: IEEE 802.1Qaz ETS CFG TLV
238  * @dcbcfg: Local store to update ETS CFG data
239  *
240  * Parses IEEE 802.1Qaz ETS CFG TLV
241  */
242 static void
243 ice_parse_ieee_etscfg_tlv(struct ice_lldp_org_tlv *tlv,
244                           struct ice_dcbx_cfg *dcbcfg)
245 {
246         struct ice_dcb_ets_cfg *etscfg;
247         u8 *buf = tlv->tlvinfo;
248
249         /* First Octet post subtype
250          * --------------------------
251          * |will-|CBS  | Re-  | Max |
252          * |ing  |     |served| TCs |
253          * --------------------------
254          * |1bit | 1bit|3 bits|3bits|
255          */
256         etscfg = &dcbcfg->etscfg;
257         etscfg->willing = ((buf[0] & ICE_IEEE_ETS_WILLING_M) >>
258                            ICE_IEEE_ETS_WILLING_S);
259         etscfg->cbs = ((buf[0] & ICE_IEEE_ETS_CBS_M) >> ICE_IEEE_ETS_CBS_S);
260         etscfg->maxtcs = ((buf[0] & ICE_IEEE_ETS_MAXTC_M) >>
261                           ICE_IEEE_ETS_MAXTC_S);
262
263         /* Begin parsing at Priority Assignment Table (offset 1 in buf) */
264         ice_parse_ieee_ets_common_tlv(&buf[1], etscfg);
265 }
266
267 /**
268  * ice_parse_ieee_etsrec_tlv
269  * @tlv: IEEE 802.1Qaz ETS REC TLV
270  * @dcbcfg: Local store to update ETS REC data
271  *
272  * Parses IEEE 802.1Qaz ETS REC TLV
273  */
274 static void
275 ice_parse_ieee_etsrec_tlv(struct ice_lldp_org_tlv *tlv,
276                           struct ice_dcbx_cfg *dcbcfg)
277 {
278         u8 *buf = tlv->tlvinfo;
279
280         /* Begin parsing at Priority Assignment Table (offset 1 in buf) */
281         ice_parse_ieee_ets_common_tlv(&buf[1], &dcbcfg->etsrec);
282 }
283
284 /**
285  * ice_parse_ieee_pfccfg_tlv
286  * @tlv: IEEE 802.1Qaz PFC CFG TLV
287  * @dcbcfg: Local store to update PFC CFG data
288  *
289  * Parses IEEE 802.1Qaz PFC CFG TLV
290  */
291 static void
292 ice_parse_ieee_pfccfg_tlv(struct ice_lldp_org_tlv *tlv,
293                           struct ice_dcbx_cfg *dcbcfg)
294 {
295         u8 *buf = tlv->tlvinfo;
296
297         /* ----------------------------------------
298          * |will-|MBC  | Re-  | PFC |  PFC Enable  |
299          * |ing  |     |served| cap |              |
300          * -----------------------------------------
301          * |1bit | 1bit|2 bits|4bits| 1 octet      |
302          */
303         dcbcfg->pfc.willing = ((buf[0] & ICE_IEEE_PFC_WILLING_M) >>
304                                ICE_IEEE_PFC_WILLING_S);
305         dcbcfg->pfc.mbc = ((buf[0] & ICE_IEEE_PFC_MBC_M) >> ICE_IEEE_PFC_MBC_S);
306         dcbcfg->pfc.pfccap = ((buf[0] & ICE_IEEE_PFC_CAP_M) >>
307                               ICE_IEEE_PFC_CAP_S);
308         dcbcfg->pfc.pfcena = buf[1];
309 }
310
311 /**
312  * ice_parse_ieee_app_tlv
313  * @tlv: IEEE 802.1Qaz APP TLV
314  * @dcbcfg: Local store to update APP PRIO data
315  *
316  * Parses IEEE 802.1Qaz APP PRIO TLV
317  */
318 static void
319 ice_parse_ieee_app_tlv(struct ice_lldp_org_tlv *tlv,
320                        struct ice_dcbx_cfg *dcbcfg)
321 {
322         u16 offset = 0;
323         u16 typelen;
324         int i = 0;
325         u16 len;
326         u8 *buf;
327
328         typelen = NTOHS(tlv->typelen);
329         len = ((typelen & ICE_LLDP_TLV_LEN_M) >> ICE_LLDP_TLV_LEN_S);
330         buf = tlv->tlvinfo;
331
332         /* Removing sizeof(ouisubtype) and reserved byte from len.
333          * Remaining len div 3 is number of APP TLVs.
334          */
335         len -= (sizeof(tlv->ouisubtype) + 1);
336
337         /* Move offset to App Priority Table */
338         offset++;
339
340         /* Application Priority Table (3 octets)
341          * Octets:|         1          |    2    |    3    |
342          *        -----------------------------------------
343          *        |Priority|Rsrvd| Sel |    Protocol ID    |
344          *        -----------------------------------------
345          *   Bits:|23    21|20 19|18 16|15                0|
346          *        -----------------------------------------
347          */
348         while (offset < len) {
349                 dcbcfg->app[i].priority = ((buf[offset] &
350                                             ICE_IEEE_APP_PRIO_M) >>
351                                            ICE_IEEE_APP_PRIO_S);
352                 dcbcfg->app[i].selector = ((buf[offset] &
353                                             ICE_IEEE_APP_SEL_M) >>
354                                            ICE_IEEE_APP_SEL_S);
355                 dcbcfg->app[i].prot_id = (buf[offset + 1] << 0x8) |
356                         buf[offset + 2];
357                 /* Move to next app */
358                 offset += 3;
359                 i++;
360                 if (i >= ICE_DCBX_MAX_APPS)
361                         break;
362         }
363
364         dcbcfg->numapps = i;
365 }
366
367 /**
368  * ice_parse_ieee_tlv
369  * @tlv: IEEE 802.1Qaz TLV
370  * @dcbcfg: Local store to update ETS REC data
371  *
372  * Get the TLV subtype and send it to parsing function
373  * based on the subtype value
374  */
375 static void
376 ice_parse_ieee_tlv(struct ice_lldp_org_tlv *tlv, struct ice_dcbx_cfg *dcbcfg)
377 {
378         u32 ouisubtype;
379         u8 subtype;
380
381         ouisubtype = NTOHL(tlv->ouisubtype);
382         subtype = (u8)((ouisubtype & ICE_LLDP_TLV_SUBTYPE_M) >>
383                        ICE_LLDP_TLV_SUBTYPE_S);
384         switch (subtype) {
385         case ICE_IEEE_SUBTYPE_ETS_CFG:
386                 ice_parse_ieee_etscfg_tlv(tlv, dcbcfg);
387                 break;
388         case ICE_IEEE_SUBTYPE_ETS_REC:
389                 ice_parse_ieee_etsrec_tlv(tlv, dcbcfg);
390                 break;
391         case ICE_IEEE_SUBTYPE_PFC_CFG:
392                 ice_parse_ieee_pfccfg_tlv(tlv, dcbcfg);
393                 break;
394         case ICE_IEEE_SUBTYPE_APP_PRI:
395                 ice_parse_ieee_app_tlv(tlv, dcbcfg);
396                 break;
397         default:
398                 break;
399         }
400 }
401
402 /**
403  * ice_parse_cee_pgcfg_tlv
404  * @tlv: CEE DCBX PG CFG TLV
405  * @dcbcfg: Local store to update ETS CFG data
406  *
407  * Parses CEE DCBX PG CFG TLV
408  */
409 static void
410 ice_parse_cee_pgcfg_tlv(struct ice_cee_feat_tlv *tlv,
411                         struct ice_dcbx_cfg *dcbcfg)
412 {
413         struct ice_dcb_ets_cfg *etscfg;
414         u8 *buf = tlv->tlvinfo;
415         u16 offset = 0;
416         int i;
417
418         etscfg = &dcbcfg->etscfg;
419
420         if (tlv->en_will_err & ICE_CEE_FEAT_TLV_WILLING_M)
421                 etscfg->willing = 1;
422
423         etscfg->cbs = 0;
424         /* Priority Group Table (4 octets)
425          * Octets:|    1    |    2    |    3    |    4    |
426          *        -----------------------------------------
427          *        |pri0|pri1|pri2|pri3|pri4|pri5|pri6|pri7|
428          *        -----------------------------------------
429          *   Bits:|7  4|3  0|7  4|3  0|7  4|3  0|7  4|3  0|
430          *        -----------------------------------------
431          */
432         for (i = 0; i < 4; i++) {
433                 etscfg->prio_table[i * 2] =
434                         ((buf[offset] & ICE_CEE_PGID_PRIO_1_M) >>
435                          ICE_CEE_PGID_PRIO_1_S);
436                 etscfg->prio_table[i * 2 + 1] =
437                         ((buf[offset] & ICE_CEE_PGID_PRIO_0_M) >>
438                          ICE_CEE_PGID_PRIO_0_S);
439                 offset++;
440         }
441
442         /* PG Percentage Table (8 octets)
443          * Octets:| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
444          *        ---------------------------------
445          *        |pg0|pg1|pg2|pg3|pg4|pg5|pg6|pg7|
446          *        ---------------------------------
447          */
448         ice_for_each_traffic_class(i) {
449                 etscfg->tcbwtable[i] = buf[offset++];
450
451                 if (etscfg->prio_table[i] == ICE_CEE_PGID_STRICT)
452                         dcbcfg->etscfg.tsatable[i] = ICE_IEEE_TSA_STRICT;
453                 else
454                         dcbcfg->etscfg.tsatable[i] = ICE_IEEE_TSA_ETS;
455         }
456
457         /* Number of TCs supported (1 octet) */
458         etscfg->maxtcs = buf[offset];
459 }
460
461 /**
462  * ice_parse_cee_pfccfg_tlv
463  * @tlv: CEE DCBX PFC CFG TLV
464  * @dcbcfg: Local store to update PFC CFG data
465  *
466  * Parses CEE DCBX PFC CFG TLV
467  */
468 static void
469 ice_parse_cee_pfccfg_tlv(struct ice_cee_feat_tlv *tlv,
470                          struct ice_dcbx_cfg *dcbcfg)
471 {
472         u8 *buf = tlv->tlvinfo;
473
474         if (tlv->en_will_err & ICE_CEE_FEAT_TLV_WILLING_M)
475                 dcbcfg->pfc.willing = 1;
476
477         /* ------------------------
478          * | PFC Enable | PFC TCs |
479          * ------------------------
480          * | 1 octet    | 1 octet |
481          */
482         dcbcfg->pfc.pfcena = buf[0];
483         dcbcfg->pfc.pfccap = buf[1];
484 }
485
486 /**
487  * ice_parse_cee_app_tlv
488  * @tlv: CEE DCBX APP TLV
489  * @dcbcfg: Local store to update APP PRIO data
490  *
491  * Parses CEE DCBX APP PRIO TLV
492  */
493 static void
494 ice_parse_cee_app_tlv(struct ice_cee_feat_tlv *tlv, struct ice_dcbx_cfg *dcbcfg)
495 {
496         u16 len, typelen, offset = 0;
497         struct ice_cee_app_prio *app;
498         u8 i;
499
500         typelen = NTOHS(tlv->hdr.typelen);
501         len = ((typelen & ICE_LLDP_TLV_LEN_M) >> ICE_LLDP_TLV_LEN_S);
502
503         dcbcfg->numapps = len / sizeof(*app);
504         if (!dcbcfg->numapps)
505                 return;
506         if (dcbcfg->numapps > ICE_DCBX_MAX_APPS)
507                 dcbcfg->numapps = ICE_DCBX_MAX_APPS;
508
509         for (i = 0; i < dcbcfg->numapps; i++) {
510                 u8 up, selector;
511
512                 app = (struct ice_cee_app_prio *)(tlv->tlvinfo + offset);
513                 for (up = 0; up < ICE_MAX_USER_PRIORITY; up++)
514                         if (app->prio_map & BIT(up))
515                                 break;
516
517                 dcbcfg->app[i].priority = up;
518
519                 /* Get Selector from lower 2 bits, and convert to IEEE */
520                 selector = (app->upper_oui_sel & ICE_CEE_APP_SELECTOR_M);
521                 switch (selector) {
522                 case ICE_CEE_APP_SEL_ETHTYPE:
523                         dcbcfg->app[i].selector = ICE_APP_SEL_ETHTYPE;
524                         break;
525                 case ICE_CEE_APP_SEL_TCPIP:
526                         dcbcfg->app[i].selector = ICE_APP_SEL_TCPIP;
527                         break;
528                 default:
529                         /* Keep selector as it is for unknown types */
530                         dcbcfg->app[i].selector = selector;
531                 }
532
533                 dcbcfg->app[i].prot_id = NTOHS(app->protocol);
534                 /* Move to next app */
535                 offset += sizeof(*app);
536         }
537 }
538
539 /**
540  * ice_parse_cee_tlv
541  * @tlv: CEE DCBX TLV
542  * @dcbcfg: Local store to update DCBX config data
543  *
544  * Get the TLV subtype and send it to parsing function
545  * based on the subtype value
546  */
547 static void
548 ice_parse_cee_tlv(struct ice_lldp_org_tlv *tlv, struct ice_dcbx_cfg *dcbcfg)
549 {
550         struct ice_cee_feat_tlv *sub_tlv;
551         u8 subtype, feat_tlv_count = 0;
552         u16 len, tlvlen, typelen;
553         u32 ouisubtype;
554
555         ouisubtype = NTOHL(tlv->ouisubtype);
556         subtype = (u8)((ouisubtype & ICE_LLDP_TLV_SUBTYPE_M) >>
557                        ICE_LLDP_TLV_SUBTYPE_S);
558         /* Return if not CEE DCBX */
559         if (subtype != ICE_CEE_DCBX_TYPE)
560                 return;
561
562         typelen = NTOHS(tlv->typelen);
563         tlvlen = ((typelen & ICE_LLDP_TLV_LEN_M) >> ICE_LLDP_TLV_LEN_S);
564         len = sizeof(tlv->typelen) + sizeof(ouisubtype) +
565                 sizeof(struct ice_cee_ctrl_tlv);
566         /* Return if no CEE DCBX Feature TLVs */
567         if (tlvlen <= len)
568                 return;
569
570         sub_tlv = (struct ice_cee_feat_tlv *)((char *)tlv + len);
571         while (feat_tlv_count < ICE_CEE_MAX_FEAT_TYPE) {
572                 u16 sublen;
573
574                 typelen = NTOHS(sub_tlv->hdr.typelen);
575                 sublen = ((typelen & ICE_LLDP_TLV_LEN_M) >> ICE_LLDP_TLV_LEN_S);
576                 subtype = (u8)((typelen & ICE_LLDP_TLV_TYPE_M) >>
577                                ICE_LLDP_TLV_TYPE_S);
578                 switch (subtype) {
579                 case ICE_CEE_SUBTYPE_PG_CFG:
580                         ice_parse_cee_pgcfg_tlv(sub_tlv, dcbcfg);
581                         break;
582                 case ICE_CEE_SUBTYPE_PFC_CFG:
583                         ice_parse_cee_pfccfg_tlv(sub_tlv, dcbcfg);
584                         break;
585                 case ICE_CEE_SUBTYPE_APP_PRI:
586                         ice_parse_cee_app_tlv(sub_tlv, dcbcfg);
587                         break;
588                 default:
589                         return; /* Invalid Sub-type return */
590                 }
591                 feat_tlv_count++;
592                 /* Move to next sub TLV */
593                 sub_tlv = (struct ice_cee_feat_tlv *)
594                           ((char *)sub_tlv + sizeof(sub_tlv->hdr.typelen) +
595                            sublen);
596         }
597 }
598
599 /**
600  * ice_parse_org_tlv
601  * @tlv: Organization specific TLV
602  * @dcbcfg: Local store to update ETS REC data
603  *
604  * Currently only IEEE 802.1Qaz TLV is supported, all others
605  * will be returned
606  */
607 static void
608 ice_parse_org_tlv(struct ice_lldp_org_tlv *tlv, struct ice_dcbx_cfg *dcbcfg)
609 {
610         u32 ouisubtype;
611         u32 oui;
612
613         ouisubtype = NTOHL(tlv->ouisubtype);
614         oui = ((ouisubtype & ICE_LLDP_TLV_OUI_M) >> ICE_LLDP_TLV_OUI_S);
615         switch (oui) {
616         case ICE_IEEE_8021QAZ_OUI:
617                 ice_parse_ieee_tlv(tlv, dcbcfg);
618                 break;
619         case ICE_CEE_DCBX_OUI:
620                 ice_parse_cee_tlv(tlv, dcbcfg);
621                 break;
622         default:
623                 break;
624         }
625 }
626
627 /**
628  * ice_lldp_to_dcb_cfg
629  * @lldpmib: LLDPDU to be parsed
630  * @dcbcfg: store for LLDPDU data
631  *
632  * Parse DCB configuration from the LLDPDU
633  */
634 enum ice_status
635 ice_lldp_to_dcb_cfg(u8 *lldpmib, struct ice_dcbx_cfg *dcbcfg)
636 {
637         struct ice_lldp_org_tlv *tlv;
638         enum ice_status ret = ICE_SUCCESS;
639         u16 offset = 0;
640         u16 typelen;
641         u16 type;
642         u16 len;
643
644         if (!lldpmib || !dcbcfg)
645                 return ICE_ERR_PARAM;
646
647         /* set to the start of LLDPDU */
648         lldpmib += ETH_HEADER_LEN;
649         tlv = (struct ice_lldp_org_tlv *)lldpmib;
650         while (1) {
651                 typelen = NTOHS(tlv->typelen);
652                 type = ((typelen & ICE_LLDP_TLV_TYPE_M) >> ICE_LLDP_TLV_TYPE_S);
653                 len = ((typelen & ICE_LLDP_TLV_LEN_M) >> ICE_LLDP_TLV_LEN_S);
654                 offset += sizeof(typelen) + len;
655
656                 /* END TLV or beyond LLDPDU size */
657                 if (type == ICE_TLV_TYPE_END || offset > ICE_LLDPDU_SIZE)
658                         break;
659
660                 switch (type) {
661                 case ICE_TLV_TYPE_ORG:
662                         ice_parse_org_tlv(tlv, dcbcfg);
663                         break;
664                 default:
665                         break;
666                 }
667
668                 /* Move to next TLV */
669                 tlv = (struct ice_lldp_org_tlv *)
670                       ((char *)tlv + sizeof(tlv->typelen) + len);
671         }
672
673         return ret;
674 }
675
676 /**
677  * ice_aq_get_dcb_cfg
678  * @hw: pointer to the HW struct
679  * @mib_type: MIB type for the query
680  * @bridgetype: bridge type for the query (remote)
681  * @dcbcfg: store for LLDPDU data
682  *
683  * Query DCB configuration from the firmware
684  */
685 enum ice_status
686 ice_aq_get_dcb_cfg(struct ice_hw *hw, u8 mib_type, u8 bridgetype,
687                    struct ice_dcbx_cfg *dcbcfg)
688 {
689         enum ice_status ret;
690         u8 *lldpmib;
691
692         /* Allocate the LLDPDU */
693         lldpmib = (u8 *)ice_malloc(hw, ICE_LLDPDU_SIZE);
694         if (!lldpmib)
695                 return ICE_ERR_NO_MEMORY;
696
697         ret = ice_aq_get_lldp_mib(hw, bridgetype, mib_type, (void *)lldpmib,
698                                   ICE_LLDPDU_SIZE, NULL, NULL, NULL);
699
700         if (ret == ICE_SUCCESS)
701                 /* Parse LLDP MIB to get DCB configuration */
702                 ret = ice_lldp_to_dcb_cfg(lldpmib, dcbcfg);
703
704         ice_free(hw, lldpmib);
705
706         return ret;
707 }
708
709 /**
710  * ice_aq_start_stop_dcbx - Start/Stop DCBX service in FW
711  * @hw: pointer to the HW struct
712  * @start_dcbx_agent: True if DCBX Agent needs to be started
713  *                    False if DCBX Agent needs to be stopped
714  * @dcbx_agent_status: FW indicates back the DCBX agent status
715  *                     True if DCBX Agent is active
716  *                     False if DCBX Agent is stopped
717  * @cd: pointer to command details structure or NULL
718  *
719  * Start/Stop the embedded dcbx Agent. In case that this wrapper function
720  * returns ICE_SUCCESS, caller will need to check if FW returns back the same
721  * value as stated in dcbx_agent_status, and react accordingly. (0x0A09)
722  */
723 enum ice_status
724 ice_aq_start_stop_dcbx(struct ice_hw *hw, bool start_dcbx_agent,
725                        bool *dcbx_agent_status, struct ice_sq_cd *cd)
726 {
727         struct ice_aqc_lldp_stop_start_specific_agent *cmd;
728         enum ice_status status;
729         struct ice_aq_desc desc;
730         u16 opcode;
731
732         cmd = &desc.params.lldp_agent_ctrl;
733
734         opcode = ice_aqc_opc_lldp_stop_start_specific_agent;
735
736         ice_fill_dflt_direct_cmd_desc(&desc, opcode);
737
738         if (start_dcbx_agent)
739                 cmd->command = ICE_AQC_START_STOP_AGENT_START_DCBX;
740
741         status = ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
742
743         *dcbx_agent_status = false;
744
745         if (status == ICE_SUCCESS &&
746             cmd->command == ICE_AQC_START_STOP_AGENT_START_DCBX)
747                 *dcbx_agent_status = true;
748
749         return status;
750 }
751
752 /**
753  * ice_aq_get_cee_dcb_cfg
754  * @hw: pointer to the HW struct
755  * @buff: response buffer that stores CEE operational configuration
756  * @cd: pointer to command details structure or NULL
757  *
758  * Get CEE DCBX mode operational configuration from firmware (0x0A07)
759  */
760 enum ice_status
761 ice_aq_get_cee_dcb_cfg(struct ice_hw *hw,
762                        struct ice_aqc_get_cee_dcb_cfg_resp *buff,
763                        struct ice_sq_cd *cd)
764 {
765         struct ice_aq_desc desc;
766
767         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_cee_dcb_cfg);
768
769         return ice_aq_send_cmd(hw, &desc, (void *)buff, sizeof(*buff), cd);
770 }
771
772 /**
773  * ice_cee_to_dcb_cfg
774  * @cee_cfg: pointer to CEE configuration struct
775  * @dcbcfg: DCB configuration struct
776  *
777  * Convert CEE configuration from firmware to DCB configuration
778  */
779 static void
780 ice_cee_to_dcb_cfg(struct ice_aqc_get_cee_dcb_cfg_resp *cee_cfg,
781                    struct ice_dcbx_cfg *dcbcfg)
782 {
783         u32 status, tlv_status = LE32_TO_CPU(cee_cfg->tlv_status);
784         u32 ice_aqc_cee_status_mask, ice_aqc_cee_status_shift;
785         u16 app_prio = LE16_TO_CPU(cee_cfg->oper_app_prio);
786         u8 i, err, sync, oper, app_index, ice_app_sel_type;
787         u16 ice_aqc_cee_app_mask, ice_aqc_cee_app_shift;
788         u16 ice_app_prot_id_type;
789
790         /* CEE PG data to ETS config */
791         dcbcfg->etscfg.maxtcs = cee_cfg->oper_num_tc;
792
793         /* Note that the FW creates the oper_prio_tc nibbles reversed
794          * from those in the CEE Priority Group sub-TLV.
795          */
796         for (i = 0; i < ICE_MAX_TRAFFIC_CLASS / 2; i++) {
797                 dcbcfg->etscfg.prio_table[i * 2] =
798                         ((cee_cfg->oper_prio_tc[i] & ICE_CEE_PGID_PRIO_0_M) >>
799                          ICE_CEE_PGID_PRIO_0_S);
800                 dcbcfg->etscfg.prio_table[i * 2 + 1] =
801                         ((cee_cfg->oper_prio_tc[i] & ICE_CEE_PGID_PRIO_1_M) >>
802                          ICE_CEE_PGID_PRIO_1_S);
803         }
804
805         ice_for_each_traffic_class(i) {
806                 dcbcfg->etscfg.tcbwtable[i] = cee_cfg->oper_tc_bw[i];
807
808                 if (dcbcfg->etscfg.prio_table[i] == ICE_CEE_PGID_STRICT) {
809                         /* Map it to next empty TC */
810                         dcbcfg->etscfg.prio_table[i] = cee_cfg->oper_num_tc - 1;
811                         dcbcfg->etscfg.tsatable[i] = ICE_IEEE_TSA_STRICT;
812                 } else {
813                         dcbcfg->etscfg.tsatable[i] = ICE_IEEE_TSA_ETS;
814                 }
815         }
816
817         /* CEE PFC data to ETS config */
818         dcbcfg->pfc.pfcena = cee_cfg->oper_pfc_en;
819         dcbcfg->pfc.pfccap = ICE_MAX_TRAFFIC_CLASS;
820
821         app_index = 0;
822         for (i = 0; i < 3; i++) {
823                 if (i == 0) {
824                         /* FCoE APP */
825                         ice_aqc_cee_status_mask = ICE_AQC_CEE_FCOE_STATUS_M;
826                         ice_aqc_cee_status_shift = ICE_AQC_CEE_FCOE_STATUS_S;
827                         ice_aqc_cee_app_mask = ICE_AQC_CEE_APP_FCOE_M;
828                         ice_aqc_cee_app_shift = ICE_AQC_CEE_APP_FCOE_S;
829                         ice_app_sel_type = ICE_APP_SEL_ETHTYPE;
830                         ice_app_prot_id_type = ICE_APP_PROT_ID_FCOE;
831                 } else if (i == 1) {
832                         /* iSCSI APP */
833                         ice_aqc_cee_status_mask = ICE_AQC_CEE_ISCSI_STATUS_M;
834                         ice_aqc_cee_status_shift = ICE_AQC_CEE_ISCSI_STATUS_S;
835                         ice_aqc_cee_app_mask = ICE_AQC_CEE_APP_ISCSI_M;
836                         ice_aqc_cee_app_shift = ICE_AQC_CEE_APP_ISCSI_S;
837                         ice_app_sel_type = ICE_APP_SEL_TCPIP;
838                         ice_app_prot_id_type = ICE_APP_PROT_ID_ISCSI;
839                 } else {
840                         /* FIP APP */
841                         ice_aqc_cee_status_mask = ICE_AQC_CEE_FIP_STATUS_M;
842                         ice_aqc_cee_status_shift = ICE_AQC_CEE_FIP_STATUS_S;
843                         ice_aqc_cee_app_mask = ICE_AQC_CEE_APP_FIP_M;
844                         ice_aqc_cee_app_shift = ICE_AQC_CEE_APP_FIP_S;
845                         ice_app_sel_type = ICE_APP_SEL_ETHTYPE;
846                         ice_app_prot_id_type = ICE_APP_PROT_ID_FIP;
847                 }
848
849                 status = (tlv_status & ice_aqc_cee_status_mask) >>
850                          ice_aqc_cee_status_shift;
851                 err = (status & ICE_TLV_STATUS_ERR) ? 1 : 0;
852                 sync = (status & ICE_TLV_STATUS_SYNC) ? 1 : 0;
853                 oper = (status & ICE_TLV_STATUS_OPER) ? 1 : 0;
854                 /* Add FCoE/iSCSI/FIP APP if Error is False and
855                  * Oper/Sync is True
856                  */
857                 if (!err && sync && oper) {
858                         dcbcfg->app[app_index].priority =
859                                 (app_prio & ice_aqc_cee_app_mask) >>
860                                 ice_aqc_cee_app_shift;
861                         dcbcfg->app[app_index].selector = ice_app_sel_type;
862                         dcbcfg->app[app_index].prot_id = ice_app_prot_id_type;
863                         app_index++;
864                 }
865         }
866
867         dcbcfg->numapps = app_index;
868 }
869
870 /**
871  * ice_get_ieee_dcb_cfg
872  * @pi: port information structure
873  * @dcbx_mode: mode of DCBX (IEEE or CEE)
874  *
875  * Get IEEE or CEE mode DCB configuration from the Firmware
876  */
877 STATIC enum ice_status
878 ice_get_ieee_or_cee_dcb_cfg(struct ice_port_info *pi, u8 dcbx_mode)
879 {
880         struct ice_dcbx_cfg *dcbx_cfg = NULL;
881         enum ice_status ret;
882
883         if (!pi)
884                 return ICE_ERR_PARAM;
885
886         if (dcbx_mode == ICE_DCBX_MODE_IEEE)
887                 dcbx_cfg = &pi->local_dcbx_cfg;
888         else if (dcbx_mode == ICE_DCBX_MODE_CEE)
889                 dcbx_cfg = &pi->desired_dcbx_cfg;
890
891         /* Get Local DCB Config in case of ICE_DCBX_MODE_IEEE
892          * or get CEE DCB Desired Config in case of ICE_DCBX_MODE_CEE
893          */
894         ret = ice_aq_get_dcb_cfg(pi->hw, ICE_AQ_LLDP_MIB_LOCAL,
895                                  ICE_AQ_LLDP_BRID_TYPE_NEAREST_BRID, dcbx_cfg);
896         if (ret)
897                 goto out;
898
899         /* Get Remote DCB Config */
900         dcbx_cfg = &pi->remote_dcbx_cfg;
901         ret = ice_aq_get_dcb_cfg(pi->hw, ICE_AQ_LLDP_MIB_REMOTE,
902                                  ICE_AQ_LLDP_BRID_TYPE_NEAREST_BRID, dcbx_cfg);
903         /* Don't treat ENOENT as an error for Remote MIBs */
904         if (pi->hw->adminq.sq_last_status == ICE_AQ_RC_ENOENT)
905                 ret = ICE_SUCCESS;
906
907 out:
908         return ret;
909 }
910
911 /**
912  * ice_get_dcb_cfg
913  * @pi: port information structure
914  *
915  * Get DCB configuration from the Firmware
916  */
917 enum ice_status ice_get_dcb_cfg(struct ice_port_info *pi)
918 {
919         struct ice_aqc_get_cee_dcb_cfg_resp cee_cfg;
920         struct ice_dcbx_cfg *dcbx_cfg;
921         enum ice_status ret;
922
923         if (!pi)
924                 return ICE_ERR_PARAM;
925
926         ret = ice_aq_get_cee_dcb_cfg(pi->hw, &cee_cfg, NULL);
927         if (ret == ICE_SUCCESS) {
928                 /* CEE mode */
929                 dcbx_cfg = &pi->local_dcbx_cfg;
930                 dcbx_cfg->dcbx_mode = ICE_DCBX_MODE_CEE;
931                 dcbx_cfg->tlv_status = LE32_TO_CPU(cee_cfg.tlv_status);
932                 ice_cee_to_dcb_cfg(&cee_cfg, dcbx_cfg);
933                 ret = ice_get_ieee_or_cee_dcb_cfg(pi, ICE_DCBX_MODE_CEE);
934         } else if (pi->hw->adminq.sq_last_status == ICE_AQ_RC_ENOENT) {
935                 /* CEE mode not enabled try querying IEEE data */
936                 dcbx_cfg = &pi->local_dcbx_cfg;
937                 dcbx_cfg->dcbx_mode = ICE_DCBX_MODE_IEEE;
938                 ret = ice_get_ieee_or_cee_dcb_cfg(pi, ICE_DCBX_MODE_IEEE);
939         }
940
941         return ret;
942 }
943
944 /**
945  * ice_init_dcb
946  * @hw: pointer to the HW struct
947  * @enable_mib_change: enable MIB change event
948  *
949  * Update DCB configuration from the Firmware
950  */
951 enum ice_status ice_init_dcb(struct ice_hw *hw, bool enable_mib_change)
952 {
953         struct ice_port_info *pi = hw->port_info;
954         enum ice_status ret = ICE_SUCCESS;
955
956         if (!hw->func_caps.common_cap.dcb)
957                 return ICE_ERR_NOT_SUPPORTED;
958
959         pi->is_sw_lldp = true;
960
961         /* Get DCBX status */
962         pi->dcbx_status = ice_get_dcbx_status(hw);
963
964         if (pi->dcbx_status == ICE_DCBX_STATUS_DONE ||
965             pi->dcbx_status == ICE_DCBX_STATUS_IN_PROGRESS ||
966             pi->dcbx_status == ICE_DCBX_STATUS_NOT_STARTED) {
967                 /* Get current DCBX configuration */
968                 ret = ice_get_dcb_cfg(pi);
969                 if (ret)
970                         return ret;
971                 pi->is_sw_lldp = false;
972         } else if (pi->dcbx_status == ICE_DCBX_STATUS_DIS) {
973                 return ICE_ERR_NOT_READY;
974         }
975
976         /* Configure the LLDP MIB change event */
977         if (enable_mib_change) {
978                 ret = ice_aq_cfg_lldp_mib_change(hw, true, NULL);
979                 if (ret)
980                         pi->is_sw_lldp = true;
981         }
982
983         return ret;
984 }
985
986 /**
987  * ice_cfg_lldp_mib_change
988  * @hw: pointer to the HW struct
989  * @ena_mib: enable/disable MIB change event
990  *
991  * Configure (disable/enable) MIB
992  */
993 enum ice_status ice_cfg_lldp_mib_change(struct ice_hw *hw, bool ena_mib)
994 {
995         struct ice_port_info *pi = hw->port_info;
996         enum ice_status ret;
997
998         if (!hw->func_caps.common_cap.dcb)
999                 return ICE_ERR_NOT_SUPPORTED;
1000
1001         /* Get DCBX status */
1002         pi->dcbx_status = ice_get_dcbx_status(hw);
1003
1004         if (pi->dcbx_status == ICE_DCBX_STATUS_DIS)
1005                 return ICE_ERR_NOT_READY;
1006
1007         ret = ice_aq_cfg_lldp_mib_change(hw, ena_mib, NULL);
1008         if (!ret)
1009                 pi->is_sw_lldp = !ena_mib;
1010
1011         return ret;
1012 }
1013
1014 /**
1015  * ice_add_ieee_ets_common_tlv
1016  * @buf: Data buffer to be populated with ice_dcb_ets_cfg data
1017  * @ets_cfg: Container for ice_dcb_ets_cfg data
1018  *
1019  * Populate the TLV buffer with ice_dcb_ets_cfg data
1020  */
1021 static void
1022 ice_add_ieee_ets_common_tlv(u8 *buf, struct ice_dcb_ets_cfg *ets_cfg)
1023 {
1024         u8 priority0, priority1;
1025         u8 offset = 0;
1026         int i;
1027
1028         /* Priority Assignment Table (4 octets)
1029          * Octets:|    1    |    2    |    3    |    4    |
1030          *        -----------------------------------------
1031          *        |pri0|pri1|pri2|pri3|pri4|pri5|pri6|pri7|
1032          *        -----------------------------------------
1033          *   Bits:|7  4|3  0|7  4|3  0|7  4|3  0|7  4|3  0|
1034          *        -----------------------------------------
1035          */
1036         for (i = 0; i < ICE_MAX_TRAFFIC_CLASS / 2; i++) {
1037                 priority0 = ets_cfg->prio_table[i * 2] & 0xF;
1038                 priority1 = ets_cfg->prio_table[i * 2 + 1] & 0xF;
1039                 buf[offset] = (priority0 << ICE_IEEE_ETS_PRIO_1_S) | priority1;
1040                 offset++;
1041         }
1042
1043         /* TC Bandwidth Table (8 octets)
1044          * Octets:| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
1045          *        ---------------------------------
1046          *        |tc0|tc1|tc2|tc3|tc4|tc5|tc6|tc7|
1047          *        ---------------------------------
1048          *
1049          * TSA Assignment Table (8 octets)
1050          * Octets:| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
1051          *        ---------------------------------
1052          *        |tc0|tc1|tc2|tc3|tc4|tc5|tc6|tc7|
1053          *        ---------------------------------
1054          */
1055         ice_for_each_traffic_class(i) {
1056                 buf[offset] = ets_cfg->tcbwtable[i];
1057                 buf[ICE_MAX_TRAFFIC_CLASS + offset] = ets_cfg->tsatable[i];
1058                 offset++;
1059         }
1060 }
1061
1062 /**
1063  * ice_add_ieee_ets_tlv - Prepare ETS TLV in IEEE format
1064  * @tlv: Fill the ETS config data in IEEE format
1065  * @dcbcfg: Local store which holds the DCB Config
1066  *
1067  * Prepare IEEE 802.1Qaz ETS CFG TLV
1068  */
1069 static void
1070 ice_add_ieee_ets_tlv(struct ice_lldp_org_tlv *tlv, struct ice_dcbx_cfg *dcbcfg)
1071 {
1072         struct ice_dcb_ets_cfg *etscfg;
1073         u8 *buf = tlv->tlvinfo;
1074         u8 maxtcwilling = 0;
1075         u32 ouisubtype;
1076         u16 typelen;
1077
1078         typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
1079                    ICE_IEEE_ETS_TLV_LEN);
1080         tlv->typelen = HTONS(typelen);
1081
1082         ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
1083                       ICE_IEEE_SUBTYPE_ETS_CFG);
1084         tlv->ouisubtype = HTONL(ouisubtype);
1085
1086         /* First Octet post subtype
1087          * --------------------------
1088          * |will-|CBS  | Re-  | Max |
1089          * |ing  |     |served| TCs |
1090          * --------------------------
1091          * |1bit | 1bit|3 bits|3bits|
1092          */
1093         etscfg = &dcbcfg->etscfg;
1094         if (etscfg->willing)
1095                 maxtcwilling = BIT(ICE_IEEE_ETS_WILLING_S);
1096         maxtcwilling |= etscfg->maxtcs & ICE_IEEE_ETS_MAXTC_M;
1097         buf[0] = maxtcwilling;
1098
1099         /* Begin adding at Priority Assignment Table (offset 1 in buf) */
1100         ice_add_ieee_ets_common_tlv(&buf[1], etscfg);
1101 }
1102
1103 /**
1104  * ice_add_ieee_etsrec_tlv - Prepare ETS Recommended TLV in IEEE format
1105  * @tlv: Fill ETS Recommended TLV in IEEE format
1106  * @dcbcfg: Local store which holds the DCB Config
1107  *
1108  * Prepare IEEE 802.1Qaz ETS REC TLV
1109  */
1110 static void
1111 ice_add_ieee_etsrec_tlv(struct ice_lldp_org_tlv *tlv,
1112                         struct ice_dcbx_cfg *dcbcfg)
1113 {
1114         struct ice_dcb_ets_cfg *etsrec;
1115         u8 *buf = tlv->tlvinfo;
1116         u32 ouisubtype;
1117         u16 typelen;
1118
1119         typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
1120                    ICE_IEEE_ETS_TLV_LEN);
1121         tlv->typelen = HTONS(typelen);
1122
1123         ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
1124                       ICE_IEEE_SUBTYPE_ETS_REC);
1125         tlv->ouisubtype = HTONL(ouisubtype);
1126
1127         etsrec = &dcbcfg->etsrec;
1128
1129         /* First Octet is reserved */
1130         /* Begin adding at Priority Assignment Table (offset 1 in buf) */
1131         ice_add_ieee_ets_common_tlv(&buf[1], etsrec);
1132 }
1133
1134 /**
1135  * ice_add_ieee_pfc_tlv - Prepare PFC TLV in IEEE format
1136  * @tlv: Fill PFC TLV in IEEE format
1137  * @dcbcfg: Local store which holds the PFC CFG data
1138  *
1139  * Prepare IEEE 802.1Qaz PFC CFG TLV
1140  */
1141 static void
1142 ice_add_ieee_pfc_tlv(struct ice_lldp_org_tlv *tlv, struct ice_dcbx_cfg *dcbcfg)
1143 {
1144         u8 *buf = tlv->tlvinfo;
1145         u32 ouisubtype;
1146         u16 typelen;
1147
1148         typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
1149                    ICE_IEEE_PFC_TLV_LEN);
1150         tlv->typelen = HTONS(typelen);
1151
1152         ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
1153                       ICE_IEEE_SUBTYPE_PFC_CFG);
1154         tlv->ouisubtype = HTONL(ouisubtype);
1155
1156         /* ----------------------------------------
1157          * |will-|MBC  | Re-  | PFC |  PFC Enable  |
1158          * |ing  |     |served| cap |              |
1159          * -----------------------------------------
1160          * |1bit | 1bit|2 bits|4bits| 1 octet      |
1161          */
1162         if (dcbcfg->pfc.willing)
1163                 buf[0] = BIT(ICE_IEEE_PFC_WILLING_S);
1164
1165         if (dcbcfg->pfc.mbc)
1166                 buf[0] |= BIT(ICE_IEEE_PFC_MBC_S);
1167
1168         buf[0] |= dcbcfg->pfc.pfccap & 0xF;
1169         buf[1] = dcbcfg->pfc.pfcena;
1170 }
1171
1172 /**
1173  * ice_add_ieee_app_pri_tlv -  Prepare APP TLV in IEEE format
1174  * @tlv: Fill APP TLV in IEEE format
1175  * @dcbcfg: Local store which holds the APP CFG data
1176  *
1177  * Prepare IEEE 802.1Qaz APP CFG TLV
1178  */
1179 static void
1180 ice_add_ieee_app_pri_tlv(struct ice_lldp_org_tlv *tlv,
1181                          struct ice_dcbx_cfg *dcbcfg)
1182 {
1183         u16 typelen, len, offset = 0;
1184         u8 priority, selector, i = 0;
1185         u8 *buf = tlv->tlvinfo;
1186         u32 ouisubtype;
1187
1188         /* No APP TLVs then just return */
1189         if (dcbcfg->numapps == 0)
1190                 return;
1191         ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
1192                       ICE_IEEE_SUBTYPE_APP_PRI);
1193         tlv->ouisubtype = HTONL(ouisubtype);
1194
1195         /* Move offset to App Priority Table */
1196         offset++;
1197         /* Application Priority Table (3 octets)
1198          * Octets:|         1          |    2    |    3    |
1199          *        -----------------------------------------
1200          *        |Priority|Rsrvd| Sel |    Protocol ID    |
1201          *        -----------------------------------------
1202          *   Bits:|23    21|20 19|18 16|15                0|
1203          *        -----------------------------------------
1204          */
1205         while (i < dcbcfg->numapps) {
1206                 priority = dcbcfg->app[i].priority & 0x7;
1207                 selector = dcbcfg->app[i].selector & 0x7;
1208                 buf[offset] = (priority << ICE_IEEE_APP_PRIO_S) | selector;
1209                 buf[offset + 1] = (dcbcfg->app[i].prot_id >> 0x8) & 0xFF;
1210                 buf[offset + 2] = dcbcfg->app[i].prot_id & 0xFF;
1211                 /* Move to next app */
1212                 offset += 3;
1213                 i++;
1214                 if (i >= ICE_DCBX_MAX_APPS)
1215                         break;
1216         }
1217         /* len includes size of ouisubtype + 1 reserved + 3*numapps */
1218         len = sizeof(tlv->ouisubtype) + 1 + (i * 3);
1219         typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) | (len & 0x1FF));
1220         tlv->typelen = HTONS(typelen);
1221 }
1222
1223 /**
1224  * ice_add_dcb_tlv - Add all IEEE TLVs
1225  * @tlv: Fill TLV data in IEEE format
1226  * @dcbcfg: Local store which holds the DCB Config
1227  * @tlvid: Type of IEEE TLV
1228  *
1229  * Add tlv information
1230  */
1231 static void
1232 ice_add_dcb_tlv(struct ice_lldp_org_tlv *tlv, struct ice_dcbx_cfg *dcbcfg,
1233                 u16 tlvid)
1234 {
1235         switch (tlvid) {
1236         case ICE_IEEE_TLV_ID_ETS_CFG:
1237                 ice_add_ieee_ets_tlv(tlv, dcbcfg);
1238                 break;
1239         case ICE_IEEE_TLV_ID_ETS_REC:
1240                 ice_add_ieee_etsrec_tlv(tlv, dcbcfg);
1241                 break;
1242         case ICE_IEEE_TLV_ID_PFC_CFG:
1243                 ice_add_ieee_pfc_tlv(tlv, dcbcfg);
1244                 break;
1245         case ICE_IEEE_TLV_ID_APP_PRI:
1246                 ice_add_ieee_app_pri_tlv(tlv, dcbcfg);
1247                 break;
1248         default:
1249                 break;
1250         }
1251 }
1252
1253 /**
1254  * ice_dcb_cfg_to_lldp - Convert DCB configuration to MIB format
1255  * @lldpmib: pointer to the HW struct
1256  * @miblen: length of LLDP MIB
1257  * @dcbcfg: Local store which holds the DCB Config
1258  *
1259  * Convert the DCB configuration to MIB format
1260  */
1261 void ice_dcb_cfg_to_lldp(u8 *lldpmib, u16 *miblen, struct ice_dcbx_cfg *dcbcfg)
1262 {
1263         u16 len, offset = 0, tlvid = ICE_TLV_ID_START;
1264         struct ice_lldp_org_tlv *tlv;
1265         u16 typelen;
1266
1267         tlv = (struct ice_lldp_org_tlv *)lldpmib;
1268         while (1) {
1269                 ice_add_dcb_tlv(tlv, dcbcfg, tlvid++);
1270                 typelen = NTOHS(tlv->typelen);
1271                 len = (typelen & ICE_LLDP_TLV_LEN_M) >> ICE_LLDP_TLV_LEN_S;
1272                 if (len)
1273                         offset += len + 2;
1274                 /* END TLV or beyond LLDPDU size */
1275                 if (tlvid >= ICE_TLV_ID_END_OF_LLDPPDU ||
1276                     offset > ICE_LLDPDU_SIZE)
1277                         break;
1278                 /* Move to next TLV */
1279                 if (len)
1280                         tlv = (struct ice_lldp_org_tlv *)
1281                                 ((char *)tlv + sizeof(tlv->typelen) + len);
1282         }
1283         *miblen = offset;
1284 }
1285
1286 /**
1287  * ice_set_dcb_cfg - Set the local LLDP MIB to FW
1288  * @pi: port information structure
1289  *
1290  * Set DCB configuration to the Firmware
1291  */
1292 enum ice_status ice_set_dcb_cfg(struct ice_port_info *pi)
1293 {
1294         u8 mib_type, *lldpmib = NULL;
1295         struct ice_dcbx_cfg *dcbcfg;
1296         enum ice_status ret;
1297         struct ice_hw *hw;
1298         u16 miblen;
1299
1300         if (!pi)
1301                 return ICE_ERR_PARAM;
1302
1303         hw = pi->hw;
1304
1305         /* update the HW local config */
1306         dcbcfg = &pi->local_dcbx_cfg;
1307         /* Allocate the LLDPDU */
1308         lldpmib = (u8 *)ice_malloc(hw, ICE_LLDPDU_SIZE);
1309         if (!lldpmib)
1310                 return ICE_ERR_NO_MEMORY;
1311
1312         mib_type = SET_LOCAL_MIB_TYPE_LOCAL_MIB;
1313         if (dcbcfg->app_mode == ICE_DCBX_APPS_NON_WILLING)
1314                 mib_type |= SET_LOCAL_MIB_TYPE_CEE_NON_WILLING;
1315
1316         ice_dcb_cfg_to_lldp(lldpmib, &miblen, dcbcfg);
1317         ret = ice_aq_set_lldp_mib(hw, mib_type, (void *)lldpmib, miblen,
1318                                   NULL);
1319
1320         ice_free(hw, lldpmib);
1321
1322         return ret;
1323 }
1324
1325 /**
1326  * ice_aq_query_port_ets - query port ets configuration
1327  * @pi: port information structure
1328  * @buf: pointer to buffer
1329  * @buf_size: buffer size in bytes
1330  * @cd: pointer to command details structure or NULL
1331  *
1332  * query current port ets configuration
1333  */
1334 enum ice_status
1335 ice_aq_query_port_ets(struct ice_port_info *pi,
1336                       struct ice_aqc_port_ets_elem *buf, u16 buf_size,
1337                       struct ice_sq_cd *cd)
1338 {
1339         struct ice_aqc_query_port_ets *cmd;
1340         struct ice_aq_desc desc;
1341         enum ice_status status;
1342
1343         if (!pi)
1344                 return ICE_ERR_PARAM;
1345         cmd = &desc.params.port_ets;
1346         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_query_port_ets);
1347         cmd->port_teid = pi->root->info.node_teid;
1348
1349         status = ice_aq_send_cmd(pi->hw, &desc, buf, buf_size, cd);
1350         return status;
1351 }
1352
1353 /**
1354  * ice_update_port_tc_tree_cfg - update TC tree configuration
1355  * @pi: port information structure
1356  * @buf: pointer to buffer
1357  *
1358  * update the SW DB with the new TC changes
1359  */
1360 enum ice_status
1361 ice_update_port_tc_tree_cfg(struct ice_port_info *pi,
1362                             struct ice_aqc_port_ets_elem *buf)
1363 {
1364         struct ice_sched_node *node, *tc_node;
1365         struct ice_aqc_get_elem elem;
1366         enum ice_status status = ICE_SUCCESS;
1367         u32 teid1, teid2;
1368         u8 i, j;
1369
1370         if (!pi)
1371                 return ICE_ERR_PARAM;
1372         /* suspend the missing TC nodes */
1373         for (i = 0; i < pi->root->num_children; i++) {
1374                 teid1 = LE32_TO_CPU(pi->root->children[i]->info.node_teid);
1375                 ice_for_each_traffic_class(j) {
1376                         teid2 = LE32_TO_CPU(buf->tc_node_teid[j]);
1377                         if (teid1 == teid2)
1378                                 break;
1379                 }
1380                 if (j < ICE_MAX_TRAFFIC_CLASS)
1381                         continue;
1382                 /* TC is missing */
1383                 pi->root->children[i]->in_use = false;
1384         }
1385         /* add the new TC nodes */
1386         ice_for_each_traffic_class(j) {
1387                 teid2 = LE32_TO_CPU(buf->tc_node_teid[j]);
1388                 if (teid2 == ICE_INVAL_TEID)
1389                         continue;
1390                 /* Is it already present in the tree ? */
1391                 for (i = 0; i < pi->root->num_children; i++) {
1392                         tc_node = pi->root->children[i];
1393                         if (!tc_node)
1394                                 continue;
1395                         teid1 = LE32_TO_CPU(tc_node->info.node_teid);
1396                         if (teid1 == teid2) {
1397                                 tc_node->tc_num = j;
1398                                 tc_node->in_use = true;
1399                                 break;
1400                         }
1401                 }
1402                 if (i < pi->root->num_children)
1403                         continue;
1404                 /* new TC */
1405                 status = ice_sched_query_elem(pi->hw, teid2, &elem);
1406                 if (!status)
1407                         status = ice_sched_add_node(pi, 1, &elem.generic[0]);
1408                 if (status)
1409                         break;
1410                 /* update the TC number */
1411                 node = ice_sched_find_node_by_teid(pi->root, teid2);
1412                 if (node)
1413                         node->tc_num = j;
1414         }
1415         return status;
1416 }
1417
1418 /**
1419  * ice_query_port_ets - query port ets configuration
1420  * @pi: port information structure
1421  * @buf: pointer to buffer
1422  * @buf_size: buffer size in bytes
1423  * @cd: pointer to command details structure or NULL
1424  *
1425  * query current port ets configuration and update the
1426  * SW DB with the TC changes
1427  */
1428 enum ice_status
1429 ice_query_port_ets(struct ice_port_info *pi,
1430                    struct ice_aqc_port_ets_elem *buf, u16 buf_size,
1431                    struct ice_sq_cd *cd)
1432 {
1433         enum ice_status status;
1434
1435         ice_acquire_lock(&pi->sched_lock);
1436         status = ice_aq_query_port_ets(pi, buf, buf_size, cd);
1437         if (!status)
1438                 status = ice_update_port_tc_tree_cfg(pi, buf);
1439         ice_release_lock(&pi->sched_lock);
1440         return status;
1441 }