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