net/qede/base: fix TM block ILT initialization
[dpdk.git] / drivers / net / qede / base / ecore_cxt.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_hsi_common.h"
12 #include "ecore_hsi_eth.h"
13 #include "ecore_rt_defs.h"
14 #include "ecore_status.h"
15 #include "ecore.h"
16 #include "ecore_init_ops.h"
17 #include "ecore_init_fw_funcs.h"
18 #include "ecore_cxt.h"
19 #include "ecore_hw.h"
20 #include "ecore_dev_api.h"
21 #include "ecore_sriov.h"
22
23 /* Max number of connection types in HW (DQ/CDU etc.) */
24 #define MAX_CONN_TYPES          PROTOCOLID_COMMON
25 #define NUM_TASK_TYPES          2
26 #define NUM_TASK_PF_SEGMENTS    4
27 #define NUM_TASK_VF_SEGMENTS    1
28
29 /* Doorbell-Queue constants */
30 #define DQ_RANGE_SHIFT  4
31 #define DQ_RANGE_ALIGN  (1 << DQ_RANGE_SHIFT)
32
33 /* Searcher constants */
34 #define SRC_MIN_NUM_ELEMS 256
35
36 /* Timers constants */
37 #define TM_SHIFT        7
38 #define TM_ALIGN        (1 << TM_SHIFT)
39 #define TM_ELEM_SIZE    4
40
41 /* ILT constants */
42 /* If for some reason, HW P size is modified to be less than 32K,
43  * special handling needs to be made for CDU initialization
44  */
45 #define ILT_DEFAULT_HW_P_SIZE   3
46
47 #define ILT_PAGE_IN_BYTES(hw_p_size)    (1U << ((hw_p_size) + 12))
48 #define ILT_CFG_REG(cli, reg)           PSWRQ2_REG_##cli##_##reg##_RT_OFFSET
49
50 /* ILT entry structure */
51 #define ILT_ENTRY_PHY_ADDR_MASK         0x000FFFFFFFFFFFULL
52 #define ILT_ENTRY_PHY_ADDR_SHIFT        0
53 #define ILT_ENTRY_VALID_MASK            0x1ULL
54 #define ILT_ENTRY_VALID_SHIFT           52
55 #define ILT_ENTRY_IN_REGS               2
56 #define ILT_REG_SIZE_IN_BYTES           4
57
58 /* connection context union */
59 union conn_context {
60         struct core_conn_context core_ctx;
61         struct eth_conn_context eth_ctx;
62 };
63
64 /* TYPE-0 task context - iSCSI, FCOE */
65 union type0_task_context {
66 };
67
68 /* TYPE-1 task context - ROCE */
69 union type1_task_context {
70 };
71
72 struct src_ent {
73         u8 opaque[56];
74         u64 next;
75 };
76
77 #define CDUT_SEG_ALIGNMET 3     /* in 4k chunks */
78 #define CDUT_SEG_ALIGNMET_IN_BYTES (1 << (CDUT_SEG_ALIGNMET + 12))
79
80 #define CONN_CXT_SIZE(p_hwfn) \
81         ALIGNED_TYPE_SIZE(union conn_context, p_hwfn)
82
83 #define SRQ_CXT_SIZE (sizeof(struct regpair) * 8) /* @DPDK */
84
85 #define TYPE0_TASK_CXT_SIZE(p_hwfn) \
86         ALIGNED_TYPE_SIZE(union type0_task_context, p_hwfn)
87
88 /* Alignment is inherent to the type1_task_context structure */
89 #define TYPE1_TASK_CXT_SIZE(p_hwfn) sizeof(union type1_task_context)
90
91 /* PF per protocl configuration object */
92 #define TASK_SEGMENTS   (NUM_TASK_PF_SEGMENTS + NUM_TASK_VF_SEGMENTS)
93 #define TASK_SEGMENT_VF (NUM_TASK_PF_SEGMENTS)
94
95 struct ecore_tid_seg {
96         u32 count;
97         u8 type;
98         bool has_fl_mem;
99 };
100
101 struct ecore_conn_type_cfg {
102         u32 cid_count;
103         u32 cid_start;
104         u32 cids_per_vf;
105         struct ecore_tid_seg tid_seg[TASK_SEGMENTS];
106 };
107
108 /* ILT Client configuration,
109  * Per connection type (protocol) resources (cids, tis, vf cids etc.)
110  * 1 - for connection context (CDUC) and for each task context we need two
111  * values, for regular task context and for force load memory
112  */
113 #define ILT_CLI_PF_BLOCKS       (1 + NUM_TASK_PF_SEGMENTS * 2)
114 #define ILT_CLI_VF_BLOCKS       (1 + NUM_TASK_VF_SEGMENTS * 2)
115 #define CDUC_BLK                (0)
116 #define SRQ_BLK                 (0)
117 #define CDUT_SEG_BLK(n)         (1 + (u8)(n))
118 #define CDUT_FL_SEG_BLK(n, X)   (1 + (n) + NUM_TASK_##X##_SEGMENTS)
119
120 enum ilt_clients {
121         ILT_CLI_CDUC,
122         ILT_CLI_CDUT,
123         ILT_CLI_QM,
124         ILT_CLI_TM,
125         ILT_CLI_SRC,
126         ILT_CLI_TSDM,
127         ILT_CLI_MAX
128 };
129
130 struct ilt_cfg_pair {
131         u32 reg;
132         u32 val;
133 };
134
135 struct ecore_ilt_cli_blk {
136         u32 total_size;         /* 0 means not active */
137         u32 real_size_in_page;
138         u32 start_line;
139         u32 dynamic_line_cnt;
140 };
141
142 struct ecore_ilt_client_cfg {
143         bool active;
144
145         /* ILT boundaries */
146         struct ilt_cfg_pair first;
147         struct ilt_cfg_pair last;
148         struct ilt_cfg_pair p_size;
149
150         /* ILT client blocks for PF */
151         struct ecore_ilt_cli_blk pf_blks[ILT_CLI_PF_BLOCKS];
152         u32 pf_total_lines;
153
154         /* ILT client blocks for VFs */
155         struct ecore_ilt_cli_blk vf_blks[ILT_CLI_VF_BLOCKS];
156         u32 vf_total_lines;
157 };
158
159 /* Per Path -
160  *      ILT shadow table
161  *      Protocol acquired CID lists
162  *      PF start line in ILT
163  */
164 struct ecore_dma_mem {
165         dma_addr_t p_phys;
166         void *p_virt;
167         osal_size_t size;
168 };
169
170 #define MAP_WORD_SIZE           sizeof(unsigned long)
171 #define BITS_PER_MAP_WORD       (MAP_WORD_SIZE * 8)
172
173 struct ecore_cid_acquired_map {
174         u32 start_cid;
175         u32 max_count;
176         unsigned long *cid_map;
177 };
178
179 struct ecore_cxt_mngr {
180         /* Per protocl configuration */
181         struct ecore_conn_type_cfg conn_cfg[MAX_CONN_TYPES];
182
183         /* computed ILT structure */
184         struct ecore_ilt_client_cfg clients[ILT_CLI_MAX];
185
186         /* Task type sizes */
187         u32 task_type_size[NUM_TASK_TYPES];
188
189         /* total number of VFs for this hwfn -
190          * ALL VFs are symmetric in terms of HW resources
191          */
192         u32 vf_count;
193
194         /* total number of SRQ's for this hwfn */
195         u32                             srq_count;
196
197         /* Acquired CIDs */
198         struct ecore_cid_acquired_map acquired[MAX_CONN_TYPES];
199
200         /* ILT  shadow table */
201         struct ecore_dma_mem *ilt_shadow;
202         u32 pf_start_line;
203
204         /* Mutex for a dynamic ILT allocation */
205         osal_mutex_t mutex;
206
207         /* SRC T2 */
208         struct ecore_dma_mem *t2;
209         u32 t2_num_pages;
210         u64 first_free;
211         u64 last_free;
212 };
213
214 /* check if resources/configuration is required according to protocol type */
215 static OSAL_INLINE bool src_proto(enum protocol_type type)
216 {
217         return type == PROTOCOLID_TOE;
218 }
219
220 static OSAL_INLINE bool tm_cid_proto(enum protocol_type type)
221 {
222         return type == PROTOCOLID_TOE;
223 }
224
225 static bool tm_tid_proto(enum protocol_type type)
226 {
227         return type == PROTOCOLID_FCOE;
228 }
229
230 /* counts the iids for the CDU/CDUC ILT client configuration */
231 struct ecore_cdu_iids {
232         u32 pf_cids;
233         u32 per_vf_cids;
234 };
235
236 static void ecore_cxt_cdu_iids(struct ecore_cxt_mngr *p_mngr,
237                                struct ecore_cdu_iids *iids)
238 {
239         u32 type;
240
241         for (type = 0; type < MAX_CONN_TYPES; type++) {
242                 iids->pf_cids += p_mngr->conn_cfg[type].cid_count;
243                 iids->per_vf_cids += p_mngr->conn_cfg[type].cids_per_vf;
244         }
245 }
246
247 /* counts the iids for the Searcher block configuration */
248 struct ecore_src_iids {
249         u32 pf_cids;
250         u32 per_vf_cids;
251 };
252
253 static OSAL_INLINE void ecore_cxt_src_iids(struct ecore_cxt_mngr *p_mngr,
254                                            struct ecore_src_iids *iids)
255 {
256         u32 i;
257
258         for (i = 0; i < MAX_CONN_TYPES; i++) {
259                 if (!src_proto(i))
260                         continue;
261
262                 iids->pf_cids += p_mngr->conn_cfg[i].cid_count;
263                 iids->per_vf_cids += p_mngr->conn_cfg[i].cids_per_vf;
264         }
265 }
266
267 /* counts the iids for the Timers block configuration */
268 struct ecore_tm_iids {
269         u32 pf_cids;
270         u32 pf_tids[NUM_TASK_PF_SEGMENTS];      /* per segment */
271         u32 pf_tids_total;
272         u32 per_vf_cids;
273         u32 per_vf_tids;
274 };
275
276 static OSAL_INLINE void ecore_cxt_tm_iids(struct ecore_cxt_mngr *p_mngr,
277                                           struct ecore_tm_iids *iids)
278 {
279         bool tm_vf_required = false;
280         bool tm_required = false;
281         u32 i, j;
282
283         for (i = 0; i < MAX_CONN_TYPES; i++) {
284                 struct ecore_conn_type_cfg *p_cfg = &p_mngr->conn_cfg[i];
285
286                 if (tm_cid_proto(i) || tm_required) {
287                         if (p_cfg->cid_count)
288                                 tm_required = true;
289
290                         iids->pf_cids += p_cfg->cid_count;
291                 }
292
293                 if (tm_cid_proto(i) || tm_vf_required) {
294                         if (p_cfg->cids_per_vf)
295                                 tm_vf_required = true;
296
297                 }
298
299                 if (tm_tid_proto(i)) {
300                         struct ecore_tid_seg *segs = p_cfg->tid_seg;
301
302                         /* for each segment there is at most one
303                          * protocol for which count is not 0.
304                          */
305                         for (j = 0; j < NUM_TASK_PF_SEGMENTS; j++)
306                                 iids->pf_tids[j] += segs[j].count;
307
308                         /* The last array elelment is for the VFs. As for PF
309                          * segments there can be only one protocol for
310                          * which this value is not 0.
311                          */
312                         iids->per_vf_tids += segs[NUM_TASK_PF_SEGMENTS].count;
313                 }
314         }
315
316         iids->pf_cids = ROUNDUP(iids->pf_cids, TM_ALIGN);
317         iids->per_vf_cids = ROUNDUP(iids->per_vf_cids, TM_ALIGN);
318         iids->per_vf_tids = ROUNDUP(iids->per_vf_tids, TM_ALIGN);
319
320         for (iids->pf_tids_total = 0, j = 0; j < NUM_TASK_PF_SEGMENTS; j++) {
321                 iids->pf_tids[j] = ROUNDUP(iids->pf_tids[j], TM_ALIGN);
322                 iids->pf_tids_total += iids->pf_tids[j];
323         }
324 }
325
326 void ecore_cxt_qm_iids(struct ecore_hwfn *p_hwfn, struct ecore_qm_iids *iids)
327 {
328         struct ecore_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
329         struct ecore_tid_seg *segs;
330         u32 vf_cids = 0, type, j;
331         u32 vf_tids = 0;
332
333         for (type = 0; type < MAX_CONN_TYPES; type++) {
334                 iids->cids += p_mngr->conn_cfg[type].cid_count;
335                 vf_cids += p_mngr->conn_cfg[type].cids_per_vf;
336
337                 segs = p_mngr->conn_cfg[type].tid_seg;
338                 /* for each segment there is at most one
339                  * protocol for which count is not 0.
340                  */
341                 for (j = 0; j < NUM_TASK_PF_SEGMENTS; j++)
342                         iids->tids += segs[j].count;
343
344                 /* The last array elelment is for the VFs. As for PF
345                  * segments there can be only one protocol for
346                  * which this value is not 0.
347                  */
348                 vf_tids += segs[NUM_TASK_PF_SEGMENTS].count;
349         }
350
351         iids->vf_cids += vf_cids * p_mngr->vf_count;
352         iids->tids += vf_tids * p_mngr->vf_count;
353
354         DP_VERBOSE(p_hwfn, ECORE_MSG_ILT,
355                    "iids: CIDS %08x vf_cids %08x tids %08x vf_tids %08x\n",
356                    iids->cids, iids->vf_cids, iids->tids, vf_tids);
357 }
358
359 static struct ecore_tid_seg *ecore_cxt_tid_seg_info(struct ecore_hwfn *p_hwfn,
360                                                     u32 seg)
361 {
362         struct ecore_cxt_mngr *p_cfg = p_hwfn->p_cxt_mngr;
363         u32 i;
364
365         /* Find the protocol with tid count > 0 for this segment.
366          * Note: there can only be one and this is already validated.
367          */
368         for (i = 0; i < MAX_CONN_TYPES; i++) {
369                 if (p_cfg->conn_cfg[i].tid_seg[seg].count)
370                         return &p_cfg->conn_cfg[i].tid_seg[seg];
371         }
372         return OSAL_NULL;
373 }
374
375 /* set the iids (cid/tid) count per protocol */
376 static void ecore_cxt_set_proto_cid_count(struct ecore_hwfn *p_hwfn,
377                                    enum protocol_type type,
378                                    u32 cid_count, u32 vf_cid_cnt)
379 {
380         struct ecore_cxt_mngr *p_mgr = p_hwfn->p_cxt_mngr;
381         struct ecore_conn_type_cfg *p_conn = &p_mgr->conn_cfg[type];
382
383         p_conn->cid_count = ROUNDUP(cid_count, DQ_RANGE_ALIGN);
384         p_conn->cids_per_vf = ROUNDUP(vf_cid_cnt, DQ_RANGE_ALIGN);
385 }
386
387 u32 ecore_cxt_get_proto_cid_count(struct ecore_hwfn *p_hwfn,
388                                   enum protocol_type type, u32 *vf_cid)
389 {
390         if (vf_cid)
391                 *vf_cid = p_hwfn->p_cxt_mngr->conn_cfg[type].cids_per_vf;
392
393         return p_hwfn->p_cxt_mngr->conn_cfg[type].cid_count;
394 }
395
396 u32 ecore_cxt_get_proto_cid_start(struct ecore_hwfn *p_hwfn,
397                                   enum protocol_type type)
398 {
399         return p_hwfn->p_cxt_mngr->acquired[type].start_cid;
400 }
401
402 u32 ecore_cxt_get_proto_tid_count(struct ecore_hwfn *p_hwfn,
403                                          enum protocol_type type)
404 {
405         u32 cnt = 0;
406         int i;
407
408         for (i = 0; i < TASK_SEGMENTS; i++)
409                 cnt += p_hwfn->p_cxt_mngr->conn_cfg[type].tid_seg[i].count;
410
411         return cnt;
412 }
413
414 static OSAL_INLINE void
415 ecore_cxt_set_proto_tid_count(struct ecore_hwfn *p_hwfn,
416                               enum protocol_type proto,
417                               u8 seg, u8 seg_type, u32 count, bool has_fl)
418 {
419         struct ecore_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
420         struct ecore_tid_seg *p_seg = &p_mngr->conn_cfg[proto].tid_seg[seg];
421
422         p_seg->count = count;
423         p_seg->has_fl_mem = has_fl;
424         p_seg->type = seg_type;
425 }
426
427 /* the *p_line parameter must be either 0 for the first invocation or the
428  * value returned in the previous invocation.
429  */
430 static void ecore_ilt_cli_blk_fill(struct ecore_ilt_client_cfg *p_cli,
431                                    struct ecore_ilt_cli_blk *p_blk,
432                                    u32 start_line,
433                                    u32 total_size, u32 elem_size)
434 {
435         u32 ilt_size = ILT_PAGE_IN_BYTES(p_cli->p_size.val);
436
437         /* verify that it's called once for each block */
438         if (p_blk->total_size)
439                 return;
440
441         p_blk->total_size = total_size;
442         p_blk->real_size_in_page = 0;
443         if (elem_size)
444                 p_blk->real_size_in_page = (ilt_size / elem_size) * elem_size;
445         p_blk->start_line = start_line;
446 }
447
448 static void ecore_ilt_cli_adv_line(struct ecore_hwfn *p_hwfn,
449                                    struct ecore_ilt_client_cfg *p_cli,
450                                    struct ecore_ilt_cli_blk *p_blk,
451                                    u32 *p_line, enum ilt_clients client_id)
452 {
453         if (!p_blk->total_size)
454                 return;
455
456         if (!p_cli->active)
457                 p_cli->first.val = *p_line;
458
459         p_cli->active = true;
460         *p_line += DIV_ROUND_UP(p_blk->total_size, p_blk->real_size_in_page);
461         p_cli->last.val = *p_line - 1;
462
463         DP_VERBOSE(p_hwfn, ECORE_MSG_ILT,
464                    "ILT[Client %d] - Lines: [%08x - %08x]. Block - Size %08x"
465                    " [Real %08x] Start line %d\n",
466                    client_id, p_cli->first.val, p_cli->last.val,
467                    p_blk->total_size, p_blk->real_size_in_page,
468                    p_blk->start_line);
469 }
470
471 static u32 ecore_ilt_get_dynamic_line_cnt(struct ecore_hwfn *p_hwfn,
472                                           enum ilt_clients ilt_client)
473 {
474         u32 cid_count = p_hwfn->p_cxt_mngr->conn_cfg[PROTOCOLID_ROCE].cid_count;
475         struct ecore_ilt_client_cfg *p_cli;
476         u32 lines_to_skip = 0;
477         u32 cxts_per_p;
478
479         /* TBD MK: ILT code should be simplified once PROTO enum is changed */
480
481         if (ilt_client == ILT_CLI_CDUC) {
482                 p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC];
483
484                 cxts_per_p = ILT_PAGE_IN_BYTES(p_cli->p_size.val) /
485                     (u32)CONN_CXT_SIZE(p_hwfn);
486
487                 lines_to_skip = cid_count / cxts_per_p;
488         }
489
490         return lines_to_skip;
491 }
492
493 enum _ecore_status_t ecore_cxt_cfg_ilt_compute(struct ecore_hwfn *p_hwfn)
494 {
495         struct ecore_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
496         u32 curr_line, total, i, task_size, line;
497         struct ecore_ilt_client_cfg *p_cli;
498         struct ecore_ilt_cli_blk *p_blk;
499         struct ecore_cdu_iids cdu_iids;
500         struct ecore_src_iids src_iids;
501         struct ecore_qm_iids qm_iids;
502         struct ecore_tm_iids tm_iids;
503         struct ecore_tid_seg *p_seg;
504
505         OSAL_MEM_ZERO(&qm_iids, sizeof(qm_iids));
506         OSAL_MEM_ZERO(&cdu_iids, sizeof(cdu_iids));
507         OSAL_MEM_ZERO(&src_iids, sizeof(src_iids));
508         OSAL_MEM_ZERO(&tm_iids, sizeof(tm_iids));
509
510         p_mngr->pf_start_line = RESC_START(p_hwfn, ECORE_ILT);
511
512         DP_VERBOSE(p_hwfn, ECORE_MSG_ILT,
513                    "hwfn [%d] - Set context mngr starting line to be 0x%08x\n",
514                    p_hwfn->my_id, p_hwfn->p_cxt_mngr->pf_start_line);
515
516         /* CDUC */
517         p_cli = &p_mngr->clients[ILT_CLI_CDUC];
518
519         curr_line = p_mngr->pf_start_line;
520
521         /* CDUC PF */
522         p_cli->pf_total_lines = 0;
523
524         /* get the counters for the CDUC,CDUC and QM clients  */
525         ecore_cxt_cdu_iids(p_mngr, &cdu_iids);
526
527         p_blk = &p_cli->pf_blks[CDUC_BLK];
528
529         total = cdu_iids.pf_cids * CONN_CXT_SIZE(p_hwfn);
530
531         ecore_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
532                                total, CONN_CXT_SIZE(p_hwfn));
533
534         ecore_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line, ILT_CLI_CDUC);
535         p_cli->pf_total_lines = curr_line - p_blk->start_line;
536
537         p_blk->dynamic_line_cnt = ecore_ilt_get_dynamic_line_cnt(p_hwfn,
538                                                                  ILT_CLI_CDUC);
539
540         /* CDUC VF */
541         p_blk = &p_cli->vf_blks[CDUC_BLK];
542         total = cdu_iids.per_vf_cids * CONN_CXT_SIZE(p_hwfn);
543
544         ecore_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
545                                total, CONN_CXT_SIZE(p_hwfn));
546
547         ecore_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line, ILT_CLI_CDUC);
548         p_cli->vf_total_lines = curr_line - p_blk->start_line;
549
550         for (i = 1; i < p_mngr->vf_count; i++)
551                 ecore_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
552                                        ILT_CLI_CDUC);
553
554         /* CDUT PF */
555         p_cli = &p_mngr->clients[ILT_CLI_CDUT];
556         p_cli->first.val = curr_line;
557
558         /* first the 'working' task memory */
559         for (i = 0; i < NUM_TASK_PF_SEGMENTS; i++) {
560                 p_seg = ecore_cxt_tid_seg_info(p_hwfn, i);
561                 if (!p_seg || p_seg->count == 0)
562                         continue;
563
564                 p_blk = &p_cli->pf_blks[CDUT_SEG_BLK(i)];
565                 total = p_seg->count * p_mngr->task_type_size[p_seg->type];
566                 ecore_ilt_cli_blk_fill(p_cli, p_blk, curr_line, total,
567                                        p_mngr->task_type_size[p_seg->type]);
568
569                 ecore_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
570                                        ILT_CLI_CDUT);
571         }
572
573         /* next the 'init' task memory (forced load memory) */
574         for (i = 0; i < NUM_TASK_PF_SEGMENTS; i++) {
575                 p_seg = ecore_cxt_tid_seg_info(p_hwfn, i);
576                 if (!p_seg || p_seg->count == 0)
577                         continue;
578
579                 p_blk = &p_cli->pf_blks[CDUT_FL_SEG_BLK(i, PF)];
580
581                 if (!p_seg->has_fl_mem) {
582                         /* The segment is active (total size pf 'working'
583                          * memory is > 0) but has no FL (forced-load, Init)
584                          * memory. Thus:
585                          *
586                          * 1.   The total-size in the corrsponding FL block of
587                          *      the ILT client is set to 0 - No ILT line are
588                          *      provisioned and no ILT memory allocated.
589                          *
590                          * 2.   The start-line of said block is set to the
591                          *      start line of the matching working memory
592                          *      block in the ILT client. This is later used to
593                          *      configure the CDU segment offset registers and
594                          *      results in an FL command for TIDs of this
595                          *      segment behaves as regular load commands
596                          *      (loading TIDs from the working memory).
597                          */
598                         line = p_cli->pf_blks[CDUT_SEG_BLK(i)].start_line;
599
600                         ecore_ilt_cli_blk_fill(p_cli, p_blk, line, 0, 0);
601                         continue;
602                 }
603                 total = p_seg->count * p_mngr->task_type_size[p_seg->type];
604
605                 ecore_ilt_cli_blk_fill(p_cli, p_blk,
606                                        curr_line, total,
607                                        p_mngr->task_type_size[p_seg->type]);
608
609                 ecore_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
610                                        ILT_CLI_CDUT);
611         }
612         p_cli->pf_total_lines = curr_line - p_cli->pf_blks[0].start_line;
613
614         /* CDUT VF */
615         p_seg = ecore_cxt_tid_seg_info(p_hwfn, TASK_SEGMENT_VF);
616         if (p_seg && p_seg->count) {
617                 /* Stricly speaking we need to iterate over all VF
618                  * task segment types, but a VF has only 1 segment
619                  */
620
621                 /* 'working' memory */
622                 total = p_seg->count * p_mngr->task_type_size[p_seg->type];
623
624                 p_blk = &p_cli->vf_blks[CDUT_SEG_BLK(0)];
625                 ecore_ilt_cli_blk_fill(p_cli, p_blk,
626                                        curr_line, total,
627                                        p_mngr->task_type_size[p_seg->type]);
628
629                 ecore_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
630                                        ILT_CLI_CDUT);
631
632                 /* 'init' memory */
633                 p_blk = &p_cli->vf_blks[CDUT_FL_SEG_BLK(0, VF)];
634                 if (!p_seg->has_fl_mem) {
635                         /* see comment above */
636                         line = p_cli->vf_blks[CDUT_SEG_BLK(0)].start_line;
637                         ecore_ilt_cli_blk_fill(p_cli, p_blk, line, 0, 0);
638                 } else {
639                         task_size = p_mngr->task_type_size[p_seg->type];
640                         ecore_ilt_cli_blk_fill(p_cli, p_blk,
641                                                curr_line, total, task_size);
642                         ecore_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
643                                                ILT_CLI_CDUT);
644                 }
645                 p_cli->vf_total_lines = curr_line -
646                     p_cli->vf_blks[0].start_line;
647
648                 /* Now for the rest of the VFs */
649                 for (i = 1; i < p_mngr->vf_count; i++) {
650                         p_blk = &p_cli->vf_blks[CDUT_SEG_BLK(0)];
651                         ecore_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
652                                                ILT_CLI_CDUT);
653
654                         p_blk = &p_cli->vf_blks[CDUT_FL_SEG_BLK(0, VF)];
655                         ecore_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
656                                                ILT_CLI_CDUT);
657                 }
658         }
659
660         /* QM */
661         p_cli = &p_mngr->clients[ILT_CLI_QM];
662         p_blk = &p_cli->pf_blks[0];
663
664         ecore_cxt_qm_iids(p_hwfn, &qm_iids);
665         total = ecore_qm_pf_mem_size(p_hwfn->rel_pf_id, qm_iids.cids,
666                                      qm_iids.vf_cids, qm_iids.tids,
667                                      p_hwfn->qm_info.num_pqs,
668                                      p_hwfn->qm_info.num_vf_pqs);
669
670         DP_VERBOSE(p_hwfn, ECORE_MSG_ILT,
671                    "QM ILT Info, (cids=%d, vf_cids=%d, tids=%d, num_pqs=%d,"
672                    " num_vf_pqs=%d, memory_size=%d)\n",
673                    qm_iids.cids, qm_iids.vf_cids, qm_iids.tids,
674                    p_hwfn->qm_info.num_pqs, p_hwfn->qm_info.num_vf_pqs, total);
675
676         ecore_ilt_cli_blk_fill(p_cli, p_blk, curr_line, total * 0x1000,
677                                QM_PQ_ELEMENT_SIZE);
678
679         ecore_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line, ILT_CLI_QM);
680         p_cli->pf_total_lines = curr_line - p_blk->start_line;
681
682         /* SRC */
683         p_cli = &p_mngr->clients[ILT_CLI_SRC];
684         ecore_cxt_src_iids(p_mngr, &src_iids);
685
686         /* Both the PF and VFs searcher connections are stored in the per PF
687          * database. Thus sum the PF searcher cids and all the VFs searcher
688          * cids.
689          */
690         total = src_iids.pf_cids + src_iids.per_vf_cids * p_mngr->vf_count;
691         if (total) {
692                 u32 local_max = OSAL_MAX_T(u32, total,
693                                            SRC_MIN_NUM_ELEMS);
694
695                 total = OSAL_ROUNDUP_POW_OF_TWO(local_max);
696
697                 p_blk = &p_cli->pf_blks[0];
698                 ecore_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
699                                        total * sizeof(struct src_ent),
700                                        sizeof(struct src_ent));
701
702                 ecore_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
703                                        ILT_CLI_SRC);
704                 p_cli->pf_total_lines = curr_line - p_blk->start_line;
705         }
706
707         /* TM PF */
708         p_cli = &p_mngr->clients[ILT_CLI_TM];
709         ecore_cxt_tm_iids(p_mngr, &tm_iids);
710         total = tm_iids.pf_cids + tm_iids.pf_tids_total;
711         if (total) {
712                 p_blk = &p_cli->pf_blks[0];
713                 ecore_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
714                                        total * TM_ELEM_SIZE, TM_ELEM_SIZE);
715
716                 ecore_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
717                                        ILT_CLI_TM);
718                 p_cli->pf_total_lines = curr_line - p_blk->start_line;
719         }
720
721         /* TM VF */
722         total = tm_iids.per_vf_cids + tm_iids.per_vf_tids;
723         if (total) {
724                 p_blk = &p_cli->vf_blks[0];
725                 ecore_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
726                                        total * TM_ELEM_SIZE, TM_ELEM_SIZE);
727
728                 ecore_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
729                                        ILT_CLI_TM);
730
731                 p_cli->vf_total_lines = curr_line - p_blk->start_line;
732                 for (i = 1; i < p_mngr->vf_count; i++) {
733                         ecore_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
734                                                ILT_CLI_TM);
735                 }
736         }
737
738         /* TSDM (SRQ CONTEXT) */
739         total = ecore_cxt_get_srq_count(p_hwfn);
740
741         if (total) {
742                 p_cli = &p_mngr->clients[ILT_CLI_TSDM];
743                 p_blk = &p_cli->pf_blks[SRQ_BLK];
744                 ecore_ilt_cli_blk_fill(p_cli, p_blk, curr_line,
745                                        total * SRQ_CXT_SIZE, SRQ_CXT_SIZE);
746
747                 ecore_ilt_cli_adv_line(p_hwfn, p_cli, p_blk, &curr_line,
748                                        ILT_CLI_TSDM);
749                 p_cli->pf_total_lines = curr_line - p_blk->start_line;
750         }
751
752         if (curr_line - p_hwfn->p_cxt_mngr->pf_start_line >
753             RESC_NUM(p_hwfn, ECORE_ILT)) {
754                 DP_ERR(p_hwfn, "too many ilt lines...#lines=%d\n",
755                        curr_line - p_hwfn->p_cxt_mngr->pf_start_line);
756                 return ECORE_INVAL;
757         }
758
759         return ECORE_SUCCESS;
760 }
761
762 static void ecore_cxt_src_t2_free(struct ecore_hwfn *p_hwfn)
763 {
764         struct ecore_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
765         u32 i;
766
767         if (!p_mngr->t2)
768                 return;
769
770         for (i = 0; i < p_mngr->t2_num_pages; i++)
771                 if (p_mngr->t2[i].p_virt)
772                         OSAL_DMA_FREE_COHERENT(p_hwfn->p_dev,
773                                                p_mngr->t2[i].p_virt,
774                                                p_mngr->t2[i].p_phys,
775                                                p_mngr->t2[i].size);
776
777         OSAL_FREE(p_hwfn->p_dev, p_mngr->t2);
778 }
779
780 static enum _ecore_status_t ecore_cxt_src_t2_alloc(struct ecore_hwfn *p_hwfn)
781 {
782         struct ecore_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
783         u32 conn_num, total_size, ent_per_page, psz, i;
784         struct ecore_ilt_client_cfg *p_src;
785         struct ecore_src_iids src_iids;
786         struct ecore_dma_mem *p_t2;
787         enum _ecore_status_t rc;
788
789         OSAL_MEM_ZERO(&src_iids, sizeof(src_iids));
790
791         /* if the SRC ILT client is inactive - there are no connection
792          * requiring the searcer, leave.
793          */
794         p_src = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_SRC];
795         if (!p_src->active)
796                 return ECORE_SUCCESS;
797
798         ecore_cxt_src_iids(p_mngr, &src_iids);
799         conn_num = src_iids.pf_cids + src_iids.per_vf_cids * p_mngr->vf_count;
800         total_size = conn_num * sizeof(struct src_ent);
801
802         /* use the same page size as the SRC ILT client */
803         psz = ILT_PAGE_IN_BYTES(p_src->p_size.val);
804         p_mngr->t2_num_pages = DIV_ROUND_UP(total_size, psz);
805
806         /* allocate t2 */
807         p_mngr->t2 = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL,
808                                  p_mngr->t2_num_pages *
809                                  sizeof(struct ecore_dma_mem));
810         if (!p_mngr->t2) {
811                 DP_NOTICE(p_hwfn, true, "Failed to allocate t2 table\n");
812                 rc = ECORE_NOMEM;
813                 goto t2_fail;
814         }
815
816         /* allocate t2 pages */
817         for (i = 0; i < p_mngr->t2_num_pages; i++) {
818                 u32 size = OSAL_MIN_T(u32, total_size, psz);
819                 void **p_virt = &p_mngr->t2[i].p_virt;
820
821                 *p_virt = OSAL_DMA_ALLOC_COHERENT(p_hwfn->p_dev,
822                                                   &p_mngr->t2[i].p_phys, size);
823                 if (!p_mngr->t2[i].p_virt) {
824                         rc = ECORE_NOMEM;
825                         goto t2_fail;
826                 }
827                 OSAL_MEM_ZERO(*p_virt, size);
828                 p_mngr->t2[i].size = size;
829                 total_size -= size;
830         }
831
832         /* Set the t2 pointers */
833
834         /* entries per page - must be a power of two */
835         ent_per_page = psz / sizeof(struct src_ent);
836
837         p_mngr->first_free = (u64)p_mngr->t2[0].p_phys;
838
839         p_t2 = &p_mngr->t2[(conn_num - 1) / ent_per_page];
840         p_mngr->last_free = (u64)p_t2->p_phys +
841             ((conn_num - 1) & (ent_per_page - 1)) * sizeof(struct src_ent);
842
843         for (i = 0; i < p_mngr->t2_num_pages; i++) {
844                 u32 ent_num = OSAL_MIN_T(u32, ent_per_page, conn_num);
845                 struct src_ent *entries = p_mngr->t2[i].p_virt;
846                 u64 p_ent_phys = (u64)p_mngr->t2[i].p_phys, val;
847                 u32 j;
848
849                 for (j = 0; j < ent_num - 1; j++) {
850                         val = p_ent_phys + (j + 1) * sizeof(struct src_ent);
851                         entries[j].next = OSAL_CPU_TO_BE64(val);
852                 }
853
854                 if (i < p_mngr->t2_num_pages - 1)
855                         val = (u64)p_mngr->t2[i + 1].p_phys;
856                 else
857                         val = 0;
858                 entries[j].next = OSAL_CPU_TO_BE64(val);
859
860                 conn_num -= ent_num;
861         }
862
863         return ECORE_SUCCESS;
864
865 t2_fail:
866         ecore_cxt_src_t2_free(p_hwfn);
867         return rc;
868 }
869
870 #define for_each_ilt_valid_client(pos, clients)         \
871         for (pos = 0; pos < ILT_CLI_MAX; pos++)         \
872                 if (!clients[pos].active) {             \
873                         continue;                       \
874                 } else                                  \
875
876
877 /* Total number of ILT lines used by this PF */
878 static u32 ecore_cxt_ilt_shadow_size(struct ecore_ilt_client_cfg *ilt_clients)
879 {
880         u32 size = 0;
881         u32 i;
882
883         for_each_ilt_valid_client(i, ilt_clients)
884                 size += (ilt_clients[i].last.val -
885                          ilt_clients[i].first.val + 1);
886
887         return size;
888 }
889
890 static void ecore_ilt_shadow_free(struct ecore_hwfn *p_hwfn)
891 {
892         struct ecore_ilt_client_cfg *p_cli = p_hwfn->p_cxt_mngr->clients;
893         struct ecore_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
894         u32 ilt_size, i;
895
896         ilt_size = ecore_cxt_ilt_shadow_size(p_cli);
897
898         for (i = 0; p_mngr->ilt_shadow && i < ilt_size; i++) {
899                 struct ecore_dma_mem *p_dma = &p_mngr->ilt_shadow[i];
900
901                 if (p_dma->p_virt)
902                         OSAL_DMA_FREE_COHERENT(p_hwfn->p_dev,
903                                                p_dma->p_virt,
904                                                p_dma->p_phys, p_dma->size);
905                 p_dma->p_virt = OSAL_NULL;
906         }
907         OSAL_FREE(p_hwfn->p_dev, p_mngr->ilt_shadow);
908 }
909
910 static enum _ecore_status_t
911 ecore_ilt_blk_alloc(struct ecore_hwfn *p_hwfn,
912                     struct ecore_ilt_cli_blk *p_blk,
913                     enum ilt_clients ilt_client, u32 start_line_offset)
914 {
915         struct ecore_dma_mem *ilt_shadow = p_hwfn->p_cxt_mngr->ilt_shadow;
916         u32 lines, line, sz_left, lines_to_skip = 0;
917
918         /* Special handling for RoCE that supports dynamic allocation */
919         if (ilt_client == ILT_CLI_CDUT || ilt_client == ILT_CLI_TSDM)
920                 return ECORE_SUCCESS;
921
922         lines_to_skip = p_blk->dynamic_line_cnt;
923
924         if (!p_blk->total_size)
925                 return ECORE_SUCCESS;
926
927         sz_left = p_blk->total_size;
928         lines = DIV_ROUND_UP(sz_left, p_blk->real_size_in_page) - lines_to_skip;
929         line = p_blk->start_line + start_line_offset -
930             p_hwfn->p_cxt_mngr->pf_start_line + lines_to_skip;
931
932         for (; lines; lines--) {
933                 dma_addr_t p_phys;
934                 void *p_virt;
935                 u32 size;
936
937                 size = OSAL_MIN_T(u32, sz_left, p_blk->real_size_in_page);
938
939 /* @DPDK */
940 #define ILT_BLOCK_ALIGN_SIZE 0x1000
941                 p_virt = OSAL_DMA_ALLOC_COHERENT_ALIGNED(p_hwfn->p_dev,
942                                                          &p_phys, size,
943                                                          ILT_BLOCK_ALIGN_SIZE);
944                 if (!p_virt)
945                         return ECORE_NOMEM;
946                 OSAL_MEM_ZERO(p_virt, size);
947
948                 ilt_shadow[line].p_phys = p_phys;
949                 ilt_shadow[line].p_virt = p_virt;
950                 ilt_shadow[line].size = size;
951
952                 DP_VERBOSE(p_hwfn, ECORE_MSG_ILT,
953                            "ILT shadow: Line [%d] Physical 0x%lx"
954                            " Virtual %p Size %d\n",
955                            line, (unsigned long)p_phys, p_virt, size);
956
957                 sz_left -= size;
958                 line++;
959         }
960
961         return ECORE_SUCCESS;
962 }
963
964 static enum _ecore_status_t ecore_ilt_shadow_alloc(struct ecore_hwfn *p_hwfn)
965 {
966         struct ecore_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
967         struct ecore_ilt_client_cfg *clients = p_mngr->clients;
968         struct ecore_ilt_cli_blk *p_blk;
969         u32 size, i, j, k;
970         enum _ecore_status_t rc;
971
972         size = ecore_cxt_ilt_shadow_size(clients);
973         p_mngr->ilt_shadow = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL,
974                                          size * sizeof(struct ecore_dma_mem));
975
976         if (!p_mngr->ilt_shadow) {
977                 DP_NOTICE(p_hwfn, true,
978                           "Failed to allocate ilt shadow table\n");
979                 rc = ECORE_NOMEM;
980                 goto ilt_shadow_fail;
981         }
982
983         DP_VERBOSE(p_hwfn, ECORE_MSG_ILT,
984                    "Allocated 0x%x bytes for ilt shadow\n",
985                    (u32)(size * sizeof(struct ecore_dma_mem)));
986
987         for_each_ilt_valid_client(i, clients) {
988                 for (j = 0; j < ILT_CLI_PF_BLOCKS; j++) {
989                         p_blk = &clients[i].pf_blks[j];
990                         rc = ecore_ilt_blk_alloc(p_hwfn, p_blk, i, 0);
991                         if (rc != ECORE_SUCCESS)
992                                 goto ilt_shadow_fail;
993                 }
994                 for (k = 0; k < p_mngr->vf_count; k++) {
995                         for (j = 0; j < ILT_CLI_VF_BLOCKS; j++) {
996                                 u32 lines = clients[i].vf_total_lines * k;
997
998                                 p_blk = &clients[i].vf_blks[j];
999                                 rc = ecore_ilt_blk_alloc(p_hwfn, p_blk,
1000                                                          i, lines);
1001                                 if (rc != ECORE_SUCCESS)
1002                                         goto ilt_shadow_fail;
1003                         }
1004                 }
1005         }
1006
1007         return ECORE_SUCCESS;
1008
1009 ilt_shadow_fail:
1010         ecore_ilt_shadow_free(p_hwfn);
1011         return rc;
1012 }
1013
1014 static void ecore_cid_map_free(struct ecore_hwfn *p_hwfn)
1015 {
1016         struct ecore_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1017         u32 type;
1018
1019         for (type = 0; type < MAX_CONN_TYPES; type++) {
1020                 OSAL_FREE(p_hwfn->p_dev, p_mngr->acquired[type].cid_map);
1021                 p_mngr->acquired[type].max_count = 0;
1022                 p_mngr->acquired[type].start_cid = 0;
1023         }
1024 }
1025
1026 static enum _ecore_status_t ecore_cid_map_alloc(struct ecore_hwfn *p_hwfn)
1027 {
1028         struct ecore_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1029         u32 start_cid = 0;
1030         u32 type;
1031
1032         for (type = 0; type < MAX_CONN_TYPES; type++) {
1033                 u32 cid_cnt = p_hwfn->p_cxt_mngr->conn_cfg[type].cid_count;
1034                 u32 size;
1035
1036                 if (cid_cnt == 0)
1037                         continue;
1038
1039                 size = MAP_WORD_SIZE * DIV_ROUND_UP(cid_cnt, BITS_PER_MAP_WORD);
1040                 p_mngr->acquired[type].cid_map = OSAL_ZALLOC(p_hwfn->p_dev,
1041                                                              GFP_KERNEL, size);
1042                 if (!p_mngr->acquired[type].cid_map)
1043                         goto cid_map_fail;
1044
1045                 p_mngr->acquired[type].max_count = cid_cnt;
1046                 p_mngr->acquired[type].start_cid = start_cid;
1047
1048                 p_hwfn->p_cxt_mngr->conn_cfg[type].cid_start = start_cid;
1049
1050                 DP_VERBOSE(p_hwfn, ECORE_MSG_CXT,
1051                            "Type %08x start: %08x count %08x\n",
1052                            type, p_mngr->acquired[type].start_cid,
1053                            p_mngr->acquired[type].max_count);
1054                 start_cid += cid_cnt;
1055         }
1056
1057         return ECORE_SUCCESS;
1058
1059 cid_map_fail:
1060         ecore_cid_map_free(p_hwfn);
1061         return ECORE_NOMEM;
1062 }
1063
1064 enum _ecore_status_t ecore_cxt_mngr_alloc(struct ecore_hwfn *p_hwfn)
1065 {
1066         struct ecore_ilt_client_cfg *clients;
1067         struct ecore_cxt_mngr *p_mngr;
1068         u32 i;
1069
1070         p_mngr = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL, sizeof(*p_mngr));
1071         if (!p_mngr) {
1072                 DP_NOTICE(p_hwfn, true,
1073                           "Failed to allocate `struct ecore_cxt_mngr'\n");
1074                 return ECORE_NOMEM;
1075         }
1076
1077         /* Initialize ILT client registers */
1078         clients = p_mngr->clients;
1079         clients[ILT_CLI_CDUC].first.reg = ILT_CFG_REG(CDUC, FIRST_ILT);
1080         clients[ILT_CLI_CDUC].last.reg  = ILT_CFG_REG(CDUC, LAST_ILT);
1081         clients[ILT_CLI_CDUC].p_size.reg = ILT_CFG_REG(CDUC, P_SIZE);
1082
1083         clients[ILT_CLI_QM].first.reg   = ILT_CFG_REG(QM, FIRST_ILT);
1084         clients[ILT_CLI_QM].last.reg    = ILT_CFG_REG(QM, LAST_ILT);
1085         clients[ILT_CLI_QM].p_size.reg  = ILT_CFG_REG(QM, P_SIZE);
1086
1087         clients[ILT_CLI_TM].first.reg   = ILT_CFG_REG(TM, FIRST_ILT);
1088         clients[ILT_CLI_TM].last.reg    = ILT_CFG_REG(TM, LAST_ILT);
1089         clients[ILT_CLI_TM].p_size.reg  = ILT_CFG_REG(TM, P_SIZE);
1090
1091         clients[ILT_CLI_SRC].first.reg  = ILT_CFG_REG(SRC, FIRST_ILT);
1092         clients[ILT_CLI_SRC].last.reg   = ILT_CFG_REG(SRC, LAST_ILT);
1093         clients[ILT_CLI_SRC].p_size.reg = ILT_CFG_REG(SRC, P_SIZE);
1094
1095         clients[ILT_CLI_CDUT].first.reg = ILT_CFG_REG(CDUT, FIRST_ILT);
1096         clients[ILT_CLI_CDUT].last.reg  = ILT_CFG_REG(CDUT, LAST_ILT);
1097         clients[ILT_CLI_CDUT].p_size.reg = ILT_CFG_REG(CDUT, P_SIZE);
1098
1099         clients[ILT_CLI_TSDM].first.reg = ILT_CFG_REG(TSDM, FIRST_ILT);
1100         clients[ILT_CLI_TSDM].last.reg  = ILT_CFG_REG(TSDM, LAST_ILT);
1101         clients[ILT_CLI_TSDM].p_size.reg = ILT_CFG_REG(TSDM, P_SIZE);
1102
1103         /* default ILT page size for all clients is 32K */
1104         for (i = 0; i < ILT_CLI_MAX; i++)
1105                 p_mngr->clients[i].p_size.val = ILT_DEFAULT_HW_P_SIZE;
1106
1107         /* due to removal of ISCSI/FCoE files union type0_task_context
1108          * task_type_size will be 0. So hardcoded for now.
1109          */
1110         p_mngr->task_type_size[0] = 512; /* @DPDK */
1111         p_mngr->task_type_size[1] = 128; /* @DPDK */
1112
1113         if (p_hwfn->p_dev->p_iov_info)
1114                 p_mngr->vf_count = p_hwfn->p_dev->p_iov_info->total_vfs;
1115
1116         /* Initialize the dynamic ILT allocation mutex */
1117         OSAL_MUTEX_ALLOC(p_hwfn, &p_mngr->mutex);
1118         OSAL_MUTEX_INIT(&p_mngr->mutex);
1119
1120         /* Set the cxt mangr pointer priori to further allocations */
1121         p_hwfn->p_cxt_mngr = p_mngr;
1122
1123         return ECORE_SUCCESS;
1124 }
1125
1126 enum _ecore_status_t ecore_cxt_tables_alloc(struct ecore_hwfn *p_hwfn)
1127 {
1128         enum _ecore_status_t rc;
1129
1130         /* Allocate the ILT shadow table */
1131         rc = ecore_ilt_shadow_alloc(p_hwfn);
1132         if (rc) {
1133                 DP_NOTICE(p_hwfn, true, "Failed to allocate ilt memory\n");
1134                 goto tables_alloc_fail;
1135         }
1136
1137         /* Allocate the T2  table */
1138         rc = ecore_cxt_src_t2_alloc(p_hwfn);
1139         if (rc) {
1140                 DP_NOTICE(p_hwfn, true, "Failed to allocate T2 memory\n");
1141                 goto tables_alloc_fail;
1142         }
1143
1144         /* Allocate and initialize the acquired cids bitmaps */
1145         rc = ecore_cid_map_alloc(p_hwfn);
1146         if (rc) {
1147                 DP_NOTICE(p_hwfn, true, "Failed to allocate cid maps\n");
1148                 goto tables_alloc_fail;
1149         }
1150
1151         return ECORE_SUCCESS;
1152
1153 tables_alloc_fail:
1154         ecore_cxt_mngr_free(p_hwfn);
1155         return rc;
1156 }
1157
1158 void ecore_cxt_mngr_free(struct ecore_hwfn *p_hwfn)
1159 {
1160         if (!p_hwfn->p_cxt_mngr)
1161                 return;
1162
1163         ecore_cid_map_free(p_hwfn);
1164         ecore_cxt_src_t2_free(p_hwfn);
1165         ecore_ilt_shadow_free(p_hwfn);
1166         OSAL_MUTEX_DEALLOC(&p_hwfn->p_cxt_mngr->mutex);
1167         OSAL_FREE(p_hwfn->p_dev, p_hwfn->p_cxt_mngr);
1168 }
1169
1170 void ecore_cxt_mngr_setup(struct ecore_hwfn *p_hwfn)
1171 {
1172         struct ecore_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1173         int type;
1174
1175         /* Reset acquired cids */
1176         for (type = 0; type < MAX_CONN_TYPES; type++) {
1177                 u32 cid_cnt = p_hwfn->p_cxt_mngr->conn_cfg[type].cid_count;
1178                 u32 i;
1179
1180                 if (cid_cnt == 0)
1181                         continue;
1182
1183                 for (i = 0; i < DIV_ROUND_UP(cid_cnt, BITS_PER_MAP_WORD); i++)
1184                         p_mngr->acquired[type].cid_map[i] = 0;
1185         }
1186 }
1187
1188 /* HW initialization helper (per Block, per phase) */
1189
1190 /* CDU Common */
1191 #define CDUC_CXT_SIZE_SHIFT                                             \
1192         CDU_REG_CID_ADDR_PARAMS_CONTEXT_SIZE_SHIFT
1193
1194 #define CDUC_CXT_SIZE_MASK                                              \
1195         (CDU_REG_CID_ADDR_PARAMS_CONTEXT_SIZE >> CDUC_CXT_SIZE_SHIFT)
1196
1197 #define CDUC_BLOCK_WASTE_SHIFT                                          \
1198         CDU_REG_CID_ADDR_PARAMS_BLOCK_WASTE_SHIFT
1199
1200 #define CDUC_BLOCK_WASTE_MASK                                           \
1201         (CDU_REG_CID_ADDR_PARAMS_BLOCK_WASTE >> CDUC_BLOCK_WASTE_SHIFT)
1202
1203 #define CDUC_NCIB_SHIFT                                                 \
1204         CDU_REG_CID_ADDR_PARAMS_NCIB_SHIFT
1205
1206 #define CDUC_NCIB_MASK                                                  \
1207         (CDU_REG_CID_ADDR_PARAMS_NCIB >> CDUC_NCIB_SHIFT)
1208
1209 #define CDUT_TYPE0_CXT_SIZE_SHIFT                                       \
1210         CDU_REG_SEGMENT0_PARAMS_T0_TID_SIZE_SHIFT
1211
1212 #define CDUT_TYPE0_CXT_SIZE_MASK                                        \
1213         (CDU_REG_SEGMENT0_PARAMS_T0_TID_SIZE >>                         \
1214         CDUT_TYPE0_CXT_SIZE_SHIFT)
1215
1216 #define CDUT_TYPE0_BLOCK_WASTE_SHIFT                                    \
1217         CDU_REG_SEGMENT0_PARAMS_T0_TID_BLOCK_WASTE_SHIFT
1218
1219 #define CDUT_TYPE0_BLOCK_WASTE_MASK                                     \
1220         (CDU_REG_SEGMENT0_PARAMS_T0_TID_BLOCK_WASTE >>                  \
1221         CDUT_TYPE0_BLOCK_WASTE_SHIFT)
1222
1223 #define CDUT_TYPE0_NCIB_SHIFT                                           \
1224         CDU_REG_SEGMENT0_PARAMS_T0_NUM_TIDS_IN_BLOCK_SHIFT
1225
1226 #define CDUT_TYPE0_NCIB_MASK                                            \
1227         (CDU_REG_SEGMENT0_PARAMS_T0_NUM_TIDS_IN_BLOCK >>                \
1228         CDUT_TYPE0_NCIB_SHIFT)
1229
1230 #define CDUT_TYPE1_CXT_SIZE_SHIFT                                       \
1231         CDU_REG_SEGMENT1_PARAMS_T1_TID_SIZE_SHIFT
1232
1233 #define CDUT_TYPE1_CXT_SIZE_MASK                                        \
1234         (CDU_REG_SEGMENT1_PARAMS_T1_TID_SIZE >>                         \
1235         CDUT_TYPE1_CXT_SIZE_SHIFT)
1236
1237 #define CDUT_TYPE1_BLOCK_WASTE_SHIFT                                    \
1238         CDU_REG_SEGMENT1_PARAMS_T1_TID_BLOCK_WASTE_SHIFT
1239
1240 #define CDUT_TYPE1_BLOCK_WASTE_MASK                                     \
1241         (CDU_REG_SEGMENT1_PARAMS_T1_TID_BLOCK_WASTE >>                  \
1242         CDUT_TYPE1_BLOCK_WASTE_SHIFT)
1243
1244 #define CDUT_TYPE1_NCIB_SHIFT                                           \
1245         CDU_REG_SEGMENT1_PARAMS_T1_NUM_TIDS_IN_BLOCK_SHIFT
1246
1247 #define CDUT_TYPE1_NCIB_MASK                                            \
1248         (CDU_REG_SEGMENT1_PARAMS_T1_NUM_TIDS_IN_BLOCK >>                \
1249         CDUT_TYPE1_NCIB_SHIFT)
1250
1251 static void ecore_cdu_init_common(struct ecore_hwfn *p_hwfn)
1252 {
1253         u32 page_sz, elems_per_page, block_waste, cxt_size, cdu_params = 0;
1254
1255         /* CDUC - connection configuration */
1256         page_sz = p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC].p_size.val;
1257         cxt_size = CONN_CXT_SIZE(p_hwfn);
1258         elems_per_page = ILT_PAGE_IN_BYTES(page_sz) / cxt_size;
1259         block_waste = ILT_PAGE_IN_BYTES(page_sz) - elems_per_page * cxt_size;
1260
1261         SET_FIELD(cdu_params, CDUC_CXT_SIZE, cxt_size);
1262         SET_FIELD(cdu_params, CDUC_BLOCK_WASTE, block_waste);
1263         SET_FIELD(cdu_params, CDUC_NCIB, elems_per_page);
1264         STORE_RT_REG(p_hwfn, CDU_REG_CID_ADDR_PARAMS_RT_OFFSET, cdu_params);
1265
1266         /* CDUT - type-0 tasks configuration */
1267         page_sz = p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT].p_size.val;
1268         cxt_size = p_hwfn->p_cxt_mngr->task_type_size[0];
1269         elems_per_page = ILT_PAGE_IN_BYTES(page_sz) / cxt_size;
1270         block_waste = ILT_PAGE_IN_BYTES(page_sz) - elems_per_page * cxt_size;
1271
1272         /* cxt size and block-waste are multipes of 8 */
1273         cdu_params = 0;
1274         SET_FIELD(cdu_params, CDUT_TYPE0_CXT_SIZE, (cxt_size >> 3));
1275         SET_FIELD(cdu_params, CDUT_TYPE0_BLOCK_WASTE, (block_waste >> 3));
1276         SET_FIELD(cdu_params, CDUT_TYPE0_NCIB, elems_per_page);
1277         STORE_RT_REG(p_hwfn, CDU_REG_SEGMENT0_PARAMS_RT_OFFSET, cdu_params);
1278
1279         /* CDUT - type-1 tasks configuration */
1280         cxt_size = p_hwfn->p_cxt_mngr->task_type_size[1];
1281         elems_per_page = ILT_PAGE_IN_BYTES(page_sz) / cxt_size;
1282         block_waste = ILT_PAGE_IN_BYTES(page_sz) - elems_per_page * cxt_size;
1283
1284         /* cxt size and block-waste are multipes of 8 */
1285         cdu_params = 0;
1286         SET_FIELD(cdu_params, CDUT_TYPE1_CXT_SIZE, (cxt_size >> 3));
1287         SET_FIELD(cdu_params, CDUT_TYPE1_BLOCK_WASTE, (block_waste >> 3));
1288         SET_FIELD(cdu_params, CDUT_TYPE1_NCIB, elems_per_page);
1289         STORE_RT_REG(p_hwfn, CDU_REG_SEGMENT1_PARAMS_RT_OFFSET, cdu_params);
1290 }
1291
1292 /* CDU PF */
1293 #define CDU_SEG_REG_TYPE_SHIFT          CDU_SEG_TYPE_OFFSET_REG_TYPE_SHIFT
1294 #define CDU_SEG_REG_TYPE_MASK           0x1
1295 #define CDU_SEG_REG_OFFSET_SHIFT        0
1296 #define CDU_SEG_REG_OFFSET_MASK         CDU_SEG_TYPE_OFFSET_REG_OFFSET_MASK
1297
1298 static void ecore_cdu_init_pf(struct ecore_hwfn *p_hwfn)
1299 {
1300         struct ecore_ilt_client_cfg *p_cli;
1301         struct ecore_tid_seg *p_seg;
1302         u32 cdu_seg_params, offset;
1303         int i;
1304
1305         static const u32 rt_type_offset_arr[] = {
1306                 CDU_REG_PF_SEG0_TYPE_OFFSET_RT_OFFSET,
1307                 CDU_REG_PF_SEG1_TYPE_OFFSET_RT_OFFSET,
1308                 CDU_REG_PF_SEG2_TYPE_OFFSET_RT_OFFSET,
1309                 CDU_REG_PF_SEG3_TYPE_OFFSET_RT_OFFSET
1310         };
1311
1312         static const u32 rt_type_offset_fl_arr[] = {
1313                 CDU_REG_PF_FL_SEG0_TYPE_OFFSET_RT_OFFSET,
1314                 CDU_REG_PF_FL_SEG1_TYPE_OFFSET_RT_OFFSET,
1315                 CDU_REG_PF_FL_SEG2_TYPE_OFFSET_RT_OFFSET,
1316                 CDU_REG_PF_FL_SEG3_TYPE_OFFSET_RT_OFFSET
1317         };
1318
1319         p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT];
1320
1321         /* There are initializations only for CDUT during pf Phase */
1322         for (i = 0; i < NUM_TASK_PF_SEGMENTS; i++) {
1323                 /* Segment 0 */
1324                 p_seg = ecore_cxt_tid_seg_info(p_hwfn, i);
1325                 if (!p_seg)
1326                         continue;
1327
1328                 /* Note: start_line is already adjusted for the CDU
1329                  * segment register granularity, so we just need to
1330                  * divide. Adjustment is implicit as we assume ILT
1331                  * Page size is larger than 32K!
1332                  */
1333                 offset = (ILT_PAGE_IN_BYTES(p_cli->p_size.val) *
1334                           (p_cli->pf_blks[CDUT_SEG_BLK(i)].start_line -
1335                            p_cli->first.val)) / CDUT_SEG_ALIGNMET_IN_BYTES;
1336
1337                 cdu_seg_params = 0;
1338                 SET_FIELD(cdu_seg_params, CDU_SEG_REG_TYPE, p_seg->type);
1339                 SET_FIELD(cdu_seg_params, CDU_SEG_REG_OFFSET, offset);
1340                 STORE_RT_REG(p_hwfn, rt_type_offset_arr[i], cdu_seg_params);
1341
1342                 offset = (ILT_PAGE_IN_BYTES(p_cli->p_size.val) *
1343                           (p_cli->pf_blks[CDUT_FL_SEG_BLK(i, PF)].start_line -
1344                            p_cli->first.val)) / CDUT_SEG_ALIGNMET_IN_BYTES;
1345
1346                 cdu_seg_params = 0;
1347                 SET_FIELD(cdu_seg_params, CDU_SEG_REG_TYPE, p_seg->type);
1348                 SET_FIELD(cdu_seg_params, CDU_SEG_REG_OFFSET, offset);
1349                 STORE_RT_REG(p_hwfn, rt_type_offset_fl_arr[i], cdu_seg_params);
1350         }
1351 }
1352
1353 void ecore_qm_init_pf(struct ecore_hwfn *p_hwfn)
1354 {
1355         struct ecore_qm_info *qm_info = &p_hwfn->qm_info;
1356         struct ecore_qm_iids iids;
1357
1358         OSAL_MEM_ZERO(&iids, sizeof(iids));
1359         ecore_cxt_qm_iids(p_hwfn, &iids);
1360
1361         ecore_qm_pf_rt_init(p_hwfn, p_hwfn->p_main_ptt, p_hwfn->port_id,
1362                             p_hwfn->rel_pf_id, qm_info->max_phys_tcs_per_port,
1363                             p_hwfn->first_on_engine,
1364                             iids.cids, iids.vf_cids, iids.tids,
1365                             qm_info->start_pq,
1366                             qm_info->num_pqs - qm_info->num_vf_pqs,
1367                             qm_info->num_vf_pqs,
1368                             qm_info->start_vport,
1369                             qm_info->num_vports, qm_info->pf_wfq,
1370                             qm_info->pf_rl, p_hwfn->qm_info.qm_pq_params,
1371                             p_hwfn->qm_info.qm_vport_params);
1372 }
1373
1374 /* CM PF */
1375 static enum _ecore_status_t ecore_cm_init_pf(struct ecore_hwfn *p_hwfn)
1376 {
1377         union ecore_qm_pq_params pq_params;
1378         u16 pq;
1379
1380         /* XCM pure-LB queue */
1381         OSAL_MEMSET(&pq_params, 0, sizeof(pq_params));
1382         pq_params.core.tc = LB_TC;
1383         pq = ecore_get_qm_pq(p_hwfn, PROTOCOLID_CORE, &pq_params);
1384         STORE_RT_REG(p_hwfn, XCM_REG_CON_PHY_Q3_RT_OFFSET, pq);
1385
1386         return ECORE_SUCCESS;
1387 }
1388
1389 /* DQ PF */
1390 static void ecore_dq_init_pf(struct ecore_hwfn *p_hwfn)
1391 {
1392         struct ecore_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1393         u32 dq_pf_max_cid = 0, dq_vf_max_cid = 0;
1394
1395         dq_pf_max_cid += (p_mngr->conn_cfg[0].cid_count >> DQ_RANGE_SHIFT);
1396         STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_0_RT_OFFSET, dq_pf_max_cid);
1397
1398         dq_vf_max_cid += (p_mngr->conn_cfg[0].cids_per_vf >> DQ_RANGE_SHIFT);
1399         STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_0_RT_OFFSET, dq_vf_max_cid);
1400
1401         dq_pf_max_cid += (p_mngr->conn_cfg[1].cid_count >> DQ_RANGE_SHIFT);
1402         STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_1_RT_OFFSET, dq_pf_max_cid);
1403
1404         dq_vf_max_cid += (p_mngr->conn_cfg[1].cids_per_vf >> DQ_RANGE_SHIFT);
1405         STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_1_RT_OFFSET, dq_vf_max_cid);
1406
1407         dq_pf_max_cid += (p_mngr->conn_cfg[2].cid_count >> DQ_RANGE_SHIFT);
1408         STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_2_RT_OFFSET, dq_pf_max_cid);
1409
1410         dq_vf_max_cid += (p_mngr->conn_cfg[2].cids_per_vf >> DQ_RANGE_SHIFT);
1411         STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_2_RT_OFFSET, dq_vf_max_cid);
1412
1413         dq_pf_max_cid += (p_mngr->conn_cfg[3].cid_count >> DQ_RANGE_SHIFT);
1414         STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_3_RT_OFFSET, dq_pf_max_cid);
1415
1416         dq_vf_max_cid += (p_mngr->conn_cfg[3].cids_per_vf >> DQ_RANGE_SHIFT);
1417         STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_3_RT_OFFSET, dq_vf_max_cid);
1418
1419         dq_pf_max_cid += (p_mngr->conn_cfg[4].cid_count >> DQ_RANGE_SHIFT);
1420         STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_4_RT_OFFSET, dq_pf_max_cid);
1421
1422         dq_vf_max_cid += (p_mngr->conn_cfg[4].cids_per_vf >> DQ_RANGE_SHIFT);
1423         STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_4_RT_OFFSET, dq_vf_max_cid);
1424
1425         dq_pf_max_cid += (p_mngr->conn_cfg[5].cid_count >> DQ_RANGE_SHIFT);
1426         STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_5_RT_OFFSET, dq_pf_max_cid);
1427
1428         dq_vf_max_cid += (p_mngr->conn_cfg[5].cids_per_vf >> DQ_RANGE_SHIFT);
1429         STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_5_RT_OFFSET, dq_vf_max_cid);
1430
1431         /* Connection types 6 & 7 are not in use, yet they must be configured
1432          * as the highest possible connection. Not configuring them means the
1433          * defaults will be  used, and with a large number of cids a bug may
1434          * occur, if the defaults will be smaller than dq_pf_max_cid /
1435          * dq_vf_max_cid.
1436          */
1437         STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_6_RT_OFFSET, dq_pf_max_cid);
1438         STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_6_RT_OFFSET, dq_vf_max_cid);
1439
1440         STORE_RT_REG(p_hwfn, DORQ_REG_PF_MAX_ICID_7_RT_OFFSET, dq_pf_max_cid);
1441         STORE_RT_REG(p_hwfn, DORQ_REG_VF_MAX_ICID_7_RT_OFFSET, dq_vf_max_cid);
1442 }
1443
1444 static void ecore_ilt_bounds_init(struct ecore_hwfn *p_hwfn)
1445 {
1446         struct ecore_ilt_client_cfg *ilt_clients;
1447         int i;
1448
1449         ilt_clients = p_hwfn->p_cxt_mngr->clients;
1450         for_each_ilt_valid_client(i, ilt_clients) {
1451                 STORE_RT_REG(p_hwfn,
1452                              ilt_clients[i].first.reg,
1453                              ilt_clients[i].first.val);
1454                 STORE_RT_REG(p_hwfn,
1455                              ilt_clients[i].last.reg, ilt_clients[i].last.val);
1456                 STORE_RT_REG(p_hwfn,
1457                              ilt_clients[i].p_size.reg,
1458                              ilt_clients[i].p_size.val);
1459         }
1460 }
1461
1462 static void ecore_ilt_vf_bounds_init(struct ecore_hwfn *p_hwfn)
1463 {
1464         struct ecore_ilt_client_cfg *p_cli;
1465         u32 blk_factor;
1466
1467         /* For simplicty  we set the 'block' to be an ILT page */
1468         if (p_hwfn->p_dev->p_iov_info) {
1469                 struct ecore_hw_sriov_info *p_iov = p_hwfn->p_dev->p_iov_info;
1470
1471                 STORE_RT_REG(p_hwfn,
1472                              PSWRQ2_REG_VF_BASE_RT_OFFSET,
1473                              p_iov->first_vf_in_pf);
1474                 STORE_RT_REG(p_hwfn,
1475                              PSWRQ2_REG_VF_LAST_ILT_RT_OFFSET,
1476                              p_iov->first_vf_in_pf + p_iov->total_vfs);
1477         }
1478
1479         p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC];
1480         blk_factor = OSAL_LOG2(ILT_PAGE_IN_BYTES(p_cli->p_size.val) >> 10);
1481         if (p_cli->active) {
1482                 STORE_RT_REG(p_hwfn,
1483                              PSWRQ2_REG_CDUC_BLOCKS_FACTOR_RT_OFFSET,
1484                              blk_factor);
1485                 STORE_RT_REG(p_hwfn,
1486                              PSWRQ2_REG_CDUC_NUMBER_OF_PF_BLOCKS_RT_OFFSET,
1487                              p_cli->pf_total_lines);
1488                 STORE_RT_REG(p_hwfn,
1489                              PSWRQ2_REG_CDUC_VF_BLOCKS_RT_OFFSET,
1490                              p_cli->vf_total_lines);
1491         }
1492
1493         p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT];
1494         blk_factor = OSAL_LOG2(ILT_PAGE_IN_BYTES(p_cli->p_size.val) >> 10);
1495         if (p_cli->active) {
1496                 STORE_RT_REG(p_hwfn,
1497                              PSWRQ2_REG_CDUT_BLOCKS_FACTOR_RT_OFFSET,
1498                              blk_factor);
1499                 STORE_RT_REG(p_hwfn,
1500                              PSWRQ2_REG_CDUT_NUMBER_OF_PF_BLOCKS_RT_OFFSET,
1501                              p_cli->pf_total_lines);
1502                 STORE_RT_REG(p_hwfn,
1503                              PSWRQ2_REG_CDUT_VF_BLOCKS_RT_OFFSET,
1504                              p_cli->vf_total_lines);
1505         }
1506
1507         p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_TM];
1508         blk_factor = OSAL_LOG2(ILT_PAGE_IN_BYTES(p_cli->p_size.val) >> 10);
1509         if (p_cli->active) {
1510                 STORE_RT_REG(p_hwfn,
1511                              PSWRQ2_REG_TM_BLOCKS_FACTOR_RT_OFFSET, blk_factor);
1512                 STORE_RT_REG(p_hwfn,
1513                              PSWRQ2_REG_TM_NUMBER_OF_PF_BLOCKS_RT_OFFSET,
1514                              p_cli->pf_total_lines);
1515                 STORE_RT_REG(p_hwfn,
1516                              PSWRQ2_REG_TM_VF_BLOCKS_RT_OFFSET,
1517                              p_cli->vf_total_lines);
1518         }
1519 }
1520
1521 /* ILT (PSWRQ2) PF */
1522 static void ecore_ilt_init_pf(struct ecore_hwfn *p_hwfn)
1523 {
1524         struct ecore_ilt_client_cfg *clients;
1525         struct ecore_cxt_mngr *p_mngr;
1526         struct ecore_dma_mem *p_shdw;
1527         u32 line, rt_offst, i;
1528
1529         ecore_ilt_bounds_init(p_hwfn);
1530         ecore_ilt_vf_bounds_init(p_hwfn);
1531
1532         p_mngr = p_hwfn->p_cxt_mngr;
1533         p_shdw = p_mngr->ilt_shadow;
1534         clients = p_hwfn->p_cxt_mngr->clients;
1535
1536         for_each_ilt_valid_client(i, clients) {
1537                 /* Client's 1st val and RT array are absolute, ILT shadows'
1538                  * lines are relative.
1539                  */
1540                 line = clients[i].first.val - p_mngr->pf_start_line;
1541                 rt_offst = PSWRQ2_REG_ILT_MEMORY_RT_OFFSET +
1542                     clients[i].first.val * ILT_ENTRY_IN_REGS;
1543
1544                 for (; line <= clients[i].last.val - p_mngr->pf_start_line;
1545                      line++, rt_offst += ILT_ENTRY_IN_REGS) {
1546                         u64 ilt_hw_entry = 0;
1547
1548                         /** p_virt could be OSAL_NULL incase of dynamic
1549                          *  allocation
1550                          */
1551                         if (p_shdw[line].p_virt != OSAL_NULL) {
1552                                 SET_FIELD(ilt_hw_entry, ILT_ENTRY_VALID, 1ULL);
1553                                 SET_FIELD(ilt_hw_entry, ILT_ENTRY_PHY_ADDR,
1554                                           (p_shdw[line].p_phys >> 12));
1555
1556                                 DP_VERBOSE(p_hwfn, ECORE_MSG_ILT,
1557                                         "Setting RT[0x%08x] from"
1558                                         " ILT[0x%08x] [Client is %d] to"
1559                                         " Physical addr: 0x%lx\n",
1560                                         rt_offst, line, i,
1561                                         (unsigned long)(p_shdw[line].
1562                                                         p_phys >> 12));
1563                         }
1564
1565                         STORE_RT_REG_AGG(p_hwfn, rt_offst, ilt_hw_entry);
1566                 }
1567         }
1568 }
1569
1570 /* SRC (Searcher) PF */
1571 static void ecore_src_init_pf(struct ecore_hwfn *p_hwfn)
1572 {
1573         struct ecore_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1574         u32 rounded_conn_num, conn_num, conn_max;
1575         struct ecore_src_iids src_iids;
1576
1577         OSAL_MEM_ZERO(&src_iids, sizeof(src_iids));
1578         ecore_cxt_src_iids(p_mngr, &src_iids);
1579         conn_num = src_iids.pf_cids + src_iids.per_vf_cids * p_mngr->vf_count;
1580         if (!conn_num)
1581                 return;
1582
1583         conn_max = OSAL_MAX_T(u32, conn_num, SRC_MIN_NUM_ELEMS);
1584         rounded_conn_num = OSAL_ROUNDUP_POW_OF_TWO(conn_max);
1585
1586         STORE_RT_REG(p_hwfn, SRC_REG_COUNTFREE_RT_OFFSET, conn_num);
1587         STORE_RT_REG(p_hwfn, SRC_REG_NUMBER_HASH_BITS_RT_OFFSET,
1588                      OSAL_LOG2(rounded_conn_num));
1589
1590         STORE_RT_REG_AGG(p_hwfn, SRC_REG_FIRSTFREE_RT_OFFSET,
1591                          p_hwfn->p_cxt_mngr->first_free);
1592         STORE_RT_REG_AGG(p_hwfn, SRC_REG_LASTFREE_RT_OFFSET,
1593                          p_hwfn->p_cxt_mngr->last_free);
1594 }
1595
1596 /* Timers PF */
1597 #define TM_CFG_NUM_IDS_SHIFT            0
1598 #define TM_CFG_NUM_IDS_MASK             0xFFFFULL
1599 #define TM_CFG_PRE_SCAN_OFFSET_SHIFT    16
1600 #define TM_CFG_PRE_SCAN_OFFSET_MASK     0x1FFULL
1601 #define TM_CFG_PARENT_PF_SHIFT          25
1602 #define TM_CFG_PARENT_PF_MASK           0x7ULL
1603
1604 #define TM_CFG_CID_PRE_SCAN_ROWS_SHIFT  30
1605 #define TM_CFG_CID_PRE_SCAN_ROWS_MASK   0x1FFULL
1606
1607 #define TM_CFG_TID_OFFSET_SHIFT         30
1608 #define TM_CFG_TID_OFFSET_MASK          0x7FFFFULL
1609 #define TM_CFG_TID_PRE_SCAN_ROWS_SHIFT  49
1610 #define TM_CFG_TID_PRE_SCAN_ROWS_MASK   0x1FFULL
1611
1612 static void ecore_tm_init_pf(struct ecore_hwfn *p_hwfn)
1613 {
1614         struct ecore_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1615         u32 active_seg_mask = 0, tm_offset, rt_reg;
1616         struct ecore_tm_iids tm_iids;
1617         u64 cfg_word;
1618         u8 i;
1619
1620         OSAL_MEM_ZERO(&tm_iids, sizeof(tm_iids));
1621         ecore_cxt_tm_iids(p_mngr, &tm_iids);
1622
1623         /* @@@TBD No pre-scan for now */
1624
1625         /* Note: We assume consecutive VFs for a PF */
1626         for (i = 0; i < p_mngr->vf_count; i++) {
1627                 cfg_word = 0;
1628                 SET_FIELD(cfg_word, TM_CFG_NUM_IDS, tm_iids.per_vf_cids);
1629                 SET_FIELD(cfg_word, TM_CFG_PRE_SCAN_OFFSET, 0);
1630                 SET_FIELD(cfg_word, TM_CFG_PARENT_PF, p_hwfn->rel_pf_id);
1631                 SET_FIELD(cfg_word, TM_CFG_CID_PRE_SCAN_ROWS, 0); /* scan all */
1632
1633                 rt_reg = TM_REG_CONFIG_CONN_MEM_RT_OFFSET +
1634                     (sizeof(cfg_word) / sizeof(u32)) *
1635                     (p_hwfn->p_dev->p_iov_info->first_vf_in_pf + i);
1636                 STORE_RT_REG_AGG(p_hwfn, rt_reg, cfg_word);
1637         }
1638
1639         cfg_word = 0;
1640         SET_FIELD(cfg_word, TM_CFG_NUM_IDS, tm_iids.pf_cids);
1641         SET_FIELD(cfg_word, TM_CFG_PRE_SCAN_OFFSET, 0);
1642         SET_FIELD(cfg_word, TM_CFG_PARENT_PF, 0);       /* n/a for PF */
1643         SET_FIELD(cfg_word, TM_CFG_CID_PRE_SCAN_ROWS, 0); /* scan all   */
1644
1645         rt_reg = TM_REG_CONFIG_CONN_MEM_RT_OFFSET +
1646             (sizeof(cfg_word) / sizeof(u32)) *
1647             (NUM_OF_VFS(p_hwfn->p_dev) + p_hwfn->rel_pf_id);
1648         STORE_RT_REG_AGG(p_hwfn, rt_reg, cfg_word);
1649
1650         /* enale scan */
1651         STORE_RT_REG(p_hwfn, TM_REG_PF_ENABLE_CONN_RT_OFFSET,
1652                      tm_iids.pf_cids ? 0x1 : 0x0);
1653
1654         /* @@@TBD how to enable the scan for the VFs */
1655
1656         tm_offset = tm_iids.per_vf_cids;
1657
1658         /* Note: We assume consecutive VFs for a PF */
1659         for (i = 0; i < p_mngr->vf_count; i++) {
1660                 cfg_word = 0;
1661                 SET_FIELD(cfg_word, TM_CFG_NUM_IDS, tm_iids.per_vf_tids);
1662                 SET_FIELD(cfg_word, TM_CFG_PRE_SCAN_OFFSET, 0);
1663                 SET_FIELD(cfg_word, TM_CFG_PARENT_PF, p_hwfn->rel_pf_id);
1664                 SET_FIELD(cfg_word, TM_CFG_TID_OFFSET, tm_offset);
1665                 SET_FIELD(cfg_word, TM_CFG_TID_PRE_SCAN_ROWS, (u64)0);
1666
1667                 rt_reg = TM_REG_CONFIG_TASK_MEM_RT_OFFSET +
1668                     (sizeof(cfg_word) / sizeof(u32)) *
1669                     (p_hwfn->p_dev->p_iov_info->first_vf_in_pf + i);
1670
1671                 STORE_RT_REG_AGG(p_hwfn, rt_reg, cfg_word);
1672         }
1673
1674         tm_offset = tm_iids.pf_cids;
1675         for (i = 0; i < NUM_TASK_PF_SEGMENTS; i++) {
1676                 cfg_word = 0;
1677                 SET_FIELD(cfg_word, TM_CFG_NUM_IDS, tm_iids.pf_tids[i]);
1678                 SET_FIELD(cfg_word, TM_CFG_PRE_SCAN_OFFSET, 0);
1679                 SET_FIELD(cfg_word, TM_CFG_PARENT_PF, 0);
1680                 SET_FIELD(cfg_word, TM_CFG_TID_OFFSET, tm_offset);
1681                 SET_FIELD(cfg_word, TM_CFG_TID_PRE_SCAN_ROWS, (u64)0);
1682
1683                 rt_reg = TM_REG_CONFIG_TASK_MEM_RT_OFFSET +
1684                     (sizeof(cfg_word) / sizeof(u32)) *
1685                     (NUM_OF_VFS(p_hwfn->p_dev) +
1686                      p_hwfn->rel_pf_id * NUM_TASK_PF_SEGMENTS + i);
1687
1688                 STORE_RT_REG_AGG(p_hwfn, rt_reg, cfg_word);
1689                 active_seg_mask |= (tm_iids.pf_tids[i] ? (1 << i) : 0);
1690
1691                 tm_offset += tm_iids.pf_tids[i];
1692         }
1693
1694         STORE_RT_REG(p_hwfn, TM_REG_PF_ENABLE_TASK_RT_OFFSET, active_seg_mask);
1695
1696         /* @@@TBD how to enable the scan for the VFs */
1697 }
1698
1699 static void ecore_prs_init_pf(struct ecore_hwfn *p_hwfn)
1700 {
1701         struct ecore_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1702         struct ecore_conn_type_cfg *p_fcoe = &p_mngr->conn_cfg[PROTOCOLID_FCOE];
1703         struct ecore_tid_seg *p_tid;
1704
1705         /* If FCoE is active set the MAX OX_ID (tid) in the Parser */
1706         if (!p_fcoe->cid_count)
1707                 return;
1708
1709         p_tid = &p_fcoe->tid_seg[ECORE_CXT_FCOE_TID_SEG];
1710         STORE_RT_REG_AGG(p_hwfn,
1711                         PRS_REG_TASK_ID_MAX_INITIATOR_PF_RT_OFFSET,
1712                         p_tid->count);
1713 }
1714
1715 void ecore_cxt_hw_init_common(struct ecore_hwfn *p_hwfn)
1716 {
1717         /* CDU configuration */
1718         ecore_cdu_init_common(p_hwfn);
1719 }
1720
1721 void ecore_cxt_hw_init_pf(struct ecore_hwfn *p_hwfn)
1722 {
1723         ecore_qm_init_pf(p_hwfn);
1724         ecore_cm_init_pf(p_hwfn);
1725         ecore_dq_init_pf(p_hwfn);
1726         ecore_cdu_init_pf(p_hwfn);
1727         ecore_ilt_init_pf(p_hwfn);
1728         ecore_src_init_pf(p_hwfn);
1729         ecore_tm_init_pf(p_hwfn);
1730         ecore_prs_init_pf(p_hwfn);
1731 }
1732
1733 enum _ecore_status_t ecore_cxt_acquire_cid(struct ecore_hwfn *p_hwfn,
1734                                            enum protocol_type type, u32 *p_cid)
1735 {
1736         struct ecore_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1737         u32 rel_cid;
1738
1739         if (type >= MAX_CONN_TYPES || !p_mngr->acquired[type].cid_map) {
1740                 DP_NOTICE(p_hwfn, true, "Invalid protocol type %d", type);
1741                 return ECORE_INVAL;
1742         }
1743
1744         rel_cid = OSAL_FIND_FIRST_ZERO_BIT(p_mngr->acquired[type].cid_map,
1745                                            p_mngr->acquired[type].max_count);
1746
1747         if (rel_cid >= p_mngr->acquired[type].max_count) {
1748                 DP_NOTICE(p_hwfn, false, "no CID available for protocol %d\n",
1749                           type);
1750                 return ECORE_NORESOURCES;
1751         }
1752
1753         OSAL_SET_BIT(rel_cid, p_mngr->acquired[type].cid_map);
1754
1755         *p_cid = rel_cid + p_mngr->acquired[type].start_cid;
1756
1757         return ECORE_SUCCESS;
1758 }
1759
1760 static bool ecore_cxt_test_cid_acquired(struct ecore_hwfn *p_hwfn,
1761                                         u32 cid, enum protocol_type *p_type)
1762 {
1763         struct ecore_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1764         struct ecore_cid_acquired_map *p_map;
1765         enum protocol_type p;
1766         u32 rel_cid;
1767
1768         /* Iterate over protocols and find matching cid range */
1769         for (p = 0; p < MAX_CONN_TYPES; p++) {
1770                 p_map = &p_mngr->acquired[p];
1771
1772                 if (!p_map->cid_map)
1773                         continue;
1774                 if (cid >= p_map->start_cid &&
1775                     cid < p_map->start_cid + p_map->max_count) {
1776                         break;
1777                 }
1778         }
1779         *p_type = p;
1780
1781         if (p == MAX_CONN_TYPES) {
1782                 DP_NOTICE(p_hwfn, true, "Invalid CID %d", cid);
1783                 return false;
1784         }
1785         rel_cid = cid - p_map->start_cid;
1786         if (!OSAL_TEST_BIT(rel_cid, p_map->cid_map)) {
1787                 DP_NOTICE(p_hwfn, true, "CID %d not acquired", cid);
1788                 return false;
1789         }
1790         return true;
1791 }
1792
1793 void ecore_cxt_release_cid(struct ecore_hwfn *p_hwfn, u32 cid)
1794 {
1795         struct ecore_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1796         enum protocol_type type;
1797         bool b_acquired;
1798         u32 rel_cid;
1799
1800         /* Test acquired and find matching per-protocol map */
1801         b_acquired = ecore_cxt_test_cid_acquired(p_hwfn, cid, &type);
1802
1803         if (!b_acquired)
1804                 return;
1805
1806         rel_cid = cid - p_mngr->acquired[type].start_cid;
1807         OSAL_CLEAR_BIT(rel_cid, p_mngr->acquired[type].cid_map);
1808 }
1809
1810 enum _ecore_status_t ecore_cxt_get_cid_info(struct ecore_hwfn *p_hwfn,
1811                                             struct ecore_cxt_info *p_info)
1812 {
1813         struct ecore_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1814         u32 conn_cxt_size, hw_p_size, cxts_per_p, line;
1815         enum protocol_type type;
1816         bool b_acquired;
1817
1818         /* Test acquired and find matching per-protocol map */
1819         b_acquired = ecore_cxt_test_cid_acquired(p_hwfn, p_info->iid, &type);
1820
1821         if (!b_acquired)
1822                 return ECORE_INVAL;
1823
1824         /* set the protocl type */
1825         p_info->type = type;
1826
1827         /* compute context virtual pointer */
1828         hw_p_size = p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC].p_size.val;
1829
1830         conn_cxt_size = CONN_CXT_SIZE(p_hwfn);
1831         cxts_per_p = ILT_PAGE_IN_BYTES(hw_p_size) / conn_cxt_size;
1832         line = p_info->iid / cxts_per_p;
1833
1834         /* Make sure context is allocated (dynamic allocation) */
1835         if (!p_mngr->ilt_shadow[line].p_virt)
1836                 return ECORE_INVAL;
1837
1838         p_info->p_cxt = (u8 *)p_mngr->ilt_shadow[line].p_virt +
1839             p_info->iid % cxts_per_p * conn_cxt_size;
1840
1841         DP_VERBOSE(p_hwfn, (ECORE_MSG_ILT | ECORE_MSG_CXT),
1842                 "Accessing ILT shadow[%d]: CXT pointer is at %p (for iid %d)\n",
1843                 (p_info->iid / cxts_per_p), p_info->p_cxt, p_info->iid);
1844
1845         return ECORE_SUCCESS;
1846 }
1847
1848 void ecore_cxt_set_srq_count(struct ecore_hwfn *p_hwfn, u32 num_srqs)
1849 {
1850         struct ecore_cxt_mngr *p_mgr = p_hwfn->p_cxt_mngr;
1851
1852         p_mgr->srq_count = num_srqs;
1853 }
1854
1855 u32 ecore_cxt_get_srq_count(struct ecore_hwfn *p_hwfn)
1856 {
1857         struct ecore_cxt_mngr *p_mgr = p_hwfn->p_cxt_mngr;
1858
1859         return p_mgr->srq_count;
1860 }
1861
1862 enum _ecore_status_t ecore_cxt_set_pf_params(struct ecore_hwfn *p_hwfn)
1863 {
1864         /* Set the number of required CORE connections */
1865         u32 core_cids = 1;      /* SPQ */
1866
1867         ecore_cxt_set_proto_cid_count(p_hwfn, PROTOCOLID_CORE, core_cids, 0);
1868
1869         switch (p_hwfn->hw_info.personality) {
1870         case ECORE_PCI_ETH:
1871                 {
1872                         struct ecore_eth_pf_params *p_params =
1873                             &p_hwfn->pf_params.eth_pf_params;
1874
1875                         ecore_cxt_set_proto_cid_count(p_hwfn,
1876                                 PROTOCOLID_ETH,
1877                                 p_params->num_cons, 1); /* FIXME VF count... */
1878
1879                         break;
1880                 }
1881         default:
1882                 return ECORE_INVAL;
1883         }
1884
1885         return ECORE_SUCCESS;
1886 }
1887
1888 enum _ecore_status_t ecore_cxt_get_tid_mem_info(struct ecore_hwfn *p_hwfn,
1889                                                 struct ecore_tid_mem *p_info)
1890 {
1891         struct ecore_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
1892         u32 proto, seg, total_lines, i, shadow_line;
1893         struct ecore_ilt_client_cfg *p_cli;
1894         struct ecore_ilt_cli_blk *p_fl_seg;
1895         struct ecore_tid_seg *p_seg_info;
1896
1897         /* Verify the personality */
1898         switch (p_hwfn->hw_info.personality) {
1899         default:
1900                 return ECORE_INVAL;
1901         }
1902
1903         p_cli = &p_mngr->clients[ILT_CLI_CDUT];
1904         if (!p_cli->active)
1905                 return ECORE_INVAL;
1906
1907         p_seg_info = &p_mngr->conn_cfg[proto].tid_seg[seg];
1908         if (!p_seg_info->has_fl_mem)
1909                 return ECORE_INVAL;
1910
1911         p_fl_seg = &p_cli->pf_blks[CDUT_FL_SEG_BLK(seg, PF)];
1912         total_lines = DIV_ROUND_UP(p_fl_seg->total_size,
1913                                    p_fl_seg->real_size_in_page);
1914
1915         for (i = 0; i < total_lines; i++) {
1916                 shadow_line = i + p_fl_seg->start_line -
1917                     p_hwfn->p_cxt_mngr->pf_start_line;
1918                 p_info->blocks[i] = p_mngr->ilt_shadow[shadow_line].p_virt;
1919         }
1920         p_info->waste = ILT_PAGE_IN_BYTES(p_cli->p_size.val) -
1921             p_fl_seg->real_size_in_page;
1922         p_info->tid_size = p_mngr->task_type_size[p_seg_info->type];
1923         p_info->num_tids_per_block = p_fl_seg->real_size_in_page /
1924             p_info->tid_size;
1925
1926         return ECORE_SUCCESS;
1927 }
1928
1929 /* This function is very RoCE oriented, if another protocol in the future
1930  * will want this feature we'll need to modify the function to be more generic
1931  */
1932 enum _ecore_status_t
1933 ecore_cxt_dynamic_ilt_alloc(struct ecore_hwfn *p_hwfn,
1934                             enum ecore_cxt_elem_type elem_type,
1935                             u32 iid)
1936 {
1937         u32 reg_offset, shadow_line, elem_size, hw_p_size, elems_per_p, line;
1938         struct ecore_ilt_client_cfg *p_cli;
1939         struct ecore_ilt_cli_blk *p_blk;
1940         struct ecore_ptt *p_ptt;
1941         dma_addr_t p_phys;
1942         u64 ilt_hw_entry;
1943         void *p_virt;
1944         enum _ecore_status_t rc = ECORE_SUCCESS;
1945
1946         switch (elem_type) {
1947         case ECORE_ELEM_CXT:
1948                 p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC];
1949                 elem_size = CONN_CXT_SIZE(p_hwfn);
1950                 p_blk = &p_cli->pf_blks[CDUC_BLK];
1951                 break;
1952         case ECORE_ELEM_SRQ:
1953                 p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_TSDM];
1954                 elem_size = SRQ_CXT_SIZE;
1955                 p_blk = &p_cli->pf_blks[SRQ_BLK];
1956                 break;
1957         case ECORE_ELEM_TASK:
1958                 p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT];
1959                 elem_size = TYPE1_TASK_CXT_SIZE(p_hwfn);
1960                 p_blk = &p_cli->pf_blks[CDUT_SEG_BLK(ECORE_CXT_ROCE_TID_SEG)];
1961                 break;
1962         default:
1963                 DP_NOTICE(p_hwfn, false,
1964                           "ECORE_INVALID elem type = %d", elem_type);
1965                 return ECORE_INVAL;
1966         }
1967
1968         /* Calculate line in ilt */
1969         hw_p_size = p_cli->p_size.val;
1970         elems_per_p = ILT_PAGE_IN_BYTES(hw_p_size) / elem_size;
1971         line = p_blk->start_line + (iid / elems_per_p);
1972         shadow_line = line - p_hwfn->p_cxt_mngr->pf_start_line;
1973
1974         /* If line is already allocated, do nothing, otherwise allocate it and
1975          * write it to the PSWRQ2 registers.
1976          * This section can be run in parallel from different contexts and thus
1977          * a mutex protection is needed.
1978          */
1979
1980         OSAL_MUTEX_ACQUIRE(&p_hwfn->p_cxt_mngr->mutex);
1981
1982         if (p_hwfn->p_cxt_mngr->ilt_shadow[shadow_line].p_virt)
1983                 goto out0;
1984
1985         p_ptt = ecore_ptt_acquire(p_hwfn);
1986         if (!p_ptt) {
1987                 DP_NOTICE(p_hwfn, false,
1988                           "ECORE_TIME_OUT on ptt acquire - dynamic allocation");
1989                 rc = ECORE_TIMEOUT;
1990                 goto out0;
1991         }
1992
1993         p_virt = OSAL_DMA_ALLOC_COHERENT(p_hwfn->p_dev,
1994                                          &p_phys,
1995                                          p_blk->real_size_in_page);
1996         if (!p_virt) {
1997                 rc = ECORE_NOMEM;
1998                 goto out1;
1999         }
2000         OSAL_MEM_ZERO(p_virt, p_blk->real_size_in_page);
2001
2002         p_hwfn->p_cxt_mngr->ilt_shadow[shadow_line].p_virt = p_virt;
2003         p_hwfn->p_cxt_mngr->ilt_shadow[shadow_line].p_phys = p_phys;
2004         p_hwfn->p_cxt_mngr->ilt_shadow[shadow_line].size =
2005                 p_blk->real_size_in_page;
2006
2007         /* compute absolute offset */
2008         reg_offset = PSWRQ2_REG_ILT_MEMORY +
2009                      (line * ILT_REG_SIZE_IN_BYTES * ILT_ENTRY_IN_REGS);
2010
2011         ilt_hw_entry = 0;
2012         SET_FIELD(ilt_hw_entry, ILT_ENTRY_VALID, 1ULL);
2013         SET_FIELD(ilt_hw_entry,
2014                   ILT_ENTRY_PHY_ADDR,
2015                   (p_hwfn->p_cxt_mngr->ilt_shadow[shadow_line].p_phys >> 12));
2016
2017 /* Write via DMAE since the PSWRQ2_REG_ILT_MEMORY line is a wide-bus */
2018
2019         ecore_dmae_host2grc(p_hwfn, p_ptt, (u64)(osal_uintptr_t)&ilt_hw_entry,
2020                             reg_offset, sizeof(ilt_hw_entry) / sizeof(u32),
2021                             0 /* no flags */);
2022
2023         if (elem_type == ECORE_ELEM_CXT) {
2024                 u32 last_cid_allocated = (1 + (iid / elems_per_p)) *
2025                                          elems_per_p;
2026
2027                 /* Update the relevant register in the parser */
2028                 ecore_wr(p_hwfn, p_ptt, PRS_REG_ROCE_DEST_QP_MAX_PF,
2029                          last_cid_allocated - 1);
2030
2031                 if (!p_hwfn->b_rdma_enabled_in_prs) {
2032                         /* Enable RoCE search */
2033                         ecore_wr(p_hwfn, p_ptt, p_hwfn->rdma_prs_search_reg, 1);
2034                         p_hwfn->b_rdma_enabled_in_prs = true;
2035                 }
2036         }
2037
2038 out1:
2039         ecore_ptt_release(p_hwfn, p_ptt);
2040 out0:
2041         OSAL_MUTEX_RELEASE(&p_hwfn->p_cxt_mngr->mutex);
2042
2043         return rc;
2044 }
2045
2046 /* This function is very RoCE oriented, if another protocol in the future
2047  * will want this feature we'll need to modify the function to be more generic
2048  */
2049 static enum _ecore_status_t
2050 ecore_cxt_free_ilt_range(struct ecore_hwfn *p_hwfn,
2051                          enum ecore_cxt_elem_type elem_type,
2052                          u32 start_iid, u32 count)
2053 {
2054         u32 start_line, end_line, shadow_start_line, shadow_end_line;
2055         u32 reg_offset, elem_size, hw_p_size, elems_per_p;
2056         struct ecore_ilt_client_cfg *p_cli;
2057         struct ecore_ilt_cli_blk *p_blk;
2058         u32 end_iid = start_iid + count;
2059         struct ecore_ptt *p_ptt;
2060         u64 ilt_hw_entry = 0;
2061         u32 i;
2062
2063         switch (elem_type) {
2064         case ECORE_ELEM_CXT:
2065                 p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUC];
2066                 elem_size = CONN_CXT_SIZE(p_hwfn);
2067                 p_blk = &p_cli->pf_blks[CDUC_BLK];
2068                 break;
2069         case ECORE_ELEM_SRQ:
2070                 p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_TSDM];
2071                 elem_size = SRQ_CXT_SIZE;
2072                 p_blk = &p_cli->pf_blks[SRQ_BLK];
2073                 break;
2074         case ECORE_ELEM_TASK:
2075                 p_cli = &p_hwfn->p_cxt_mngr->clients[ILT_CLI_CDUT];
2076                 elem_size = TYPE1_TASK_CXT_SIZE(p_hwfn);
2077                 p_blk = &p_cli->pf_blks[CDUT_SEG_BLK(ECORE_CXT_ROCE_TID_SEG)];
2078                 break;
2079         default:
2080                 DP_NOTICE(p_hwfn, false,
2081                           "ECORE_INVALID elem type = %d", elem_type);
2082                 return ECORE_INVAL;
2083         }
2084
2085         /* Calculate line in ilt */
2086         hw_p_size = p_cli->p_size.val;
2087         elems_per_p = ILT_PAGE_IN_BYTES(hw_p_size) / elem_size;
2088         start_line = p_blk->start_line + (start_iid / elems_per_p);
2089         end_line = p_blk->start_line + (end_iid / elems_per_p);
2090         if (((end_iid + 1) / elems_per_p) != (end_iid / elems_per_p))
2091                 end_line--;
2092
2093         shadow_start_line = start_line - p_hwfn->p_cxt_mngr->pf_start_line;
2094         shadow_end_line = end_line - p_hwfn->p_cxt_mngr->pf_start_line;
2095
2096         p_ptt = ecore_ptt_acquire(p_hwfn);
2097         if (!p_ptt) {
2098                 DP_NOTICE(p_hwfn, false,
2099                           "ECORE_TIME_OUT on ptt acquire - dynamic allocation");
2100                 return ECORE_TIMEOUT;
2101         }
2102
2103         for (i = shadow_start_line; i < shadow_end_line; i++) {
2104                 if (!p_hwfn->p_cxt_mngr->ilt_shadow[i].p_virt)
2105                         continue;
2106
2107                 OSAL_DMA_FREE_COHERENT(p_hwfn->p_dev,
2108                                        p_hwfn->p_cxt_mngr->ilt_shadow[i].p_virt,
2109                                        p_hwfn->p_cxt_mngr->ilt_shadow[i].p_phys,
2110                                        p_hwfn->p_cxt_mngr->ilt_shadow[i].size);
2111
2112                 p_hwfn->p_cxt_mngr->ilt_shadow[i].p_virt = OSAL_NULL;
2113                 p_hwfn->p_cxt_mngr->ilt_shadow[i].p_phys = 0;
2114                 p_hwfn->p_cxt_mngr->ilt_shadow[i].size = 0;
2115
2116                 /* compute absolute offset */
2117                 reg_offset = PSWRQ2_REG_ILT_MEMORY +
2118                     ((start_line++) * ILT_REG_SIZE_IN_BYTES *
2119                      ILT_ENTRY_IN_REGS);
2120
2121                 /* Write via DMAE since the PSWRQ2_REG_ILT_MEMORY line is a
2122                  * wide-bus.
2123                  */
2124                 ecore_dmae_host2grc(p_hwfn, p_ptt,
2125                                     (u64)(osal_uintptr_t)&ilt_hw_entry,
2126                                     reg_offset,
2127                                     sizeof(ilt_hw_entry) / sizeof(u32),
2128                                     0 /* no flags */);
2129         }
2130
2131         ecore_ptt_release(p_hwfn, p_ptt);
2132
2133         return ECORE_SUCCESS;
2134 }
2135
2136 enum _ecore_status_t ecore_cxt_free_proto_ilt(struct ecore_hwfn *p_hwfn,
2137                                               enum protocol_type proto)
2138 {
2139         enum _ecore_status_t rc;
2140         u32 cid;
2141
2142         /* Free Connection CXT */
2143         rc = ecore_cxt_free_ilt_range(p_hwfn, ECORE_ELEM_CXT,
2144                                       ecore_cxt_get_proto_cid_start(p_hwfn,
2145                                                                     proto),
2146                                       ecore_cxt_get_proto_cid_count(p_hwfn,
2147                                                                     proto,
2148                                                                     &cid));
2149
2150         if (rc)
2151                 return rc;
2152
2153         /* Free Task CXT */
2154         rc = ecore_cxt_free_ilt_range(p_hwfn, ECORE_ELEM_TASK, 0,
2155                                       ecore_cxt_get_proto_tid_count(p_hwfn,
2156                                                                     proto));
2157         if (rc)
2158                 return rc;
2159
2160         /* Free TSDM CXT */
2161         rc = ecore_cxt_free_ilt_range(p_hwfn, ECORE_ELEM_SRQ, 0,
2162                                       ecore_cxt_get_srq_count(p_hwfn));
2163
2164         return rc;
2165 }
2166
2167 enum _ecore_status_t ecore_cxt_get_task_ctx(struct ecore_hwfn *p_hwfn,
2168                                             u32 tid,
2169                                             u8 ctx_type, void **pp_task_ctx)
2170 {
2171         struct ecore_cxt_mngr *p_mngr = p_hwfn->p_cxt_mngr;
2172         struct ecore_ilt_client_cfg *p_cli;
2173         struct ecore_ilt_cli_blk *p_seg;
2174         struct ecore_tid_seg *p_seg_info;
2175         u32 proto, seg;
2176         u32 total_lines;
2177         u32 tid_size, ilt_idx;
2178         u32 num_tids_per_block;
2179
2180         /* Verify the personality */
2181         switch (p_hwfn->hw_info.personality) {
2182         default:
2183                 return ECORE_INVAL;
2184         }
2185
2186         p_cli = &p_mngr->clients[ILT_CLI_CDUT];
2187         if (!p_cli->active)
2188                 return ECORE_INVAL;
2189
2190         p_seg_info = &p_mngr->conn_cfg[proto].tid_seg[seg];
2191
2192         if (ctx_type == ECORE_CTX_WORKING_MEM) {
2193                 p_seg = &p_cli->pf_blks[CDUT_SEG_BLK(seg)];
2194         } else if (ctx_type == ECORE_CTX_FL_MEM) {
2195                 if (!p_seg_info->has_fl_mem)
2196                         return ECORE_INVAL;
2197                 p_seg = &p_cli->pf_blks[CDUT_FL_SEG_BLK(seg, PF)];
2198         } else {
2199                 return ECORE_INVAL;
2200         }
2201         total_lines = DIV_ROUND_UP(p_seg->total_size, p_seg->real_size_in_page);
2202         tid_size = p_mngr->task_type_size[p_seg_info->type];
2203         num_tids_per_block = p_seg->real_size_in_page / tid_size;
2204
2205         if (total_lines < tid / num_tids_per_block)
2206                 return ECORE_INVAL;
2207
2208         ilt_idx = tid / num_tids_per_block + p_seg->start_line -
2209             p_mngr->pf_start_line;
2210         *pp_task_ctx = (u8 *)p_mngr->ilt_shadow[ilt_idx].p_virt +
2211             (tid % num_tids_per_block) * tid_size;
2212
2213         return ECORE_SUCCESS;
2214 }