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