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