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