f09f77130b15376f467ac1b0094cc89622c2452d
[dpdk.git] / drivers / net / qede / base / ecore_dev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2016 - 2018 Cavium Inc.
3  * All rights reserved.
4  * www.cavium.com
5  */
6
7 #include "bcm_osal.h"
8 #include "reg_addr.h"
9 #include "ecore_gtt_reg_addr.h"
10 #include "ecore.h"
11 #include "ecore_chain.h"
12 #include "ecore_status.h"
13 #include "ecore_hw.h"
14 #include "ecore_rt_defs.h"
15 #include "ecore_init_ops.h"
16 #include "ecore_int.h"
17 #include "ecore_cxt.h"
18 #include "ecore_spq.h"
19 #include "ecore_init_fw_funcs.h"
20 #include "ecore_sp_commands.h"
21 #include "ecore_dev_api.h"
22 #include "ecore_sriov.h"
23 #include "ecore_vf.h"
24 #include "ecore_mcp.h"
25 #include "ecore_hw_defs.h"
26 #include "mcp_public.h"
27 #include "ecore_iro.h"
28 #include "nvm_cfg.h"
29 #include "ecore_dcbx.h"
30 #include "ecore_l2.h"
31
32 /* TODO - there's a bug in DCBx re-configuration flows in MF, as the QM
33  * registers involved are not split and thus configuration is a race where
34  * some of the PFs configuration might be lost.
35  * Eventually, this needs to move into a MFW-covered HW-lock as arbitration
36  * mechanism as this doesn't cover some cases [E.g., PDA or scenarios where
37  * there's more than a single compiled ecore component in system].
38  */
39 static osal_spinlock_t qm_lock;
40 static u32 qm_lock_ref_cnt;
41
42 /******************** Doorbell Recovery *******************/
43 /* The doorbell recovery mechanism consists of a list of entries which represent
44  * doorbelling entities (l2 queues, roce sq/rq/cqs, the slowpath spq, etc). Each
45  * entity needs to register with the mechanism and provide the parameters
46  * describing it's doorbell, including a location where last used doorbell data
47  * can be found. The doorbell execute function will traverse the list and
48  * doorbell all of the registered entries.
49  */
50 struct ecore_db_recovery_entry {
51         osal_list_entry_t       list_entry;
52         void OSAL_IOMEM         *db_addr;
53         void                    *db_data;
54         enum ecore_db_rec_width db_width;
55         enum ecore_db_rec_space db_space;
56         u8                      hwfn_idx;
57 };
58
59 /* display a single doorbell recovery entry */
60 void ecore_db_recovery_dp_entry(struct ecore_hwfn *p_hwfn,
61                                 struct ecore_db_recovery_entry *db_entry,
62                                 const char *action)
63 {
64         DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ, "(%s: db_entry %p, addr %p, data %p, width %s, %s space, hwfn %d)\n",
65                    action, db_entry, db_entry->db_addr, db_entry->db_data,
66                    db_entry->db_width == DB_REC_WIDTH_32B ? "32b" : "64b",
67                    db_entry->db_space == DB_REC_USER ? "user" : "kernel",
68                    db_entry->hwfn_idx);
69 }
70
71 /* doorbell address sanity (address within doorbell bar range) */
72 bool ecore_db_rec_sanity(struct ecore_dev *p_dev, void OSAL_IOMEM *db_addr,
73                          void *db_data)
74 {
75         /* make sure doorbell address  is within the doorbell bar */
76         if (db_addr < p_dev->doorbells || (u8 *)db_addr >
77                         (u8 *)p_dev->doorbells + p_dev->db_size) {
78                 OSAL_WARN(true,
79                           "Illegal doorbell address: %p. Legal range for doorbell addresses is [%p..%p]\n",
80                           db_addr, p_dev->doorbells,
81                           (u8 *)p_dev->doorbells + p_dev->db_size);
82                 return false;
83         }
84
85         /* make sure doorbell data pointer is not null */
86         if (!db_data) {
87                 OSAL_WARN(true, "Illegal doorbell data pointer: %p", db_data);
88                 return false;
89         }
90
91         return true;
92 }
93
94 /* find hwfn according to the doorbell address */
95 struct ecore_hwfn *ecore_db_rec_find_hwfn(struct ecore_dev *p_dev,
96                                           void OSAL_IOMEM *db_addr)
97 {
98         struct ecore_hwfn *p_hwfn;
99
100         /* In CMT doorbell bar is split down the middle between engine 0 and
101          * enigne 1
102          */
103         if (ECORE_IS_CMT(p_dev))
104                 p_hwfn = db_addr < p_dev->hwfns[1].doorbells ?
105                         &p_dev->hwfns[0] : &p_dev->hwfns[1];
106         else
107                 p_hwfn = ECORE_LEADING_HWFN(p_dev);
108
109         return p_hwfn;
110 }
111
112 /* add a new entry to the doorbell recovery mechanism */
113 enum _ecore_status_t ecore_db_recovery_add(struct ecore_dev *p_dev,
114                                            void OSAL_IOMEM *db_addr,
115                                            void *db_data,
116                                            enum ecore_db_rec_width db_width,
117                                            enum ecore_db_rec_space db_space)
118 {
119         struct ecore_db_recovery_entry *db_entry;
120         struct ecore_hwfn *p_hwfn;
121
122         /* shortcircuit VFs, for now */
123         if (IS_VF(p_dev)) {
124                 DP_VERBOSE(p_dev, ECORE_MSG_IOV, "db recovery - skipping VF doorbell\n");
125                 return ECORE_SUCCESS;
126         }
127
128         /* sanitize doorbell address */
129         if (!ecore_db_rec_sanity(p_dev, db_addr, db_data))
130                 return ECORE_INVAL;
131
132         /* obtain hwfn from doorbell address */
133         p_hwfn = ecore_db_rec_find_hwfn(p_dev, db_addr);
134
135         /* create entry */
136         db_entry = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL, sizeof(*db_entry));
137         if (!db_entry) {
138                 DP_NOTICE(p_dev, false, "Failed to allocate a db recovery entry\n");
139                 return ECORE_NOMEM;
140         }
141
142         /* populate entry */
143         db_entry->db_addr = db_addr;
144         db_entry->db_data = db_data;
145         db_entry->db_width = db_width;
146         db_entry->db_space = db_space;
147         db_entry->hwfn_idx = p_hwfn->my_id;
148
149         /* display */
150         ecore_db_recovery_dp_entry(p_hwfn, db_entry, "Adding");
151
152         /* protect the list */
153         OSAL_SPIN_LOCK(&p_hwfn->db_recovery_info.lock);
154         OSAL_LIST_PUSH_TAIL(&db_entry->list_entry,
155                             &p_hwfn->db_recovery_info.list);
156         OSAL_SPIN_UNLOCK(&p_hwfn->db_recovery_info.lock);
157
158         return ECORE_SUCCESS;
159 }
160
161 /* remove an entry from the doorbell recovery mechanism */
162 enum _ecore_status_t ecore_db_recovery_del(struct ecore_dev *p_dev,
163                                            void OSAL_IOMEM *db_addr,
164                                            void *db_data)
165 {
166         struct ecore_db_recovery_entry *db_entry = OSAL_NULL;
167         enum _ecore_status_t rc = ECORE_INVAL;
168         struct ecore_hwfn *p_hwfn;
169
170         /* shortcircuit VFs, for now */
171         if (IS_VF(p_dev)) {
172                 DP_VERBOSE(p_dev, ECORE_MSG_IOV, "db recovery - skipping VF doorbell\n");
173                 return ECORE_SUCCESS;
174         }
175
176         /* sanitize doorbell address */
177         if (!ecore_db_rec_sanity(p_dev, db_addr, db_data))
178                 return ECORE_INVAL;
179
180         /* obtain hwfn from doorbell address */
181         p_hwfn = ecore_db_rec_find_hwfn(p_dev, db_addr);
182
183         /* protect the list */
184         OSAL_SPIN_LOCK(&p_hwfn->db_recovery_info.lock);
185         OSAL_LIST_FOR_EACH_ENTRY(db_entry,
186                                  &p_hwfn->db_recovery_info.list,
187                                  list_entry,
188                                  struct ecore_db_recovery_entry) {
189                 /* search according to db_data addr since db_addr is not unique
190                  * (roce)
191                  */
192                 if (db_entry->db_data == db_data) {
193                         ecore_db_recovery_dp_entry(p_hwfn, db_entry,
194                                                    "Deleting");
195                         OSAL_LIST_REMOVE_ENTRY(&db_entry->list_entry,
196                                                &p_hwfn->db_recovery_info.list);
197                         rc = ECORE_SUCCESS;
198                         break;
199                 }
200         }
201
202         OSAL_SPIN_UNLOCK(&p_hwfn->db_recovery_info.lock);
203
204         if (rc == ECORE_INVAL)
205                 /*OSAL_WARN(true,*/
206                 DP_NOTICE(p_hwfn, false,
207                           "Failed to find element in list. Key (db_data addr) was %p. db_addr was %p\n",
208                           db_data, db_addr);
209         else
210                 OSAL_FREE(p_dev, db_entry);
211
212         return rc;
213 }
214
215 /* initialize the doorbell recovery mechanism */
216 enum _ecore_status_t ecore_db_recovery_setup(struct ecore_hwfn *p_hwfn)
217 {
218         DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ, "Setting up db recovery\n");
219
220         /* make sure db_size was set in p_dev */
221         if (!p_hwfn->p_dev->db_size) {
222                 DP_ERR(p_hwfn->p_dev, "db_size not set\n");
223                 return ECORE_INVAL;
224         }
225
226         OSAL_LIST_INIT(&p_hwfn->db_recovery_info.list);
227 #ifdef CONFIG_ECORE_LOCK_ALLOC
228         if (OSAL_SPIN_LOCK_ALLOC(p_hwfn, &p_hwfn->db_recovery_info.lock))
229                 return ECORE_NOMEM;
230 #endif
231         OSAL_SPIN_LOCK_INIT(&p_hwfn->db_recovery_info.lock);
232         p_hwfn->db_recovery_info.db_recovery_counter = 0;
233
234         return ECORE_SUCCESS;
235 }
236
237 /* destroy the doorbell recovery mechanism */
238 void ecore_db_recovery_teardown(struct ecore_hwfn *p_hwfn)
239 {
240         struct ecore_db_recovery_entry *db_entry = OSAL_NULL;
241
242         DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ, "Tearing down db recovery\n");
243         if (!OSAL_LIST_IS_EMPTY(&p_hwfn->db_recovery_info.list)) {
244                 DP_VERBOSE(p_hwfn, false, "Doorbell Recovery teardown found the doorbell recovery list was not empty (Expected in disorderly driver unload (e.g. recovery) otherwise this probably means some flow forgot to db_recovery_del). Prepare to purge doorbell recovery list...\n");
245                 while (!OSAL_LIST_IS_EMPTY(&p_hwfn->db_recovery_info.list)) {
246                         db_entry = OSAL_LIST_FIRST_ENTRY(
247                                                 &p_hwfn->db_recovery_info.list,
248                                                 struct ecore_db_recovery_entry,
249                                                 list_entry);
250                         ecore_db_recovery_dp_entry(p_hwfn, db_entry, "Purging");
251                         OSAL_LIST_REMOVE_ENTRY(&db_entry->list_entry,
252                                                &p_hwfn->db_recovery_info.list);
253                         OSAL_FREE(p_hwfn->p_dev, db_entry);
254                 }
255         }
256 #ifdef CONFIG_ECORE_LOCK_ALLOC
257         OSAL_SPIN_LOCK_DEALLOC(&p_hwfn->db_recovery_info.lock);
258 #endif
259         p_hwfn->db_recovery_info.db_recovery_counter = 0;
260 }
261
262 /* print the content of the doorbell recovery mechanism */
263 void ecore_db_recovery_dp(struct ecore_hwfn *p_hwfn)
264 {
265         struct ecore_db_recovery_entry *db_entry = OSAL_NULL;
266
267         DP_NOTICE(p_hwfn, false,
268                   "Dispalying doorbell recovery database. Counter was %d\n",
269                   p_hwfn->db_recovery_info.db_recovery_counter);
270
271         /* protect the list */
272         OSAL_SPIN_LOCK(&p_hwfn->db_recovery_info.lock);
273         OSAL_LIST_FOR_EACH_ENTRY(db_entry,
274                                  &p_hwfn->db_recovery_info.list,
275                                  list_entry,
276                                  struct ecore_db_recovery_entry) {
277                 ecore_db_recovery_dp_entry(p_hwfn, db_entry, "Printing");
278         }
279
280         OSAL_SPIN_UNLOCK(&p_hwfn->db_recovery_info.lock);
281 }
282
283 /* ring the doorbell of a single doorbell recovery entry */
284 void ecore_db_recovery_ring(struct ecore_hwfn *p_hwfn,
285                             struct ecore_db_recovery_entry *db_entry,
286                             enum ecore_db_rec_exec db_exec)
287 {
288         /* Print according to width */
289         if (db_entry->db_width == DB_REC_WIDTH_32B)
290                 DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ, "%s doorbell address %p data %x\n",
291                            db_exec == DB_REC_DRY_RUN ? "would have rung" : "ringing",
292                            db_entry->db_addr, *(u32 *)db_entry->db_data);
293         else
294                 DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ, "%s doorbell address %p data %lx\n",
295                            db_exec == DB_REC_DRY_RUN ? "would have rung" : "ringing",
296                            db_entry->db_addr,
297                            *(unsigned long *)(db_entry->db_data));
298
299         /* Sanity */
300         if (!ecore_db_rec_sanity(p_hwfn->p_dev, db_entry->db_addr,
301                                  db_entry->db_data))
302                 return;
303
304         /* Flush the write combined buffer. Since there are multiple doorbelling
305          * entities using the same address, if we don't flush, a transaction
306          * could be lost.
307          */
308         OSAL_WMB(p_hwfn->p_dev);
309
310         /* Ring the doorbell */
311         if (db_exec == DB_REC_REAL_DEAL || db_exec == DB_REC_ONCE) {
312                 if (db_entry->db_width == DB_REC_WIDTH_32B)
313                         DIRECT_REG_WR(p_hwfn, db_entry->db_addr,
314                                       *(u32 *)(db_entry->db_data));
315                 else
316                         DIRECT_REG_WR64(p_hwfn, db_entry->db_addr,
317                                         *(u64 *)(db_entry->db_data));
318         }
319
320         /* Flush the write combined buffer. Next doorbell may come from a
321          * different entity to the same address...
322          */
323         OSAL_WMB(p_hwfn->p_dev);
324 }
325
326 /* traverse the doorbell recovery entry list and ring all the doorbells */
327 void ecore_db_recovery_execute(struct ecore_hwfn *p_hwfn,
328                                enum ecore_db_rec_exec db_exec)
329 {
330         struct ecore_db_recovery_entry *db_entry = OSAL_NULL;
331
332         if (db_exec != DB_REC_ONCE) {
333                 DP_NOTICE(p_hwfn, false, "Executing doorbell recovery. Counter was %d\n",
334                           p_hwfn->db_recovery_info.db_recovery_counter);
335
336                 /* track amount of times recovery was executed */
337                 p_hwfn->db_recovery_info.db_recovery_counter++;
338         }
339
340         /* protect the list */
341         OSAL_SPIN_LOCK(&p_hwfn->db_recovery_info.lock);
342         OSAL_LIST_FOR_EACH_ENTRY(db_entry,
343                                  &p_hwfn->db_recovery_info.list,
344                                  list_entry,
345                                  struct ecore_db_recovery_entry) {
346                 ecore_db_recovery_ring(p_hwfn, db_entry, db_exec);
347                 if (db_exec == DB_REC_ONCE)
348                         break;
349         }
350
351         OSAL_SPIN_UNLOCK(&p_hwfn->db_recovery_info.lock);
352 }
353 /******************** Doorbell Recovery end ****************/
354
355 /* Configurable */
356 #define ECORE_MIN_DPIS          (4)     /* The minimal num of DPIs required to
357                                          * load the driver. The number was
358                                          * arbitrarily set.
359                                          */
360
361 /* Derived */
362 #define ECORE_MIN_PWM_REGION    (ECORE_WID_SIZE * ECORE_MIN_DPIS)
363
364 static u32 ecore_hw_bar_size(struct ecore_hwfn *p_hwfn,
365                              struct ecore_ptt *p_ptt,
366                              enum BAR_ID bar_id)
367 {
368         u32 bar_reg = (bar_id == BAR_ID_0 ?
369                        PGLUE_B_REG_PF_BAR0_SIZE : PGLUE_B_REG_PF_BAR1_SIZE);
370         u32 val;
371
372         if (IS_VF(p_hwfn->p_dev))
373                 return ecore_vf_hw_bar_size(p_hwfn, bar_id);
374
375         val = ecore_rd(p_hwfn, p_ptt, bar_reg);
376         if (val)
377                 return 1 << (val + 15);
378
379         /* The above registers were updated in the past only in CMT mode. Since
380          * they were found to be useful MFW started updating them from 8.7.7.0.
381          * In older MFW versions they are set to 0 which means disabled.
382          */
383         if (ECORE_IS_CMT(p_hwfn->p_dev)) {
384                 DP_INFO(p_hwfn,
385                         "BAR size not configured. Assuming BAR size of 256kB for GRC and 512kB for DB\n");
386                 val = BAR_ID_0 ? 256 * 1024 : 512 * 1024;
387         } else {
388                 DP_INFO(p_hwfn,
389                         "BAR size not configured. Assuming BAR size of 512kB for GRC and 512kB for DB\n");
390                 val = 512 * 1024;
391         }
392
393         return val;
394 }
395
396 void ecore_init_dp(struct ecore_dev *p_dev,
397                    u32 dp_module, u8 dp_level, void *dp_ctx)
398 {
399         u32 i;
400
401         p_dev->dp_level = dp_level;
402         p_dev->dp_module = dp_module;
403         p_dev->dp_ctx = dp_ctx;
404         for (i = 0; i < MAX_HWFNS_PER_DEVICE; i++) {
405                 struct ecore_hwfn *p_hwfn = &p_dev->hwfns[i];
406
407                 p_hwfn->dp_level = dp_level;
408                 p_hwfn->dp_module = dp_module;
409                 p_hwfn->dp_ctx = dp_ctx;
410         }
411 }
412
413 enum _ecore_status_t ecore_init_struct(struct ecore_dev *p_dev)
414 {
415         u8 i;
416
417         for (i = 0; i < MAX_HWFNS_PER_DEVICE; i++) {
418                 struct ecore_hwfn *p_hwfn = &p_dev->hwfns[i];
419
420                 p_hwfn->p_dev = p_dev;
421                 p_hwfn->my_id = i;
422                 p_hwfn->b_active = false;
423
424 #ifdef CONFIG_ECORE_LOCK_ALLOC
425                 if (OSAL_SPIN_LOCK_ALLOC(p_hwfn, &p_hwfn->dmae_info.lock))
426                         goto handle_err;
427 #endif
428                 OSAL_SPIN_LOCK_INIT(&p_hwfn->dmae_info.lock);
429         }
430
431         /* hwfn 0 is always active */
432         p_dev->hwfns[0].b_active = true;
433
434         /* set the default cache alignment to 128 (may be overridden later) */
435         p_dev->cache_shift = 7;
436         return ECORE_SUCCESS;
437 #ifdef CONFIG_ECORE_LOCK_ALLOC
438 handle_err:
439         while (--i) {
440                 struct ecore_hwfn *p_hwfn = OSAL_NULL;
441
442                 p_hwfn = &p_dev->hwfns[i];
443                 OSAL_SPIN_LOCK_DEALLOC(&p_hwfn->dmae_info.lock);
444         }
445         return ECORE_NOMEM;
446 #endif
447 }
448
449 static void ecore_qm_info_free(struct ecore_hwfn *p_hwfn)
450 {
451         struct ecore_qm_info *qm_info = &p_hwfn->qm_info;
452
453         OSAL_FREE(p_hwfn->p_dev, qm_info->qm_pq_params);
454         OSAL_FREE(p_hwfn->p_dev, qm_info->qm_vport_params);
455         OSAL_FREE(p_hwfn->p_dev, qm_info->qm_port_params);
456         OSAL_FREE(p_hwfn->p_dev, qm_info->wfq_data);
457 }
458
459 static void ecore_dbg_user_data_free(struct ecore_hwfn *p_hwfn)
460 {
461         OSAL_FREE(p_hwfn->p_dev, p_hwfn->dbg_user_info);
462         p_hwfn->dbg_user_info = OSAL_NULL;
463 }
464
465 void ecore_resc_free(struct ecore_dev *p_dev)
466 {
467         int i;
468
469         if (IS_VF(p_dev)) {
470                 for_each_hwfn(p_dev, i)
471                         ecore_l2_free(&p_dev->hwfns[i]);
472                 return;
473         }
474
475         OSAL_FREE(p_dev, p_dev->fw_data);
476
477         OSAL_FREE(p_dev, p_dev->reset_stats);
478
479         for_each_hwfn(p_dev, i) {
480                 struct ecore_hwfn *p_hwfn = &p_dev->hwfns[i];
481
482                 ecore_cxt_mngr_free(p_hwfn);
483                 ecore_qm_info_free(p_hwfn);
484                 ecore_spq_free(p_hwfn);
485                 ecore_eq_free(p_hwfn);
486                 ecore_consq_free(p_hwfn);
487                 ecore_int_free(p_hwfn);
488                 ecore_iov_free(p_hwfn);
489                 ecore_l2_free(p_hwfn);
490                 ecore_dmae_info_free(p_hwfn);
491                 ecore_dcbx_info_free(p_hwfn);
492                 ecore_dbg_user_data_free(p_hwfn);
493                 /* @@@TBD Flush work-queue ? */
494
495                 /* destroy doorbell recovery mechanism */
496                 ecore_db_recovery_teardown(p_hwfn);
497         }
498 }
499
500 /******************** QM initialization *******************/
501
502 /* bitmaps for indicating active traffic classes.
503  * Special case for Arrowhead 4 port
504  */
505 /* 0..3 actualy used, 4 serves OOO, 7 serves high priority stuff (e.g. DCQCN) */
506 #define ACTIVE_TCS_BMAP 0x9f
507 /* 0..3 actually used, OOO and high priority stuff all use 3 */
508 #define ACTIVE_TCS_BMAP_4PORT_K2 0xf
509
510 /* determines the physical queue flags for a given PF. */
511 static u32 ecore_get_pq_flags(struct ecore_hwfn *p_hwfn)
512 {
513         u32 flags;
514
515         /* common flags */
516         flags = PQ_FLAGS_LB;
517
518         /* feature flags */
519         if (IS_ECORE_SRIOV(p_hwfn->p_dev))
520                 flags |= PQ_FLAGS_VFS;
521         if (IS_ECORE_PACING(p_hwfn))
522                 flags |= PQ_FLAGS_RLS;
523
524         /* protocol flags */
525         switch (p_hwfn->hw_info.personality) {
526         case ECORE_PCI_ETH:
527                 if (!IS_ECORE_PACING(p_hwfn))
528                         flags |= PQ_FLAGS_MCOS;
529                 break;
530         case ECORE_PCI_FCOE:
531                 flags |= PQ_FLAGS_OFLD;
532                 break;
533         case ECORE_PCI_ISCSI:
534                 flags |= PQ_FLAGS_ACK | PQ_FLAGS_OOO | PQ_FLAGS_OFLD;
535                 break;
536         case ECORE_PCI_ETH_ROCE:
537                 flags |= PQ_FLAGS_OFLD | PQ_FLAGS_LLT;
538                 if (!IS_ECORE_PACING(p_hwfn))
539                         flags |= PQ_FLAGS_MCOS;
540                 break;
541         case ECORE_PCI_ETH_IWARP:
542                 flags |= PQ_FLAGS_ACK | PQ_FLAGS_OOO | PQ_FLAGS_OFLD;
543                 if (!IS_ECORE_PACING(p_hwfn))
544                         flags |= PQ_FLAGS_MCOS;
545                 break;
546         default:
547                 DP_ERR(p_hwfn, "unknown personality %d\n",
548                        p_hwfn->hw_info.personality);
549                 return 0;
550         }
551         return flags;
552 }
553
554 /* Getters for resource amounts necessary for qm initialization */
555 u8 ecore_init_qm_get_num_tcs(struct ecore_hwfn *p_hwfn)
556 {
557         return p_hwfn->hw_info.num_hw_tc;
558 }
559
560 u16 ecore_init_qm_get_num_vfs(struct ecore_hwfn *p_hwfn)
561 {
562         return IS_ECORE_SRIOV(p_hwfn->p_dev) ?
563                         p_hwfn->p_dev->p_iov_info->total_vfs : 0;
564 }
565
566 #define NUM_DEFAULT_RLS 1
567
568 u16 ecore_init_qm_get_num_pf_rls(struct ecore_hwfn *p_hwfn)
569 {
570         u16 num_pf_rls, num_vfs = ecore_init_qm_get_num_vfs(p_hwfn);
571
572         /* @DPDK */
573         /* num RLs can't exceed resource amount of rls or vports or the
574          * dcqcn qps
575          */
576         num_pf_rls = (u16)OSAL_MIN_T(u32, RESC_NUM(p_hwfn, ECORE_RL),
577                                      (u16)RESC_NUM(p_hwfn, ECORE_VPORT));
578
579         /* make sure after we reserve the default and VF rls we'll have
580          * something left
581          */
582         if (num_pf_rls < num_vfs + NUM_DEFAULT_RLS) {
583                 DP_NOTICE(p_hwfn, false,
584                           "no rate limiters left for PF rate limiting"
585                           " [num_pf_rls %d num_vfs %d]\n", num_pf_rls, num_vfs);
586                 return 0;
587         }
588
589         /* subtract rls necessary for VFs and one default one for the PF */
590         num_pf_rls -= num_vfs + NUM_DEFAULT_RLS;
591
592         return num_pf_rls;
593 }
594
595 u16 ecore_init_qm_get_num_vports(struct ecore_hwfn *p_hwfn)
596 {
597         u32 pq_flags = ecore_get_pq_flags(p_hwfn);
598
599         /* all pqs share the same vport (hence the 1 below), except for vfs
600          * and pf_rl pqs
601          */
602         return (!!(PQ_FLAGS_RLS & pq_flags)) *
603                 ecore_init_qm_get_num_pf_rls(p_hwfn) +
604                (!!(PQ_FLAGS_VFS & pq_flags)) *
605                 ecore_init_qm_get_num_vfs(p_hwfn) + 1;
606 }
607
608 /* calc amount of PQs according to the requested flags */
609 u16 ecore_init_qm_get_num_pqs(struct ecore_hwfn *p_hwfn)
610 {
611         u32 pq_flags = ecore_get_pq_flags(p_hwfn);
612
613         return (!!(PQ_FLAGS_RLS & pq_flags)) *
614                 ecore_init_qm_get_num_pf_rls(p_hwfn) +
615                (!!(PQ_FLAGS_MCOS & pq_flags)) *
616                 ecore_init_qm_get_num_tcs(p_hwfn) +
617                (!!(PQ_FLAGS_LB & pq_flags)) +
618                (!!(PQ_FLAGS_OOO & pq_flags)) +
619                (!!(PQ_FLAGS_ACK & pq_flags)) +
620                (!!(PQ_FLAGS_OFLD & pq_flags)) +
621                (!!(PQ_FLAGS_VFS & pq_flags)) *
622                 ecore_init_qm_get_num_vfs(p_hwfn);
623 }
624
625 /* initialize the top level QM params */
626 static void ecore_init_qm_params(struct ecore_hwfn *p_hwfn)
627 {
628         struct ecore_qm_info *qm_info = &p_hwfn->qm_info;
629         bool four_port;
630
631         /* pq and vport bases for this PF */
632         qm_info->start_pq = (u16)RESC_START(p_hwfn, ECORE_PQ);
633         qm_info->start_vport = (u8)RESC_START(p_hwfn, ECORE_VPORT);
634
635         /* rate limiting and weighted fair queueing are always enabled */
636         qm_info->vport_rl_en = 1;
637         qm_info->vport_wfq_en = 1;
638
639         /* TC config is different for AH 4 port */
640         four_port = p_hwfn->p_dev->num_ports_in_engine == MAX_NUM_PORTS_K2;
641
642         /* in AH 4 port we have fewer TCs per port */
643         qm_info->max_phys_tcs_per_port = four_port ? NUM_PHYS_TCS_4PORT_K2 :
644                                                      NUM_OF_PHYS_TCS;
645
646         /* unless MFW indicated otherwise, ooo_tc should be 3 for AH 4 port and
647          * 4 otherwise
648          */
649         if (!qm_info->ooo_tc)
650                 qm_info->ooo_tc = four_port ? DCBX_TCP_OOO_K2_4PORT_TC :
651                                               DCBX_TCP_OOO_TC;
652 }
653
654 /* initialize qm vport params */
655 static void ecore_init_qm_vport_params(struct ecore_hwfn *p_hwfn)
656 {
657         struct ecore_qm_info *qm_info = &p_hwfn->qm_info;
658         u8 i;
659
660         /* all vports participate in weighted fair queueing */
661         for (i = 0; i < ecore_init_qm_get_num_vports(p_hwfn); i++)
662                 qm_info->qm_vport_params[i].vport_wfq = 1;
663 }
664
665 /* initialize qm port params */
666 static void ecore_init_qm_port_params(struct ecore_hwfn *p_hwfn)
667 {
668         /* Initialize qm port parameters */
669         u8 i, active_phys_tcs, num_ports = p_hwfn->p_dev->num_ports_in_engine;
670
671         /* indicate how ooo and high pri traffic is dealt with */
672         active_phys_tcs = num_ports == MAX_NUM_PORTS_K2 ?
673                 ACTIVE_TCS_BMAP_4PORT_K2 : ACTIVE_TCS_BMAP;
674
675         for (i = 0; i < num_ports; i++) {
676                 struct init_qm_port_params *p_qm_port =
677                         &p_hwfn->qm_info.qm_port_params[i];
678
679                 p_qm_port->active = 1;
680                 p_qm_port->active_phys_tcs = active_phys_tcs;
681                 p_qm_port->num_pbf_cmd_lines = PBF_MAX_CMD_LINES_E4 / num_ports;
682                 p_qm_port->num_btb_blocks = BTB_MAX_BLOCKS / num_ports;
683         }
684 }
685
686 /* Reset the params which must be reset for qm init. QM init may be called as
687  * a result of flows other than driver load (e.g. dcbx renegotiation). Other
688  * params may be affected by the init but would simply recalculate to the same
689  * values. The allocations made for QM init, ports, vports, pqs and vfqs are not
690  * affected as these amounts stay the same.
691  */
692 static void ecore_init_qm_reset_params(struct ecore_hwfn *p_hwfn)
693 {
694         struct ecore_qm_info *qm_info = &p_hwfn->qm_info;
695
696         qm_info->num_pqs = 0;
697         qm_info->num_vports = 0;
698         qm_info->num_pf_rls = 0;
699         qm_info->num_vf_pqs = 0;
700         qm_info->first_vf_pq = 0;
701         qm_info->first_mcos_pq = 0;
702         qm_info->first_rl_pq = 0;
703 }
704
705 static void ecore_init_qm_advance_vport(struct ecore_hwfn *p_hwfn)
706 {
707         struct ecore_qm_info *qm_info = &p_hwfn->qm_info;
708
709         qm_info->num_vports++;
710
711         if (qm_info->num_vports > ecore_init_qm_get_num_vports(p_hwfn))
712                 DP_ERR(p_hwfn,
713                        "vport overflow! qm_info->num_vports %d,"
714                        " qm_init_get_num_vports() %d\n",
715                        qm_info->num_vports,
716                        ecore_init_qm_get_num_vports(p_hwfn));
717 }
718
719 /* initialize a single pq and manage qm_info resources accounting.
720  * The pq_init_flags param determines whether the PQ is rate limited
721  * (for VF or PF)
722  * and whether a new vport is allocated to the pq or not (i.e. vport will be
723  * shared)
724  */
725
726 /* flags for pq init */
727 #define PQ_INIT_SHARE_VPORT     (1 << 0)
728 #define PQ_INIT_PF_RL           (1 << 1)
729 #define PQ_INIT_VF_RL           (1 << 2)
730
731 /* defines for pq init */
732 #define PQ_INIT_DEFAULT_WRR_GROUP       1
733 #define PQ_INIT_DEFAULT_TC              0
734 #define PQ_INIT_OFLD_TC                 (p_hwfn->hw_info.offload_tc)
735
736 static void ecore_init_qm_pq(struct ecore_hwfn *p_hwfn,
737                              struct ecore_qm_info *qm_info,
738                              u8 tc, u32 pq_init_flags)
739 {
740         u16 pq_idx = qm_info->num_pqs, max_pq =
741                                         ecore_init_qm_get_num_pqs(p_hwfn);
742
743         if (pq_idx > max_pq)
744                 DP_ERR(p_hwfn,
745                        "pq overflow! pq %d, max pq %d\n", pq_idx, max_pq);
746
747         /* init pq params */
748         qm_info->qm_pq_params[pq_idx].port_id = p_hwfn->port_id;
749         qm_info->qm_pq_params[pq_idx].vport_id = qm_info->start_vport +
750                                                  qm_info->num_vports;
751         qm_info->qm_pq_params[pq_idx].tc_id = tc;
752         qm_info->qm_pq_params[pq_idx].wrr_group = PQ_INIT_DEFAULT_WRR_GROUP;
753         qm_info->qm_pq_params[pq_idx].rl_valid =
754                 (pq_init_flags & PQ_INIT_PF_RL ||
755                  pq_init_flags & PQ_INIT_VF_RL);
756
757         /* qm params accounting */
758         qm_info->num_pqs++;
759         if (!(pq_init_flags & PQ_INIT_SHARE_VPORT))
760                 qm_info->num_vports++;
761
762         if (pq_init_flags & PQ_INIT_PF_RL)
763                 qm_info->num_pf_rls++;
764
765         if (qm_info->num_vports > ecore_init_qm_get_num_vports(p_hwfn))
766                 DP_ERR(p_hwfn,
767                        "vport overflow! qm_info->num_vports %d,"
768                        " qm_init_get_num_vports() %d\n",
769                        qm_info->num_vports,
770                        ecore_init_qm_get_num_vports(p_hwfn));
771
772         if (qm_info->num_pf_rls > ecore_init_qm_get_num_pf_rls(p_hwfn))
773                 DP_ERR(p_hwfn, "rl overflow! qm_info->num_pf_rls %d,"
774                        " qm_init_get_num_pf_rls() %d\n",
775                        qm_info->num_pf_rls,
776                        ecore_init_qm_get_num_pf_rls(p_hwfn));
777 }
778
779 /* get pq index according to PQ_FLAGS */
780 static u16 *ecore_init_qm_get_idx_from_flags(struct ecore_hwfn *p_hwfn,
781                                              u32 pq_flags)
782 {
783         struct ecore_qm_info *qm_info = &p_hwfn->qm_info;
784
785         /* Can't have multiple flags set here */
786         if (OSAL_BITMAP_WEIGHT((unsigned long *)&pq_flags,
787                                 sizeof(pq_flags)) > 1)
788                 goto err;
789
790         switch (pq_flags) {
791         case PQ_FLAGS_RLS:
792                 return &qm_info->first_rl_pq;
793         case PQ_FLAGS_MCOS:
794                 return &qm_info->first_mcos_pq;
795         case PQ_FLAGS_LB:
796                 return &qm_info->pure_lb_pq;
797         case PQ_FLAGS_OOO:
798                 return &qm_info->ooo_pq;
799         case PQ_FLAGS_ACK:
800                 return &qm_info->pure_ack_pq;
801         case PQ_FLAGS_OFLD:
802                 return &qm_info->offload_pq;
803         case PQ_FLAGS_VFS:
804                 return &qm_info->first_vf_pq;
805         default:
806                 goto err;
807         }
808
809 err:
810         DP_ERR(p_hwfn, "BAD pq flags %d\n", pq_flags);
811         return OSAL_NULL;
812 }
813
814 /* save pq index in qm info */
815 static void ecore_init_qm_set_idx(struct ecore_hwfn *p_hwfn,
816                                   u32 pq_flags, u16 pq_val)
817 {
818         u16 *base_pq_idx = ecore_init_qm_get_idx_from_flags(p_hwfn, pq_flags);
819
820         *base_pq_idx = p_hwfn->qm_info.start_pq + pq_val;
821 }
822
823 /* get tx pq index, with the PQ TX base already set (ready for context init) */
824 u16 ecore_get_cm_pq_idx(struct ecore_hwfn *p_hwfn, u32 pq_flags)
825 {
826         u16 *base_pq_idx = ecore_init_qm_get_idx_from_flags(p_hwfn, pq_flags);
827
828         return *base_pq_idx + CM_TX_PQ_BASE;
829 }
830
831 u16 ecore_get_cm_pq_idx_mcos(struct ecore_hwfn *p_hwfn, u8 tc)
832 {
833         u8 max_tc = ecore_init_qm_get_num_tcs(p_hwfn);
834
835         if (tc > max_tc)
836                 DP_ERR(p_hwfn, "tc %d must be smaller than %d\n", tc, max_tc);
837
838         return ecore_get_cm_pq_idx(p_hwfn, PQ_FLAGS_MCOS) + tc;
839 }
840
841 u16 ecore_get_cm_pq_idx_vf(struct ecore_hwfn *p_hwfn, u16 vf)
842 {
843         u16 max_vf = ecore_init_qm_get_num_vfs(p_hwfn);
844
845         if (vf > max_vf)
846                 DP_ERR(p_hwfn, "vf %d must be smaller than %d\n", vf, max_vf);
847
848         return ecore_get_cm_pq_idx(p_hwfn, PQ_FLAGS_VFS) + vf;
849 }
850
851 u16 ecore_get_cm_pq_idx_rl(struct ecore_hwfn *p_hwfn, u16 rl)
852 {
853         u16 max_rl = ecore_init_qm_get_num_pf_rls(p_hwfn);
854
855         if (rl > max_rl)
856                 DP_ERR(p_hwfn, "rl %d must be smaller than %d\n", rl, max_rl);
857
858         return ecore_get_cm_pq_idx(p_hwfn, PQ_FLAGS_RLS) + rl;
859 }
860
861 u16 ecore_get_qm_vport_idx_rl(struct ecore_hwfn *p_hwfn, u16 rl)
862 {
863         u16 start_pq, pq, qm_pq_idx;
864
865         pq = ecore_get_cm_pq_idx_rl(p_hwfn, rl);
866         start_pq = p_hwfn->qm_info.start_pq;
867         qm_pq_idx = pq - start_pq - CM_TX_PQ_BASE;
868
869         if (qm_pq_idx > p_hwfn->qm_info.num_pqs) {
870                 DP_ERR(p_hwfn,
871                        "qm_pq_idx %d must be smaller than %d\n",
872                         qm_pq_idx, p_hwfn->qm_info.num_pqs);
873         }
874
875         return p_hwfn->qm_info.qm_pq_params[qm_pq_idx].vport_id;
876 }
877
878 /* Functions for creating specific types of pqs */
879 static void ecore_init_qm_lb_pq(struct ecore_hwfn *p_hwfn)
880 {
881         struct ecore_qm_info *qm_info = &p_hwfn->qm_info;
882
883         if (!(ecore_get_pq_flags(p_hwfn) & PQ_FLAGS_LB))
884                 return;
885
886         ecore_init_qm_set_idx(p_hwfn, PQ_FLAGS_LB, qm_info->num_pqs);
887         ecore_init_qm_pq(p_hwfn, qm_info, PURE_LB_TC, PQ_INIT_SHARE_VPORT);
888 }
889
890 static void ecore_init_qm_ooo_pq(struct ecore_hwfn *p_hwfn)
891 {
892         struct ecore_qm_info *qm_info = &p_hwfn->qm_info;
893
894         if (!(ecore_get_pq_flags(p_hwfn) & PQ_FLAGS_OOO))
895                 return;
896
897         ecore_init_qm_set_idx(p_hwfn, PQ_FLAGS_OOO, qm_info->num_pqs);
898         ecore_init_qm_pq(p_hwfn, qm_info, qm_info->ooo_tc, PQ_INIT_SHARE_VPORT);
899 }
900
901 static void ecore_init_qm_pure_ack_pq(struct ecore_hwfn *p_hwfn)
902 {
903         struct ecore_qm_info *qm_info = &p_hwfn->qm_info;
904
905         if (!(ecore_get_pq_flags(p_hwfn) & PQ_FLAGS_ACK))
906                 return;
907
908         ecore_init_qm_set_idx(p_hwfn, PQ_FLAGS_ACK, qm_info->num_pqs);
909         ecore_init_qm_pq(p_hwfn, qm_info, PQ_INIT_OFLD_TC, PQ_INIT_SHARE_VPORT);
910 }
911
912 static void ecore_init_qm_offload_pq(struct ecore_hwfn *p_hwfn)
913 {
914         struct ecore_qm_info *qm_info = &p_hwfn->qm_info;
915
916         if (!(ecore_get_pq_flags(p_hwfn) & PQ_FLAGS_OFLD))
917                 return;
918
919         ecore_init_qm_set_idx(p_hwfn, PQ_FLAGS_OFLD, qm_info->num_pqs);
920         ecore_init_qm_pq(p_hwfn, qm_info, PQ_INIT_OFLD_TC, PQ_INIT_SHARE_VPORT);
921 }
922
923 static void ecore_init_qm_mcos_pqs(struct ecore_hwfn *p_hwfn)
924 {
925         struct ecore_qm_info *qm_info = &p_hwfn->qm_info;
926         u8 tc_idx;
927
928         if (!(ecore_get_pq_flags(p_hwfn) & PQ_FLAGS_MCOS))
929                 return;
930
931         ecore_init_qm_set_idx(p_hwfn, PQ_FLAGS_MCOS, qm_info->num_pqs);
932         for (tc_idx = 0; tc_idx < ecore_init_qm_get_num_tcs(p_hwfn); tc_idx++)
933                 ecore_init_qm_pq(p_hwfn, qm_info, tc_idx, PQ_INIT_SHARE_VPORT);
934 }
935
936 static void ecore_init_qm_vf_pqs(struct ecore_hwfn *p_hwfn)
937 {
938         struct ecore_qm_info *qm_info = &p_hwfn->qm_info;
939         u16 vf_idx, num_vfs = ecore_init_qm_get_num_vfs(p_hwfn);
940
941         if (!(ecore_get_pq_flags(p_hwfn) & PQ_FLAGS_VFS))
942                 return;
943
944         ecore_init_qm_set_idx(p_hwfn, PQ_FLAGS_VFS, qm_info->num_pqs);
945
946         qm_info->num_vf_pqs = num_vfs;
947         for (vf_idx = 0; vf_idx < num_vfs; vf_idx++)
948                 ecore_init_qm_pq(p_hwfn, qm_info, PQ_INIT_DEFAULT_TC,
949                                  PQ_INIT_VF_RL);
950 }
951
952 static void ecore_init_qm_rl_pqs(struct ecore_hwfn *p_hwfn)
953 {
954         u16 pf_rls_idx, num_pf_rls = ecore_init_qm_get_num_pf_rls(p_hwfn);
955         struct ecore_qm_info *qm_info = &p_hwfn->qm_info;
956
957         if (!(ecore_get_pq_flags(p_hwfn) & PQ_FLAGS_RLS))
958                 return;
959
960         ecore_init_qm_set_idx(p_hwfn, PQ_FLAGS_RLS, qm_info->num_pqs);
961         for (pf_rls_idx = 0; pf_rls_idx < num_pf_rls; pf_rls_idx++)
962                 ecore_init_qm_pq(p_hwfn, qm_info, PQ_INIT_OFLD_TC,
963                                  PQ_INIT_PF_RL);
964 }
965
966 static void ecore_init_qm_pq_params(struct ecore_hwfn *p_hwfn)
967 {
968         /* rate limited pqs, must come first (FW assumption) */
969         ecore_init_qm_rl_pqs(p_hwfn);
970
971         /* pqs for multi cos */
972         ecore_init_qm_mcos_pqs(p_hwfn);
973
974         /* pure loopback pq */
975         ecore_init_qm_lb_pq(p_hwfn);
976
977         /* out of order pq */
978         ecore_init_qm_ooo_pq(p_hwfn);
979
980         /* pure ack pq */
981         ecore_init_qm_pure_ack_pq(p_hwfn);
982
983         /* pq for offloaded protocol */
984         ecore_init_qm_offload_pq(p_hwfn);
985
986         /* done sharing vports */
987         ecore_init_qm_advance_vport(p_hwfn);
988
989         /* pqs for vfs */
990         ecore_init_qm_vf_pqs(p_hwfn);
991 }
992
993 /* compare values of getters against resources amounts */
994 static enum _ecore_status_t ecore_init_qm_sanity(struct ecore_hwfn *p_hwfn)
995 {
996         if (ecore_init_qm_get_num_vports(p_hwfn) >
997             RESC_NUM(p_hwfn, ECORE_VPORT)) {
998                 DP_ERR(p_hwfn, "requested amount of vports exceeds resource\n");
999                 return ECORE_INVAL;
1000         }
1001
1002         if (ecore_init_qm_get_num_pqs(p_hwfn) > RESC_NUM(p_hwfn, ECORE_PQ)) {
1003                 DP_ERR(p_hwfn, "requested amount of pqs exceeds resource\n");
1004                 return ECORE_INVAL;
1005         }
1006
1007         return ECORE_SUCCESS;
1008 }
1009
1010 /*
1011  * Function for verbose printing of the qm initialization results
1012  */
1013 static void ecore_dp_init_qm_params(struct ecore_hwfn *p_hwfn)
1014 {
1015         struct ecore_qm_info *qm_info = &p_hwfn->qm_info;
1016         struct init_qm_vport_params *vport;
1017         struct init_qm_port_params *port;
1018         struct init_qm_pq_params *pq;
1019         int i, tc;
1020
1021         /* top level params */
1022         DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
1023                    "qm init top level params: start_pq %d, start_vport %d,"
1024                    " pure_lb_pq %d, offload_pq %d, pure_ack_pq %d\n",
1025                    qm_info->start_pq, qm_info->start_vport, qm_info->pure_lb_pq,
1026                    qm_info->offload_pq, qm_info->pure_ack_pq);
1027         DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
1028                    "ooo_pq %d, first_vf_pq %d, num_pqs %d, num_vf_pqs %d,"
1029                    " num_vports %d, max_phys_tcs_per_port %d\n",
1030                    qm_info->ooo_pq, qm_info->first_vf_pq, qm_info->num_pqs,
1031                    qm_info->num_vf_pqs, qm_info->num_vports,
1032                    qm_info->max_phys_tcs_per_port);
1033         DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
1034                    "pf_rl_en %d, pf_wfq_en %d, vport_rl_en %d, vport_wfq_en %d,"
1035                    " pf_wfq %d, pf_rl %d, num_pf_rls %d, pq_flags %x\n",
1036                    qm_info->pf_rl_en, qm_info->pf_wfq_en, qm_info->vport_rl_en,
1037                    qm_info->vport_wfq_en, qm_info->pf_wfq, qm_info->pf_rl,
1038                    qm_info->num_pf_rls, ecore_get_pq_flags(p_hwfn));
1039
1040         /* port table */
1041         for (i = 0; i < p_hwfn->p_dev->num_ports_in_engine; i++) {
1042                 port = &qm_info->qm_port_params[i];
1043                 DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
1044                            "port idx %d, active %d, active_phys_tcs %d,"
1045                            " num_pbf_cmd_lines %d, num_btb_blocks %d,"
1046                            " reserved %d\n",
1047                            i, port->active, port->active_phys_tcs,
1048                            port->num_pbf_cmd_lines, port->num_btb_blocks,
1049                            port->reserved);
1050         }
1051
1052         /* vport table */
1053         for (i = 0; i < qm_info->num_vports; i++) {
1054                 vport = &qm_info->qm_vport_params[i];
1055                 DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
1056                            "vport idx %d, vport_rl %d, wfq %d,"
1057                            " first_tx_pq_id [ ",
1058                            qm_info->start_vport + i, vport->vport_rl,
1059                            vport->vport_wfq);
1060                 for (tc = 0; tc < NUM_OF_TCS; tc++)
1061                         DP_VERBOSE(p_hwfn, ECORE_MSG_HW, "%d ",
1062                                    vport->first_tx_pq_id[tc]);
1063                 DP_VERBOSE(p_hwfn, ECORE_MSG_HW, "]\n");
1064         }
1065
1066         /* pq table */
1067         for (i = 0; i < qm_info->num_pqs; i++) {
1068                 pq = &qm_info->qm_pq_params[i];
1069                 DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
1070                            "pq idx %d, port %d, vport_id %d, tc %d, wrr_grp %d, rl_valid %d\n",
1071                            qm_info->start_pq + i, pq->port_id, pq->vport_id,
1072                            pq->tc_id, pq->wrr_group, pq->rl_valid);
1073         }
1074 }
1075
1076 static void ecore_init_qm_info(struct ecore_hwfn *p_hwfn)
1077 {
1078         /* reset params required for init run */
1079         ecore_init_qm_reset_params(p_hwfn);
1080
1081         /* init QM top level params */
1082         ecore_init_qm_params(p_hwfn);
1083
1084         /* init QM port params */
1085         ecore_init_qm_port_params(p_hwfn);
1086
1087         /* init QM vport params */
1088         ecore_init_qm_vport_params(p_hwfn);
1089
1090         /* init QM physical queue params */
1091         ecore_init_qm_pq_params(p_hwfn);
1092
1093         /* display all that init */
1094         ecore_dp_init_qm_params(p_hwfn);
1095 }
1096
1097 /* This function reconfigures the QM pf on the fly.
1098  * For this purpose we:
1099  * 1. reconfigure the QM database
1100  * 2. set new values to runtime array
1101  * 3. send an sdm_qm_cmd through the rbc interface to stop the QM
1102  * 4. activate init tool in QM_PF stage
1103  * 5. send an sdm_qm_cmd through rbc interface to release the QM
1104  */
1105 enum _ecore_status_t ecore_qm_reconf(struct ecore_hwfn *p_hwfn,
1106                                      struct ecore_ptt *p_ptt)
1107 {
1108         struct ecore_qm_info *qm_info = &p_hwfn->qm_info;
1109         bool b_rc;
1110         enum _ecore_status_t rc;
1111
1112         /* initialize ecore's qm data structure */
1113         ecore_init_qm_info(p_hwfn);
1114
1115         /* stop PF's qm queues */
1116         OSAL_SPIN_LOCK(&qm_lock);
1117         b_rc = ecore_send_qm_stop_cmd(p_hwfn, p_ptt, false, true,
1118                                       qm_info->start_pq, qm_info->num_pqs);
1119         OSAL_SPIN_UNLOCK(&qm_lock);
1120         if (!b_rc)
1121                 return ECORE_INVAL;
1122
1123         /* clear the QM_PF runtime phase leftovers from previous init */
1124         ecore_init_clear_rt_data(p_hwfn);
1125
1126         /* prepare QM portion of runtime array */
1127         ecore_qm_init_pf(p_hwfn, p_ptt, false);
1128
1129         /* activate init tool on runtime array */
1130         rc = ecore_init_run(p_hwfn, p_ptt, PHASE_QM_PF, p_hwfn->rel_pf_id,
1131                             p_hwfn->hw_info.hw_mode);
1132         if (rc != ECORE_SUCCESS)
1133                 return rc;
1134
1135         /* start PF's qm queues */
1136         OSAL_SPIN_LOCK(&qm_lock);
1137         b_rc = ecore_send_qm_stop_cmd(p_hwfn, p_ptt, true, true,
1138                                       qm_info->start_pq, qm_info->num_pqs);
1139         OSAL_SPIN_UNLOCK(&qm_lock);
1140         if (!b_rc)
1141                 return ECORE_INVAL;
1142
1143         return ECORE_SUCCESS;
1144 }
1145
1146 static enum _ecore_status_t ecore_alloc_qm_data(struct ecore_hwfn *p_hwfn)
1147 {
1148         struct ecore_qm_info *qm_info = &p_hwfn->qm_info;
1149         enum _ecore_status_t rc;
1150
1151         rc = ecore_init_qm_sanity(p_hwfn);
1152         if (rc != ECORE_SUCCESS)
1153                 goto alloc_err;
1154
1155         qm_info->qm_pq_params = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL,
1156                                             sizeof(struct init_qm_pq_params) *
1157                                             ecore_init_qm_get_num_pqs(p_hwfn));
1158         if (!qm_info->qm_pq_params)
1159                 goto alloc_err;
1160
1161         qm_info->qm_vport_params = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL,
1162                                        sizeof(struct init_qm_vport_params) *
1163                                        ecore_init_qm_get_num_vports(p_hwfn));
1164         if (!qm_info->qm_vport_params)
1165                 goto alloc_err;
1166
1167         qm_info->qm_port_params = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL,
1168                                       sizeof(struct init_qm_port_params) *
1169                                       p_hwfn->p_dev->num_ports_in_engine);
1170         if (!qm_info->qm_port_params)
1171                 goto alloc_err;
1172
1173         qm_info->wfq_data = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL,
1174                                         sizeof(struct ecore_wfq_data) *
1175                                         ecore_init_qm_get_num_vports(p_hwfn));
1176         if (!qm_info->wfq_data)
1177                 goto alloc_err;
1178
1179         return ECORE_SUCCESS;
1180
1181 alloc_err:
1182         DP_NOTICE(p_hwfn, false, "Failed to allocate memory for QM params\n");
1183         ecore_qm_info_free(p_hwfn);
1184         return ECORE_NOMEM;
1185 }
1186 /******************** End QM initialization ***************/
1187
1188 enum _ecore_status_t ecore_resc_alloc(struct ecore_dev *p_dev)
1189 {
1190         enum _ecore_status_t rc = ECORE_SUCCESS;
1191         int i;
1192
1193         if (IS_VF(p_dev)) {
1194                 for_each_hwfn(p_dev, i) {
1195                         rc = ecore_l2_alloc(&p_dev->hwfns[i]);
1196                         if (rc != ECORE_SUCCESS)
1197                                 return rc;
1198                 }
1199                 return rc;
1200         }
1201
1202         p_dev->fw_data = OSAL_ZALLOC(p_dev, GFP_KERNEL,
1203                                      sizeof(*p_dev->fw_data));
1204         if (!p_dev->fw_data)
1205                 return ECORE_NOMEM;
1206
1207         for_each_hwfn(p_dev, i) {
1208                 struct ecore_hwfn *p_hwfn = &p_dev->hwfns[i];
1209                 u32 n_eqes, num_cons;
1210
1211                 /* initialize the doorbell recovery mechanism */
1212                 rc = ecore_db_recovery_setup(p_hwfn);
1213                 if (rc)
1214                         goto alloc_err;
1215
1216                 /* First allocate the context manager structure */
1217                 rc = ecore_cxt_mngr_alloc(p_hwfn);
1218                 if (rc)
1219                         goto alloc_err;
1220
1221                 /* Set the HW cid/tid numbers (in the context manager)
1222                  * Must be done prior to any further computations.
1223                  */
1224                 rc = ecore_cxt_set_pf_params(p_hwfn);
1225                 if (rc)
1226                         goto alloc_err;
1227
1228                 rc = ecore_alloc_qm_data(p_hwfn);
1229                 if (rc)
1230                         goto alloc_err;
1231
1232                 /* init qm info */
1233                 ecore_init_qm_info(p_hwfn);
1234
1235                 /* Compute the ILT client partition */
1236                 rc = ecore_cxt_cfg_ilt_compute(p_hwfn);
1237                 if (rc)
1238                         goto alloc_err;
1239
1240                 /* CID map / ILT shadow table / T2
1241                  * The talbes sizes are determined by the computations above
1242                  */
1243                 rc = ecore_cxt_tables_alloc(p_hwfn);
1244                 if (rc)
1245                         goto alloc_err;
1246
1247                 /* SPQ, must follow ILT because initializes SPQ context */
1248                 rc = ecore_spq_alloc(p_hwfn);
1249                 if (rc)
1250                         goto alloc_err;
1251
1252                 /* SP status block allocation */
1253                 p_hwfn->p_dpc_ptt = ecore_get_reserved_ptt(p_hwfn,
1254                                                            RESERVED_PTT_DPC);
1255
1256                 rc = ecore_int_alloc(p_hwfn, p_hwfn->p_main_ptt);
1257                 if (rc)
1258                         goto alloc_err;
1259
1260                 rc = ecore_iov_alloc(p_hwfn);
1261                 if (rc)
1262                         goto alloc_err;
1263
1264                 /* EQ */
1265                 n_eqes = ecore_chain_get_capacity(&p_hwfn->p_spq->chain);
1266                 if (ECORE_IS_RDMA_PERSONALITY(p_hwfn)) {
1267                         /* Calculate the EQ size
1268                          * ---------------------
1269                          * Each ICID may generate up to one event at a time i.e.
1270                          * the event must be handled/cleared before a new one
1271                          * can be generated. We calculate the sum of events per
1272                          * protocol and create an EQ deep enough to handle the
1273                          * worst case:
1274                          * - Core - according to SPQ.
1275                          * - RoCE - per QP there are a couple of ICIDs, one
1276                          *        responder and one requester, each can
1277                          *        generate an EQE => n_eqes_qp = 2 * n_qp.
1278                          *        Each CQ can generate an EQE. There are 2 CQs
1279                          *        per QP => n_eqes_cq = 2 * n_qp.
1280                          *        Hence the RoCE total is 4 * n_qp or
1281                          *        2 * num_cons.
1282                          * - ENet - There can be up to two events per VF. One
1283                          *        for VF-PF channel and another for VF FLR
1284                          *        initial cleanup. The number of VFs is
1285                          *        bounded by MAX_NUM_VFS_BB, and is much
1286                          *        smaller than RoCE's so we avoid exact
1287                          *        calculation.
1288                          */
1289                         if (ECORE_IS_ROCE_PERSONALITY(p_hwfn)) {
1290                                 num_cons =
1291                                     ecore_cxt_get_proto_cid_count(
1292                                                 p_hwfn,
1293                                                 PROTOCOLID_ROCE,
1294                                                 OSAL_NULL);
1295                                 num_cons *= 2;
1296                         } else {
1297                                 num_cons = ecore_cxt_get_proto_cid_count(
1298                                                 p_hwfn,
1299                                                 PROTOCOLID_IWARP,
1300                                                 OSAL_NULL);
1301                         }
1302                         n_eqes += num_cons + 2 * MAX_NUM_VFS_BB;
1303                 } else if (p_hwfn->hw_info.personality == ECORE_PCI_ISCSI) {
1304                         num_cons =
1305                             ecore_cxt_get_proto_cid_count(p_hwfn,
1306                                                           PROTOCOLID_ISCSI,
1307                                                           OSAL_NULL);
1308                         n_eqes += 2 * num_cons;
1309                 }
1310
1311                 if (n_eqes > 0xFFFF) {
1312                         DP_ERR(p_hwfn, "Cannot allocate 0x%x EQ elements."
1313                                        "The maximum of a u16 chain is 0x%x\n",
1314                                n_eqes, 0xFFFF);
1315                         goto alloc_no_mem;
1316                 }
1317
1318                 rc = ecore_eq_alloc(p_hwfn, (u16)n_eqes);
1319                 if (rc)
1320                         goto alloc_err;
1321
1322                 rc = ecore_consq_alloc(p_hwfn);
1323                 if (rc)
1324                         goto alloc_err;
1325
1326                 rc = ecore_l2_alloc(p_hwfn);
1327                 if (rc != ECORE_SUCCESS)
1328                         goto alloc_err;
1329
1330                 /* DMA info initialization */
1331                 rc = ecore_dmae_info_alloc(p_hwfn);
1332                 if (rc) {
1333                         DP_NOTICE(p_hwfn, false, "Failed to allocate memory for dmae_info structure\n");
1334                         goto alloc_err;
1335                 }
1336
1337                 /* DCBX initialization */
1338                 rc = ecore_dcbx_info_alloc(p_hwfn);
1339                 if (rc) {
1340                         DP_NOTICE(p_hwfn, false,
1341                                   "Failed to allocate memory for dcbx structure\n");
1342                         goto alloc_err;
1343                 }
1344
1345                 rc = OSAL_DBG_ALLOC_USER_DATA(p_hwfn, &p_hwfn->dbg_user_info);
1346                 if (rc) {
1347                         DP_NOTICE(p_hwfn, false,
1348                                   "Failed to allocate dbg user info structure\n");
1349                         goto alloc_err;
1350                 }
1351         } /* hwfn loop */
1352
1353         p_dev->reset_stats = OSAL_ZALLOC(p_dev, GFP_KERNEL,
1354                                          sizeof(*p_dev->reset_stats));
1355         if (!p_dev->reset_stats) {
1356                 DP_NOTICE(p_dev, false, "Failed to allocate reset statistics\n");
1357                 goto alloc_no_mem;
1358         }
1359
1360         return ECORE_SUCCESS;
1361
1362 alloc_no_mem:
1363         rc = ECORE_NOMEM;
1364 alloc_err:
1365         ecore_resc_free(p_dev);
1366         return rc;
1367 }
1368
1369 void ecore_resc_setup(struct ecore_dev *p_dev)
1370 {
1371         int i;
1372
1373         if (IS_VF(p_dev)) {
1374                 for_each_hwfn(p_dev, i)
1375                         ecore_l2_setup(&p_dev->hwfns[i]);
1376                 return;
1377         }
1378
1379         for_each_hwfn(p_dev, i) {
1380                 struct ecore_hwfn *p_hwfn = &p_dev->hwfns[i];
1381
1382                 ecore_cxt_mngr_setup(p_hwfn);
1383                 ecore_spq_setup(p_hwfn);
1384                 ecore_eq_setup(p_hwfn);
1385                 ecore_consq_setup(p_hwfn);
1386
1387                 /* Read shadow of current MFW mailbox */
1388                 ecore_mcp_read_mb(p_hwfn, p_hwfn->p_main_ptt);
1389                 OSAL_MEMCPY(p_hwfn->mcp_info->mfw_mb_shadow,
1390                             p_hwfn->mcp_info->mfw_mb_cur,
1391                             p_hwfn->mcp_info->mfw_mb_length);
1392
1393                 ecore_int_setup(p_hwfn, p_hwfn->p_main_ptt);
1394
1395                 ecore_l2_setup(p_hwfn);
1396                 ecore_iov_setup(p_hwfn);
1397         }
1398 }
1399
1400 #define FINAL_CLEANUP_POLL_CNT  (100)
1401 #define FINAL_CLEANUP_POLL_TIME (10)
1402 enum _ecore_status_t ecore_final_cleanup(struct ecore_hwfn *p_hwfn,
1403                                          struct ecore_ptt *p_ptt,
1404                                          u16 id, bool is_vf)
1405 {
1406         u32 command = 0, addr, count = FINAL_CLEANUP_POLL_CNT;
1407         enum _ecore_status_t rc = ECORE_TIMEOUT;
1408
1409 #ifndef ASIC_ONLY
1410         if (CHIP_REV_IS_TEDIBEAR(p_hwfn->p_dev) ||
1411             CHIP_REV_IS_SLOW(p_hwfn->p_dev)) {
1412                 DP_INFO(p_hwfn, "Skipping final cleanup for non-ASIC\n");
1413                 return ECORE_SUCCESS;
1414         }
1415 #endif
1416
1417         addr = GTT_BAR0_MAP_REG_USDM_RAM +
1418             USTORM_FLR_FINAL_ACK_OFFSET(p_hwfn->rel_pf_id);
1419
1420         if (is_vf)
1421                 id += 0x10;
1422
1423         command |= X_FINAL_CLEANUP_AGG_INT <<
1424             SDM_AGG_INT_COMP_PARAMS_AGG_INT_INDEX_SHIFT;
1425         command |= 1 << SDM_AGG_INT_COMP_PARAMS_AGG_VECTOR_ENABLE_SHIFT;
1426         command |= id << SDM_AGG_INT_COMP_PARAMS_AGG_VECTOR_BIT_SHIFT;
1427         command |= SDM_COMP_TYPE_AGG_INT << SDM_OP_GEN_COMP_TYPE_SHIFT;
1428
1429 /* Make sure notification is not set before initiating final cleanup */
1430
1431         if (REG_RD(p_hwfn, addr)) {
1432                 DP_NOTICE(p_hwfn, false,
1433                           "Unexpected; Found final cleanup notification");
1434                 DP_NOTICE(p_hwfn, false,
1435                           " before initiating final cleanup\n");
1436                 REG_WR(p_hwfn, addr, 0);
1437         }
1438
1439         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1440                    "Sending final cleanup for PFVF[%d] [Command %08x]\n",
1441                    id, command);
1442
1443         ecore_wr(p_hwfn, p_ptt, XSDM_REG_OPERATION_GEN, command);
1444
1445         /* Poll until completion */
1446         while (!REG_RD(p_hwfn, addr) && count--)
1447                 OSAL_MSLEEP(FINAL_CLEANUP_POLL_TIME);
1448
1449         if (REG_RD(p_hwfn, addr))
1450                 rc = ECORE_SUCCESS;
1451         else
1452                 DP_NOTICE(p_hwfn, true,
1453                           "Failed to receive FW final cleanup notification\n");
1454
1455         /* Cleanup afterwards */
1456         REG_WR(p_hwfn, addr, 0);
1457
1458         return rc;
1459 }
1460
1461 static enum _ecore_status_t ecore_calc_hw_mode(struct ecore_hwfn *p_hwfn)
1462 {
1463         int hw_mode = 0;
1464
1465         if (ECORE_IS_BB_B0(p_hwfn->p_dev)) {
1466                 hw_mode |= 1 << MODE_BB;
1467         } else if (ECORE_IS_AH(p_hwfn->p_dev)) {
1468                 hw_mode |= 1 << MODE_K2;
1469         } else {
1470                 DP_NOTICE(p_hwfn, true, "Unknown chip type %#x\n",
1471                           p_hwfn->p_dev->type);
1472                 return ECORE_INVAL;
1473         }
1474
1475         /* Ports per engine is based on the values in CNIG_REG_NW_PORT_MODE */
1476         switch (p_hwfn->p_dev->num_ports_in_engine) {
1477         case 1:
1478                 hw_mode |= 1 << MODE_PORTS_PER_ENG_1;
1479                 break;
1480         case 2:
1481                 hw_mode |= 1 << MODE_PORTS_PER_ENG_2;
1482                 break;
1483         case 4:
1484                 hw_mode |= 1 << MODE_PORTS_PER_ENG_4;
1485                 break;
1486         default:
1487                 DP_NOTICE(p_hwfn, true,
1488                           "num_ports_in_engine = %d not supported\n",
1489                           p_hwfn->p_dev->num_ports_in_engine);
1490                 return ECORE_INVAL;
1491         }
1492
1493         if (OSAL_TEST_BIT(ECORE_MF_OVLAN_CLSS,
1494                           &p_hwfn->p_dev->mf_bits))
1495                 hw_mode |= 1 << MODE_MF_SD;
1496         else
1497                 hw_mode |= 1 << MODE_MF_SI;
1498
1499 #ifndef ASIC_ONLY
1500         if (CHIP_REV_IS_SLOW(p_hwfn->p_dev)) {
1501                 if (CHIP_REV_IS_FPGA(p_hwfn->p_dev)) {
1502                         hw_mode |= 1 << MODE_FPGA;
1503                 } else {
1504                         if (p_hwfn->p_dev->b_is_emul_full)
1505                                 hw_mode |= 1 << MODE_EMUL_FULL;
1506                         else
1507                                 hw_mode |= 1 << MODE_EMUL_REDUCED;
1508                 }
1509         } else
1510 #endif
1511                 hw_mode |= 1 << MODE_ASIC;
1512
1513         if (ECORE_IS_CMT(p_hwfn->p_dev))
1514                 hw_mode |= 1 << MODE_100G;
1515
1516         p_hwfn->hw_info.hw_mode = hw_mode;
1517
1518         DP_VERBOSE(p_hwfn, (ECORE_MSG_PROBE | ECORE_MSG_IFUP),
1519                    "Configuring function for hw_mode: 0x%08x\n",
1520                    p_hwfn->hw_info.hw_mode);
1521
1522         return ECORE_SUCCESS;
1523 }
1524
1525 #ifndef ASIC_ONLY
1526 /* MFW-replacement initializations for non-ASIC */
1527 static enum _ecore_status_t ecore_hw_init_chip(struct ecore_hwfn *p_hwfn,
1528                                                struct ecore_ptt *p_ptt)
1529 {
1530         struct ecore_dev *p_dev = p_hwfn->p_dev;
1531         u32 pl_hv = 1;
1532         int i;
1533
1534         if (CHIP_REV_IS_EMUL(p_dev)) {
1535                 if (ECORE_IS_AH(p_dev))
1536                         pl_hv |= 0x600;
1537         }
1538
1539         ecore_wr(p_hwfn, p_ptt, MISCS_REG_RESET_PL_HV + 4, pl_hv);
1540
1541         if (CHIP_REV_IS_EMUL(p_dev) &&
1542             (ECORE_IS_AH(p_dev)))
1543                 ecore_wr(p_hwfn, p_ptt, MISCS_REG_RESET_PL_HV_2_K2_E5,
1544                          0x3ffffff);
1545
1546         /* initialize port mode to 4x10G_E (10G with 4x10 SERDES) */
1547         /* CNIG_REG_NW_PORT_MODE is same for A0 and B0 */
1548         if (!CHIP_REV_IS_EMUL(p_dev) || ECORE_IS_BB(p_dev))
1549                 ecore_wr(p_hwfn, p_ptt, CNIG_REG_NW_PORT_MODE_BB, 4);
1550
1551         if (CHIP_REV_IS_EMUL(p_dev)) {
1552                 if (ECORE_IS_AH(p_dev)) {
1553                         /* 2 for 4-port, 1 for 2-port, 0 for 1-port */
1554                         ecore_wr(p_hwfn, p_ptt, MISC_REG_PORT_MODE,
1555                                  (p_dev->num_ports_in_engine >> 1));
1556
1557                         ecore_wr(p_hwfn, p_ptt, MISC_REG_BLOCK_256B_EN,
1558                                  p_dev->num_ports_in_engine == 4 ? 0 : 3);
1559                 }
1560         }
1561
1562         /* Poll on RBC */
1563         ecore_wr(p_hwfn, p_ptt, PSWRQ2_REG_RBC_DONE, 1);
1564         for (i = 0; i < 100; i++) {
1565                 OSAL_UDELAY(50);
1566                 if (ecore_rd(p_hwfn, p_ptt, PSWRQ2_REG_CFG_DONE) == 1)
1567                         break;
1568         }
1569         if (i == 100)
1570                 DP_NOTICE(p_hwfn, true,
1571                           "RBC done failed to complete in PSWRQ2\n");
1572
1573         return ECORE_SUCCESS;
1574 }
1575 #endif
1576
1577 /* Init run time data for all PFs and their VFs on an engine.
1578  * TBD - for VFs - Once we have parent PF info for each VF in
1579  * shmem available as CAU requires knowledge of parent PF for each VF.
1580  */
1581 static void ecore_init_cau_rt_data(struct ecore_dev *p_dev)
1582 {
1583         u32 offset = CAU_REG_SB_VAR_MEMORY_RT_OFFSET;
1584         int i, igu_sb_id;
1585
1586         for_each_hwfn(p_dev, i) {
1587                 struct ecore_hwfn *p_hwfn = &p_dev->hwfns[i];
1588                 struct ecore_igu_info *p_igu_info;
1589                 struct ecore_igu_block *p_block;
1590                 struct cau_sb_entry sb_entry;
1591
1592                 p_igu_info = p_hwfn->hw_info.p_igu_info;
1593
1594                 for (igu_sb_id = 0;
1595                      igu_sb_id < ECORE_MAPPING_MEMORY_SIZE(p_dev);
1596                      igu_sb_id++) {
1597                         p_block = &p_igu_info->entry[igu_sb_id];
1598
1599                         if (!p_block->is_pf)
1600                                 continue;
1601
1602                         ecore_init_cau_sb_entry(p_hwfn, &sb_entry,
1603                                                 p_block->function_id, 0, 0);
1604                         STORE_RT_REG_AGG(p_hwfn, offset + igu_sb_id * 2,
1605                                          sb_entry);
1606                 }
1607         }
1608 }
1609
1610 static void ecore_init_cache_line_size(struct ecore_hwfn *p_hwfn,
1611                                        struct ecore_ptt *p_ptt)
1612 {
1613         u32 val, wr_mbs, cache_line_size;
1614
1615         val = ecore_rd(p_hwfn, p_ptt, PSWRQ2_REG_WR_MBS0);
1616         switch (val) {
1617         case 0:
1618                 wr_mbs = 128;
1619                 break;
1620         case 1:
1621                 wr_mbs = 256;
1622                 break;
1623         case 2:
1624                 wr_mbs = 512;
1625                 break;
1626         default:
1627                 DP_INFO(p_hwfn,
1628                         "Unexpected value of PSWRQ2_REG_WR_MBS0 [0x%x]. Avoid configuring PGLUE_B_REG_CACHE_LINE_SIZE.\n",
1629                         val);
1630                 return;
1631         }
1632
1633         cache_line_size = OSAL_MIN_T(u32, OSAL_CACHE_LINE_SIZE, wr_mbs);
1634         switch (cache_line_size) {
1635         case 32:
1636                 val = 0;
1637                 break;
1638         case 64:
1639                 val = 1;
1640                 break;
1641         case 128:
1642                 val = 2;
1643                 break;
1644         case 256:
1645                 val = 3;
1646                 break;
1647         default:
1648                 DP_INFO(p_hwfn,
1649                         "Unexpected value of cache line size [0x%x]. Avoid configuring PGLUE_B_REG_CACHE_LINE_SIZE.\n",
1650                         cache_line_size);
1651         }
1652
1653         if (wr_mbs < OSAL_CACHE_LINE_SIZE)
1654                 DP_INFO(p_hwfn,
1655                         "The cache line size for padding is suboptimal for performance [OS cache line size 0x%x, wr mbs 0x%x]\n",
1656                         OSAL_CACHE_LINE_SIZE, wr_mbs);
1657
1658         STORE_RT_REG(p_hwfn, PGLUE_REG_B_CACHE_LINE_SIZE_RT_OFFSET, val);
1659         if (val > 0) {
1660                 STORE_RT_REG(p_hwfn, PSWRQ2_REG_DRAM_ALIGN_WR_RT_OFFSET, val);
1661                 STORE_RT_REG(p_hwfn, PSWRQ2_REG_DRAM_ALIGN_RD_RT_OFFSET, val);
1662         }
1663 }
1664
1665 static enum _ecore_status_t ecore_hw_init_common(struct ecore_hwfn *p_hwfn,
1666                                                  struct ecore_ptt *p_ptt,
1667                                                  int hw_mode)
1668 {
1669         struct ecore_qm_info *qm_info = &p_hwfn->qm_info;
1670         struct ecore_dev *p_dev = p_hwfn->p_dev;
1671         u8 vf_id, max_num_vfs;
1672         u16 num_pfs, pf_id;
1673         u32 concrete_fid;
1674         enum _ecore_status_t rc = ECORE_SUCCESS;
1675
1676         ecore_init_cau_rt_data(p_dev);
1677
1678         /* Program GTT windows */
1679         ecore_gtt_init(p_hwfn, p_ptt);
1680
1681 #ifndef ASIC_ONLY
1682         if (CHIP_REV_IS_EMUL(p_dev)) {
1683                 rc = ecore_hw_init_chip(p_hwfn, p_ptt);
1684                 if (rc != ECORE_SUCCESS)
1685                         return rc;
1686         }
1687 #endif
1688
1689         if (p_hwfn->mcp_info) {
1690                 if (p_hwfn->mcp_info->func_info.bandwidth_max)
1691                         qm_info->pf_rl_en = 1;
1692                 if (p_hwfn->mcp_info->func_info.bandwidth_min)
1693                         qm_info->pf_wfq_en = 1;
1694         }
1695
1696         ecore_qm_common_rt_init(p_hwfn,
1697                                 p_dev->num_ports_in_engine,
1698                                 qm_info->max_phys_tcs_per_port,
1699                                 qm_info->pf_rl_en, qm_info->pf_wfq_en,
1700                                 qm_info->vport_rl_en, qm_info->vport_wfq_en,
1701                                 qm_info->qm_port_params);
1702
1703         ecore_cxt_hw_init_common(p_hwfn);
1704
1705         ecore_init_cache_line_size(p_hwfn, p_ptt);
1706
1707         rc = ecore_init_run(p_hwfn, p_ptt, PHASE_ENGINE, ECORE_PATH_ID(p_hwfn),
1708                             hw_mode);
1709         if (rc != ECORE_SUCCESS)
1710                 return rc;
1711
1712         /* @@TBD MichalK - should add VALIDATE_VFID to init tool...
1713          * need to decide with which value, maybe runtime
1714          */
1715         ecore_wr(p_hwfn, p_ptt, PSWRQ2_REG_L2P_VALIDATE_VFID, 0);
1716         ecore_wr(p_hwfn, p_ptt, PGLUE_B_REG_USE_CLIENTID_IN_TAG, 1);
1717
1718         if (ECORE_IS_BB(p_dev)) {
1719                 /* Workaround clears ROCE search for all functions to prevent
1720                  * involving non initialized function in processing ROCE packet.
1721                  */
1722                 num_pfs = NUM_OF_ENG_PFS(p_dev);
1723                 for (pf_id = 0; pf_id < num_pfs; pf_id++) {
1724                         ecore_fid_pretend(p_hwfn, p_ptt, pf_id);
1725                         ecore_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_ROCE, 0x0);
1726                         ecore_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_TCP, 0x0);
1727                 }
1728                 /* pretend to original PF */
1729                 ecore_fid_pretend(p_hwfn, p_ptt, p_hwfn->rel_pf_id);
1730         }
1731
1732         /* Workaround for avoiding CCFC execution error when getting packets
1733          * with CRC errors, and allowing instead the invoking of the FW error
1734          * handler.
1735          * This is not done inside the init tool since it currently can't
1736          * perform a pretending to VFs.
1737          */
1738         max_num_vfs = ECORE_IS_AH(p_dev) ? MAX_NUM_VFS_K2 : MAX_NUM_VFS_BB;
1739         for (vf_id = 0; vf_id < max_num_vfs; vf_id++) {
1740                 concrete_fid = ecore_vfid_to_concrete(p_hwfn, vf_id);
1741                 ecore_fid_pretend(p_hwfn, p_ptt, (u16)concrete_fid);
1742                 ecore_wr(p_hwfn, p_ptt, CCFC_REG_STRONG_ENABLE_VF, 0x1);
1743                 ecore_wr(p_hwfn, p_ptt, CCFC_REG_WEAK_ENABLE_VF, 0x0);
1744                 ecore_wr(p_hwfn, p_ptt, TCFC_REG_STRONG_ENABLE_VF, 0x1);
1745                 ecore_wr(p_hwfn, p_ptt, TCFC_REG_WEAK_ENABLE_VF, 0x0);
1746         }
1747         /* pretend to original PF */
1748         ecore_fid_pretend(p_hwfn, p_ptt, p_hwfn->rel_pf_id);
1749
1750         return rc;
1751 }
1752
1753 #ifndef ASIC_ONLY
1754 #define MISC_REG_RESET_REG_2_XMAC_BIT (1 << 4)
1755 #define MISC_REG_RESET_REG_2_XMAC_SOFT_BIT (1 << 5)
1756
1757 #define PMEG_IF_BYTE_COUNT      8
1758
1759 static void ecore_wr_nw_port(struct ecore_hwfn *p_hwfn,
1760                              struct ecore_ptt *p_ptt,
1761                              u32 addr, u64 data, u8 reg_type, u8 port)
1762 {
1763         DP_VERBOSE(p_hwfn, ECORE_MSG_LINK,
1764                    "CMD: %08x, ADDR: 0x%08x, DATA: %08x:%08x\n",
1765                    ecore_rd(p_hwfn, p_ptt, CNIG_REG_PMEG_IF_CMD_BB) |
1766                    (8 << PMEG_IF_BYTE_COUNT),
1767                    (reg_type << 25) | (addr << 8) | port,
1768                    (u32)((data >> 32) & 0xffffffff),
1769                    (u32)(data & 0xffffffff));
1770
1771         ecore_wr(p_hwfn, p_ptt, CNIG_REG_PMEG_IF_CMD_BB,
1772                  (ecore_rd(p_hwfn, p_ptt, CNIG_REG_PMEG_IF_CMD_BB) &
1773                   0xffff00fe) | (8 << PMEG_IF_BYTE_COUNT));
1774         ecore_wr(p_hwfn, p_ptt, CNIG_REG_PMEG_IF_ADDR_BB,
1775                  (reg_type << 25) | (addr << 8) | port);
1776         ecore_wr(p_hwfn, p_ptt, CNIG_REG_PMEG_IF_WRDATA_BB, data & 0xffffffff);
1777         ecore_wr(p_hwfn, p_ptt, CNIG_REG_PMEG_IF_WRDATA_BB,
1778                  (data >> 32) & 0xffffffff);
1779 }
1780
1781 #define XLPORT_MODE_REG (0x20a)
1782 #define XLPORT_MAC_CONTROL (0x210)
1783 #define XLPORT_FLOW_CONTROL_CONFIG (0x207)
1784 #define XLPORT_ENABLE_REG (0x20b)
1785
1786 #define XLMAC_CTRL (0x600)
1787 #define XLMAC_MODE (0x601)
1788 #define XLMAC_RX_MAX_SIZE (0x608)
1789 #define XLMAC_TX_CTRL (0x604)
1790 #define XLMAC_PAUSE_CTRL (0x60d)
1791 #define XLMAC_PFC_CTRL (0x60e)
1792
1793 static void ecore_emul_link_init_bb(struct ecore_hwfn *p_hwfn,
1794                                     struct ecore_ptt *p_ptt)
1795 {
1796         u8 loopback = 0, port = p_hwfn->port_id * 2;
1797
1798         DP_INFO(p_hwfn->p_dev, "Configurating Emulation Link %02x\n", port);
1799
1800         /* XLPORT MAC MODE *//* 0 Quad, 4 Single... */
1801         ecore_wr_nw_port(p_hwfn, p_ptt, XLPORT_MODE_REG, (0x4 << 4) | 0x4, 1,
1802                          port);
1803         ecore_wr_nw_port(p_hwfn, p_ptt, XLPORT_MAC_CONTROL, 0, 1, port);
1804         /* XLMAC: SOFT RESET */
1805         ecore_wr_nw_port(p_hwfn, p_ptt, XLMAC_CTRL, 0x40, 0, port);
1806         /* XLMAC: Port Speed >= 10Gbps */
1807         ecore_wr_nw_port(p_hwfn, p_ptt, XLMAC_MODE, 0x40, 0, port);
1808         /* XLMAC: Max Size */
1809         ecore_wr_nw_port(p_hwfn, p_ptt, XLMAC_RX_MAX_SIZE, 0x3fff, 0, port);
1810         ecore_wr_nw_port(p_hwfn, p_ptt, XLMAC_TX_CTRL,
1811                          0x01000000800ULL | (0xa << 12) | ((u64)1 << 38),
1812                          0, port);
1813         ecore_wr_nw_port(p_hwfn, p_ptt, XLMAC_PAUSE_CTRL, 0x7c000, 0, port);
1814         ecore_wr_nw_port(p_hwfn, p_ptt, XLMAC_PFC_CTRL,
1815                          0x30ffffc000ULL, 0, port);
1816         ecore_wr_nw_port(p_hwfn, p_ptt, XLMAC_CTRL, 0x3 | (loopback << 2), 0,
1817                          port); /* XLMAC: TX_EN, RX_EN */
1818         /* XLMAC: TX_EN, RX_EN, SW_LINK_STATUS */
1819         ecore_wr_nw_port(p_hwfn, p_ptt, XLMAC_CTRL,
1820                          0x1003 | (loopback << 2), 0, port);
1821         /* Enabled Parallel PFC interface */
1822         ecore_wr_nw_port(p_hwfn, p_ptt, XLPORT_FLOW_CONTROL_CONFIG, 1, 0, port);
1823
1824         /* XLPORT port enable */
1825         ecore_wr_nw_port(p_hwfn, p_ptt, XLPORT_ENABLE_REG, 0xf, 1, port);
1826 }
1827
1828 static void ecore_emul_link_init_ah_e5(struct ecore_hwfn *p_hwfn,
1829                                        struct ecore_ptt *p_ptt)
1830 {
1831         u8 port = p_hwfn->port_id;
1832         u32 mac_base = NWM_REG_MAC0_K2_E5 + (port << 2) * NWM_REG_MAC0_SIZE;
1833
1834         DP_INFO(p_hwfn->p_dev, "Configurating Emulation Link %02x\n", port);
1835
1836         ecore_wr(p_hwfn, p_ptt, CNIG_REG_NIG_PORT0_CONF_K2_E5 + (port << 2),
1837                  (1 << CNIG_REG_NIG_PORT0_CONF_NIG_PORT_ENABLE_0_K2_E5_SHIFT) |
1838                  (port <<
1839                   CNIG_REG_NIG_PORT0_CONF_NIG_PORT_NWM_PORT_MAP_0_K2_E5_SHIFT) |
1840                  (0 << CNIG_REG_NIG_PORT0_CONF_NIG_PORT_RATE_0_K2_E5_SHIFT));
1841
1842         ecore_wr(p_hwfn, p_ptt, mac_base + ETH_MAC_REG_XIF_MODE_K2_E5,
1843                  1 << ETH_MAC_REG_XIF_MODE_XGMII_K2_E5_SHIFT);
1844
1845         ecore_wr(p_hwfn, p_ptt, mac_base + ETH_MAC_REG_FRM_LENGTH_K2_E5,
1846                  9018 << ETH_MAC_REG_FRM_LENGTH_FRM_LENGTH_K2_E5_SHIFT);
1847
1848         ecore_wr(p_hwfn, p_ptt, mac_base + ETH_MAC_REG_TX_IPG_LENGTH_K2_E5,
1849                  0xc << ETH_MAC_REG_TX_IPG_LENGTH_TXIPG_K2_E5_SHIFT);
1850
1851         ecore_wr(p_hwfn, p_ptt, mac_base + ETH_MAC_REG_RX_FIFO_SECTIONS_K2_E5,
1852                  8 << ETH_MAC_REG_RX_FIFO_SECTIONS_RX_SECTION_FULL_K2_E5_SHIFT);
1853
1854         ecore_wr(p_hwfn, p_ptt, mac_base + ETH_MAC_REG_TX_FIFO_SECTIONS_K2_E5,
1855                  (0xA <<
1856                   ETH_MAC_REG_TX_FIFO_SECTIONS_TX_SECTION_EMPTY_K2_E5_SHIFT) |
1857                  (8 <<
1858                   ETH_MAC_REG_TX_FIFO_SECTIONS_TX_SECTION_FULL_K2_E5_SHIFT));
1859
1860         ecore_wr(p_hwfn, p_ptt, mac_base + ETH_MAC_REG_COMMAND_CONFIG_K2_E5,
1861                  0xa853);
1862 }
1863
1864 static void ecore_emul_link_init(struct ecore_hwfn *p_hwfn,
1865                                  struct ecore_ptt *p_ptt)
1866 {
1867         if (ECORE_IS_AH(p_hwfn->p_dev))
1868                 ecore_emul_link_init_ah_e5(p_hwfn, p_ptt);
1869         else /* BB */
1870                 ecore_emul_link_init_bb(p_hwfn, p_ptt);
1871 }
1872
1873 static void ecore_link_init_bb(struct ecore_hwfn *p_hwfn,
1874                                struct ecore_ptt *p_ptt,  u8 port)
1875 {
1876         int port_offset = port ? 0x800 : 0;
1877         u32 xmac_rxctrl = 0;
1878
1879         /* Reset of XMAC */
1880         /* FIXME: move to common start */
1881         ecore_wr(p_hwfn, p_ptt, MISC_REG_RESET_PL_PDA_VAUX + 2 * sizeof(u32),
1882                  MISC_REG_RESET_REG_2_XMAC_BIT);        /* Clear */
1883         OSAL_MSLEEP(1);
1884         ecore_wr(p_hwfn, p_ptt, MISC_REG_RESET_PL_PDA_VAUX + sizeof(u32),
1885                  MISC_REG_RESET_REG_2_XMAC_BIT);        /* Set */
1886
1887         ecore_wr(p_hwfn, p_ptt, MISC_REG_XMAC_CORE_PORT_MODE_BB, 1);
1888
1889         /* Set the number of ports on the Warp Core to 10G */
1890         ecore_wr(p_hwfn, p_ptt, MISC_REG_XMAC_PHY_PORT_MODE_BB, 3);
1891
1892         /* Soft reset of XMAC */
1893         ecore_wr(p_hwfn, p_ptt, MISC_REG_RESET_PL_PDA_VAUX + 2 * sizeof(u32),
1894                  MISC_REG_RESET_REG_2_XMAC_SOFT_BIT);
1895         OSAL_MSLEEP(1);
1896         ecore_wr(p_hwfn, p_ptt, MISC_REG_RESET_PL_PDA_VAUX + sizeof(u32),
1897                  MISC_REG_RESET_REG_2_XMAC_SOFT_BIT);
1898
1899         /* FIXME: move to common end */
1900         if (CHIP_REV_IS_FPGA(p_hwfn->p_dev))
1901                 ecore_wr(p_hwfn, p_ptt, XMAC_REG_MODE_BB + port_offset, 0x20);
1902
1903         /* Set Max packet size: initialize XMAC block register for port 0 */
1904         ecore_wr(p_hwfn, p_ptt, XMAC_REG_RX_MAX_SIZE_BB + port_offset, 0x2710);
1905
1906         /* CRC append for Tx packets: init XMAC block register for port 1 */
1907         ecore_wr(p_hwfn, p_ptt, XMAC_REG_TX_CTRL_LO_BB + port_offset, 0xC800);
1908
1909         /* Enable TX and RX: initialize XMAC block register for port 1 */
1910         ecore_wr(p_hwfn, p_ptt, XMAC_REG_CTRL_BB + port_offset,
1911                  XMAC_REG_CTRL_TX_EN_BB | XMAC_REG_CTRL_RX_EN_BB);
1912         xmac_rxctrl = ecore_rd(p_hwfn, p_ptt,
1913                                XMAC_REG_RX_CTRL_BB + port_offset);
1914         xmac_rxctrl |= XMAC_REG_RX_CTRL_PROCESS_VARIABLE_PREAMBLE_BB;
1915         ecore_wr(p_hwfn, p_ptt, XMAC_REG_RX_CTRL_BB + port_offset, xmac_rxctrl);
1916 }
1917 #endif
1918
1919 static enum _ecore_status_t
1920 ecore_hw_init_dpi_size(struct ecore_hwfn *p_hwfn,
1921                        struct ecore_ptt *p_ptt, u32 pwm_region_size, u32 n_cpus)
1922 {
1923         u32 dpi_bit_shift, dpi_count, dpi_page_size;
1924         u32 min_dpis;
1925         u32 n_wids;
1926
1927         /* Calculate DPI size
1928          * ------------------
1929          * The PWM region contains Doorbell Pages. The first is reserverd for
1930          * the kernel for, e.g, L2. The others are free to be used by non-
1931          * trusted applications, typically from user space. Each page, called a
1932          * doorbell page is sectioned into windows that allow doorbells to be
1933          * issued in parallel by the kernel/application. The size of such a
1934          * window (a.k.a. WID) is 1kB.
1935          * Summary:
1936          *    1kB WID x N WIDS = DPI page size
1937          *    DPI page size x N DPIs = PWM region size
1938          * Notes:
1939          * The size of the DPI page size must be in multiples of OSAL_PAGE_SIZE
1940          * in order to ensure that two applications won't share the same page.
1941          * It also must contain at least one WID per CPU to allow parallelism.
1942          * It also must be a power of 2, since it is stored as a bit shift.
1943          *
1944          * The DPI page size is stored in a register as 'dpi_bit_shift' so that
1945          * 0 is 4kB, 1 is 8kB and etc. Hence the minimum size is 4,096
1946          * containing 4 WIDs.
1947          */
1948         n_wids = OSAL_MAX_T(u32, ECORE_MIN_WIDS, n_cpus);
1949         dpi_page_size = ECORE_WID_SIZE * OSAL_ROUNDUP_POW_OF_TWO(n_wids);
1950         dpi_page_size = (dpi_page_size + OSAL_PAGE_SIZE - 1) &
1951                         ~(OSAL_PAGE_SIZE - 1);
1952         dpi_bit_shift = OSAL_LOG2(dpi_page_size / 4096);
1953         dpi_count = pwm_region_size / dpi_page_size;
1954
1955         min_dpis = p_hwfn->pf_params.rdma_pf_params.min_dpis;
1956         min_dpis = OSAL_MAX_T(u32, ECORE_MIN_DPIS, min_dpis);
1957
1958         /* Update hwfn */
1959         p_hwfn->dpi_size = dpi_page_size;
1960         p_hwfn->dpi_count = dpi_count;
1961
1962         /* Update registers */
1963         ecore_wr(p_hwfn, p_ptt, DORQ_REG_PF_DPI_BIT_SHIFT, dpi_bit_shift);
1964
1965         if (dpi_count < min_dpis)
1966                 return ECORE_NORESOURCES;
1967
1968         return ECORE_SUCCESS;
1969 }
1970
1971 enum ECORE_ROCE_EDPM_MODE {
1972         ECORE_ROCE_EDPM_MODE_ENABLE = 0,
1973         ECORE_ROCE_EDPM_MODE_FORCE_ON = 1,
1974         ECORE_ROCE_EDPM_MODE_DISABLE = 2,
1975 };
1976
1977 bool ecore_edpm_enabled(struct ecore_hwfn *p_hwfn)
1978 {
1979         if (p_hwfn->dcbx_no_edpm || p_hwfn->db_bar_no_edpm)
1980                 return false;
1981
1982         return true;
1983 }
1984
1985 static enum _ecore_status_t
1986 ecore_hw_init_pf_doorbell_bar(struct ecore_hwfn *p_hwfn,
1987                               struct ecore_ptt *p_ptt)
1988 {
1989         u32 pwm_regsize, norm_regsize;
1990         u32 non_pwm_conn, min_addr_reg1;
1991         u32 db_bar_size, n_cpus;
1992         u32 roce_edpm_mode;
1993         u32 pf_dems_shift;
1994         enum _ecore_status_t rc = ECORE_SUCCESS;
1995         u8 cond;
1996
1997         db_bar_size = ecore_hw_bar_size(p_hwfn, p_ptt, BAR_ID_1);
1998         if (ECORE_IS_CMT(p_hwfn->p_dev))
1999                 db_bar_size /= 2;
2000
2001         /* Calculate doorbell regions
2002          * -----------------------------------
2003          * The doorbell BAR is made of two regions. The first is called normal
2004          * region and the second is called PWM region. In the normal region
2005          * each ICID has its own set of addresses so that writing to that
2006          * specific address identifies the ICID. In the Process Window Mode
2007          * region the ICID is given in the data written to the doorbell. The
2008          * above per PF register denotes the offset in the doorbell BAR in which
2009          * the PWM region begins.
2010          * The normal region has ECORE_PF_DEMS_SIZE bytes per ICID, that is per
2011          * non-PWM connection. The calculation below computes the total non-PWM
2012          * connections. The DORQ_REG_PF_MIN_ADDR_REG1 register is
2013          * in units of 4,096 bytes.
2014          */
2015         non_pwm_conn = ecore_cxt_get_proto_cid_start(p_hwfn, PROTOCOLID_CORE) +
2016             ecore_cxt_get_proto_cid_count(p_hwfn, PROTOCOLID_CORE,
2017                                           OSAL_NULL) +
2018             ecore_cxt_get_proto_cid_count(p_hwfn, PROTOCOLID_ETH, OSAL_NULL);
2019         norm_regsize = ROUNDUP(ECORE_PF_DEMS_SIZE * non_pwm_conn,
2020                                OSAL_PAGE_SIZE);
2021         min_addr_reg1 = norm_regsize / 4096;
2022         pwm_regsize = db_bar_size - norm_regsize;
2023
2024         /* Check that the normal and PWM sizes are valid */
2025         if (db_bar_size < norm_regsize) {
2026                 DP_ERR(p_hwfn->p_dev,
2027                        "Doorbell BAR size 0x%x is too small (normal region is 0x%0x )\n",
2028                        db_bar_size, norm_regsize);
2029                 return ECORE_NORESOURCES;
2030         }
2031         if (pwm_regsize < ECORE_MIN_PWM_REGION) {
2032                 DP_ERR(p_hwfn->p_dev,
2033                        "PWM region size 0x%0x is too small. Should be at least 0x%0x (Doorbell BAR size is 0x%x and normal region size is 0x%0x)\n",
2034                        pwm_regsize, ECORE_MIN_PWM_REGION, db_bar_size,
2035                        norm_regsize);
2036                 return ECORE_NORESOURCES;
2037         }
2038
2039         /* Calculate number of DPIs */
2040         roce_edpm_mode = p_hwfn->pf_params.rdma_pf_params.roce_edpm_mode;
2041         if ((roce_edpm_mode == ECORE_ROCE_EDPM_MODE_ENABLE) ||
2042             ((roce_edpm_mode == ECORE_ROCE_EDPM_MODE_FORCE_ON))) {
2043                 /* Either EDPM is mandatory, or we are attempting to allocate a
2044                  * WID per CPU.
2045                  */
2046                 n_cpus = OSAL_NUM_CPUS();
2047                 rc = ecore_hw_init_dpi_size(p_hwfn, p_ptt, pwm_regsize, n_cpus);
2048         }
2049
2050         cond = ((rc != ECORE_SUCCESS) &&
2051                 (roce_edpm_mode == ECORE_ROCE_EDPM_MODE_ENABLE)) ||
2052                 (roce_edpm_mode == ECORE_ROCE_EDPM_MODE_DISABLE);
2053         if (cond || p_hwfn->dcbx_no_edpm) {
2054                 /* Either EDPM is disabled from user configuration, or it is
2055                  * disabled via DCBx, or it is not mandatory and we failed to
2056                  * allocated a WID per CPU.
2057                  */
2058                 n_cpus = 1;
2059                 rc = ecore_hw_init_dpi_size(p_hwfn, p_ptt, pwm_regsize, n_cpus);
2060
2061                 /* If we entered this flow due to DCBX then the DPM register is
2062                  * already configured.
2063                  */
2064         }
2065
2066         DP_INFO(p_hwfn,
2067                 "doorbell bar: normal_region_size=%d, pwm_region_size=%d",
2068                 norm_regsize, pwm_regsize);
2069         DP_INFO(p_hwfn,
2070                 " dpi_size=%d, dpi_count=%d, roce_edpm=%s\n",
2071                 p_hwfn->dpi_size, p_hwfn->dpi_count,
2072                 (!ecore_edpm_enabled(p_hwfn)) ?
2073                 "disabled" : "enabled");
2074
2075         /* Check return codes from above calls */
2076         if (rc != ECORE_SUCCESS) {
2077                 DP_ERR(p_hwfn,
2078                        "Failed to allocate enough DPIs\n");
2079                 return ECORE_NORESOURCES;
2080         }
2081
2082         /* Update hwfn */
2083         p_hwfn->dpi_start_offset = norm_regsize;
2084
2085         /* Update registers */
2086         /* DEMS size is configured log2 of DWORDs, hence the division by 4 */
2087         pf_dems_shift = OSAL_LOG2(ECORE_PF_DEMS_SIZE / 4);
2088         ecore_wr(p_hwfn, p_ptt, DORQ_REG_PF_ICID_BIT_SHIFT_NORM, pf_dems_shift);
2089         ecore_wr(p_hwfn, p_ptt, DORQ_REG_PF_MIN_ADDR_REG1, min_addr_reg1);
2090
2091         return ECORE_SUCCESS;
2092 }
2093
2094 static enum _ecore_status_t ecore_hw_init_port(struct ecore_hwfn *p_hwfn,
2095                                                struct ecore_ptt *p_ptt,
2096                                                int hw_mode)
2097 {
2098         u32 ppf_to_eng_sel[NIG_REG_PPF_TO_ENGINE_SEL_RT_SIZE];
2099         u32 val;
2100         enum _ecore_status_t rc = ECORE_SUCCESS;
2101         u8 i;
2102
2103         /* In CMT for non-RoCE packets - use connection based classification */
2104         val = ECORE_IS_CMT(p_hwfn->p_dev) ? 0x8 : 0x0;
2105         for (i = 0; i < NIG_REG_PPF_TO_ENGINE_SEL_RT_SIZE; i++)
2106                 ppf_to_eng_sel[i] = val;
2107         STORE_RT_REG_AGG(p_hwfn, NIG_REG_PPF_TO_ENGINE_SEL_RT_OFFSET,
2108                          ppf_to_eng_sel);
2109
2110         /* In CMT the gate should be cleared by the 2nd hwfn */
2111         if (!ECORE_IS_CMT(p_hwfn->p_dev) || !IS_LEAD_HWFN(p_hwfn))
2112                 STORE_RT_REG(p_hwfn, NIG_REG_BRB_GATE_DNTFWD_PORT_RT_OFFSET, 0);
2113
2114         rc = ecore_init_run(p_hwfn, p_ptt, PHASE_PORT, p_hwfn->port_id,
2115                             hw_mode);
2116         if (rc != ECORE_SUCCESS)
2117                 return rc;
2118
2119         ecore_wr(p_hwfn, p_ptt, PGLUE_B_REG_MASTER_WRITE_PAD_ENABLE, 0);
2120
2121 #ifndef ASIC_ONLY
2122         if (CHIP_REV_IS_ASIC(p_hwfn->p_dev))
2123                 return ECORE_SUCCESS;
2124
2125         if (CHIP_REV_IS_FPGA(p_hwfn->p_dev)) {
2126                 if (ECORE_IS_AH(p_hwfn->p_dev))
2127                         return ECORE_SUCCESS;
2128                 else if (ECORE_IS_BB(p_hwfn->p_dev))
2129                         ecore_link_init_bb(p_hwfn, p_ptt, p_hwfn->port_id);
2130         } else if (CHIP_REV_IS_EMUL(p_hwfn->p_dev)) {
2131                 if (ECORE_IS_CMT(p_hwfn->p_dev)) {
2132                         /* Activate OPTE in CMT */
2133                         u32 val;
2134
2135                         val = ecore_rd(p_hwfn, p_ptt, MISCS_REG_RESET_PL_HV);
2136                         val |= 0x10;
2137                         ecore_wr(p_hwfn, p_ptt, MISCS_REG_RESET_PL_HV, val);
2138                         ecore_wr(p_hwfn, p_ptt, MISC_REG_CLK_100G_MODE, 1);
2139                         ecore_wr(p_hwfn, p_ptt, MISCS_REG_CLK_100G_MODE, 1);
2140                         ecore_wr(p_hwfn, p_ptt, MISC_REG_OPTE_MODE, 1);
2141                         ecore_wr(p_hwfn, p_ptt,
2142                                  NIG_REG_LLH_ENG_CLS_TCP_4_TUPLE_SEARCH, 1);
2143                         ecore_wr(p_hwfn, p_ptt,
2144                                  NIG_REG_LLH_ENG_CLS_ENG_ID_TBL, 0x55555555);
2145                         ecore_wr(p_hwfn, p_ptt,
2146                                  NIG_REG_LLH_ENG_CLS_ENG_ID_TBL + 0x4,
2147                                  0x55555555);
2148                 }
2149
2150                 ecore_emul_link_init(p_hwfn, p_ptt);
2151         } else {
2152                 DP_INFO(p_hwfn->p_dev, "link is not being configured\n");
2153         }
2154 #endif
2155
2156         return rc;
2157 }
2158
2159 static enum _ecore_status_t
2160 ecore_hw_init_pf(struct ecore_hwfn *p_hwfn,
2161                  struct ecore_ptt *p_ptt,
2162                  struct ecore_tunnel_info *p_tunn,
2163                  int hw_mode,
2164                  bool b_hw_start,
2165                  enum ecore_int_mode int_mode, bool allow_npar_tx_switch)
2166 {
2167         u8 rel_pf_id = p_hwfn->rel_pf_id;
2168         u32 prs_reg;
2169         enum _ecore_status_t rc = ECORE_SUCCESS;
2170         u16 ctrl;
2171         int pos;
2172
2173         if (p_hwfn->mcp_info) {
2174                 struct ecore_mcp_function_info *p_info;
2175
2176                 p_info = &p_hwfn->mcp_info->func_info;
2177                 if (p_info->bandwidth_min)
2178                         p_hwfn->qm_info.pf_wfq = p_info->bandwidth_min;
2179
2180                 /* Update rate limit once we'll actually have a link */
2181                 p_hwfn->qm_info.pf_rl = 100000;
2182         }
2183         ecore_cxt_hw_init_pf(p_hwfn, p_ptt);
2184
2185         ecore_int_igu_init_rt(p_hwfn);
2186
2187         /* Set VLAN in NIG if needed */
2188         if (hw_mode & (1 << MODE_MF_SD)) {
2189                 DP_VERBOSE(p_hwfn, ECORE_MSG_HW, "Configuring LLH_FUNC_TAG\n");
2190                 STORE_RT_REG(p_hwfn, NIG_REG_LLH_FUNC_TAG_EN_RT_OFFSET, 1);
2191                 STORE_RT_REG(p_hwfn, NIG_REG_LLH_FUNC_TAG_VALUE_RT_OFFSET,
2192                              p_hwfn->hw_info.ovlan);
2193
2194                 DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
2195                            "Configuring LLH_FUNC_FILTER_HDR_SEL\n");
2196                 STORE_RT_REG(p_hwfn, NIG_REG_LLH_FUNC_FILTER_HDR_SEL_RT_OFFSET,
2197                              1);
2198         }
2199
2200         /* Enable classification by MAC if needed */
2201         if (hw_mode & (1 << MODE_MF_SI)) {
2202                 DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
2203                            "Configuring TAGMAC_CLS_TYPE\n");
2204                 STORE_RT_REG(p_hwfn, NIG_REG_LLH_FUNC_TAGMAC_CLS_TYPE_RT_OFFSET,
2205                              1);
2206         }
2207
2208         /* Protocl Configuration  - @@@TBD - should we set 0 otherwise? */
2209         STORE_RT_REG(p_hwfn, PRS_REG_SEARCH_TCP_RT_OFFSET,
2210                      (p_hwfn->hw_info.personality == ECORE_PCI_ISCSI) ? 1 : 0);
2211         STORE_RT_REG(p_hwfn, PRS_REG_SEARCH_FCOE_RT_OFFSET,
2212                      (p_hwfn->hw_info.personality == ECORE_PCI_FCOE) ? 1 : 0);
2213         STORE_RT_REG(p_hwfn, PRS_REG_SEARCH_ROCE_RT_OFFSET, 0);
2214
2215         /* perform debug configuration when chip is out of reset */
2216         OSAL_BEFORE_PF_START((void *)p_hwfn->p_dev, p_hwfn->my_id);
2217
2218         /* Sanity check before the PF init sequence that uses DMAE */
2219         rc = ecore_dmae_sanity(p_hwfn, p_ptt, "pf_phase");
2220         if (rc)
2221                 return rc;
2222
2223         /* PF Init sequence */
2224         rc = ecore_init_run(p_hwfn, p_ptt, PHASE_PF, rel_pf_id, hw_mode);
2225         if (rc)
2226                 return rc;
2227
2228         /* QM_PF Init sequence (may be invoked separately e.g. for DCB) */
2229         rc = ecore_init_run(p_hwfn, p_ptt, PHASE_QM_PF, rel_pf_id, hw_mode);
2230         if (rc)
2231                 return rc;
2232
2233         /* Pure runtime initializations - directly to the HW  */
2234         ecore_int_igu_init_pure_rt(p_hwfn, p_ptt, true, true);
2235
2236         /* PCI relaxed ordering causes a decrease in the performance on some
2237          * systems. Till a root cause is found, disable this attribute in the
2238          * PCI config space.
2239          */
2240         /* Not in use @DPDK
2241         * pos = OSAL_PCI_FIND_CAPABILITY(p_hwfn->p_dev, PCI_CAP_ID_EXP);
2242         * if (!pos) {
2243         *       DP_NOTICE(p_hwfn, true,
2244         *                 "Failed to find the PCIe Cap\n");
2245         *       return ECORE_IO;
2246         * }
2247         * OSAL_PCI_READ_CONFIG_WORD(p_hwfn->p_dev, pos + PCI_EXP_DEVCTL, &ctrl);
2248         * ctrl &= ~PCI_EXP_DEVCTL_RELAX_EN;
2249         * OSAL_PCI_WRITE_CONFIG_WORD(p_hwfn->p_dev, pos + PCI_EXP_DEVCTL, ctrl);
2250         */
2251
2252         rc = ecore_hw_init_pf_doorbell_bar(p_hwfn, p_ptt);
2253         if (rc)
2254                 return rc;
2255         if (b_hw_start) {
2256                 /* enable interrupts */
2257                 rc = ecore_int_igu_enable(p_hwfn, p_ptt, int_mode);
2258                 if (rc != ECORE_SUCCESS)
2259                         return rc;
2260
2261                 /* send function start command */
2262                 rc = ecore_sp_pf_start(p_hwfn, p_ptt, p_tunn,
2263                                        allow_npar_tx_switch);
2264                 if (rc) {
2265                         DP_NOTICE(p_hwfn, true,
2266                                   "Function start ramrod failed\n");
2267                 } else {
2268                         return rc;
2269                 }
2270                 prs_reg = ecore_rd(p_hwfn, p_ptt, PRS_REG_SEARCH_TAG1);
2271                 DP_VERBOSE(p_hwfn, ECORE_MSG_STORAGE,
2272                                 "PRS_REG_SEARCH_TAG1: %x\n", prs_reg);
2273
2274                 if (p_hwfn->hw_info.personality == ECORE_PCI_FCOE) {
2275                         ecore_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_TAG1,
2276                                         (1 << 2));
2277                         ecore_wr(p_hwfn, p_ptt,
2278                                  PRS_REG_PKT_LEN_STAT_TAGS_NOT_COUNTED_FIRST,
2279                                  0x100);
2280                 }
2281                 DP_VERBOSE(p_hwfn, ECORE_MSG_STORAGE,
2282                                 "PRS_REG_SEARCH registers after start PFn\n");
2283                 prs_reg = ecore_rd(p_hwfn, p_ptt, PRS_REG_SEARCH_TCP);
2284                 DP_VERBOSE(p_hwfn, ECORE_MSG_STORAGE,
2285                                 "PRS_REG_SEARCH_TCP: %x\n", prs_reg);
2286                 prs_reg = ecore_rd(p_hwfn, p_ptt, PRS_REG_SEARCH_UDP);
2287                 DP_VERBOSE(p_hwfn, ECORE_MSG_STORAGE,
2288                                 "PRS_REG_SEARCH_UDP: %x\n", prs_reg);
2289                 prs_reg = ecore_rd(p_hwfn, p_ptt, PRS_REG_SEARCH_FCOE);
2290                 DP_VERBOSE(p_hwfn, ECORE_MSG_STORAGE,
2291                                 "PRS_REG_SEARCH_FCOE: %x\n", prs_reg);
2292                 prs_reg = ecore_rd(p_hwfn, p_ptt, PRS_REG_SEARCH_ROCE);
2293                 DP_VERBOSE(p_hwfn, ECORE_MSG_STORAGE,
2294                                 "PRS_REG_SEARCH_ROCE: %x\n", prs_reg);
2295                 prs_reg = ecore_rd(p_hwfn, p_ptt,
2296                                 PRS_REG_SEARCH_TCP_FIRST_FRAG);
2297                 DP_VERBOSE(p_hwfn, ECORE_MSG_STORAGE,
2298                                 "PRS_REG_SEARCH_TCP_FIRST_FRAG: %x\n",
2299                                 prs_reg);
2300                 prs_reg = ecore_rd(p_hwfn, p_ptt, PRS_REG_SEARCH_TAG1);
2301                 DP_VERBOSE(p_hwfn, ECORE_MSG_STORAGE,
2302                                 "PRS_REG_SEARCH_TAG1: %x\n", prs_reg);
2303         }
2304         return ECORE_SUCCESS;
2305 }
2306
2307 enum _ecore_status_t ecore_pglueb_set_pfid_enable(struct ecore_hwfn *p_hwfn,
2308                                                   struct ecore_ptt *p_ptt,
2309                                                   bool b_enable)
2310 {
2311         u32 delay_idx = 0, val, set_val = b_enable ? 1 : 0;
2312
2313         /* Configure the PF's internal FID_enable for master transactions */
2314         ecore_wr(p_hwfn, p_ptt,
2315                  PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, set_val);
2316
2317         /* Wait until value is set - try for 1 second every 50us */
2318         for (delay_idx = 0; delay_idx < 20000; delay_idx++) {
2319                 val = ecore_rd(p_hwfn, p_ptt,
2320                                PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER);
2321                 if (val == set_val)
2322                         break;
2323
2324                 OSAL_UDELAY(50);
2325         }
2326
2327         if (val != set_val) {
2328                 DP_NOTICE(p_hwfn, true,
2329                           "PFID_ENABLE_MASTER wasn't changed after a second\n");
2330                 return ECORE_UNKNOWN_ERROR;
2331         }
2332
2333         return ECORE_SUCCESS;
2334 }
2335
2336 static void ecore_reset_mb_shadow(struct ecore_hwfn *p_hwfn,
2337                                   struct ecore_ptt *p_main_ptt)
2338 {
2339         /* Read shadow of current MFW mailbox */
2340         ecore_mcp_read_mb(p_hwfn, p_main_ptt);
2341         OSAL_MEMCPY(p_hwfn->mcp_info->mfw_mb_shadow,
2342                     p_hwfn->mcp_info->mfw_mb_cur,
2343                     p_hwfn->mcp_info->mfw_mb_length);
2344 }
2345
2346 static void ecore_pglueb_clear_err(struct ecore_hwfn *p_hwfn,
2347                                    struct ecore_ptt *p_ptt)
2348 {
2349         ecore_wr(p_hwfn, p_ptt, PGLUE_B_REG_WAS_ERROR_PF_31_0_CLR,
2350                  1 << p_hwfn->abs_pf_id);
2351 }
2352
2353 static enum _ecore_status_t
2354 ecore_fill_load_req_params(struct ecore_hwfn *p_hwfn,
2355                            struct ecore_load_req_params *p_load_req,
2356                            struct ecore_drv_load_params *p_drv_load)
2357 {
2358         /* Make sure that if ecore-client didn't provide inputs, all the
2359          * expected defaults are indeed zero.
2360          */
2361         OSAL_BUILD_BUG_ON(ECORE_DRV_ROLE_OS != 0);
2362         OSAL_BUILD_BUG_ON(ECORE_LOAD_REQ_LOCK_TO_DEFAULT != 0);
2363         OSAL_BUILD_BUG_ON(ECORE_OVERRIDE_FORCE_LOAD_NONE != 0);
2364
2365         OSAL_MEM_ZERO(p_load_req, sizeof(*p_load_req));
2366
2367         if (p_drv_load == OSAL_NULL)
2368                 goto out;
2369
2370         p_load_req->drv_role = p_drv_load->is_crash_kernel ?
2371                                ECORE_DRV_ROLE_KDUMP :
2372                                ECORE_DRV_ROLE_OS;
2373         p_load_req->avoid_eng_reset = p_drv_load->avoid_eng_reset;
2374         p_load_req->override_force_load = p_drv_load->override_force_load;
2375
2376         /* Old MFW versions don't support timeout values other than default and
2377          * none, so these values are replaced according to the fall-back action.
2378          */
2379
2380         if (p_drv_load->mfw_timeout_val == ECORE_LOAD_REQ_LOCK_TO_DEFAULT ||
2381             p_drv_load->mfw_timeout_val == ECORE_LOAD_REQ_LOCK_TO_NONE ||
2382             (p_hwfn->mcp_info->capabilities &
2383              FW_MB_PARAM_FEATURE_SUPPORT_DRV_LOAD_TO)) {
2384                 p_load_req->timeout_val = p_drv_load->mfw_timeout_val;
2385                 goto out;
2386         }
2387
2388         switch (p_drv_load->mfw_timeout_fallback) {
2389         case ECORE_TO_FALLBACK_TO_NONE:
2390                 p_load_req->timeout_val = ECORE_LOAD_REQ_LOCK_TO_NONE;
2391                 break;
2392         case ECORE_TO_FALLBACK_TO_DEFAULT:
2393                 p_load_req->timeout_val = ECORE_LOAD_REQ_LOCK_TO_DEFAULT;
2394                 break;
2395         case ECORE_TO_FALLBACK_FAIL_LOAD:
2396                 DP_NOTICE(p_hwfn, false,
2397                           "Received %d as a value for MFW timeout while the MFW supports only default [%d] or none [%d]. Abort.\n",
2398                           p_drv_load->mfw_timeout_val,
2399                           ECORE_LOAD_REQ_LOCK_TO_DEFAULT,
2400                           ECORE_LOAD_REQ_LOCK_TO_NONE);
2401                 return ECORE_ABORTED;
2402         }
2403
2404         DP_INFO(p_hwfn,
2405                 "Modified the MFW timeout value from %d to %s [%d] due to lack of MFW support\n",
2406                 p_drv_load->mfw_timeout_val,
2407                 (p_load_req->timeout_val == ECORE_LOAD_REQ_LOCK_TO_DEFAULT) ?
2408                 "default" : "none",
2409                 p_load_req->timeout_val);
2410 out:
2411         return ECORE_SUCCESS;
2412 }
2413
2414 enum _ecore_status_t ecore_vf_start(struct ecore_hwfn *p_hwfn,
2415                                     struct ecore_hw_init_params *p_params)
2416 {
2417         if (p_params->p_tunn) {
2418                 ecore_vf_set_vf_start_tunn_update_param(p_params->p_tunn);
2419                 ecore_vf_pf_tunnel_param_update(p_hwfn, p_params->p_tunn);
2420         }
2421
2422         p_hwfn->b_int_enabled = 1;
2423
2424         return ECORE_SUCCESS;
2425 }
2426
2427 enum _ecore_status_t ecore_hw_init(struct ecore_dev *p_dev,
2428                                    struct ecore_hw_init_params *p_params)
2429 {
2430         struct ecore_load_req_params load_req_params;
2431         u32 load_code, resp, param, drv_mb_param;
2432         bool b_default_mtu = true;
2433         struct ecore_hwfn *p_hwfn;
2434         enum _ecore_status_t rc = ECORE_SUCCESS;
2435         u16 ether_type;
2436         int i;
2437
2438         if ((p_params->int_mode == ECORE_INT_MODE_MSI) && ECORE_IS_CMT(p_dev)) {
2439                 DP_NOTICE(p_dev, false,
2440                           "MSI mode is not supported for CMT devices\n");
2441                 return ECORE_INVAL;
2442         }
2443
2444         if (IS_PF(p_dev)) {
2445                 rc = ecore_init_fw_data(p_dev, p_params->bin_fw_data);
2446                 if (rc != ECORE_SUCCESS)
2447                         return rc;
2448         }
2449
2450         for_each_hwfn(p_dev, i) {
2451                 p_hwfn = &p_dev->hwfns[i];
2452
2453                 /* If management didn't provide a default, set one of our own */
2454                 if (!p_hwfn->hw_info.mtu) {
2455                         p_hwfn->hw_info.mtu = 1500;
2456                         b_default_mtu = false;
2457                 }
2458
2459                 if (IS_VF(p_dev)) {
2460                         ecore_vf_start(p_hwfn, p_params);
2461                         continue;
2462                 }
2463
2464                 rc = ecore_calc_hw_mode(p_hwfn);
2465                 if (rc != ECORE_SUCCESS)
2466                         return rc;
2467
2468                 if (IS_PF(p_dev) && (OSAL_TEST_BIT(ECORE_MF_8021Q_TAGGING,
2469                                                    &p_dev->mf_bits) ||
2470                                      OSAL_TEST_BIT(ECORE_MF_8021AD_TAGGING,
2471                                                    &p_dev->mf_bits))) {
2472                         if (OSAL_TEST_BIT(ECORE_MF_8021Q_TAGGING,
2473                                           &p_dev->mf_bits))
2474                                 ether_type = ETHER_TYPE_VLAN;
2475                         else
2476                                 ether_type = ETHER_TYPE_QINQ;
2477                         STORE_RT_REG(p_hwfn, PRS_REG_TAG_ETHERTYPE_0_RT_OFFSET,
2478                                      ether_type);
2479                         STORE_RT_REG(p_hwfn, NIG_REG_TAG_ETHERTYPE_0_RT_OFFSET,
2480                                      ether_type);
2481                         STORE_RT_REG(p_hwfn, PBF_REG_TAG_ETHERTYPE_0_RT_OFFSET,
2482                                      ether_type);
2483                         STORE_RT_REG(p_hwfn, DORQ_REG_TAG1_ETHERTYPE_RT_OFFSET,
2484                                      ether_type);
2485                 }
2486
2487                 ecore_set_spq_block_timeout(p_hwfn, p_params->spq_timeout_ms);
2488
2489                 rc = ecore_fill_load_req_params(p_hwfn, &load_req_params,
2490                                                 p_params->p_drv_load_params);
2491                 if (rc != ECORE_SUCCESS)
2492                         return rc;
2493
2494                 rc = ecore_mcp_load_req(p_hwfn, p_hwfn->p_main_ptt,
2495                                         &load_req_params);
2496                 if (rc != ECORE_SUCCESS) {
2497                         DP_NOTICE(p_hwfn, false,
2498                                   "Failed sending a LOAD_REQ command\n");
2499                         return rc;
2500                 }
2501
2502                 load_code = load_req_params.load_code;
2503                 DP_VERBOSE(p_hwfn, ECORE_MSG_SP,
2504                            "Load request was sent. Load code: 0x%x\n",
2505                            load_code);
2506
2507                 ecore_mcp_set_capabilities(p_hwfn, p_hwfn->p_main_ptt);
2508
2509                 /* CQ75580:
2510                  * When coming back from hiberbate state, the registers from
2511                  * which shadow is read initially are not initialized. It turns
2512                  * out that these registers get initialized during the call to
2513                  * ecore_mcp_load_req request. So we need to reread them here
2514                  * to get the proper shadow register value.
2515                  * Note: This is a workaround for the missing MFW
2516                  * initialization. It may be removed once the implementation
2517                  * is done.
2518                  */
2519                 ecore_reset_mb_shadow(p_hwfn, p_hwfn->p_main_ptt);
2520
2521                 /* Only relevant for recovery:
2522                  * Clear the indication after the LOAD_REQ command is responded
2523                  * by the MFW.
2524                  */
2525                 p_dev->recov_in_prog = false;
2526
2527                 p_hwfn->first_on_engine = (load_code ==
2528                                            FW_MSG_CODE_DRV_LOAD_ENGINE);
2529
2530                 if (!qm_lock_ref_cnt) {
2531 #ifdef CONFIG_ECORE_LOCK_ALLOC
2532                         rc = OSAL_SPIN_LOCK_ALLOC(p_hwfn, &qm_lock);
2533                         if (rc) {
2534                                 DP_ERR(p_hwfn, "qm_lock allocation failed\n");
2535                                 goto qm_lock_fail;
2536                         }
2537 #endif
2538                         OSAL_SPIN_LOCK_INIT(&qm_lock);
2539                 }
2540                 ++qm_lock_ref_cnt;
2541
2542                 /* Clean up chip from previous driver if such remains exist.
2543                  * This is not needed when the PF is the first one on the
2544                  * engine, since afterwards we are going to init the FW.
2545                  */
2546                 if (load_code != FW_MSG_CODE_DRV_LOAD_ENGINE) {
2547                         rc = ecore_final_cleanup(p_hwfn, p_hwfn->p_main_ptt,
2548                                                  p_hwfn->rel_pf_id, false);
2549                         if (rc != ECORE_SUCCESS) {
2550                                 ecore_hw_err_notify(p_hwfn,
2551                                                     ECORE_HW_ERR_RAMROD_FAIL);
2552                                 goto load_err;
2553                         }
2554                 }
2555
2556                 /* Log and clear previous pglue_b errors if such exist */
2557                 ecore_pglueb_rbc_attn_handler(p_hwfn, p_hwfn->p_main_ptt, true);
2558
2559                 /* Enable the PF's internal FID_enable in the PXP */
2560                 rc = ecore_pglueb_set_pfid_enable(p_hwfn, p_hwfn->p_main_ptt,
2561                                                   true);
2562                 if (rc != ECORE_SUCCESS)
2563                         goto load_err;
2564
2565                 /* Clear the pglue_b was_error indication.
2566                  * In E4 it must be done after the BME and the internal
2567                  * FID_enable for the PF are set, since VDMs may cause the
2568                  * indication to be set again.
2569                  */
2570                 ecore_pglueb_clear_err(p_hwfn, p_hwfn->p_main_ptt);
2571
2572                 switch (load_code) {
2573                 case FW_MSG_CODE_DRV_LOAD_ENGINE:
2574                         rc = ecore_hw_init_common(p_hwfn, p_hwfn->p_main_ptt,
2575                                                   p_hwfn->hw_info.hw_mode);
2576                         if (rc != ECORE_SUCCESS)
2577                                 break;
2578                         /* Fall into */
2579                 case FW_MSG_CODE_DRV_LOAD_PORT:
2580                         rc = ecore_hw_init_port(p_hwfn, p_hwfn->p_main_ptt,
2581                                                 p_hwfn->hw_info.hw_mode);
2582                         if (rc != ECORE_SUCCESS)
2583                                 break;
2584                         /* Fall into */
2585                 case FW_MSG_CODE_DRV_LOAD_FUNCTION:
2586                         rc = ecore_hw_init_pf(p_hwfn, p_hwfn->p_main_ptt,
2587                                               p_params->p_tunn,
2588                                               p_hwfn->hw_info.hw_mode,
2589                                               p_params->b_hw_start,
2590                                               p_params->int_mode,
2591                                               p_params->allow_npar_tx_switch);
2592                         break;
2593                 default:
2594                         DP_NOTICE(p_hwfn, false,
2595                                   "Unexpected load code [0x%08x]", load_code);
2596                         rc = ECORE_NOTIMPL;
2597                         break;
2598                 }
2599
2600                 if (rc != ECORE_SUCCESS) {
2601                         DP_NOTICE(p_hwfn, false,
2602                                   "init phase failed for loadcode 0x%x (rc %d)\n",
2603                                   load_code, rc);
2604                         goto load_err;
2605                 }
2606
2607                 rc = ecore_mcp_load_done(p_hwfn, p_hwfn->p_main_ptt);
2608                 if (rc != ECORE_SUCCESS) {
2609                         DP_NOTICE(p_hwfn, false,
2610                                   "Sending load done failed, rc = %d\n", rc);
2611                         if (rc == ECORE_NOMEM) {
2612                                 DP_NOTICE(p_hwfn, false,
2613                                           "Sending load done was failed due to memory allocation failure\n");
2614                                 goto load_err;
2615                         }
2616                         return rc;
2617                 }
2618
2619                 /* send DCBX attention request command */
2620                 DP_VERBOSE(p_hwfn, ECORE_MSG_DCB,
2621                            "sending phony dcbx set command to trigger DCBx attention handling\n");
2622                 rc = ecore_mcp_cmd(p_hwfn, p_hwfn->p_main_ptt,
2623                                    DRV_MSG_CODE_SET_DCBX,
2624                                    1 << DRV_MB_PARAM_DCBX_NOTIFY_OFFSET, &resp,
2625                                    &param);
2626                 if (rc != ECORE_SUCCESS) {
2627                         DP_NOTICE(p_hwfn, false,
2628                                   "Failed to send DCBX attention request\n");
2629                         return rc;
2630                 }
2631
2632                 p_hwfn->hw_init_done = true;
2633         }
2634
2635         if (IS_PF(p_dev)) {
2636                 /* Get pre-negotiated values for stag, bandwidth etc. */
2637                 p_hwfn = ECORE_LEADING_HWFN(p_dev);
2638                 DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ,
2639                            "Sending GET_OEM_UPDATES command to trigger stag/bandwidth attention handling\n");
2640                 rc = ecore_mcp_cmd(p_hwfn, p_hwfn->p_main_ptt,
2641                                    DRV_MSG_CODE_GET_OEM_UPDATES,
2642                                    1 << DRV_MB_PARAM_DUMMY_OEM_UPDATES_OFFSET,
2643                                    &resp, &param);
2644                 if (rc != ECORE_SUCCESS)
2645                         DP_NOTICE(p_hwfn, false,
2646                                   "Failed to send GET_OEM_UPDATES attention request\n");
2647         }
2648
2649         if (IS_PF(p_dev)) {
2650                 p_hwfn = ECORE_LEADING_HWFN(p_dev);
2651                 drv_mb_param = STORM_FW_VERSION;
2652                 rc = ecore_mcp_cmd(p_hwfn, p_hwfn->p_main_ptt,
2653                                    DRV_MSG_CODE_OV_UPDATE_STORM_FW_VER,
2654                                    drv_mb_param, &resp, &param);
2655                 if (rc != ECORE_SUCCESS)
2656                         DP_INFO(p_hwfn, "Failed to update firmware version\n");
2657
2658                 if (!b_default_mtu) {
2659                         rc = ecore_mcp_ov_update_mtu(p_hwfn, p_hwfn->p_main_ptt,
2660                                                       p_hwfn->hw_info.mtu);
2661                         if (rc != ECORE_SUCCESS)
2662                                 DP_INFO(p_hwfn, "Failed to update default mtu\n");
2663                 }
2664
2665                 rc = ecore_mcp_ov_update_driver_state(p_hwfn,
2666                                                       p_hwfn->p_main_ptt,
2667                                                 ECORE_OV_DRIVER_STATE_DISABLED);
2668                 if (rc != ECORE_SUCCESS)
2669                         DP_INFO(p_hwfn, "Failed to update driver state\n");
2670
2671                 rc = ecore_mcp_ov_update_eswitch(p_hwfn, p_hwfn->p_main_ptt,
2672                                                  ECORE_OV_ESWITCH_NONE);
2673                 if (rc != ECORE_SUCCESS)
2674                         DP_INFO(p_hwfn, "Failed to update eswitch mode\n");
2675         }
2676
2677         return rc;
2678
2679 load_err:
2680         --qm_lock_ref_cnt;
2681 #ifdef CONFIG_ECORE_LOCK_ALLOC
2682         if (!qm_lock_ref_cnt)
2683                 OSAL_SPIN_LOCK_DEALLOC(&qm_lock);
2684 qm_lock_fail:
2685 #endif
2686         /* The MFW load lock should be released regardless of success or failure
2687          * of initialization.
2688          * TODO: replace this with an attempt to send cancel_load.
2689          */
2690         ecore_mcp_load_done(p_hwfn, p_hwfn->p_main_ptt);
2691         return rc;
2692 }
2693
2694 #define ECORE_HW_STOP_RETRY_LIMIT       (10)
2695 static void ecore_hw_timers_stop(struct ecore_dev *p_dev,
2696                                  struct ecore_hwfn *p_hwfn,
2697                                  struct ecore_ptt *p_ptt)
2698 {
2699         int i;
2700
2701         /* close timers */
2702         ecore_wr(p_hwfn, p_ptt, TM_REG_PF_ENABLE_CONN, 0x0);
2703         ecore_wr(p_hwfn, p_ptt, TM_REG_PF_ENABLE_TASK, 0x0);
2704         for (i = 0; i < ECORE_HW_STOP_RETRY_LIMIT && !p_dev->recov_in_prog;
2705                                                                         i++) {
2706                 if ((!ecore_rd(p_hwfn, p_ptt,
2707                                TM_REG_PF_SCAN_ACTIVE_CONN)) &&
2708                     (!ecore_rd(p_hwfn, p_ptt, TM_REG_PF_SCAN_ACTIVE_TASK)))
2709                         break;
2710
2711                 /* Dependent on number of connection/tasks, possibly
2712                  * 1ms sleep is required between polls
2713                  */
2714                 OSAL_MSLEEP(1);
2715         }
2716
2717         if (i < ECORE_HW_STOP_RETRY_LIMIT)
2718                 return;
2719
2720         DP_NOTICE(p_hwfn, false,
2721                   "Timers linear scans are not over [Connection %02x Tasks %02x]\n",
2722                   (u8)ecore_rd(p_hwfn, p_ptt, TM_REG_PF_SCAN_ACTIVE_CONN),
2723                   (u8)ecore_rd(p_hwfn, p_ptt, TM_REG_PF_SCAN_ACTIVE_TASK));
2724 }
2725
2726 void ecore_hw_timers_stop_all(struct ecore_dev *p_dev)
2727 {
2728         int j;
2729
2730         for_each_hwfn(p_dev, j) {
2731                 struct ecore_hwfn *p_hwfn = &p_dev->hwfns[j];
2732                 struct ecore_ptt *p_ptt = p_hwfn->p_main_ptt;
2733
2734                 ecore_hw_timers_stop(p_dev, p_hwfn, p_ptt);
2735         }
2736 }
2737
2738 static enum _ecore_status_t ecore_verify_reg_val(struct ecore_hwfn *p_hwfn,
2739                                                  struct ecore_ptt *p_ptt,
2740                                                  u32 addr, u32 expected_val)
2741 {
2742         u32 val = ecore_rd(p_hwfn, p_ptt, addr);
2743
2744         if (val != expected_val) {
2745                 DP_NOTICE(p_hwfn, true,
2746                           "Value at address 0x%08x is 0x%08x while the expected value is 0x%08x\n",
2747                           addr, val, expected_val);
2748                 return ECORE_UNKNOWN_ERROR;
2749         }
2750
2751         return ECORE_SUCCESS;
2752 }
2753
2754 enum _ecore_status_t ecore_hw_stop(struct ecore_dev *p_dev)
2755 {
2756         struct ecore_hwfn *p_hwfn;
2757         struct ecore_ptt *p_ptt;
2758         enum _ecore_status_t rc, rc2 = ECORE_SUCCESS;
2759         int j;
2760
2761         for_each_hwfn(p_dev, j) {
2762                 p_hwfn = &p_dev->hwfns[j];
2763                 p_ptt = p_hwfn->p_main_ptt;
2764
2765                 DP_VERBOSE(p_hwfn, ECORE_MSG_IFDOWN, "Stopping hw/fw\n");
2766
2767                 if (IS_VF(p_dev)) {
2768                         ecore_vf_pf_int_cleanup(p_hwfn);
2769                         rc = ecore_vf_pf_reset(p_hwfn);
2770                         if (rc != ECORE_SUCCESS) {
2771                                 DP_NOTICE(p_hwfn, true,
2772                                           "ecore_vf_pf_reset failed. rc = %d.\n",
2773                                           rc);
2774                                 rc2 = ECORE_UNKNOWN_ERROR;
2775                         }
2776                         continue;
2777                 }
2778
2779                 /* mark the hw as uninitialized... */
2780                 p_hwfn->hw_init_done = false;
2781
2782                 /* Send unload command to MCP */
2783                 if (!p_dev->recov_in_prog) {
2784                         rc = ecore_mcp_unload_req(p_hwfn, p_ptt);
2785                         if (rc != ECORE_SUCCESS) {
2786                                 DP_NOTICE(p_hwfn, false,
2787                                           "Failed sending a UNLOAD_REQ command. rc = %d.\n",
2788                                           rc);
2789                                 rc2 = ECORE_UNKNOWN_ERROR;
2790                         }
2791                 }
2792
2793                 OSAL_DPC_SYNC(p_hwfn);
2794
2795                 /* After this point no MFW attentions are expected, e.g. prevent
2796                  * race between pf stop and dcbx pf update.
2797                  */
2798
2799                 rc = ecore_sp_pf_stop(p_hwfn);
2800                 if (rc != ECORE_SUCCESS) {
2801                         DP_NOTICE(p_hwfn, false,
2802                                   "Failed to close PF against FW [rc = %d]. Continue to stop HW to prevent illegal host access by the device.\n",
2803                                   rc);
2804                         rc2 = ECORE_UNKNOWN_ERROR;
2805                 }
2806
2807                 /* perform debug action after PF stop was sent */
2808                 OSAL_AFTER_PF_STOP((void *)p_dev, p_hwfn->my_id);
2809
2810                 /* close NIG to BRB gate */
2811                 ecore_wr(p_hwfn, p_ptt,
2812                          NIG_REG_RX_LLH_BRB_GATE_DNTFWD_PERPF, 0x1);
2813
2814                 /* close parser */
2815                 ecore_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_TCP, 0x0);
2816                 ecore_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_UDP, 0x0);
2817                 ecore_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_FCOE, 0x0);
2818                 ecore_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_ROCE, 0x0);
2819                 ecore_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_OPENFLOW, 0x0);
2820
2821                 /* @@@TBD - clean transmission queues (5.b) */
2822                 /* @@@TBD - clean BTB (5.c) */
2823
2824                 ecore_hw_timers_stop(p_dev, p_hwfn, p_ptt);
2825
2826                 /* @@@TBD - verify DMAE requests are done (8) */
2827
2828                 /* Disable Attention Generation */
2829                 ecore_int_igu_disable_int(p_hwfn, p_ptt);
2830                 ecore_wr(p_hwfn, p_ptt, IGU_REG_LEADING_EDGE_LATCH, 0);
2831                 ecore_wr(p_hwfn, p_ptt, IGU_REG_TRAILING_EDGE_LATCH, 0);
2832                 ecore_int_igu_init_pure_rt(p_hwfn, p_ptt, false, true);
2833                 rc = ecore_int_igu_reset_cam_default(p_hwfn, p_ptt);
2834                 if (rc != ECORE_SUCCESS) {
2835                         DP_NOTICE(p_hwfn, true,
2836                                   "Failed to return IGU CAM to default\n");
2837                         rc2 = ECORE_UNKNOWN_ERROR;
2838                 }
2839
2840                 /* Need to wait 1ms to guarantee SBs are cleared */
2841                 OSAL_MSLEEP(1);
2842
2843                 if (!p_dev->recov_in_prog) {
2844                         ecore_verify_reg_val(p_hwfn, p_ptt,
2845                                              QM_REG_USG_CNT_PF_TX, 0);
2846                         ecore_verify_reg_val(p_hwfn, p_ptt,
2847                                              QM_REG_USG_CNT_PF_OTHER, 0);
2848                         /* @@@TBD - assert on incorrect xCFC values (10.b) */
2849                 }
2850
2851                 /* Disable PF in HW blocks */
2852                 ecore_wr(p_hwfn, p_ptt, DORQ_REG_PF_DB_ENABLE, 0);
2853                 ecore_wr(p_hwfn, p_ptt, QM_REG_PF_EN, 0);
2854
2855                 --qm_lock_ref_cnt;
2856 #ifdef CONFIG_ECORE_LOCK_ALLOC
2857                 if (!qm_lock_ref_cnt)
2858                         OSAL_SPIN_LOCK_DEALLOC(&qm_lock);
2859 #endif
2860
2861                 if (!p_dev->recov_in_prog) {
2862                         rc = ecore_mcp_unload_done(p_hwfn, p_ptt);
2863                         if (rc == ECORE_NOMEM) {
2864                                 DP_NOTICE(p_hwfn, false,
2865                                          "Failed sending an UNLOAD_DONE command due to a memory allocation failure. Resending.\n");
2866                                 rc = ecore_mcp_unload_done(p_hwfn, p_ptt);
2867                         }
2868                         if (rc != ECORE_SUCCESS) {
2869                                 DP_NOTICE(p_hwfn, false,
2870                                           "Failed sending a UNLOAD_DONE command. rc = %d.\n",
2871                                           rc);
2872                                 rc2 = ECORE_UNKNOWN_ERROR;
2873                         }
2874                 }
2875         } /* hwfn loop */
2876
2877         if (IS_PF(p_dev) && !p_dev->recov_in_prog) {
2878                 p_hwfn = ECORE_LEADING_HWFN(p_dev);
2879                 p_ptt = ECORE_LEADING_HWFN(p_dev)->p_main_ptt;
2880
2881                  /* Clear the PF's internal FID_enable in the PXP.
2882                   * In CMT this should only be done for first hw-function, and
2883                   * only after all transactions have stopped for all active
2884                   * hw-functions.
2885                   */
2886                 rc = ecore_pglueb_set_pfid_enable(p_hwfn, p_hwfn->p_main_ptt,
2887                                                   false);
2888                 if (rc != ECORE_SUCCESS) {
2889                         DP_NOTICE(p_hwfn, true,
2890                                   "ecore_pglueb_set_pfid_enable() failed. rc = %d.\n",
2891                                   rc);
2892                         rc2 = ECORE_UNKNOWN_ERROR;
2893                 }
2894         }
2895
2896         return rc2;
2897 }
2898
2899 enum _ecore_status_t ecore_hw_stop_fastpath(struct ecore_dev *p_dev)
2900 {
2901         int j;
2902
2903         for_each_hwfn(p_dev, j) {
2904                 struct ecore_hwfn *p_hwfn = &p_dev->hwfns[j];
2905                 struct ecore_ptt *p_ptt;
2906
2907                 if (IS_VF(p_dev)) {
2908                         ecore_vf_pf_int_cleanup(p_hwfn);
2909                         continue;
2910                 }
2911                 p_ptt = ecore_ptt_acquire(p_hwfn);
2912                 if (!p_ptt)
2913                         return ECORE_AGAIN;
2914
2915                 DP_VERBOSE(p_hwfn, ECORE_MSG_IFDOWN,
2916                            "Shutting down the fastpath\n");
2917
2918                 ecore_wr(p_hwfn, p_ptt,
2919                          NIG_REG_RX_LLH_BRB_GATE_DNTFWD_PERPF, 0x1);
2920
2921                 ecore_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_TCP, 0x0);
2922                 ecore_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_UDP, 0x0);
2923                 ecore_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_FCOE, 0x0);
2924                 ecore_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_ROCE, 0x0);
2925                 ecore_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_OPENFLOW, 0x0);
2926
2927                 /* @@@TBD - clean transmission queues (5.b) */
2928                 /* @@@TBD - clean BTB (5.c) */
2929
2930                 /* @@@TBD - verify DMAE requests are done (8) */
2931
2932                 ecore_int_igu_init_pure_rt(p_hwfn, p_ptt, false, false);
2933                 /* Need to wait 1ms to guarantee SBs are cleared */
2934                 OSAL_MSLEEP(1);
2935                 ecore_ptt_release(p_hwfn, p_ptt);
2936         }
2937
2938         return ECORE_SUCCESS;
2939 }
2940
2941 enum _ecore_status_t ecore_hw_start_fastpath(struct ecore_hwfn *p_hwfn)
2942 {
2943         struct ecore_ptt *p_ptt;
2944
2945         if (IS_VF(p_hwfn->p_dev))
2946                 return ECORE_SUCCESS;
2947
2948         p_ptt = ecore_ptt_acquire(p_hwfn);
2949         if (!p_ptt)
2950                 return ECORE_AGAIN;
2951
2952         /* If roce info is allocated it means roce is initialized and should
2953          * be enabled in searcher.
2954          */
2955         if (p_hwfn->p_rdma_info) {
2956                 if (p_hwfn->b_rdma_enabled_in_prs)
2957                         ecore_wr(p_hwfn, p_ptt,
2958                                  p_hwfn->rdma_prs_search_reg, 0x1);
2959                 ecore_wr(p_hwfn, p_ptt, TM_REG_PF_ENABLE_CONN, 0x1);
2960         }
2961
2962         /* Re-open incoming traffic */
2963         ecore_wr(p_hwfn, p_ptt,
2964                  NIG_REG_RX_LLH_BRB_GATE_DNTFWD_PERPF, 0x0);
2965         ecore_ptt_release(p_hwfn, p_ptt);
2966
2967         return ECORE_SUCCESS;
2968 }
2969
2970 /* Free hwfn memory and resources acquired in hw_hwfn_prepare */
2971 static void ecore_hw_hwfn_free(struct ecore_hwfn *p_hwfn)
2972 {
2973         ecore_ptt_pool_free(p_hwfn);
2974         OSAL_FREE(p_hwfn->p_dev, p_hwfn->hw_info.p_igu_info);
2975 }
2976
2977 /* Setup bar access */
2978 static void ecore_hw_hwfn_prepare(struct ecore_hwfn *p_hwfn)
2979 {
2980         /* clear indirect access */
2981         if (ECORE_IS_AH(p_hwfn->p_dev)) {
2982                 ecore_wr(p_hwfn, p_hwfn->p_main_ptt,
2983                          PGLUE_B_REG_PGL_ADDR_E8_F0_K2_E5, 0);
2984                 ecore_wr(p_hwfn, p_hwfn->p_main_ptt,
2985                          PGLUE_B_REG_PGL_ADDR_EC_F0_K2_E5, 0);
2986                 ecore_wr(p_hwfn, p_hwfn->p_main_ptt,
2987                          PGLUE_B_REG_PGL_ADDR_F0_F0_K2_E5, 0);
2988                 ecore_wr(p_hwfn, p_hwfn->p_main_ptt,
2989                          PGLUE_B_REG_PGL_ADDR_F4_F0_K2_E5, 0);
2990         } else {
2991                 ecore_wr(p_hwfn, p_hwfn->p_main_ptt,
2992                          PGLUE_B_REG_PGL_ADDR_88_F0_BB, 0);
2993                 ecore_wr(p_hwfn, p_hwfn->p_main_ptt,
2994                          PGLUE_B_REG_PGL_ADDR_8C_F0_BB, 0);
2995                 ecore_wr(p_hwfn, p_hwfn->p_main_ptt,
2996                          PGLUE_B_REG_PGL_ADDR_90_F0_BB, 0);
2997                 ecore_wr(p_hwfn, p_hwfn->p_main_ptt,
2998                          PGLUE_B_REG_PGL_ADDR_94_F0_BB, 0);
2999         }
3000
3001         /* Clean previous pglue_b errors if such exist */
3002         ecore_pglueb_clear_err(p_hwfn, p_hwfn->p_main_ptt);
3003
3004         /* enable internal target-read */
3005         ecore_wr(p_hwfn, p_hwfn->p_main_ptt,
3006                  PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
3007 }
3008
3009 static void get_function_id(struct ecore_hwfn *p_hwfn)
3010 {
3011         /* ME Register */
3012         p_hwfn->hw_info.opaque_fid = (u16)REG_RD(p_hwfn,
3013                                                   PXP_PF_ME_OPAQUE_ADDR);
3014
3015         p_hwfn->hw_info.concrete_fid = REG_RD(p_hwfn, PXP_PF_ME_CONCRETE_ADDR);
3016
3017         /* Bits 16-19 from the ME registers are the pf_num */
3018         p_hwfn->abs_pf_id = (p_hwfn->hw_info.concrete_fid >> 16) & 0xf;
3019         p_hwfn->rel_pf_id = GET_FIELD(p_hwfn->hw_info.concrete_fid,
3020                                       PXP_CONCRETE_FID_PFID);
3021         p_hwfn->port_id = GET_FIELD(p_hwfn->hw_info.concrete_fid,
3022                                     PXP_CONCRETE_FID_PORT);
3023
3024         DP_VERBOSE(p_hwfn, ECORE_MSG_PROBE,
3025                    "Read ME register: Concrete 0x%08x Opaque 0x%04x\n",
3026                    p_hwfn->hw_info.concrete_fid, p_hwfn->hw_info.opaque_fid);
3027 }
3028
3029 static void ecore_hw_set_feat(struct ecore_hwfn *p_hwfn)
3030 {
3031         u32 *feat_num = p_hwfn->hw_info.feat_num;
3032         struct ecore_sb_cnt_info sb_cnt;
3033         u32 non_l2_sbs = 0;
3034
3035         OSAL_MEM_ZERO(&sb_cnt, sizeof(sb_cnt));
3036         ecore_int_get_num_sbs(p_hwfn, &sb_cnt);
3037
3038         /* L2 Queues require each: 1 status block. 1 L2 queue */
3039         if (ECORE_IS_L2_PERSONALITY(p_hwfn)) {
3040                 /* Start by allocating VF queues, then PF's */
3041                 feat_num[ECORE_VF_L2_QUE] =
3042                         OSAL_MIN_T(u32,
3043                                    RESC_NUM(p_hwfn, ECORE_L2_QUEUE),
3044                                    sb_cnt.iov_cnt);
3045                 feat_num[ECORE_PF_L2_QUE] =
3046                         OSAL_MIN_T(u32,
3047                                    sb_cnt.cnt - non_l2_sbs,
3048                                    RESC_NUM(p_hwfn, ECORE_L2_QUEUE) -
3049                                    FEAT_NUM(p_hwfn, ECORE_VF_L2_QUE));
3050         }
3051
3052         if (ECORE_IS_FCOE_PERSONALITY(p_hwfn) ||
3053             ECORE_IS_ISCSI_PERSONALITY(p_hwfn)) {
3054                 u32 *p_storage_feat = ECORE_IS_FCOE_PERSONALITY(p_hwfn) ?
3055                                       &feat_num[ECORE_FCOE_CQ] :
3056                                       &feat_num[ECORE_ISCSI_CQ];
3057                 u32 limit = sb_cnt.cnt;
3058
3059                 /* The number of queues should not exceed the number of FP SBs.
3060                  * In storage target, the queues are divided into pairs of a CQ
3061                  * and a CmdQ, and each pair uses a single SB. The limit in
3062                  * this case should allow a max ratio of 2:1 instead of 1:1.
3063                  */
3064                 if (p_hwfn->p_dev->b_is_target)
3065                         limit *= 2;
3066                 *p_storage_feat = OSAL_MIN_T(u32, limit,
3067                                              RESC_NUM(p_hwfn, ECORE_CMDQS_CQS));
3068
3069                 /* @DPDK */
3070                 /* The size of "cq_cmdq_sb_num_arr" in the fcoe/iscsi init
3071                  * ramrod is limited to "NUM_OF_GLOBAL_QUEUES / 2".
3072                  */
3073                 *p_storage_feat = OSAL_MIN_T(u32, *p_storage_feat,
3074                                              (NUM_OF_GLOBAL_QUEUES / 2));
3075         }
3076
3077         DP_VERBOSE(p_hwfn, ECORE_MSG_PROBE,
3078                    "#PF_L2_QUEUE=%d VF_L2_QUEUES=%d #ROCE_CNQ=%d #FCOE_CQ=%d #ISCSI_CQ=%d #SB=%d\n",
3079                    (int)FEAT_NUM(p_hwfn, ECORE_PF_L2_QUE),
3080                    (int)FEAT_NUM(p_hwfn, ECORE_VF_L2_QUE),
3081                    (int)FEAT_NUM(p_hwfn, ECORE_RDMA_CNQ),
3082                    (int)FEAT_NUM(p_hwfn, ECORE_FCOE_CQ),
3083                    (int)FEAT_NUM(p_hwfn, ECORE_ISCSI_CQ),
3084                    (int)sb_cnt.cnt);
3085 }
3086
3087 const char *ecore_hw_get_resc_name(enum ecore_resources res_id)
3088 {
3089         switch (res_id) {
3090         case ECORE_L2_QUEUE:
3091                 return "L2_QUEUE";
3092         case ECORE_VPORT:
3093                 return "VPORT";
3094         case ECORE_RSS_ENG:
3095                 return "RSS_ENG";
3096         case ECORE_PQ:
3097                 return "PQ";
3098         case ECORE_RL:
3099                 return "RL";
3100         case ECORE_MAC:
3101                 return "MAC";
3102         case ECORE_VLAN:
3103                 return "VLAN";
3104         case ECORE_RDMA_CNQ_RAM:
3105                 return "RDMA_CNQ_RAM";
3106         case ECORE_ILT:
3107                 return "ILT";
3108         case ECORE_LL2_QUEUE:
3109                 return "LL2_QUEUE";
3110         case ECORE_CMDQS_CQS:
3111                 return "CMDQS_CQS";
3112         case ECORE_RDMA_STATS_QUEUE:
3113                 return "RDMA_STATS_QUEUE";
3114         case ECORE_BDQ:
3115                 return "BDQ";
3116         case ECORE_SB:
3117                 return "SB";
3118         default:
3119                 return "UNKNOWN_RESOURCE";
3120         }
3121 }
3122
3123 static enum _ecore_status_t
3124 __ecore_hw_set_soft_resc_size(struct ecore_hwfn *p_hwfn,
3125                               struct ecore_ptt *p_ptt,
3126                               enum ecore_resources res_id,
3127                               u32 resc_max_val,
3128                               u32 *p_mcp_resp)
3129 {
3130         enum _ecore_status_t rc;
3131
3132         rc = ecore_mcp_set_resc_max_val(p_hwfn, p_ptt, res_id,
3133                                         resc_max_val, p_mcp_resp);
3134         if (rc != ECORE_SUCCESS) {
3135                 DP_NOTICE(p_hwfn, false,
3136                           "MFW response failure for a max value setting of resource %d [%s]\n",
3137                           res_id, ecore_hw_get_resc_name(res_id));
3138                 return rc;
3139         }
3140
3141         if (*p_mcp_resp != FW_MSG_CODE_RESOURCE_ALLOC_OK)
3142                 DP_INFO(p_hwfn,
3143                         "Failed to set the max value of resource %d [%s]. mcp_resp = 0x%08x.\n",
3144                         res_id, ecore_hw_get_resc_name(res_id), *p_mcp_resp);
3145
3146         return ECORE_SUCCESS;
3147 }
3148
3149 static enum _ecore_status_t
3150 ecore_hw_set_soft_resc_size(struct ecore_hwfn *p_hwfn,
3151                             struct ecore_ptt *p_ptt)
3152 {
3153         bool b_ah = ECORE_IS_AH(p_hwfn->p_dev);
3154         u32 resc_max_val, mcp_resp;
3155         u8 res_id;
3156         enum _ecore_status_t rc;
3157
3158         for (res_id = 0; res_id < ECORE_MAX_RESC; res_id++) {
3159                 /* @DPDK */
3160                 switch (res_id) {
3161                 case ECORE_LL2_QUEUE:
3162                 case ECORE_RDMA_CNQ_RAM:
3163                 case ECORE_RDMA_STATS_QUEUE:
3164                 case ECORE_BDQ:
3165                         resc_max_val = 0;
3166                         break;
3167                 default:
3168                         continue;
3169                 }
3170
3171                 rc = __ecore_hw_set_soft_resc_size(p_hwfn, p_ptt, res_id,
3172                                                    resc_max_val, &mcp_resp);
3173                 if (rc != ECORE_SUCCESS)
3174                         return rc;
3175
3176                 /* There's no point to continue to the next resource if the
3177                  * command is not supported by the MFW.
3178                  * We do continue if the command is supported but the resource
3179                  * is unknown to the MFW. Such a resource will be later
3180                  * configured with the default allocation values.
3181                  */
3182                 if (mcp_resp == FW_MSG_CODE_UNSUPPORTED)
3183                         return ECORE_NOTIMPL;
3184         }
3185
3186         return ECORE_SUCCESS;
3187 }
3188
3189 static
3190 enum _ecore_status_t ecore_hw_get_dflt_resc(struct ecore_hwfn *p_hwfn,
3191                                             enum ecore_resources res_id,
3192                                             u32 *p_resc_num, u32 *p_resc_start)
3193 {
3194         u8 num_funcs = p_hwfn->num_funcs_on_engine;
3195         bool b_ah = ECORE_IS_AH(p_hwfn->p_dev);
3196
3197         switch (res_id) {
3198         case ECORE_L2_QUEUE:
3199                 *p_resc_num = (b_ah ? MAX_NUM_L2_QUEUES_K2 :
3200                                  MAX_NUM_L2_QUEUES_BB) / num_funcs;
3201                 break;
3202         case ECORE_VPORT:
3203                 *p_resc_num = (b_ah ? MAX_NUM_VPORTS_K2 :
3204                                  MAX_NUM_VPORTS_BB) / num_funcs;
3205                 break;
3206         case ECORE_RSS_ENG:
3207                 *p_resc_num = (b_ah ? ETH_RSS_ENGINE_NUM_K2 :
3208                                  ETH_RSS_ENGINE_NUM_BB) / num_funcs;
3209                 break;
3210         case ECORE_PQ:
3211                 *p_resc_num = (b_ah ? MAX_QM_TX_QUEUES_K2 :
3212                                  MAX_QM_TX_QUEUES_BB) / num_funcs;
3213                 break;
3214         case ECORE_RL:
3215                 *p_resc_num = MAX_QM_GLOBAL_RLS / num_funcs;
3216                 break;
3217         case ECORE_MAC:
3218         case ECORE_VLAN:
3219                 /* Each VFC resource can accommodate both a MAC and a VLAN */
3220                 *p_resc_num = ETH_NUM_MAC_FILTERS / num_funcs;
3221                 break;
3222         case ECORE_ILT:
3223                 *p_resc_num = (b_ah ? PXP_NUM_ILT_RECORDS_K2 :
3224                                  PXP_NUM_ILT_RECORDS_BB) / num_funcs;
3225                 break;
3226         case ECORE_LL2_QUEUE:
3227                 *p_resc_num = MAX_NUM_LL2_RX_QUEUES / num_funcs;
3228                 break;
3229         case ECORE_RDMA_CNQ_RAM:
3230         case ECORE_CMDQS_CQS:
3231                 /* CNQ/CMDQS are the same resource */
3232                 /* @DPDK */
3233                 *p_resc_num = (NUM_OF_GLOBAL_QUEUES / 2) / num_funcs;
3234                 break;
3235         case ECORE_RDMA_STATS_QUEUE:
3236                 /* @DPDK */
3237                 *p_resc_num = (b_ah ? MAX_NUM_VPORTS_K2 :
3238                                  MAX_NUM_VPORTS_BB) / num_funcs;
3239                 break;
3240         case ECORE_BDQ:
3241                 /* @DPDK */
3242                 *p_resc_num = 0;
3243                 break;
3244         default:
3245                 break;
3246         }
3247
3248
3249         switch (res_id) {
3250         case ECORE_BDQ:
3251                 if (!*p_resc_num)
3252                         *p_resc_start = 0;
3253                 break;
3254         case ECORE_SB:
3255                 /* Since we want its value to reflect whether MFW supports
3256                  * the new scheme, have a default of 0.
3257                  */
3258                 *p_resc_num = 0;
3259                 break;
3260         default:
3261                 *p_resc_start = *p_resc_num * p_hwfn->enabled_func_idx;
3262                 break;
3263         }
3264
3265         return ECORE_SUCCESS;
3266 }
3267
3268 static enum _ecore_status_t
3269 __ecore_hw_set_resc_info(struct ecore_hwfn *p_hwfn, enum ecore_resources res_id,
3270                          bool drv_resc_alloc)
3271 {
3272         u32 dflt_resc_num = 0, dflt_resc_start = 0;
3273         u32 mcp_resp, *p_resc_num, *p_resc_start;
3274         enum _ecore_status_t rc;
3275
3276         p_resc_num = &RESC_NUM(p_hwfn, res_id);
3277         p_resc_start = &RESC_START(p_hwfn, res_id);
3278
3279         rc = ecore_hw_get_dflt_resc(p_hwfn, res_id, &dflt_resc_num,
3280                                     &dflt_resc_start);
3281         if (rc != ECORE_SUCCESS) {
3282                 DP_ERR(p_hwfn,
3283                        "Failed to get default amount for resource %d [%s]\n",
3284                         res_id, ecore_hw_get_resc_name(res_id));
3285                 return rc;
3286         }
3287
3288 #ifndef ASIC_ONLY
3289         if (CHIP_REV_IS_SLOW(p_hwfn->p_dev)) {
3290                 *p_resc_num = dflt_resc_num;
3291                 *p_resc_start = dflt_resc_start;
3292                 goto out;
3293         }
3294 #endif
3295
3296         rc = ecore_mcp_get_resc_info(p_hwfn, p_hwfn->p_main_ptt, res_id,
3297                                      &mcp_resp, p_resc_num, p_resc_start);
3298         if (rc != ECORE_SUCCESS) {
3299                 DP_NOTICE(p_hwfn, true,
3300                           "MFW response failure for an allocation request for"
3301                           " resource %d [%s]\n",
3302                           res_id, ecore_hw_get_resc_name(res_id));
3303                 return rc;
3304         }
3305
3306         /* Default driver values are applied in the following cases:
3307          * - The resource allocation MB command is not supported by the MFW
3308          * - There is an internal error in the MFW while processing the request
3309          * - The resource ID is unknown to the MFW
3310          */
3311         if (mcp_resp != FW_MSG_CODE_RESOURCE_ALLOC_OK) {
3312                 DP_INFO(p_hwfn,
3313                         "Failed to receive allocation info for resource %d [%s]."
3314                         " mcp_resp = 0x%x. Applying default values"
3315                         " [%d,%d].\n",
3316                         res_id, ecore_hw_get_resc_name(res_id), mcp_resp,
3317                         dflt_resc_num, dflt_resc_start);
3318
3319                 *p_resc_num = dflt_resc_num;
3320                 *p_resc_start = dflt_resc_start;
3321                 goto out;
3322         }
3323
3324         if ((*p_resc_num != dflt_resc_num ||
3325              *p_resc_start != dflt_resc_start) &&
3326             res_id != ECORE_SB) {
3327                 DP_INFO(p_hwfn,
3328                         "MFW allocation for resource %d [%s] differs from default values [%d,%d vs. %d,%d]%s\n",
3329                         res_id, ecore_hw_get_resc_name(res_id), *p_resc_num,
3330                         *p_resc_start, dflt_resc_num, dflt_resc_start,
3331                         drv_resc_alloc ? " - Applying default values" : "");
3332                 if (drv_resc_alloc) {
3333                         *p_resc_num = dflt_resc_num;
3334                         *p_resc_start = dflt_resc_start;
3335                 }
3336         }
3337 out:
3338         return ECORE_SUCCESS;
3339 }
3340
3341 static enum _ecore_status_t ecore_hw_set_resc_info(struct ecore_hwfn *p_hwfn,
3342                                                    bool drv_resc_alloc)
3343 {
3344         enum _ecore_status_t rc;
3345         u8 res_id;
3346
3347         for (res_id = 0; res_id < ECORE_MAX_RESC; res_id++) {
3348                 rc = __ecore_hw_set_resc_info(p_hwfn, res_id, drv_resc_alloc);
3349                 if (rc != ECORE_SUCCESS)
3350                         return rc;
3351         }
3352
3353         return ECORE_SUCCESS;
3354 }
3355
3356 static enum _ecore_status_t ecore_hw_get_resc(struct ecore_hwfn *p_hwfn,
3357                                               struct ecore_ptt *p_ptt,
3358                                               bool drv_resc_alloc)
3359 {
3360         struct ecore_resc_unlock_params resc_unlock_params;
3361         struct ecore_resc_lock_params resc_lock_params;
3362         bool b_ah = ECORE_IS_AH(p_hwfn->p_dev);
3363         u8 res_id;
3364         enum _ecore_status_t rc;
3365 #ifndef ASIC_ONLY
3366         u32 *resc_start = p_hwfn->hw_info.resc_start;
3367         u32 *resc_num = p_hwfn->hw_info.resc_num;
3368         /* For AH, an equal share of the ILT lines between the maximal number of
3369          * PFs is not enough for RoCE. This would be solved by the future
3370          * resource allocation scheme, but isn't currently present for
3371          * FPGA/emulation. For now we keep a number that is sufficient for RoCE
3372          * to work - the BB number of ILT lines divided by its max PFs number.
3373          */
3374         u32 roce_min_ilt_lines = PXP_NUM_ILT_RECORDS_BB / MAX_NUM_PFS_BB;
3375 #endif
3376
3377         /* Setting the max values of the soft resources and the following
3378          * resources allocation queries should be atomic. Since several PFs can
3379          * run in parallel - a resource lock is needed.
3380          * If either the resource lock or resource set value commands are not
3381          * supported - skip the max values setting, release the lock if
3382          * needed, and proceed to the queries. Other failures, including a
3383          * failure to acquire the lock, will cause this function to fail.
3384          * Old drivers that don't acquire the lock can run in parallel, and
3385          * their allocation values won't be affected by the updated max values.
3386          */
3387         ecore_mcp_resc_lock_default_init(&resc_lock_params, &resc_unlock_params,
3388                                          ECORE_RESC_LOCK_RESC_ALLOC, false);
3389
3390         rc = ecore_mcp_resc_lock(p_hwfn, p_ptt, &resc_lock_params);
3391         if (rc != ECORE_SUCCESS && rc != ECORE_NOTIMPL) {
3392                 return rc;
3393         } else if (rc == ECORE_NOTIMPL) {
3394                 DP_INFO(p_hwfn,
3395                         "Skip the max values setting of the soft resources since the resource lock is not supported by the MFW\n");
3396         } else if (rc == ECORE_SUCCESS && !resc_lock_params.b_granted) {
3397                 DP_NOTICE(p_hwfn, false,
3398                           "Failed to acquire the resource lock for the resource allocation commands\n");
3399                 rc = ECORE_BUSY;
3400                 goto unlock_and_exit;
3401         } else {
3402                 rc = ecore_hw_set_soft_resc_size(p_hwfn, p_ptt);
3403                 if (rc != ECORE_SUCCESS && rc != ECORE_NOTIMPL) {
3404                         DP_NOTICE(p_hwfn, false,
3405                                   "Failed to set the max values of the soft resources\n");
3406                         goto unlock_and_exit;
3407                 } else if (rc == ECORE_NOTIMPL) {
3408                         DP_INFO(p_hwfn,
3409                                 "Skip the max values setting of the soft resources since it is not supported by the MFW\n");
3410                         rc = ecore_mcp_resc_unlock(p_hwfn, p_ptt,
3411                                                    &resc_unlock_params);
3412                         if (rc != ECORE_SUCCESS)
3413                                 DP_INFO(p_hwfn,
3414                                         "Failed to release the resource lock for the resource allocation commands\n");
3415                 }
3416         }
3417
3418         rc = ecore_hw_set_resc_info(p_hwfn, drv_resc_alloc);
3419         if (rc != ECORE_SUCCESS)
3420                 goto unlock_and_exit;
3421
3422         if (resc_lock_params.b_granted && !resc_unlock_params.b_released) {
3423                 rc = ecore_mcp_resc_unlock(p_hwfn, p_ptt,
3424                                            &resc_unlock_params);
3425                 if (rc != ECORE_SUCCESS)
3426                         DP_INFO(p_hwfn,
3427                                 "Failed to release the resource lock for the resource allocation commands\n");
3428         }
3429
3430 #ifndef ASIC_ONLY
3431         if (CHIP_REV_IS_SLOW(p_hwfn->p_dev)) {
3432                 /* Reduced build contains less PQs */
3433                 if (!(p_hwfn->p_dev->b_is_emul_full)) {
3434                         resc_num[ECORE_PQ] = 32;
3435                         resc_start[ECORE_PQ] = resc_num[ECORE_PQ] *
3436                             p_hwfn->enabled_func_idx;
3437                 }
3438
3439                 /* For AH emulation, since we have a possible maximal number of
3440                  * 16 enabled PFs, in case there are not enough ILT lines -
3441                  * allocate only first PF as RoCE and have all the other ETH
3442                  * only with less ILT lines.
3443                  */
3444                 if (!p_hwfn->rel_pf_id && p_hwfn->p_dev->b_is_emul_full)
3445                         resc_num[ECORE_ILT] = OSAL_MAX_T(u32,
3446                                                          resc_num[ECORE_ILT],
3447                                                          roce_min_ilt_lines);
3448         }
3449
3450         /* Correct the common ILT calculation if PF0 has more */
3451         if (CHIP_REV_IS_SLOW(p_hwfn->p_dev) &&
3452             p_hwfn->p_dev->b_is_emul_full &&
3453             p_hwfn->rel_pf_id && resc_num[ECORE_ILT] < roce_min_ilt_lines)
3454                 resc_start[ECORE_ILT] += roce_min_ilt_lines -
3455                     resc_num[ECORE_ILT];
3456 #endif
3457
3458         /* Sanity for ILT */
3459         if ((b_ah && (RESC_END(p_hwfn, ECORE_ILT) > PXP_NUM_ILT_RECORDS_K2)) ||
3460             (!b_ah && (RESC_END(p_hwfn, ECORE_ILT) > PXP_NUM_ILT_RECORDS_BB))) {
3461                 DP_NOTICE(p_hwfn, true,
3462                           "Can't assign ILT pages [%08x,...,%08x]\n",
3463                           RESC_START(p_hwfn, ECORE_ILT), RESC_END(p_hwfn,
3464                                                                   ECORE_ILT) -
3465                           1);
3466                 return ECORE_INVAL;
3467         }
3468
3469         /* This will also learn the number of SBs from MFW */
3470         if (ecore_int_igu_reset_cam(p_hwfn, p_ptt))
3471                 return ECORE_INVAL;
3472
3473         ecore_hw_set_feat(p_hwfn);
3474
3475         DP_VERBOSE(p_hwfn, ECORE_MSG_PROBE,
3476                    "The numbers for each resource are:\n");
3477         for (res_id = 0; res_id < ECORE_MAX_RESC; res_id++)
3478                 DP_VERBOSE(p_hwfn, ECORE_MSG_PROBE, "%s = %d start = %d\n",
3479                            ecore_hw_get_resc_name(res_id),
3480                            RESC_NUM(p_hwfn, res_id),
3481                            RESC_START(p_hwfn, res_id));
3482
3483         return ECORE_SUCCESS;
3484
3485 unlock_and_exit:
3486         if (resc_lock_params.b_granted && !resc_unlock_params.b_released)
3487                 ecore_mcp_resc_unlock(p_hwfn, p_ptt,
3488                                       &resc_unlock_params);
3489         return rc;
3490 }
3491
3492 static enum _ecore_status_t
3493 ecore_hw_get_nvm_info(struct ecore_hwfn *p_hwfn,
3494                       struct ecore_ptt *p_ptt,
3495                       struct ecore_hw_prepare_params *p_params)
3496 {
3497         u32 nvm_cfg1_offset, mf_mode, addr, generic_cont0, core_cfg, dcbx_mode;
3498         u32 port_cfg_addr, link_temp, nvm_cfg_addr, device_capabilities;
3499         struct ecore_mcp_link_capabilities *p_caps;
3500         struct ecore_mcp_link_params *link;
3501         enum _ecore_status_t rc;
3502
3503         /* Read global nvm_cfg address */
3504         nvm_cfg_addr = ecore_rd(p_hwfn, p_ptt, MISC_REG_GEN_PURP_CR0);
3505
3506         /* Verify MCP has initialized it */
3507         if (!nvm_cfg_addr) {
3508                 DP_NOTICE(p_hwfn, false, "Shared memory not initialized\n");
3509                 if (p_params->b_relaxed_probe)
3510                         p_params->p_relaxed_res = ECORE_HW_PREPARE_FAILED_NVM;
3511                 return ECORE_INVAL;
3512         }
3513
3514 /* Read nvm_cfg1  (Notice this is just offset, and not offsize (TBD) */
3515
3516         nvm_cfg1_offset = ecore_rd(p_hwfn, p_ptt, nvm_cfg_addr + 4);
3517
3518         addr = MCP_REG_SCRATCH + nvm_cfg1_offset +
3519                    OFFSETOF(struct nvm_cfg1, glob) +
3520                    OFFSETOF(struct nvm_cfg1_glob, core_cfg);
3521
3522         core_cfg = ecore_rd(p_hwfn, p_ptt, addr);
3523
3524         switch ((core_cfg & NVM_CFG1_GLOB_NETWORK_PORT_MODE_MASK) >>
3525                 NVM_CFG1_GLOB_NETWORK_PORT_MODE_OFFSET) {
3526         case NVM_CFG1_GLOB_NETWORK_PORT_MODE_BB_2X40G:
3527                 p_hwfn->hw_info.port_mode = ECORE_PORT_MODE_DE_2X40G;
3528                 break;
3529         case NVM_CFG1_GLOB_NETWORK_PORT_MODE_2X50G:
3530                 p_hwfn->hw_info.port_mode = ECORE_PORT_MODE_DE_2X50G;
3531                 break;
3532         case NVM_CFG1_GLOB_NETWORK_PORT_MODE_BB_1X100G:
3533                 p_hwfn->hw_info.port_mode = ECORE_PORT_MODE_DE_1X100G;
3534                 break;
3535         case NVM_CFG1_GLOB_NETWORK_PORT_MODE_4X10G_F:
3536                 p_hwfn->hw_info.port_mode = ECORE_PORT_MODE_DE_4X10G_F;
3537                 break;
3538         case NVM_CFG1_GLOB_NETWORK_PORT_MODE_BB_4X10G_E:
3539                 p_hwfn->hw_info.port_mode = ECORE_PORT_MODE_DE_4X10G_E;
3540                 break;
3541         case NVM_CFG1_GLOB_NETWORK_PORT_MODE_BB_4X20G:
3542                 p_hwfn->hw_info.port_mode = ECORE_PORT_MODE_DE_4X20G;
3543                 break;
3544         case NVM_CFG1_GLOB_NETWORK_PORT_MODE_1X40G:
3545                 p_hwfn->hw_info.port_mode = ECORE_PORT_MODE_DE_1X40G;
3546                 break;
3547         case NVM_CFG1_GLOB_NETWORK_PORT_MODE_2X25G:
3548                 p_hwfn->hw_info.port_mode = ECORE_PORT_MODE_DE_2X25G;
3549                 break;
3550         case NVM_CFG1_GLOB_NETWORK_PORT_MODE_2X10G:
3551                 p_hwfn->hw_info.port_mode = ECORE_PORT_MODE_DE_2X10G;
3552                 break;
3553         case NVM_CFG1_GLOB_NETWORK_PORT_MODE_1X25G:
3554                 p_hwfn->hw_info.port_mode = ECORE_PORT_MODE_DE_1X25G;
3555                 break;
3556         case NVM_CFG1_GLOB_NETWORK_PORT_MODE_4X25G:
3557                 p_hwfn->hw_info.port_mode = ECORE_PORT_MODE_DE_4X25G;
3558                 break;
3559         default:
3560                 DP_NOTICE(p_hwfn, true, "Unknown port mode in 0x%08x\n",
3561                           core_cfg);
3562                 break;
3563         }
3564
3565         /* Read DCBX configuration */
3566         port_cfg_addr = MCP_REG_SCRATCH + nvm_cfg1_offset +
3567                         OFFSETOF(struct nvm_cfg1, port[MFW_PORT(p_hwfn)]);
3568         dcbx_mode = ecore_rd(p_hwfn, p_ptt,
3569                              port_cfg_addr +
3570                              OFFSETOF(struct nvm_cfg1_port, generic_cont0));
3571         dcbx_mode = (dcbx_mode & NVM_CFG1_PORT_DCBX_MODE_MASK)
3572                 >> NVM_CFG1_PORT_DCBX_MODE_OFFSET;
3573         switch (dcbx_mode) {
3574         case NVM_CFG1_PORT_DCBX_MODE_DYNAMIC:
3575                 p_hwfn->hw_info.dcbx_mode = ECORE_DCBX_VERSION_DYNAMIC;
3576                 break;
3577         case NVM_CFG1_PORT_DCBX_MODE_CEE:
3578                 p_hwfn->hw_info.dcbx_mode = ECORE_DCBX_VERSION_CEE;
3579                 break;
3580         case NVM_CFG1_PORT_DCBX_MODE_IEEE:
3581                 p_hwfn->hw_info.dcbx_mode = ECORE_DCBX_VERSION_IEEE;
3582                 break;
3583         default:
3584                 p_hwfn->hw_info.dcbx_mode = ECORE_DCBX_VERSION_DISABLED;
3585         }
3586
3587         /* Read default link configuration */
3588         link = &p_hwfn->mcp_info->link_input;
3589         p_caps = &p_hwfn->mcp_info->link_capabilities;
3590         port_cfg_addr = MCP_REG_SCRATCH + nvm_cfg1_offset +
3591             OFFSETOF(struct nvm_cfg1, port[MFW_PORT(p_hwfn)]);
3592         link_temp = ecore_rd(p_hwfn, p_ptt,
3593                              port_cfg_addr +
3594                              OFFSETOF(struct nvm_cfg1_port, speed_cap_mask));
3595         link_temp &= NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_MASK;
3596         link->speed.advertised_speeds = link_temp;
3597         p_caps->speed_capabilities = link->speed.advertised_speeds;
3598
3599         link_temp = ecore_rd(p_hwfn, p_ptt,
3600                                  port_cfg_addr +
3601                                  OFFSETOF(struct nvm_cfg1_port, link_settings));
3602         switch ((link_temp & NVM_CFG1_PORT_DRV_LINK_SPEED_MASK) >>
3603                 NVM_CFG1_PORT_DRV_LINK_SPEED_OFFSET) {
3604         case NVM_CFG1_PORT_DRV_LINK_SPEED_AUTONEG:
3605                 link->speed.autoneg = true;
3606                 break;
3607         case NVM_CFG1_PORT_DRV_LINK_SPEED_1G:
3608                 link->speed.forced_speed = 1000;
3609                 break;
3610         case NVM_CFG1_PORT_DRV_LINK_SPEED_10G:
3611                 link->speed.forced_speed = 10000;
3612                 break;
3613         case NVM_CFG1_PORT_DRV_LINK_SPEED_25G:
3614                 link->speed.forced_speed = 25000;
3615                 break;
3616         case NVM_CFG1_PORT_DRV_LINK_SPEED_40G:
3617                 link->speed.forced_speed = 40000;
3618                 break;
3619         case NVM_CFG1_PORT_DRV_LINK_SPEED_50G:
3620                 link->speed.forced_speed = 50000;
3621                 break;
3622         case NVM_CFG1_PORT_DRV_LINK_SPEED_BB_100G:
3623                 link->speed.forced_speed = 100000;
3624                 break;
3625         default:
3626                 DP_NOTICE(p_hwfn, true, "Unknown Speed in 0x%08x\n", link_temp);
3627         }
3628
3629         p_caps->default_speed = link->speed.forced_speed;
3630         p_caps->default_speed_autoneg = link->speed.autoneg;
3631
3632         link_temp &= NVM_CFG1_PORT_DRV_FLOW_CONTROL_MASK;
3633         link_temp >>= NVM_CFG1_PORT_DRV_FLOW_CONTROL_OFFSET;
3634         link->pause.autoneg = !!(link_temp &
3635                                   NVM_CFG1_PORT_DRV_FLOW_CONTROL_AUTONEG);
3636         link->pause.forced_rx = !!(link_temp &
3637                                     NVM_CFG1_PORT_DRV_FLOW_CONTROL_RX);
3638         link->pause.forced_tx = !!(link_temp &
3639                                     NVM_CFG1_PORT_DRV_FLOW_CONTROL_TX);
3640         link->loopback_mode = 0;
3641
3642         if (p_hwfn->mcp_info->capabilities & FW_MB_PARAM_FEATURE_SUPPORT_EEE) {
3643                 link_temp = ecore_rd(p_hwfn, p_ptt, port_cfg_addr +
3644                                      OFFSETOF(struct nvm_cfg1_port, ext_phy));
3645                 link_temp &= NVM_CFG1_PORT_EEE_POWER_SAVING_MODE_MASK;
3646                 link_temp >>= NVM_CFG1_PORT_EEE_POWER_SAVING_MODE_OFFSET;
3647                 p_caps->default_eee = ECORE_MCP_EEE_ENABLED;
3648                 link->eee.enable = true;
3649                 switch (link_temp) {
3650                 case NVM_CFG1_PORT_EEE_POWER_SAVING_MODE_DISABLED:
3651                         p_caps->default_eee = ECORE_MCP_EEE_DISABLED;
3652                         link->eee.enable = false;
3653                         break;
3654                 case NVM_CFG1_PORT_EEE_POWER_SAVING_MODE_BALANCED:
3655                         p_caps->eee_lpi_timer = EEE_TX_TIMER_USEC_BALANCED_TIME;
3656                         break;
3657                 case NVM_CFG1_PORT_EEE_POWER_SAVING_MODE_AGGRESSIVE:
3658                         p_caps->eee_lpi_timer =
3659                                 EEE_TX_TIMER_USEC_AGGRESSIVE_TIME;
3660                         break;
3661                 case NVM_CFG1_PORT_EEE_POWER_SAVING_MODE_LOW_LATENCY:
3662                         p_caps->eee_lpi_timer = EEE_TX_TIMER_USEC_LATENCY_TIME;
3663                         break;
3664                 }
3665
3666                 link->eee.tx_lpi_timer = p_caps->eee_lpi_timer;
3667                 link->eee.tx_lpi_enable = link->eee.enable;
3668                 link->eee.adv_caps = ECORE_EEE_1G_ADV | ECORE_EEE_10G_ADV;
3669         } else {
3670                 p_caps->default_eee = ECORE_MCP_EEE_UNSUPPORTED;
3671         }
3672
3673         DP_VERBOSE(p_hwfn, ECORE_MSG_LINK,
3674                    "Read default link: Speed 0x%08x, Adv. Speed 0x%08x, AN: 0x%02x, PAUSE AN: 0x%02x\n EEE: %02x [%08x usec]",
3675                    link->speed.forced_speed, link->speed.advertised_speeds,
3676                    link->speed.autoneg, link->pause.autoneg,
3677                    p_caps->default_eee, p_caps->eee_lpi_timer);
3678
3679         /* Read Multi-function information from shmem */
3680         addr = MCP_REG_SCRATCH + nvm_cfg1_offset +
3681                    OFFSETOF(struct nvm_cfg1, glob) +
3682                    OFFSETOF(struct nvm_cfg1_glob, generic_cont0);
3683
3684         generic_cont0 = ecore_rd(p_hwfn, p_ptt, addr);
3685
3686         mf_mode = (generic_cont0 & NVM_CFG1_GLOB_MF_MODE_MASK) >>
3687             NVM_CFG1_GLOB_MF_MODE_OFFSET;
3688
3689         switch (mf_mode) {
3690         case NVM_CFG1_GLOB_MF_MODE_MF_ALLOWED:
3691                 p_hwfn->p_dev->mf_bits = 1 << ECORE_MF_OVLAN_CLSS;
3692                 break;
3693         case NVM_CFG1_GLOB_MF_MODE_UFP:
3694                 p_hwfn->p_dev->mf_bits = 1 << ECORE_MF_OVLAN_CLSS |
3695                                          1 << ECORE_MF_UFP_SPECIFIC |
3696                                          1 << ECORE_MF_8021Q_TAGGING;
3697                 break;
3698         case NVM_CFG1_GLOB_MF_MODE_BD:
3699                 p_hwfn->p_dev->mf_bits = 1 << ECORE_MF_OVLAN_CLSS |
3700                                          1 << ECORE_MF_LLH_PROTO_CLSS |
3701                                          1 << ECORE_MF_8021AD_TAGGING;
3702                 break;
3703         case NVM_CFG1_GLOB_MF_MODE_NPAR1_0:
3704                 p_hwfn->p_dev->mf_bits = 1 << ECORE_MF_LLH_MAC_CLSS |
3705                                          1 << ECORE_MF_LLH_PROTO_CLSS |
3706                                          1 << ECORE_MF_LL2_NON_UNICAST |
3707                                          1 << ECORE_MF_INTER_PF_SWITCH |
3708                                          1 << ECORE_MF_DISABLE_ARFS;
3709                 break;
3710         case NVM_CFG1_GLOB_MF_MODE_DEFAULT:
3711                 p_hwfn->p_dev->mf_bits = 1 << ECORE_MF_LLH_MAC_CLSS |
3712                                          1 << ECORE_MF_LLH_PROTO_CLSS |
3713                                          1 << ECORE_MF_LL2_NON_UNICAST;
3714                 if (ECORE_IS_BB(p_hwfn->p_dev))
3715                         p_hwfn->p_dev->mf_bits |= 1 << ECORE_MF_NEED_DEF_PF;
3716                 break;
3717         }
3718         DP_INFO(p_hwfn, "Multi function mode is 0x%lx\n",
3719                 p_hwfn->p_dev->mf_bits);
3720
3721         if (ECORE_IS_CMT(p_hwfn->p_dev))
3722                 p_hwfn->p_dev->mf_bits |= (1 << ECORE_MF_DISABLE_ARFS);
3723
3724         /* It's funny since we have another switch, but it's easier
3725          * to throw this away in linux this way. Long term, it might be
3726          * better to have have getters for needed ECORE_MF_* fields,
3727          * convert client code and eliminate this.
3728          */
3729         switch (mf_mode) {
3730         case NVM_CFG1_GLOB_MF_MODE_MF_ALLOWED:
3731         case NVM_CFG1_GLOB_MF_MODE_BD:
3732                 p_hwfn->p_dev->mf_mode = ECORE_MF_OVLAN;
3733                 break;
3734         case NVM_CFG1_GLOB_MF_MODE_NPAR1_0:
3735                 p_hwfn->p_dev->mf_mode = ECORE_MF_NPAR;
3736                 break;
3737         case NVM_CFG1_GLOB_MF_MODE_DEFAULT:
3738                 p_hwfn->p_dev->mf_mode = ECORE_MF_DEFAULT;
3739                 break;
3740         case NVM_CFG1_GLOB_MF_MODE_UFP:
3741                 p_hwfn->p_dev->mf_mode = ECORE_MF_UFP;
3742                 break;
3743         }
3744
3745         /* Read Multi-function information from shmem */
3746         addr = MCP_REG_SCRATCH + nvm_cfg1_offset +
3747                    OFFSETOF(struct nvm_cfg1, glob) +
3748                    OFFSETOF(struct nvm_cfg1_glob, device_capabilities);
3749
3750         device_capabilities = ecore_rd(p_hwfn, p_ptt, addr);
3751         if (device_capabilities & NVM_CFG1_GLOB_DEVICE_CAPABILITIES_ETHERNET)
3752                 OSAL_SET_BIT(ECORE_DEV_CAP_ETH,
3753                                 &p_hwfn->hw_info.device_capabilities);
3754         if (device_capabilities & NVM_CFG1_GLOB_DEVICE_CAPABILITIES_FCOE)
3755                 OSAL_SET_BIT(ECORE_DEV_CAP_FCOE,
3756                                 &p_hwfn->hw_info.device_capabilities);
3757         if (device_capabilities & NVM_CFG1_GLOB_DEVICE_CAPABILITIES_ISCSI)
3758                 OSAL_SET_BIT(ECORE_DEV_CAP_ISCSI,
3759                                 &p_hwfn->hw_info.device_capabilities);
3760         if (device_capabilities & NVM_CFG1_GLOB_DEVICE_CAPABILITIES_ROCE)
3761                 OSAL_SET_BIT(ECORE_DEV_CAP_ROCE,
3762                                 &p_hwfn->hw_info.device_capabilities);
3763         if (device_capabilities & NVM_CFG1_GLOB_DEVICE_CAPABILITIES_IWARP)
3764                 OSAL_SET_BIT(ECORE_DEV_CAP_IWARP,
3765                                 &p_hwfn->hw_info.device_capabilities);
3766
3767         rc = ecore_mcp_fill_shmem_func_info(p_hwfn, p_ptt);
3768         if (rc != ECORE_SUCCESS && p_params->b_relaxed_probe) {
3769                 rc = ECORE_SUCCESS;
3770                 p_params->p_relaxed_res = ECORE_HW_PREPARE_BAD_MCP;
3771         }
3772
3773         return rc;
3774 }
3775
3776 static void ecore_get_num_funcs(struct ecore_hwfn *p_hwfn,
3777                                 struct ecore_ptt *p_ptt)
3778 {
3779         u8 num_funcs, enabled_func_idx = p_hwfn->rel_pf_id;
3780         u32 reg_function_hide, tmp, eng_mask, low_pfs_mask;
3781         struct ecore_dev *p_dev = p_hwfn->p_dev;
3782
3783         num_funcs = ECORE_IS_AH(p_dev) ? MAX_NUM_PFS_K2 : MAX_NUM_PFS_BB;
3784
3785         /* Bit 0 of MISCS_REG_FUNCTION_HIDE indicates whether the bypass values
3786          * in the other bits are selected.
3787          * Bits 1-15 are for functions 1-15, respectively, and their value is
3788          * '0' only for enabled functions (function 0 always exists and
3789          * enabled).
3790          * In case of CMT in BB, only the "even" functions are enabled, and thus
3791          * the number of functions for both hwfns is learnt from the same bits.
3792          */
3793         if (ECORE_IS_BB(p_dev) || ECORE_IS_AH(p_dev)) {
3794                 reg_function_hide = ecore_rd(p_hwfn, p_ptt,
3795                                              MISCS_REG_FUNCTION_HIDE_BB_K2);
3796         } else { /* E5 */
3797                 reg_function_hide = 0;
3798         }
3799
3800         if (reg_function_hide & 0x1) {
3801                 if (ECORE_IS_BB(p_dev)) {
3802                         if (ECORE_PATH_ID(p_hwfn) && !ECORE_IS_CMT(p_dev)) {
3803                                 num_funcs = 0;
3804                                 eng_mask = 0xaaaa;
3805                         } else {
3806                                 num_funcs = 1;
3807                                 eng_mask = 0x5554;
3808                         }
3809                 } else {
3810                         num_funcs = 1;
3811                         eng_mask = 0xfffe;
3812                 }
3813
3814                 /* Get the number of the enabled functions on the engine */
3815                 tmp = (reg_function_hide ^ 0xffffffff) & eng_mask;
3816                 while (tmp) {
3817                         if (tmp & 0x1)
3818                                 num_funcs++;
3819                         tmp >>= 0x1;
3820                 }
3821
3822                 /* Get the PF index within the enabled functions */
3823                 low_pfs_mask = (0x1 << p_hwfn->abs_pf_id) - 1;
3824                 tmp = reg_function_hide & eng_mask & low_pfs_mask;
3825                 while (tmp) {
3826                         if (tmp & 0x1)
3827                                 enabled_func_idx--;
3828                         tmp >>= 0x1;
3829                 }
3830         }
3831
3832         p_hwfn->num_funcs_on_engine = num_funcs;
3833         p_hwfn->enabled_func_idx = enabled_func_idx;
3834
3835 #ifndef ASIC_ONLY
3836         if (CHIP_REV_IS_FPGA(p_dev)) {
3837                 DP_NOTICE(p_hwfn, false,
3838                           "FPGA: Limit number of PFs to 4 [would affect resource allocation, needed for IOV]\n");
3839                 p_hwfn->num_funcs_on_engine = 4;
3840         }
3841 #endif
3842
3843         DP_VERBOSE(p_hwfn, ECORE_MSG_PROBE,
3844                    "PF [rel_id %d, abs_id %d] occupies index %d within the %d enabled functions on the engine\n",
3845                    p_hwfn->rel_pf_id, p_hwfn->abs_pf_id,
3846                    p_hwfn->enabled_func_idx, p_hwfn->num_funcs_on_engine);
3847 }
3848
3849 static void ecore_hw_info_port_num_bb(struct ecore_hwfn *p_hwfn,
3850                                       struct ecore_ptt *p_ptt)
3851 {
3852         struct ecore_dev *p_dev = p_hwfn->p_dev;
3853         u32 port_mode;
3854
3855 #ifndef ASIC_ONLY
3856         /* Read the port mode */
3857         if (CHIP_REV_IS_FPGA(p_dev))
3858                 port_mode = 4;
3859         else if (CHIP_REV_IS_EMUL(p_dev) && ECORE_IS_CMT(p_dev))
3860                 /* In CMT on emulation, assume 1 port */
3861                 port_mode = 1;
3862         else
3863 #endif
3864         port_mode = ecore_rd(p_hwfn, p_ptt, CNIG_REG_NW_PORT_MODE_BB);
3865
3866         if (port_mode < 3) {
3867                 p_dev->num_ports_in_engine = 1;
3868         } else if (port_mode <= 5) {
3869                 p_dev->num_ports_in_engine = 2;
3870         } else {
3871                 DP_NOTICE(p_hwfn, true, "PORT MODE: %d not supported\n",
3872                           p_dev->num_ports_in_engine);
3873
3874                 /* Default num_ports_in_engine to something */
3875                 p_dev->num_ports_in_engine = 1;
3876         }
3877 }
3878
3879 static void ecore_hw_info_port_num_ah_e5(struct ecore_hwfn *p_hwfn,
3880                                          struct ecore_ptt *p_ptt)
3881 {
3882         struct ecore_dev *p_dev = p_hwfn->p_dev;
3883         u32 port;
3884         int i;
3885
3886         p_dev->num_ports_in_engine = 0;
3887
3888 #ifndef ASIC_ONLY
3889         if (CHIP_REV_IS_EMUL(p_dev)) {
3890                 port = ecore_rd(p_hwfn, p_ptt, MISCS_REG_ECO_RESERVED);
3891                 switch ((port & 0xf000) >> 12) {
3892                 case 1:
3893                         p_dev->num_ports_in_engine = 1;
3894                         break;
3895                 case 3:
3896                         p_dev->num_ports_in_engine = 2;
3897                         break;
3898                 case 0xf:
3899                         p_dev->num_ports_in_engine = 4;
3900                         break;
3901                 default:
3902                         DP_NOTICE(p_hwfn, false,
3903                                   "Unknown port mode in ECO_RESERVED %08x\n",
3904                                   port);
3905                 }
3906         } else
3907 #endif
3908                 for (i = 0; i < MAX_NUM_PORTS_K2; i++) {
3909                         port = ecore_rd(p_hwfn, p_ptt,
3910                                         CNIG_REG_NIG_PORT0_CONF_K2_E5 +
3911                                         (i * 4));
3912                         if (port & 1)
3913                                 p_dev->num_ports_in_engine++;
3914                 }
3915
3916         if (!p_dev->num_ports_in_engine) {
3917                 DP_NOTICE(p_hwfn, true, "All NIG ports are inactive\n");
3918
3919                 /* Default num_ports_in_engine to something */
3920                 p_dev->num_ports_in_engine = 1;
3921         }
3922 }
3923
3924 static void ecore_hw_info_port_num(struct ecore_hwfn *p_hwfn,
3925                                    struct ecore_ptt *p_ptt)
3926 {
3927         struct ecore_dev *p_dev = p_hwfn->p_dev;
3928
3929         /* Determine the number of ports per engine */
3930         if (ECORE_IS_BB(p_dev))
3931                 ecore_hw_info_port_num_bb(p_hwfn, p_ptt);
3932         else
3933                 ecore_hw_info_port_num_ah_e5(p_hwfn, p_ptt);
3934
3935         /* Get the total number of ports of the device */
3936         if (ECORE_IS_CMT(p_dev)) {
3937                 /* In CMT there is always only one port */
3938                 p_dev->num_ports = 1;
3939 #ifndef ASIC_ONLY
3940         } else if (CHIP_REV_IS_EMUL(p_dev) || CHIP_REV_IS_TEDIBEAR(p_dev)) {
3941                 p_dev->num_ports = p_dev->num_ports_in_engine *
3942                                    ecore_device_num_engines(p_dev);
3943 #endif
3944         } else {
3945                 u32 addr, global_offsize, global_addr;
3946
3947                 addr = SECTION_OFFSIZE_ADDR(p_hwfn->mcp_info->public_base,
3948                                             PUBLIC_GLOBAL);
3949                 global_offsize = ecore_rd(p_hwfn, p_ptt, addr);
3950                 global_addr = SECTION_ADDR(global_offsize, 0);
3951                 addr = global_addr + OFFSETOF(struct public_global, max_ports);
3952                 p_dev->num_ports = (u8)ecore_rd(p_hwfn, p_ptt, addr);
3953         }
3954 }
3955
3956 static void ecore_mcp_get_eee_caps(struct ecore_hwfn *p_hwfn,
3957                                    struct ecore_ptt *p_ptt)
3958 {
3959         struct ecore_mcp_link_capabilities *p_caps;
3960         u32 eee_status;
3961
3962         p_caps = &p_hwfn->mcp_info->link_capabilities;
3963         if (p_caps->default_eee == ECORE_MCP_EEE_UNSUPPORTED)
3964                 return;
3965
3966         p_caps->eee_speed_caps = 0;
3967         eee_status = ecore_rd(p_hwfn, p_ptt, p_hwfn->mcp_info->port_addr +
3968                               OFFSETOF(struct public_port, eee_status));
3969         eee_status = (eee_status & EEE_SUPPORTED_SPEED_MASK) >>
3970                         EEE_SUPPORTED_SPEED_OFFSET;
3971         if (eee_status & EEE_1G_SUPPORTED)
3972                 p_caps->eee_speed_caps |= ECORE_EEE_1G_ADV;
3973         if (eee_status & EEE_10G_ADV)
3974                 p_caps->eee_speed_caps |= ECORE_EEE_10G_ADV;
3975 }
3976
3977 static enum _ecore_status_t
3978 ecore_get_hw_info(struct ecore_hwfn *p_hwfn, struct ecore_ptt *p_ptt,
3979                   enum ecore_pci_personality personality,
3980                   struct ecore_hw_prepare_params *p_params)
3981 {
3982         bool drv_resc_alloc = p_params->drv_resc_alloc;
3983         enum _ecore_status_t rc;
3984
3985         if (IS_ECORE_PACING(p_hwfn)) {
3986                 DP_VERBOSE(p_hwfn->p_dev, ECORE_MSG_IOV,
3987                            "Skipping IOV as packet pacing is requested\n");
3988         }
3989
3990         /* Since all information is common, only first hwfns should do this */
3991         if (IS_LEAD_HWFN(p_hwfn) && !IS_ECORE_PACING(p_hwfn)) {
3992                 rc = ecore_iov_hw_info(p_hwfn);
3993                 if (rc != ECORE_SUCCESS) {
3994                         if (p_params->b_relaxed_probe)
3995                                 p_params->p_relaxed_res =
3996                                                 ECORE_HW_PREPARE_BAD_IOV;
3997                         else
3998                                 return rc;
3999                 }
4000         }
4001
4002         if (IS_LEAD_HWFN(p_hwfn))
4003                 ecore_hw_info_port_num(p_hwfn, p_ptt);
4004
4005         ecore_mcp_get_capabilities(p_hwfn, p_ptt);
4006
4007 #ifndef ASIC_ONLY
4008         if (CHIP_REV_IS_ASIC(p_hwfn->p_dev)) {
4009 #endif
4010         rc = ecore_hw_get_nvm_info(p_hwfn, p_ptt, p_params);
4011         if (rc != ECORE_SUCCESS)
4012                 return rc;
4013 #ifndef ASIC_ONLY
4014         }
4015 #endif
4016
4017         rc = ecore_int_igu_read_cam(p_hwfn, p_ptt);
4018         if (rc != ECORE_SUCCESS) {
4019                 if (p_params->b_relaxed_probe)
4020                         p_params->p_relaxed_res = ECORE_HW_PREPARE_BAD_IGU;
4021                 else
4022                         return rc;
4023         }
4024
4025 #ifndef ASIC_ONLY
4026         if (CHIP_REV_IS_ASIC(p_hwfn->p_dev) && ecore_mcp_is_init(p_hwfn)) {
4027 #endif
4028                 OSAL_MEMCPY(p_hwfn->hw_info.hw_mac_addr,
4029                             p_hwfn->mcp_info->func_info.mac, ETH_ALEN);
4030 #ifndef ASIC_ONLY
4031         } else {
4032                 static u8 mcp_hw_mac[6] = { 0, 2, 3, 4, 5, 6 };
4033
4034                 OSAL_MEMCPY(p_hwfn->hw_info.hw_mac_addr, mcp_hw_mac, ETH_ALEN);
4035                 p_hwfn->hw_info.hw_mac_addr[5] = p_hwfn->abs_pf_id;
4036         }
4037 #endif
4038
4039         if (ecore_mcp_is_init(p_hwfn)) {
4040                 if (p_hwfn->mcp_info->func_info.ovlan != ECORE_MCP_VLAN_UNSET)
4041                         p_hwfn->hw_info.ovlan =
4042                             p_hwfn->mcp_info->func_info.ovlan;
4043
4044                 ecore_mcp_cmd_port_init(p_hwfn, p_ptt);
4045
4046                 ecore_mcp_get_eee_caps(p_hwfn, p_ptt);
4047
4048                 ecore_mcp_read_ufp_config(p_hwfn, p_ptt);
4049         }
4050
4051         if (personality != ECORE_PCI_DEFAULT) {
4052                 p_hwfn->hw_info.personality = personality;
4053         } else if (ecore_mcp_is_init(p_hwfn)) {
4054                 enum ecore_pci_personality protocol;
4055
4056                 protocol = p_hwfn->mcp_info->func_info.protocol;
4057                 p_hwfn->hw_info.personality = protocol;
4058         }
4059
4060 #ifndef ASIC_ONLY
4061         /* To overcome ILT lack for emulation, until at least until we'll have
4062          * a definite answer from system about it, allow only PF0 to be RoCE.
4063          */
4064         if (CHIP_REV_IS_EMUL(p_hwfn->p_dev) && ECORE_IS_AH(p_hwfn->p_dev)) {
4065                 if (!p_hwfn->rel_pf_id)
4066                         p_hwfn->hw_info.personality = ECORE_PCI_ETH_ROCE;
4067                 else
4068                         p_hwfn->hw_info.personality = ECORE_PCI_ETH;
4069         }
4070 #endif
4071
4072         /* although in BB some constellations may support more than 4 tcs,
4073          * that can result in performance penalty in some cases. 4
4074          * represents a good tradeoff between performance and flexibility.
4075          */
4076         if (IS_ECORE_PACING(p_hwfn))
4077                 p_hwfn->hw_info.num_hw_tc = 1;
4078         else
4079                 p_hwfn->hw_info.num_hw_tc = NUM_PHYS_TCS_4PORT_K2;
4080
4081         /* start out with a single active tc. This can be increased either
4082          * by dcbx negotiation or by upper layer driver
4083          */
4084         p_hwfn->hw_info.num_active_tc = 1;
4085
4086         ecore_get_num_funcs(p_hwfn, p_ptt);
4087
4088         if (ecore_mcp_is_init(p_hwfn))
4089                 p_hwfn->hw_info.mtu = p_hwfn->mcp_info->func_info.mtu;
4090
4091         /* In case of forcing the driver's default resource allocation, calling
4092          * ecore_hw_get_resc() should come after initializing the personality
4093          * and after getting the number of functions, since the calculation of
4094          * the resources/features depends on them.
4095          * This order is not harmful if not forcing.
4096          */
4097         rc = ecore_hw_get_resc(p_hwfn, p_ptt, drv_resc_alloc);
4098         if (rc != ECORE_SUCCESS && p_params->b_relaxed_probe) {
4099                 rc = ECORE_SUCCESS;
4100                 p_params->p_relaxed_res = ECORE_HW_PREPARE_BAD_MCP;
4101         }
4102
4103         return rc;
4104 }
4105
4106 static enum _ecore_status_t ecore_get_dev_info(struct ecore_hwfn *p_hwfn,
4107                                                struct ecore_ptt *p_ptt)
4108 {
4109         struct ecore_dev *p_dev = p_hwfn->p_dev;
4110         u16 device_id_mask;
4111         u32 tmp;
4112
4113         /* Read Vendor Id / Device Id */
4114         OSAL_PCI_READ_CONFIG_WORD(p_dev, PCICFG_VENDOR_ID_OFFSET,
4115                                   &p_dev->vendor_id);
4116         OSAL_PCI_READ_CONFIG_WORD(p_dev, PCICFG_DEVICE_ID_OFFSET,
4117                                   &p_dev->device_id);
4118
4119         /* Determine type */
4120         device_id_mask = p_dev->device_id & ECORE_DEV_ID_MASK;
4121         switch (device_id_mask) {
4122         case ECORE_DEV_ID_MASK_BB:
4123                 p_dev->type = ECORE_DEV_TYPE_BB;
4124                 break;
4125         case ECORE_DEV_ID_MASK_AH:
4126                 p_dev->type = ECORE_DEV_TYPE_AH;
4127                 break;
4128         default:
4129                 DP_NOTICE(p_hwfn, true, "Unknown device id 0x%x\n",
4130                           p_dev->device_id);
4131                 return ECORE_ABORTED;
4132         }
4133
4134         tmp = ecore_rd(p_hwfn, p_ptt, MISCS_REG_CHIP_NUM);
4135         p_dev->chip_num = (u16)GET_FIELD(tmp, CHIP_NUM);
4136         tmp = ecore_rd(p_hwfn, p_ptt, MISCS_REG_CHIP_REV);
4137         p_dev->chip_rev = (u8)GET_FIELD(tmp, CHIP_REV);
4138
4139         /* Learn number of HW-functions */
4140         tmp = ecore_rd(p_hwfn, p_ptt, MISCS_REG_CMT_ENABLED_FOR_PAIR);
4141
4142         if (tmp & (1 << p_hwfn->rel_pf_id)) {
4143                 DP_NOTICE(p_dev->hwfns, false, "device in CMT mode\n");
4144                 p_dev->num_hwfns = 2;
4145         } else {
4146                 p_dev->num_hwfns = 1;
4147         }
4148
4149 #ifndef ASIC_ONLY
4150         if (CHIP_REV_IS_EMUL(p_dev)) {
4151                 /* For some reason we have problems with this register
4152                  * in B0 emulation; Simply assume no CMT
4153                  */
4154                 DP_NOTICE(p_dev->hwfns, false,
4155                           "device on emul - assume no CMT\n");
4156                 p_dev->num_hwfns = 1;
4157         }
4158 #endif
4159
4160         tmp = ecore_rd(p_hwfn, p_ptt, MISCS_REG_CHIP_TEST_REG);
4161         p_dev->chip_bond_id = (u8)GET_FIELD(tmp, CHIP_BOND_ID);
4162         tmp = ecore_rd(p_hwfn, p_ptt, MISCS_REG_CHIP_METAL);
4163         p_dev->chip_metal = (u8)GET_FIELD(tmp, CHIP_METAL);
4164
4165         DP_INFO(p_dev->hwfns,
4166                 "Chip details - %s %c%d, Num: %04x Rev: %02x Bond id: %02x Metal: %02x\n",
4167                 ECORE_IS_BB(p_dev) ? "BB" : "AH",
4168                 'A' + p_dev->chip_rev, (int)p_dev->chip_metal,
4169                 p_dev->chip_num, p_dev->chip_rev, p_dev->chip_bond_id,
4170                 p_dev->chip_metal);
4171
4172         if (ECORE_IS_BB_A0(p_dev)) {
4173                 DP_NOTICE(p_dev->hwfns, false,
4174                           "The chip type/rev (BB A0) is not supported!\n");
4175                 return ECORE_ABORTED;
4176         }
4177 #ifndef ASIC_ONLY
4178         if (CHIP_REV_IS_EMUL(p_dev) && ECORE_IS_AH(p_dev))
4179                 ecore_wr(p_hwfn, p_ptt, MISCS_REG_PLL_MAIN_CTRL_4, 0x1);
4180
4181         if (CHIP_REV_IS_EMUL(p_dev)) {
4182                 tmp = ecore_rd(p_hwfn, p_ptt, MISCS_REG_ECO_RESERVED);
4183                 if (tmp & (1 << 29)) {
4184                         DP_NOTICE(p_hwfn, false,
4185                                   "Emulation: Running on a FULL build\n");
4186                         p_dev->b_is_emul_full = true;
4187                 } else {
4188                         DP_NOTICE(p_hwfn, false,
4189                                   "Emulation: Running on a REDUCED build\n");
4190                 }
4191         }
4192 #endif
4193
4194         return ECORE_SUCCESS;
4195 }
4196
4197 #ifndef LINUX_REMOVE
4198 void ecore_prepare_hibernate(struct ecore_dev *p_dev)
4199 {
4200         int j;
4201
4202         if (IS_VF(p_dev))
4203                 return;
4204
4205         for_each_hwfn(p_dev, j) {
4206                 struct ecore_hwfn *p_hwfn = &p_dev->hwfns[j];
4207
4208                 DP_VERBOSE(p_hwfn, ECORE_MSG_IFDOWN,
4209                            "Mark hw/fw uninitialized\n");
4210
4211                 p_hwfn->hw_init_done = false;
4212
4213                 ecore_ptt_invalidate(p_hwfn);
4214         }
4215 }
4216 #endif
4217
4218 static enum _ecore_status_t
4219 ecore_hw_prepare_single(struct ecore_hwfn *p_hwfn,
4220                         void OSAL_IOMEM * p_regview,
4221                         void OSAL_IOMEM * p_doorbells,
4222                         struct ecore_hw_prepare_params *p_params)
4223 {
4224         struct ecore_mdump_retain_data mdump_retain;
4225         struct ecore_dev *p_dev = p_hwfn->p_dev;
4226         struct ecore_mdump_info mdump_info;
4227         enum _ecore_status_t rc = ECORE_SUCCESS;
4228
4229         /* Split PCI bars evenly between hwfns */
4230         p_hwfn->regview = p_regview;
4231         p_hwfn->doorbells = p_doorbells;
4232
4233         if (IS_VF(p_dev))
4234                 return ecore_vf_hw_prepare(p_hwfn);
4235
4236         /* Validate that chip access is feasible */
4237         if (REG_RD(p_hwfn, PXP_PF_ME_OPAQUE_ADDR) == 0xffffffff) {
4238                 DP_ERR(p_hwfn,
4239                        "Reading the ME register returns all Fs; Preventing further chip access\n");
4240                 if (p_params->b_relaxed_probe)
4241                         p_params->p_relaxed_res = ECORE_HW_PREPARE_FAILED_ME;
4242                 return ECORE_INVAL;
4243         }
4244
4245         get_function_id(p_hwfn);
4246
4247         /* Allocate PTT pool */
4248         rc = ecore_ptt_pool_alloc(p_hwfn);
4249         if (rc) {
4250                 DP_NOTICE(p_hwfn, false, "Failed to prepare hwfn's hw\n");
4251                 if (p_params->b_relaxed_probe)
4252                         p_params->p_relaxed_res = ECORE_HW_PREPARE_FAILED_MEM;
4253                 goto err0;
4254         }
4255
4256         /* Allocate the main PTT */
4257         p_hwfn->p_main_ptt = ecore_get_reserved_ptt(p_hwfn, RESERVED_PTT_MAIN);
4258
4259         /* First hwfn learns basic information, e.g., number of hwfns */
4260         if (!p_hwfn->my_id) {
4261                 rc = ecore_get_dev_info(p_hwfn, p_hwfn->p_main_ptt);
4262                 if (rc != ECORE_SUCCESS) {
4263                         if (p_params->b_relaxed_probe)
4264                                 p_params->p_relaxed_res =
4265                                         ECORE_HW_PREPARE_FAILED_DEV;
4266                         goto err1;
4267                 }
4268         }
4269
4270         ecore_hw_hwfn_prepare(p_hwfn);
4271
4272         /* Initialize MCP structure */
4273         rc = ecore_mcp_cmd_init(p_hwfn, p_hwfn->p_main_ptt);
4274         if (rc) {
4275                 DP_NOTICE(p_hwfn, false, "Failed initializing mcp command\n");
4276                 if (p_params->b_relaxed_probe)
4277                         p_params->p_relaxed_res = ECORE_HW_PREPARE_FAILED_MEM;
4278                 goto err1;
4279         }
4280
4281         /* Read the device configuration information from the HW and SHMEM */
4282         rc = ecore_get_hw_info(p_hwfn, p_hwfn->p_main_ptt,
4283                                p_params->personality, p_params);
4284         if (rc) {
4285                 DP_NOTICE(p_hwfn, false, "Failed to get HW information\n");
4286                 goto err2;
4287         }
4288
4289         /* Sending a mailbox to the MFW should be after ecore_get_hw_info() is
4290          * called, since among others it sets the ports number in an engine.
4291          */
4292         if (p_params->initiate_pf_flr && IS_LEAD_HWFN(p_hwfn) &&
4293             !p_dev->recov_in_prog) {
4294                 rc = ecore_mcp_initiate_pf_flr(p_hwfn, p_hwfn->p_main_ptt);
4295                 if (rc != ECORE_SUCCESS)
4296                         DP_NOTICE(p_hwfn, false, "Failed to initiate PF FLR\n");
4297
4298                 /* Workaround for MFW issue where PF FLR does not cleanup
4299                  * IGU block
4300                  */
4301                 if (!(p_hwfn->mcp_info->capabilities &
4302                       FW_MB_PARAM_FEATURE_SUPPORT_IGU_CLEANUP))
4303                         ecore_pf_flr_igu_cleanup(p_hwfn);
4304         }
4305
4306         /* Check if mdump logs/data are present and update the epoch value */
4307         if (IS_LEAD_HWFN(p_hwfn)) {
4308 #ifndef ASIC_ONLY
4309                 if (!CHIP_REV_IS_EMUL(p_dev)) {
4310 #endif
4311                 rc = ecore_mcp_mdump_get_info(p_hwfn, p_hwfn->p_main_ptt,
4312                                               &mdump_info);
4313                 if (rc == ECORE_SUCCESS && mdump_info.num_of_logs)
4314                         DP_NOTICE(p_hwfn, false,
4315                                   "* * * IMPORTANT - HW ERROR register dump captured by device * * *\n");
4316
4317                 rc = ecore_mcp_mdump_get_retain(p_hwfn, p_hwfn->p_main_ptt,
4318                                                 &mdump_retain);
4319                 if (rc == ECORE_SUCCESS && mdump_retain.valid)
4320                         DP_NOTICE(p_hwfn, false,
4321                                   "mdump retained data: epoch 0x%08x, pf 0x%x, status 0x%08x\n",
4322                                   mdump_retain.epoch, mdump_retain.pf,
4323                                   mdump_retain.status);
4324
4325                 ecore_mcp_mdump_set_values(p_hwfn, p_hwfn->p_main_ptt,
4326                                            p_params->epoch);
4327 #ifndef ASIC_ONLY
4328                 }
4329 #endif
4330         }
4331
4332         /* Allocate the init RT array and initialize the init-ops engine */
4333         rc = ecore_init_alloc(p_hwfn);
4334         if (rc) {
4335                 DP_NOTICE(p_hwfn, false, "Failed to allocate the init array\n");
4336                 if (p_params->b_relaxed_probe)
4337                         p_params->p_relaxed_res = ECORE_HW_PREPARE_FAILED_MEM;
4338                 goto err2;
4339         }
4340 #ifndef ASIC_ONLY
4341         if (CHIP_REV_IS_FPGA(p_dev)) {
4342                 DP_NOTICE(p_hwfn, false,
4343                           "FPGA: workaround; Prevent DMAE parities\n");
4344                 ecore_wr(p_hwfn, p_hwfn->p_main_ptt, PCIE_REG_PRTY_MASK_K2_E5,
4345                          7);
4346
4347                 DP_NOTICE(p_hwfn, false,
4348                           "FPGA: workaround: Set VF bar0 size\n");
4349                 ecore_wr(p_hwfn, p_hwfn->p_main_ptt,
4350                          PGLUE_B_REG_VF_BAR0_SIZE_K2_E5, 4);
4351         }
4352 #endif
4353
4354         return rc;
4355 err2:
4356         if (IS_LEAD_HWFN(p_hwfn))
4357                 ecore_iov_free_hw_info(p_dev);
4358         ecore_mcp_free(p_hwfn);
4359 err1:
4360         ecore_hw_hwfn_free(p_hwfn);
4361 err0:
4362         return rc;
4363 }
4364
4365 enum _ecore_status_t ecore_hw_prepare(struct ecore_dev *p_dev,
4366                                       struct ecore_hw_prepare_params *p_params)
4367 {
4368         struct ecore_hwfn *p_hwfn = ECORE_LEADING_HWFN(p_dev);
4369         enum _ecore_status_t rc;
4370
4371         p_dev->chk_reg_fifo = p_params->chk_reg_fifo;
4372         p_dev->allow_mdump = p_params->allow_mdump;
4373         p_hwfn->b_en_pacing = p_params->b_en_pacing;
4374         p_dev->b_is_target = p_params->b_is_target;
4375
4376         if (p_params->b_relaxed_probe)
4377                 p_params->p_relaxed_res = ECORE_HW_PREPARE_SUCCESS;
4378
4379         /* Store the precompiled init data ptrs */
4380         if (IS_PF(p_dev))
4381                 ecore_init_iro_array(p_dev);
4382
4383         /* Initialize the first hwfn - will learn number of hwfns */
4384         rc = ecore_hw_prepare_single(p_hwfn,
4385                                      p_dev->regview,
4386                                      p_dev->doorbells, p_params);
4387         if (rc != ECORE_SUCCESS)
4388                 return rc;
4389
4390         p_params->personality = p_hwfn->hw_info.personality;
4391
4392         /* initilalize 2nd hwfn if necessary */
4393         if (ECORE_IS_CMT(p_dev)) {
4394                 void OSAL_IOMEM *p_regview, *p_doorbell;
4395                 u8 OSAL_IOMEM *addr;
4396
4397                 /* adjust bar offset for second engine */
4398                 addr = (u8 OSAL_IOMEM *)p_dev->regview +
4399                                         ecore_hw_bar_size(p_hwfn,
4400                                                           p_hwfn->p_main_ptt,
4401                                                           BAR_ID_0) / 2;
4402                 p_regview = (void OSAL_IOMEM *)addr;
4403
4404                 addr = (u8 OSAL_IOMEM *)p_dev->doorbells +
4405                                         ecore_hw_bar_size(p_hwfn,
4406                                                           p_hwfn->p_main_ptt,
4407                                                           BAR_ID_1) / 2;
4408                 p_doorbell = (void OSAL_IOMEM *)addr;
4409
4410                 p_dev->hwfns[1].b_en_pacing = p_params->b_en_pacing;
4411                 /* prepare second hw function */
4412                 rc = ecore_hw_prepare_single(&p_dev->hwfns[1], p_regview,
4413                                              p_doorbell, p_params);
4414
4415                 /* in case of error, need to free the previously
4416                  * initiliazed hwfn 0.
4417                  */
4418                 if (rc != ECORE_SUCCESS) {
4419                         if (p_params->b_relaxed_probe)
4420                                 p_params->p_relaxed_res =
4421                                                 ECORE_HW_PREPARE_FAILED_ENG2;
4422
4423                         if (IS_PF(p_dev)) {
4424                                 ecore_init_free(p_hwfn);
4425                                 ecore_mcp_free(p_hwfn);
4426                                 ecore_hw_hwfn_free(p_hwfn);
4427                         } else {
4428                                 DP_NOTICE(p_dev, false, "What do we need to free when VF hwfn1 init fails\n");
4429                         }
4430                         return rc;
4431                 }
4432         }
4433
4434         return rc;
4435 }
4436
4437 void ecore_hw_remove(struct ecore_dev *p_dev)
4438 {
4439         struct ecore_hwfn *p_hwfn = ECORE_LEADING_HWFN(p_dev);
4440         int i;
4441
4442         if (IS_PF(p_dev))
4443                 ecore_mcp_ov_update_driver_state(p_hwfn, p_hwfn->p_main_ptt,
4444                                         ECORE_OV_DRIVER_STATE_NOT_LOADED);
4445
4446         for_each_hwfn(p_dev, i) {
4447                 struct ecore_hwfn *p_hwfn = &p_dev->hwfns[i];
4448
4449                 if (IS_VF(p_dev)) {
4450                         ecore_vf_pf_release(p_hwfn);
4451                         continue;
4452                 }
4453
4454                 ecore_init_free(p_hwfn);
4455                 ecore_hw_hwfn_free(p_hwfn);
4456                 ecore_mcp_free(p_hwfn);
4457
4458 #ifdef CONFIG_ECORE_LOCK_ALLOC
4459                 OSAL_SPIN_LOCK_DEALLOC(&p_hwfn->dmae_info.lock);
4460 #endif
4461         }
4462
4463         ecore_iov_free_hw_info(p_dev);
4464 }
4465
4466 static void ecore_chain_free_next_ptr(struct ecore_dev *p_dev,
4467                                       struct ecore_chain *p_chain)
4468 {
4469         void *p_virt = p_chain->p_virt_addr, *p_virt_next = OSAL_NULL;
4470         dma_addr_t p_phys = p_chain->p_phys_addr, p_phys_next = 0;
4471         struct ecore_chain_next *p_next;
4472         u32 size, i;
4473
4474         if (!p_virt)
4475                 return;
4476
4477         size = p_chain->elem_size * p_chain->usable_per_page;
4478
4479         for (i = 0; i < p_chain->page_cnt; i++) {
4480                 if (!p_virt)
4481                         break;
4482
4483                 p_next = (struct ecore_chain_next *)((u8 *)p_virt + size);
4484                 p_virt_next = p_next->next_virt;
4485                 p_phys_next = HILO_DMA_REGPAIR(p_next->next_phys);
4486
4487                 OSAL_DMA_FREE_COHERENT(p_dev, p_virt, p_phys,
4488                                        ECORE_CHAIN_PAGE_SIZE);
4489
4490                 p_virt = p_virt_next;
4491                 p_phys = p_phys_next;
4492         }
4493 }
4494
4495 static void ecore_chain_free_single(struct ecore_dev *p_dev,
4496                                     struct ecore_chain *p_chain)
4497 {
4498         if (!p_chain->p_virt_addr)
4499                 return;
4500
4501         OSAL_DMA_FREE_COHERENT(p_dev, p_chain->p_virt_addr,
4502                                p_chain->p_phys_addr, ECORE_CHAIN_PAGE_SIZE);
4503 }
4504
4505 static void ecore_chain_free_pbl(struct ecore_dev *p_dev,
4506                                  struct ecore_chain *p_chain)
4507 {
4508         void **pp_virt_addr_tbl = p_chain->pbl.pp_virt_addr_tbl;
4509         u8 *p_pbl_virt = (u8 *)p_chain->pbl_sp.p_virt_table;
4510         u32 page_cnt = p_chain->page_cnt, i, pbl_size;
4511
4512         if (!pp_virt_addr_tbl)
4513                 return;
4514
4515         if (!p_pbl_virt)
4516                 goto out;
4517
4518         for (i = 0; i < page_cnt; i++) {
4519                 if (!pp_virt_addr_tbl[i])
4520                         break;
4521
4522                 OSAL_DMA_FREE_COHERENT(p_dev, pp_virt_addr_tbl[i],
4523                                        *(dma_addr_t *)p_pbl_virt,
4524                                        ECORE_CHAIN_PAGE_SIZE);
4525
4526                 p_pbl_virt += ECORE_CHAIN_PBL_ENTRY_SIZE;
4527         }
4528
4529         pbl_size = page_cnt * ECORE_CHAIN_PBL_ENTRY_SIZE;
4530
4531         if (!p_chain->b_external_pbl)
4532                 OSAL_DMA_FREE_COHERENT(p_dev, p_chain->pbl_sp.p_virt_table,
4533                                        p_chain->pbl_sp.p_phys_table, pbl_size);
4534 out:
4535         OSAL_VFREE(p_dev, p_chain->pbl.pp_virt_addr_tbl);
4536 }
4537
4538 void ecore_chain_free(struct ecore_dev *p_dev, struct ecore_chain *p_chain)
4539 {
4540         switch (p_chain->mode) {
4541         case ECORE_CHAIN_MODE_NEXT_PTR:
4542                 ecore_chain_free_next_ptr(p_dev, p_chain);
4543                 break;
4544         case ECORE_CHAIN_MODE_SINGLE:
4545                 ecore_chain_free_single(p_dev, p_chain);
4546                 break;
4547         case ECORE_CHAIN_MODE_PBL:
4548                 ecore_chain_free_pbl(p_dev, p_chain);
4549                 break;
4550         }
4551 }
4552
4553 static enum _ecore_status_t
4554 ecore_chain_alloc_sanity_check(struct ecore_dev *p_dev,
4555                                enum ecore_chain_cnt_type cnt_type,
4556                                osal_size_t elem_size, u32 page_cnt)
4557 {
4558         u64 chain_size = ELEMS_PER_PAGE(elem_size) * page_cnt;
4559
4560         /* The actual chain size can be larger than the maximal possible value
4561          * after rounding up the requested elements number to pages, and after
4562          * taking into acount the unusuable elements (next-ptr elements).
4563          * The size of a "u16" chain can be (U16_MAX + 1) since the chain
4564          * size/capacity fields are of a u32 type.
4565          */
4566         if ((cnt_type == ECORE_CHAIN_CNT_TYPE_U16 &&
4567              chain_size > ((u32)ECORE_U16_MAX + 1)) ||
4568             (cnt_type == ECORE_CHAIN_CNT_TYPE_U32 &&
4569              chain_size > ECORE_U32_MAX)) {
4570                 DP_NOTICE(p_dev, true,
4571                           "The actual chain size (0x%lx) is larger than the maximal possible value\n",
4572                           (unsigned long)chain_size);
4573                 return ECORE_INVAL;
4574         }
4575
4576         return ECORE_SUCCESS;
4577 }
4578
4579 static enum _ecore_status_t
4580 ecore_chain_alloc_next_ptr(struct ecore_dev *p_dev, struct ecore_chain *p_chain)
4581 {
4582         void *p_virt = OSAL_NULL, *p_virt_prev = OSAL_NULL;
4583         dma_addr_t p_phys = 0;
4584         u32 i;
4585
4586         for (i = 0; i < p_chain->page_cnt; i++) {
4587                 p_virt = OSAL_DMA_ALLOC_COHERENT(p_dev, &p_phys,
4588                                                  ECORE_CHAIN_PAGE_SIZE);
4589                 if (!p_virt) {
4590                         DP_NOTICE(p_dev, false,
4591                                   "Failed to allocate chain memory\n");
4592                         return ECORE_NOMEM;
4593                 }
4594
4595                 if (i == 0) {
4596                         ecore_chain_init_mem(p_chain, p_virt, p_phys);
4597                         ecore_chain_reset(p_chain);
4598                 } else {
4599                         ecore_chain_init_next_ptr_elem(p_chain, p_virt_prev,
4600                                                        p_virt, p_phys);
4601                 }
4602
4603                 p_virt_prev = p_virt;
4604         }
4605         /* Last page's next element should point to the beginning of the
4606          * chain.
4607          */
4608         ecore_chain_init_next_ptr_elem(p_chain, p_virt_prev,
4609                                        p_chain->p_virt_addr,
4610                                        p_chain->p_phys_addr);
4611
4612         return ECORE_SUCCESS;
4613 }
4614
4615 static enum _ecore_status_t
4616 ecore_chain_alloc_single(struct ecore_dev *p_dev, struct ecore_chain *p_chain)
4617 {
4618         dma_addr_t p_phys = 0;
4619         void *p_virt = OSAL_NULL;
4620
4621         p_virt = OSAL_DMA_ALLOC_COHERENT(p_dev, &p_phys, ECORE_CHAIN_PAGE_SIZE);
4622         if (!p_virt) {
4623                 DP_NOTICE(p_dev, false, "Failed to allocate chain memory\n");
4624                 return ECORE_NOMEM;
4625         }
4626
4627         ecore_chain_init_mem(p_chain, p_virt, p_phys);
4628         ecore_chain_reset(p_chain);
4629
4630         return ECORE_SUCCESS;
4631 }
4632
4633 static enum _ecore_status_t
4634 ecore_chain_alloc_pbl(struct ecore_dev *p_dev,
4635                       struct ecore_chain *p_chain,
4636                       struct ecore_chain_ext_pbl *ext_pbl)
4637 {
4638         u32 page_cnt = p_chain->page_cnt, size, i;
4639         dma_addr_t p_phys = 0, p_pbl_phys = 0;
4640         void **pp_virt_addr_tbl = OSAL_NULL;
4641         u8 *p_pbl_virt = OSAL_NULL;
4642         void *p_virt = OSAL_NULL;
4643
4644         size = page_cnt * sizeof(*pp_virt_addr_tbl);
4645         pp_virt_addr_tbl = (void **)OSAL_VZALLOC(p_dev, size);
4646         if (!pp_virt_addr_tbl) {
4647                 DP_NOTICE(p_dev, false,
4648                           "Failed to allocate memory for the chain virtual addresses table\n");
4649                 return ECORE_NOMEM;
4650         }
4651
4652         /* The allocation of the PBL table is done with its full size, since it
4653          * is expected to be successive.
4654          * ecore_chain_init_pbl_mem() is called even in a case of an allocation
4655          * failure, since pp_virt_addr_tbl was previously allocated, and it
4656          * should be saved to allow its freeing during the error flow.
4657          */
4658         size = page_cnt * ECORE_CHAIN_PBL_ENTRY_SIZE;
4659
4660         if (ext_pbl == OSAL_NULL) {
4661                 p_pbl_virt = OSAL_DMA_ALLOC_COHERENT(p_dev, &p_pbl_phys, size);
4662         } else {
4663                 p_pbl_virt = ext_pbl->p_pbl_virt;
4664                 p_pbl_phys = ext_pbl->p_pbl_phys;
4665                 p_chain->b_external_pbl = true;
4666         }
4667
4668         ecore_chain_init_pbl_mem(p_chain, p_pbl_virt, p_pbl_phys,
4669                                  pp_virt_addr_tbl);
4670         if (!p_pbl_virt) {
4671                 DP_NOTICE(p_dev, false, "Failed to allocate chain pbl memory\n");
4672                 return ECORE_NOMEM;
4673         }
4674
4675         for (i = 0; i < page_cnt; i++) {
4676                 p_virt = OSAL_DMA_ALLOC_COHERENT(p_dev, &p_phys,
4677                                                  ECORE_CHAIN_PAGE_SIZE);
4678                 if (!p_virt) {
4679                         DP_NOTICE(p_dev, false,
4680                                   "Failed to allocate chain memory\n");
4681                         return ECORE_NOMEM;
4682                 }
4683
4684                 if (i == 0) {
4685                         ecore_chain_init_mem(p_chain, p_virt, p_phys);
4686                         ecore_chain_reset(p_chain);
4687                 }
4688
4689                 /* Fill the PBL table with the physical address of the page */
4690                 *(dma_addr_t *)p_pbl_virt = p_phys;
4691                 /* Keep the virtual address of the page */
4692                 p_chain->pbl.pp_virt_addr_tbl[i] = p_virt;
4693
4694                 p_pbl_virt += ECORE_CHAIN_PBL_ENTRY_SIZE;
4695         }
4696
4697         return ECORE_SUCCESS;
4698 }
4699
4700 enum _ecore_status_t ecore_chain_alloc(struct ecore_dev *p_dev,
4701                                        enum ecore_chain_use_mode intended_use,
4702                                        enum ecore_chain_mode mode,
4703                                        enum ecore_chain_cnt_type cnt_type,
4704                                        u32 num_elems, osal_size_t elem_size,
4705                                        struct ecore_chain *p_chain,
4706                                        struct ecore_chain_ext_pbl *ext_pbl)
4707 {
4708         u32 page_cnt;
4709         enum _ecore_status_t rc = ECORE_SUCCESS;
4710
4711         if (mode == ECORE_CHAIN_MODE_SINGLE)
4712                 page_cnt = 1;
4713         else
4714                 page_cnt = ECORE_CHAIN_PAGE_CNT(num_elems, elem_size, mode);
4715
4716         rc = ecore_chain_alloc_sanity_check(p_dev, cnt_type, elem_size,
4717                                             page_cnt);
4718         if (rc) {
4719                 DP_NOTICE(p_dev, false,
4720                           "Cannot allocate a chain with the given arguments:\n"
4721                           "[use_mode %d, mode %d, cnt_type %d, num_elems %d, elem_size %zu]\n",
4722                           intended_use, mode, cnt_type, num_elems, elem_size);
4723                 return rc;
4724         }
4725
4726         ecore_chain_init_params(p_chain, page_cnt, (u8)elem_size, intended_use,
4727                                 mode, cnt_type, p_dev->dp_ctx);
4728
4729         switch (mode) {
4730         case ECORE_CHAIN_MODE_NEXT_PTR:
4731                 rc = ecore_chain_alloc_next_ptr(p_dev, p_chain);
4732                 break;
4733         case ECORE_CHAIN_MODE_SINGLE:
4734                 rc = ecore_chain_alloc_single(p_dev, p_chain);
4735                 break;
4736         case ECORE_CHAIN_MODE_PBL:
4737                 rc = ecore_chain_alloc_pbl(p_dev, p_chain, ext_pbl);
4738                 break;
4739         }
4740         if (rc)
4741                 goto nomem;
4742
4743         return ECORE_SUCCESS;
4744
4745 nomem:
4746         ecore_chain_free(p_dev, p_chain);
4747         return rc;
4748 }
4749
4750 enum _ecore_status_t ecore_fw_l2_queue(struct ecore_hwfn *p_hwfn,
4751                                        u16 src_id, u16 *dst_id)
4752 {
4753         if (src_id >= RESC_NUM(p_hwfn, ECORE_L2_QUEUE)) {
4754                 u16 min, max;
4755
4756                 min = (u16)RESC_START(p_hwfn, ECORE_L2_QUEUE);
4757                 max = min + RESC_NUM(p_hwfn, ECORE_L2_QUEUE);
4758                 DP_NOTICE(p_hwfn, true,
4759                           "l2_queue id [%d] is not valid, available indices [%d - %d]\n",
4760                           src_id, min, max);
4761
4762                 return ECORE_INVAL;
4763         }
4764
4765         *dst_id = RESC_START(p_hwfn, ECORE_L2_QUEUE) + src_id;
4766
4767         return ECORE_SUCCESS;
4768 }
4769
4770 enum _ecore_status_t ecore_fw_vport(struct ecore_hwfn *p_hwfn,
4771                                     u8 src_id, u8 *dst_id)
4772 {
4773         if (src_id >= RESC_NUM(p_hwfn, ECORE_VPORT)) {
4774                 u8 min, max;
4775
4776                 min = (u8)RESC_START(p_hwfn, ECORE_VPORT);
4777                 max = min + RESC_NUM(p_hwfn, ECORE_VPORT);
4778                 DP_NOTICE(p_hwfn, true,
4779                           "vport id [%d] is not valid, available indices [%d - %d]\n",
4780                           src_id, min, max);
4781
4782                 return ECORE_INVAL;
4783         }
4784
4785         *dst_id = RESC_START(p_hwfn, ECORE_VPORT) + src_id;
4786
4787         return ECORE_SUCCESS;
4788 }
4789
4790 enum _ecore_status_t ecore_fw_rss_eng(struct ecore_hwfn *p_hwfn,
4791                                       u8 src_id, u8 *dst_id)
4792 {
4793         if (src_id >= RESC_NUM(p_hwfn, ECORE_RSS_ENG)) {
4794                 u8 min, max;
4795
4796                 min = (u8)RESC_START(p_hwfn, ECORE_RSS_ENG);
4797                 max = min + RESC_NUM(p_hwfn, ECORE_RSS_ENG);
4798                 DP_NOTICE(p_hwfn, true,
4799                           "rss_eng id [%d] is not valid, available indices [%d - %d]\n",
4800                           src_id, min, max);
4801
4802                 return ECORE_INVAL;
4803         }
4804
4805         *dst_id = RESC_START(p_hwfn, ECORE_RSS_ENG) + src_id;
4806
4807         return ECORE_SUCCESS;
4808 }
4809
4810 static enum _ecore_status_t
4811 ecore_llh_add_mac_filter_bb_ah(struct ecore_hwfn *p_hwfn,
4812                                struct ecore_ptt *p_ptt, u32 high, u32 low,
4813                                u32 *p_entry_num)
4814 {
4815         u32 en;
4816         int i;
4817
4818         /* Find a free entry and utilize it */
4819         for (i = 0; i < NIG_REG_LLH_FUNC_FILTER_EN_SIZE; i++) {
4820                 en = ecore_rd(p_hwfn, p_ptt,
4821                               NIG_REG_LLH_FUNC_FILTER_EN_BB_K2 +
4822                               i * sizeof(u32));
4823                 if (en)
4824                         continue;
4825                 ecore_wr(p_hwfn, p_ptt,
4826                          NIG_REG_LLH_FUNC_FILTER_VALUE_BB_K2 +
4827                          2 * i * sizeof(u32), low);
4828                 ecore_wr(p_hwfn, p_ptt,
4829                          NIG_REG_LLH_FUNC_FILTER_VALUE_BB_K2 +
4830                          (2 * i + 1) * sizeof(u32), high);
4831                 ecore_wr(p_hwfn, p_ptt,
4832                          NIG_REG_LLH_FUNC_FILTER_MODE_BB_K2 +
4833                          i * sizeof(u32), 0);
4834                 ecore_wr(p_hwfn, p_ptt,
4835                          NIG_REG_LLH_FUNC_FILTER_PROTOCOL_TYPE_BB_K2 +
4836                          i * sizeof(u32), 0);
4837                 ecore_wr(p_hwfn, p_ptt,
4838                          NIG_REG_LLH_FUNC_FILTER_EN_BB_K2 +
4839                          i * sizeof(u32), 1);
4840                 break;
4841         }
4842
4843         if (i >= NIG_REG_LLH_FUNC_FILTER_EN_SIZE)
4844                 return ECORE_NORESOURCES;
4845
4846         *p_entry_num = i;
4847
4848         return ECORE_SUCCESS;
4849 }
4850
4851 enum _ecore_status_t ecore_llh_add_mac_filter(struct ecore_hwfn *p_hwfn,
4852                                           struct ecore_ptt *p_ptt, u8 *p_filter)
4853 {
4854         u32 high, low, entry_num;
4855         enum _ecore_status_t rc = ECORE_SUCCESS;
4856
4857         if (!OSAL_TEST_BIT(ECORE_MF_LLH_MAC_CLSS,
4858                            &p_hwfn->p_dev->mf_bits))
4859                 return ECORE_SUCCESS;
4860
4861         high = p_filter[1] | (p_filter[0] << 8);
4862         low = p_filter[5] | (p_filter[4] << 8) |
4863               (p_filter[3] << 16) | (p_filter[2] << 24);
4864
4865         if (ECORE_IS_BB(p_hwfn->p_dev) || ECORE_IS_AH(p_hwfn->p_dev))
4866                 rc = ecore_llh_add_mac_filter_bb_ah(p_hwfn, p_ptt, high, low,
4867                                                     &entry_num);
4868         if (rc != ECORE_SUCCESS) {
4869                 DP_NOTICE(p_hwfn, false,
4870                           "Failed to find an empty LLH filter to utilize\n");
4871                 return rc;
4872         }
4873
4874         DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
4875                    "MAC: %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx is added at %d\n",
4876                    p_filter[0], p_filter[1], p_filter[2], p_filter[3],
4877                    p_filter[4], p_filter[5], entry_num);
4878
4879         return rc;
4880 }
4881
4882 static enum _ecore_status_t
4883 ecore_llh_remove_mac_filter_bb_ah(struct ecore_hwfn *p_hwfn,
4884                                   struct ecore_ptt *p_ptt, u32 high, u32 low,
4885                                   u32 *p_entry_num)
4886 {
4887         int i;
4888
4889         /* Find the entry and clean it */
4890         for (i = 0; i < NIG_REG_LLH_FUNC_FILTER_EN_SIZE; i++) {
4891                 if (ecore_rd(p_hwfn, p_ptt,
4892                              NIG_REG_LLH_FUNC_FILTER_VALUE_BB_K2 +
4893                              2 * i * sizeof(u32)) != low)
4894                         continue;
4895                 if (ecore_rd(p_hwfn, p_ptt,
4896                              NIG_REG_LLH_FUNC_FILTER_VALUE_BB_K2 +
4897                              (2 * i + 1) * sizeof(u32)) != high)
4898                         continue;
4899
4900                 ecore_wr(p_hwfn, p_ptt,
4901                          NIG_REG_LLH_FUNC_FILTER_EN_BB_K2 + i * sizeof(u32), 0);
4902                 ecore_wr(p_hwfn, p_ptt,
4903                          NIG_REG_LLH_FUNC_FILTER_VALUE_BB_K2 +
4904                          2 * i * sizeof(u32), 0);
4905                 ecore_wr(p_hwfn, p_ptt,
4906                          NIG_REG_LLH_FUNC_FILTER_VALUE_BB_K2 +
4907                          (2 * i + 1) * sizeof(u32), 0);
4908                 break;
4909         }
4910
4911         if (i >= NIG_REG_LLH_FUNC_FILTER_EN_SIZE)
4912                 return ECORE_INVAL;
4913
4914         *p_entry_num = i;
4915
4916         return ECORE_SUCCESS;
4917 }
4918
4919 void ecore_llh_remove_mac_filter(struct ecore_hwfn *p_hwfn,
4920                              struct ecore_ptt *p_ptt, u8 *p_filter)
4921 {
4922         u32 high, low, entry_num;
4923         enum _ecore_status_t rc = ECORE_SUCCESS;
4924
4925         if (!OSAL_TEST_BIT(ECORE_MF_LLH_MAC_CLSS,
4926                            &p_hwfn->p_dev->mf_bits))
4927                 return;
4928
4929         high = p_filter[1] | (p_filter[0] << 8);
4930         low = p_filter[5] | (p_filter[4] << 8) |
4931               (p_filter[3] << 16) | (p_filter[2] << 24);
4932
4933         if (ECORE_IS_BB(p_hwfn->p_dev) || ECORE_IS_AH(p_hwfn->p_dev))
4934                 rc = ecore_llh_remove_mac_filter_bb_ah(p_hwfn, p_ptt, high,
4935                                                        low, &entry_num);
4936         if (rc != ECORE_SUCCESS) {
4937                 DP_NOTICE(p_hwfn, false,
4938                           "Tried to remove a non-configured filter\n");
4939                 return;
4940         }
4941
4942
4943         DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
4944                    "MAC: %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx was removed from %d\n",
4945                    p_filter[0], p_filter[1], p_filter[2], p_filter[3],
4946                    p_filter[4], p_filter[5], entry_num);
4947 }
4948
4949 static enum _ecore_status_t
4950 ecore_llh_add_protocol_filter_bb_ah(struct ecore_hwfn *p_hwfn,
4951                                     struct ecore_ptt *p_ptt,
4952                                     enum ecore_llh_port_filter_type_t type,
4953                                     u32 high, u32 low, u32 *p_entry_num)
4954 {
4955         u32 en;
4956         int i;
4957
4958         /* Find a free entry and utilize it */
4959         for (i = 0; i < NIG_REG_LLH_FUNC_FILTER_EN_SIZE; i++) {
4960                 en = ecore_rd(p_hwfn, p_ptt,
4961                               NIG_REG_LLH_FUNC_FILTER_EN_BB_K2 +
4962                               i * sizeof(u32));
4963                 if (en)
4964                         continue;
4965                 ecore_wr(p_hwfn, p_ptt,
4966                          NIG_REG_LLH_FUNC_FILTER_VALUE_BB_K2 +
4967                          2 * i * sizeof(u32), low);
4968                 ecore_wr(p_hwfn, p_ptt,
4969                          NIG_REG_LLH_FUNC_FILTER_VALUE_BB_K2 +
4970                          (2 * i + 1) * sizeof(u32), high);
4971                 ecore_wr(p_hwfn, p_ptt,
4972                          NIG_REG_LLH_FUNC_FILTER_MODE_BB_K2 +
4973                          i * sizeof(u32), 1);
4974                 ecore_wr(p_hwfn, p_ptt,
4975                          NIG_REG_LLH_FUNC_FILTER_PROTOCOL_TYPE_BB_K2 +
4976                          i * sizeof(u32), 1 << type);
4977                 ecore_wr(p_hwfn, p_ptt,
4978                          NIG_REG_LLH_FUNC_FILTER_EN_BB_K2 + i * sizeof(u32), 1);
4979                 break;
4980         }
4981
4982         if (i >= NIG_REG_LLH_FUNC_FILTER_EN_SIZE)
4983                 return ECORE_NORESOURCES;
4984
4985         *p_entry_num = i;
4986
4987         return ECORE_SUCCESS;
4988 }
4989
4990 enum _ecore_status_t
4991 ecore_llh_add_protocol_filter(struct ecore_hwfn *p_hwfn,
4992                               struct ecore_ptt *p_ptt,
4993                               u16 source_port_or_eth_type,
4994                               u16 dest_port,
4995                               enum ecore_llh_port_filter_type_t type)
4996 {
4997         u32 high, low, entry_num;
4998         enum _ecore_status_t rc = ECORE_SUCCESS;
4999
5000         if (!OSAL_TEST_BIT(ECORE_MF_LLH_PROTO_CLSS,
5001                            &p_hwfn->p_dev->mf_bits))
5002                 return rc;
5003
5004         high = 0;
5005         low = 0;
5006
5007         switch (type) {
5008         case ECORE_LLH_FILTER_ETHERTYPE:
5009                 high = source_port_or_eth_type;
5010                 break;
5011         case ECORE_LLH_FILTER_TCP_SRC_PORT:
5012         case ECORE_LLH_FILTER_UDP_SRC_PORT:
5013                 low = source_port_or_eth_type << 16;
5014                 break;
5015         case ECORE_LLH_FILTER_TCP_DEST_PORT:
5016         case ECORE_LLH_FILTER_UDP_DEST_PORT:
5017                 low = dest_port;
5018                 break;
5019         case ECORE_LLH_FILTER_TCP_SRC_AND_DEST_PORT:
5020         case ECORE_LLH_FILTER_UDP_SRC_AND_DEST_PORT:
5021                 low = (source_port_or_eth_type << 16) | dest_port;
5022                 break;
5023         default:
5024                 DP_NOTICE(p_hwfn, true,
5025                           "Non valid LLH protocol filter type %d\n", type);
5026                 return ECORE_INVAL;
5027         }
5028
5029         if (ECORE_IS_BB(p_hwfn->p_dev) || ECORE_IS_AH(p_hwfn->p_dev))
5030                 rc = ecore_llh_add_protocol_filter_bb_ah(p_hwfn, p_ptt, type,
5031                                                          high, low, &entry_num);
5032         if (rc != ECORE_SUCCESS) {
5033                 DP_NOTICE(p_hwfn, false,
5034                           "Failed to find an empty LLH filter to utilize\n");
5035                 return rc;
5036         }
5037         switch (type) {
5038         case ECORE_LLH_FILTER_ETHERTYPE:
5039                 DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
5040                            "ETH type %x is added at %d\n",
5041                            source_port_or_eth_type, entry_num);
5042                 break;
5043         case ECORE_LLH_FILTER_TCP_SRC_PORT:
5044                 DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
5045                            "TCP src port %x is added at %d\n",
5046                            source_port_or_eth_type, entry_num);
5047                 break;
5048         case ECORE_LLH_FILTER_UDP_SRC_PORT:
5049                 DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
5050                            "UDP src port %x is added at %d\n",
5051                            source_port_or_eth_type, entry_num);
5052                 break;
5053         case ECORE_LLH_FILTER_TCP_DEST_PORT:
5054                 DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
5055                            "TCP dst port %x is added at %d\n", dest_port,
5056                            entry_num);
5057                 break;
5058         case ECORE_LLH_FILTER_UDP_DEST_PORT:
5059                 DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
5060                            "UDP dst port %x is added at %d\n", dest_port,
5061                            entry_num);
5062                 break;
5063         case ECORE_LLH_FILTER_TCP_SRC_AND_DEST_PORT:
5064                 DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
5065                            "TCP src/dst ports %x/%x are added at %d\n",
5066                            source_port_or_eth_type, dest_port, entry_num);
5067                 break;
5068         case ECORE_LLH_FILTER_UDP_SRC_AND_DEST_PORT:
5069                 DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
5070                            "UDP src/dst ports %x/%x are added at %d\n",
5071                            source_port_or_eth_type, dest_port, entry_num);
5072                 break;
5073         }
5074
5075         return rc;
5076 }
5077
5078 static enum _ecore_status_t
5079 ecore_llh_remove_protocol_filter_bb_ah(struct ecore_hwfn *p_hwfn,
5080                                        struct ecore_ptt *p_ptt,
5081                                        enum ecore_llh_port_filter_type_t type,
5082                                        u32 high, u32 low, u32 *p_entry_num)
5083 {
5084         int i;
5085
5086         /* Find the entry and clean it */
5087         for (i = 0; i < NIG_REG_LLH_FUNC_FILTER_EN_SIZE; i++) {
5088                 if (!ecore_rd(p_hwfn, p_ptt,
5089                               NIG_REG_LLH_FUNC_FILTER_EN_BB_K2 +
5090                               i * sizeof(u32)))
5091                         continue;
5092                 if (!ecore_rd(p_hwfn, p_ptt,
5093                               NIG_REG_LLH_FUNC_FILTER_MODE_BB_K2 +
5094                               i * sizeof(u32)))
5095                         continue;
5096                 if (!(ecore_rd(p_hwfn, p_ptt,
5097                                NIG_REG_LLH_FUNC_FILTER_PROTOCOL_TYPE_BB_K2 +
5098                                i * sizeof(u32)) & (1 << type)))
5099                         continue;
5100                 if (ecore_rd(p_hwfn, p_ptt,
5101                              NIG_REG_LLH_FUNC_FILTER_VALUE_BB_K2 +
5102                              2 * i * sizeof(u32)) != low)
5103                         continue;
5104                 if (ecore_rd(p_hwfn, p_ptt,
5105                              NIG_REG_LLH_FUNC_FILTER_VALUE_BB_K2 +
5106                              (2 * i + 1) * sizeof(u32)) != high)
5107                         continue;
5108
5109                 ecore_wr(p_hwfn, p_ptt,
5110                          NIG_REG_LLH_FUNC_FILTER_EN_BB_K2 + i * sizeof(u32), 0);
5111                 ecore_wr(p_hwfn, p_ptt,
5112                          NIG_REG_LLH_FUNC_FILTER_MODE_BB_K2 +
5113                          i * sizeof(u32), 0);
5114                 ecore_wr(p_hwfn, p_ptt,
5115                          NIG_REG_LLH_FUNC_FILTER_PROTOCOL_TYPE_BB_K2 +
5116                          i * sizeof(u32), 0);
5117                 ecore_wr(p_hwfn, p_ptt,
5118                          NIG_REG_LLH_FUNC_FILTER_VALUE_BB_K2 +
5119                          2 * i * sizeof(u32), 0);
5120                 ecore_wr(p_hwfn, p_ptt,
5121                          NIG_REG_LLH_FUNC_FILTER_VALUE_BB_K2 +
5122                          (2 * i + 1) * sizeof(u32), 0);
5123                 break;
5124         }
5125
5126         if (i >= NIG_REG_LLH_FUNC_FILTER_EN_SIZE)
5127                 return ECORE_INVAL;
5128
5129         *p_entry_num = i;
5130
5131         return ECORE_SUCCESS;
5132 }
5133
5134 void
5135 ecore_llh_remove_protocol_filter(struct ecore_hwfn *p_hwfn,
5136                                  struct ecore_ptt *p_ptt,
5137                                  u16 source_port_or_eth_type,
5138                                  u16 dest_port,
5139                                  enum ecore_llh_port_filter_type_t type)
5140 {
5141         u32 high, low, entry_num;
5142         enum _ecore_status_t rc = ECORE_SUCCESS;
5143
5144         if (!OSAL_TEST_BIT(ECORE_MF_LLH_PROTO_CLSS,
5145                            &p_hwfn->p_dev->mf_bits))
5146                 return;
5147
5148         high = 0;
5149         low = 0;
5150
5151         switch (type) {
5152         case ECORE_LLH_FILTER_ETHERTYPE:
5153                 high = source_port_or_eth_type;
5154                 break;
5155         case ECORE_LLH_FILTER_TCP_SRC_PORT:
5156         case ECORE_LLH_FILTER_UDP_SRC_PORT:
5157                 low = source_port_or_eth_type << 16;
5158                 break;
5159         case ECORE_LLH_FILTER_TCP_DEST_PORT:
5160         case ECORE_LLH_FILTER_UDP_DEST_PORT:
5161                 low = dest_port;
5162                 break;
5163         case ECORE_LLH_FILTER_TCP_SRC_AND_DEST_PORT:
5164         case ECORE_LLH_FILTER_UDP_SRC_AND_DEST_PORT:
5165                 low = (source_port_or_eth_type << 16) | dest_port;
5166                 break;
5167         default:
5168                 DP_NOTICE(p_hwfn, true,
5169                           "Non valid LLH protocol filter type %d\n", type);
5170                 return;
5171         }
5172
5173         if (ECORE_IS_BB(p_hwfn->p_dev) || ECORE_IS_AH(p_hwfn->p_dev))
5174                 rc = ecore_llh_remove_protocol_filter_bb_ah(p_hwfn, p_ptt, type,
5175                                                             high, low,
5176                                                             &entry_num);
5177         if (rc != ECORE_SUCCESS) {
5178                 DP_NOTICE(p_hwfn, false,
5179                           "Tried to remove a non-configured filter [type %d, source_port_or_eth_type 0x%x, dest_port 0x%x]\n",
5180                           type, source_port_or_eth_type, dest_port);
5181                 return;
5182         }
5183
5184         DP_VERBOSE(p_hwfn, ECORE_MSG_HW,
5185                    "Protocol filter [type %d, source_port_or_eth_type 0x%x, dest_port 0x%x] was removed from %d\n",
5186                    type, source_port_or_eth_type, dest_port, entry_num);
5187 }
5188
5189 static void ecore_llh_clear_all_filters_bb_ah(struct ecore_hwfn *p_hwfn,
5190                                               struct ecore_ptt *p_ptt)
5191 {
5192         int i;
5193
5194         if (!(IS_MF_SI(p_hwfn) || IS_MF_DEFAULT(p_hwfn)))
5195                 return;
5196
5197         for (i = 0; i < NIG_REG_LLH_FUNC_FILTER_EN_SIZE; i++) {
5198                 ecore_wr(p_hwfn, p_ptt,
5199                          NIG_REG_LLH_FUNC_FILTER_EN_BB_K2  +
5200                          i * sizeof(u32), 0);
5201                 ecore_wr(p_hwfn, p_ptt,
5202                          NIG_REG_LLH_FUNC_FILTER_VALUE_BB_K2 +
5203                          2 * i * sizeof(u32), 0);
5204                 ecore_wr(p_hwfn, p_ptt,
5205                          NIG_REG_LLH_FUNC_FILTER_VALUE_BB_K2 +
5206                          (2 * i + 1) * sizeof(u32), 0);
5207         }
5208 }
5209
5210 void ecore_llh_clear_all_filters(struct ecore_hwfn *p_hwfn,
5211                              struct ecore_ptt *p_ptt)
5212 {
5213         if (!OSAL_TEST_BIT(ECORE_MF_LLH_PROTO_CLSS,
5214                            &p_hwfn->p_dev->mf_bits) &&
5215             !OSAL_TEST_BIT(ECORE_MF_LLH_MAC_CLSS,
5216                            &p_hwfn->p_dev->mf_bits))
5217                 return;
5218
5219         if (ECORE_IS_BB(p_hwfn->p_dev) || ECORE_IS_AH(p_hwfn->p_dev))
5220                 ecore_llh_clear_all_filters_bb_ah(p_hwfn, p_ptt);
5221 }
5222
5223 enum _ecore_status_t
5224 ecore_llh_set_function_as_default(struct ecore_hwfn *p_hwfn,
5225                                   struct ecore_ptt *p_ptt)
5226 {
5227         if (OSAL_TEST_BIT(ECORE_MF_NEED_DEF_PF, &p_hwfn->p_dev->mf_bits)) {
5228                 ecore_wr(p_hwfn, p_ptt,
5229                          NIG_REG_LLH_TAGMAC_DEF_PF_VECTOR,
5230                          1 << p_hwfn->abs_pf_id / 2);
5231                 ecore_wr(p_hwfn, p_ptt, PRS_REG_MSG_INFO, 0);
5232                 return ECORE_SUCCESS;
5233         }
5234
5235         DP_NOTICE(p_hwfn, false,
5236                   "This function can't be set as default\n");
5237         return ECORE_INVAL;
5238 }
5239
5240 static enum _ecore_status_t ecore_set_coalesce(struct ecore_hwfn *p_hwfn,
5241                                                struct ecore_ptt *p_ptt,
5242                                                u32 hw_addr, void *p_eth_qzone,
5243                                                osal_size_t eth_qzone_size,
5244                                                u8 timeset)
5245 {
5246         struct coalescing_timeset *p_coal_timeset;
5247
5248         if (p_hwfn->p_dev->int_coalescing_mode != ECORE_COAL_MODE_ENABLE) {
5249                 DP_NOTICE(p_hwfn, true,
5250                           "Coalescing configuration not enabled\n");
5251                 return ECORE_INVAL;
5252         }
5253
5254         p_coal_timeset = p_eth_qzone;
5255         OSAL_MEMSET(p_eth_qzone, 0, eth_qzone_size);
5256         SET_FIELD(p_coal_timeset->value, COALESCING_TIMESET_TIMESET, timeset);
5257         SET_FIELD(p_coal_timeset->value, COALESCING_TIMESET_VALID, 1);
5258         ecore_memcpy_to(p_hwfn, p_ptt, hw_addr, p_eth_qzone, eth_qzone_size);
5259
5260         return ECORE_SUCCESS;
5261 }
5262
5263 enum _ecore_status_t ecore_set_queue_coalesce(struct ecore_hwfn *p_hwfn,
5264                                               u16 rx_coal, u16 tx_coal,
5265                                               void *p_handle)
5266 {
5267         struct ecore_queue_cid *p_cid = (struct ecore_queue_cid *)p_handle;
5268         enum _ecore_status_t rc = ECORE_SUCCESS;
5269         struct ecore_ptt *p_ptt;
5270
5271         /* TODO - Configuring a single queue's coalescing but
5272          * claiming all queues are abiding same configuration
5273          * for PF and VF both.
5274          */
5275
5276         if (IS_VF(p_hwfn->p_dev))
5277                 return ecore_vf_pf_set_coalesce(p_hwfn, rx_coal,
5278                                                 tx_coal, p_cid);
5279
5280         p_ptt = ecore_ptt_acquire(p_hwfn);
5281         if (!p_ptt)
5282                 return ECORE_AGAIN;
5283
5284         if (rx_coal) {
5285                 rc = ecore_set_rxq_coalesce(p_hwfn, p_ptt, rx_coal, p_cid);
5286                 if (rc)
5287                         goto out;
5288                 p_hwfn->p_dev->rx_coalesce_usecs = rx_coal;
5289         }
5290
5291         if (tx_coal) {
5292                 rc = ecore_set_txq_coalesce(p_hwfn, p_ptt, tx_coal, p_cid);
5293                 if (rc)
5294                         goto out;
5295                 p_hwfn->p_dev->tx_coalesce_usecs = tx_coal;
5296         }
5297 out:
5298         ecore_ptt_release(p_hwfn, p_ptt);
5299
5300         return rc;
5301 }
5302
5303 enum _ecore_status_t ecore_set_rxq_coalesce(struct ecore_hwfn *p_hwfn,
5304                                             struct ecore_ptt *p_ptt,
5305                                             u16 coalesce,
5306                                             struct ecore_queue_cid *p_cid)
5307 {
5308         struct ustorm_eth_queue_zone eth_qzone;
5309         u8 timeset, timer_res;
5310         u32 address;
5311         enum _ecore_status_t rc;
5312
5313         /* Coalesce = (timeset << timer-resolution), timeset is 7bit wide */
5314         if (coalesce <= 0x7F) {
5315                 timer_res = 0;
5316         } else if (coalesce <= 0xFF) {
5317                 timer_res = 1;
5318         } else if (coalesce <= 0x1FF) {
5319                 timer_res = 2;
5320         } else {
5321                 DP_ERR(p_hwfn, "Invalid coalesce value - %d\n", coalesce);
5322                 return ECORE_INVAL;
5323         }
5324         timeset = (u8)(coalesce >> timer_res);
5325
5326         rc = ecore_int_set_timer_res(p_hwfn, p_ptt, timer_res,
5327                                      p_cid->sb_igu_id, false);
5328         if (rc != ECORE_SUCCESS)
5329                 goto out;
5330
5331         address = BAR0_MAP_REG_USDM_RAM +
5332                   USTORM_ETH_QUEUE_ZONE_OFFSET(p_cid->abs.queue_id);
5333
5334         rc = ecore_set_coalesce(p_hwfn, p_ptt, address, &eth_qzone,
5335                                 sizeof(struct ustorm_eth_queue_zone), timeset);
5336         if (rc != ECORE_SUCCESS)
5337                 goto out;
5338
5339 out:
5340         return rc;
5341 }
5342
5343 enum _ecore_status_t ecore_set_txq_coalesce(struct ecore_hwfn *p_hwfn,
5344                                             struct ecore_ptt *p_ptt,
5345                                             u16 coalesce,
5346                                             struct ecore_queue_cid *p_cid)
5347 {
5348         struct xstorm_eth_queue_zone eth_qzone;
5349         u8 timeset, timer_res;
5350         u32 address;
5351         enum _ecore_status_t rc;
5352
5353         /* Coalesce = (timeset << timer-resolution), timeset is 7bit wide */
5354         if (coalesce <= 0x7F) {
5355                 timer_res = 0;
5356         } else if (coalesce <= 0xFF) {
5357                 timer_res = 1;
5358         } else if (coalesce <= 0x1FF) {
5359                 timer_res = 2;
5360         } else {
5361                 DP_ERR(p_hwfn, "Invalid coalesce value - %d\n", coalesce);
5362                 return ECORE_INVAL;
5363         }
5364
5365         timeset = (u8)(coalesce >> timer_res);
5366
5367         rc = ecore_int_set_timer_res(p_hwfn, p_ptt, timer_res,
5368                                      p_cid->sb_igu_id, true);
5369         if (rc != ECORE_SUCCESS)
5370                 goto out;
5371
5372         address = BAR0_MAP_REG_XSDM_RAM +
5373                   XSTORM_ETH_QUEUE_ZONE_OFFSET(p_cid->abs.queue_id);
5374
5375         rc = ecore_set_coalesce(p_hwfn, p_ptt, address, &eth_qzone,
5376                                 sizeof(struct xstorm_eth_queue_zone), timeset);
5377 out:
5378         return rc;
5379 }
5380
5381 /* Calculate final WFQ values for all vports and configure it.
5382  * After this configuration each vport must have
5383  * approx min rate =  vport_wfq * min_pf_rate / ECORE_WFQ_UNIT
5384  */
5385 static void ecore_configure_wfq_for_all_vports(struct ecore_hwfn *p_hwfn,
5386                                                struct ecore_ptt *p_ptt,
5387                                                u32 min_pf_rate)
5388 {
5389         struct init_qm_vport_params *vport_params;
5390         int i;
5391
5392         vport_params = p_hwfn->qm_info.qm_vport_params;
5393
5394         for (i = 0; i < p_hwfn->qm_info.num_vports; i++) {
5395                 u32 wfq_speed = p_hwfn->qm_info.wfq_data[i].min_speed;
5396
5397                 vport_params[i].vport_wfq = (wfq_speed * ECORE_WFQ_UNIT) /
5398                     min_pf_rate;
5399                 ecore_init_vport_wfq(p_hwfn, p_ptt,
5400                                      vport_params[i].first_tx_pq_id,
5401                                      vport_params[i].vport_wfq);
5402         }
5403 }
5404
5405 static void ecore_init_wfq_default_param(struct ecore_hwfn *p_hwfn)
5406 {
5407         int i;
5408
5409         for (i = 0; i < p_hwfn->qm_info.num_vports; i++)
5410                 p_hwfn->qm_info.qm_vport_params[i].vport_wfq = 1;
5411 }
5412
5413 static void ecore_disable_wfq_for_all_vports(struct ecore_hwfn *p_hwfn,
5414                                              struct ecore_ptt *p_ptt)
5415 {
5416         struct init_qm_vport_params *vport_params;
5417         int i;
5418
5419         vport_params = p_hwfn->qm_info.qm_vport_params;
5420
5421         for (i = 0; i < p_hwfn->qm_info.num_vports; i++) {
5422                 ecore_init_wfq_default_param(p_hwfn);
5423                 ecore_init_vport_wfq(p_hwfn, p_ptt,
5424                                      vport_params[i].first_tx_pq_id,
5425                                      vport_params[i].vport_wfq);
5426         }
5427 }
5428
5429 /* This function performs several validations for WFQ
5430  * configuration and required min rate for a given vport
5431  * 1. req_rate must be greater than one percent of min_pf_rate.
5432  * 2. req_rate should not cause other vports [not configured for WFQ explicitly]
5433  *    rates to get less than one percent of min_pf_rate.
5434  * 3. total_req_min_rate [all vports min rate sum] shouldn't exceed min_pf_rate.
5435  */
5436 static enum _ecore_status_t ecore_init_wfq_param(struct ecore_hwfn *p_hwfn,
5437                                                  u16 vport_id, u32 req_rate,
5438                                                  u32 min_pf_rate)
5439 {
5440         u32 total_req_min_rate = 0, total_left_rate = 0, left_rate_per_vp = 0;
5441         int non_requested_count = 0, req_count = 0, i, num_vports;
5442
5443         num_vports = p_hwfn->qm_info.num_vports;
5444
5445 /* Accounting for the vports which are configured for WFQ explicitly */
5446
5447         for (i = 0; i < num_vports; i++) {
5448                 u32 tmp_speed;
5449
5450                 if ((i != vport_id) && p_hwfn->qm_info.wfq_data[i].configured) {
5451                         req_count++;
5452                         tmp_speed = p_hwfn->qm_info.wfq_data[i].min_speed;
5453                         total_req_min_rate += tmp_speed;
5454                 }
5455         }
5456
5457         /* Include current vport data as well */
5458         req_count++;
5459         total_req_min_rate += req_rate;
5460         non_requested_count = num_vports - req_count;
5461
5462         /* validate possible error cases */
5463         if (req_rate < min_pf_rate / ECORE_WFQ_UNIT) {
5464                 DP_VERBOSE(p_hwfn, ECORE_MSG_LINK,
5465                            "Vport [%d] - Requested rate[%d Mbps] is less than one percent of configured PF min rate[%d Mbps]\n",
5466                            vport_id, req_rate, min_pf_rate);
5467                 return ECORE_INVAL;
5468         }
5469
5470         /* TBD - for number of vports greater than 100 */
5471         if (num_vports > ECORE_WFQ_UNIT) {
5472                 DP_VERBOSE(p_hwfn, ECORE_MSG_LINK,
5473                            "Number of vports is greater than %d\n",
5474                            ECORE_WFQ_UNIT);
5475                 return ECORE_INVAL;
5476         }
5477
5478         if (total_req_min_rate > min_pf_rate) {
5479                 DP_VERBOSE(p_hwfn, ECORE_MSG_LINK,
5480                            "Total requested min rate for all vports[%d Mbps] is greater than configured PF min rate[%d Mbps]\n",
5481                            total_req_min_rate, min_pf_rate);
5482                 return ECORE_INVAL;
5483         }
5484
5485         /* Data left for non requested vports */
5486         total_left_rate = min_pf_rate - total_req_min_rate;
5487         left_rate_per_vp = total_left_rate / non_requested_count;
5488
5489         /* validate if non requested get < 1% of min bw */
5490         if (left_rate_per_vp < min_pf_rate / ECORE_WFQ_UNIT) {
5491                 DP_VERBOSE(p_hwfn, ECORE_MSG_LINK,
5492                            "Non WFQ configured vports rate [%d Mbps] is less than one percent of configured PF min rate[%d Mbps]\n",
5493                            left_rate_per_vp, min_pf_rate);
5494                 return ECORE_INVAL;
5495         }
5496
5497         /* now req_rate for given vport passes all scenarios.
5498          * assign final wfq rates to all vports.
5499          */
5500         p_hwfn->qm_info.wfq_data[vport_id].min_speed = req_rate;
5501         p_hwfn->qm_info.wfq_data[vport_id].configured = true;
5502
5503         for (i = 0; i < num_vports; i++) {
5504                 if (p_hwfn->qm_info.wfq_data[i].configured)
5505                         continue;
5506
5507                 p_hwfn->qm_info.wfq_data[i].min_speed = left_rate_per_vp;
5508         }
5509
5510         return ECORE_SUCCESS;
5511 }
5512
5513 static int __ecore_configure_vport_wfq(struct ecore_hwfn *p_hwfn,
5514                                        struct ecore_ptt *p_ptt,
5515                                        u16 vp_id, u32 rate)
5516 {
5517         struct ecore_mcp_link_state *p_link;
5518         int rc = ECORE_SUCCESS;
5519
5520         p_link = &p_hwfn->p_dev->hwfns[0].mcp_info->link_output;
5521
5522         if (!p_link->min_pf_rate) {
5523                 p_hwfn->qm_info.wfq_data[vp_id].min_speed = rate;
5524                 p_hwfn->qm_info.wfq_data[vp_id].configured = true;
5525                 return rc;
5526         }
5527
5528         rc = ecore_init_wfq_param(p_hwfn, vp_id, rate, p_link->min_pf_rate);
5529
5530         if (rc == ECORE_SUCCESS)
5531                 ecore_configure_wfq_for_all_vports(p_hwfn, p_ptt,
5532                                                    p_link->min_pf_rate);
5533         else
5534                 DP_NOTICE(p_hwfn, false,
5535                           "Validation failed while configuring min rate\n");
5536
5537         return rc;
5538 }
5539
5540 static int __ecore_configure_vp_wfq_on_link_change(struct ecore_hwfn *p_hwfn,
5541                                                    struct ecore_ptt *p_ptt,
5542                                                    u32 min_pf_rate)
5543 {
5544         bool use_wfq = false;
5545         int rc = ECORE_SUCCESS;
5546         u16 i;
5547
5548         /* Validate all pre configured vports for wfq */
5549         for (i = 0; i < p_hwfn->qm_info.num_vports; i++) {
5550                 u32 rate;
5551
5552                 if (!p_hwfn->qm_info.wfq_data[i].configured)
5553                         continue;
5554
5555                 rate = p_hwfn->qm_info.wfq_data[i].min_speed;
5556                 use_wfq = true;
5557
5558                 rc = ecore_init_wfq_param(p_hwfn, i, rate, min_pf_rate);
5559                 if (rc != ECORE_SUCCESS) {
5560                         DP_NOTICE(p_hwfn, false,
5561                                   "WFQ validation failed while configuring min rate\n");
5562                         break;
5563                 }
5564         }
5565
5566         if (rc == ECORE_SUCCESS && use_wfq)
5567                 ecore_configure_wfq_for_all_vports(p_hwfn, p_ptt, min_pf_rate);
5568         else
5569                 ecore_disable_wfq_for_all_vports(p_hwfn, p_ptt);
5570
5571         return rc;
5572 }
5573
5574 /* Main API for ecore clients to configure vport min rate.
5575  * vp_id - vport id in PF Range[0 - (total_num_vports_per_pf - 1)]
5576  * rate - Speed in Mbps needs to be assigned to a given vport.
5577  */
5578 int ecore_configure_vport_wfq(struct ecore_dev *p_dev, u16 vp_id, u32 rate)
5579 {
5580         int i, rc = ECORE_INVAL;
5581
5582         /* TBD - for multiple hardware functions - that is 100 gig */
5583         if (ECORE_IS_CMT(p_dev)) {
5584                 DP_NOTICE(p_dev, false,
5585                           "WFQ configuration is not supported for this device\n");
5586                 return rc;
5587         }
5588
5589         for_each_hwfn(p_dev, i) {
5590                 struct ecore_hwfn *p_hwfn = &p_dev->hwfns[i];
5591                 struct ecore_ptt *p_ptt;
5592
5593                 p_ptt = ecore_ptt_acquire(p_hwfn);
5594                 if (!p_ptt)
5595                         return ECORE_TIMEOUT;
5596
5597                 rc = __ecore_configure_vport_wfq(p_hwfn, p_ptt, vp_id, rate);
5598
5599                 if (rc != ECORE_SUCCESS) {
5600                         ecore_ptt_release(p_hwfn, p_ptt);
5601                         return rc;
5602                 }
5603
5604                 ecore_ptt_release(p_hwfn, p_ptt);
5605         }
5606
5607         return rc;
5608 }
5609
5610 /* API to configure WFQ from mcp link change */
5611 void ecore_configure_vp_wfq_on_link_change(struct ecore_dev *p_dev,
5612                                            struct ecore_ptt *p_ptt,
5613                                            u32 min_pf_rate)
5614 {
5615         int i;
5616
5617         /* TBD - for multiple hardware functions - that is 100 gig */
5618         if (ECORE_IS_CMT(p_dev)) {
5619                 DP_VERBOSE(p_dev, ECORE_MSG_LINK,
5620                            "WFQ configuration is not supported for this device\n");
5621                 return;
5622         }
5623
5624         for_each_hwfn(p_dev, i) {
5625                 struct ecore_hwfn *p_hwfn = &p_dev->hwfns[i];
5626
5627                 __ecore_configure_vp_wfq_on_link_change(p_hwfn, p_ptt,
5628                                                         min_pf_rate);
5629         }
5630 }
5631
5632 int __ecore_configure_pf_max_bandwidth(struct ecore_hwfn *p_hwfn,
5633                                        struct ecore_ptt *p_ptt,
5634                                        struct ecore_mcp_link_state *p_link,
5635                                        u8 max_bw)
5636 {
5637         int rc = ECORE_SUCCESS;
5638
5639         p_hwfn->mcp_info->func_info.bandwidth_max = max_bw;
5640
5641         if (!p_link->line_speed && (max_bw != 100))
5642                 return rc;
5643
5644         p_link->speed = (p_link->line_speed * max_bw) / 100;
5645         p_hwfn->qm_info.pf_rl = p_link->speed;
5646
5647         /* Since the limiter also affects Tx-switched traffic, we don't want it
5648          * to limit such traffic in case there's no actual limit.
5649          * In that case, set limit to imaginary high boundary.
5650          */
5651         if (max_bw == 100)
5652                 p_hwfn->qm_info.pf_rl = 100000;
5653
5654         rc = ecore_init_pf_rl(p_hwfn, p_ptt, p_hwfn->rel_pf_id,
5655                               p_hwfn->qm_info.pf_rl);
5656
5657         DP_VERBOSE(p_hwfn, ECORE_MSG_LINK,
5658                    "Configured MAX bandwidth to be %08x Mb/sec\n",
5659                    p_link->speed);
5660
5661         return rc;
5662 }
5663
5664 /* Main API to configure PF max bandwidth where bw range is [1 - 100] */
5665 int ecore_configure_pf_max_bandwidth(struct ecore_dev *p_dev, u8 max_bw)
5666 {
5667         int i, rc = ECORE_INVAL;
5668
5669         if (max_bw < 1 || max_bw > 100) {
5670                 DP_NOTICE(p_dev, false, "PF max bw valid range is [1-100]\n");
5671                 return rc;
5672         }
5673
5674         for_each_hwfn(p_dev, i) {
5675                 struct ecore_hwfn *p_hwfn = &p_dev->hwfns[i];
5676                 struct ecore_hwfn *p_lead = ECORE_LEADING_HWFN(p_dev);
5677                 struct ecore_mcp_link_state *p_link;
5678                 struct ecore_ptt *p_ptt;
5679
5680                 p_link = &p_lead->mcp_info->link_output;
5681
5682                 p_ptt = ecore_ptt_acquire(p_hwfn);
5683                 if (!p_ptt)
5684                         return ECORE_TIMEOUT;
5685
5686                 rc = __ecore_configure_pf_max_bandwidth(p_hwfn, p_ptt,
5687                                                         p_link, max_bw);
5688
5689                 ecore_ptt_release(p_hwfn, p_ptt);
5690
5691                 if (rc != ECORE_SUCCESS)
5692                         break;
5693         }
5694
5695         return rc;
5696 }
5697
5698 int __ecore_configure_pf_min_bandwidth(struct ecore_hwfn *p_hwfn,
5699                                        struct ecore_ptt *p_ptt,
5700                                        struct ecore_mcp_link_state *p_link,
5701                                        u8 min_bw)
5702 {
5703         int rc = ECORE_SUCCESS;
5704
5705         p_hwfn->mcp_info->func_info.bandwidth_min = min_bw;
5706         p_hwfn->qm_info.pf_wfq = min_bw;
5707
5708         if (!p_link->line_speed)
5709                 return rc;
5710
5711         p_link->min_pf_rate = (p_link->line_speed * min_bw) / 100;
5712
5713         rc = ecore_init_pf_wfq(p_hwfn, p_ptt, p_hwfn->rel_pf_id, min_bw);
5714
5715         DP_VERBOSE(p_hwfn, ECORE_MSG_LINK,
5716                    "Configured MIN bandwidth to be %d Mb/sec\n",
5717                    p_link->min_pf_rate);
5718
5719         return rc;
5720 }
5721
5722 /* Main API to configure PF min bandwidth where bw range is [1-100] */
5723 int ecore_configure_pf_min_bandwidth(struct ecore_dev *p_dev, u8 min_bw)
5724 {
5725         int i, rc = ECORE_INVAL;
5726
5727         if (min_bw < 1 || min_bw > 100) {
5728                 DP_NOTICE(p_dev, false, "PF min bw valid range is [1-100]\n");
5729                 return rc;
5730         }
5731
5732         for_each_hwfn(p_dev, i) {
5733                 struct ecore_hwfn *p_hwfn = &p_dev->hwfns[i];
5734                 struct ecore_hwfn *p_lead = ECORE_LEADING_HWFN(p_dev);
5735                 struct ecore_mcp_link_state *p_link;
5736                 struct ecore_ptt *p_ptt;
5737
5738                 p_link = &p_lead->mcp_info->link_output;
5739
5740                 p_ptt = ecore_ptt_acquire(p_hwfn);
5741                 if (!p_ptt)
5742                         return ECORE_TIMEOUT;
5743
5744                 rc = __ecore_configure_pf_min_bandwidth(p_hwfn, p_ptt,
5745                                                         p_link, min_bw);
5746                 if (rc != ECORE_SUCCESS) {
5747                         ecore_ptt_release(p_hwfn, p_ptt);
5748                         return rc;
5749                 }
5750
5751                 if (p_link->min_pf_rate) {
5752                         u32 min_rate = p_link->min_pf_rate;
5753
5754                         rc = __ecore_configure_vp_wfq_on_link_change(p_hwfn,
5755                                                                      p_ptt,
5756                                                                      min_rate);
5757                 }
5758
5759                 ecore_ptt_release(p_hwfn, p_ptt);
5760         }
5761
5762         return rc;
5763 }
5764
5765 void ecore_clean_wfq_db(struct ecore_hwfn *p_hwfn, struct ecore_ptt *p_ptt)
5766 {
5767         struct ecore_mcp_link_state *p_link;
5768
5769         p_link = &p_hwfn->mcp_info->link_output;
5770
5771         if (p_link->min_pf_rate)
5772                 ecore_disable_wfq_for_all_vports(p_hwfn, p_ptt);
5773
5774         OSAL_MEMSET(p_hwfn->qm_info.wfq_data, 0,
5775                     sizeof(*p_hwfn->qm_info.wfq_data) *
5776                     p_hwfn->qm_info.num_vports);
5777 }
5778
5779 int ecore_device_num_engines(struct ecore_dev *p_dev)
5780 {
5781         return ECORE_IS_BB(p_dev) ? 2 : 1;
5782 }
5783
5784 int ecore_device_num_ports(struct ecore_dev *p_dev)
5785 {
5786         return p_dev->num_ports;
5787 }
5788
5789 void ecore_set_fw_mac_addr(__le16 *fw_msb,
5790                           __le16 *fw_mid,
5791                           __le16 *fw_lsb,
5792                           u8 *mac)
5793 {
5794         ((u8 *)fw_msb)[0] = mac[1];
5795         ((u8 *)fw_msb)[1] = mac[0];
5796         ((u8 *)fw_mid)[0] = mac[3];
5797         ((u8 *)fw_mid)[1] = mac[2];
5798         ((u8 *)fw_lsb)[0] = mac[5];
5799         ((u8 *)fw_lsb)[1] = mac[4];
5800 }