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