102774d8113687e7b1dee323e578202ac69476c0
[dpdk.git] / drivers / net / qede / base / ecore_dcbx.c
1 /*
2  * Copyright (c) 2016 QLogic Corporation.
3  * All rights reserved.
4  * www.qlogic.com
5  *
6  * See LICENSE.qede_pmd for copyright and licensing details.
7  */
8
9 #include "bcm_osal.h"
10 #include "ecore.h"
11 #include "ecore_sp_commands.h"
12 #include "ecore_dcbx.h"
13 #include "ecore_cxt.h"
14 #include "ecore_gtt_reg_addr.h"
15 #include "ecore_iro.h"
16 #include "ecore_iov_api.h"
17
18 #define ECORE_DCBX_MAX_MIB_READ_TRY     (100)
19 #define ECORE_ETH_TYPE_DEFAULT          (0)
20
21 #define ECORE_DCBX_INVALID_PRIORITY     0xFF
22
23 /* Get Traffic Class from priority traffic class table, 4 bits represent
24  * the traffic class corresponding to the priority.
25  */
26 #define ECORE_DCBX_PRIO2TC(prio_tc_tbl, prio) \
27                 ((u32)(prio_tc_tbl >> ((7 - prio) * 4)) & 0x7)
28
29 static bool ecore_dcbx_app_ethtype(u32 app_info_bitmap)
30 {
31         return !!(ECORE_MFW_GET_FIELD(app_info_bitmap, DCBX_APP_SF) ==
32                   DCBX_APP_SF_ETHTYPE);
33 }
34
35 static bool ecore_dcbx_ieee_app_ethtype(u32 app_info_bitmap)
36 {
37         u8 mfw_val = ECORE_MFW_GET_FIELD(app_info_bitmap, DCBX_APP_SF_IEEE);
38
39         /* Old MFW */
40         if (mfw_val == DCBX_APP_SF_IEEE_RESERVED)
41                 return ecore_dcbx_app_ethtype(app_info_bitmap);
42
43         return !!(mfw_val == DCBX_APP_SF_IEEE_ETHTYPE);
44 }
45
46 static bool ecore_dcbx_app_port(u32 app_info_bitmap)
47 {
48         return !!(ECORE_MFW_GET_FIELD(app_info_bitmap, DCBX_APP_SF) ==
49                   DCBX_APP_SF_PORT);
50 }
51
52 static bool ecore_dcbx_ieee_app_port(u32 app_info_bitmap, u8 type)
53 {
54         u8 mfw_val = ECORE_MFW_GET_FIELD(app_info_bitmap, DCBX_APP_SF_IEEE);
55
56         /* Old MFW */
57         if (mfw_val == DCBX_APP_SF_IEEE_RESERVED)
58                 return ecore_dcbx_app_port(app_info_bitmap);
59
60         return !!(mfw_val == type || mfw_val == DCBX_APP_SF_IEEE_TCP_UDP_PORT);
61 }
62
63 static bool ecore_dcbx_default_tlv(u32 app_info_bitmap, u16 proto_id, bool ieee)
64 {
65         bool ethtype;
66
67         if (ieee)
68                 ethtype = ecore_dcbx_ieee_app_ethtype(app_info_bitmap);
69         else
70                 ethtype = ecore_dcbx_app_ethtype(app_info_bitmap);
71
72         return !!(ethtype && (proto_id == ECORE_ETH_TYPE_DEFAULT));
73 }
74
75 static void
76 ecore_dcbx_dp_protocol(struct ecore_hwfn *p_hwfn,
77                        struct ecore_dcbx_results *p_data)
78 {
79         enum dcbx_protocol_type id;
80         int i;
81
82         DP_VERBOSE(p_hwfn, ECORE_MSG_DCB, "DCBX negotiated: %d\n",
83                    p_data->dcbx_enabled);
84
85         for (i = 0; i < OSAL_ARRAY_SIZE(ecore_dcbx_app_update); i++) {
86                 id = ecore_dcbx_app_update[i].id;
87
88                 DP_VERBOSE(p_hwfn, ECORE_MSG_DCB,
89                            "%s info: update %d, enable %d, prio %d, tc %d,"
90                            " num_active_tc %d dscp_enable = %d dscp_val = %d\n",
91                            ecore_dcbx_app_update[i].name,
92                            p_data->arr[id].update,
93                            p_data->arr[id].enable, p_data->arr[id].priority,
94                            p_data->arr[id].tc, p_hwfn->hw_info.num_active_tc,
95                            p_data->arr[id].dscp_enable,
96                            p_data->arr[id].dscp_val);
97         }
98 }
99
100 void
101 ecore_dcbx_set_params(struct ecore_dcbx_results *p_data,
102                       struct ecore_hwfn *p_hwfn,
103                       bool enable, u8 prio, u8 tc,
104                       enum dcbx_protocol_type type,
105                       enum ecore_pci_personality personality)
106 {
107         struct ecore_dcbx_dscp_params *dscp = &p_hwfn->p_dcbx_info->get.dscp;
108
109         /* PF update ramrod data */
110         p_data->arr[type].enable = enable;
111         p_data->arr[type].priority = prio;
112         p_data->arr[type].tc = tc;
113         p_data->arr[type].dscp_enable = dscp->enabled;
114         if (p_data->arr[type].dscp_enable) {
115                 u8 i;
116
117                 for (i = 0; i < ECORE_DCBX_DSCP_SIZE; i++)
118                         if (prio == dscp->dscp_pri_map[i]) {
119                                 p_data->arr[type].dscp_val = i;
120                                 break;
121                         }
122         }
123
124         if (enable && p_data->arr[type].dscp_enable)
125                 p_data->arr[type].update = UPDATE_DCB_DSCP;
126         else if (enable)
127                 p_data->arr[type].update = UPDATE_DCB;
128         else
129                 p_data->arr[type].update = DONT_UPDATE_DCB_DSCP;
130
131         /* QM reconf data */
132         if (p_hwfn->hw_info.personality == personality) {
133                 p_hwfn->hw_info.offload_tc = tc;
134                 if (personality == ECORE_PCI_ISCSI)
135                         p_hwfn->hw_info.ooo_tc = DCBX_ISCSI_OOO_TC;
136         }
137 }
138
139 /* Update app protocol data and hw_info fields with the TLV info */
140 static void
141 ecore_dcbx_update_app_info(struct ecore_dcbx_results *p_data,
142                            struct ecore_hwfn *p_hwfn,
143                            bool enable, u8 prio, u8 tc,
144                            enum dcbx_protocol_type type)
145 {
146         enum ecore_pci_personality personality;
147         enum dcbx_protocol_type id;
148         const char *name;       /* @DPDK */
149         int i;
150
151         for (i = 0; i < OSAL_ARRAY_SIZE(ecore_dcbx_app_update); i++) {
152                 id = ecore_dcbx_app_update[i].id;
153
154                 if (type != id)
155                         continue;
156
157                 personality = ecore_dcbx_app_update[i].personality;
158                 name = ecore_dcbx_app_update[i].name;
159
160                 ecore_dcbx_set_params(p_data, p_hwfn, enable,
161                                       prio, tc, type, personality);
162         }
163 }
164
165 static enum _ecore_status_t
166 ecore_dcbx_get_app_priority(u8 pri_bitmap, u8 *priority)
167 {
168         u32 pri_mask, pri = ECORE_MAX_PFC_PRIORITIES;
169         u32 index = ECORE_MAX_PFC_PRIORITIES - 1;
170         enum _ecore_status_t rc = ECORE_SUCCESS;
171
172         /* Bitmap 1 corresponds to priority 0, return priority 0 */
173         if (pri_bitmap == 1) {
174                 *priority = 0;
175                 return rc;
176         }
177
178         /* Choose the highest priority */
179         while ((pri == ECORE_MAX_PFC_PRIORITIES) && index) {
180                 pri_mask = 1 << index;
181                 if (pri_bitmap & pri_mask)
182                         pri = index;
183                 index--;
184         }
185
186         if (pri < ECORE_MAX_PFC_PRIORITIES)
187                 *priority = (u8)pri;
188         else
189                 rc = ECORE_INVAL;
190
191         return rc;
192 }
193
194 static bool
195 ecore_dcbx_get_app_protocol_type(struct ecore_hwfn *p_hwfn,
196                                  u32 app_prio_bitmap, u16 id,
197                                  enum dcbx_protocol_type *type, bool ieee)
198 {
199         if (ecore_dcbx_default_tlv(app_prio_bitmap, id, ieee)) {
200                 *type = DCBX_PROTOCOL_ETH;
201         } else {
202                 *type = DCBX_MAX_PROTOCOL_TYPE;
203                 DP_ERR(p_hwfn,
204                        "No action required, App TLV id = 0x%x"
205                        " app_prio_bitmap = 0x%x\n",
206                        id, app_prio_bitmap);
207                 return false;
208         }
209
210         return true;
211 }
212
213 /*  Parse app TLV's to update TC information in hw_info structure for
214  * reconfiguring QM. Get protocol specific data for PF update ramrod command.
215  */
216 static enum _ecore_status_t
217 ecore_dcbx_process_tlv(struct ecore_hwfn *p_hwfn,
218                        struct ecore_dcbx_results *p_data,
219                        struct dcbx_app_priority_entry *p_tbl, u32 pri_tc_tbl,
220                        int count, u8 dcbx_version)
221 {
222         enum dcbx_protocol_type type;
223         u8 tc, priority_map;
224         bool enable, ieee;
225         u16 protocol_id;
226         u8 priority;
227         enum _ecore_status_t rc = ECORE_SUCCESS;
228         int i;
229
230         DP_VERBOSE(p_hwfn, ECORE_MSG_DCB,
231                    "Num APP entries = %d pri_tc_tbl = 0x%x dcbx_version = %u\n",
232                    count, pri_tc_tbl, dcbx_version);
233
234         ieee = (dcbx_version == DCBX_CONFIG_VERSION_IEEE);
235         /* Parse APP TLV */
236         for (i = 0; i < count; i++) {
237                 protocol_id = ECORE_MFW_GET_FIELD(p_tbl[i].entry,
238                                                   DCBX_APP_PROTOCOL_ID);
239                 priority_map = ECORE_MFW_GET_FIELD(p_tbl[i].entry,
240                                                    DCBX_APP_PRI_MAP);
241                 DP_VERBOSE(p_hwfn, ECORE_MSG_DCB, "Id = 0x%x pri_map = %u\n",
242                            protocol_id, priority_map);
243                 rc = ecore_dcbx_get_app_priority(priority_map, &priority);
244                 if (rc == ECORE_INVAL) {
245                         DP_ERR(p_hwfn, "Invalid priority\n");
246                         return ECORE_INVAL;
247                 }
248
249                 tc = ECORE_DCBX_PRIO2TC(pri_tc_tbl, priority);
250                 if (ecore_dcbx_get_app_protocol_type(p_hwfn, p_tbl[i].entry,
251                                                      protocol_id, &type,
252                                                      ieee)) {
253                         /* ETH always have the enable bit reset, as it gets
254                          * vlan information per packet. For other protocols,
255                          * should be set according to the dcbx_enabled
256                          * indication, but we only got here if there was an
257                          * app tlv for the protocol, so dcbx must be enabled.
258                          */
259                         enable = !(type == DCBX_PROTOCOL_ETH);
260
261                         ecore_dcbx_update_app_info(p_data, p_hwfn, enable,
262                                                    priority, tc, type);
263                 }
264         }
265         /* Update ramrod protocol data and hw_info fields
266          * with default info when corresponding APP TLV's are not detected.
267          * The enabled field has a different logic for ethernet as only for
268          * ethernet dcb should disabled by default, as the information arrives
269          * from the OS (unless an explicit app tlv was present).
270          */
271         tc = p_data->arr[DCBX_PROTOCOL_ETH].tc;
272         priority = p_data->arr[DCBX_PROTOCOL_ETH].priority;
273         for (type = 0; type < DCBX_MAX_PROTOCOL_TYPE; type++) {
274                 if (p_data->arr[type].update)
275                         continue;
276
277                 enable = (type == DCBX_PROTOCOL_ETH) ? false : !!dcbx_version;
278                 ecore_dcbx_update_app_info(p_data, p_hwfn, enable,
279                                            priority, tc, type);
280         }
281
282         return ECORE_SUCCESS;
283 }
284
285 /* Parse app TLV's to update TC information in hw_info structure for
286  * reconfiguring QM. Get protocol specific data for PF update ramrod command.
287  */
288 static enum _ecore_status_t
289 ecore_dcbx_process_mib_info(struct ecore_hwfn *p_hwfn)
290 {
291         struct dcbx_app_priority_feature *p_app;
292         enum _ecore_status_t rc = ECORE_SUCCESS;
293         struct ecore_dcbx_results data = { 0 };
294         struct dcbx_app_priority_entry *p_tbl;
295         struct dcbx_ets_feature *p_ets;
296         struct ecore_hw_info *p_info;
297         u32 pri_tc_tbl, flags;
298         u8 dcbx_version;
299         int num_entries;
300
301         flags = p_hwfn->p_dcbx_info->operational.flags;
302         dcbx_version = ECORE_MFW_GET_FIELD(flags, DCBX_CONFIG_VERSION);
303
304         p_app = &p_hwfn->p_dcbx_info->operational.features.app;
305         p_tbl = p_app->app_pri_tbl;
306
307         p_ets = &p_hwfn->p_dcbx_info->operational.features.ets;
308         pri_tc_tbl = p_ets->pri_tc_tbl[0];
309
310         p_info = &p_hwfn->hw_info;
311         num_entries = ECORE_MFW_GET_FIELD(p_app->flags, DCBX_APP_NUM_ENTRIES);
312
313         rc = ecore_dcbx_process_tlv(p_hwfn, &data, p_tbl, pri_tc_tbl,
314                                     num_entries, dcbx_version);
315         if (rc != ECORE_SUCCESS)
316                 return rc;
317
318         p_info->num_active_tc = ECORE_MFW_GET_FIELD(p_ets->flags,
319                                                     DCBX_ETS_MAX_TCS);
320         data.pf_id = p_hwfn->rel_pf_id;
321         data.dcbx_enabled = !!dcbx_version;
322
323         ecore_dcbx_dp_protocol(p_hwfn, &data);
324
325         OSAL_MEMCPY(&p_hwfn->p_dcbx_info->results, &data,
326                     sizeof(struct ecore_dcbx_results));
327
328         return ECORE_SUCCESS;
329 }
330
331 static enum _ecore_status_t
332 ecore_dcbx_copy_mib(struct ecore_hwfn *p_hwfn,
333                     struct ecore_ptt *p_ptt,
334                     struct ecore_dcbx_mib_meta_data *p_data,
335                     enum ecore_mib_read_type type)
336 {
337         enum _ecore_status_t rc = ECORE_SUCCESS;
338         u32 prefix_seq_num, suffix_seq_num;
339         int read_count = 0;
340
341         /* The data is considered to be valid only if both sequence numbers are
342          * the same.
343          */
344         do {
345                 if (type == ECORE_DCBX_REMOTE_LLDP_MIB) {
346                         ecore_memcpy_from(p_hwfn, p_ptt, p_data->lldp_remote,
347                                           p_data->addr, p_data->size);
348                         prefix_seq_num = p_data->lldp_remote->prefix_seq_num;
349                         suffix_seq_num = p_data->lldp_remote->suffix_seq_num;
350                 } else {
351                         ecore_memcpy_from(p_hwfn, p_ptt, p_data->mib,
352                                           p_data->addr, p_data->size);
353                         prefix_seq_num = p_data->mib->prefix_seq_num;
354                         suffix_seq_num = p_data->mib->suffix_seq_num;
355                 }
356                 read_count++;
357
358                 DP_VERBOSE(p_hwfn, ECORE_MSG_DCB,
359                            "mib type = %d, try count = %d prefix seq num  ="
360                            " %d suffix seq num = %d\n",
361                            type, read_count, prefix_seq_num, suffix_seq_num);
362         } while ((prefix_seq_num != suffix_seq_num) &&
363                  (read_count < ECORE_DCBX_MAX_MIB_READ_TRY));
364
365         if (read_count >= ECORE_DCBX_MAX_MIB_READ_TRY) {
366                 DP_ERR(p_hwfn,
367                        "MIB read err, mib type = %d, try count ="
368                        " %d prefix seq num = %d suffix seq num = %d\n",
369                        type, read_count, prefix_seq_num, suffix_seq_num);
370                 rc = ECORE_IO;
371         }
372
373         return rc;
374 }
375
376 static void
377 ecore_dcbx_get_priority_info(struct ecore_hwfn *p_hwfn,
378                              struct ecore_dcbx_app_prio *p_prio,
379                              struct ecore_dcbx_results *p_results)
380 {
381         u8 val;
382
383         if (p_results->arr[DCBX_PROTOCOL_ETH].update &&
384             p_results->arr[DCBX_PROTOCOL_ETH].enable)
385                 p_prio->eth = p_results->arr[DCBX_PROTOCOL_ETH].priority;
386
387         DP_VERBOSE(p_hwfn, ECORE_MSG_DCB,
388                    "Priorities: eth %d\n",
389                    p_prio->eth);
390 }
391
392 static void
393 ecore_dcbx_get_app_data(struct ecore_hwfn *p_hwfn,
394                         struct dcbx_app_priority_feature *p_app,
395                         struct dcbx_app_priority_entry *p_tbl,
396                         struct ecore_dcbx_params *p_params, bool ieee)
397 {
398         struct ecore_app_entry *entry;
399         u8 pri_map;
400         int i;
401
402         p_params->app_willing = ECORE_MFW_GET_FIELD(p_app->flags,
403                                                     DCBX_APP_WILLING);
404         p_params->app_valid = ECORE_MFW_GET_FIELD(p_app->flags,
405                                                   DCBX_APP_ENABLED);
406         p_params->app_error = ECORE_MFW_GET_FIELD(p_app->flags, DCBX_APP_ERROR);
407         p_params->num_app_entries = ECORE_MFW_GET_FIELD(p_app->flags,
408                                                         DCBX_APP_NUM_ENTRIES);
409         for (i = 0; i < DCBX_MAX_APP_PROTOCOL; i++) {
410                 entry = &p_params->app_entry[i];
411                 if (ieee) {
412                         u8 sf_ieee;
413                         u32 val;
414
415                         sf_ieee = ECORE_MFW_GET_FIELD(p_tbl[i].entry,
416                                                       DCBX_APP_SF_IEEE);
417                         switch (sf_ieee) {
418                         case DCBX_APP_SF_IEEE_RESERVED:
419                                 /* Old MFW */
420                                 val = ECORE_MFW_GET_FIELD(p_tbl[i].entry,
421                                                             DCBX_APP_SF);
422                                 entry->sf_ieee = val ?
423                                         ECORE_DCBX_SF_IEEE_TCP_UDP_PORT :
424                                         ECORE_DCBX_SF_IEEE_ETHTYPE;
425                                 break;
426                         case DCBX_APP_SF_IEEE_ETHTYPE:
427                                 entry->sf_ieee = ECORE_DCBX_SF_IEEE_ETHTYPE;
428                                 break;
429                         case DCBX_APP_SF_IEEE_TCP_PORT:
430                                 entry->sf_ieee = ECORE_DCBX_SF_IEEE_TCP_PORT;
431                                 break;
432                         case DCBX_APP_SF_IEEE_UDP_PORT:
433                                 entry->sf_ieee = ECORE_DCBX_SF_IEEE_UDP_PORT;
434                                 break;
435                         case DCBX_APP_SF_IEEE_TCP_UDP_PORT:
436                                 entry->sf_ieee =
437                                                 ECORE_DCBX_SF_IEEE_TCP_UDP_PORT;
438                                 break;
439                         }
440                 } else {
441                         entry->ethtype = !(ECORE_MFW_GET_FIELD(p_tbl[i].entry,
442                                                                DCBX_APP_SF));
443                 }
444
445                 pri_map = ECORE_MFW_GET_FIELD(p_tbl[i].entry, DCBX_APP_PRI_MAP);
446                 ecore_dcbx_get_app_priority(pri_map, &entry->prio);
447                 entry->proto_id = ECORE_MFW_GET_FIELD(p_tbl[i].entry,
448                                                       DCBX_APP_PROTOCOL_ID);
449                 ecore_dcbx_get_app_protocol_type(p_hwfn, p_tbl[i].entry,
450                                                  entry->proto_id,
451                                                  &entry->proto_type, ieee);
452         }
453
454         DP_VERBOSE(p_hwfn, ECORE_MSG_DCB,
455                    "APP params: willing %d, valid %d error = %d\n",
456                    p_params->app_willing, p_params->app_valid,
457                    p_params->app_error);
458 }
459
460 static void
461 ecore_dcbx_get_pfc_data(struct ecore_hwfn *p_hwfn,
462                         u32 pfc, struct ecore_dcbx_params *p_params)
463 {
464         u8 pfc_map;
465
466         p_params->pfc.willing = ECORE_MFW_GET_FIELD(pfc, DCBX_PFC_WILLING);
467         p_params->pfc.max_tc = ECORE_MFW_GET_FIELD(pfc, DCBX_PFC_CAPS);
468         p_params->pfc.enabled = ECORE_MFW_GET_FIELD(pfc, DCBX_PFC_ENABLED);
469         pfc_map = ECORE_MFW_GET_FIELD(pfc, DCBX_PFC_PRI_EN_BITMAP);
470         p_params->pfc.prio[0] = !!(pfc_map & DCBX_PFC_PRI_EN_BITMAP_PRI_0);
471         p_params->pfc.prio[1] = !!(pfc_map & DCBX_PFC_PRI_EN_BITMAP_PRI_1);
472         p_params->pfc.prio[2] = !!(pfc_map & DCBX_PFC_PRI_EN_BITMAP_PRI_2);
473         p_params->pfc.prio[3] = !!(pfc_map & DCBX_PFC_PRI_EN_BITMAP_PRI_3);
474         p_params->pfc.prio[4] = !!(pfc_map & DCBX_PFC_PRI_EN_BITMAP_PRI_4);
475         p_params->pfc.prio[5] = !!(pfc_map & DCBX_PFC_PRI_EN_BITMAP_PRI_5);
476         p_params->pfc.prio[6] = !!(pfc_map & DCBX_PFC_PRI_EN_BITMAP_PRI_6);
477         p_params->pfc.prio[7] = !!(pfc_map & DCBX_PFC_PRI_EN_BITMAP_PRI_7);
478
479         DP_VERBOSE(p_hwfn, ECORE_MSG_DCB,
480                    "PFC params: willing %d, pfc_bitmap %u max_tc = %u enabled = %d\n",
481                    p_params->pfc.willing, pfc_map, p_params->pfc.max_tc,
482                    p_params->pfc.enabled);
483 }
484
485 static void
486 ecore_dcbx_get_ets_data(struct ecore_hwfn *p_hwfn,
487                         struct dcbx_ets_feature *p_ets,
488                         struct ecore_dcbx_params *p_params)
489 {
490         u32 bw_map[2], tsa_map[2], pri_map;
491         int i;
492
493         p_params->ets_willing = ECORE_MFW_GET_FIELD(p_ets->flags,
494                                                     DCBX_ETS_WILLING);
495         p_params->ets_enabled = ECORE_MFW_GET_FIELD(p_ets->flags,
496                                                     DCBX_ETS_ENABLED);
497         p_params->ets_cbs = ECORE_MFW_GET_FIELD(p_ets->flags, DCBX_ETS_CBS);
498         p_params->max_ets_tc = ECORE_MFW_GET_FIELD(p_ets->flags,
499                                                    DCBX_ETS_MAX_TCS);
500         DP_VERBOSE(p_hwfn, ECORE_MSG_DCB,
501                    "ETS params: willing %d, enabled = %d ets_cbs %d pri_tc_tbl_0 %x max_ets_tc %d\n",
502                    p_params->ets_willing, p_params->ets_enabled,
503                    p_params->ets_cbs, p_ets->pri_tc_tbl[0],
504                    p_params->max_ets_tc);
505
506         /* 8 bit tsa and bw data corresponding to each of the 8 TC's are
507          * encoded in a type u32 array of size 2.
508          */
509         bw_map[0] = OSAL_BE32_TO_CPU(p_ets->tc_bw_tbl[0]);
510         bw_map[1] = OSAL_BE32_TO_CPU(p_ets->tc_bw_tbl[1]);
511         tsa_map[0] = OSAL_BE32_TO_CPU(p_ets->tc_tsa_tbl[0]);
512         tsa_map[1] = OSAL_BE32_TO_CPU(p_ets->tc_tsa_tbl[1]);
513         pri_map = p_ets->pri_tc_tbl[0];
514         for (i = 0; i < ECORE_MAX_PFC_PRIORITIES; i++) {
515                 p_params->ets_tc_bw_tbl[i] = ((u8 *)bw_map)[i];
516                 p_params->ets_tc_tsa_tbl[i] = ((u8 *)tsa_map)[i];
517                 p_params->ets_pri_tc_tbl[i] = ECORE_DCBX_PRIO2TC(pri_map, i);
518                 DP_VERBOSE(p_hwfn, ECORE_MSG_DCB,
519                            "elem %d  bw_tbl %x tsa_tbl %x\n",
520                            i, p_params->ets_tc_bw_tbl[i],
521                            p_params->ets_tc_tsa_tbl[i]);
522         }
523 }
524
525 static void
526 ecore_dcbx_get_common_params(struct ecore_hwfn *p_hwfn,
527                              struct dcbx_app_priority_feature *p_app,
528                              struct dcbx_app_priority_entry *p_tbl,
529                              struct dcbx_ets_feature *p_ets,
530                              u32 pfc, struct ecore_dcbx_params *p_params,
531                              bool ieee)
532 {
533         ecore_dcbx_get_app_data(p_hwfn, p_app, p_tbl, p_params, ieee);
534         ecore_dcbx_get_ets_data(p_hwfn, p_ets, p_params);
535         ecore_dcbx_get_pfc_data(p_hwfn, pfc, p_params);
536 }
537
538 static void
539 ecore_dcbx_get_local_params(struct ecore_hwfn *p_hwfn,
540                             struct ecore_ptt *p_ptt,
541                             struct ecore_dcbx_get *params)
542 {
543         struct dcbx_features *p_feat;
544
545         p_feat = &p_hwfn->p_dcbx_info->local_admin.features;
546         ecore_dcbx_get_common_params(p_hwfn, &p_feat->app,
547                                      p_feat->app.app_pri_tbl, &p_feat->ets,
548                                      p_feat->pfc, &params->local.params, false);
549         params->local.valid = true;
550 }
551
552 static void
553 ecore_dcbx_get_remote_params(struct ecore_hwfn *p_hwfn,
554                              struct ecore_ptt *p_ptt,
555                              struct ecore_dcbx_get *params)
556 {
557         struct dcbx_features *p_feat;
558
559         p_feat = &p_hwfn->p_dcbx_info->remote.features;
560         ecore_dcbx_get_common_params(p_hwfn, &p_feat->app,
561                                      p_feat->app.app_pri_tbl, &p_feat->ets,
562                                      p_feat->pfc, &params->remote.params,
563                                      false);
564         params->remote.valid = true;
565 }
566
567 static enum _ecore_status_t
568 ecore_dcbx_get_operational_params(struct ecore_hwfn *p_hwfn,
569                                   struct ecore_ptt *p_ptt,
570                                   struct ecore_dcbx_get *params)
571 {
572         struct ecore_dcbx_operational_params *p_operational;
573         struct ecore_dcbx_results *p_results;
574         struct dcbx_features *p_feat;
575         bool enabled, err;
576         u32 flags;
577         bool val;
578
579         flags = p_hwfn->p_dcbx_info->operational.flags;
580
581         /* If DCBx version is non zero, then negotiation
582          * was successfuly performed
583          */
584         p_operational = &params->operational;
585         enabled = !!(ECORE_MFW_GET_FIELD(flags, DCBX_CONFIG_VERSION) !=
586                      DCBX_CONFIG_VERSION_DISABLED);
587         if (!enabled) {
588                 p_operational->enabled = enabled;
589                 p_operational->valid = false;
590                 DP_VERBOSE(p_hwfn, ECORE_MSG_DCB, "Dcbx is disabled\n");
591                 return ECORE_INVAL;
592         }
593
594         p_feat = &p_hwfn->p_dcbx_info->operational.features;
595         p_results = &p_hwfn->p_dcbx_info->results;
596
597         val = !!(ECORE_MFW_GET_FIELD(flags, DCBX_CONFIG_VERSION) ==
598                  DCBX_CONFIG_VERSION_IEEE);
599         p_operational->ieee = val;
600
601         val = !!(ECORE_MFW_GET_FIELD(flags, DCBX_CONFIG_VERSION) ==
602                  DCBX_CONFIG_VERSION_CEE);
603         p_operational->cee = val;
604
605         val = !!(ECORE_MFW_GET_FIELD(flags, DCBX_CONFIG_VERSION) ==
606                  DCBX_CONFIG_VERSION_STATIC);
607         p_operational->local = val;
608
609         DP_VERBOSE(p_hwfn, ECORE_MSG_DCB,
610                    "Version support: ieee %d, cee %d, static %d\n",
611                    p_operational->ieee, p_operational->cee,
612                    p_operational->local);
613
614         ecore_dcbx_get_common_params(p_hwfn, &p_feat->app,
615                                      p_feat->app.app_pri_tbl, &p_feat->ets,
616                                      p_feat->pfc, &params->operational.params,
617                                      p_operational->ieee);
618         ecore_dcbx_get_priority_info(p_hwfn, &p_operational->app_prio,
619                                      p_results);
620         err = ECORE_MFW_GET_FIELD(p_feat->app.flags, DCBX_APP_ERROR);
621         p_operational->err = err;
622         p_operational->enabled = enabled;
623         p_operational->valid = true;
624
625         return ECORE_SUCCESS;
626 }
627
628 static void
629 ecore_dcbx_get_dscp_params(struct ecore_hwfn *p_hwfn,
630                            struct ecore_ptt *p_ptt,
631                            struct ecore_dcbx_get *params)
632 {
633         struct ecore_dcbx_dscp_params *p_dscp;
634         struct dcb_dscp_map *p_dscp_map;
635         int i, j, entry;
636         u32 pri_map;
637
638         p_dscp = &params->dscp;
639         p_dscp_map = &p_hwfn->p_dcbx_info->dscp_map;
640         p_dscp->enabled = ECORE_MFW_GET_FIELD(p_dscp_map->flags,
641                                               DCB_DSCP_ENABLE);
642         /* MFW encodes 64 dscp entries into 8 element array of u32 entries,
643          * where each entry holds the 4bit priority map for 8 dscp entries.
644          */
645         for (i = 0, entry = 0; i < ECORE_DCBX_DSCP_SIZE / 8; i++) {
646                 pri_map = OSAL_BE32_TO_CPU(p_dscp_map->dscp_pri_map[i]);
647                 DP_VERBOSE(p_hwfn, ECORE_MSG_DCB, "elem %d pri_map 0x%x\n",
648                            entry, pri_map);
649                 for (j = 0; j < ECORE_DCBX_DSCP_SIZE / 8; j++, entry++)
650                         p_dscp->dscp_pri_map[entry] = (u32)(pri_map >>
651                                                            (j * 4)) & 0xf;
652         }
653 }
654
655 static void
656 ecore_dcbx_get_local_lldp_params(struct ecore_hwfn *p_hwfn,
657                                  struct ecore_ptt *p_ptt,
658                                  struct ecore_dcbx_get *params)
659 {
660         struct lldp_config_params_s *p_local;
661
662         p_local = &p_hwfn->p_dcbx_info->lldp_local[LLDP_NEAREST_BRIDGE];
663
664         OSAL_MEMCPY(params->lldp_local.local_chassis_id,
665                     p_local->local_chassis_id,
666                     OSAL_ARRAY_SIZE(p_local->local_chassis_id));
667         OSAL_MEMCPY(params->lldp_local.local_port_id, p_local->local_port_id,
668                     OSAL_ARRAY_SIZE(p_local->local_port_id));
669 }
670
671 static void
672 ecore_dcbx_get_remote_lldp_params(struct ecore_hwfn *p_hwfn,
673                                   struct ecore_ptt *p_ptt,
674                                   struct ecore_dcbx_get *params)
675 {
676         struct lldp_status_params_s *p_remote;
677
678         p_remote = &p_hwfn->p_dcbx_info->lldp_remote[LLDP_NEAREST_BRIDGE];
679
680         OSAL_MEMCPY(params->lldp_remote.peer_chassis_id,
681                     p_remote->peer_chassis_id,
682                     OSAL_ARRAY_SIZE(p_remote->peer_chassis_id));
683         OSAL_MEMCPY(params->lldp_remote.peer_port_id, p_remote->peer_port_id,
684                     OSAL_ARRAY_SIZE(p_remote->peer_port_id));
685 }
686
687 static enum _ecore_status_t
688 ecore_dcbx_get_params(struct ecore_hwfn *p_hwfn, struct ecore_ptt *p_ptt,
689                       struct ecore_dcbx_get *p_params,
690                       enum ecore_mib_read_type type)
691 {
692         enum _ecore_status_t rc = ECORE_SUCCESS;
693
694         switch (type) {
695         case ECORE_DCBX_REMOTE_MIB:
696                 ecore_dcbx_get_remote_params(p_hwfn, p_ptt, p_params);
697                 break;
698         case ECORE_DCBX_LOCAL_MIB:
699                 ecore_dcbx_get_local_params(p_hwfn, p_ptt, p_params);
700                 break;
701         case ECORE_DCBX_OPERATIONAL_MIB:
702                 ecore_dcbx_get_operational_params(p_hwfn, p_ptt, p_params);
703                 break;
704         case ECORE_DCBX_REMOTE_LLDP_MIB:
705                 ecore_dcbx_get_remote_lldp_params(p_hwfn, p_ptt, p_params);
706                 break;
707         case ECORE_DCBX_LOCAL_LLDP_MIB:
708                 ecore_dcbx_get_local_lldp_params(p_hwfn, p_ptt, p_params);
709                 break;
710         default:
711                 DP_ERR(p_hwfn, "MIB read err, unknown mib type %d\n", type);
712                 return ECORE_INVAL;
713         }
714
715         return rc;
716 }
717
718 static enum _ecore_status_t
719 ecore_dcbx_read_local_lldp_mib(struct ecore_hwfn *p_hwfn,
720                                struct ecore_ptt *p_ptt)
721 {
722         struct ecore_dcbx_mib_meta_data data;
723         enum _ecore_status_t rc = ECORE_SUCCESS;
724
725         OSAL_MEM_ZERO(&data, sizeof(data));
726         data.addr = p_hwfn->mcp_info->port_addr + offsetof(struct public_port,
727                                                            lldp_config_params);
728         data.lldp_local = p_hwfn->p_dcbx_info->lldp_local;
729         data.size = sizeof(struct lldp_config_params_s);
730         ecore_memcpy_from(p_hwfn, p_ptt, data.lldp_local, data.addr, data.size);
731
732         return rc;
733 }
734
735 static enum _ecore_status_t
736 ecore_dcbx_read_remote_lldp_mib(struct ecore_hwfn *p_hwfn,
737                                 struct ecore_ptt *p_ptt,
738                                 enum ecore_mib_read_type type)
739 {
740         struct ecore_dcbx_mib_meta_data data;
741         enum _ecore_status_t rc = ECORE_SUCCESS;
742
743         OSAL_MEM_ZERO(&data, sizeof(data));
744         data.addr = p_hwfn->mcp_info->port_addr + offsetof(struct public_port,
745                                                            lldp_status_params);
746         data.lldp_remote = p_hwfn->p_dcbx_info->lldp_remote;
747         data.size = sizeof(struct lldp_status_params_s);
748         rc = ecore_dcbx_copy_mib(p_hwfn, p_ptt, &data, type);
749
750         return rc;
751 }
752
753 static enum _ecore_status_t
754 ecore_dcbx_read_operational_mib(struct ecore_hwfn *p_hwfn,
755                                 struct ecore_ptt *p_ptt,
756                                 enum ecore_mib_read_type type)
757 {
758         struct ecore_dcbx_mib_meta_data data;
759         enum _ecore_status_t rc = ECORE_SUCCESS;
760
761         OSAL_MEM_ZERO(&data, sizeof(data));
762         data.addr = p_hwfn->mcp_info->port_addr +
763             offsetof(struct public_port, operational_dcbx_mib);
764         data.mib = &p_hwfn->p_dcbx_info->operational;
765         data.size = sizeof(struct dcbx_mib);
766         rc = ecore_dcbx_copy_mib(p_hwfn, p_ptt, &data, type);
767
768         return rc;
769 }
770
771 static enum _ecore_status_t
772 ecore_dcbx_read_remote_mib(struct ecore_hwfn *p_hwfn,
773                            struct ecore_ptt *p_ptt,
774                            enum ecore_mib_read_type type)
775 {
776         struct ecore_dcbx_mib_meta_data data;
777         enum _ecore_status_t rc = ECORE_SUCCESS;
778
779         OSAL_MEM_ZERO(&data, sizeof(data));
780         data.addr = p_hwfn->mcp_info->port_addr +
781             offsetof(struct public_port, remote_dcbx_mib);
782         data.mib = &p_hwfn->p_dcbx_info->remote;
783         data.size = sizeof(struct dcbx_mib);
784         rc = ecore_dcbx_copy_mib(p_hwfn, p_ptt, &data, type);
785
786         return rc;
787 }
788
789 static enum _ecore_status_t
790 ecore_dcbx_read_local_mib(struct ecore_hwfn *p_hwfn, struct ecore_ptt *p_ptt)
791 {
792         struct ecore_dcbx_mib_meta_data data;
793         enum _ecore_status_t rc = ECORE_SUCCESS;
794
795         OSAL_MEM_ZERO(&data, sizeof(data));
796         data.addr = p_hwfn->mcp_info->port_addr +
797             offsetof(struct public_port, local_admin_dcbx_mib);
798         data.local_admin = &p_hwfn->p_dcbx_info->local_admin;
799         data.size = sizeof(struct dcbx_local_params);
800         ecore_memcpy_from(p_hwfn, p_ptt, data.local_admin,
801                           data.addr, data.size);
802
803         return rc;
804 }
805
806 static void
807 ecore_dcbx_read_dscp_mib(struct ecore_hwfn *p_hwfn, struct ecore_ptt *p_ptt)
808 {
809         struct ecore_dcbx_mib_meta_data data;
810
811         data.addr = p_hwfn->mcp_info->port_addr +
812                         offsetof(struct public_port, dcb_dscp_map);
813         data.dscp_map = &p_hwfn->p_dcbx_info->dscp_map;
814         data.size = sizeof(struct dcb_dscp_map);
815         ecore_memcpy_from(p_hwfn, p_ptt, data.dscp_map, data.addr, data.size);
816 }
817
818 static enum _ecore_status_t ecore_dcbx_read_mib(struct ecore_hwfn *p_hwfn,
819                                                 struct ecore_ptt *p_ptt,
820                                                 enum ecore_mib_read_type type)
821 {
822         enum _ecore_status_t rc = ECORE_INVAL;
823
824         switch (type) {
825         case ECORE_DCBX_OPERATIONAL_MIB:
826                 ecore_dcbx_read_dscp_mib(p_hwfn, p_ptt);
827                 rc = ecore_dcbx_read_operational_mib(p_hwfn, p_ptt, type);
828                 break;
829         case ECORE_DCBX_REMOTE_MIB:
830                 rc = ecore_dcbx_read_remote_mib(p_hwfn, p_ptt, type);
831                 break;
832         case ECORE_DCBX_LOCAL_MIB:
833                 rc = ecore_dcbx_read_local_mib(p_hwfn, p_ptt);
834                 break;
835         case ECORE_DCBX_REMOTE_LLDP_MIB:
836                 rc = ecore_dcbx_read_remote_lldp_mib(p_hwfn, p_ptt, type);
837                 break;
838         case ECORE_DCBX_LOCAL_LLDP_MIB:
839                 rc = ecore_dcbx_read_local_lldp_mib(p_hwfn, p_ptt);
840                 break;
841         default:
842                 DP_ERR(p_hwfn, "MIB read err, unknown mib type %d\n", type);
843         }
844
845         return rc;
846 }
847
848 /*
849  * Read updated MIB.
850  * Reconfigure QM and invoke PF update ramrod command if operational MIB
851  * change is detected.
852  */
853 enum _ecore_status_t
854 ecore_dcbx_mib_update_event(struct ecore_hwfn *p_hwfn, struct ecore_ptt *p_ptt,
855                             enum ecore_mib_read_type type)
856 {
857         enum _ecore_status_t rc = ECORE_SUCCESS;
858
859         rc = ecore_dcbx_read_mib(p_hwfn, p_ptt, type);
860         if (rc)
861                 return rc;
862
863         if (type == ECORE_DCBX_OPERATIONAL_MIB) {
864                 ecore_dcbx_get_dscp_params(p_hwfn, p_ptt,
865                                            &p_hwfn->p_dcbx_info->get);
866
867                 rc = ecore_dcbx_process_mib_info(p_hwfn);
868                 if (!rc) {
869                         bool enabled;
870
871                         /* reconfigure tcs of QM queues according
872                          * to negotiation results
873                          */
874                         ecore_qm_reconf(p_hwfn, p_ptt);
875
876                         /* update storm FW with negotiation results */
877                         ecore_sp_pf_update(p_hwfn);
878
879                         /* set eagle enigne 1 flow control workaround
880                          * according to negotiation results
881                          */
882                         enabled = p_hwfn->p_dcbx_info->results.dcbx_enabled;
883                 }
884         }
885         ecore_dcbx_get_params(p_hwfn, p_ptt, &p_hwfn->p_dcbx_info->get, type);
886
887         /* Update the DSCP to TC mapping bit if required */
888         if ((type == ECORE_DCBX_OPERATIONAL_MIB) &&
889             p_hwfn->p_dcbx_info->dscp_nig_update) {
890                 ecore_wr(p_hwfn, p_ptt, NIG_REG_DSCP_TO_TC_MAP_ENABLE, 0x1);
891                 p_hwfn->p_dcbx_info->dscp_nig_update = false;
892         }
893
894         OSAL_DCBX_AEN(p_hwfn, type);
895
896         return rc;
897 }
898
899 enum _ecore_status_t ecore_dcbx_info_alloc(struct ecore_hwfn *p_hwfn)
900 {
901         enum _ecore_status_t rc = ECORE_SUCCESS;
902
903         p_hwfn->p_dcbx_info = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL,
904                                           sizeof(*p_hwfn->p_dcbx_info));
905         if (!p_hwfn->p_dcbx_info) {
906                 DP_NOTICE(p_hwfn, true,
907                           "Failed to allocate `struct ecore_dcbx_info'");
908                 rc = ECORE_NOMEM;
909         }
910
911         return rc;
912 }
913
914 void ecore_dcbx_info_free(struct ecore_hwfn *p_hwfn,
915                           struct ecore_dcbx_info *p_dcbx_info)
916 {
917         OSAL_FREE(p_hwfn->p_dev, p_hwfn->p_dcbx_info);
918 }
919
920 static void ecore_dcbx_update_protocol_data(struct protocol_dcb_data *p_data,
921                                             struct ecore_dcbx_results *p_src,
922                                             enum dcbx_protocol_type type)
923 {
924         p_data->dcb_enable_flag = p_src->arr[type].enable;
925         p_data->dcb_priority = p_src->arr[type].priority;
926         p_data->dcb_tc = p_src->arr[type].tc;
927         p_data->dscp_enable_flag = p_src->arr[type].dscp_enable;
928         p_data->dscp_val = p_src->arr[type].dscp_val;
929 }
930
931 /* Set pf update ramrod command params */
932 void ecore_dcbx_set_pf_update_params(struct ecore_dcbx_results *p_src,
933                                      struct pf_update_ramrod_data *p_dest)
934 {
935         struct protocol_dcb_data *p_dcb_data;
936         u8 update_flag;
937
938         p_dest->pf_id = p_src->pf_id;
939
940         update_flag = p_src->arr[DCBX_PROTOCOL_ETH].update;
941         p_dest->update_eth_dcb_data_mode = update_flag;
942
943         p_dcb_data = &p_dest->eth_dcb_data;
944         ecore_dcbx_update_protocol_data(p_dcb_data, p_src, DCBX_PROTOCOL_ETH);
945 }
946
947 enum _ecore_status_t ecore_dcbx_query_params(struct ecore_hwfn *p_hwfn,
948                                              struct ecore_dcbx_get *p_get,
949                                              enum ecore_mib_read_type type)
950 {
951         struct ecore_ptt *p_ptt;
952         enum _ecore_status_t rc;
953
954         if (IS_VF(p_hwfn->p_dev))
955                 return ECORE_INVAL;
956
957         p_ptt = ecore_ptt_acquire(p_hwfn);
958         if (!p_ptt) {
959                 rc = ECORE_TIMEOUT;
960                 DP_ERR(p_hwfn, "rc = %d\n", rc);
961                 return rc;
962         }
963
964         rc = ecore_dcbx_read_mib(p_hwfn, p_ptt, type);
965         if (rc != ECORE_SUCCESS)
966                 goto out;
967
968         rc = ecore_dcbx_get_params(p_hwfn, p_ptt, p_get, type);
969
970 out:
971         ecore_ptt_release(p_hwfn, p_ptt);
972         return rc;
973 }
974
975 static void
976 ecore_dcbx_set_pfc_data(struct ecore_hwfn *p_hwfn,
977                         u32 *pfc, struct ecore_dcbx_params *p_params)
978 {
979         u8 pfc_map = 0;
980         int i;
981
982         if (p_params->pfc.willing)
983                 *pfc |= DCBX_PFC_WILLING_MASK;
984         else
985                 *pfc &= ~DCBX_PFC_WILLING_MASK;
986
987         if (p_params->pfc.enabled)
988                 *pfc |= DCBX_PFC_ENABLED_MASK;
989         else
990                 *pfc &= ~DCBX_PFC_ENABLED_MASK;
991
992         *pfc &= ~DCBX_PFC_CAPS_MASK;
993         *pfc |= (u32)p_params->pfc.max_tc << DCBX_PFC_CAPS_SHIFT;
994
995         for (i = 0; i < ECORE_MAX_PFC_PRIORITIES; i++)
996                 if (p_params->pfc.prio[i])
997                         pfc_map |= (1 << i);
998         *pfc &= ~DCBX_PFC_PRI_EN_BITMAP_MASK;
999         *pfc |= (pfc_map << DCBX_PFC_PRI_EN_BITMAP_SHIFT);
1000
1001         DP_VERBOSE(p_hwfn, ECORE_MSG_DCB, "pfc = 0x%x\n", *pfc);
1002 }
1003
1004 static void
1005 ecore_dcbx_set_ets_data(struct ecore_hwfn *p_hwfn,
1006                         struct dcbx_ets_feature *p_ets,
1007                         struct ecore_dcbx_params *p_params)
1008 {
1009         u8 *bw_map, *tsa_map;
1010         u32 val;
1011         int i;
1012
1013         if (p_params->ets_willing)
1014                 p_ets->flags |= DCBX_ETS_WILLING_MASK;
1015         else
1016                 p_ets->flags &= ~DCBX_ETS_WILLING_MASK;
1017
1018         if (p_params->ets_cbs)
1019                 p_ets->flags |= DCBX_ETS_CBS_MASK;
1020         else
1021                 p_ets->flags &= ~DCBX_ETS_CBS_MASK;
1022
1023         if (p_params->ets_enabled)
1024                 p_ets->flags |= DCBX_ETS_ENABLED_MASK;
1025         else
1026                 p_ets->flags &= ~DCBX_ETS_ENABLED_MASK;
1027
1028         p_ets->flags &= ~DCBX_ETS_MAX_TCS_MASK;
1029         p_ets->flags |= (u32)p_params->max_ets_tc << DCBX_ETS_MAX_TCS_SHIFT;
1030
1031         bw_map = (u8 *)&p_ets->tc_bw_tbl[0];
1032         tsa_map = (u8 *)&p_ets->tc_tsa_tbl[0];
1033         p_ets->pri_tc_tbl[0] = 0;
1034         for (i = 0; i < ECORE_MAX_PFC_PRIORITIES; i++) {
1035                 bw_map[i] = p_params->ets_tc_bw_tbl[i];
1036                 tsa_map[i] = p_params->ets_tc_tsa_tbl[i];
1037                 /* Copy the priority value to the corresponding 4 bits in the
1038                  * traffic class table.
1039                  */
1040                 val = (((u32)p_params->ets_pri_tc_tbl[i]) << ((7 - i) * 4));
1041                 p_ets->pri_tc_tbl[0] |= val;
1042         }
1043         for (i = 0; i < 2; i++) {
1044                 p_ets->tc_bw_tbl[i] = OSAL_CPU_TO_BE32(p_ets->tc_bw_tbl[i]);
1045                 p_ets->tc_tsa_tbl[i] = OSAL_CPU_TO_BE32(p_ets->tc_tsa_tbl[i]);
1046         }
1047
1048         DP_VERBOSE(p_hwfn, ECORE_MSG_DCB,
1049                    "flags = 0x%x pri_tc = 0x%x tc_bwl[] = {0x%x, 0x%x} tc_tsa = {0x%x, 0x%x}\n",
1050                    p_ets->flags, p_ets->pri_tc_tbl[0], p_ets->tc_bw_tbl[0],
1051                    p_ets->tc_bw_tbl[1], p_ets->tc_tsa_tbl[0],
1052                    p_ets->tc_tsa_tbl[1]);
1053 }
1054
1055 static void
1056 ecore_dcbx_set_app_data(struct ecore_hwfn *p_hwfn,
1057                         struct dcbx_app_priority_feature *p_app,
1058                         struct ecore_dcbx_params *p_params, bool ieee)
1059 {
1060         u32 *entry;
1061         int i;
1062
1063         if (p_params->app_willing)
1064                 p_app->flags |= DCBX_APP_WILLING_MASK;
1065         else
1066                 p_app->flags &= ~DCBX_APP_WILLING_MASK;
1067
1068         if (p_params->app_valid)
1069                 p_app->flags |= DCBX_APP_ENABLED_MASK;
1070         else
1071                 p_app->flags &= ~DCBX_APP_ENABLED_MASK;
1072
1073         p_app->flags &= ~DCBX_APP_NUM_ENTRIES_MASK;
1074         p_app->flags |= (u32)p_params->num_app_entries <<
1075                                         DCBX_APP_NUM_ENTRIES_SHIFT;
1076
1077         for (i = 0; i < DCBX_MAX_APP_PROTOCOL; i++) {
1078                 entry = &p_app->app_pri_tbl[i].entry;
1079                 *entry = 0;
1080                 if (ieee) {
1081                         *entry &= ~(DCBX_APP_SF_IEEE_MASK | DCBX_APP_SF_MASK);
1082                         switch (p_params->app_entry[i].sf_ieee) {
1083                         case ECORE_DCBX_SF_IEEE_ETHTYPE:
1084                                 *entry  |= ((u32)DCBX_APP_SF_IEEE_ETHTYPE <<
1085                                             DCBX_APP_SF_IEEE_SHIFT);
1086                                 *entry  |= ((u32)DCBX_APP_SF_ETHTYPE <<
1087                                             DCBX_APP_SF_SHIFT);
1088                                 break;
1089                         case ECORE_DCBX_SF_IEEE_TCP_PORT:
1090                                 *entry  |= ((u32)DCBX_APP_SF_IEEE_TCP_PORT <<
1091                                             DCBX_APP_SF_IEEE_SHIFT);
1092                                 *entry  |= ((u32)DCBX_APP_SF_PORT <<
1093                                             DCBX_APP_SF_SHIFT);
1094                                 break;
1095                         case ECORE_DCBX_SF_IEEE_UDP_PORT:
1096                                 *entry  |= ((u32)DCBX_APP_SF_IEEE_UDP_PORT <<
1097                                             DCBX_APP_SF_IEEE_SHIFT);
1098                                 *entry  |= ((u32)DCBX_APP_SF_PORT <<
1099                                             DCBX_APP_SF_SHIFT);
1100                                 break;
1101                         case ECORE_DCBX_SF_IEEE_TCP_UDP_PORT:
1102                                 *entry  |= (u32)DCBX_APP_SF_IEEE_TCP_UDP_PORT <<
1103                                             DCBX_APP_SF_IEEE_SHIFT;
1104                                 *entry  |= ((u32)DCBX_APP_SF_PORT <<
1105                                             DCBX_APP_SF_SHIFT);
1106                                 break;
1107                         }
1108                 } else {
1109                         *entry &= ~DCBX_APP_SF_MASK;
1110                         if (p_params->app_entry[i].ethtype)
1111                                 *entry  |= ((u32)DCBX_APP_SF_ETHTYPE <<
1112                                             DCBX_APP_SF_SHIFT);
1113                         else
1114                                 *entry  |= ((u32)DCBX_APP_SF_PORT <<
1115                                             DCBX_APP_SF_SHIFT);
1116                 }
1117                 *entry &= ~DCBX_APP_PROTOCOL_ID_MASK;
1118                 *entry |= ((u32)p_params->app_entry[i].proto_id <<
1119                                 DCBX_APP_PROTOCOL_ID_SHIFT);
1120                 *entry &= ~DCBX_APP_PRI_MAP_MASK;
1121                 *entry |= ((u32)(p_params->app_entry[i].prio) <<
1122                                 DCBX_APP_PRI_MAP_SHIFT);
1123         }
1124
1125         DP_VERBOSE(p_hwfn, ECORE_MSG_DCB, "flags = 0x%x\n", p_app->flags);
1126 }
1127
1128 static enum _ecore_status_t
1129 ecore_dcbx_set_local_params(struct ecore_hwfn *p_hwfn,
1130                             struct dcbx_local_params *local_admin,
1131                             struct ecore_dcbx_set *params)
1132 {
1133         bool ieee = false;
1134
1135         local_admin->flags = 0;
1136         OSAL_MEMCPY(&local_admin->features,
1137                     &p_hwfn->p_dcbx_info->operational.features,
1138                     sizeof(local_admin->features));
1139
1140         if (params->enabled) {
1141                 local_admin->config = params->ver_num;
1142                 ieee = !!(params->ver_num & DCBX_CONFIG_VERSION_IEEE);
1143         } else {
1144                 local_admin->config = DCBX_CONFIG_VERSION_DISABLED;
1145         }
1146
1147         if (params->override_flags & ECORE_DCBX_OVERRIDE_PFC_CFG)
1148                 ecore_dcbx_set_pfc_data(p_hwfn, &local_admin->features.pfc,
1149                                         &params->config.params);
1150
1151         if (params->override_flags & ECORE_DCBX_OVERRIDE_ETS_CFG)
1152                 ecore_dcbx_set_ets_data(p_hwfn, &local_admin->features.ets,
1153                                         &params->config.params);
1154
1155         if (params->override_flags & ECORE_DCBX_OVERRIDE_APP_CFG)
1156                 ecore_dcbx_set_app_data(p_hwfn, &local_admin->features.app,
1157                                         &params->config.params, ieee);
1158
1159         return ECORE_SUCCESS;
1160 }
1161
1162 static enum _ecore_status_t
1163 ecore_dcbx_set_dscp_params(struct ecore_hwfn *p_hwfn,
1164                            struct dcb_dscp_map *p_dscp_map,
1165                            struct ecore_dcbx_set *p_params)
1166 {
1167         int entry, i, j;
1168         u32 val;
1169
1170         OSAL_MEMCPY(p_dscp_map, &p_hwfn->p_dcbx_info->dscp_map,
1171                     sizeof(*p_dscp_map));
1172
1173         p_dscp_map->flags &= ~DCB_DSCP_ENABLE_MASK;
1174         if (p_params->dscp.enabled)
1175                 p_dscp_map->flags |= DCB_DSCP_ENABLE_MASK;
1176
1177         for (i = 0, entry = 0; i < 8; i++) {
1178                 val = 0;
1179                 for (j = 0; j < 8; j++, entry++)
1180                         val |= (((u32)p_params->dscp.dscp_pri_map[entry]) <<
1181                                 (j * 4));
1182
1183                 p_dscp_map->dscp_pri_map[i] = OSAL_CPU_TO_BE32(val);
1184         }
1185
1186         p_hwfn->p_dcbx_info->dscp_nig_update = true;
1187
1188         DP_VERBOSE(p_hwfn, ECORE_MSG_DCB, "flags = 0x%x\n", p_dscp_map->flags);
1189
1190         return ECORE_SUCCESS;
1191 }
1192
1193 enum _ecore_status_t ecore_dcbx_config_params(struct ecore_hwfn *p_hwfn,
1194                                               struct ecore_ptt *p_ptt,
1195                                               struct ecore_dcbx_set *params,
1196                                               bool hw_commit)
1197 {
1198         struct dcbx_local_params local_admin;
1199         struct ecore_dcbx_mib_meta_data data;
1200         struct dcb_dscp_map dscp_map;
1201         u32 resp = 0, param = 0;
1202         enum _ecore_status_t rc = ECORE_SUCCESS;
1203
1204         if (!hw_commit) {
1205                 OSAL_MEMCPY(&p_hwfn->p_dcbx_info->set, params,
1206                             sizeof(p_hwfn->p_dcbx_info->set));
1207                 return ECORE_SUCCESS;
1208         }
1209
1210         /* clear set-parmas cache */
1211         OSAL_MEMSET(&p_hwfn->p_dcbx_info->set, 0,
1212                     sizeof(struct ecore_dcbx_set));
1213
1214         OSAL_MEMSET(&local_admin, 0, sizeof(local_admin));
1215         ecore_dcbx_set_local_params(p_hwfn, &local_admin, params);
1216
1217         data.addr = p_hwfn->mcp_info->port_addr +
1218                         offsetof(struct public_port, local_admin_dcbx_mib);
1219         data.local_admin = &local_admin;
1220         data.size = sizeof(struct dcbx_local_params);
1221         ecore_memcpy_to(p_hwfn, p_ptt, data.addr, data.local_admin, data.size);
1222
1223         if (params->override_flags & ECORE_DCBX_OVERRIDE_DSCP_CFG) {
1224                 OSAL_MEMSET(&dscp_map, 0, sizeof(dscp_map));
1225                 ecore_dcbx_set_dscp_params(p_hwfn, &dscp_map, params);
1226
1227                 data.addr = p_hwfn->mcp_info->port_addr +
1228                                 offsetof(struct public_port, dcb_dscp_map);
1229                 data.dscp_map = &dscp_map;
1230                 data.size = sizeof(struct dcb_dscp_map);
1231                 ecore_memcpy_to(p_hwfn, p_ptt, data.addr, data.dscp_map,
1232                                 data.size);
1233         }
1234
1235         rc = ecore_mcp_cmd(p_hwfn, p_ptt, DRV_MSG_CODE_SET_DCBX,
1236                            1 << DRV_MB_PARAM_LLDP_SEND_SHIFT, &resp, &param);
1237         if (rc != ECORE_SUCCESS) {
1238                 DP_NOTICE(p_hwfn, false,
1239                           "Failed to send DCBX update request\n");
1240                 return rc;
1241         }
1242
1243         return rc;
1244 }
1245
1246 enum _ecore_status_t ecore_dcbx_get_config_params(struct ecore_hwfn *p_hwfn,
1247                                                   struct ecore_dcbx_set *params)
1248 {
1249         struct ecore_dcbx_get *dcbx_info;
1250         int rc;
1251
1252         if (p_hwfn->p_dcbx_info->set.config.valid) {
1253                 OSAL_MEMCPY(params, &p_hwfn->p_dcbx_info->set,
1254                             sizeof(struct ecore_dcbx_set));
1255                 return ECORE_SUCCESS;
1256         }
1257
1258         dcbx_info = OSAL_ALLOC(p_hwfn->p_dev, GFP_KERNEL,
1259                                sizeof(*dcbx_info));
1260         if (!dcbx_info) {
1261                 DP_ERR(p_hwfn, "Failed to allocate struct ecore_dcbx_info\n");
1262                 return ECORE_NOMEM;
1263         }
1264
1265         OSAL_MEMSET(dcbx_info, 0, sizeof(*dcbx_info));
1266         rc = ecore_dcbx_query_params(p_hwfn, dcbx_info,
1267                                      ECORE_DCBX_OPERATIONAL_MIB);
1268         if (rc) {
1269                 OSAL_FREE(p_hwfn->p_dev, dcbx_info);
1270                 return rc;
1271         }
1272         p_hwfn->p_dcbx_info->set.override_flags = 0;
1273
1274         p_hwfn->p_dcbx_info->set.ver_num = DCBX_CONFIG_VERSION_DISABLED;
1275         if (dcbx_info->operational.cee)
1276                 p_hwfn->p_dcbx_info->set.ver_num |= DCBX_CONFIG_VERSION_CEE;
1277         if (dcbx_info->operational.ieee)
1278                 p_hwfn->p_dcbx_info->set.ver_num |= DCBX_CONFIG_VERSION_IEEE;
1279         if (dcbx_info->operational.local)
1280                 p_hwfn->p_dcbx_info->set.ver_num |= DCBX_CONFIG_VERSION_STATIC;
1281
1282         p_hwfn->p_dcbx_info->set.enabled = dcbx_info->operational.enabled;
1283         OSAL_MEMCPY(&p_hwfn->p_dcbx_info->set.config.params,
1284                     &dcbx_info->operational.params,
1285                     sizeof(struct ecore_dcbx_admin_params));
1286         p_hwfn->p_dcbx_info->set.config.valid = true;
1287
1288         OSAL_MEMCPY(params, &p_hwfn->p_dcbx_info->set,
1289                     sizeof(struct ecore_dcbx_set));
1290
1291         OSAL_FREE(p_hwfn->p_dev, dcbx_info);
1292
1293         return ECORE_SUCCESS;
1294 }