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