1 /* QLogic qed NIC Driver
2  * Copyright (c) 2015 QLogic Corporation
3  *
4  * This software is available under the terms of the GNU General Public License
5  * (GPL) Version 2, available from the file COPYING in the main directory of
6  * this source tree.
7  */
8 
9 #include <linux/types.h>
10 #include <asm/byteorder.h>
11 #include <linux/bitops.h>
12 #include <linux/dcbnl.h>
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
17 #include "qed.h"
18 #include "qed_cxt.h"
19 #include "qed_dcbx.h"
20 #include "qed_hsi.h"
21 #include "qed_sp.h"
22 #ifdef CONFIG_DCB
23 #include <linux/qed/qed_eth_if.h>
24 #endif
25 
26 #define QED_DCBX_MAX_MIB_READ_TRY       (100)
27 #define QED_ETH_TYPE_DEFAULT            (0)
28 #define QED_ETH_TYPE_ROCE               (0x8915)
29 #define QED_UDP_PORT_TYPE_ROCE_V2       (0x12B7)
30 #define QED_ETH_TYPE_FCOE               (0x8906)
31 #define QED_TCP_PORT_ISCSI              (0xCBC)
32 
33 #define QED_DCBX_INVALID_PRIORITY       0xFF
34 
35 /* Get Traffic Class from priority traffic class table, 4 bits represent
36  * the traffic class corresponding to the priority.
37  */
38 #define QED_DCBX_PRIO2TC(prio_tc_tbl, prio) \
39 	((u32)(prio_tc_tbl >> ((7 - prio) * 4)) & 0x7)
40 
41 static const struct qed_dcbx_app_metadata qed_dcbx_app_update[] = {
42 	{DCBX_PROTOCOL_ISCSI, "ISCSI", QED_PCI_DEFAULT},
43 	{DCBX_PROTOCOL_FCOE, "FCOE", QED_PCI_DEFAULT},
44 	{DCBX_PROTOCOL_ROCE, "ROCE", QED_PCI_DEFAULT},
45 	{DCBX_PROTOCOL_ROCE_V2, "ROCE_V2", QED_PCI_DEFAULT},
46 	{DCBX_PROTOCOL_ETH, "ETH", QED_PCI_ETH}
47 };
48 
49 static bool qed_dcbx_app_ethtype(u32 app_info_bitmap)
50 {
51 	return !!(QED_MFW_GET_FIELD(app_info_bitmap, DCBX_APP_SF) ==
52 		  DCBX_APP_SF_ETHTYPE);
53 }
54 
55 static bool qed_dcbx_ieee_app_ethtype(u32 app_info_bitmap)
56 {
57 	u8 mfw_val = QED_MFW_GET_FIELD(app_info_bitmap, DCBX_APP_SF_IEEE);
58 
59 	/* Old MFW */
60 	if (mfw_val == DCBX_APP_SF_IEEE_RESERVED)
61 		return qed_dcbx_app_ethtype(app_info_bitmap);
62 
63 	return !!(mfw_val == DCBX_APP_SF_IEEE_ETHTYPE);
64 }
65 
66 static bool qed_dcbx_app_port(u32 app_info_bitmap)
67 {
68 	return !!(QED_MFW_GET_FIELD(app_info_bitmap, DCBX_APP_SF) ==
69 		  DCBX_APP_SF_PORT);
70 }
71 
72 static bool qed_dcbx_ieee_app_port(u32 app_info_bitmap, u8 type)
73 {
74 	u8 mfw_val = QED_MFW_GET_FIELD(app_info_bitmap, DCBX_APP_SF_IEEE);
75 
76 	/* Old MFW */
77 	if (mfw_val == DCBX_APP_SF_IEEE_RESERVED)
78 		return qed_dcbx_app_port(app_info_bitmap);
79 
80 	return !!(mfw_val == type || mfw_val == DCBX_APP_SF_IEEE_TCP_UDP_PORT);
81 }
82 
83 static bool qed_dcbx_default_tlv(u32 app_info_bitmap, u16 proto_id, bool ieee)
84 {
85 	bool ethtype;
86 
87 	if (ieee)
88 		ethtype = qed_dcbx_ieee_app_ethtype(app_info_bitmap);
89 	else
90 		ethtype = qed_dcbx_app_ethtype(app_info_bitmap);
91 
92 	return !!(ethtype && (proto_id == QED_ETH_TYPE_DEFAULT));
93 }
94 
95 static bool qed_dcbx_iscsi_tlv(u32 app_info_bitmap, u16 proto_id, bool ieee)
96 {
97 	bool port;
98 
99 	if (ieee)
100 		port = qed_dcbx_ieee_app_port(app_info_bitmap,
101 					      DCBX_APP_SF_IEEE_TCP_PORT);
102 	else
103 		port = qed_dcbx_app_port(app_info_bitmap);
104 
105 	return !!(port && (proto_id == QED_TCP_PORT_ISCSI));
106 }
107 
108 static bool qed_dcbx_fcoe_tlv(u32 app_info_bitmap, u16 proto_id, bool ieee)
109 {
110 	bool ethtype;
111 
112 	if (ieee)
113 		ethtype = qed_dcbx_ieee_app_ethtype(app_info_bitmap);
114 	else
115 		ethtype = qed_dcbx_app_ethtype(app_info_bitmap);
116 
117 	return !!(ethtype && (proto_id == QED_ETH_TYPE_FCOE));
118 }
119 
120 static bool qed_dcbx_roce_tlv(u32 app_info_bitmap, u16 proto_id, bool ieee)
121 {
122 	bool ethtype;
123 
124 	if (ieee)
125 		ethtype = qed_dcbx_ieee_app_ethtype(app_info_bitmap);
126 	else
127 		ethtype = qed_dcbx_app_ethtype(app_info_bitmap);
128 
129 	return !!(ethtype && (proto_id == QED_ETH_TYPE_ROCE));
130 }
131 
132 static bool qed_dcbx_roce_v2_tlv(u32 app_info_bitmap, u16 proto_id, bool ieee)
133 {
134 	bool port;
135 
136 	if (ieee)
137 		port = qed_dcbx_ieee_app_port(app_info_bitmap,
138 					      DCBX_APP_SF_IEEE_UDP_PORT);
139 	else
140 		port = qed_dcbx_app_port(app_info_bitmap);
141 
142 	return !!(port && (proto_id == QED_UDP_PORT_TYPE_ROCE_V2));
143 }
144 
145 static void
146 qed_dcbx_dp_protocol(struct qed_hwfn *p_hwfn, struct qed_dcbx_results *p_data)
147 {
148 	enum dcbx_protocol_type id;
149 	int i;
150 
151 	DP_VERBOSE(p_hwfn, QED_MSG_DCB, "DCBX negotiated: %d\n",
152 		   p_data->dcbx_enabled);
153 
154 	for (i = 0; i < ARRAY_SIZE(qed_dcbx_app_update); i++) {
155 		id = qed_dcbx_app_update[i].id;
156 
157 		DP_VERBOSE(p_hwfn, QED_MSG_DCB,
158 			   "%s info: update %d, enable %d, prio %d, tc %d, num_tc %d\n",
159 			   qed_dcbx_app_update[i].name, p_data->arr[id].update,
160 			   p_data->arr[id].enable, p_data->arr[id].priority,
161 			   p_data->arr[id].tc, p_hwfn->hw_info.num_tc);
162 	}
163 }
164 
165 static void
166 qed_dcbx_set_params(struct qed_dcbx_results *p_data,
167 		    struct qed_hw_info *p_info,
168 		    bool enable,
169 		    bool update,
170 		    u8 prio,
171 		    u8 tc,
172 		    enum dcbx_protocol_type type,
173 		    enum qed_pci_personality personality)
174 {
175 	/* PF update ramrod data */
176 	p_data->arr[type].update = update;
177 	p_data->arr[type].enable = enable;
178 	p_data->arr[type].priority = prio;
179 	p_data->arr[type].tc = tc;
180 
181 	/* QM reconf data */
182 	if (p_info->personality == personality) {
183 		if (personality == QED_PCI_ETH)
184 			p_info->non_offload_tc = tc;
185 		else
186 			p_info->offload_tc = tc;
187 	}
188 }
189 
190 /* Update app protocol data and hw_info fields with the TLV info */
191 static void
192 qed_dcbx_update_app_info(struct qed_dcbx_results *p_data,
193 			 struct qed_hwfn *p_hwfn,
194 			 bool enable,
195 			 bool update,
196 			 u8 prio, u8 tc, enum dcbx_protocol_type type)
197 {
198 	struct qed_hw_info *p_info = &p_hwfn->hw_info;
199 	enum qed_pci_personality personality;
200 	enum dcbx_protocol_type id;
201 	char *name;
202 	int i;
203 
204 	for (i = 0; i < ARRAY_SIZE(qed_dcbx_app_update); i++) {
205 		id = qed_dcbx_app_update[i].id;
206 
207 		if (type != id)
208 			continue;
209 
210 		personality = qed_dcbx_app_update[i].personality;
211 		name = qed_dcbx_app_update[i].name;
212 
213 		qed_dcbx_set_params(p_data, p_info, enable, update,
214 				    prio, tc, type, personality);
215 	}
216 }
217 
218 static bool
219 qed_dcbx_get_app_protocol_type(struct qed_hwfn *p_hwfn,
220 			       u32 app_prio_bitmap,
221 			       u16 id, enum dcbx_protocol_type *type, bool ieee)
222 {
223 	if (qed_dcbx_fcoe_tlv(app_prio_bitmap, id, ieee)) {
224 		*type = DCBX_PROTOCOL_FCOE;
225 	} else if (qed_dcbx_roce_tlv(app_prio_bitmap, id, ieee)) {
226 		*type = DCBX_PROTOCOL_ROCE;
227 	} else if (qed_dcbx_iscsi_tlv(app_prio_bitmap, id, ieee)) {
228 		*type = DCBX_PROTOCOL_ISCSI;
229 	} else if (qed_dcbx_default_tlv(app_prio_bitmap, id, ieee)) {
230 		*type = DCBX_PROTOCOL_ETH;
231 	} else if (qed_dcbx_roce_v2_tlv(app_prio_bitmap, id, ieee)) {
232 		*type = DCBX_PROTOCOL_ROCE_V2;
233 	} else {
234 		*type = DCBX_MAX_PROTOCOL_TYPE;
235 		DP_ERR(p_hwfn,
236 		       "No action required, App TLV id = 0x%x app_prio_bitmap = 0x%x\n",
237 		       id, app_prio_bitmap);
238 		return false;
239 	}
240 
241 	return true;
242 }
243 
244 /* Parse app TLV's to update TC information in hw_info structure for
245  * reconfiguring QM. Get protocol specific data for PF update ramrod command.
246  */
247 static int
248 qed_dcbx_process_tlv(struct qed_hwfn *p_hwfn,
249 		     struct qed_dcbx_results *p_data,
250 		     struct dcbx_app_priority_entry *p_tbl,
251 		     u32 pri_tc_tbl, int count, u8 dcbx_version)
252 {
253 	u8 tc, priority_map;
254 	enum dcbx_protocol_type type;
255 	bool enable, ieee;
256 	u16 protocol_id;
257 	int priority;
258 	int i;
259 
260 	DP_VERBOSE(p_hwfn, QED_MSG_DCB, "Num APP entries = %d\n", count);
261 
262 	ieee = (dcbx_version == DCBX_CONFIG_VERSION_IEEE);
263 	/* Parse APP TLV */
264 	for (i = 0; i < count; i++) {
265 		protocol_id = QED_MFW_GET_FIELD(p_tbl[i].entry,
266 						DCBX_APP_PROTOCOL_ID);
267 		priority_map = QED_MFW_GET_FIELD(p_tbl[i].entry,
268 						 DCBX_APP_PRI_MAP);
269 		priority = ffs(priority_map) - 1;
270 		if (priority < 0) {
271 			DP_ERR(p_hwfn, "Invalid priority\n");
272 			return -EINVAL;
273 		}
274 
275 		tc = QED_DCBX_PRIO2TC(pri_tc_tbl, priority);
276 		if (qed_dcbx_get_app_protocol_type(p_hwfn, p_tbl[i].entry,
277 						   protocol_id, &type, ieee)) {
278 			/* ETH always have the enable bit reset, as it gets
279 			 * vlan information per packet. For other protocols,
280 			 * should be set according to the dcbx_enabled
281 			 * indication, but we only got here if there was an
282 			 * app tlv for the protocol, so dcbx must be enabled.
283 			 */
284 			enable = !(type == DCBX_PROTOCOL_ETH);
285 
286 			qed_dcbx_update_app_info(p_data, p_hwfn, enable, true,
287 						 priority, tc, type);
288 		}
289 	}
290 
291 	/* If RoCE-V2 TLV is not detected, driver need to use RoCE app
292 	 * data for RoCE-v2 not the default app data.
293 	 */
294 	if (!p_data->arr[DCBX_PROTOCOL_ROCE_V2].update &&
295 	    p_data->arr[DCBX_PROTOCOL_ROCE].update) {
296 		tc = p_data->arr[DCBX_PROTOCOL_ROCE].tc;
297 		priority = p_data->arr[DCBX_PROTOCOL_ROCE].priority;
298 		qed_dcbx_update_app_info(p_data, p_hwfn, true, true,
299 					 priority, tc, DCBX_PROTOCOL_ROCE_V2);
300 	}
301 
302 	/* Update ramrod protocol data and hw_info fields
303 	 * with default info when corresponding APP TLV's are not detected.
304 	 * The enabled field has a different logic for ethernet as only for
305 	 * ethernet dcb should disabled by default, as the information arrives
306 	 * from the OS (unless an explicit app tlv was present).
307 	 */
308 	tc = p_data->arr[DCBX_PROTOCOL_ETH].tc;
309 	priority = p_data->arr[DCBX_PROTOCOL_ETH].priority;
310 	for (type = 0; type < DCBX_MAX_PROTOCOL_TYPE; type++) {
311 		if (p_data->arr[type].update)
312 			continue;
313 
314 		enable = !(type == DCBX_PROTOCOL_ETH);
315 		qed_dcbx_update_app_info(p_data, p_hwfn, enable, true,
316 					 priority, tc, type);
317 	}
318 
319 	return 0;
320 }
321 
322 /* Parse app TLV's to update TC information in hw_info structure for
323  * reconfiguring QM. Get protocol specific data for PF update ramrod command.
324  */
325 static int qed_dcbx_process_mib_info(struct qed_hwfn *p_hwfn)
326 {
327 	struct dcbx_app_priority_feature *p_app;
328 	struct dcbx_app_priority_entry *p_tbl;
329 	struct qed_dcbx_results data = { 0 };
330 	struct dcbx_ets_feature *p_ets;
331 	struct qed_hw_info *p_info;
332 	u32 pri_tc_tbl, flags;
333 	u8 dcbx_version;
334 	int num_entries;
335 	int rc = 0;
336 
337 	flags = p_hwfn->p_dcbx_info->operational.flags;
338 	dcbx_version = QED_MFW_GET_FIELD(flags, DCBX_CONFIG_VERSION);
339 
340 	p_app = &p_hwfn->p_dcbx_info->operational.features.app;
341 	p_tbl = p_app->app_pri_tbl;
342 
343 	p_ets = &p_hwfn->p_dcbx_info->operational.features.ets;
344 	pri_tc_tbl = p_ets->pri_tc_tbl[0];
345 
346 	p_info = &p_hwfn->hw_info;
347 	num_entries = QED_MFW_GET_FIELD(p_app->flags, DCBX_APP_NUM_ENTRIES);
348 
349 	rc = qed_dcbx_process_tlv(p_hwfn, &data, p_tbl, pri_tc_tbl,
350 				  num_entries, dcbx_version);
351 	if (rc)
352 		return rc;
353 
354 	p_info->num_tc = QED_MFW_GET_FIELD(p_ets->flags, DCBX_ETS_MAX_TCS);
355 	data.pf_id = p_hwfn->rel_pf_id;
356 	data.dcbx_enabled = !!dcbx_version;
357 
358 	qed_dcbx_dp_protocol(p_hwfn, &data);
359 
360 	memcpy(&p_hwfn->p_dcbx_info->results, &data,
361 	       sizeof(struct qed_dcbx_results));
362 
363 	return 0;
364 }
365 
366 static int
367 qed_dcbx_copy_mib(struct qed_hwfn *p_hwfn,
368 		  struct qed_ptt *p_ptt,
369 		  struct qed_dcbx_mib_meta_data *p_data,
370 		  enum qed_mib_read_type type)
371 {
372 	u32 prefix_seq_num, suffix_seq_num;
373 	int read_count = 0;
374 	int rc = 0;
375 
376 	/* The data is considered to be valid only if both sequence numbers are
377 	 * the same.
378 	 */
379 	do {
380 		if (type == QED_DCBX_REMOTE_LLDP_MIB) {
381 			qed_memcpy_from(p_hwfn, p_ptt, p_data->lldp_remote,
382 					p_data->addr, p_data->size);
383 			prefix_seq_num = p_data->lldp_remote->prefix_seq_num;
384 			suffix_seq_num = p_data->lldp_remote->suffix_seq_num;
385 		} else {
386 			qed_memcpy_from(p_hwfn, p_ptt, p_data->mib,
387 					p_data->addr, p_data->size);
388 			prefix_seq_num = p_data->mib->prefix_seq_num;
389 			suffix_seq_num = p_data->mib->suffix_seq_num;
390 		}
391 		read_count++;
392 
393 		DP_VERBOSE(p_hwfn,
394 			   QED_MSG_DCB,
395 			   "mib type = %d, try count = %d prefix seq num  = %d suffix seq num = %d\n",
396 			   type, read_count, prefix_seq_num, suffix_seq_num);
397 	} while ((prefix_seq_num != suffix_seq_num) &&
398 		 (read_count < QED_DCBX_MAX_MIB_READ_TRY));
399 
400 	if (read_count >= QED_DCBX_MAX_MIB_READ_TRY) {
401 		DP_ERR(p_hwfn,
402 		       "MIB read err, mib type = %d, try count = %d prefix seq num = %d suffix seq num = %d\n",
403 		       type, read_count, prefix_seq_num, suffix_seq_num);
404 		rc = -EIO;
405 	}
406 
407 	return rc;
408 }
409 
410 #ifdef CONFIG_DCB
411 static void
412 qed_dcbx_get_priority_info(struct qed_hwfn *p_hwfn,
413 			   struct qed_dcbx_app_prio *p_prio,
414 			   struct qed_dcbx_results *p_results)
415 {
416 	u8 val;
417 
418 	p_prio->roce = QED_DCBX_INVALID_PRIORITY;
419 	p_prio->roce_v2 = QED_DCBX_INVALID_PRIORITY;
420 	p_prio->iscsi = QED_DCBX_INVALID_PRIORITY;
421 	p_prio->fcoe = QED_DCBX_INVALID_PRIORITY;
422 
423 	if (p_results->arr[DCBX_PROTOCOL_ROCE].update &&
424 	    p_results->arr[DCBX_PROTOCOL_ROCE].enable)
425 		p_prio->roce = p_results->arr[DCBX_PROTOCOL_ROCE].priority;
426 
427 	if (p_results->arr[DCBX_PROTOCOL_ROCE_V2].update &&
428 	    p_results->arr[DCBX_PROTOCOL_ROCE_V2].enable) {
429 		val = p_results->arr[DCBX_PROTOCOL_ROCE_V2].priority;
430 		p_prio->roce_v2 = val;
431 	}
432 
433 	if (p_results->arr[DCBX_PROTOCOL_ISCSI].update &&
434 	    p_results->arr[DCBX_PROTOCOL_ISCSI].enable)
435 		p_prio->iscsi = p_results->arr[DCBX_PROTOCOL_ISCSI].priority;
436 
437 	if (p_results->arr[DCBX_PROTOCOL_FCOE].update &&
438 	    p_results->arr[DCBX_PROTOCOL_FCOE].enable)
439 		p_prio->fcoe = p_results->arr[DCBX_PROTOCOL_FCOE].priority;
440 
441 	if (p_results->arr[DCBX_PROTOCOL_ETH].update &&
442 	    p_results->arr[DCBX_PROTOCOL_ETH].enable)
443 		p_prio->eth = p_results->arr[DCBX_PROTOCOL_ETH].priority;
444 
445 	DP_VERBOSE(p_hwfn, QED_MSG_DCB,
446 		   "Priorities: iscsi %d, roce %d, roce v2 %d, fcoe %d, eth %d\n",
447 		   p_prio->iscsi, p_prio->roce, p_prio->roce_v2, p_prio->fcoe,
448 		   p_prio->eth);
449 }
450 
451 static void
452 qed_dcbx_get_app_data(struct qed_hwfn *p_hwfn,
453 		      struct dcbx_app_priority_feature *p_app,
454 		      struct dcbx_app_priority_entry *p_tbl,
455 		      struct qed_dcbx_params *p_params, bool ieee)
456 {
457 	struct qed_app_entry *entry;
458 	u8 pri_map;
459 	int i;
460 
461 	p_params->app_willing = QED_MFW_GET_FIELD(p_app->flags,
462 						  DCBX_APP_WILLING);
463 	p_params->app_valid = QED_MFW_GET_FIELD(p_app->flags, DCBX_APP_ENABLED);
464 	p_params->app_error = QED_MFW_GET_FIELD(p_app->flags, DCBX_APP_ERROR);
465 	p_params->num_app_entries = QED_MFW_GET_FIELD(p_app->flags,
466 						      DCBX_APP_NUM_ENTRIES);
467 	for (i = 0; i < DCBX_MAX_APP_PROTOCOL; i++) {
468 		entry = &p_params->app_entry[i];
469 		if (ieee) {
470 			u8 sf_ieee;
471 			u32 val;
472 
473 			sf_ieee = QED_MFW_GET_FIELD(p_tbl[i].entry,
474 						    DCBX_APP_SF_IEEE);
475 			switch (sf_ieee) {
476 			case DCBX_APP_SF_IEEE_RESERVED:
477 				/* Old MFW */
478 				val = QED_MFW_GET_FIELD(p_tbl[i].entry,
479 							DCBX_APP_SF);
480 				entry->sf_ieee = val ?
481 				    QED_DCBX_SF_IEEE_TCP_UDP_PORT :
482 				    QED_DCBX_SF_IEEE_ETHTYPE;
483 				break;
484 			case DCBX_APP_SF_IEEE_ETHTYPE:
485 				entry->sf_ieee = QED_DCBX_SF_IEEE_ETHTYPE;
486 				break;
487 			case DCBX_APP_SF_IEEE_TCP_PORT:
488 				entry->sf_ieee = QED_DCBX_SF_IEEE_TCP_PORT;
489 				break;
490 			case DCBX_APP_SF_IEEE_UDP_PORT:
491 				entry->sf_ieee = QED_DCBX_SF_IEEE_UDP_PORT;
492 				break;
493 			case DCBX_APP_SF_IEEE_TCP_UDP_PORT:
494 				entry->sf_ieee = QED_DCBX_SF_IEEE_TCP_UDP_PORT;
495 				break;
496 			}
497 		} else {
498 			entry->ethtype = !(QED_MFW_GET_FIELD(p_tbl[i].entry,
499 							     DCBX_APP_SF));
500 		}
501 
502 		pri_map = QED_MFW_GET_FIELD(p_tbl[i].entry, DCBX_APP_PRI_MAP);
503 		entry->prio = ffs(pri_map) - 1;
504 		entry->proto_id = QED_MFW_GET_FIELD(p_tbl[i].entry,
505 						    DCBX_APP_PROTOCOL_ID);
506 		qed_dcbx_get_app_protocol_type(p_hwfn, p_tbl[i].entry,
507 					       entry->proto_id,
508 					       &entry->proto_type, ieee);
509 	}
510 
511 	DP_VERBOSE(p_hwfn, QED_MSG_DCB,
512 		   "APP params: willing %d, valid %d error = %d\n",
513 		   p_params->app_willing, p_params->app_valid,
514 		   p_params->app_error);
515 }
516 
517 static void
518 qed_dcbx_get_pfc_data(struct qed_hwfn *p_hwfn,
519 		      u32 pfc, struct qed_dcbx_params *p_params)
520 {
521 	u8 pfc_map;
522 
523 	p_params->pfc.willing = QED_MFW_GET_FIELD(pfc, DCBX_PFC_WILLING);
524 	p_params->pfc.max_tc = QED_MFW_GET_FIELD(pfc, DCBX_PFC_CAPS);
525 	p_params->pfc.enabled = QED_MFW_GET_FIELD(pfc, DCBX_PFC_ENABLED);
526 	pfc_map = QED_MFW_GET_FIELD(pfc, DCBX_PFC_PRI_EN_BITMAP);
527 	p_params->pfc.prio[0] = !!(pfc_map & DCBX_PFC_PRI_EN_BITMAP_PRI_0);
528 	p_params->pfc.prio[1] = !!(pfc_map & DCBX_PFC_PRI_EN_BITMAP_PRI_1);
529 	p_params->pfc.prio[2] = !!(pfc_map & DCBX_PFC_PRI_EN_BITMAP_PRI_2);
530 	p_params->pfc.prio[3] = !!(pfc_map & DCBX_PFC_PRI_EN_BITMAP_PRI_3);
531 	p_params->pfc.prio[4] = !!(pfc_map & DCBX_PFC_PRI_EN_BITMAP_PRI_4);
532 	p_params->pfc.prio[5] = !!(pfc_map & DCBX_PFC_PRI_EN_BITMAP_PRI_5);
533 	p_params->pfc.prio[6] = !!(pfc_map & DCBX_PFC_PRI_EN_BITMAP_PRI_6);
534 	p_params->pfc.prio[7] = !!(pfc_map & DCBX_PFC_PRI_EN_BITMAP_PRI_7);
535 
536 	DP_VERBOSE(p_hwfn, QED_MSG_DCB,
537 		   "PFC params: willing %d, pfc_bitmap %d\n",
538 		   p_params->pfc.willing, pfc_map);
539 }
540 
541 static void
542 qed_dcbx_get_ets_data(struct qed_hwfn *p_hwfn,
543 		      struct dcbx_ets_feature *p_ets,
544 		      struct qed_dcbx_params *p_params)
545 {
546 	u32 bw_map[2], tsa_map[2], pri_map;
547 	int i;
548 
549 	p_params->ets_willing = QED_MFW_GET_FIELD(p_ets->flags,
550 						  DCBX_ETS_WILLING);
551 	p_params->ets_enabled = QED_MFW_GET_FIELD(p_ets->flags,
552 						  DCBX_ETS_ENABLED);
553 	p_params->ets_cbs = QED_MFW_GET_FIELD(p_ets->flags, DCBX_ETS_CBS);
554 	p_params->max_ets_tc = QED_MFW_GET_FIELD(p_ets->flags,
555 						 DCBX_ETS_MAX_TCS);
556 	DP_VERBOSE(p_hwfn, QED_MSG_DCB,
557 		   "ETS params: willing %d, ets_cbs %d pri_tc_tbl_0 %x max_ets_tc %d\n",
558 		   p_params->ets_willing,
559 		   p_params->ets_cbs,
560 		   p_ets->pri_tc_tbl[0], p_params->max_ets_tc);
561 
562 	/* 8 bit tsa and bw data corresponding to each of the 8 TC's are
563 	 * encoded in a type u32 array of size 2.
564 	 */
565 	bw_map[0] = be32_to_cpu(p_ets->tc_bw_tbl[0]);
566 	bw_map[1] = be32_to_cpu(p_ets->tc_bw_tbl[1]);
567 	tsa_map[0] = be32_to_cpu(p_ets->tc_tsa_tbl[0]);
568 	tsa_map[1] = be32_to_cpu(p_ets->tc_tsa_tbl[1]);
569 	pri_map = p_ets->pri_tc_tbl[0];
570 	for (i = 0; i < QED_MAX_PFC_PRIORITIES; i++) {
571 		p_params->ets_tc_bw_tbl[i] = ((u8 *)bw_map)[i];
572 		p_params->ets_tc_tsa_tbl[i] = ((u8 *)tsa_map)[i];
573 		p_params->ets_pri_tc_tbl[i] = QED_DCBX_PRIO2TC(pri_map, i);
574 		DP_VERBOSE(p_hwfn, QED_MSG_DCB,
575 			   "elem %d  bw_tbl %x tsa_tbl %x\n",
576 			   i, p_params->ets_tc_bw_tbl[i],
577 			   p_params->ets_tc_tsa_tbl[i]);
578 	}
579 }
580 
581 static void
582 qed_dcbx_get_common_params(struct qed_hwfn *p_hwfn,
583 			   struct dcbx_app_priority_feature *p_app,
584 			   struct dcbx_app_priority_entry *p_tbl,
585 			   struct dcbx_ets_feature *p_ets,
586 			   u32 pfc, struct qed_dcbx_params *p_params, bool ieee)
587 {
588 	qed_dcbx_get_app_data(p_hwfn, p_app, p_tbl, p_params, ieee);
589 	qed_dcbx_get_ets_data(p_hwfn, p_ets, p_params);
590 	qed_dcbx_get_pfc_data(p_hwfn, pfc, p_params);
591 }
592 
593 static void
594 qed_dcbx_get_local_params(struct qed_hwfn *p_hwfn,
595 			  struct qed_ptt *p_ptt, struct qed_dcbx_get *params)
596 {
597 	struct dcbx_features *p_feat;
598 
599 	p_feat = &p_hwfn->p_dcbx_info->local_admin.features;
600 	qed_dcbx_get_common_params(p_hwfn, &p_feat->app,
601 				   p_feat->app.app_pri_tbl, &p_feat->ets,
602 				   p_feat->pfc, &params->local.params, false);
603 	params->local.valid = true;
604 }
605 
606 static void
607 qed_dcbx_get_remote_params(struct qed_hwfn *p_hwfn,
608 			   struct qed_ptt *p_ptt, struct qed_dcbx_get *params)
609 {
610 	struct dcbx_features *p_feat;
611 
612 	p_feat = &p_hwfn->p_dcbx_info->remote.features;
613 	qed_dcbx_get_common_params(p_hwfn, &p_feat->app,
614 				   p_feat->app.app_pri_tbl, &p_feat->ets,
615 				   p_feat->pfc, &params->remote.params, false);
616 	params->remote.valid = true;
617 }
618 
619 static void
620 qed_dcbx_get_operational_params(struct qed_hwfn *p_hwfn,
621 				struct qed_ptt *p_ptt,
622 				struct qed_dcbx_get *params)
623 {
624 	struct qed_dcbx_operational_params *p_operational;
625 	struct qed_dcbx_results *p_results;
626 	struct dcbx_features *p_feat;
627 	bool enabled, err;
628 	u32 flags;
629 	bool val;
630 
631 	flags = p_hwfn->p_dcbx_info->operational.flags;
632 
633 	/* If DCBx version is non zero, then negotiation
634 	 * was successfuly performed
635 	 */
636 	p_operational = &params->operational;
637 	enabled = !!(QED_MFW_GET_FIELD(flags, DCBX_CONFIG_VERSION) !=
638 		     DCBX_CONFIG_VERSION_DISABLED);
639 	if (!enabled) {
640 		p_operational->enabled = enabled;
641 		p_operational->valid = false;
642 		return;
643 	}
644 
645 	p_feat = &p_hwfn->p_dcbx_info->operational.features;
646 	p_results = &p_hwfn->p_dcbx_info->results;
647 
648 	val = !!(QED_MFW_GET_FIELD(flags, DCBX_CONFIG_VERSION) ==
649 		 DCBX_CONFIG_VERSION_IEEE);
650 	p_operational->ieee = val;
651 	val = !!(QED_MFW_GET_FIELD(flags, DCBX_CONFIG_VERSION) ==
652 		 DCBX_CONFIG_VERSION_CEE);
653 	p_operational->cee = val;
654 
655 	DP_VERBOSE(p_hwfn, QED_MSG_DCB, "Version support: ieee %d, cee %d\n",
656 		   p_operational->ieee, p_operational->cee);
657 
658 	qed_dcbx_get_common_params(p_hwfn, &p_feat->app,
659 				   p_feat->app.app_pri_tbl, &p_feat->ets,
660 				   p_feat->pfc, &params->operational.params,
661 				   p_operational->ieee);
662 	qed_dcbx_get_priority_info(p_hwfn, &p_operational->app_prio, p_results);
663 	err = QED_MFW_GET_FIELD(p_feat->app.flags, DCBX_APP_ERROR);
664 	p_operational->err = err;
665 	p_operational->enabled = enabled;
666 	p_operational->valid = true;
667 }
668 
669 static void
670 qed_dcbx_get_local_lldp_params(struct qed_hwfn *p_hwfn,
671 			       struct qed_ptt *p_ptt,
672 			       struct qed_dcbx_get *params)
673 {
674 	struct lldp_config_params_s *p_local;
675 
676 	p_local = &p_hwfn->p_dcbx_info->lldp_local[LLDP_NEAREST_BRIDGE];
677 
678 	memcpy(params->lldp_local.local_chassis_id, p_local->local_chassis_id,
679 	       ARRAY_SIZE(p_local->local_chassis_id));
680 	memcpy(params->lldp_local.local_port_id, p_local->local_port_id,
681 	       ARRAY_SIZE(p_local->local_port_id));
682 }
683 
684 static void
685 qed_dcbx_get_remote_lldp_params(struct qed_hwfn *p_hwfn,
686 				struct qed_ptt *p_ptt,
687 				struct qed_dcbx_get *params)
688 {
689 	struct lldp_status_params_s *p_remote;
690 
691 	p_remote = &p_hwfn->p_dcbx_info->lldp_remote[LLDP_NEAREST_BRIDGE];
692 
693 	memcpy(params->lldp_remote.peer_chassis_id, p_remote->peer_chassis_id,
694 	       ARRAY_SIZE(p_remote->peer_chassis_id));
695 	memcpy(params->lldp_remote.peer_port_id, p_remote->peer_port_id,
696 	       ARRAY_SIZE(p_remote->peer_port_id));
697 }
698 
699 static int
700 qed_dcbx_get_params(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt,
701 		    struct qed_dcbx_get *p_params,
702 		    enum qed_mib_read_type type)
703 {
704 	switch (type) {
705 	case QED_DCBX_REMOTE_MIB:
706 		qed_dcbx_get_remote_params(p_hwfn, p_ptt, p_params);
707 		break;
708 	case QED_DCBX_LOCAL_MIB:
709 		qed_dcbx_get_local_params(p_hwfn, p_ptt, p_params);
710 		break;
711 	case QED_DCBX_OPERATIONAL_MIB:
712 		qed_dcbx_get_operational_params(p_hwfn, p_ptt, p_params);
713 		break;
714 	case QED_DCBX_REMOTE_LLDP_MIB:
715 		qed_dcbx_get_remote_lldp_params(p_hwfn, p_ptt, p_params);
716 		break;
717 	case QED_DCBX_LOCAL_LLDP_MIB:
718 		qed_dcbx_get_local_lldp_params(p_hwfn, p_ptt, p_params);
719 		break;
720 	default:
721 		DP_ERR(p_hwfn, "MIB read err, unknown mib type %d\n", type);
722 		return -EINVAL;
723 	}
724 
725 	return 0;
726 }
727 #endif
728 
729 static int
730 qed_dcbx_read_local_lldp_mib(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
731 {
732 	struct qed_dcbx_mib_meta_data data;
733 	int rc = 0;
734 
735 	memset(&data, 0, sizeof(data));
736 	data.addr = p_hwfn->mcp_info->port_addr + offsetof(struct public_port,
737 							   lldp_config_params);
738 	data.lldp_local = p_hwfn->p_dcbx_info->lldp_local;
739 	data.size = sizeof(struct lldp_config_params_s);
740 	qed_memcpy_from(p_hwfn, p_ptt, data.lldp_local, data.addr, data.size);
741 
742 	return rc;
743 }
744 
745 static int
746 qed_dcbx_read_remote_lldp_mib(struct qed_hwfn *p_hwfn,
747 			      struct qed_ptt *p_ptt,
748 			      enum qed_mib_read_type type)
749 {
750 	struct qed_dcbx_mib_meta_data data;
751 	int rc = 0;
752 
753 	memset(&data, 0, sizeof(data));
754 	data.addr = p_hwfn->mcp_info->port_addr + offsetof(struct public_port,
755 							   lldp_status_params);
756 	data.lldp_remote = p_hwfn->p_dcbx_info->lldp_remote;
757 	data.size = sizeof(struct lldp_status_params_s);
758 	rc = qed_dcbx_copy_mib(p_hwfn, p_ptt, &data, type);
759 
760 	return rc;
761 }
762 
763 static int
764 qed_dcbx_read_operational_mib(struct qed_hwfn *p_hwfn,
765 			      struct qed_ptt *p_ptt,
766 			      enum qed_mib_read_type type)
767 {
768 	struct qed_dcbx_mib_meta_data data;
769 	int rc = 0;
770 
771 	memset(&data, 0, sizeof(data));
772 	data.addr = p_hwfn->mcp_info->port_addr +
773 		    offsetof(struct public_port, operational_dcbx_mib);
774 	data.mib = &p_hwfn->p_dcbx_info->operational;
775 	data.size = sizeof(struct dcbx_mib);
776 	rc = qed_dcbx_copy_mib(p_hwfn, p_ptt, &data, type);
777 
778 	return rc;
779 }
780 
781 static int
782 qed_dcbx_read_remote_mib(struct qed_hwfn *p_hwfn,
783 			 struct qed_ptt *p_ptt, enum qed_mib_read_type type)
784 {
785 	struct qed_dcbx_mib_meta_data data;
786 	int rc = 0;
787 
788 	memset(&data, 0, sizeof(data));
789 	data.addr = p_hwfn->mcp_info->port_addr +
790 		    offsetof(struct public_port, remote_dcbx_mib);
791 	data.mib = &p_hwfn->p_dcbx_info->remote;
792 	data.size = sizeof(struct dcbx_mib);
793 	rc = qed_dcbx_copy_mib(p_hwfn, p_ptt, &data, type);
794 
795 	return rc;
796 }
797 
798 static int
799 qed_dcbx_read_local_mib(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
800 {
801 	struct qed_dcbx_mib_meta_data data;
802 	int rc = 0;
803 
804 	memset(&data, 0, sizeof(data));
805 	data.addr = p_hwfn->mcp_info->port_addr +
806 		    offsetof(struct public_port, local_admin_dcbx_mib);
807 	data.local_admin = &p_hwfn->p_dcbx_info->local_admin;
808 	data.size = sizeof(struct dcbx_local_params);
809 	qed_memcpy_from(p_hwfn, p_ptt, data.local_admin, data.addr, data.size);
810 
811 	return rc;
812 }
813 
814 static int qed_dcbx_read_mib(struct qed_hwfn *p_hwfn,
815 			     struct qed_ptt *p_ptt, enum qed_mib_read_type type)
816 {
817 	int rc = -EINVAL;
818 
819 	switch (type) {
820 	case QED_DCBX_OPERATIONAL_MIB:
821 		rc = qed_dcbx_read_operational_mib(p_hwfn, p_ptt, type);
822 		break;
823 	case QED_DCBX_REMOTE_MIB:
824 		rc = qed_dcbx_read_remote_mib(p_hwfn, p_ptt, type);
825 		break;
826 	case QED_DCBX_LOCAL_MIB:
827 		rc = qed_dcbx_read_local_mib(p_hwfn, p_ptt);
828 		break;
829 	case QED_DCBX_REMOTE_LLDP_MIB:
830 		rc = qed_dcbx_read_remote_lldp_mib(p_hwfn, p_ptt, type);
831 		break;
832 	case QED_DCBX_LOCAL_LLDP_MIB:
833 		rc = qed_dcbx_read_local_lldp_mib(p_hwfn, p_ptt);
834 		break;
835 	default:
836 		DP_ERR(p_hwfn, "MIB read err, unknown mib type %d\n", type);
837 	}
838 
839 	return rc;
840 }
841 
842 /* Read updated MIB.
843  * Reconfigure QM and invoke PF update ramrod command if operational MIB
844  * change is detected.
845  */
846 int
847 qed_dcbx_mib_update_event(struct qed_hwfn *p_hwfn,
848 			  struct qed_ptt *p_ptt, enum qed_mib_read_type type)
849 {
850 	int rc = 0;
851 
852 	rc = qed_dcbx_read_mib(p_hwfn, p_ptt, type);
853 	if (rc)
854 		return rc;
855 
856 	if (type == QED_DCBX_OPERATIONAL_MIB) {
857 		rc = qed_dcbx_process_mib_info(p_hwfn);
858 		if (!rc) {
859 			/* reconfigure tcs of QM queues according
860 			 * to negotiation results
861 			 */
862 			qed_qm_reconf(p_hwfn, p_ptt);
863 
864 			/* update storm FW with negotiation results */
865 			qed_sp_pf_update(p_hwfn);
866 		}
867 	}
868 
869 	return rc;
870 }
871 
872 int qed_dcbx_info_alloc(struct qed_hwfn *p_hwfn)
873 {
874 	int rc = 0;
875 
876 	p_hwfn->p_dcbx_info = kzalloc(sizeof(*p_hwfn->p_dcbx_info), GFP_KERNEL);
877 	if (!p_hwfn->p_dcbx_info)
878 		rc = -ENOMEM;
879 
880 	return rc;
881 }
882 
883 void qed_dcbx_info_free(struct qed_hwfn *p_hwfn,
884 			struct qed_dcbx_info *p_dcbx_info)
885 {
886 	kfree(p_hwfn->p_dcbx_info);
887 }
888 
889 static void qed_dcbx_update_protocol_data(struct protocol_dcb_data *p_data,
890 					  struct qed_dcbx_results *p_src,
891 					  enum dcbx_protocol_type type)
892 {
893 	p_data->dcb_enable_flag = p_src->arr[type].enable;
894 	p_data->dcb_priority = p_src->arr[type].priority;
895 	p_data->dcb_tc = p_src->arr[type].tc;
896 }
897 
898 /* Set pf update ramrod command params */
899 void qed_dcbx_set_pf_update_params(struct qed_dcbx_results *p_src,
900 				   struct pf_update_ramrod_data *p_dest)
901 {
902 	struct protocol_dcb_data *p_dcb_data;
903 	bool update_flag = false;
904 
905 	p_dest->pf_id = p_src->pf_id;
906 
907 	update_flag = p_src->arr[DCBX_PROTOCOL_FCOE].update;
908 	p_dest->update_fcoe_dcb_data_flag = update_flag;
909 
910 	update_flag = p_src->arr[DCBX_PROTOCOL_ROCE].update;
911 	p_dest->update_roce_dcb_data_flag = update_flag;
912 	update_flag = p_src->arr[DCBX_PROTOCOL_ROCE_V2].update;
913 	p_dest->update_roce_dcb_data_flag = update_flag;
914 
915 	update_flag = p_src->arr[DCBX_PROTOCOL_ISCSI].update;
916 	p_dest->update_iscsi_dcb_data_flag = update_flag;
917 	update_flag = p_src->arr[DCBX_PROTOCOL_ETH].update;
918 	p_dest->update_eth_dcb_data_flag = update_flag;
919 
920 	p_dcb_data = &p_dest->fcoe_dcb_data;
921 	qed_dcbx_update_protocol_data(p_dcb_data, p_src, DCBX_PROTOCOL_FCOE);
922 	p_dcb_data = &p_dest->roce_dcb_data;
923 
924 	if (p_src->arr[DCBX_PROTOCOL_ROCE].update)
925 		qed_dcbx_update_protocol_data(p_dcb_data, p_src,
926 					      DCBX_PROTOCOL_ROCE);
927 	if (p_src->arr[DCBX_PROTOCOL_ROCE_V2].update)
928 		qed_dcbx_update_protocol_data(p_dcb_data, p_src,
929 					      DCBX_PROTOCOL_ROCE_V2);
930 
931 	p_dcb_data = &p_dest->iscsi_dcb_data;
932 	qed_dcbx_update_protocol_data(p_dcb_data, p_src, DCBX_PROTOCOL_ISCSI);
933 	p_dcb_data = &p_dest->eth_dcb_data;
934 	qed_dcbx_update_protocol_data(p_dcb_data, p_src, DCBX_PROTOCOL_ETH);
935 }
936 
937 #ifdef CONFIG_DCB
938 static int qed_dcbx_query_params(struct qed_hwfn *p_hwfn,
939 				 struct qed_dcbx_get *p_get,
940 				 enum qed_mib_read_type type)
941 {
942 	struct qed_ptt *p_ptt;
943 	int rc;
944 
945 	p_ptt = qed_ptt_acquire(p_hwfn);
946 	if (!p_ptt)
947 		return -EBUSY;
948 
949 	rc = qed_dcbx_read_mib(p_hwfn, p_ptt, type);
950 	if (rc)
951 		goto out;
952 
953 	rc = qed_dcbx_get_params(p_hwfn, p_ptt, p_get, type);
954 
955 out:
956 	qed_ptt_release(p_hwfn, p_ptt);
957 	return rc;
958 }
959 
960 static void
961 qed_dcbx_set_pfc_data(struct qed_hwfn *p_hwfn,
962 		      u32 *pfc, struct qed_dcbx_params *p_params)
963 {
964 	u8 pfc_map = 0;
965 	int i;
966 
967 	if (p_params->pfc.willing)
968 		*pfc |= DCBX_PFC_WILLING_MASK;
969 	else
970 		*pfc &= ~DCBX_PFC_WILLING_MASK;
971 
972 	if (p_params->pfc.enabled)
973 		*pfc |= DCBX_PFC_ENABLED_MASK;
974 	else
975 		*pfc &= ~DCBX_PFC_ENABLED_MASK;
976 
977 	*pfc &= ~DCBX_PFC_CAPS_MASK;
978 	*pfc |= (u32)p_params->pfc.max_tc << DCBX_PFC_CAPS_SHIFT;
979 
980 	for (i = 0; i < QED_MAX_PFC_PRIORITIES; i++)
981 		if (p_params->pfc.prio[i])
982 			pfc_map |= BIT(i);
983 
984 	*pfc |= (pfc_map << DCBX_PFC_PRI_EN_BITMAP_SHIFT);
985 
986 	DP_VERBOSE(p_hwfn, QED_MSG_DCB, "pfc = 0x%x\n", *pfc);
987 }
988 
989 static void
990 qed_dcbx_set_ets_data(struct qed_hwfn *p_hwfn,
991 		      struct dcbx_ets_feature *p_ets,
992 		      struct qed_dcbx_params *p_params)
993 {
994 	u8 *bw_map, *tsa_map;
995 	u32 val;
996 	int i;
997 
998 	if (p_params->ets_willing)
999 		p_ets->flags |= DCBX_ETS_WILLING_MASK;
1000 	else
1001 		p_ets->flags &= ~DCBX_ETS_WILLING_MASK;
1002 
1003 	if (p_params->ets_cbs)
1004 		p_ets->flags |= DCBX_ETS_CBS_MASK;
1005 	else
1006 		p_ets->flags &= ~DCBX_ETS_CBS_MASK;
1007 
1008 	if (p_params->ets_enabled)
1009 		p_ets->flags |= DCBX_ETS_ENABLED_MASK;
1010 	else
1011 		p_ets->flags &= ~DCBX_ETS_ENABLED_MASK;
1012 
1013 	p_ets->flags &= ~DCBX_ETS_MAX_TCS_MASK;
1014 	p_ets->flags |= (u32)p_params->max_ets_tc << DCBX_ETS_MAX_TCS_SHIFT;
1015 
1016 	bw_map = (u8 *)&p_ets->tc_bw_tbl[0];
1017 	tsa_map = (u8 *)&p_ets->tc_tsa_tbl[0];
1018 	p_ets->pri_tc_tbl[0] = 0;
1019 	for (i = 0; i < QED_MAX_PFC_PRIORITIES; i++) {
1020 		bw_map[i] = p_params->ets_tc_bw_tbl[i];
1021 		tsa_map[i] = p_params->ets_tc_tsa_tbl[i];
1022 		/* Copy the priority value to the corresponding 4 bits in the
1023 		 * traffic class table.
1024 		 */
1025 		val = (((u32)p_params->ets_pri_tc_tbl[i]) << ((7 - i) * 4));
1026 		p_ets->pri_tc_tbl[0] |= val;
1027 	}
1028 	for (i = 0; i < 2; i++) {
1029 		p_ets->tc_bw_tbl[i] = cpu_to_be32(p_ets->tc_bw_tbl[i]);
1030 		p_ets->tc_tsa_tbl[i] = cpu_to_be32(p_ets->tc_tsa_tbl[i]);
1031 	}
1032 }
1033 
1034 static void
1035 qed_dcbx_set_app_data(struct qed_hwfn *p_hwfn,
1036 		      struct dcbx_app_priority_feature *p_app,
1037 		      struct qed_dcbx_params *p_params, bool ieee)
1038 {
1039 	u32 *entry;
1040 	int i;
1041 
1042 	if (p_params->app_willing)
1043 		p_app->flags |= DCBX_APP_WILLING_MASK;
1044 	else
1045 		p_app->flags &= ~DCBX_APP_WILLING_MASK;
1046 
1047 	if (p_params->app_valid)
1048 		p_app->flags |= DCBX_APP_ENABLED_MASK;
1049 	else
1050 		p_app->flags &= ~DCBX_APP_ENABLED_MASK;
1051 
1052 	p_app->flags &= ~DCBX_APP_NUM_ENTRIES_MASK;
1053 	p_app->flags |= (u32)p_params->num_app_entries <<
1054 	    DCBX_APP_NUM_ENTRIES_SHIFT;
1055 
1056 	for (i = 0; i < DCBX_MAX_APP_PROTOCOL; i++) {
1057 		entry = &p_app->app_pri_tbl[i].entry;
1058 		if (ieee) {
1059 			*entry &= ~DCBX_APP_SF_IEEE_MASK;
1060 			switch (p_params->app_entry[i].sf_ieee) {
1061 			case QED_DCBX_SF_IEEE_ETHTYPE:
1062 				*entry |= ((u32)DCBX_APP_SF_IEEE_ETHTYPE <<
1063 					   DCBX_APP_SF_IEEE_SHIFT);
1064 				break;
1065 			case QED_DCBX_SF_IEEE_TCP_PORT:
1066 				*entry |= ((u32)DCBX_APP_SF_IEEE_TCP_PORT <<
1067 					   DCBX_APP_SF_IEEE_SHIFT);
1068 				break;
1069 			case QED_DCBX_SF_IEEE_UDP_PORT:
1070 				*entry |= ((u32)DCBX_APP_SF_IEEE_UDP_PORT <<
1071 					   DCBX_APP_SF_IEEE_SHIFT);
1072 				break;
1073 			case QED_DCBX_SF_IEEE_TCP_UDP_PORT:
1074 				*entry |= ((u32)DCBX_APP_SF_IEEE_TCP_UDP_PORT <<
1075 					   DCBX_APP_SF_IEEE_SHIFT);
1076 				break;
1077 			}
1078 		} else {
1079 			*entry &= ~DCBX_APP_SF_MASK;
1080 			if (p_params->app_entry[i].ethtype)
1081 				*entry |= ((u32)DCBX_APP_SF_ETHTYPE <<
1082 					   DCBX_APP_SF_SHIFT);
1083 			else
1084 				*entry |= ((u32)DCBX_APP_SF_PORT <<
1085 					   DCBX_APP_SF_SHIFT);
1086 		}
1087 
1088 		*entry &= ~DCBX_APP_PROTOCOL_ID_MASK;
1089 		*entry |= ((u32)p_params->app_entry[i].proto_id <<
1090 			   DCBX_APP_PROTOCOL_ID_SHIFT);
1091 		*entry &= ~DCBX_APP_PRI_MAP_MASK;
1092 		*entry |= ((u32)(p_params->app_entry[i].prio) <<
1093 			   DCBX_APP_PRI_MAP_SHIFT);
1094 	}
1095 }
1096 
1097 static void
1098 qed_dcbx_set_local_params(struct qed_hwfn *p_hwfn,
1099 			  struct dcbx_local_params *local_admin,
1100 			  struct qed_dcbx_set *params)
1101 {
1102 	bool ieee = false;
1103 
1104 	local_admin->flags = 0;
1105 	memcpy(&local_admin->features,
1106 	       &p_hwfn->p_dcbx_info->operational.features,
1107 	       sizeof(local_admin->features));
1108 
1109 	if (params->enabled) {
1110 		local_admin->config = params->ver_num;
1111 		ieee = !!(params->ver_num & DCBX_CONFIG_VERSION_IEEE);
1112 	} else {
1113 		local_admin->config = DCBX_CONFIG_VERSION_DISABLED;
1114 	}
1115 
1116 	if (params->override_flags & QED_DCBX_OVERRIDE_PFC_CFG)
1117 		qed_dcbx_set_pfc_data(p_hwfn, &local_admin->features.pfc,
1118 				      &params->config.params);
1119 
1120 	if (params->override_flags & QED_DCBX_OVERRIDE_ETS_CFG)
1121 		qed_dcbx_set_ets_data(p_hwfn, &local_admin->features.ets,
1122 				      &params->config.params);
1123 
1124 	if (params->override_flags & QED_DCBX_OVERRIDE_APP_CFG)
1125 		qed_dcbx_set_app_data(p_hwfn, &local_admin->features.app,
1126 				      &params->config.params, ieee);
1127 }
1128 
1129 int qed_dcbx_config_params(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt,
1130 			   struct qed_dcbx_set *params, bool hw_commit)
1131 {
1132 	struct dcbx_local_params local_admin;
1133 	struct qed_dcbx_mib_meta_data data;
1134 	u32 resp = 0, param = 0;
1135 	int rc = 0;
1136 
1137 	if (!hw_commit) {
1138 		memcpy(&p_hwfn->p_dcbx_info->set, params,
1139 		       sizeof(struct qed_dcbx_set));
1140 		return 0;
1141 	}
1142 
1143 	/* clear set-parmas cache */
1144 	memset(&p_hwfn->p_dcbx_info->set, 0, sizeof(p_hwfn->p_dcbx_info->set));
1145 
1146 	memset(&local_admin, 0, sizeof(local_admin));
1147 	qed_dcbx_set_local_params(p_hwfn, &local_admin, params);
1148 
1149 	data.addr = p_hwfn->mcp_info->port_addr +
1150 	    offsetof(struct public_port, local_admin_dcbx_mib);
1151 	data.local_admin = &local_admin;
1152 	data.size = sizeof(struct dcbx_local_params);
1153 	qed_memcpy_to(p_hwfn, p_ptt, data.addr, data.local_admin, data.size);
1154 
1155 	rc = qed_mcp_cmd(p_hwfn, p_ptt, DRV_MSG_CODE_SET_DCBX,
1156 			 1 << DRV_MB_PARAM_LLDP_SEND_SHIFT, &resp, &param);
1157 	if (rc)
1158 		DP_NOTICE(p_hwfn, "Failed to send DCBX update request\n");
1159 
1160 	return rc;
1161 }
1162 
1163 int qed_dcbx_get_config_params(struct qed_hwfn *p_hwfn,
1164 			       struct qed_dcbx_set *params)
1165 {
1166 	struct qed_dcbx_get *dcbx_info;
1167 	int rc;
1168 
1169 	if (p_hwfn->p_dcbx_info->set.config.valid) {
1170 		memcpy(params, &p_hwfn->p_dcbx_info->set,
1171 		       sizeof(struct qed_dcbx_set));
1172 		return 0;
1173 	}
1174 
1175 	dcbx_info = kmalloc(sizeof(*dcbx_info), GFP_KERNEL);
1176 	if (!dcbx_info)
1177 		return -ENOMEM;
1178 
1179 	rc = qed_dcbx_query_params(p_hwfn, dcbx_info, QED_DCBX_OPERATIONAL_MIB);
1180 	if (rc) {
1181 		kfree(dcbx_info);
1182 		return rc;
1183 	}
1184 
1185 	p_hwfn->p_dcbx_info->set.override_flags = 0;
1186 	p_hwfn->p_dcbx_info->set.ver_num = DCBX_CONFIG_VERSION_DISABLED;
1187 	if (dcbx_info->operational.cee)
1188 		p_hwfn->p_dcbx_info->set.ver_num |= DCBX_CONFIG_VERSION_CEE;
1189 	if (dcbx_info->operational.ieee)
1190 		p_hwfn->p_dcbx_info->set.ver_num |= DCBX_CONFIG_VERSION_IEEE;
1191 
1192 	p_hwfn->p_dcbx_info->set.enabled = dcbx_info->operational.enabled;
1193 	memcpy(&p_hwfn->p_dcbx_info->set.config.params,
1194 	       &dcbx_info->operational.params,
1195 	       sizeof(struct qed_dcbx_admin_params));
1196 	p_hwfn->p_dcbx_info->set.config.valid = true;
1197 
1198 	memcpy(params, &p_hwfn->p_dcbx_info->set, sizeof(struct qed_dcbx_set));
1199 
1200 	kfree(dcbx_info);
1201 
1202 	return 0;
1203 }
1204 
1205 static struct qed_dcbx_get *qed_dcbnl_get_dcbx(struct qed_hwfn *hwfn,
1206 					       enum qed_mib_read_type type)
1207 {
1208 	struct qed_dcbx_get *dcbx_info;
1209 
1210 	dcbx_info = kmalloc(sizeof(*dcbx_info), GFP_KERNEL);
1211 	if (!dcbx_info)
1212 		return NULL;
1213 
1214 	if (qed_dcbx_query_params(hwfn, dcbx_info, type)) {
1215 		kfree(dcbx_info);
1216 		return NULL;
1217 	}
1218 
1219 	if ((type == QED_DCBX_OPERATIONAL_MIB) &&
1220 	    !dcbx_info->operational.enabled) {
1221 		DP_INFO(hwfn, "DCBX is not enabled/operational\n");
1222 		kfree(dcbx_info);
1223 		return NULL;
1224 	}
1225 
1226 	return dcbx_info;
1227 }
1228 
1229 static u8 qed_dcbnl_getstate(struct qed_dev *cdev)
1230 {
1231 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
1232 	struct qed_dcbx_get *dcbx_info;
1233 	bool enabled;
1234 
1235 	dcbx_info = qed_dcbnl_get_dcbx(hwfn, QED_DCBX_OPERATIONAL_MIB);
1236 	if (!dcbx_info)
1237 		return 0;
1238 
1239 	enabled = dcbx_info->operational.enabled;
1240 	DP_VERBOSE(hwfn, QED_MSG_DCB, "DCB state = %d\n", enabled);
1241 	kfree(dcbx_info);
1242 
1243 	return enabled;
1244 }
1245 
1246 static u8 qed_dcbnl_setstate(struct qed_dev *cdev, u8 state)
1247 {
1248 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
1249 	struct qed_dcbx_set dcbx_set;
1250 	struct qed_ptt *ptt;
1251 	int rc;
1252 
1253 	DP_VERBOSE(hwfn, QED_MSG_DCB, "DCB state = %d\n", state);
1254 
1255 	memset(&dcbx_set, 0, sizeof(dcbx_set));
1256 	rc = qed_dcbx_get_config_params(hwfn, &dcbx_set);
1257 	if (rc)
1258 		return 1;
1259 
1260 	dcbx_set.enabled = !!state;
1261 
1262 	ptt = qed_ptt_acquire(hwfn);
1263 	if (!ptt)
1264 		return 1;
1265 
1266 	rc = qed_dcbx_config_params(hwfn, ptt, &dcbx_set, 0);
1267 
1268 	qed_ptt_release(hwfn, ptt);
1269 
1270 	return rc ? 1 : 0;
1271 }
1272 
1273 static void qed_dcbnl_getpgtccfgtx(struct qed_dev *cdev, int tc, u8 *prio_type,
1274 				   u8 *pgid, u8 *bw_pct, u8 *up_map)
1275 {
1276 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
1277 	struct qed_dcbx_get *dcbx_info;
1278 
1279 	DP_VERBOSE(hwfn, QED_MSG_DCB, "tc = %d\n", tc);
1280 	*prio_type = *pgid = *bw_pct = *up_map = 0;
1281 	if (tc < 0 || tc >= QED_MAX_PFC_PRIORITIES) {
1282 		DP_INFO(hwfn, "Invalid tc %d\n", tc);
1283 		return;
1284 	}
1285 
1286 	dcbx_info = qed_dcbnl_get_dcbx(hwfn, QED_DCBX_OPERATIONAL_MIB);
1287 	if (!dcbx_info)
1288 		return;
1289 
1290 	*pgid = dcbx_info->operational.params.ets_pri_tc_tbl[tc];
1291 	kfree(dcbx_info);
1292 }
1293 
1294 static void qed_dcbnl_getpgbwgcfgtx(struct qed_dev *cdev, int pgid, u8 *bw_pct)
1295 {
1296 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
1297 	struct qed_dcbx_get *dcbx_info;
1298 
1299 	*bw_pct = 0;
1300 	DP_VERBOSE(hwfn, QED_MSG_DCB, "pgid = %d\n", pgid);
1301 	if (pgid < 0 || pgid >= QED_MAX_PFC_PRIORITIES) {
1302 		DP_INFO(hwfn, "Invalid pgid %d\n", pgid);
1303 		return;
1304 	}
1305 
1306 	dcbx_info = qed_dcbnl_get_dcbx(hwfn, QED_DCBX_OPERATIONAL_MIB);
1307 	if (!dcbx_info)
1308 		return;
1309 
1310 	*bw_pct = dcbx_info->operational.params.ets_tc_bw_tbl[pgid];
1311 	DP_VERBOSE(hwfn, QED_MSG_DCB, "bw_pct = %d\n", *bw_pct);
1312 	kfree(dcbx_info);
1313 }
1314 
1315 static void qed_dcbnl_getpgtccfgrx(struct qed_dev *cdev, int tc, u8 *prio,
1316 				   u8 *bwg_id, u8 *bw_pct, u8 *up_map)
1317 {
1318 	DP_INFO(QED_LEADING_HWFN(cdev), "Rx ETS is not supported\n");
1319 	*prio = *bwg_id = *bw_pct = *up_map = 0;
1320 }
1321 
1322 static void qed_dcbnl_getpgbwgcfgrx(struct qed_dev *cdev,
1323 				    int bwg_id, u8 *bw_pct)
1324 {
1325 	DP_INFO(QED_LEADING_HWFN(cdev), "Rx ETS is not supported\n");
1326 	*bw_pct = 0;
1327 }
1328 
1329 static void qed_dcbnl_getpfccfg(struct qed_dev *cdev,
1330 				int priority, u8 *setting)
1331 {
1332 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
1333 	struct qed_dcbx_get *dcbx_info;
1334 
1335 	DP_VERBOSE(hwfn, QED_MSG_DCB, "priority = %d\n", priority);
1336 	if (priority < 0 || priority >= QED_MAX_PFC_PRIORITIES) {
1337 		DP_INFO(hwfn, "Invalid priority %d\n", priority);
1338 		return;
1339 	}
1340 
1341 	dcbx_info = qed_dcbnl_get_dcbx(hwfn, QED_DCBX_OPERATIONAL_MIB);
1342 	if (!dcbx_info)
1343 		return;
1344 
1345 	*setting = dcbx_info->operational.params.pfc.prio[priority];
1346 	DP_VERBOSE(hwfn, QED_MSG_DCB, "setting = %d\n", *setting);
1347 	kfree(dcbx_info);
1348 }
1349 
1350 static void qed_dcbnl_setpfccfg(struct qed_dev *cdev, int priority, u8 setting)
1351 {
1352 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
1353 	struct qed_dcbx_set dcbx_set;
1354 	struct qed_ptt *ptt;
1355 	int rc;
1356 
1357 	DP_VERBOSE(hwfn, QED_MSG_DCB, "priority = %d setting = %d\n",
1358 		   priority, setting);
1359 	if (priority < 0 || priority >= QED_MAX_PFC_PRIORITIES) {
1360 		DP_INFO(hwfn, "Invalid priority %d\n", priority);
1361 		return;
1362 	}
1363 
1364 	memset(&dcbx_set, 0, sizeof(dcbx_set));
1365 	rc = qed_dcbx_get_config_params(hwfn, &dcbx_set);
1366 	if (rc)
1367 		return;
1368 
1369 	dcbx_set.override_flags |= QED_DCBX_OVERRIDE_PFC_CFG;
1370 	dcbx_set.config.params.pfc.prio[priority] = !!setting;
1371 
1372 	ptt = qed_ptt_acquire(hwfn);
1373 	if (!ptt)
1374 		return;
1375 
1376 	rc = qed_dcbx_config_params(hwfn, ptt, &dcbx_set, 0);
1377 
1378 	qed_ptt_release(hwfn, ptt);
1379 }
1380 
1381 static u8 qed_dcbnl_getcap(struct qed_dev *cdev, int capid, u8 *cap)
1382 {
1383 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
1384 	struct qed_dcbx_get *dcbx_info;
1385 	int rc = 0;
1386 
1387 	DP_VERBOSE(hwfn, QED_MSG_DCB, "capid = %d\n", capid);
1388 	dcbx_info = qed_dcbnl_get_dcbx(hwfn, QED_DCBX_OPERATIONAL_MIB);
1389 	if (!dcbx_info)
1390 		return 1;
1391 
1392 	switch (capid) {
1393 	case DCB_CAP_ATTR_PG:
1394 	case DCB_CAP_ATTR_PFC:
1395 	case DCB_CAP_ATTR_UP2TC:
1396 	case DCB_CAP_ATTR_GSP:
1397 		*cap = true;
1398 		break;
1399 	case DCB_CAP_ATTR_PG_TCS:
1400 	case DCB_CAP_ATTR_PFC_TCS:
1401 		*cap = 0x80;
1402 		break;
1403 	case DCB_CAP_ATTR_DCBX:
1404 		*cap = (DCB_CAP_DCBX_LLD_MANAGED | DCB_CAP_DCBX_VER_CEE |
1405 			DCB_CAP_DCBX_VER_IEEE);
1406 		break;
1407 	default:
1408 		*cap = false;
1409 		rc = 1;
1410 	}
1411 
1412 	DP_VERBOSE(hwfn, QED_MSG_DCB, "id = %d caps = %d\n", capid, *cap);
1413 	kfree(dcbx_info);
1414 
1415 	return rc;
1416 }
1417 
1418 static int qed_dcbnl_getnumtcs(struct qed_dev *cdev, int tcid, u8 *num)
1419 {
1420 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
1421 	struct qed_dcbx_get *dcbx_info;
1422 	int rc = 0;
1423 
1424 	DP_VERBOSE(hwfn, QED_MSG_DCB, "tcid = %d\n", tcid);
1425 	dcbx_info = qed_dcbnl_get_dcbx(hwfn, QED_DCBX_OPERATIONAL_MIB);
1426 	if (!dcbx_info)
1427 		return -EINVAL;
1428 
1429 	switch (tcid) {
1430 	case DCB_NUMTCS_ATTR_PG:
1431 		*num = dcbx_info->operational.params.max_ets_tc;
1432 		break;
1433 	case DCB_NUMTCS_ATTR_PFC:
1434 		*num = dcbx_info->operational.params.pfc.max_tc;
1435 		break;
1436 	default:
1437 		rc = -EINVAL;
1438 	}
1439 
1440 	kfree(dcbx_info);
1441 	DP_VERBOSE(hwfn, QED_MSG_DCB, "numtcs = %d\n", *num);
1442 
1443 	return rc;
1444 }
1445 
1446 static u8 qed_dcbnl_getpfcstate(struct qed_dev *cdev)
1447 {
1448 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
1449 	struct qed_dcbx_get *dcbx_info;
1450 	bool enabled;
1451 
1452 	dcbx_info = qed_dcbnl_get_dcbx(hwfn, QED_DCBX_OPERATIONAL_MIB);
1453 	if (!dcbx_info)
1454 		return 0;
1455 
1456 	enabled = dcbx_info->operational.params.pfc.enabled;
1457 	DP_VERBOSE(hwfn, QED_MSG_DCB, "pfc state = %d\n", enabled);
1458 	kfree(dcbx_info);
1459 
1460 	return enabled;
1461 }
1462 
1463 static u8 qed_dcbnl_getdcbx(struct qed_dev *cdev)
1464 {
1465 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
1466 	struct qed_dcbx_get *dcbx_info;
1467 	u8 mode = 0;
1468 
1469 	dcbx_info = qed_dcbnl_get_dcbx(hwfn, QED_DCBX_OPERATIONAL_MIB);
1470 	if (!dcbx_info)
1471 		return 0;
1472 
1473 	if (dcbx_info->operational.enabled)
1474 		mode |= DCB_CAP_DCBX_LLD_MANAGED;
1475 	if (dcbx_info->operational.ieee)
1476 		mode |= DCB_CAP_DCBX_VER_IEEE;
1477 	if (dcbx_info->operational.cee)
1478 		mode |= DCB_CAP_DCBX_VER_CEE;
1479 
1480 	DP_VERBOSE(hwfn, QED_MSG_DCB, "dcb mode = %d\n", mode);
1481 	kfree(dcbx_info);
1482 
1483 	return mode;
1484 }
1485 
1486 static void qed_dcbnl_setpgtccfgtx(struct qed_dev *cdev,
1487 				   int tc,
1488 				   u8 pri_type, u8 pgid, u8 bw_pct, u8 up_map)
1489 {
1490 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
1491 	struct qed_dcbx_set dcbx_set;
1492 	struct qed_ptt *ptt;
1493 	int rc;
1494 
1495 	DP_VERBOSE(hwfn, QED_MSG_DCB,
1496 		   "tc = %d pri_type = %d pgid = %d bw_pct = %d up_map = %d\n",
1497 		   tc, pri_type, pgid, bw_pct, up_map);
1498 
1499 	if (tc < 0 || tc >= QED_MAX_PFC_PRIORITIES) {
1500 		DP_INFO(hwfn, "Invalid tc %d\n", tc);
1501 		return;
1502 	}
1503 
1504 	memset(&dcbx_set, 0, sizeof(dcbx_set));
1505 	rc = qed_dcbx_get_config_params(hwfn, &dcbx_set);
1506 	if (rc)
1507 		return;
1508 
1509 	dcbx_set.override_flags |= QED_DCBX_OVERRIDE_ETS_CFG;
1510 	dcbx_set.config.params.ets_pri_tc_tbl[tc] = pgid;
1511 
1512 	ptt = qed_ptt_acquire(hwfn);
1513 	if (!ptt)
1514 		return;
1515 
1516 	rc = qed_dcbx_config_params(hwfn, ptt, &dcbx_set, 0);
1517 
1518 	qed_ptt_release(hwfn, ptt);
1519 }
1520 
1521 static void qed_dcbnl_setpgtccfgrx(struct qed_dev *cdev, int prio,
1522 				   u8 pri_type, u8 pgid, u8 bw_pct, u8 up_map)
1523 {
1524 	DP_INFO(QED_LEADING_HWFN(cdev), "Rx ETS is not supported\n");
1525 }
1526 
1527 static void qed_dcbnl_setpgbwgcfgtx(struct qed_dev *cdev, int pgid, u8 bw_pct)
1528 {
1529 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
1530 	struct qed_dcbx_set dcbx_set;
1531 	struct qed_ptt *ptt;
1532 	int rc;
1533 
1534 	DP_VERBOSE(hwfn, QED_MSG_DCB, "pgid = %d bw_pct = %d\n", pgid, bw_pct);
1535 	if (pgid < 0 || pgid >= QED_MAX_PFC_PRIORITIES) {
1536 		DP_INFO(hwfn, "Invalid pgid %d\n", pgid);
1537 		return;
1538 	}
1539 
1540 	memset(&dcbx_set, 0, sizeof(dcbx_set));
1541 	rc = qed_dcbx_get_config_params(hwfn, &dcbx_set);
1542 	if (rc)
1543 		return;
1544 
1545 	dcbx_set.override_flags |= QED_DCBX_OVERRIDE_ETS_CFG;
1546 	dcbx_set.config.params.ets_tc_bw_tbl[pgid] = bw_pct;
1547 
1548 	ptt = qed_ptt_acquire(hwfn);
1549 	if (!ptt)
1550 		return;
1551 
1552 	rc = qed_dcbx_config_params(hwfn, ptt, &dcbx_set, 0);
1553 
1554 	qed_ptt_release(hwfn, ptt);
1555 }
1556 
1557 static void qed_dcbnl_setpgbwgcfgrx(struct qed_dev *cdev, int pgid, u8 bw_pct)
1558 {
1559 	DP_INFO(QED_LEADING_HWFN(cdev), "Rx ETS is not supported\n");
1560 }
1561 
1562 static u8 qed_dcbnl_setall(struct qed_dev *cdev)
1563 {
1564 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
1565 	struct qed_dcbx_set dcbx_set;
1566 	struct qed_ptt *ptt;
1567 	int rc;
1568 
1569 	memset(&dcbx_set, 0, sizeof(dcbx_set));
1570 	rc = qed_dcbx_get_config_params(hwfn, &dcbx_set);
1571 	if (rc)
1572 		return 1;
1573 
1574 	ptt = qed_ptt_acquire(hwfn);
1575 	if (!ptt)
1576 		return 1;
1577 
1578 	rc = qed_dcbx_config_params(hwfn, ptt, &dcbx_set, 1);
1579 
1580 	qed_ptt_release(hwfn, ptt);
1581 
1582 	return rc;
1583 }
1584 
1585 static int qed_dcbnl_setnumtcs(struct qed_dev *cdev, int tcid, u8 num)
1586 {
1587 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
1588 	struct qed_dcbx_set dcbx_set;
1589 	struct qed_ptt *ptt;
1590 	int rc;
1591 
1592 	DP_VERBOSE(hwfn, QED_MSG_DCB, "tcid = %d num = %d\n", tcid, num);
1593 	memset(&dcbx_set, 0, sizeof(dcbx_set));
1594 	rc = qed_dcbx_get_config_params(hwfn, &dcbx_set);
1595 	if (rc)
1596 		return 1;
1597 
1598 	switch (tcid) {
1599 	case DCB_NUMTCS_ATTR_PG:
1600 		dcbx_set.override_flags |= QED_DCBX_OVERRIDE_ETS_CFG;
1601 		dcbx_set.config.params.max_ets_tc = num;
1602 		break;
1603 	case DCB_NUMTCS_ATTR_PFC:
1604 		dcbx_set.override_flags |= QED_DCBX_OVERRIDE_PFC_CFG;
1605 		dcbx_set.config.params.pfc.max_tc = num;
1606 		break;
1607 	default:
1608 		DP_INFO(hwfn, "Invalid tcid %d\n", tcid);
1609 		return -EINVAL;
1610 	}
1611 
1612 	ptt = qed_ptt_acquire(hwfn);
1613 	if (!ptt)
1614 		return -EINVAL;
1615 
1616 	rc = qed_dcbx_config_params(hwfn, ptt, &dcbx_set, 0);
1617 
1618 	qed_ptt_release(hwfn, ptt);
1619 
1620 	return 0;
1621 }
1622 
1623 static void qed_dcbnl_setpfcstate(struct qed_dev *cdev, u8 state)
1624 {
1625 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
1626 	struct qed_dcbx_set dcbx_set;
1627 	struct qed_ptt *ptt;
1628 	int rc;
1629 
1630 	DP_VERBOSE(hwfn, QED_MSG_DCB, "new state = %d\n", state);
1631 
1632 	memset(&dcbx_set, 0, sizeof(dcbx_set));
1633 	rc = qed_dcbx_get_config_params(hwfn, &dcbx_set);
1634 	if (rc)
1635 		return;
1636 
1637 	dcbx_set.override_flags |= QED_DCBX_OVERRIDE_PFC_CFG;
1638 	dcbx_set.config.params.pfc.enabled = !!state;
1639 
1640 	ptt = qed_ptt_acquire(hwfn);
1641 	if (!ptt)
1642 		return;
1643 
1644 	rc = qed_dcbx_config_params(hwfn, ptt, &dcbx_set, 0);
1645 
1646 	qed_ptt_release(hwfn, ptt);
1647 }
1648 
1649 static int qed_dcbnl_getapp(struct qed_dev *cdev, u8 idtype, u16 idval)
1650 {
1651 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
1652 	struct qed_dcbx_get *dcbx_info;
1653 	struct qed_app_entry *entry;
1654 	bool ethtype;
1655 	u8 prio = 0;
1656 	int i;
1657 
1658 	dcbx_info = qed_dcbnl_get_dcbx(hwfn, QED_DCBX_OPERATIONAL_MIB);
1659 	if (!dcbx_info)
1660 		return -EINVAL;
1661 
1662 	ethtype = !!(idtype == DCB_APP_IDTYPE_ETHTYPE);
1663 	for (i = 0; i < QED_DCBX_MAX_APP_PROTOCOL; i++) {
1664 		entry = &dcbx_info->operational.params.app_entry[i];
1665 		if ((entry->ethtype == ethtype) && (entry->proto_id == idval)) {
1666 			prio = entry->prio;
1667 			break;
1668 		}
1669 	}
1670 
1671 	if (i == QED_DCBX_MAX_APP_PROTOCOL) {
1672 		DP_ERR(cdev, "App entry (%d, %d) not found\n", idtype, idval);
1673 		kfree(dcbx_info);
1674 		return -EINVAL;
1675 	}
1676 
1677 	kfree(dcbx_info);
1678 
1679 	return prio;
1680 }
1681 
1682 static int qed_dcbnl_setapp(struct qed_dev *cdev,
1683 			    u8 idtype, u16 idval, u8 pri_map)
1684 {
1685 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
1686 	struct qed_dcbx_set dcbx_set;
1687 	struct qed_app_entry *entry;
1688 	struct qed_ptt *ptt;
1689 	bool ethtype;
1690 	int rc, i;
1691 
1692 	memset(&dcbx_set, 0, sizeof(dcbx_set));
1693 	rc = qed_dcbx_get_config_params(hwfn, &dcbx_set);
1694 	if (rc)
1695 		return -EINVAL;
1696 
1697 	ethtype = !!(idtype == DCB_APP_IDTYPE_ETHTYPE);
1698 	for (i = 0; i < QED_DCBX_MAX_APP_PROTOCOL; i++) {
1699 		entry = &dcbx_set.config.params.app_entry[i];
1700 		if ((entry->ethtype == ethtype) && (entry->proto_id == idval))
1701 			break;
1702 		/* First empty slot */
1703 		if (!entry->proto_id) {
1704 			dcbx_set.config.params.num_app_entries++;
1705 			break;
1706 		}
1707 	}
1708 
1709 	if (i == QED_DCBX_MAX_APP_PROTOCOL) {
1710 		DP_ERR(cdev, "App table is full\n");
1711 		return -EBUSY;
1712 	}
1713 
1714 	dcbx_set.override_flags |= QED_DCBX_OVERRIDE_APP_CFG;
1715 	dcbx_set.config.params.app_entry[i].ethtype = ethtype;
1716 	dcbx_set.config.params.app_entry[i].proto_id = idval;
1717 	dcbx_set.config.params.app_entry[i].prio = pri_map;
1718 
1719 	ptt = qed_ptt_acquire(hwfn);
1720 	if (!ptt)
1721 		return -EBUSY;
1722 
1723 	rc = qed_dcbx_config_params(hwfn, ptt, &dcbx_set, 0);
1724 
1725 	qed_ptt_release(hwfn, ptt);
1726 
1727 	return rc;
1728 }
1729 
1730 static u8 qed_dcbnl_setdcbx(struct qed_dev *cdev, u8 mode)
1731 {
1732 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
1733 	struct qed_dcbx_set dcbx_set;
1734 	struct qed_ptt *ptt;
1735 	int rc;
1736 
1737 	DP_VERBOSE(hwfn, QED_MSG_DCB, "new mode = %x\n", mode);
1738 
1739 	if (!(mode & DCB_CAP_DCBX_VER_IEEE) && !(mode & DCB_CAP_DCBX_VER_CEE)) {
1740 		DP_INFO(hwfn, "Allowed mode is cee, ieee or both\n");
1741 		return 1;
1742 	}
1743 
1744 	memset(&dcbx_set, 0, sizeof(dcbx_set));
1745 	rc = qed_dcbx_get_config_params(hwfn, &dcbx_set);
1746 	if (rc)
1747 		return 1;
1748 
1749 	dcbx_set.ver_num = 0;
1750 	if (mode & DCB_CAP_DCBX_VER_CEE) {
1751 		dcbx_set.ver_num |= DCBX_CONFIG_VERSION_CEE;
1752 		dcbx_set.enabled = true;
1753 	}
1754 
1755 	if (mode & DCB_CAP_DCBX_VER_IEEE) {
1756 		dcbx_set.ver_num |= DCBX_CONFIG_VERSION_IEEE;
1757 		dcbx_set.enabled = true;
1758 	}
1759 
1760 	ptt = qed_ptt_acquire(hwfn);
1761 	if (!ptt)
1762 		return 1;
1763 
1764 	rc = qed_dcbx_config_params(hwfn, ptt, &dcbx_set, 0);
1765 
1766 	qed_ptt_release(hwfn, ptt);
1767 
1768 	return 0;
1769 }
1770 
1771 static u8 qed_dcbnl_getfeatcfg(struct qed_dev *cdev, int featid, u8 *flags)
1772 {
1773 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
1774 	struct qed_dcbx_get *dcbx_info;
1775 
1776 	DP_VERBOSE(hwfn, QED_MSG_DCB, "Feature id  = %d\n", featid);
1777 	dcbx_info = qed_dcbnl_get_dcbx(hwfn, QED_DCBX_OPERATIONAL_MIB);
1778 	if (!dcbx_info)
1779 		return 1;
1780 
1781 	*flags = 0;
1782 	switch (featid) {
1783 	case DCB_FEATCFG_ATTR_PG:
1784 		if (dcbx_info->operational.params.ets_enabled)
1785 			*flags = DCB_FEATCFG_ENABLE;
1786 		else
1787 			*flags = DCB_FEATCFG_ERROR;
1788 		break;
1789 	case DCB_FEATCFG_ATTR_PFC:
1790 		if (dcbx_info->operational.params.pfc.enabled)
1791 			*flags = DCB_FEATCFG_ENABLE;
1792 		else
1793 			*flags = DCB_FEATCFG_ERROR;
1794 		break;
1795 	case DCB_FEATCFG_ATTR_APP:
1796 		if (dcbx_info->operational.params.app_valid)
1797 			*flags = DCB_FEATCFG_ENABLE;
1798 		else
1799 			*flags = DCB_FEATCFG_ERROR;
1800 		break;
1801 	default:
1802 		DP_INFO(hwfn, "Invalid feature-ID %d\n", featid);
1803 		kfree(dcbx_info);
1804 		return 1;
1805 	}
1806 
1807 	DP_VERBOSE(hwfn, QED_MSG_DCB, "flags = %d\n", *flags);
1808 	kfree(dcbx_info);
1809 
1810 	return 0;
1811 }
1812 
1813 static u8 qed_dcbnl_setfeatcfg(struct qed_dev *cdev, int featid, u8 flags)
1814 {
1815 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
1816 	struct qed_dcbx_set dcbx_set;
1817 	bool enabled, willing;
1818 	struct qed_ptt *ptt;
1819 	int rc;
1820 
1821 	DP_VERBOSE(hwfn, QED_MSG_DCB, "featid = %d flags = %d\n",
1822 		   featid, flags);
1823 	memset(&dcbx_set, 0, sizeof(dcbx_set));
1824 	rc = qed_dcbx_get_config_params(hwfn, &dcbx_set);
1825 	if (rc)
1826 		return 1;
1827 
1828 	enabled = !!(flags & DCB_FEATCFG_ENABLE);
1829 	willing = !!(flags & DCB_FEATCFG_WILLING);
1830 	switch (featid) {
1831 	case DCB_FEATCFG_ATTR_PG:
1832 		dcbx_set.override_flags |= QED_DCBX_OVERRIDE_ETS_CFG;
1833 		dcbx_set.config.params.ets_enabled = enabled;
1834 		dcbx_set.config.params.ets_willing = willing;
1835 		break;
1836 	case DCB_FEATCFG_ATTR_PFC:
1837 		dcbx_set.override_flags |= QED_DCBX_OVERRIDE_PFC_CFG;
1838 		dcbx_set.config.params.pfc.enabled = enabled;
1839 		dcbx_set.config.params.pfc.willing = willing;
1840 		break;
1841 	case DCB_FEATCFG_ATTR_APP:
1842 		dcbx_set.override_flags |= QED_DCBX_OVERRIDE_APP_CFG;
1843 		dcbx_set.config.params.app_willing = willing;
1844 		break;
1845 	default:
1846 		DP_INFO(hwfn, "Invalid feature-ID %d\n", featid);
1847 		return 1;
1848 	}
1849 
1850 	ptt = qed_ptt_acquire(hwfn);
1851 	if (!ptt)
1852 		return 1;
1853 
1854 	rc = qed_dcbx_config_params(hwfn, ptt, &dcbx_set, 0);
1855 
1856 	qed_ptt_release(hwfn, ptt);
1857 
1858 	return 0;
1859 }
1860 
1861 static int qed_dcbnl_peer_getappinfo(struct qed_dev *cdev,
1862 				     struct dcb_peer_app_info *info,
1863 				     u16 *app_count)
1864 {
1865 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
1866 	struct qed_dcbx_get *dcbx_info;
1867 
1868 	dcbx_info = qed_dcbnl_get_dcbx(hwfn, QED_DCBX_REMOTE_MIB);
1869 	if (!dcbx_info)
1870 		return -EINVAL;
1871 
1872 	info->willing = dcbx_info->remote.params.app_willing;
1873 	info->error = dcbx_info->remote.params.app_error;
1874 	*app_count = dcbx_info->remote.params.num_app_entries;
1875 	kfree(dcbx_info);
1876 
1877 	return 0;
1878 }
1879 
1880 static int qed_dcbnl_peer_getapptable(struct qed_dev *cdev,
1881 				      struct dcb_app *table)
1882 {
1883 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
1884 	struct qed_dcbx_get *dcbx_info;
1885 	int i;
1886 
1887 	dcbx_info = qed_dcbnl_get_dcbx(hwfn, QED_DCBX_REMOTE_MIB);
1888 	if (!dcbx_info)
1889 		return -EINVAL;
1890 
1891 	for (i = 0; i < dcbx_info->remote.params.num_app_entries; i++) {
1892 		if (dcbx_info->remote.params.app_entry[i].ethtype)
1893 			table[i].selector = DCB_APP_IDTYPE_ETHTYPE;
1894 		else
1895 			table[i].selector = DCB_APP_IDTYPE_PORTNUM;
1896 		table[i].priority = dcbx_info->remote.params.app_entry[i].prio;
1897 		table[i].protocol =
1898 		    dcbx_info->remote.params.app_entry[i].proto_id;
1899 	}
1900 
1901 	kfree(dcbx_info);
1902 
1903 	return 0;
1904 }
1905 
1906 static int qed_dcbnl_cee_peer_getpfc(struct qed_dev *cdev, struct cee_pfc *pfc)
1907 {
1908 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
1909 	struct qed_dcbx_get *dcbx_info;
1910 	int i;
1911 
1912 	dcbx_info = qed_dcbnl_get_dcbx(hwfn, QED_DCBX_REMOTE_MIB);
1913 	if (!dcbx_info)
1914 		return -EINVAL;
1915 
1916 	for (i = 0; i < QED_MAX_PFC_PRIORITIES; i++)
1917 		if (dcbx_info->remote.params.pfc.prio[i])
1918 			pfc->pfc_en |= BIT(i);
1919 
1920 	pfc->tcs_supported = dcbx_info->remote.params.pfc.max_tc;
1921 	DP_VERBOSE(hwfn, QED_MSG_DCB, "pfc state = %d tcs_supported = %d\n",
1922 		   pfc->pfc_en, pfc->tcs_supported);
1923 	kfree(dcbx_info);
1924 
1925 	return 0;
1926 }
1927 
1928 static int qed_dcbnl_cee_peer_getpg(struct qed_dev *cdev, struct cee_pg *pg)
1929 {
1930 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
1931 	struct qed_dcbx_get *dcbx_info;
1932 	int i;
1933 
1934 	dcbx_info = qed_dcbnl_get_dcbx(hwfn, QED_DCBX_REMOTE_MIB);
1935 	if (!dcbx_info)
1936 		return -EINVAL;
1937 
1938 	pg->willing = dcbx_info->remote.params.ets_willing;
1939 	for (i = 0; i < QED_MAX_PFC_PRIORITIES; i++) {
1940 		pg->pg_bw[i] = dcbx_info->remote.params.ets_tc_bw_tbl[i];
1941 		pg->prio_pg[i] = dcbx_info->remote.params.ets_pri_tc_tbl[i];
1942 	}
1943 
1944 	DP_VERBOSE(hwfn, QED_MSG_DCB, "willing = %d", pg->willing);
1945 	kfree(dcbx_info);
1946 
1947 	return 0;
1948 }
1949 
1950 static int qed_dcbnl_get_ieee_pfc(struct qed_dev *cdev,
1951 				  struct ieee_pfc *pfc, bool remote)
1952 {
1953 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
1954 	struct qed_dcbx_params *params;
1955 	struct qed_dcbx_get *dcbx_info;
1956 	int rc, i;
1957 
1958 	dcbx_info = qed_dcbnl_get_dcbx(hwfn, QED_DCBX_OPERATIONAL_MIB);
1959 	if (!dcbx_info)
1960 		return -EINVAL;
1961 
1962 	if (!dcbx_info->operational.ieee) {
1963 		DP_INFO(hwfn, "DCBX is not enabled/operational in IEEE mode\n");
1964 		kfree(dcbx_info);
1965 		return -EINVAL;
1966 	}
1967 
1968 	if (remote) {
1969 		memset(dcbx_info, 0, sizeof(*dcbx_info));
1970 		rc = qed_dcbx_query_params(hwfn, dcbx_info,
1971 					   QED_DCBX_REMOTE_MIB);
1972 		if (rc) {
1973 			kfree(dcbx_info);
1974 			return -EINVAL;
1975 		}
1976 
1977 		params = &dcbx_info->remote.params;
1978 	} else {
1979 		params = &dcbx_info->operational.params;
1980 	}
1981 
1982 	pfc->pfc_cap = params->pfc.max_tc;
1983 	pfc->pfc_en = 0;
1984 	for (i = 0; i < QED_MAX_PFC_PRIORITIES; i++)
1985 		if (params->pfc.prio[i])
1986 			pfc->pfc_en |= BIT(i);
1987 
1988 	kfree(dcbx_info);
1989 
1990 	return 0;
1991 }
1992 
1993 static int qed_dcbnl_ieee_getpfc(struct qed_dev *cdev, struct ieee_pfc *pfc)
1994 {
1995 	return qed_dcbnl_get_ieee_pfc(cdev, pfc, false);
1996 }
1997 
1998 static int qed_dcbnl_ieee_setpfc(struct qed_dev *cdev, struct ieee_pfc *pfc)
1999 {
2000 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
2001 	struct qed_dcbx_get *dcbx_info;
2002 	struct qed_dcbx_set dcbx_set;
2003 	struct qed_ptt *ptt;
2004 	int rc, i;
2005 
2006 	dcbx_info = qed_dcbnl_get_dcbx(hwfn, QED_DCBX_OPERATIONAL_MIB);
2007 	if (!dcbx_info)
2008 		return -EINVAL;
2009 
2010 	if (!dcbx_info->operational.ieee) {
2011 		DP_INFO(hwfn, "DCBX is not enabled/operational in IEEE mode\n");
2012 		kfree(dcbx_info);
2013 		return -EINVAL;
2014 	}
2015 
2016 	kfree(dcbx_info);
2017 
2018 	memset(&dcbx_set, 0, sizeof(dcbx_set));
2019 	rc = qed_dcbx_get_config_params(hwfn, &dcbx_set);
2020 	if (rc)
2021 		return -EINVAL;
2022 
2023 	dcbx_set.override_flags |= QED_DCBX_OVERRIDE_PFC_CFG;
2024 	for (i = 0; i < QED_MAX_PFC_PRIORITIES; i++)
2025 		dcbx_set.config.params.pfc.prio[i] = !!(pfc->pfc_en & BIT(i));
2026 
2027 	ptt = qed_ptt_acquire(hwfn);
2028 	if (!ptt)
2029 		return -EINVAL;
2030 
2031 	rc = qed_dcbx_config_params(hwfn, ptt, &dcbx_set, 0);
2032 
2033 	qed_ptt_release(hwfn, ptt);
2034 
2035 	return rc;
2036 }
2037 
2038 static int qed_dcbnl_get_ieee_ets(struct qed_dev *cdev,
2039 				  struct ieee_ets *ets, bool remote)
2040 {
2041 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
2042 	struct qed_dcbx_get *dcbx_info;
2043 	struct qed_dcbx_params *params;
2044 	int rc;
2045 
2046 	dcbx_info = qed_dcbnl_get_dcbx(hwfn, QED_DCBX_OPERATIONAL_MIB);
2047 	if (!dcbx_info)
2048 		return -EINVAL;
2049 
2050 	if (!dcbx_info->operational.ieee) {
2051 		DP_INFO(hwfn, "DCBX is not enabled/operational in IEEE mode\n");
2052 		kfree(dcbx_info);
2053 		return -EINVAL;
2054 	}
2055 
2056 	if (remote) {
2057 		memset(dcbx_info, 0, sizeof(*dcbx_info));
2058 		rc = qed_dcbx_query_params(hwfn, dcbx_info,
2059 					   QED_DCBX_REMOTE_MIB);
2060 		if (rc) {
2061 			kfree(dcbx_info);
2062 			return -EINVAL;
2063 		}
2064 
2065 		params = &dcbx_info->remote.params;
2066 	} else {
2067 		params = &dcbx_info->operational.params;
2068 	}
2069 
2070 	ets->ets_cap = params->max_ets_tc;
2071 	ets->willing = params->ets_willing;
2072 	ets->cbs = params->ets_cbs;
2073 	memcpy(ets->tc_tx_bw, params->ets_tc_bw_tbl, sizeof(ets->tc_tx_bw));
2074 	memcpy(ets->tc_tsa, params->ets_tc_tsa_tbl, sizeof(ets->tc_tsa));
2075 	memcpy(ets->prio_tc, params->ets_pri_tc_tbl, sizeof(ets->prio_tc));
2076 	kfree(dcbx_info);
2077 
2078 	return 0;
2079 }
2080 
2081 static int qed_dcbnl_ieee_getets(struct qed_dev *cdev, struct ieee_ets *ets)
2082 {
2083 	return qed_dcbnl_get_ieee_ets(cdev, ets, false);
2084 }
2085 
2086 static int qed_dcbnl_ieee_setets(struct qed_dev *cdev, struct ieee_ets *ets)
2087 {
2088 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
2089 	struct qed_dcbx_get *dcbx_info;
2090 	struct qed_dcbx_set dcbx_set;
2091 	struct qed_ptt *ptt;
2092 	int rc;
2093 
2094 	dcbx_info = qed_dcbnl_get_dcbx(hwfn, QED_DCBX_OPERATIONAL_MIB);
2095 	if (!dcbx_info)
2096 		return -EINVAL;
2097 
2098 	if (!dcbx_info->operational.ieee) {
2099 		DP_INFO(hwfn, "DCBX is not enabled/operational in IEEE mode\n");
2100 		kfree(dcbx_info);
2101 		return -EINVAL;
2102 	}
2103 
2104 	kfree(dcbx_info);
2105 
2106 	memset(&dcbx_set, 0, sizeof(dcbx_set));
2107 	rc = qed_dcbx_get_config_params(hwfn, &dcbx_set);
2108 	if (rc)
2109 		return -EINVAL;
2110 
2111 	dcbx_set.override_flags |= QED_DCBX_OVERRIDE_ETS_CFG;
2112 	dcbx_set.config.params.max_ets_tc = ets->ets_cap;
2113 	dcbx_set.config.params.ets_willing = ets->willing;
2114 	dcbx_set.config.params.ets_cbs = ets->cbs;
2115 	memcpy(dcbx_set.config.params.ets_tc_bw_tbl, ets->tc_tx_bw,
2116 	       sizeof(ets->tc_tx_bw));
2117 	memcpy(dcbx_set.config.params.ets_tc_tsa_tbl, ets->tc_tsa,
2118 	       sizeof(ets->tc_tsa));
2119 	memcpy(dcbx_set.config.params.ets_pri_tc_tbl, ets->prio_tc,
2120 	       sizeof(ets->prio_tc));
2121 
2122 	ptt = qed_ptt_acquire(hwfn);
2123 	if (!ptt)
2124 		return -EINVAL;
2125 
2126 	rc = qed_dcbx_config_params(hwfn, ptt, &dcbx_set, 0);
2127 
2128 	qed_ptt_release(hwfn, ptt);
2129 
2130 	return rc;
2131 }
2132 
2133 static int
2134 qed_dcbnl_ieee_peer_getets(struct qed_dev *cdev, struct ieee_ets *ets)
2135 {
2136 	return qed_dcbnl_get_ieee_ets(cdev, ets, true);
2137 }
2138 
2139 static int
2140 qed_dcbnl_ieee_peer_getpfc(struct qed_dev *cdev, struct ieee_pfc *pfc)
2141 {
2142 	return qed_dcbnl_get_ieee_pfc(cdev, pfc, true);
2143 }
2144 
2145 static int qed_dcbnl_ieee_getapp(struct qed_dev *cdev, struct dcb_app *app)
2146 {
2147 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
2148 	struct qed_dcbx_get *dcbx_info;
2149 	struct qed_app_entry *entry;
2150 	bool ethtype;
2151 	u8 prio = 0;
2152 	int i;
2153 
2154 	dcbx_info = qed_dcbnl_get_dcbx(hwfn, QED_DCBX_OPERATIONAL_MIB);
2155 	if (!dcbx_info)
2156 		return -EINVAL;
2157 
2158 	if (!dcbx_info->operational.ieee) {
2159 		DP_INFO(hwfn, "DCBX is not enabled/operational in IEEE mode\n");
2160 		kfree(dcbx_info);
2161 		return -EINVAL;
2162 	}
2163 
2164 	/* ieee defines the selector field value for ethertype to be 1 */
2165 	ethtype = !!((app->selector - 1) == DCB_APP_IDTYPE_ETHTYPE);
2166 	for (i = 0; i < QED_DCBX_MAX_APP_PROTOCOL; i++) {
2167 		entry = &dcbx_info->operational.params.app_entry[i];
2168 		if ((entry->ethtype == ethtype) &&
2169 		    (entry->proto_id == app->protocol)) {
2170 			prio = entry->prio;
2171 			break;
2172 		}
2173 	}
2174 
2175 	if (i == QED_DCBX_MAX_APP_PROTOCOL) {
2176 		DP_ERR(cdev, "App entry (%d, %d) not found\n", app->selector,
2177 		       app->protocol);
2178 		kfree(dcbx_info);
2179 		return -EINVAL;
2180 	}
2181 
2182 	app->priority = ffs(prio) - 1;
2183 
2184 	kfree(dcbx_info);
2185 
2186 	return 0;
2187 }
2188 
2189 static int qed_dcbnl_ieee_setapp(struct qed_dev *cdev, struct dcb_app *app)
2190 {
2191 	struct qed_hwfn *hwfn = QED_LEADING_HWFN(cdev);
2192 	struct qed_dcbx_get *dcbx_info;
2193 	struct qed_dcbx_set dcbx_set;
2194 	struct qed_app_entry *entry;
2195 	struct qed_ptt *ptt;
2196 	bool ethtype;
2197 	int rc, i;
2198 
2199 	if (app->priority < 0 || app->priority >= QED_MAX_PFC_PRIORITIES) {
2200 		DP_INFO(hwfn, "Invalid priority %d\n", app->priority);
2201 		return -EINVAL;
2202 	}
2203 
2204 	dcbx_info = qed_dcbnl_get_dcbx(hwfn, QED_DCBX_OPERATIONAL_MIB);
2205 	if (!dcbx_info)
2206 		return -EINVAL;
2207 
2208 	if (!dcbx_info->operational.ieee) {
2209 		DP_INFO(hwfn, "DCBX is not enabled/operational in IEEE mode\n");
2210 		kfree(dcbx_info);
2211 		return -EINVAL;
2212 	}
2213 
2214 	kfree(dcbx_info);
2215 
2216 	memset(&dcbx_set, 0, sizeof(dcbx_set));
2217 	rc = qed_dcbx_get_config_params(hwfn, &dcbx_set);
2218 	if (rc)
2219 		return -EINVAL;
2220 
2221 	/* ieee defines the selector field value for ethertype to be 1 */
2222 	ethtype = !!((app->selector - 1) == DCB_APP_IDTYPE_ETHTYPE);
2223 	for (i = 0; i < QED_DCBX_MAX_APP_PROTOCOL; i++) {
2224 		entry = &dcbx_set.config.params.app_entry[i];
2225 		if ((entry->ethtype == ethtype) &&
2226 		    (entry->proto_id == app->protocol))
2227 			break;
2228 		/* First empty slot */
2229 		if (!entry->proto_id) {
2230 			dcbx_set.config.params.num_app_entries++;
2231 			break;
2232 		}
2233 	}
2234 
2235 	if (i == QED_DCBX_MAX_APP_PROTOCOL) {
2236 		DP_ERR(cdev, "App table is full\n");
2237 		return -EBUSY;
2238 	}
2239 
2240 	dcbx_set.override_flags |= QED_DCBX_OVERRIDE_APP_CFG;
2241 	dcbx_set.config.params.app_entry[i].ethtype = ethtype;
2242 	dcbx_set.config.params.app_entry[i].proto_id = app->protocol;
2243 	dcbx_set.config.params.app_entry[i].prio = BIT(app->priority);
2244 
2245 	ptt = qed_ptt_acquire(hwfn);
2246 	if (!ptt)
2247 		return -EBUSY;
2248 
2249 	rc = qed_dcbx_config_params(hwfn, ptt, &dcbx_set, 0);
2250 
2251 	qed_ptt_release(hwfn, ptt);
2252 
2253 	return rc;
2254 }
2255 
2256 const struct qed_eth_dcbnl_ops qed_dcbnl_ops_pass = {
2257 	.getstate = qed_dcbnl_getstate,
2258 	.setstate = qed_dcbnl_setstate,
2259 	.getpgtccfgtx = qed_dcbnl_getpgtccfgtx,
2260 	.getpgbwgcfgtx = qed_dcbnl_getpgbwgcfgtx,
2261 	.getpgtccfgrx = qed_dcbnl_getpgtccfgrx,
2262 	.getpgbwgcfgrx = qed_dcbnl_getpgbwgcfgrx,
2263 	.getpfccfg = qed_dcbnl_getpfccfg,
2264 	.setpfccfg = qed_dcbnl_setpfccfg,
2265 	.getcap = qed_dcbnl_getcap,
2266 	.getnumtcs = qed_dcbnl_getnumtcs,
2267 	.getpfcstate = qed_dcbnl_getpfcstate,
2268 	.getdcbx = qed_dcbnl_getdcbx,
2269 	.setpgtccfgtx = qed_dcbnl_setpgtccfgtx,
2270 	.setpgtccfgrx = qed_dcbnl_setpgtccfgrx,
2271 	.setpgbwgcfgtx = qed_dcbnl_setpgbwgcfgtx,
2272 	.setpgbwgcfgrx = qed_dcbnl_setpgbwgcfgrx,
2273 	.setall = qed_dcbnl_setall,
2274 	.setnumtcs = qed_dcbnl_setnumtcs,
2275 	.setpfcstate = qed_dcbnl_setpfcstate,
2276 	.setapp = qed_dcbnl_setapp,
2277 	.setdcbx = qed_dcbnl_setdcbx,
2278 	.setfeatcfg = qed_dcbnl_setfeatcfg,
2279 	.getfeatcfg = qed_dcbnl_getfeatcfg,
2280 	.getapp = qed_dcbnl_getapp,
2281 	.peer_getappinfo = qed_dcbnl_peer_getappinfo,
2282 	.peer_getapptable = qed_dcbnl_peer_getapptable,
2283 	.cee_peer_getpfc = qed_dcbnl_cee_peer_getpfc,
2284 	.cee_peer_getpg = qed_dcbnl_cee_peer_getpg,
2285 	.ieee_getpfc = qed_dcbnl_ieee_getpfc,
2286 	.ieee_setpfc = qed_dcbnl_ieee_setpfc,
2287 	.ieee_getets = qed_dcbnl_ieee_getets,
2288 	.ieee_setets = qed_dcbnl_ieee_setets,
2289 	.ieee_peer_getpfc = qed_dcbnl_ieee_peer_getpfc,
2290 	.ieee_peer_getets = qed_dcbnl_ieee_peer_getets,
2291 	.ieee_getapp = qed_dcbnl_ieee_getapp,
2292 	.ieee_setapp = qed_dcbnl_ieee_setapp,
2293 };
2294 
2295 #endif
2296