3c1d05b3e1240e025606ad142efdfc0d2abc4aa7
[deb_dpdk.git] / drivers / net / qede / base / ecore_spq.c
1 /*
2  * Copyright (c) 2016 QLogic Corporation.
3  * All rights reserved.
4  * www.qlogic.com
5  *
6  * See LICENSE.qede_pmd for copyright and licensing details.
7  */
8
9 #include "bcm_osal.h"
10 #include "reg_addr.h"
11 #include "ecore_gtt_reg_addr.h"
12 #include "ecore_hsi_common.h"
13 #include "ecore.h"
14 #include "ecore_sp_api.h"
15 #include "ecore_spq.h"
16 #include "ecore_iro.h"
17 #include "ecore_init_fw_funcs.h"
18 #include "ecore_cxt.h"
19 #include "ecore_int.h"
20 #include "ecore_dev_api.h"
21 #include "ecore_mcp.h"
22 #include "ecore_hw.h"
23 #include "ecore_sriov.h"
24
25 /***************************************************************************
26  * Structures & Definitions
27  ***************************************************************************/
28
29 #define SPQ_HIGH_PRI_RESERVE_DEFAULT    (1)
30
31 #define SPQ_BLOCK_DELAY_MAX_ITER        (10)
32 #define SPQ_BLOCK_DELAY_US              (10)
33 #define SPQ_BLOCK_SLEEP_MAX_ITER        (1000)
34 #define SPQ_BLOCK_SLEEP_MS              (5)
35
36 /***************************************************************************
37  * Blocking Imp. (BLOCK/EBLOCK mode)
38  ***************************************************************************/
39 static void ecore_spq_blocking_cb(struct ecore_hwfn *p_hwfn,
40                                   void *cookie,
41                                   union event_ring_data *data,
42                                   u8 fw_return_code)
43 {
44         struct ecore_spq_comp_done *comp_done;
45
46         comp_done = (struct ecore_spq_comp_done *)cookie;
47
48         comp_done->done = 0x1;
49         comp_done->fw_return_code = fw_return_code;
50
51         /* make update visible to waiting thread */
52         OSAL_SMP_WMB(p_hwfn->p_dev);
53 }
54
55 static enum _ecore_status_t __ecore_spq_block(struct ecore_hwfn *p_hwfn,
56                                               struct ecore_spq_entry *p_ent,
57                                               u8 *p_fw_ret,
58                                               bool sleep_between_iter)
59 {
60         struct ecore_spq_comp_done *comp_done;
61         u32 iter_cnt;
62
63         comp_done = (struct ecore_spq_comp_done *)p_ent->comp_cb.cookie;
64         iter_cnt = sleep_between_iter ? SPQ_BLOCK_SLEEP_MAX_ITER
65                                       : SPQ_BLOCK_DELAY_MAX_ITER;
66
67         while (iter_cnt--) {
68                 OSAL_POLL_MODE_DPC(p_hwfn);
69                 OSAL_SMP_RMB(p_hwfn->p_dev);
70                 if (comp_done->done == 1) {
71                         if (p_fw_ret)
72                                 *p_fw_ret = comp_done->fw_return_code;
73                         return ECORE_SUCCESS;
74                 }
75
76                 if (sleep_between_iter)
77                         OSAL_MSLEEP(SPQ_BLOCK_SLEEP_MS);
78                 else
79                         OSAL_UDELAY(SPQ_BLOCK_DELAY_US);
80         }
81
82         return ECORE_TIMEOUT;
83 }
84
85 static enum _ecore_status_t ecore_spq_block(struct ecore_hwfn *p_hwfn,
86                                             struct ecore_spq_entry *p_ent,
87                                             u8 *p_fw_ret, bool skip_quick_poll)
88 {
89         struct ecore_spq_comp_done *comp_done;
90         enum _ecore_status_t rc;
91
92         /* A relatively short polling period w/o sleeping, to allow the FW to
93          * complete the ramrod and thus possibly to avoid the following sleeps.
94          */
95         if (!skip_quick_poll) {
96                 rc = __ecore_spq_block(p_hwfn, p_ent, p_fw_ret, false);
97                 if (rc == ECORE_SUCCESS)
98                         return ECORE_SUCCESS;
99         }
100
101         /* Move to polling with a sleeping period between iterations */
102         rc = __ecore_spq_block(p_hwfn, p_ent, p_fw_ret, true);
103         if (rc == ECORE_SUCCESS)
104                 return ECORE_SUCCESS;
105
106         DP_INFO(p_hwfn, "Ramrod is stuck, requesting MCP drain\n");
107         rc = ecore_mcp_drain(p_hwfn, p_hwfn->p_main_ptt);
108         if (rc != ECORE_SUCCESS) {
109                 DP_NOTICE(p_hwfn, true, "MCP drain failed\n");
110                 goto err;
111         }
112
113         /* Retry after drain */
114         rc = __ecore_spq_block(p_hwfn, p_ent, p_fw_ret, true);
115         if (rc == ECORE_SUCCESS)
116                 return ECORE_SUCCESS;
117
118         comp_done = (struct ecore_spq_comp_done *)p_ent->comp_cb.cookie;
119         if (comp_done->done == 1) {
120                 if (p_fw_ret)
121                         *p_fw_ret = comp_done->fw_return_code;
122                 return ECORE_SUCCESS;
123         }
124 err:
125         DP_NOTICE(p_hwfn, true,
126                   "Ramrod is stuck [CID %08x cmd %02x proto %02x echo %04x]\n",
127                   OSAL_LE32_TO_CPU(p_ent->elem.hdr.cid),
128                   p_ent->elem.hdr.cmd_id, p_ent->elem.hdr.protocol_id,
129                   OSAL_LE16_TO_CPU(p_ent->elem.hdr.echo));
130
131         ecore_hw_err_notify(p_hwfn, ECORE_HW_ERR_RAMROD_FAIL);
132
133         return ECORE_BUSY;
134 }
135
136 /***************************************************************************
137  * SPQ entries inner API
138  ***************************************************************************/
139 static enum _ecore_status_t
140 ecore_spq_fill_entry(struct ecore_hwfn *p_hwfn, struct ecore_spq_entry *p_ent)
141 {
142         p_ent->flags = 0;
143
144         switch (p_ent->comp_mode) {
145         case ECORE_SPQ_MODE_EBLOCK:
146         case ECORE_SPQ_MODE_BLOCK:
147                 p_ent->comp_cb.function = ecore_spq_blocking_cb;
148                 break;
149         case ECORE_SPQ_MODE_CB:
150                 break;
151         default:
152                 DP_NOTICE(p_hwfn, true, "Unknown SPQE completion mode %d\n",
153                           p_ent->comp_mode);
154                 return ECORE_INVAL;
155         }
156
157         DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ,
158                    "Ramrod header: [CID 0x%08x CMD 0x%02x protocol 0x%02x]"
159                    " Data pointer: [%08x:%08x] Completion Mode: %s\n",
160                    p_ent->elem.hdr.cid, p_ent->elem.hdr.cmd_id,
161                    p_ent->elem.hdr.protocol_id,
162                    p_ent->elem.data_ptr.hi, p_ent->elem.data_ptr.lo,
163                    D_TRINE(p_ent->comp_mode, ECORE_SPQ_MODE_EBLOCK,
164                            ECORE_SPQ_MODE_BLOCK, "MODE_EBLOCK", "MODE_BLOCK",
165                            "MODE_CB"));
166
167         return ECORE_SUCCESS;
168 }
169
170 /***************************************************************************
171  * HSI access
172  ***************************************************************************/
173 static void ecore_spq_hw_initialize(struct ecore_hwfn *p_hwfn,
174                                     struct ecore_spq *p_spq)
175 {
176         struct ecore_cxt_info cxt_info;
177         struct core_conn_context *p_cxt;
178         enum _ecore_status_t rc;
179         u16 physical_q;
180
181         cxt_info.iid = p_spq->cid;
182
183         rc = ecore_cxt_get_cid_info(p_hwfn, &cxt_info);
184
185         if (rc < 0) {
186                 DP_NOTICE(p_hwfn, true, "Cannot find context info for cid=%d\n",
187                           p_spq->cid);
188                 return;
189         }
190
191         p_cxt = cxt_info.p_cxt;
192
193         /* @@@TBD we zero the context until we have ilt_reset implemented. */
194         OSAL_MEM_ZERO(p_cxt, sizeof(*p_cxt));
195
196         if (ECORE_IS_BB(p_hwfn->p_dev) || ECORE_IS_AH(p_hwfn->p_dev)) {
197                 SET_FIELD(p_cxt->xstorm_ag_context.flags10,
198                           E4_XSTORM_CORE_CONN_AG_CTX_DQ_CF_EN, 1);
199                 SET_FIELD(p_cxt->xstorm_ag_context.flags1,
200                           E4_XSTORM_CORE_CONN_AG_CTX_DQ_CF_ACTIVE, 1);
201                 /* SET_FIELD(p_cxt->xstorm_ag_context.flags10,
202                  *        E4_XSTORM_CORE_CONN_AG_CTX_SLOW_PATH_EN, 1);
203                  */
204                 SET_FIELD(p_cxt->xstorm_ag_context.flags9,
205                           E4_XSTORM_CORE_CONN_AG_CTX_CONSOLID_PROD_CF_EN, 1);
206         }
207
208         /* CDU validation - FIXME currently disabled */
209
210         /* QM physical queue */
211         physical_q = ecore_get_cm_pq_idx(p_hwfn, PQ_FLAGS_LB);
212         p_cxt->xstorm_ag_context.physical_q0 = OSAL_CPU_TO_LE16(physical_q);
213
214         p_cxt->xstorm_st_context.spq_base_lo =
215             DMA_LO_LE(p_spq->chain.p_phys_addr);
216         p_cxt->xstorm_st_context.spq_base_hi =
217             DMA_HI_LE(p_spq->chain.p_phys_addr);
218
219         DMA_REGPAIR_LE(p_cxt->xstorm_st_context.consolid_base_addr,
220                        p_hwfn->p_consq->chain.p_phys_addr);
221 }
222
223 static enum _ecore_status_t ecore_spq_hw_post(struct ecore_hwfn *p_hwfn,
224                                               struct ecore_spq *p_spq,
225                                               struct ecore_spq_entry *p_ent)
226 {
227         struct ecore_chain *p_chain = &p_hwfn->p_spq->chain;
228         u16 echo = ecore_chain_get_prod_idx(p_chain);
229         struct slow_path_element *elem;
230         struct core_db_data db;
231
232         p_ent->elem.hdr.echo = OSAL_CPU_TO_LE16(echo);
233         elem = ecore_chain_produce(p_chain);
234         if (!elem) {
235                 DP_NOTICE(p_hwfn, true, "Failed to produce from SPQ chain\n");
236                 return ECORE_INVAL;
237         }
238
239         *elem = p_ent->elem;    /* struct assignment */
240
241         /* send a doorbell on the slow hwfn session */
242         OSAL_MEMSET(&db, 0, sizeof(db));
243         SET_FIELD(db.params, CORE_DB_DATA_DEST, DB_DEST_XCM);
244         SET_FIELD(db.params, CORE_DB_DATA_AGG_CMD, DB_AGG_CMD_SET);
245         SET_FIELD(db.params, CORE_DB_DATA_AGG_VAL_SEL,
246                   DQ_XCM_CORE_SPQ_PROD_CMD);
247         db.agg_flags = DQ_XCM_CORE_DQ_CF_CMD;
248         db.spq_prod = OSAL_CPU_TO_LE16(ecore_chain_get_prod_idx(p_chain));
249
250         /* make sure the SPQE is updated before the doorbell */
251         OSAL_WMB(p_hwfn->p_dev);
252
253         DOORBELL(p_hwfn, DB_ADDR(p_spq->cid, DQ_DEMS_LEGACY),
254                  *(u32 *)&db);
255
256         /* make sure doorbell is rang */
257         OSAL_WMB(p_hwfn->p_dev);
258
259         DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ,
260                    "Doorbelled [0x%08x, CID 0x%08x] with Flags: %02x"
261                    " agg_params: %02x, prod: %04x\n",
262                    DB_ADDR(p_spq->cid, DQ_DEMS_LEGACY), p_spq->cid, db.params,
263                    db.agg_flags, ecore_chain_get_prod_idx(p_chain));
264
265         return ECORE_SUCCESS;
266 }
267
268 /***************************************************************************
269  * Asynchronous events
270  ***************************************************************************/
271
272 static enum _ecore_status_t
273 ecore_async_event_completion(struct ecore_hwfn *p_hwfn,
274                              struct event_ring_entry *p_eqe)
275 {
276         switch (p_eqe->protocol_id) {
277         case PROTOCOLID_COMMON:
278                 return ecore_sriov_eqe_event(p_hwfn,
279                                              p_eqe->opcode,
280                                              p_eqe->echo, &p_eqe->data);
281         default:
282                 DP_NOTICE(p_hwfn,
283                           true, "Unknown Async completion for protocol: %d\n",
284                           p_eqe->protocol_id);
285                 return ECORE_INVAL;
286         }
287 }
288
289 /***************************************************************************
290  * EQ API
291  ***************************************************************************/
292 void ecore_eq_prod_update(struct ecore_hwfn *p_hwfn, u16 prod)
293 {
294         u32 addr = GTT_BAR0_MAP_REG_USDM_RAM +
295             USTORM_EQE_CONS_OFFSET(p_hwfn->rel_pf_id);
296
297         REG_WR16(p_hwfn, addr, prod);
298
299         /* keep prod updates ordered */
300         OSAL_MMIOWB(p_hwfn->p_dev);
301 }
302
303 enum _ecore_status_t ecore_eq_completion(struct ecore_hwfn *p_hwfn,
304                                          void *cookie)
305 {
306         struct ecore_eq *p_eq = cookie;
307         struct ecore_chain *p_chain = &p_eq->chain;
308         enum _ecore_status_t rc = 0;
309
310         /* take a snapshot of the FW consumer */
311         u16 fw_cons_idx = OSAL_LE16_TO_CPU(*p_eq->p_fw_cons);
312
313         DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ, "fw_cons_idx %x\n", fw_cons_idx);
314
315         /* Need to guarantee the fw_cons index we use points to a usuable
316          * element (to comply with our chain), so our macros would comply
317          */
318         if ((fw_cons_idx & ecore_chain_get_usable_per_page(p_chain)) ==
319             ecore_chain_get_usable_per_page(p_chain)) {
320                 fw_cons_idx += ecore_chain_get_unusable_per_page(p_chain);
321         }
322
323         /* Complete current segment of eq entries */
324         while (fw_cons_idx != ecore_chain_get_cons_idx(p_chain)) {
325                 struct event_ring_entry *p_eqe = ecore_chain_consume(p_chain);
326                 if (!p_eqe) {
327                         rc = ECORE_INVAL;
328                         break;
329                 }
330
331                 DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ,
332                            "op %x prot %x res0 %x echo %x fwret %x flags %x\n",
333                            p_eqe->opcode,            /* Event Opcode */
334                            p_eqe->protocol_id,  /* Event Protocol ID */
335                            p_eqe->reserved0,    /* Reserved */
336                            /* Echo value from ramrod data on the host */
337                            OSAL_LE16_TO_CPU(p_eqe->echo),
338                            p_eqe->fw_return_code,    /* FW return code for SP
339                                                       * ramrods
340                                                       */
341                            p_eqe->flags);
342
343                 if (GET_FIELD(p_eqe->flags, EVENT_RING_ENTRY_ASYNC)) {
344                         if (ecore_async_event_completion(p_hwfn, p_eqe))
345                                 rc = ECORE_INVAL;
346                 } else if (ecore_spq_completion(p_hwfn,
347                                                 p_eqe->echo,
348                                                 p_eqe->fw_return_code,
349                                                 &p_eqe->data)) {
350                         rc = ECORE_INVAL;
351                 }
352
353                 ecore_chain_recycle_consumed(p_chain);
354         }
355
356         ecore_eq_prod_update(p_hwfn, ecore_chain_get_prod_idx(p_chain));
357
358         return rc;
359 }
360
361 enum _ecore_status_t ecore_eq_alloc(struct ecore_hwfn *p_hwfn, u16 num_elem)
362 {
363         struct ecore_eq *p_eq;
364
365         /* Allocate EQ struct */
366         p_eq = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL, sizeof(*p_eq));
367         if (!p_eq) {
368                 DP_NOTICE(p_hwfn, true,
369                           "Failed to allocate `struct ecore_eq'\n");
370                 return ECORE_NOMEM;
371         }
372
373         /* Allocate and initialize EQ chain*/
374         if (ecore_chain_alloc(p_hwfn->p_dev,
375                               ECORE_CHAIN_USE_TO_PRODUCE,
376                               ECORE_CHAIN_MODE_PBL,
377                               ECORE_CHAIN_CNT_TYPE_U16,
378                               num_elem,
379                               sizeof(union event_ring_element),
380                               &p_eq->chain, OSAL_NULL) != ECORE_SUCCESS) {
381                 DP_NOTICE(p_hwfn, true, "Failed to allocate eq chain\n");
382                 goto eq_allocate_fail;
383         }
384
385         /* register EQ completion on the SP SB */
386         ecore_int_register_cb(p_hwfn, ecore_eq_completion,
387                               p_eq, &p_eq->eq_sb_index, &p_eq->p_fw_cons);
388
389         p_hwfn->p_eq = p_eq;
390         return ECORE_SUCCESS;
391
392 eq_allocate_fail:
393         OSAL_FREE(p_hwfn->p_dev, p_eq);
394         return ECORE_NOMEM;
395 }
396
397 void ecore_eq_setup(struct ecore_hwfn *p_hwfn)
398 {
399         ecore_chain_reset(&p_hwfn->p_eq->chain);
400 }
401
402 void ecore_eq_free(struct ecore_hwfn *p_hwfn)
403 {
404         if (!p_hwfn->p_eq)
405                 return;
406
407         ecore_chain_free(p_hwfn->p_dev, &p_hwfn->p_eq->chain);
408
409         OSAL_FREE(p_hwfn->p_dev, p_hwfn->p_eq);
410         p_hwfn->p_eq = OSAL_NULL;
411 }
412
413 /***************************************************************************
414 * CQE API - manipulate EQ functionality
415 ***************************************************************************/
416 static enum _ecore_status_t ecore_cqe_completion(struct ecore_hwfn *p_hwfn,
417                                                  struct eth_slow_path_rx_cqe
418                                                  *cqe,
419                                                  enum protocol_type protocol)
420 {
421         if (IS_VF(p_hwfn->p_dev))
422                 return OSAL_VF_CQE_COMPLETION(p_hwfn, cqe, protocol);
423
424         /* @@@tmp - it's possible we'll eventually want to handle some
425          * actual commands that can arrive here, but for now this is only
426          * used to complete the ramrod using the echo value on the cqe
427          */
428         return ecore_spq_completion(p_hwfn, cqe->echo, 0, OSAL_NULL);
429 }
430
431 enum _ecore_status_t ecore_eth_cqe_completion(struct ecore_hwfn *p_hwfn,
432                                               struct eth_slow_path_rx_cqe *cqe)
433 {
434         enum _ecore_status_t rc;
435
436         rc = ecore_cqe_completion(p_hwfn, cqe, PROTOCOLID_ETH);
437         if (rc) {
438                 DP_NOTICE(p_hwfn, true,
439                           "Failed to handle RXQ CQE [cmd 0x%02x]\n",
440                           cqe->ramrod_cmd_id);
441         }
442
443         return rc;
444 }
445
446 /***************************************************************************
447  * Slow hwfn Queue (spq)
448  ***************************************************************************/
449 void ecore_spq_setup(struct ecore_hwfn *p_hwfn)
450 {
451         struct ecore_spq *p_spq = p_hwfn->p_spq;
452         struct ecore_spq_entry *p_virt = OSAL_NULL;
453         dma_addr_t p_phys = 0;
454         u32 i, capacity;
455
456         OSAL_LIST_INIT(&p_spq->pending);
457         OSAL_LIST_INIT(&p_spq->completion_pending);
458         OSAL_LIST_INIT(&p_spq->free_pool);
459         OSAL_LIST_INIT(&p_spq->unlimited_pending);
460         OSAL_SPIN_LOCK_INIT(&p_spq->lock);
461
462         /* SPQ empty pool */
463         p_phys = p_spq->p_phys + OFFSETOF(struct ecore_spq_entry, ramrod);
464         p_virt = p_spq->p_virt;
465
466         capacity = ecore_chain_get_capacity(&p_spq->chain);
467         for (i = 0; i < capacity; i++) {
468                 DMA_REGPAIR_LE(p_virt->elem.data_ptr, p_phys);
469
470                 OSAL_LIST_PUSH_TAIL(&p_virt->list, &p_spq->free_pool);
471
472                 p_virt++;
473                 p_phys += sizeof(struct ecore_spq_entry);
474         }
475
476         /* Statistics */
477         p_spq->normal_count = 0;
478         p_spq->comp_count = 0;
479         p_spq->comp_sent_count = 0;
480         p_spq->unlimited_pending_count = 0;
481
482         OSAL_MEM_ZERO(p_spq->p_comp_bitmap,
483                       SPQ_COMP_BMAP_SIZE * sizeof(unsigned long));
484         p_spq->comp_bitmap_idx = 0;
485
486         /* SPQ cid, cannot fail */
487         ecore_cxt_acquire_cid(p_hwfn, PROTOCOLID_CORE, &p_spq->cid);
488         ecore_spq_hw_initialize(p_hwfn, p_spq);
489
490         /* reset the chain itself */
491         ecore_chain_reset(&p_spq->chain);
492 }
493
494 enum _ecore_status_t ecore_spq_alloc(struct ecore_hwfn *p_hwfn)
495 {
496         struct ecore_spq_entry *p_virt = OSAL_NULL;
497         struct ecore_spq *p_spq = OSAL_NULL;
498         dma_addr_t p_phys = 0;
499         u32 capacity;
500
501         /* SPQ struct */
502         p_spq =
503             OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL, sizeof(struct ecore_spq));
504         if (!p_spq) {
505                 DP_NOTICE(p_hwfn, true,
506                           "Failed to allocate `struct ecore_spq'\n");
507                 return ECORE_NOMEM;
508         }
509
510         /* SPQ ring  */
511         if (ecore_chain_alloc(p_hwfn->p_dev,
512                               ECORE_CHAIN_USE_TO_PRODUCE,
513                               ECORE_CHAIN_MODE_SINGLE,
514                               ECORE_CHAIN_CNT_TYPE_U16,
515                               0, /* N/A when the mode is SINGLE */
516                               sizeof(struct slow_path_element),
517                               &p_spq->chain, OSAL_NULL)) {
518                 DP_NOTICE(p_hwfn, true, "Failed to allocate spq chain\n");
519                 goto spq_allocate_fail;
520         }
521
522         /* allocate and fill the SPQ elements (incl. ramrod data list) */
523         capacity = ecore_chain_get_capacity(&p_spq->chain);
524         p_virt = OSAL_DMA_ALLOC_COHERENT(p_hwfn->p_dev, &p_phys,
525                                          capacity *
526                                          sizeof(struct ecore_spq_entry));
527         if (!p_virt)
528                 goto spq_allocate_fail;
529
530         p_spq->p_virt = p_virt;
531         p_spq->p_phys = p_phys;
532
533         OSAL_SPIN_LOCK_ALLOC(p_hwfn, &p_spq->lock);
534
535         p_hwfn->p_spq = p_spq;
536         return ECORE_SUCCESS;
537
538 spq_allocate_fail:
539         ecore_chain_free(p_hwfn->p_dev, &p_spq->chain);
540         OSAL_FREE(p_hwfn->p_dev, p_spq);
541         return ECORE_NOMEM;
542 }
543
544 void ecore_spq_free(struct ecore_hwfn *p_hwfn)
545 {
546         struct ecore_spq *p_spq = p_hwfn->p_spq;
547         u32 capacity;
548
549         if (!p_spq)
550                 return;
551
552         if (p_spq->p_virt) {
553                 capacity = ecore_chain_get_capacity(&p_spq->chain);
554                 OSAL_DMA_FREE_COHERENT(p_hwfn->p_dev,
555                                        p_spq->p_virt,
556                                        p_spq->p_phys,
557                                        capacity *
558                                        sizeof(struct ecore_spq_entry));
559         }
560
561         ecore_chain_free(p_hwfn->p_dev, &p_spq->chain);
562         OSAL_SPIN_LOCK_DEALLOC(&p_spq->lock);
563         OSAL_FREE(p_hwfn->p_dev, p_spq);
564 }
565
566 enum _ecore_status_t
567 ecore_spq_get_entry(struct ecore_hwfn *p_hwfn, struct ecore_spq_entry **pp_ent)
568 {
569         struct ecore_spq *p_spq = p_hwfn->p_spq;
570         struct ecore_spq_entry *p_ent = OSAL_NULL;
571         enum _ecore_status_t rc = ECORE_SUCCESS;
572
573         OSAL_SPIN_LOCK(&p_spq->lock);
574
575         if (OSAL_LIST_IS_EMPTY(&p_spq->free_pool)) {
576                 p_ent = OSAL_ZALLOC(p_hwfn->p_dev, GFP_ATOMIC, sizeof(*p_ent));
577                 if (!p_ent) {
578                         DP_NOTICE(p_hwfn, true,
579                                  "Failed to allocate an SPQ entry for a pending"
580                                  " ramrod\n");
581                         rc = ECORE_NOMEM;
582                         goto out_unlock;
583                 }
584                 p_ent->queue = &p_spq->unlimited_pending;
585         } else {
586                 p_ent = OSAL_LIST_FIRST_ENTRY(&p_spq->free_pool,
587                                               struct ecore_spq_entry, list);
588                 OSAL_LIST_REMOVE_ENTRY(&p_ent->list, &p_spq->free_pool);
589                 p_ent->queue = &p_spq->pending;
590         }
591
592         *pp_ent = p_ent;
593
594 out_unlock:
595         OSAL_SPIN_UNLOCK(&p_spq->lock);
596         return rc;
597 }
598
599 /* Locked variant; Should be called while the SPQ lock is taken */
600 static void __ecore_spq_return_entry(struct ecore_hwfn *p_hwfn,
601                                      struct ecore_spq_entry *p_ent)
602 {
603         OSAL_LIST_PUSH_TAIL(&p_ent->list, &p_hwfn->p_spq->free_pool);
604 }
605
606 void ecore_spq_return_entry(struct ecore_hwfn *p_hwfn,
607                             struct ecore_spq_entry *p_ent)
608 {
609         OSAL_SPIN_LOCK(&p_hwfn->p_spq->lock);
610         __ecore_spq_return_entry(p_hwfn, p_ent);
611         OSAL_SPIN_UNLOCK(&p_hwfn->p_spq->lock);
612 }
613
614 /**
615  * @brief ecore_spq_add_entry - adds a new entry to the pending
616  *        list. Should be used while lock is being held.
617  *
618  * Addes an entry to the pending list is there is room (en empty
619  * element is available in the free_pool), or else places the
620  * entry in the unlimited_pending pool.
621  *
622  * @param p_hwfn
623  * @param p_ent
624  * @param priority
625  *
626  * @return enum _ecore_status_t
627  */
628 static enum _ecore_status_t
629 ecore_spq_add_entry(struct ecore_hwfn *p_hwfn,
630                     struct ecore_spq_entry *p_ent, enum spq_priority priority)
631 {
632         struct ecore_spq *p_spq = p_hwfn->p_spq;
633
634         if (p_ent->queue == &p_spq->unlimited_pending) {
635                 if (OSAL_LIST_IS_EMPTY(&p_spq->free_pool)) {
636                         OSAL_LIST_PUSH_TAIL(&p_ent->list,
637                                             &p_spq->unlimited_pending);
638                         p_spq->unlimited_pending_count++;
639
640                         return ECORE_SUCCESS;
641
642                 } else {
643                         struct ecore_spq_entry *p_en2;
644
645                         p_en2 = OSAL_LIST_FIRST_ENTRY(&p_spq->free_pool,
646                                                      struct ecore_spq_entry,
647                                                      list);
648                         OSAL_LIST_REMOVE_ENTRY(&p_en2->list, &p_spq->free_pool);
649
650                         /* Copy the ring element physical pointer to the new
651                          * entry, since we are about to override the entire ring
652                          * entry and don't want to lose the pointer.
653                          */
654                         p_ent->elem.data_ptr = p_en2->elem.data_ptr;
655
656                         *p_en2 = *p_ent;
657
658                         /* EBLOCK responsible to free the allocated p_ent */
659                         if (p_ent->comp_mode != ECORE_SPQ_MODE_EBLOCK)
660                                 OSAL_FREE(p_hwfn->p_dev, p_ent);
661
662                         p_ent = p_en2;
663                 }
664         }
665
666         /* entry is to be placed in 'pending' queue */
667         switch (priority) {
668         case ECORE_SPQ_PRIORITY_NORMAL:
669                 OSAL_LIST_PUSH_TAIL(&p_ent->list, &p_spq->pending);
670                 p_spq->normal_count++;
671                 break;
672         case ECORE_SPQ_PRIORITY_HIGH:
673                 OSAL_LIST_PUSH_HEAD(&p_ent->list, &p_spq->pending);
674                 p_spq->high_count++;
675                 break;
676         default:
677                 return ECORE_INVAL;
678         }
679
680         return ECORE_SUCCESS;
681 }
682
683 /***************************************************************************
684  * Accessor
685  ***************************************************************************/
686
687 u32 ecore_spq_get_cid(struct ecore_hwfn *p_hwfn)
688 {
689         if (!p_hwfn->p_spq)
690                 return 0xffffffff;      /* illegal */
691         return p_hwfn->p_spq->cid;
692 }
693
694 /***************************************************************************
695  * Posting new Ramrods
696  ***************************************************************************/
697
698 static enum _ecore_status_t ecore_spq_post_list(struct ecore_hwfn *p_hwfn,
699                                                 osal_list_t *head,
700                                                 u32 keep_reserve)
701 {
702         struct ecore_spq *p_spq = p_hwfn->p_spq;
703         enum _ecore_status_t rc;
704
705         /* TODO - implementation might be wasteful; will always keep room
706          * for an additional high priority ramrod (even if one is already
707          * pending FW)
708          */
709         while (ecore_chain_get_elem_left(&p_spq->chain) > keep_reserve &&
710                !OSAL_LIST_IS_EMPTY(head)) {
711                 struct ecore_spq_entry *p_ent =
712                     OSAL_LIST_FIRST_ENTRY(head, struct ecore_spq_entry, list);
713                 if (p_ent != OSAL_NULL) {
714 #if defined(_NTDDK_)
715 #pragma warning(suppress : 6011 28182)
716 #endif
717                         OSAL_LIST_REMOVE_ENTRY(&p_ent->list, head);
718                         OSAL_LIST_PUSH_TAIL(&p_ent->list,
719                                             &p_spq->completion_pending);
720                         p_spq->comp_sent_count++;
721
722                         rc = ecore_spq_hw_post(p_hwfn, p_spq, p_ent);
723                         if (rc) {
724                                 OSAL_LIST_REMOVE_ENTRY(&p_ent->list,
725                                                     &p_spq->completion_pending);
726                                 __ecore_spq_return_entry(p_hwfn, p_ent);
727                                 return rc;
728                         }
729                 }
730         }
731
732         return ECORE_SUCCESS;
733 }
734
735 static enum _ecore_status_t ecore_spq_pend_post(struct ecore_hwfn *p_hwfn)
736 {
737         struct ecore_spq *p_spq = p_hwfn->p_spq;
738         struct ecore_spq_entry *p_ent = OSAL_NULL;
739
740         while (!OSAL_LIST_IS_EMPTY(&p_spq->free_pool)) {
741                 if (OSAL_LIST_IS_EMPTY(&p_spq->unlimited_pending))
742                         break;
743
744                 p_ent = OSAL_LIST_FIRST_ENTRY(&p_spq->unlimited_pending,
745                                               struct ecore_spq_entry, list);
746                 if (!p_ent)
747                         return ECORE_INVAL;
748
749 #if defined(_NTDDK_)
750 #pragma warning(suppress : 6011)
751 #endif
752                 OSAL_LIST_REMOVE_ENTRY(&p_ent->list, &p_spq->unlimited_pending);
753
754                 ecore_spq_add_entry(p_hwfn, p_ent, p_ent->priority);
755         }
756
757         return ecore_spq_post_list(p_hwfn,
758                                  &p_spq->pending, SPQ_HIGH_PRI_RESERVE_DEFAULT);
759 }
760
761 enum _ecore_status_t ecore_spq_post(struct ecore_hwfn *p_hwfn,
762                                     struct ecore_spq_entry *p_ent,
763                                     u8 *fw_return_code)
764 {
765         enum _ecore_status_t rc = ECORE_SUCCESS;
766         struct ecore_spq *p_spq = p_hwfn ? p_hwfn->p_spq : OSAL_NULL;
767         bool b_ret_ent = true;
768
769         if (!p_hwfn)
770                 return ECORE_INVAL;
771
772         if (!p_ent) {
773                 DP_NOTICE(p_hwfn, true, "Got a NULL pointer\n");
774                 return ECORE_INVAL;
775         }
776
777         if (p_hwfn->p_dev->recov_in_prog) {
778                 DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ,
779                            "Recovery is in progress -> skip spq post"
780                            " [cmd %02x protocol %02x]\n",
781                            p_ent->elem.hdr.cmd_id, p_ent->elem.hdr.protocol_id);
782                 /* Return success to let the flows to be completed successfully
783                  * w/o any error handling.
784                  */
785                 return ECORE_SUCCESS;
786         }
787
788         OSAL_SPIN_LOCK(&p_spq->lock);
789
790         /* Complete the entry */
791         rc = ecore_spq_fill_entry(p_hwfn, p_ent);
792
793         /* Check return value after LOCK is taken for cleaner error flow */
794         if (rc)
795                 goto spq_post_fail;
796
797         /* Add the request to the pending queue */
798         rc = ecore_spq_add_entry(p_hwfn, p_ent, p_ent->priority);
799         if (rc)
800                 goto spq_post_fail;
801
802         rc = ecore_spq_pend_post(p_hwfn);
803         if (rc) {
804                 /* Since it's possible that pending failed for a different
805                  * entry [although unlikely], the failed entry was already
806                  * dealt with; No need to return it here.
807                  */
808                 b_ret_ent = false;
809                 goto spq_post_fail;
810         }
811
812         OSAL_SPIN_UNLOCK(&p_spq->lock);
813
814         if (p_ent->comp_mode == ECORE_SPQ_MODE_EBLOCK) {
815                 /* For entries in ECORE BLOCK mode, the completion code cannot
816                  * perform the necessary cleanup - if it did, we couldn't
817                  * access p_ent here to see whether it's successful or not.
818                  * Thus, after gaining the answer perform the cleanup here.
819                  */
820                 rc = ecore_spq_block(p_hwfn, p_ent, fw_return_code,
821                                      p_ent->queue == &p_spq->unlimited_pending);
822
823                 if (p_ent->queue == &p_spq->unlimited_pending) {
824                         /* This is an allocated p_ent which does not need to
825                          * return to pool.
826                          */
827                         OSAL_FREE(p_hwfn->p_dev, p_ent);
828
829                         /* TBD: handle error flow and remove p_ent from
830                          * completion pending
831                          */
832                         return rc;
833                 }
834
835                 if (rc)
836                         goto spq_post_fail2;
837
838                 /* return to pool */
839                 ecore_spq_return_entry(p_hwfn, p_ent);
840         }
841         return rc;
842
843 spq_post_fail2:
844         OSAL_SPIN_LOCK(&p_spq->lock);
845         OSAL_LIST_REMOVE_ENTRY(&p_ent->list, &p_spq->completion_pending);
846         ecore_chain_return_produced(&p_spq->chain);
847
848 spq_post_fail:
849         /* return to the free pool */
850         if (b_ret_ent)
851                 __ecore_spq_return_entry(p_hwfn, p_ent);
852         OSAL_SPIN_UNLOCK(&p_spq->lock);
853
854         return rc;
855 }
856
857 enum _ecore_status_t ecore_spq_completion(struct ecore_hwfn *p_hwfn,
858                                           __le16 echo,
859                                           u8 fw_return_code,
860                                           union event_ring_data *p_data)
861 {
862         struct ecore_spq *p_spq;
863         struct ecore_spq_entry *p_ent = OSAL_NULL;
864         struct ecore_spq_entry *tmp;
865         struct ecore_spq_entry *found = OSAL_NULL;
866         enum _ecore_status_t rc;
867
868         if (!p_hwfn)
869                 return ECORE_INVAL;
870
871         p_spq = p_hwfn->p_spq;
872         if (!p_spq)
873                 return ECORE_INVAL;
874
875         OSAL_SPIN_LOCK(&p_spq->lock);
876         OSAL_LIST_FOR_EACH_ENTRY_SAFE(p_ent,
877                                       tmp,
878                                       &p_spq->completion_pending,
879                                       list, struct ecore_spq_entry) {
880                 if (p_ent->elem.hdr.echo == echo) {
881                         OSAL_LIST_REMOVE_ENTRY(&p_ent->list,
882                                                &p_spq->completion_pending);
883
884                         /* Avoid overriding of SPQ entries when getting
885                          * out-of-order completions, by marking the completions
886                          * in a bitmap and increasing the chain consumer only
887                          * for the first successive completed entries.
888                          */
889                         SPQ_COMP_BMAP_SET_BIT(p_spq, echo);
890                         while (SPQ_COMP_BMAP_TEST_BIT(p_spq,
891                                                       p_spq->comp_bitmap_idx)) {
892                                 SPQ_COMP_BMAP_CLEAR_BIT(p_spq,
893                                                         p_spq->comp_bitmap_idx);
894                                 p_spq->comp_bitmap_idx++;
895                                 ecore_chain_return_produced(&p_spq->chain);
896                         }
897
898                         p_spq->comp_count++;
899                         found = p_ent;
900                         break;
901                 }
902
903                 /* This is debug and should be relatively uncommon - depends
904                  * on scenarios which have mutliple per-PF sent ramrods.
905                  */
906                 DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ,
907                            "Got completion for echo %04x - doesn't match"
908                            " echo %04x in completion pending list\n",
909                            OSAL_LE16_TO_CPU(echo),
910                            OSAL_LE16_TO_CPU(p_ent->elem.hdr.echo));
911         }
912
913         /* Release lock before callback, as callback may post
914          * an additional ramrod.
915          */
916         OSAL_SPIN_UNLOCK(&p_spq->lock);
917
918         if (!found) {
919                 DP_NOTICE(p_hwfn, true,
920                           "Failed to find an entry this"
921                           " EQE [echo %04x] completes\n",
922                           OSAL_LE16_TO_CPU(echo));
923                 return ECORE_EXISTS;
924         }
925
926         DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ,
927                    "Complete EQE [echo %04x]: func %p cookie %p)\n",
928                    OSAL_LE16_TO_CPU(echo),
929                    p_ent->comp_cb.function, p_ent->comp_cb.cookie);
930         if (found->comp_cb.function)
931                 found->comp_cb.function(p_hwfn, found->comp_cb.cookie, p_data,
932                                         fw_return_code);
933         else
934                 DP_VERBOSE(p_hwfn, ECORE_MSG_SPQ,
935                            "Got a completion without a callback function\n");
936
937         if ((found->comp_mode != ECORE_SPQ_MODE_EBLOCK) ||
938             (found->queue == &p_spq->unlimited_pending))
939                 /* EBLOCK  is responsible for returning its own entry into the
940                  * free list, unless it originally added the entry into the
941                  * unlimited pending list.
942                  */
943                 ecore_spq_return_entry(p_hwfn, found);
944
945         /* Attempt to post pending requests */
946         OSAL_SPIN_LOCK(&p_spq->lock);
947         rc = ecore_spq_pend_post(p_hwfn);
948         OSAL_SPIN_UNLOCK(&p_spq->lock);
949
950         return rc;
951 }
952
953 enum _ecore_status_t ecore_consq_alloc(struct ecore_hwfn *p_hwfn)
954 {
955         struct ecore_consq *p_consq;
956
957         /* Allocate ConsQ struct */
958         p_consq =
959             OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL, sizeof(*p_consq));
960         if (!p_consq) {
961                 DP_NOTICE(p_hwfn, true,
962                           "Failed to allocate `struct ecore_consq'\n");
963                 return ECORE_NOMEM;
964         }
965
966         /* Allocate and initialize EQ chain */
967         if (ecore_chain_alloc(p_hwfn->p_dev,
968                               ECORE_CHAIN_USE_TO_PRODUCE,
969                               ECORE_CHAIN_MODE_PBL,
970                               ECORE_CHAIN_CNT_TYPE_U16,
971                               ECORE_CHAIN_PAGE_SIZE / 0x80,
972                               0x80,
973                               &p_consq->chain, OSAL_NULL) != ECORE_SUCCESS) {
974                 DP_NOTICE(p_hwfn, true, "Failed to allocate consq chain");
975                 goto consq_allocate_fail;
976         }
977
978         p_hwfn->p_consq = p_consq;
979         return ECORE_SUCCESS;
980
981 consq_allocate_fail:
982         OSAL_FREE(p_hwfn->p_dev, p_consq);
983         return ECORE_NOMEM;
984 }
985
986 void ecore_consq_setup(struct ecore_hwfn *p_hwfn)
987 {
988         ecore_chain_reset(&p_hwfn->p_consq->chain);
989 }
990
991 void ecore_consq_free(struct ecore_hwfn *p_hwfn)
992 {
993         if (!p_hwfn->p_consq)
994                 return;
995
996         ecore_chain_free(p_hwfn->p_dev, &p_hwfn->p_consq->chain);
997         OSAL_FREE(p_hwfn->p_dev, p_hwfn->p_consq);
998 }