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