net/qede/base: update formatting and comments
[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
17 #define ECORE_DCBX_MAX_MIB_READ_TRY     (100)
18 #define ECORE_MAX_PFC_PRIORITIES        8
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)(pri_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) ? true : false;
33 }
34
35 static bool ecore_dcbx_app_port(u32 app_info_bitmap)
36 {
37         return (ECORE_MFW_GET_FIELD(app_info_bitmap, DCBX_APP_SF) ==
38                 DCBX_APP_SF_PORT) ? true : false;
39 }
40
41 static bool ecore_dcbx_default_tlv(u32 app_info_bitmap, u16 proto_id)
42 {
43         return (ecore_dcbx_app_ethtype(app_info_bitmap) &&
44                 proto_id == ECORE_ETH_TYPE_DEFAULT) ? true : false;
45 }
46
47 static bool ecore_dcbx_enabled(u32 dcbx_cfg_bitmap)
48 {
49         return (ECORE_MFW_GET_FIELD(dcbx_cfg_bitmap, DCBX_CONFIG_VERSION) ==
50                 DCBX_CONFIG_VERSION_DISABLED) ? false : true;
51 }
52
53 static bool ecore_dcbx_cee(u32 dcbx_cfg_bitmap)
54 {
55         return (ECORE_MFW_GET_FIELD(dcbx_cfg_bitmap, DCBX_CONFIG_VERSION) ==
56                 DCBX_CONFIG_VERSION_CEE) ? true : false;
57 }
58
59 static bool ecore_dcbx_ieee(u32 dcbx_cfg_bitmap)
60 {
61         return (ECORE_MFW_GET_FIELD(dcbx_cfg_bitmap, DCBX_CONFIG_VERSION) ==
62                 DCBX_CONFIG_VERSION_IEEE) ? true : false;
63 }
64
65 /* @@@TBD A0 Eagle workaround */
66 void ecore_dcbx_eagle_workaround(struct ecore_hwfn *p_hwfn,
67                                  struct ecore_ptt *p_ptt, bool set_to_pfc)
68 {
69         if (!ENABLE_EAGLE_ENG1_WORKAROUND(p_hwfn))
70                 return;
71
72         ecore_wr(p_hwfn, p_ptt,
73                  YSEM_REG_FAST_MEMORY + 0x20000 /* RAM in FASTMEM */  +
74                  YSTORM_FLOW_CONTROL_MODE_OFFSET,
75                  set_to_pfc ? flow_ctrl_pfc : flow_ctrl_pause);
76         ecore_wr(p_hwfn, p_ptt, NIG_REG_FLOWCTRL_MODE,
77                  EAGLE_ENG1_WORKAROUND_NIG_FLOWCTRL_MODE);
78 }
79
80 static void
81 ecore_dcbx_dp_protocol(struct ecore_hwfn *p_hwfn,
82                        struct ecore_dcbx_results *p_data)
83 {
84         struct ecore_hw_info *p_info = &p_hwfn->hw_info;
85         enum dcbx_protocol_type id;
86         bool enable, update;
87         u8 prio, tc, size;
88         const char *name;       /* @DPDK */
89         int i;
90
91         size = OSAL_ARRAY_SIZE(ecore_dcbx_app_update);
92
93         DP_INFO(p_hwfn, "DCBX negotiated: %d\n", p_data->dcbx_enabled);
94
95         for (i = 0; i < size; i++) {
96                 id = ecore_dcbx_app_update[i].id;
97                 name = ecore_dcbx_app_update[i].name;
98
99                 enable = p_data->arr[id].enable;
100                 update = p_data->arr[id].update;
101                 tc = p_data->arr[id].tc;
102                 prio = p_data->arr[id].priority;
103
104                 DP_INFO(p_hwfn,
105                         "%s info: update %d, enable %d, prio %d, tc %d, num_tc %d\n",
106                         name, update, enable, prio, tc, p_info->num_tc);
107         }
108 }
109
110 static void
111 ecore_dcbx_set_pf_tcs(struct ecore_hw_info *p_info,
112                       u8 tc, enum ecore_pci_personality personality)
113 {
114         /* QM reconf data */
115         if (p_info->personality == personality) {
116                 if (personality == ECORE_PCI_ETH)
117                         p_info->non_offload_tc = tc;
118                 else
119                 p_info->offload_tc = tc;
120 }
121 }
122
123 void
124 ecore_dcbx_set_params(struct ecore_dcbx_results *p_data,
125                       struct ecore_hw_info *p_info,
126                       bool enable, bool update, u8 prio, u8 tc,
127                       enum dcbx_protocol_type type,
128                       enum ecore_pci_personality personality)
129 {
130         /* PF update ramrod data */
131         p_data->arr[type].update = update;
132         p_data->arr[type].enable = enable;
133         p_data->arr[type].priority = prio;
134         p_data->arr[type].tc = tc;
135
136         ecore_dcbx_set_pf_tcs(p_info, tc, personality);
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, bool update, u8 prio, u8 tc,
144                            enum dcbx_protocol_type type)
145 {
146         struct ecore_hw_info *p_info = &p_hwfn->hw_info;
147         enum ecore_pci_personality personality;
148         enum dcbx_protocol_type id;
149         const char *name;       /* @DPDK */
150         u8 size;
151         int i;
152
153         size = OSAL_ARRAY_SIZE(ecore_dcbx_app_update);
154
155         for (i = 0; i < size; i++) {
156                 id = ecore_dcbx_app_update[i].id;
157
158                 if (type != id)
159                         continue;
160
161                 personality = ecore_dcbx_app_update[i].personality;
162                 name = ecore_dcbx_app_update[i].name;
163
164                 ecore_dcbx_set_params(p_data, p_info, enable, update,
165                                       prio, tc, type, personality);
166         }
167 }
168
169 static enum _ecore_status_t
170 ecore_dcbx_get_app_priority(u8 pri_bitmap, u8 *priority)
171 {
172         u32 pri_mask, pri = ECORE_MAX_PFC_PRIORITIES;
173         u32 index = ECORE_MAX_PFC_PRIORITIES - 1;
174         enum _ecore_status_t rc = ECORE_SUCCESS;
175
176         /* Bitmap 1 corresponds to priority 0, return priority 0 */
177         if (pri_bitmap == 1) {
178                 *priority = 0;
179                 return rc;
180         }
181
182         /* Choose the highest priority */
183         while ((pri == ECORE_MAX_PFC_PRIORITIES) && index) {
184                 pri_mask = 1 << index;
185                 if (pri_bitmap & pri_mask)
186                         pri = index;
187                 index--;
188         }
189
190         if (pri < ECORE_MAX_PFC_PRIORITIES)
191                 *priority = (u8)pri;
192         else
193                 rc = ECORE_INVAL;
194
195         return rc;
196 }
197
198 static bool
199 ecore_dcbx_get_app_protocol_type(struct ecore_hwfn *p_hwfn,
200                                  u32 app_prio_bitmap, u16 id, int *type)
201 {
202         bool status = false;
203
204         if (ecore_dcbx_default_tlv(app_prio_bitmap, id)) {
205                 *type = DCBX_PROTOCOL_ETH;
206                 status = true;
207         } else {
208                 DP_ERR(p_hwfn, "Unsupported protocol %d\n", id);
209         }
210
211         return status;
212 }
213
214 /*  Parse app TLV's to update TC information in hw_info structure for
215  * reconfiguring QM. Get protocol specific data for PF update ramrod command.
216  */
217 static enum _ecore_status_t
218 ecore_dcbx_process_tlv(struct ecore_hwfn *p_hwfn,
219                        struct ecore_dcbx_results *p_data,
220                        struct dcbx_app_priority_entry *p_tbl, u32 pri_tc_tbl,
221                        int count, bool dcbx_enabled)
222 {
223         enum _ecore_status_t rc = ECORE_SUCCESS;
224         u8 tc, priority, priority_map;
225         int i, type = -1;
226         u16 protocol_id;
227         bool enable;
228
229         DP_VERBOSE(p_hwfn, ECORE_MSG_DCB, "Num APP entries = %d\n", count);
230
231         /* Parse APP TLV */
232         for (i = 0; i < count; i++) {
233                 protocol_id = ECORE_MFW_GET_FIELD(p_tbl[i].entry,
234                                                   DCBX_APP_PROTOCOL_ID);
235                 priority_map = ECORE_MFW_GET_FIELD(p_tbl[i].entry,
236                                                    DCBX_APP_PRI_MAP);
237                 rc = ecore_dcbx_get_app_priority(priority_map, &priority);
238                 if (rc == ECORE_INVAL) {
239                         DP_ERR(p_hwfn, "Invalid priority\n");
240                         return rc;
241                 }
242
243                 tc = ECORE_DCBX_PRIO2TC(pri_tc_tbl, priority);
244                 if (ecore_dcbx_get_app_protocol_type(p_hwfn, p_tbl[i].entry,
245                                                      protocol_id, &type)) {
246                         /* ETH always have the enable bit reset, as it gets
247                          * vlan information per packet. For other protocols,
248                          * should be set according to the dcbx_enabled
249                          * indication, but we only got here if there was an
250                          * app tlv for the protocol, so dcbx must be enabled.
251                          */
252                         enable = (type == DCBX_PROTOCOL_ETH ? false : true);
253
254                         ecore_dcbx_update_app_info(p_data, p_hwfn, enable, true,
255                                                    priority, tc, type);
256                 }
257         }
258         /* Update ramrod protocol data and hw_info fields
259          * with default info when corresponding APP TLV's are not detected.
260          * The enabled field has a different logic for ethernet as only for
261          * ethernet dcb should disabled by default, as the information arrives
262          * from the OS (unless an explicit app tlv was present).
263          */
264         tc = p_data->arr[DCBX_PROTOCOL_ETH].tc;
265         priority = p_data->arr[DCBX_PROTOCOL_ETH].priority;
266         for (type = 0; type < DCBX_MAX_PROTOCOL_TYPE; type++) {
267                 if (p_data->arr[type].update)
268                         continue;
269
270                 enable = (type == DCBX_PROTOCOL_ETH) ? false : dcbx_enabled;
271                 ecore_dcbx_update_app_info(p_data, p_hwfn, enable, true,
272                                            priority, tc, type);
273         }
274
275         return ECORE_SUCCESS;
276 }
277
278 /* Parse app TLV's to update TC information in hw_info structure for
279  * reconfiguring QM. Get protocol specific data for PF update ramrod command.
280  */
281 static enum _ecore_status_t
282 ecore_dcbx_process_mib_info(struct ecore_hwfn *p_hwfn)
283 {
284         struct dcbx_app_priority_feature *p_app;
285         enum _ecore_status_t rc = ECORE_SUCCESS;
286         struct ecore_dcbx_results data = { 0 };
287         struct dcbx_app_priority_entry *p_tbl;
288         struct dcbx_ets_feature *p_ets;
289         struct ecore_hw_info *p_info;
290         u32 pri_tc_tbl, flags;
291         bool dcbx_enabled;
292         int num_entries;
293
294         /* If DCBx version is non zero, then negotiation was
295          * successfuly performed
296          */
297         flags = p_hwfn->p_dcbx_info->operational.flags;
298         dcbx_enabled = ECORE_MFW_GET_FIELD(flags, DCBX_CONFIG_VERSION) != 0;
299
300         p_app = &p_hwfn->p_dcbx_info->operational.features.app;
301         p_tbl = p_app->app_pri_tbl;
302
303         p_ets = &p_hwfn->p_dcbx_info->operational.features.ets;
304         pri_tc_tbl = p_ets->pri_tc_tbl[0];
305
306         p_info = &p_hwfn->hw_info;
307         num_entries = ECORE_MFW_GET_FIELD(p_app->flags, DCBX_APP_NUM_ENTRIES);
308
309         rc = ecore_dcbx_process_tlv(p_hwfn, &data, p_tbl, pri_tc_tbl,
310                                     num_entries, dcbx_enabled);
311         if (rc != ECORE_SUCCESS)
312                 return rc;
313
314         p_info->num_tc = ECORE_MFW_GET_FIELD(p_ets->flags, DCBX_ETS_MAX_TCS);
315         data.pf_id = p_hwfn->rel_pf_id;
316         data.dcbx_enabled = dcbx_enabled;
317
318         ecore_dcbx_dp_protocol(p_hwfn, &data);
319
320         OSAL_MEMCPY(&p_hwfn->p_dcbx_info->results, &data,
321                     sizeof(struct ecore_dcbx_results));
322
323         return ECORE_SUCCESS;
324 }
325
326 static enum _ecore_status_t
327 ecore_dcbx_copy_mib(struct ecore_hwfn *p_hwfn,
328                     struct ecore_ptt *p_ptt,
329                     struct ecore_dcbx_mib_meta_data *p_data,
330                     enum ecore_mib_read_type type)
331 {
332         enum _ecore_status_t rc = ECORE_SUCCESS;
333         u32 prefix_seq_num, suffix_seq_num;
334         int read_count = 0;
335
336         do {
337                 if (type == ECORE_DCBX_REMOTE_LLDP_MIB) {
338                         ecore_memcpy_from(p_hwfn, p_ptt, p_data->lldp_remote,
339                                           p_data->addr, p_data->size);
340                         prefix_seq_num = p_data->lldp_remote->prefix_seq_num;
341                         suffix_seq_num = p_data->lldp_remote->suffix_seq_num;
342                 } else {
343                         ecore_memcpy_from(p_hwfn, p_ptt, p_data->mib,
344                                           p_data->addr, p_data->size);
345                         prefix_seq_num = p_data->mib->prefix_seq_num;
346                         suffix_seq_num = p_data->mib->suffix_seq_num;
347                 }
348                 read_count++;
349
350                 DP_VERBOSE(p_hwfn, ECORE_MSG_DCB,
351                            "mib type = %d, try count = %d prefix seq num  ="
352                            " %d suffix seq num = %d\n",
353                            type, read_count, prefix_seq_num, suffix_seq_num);
354         } while ((prefix_seq_num != suffix_seq_num) &&
355                  (read_count < ECORE_DCBX_MAX_MIB_READ_TRY));
356
357         if (read_count >= ECORE_DCBX_MAX_MIB_READ_TRY) {
358                 DP_ERR(p_hwfn,
359                        "MIB read err, mib type = %d, try count ="
360                        " %d prefix seq num = %d suffix seq num = %d\n",
361                        type, read_count, prefix_seq_num, suffix_seq_num);
362                 rc = ECORE_IO;
363         }
364
365         return rc;
366 }
367
368 static enum _ecore_status_t
369 ecore_dcbx_get_priority_info(struct ecore_hwfn *p_hwfn,
370                              struct ecore_dcbx_app_prio *p_prio,
371                              struct ecore_dcbx_results *p_results)
372 {
373         enum _ecore_status_t rc = ECORE_SUCCESS;
374
375         if (p_results->arr[DCBX_PROTOCOL_ETH].update &&
376             p_results->arr[DCBX_PROTOCOL_ETH].enable) {
377                 p_prio->eth = p_results->arr[DCBX_PROTOCOL_ETH].priority;
378                 DP_VERBOSE(p_hwfn, ECORE_MSG_DCB,
379                            "Priority: eth %d\n", p_prio->eth);
380         }
381
382         return rc;
383 }
384
385 static void
386 ecore_dcbx_get_app_data(struct ecore_hwfn *p_hwfn,
387                         struct dcbx_app_priority_feature *p_app,
388                         struct dcbx_app_priority_entry *p_tbl,
389                         struct ecore_dcbx_params *p_params)
390 {
391         int i;
392
393         p_params->app_willing = ECORE_MFW_GET_FIELD(p_app->flags,
394                                                     DCBX_APP_WILLING);
395         p_params->app_valid = ECORE_MFW_GET_FIELD(p_app->flags,
396                                                   DCBX_APP_ENABLED);
397         p_params->num_app_entries = ECORE_MFW_GET_FIELD(p_app->flags,
398                                                         DCBX_APP_ENABLED);
399         for (i = 0; i < DCBX_MAX_APP_PROTOCOL; i++)
400                 p_params->app_bitmap[i] = p_tbl[i].entry;
401
402         DP_VERBOSE(p_hwfn, ECORE_MSG_DCB,
403                    "APP params: willing %d, valid %d\n",
404                    p_params->app_willing, p_params->app_valid);
405 }
406
407 static void
408 ecore_dcbx_get_pfc_data(struct ecore_hwfn *p_hwfn,
409                         u32 pfc, struct ecore_dcbx_params *p_params)
410 {
411         p_params->pfc_willing = ECORE_MFW_GET_FIELD(pfc, DCBX_PFC_WILLING);
412         p_params->max_pfc_tc = ECORE_MFW_GET_FIELD(pfc, DCBX_PFC_CAPS);
413         p_params->pfc_enabled = ECORE_MFW_GET_FIELD(pfc, DCBX_PFC_ENABLED);
414         p_params->pfc_bitmap = pfc;
415
416         DP_VERBOSE(p_hwfn, ECORE_MSG_DCB,
417                    "PFC params: willing %d, pfc_bitmap %d\n",
418                    p_params->pfc_willing, p_params->pfc_bitmap);
419 }
420
421 static void
422 ecore_dcbx_get_ets_data(struct ecore_hwfn *p_hwfn,
423                         struct dcbx_ets_feature *p_ets,
424                         struct ecore_dcbx_params *p_params)
425 {
426         int i;
427
428         p_params->ets_willing = ECORE_MFW_GET_FIELD(p_ets->flags,
429                                                     DCBX_ETS_WILLING);
430         p_params->ets_enabled = ECORE_MFW_GET_FIELD(p_ets->flags,
431                                                     DCBX_ETS_ENABLED);
432         p_params->max_ets_tc = ECORE_MFW_GET_FIELD(p_ets->flags,
433                                                    DCBX_ETS_MAX_TCS);
434         p_params->ets_pri_tc_tbl[0] = p_ets->pri_tc_tbl[0];
435
436         DP_VERBOSE(p_hwfn, ECORE_MSG_DCB,
437                    "ETS params: willing %d, pri_tc_tbl_0 %x max_ets_tc %d\n",
438                    p_params->ets_willing, p_params->ets_pri_tc_tbl[0],
439                    p_params->max_ets_tc);
440
441         /* 8 bit tsa and bw data corresponding to each of the 8 TC's are
442          * encoded in a type u32 array of size 2.
443          */
444         for (i = 0; i < 2; i++) {
445                 p_params->ets_tc_tsa_tbl[i] = p_ets->tc_tsa_tbl[i];
446                 p_params->ets_tc_bw_tbl[i] = p_ets->tc_bw_tbl[i];
447
448                 DP_VERBOSE(p_hwfn, ECORE_MSG_DCB,
449                            "elem %d  bw_tbl %x tsa_tbl %x\n",
450                            i, p_params->ets_tc_bw_tbl[i],
451                            p_params->ets_tc_tsa_tbl[i]);
452         }
453 }
454
455 static enum _ecore_status_t
456 ecore_dcbx_get_common_params(struct ecore_hwfn *p_hwfn,
457                              struct dcbx_app_priority_feature *p_app,
458                              struct dcbx_app_priority_entry *p_tbl,
459                              struct dcbx_ets_feature *p_ets,
460                              u32 pfc, struct ecore_dcbx_params *p_params)
461 {
462         ecore_dcbx_get_app_data(p_hwfn, p_app, p_tbl, p_params);
463         ecore_dcbx_get_ets_data(p_hwfn, p_ets, p_params);
464         ecore_dcbx_get_pfc_data(p_hwfn, pfc, p_params);
465
466         return ECORE_SUCCESS;
467 }
468
469 static enum _ecore_status_t
470 ecore_dcbx_get_local_params(struct ecore_hwfn *p_hwfn,
471                             struct ecore_ptt *p_ptt,
472                             struct ecore_dcbx_get *params)
473 {
474         struct ecore_dcbx_admin_params *p_local;
475         struct dcbx_app_priority_feature *p_app;
476         struct dcbx_app_priority_entry *p_tbl;
477         struct ecore_dcbx_params *p_data;
478         struct dcbx_ets_feature *p_ets;
479         u32 pfc;
480
481         p_local = &params->local;
482         p_data = &p_local->params;
483         p_app = &p_hwfn->p_dcbx_info->local_admin.features.app;
484         p_tbl = p_app->app_pri_tbl;
485         p_ets = &p_hwfn->p_dcbx_info->local_admin.features.ets;
486         pfc = p_hwfn->p_dcbx_info->local_admin.features.pfc;
487
488         ecore_dcbx_get_common_params(p_hwfn, p_app, p_tbl, p_ets, pfc, p_data);
489         p_local->valid = true;
490
491         return ECORE_SUCCESS;
492 }
493
494 static enum _ecore_status_t
495 ecore_dcbx_get_remote_params(struct ecore_hwfn *p_hwfn,
496                              struct ecore_ptt *p_ptt,
497                              struct ecore_dcbx_get *params)
498 {
499         struct ecore_dcbx_remote_params *p_remote;
500         struct dcbx_app_priority_feature *p_app;
501         struct dcbx_app_priority_entry *p_tbl;
502         struct ecore_dcbx_params *p_data;
503         struct dcbx_ets_feature *p_ets;
504         u32 pfc;
505
506         p_remote = &params->remote;
507         p_data = &p_remote->params;
508         p_app = &p_hwfn->p_dcbx_info->remote.features.app;
509         p_tbl = p_app->app_pri_tbl;
510         p_ets = &p_hwfn->p_dcbx_info->remote.features.ets;
511         pfc = p_hwfn->p_dcbx_info->remote.features.pfc;
512
513         ecore_dcbx_get_common_params(p_hwfn, p_app, p_tbl, p_ets, pfc, p_data);
514         p_remote->valid = true;
515
516         return ECORE_SUCCESS;
517 }
518
519 static enum _ecore_status_t
520 ecore_dcbx_get_operational_params(struct ecore_hwfn *p_hwfn,
521                                   struct ecore_ptt *p_ptt,
522                                   struct ecore_dcbx_get *params)
523 {
524         struct ecore_dcbx_operational_params *p_operational;
525         enum _ecore_status_t rc = ECORE_SUCCESS;
526         struct dcbx_app_priority_feature *p_app;
527         struct dcbx_app_priority_entry *p_tbl;
528         struct ecore_dcbx_results *p_results;
529         struct ecore_dcbx_params *p_data;
530         struct dcbx_ets_feature *p_ets;
531         bool enabled, err;
532         u32 pfc, flags;
533
534         flags = p_hwfn->p_dcbx_info->operational.flags;
535
536         /* If DCBx version is non zero, then negotiation
537          * was successfuly performed
538          */
539         p_operational = &params->operational;
540         enabled = ecore_dcbx_enabled(flags);
541         if (!enabled) {
542                 p_operational->enabled = enabled;
543                 p_operational->valid = false;
544                 return ECORE_INVAL;
545         }
546
547         p_data = &p_operational->params;
548         p_results = &p_hwfn->p_dcbx_info->results;
549         p_app = &p_hwfn->p_dcbx_info->operational.features.app;
550         p_tbl = p_app->app_pri_tbl;
551         p_ets = &p_hwfn->p_dcbx_info->operational.features.ets;
552         pfc = p_hwfn->p_dcbx_info->operational.features.pfc;
553
554         p_operational->ieee = ecore_dcbx_ieee(flags);
555         p_operational->cee = ecore_dcbx_cee(flags);
556
557         DP_VERBOSE(p_hwfn, ECORE_MSG_DCB,
558                    "Version support: ieee %d, cee %d\n",
559                    p_operational->ieee, p_operational->cee);
560
561         ecore_dcbx_get_common_params(p_hwfn, p_app, p_tbl, p_ets, pfc, p_data);
562         ecore_dcbx_get_priority_info(p_hwfn, &p_operational->app_prio,
563                                      p_results);
564         err = ECORE_MFW_GET_FIELD(p_app->flags, DCBX_APP_ERROR);
565         p_operational->err = err;
566         p_operational->enabled = enabled;
567         p_operational->valid = true;
568
569         return rc;
570 }
571
572 static enum _ecore_status_t
573 ecore_dcbx_get_local_lldp_params(struct ecore_hwfn *p_hwfn,
574                                  struct ecore_ptt *p_ptt,
575                                  struct ecore_dcbx_get *params)
576 {
577         struct ecore_dcbx_lldp_local *p_local;
578         osal_size_t size;
579         u32 *dest;
580
581         p_local = &params->lldp_local;
582
583         size = OSAL_ARRAY_SIZE(p_local->local_chassis_id);
584         dest = p_hwfn->p_dcbx_info->get.lldp_local.local_chassis_id;
585         OSAL_MEMCPY(dest, p_local->local_chassis_id, size);
586
587         size = OSAL_ARRAY_SIZE(p_local->local_port_id);
588         dest = p_hwfn->p_dcbx_info->get.lldp_local.local_port_id;
589         OSAL_MEMCPY(dest, p_local->local_port_id, size);
590
591         return ECORE_SUCCESS;
592 }
593
594 static enum _ecore_status_t
595 ecore_dcbx_get_remote_lldp_params(struct ecore_hwfn *p_hwfn,
596                                   struct ecore_ptt *p_ptt,
597                                   struct ecore_dcbx_get *params)
598 {
599         struct ecore_dcbx_lldp_remote *p_remote;
600         osal_size_t size;
601         u32 *dest;
602
603         p_remote = &params->lldp_remote;
604
605         size = OSAL_ARRAY_SIZE(p_remote->peer_chassis_id);
606         dest = p_hwfn->p_dcbx_info->get.lldp_remote.peer_chassis_id;
607         OSAL_MEMCPY(dest, p_remote->peer_chassis_id, size);
608
609         size = OSAL_ARRAY_SIZE(p_remote->peer_port_id);
610         dest = p_hwfn->p_dcbx_info->get.lldp_remote.peer_port_id;
611         OSAL_MEMCPY(dest, p_remote->peer_port_id, size);
612
613         return ECORE_SUCCESS;
614 }
615
616 static enum _ecore_status_t
617 ecore_dcbx_get_params(struct ecore_hwfn *p_hwfn,
618                       struct ecore_ptt *p_ptt, enum ecore_mib_read_type type)
619 {
620         enum _ecore_status_t rc = ECORE_SUCCESS;
621         struct ecore_dcbx_get *p_params;
622
623         p_params = &p_hwfn->p_dcbx_info->get;
624
625         switch (type) {
626         case ECORE_DCBX_REMOTE_MIB:
627                 ecore_dcbx_get_remote_params(p_hwfn, p_ptt, p_params);
628                 break;
629         case ECORE_DCBX_LOCAL_MIB:
630                 ecore_dcbx_get_local_params(p_hwfn, p_ptt, p_params);
631                 break;
632         case ECORE_DCBX_OPERATIONAL_MIB:
633                 ecore_dcbx_get_operational_params(p_hwfn, p_ptt, p_params);
634                 break;
635         case ECORE_DCBX_REMOTE_LLDP_MIB:
636                 rc = ecore_dcbx_get_remote_lldp_params(p_hwfn, p_ptt, p_params);
637                 break;
638         case ECORE_DCBX_LOCAL_LLDP_MIB:
639                 rc = ecore_dcbx_get_local_lldp_params(p_hwfn, p_ptt, p_params);
640                 break;
641         default:
642                 DP_ERR(p_hwfn, "MIB read err, unknown mib type %d\n", type);
643                 return ECORE_INVAL;
644         }
645
646         return rc;
647 }
648
649 static enum _ecore_status_t
650 ecore_dcbx_read_local_lldp_mib(struct ecore_hwfn *p_hwfn,
651                                struct ecore_ptt *p_ptt)
652 {
653         enum _ecore_status_t rc = ECORE_SUCCESS;
654         struct ecore_dcbx_mib_meta_data data;
655
656         data.addr = p_hwfn->mcp_info->port_addr + offsetof(struct public_port,
657                                                            lldp_config_params);
658         data.lldp_local = p_hwfn->p_dcbx_info->lldp_local;
659         data.size = sizeof(struct lldp_config_params_s);
660         ecore_memcpy_from(p_hwfn, p_ptt, data.lldp_local, data.addr, data.size);
661
662         return rc;
663 }
664
665 static enum _ecore_status_t
666 ecore_dcbx_read_remote_lldp_mib(struct ecore_hwfn *p_hwfn,
667                                 struct ecore_ptt *p_ptt,
668                                 enum ecore_mib_read_type type)
669 {
670         enum _ecore_status_t rc = ECORE_SUCCESS;
671         struct ecore_dcbx_mib_meta_data data;
672
673         data.addr = p_hwfn->mcp_info->port_addr + offsetof(struct public_port,
674                                                            lldp_status_params);
675         data.lldp_remote = p_hwfn->p_dcbx_info->lldp_remote;
676         data.size = sizeof(struct lldp_status_params_s);
677         rc = ecore_dcbx_copy_mib(p_hwfn, p_ptt, &data, type);
678
679         return rc;
680 }
681
682 static enum _ecore_status_t
683 ecore_dcbx_read_operational_mib(struct ecore_hwfn *p_hwfn,
684                                 struct ecore_ptt *p_ptt,
685                                 enum ecore_mib_read_type type)
686 {
687         struct ecore_dcbx_mib_meta_data data;
688         enum _ecore_status_t rc = ECORE_SUCCESS;
689
690         data.addr = p_hwfn->mcp_info->port_addr +
691             offsetof(struct public_port, operational_dcbx_mib);
692         data.mib = &p_hwfn->p_dcbx_info->operational;
693         data.size = sizeof(struct dcbx_mib);
694         rc = ecore_dcbx_copy_mib(p_hwfn, p_ptt, &data, type);
695
696         return rc;
697 }
698
699 static enum _ecore_status_t
700 ecore_dcbx_read_remote_mib(struct ecore_hwfn *p_hwfn,
701                            struct ecore_ptt *p_ptt,
702                            enum ecore_mib_read_type type)
703 {
704         struct ecore_dcbx_mib_meta_data data;
705         enum _ecore_status_t rc = ECORE_SUCCESS;
706
707         data.addr = p_hwfn->mcp_info->port_addr +
708             offsetof(struct public_port, remote_dcbx_mib);
709         data.mib = &p_hwfn->p_dcbx_info->remote;
710         data.size = sizeof(struct dcbx_mib);
711         rc = ecore_dcbx_copy_mib(p_hwfn, p_ptt, &data, type);
712
713         return rc;
714 }
715
716 static enum _ecore_status_t
717 ecore_dcbx_read_local_mib(struct ecore_hwfn *p_hwfn, struct ecore_ptt *p_ptt)
718 {
719         struct ecore_dcbx_mib_meta_data data;
720         enum _ecore_status_t rc = ECORE_SUCCESS;
721
722         data.addr = p_hwfn->mcp_info->port_addr +
723             offsetof(struct public_port, local_admin_dcbx_mib);
724         data.local_admin = &p_hwfn->p_dcbx_info->local_admin;
725         data.size = sizeof(struct dcbx_local_params);
726         ecore_memcpy_from(p_hwfn, p_ptt, data.local_admin,
727                           data.addr, data.size);
728
729         return rc;
730 }
731
732 static enum _ecore_status_t ecore_dcbx_read_mib(struct ecore_hwfn *p_hwfn,
733                                                 struct ecore_ptt *p_ptt,
734                                                 enum ecore_mib_read_type type)
735 {
736         enum _ecore_status_t rc = ECORE_SUCCESS;
737
738         switch (type) {
739         case ECORE_DCBX_OPERATIONAL_MIB:
740                 rc = ecore_dcbx_read_operational_mib(p_hwfn, p_ptt, type);
741                 break;
742         case ECORE_DCBX_REMOTE_MIB:
743                 rc = ecore_dcbx_read_remote_mib(p_hwfn, p_ptt, type);
744                 break;
745         case ECORE_DCBX_LOCAL_MIB:
746                 rc = ecore_dcbx_read_local_mib(p_hwfn, p_ptt);
747                 break;
748         case ECORE_DCBX_REMOTE_LLDP_MIB:
749                 rc = ecore_dcbx_read_remote_lldp_mib(p_hwfn, p_ptt, type);
750                 break;
751         case ECORE_DCBX_LOCAL_LLDP_MIB:
752                 rc = ecore_dcbx_read_local_lldp_mib(p_hwfn, p_ptt);
753                 break;
754         default:
755                 DP_ERR(p_hwfn, "MIB read err, unknown mib type %d\n", type);
756                 return ECORE_INVAL;
757         }
758
759         return rc;
760 }
761
762 /*
763  * Read updated MIB.
764  * Reconfigure QM and invoke PF update ramrod command if operational MIB
765  * change is detected.
766  */
767 enum _ecore_status_t
768 ecore_dcbx_mib_update_event(struct ecore_hwfn *p_hwfn, struct ecore_ptt *p_ptt,
769                             enum ecore_mib_read_type type)
770 {
771         enum _ecore_status_t rc = ECORE_SUCCESS;
772
773         rc = ecore_dcbx_read_mib(p_hwfn, p_ptt, type);
774         if (rc)
775                 return rc;
776
777         if (type == ECORE_DCBX_OPERATIONAL_MIB) {
778                 rc = ecore_dcbx_process_mib_info(p_hwfn);
779                 if (!rc) {
780                         bool enabled;
781
782                         /* reconfigure tcs of QM queues according
783                          * to negotiation results
784                          */
785                         ecore_qm_reconf(p_hwfn, p_ptt);
786
787                         /* update storm FW with negotiation results */
788                         ecore_sp_pf_update(p_hwfn);
789
790                         /* set eagle enigne 1 flow control workaround
791                          * according to negotiation results
792                          */
793                         enabled = p_hwfn->p_dcbx_info->results.dcbx_enabled;
794                         ecore_dcbx_eagle_workaround(p_hwfn, p_ptt, enabled);
795                 }
796         }
797         ecore_dcbx_get_params(p_hwfn, p_ptt, type);
798         OSAL_DCBX_AEN(p_hwfn, type);
799
800         return rc;
801 }
802
803 enum _ecore_status_t ecore_dcbx_info_alloc(struct ecore_hwfn *p_hwfn)
804 {
805         enum _ecore_status_t rc = ECORE_SUCCESS;
806
807         p_hwfn->p_dcbx_info = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL,
808                                           sizeof(struct ecore_dcbx_info));
809         if (!p_hwfn->p_dcbx_info) {
810                 DP_NOTICE(p_hwfn, true,
811                           "Failed to allocate `struct ecore_dcbx_info'");
812                 rc = ECORE_NOMEM;
813         }
814
815         return rc;
816 }
817
818 void ecore_dcbx_info_free(struct ecore_hwfn *p_hwfn,
819                           struct ecore_dcbx_info *p_dcbx_info)
820 {
821         OSAL_FREE(p_hwfn->p_dev, p_hwfn->p_dcbx_info);
822 }
823
824 static void ecore_dcbx_update_protocol_data(struct protocol_dcb_data *p_data,
825                                             struct ecore_dcbx_results *p_src,
826                                             enum dcbx_protocol_type type)
827 {
828         p_data->dcb_enable_flag = p_src->arr[type].enable;
829         p_data->dcb_priority = p_src->arr[type].priority;
830         p_data->dcb_tc = p_src->arr[type].tc;
831 }
832
833 /* Set pf update ramrod command params */
834 void ecore_dcbx_set_pf_update_params(struct ecore_dcbx_results *p_src,
835                                      struct pf_update_ramrod_data *p_dest)
836 {
837         struct protocol_dcb_data *p_dcb_data;
838         bool update_flag;
839
840         p_dest->pf_id = p_src->pf_id;
841
842         update_flag = p_src->arr[DCBX_PROTOCOL_ETH].update;
843         p_dest->update_eth_dcb_data_flag = update_flag;
844
845         p_dcb_data = &p_dest->eth_dcb_data;
846         ecore_dcbx_update_protocol_data(p_dcb_data, p_src, DCBX_PROTOCOL_ETH);
847 }
848
849 static
850 enum _ecore_status_t ecore_dcbx_query(struct ecore_hwfn *p_hwfn,
851                                       enum ecore_mib_read_type type)
852 {
853         struct ecore_ptt *p_ptt;
854         enum _ecore_status_t rc;
855
856         p_ptt = ecore_ptt_acquire(p_hwfn);
857         if (!p_ptt) {
858                 rc = ECORE_TIMEOUT;
859                 DP_ERR(p_hwfn, "rc = %d\n", rc);
860                 return rc;
861         }
862
863         rc = ecore_dcbx_read_mib(p_hwfn, p_ptt, type);
864         if (rc != ECORE_SUCCESS)
865                 goto out;
866
867         rc = ecore_dcbx_get_params(p_hwfn, p_ptt, type);
868
869 out:
870         ecore_ptt_release(p_hwfn, p_ptt);
871         return rc;
872 }
873
874 enum _ecore_status_t ecore_dcbx_query_params(struct ecore_hwfn *p_hwfn,
875                                              struct ecore_dcbx_get *p_get,
876                                              enum ecore_mib_read_type type)
877 {
878         enum _ecore_status_t rc;
879
880         rc = ecore_dcbx_query(p_hwfn, type);
881         if (rc)
882                 return rc;
883
884         if (p_get != OSAL_NULL)
885                 OSAL_MEMCPY(p_get, &p_hwfn->p_dcbx_info->get,
886                             sizeof(struct ecore_dcbx_get));
887
888         return rc;
889 }