net/liquidio: add API to setup Rx queue
[dpdk.git] / drivers / net / liquidio / lio_rxtx.c
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Cavium, Inc.. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Cavium, Inc. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <rte_ethdev.h>
35 #include <rte_cycles.h>
36 #include <rte_malloc.h>
37
38 #include "lio_logs.h"
39 #include "lio_struct.h"
40 #include "lio_ethdev.h"
41 #include "lio_rxtx.h"
42
43 static void
44 lio_dma_zone_free(struct lio_device *lio_dev, const struct rte_memzone *mz)
45 {
46         const struct rte_memzone *mz_tmp;
47         int ret = 0;
48
49         if (mz == NULL) {
50                 lio_dev_err(lio_dev, "Memzone NULL\n");
51                 return;
52         }
53
54         mz_tmp = rte_memzone_lookup(mz->name);
55         if (mz_tmp == NULL) {
56                 lio_dev_err(lio_dev, "Memzone %s Not Found\n", mz->name);
57                 return;
58         }
59
60         ret = rte_memzone_free(mz);
61         if (ret)
62                 lio_dev_err(lio_dev, "Memzone free Failed ret %d\n", ret);
63 }
64
65 /**
66  *  Frees the space for descriptor ring for the droq.
67  *
68  *  @param lio_dev      - pointer to the lio device structure
69  *  @param q_no         - droq no.
70  */
71 static void
72 lio_delete_droq(struct lio_device *lio_dev, uint32_t q_no)
73 {
74         struct lio_droq *droq = lio_dev->droq[q_no];
75
76         lio_dev_dbg(lio_dev, "OQ[%d]\n", q_no);
77
78         rte_free(droq->recv_buf_list);
79         droq->recv_buf_list = NULL;
80         lio_dma_zone_free(lio_dev, droq->info_mz);
81         lio_dma_zone_free(lio_dev, droq->desc_ring_mz);
82
83         memset(droq, 0, LIO_DROQ_SIZE);
84 }
85
86 static void *
87 lio_alloc_info_buffer(struct lio_device *lio_dev,
88                       struct lio_droq *droq, unsigned int socket_id)
89 {
90         droq->info_mz = rte_eth_dma_zone_reserve(lio_dev->eth_dev,
91                                                  "info_list", droq->q_no,
92                                                  (droq->max_count *
93                                                         LIO_DROQ_INFO_SIZE),
94                                                  RTE_CACHE_LINE_SIZE,
95                                                  socket_id);
96
97         if (droq->info_mz == NULL)
98                 return NULL;
99
100         droq->info_list_dma = droq->info_mz->phys_addr;
101         droq->info_alloc_size = droq->info_mz->len;
102         droq->info_base_addr = (size_t)droq->info_mz->addr;
103
104         return droq->info_mz->addr;
105 }
106
107 /**
108  *  Allocates space for the descriptor ring for the droq and
109  *  sets the base addr, num desc etc in Octeon registers.
110  *
111  * @param lio_dev       - pointer to the lio device structure
112  * @param q_no          - droq no.
113  * @param app_ctx       - pointer to application context
114  * @return Success: 0   Failure: -1
115  */
116 static int
117 lio_init_droq(struct lio_device *lio_dev, uint32_t q_no,
118               uint32_t num_descs, uint32_t desc_size,
119               struct rte_mempool *mpool, unsigned int socket_id)
120 {
121         uint32_t c_refill_threshold;
122         uint32_t desc_ring_size;
123         struct lio_droq *droq;
124
125         lio_dev_dbg(lio_dev, "OQ[%d]\n", q_no);
126
127         droq = lio_dev->droq[q_no];
128         droq->lio_dev = lio_dev;
129         droq->q_no = q_no;
130         droq->mpool = mpool;
131
132         c_refill_threshold = LIO_OQ_REFILL_THRESHOLD_CFG(lio_dev);
133
134         droq->max_count = num_descs;
135         droq->buffer_size = desc_size;
136
137         desc_ring_size = droq->max_count * LIO_DROQ_DESC_SIZE;
138         droq->desc_ring_mz = rte_eth_dma_zone_reserve(lio_dev->eth_dev,
139                                                       "droq", q_no,
140                                                       desc_ring_size,
141                                                       RTE_CACHE_LINE_SIZE,
142                                                       socket_id);
143
144         if (droq->desc_ring_mz == NULL) {
145                 lio_dev_err(lio_dev,
146                             "Output queue %d ring alloc failed\n", q_no);
147                 return -1;
148         }
149
150         droq->desc_ring_dma = droq->desc_ring_mz->phys_addr;
151         droq->desc_ring = (struct lio_droq_desc *)droq->desc_ring_mz->addr;
152
153         lio_dev_dbg(lio_dev, "droq[%d]: desc_ring: virt: 0x%p, dma: %lx\n",
154                     q_no, droq->desc_ring, (unsigned long)droq->desc_ring_dma);
155         lio_dev_dbg(lio_dev, "droq[%d]: num_desc: %d\n", q_no,
156                     droq->max_count);
157
158         droq->info_list = lio_alloc_info_buffer(lio_dev, droq, socket_id);
159         if (droq->info_list == NULL) {
160                 lio_dev_err(lio_dev, "Cannot allocate memory for info list.\n");
161                 goto init_droq_fail;
162         }
163
164         droq->recv_buf_list = rte_zmalloc_socket("recv_buf_list",
165                                                  (droq->max_count *
166                                                         LIO_DROQ_RECVBUF_SIZE),
167                                                  RTE_CACHE_LINE_SIZE,
168                                                  socket_id);
169         if (droq->recv_buf_list == NULL) {
170                 lio_dev_err(lio_dev,
171                             "Output queue recv buf list alloc failed\n");
172                 goto init_droq_fail;
173         }
174
175         droq->refill_threshold = c_refill_threshold;
176
177         rte_spinlock_init(&droq->lock);
178
179         lio_dev->io_qmask.oq |= (1ULL << q_no);
180
181         return 0;
182
183 init_droq_fail:
184         lio_delete_droq(lio_dev, q_no);
185
186         return -1;
187 }
188
189 int
190 lio_setup_droq(struct lio_device *lio_dev, int oq_no, int num_descs,
191                int desc_size, struct rte_mempool *mpool, unsigned int socket_id)
192 {
193         struct lio_droq *droq;
194
195         PMD_INIT_FUNC_TRACE();
196
197         if (lio_dev->droq[oq_no]) {
198                 lio_dev_dbg(lio_dev, "Droq %d in use\n", oq_no);
199                 return 0;
200         }
201
202         /* Allocate the DS for the new droq. */
203         droq = rte_zmalloc_socket("ethdev RX queue", sizeof(*droq),
204                                   RTE_CACHE_LINE_SIZE, socket_id);
205         if (droq == NULL)
206                 return -ENOMEM;
207
208         lio_dev->droq[oq_no] = droq;
209
210         /* Initialize the Droq */
211         if (lio_init_droq(lio_dev, oq_no, num_descs, desc_size, mpool,
212                           socket_id)) {
213                 lio_dev_err(lio_dev, "Droq[%u] Initialization Failed\n", oq_no);
214                 rte_free(lio_dev->droq[oq_no]);
215                 lio_dev->droq[oq_no] = NULL;
216                 return -ENOMEM;
217         }
218
219         lio_dev->num_oqs++;
220
221         lio_dev_dbg(lio_dev, "Total number of OQ: %d\n", lio_dev->num_oqs);
222
223         /* Send credit for octeon output queues. credits are always
224          * sent after the output queue is enabled.
225          */
226         rte_write32(lio_dev->droq[oq_no]->max_count,
227                     lio_dev->droq[oq_no]->pkts_credit_reg);
228         rte_wmb();
229
230         return 0;
231 }
232
233 /**
234  *  lio_init_instr_queue()
235  *  @param lio_dev      - pointer to the lio device structure.
236  *  @param txpciq       - queue to be initialized.
237  *
238  *  Called at driver init time for each input queue. iq_conf has the
239  *  configuration parameters for the queue.
240  *
241  *  @return  Success: 0 Failure: -1
242  */
243 static int
244 lio_init_instr_queue(struct lio_device *lio_dev,
245                      union octeon_txpciq txpciq,
246                      uint32_t num_descs, unsigned int socket_id)
247 {
248         uint32_t iq_no = (uint32_t)txpciq.s.q_no;
249         struct lio_instr_queue *iq;
250         uint32_t instr_type;
251         uint32_t q_size;
252
253         instr_type = LIO_IQ_INSTR_TYPE(lio_dev);
254
255         q_size = instr_type * num_descs;
256         iq = lio_dev->instr_queue[iq_no];
257         iq->iq_mz = rte_eth_dma_zone_reserve(lio_dev->eth_dev,
258                                              "instr_queue", iq_no, q_size,
259                                              RTE_CACHE_LINE_SIZE,
260                                              socket_id);
261         if (iq->iq_mz == NULL) {
262                 lio_dev_err(lio_dev, "Cannot allocate memory for instr queue %d\n",
263                             iq_no);
264                 return -1;
265         }
266
267         iq->base_addr_dma = iq->iq_mz->phys_addr;
268         iq->base_addr = (uint8_t *)iq->iq_mz->addr;
269
270         iq->max_count = num_descs;
271
272         /* Initialize a list to holds requests that have been posted to Octeon
273          * but has yet to be fetched by octeon
274          */
275         iq->request_list = rte_zmalloc_socket("request_list",
276                                               sizeof(*iq->request_list) *
277                                                         num_descs,
278                                               RTE_CACHE_LINE_SIZE,
279                                               socket_id);
280         if (iq->request_list == NULL) {
281                 lio_dev_err(lio_dev, "Alloc failed for IQ[%d] nr free list\n",
282                             iq_no);
283                 lio_dma_zone_free(lio_dev, iq->iq_mz);
284                 return -1;
285         }
286
287         lio_dev_dbg(lio_dev, "IQ[%d]: base: %p basedma: %lx count: %d\n",
288                     iq_no, iq->base_addr, (unsigned long)iq->base_addr_dma,
289                     iq->max_count);
290
291         iq->lio_dev = lio_dev;
292         iq->txpciq.txpciq64 = txpciq.txpciq64;
293         iq->fill_cnt = 0;
294         iq->host_write_index = 0;
295         iq->lio_read_index = 0;
296         iq->flush_index = 0;
297
298         rte_atomic64_set(&iq->instr_pending, 0);
299
300         /* Initialize the spinlock for this instruction queue */
301         rte_spinlock_init(&iq->lock);
302         rte_spinlock_init(&iq->post_lock);
303
304         rte_atomic64_clear(&iq->iq_flush_running);
305
306         lio_dev->io_qmask.iq |= (1ULL << iq_no);
307
308         /* Set the 32B/64B mode for each input queue */
309         lio_dev->io_qmask.iq64B |= ((instr_type == 64) << iq_no);
310         iq->iqcmd_64B = (instr_type == 64);
311
312         lio_dev->fn_list.setup_iq_regs(lio_dev, iq_no);
313
314         return 0;
315 }
316
317 int
318 lio_setup_instr_queue0(struct lio_device *lio_dev)
319 {
320         union octeon_txpciq txpciq;
321         uint32_t num_descs = 0;
322         uint32_t iq_no = 0;
323
324         num_descs = LIO_NUM_DEF_TX_DESCS_CFG(lio_dev);
325
326         lio_dev->num_iqs = 0;
327
328         lio_dev->instr_queue[0] = rte_zmalloc(NULL,
329                                         sizeof(struct lio_instr_queue), 0);
330         if (lio_dev->instr_queue[0] == NULL)
331                 return -ENOMEM;
332
333         lio_dev->instr_queue[0]->q_index = 0;
334         lio_dev->instr_queue[0]->app_ctx = (void *)(size_t)0;
335         txpciq.txpciq64 = 0;
336         txpciq.s.q_no = iq_no;
337         txpciq.s.pkind = lio_dev->pfvf_hsword.pkind;
338         txpciq.s.use_qpg = 0;
339         txpciq.s.qpg = 0;
340         if (lio_init_instr_queue(lio_dev, txpciq, num_descs, SOCKET_ID_ANY)) {
341                 rte_free(lio_dev->instr_queue[0]);
342                 lio_dev->instr_queue[0] = NULL;
343                 return -1;
344         }
345
346         lio_dev->num_iqs++;
347
348         return 0;
349 }
350
351 /**
352  *  lio_delete_instr_queue()
353  *  @param lio_dev      - pointer to the lio device structure.
354  *  @param iq_no        - queue to be deleted.
355  *
356  *  Called at driver unload time for each input queue. Deletes all
357  *  allocated resources for the input queue.
358  */
359 static void
360 lio_delete_instr_queue(struct lio_device *lio_dev, uint32_t iq_no)
361 {
362         struct lio_instr_queue *iq = lio_dev->instr_queue[iq_no];
363
364         rte_free(iq->request_list);
365         iq->request_list = NULL;
366         lio_dma_zone_free(lio_dev, iq->iq_mz);
367 }
368
369 void
370 lio_free_instr_queue0(struct lio_device *lio_dev)
371 {
372         lio_delete_instr_queue(lio_dev, 0);
373         rte_free(lio_dev->instr_queue[0]);
374         lio_dev->instr_queue[0] = NULL;
375         lio_dev->num_iqs--;
376 }
377
378 static inline void
379 lio_ring_doorbell(struct lio_device *lio_dev,
380                   struct lio_instr_queue *iq)
381 {
382         if (rte_atomic64_read(&lio_dev->status) == LIO_DEV_RUNNING) {
383                 rte_write32(iq->fill_cnt, iq->doorbell_reg);
384                 /* make sure doorbell write goes through */
385                 rte_wmb();
386                 iq->fill_cnt = 0;
387         }
388 }
389
390 static inline void
391 copy_cmd_into_iq(struct lio_instr_queue *iq, uint8_t *cmd)
392 {
393         uint8_t *iqptr, cmdsize;
394
395         cmdsize = ((iq->iqcmd_64B) ? 64 : 32);
396         iqptr = iq->base_addr + (cmdsize * iq->host_write_index);
397
398         rte_memcpy(iqptr, cmd, cmdsize);
399 }
400
401 static inline struct lio_iq_post_status
402 post_command2(struct lio_instr_queue *iq, uint8_t *cmd)
403 {
404         struct lio_iq_post_status st;
405
406         st.status = LIO_IQ_SEND_OK;
407
408         /* This ensures that the read index does not wrap around to the same
409          * position if queue gets full before Octeon could fetch any instr.
410          */
411         if (rte_atomic64_read(&iq->instr_pending) >=
412                         (int32_t)(iq->max_count - 1)) {
413                 st.status = LIO_IQ_SEND_FAILED;
414                 st.index = -1;
415                 return st;
416         }
417
418         if (rte_atomic64_read(&iq->instr_pending) >=
419                         (int32_t)(iq->max_count - 2))
420                 st.status = LIO_IQ_SEND_STOP;
421
422         copy_cmd_into_iq(iq, cmd);
423
424         /* "index" is returned, host_write_index is modified. */
425         st.index = iq->host_write_index;
426         iq->host_write_index = lio_incr_index(iq->host_write_index, 1,
427                                               iq->max_count);
428         iq->fill_cnt++;
429
430         /* Flush the command into memory. We need to be sure the data is in
431          * memory before indicating that the instruction is pending.
432          */
433         rte_wmb();
434
435         rte_atomic64_inc(&iq->instr_pending);
436
437         return st;
438 }
439
440 static inline void
441 lio_add_to_request_list(struct lio_instr_queue *iq,
442                         int idx, void *buf, int reqtype)
443 {
444         iq->request_list[idx].buf = buf;
445         iq->request_list[idx].reqtype = reqtype;
446 }
447
448 static int
449 lio_send_command(struct lio_device *lio_dev, uint32_t iq_no, void *cmd,
450                  void *buf, uint32_t datasize __rte_unused, uint32_t reqtype)
451 {
452         struct lio_instr_queue *iq = lio_dev->instr_queue[iq_no];
453         struct lio_iq_post_status st;
454
455         rte_spinlock_lock(&iq->post_lock);
456
457         st = post_command2(iq, cmd);
458
459         if (st.status != LIO_IQ_SEND_FAILED) {
460                 lio_add_to_request_list(iq, st.index, buf, reqtype);
461                 lio_ring_doorbell(lio_dev, iq);
462         }
463
464         rte_spinlock_unlock(&iq->post_lock);
465
466         return st.status;
467 }
468
469 void
470 lio_prepare_soft_command(struct lio_device *lio_dev,
471                          struct lio_soft_command *sc, uint8_t opcode,
472                          uint8_t subcode, uint32_t irh_ossp, uint64_t ossp0,
473                          uint64_t ossp1)
474 {
475         struct octeon_instr_pki_ih3 *pki_ih3;
476         struct octeon_instr_ih3 *ih3;
477         struct octeon_instr_irh *irh;
478         struct octeon_instr_rdp *rdp;
479
480         RTE_ASSERT(opcode <= 15);
481         RTE_ASSERT(subcode <= 127);
482
483         ih3       = (struct octeon_instr_ih3 *)&sc->cmd.cmd3.ih3;
484
485         ih3->pkind = lio_dev->instr_queue[sc->iq_no]->txpciq.s.pkind;
486
487         pki_ih3 = (struct octeon_instr_pki_ih3 *)&sc->cmd.cmd3.pki_ih3;
488
489         pki_ih3->w      = 1;
490         pki_ih3->raw    = 1;
491         pki_ih3->utag   = 1;
492         pki_ih3->uqpg   = lio_dev->instr_queue[sc->iq_no]->txpciq.s.use_qpg;
493         pki_ih3->utt    = 1;
494
495         pki_ih3->tag    = LIO_CONTROL;
496         pki_ih3->tagtype = OCTEON_ATOMIC_TAG;
497         pki_ih3->qpg    = lio_dev->instr_queue[sc->iq_no]->txpciq.s.qpg;
498         pki_ih3->pm     = 0x7;
499         pki_ih3->sl     = 8;
500
501         if (sc->datasize)
502                 ih3->dlengsz = sc->datasize;
503
504         irh             = (struct octeon_instr_irh *)&sc->cmd.cmd3.irh;
505         irh->opcode     = opcode;
506         irh->subcode    = subcode;
507
508         /* opcode/subcode specific parameters (ossp) */
509         irh->ossp = irh_ossp;
510         sc->cmd.cmd3.ossp[0] = ossp0;
511         sc->cmd.cmd3.ossp[1] = ossp1;
512
513         if (sc->rdatasize) {
514                 rdp = (struct octeon_instr_rdp *)&sc->cmd.cmd3.rdp;
515                 rdp->pcie_port = lio_dev->pcie_port;
516                 rdp->rlen      = sc->rdatasize;
517                 irh->rflag = 1;
518                 /* PKI IH3 */
519                 ih3->fsz    = OCTEON_SOFT_CMD_RESP_IH3;
520         } else {
521                 irh->rflag = 0;
522                 /* PKI IH3 */
523                 ih3->fsz    = OCTEON_PCI_CMD_O3;
524         }
525 }
526
527 int
528 lio_send_soft_command(struct lio_device *lio_dev,
529                       struct lio_soft_command *sc)
530 {
531         struct octeon_instr_ih3 *ih3;
532         struct octeon_instr_irh *irh;
533         uint32_t len = 0;
534
535         ih3 = (struct octeon_instr_ih3 *)&sc->cmd.cmd3.ih3;
536         if (ih3->dlengsz) {
537                 RTE_ASSERT(sc->dmadptr);
538                 sc->cmd.cmd3.dptr = sc->dmadptr;
539         }
540
541         irh = (struct octeon_instr_irh *)&sc->cmd.cmd3.irh;
542         if (irh->rflag) {
543                 RTE_ASSERT(sc->dmarptr);
544                 RTE_ASSERT(sc->status_word != NULL);
545                 *sc->status_word = LIO_COMPLETION_WORD_INIT;
546                 sc->cmd.cmd3.rptr = sc->dmarptr;
547         }
548
549         len = (uint32_t)ih3->dlengsz;
550
551         if (sc->wait_time)
552                 sc->timeout = lio_uptime + sc->wait_time;
553
554         return lio_send_command(lio_dev, sc->iq_no, &sc->cmd, sc, len,
555                                 LIO_REQTYPE_SOFT_COMMAND);
556 }
557
558 int
559 lio_setup_sc_buffer_pool(struct lio_device *lio_dev)
560 {
561         char sc_pool_name[RTE_MEMPOOL_NAMESIZE];
562         uint16_t buf_size;
563
564         buf_size = LIO_SOFT_COMMAND_BUFFER_SIZE + RTE_PKTMBUF_HEADROOM;
565         snprintf(sc_pool_name, sizeof(sc_pool_name),
566                  "lio_sc_pool_%u", lio_dev->port_id);
567         lio_dev->sc_buf_pool = rte_pktmbuf_pool_create(sc_pool_name,
568                                                 LIO_MAX_SOFT_COMMAND_BUFFERS,
569                                                 0, 0, buf_size, SOCKET_ID_ANY);
570         return 0;
571 }
572
573 void
574 lio_free_sc_buffer_pool(struct lio_device *lio_dev)
575 {
576         rte_mempool_free(lio_dev->sc_buf_pool);
577 }
578
579 struct lio_soft_command *
580 lio_alloc_soft_command(struct lio_device *lio_dev, uint32_t datasize,
581                        uint32_t rdatasize, uint32_t ctxsize)
582 {
583         uint32_t offset = sizeof(struct lio_soft_command);
584         struct lio_soft_command *sc;
585         struct rte_mbuf *m;
586         uint64_t dma_addr;
587
588         RTE_ASSERT((offset + datasize + rdatasize + ctxsize) <=
589                    LIO_SOFT_COMMAND_BUFFER_SIZE);
590
591         m = rte_pktmbuf_alloc(lio_dev->sc_buf_pool);
592         if (m == NULL) {
593                 lio_dev_err(lio_dev, "Cannot allocate mbuf for sc\n");
594                 return NULL;
595         }
596
597         /* set rte_mbuf data size and there is only 1 segment */
598         m->pkt_len = LIO_SOFT_COMMAND_BUFFER_SIZE;
599         m->data_len = LIO_SOFT_COMMAND_BUFFER_SIZE;
600
601         /* use rte_mbuf buffer for soft command */
602         sc = rte_pktmbuf_mtod(m, struct lio_soft_command *);
603         memset(sc, 0, LIO_SOFT_COMMAND_BUFFER_SIZE);
604         sc->size = LIO_SOFT_COMMAND_BUFFER_SIZE;
605         sc->dma_addr = rte_mbuf_data_dma_addr(m);
606         sc->mbuf = m;
607
608         dma_addr = sc->dma_addr;
609
610         if (ctxsize) {
611                 sc->ctxptr = (uint8_t *)sc + offset;
612                 sc->ctxsize = ctxsize;
613         }
614
615         /* Start data at 128 byte boundary */
616         offset = (offset + ctxsize + 127) & 0xffffff80;
617
618         if (datasize) {
619                 sc->virtdptr = (uint8_t *)sc + offset;
620                 sc->dmadptr = dma_addr + offset;
621                 sc->datasize = datasize;
622         }
623
624         /* Start rdata at 128 byte boundary */
625         offset = (offset + datasize + 127) & 0xffffff80;
626
627         if (rdatasize) {
628                 RTE_ASSERT(rdatasize >= 16);
629                 sc->virtrptr = (uint8_t *)sc + offset;
630                 sc->dmarptr = dma_addr + offset;
631                 sc->rdatasize = rdatasize;
632                 sc->status_word = (uint64_t *)((uint8_t *)(sc->virtrptr) +
633                                                rdatasize - 8);
634         }
635
636         return sc;
637 }
638
639 void
640 lio_free_soft_command(struct lio_soft_command *sc)
641 {
642         rte_pktmbuf_free(sc->mbuf);
643 }
644
645 void
646 lio_setup_response_list(struct lio_device *lio_dev)
647 {
648         STAILQ_INIT(&lio_dev->response_list.head);
649         rte_spinlock_init(&lio_dev->response_list.lock);
650         rte_atomic64_set(&lio_dev->response_list.pending_req_count, 0);
651 }
652
653 int
654 lio_process_ordered_list(struct lio_device *lio_dev)
655 {
656         int resp_to_process = LIO_MAX_ORD_REQS_TO_PROCESS;
657         struct lio_response_list *ordered_sc_list;
658         struct lio_soft_command *sc;
659         int request_complete = 0;
660         uint64_t status64;
661         uint32_t status;
662
663         ordered_sc_list = &lio_dev->response_list;
664
665         do {
666                 rte_spinlock_lock(&ordered_sc_list->lock);
667
668                 if (STAILQ_EMPTY(&ordered_sc_list->head)) {
669                         /* ordered_sc_list is empty; there is
670                          * nothing to process
671                          */
672                         rte_spinlock_unlock(&ordered_sc_list->lock);
673                         return -1;
674                 }
675
676                 sc = LIO_STQUEUE_FIRST_ENTRY(&ordered_sc_list->head,
677                                              struct lio_soft_command, node);
678
679                 status = LIO_REQUEST_PENDING;
680
681                 /* check if octeon has finished DMA'ing a response
682                  * to where rptr is pointing to
683                  */
684                 status64 = *sc->status_word;
685
686                 if (status64 != LIO_COMPLETION_WORD_INIT) {
687                         /* This logic ensures that all 64b have been written.
688                          * 1. check byte 0 for non-FF
689                          * 2. if non-FF, then swap result from BE to host order
690                          * 3. check byte 7 (swapped to 0) for non-FF
691                          * 4. if non-FF, use the low 32-bit status code
692                          * 5. if either byte 0 or byte 7 is FF, don't use status
693                          */
694                         if ((status64 & 0xff) != 0xff) {
695                                 lio_swap_8B_data(&status64, 1);
696                                 if (((status64 & 0xff) != 0xff)) {
697                                         /* retrieve 16-bit firmware status */
698                                         status = (uint32_t)(status64 &
699                                                             0xffffULL);
700                                         if (status) {
701                                                 status =
702                                                 LIO_FIRMWARE_STATUS_CODE(
703                                                                         status);
704                                         } else {
705                                                 /* i.e. no error */
706                                                 status = LIO_REQUEST_DONE;
707                                         }
708                                 }
709                         }
710                 } else if ((sc->timeout && lio_check_timeout(lio_uptime,
711                                                              sc->timeout))) {
712                         lio_dev_err(lio_dev,
713                                     "cmd failed, timeout (%ld, %ld)\n",
714                                     (long)lio_uptime, (long)sc->timeout);
715                         status = LIO_REQUEST_TIMEOUT;
716                 }
717
718                 if (status != LIO_REQUEST_PENDING) {
719                         /* we have received a response or we have timed out.
720                          * remove node from linked list
721                          */
722                         STAILQ_REMOVE(&ordered_sc_list->head,
723                                       &sc->node, lio_stailq_node, entries);
724                         rte_atomic64_dec(
725                             &lio_dev->response_list.pending_req_count);
726                         rte_spinlock_unlock(&ordered_sc_list->lock);
727
728                         if (sc->callback)
729                                 sc->callback(status, sc->callback_arg);
730
731                         request_complete++;
732                 } else {
733                         /* no response yet */
734                         request_complete = 0;
735                         rte_spinlock_unlock(&ordered_sc_list->lock);
736                 }
737
738                 /* If we hit the Max Ordered requests to process every loop,
739                  * we quit and let this function be invoked the next time
740                  * the poll thread runs to process the remaining requests.
741                  * This function can take up the entire CPU if there is
742                  * no upper limit to the requests processed.
743                  */
744                 if (request_complete >= resp_to_process)
745                         break;
746         } while (request_complete);
747
748         return 0;
749 }