net/liquidio: support Tx stats
[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 #define LIO_MAX_SG 12
44 /* Flush iq if available tx_desc fall below LIO_FLUSH_WM */
45 #define LIO_FLUSH_WM(_iq) ((_iq)->max_count / 2)
46 #define LIO_PKT_IN_DONE_CNT_MASK 0x00000000FFFFFFFFULL
47
48 static void
49 lio_droq_compute_max_packet_bufs(struct lio_droq *droq)
50 {
51         uint32_t count = 0;
52
53         do {
54                 count += droq->buffer_size;
55         } while (count < LIO_MAX_RX_PKTLEN);
56 }
57
58 static void
59 lio_droq_reset_indices(struct lio_droq *droq)
60 {
61         droq->read_idx  = 0;
62         droq->write_idx = 0;
63         droq->refill_idx = 0;
64         droq->refill_count = 0;
65         rte_atomic64_set(&droq->pkts_pending, 0);
66 }
67
68 static void
69 lio_droq_destroy_ring_buffers(struct lio_droq *droq)
70 {
71         uint32_t i;
72
73         for (i = 0; i < droq->max_count; i++) {
74                 if (droq->recv_buf_list[i].buffer) {
75                         rte_pktmbuf_free((struct rte_mbuf *)
76                                          droq->recv_buf_list[i].buffer);
77                         droq->recv_buf_list[i].buffer = NULL;
78                 }
79         }
80
81         lio_droq_reset_indices(droq);
82 }
83
84 static void *
85 lio_recv_buffer_alloc(struct lio_device *lio_dev, int q_no)
86 {
87         struct lio_droq *droq = lio_dev->droq[q_no];
88         struct rte_mempool *mpool = droq->mpool;
89         struct rte_mbuf *m;
90
91         m = rte_pktmbuf_alloc(mpool);
92         if (m == NULL) {
93                 lio_dev_err(lio_dev, "Cannot allocate\n");
94                 return NULL;
95         }
96
97         rte_mbuf_refcnt_set(m, 1);
98         m->next = NULL;
99         m->data_off = RTE_PKTMBUF_HEADROOM;
100         m->nb_segs = 1;
101         m->pool = mpool;
102
103         return m;
104 }
105
106 static int
107 lio_droq_setup_ring_buffers(struct lio_device *lio_dev,
108                             struct lio_droq *droq)
109 {
110         struct lio_droq_desc *desc_ring = droq->desc_ring;
111         uint32_t i;
112         void *buf;
113
114         for (i = 0; i < droq->max_count; i++) {
115                 buf = lio_recv_buffer_alloc(lio_dev, droq->q_no);
116                 if (buf == NULL) {
117                         lio_dev_err(lio_dev, "buffer alloc failed\n");
118                         droq->stats.rx_alloc_failure++;
119                         lio_droq_destroy_ring_buffers(droq);
120                         return -ENOMEM;
121                 }
122
123                 droq->recv_buf_list[i].buffer = buf;
124                 droq->info_list[i].length = 0;
125
126                 /* map ring buffers into memory */
127                 desc_ring[i].info_ptr = lio_map_ring_info(droq, i);
128                 desc_ring[i].buffer_ptr =
129                         lio_map_ring(droq->recv_buf_list[i].buffer);
130         }
131
132         lio_droq_reset_indices(droq);
133
134         lio_droq_compute_max_packet_bufs(droq);
135
136         return 0;
137 }
138
139 static void
140 lio_dma_zone_free(struct lio_device *lio_dev, const struct rte_memzone *mz)
141 {
142         const struct rte_memzone *mz_tmp;
143         int ret = 0;
144
145         if (mz == NULL) {
146                 lio_dev_err(lio_dev, "Memzone NULL\n");
147                 return;
148         }
149
150         mz_tmp = rte_memzone_lookup(mz->name);
151         if (mz_tmp == NULL) {
152                 lio_dev_err(lio_dev, "Memzone %s Not Found\n", mz->name);
153                 return;
154         }
155
156         ret = rte_memzone_free(mz);
157         if (ret)
158                 lio_dev_err(lio_dev, "Memzone free Failed ret %d\n", ret);
159 }
160
161 /**
162  *  Frees the space for descriptor ring for the droq.
163  *
164  *  @param lio_dev      - pointer to the lio device structure
165  *  @param q_no         - droq no.
166  */
167 static void
168 lio_delete_droq(struct lio_device *lio_dev, uint32_t q_no)
169 {
170         struct lio_droq *droq = lio_dev->droq[q_no];
171
172         lio_dev_dbg(lio_dev, "OQ[%d]\n", q_no);
173
174         lio_droq_destroy_ring_buffers(droq);
175         rte_free(droq->recv_buf_list);
176         droq->recv_buf_list = NULL;
177         lio_dma_zone_free(lio_dev, droq->info_mz);
178         lio_dma_zone_free(lio_dev, droq->desc_ring_mz);
179
180         memset(droq, 0, LIO_DROQ_SIZE);
181 }
182
183 static void *
184 lio_alloc_info_buffer(struct lio_device *lio_dev,
185                       struct lio_droq *droq, unsigned int socket_id)
186 {
187         droq->info_mz = rte_eth_dma_zone_reserve(lio_dev->eth_dev,
188                                                  "info_list", droq->q_no,
189                                                  (droq->max_count *
190                                                         LIO_DROQ_INFO_SIZE),
191                                                  RTE_CACHE_LINE_SIZE,
192                                                  socket_id);
193
194         if (droq->info_mz == NULL)
195                 return NULL;
196
197         droq->info_list_dma = droq->info_mz->phys_addr;
198         droq->info_alloc_size = droq->info_mz->len;
199         droq->info_base_addr = (size_t)droq->info_mz->addr;
200
201         return droq->info_mz->addr;
202 }
203
204 /**
205  *  Allocates space for the descriptor ring for the droq and
206  *  sets the base addr, num desc etc in Octeon registers.
207  *
208  * @param lio_dev       - pointer to the lio device structure
209  * @param q_no          - droq no.
210  * @param app_ctx       - pointer to application context
211  * @return Success: 0   Failure: -1
212  */
213 static int
214 lio_init_droq(struct lio_device *lio_dev, uint32_t q_no,
215               uint32_t num_descs, uint32_t desc_size,
216               struct rte_mempool *mpool, unsigned int socket_id)
217 {
218         uint32_t c_refill_threshold;
219         uint32_t desc_ring_size;
220         struct lio_droq *droq;
221
222         lio_dev_dbg(lio_dev, "OQ[%d]\n", q_no);
223
224         droq = lio_dev->droq[q_no];
225         droq->lio_dev = lio_dev;
226         droq->q_no = q_no;
227         droq->mpool = mpool;
228
229         c_refill_threshold = LIO_OQ_REFILL_THRESHOLD_CFG(lio_dev);
230
231         droq->max_count = num_descs;
232         droq->buffer_size = desc_size;
233
234         desc_ring_size = droq->max_count * LIO_DROQ_DESC_SIZE;
235         droq->desc_ring_mz = rte_eth_dma_zone_reserve(lio_dev->eth_dev,
236                                                       "droq", q_no,
237                                                       desc_ring_size,
238                                                       RTE_CACHE_LINE_SIZE,
239                                                       socket_id);
240
241         if (droq->desc_ring_mz == NULL) {
242                 lio_dev_err(lio_dev,
243                             "Output queue %d ring alloc failed\n", q_no);
244                 return -1;
245         }
246
247         droq->desc_ring_dma = droq->desc_ring_mz->phys_addr;
248         droq->desc_ring = (struct lio_droq_desc *)droq->desc_ring_mz->addr;
249
250         lio_dev_dbg(lio_dev, "droq[%d]: desc_ring: virt: 0x%p, dma: %lx\n",
251                     q_no, droq->desc_ring, (unsigned long)droq->desc_ring_dma);
252         lio_dev_dbg(lio_dev, "droq[%d]: num_desc: %d\n", q_no,
253                     droq->max_count);
254
255         droq->info_list = lio_alloc_info_buffer(lio_dev, droq, socket_id);
256         if (droq->info_list == NULL) {
257                 lio_dev_err(lio_dev, "Cannot allocate memory for info list.\n");
258                 goto init_droq_fail;
259         }
260
261         droq->recv_buf_list = rte_zmalloc_socket("recv_buf_list",
262                                                  (droq->max_count *
263                                                         LIO_DROQ_RECVBUF_SIZE),
264                                                  RTE_CACHE_LINE_SIZE,
265                                                  socket_id);
266         if (droq->recv_buf_list == NULL) {
267                 lio_dev_err(lio_dev,
268                             "Output queue recv buf list alloc failed\n");
269                 goto init_droq_fail;
270         }
271
272         if (lio_droq_setup_ring_buffers(lio_dev, droq))
273                 goto init_droq_fail;
274
275         droq->refill_threshold = c_refill_threshold;
276
277         rte_spinlock_init(&droq->lock);
278
279         lio_dev->fn_list.setup_oq_regs(lio_dev, q_no);
280
281         lio_dev->io_qmask.oq |= (1ULL << q_no);
282
283         return 0;
284
285 init_droq_fail:
286         lio_delete_droq(lio_dev, q_no);
287
288         return -1;
289 }
290
291 int
292 lio_setup_droq(struct lio_device *lio_dev, int oq_no, int num_descs,
293                int desc_size, struct rte_mempool *mpool, unsigned int socket_id)
294 {
295         struct lio_droq *droq;
296
297         PMD_INIT_FUNC_TRACE();
298
299         if (lio_dev->droq[oq_no]) {
300                 lio_dev_dbg(lio_dev, "Droq %d in use\n", oq_no);
301                 return 0;
302         }
303
304         /* Allocate the DS for the new droq. */
305         droq = rte_zmalloc_socket("ethdev RX queue", sizeof(*droq),
306                                   RTE_CACHE_LINE_SIZE, socket_id);
307         if (droq == NULL)
308                 return -ENOMEM;
309
310         lio_dev->droq[oq_no] = droq;
311
312         /* Initialize the Droq */
313         if (lio_init_droq(lio_dev, oq_no, num_descs, desc_size, mpool,
314                           socket_id)) {
315                 lio_dev_err(lio_dev, "Droq[%u] Initialization Failed\n", oq_no);
316                 rte_free(lio_dev->droq[oq_no]);
317                 lio_dev->droq[oq_no] = NULL;
318                 return -ENOMEM;
319         }
320
321         lio_dev->num_oqs++;
322
323         lio_dev_dbg(lio_dev, "Total number of OQ: %d\n", lio_dev->num_oqs);
324
325         /* Send credit for octeon output queues. credits are always
326          * sent after the output queue is enabled.
327          */
328         rte_write32(lio_dev->droq[oq_no]->max_count,
329                     lio_dev->droq[oq_no]->pkts_credit_reg);
330         rte_wmb();
331
332         return 0;
333 }
334
335 static inline uint32_t
336 lio_droq_get_bufcount(uint32_t buf_size, uint32_t total_len)
337 {
338         uint32_t buf_cnt = 0;
339
340         while (total_len > (buf_size * buf_cnt))
341                 buf_cnt++;
342
343         return buf_cnt;
344 }
345
346 /* If we were not able to refill all buffers, try to move around
347  * the buffers that were not dispatched.
348  */
349 static inline uint32_t
350 lio_droq_refill_pullup_descs(struct lio_droq *droq,
351                              struct lio_droq_desc *desc_ring)
352 {
353         uint32_t refill_index = droq->refill_idx;
354         uint32_t desc_refilled = 0;
355
356         while (refill_index != droq->read_idx) {
357                 if (droq->recv_buf_list[refill_index].buffer) {
358                         droq->recv_buf_list[droq->refill_idx].buffer =
359                                 droq->recv_buf_list[refill_index].buffer;
360                         desc_ring[droq->refill_idx].buffer_ptr =
361                                 desc_ring[refill_index].buffer_ptr;
362                         droq->recv_buf_list[refill_index].buffer = NULL;
363                         desc_ring[refill_index].buffer_ptr = 0;
364                         do {
365                                 droq->refill_idx = lio_incr_index(
366                                                         droq->refill_idx, 1,
367                                                         droq->max_count);
368                                 desc_refilled++;
369                                 droq->refill_count--;
370                         } while (droq->recv_buf_list[droq->refill_idx].buffer);
371                 }
372                 refill_index = lio_incr_index(refill_index, 1,
373                                               droq->max_count);
374         }       /* while */
375
376         return desc_refilled;
377 }
378
379 /* lio_droq_refill
380  *
381  * @param lio_dev       - pointer to the lio device structure
382  * @param droq          - droq in which descriptors require new buffers.
383  *
384  * Description:
385  *  Called during normal DROQ processing in interrupt mode or by the poll
386  *  thread to refill the descriptors from which buffers were dispatched
387  *  to upper layers. Attempts to allocate new buffers. If that fails, moves
388  *  up buffers (that were not dispatched) to form a contiguous ring.
389  *
390  * Returns:
391  *  No of descriptors refilled.
392  *
393  * Locks:
394  * This routine is called with droq->lock held.
395  */
396 static uint32_t
397 lio_droq_refill(struct lio_device *lio_dev, struct lio_droq *droq)
398 {
399         struct lio_droq_desc *desc_ring;
400         uint32_t desc_refilled = 0;
401         void *buf = NULL;
402
403         desc_ring = droq->desc_ring;
404
405         while (droq->refill_count && (desc_refilled < droq->max_count)) {
406                 /* If a valid buffer exists (happens if there is no dispatch),
407                  * reuse the buffer, else allocate.
408                  */
409                 if (droq->recv_buf_list[droq->refill_idx].buffer == NULL) {
410                         buf = lio_recv_buffer_alloc(lio_dev, droq->q_no);
411                         /* If a buffer could not be allocated, no point in
412                          * continuing
413                          */
414                         if (buf == NULL) {
415                                 droq->stats.rx_alloc_failure++;
416                                 break;
417                         }
418
419                         droq->recv_buf_list[droq->refill_idx].buffer = buf;
420                 }
421
422                 desc_ring[droq->refill_idx].buffer_ptr =
423                     lio_map_ring(droq->recv_buf_list[droq->refill_idx].buffer);
424                 /* Reset any previous values in the length field. */
425                 droq->info_list[droq->refill_idx].length = 0;
426
427                 droq->refill_idx = lio_incr_index(droq->refill_idx, 1,
428                                                   droq->max_count);
429                 desc_refilled++;
430                 droq->refill_count--;
431         }
432
433         if (droq->refill_count)
434                 desc_refilled += lio_droq_refill_pullup_descs(droq, desc_ring);
435
436         /* if droq->refill_count
437          * The refill count would not change in pass two. We only moved buffers
438          * to close the gap in the ring, but we would still have the same no. of
439          * buffers to refill.
440          */
441         return desc_refilled;
442 }
443
444 static int
445 lio_droq_fast_process_packet(struct lio_device *lio_dev,
446                              struct lio_droq *droq,
447                              struct rte_mbuf **rx_pkts)
448 {
449         struct rte_mbuf *nicbuf = NULL;
450         struct lio_droq_info *info;
451         uint32_t total_len = 0;
452         int data_total_len = 0;
453         uint32_t pkt_len = 0;
454         union octeon_rh *rh;
455         int data_pkts = 0;
456
457         info = &droq->info_list[droq->read_idx];
458         lio_swap_8B_data((uint64_t *)info, 2);
459
460         if (!info->length)
461                 return -1;
462
463         /* Len of resp hdr in included in the received data len. */
464         info->length -= OCTEON_RH_SIZE;
465         rh = &info->rh;
466
467         total_len += (uint32_t)info->length;
468
469         if (lio_opcode_slow_path(rh)) {
470                 uint32_t buf_cnt;
471
472                 buf_cnt = lio_droq_get_bufcount(droq->buffer_size,
473                                                 (uint32_t)info->length);
474                 droq->read_idx = lio_incr_index(droq->read_idx, buf_cnt,
475                                                 droq->max_count);
476                 droq->refill_count += buf_cnt;
477         } else {
478                 if (info->length <= droq->buffer_size) {
479                         if (rh->r_dh.has_hash)
480                                 pkt_len = (uint32_t)(info->length - 8);
481                         else
482                                 pkt_len = (uint32_t)info->length;
483
484                         nicbuf = droq->recv_buf_list[droq->read_idx].buffer;
485                         droq->recv_buf_list[droq->read_idx].buffer = NULL;
486                         droq->read_idx = lio_incr_index(
487                                                 droq->read_idx, 1,
488                                                 droq->max_count);
489                         droq->refill_count++;
490
491                         if (likely(nicbuf != NULL)) {
492                                 nicbuf->data_off = RTE_PKTMBUF_HEADROOM;
493                                 nicbuf->nb_segs = 1;
494                                 nicbuf->next = NULL;
495                                 /* We don't have a way to pass flags yet */
496                                 nicbuf->ol_flags = 0;
497                                 if (rh->r_dh.has_hash) {
498                                         uint64_t *hash_ptr;
499
500                                         nicbuf->ol_flags |= PKT_RX_RSS_HASH;
501                                         hash_ptr = rte_pktmbuf_mtod(nicbuf,
502                                                                     uint64_t *);
503                                         lio_swap_8B_data(hash_ptr, 1);
504                                         nicbuf->hash.rss = (uint32_t)*hash_ptr;
505                                         nicbuf->data_off += 8;
506                                 }
507
508                                 nicbuf->pkt_len = pkt_len;
509                                 nicbuf->data_len = pkt_len;
510                                 nicbuf->port = lio_dev->port_id;
511                                 /* Store the mbuf */
512                                 rx_pkts[data_pkts++] = nicbuf;
513                                 data_total_len += pkt_len;
514                         }
515
516                         /* Prefetch buffer pointers when on a cache line
517                          * boundary
518                          */
519                         if ((droq->read_idx & 3) == 0) {
520                                 rte_prefetch0(
521                                     &droq->recv_buf_list[droq->read_idx]);
522                                 rte_prefetch0(
523                                     &droq->info_list[droq->read_idx]);
524                         }
525                 } else {
526                         struct rte_mbuf *first_buf = NULL;
527                         struct rte_mbuf *last_buf = NULL;
528
529                         while (pkt_len < info->length) {
530                                 int cpy_len = 0;
531
532                                 cpy_len = ((pkt_len + droq->buffer_size) >
533                                                 info->length)
534                                                 ? ((uint32_t)info->length -
535                                                         pkt_len)
536                                                 : droq->buffer_size;
537
538                                 nicbuf =
539                                     droq->recv_buf_list[droq->read_idx].buffer;
540                                 droq->recv_buf_list[droq->read_idx].buffer =
541                                     NULL;
542
543                                 if (likely(nicbuf != NULL)) {
544                                         /* Note the first seg */
545                                         if (!pkt_len)
546                                                 first_buf = nicbuf;
547
548                                         nicbuf->data_off = RTE_PKTMBUF_HEADROOM;
549                                         nicbuf->nb_segs = 1;
550                                         nicbuf->next = NULL;
551                                         nicbuf->port = lio_dev->port_id;
552                                         /* We don't have a way to pass
553                                          * flags yet
554                                          */
555                                         nicbuf->ol_flags = 0;
556                                         if ((!pkt_len) && (rh->r_dh.has_hash)) {
557                                                 uint64_t *hash_ptr;
558
559                                                 nicbuf->ol_flags |=
560                                                     PKT_RX_RSS_HASH;
561                                                 hash_ptr = rte_pktmbuf_mtod(
562                                                     nicbuf, uint64_t *);
563                                                 lio_swap_8B_data(hash_ptr, 1);
564                                                 nicbuf->hash.rss =
565                                                     (uint32_t)*hash_ptr;
566                                                 nicbuf->data_off += 8;
567                                                 nicbuf->pkt_len = cpy_len - 8;
568                                                 nicbuf->data_len = cpy_len - 8;
569                                         } else {
570                                                 nicbuf->pkt_len = cpy_len;
571                                                 nicbuf->data_len = cpy_len;
572                                         }
573
574                                         if (pkt_len)
575                                                 first_buf->nb_segs++;
576
577                                         if (last_buf)
578                                                 last_buf->next = nicbuf;
579
580                                         last_buf = nicbuf;
581                                 } else {
582                                         PMD_RX_LOG(lio_dev, ERR, "no buf\n");
583                                 }
584
585                                 pkt_len += cpy_len;
586                                 droq->read_idx = lio_incr_index(
587                                                         droq->read_idx,
588                                                         1, droq->max_count);
589                                 droq->refill_count++;
590
591                                 /* Prefetch buffer pointers when on a
592                                  * cache line boundary
593                                  */
594                                 if ((droq->read_idx & 3) == 0) {
595                                         rte_prefetch0(&droq->recv_buf_list
596                                                               [droq->read_idx]);
597
598                                         rte_prefetch0(
599                                             &droq->info_list[droq->read_idx]);
600                                 }
601                         }
602                         rx_pkts[data_pkts++] = first_buf;
603                         if (rh->r_dh.has_hash)
604                                 data_total_len += (pkt_len - 8);
605                         else
606                                 data_total_len += pkt_len;
607                 }
608
609                 /* Inform upper layer about packet checksum verification */
610                 struct rte_mbuf *m = rx_pkts[data_pkts - 1];
611
612                 if (rh->r_dh.csum_verified & LIO_IP_CSUM_VERIFIED)
613                         m->ol_flags |= PKT_RX_IP_CKSUM_GOOD;
614
615                 if (rh->r_dh.csum_verified & LIO_L4_CSUM_VERIFIED)
616                         m->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
617         }
618
619         if (droq->refill_count >= droq->refill_threshold) {
620                 int desc_refilled = lio_droq_refill(lio_dev, droq);
621
622                 /* Flush the droq descriptor data to memory to be sure
623                  * that when we update the credits the data in memory is
624                  * accurate.
625                  */
626                 rte_wmb();
627                 rte_write32(desc_refilled, droq->pkts_credit_reg);
628                 /* make sure mmio write completes */
629                 rte_wmb();
630         }
631
632         info->length = 0;
633         info->rh.rh64 = 0;
634
635         droq->stats.pkts_received++;
636         droq->stats.rx_pkts_received += data_pkts;
637         droq->stats.rx_bytes_received += data_total_len;
638         droq->stats.bytes_received += total_len;
639
640         return data_pkts;
641 }
642
643 static uint32_t
644 lio_droq_fast_process_packets(struct lio_device *lio_dev,
645                               struct lio_droq *droq,
646                               struct rte_mbuf **rx_pkts,
647                               uint32_t pkts_to_process)
648 {
649         int ret, data_pkts = 0;
650         uint32_t pkt;
651
652         for (pkt = 0; pkt < pkts_to_process; pkt++) {
653                 ret = lio_droq_fast_process_packet(lio_dev, droq,
654                                                    &rx_pkts[data_pkts]);
655                 if (ret < 0) {
656                         lio_dev_err(lio_dev, "Port[%d] DROQ[%d] idx: %d len:0, pkt_cnt: %d\n",
657                                     lio_dev->port_id, droq->q_no,
658                                     droq->read_idx, pkts_to_process);
659                         break;
660                 }
661                 data_pkts += ret;
662         }
663
664         rte_atomic64_sub(&droq->pkts_pending, pkt);
665
666         return data_pkts;
667 }
668
669 static inline uint32_t
670 lio_droq_check_hw_for_pkts(struct lio_droq *droq)
671 {
672         uint32_t last_count;
673         uint32_t pkt_count;
674
675         pkt_count = rte_read32(droq->pkts_sent_reg);
676
677         last_count = pkt_count - droq->pkt_count;
678         droq->pkt_count = pkt_count;
679
680         if (last_count)
681                 rte_atomic64_add(&droq->pkts_pending, last_count);
682
683         return last_count;
684 }
685
686 uint16_t
687 lio_dev_recv_pkts(void *rx_queue,
688                   struct rte_mbuf **rx_pkts,
689                   uint16_t budget)
690 {
691         struct lio_droq *droq = rx_queue;
692         struct lio_device *lio_dev = droq->lio_dev;
693         uint32_t pkts_processed = 0;
694         uint32_t pkt_count = 0;
695
696         lio_droq_check_hw_for_pkts(droq);
697
698         pkt_count = rte_atomic64_read(&droq->pkts_pending);
699         if (!pkt_count)
700                 return 0;
701
702         if (pkt_count > budget)
703                 pkt_count = budget;
704
705         /* Grab the lock */
706         rte_spinlock_lock(&droq->lock);
707         pkts_processed = lio_droq_fast_process_packets(lio_dev,
708                                                        droq, rx_pkts,
709                                                        pkt_count);
710
711         if (droq->pkt_count) {
712                 rte_write32(droq->pkt_count, droq->pkts_sent_reg);
713                 droq->pkt_count = 0;
714         }
715
716         /* Release the spin lock */
717         rte_spinlock_unlock(&droq->lock);
718
719         return pkts_processed;
720 }
721
722 void
723 lio_delete_droq_queue(struct lio_device *lio_dev,
724                       int oq_no)
725 {
726         lio_delete_droq(lio_dev, oq_no);
727         lio_dev->num_oqs--;
728         rte_free(lio_dev->droq[oq_no]);
729         lio_dev->droq[oq_no] = NULL;
730 }
731
732 /**
733  *  lio_init_instr_queue()
734  *  @param lio_dev      - pointer to the lio device structure.
735  *  @param txpciq       - queue to be initialized.
736  *
737  *  Called at driver init time for each input queue. iq_conf has the
738  *  configuration parameters for the queue.
739  *
740  *  @return  Success: 0 Failure: -1
741  */
742 static int
743 lio_init_instr_queue(struct lio_device *lio_dev,
744                      union octeon_txpciq txpciq,
745                      uint32_t num_descs, unsigned int socket_id)
746 {
747         uint32_t iq_no = (uint32_t)txpciq.s.q_no;
748         struct lio_instr_queue *iq;
749         uint32_t instr_type;
750         uint32_t q_size;
751
752         instr_type = LIO_IQ_INSTR_TYPE(lio_dev);
753
754         q_size = instr_type * num_descs;
755         iq = lio_dev->instr_queue[iq_no];
756         iq->iq_mz = rte_eth_dma_zone_reserve(lio_dev->eth_dev,
757                                              "instr_queue", iq_no, q_size,
758                                              RTE_CACHE_LINE_SIZE,
759                                              socket_id);
760         if (iq->iq_mz == NULL) {
761                 lio_dev_err(lio_dev, "Cannot allocate memory for instr queue %d\n",
762                             iq_no);
763                 return -1;
764         }
765
766         iq->base_addr_dma = iq->iq_mz->phys_addr;
767         iq->base_addr = (uint8_t *)iq->iq_mz->addr;
768
769         iq->max_count = num_descs;
770
771         /* Initialize a list to holds requests that have been posted to Octeon
772          * but has yet to be fetched by octeon
773          */
774         iq->request_list = rte_zmalloc_socket("request_list",
775                                               sizeof(*iq->request_list) *
776                                                         num_descs,
777                                               RTE_CACHE_LINE_SIZE,
778                                               socket_id);
779         if (iq->request_list == NULL) {
780                 lio_dev_err(lio_dev, "Alloc failed for IQ[%d] nr free list\n",
781                             iq_no);
782                 lio_dma_zone_free(lio_dev, iq->iq_mz);
783                 return -1;
784         }
785
786         lio_dev_dbg(lio_dev, "IQ[%d]: base: %p basedma: %lx count: %d\n",
787                     iq_no, iq->base_addr, (unsigned long)iq->base_addr_dma,
788                     iq->max_count);
789
790         iq->lio_dev = lio_dev;
791         iq->txpciq.txpciq64 = txpciq.txpciq64;
792         iq->fill_cnt = 0;
793         iq->host_write_index = 0;
794         iq->lio_read_index = 0;
795         iq->flush_index = 0;
796
797         rte_atomic64_set(&iq->instr_pending, 0);
798
799         /* Initialize the spinlock for this instruction queue */
800         rte_spinlock_init(&iq->lock);
801         rte_spinlock_init(&iq->post_lock);
802
803         rte_atomic64_clear(&iq->iq_flush_running);
804
805         lio_dev->io_qmask.iq |= (1ULL << iq_no);
806
807         /* Set the 32B/64B mode for each input queue */
808         lio_dev->io_qmask.iq64B |= ((instr_type == 64) << iq_no);
809         iq->iqcmd_64B = (instr_type == 64);
810
811         lio_dev->fn_list.setup_iq_regs(lio_dev, iq_no);
812
813         return 0;
814 }
815
816 int
817 lio_setup_instr_queue0(struct lio_device *lio_dev)
818 {
819         union octeon_txpciq txpciq;
820         uint32_t num_descs = 0;
821         uint32_t iq_no = 0;
822
823         num_descs = LIO_NUM_DEF_TX_DESCS_CFG(lio_dev);
824
825         lio_dev->num_iqs = 0;
826
827         lio_dev->instr_queue[0] = rte_zmalloc(NULL,
828                                         sizeof(struct lio_instr_queue), 0);
829         if (lio_dev->instr_queue[0] == NULL)
830                 return -ENOMEM;
831
832         lio_dev->instr_queue[0]->q_index = 0;
833         lio_dev->instr_queue[0]->app_ctx = (void *)(size_t)0;
834         txpciq.txpciq64 = 0;
835         txpciq.s.q_no = iq_no;
836         txpciq.s.pkind = lio_dev->pfvf_hsword.pkind;
837         txpciq.s.use_qpg = 0;
838         txpciq.s.qpg = 0;
839         if (lio_init_instr_queue(lio_dev, txpciq, num_descs, SOCKET_ID_ANY)) {
840                 rte_free(lio_dev->instr_queue[0]);
841                 lio_dev->instr_queue[0] = NULL;
842                 return -1;
843         }
844
845         lio_dev->num_iqs++;
846
847         return 0;
848 }
849
850 /**
851  *  lio_delete_instr_queue()
852  *  @param lio_dev      - pointer to the lio device structure.
853  *  @param iq_no        - queue to be deleted.
854  *
855  *  Called at driver unload time for each input queue. Deletes all
856  *  allocated resources for the input queue.
857  */
858 static void
859 lio_delete_instr_queue(struct lio_device *lio_dev, uint32_t iq_no)
860 {
861         struct lio_instr_queue *iq = lio_dev->instr_queue[iq_no];
862
863         rte_free(iq->request_list);
864         iq->request_list = NULL;
865         lio_dma_zone_free(lio_dev, iq->iq_mz);
866 }
867
868 void
869 lio_free_instr_queue0(struct lio_device *lio_dev)
870 {
871         lio_delete_instr_queue(lio_dev, 0);
872         rte_free(lio_dev->instr_queue[0]);
873         lio_dev->instr_queue[0] = NULL;
874         lio_dev->num_iqs--;
875 }
876
877 /* Return 0 on success, -1 on failure */
878 int
879 lio_setup_iq(struct lio_device *lio_dev, int q_index,
880              union octeon_txpciq txpciq, uint32_t num_descs, void *app_ctx,
881              unsigned int socket_id)
882 {
883         uint32_t iq_no = (uint32_t)txpciq.s.q_no;
884
885         if (lio_dev->instr_queue[iq_no]) {
886                 lio_dev_dbg(lio_dev, "IQ is in use. Cannot create the IQ: %d again\n",
887                             iq_no);
888                 lio_dev->instr_queue[iq_no]->txpciq.txpciq64 = txpciq.txpciq64;
889                 lio_dev->instr_queue[iq_no]->app_ctx = app_ctx;
890                 return 0;
891         }
892
893         lio_dev->instr_queue[iq_no] = rte_zmalloc_socket("ethdev TX queue",
894                                                 sizeof(struct lio_instr_queue),
895                                                 RTE_CACHE_LINE_SIZE, socket_id);
896         if (lio_dev->instr_queue[iq_no] == NULL)
897                 return -1;
898
899         lio_dev->instr_queue[iq_no]->q_index = q_index;
900         lio_dev->instr_queue[iq_no]->app_ctx = app_ctx;
901
902         if (lio_init_instr_queue(lio_dev, txpciq, num_descs, socket_id))
903                 goto release_lio_iq;
904
905         lio_dev->num_iqs++;
906         if (lio_dev->fn_list.enable_io_queues(lio_dev))
907                 goto delete_lio_iq;
908
909         return 0;
910
911 delete_lio_iq:
912         lio_delete_instr_queue(lio_dev, iq_no);
913         lio_dev->num_iqs--;
914 release_lio_iq:
915         rte_free(lio_dev->instr_queue[iq_no]);
916         lio_dev->instr_queue[iq_no] = NULL;
917
918         return -1;
919 }
920
921 static inline void
922 lio_ring_doorbell(struct lio_device *lio_dev,
923                   struct lio_instr_queue *iq)
924 {
925         if (rte_atomic64_read(&lio_dev->status) == LIO_DEV_RUNNING) {
926                 rte_write32(iq->fill_cnt, iq->doorbell_reg);
927                 /* make sure doorbell write goes through */
928                 rte_wmb();
929                 iq->fill_cnt = 0;
930         }
931 }
932
933 static inline void
934 copy_cmd_into_iq(struct lio_instr_queue *iq, uint8_t *cmd)
935 {
936         uint8_t *iqptr, cmdsize;
937
938         cmdsize = ((iq->iqcmd_64B) ? 64 : 32);
939         iqptr = iq->base_addr + (cmdsize * iq->host_write_index);
940
941         rte_memcpy(iqptr, cmd, cmdsize);
942 }
943
944 static inline struct lio_iq_post_status
945 post_command2(struct lio_instr_queue *iq, uint8_t *cmd)
946 {
947         struct lio_iq_post_status st;
948
949         st.status = LIO_IQ_SEND_OK;
950
951         /* This ensures that the read index does not wrap around to the same
952          * position if queue gets full before Octeon could fetch any instr.
953          */
954         if (rte_atomic64_read(&iq->instr_pending) >=
955                         (int32_t)(iq->max_count - 1)) {
956                 st.status = LIO_IQ_SEND_FAILED;
957                 st.index = -1;
958                 return st;
959         }
960
961         if (rte_atomic64_read(&iq->instr_pending) >=
962                         (int32_t)(iq->max_count - 2))
963                 st.status = LIO_IQ_SEND_STOP;
964
965         copy_cmd_into_iq(iq, cmd);
966
967         /* "index" is returned, host_write_index is modified. */
968         st.index = iq->host_write_index;
969         iq->host_write_index = lio_incr_index(iq->host_write_index, 1,
970                                               iq->max_count);
971         iq->fill_cnt++;
972
973         /* Flush the command into memory. We need to be sure the data is in
974          * memory before indicating that the instruction is pending.
975          */
976         rte_wmb();
977
978         rte_atomic64_inc(&iq->instr_pending);
979
980         return st;
981 }
982
983 static inline void
984 lio_add_to_request_list(struct lio_instr_queue *iq,
985                         int idx, void *buf, int reqtype)
986 {
987         iq->request_list[idx].buf = buf;
988         iq->request_list[idx].reqtype = reqtype;
989 }
990
991 static inline void
992 lio_free_netsgbuf(void *buf)
993 {
994         struct lio_buf_free_info *finfo = buf;
995         struct lio_device *lio_dev = finfo->lio_dev;
996         struct rte_mbuf *m = finfo->mbuf;
997         struct lio_gather *g = finfo->g;
998         uint8_t iq = finfo->iq_no;
999
1000         /* This will take care of multiple segments also */
1001         rte_pktmbuf_free(m);
1002
1003         rte_spinlock_lock(&lio_dev->glist_lock[iq]);
1004         STAILQ_INSERT_TAIL(&lio_dev->glist_head[iq], &g->list, entries);
1005         rte_spinlock_unlock(&lio_dev->glist_lock[iq]);
1006         rte_free(finfo);
1007 }
1008
1009 /* Can only run in process context */
1010 static int
1011 lio_process_iq_request_list(struct lio_device *lio_dev,
1012                             struct lio_instr_queue *iq)
1013 {
1014         struct octeon_instr_irh *irh = NULL;
1015         uint32_t old = iq->flush_index;
1016         struct lio_soft_command *sc;
1017         uint32_t inst_count = 0;
1018         int reqtype;
1019         void *buf;
1020
1021         while (old != iq->lio_read_index) {
1022                 reqtype = iq->request_list[old].reqtype;
1023                 buf     = iq->request_list[old].buf;
1024
1025                 if (reqtype == LIO_REQTYPE_NONE)
1026                         goto skip_this;
1027
1028                 switch (reqtype) {
1029                 case LIO_REQTYPE_NORESP_NET:
1030                         rte_pktmbuf_free((struct rte_mbuf *)buf);
1031                         break;
1032                 case LIO_REQTYPE_NORESP_NET_SG:
1033                         lio_free_netsgbuf(buf);
1034                         break;
1035                 case LIO_REQTYPE_SOFT_COMMAND:
1036                         sc = buf;
1037                         irh = (struct octeon_instr_irh *)&sc->cmd.cmd3.irh;
1038                         if (irh->rflag) {
1039                                 /* We're expecting a response from Octeon.
1040                                  * It's up to lio_process_ordered_list() to
1041                                  * process sc. Add sc to the ordered soft
1042                                  * command response list because we expect
1043                                  * a response from Octeon.
1044                                  */
1045                                 rte_spinlock_lock(&lio_dev->response_list.lock);
1046                                 rte_atomic64_inc(
1047                                     &lio_dev->response_list.pending_req_count);
1048                                 STAILQ_INSERT_TAIL(
1049                                         &lio_dev->response_list.head,
1050                                         &sc->node, entries);
1051                                 rte_spinlock_unlock(
1052                                                 &lio_dev->response_list.lock);
1053                         } else {
1054                                 if (sc->callback) {
1055                                         /* This callback must not sleep */
1056                                         sc->callback(LIO_REQUEST_DONE,
1057                                                      sc->callback_arg);
1058                                 }
1059                         }
1060                         break;
1061                 default:
1062                         lio_dev_err(lio_dev,
1063                                     "Unknown reqtype: %d buf: %p at idx %d\n",
1064                                     reqtype, buf, old);
1065                 }
1066
1067                 iq->request_list[old].buf = NULL;
1068                 iq->request_list[old].reqtype = 0;
1069
1070 skip_this:
1071                 inst_count++;
1072                 old = lio_incr_index(old, 1, iq->max_count);
1073         }
1074
1075         iq->flush_index = old;
1076
1077         return inst_count;
1078 }
1079
1080 static void
1081 lio_update_read_index(struct lio_instr_queue *iq)
1082 {
1083         uint32_t pkt_in_done = rte_read32(iq->inst_cnt_reg);
1084         uint32_t last_done;
1085
1086         last_done = pkt_in_done - iq->pkt_in_done;
1087         iq->pkt_in_done = pkt_in_done;
1088
1089         /* Add last_done and modulo with the IQ size to get new index */
1090         iq->lio_read_index = (iq->lio_read_index +
1091                         (uint32_t)(last_done & LIO_PKT_IN_DONE_CNT_MASK)) %
1092                         iq->max_count;
1093 }
1094
1095 int
1096 lio_flush_iq(struct lio_device *lio_dev, struct lio_instr_queue *iq)
1097 {
1098         uint32_t tot_inst_processed = 0;
1099         uint32_t inst_processed = 0;
1100         int tx_done = 1;
1101
1102         if (rte_atomic64_test_and_set(&iq->iq_flush_running) == 0)
1103                 return tx_done;
1104
1105         rte_spinlock_lock(&iq->lock);
1106
1107         lio_update_read_index(iq);
1108
1109         do {
1110                 /* Process any outstanding IQ packets. */
1111                 if (iq->flush_index == iq->lio_read_index)
1112                         break;
1113
1114                 inst_processed = lio_process_iq_request_list(lio_dev, iq);
1115
1116                 if (inst_processed) {
1117                         rte_atomic64_sub(&iq->instr_pending, inst_processed);
1118                         iq->stats.instr_processed += inst_processed;
1119                 }
1120
1121                 tot_inst_processed += inst_processed;
1122                 inst_processed = 0;
1123
1124         } while (1);
1125
1126         rte_spinlock_unlock(&iq->lock);
1127
1128         rte_atomic64_clear(&iq->iq_flush_running);
1129
1130         return tx_done;
1131 }
1132
1133 static int
1134 lio_send_command(struct lio_device *lio_dev, uint32_t iq_no, void *cmd,
1135                  void *buf, uint32_t datasize, uint32_t reqtype)
1136 {
1137         struct lio_instr_queue *iq = lio_dev->instr_queue[iq_no];
1138         struct lio_iq_post_status st;
1139
1140         rte_spinlock_lock(&iq->post_lock);
1141
1142         st = post_command2(iq, cmd);
1143
1144         if (st.status != LIO_IQ_SEND_FAILED) {
1145                 lio_add_to_request_list(iq, st.index, buf, reqtype);
1146                 LIO_INCR_INSTRQUEUE_PKT_COUNT(lio_dev, iq_no, bytes_sent,
1147                                               datasize);
1148                 LIO_INCR_INSTRQUEUE_PKT_COUNT(lio_dev, iq_no, instr_posted, 1);
1149
1150                 lio_ring_doorbell(lio_dev, iq);
1151         } else {
1152                 LIO_INCR_INSTRQUEUE_PKT_COUNT(lio_dev, iq_no, instr_dropped, 1);
1153         }
1154
1155         rte_spinlock_unlock(&iq->post_lock);
1156
1157         return st.status;
1158 }
1159
1160 void
1161 lio_prepare_soft_command(struct lio_device *lio_dev,
1162                          struct lio_soft_command *sc, uint8_t opcode,
1163                          uint8_t subcode, uint32_t irh_ossp, uint64_t ossp0,
1164                          uint64_t ossp1)
1165 {
1166         struct octeon_instr_pki_ih3 *pki_ih3;
1167         struct octeon_instr_ih3 *ih3;
1168         struct octeon_instr_irh *irh;
1169         struct octeon_instr_rdp *rdp;
1170
1171         RTE_ASSERT(opcode <= 15);
1172         RTE_ASSERT(subcode <= 127);
1173
1174         ih3       = (struct octeon_instr_ih3 *)&sc->cmd.cmd3.ih3;
1175
1176         ih3->pkind = lio_dev->instr_queue[sc->iq_no]->txpciq.s.pkind;
1177
1178         pki_ih3 = (struct octeon_instr_pki_ih3 *)&sc->cmd.cmd3.pki_ih3;
1179
1180         pki_ih3->w      = 1;
1181         pki_ih3->raw    = 1;
1182         pki_ih3->utag   = 1;
1183         pki_ih3->uqpg   = lio_dev->instr_queue[sc->iq_no]->txpciq.s.use_qpg;
1184         pki_ih3->utt    = 1;
1185
1186         pki_ih3->tag    = LIO_CONTROL;
1187         pki_ih3->tagtype = OCTEON_ATOMIC_TAG;
1188         pki_ih3->qpg    = lio_dev->instr_queue[sc->iq_no]->txpciq.s.qpg;
1189         pki_ih3->pm     = 0x7;
1190         pki_ih3->sl     = 8;
1191
1192         if (sc->datasize)
1193                 ih3->dlengsz = sc->datasize;
1194
1195         irh             = (struct octeon_instr_irh *)&sc->cmd.cmd3.irh;
1196         irh->opcode     = opcode;
1197         irh->subcode    = subcode;
1198
1199         /* opcode/subcode specific parameters (ossp) */
1200         irh->ossp = irh_ossp;
1201         sc->cmd.cmd3.ossp[0] = ossp0;
1202         sc->cmd.cmd3.ossp[1] = ossp1;
1203
1204         if (sc->rdatasize) {
1205                 rdp = (struct octeon_instr_rdp *)&sc->cmd.cmd3.rdp;
1206                 rdp->pcie_port = lio_dev->pcie_port;
1207                 rdp->rlen      = sc->rdatasize;
1208                 irh->rflag = 1;
1209                 /* PKI IH3 */
1210                 ih3->fsz    = OCTEON_SOFT_CMD_RESP_IH3;
1211         } else {
1212                 irh->rflag = 0;
1213                 /* PKI IH3 */
1214                 ih3->fsz    = OCTEON_PCI_CMD_O3;
1215         }
1216 }
1217
1218 int
1219 lio_send_soft_command(struct lio_device *lio_dev,
1220                       struct lio_soft_command *sc)
1221 {
1222         struct octeon_instr_ih3 *ih3;
1223         struct octeon_instr_irh *irh;
1224         uint32_t len = 0;
1225
1226         ih3 = (struct octeon_instr_ih3 *)&sc->cmd.cmd3.ih3;
1227         if (ih3->dlengsz) {
1228                 RTE_ASSERT(sc->dmadptr);
1229                 sc->cmd.cmd3.dptr = sc->dmadptr;
1230         }
1231
1232         irh = (struct octeon_instr_irh *)&sc->cmd.cmd3.irh;
1233         if (irh->rflag) {
1234                 RTE_ASSERT(sc->dmarptr);
1235                 RTE_ASSERT(sc->status_word != NULL);
1236                 *sc->status_word = LIO_COMPLETION_WORD_INIT;
1237                 sc->cmd.cmd3.rptr = sc->dmarptr;
1238         }
1239
1240         len = (uint32_t)ih3->dlengsz;
1241
1242         if (sc->wait_time)
1243                 sc->timeout = lio_uptime + sc->wait_time;
1244
1245         return lio_send_command(lio_dev, sc->iq_no, &sc->cmd, sc, len,
1246                                 LIO_REQTYPE_SOFT_COMMAND);
1247 }
1248
1249 int
1250 lio_setup_sc_buffer_pool(struct lio_device *lio_dev)
1251 {
1252         char sc_pool_name[RTE_MEMPOOL_NAMESIZE];
1253         uint16_t buf_size;
1254
1255         buf_size = LIO_SOFT_COMMAND_BUFFER_SIZE + RTE_PKTMBUF_HEADROOM;
1256         snprintf(sc_pool_name, sizeof(sc_pool_name),
1257                  "lio_sc_pool_%u", lio_dev->port_id);
1258         lio_dev->sc_buf_pool = rte_pktmbuf_pool_create(sc_pool_name,
1259                                                 LIO_MAX_SOFT_COMMAND_BUFFERS,
1260                                                 0, 0, buf_size, SOCKET_ID_ANY);
1261         return 0;
1262 }
1263
1264 void
1265 lio_free_sc_buffer_pool(struct lio_device *lio_dev)
1266 {
1267         rte_mempool_free(lio_dev->sc_buf_pool);
1268 }
1269
1270 struct lio_soft_command *
1271 lio_alloc_soft_command(struct lio_device *lio_dev, uint32_t datasize,
1272                        uint32_t rdatasize, uint32_t ctxsize)
1273 {
1274         uint32_t offset = sizeof(struct lio_soft_command);
1275         struct lio_soft_command *sc;
1276         struct rte_mbuf *m;
1277         uint64_t dma_addr;
1278
1279         RTE_ASSERT((offset + datasize + rdatasize + ctxsize) <=
1280                    LIO_SOFT_COMMAND_BUFFER_SIZE);
1281
1282         m = rte_pktmbuf_alloc(lio_dev->sc_buf_pool);
1283         if (m == NULL) {
1284                 lio_dev_err(lio_dev, "Cannot allocate mbuf for sc\n");
1285                 return NULL;
1286         }
1287
1288         /* set rte_mbuf data size and there is only 1 segment */
1289         m->pkt_len = LIO_SOFT_COMMAND_BUFFER_SIZE;
1290         m->data_len = LIO_SOFT_COMMAND_BUFFER_SIZE;
1291
1292         /* use rte_mbuf buffer for soft command */
1293         sc = rte_pktmbuf_mtod(m, struct lio_soft_command *);
1294         memset(sc, 0, LIO_SOFT_COMMAND_BUFFER_SIZE);
1295         sc->size = LIO_SOFT_COMMAND_BUFFER_SIZE;
1296         sc->dma_addr = rte_mbuf_data_dma_addr(m);
1297         sc->mbuf = m;
1298
1299         dma_addr = sc->dma_addr;
1300
1301         if (ctxsize) {
1302                 sc->ctxptr = (uint8_t *)sc + offset;
1303                 sc->ctxsize = ctxsize;
1304         }
1305
1306         /* Start data at 128 byte boundary */
1307         offset = (offset + ctxsize + 127) & 0xffffff80;
1308
1309         if (datasize) {
1310                 sc->virtdptr = (uint8_t *)sc + offset;
1311                 sc->dmadptr = dma_addr + offset;
1312                 sc->datasize = datasize;
1313         }
1314
1315         /* Start rdata at 128 byte boundary */
1316         offset = (offset + datasize + 127) & 0xffffff80;
1317
1318         if (rdatasize) {
1319                 RTE_ASSERT(rdatasize >= 16);
1320                 sc->virtrptr = (uint8_t *)sc + offset;
1321                 sc->dmarptr = dma_addr + offset;
1322                 sc->rdatasize = rdatasize;
1323                 sc->status_word = (uint64_t *)((uint8_t *)(sc->virtrptr) +
1324                                                rdatasize - 8);
1325         }
1326
1327         return sc;
1328 }
1329
1330 void
1331 lio_free_soft_command(struct lio_soft_command *sc)
1332 {
1333         rte_pktmbuf_free(sc->mbuf);
1334 }
1335
1336 void
1337 lio_setup_response_list(struct lio_device *lio_dev)
1338 {
1339         STAILQ_INIT(&lio_dev->response_list.head);
1340         rte_spinlock_init(&lio_dev->response_list.lock);
1341         rte_atomic64_set(&lio_dev->response_list.pending_req_count, 0);
1342 }
1343
1344 int
1345 lio_process_ordered_list(struct lio_device *lio_dev)
1346 {
1347         int resp_to_process = LIO_MAX_ORD_REQS_TO_PROCESS;
1348         struct lio_response_list *ordered_sc_list;
1349         struct lio_soft_command *sc;
1350         int request_complete = 0;
1351         uint64_t status64;
1352         uint32_t status;
1353
1354         ordered_sc_list = &lio_dev->response_list;
1355
1356         do {
1357                 rte_spinlock_lock(&ordered_sc_list->lock);
1358
1359                 if (STAILQ_EMPTY(&ordered_sc_list->head)) {
1360                         /* ordered_sc_list is empty; there is
1361                          * nothing to process
1362                          */
1363                         rte_spinlock_unlock(&ordered_sc_list->lock);
1364                         return -1;
1365                 }
1366
1367                 sc = LIO_STQUEUE_FIRST_ENTRY(&ordered_sc_list->head,
1368                                              struct lio_soft_command, node);
1369
1370                 status = LIO_REQUEST_PENDING;
1371
1372                 /* check if octeon has finished DMA'ing a response
1373                  * to where rptr is pointing to
1374                  */
1375                 status64 = *sc->status_word;
1376
1377                 if (status64 != LIO_COMPLETION_WORD_INIT) {
1378                         /* This logic ensures that all 64b have been written.
1379                          * 1. check byte 0 for non-FF
1380                          * 2. if non-FF, then swap result from BE to host order
1381                          * 3. check byte 7 (swapped to 0) for non-FF
1382                          * 4. if non-FF, use the low 32-bit status code
1383                          * 5. if either byte 0 or byte 7 is FF, don't use status
1384                          */
1385                         if ((status64 & 0xff) != 0xff) {
1386                                 lio_swap_8B_data(&status64, 1);
1387                                 if (((status64 & 0xff) != 0xff)) {
1388                                         /* retrieve 16-bit firmware status */
1389                                         status = (uint32_t)(status64 &
1390                                                             0xffffULL);
1391                                         if (status) {
1392                                                 status =
1393                                                 LIO_FIRMWARE_STATUS_CODE(
1394                                                                         status);
1395                                         } else {
1396                                                 /* i.e. no error */
1397                                                 status = LIO_REQUEST_DONE;
1398                                         }
1399                                 }
1400                         }
1401                 } else if ((sc->timeout && lio_check_timeout(lio_uptime,
1402                                                              sc->timeout))) {
1403                         lio_dev_err(lio_dev,
1404                                     "cmd failed, timeout (%ld, %ld)\n",
1405                                     (long)lio_uptime, (long)sc->timeout);
1406                         status = LIO_REQUEST_TIMEOUT;
1407                 }
1408
1409                 if (status != LIO_REQUEST_PENDING) {
1410                         /* we have received a response or we have timed out.
1411                          * remove node from linked list
1412                          */
1413                         STAILQ_REMOVE(&ordered_sc_list->head,
1414                                       &sc->node, lio_stailq_node, entries);
1415                         rte_atomic64_dec(
1416                             &lio_dev->response_list.pending_req_count);
1417                         rte_spinlock_unlock(&ordered_sc_list->lock);
1418
1419                         if (sc->callback)
1420                                 sc->callback(status, sc->callback_arg);
1421
1422                         request_complete++;
1423                 } else {
1424                         /* no response yet */
1425                         request_complete = 0;
1426                         rte_spinlock_unlock(&ordered_sc_list->lock);
1427                 }
1428
1429                 /* If we hit the Max Ordered requests to process every loop,
1430                  * we quit and let this function be invoked the next time
1431                  * the poll thread runs to process the remaining requests.
1432                  * This function can take up the entire CPU if there is
1433                  * no upper limit to the requests processed.
1434                  */
1435                 if (request_complete >= resp_to_process)
1436                         break;
1437         } while (request_complete);
1438
1439         return 0;
1440 }
1441
1442 static inline struct lio_stailq_node *
1443 list_delete_first_node(struct lio_stailq_head *head)
1444 {
1445         struct lio_stailq_node *node;
1446
1447         if (STAILQ_EMPTY(head))
1448                 node = NULL;
1449         else
1450                 node = STAILQ_FIRST(head);
1451
1452         if (node)
1453                 STAILQ_REMOVE(head, node, lio_stailq_node, entries);
1454
1455         return node;
1456 }
1457
1458 void
1459 lio_delete_sglist(struct lio_instr_queue *txq)
1460 {
1461         struct lio_device *lio_dev = txq->lio_dev;
1462         int iq_no = txq->q_index;
1463         struct lio_gather *g;
1464
1465         if (lio_dev->glist_head == NULL)
1466                 return;
1467
1468         do {
1469                 g = (struct lio_gather *)list_delete_first_node(
1470                                                 &lio_dev->glist_head[iq_no]);
1471                 if (g) {
1472                         if (g->sg)
1473                                 rte_free(
1474                                     (void *)((unsigned long)g->sg - g->adjust));
1475                         rte_free(g);
1476                 }
1477         } while (g);
1478 }
1479
1480 /**
1481  * \brief Setup gather lists
1482  * @param lio per-network private data
1483  */
1484 int
1485 lio_setup_sglists(struct lio_device *lio_dev, int iq_no,
1486                   int fw_mapped_iq, int num_descs, unsigned int socket_id)
1487 {
1488         struct lio_gather *g;
1489         int i;
1490
1491         rte_spinlock_init(&lio_dev->glist_lock[iq_no]);
1492
1493         STAILQ_INIT(&lio_dev->glist_head[iq_no]);
1494
1495         for (i = 0; i < num_descs; i++) {
1496                 g = rte_zmalloc_socket(NULL, sizeof(*g), RTE_CACHE_LINE_SIZE,
1497                                        socket_id);
1498                 if (g == NULL) {
1499                         lio_dev_err(lio_dev,
1500                                     "lio_gather memory allocation failed for qno %d\n",
1501                                     iq_no);
1502                         break;
1503                 }
1504
1505                 g->sg_size =
1506                     ((ROUNDUP4(LIO_MAX_SG) >> 2) * LIO_SG_ENTRY_SIZE);
1507
1508                 g->sg = rte_zmalloc_socket(NULL, g->sg_size + 8,
1509                                            RTE_CACHE_LINE_SIZE, socket_id);
1510                 if (g->sg == NULL) {
1511                         lio_dev_err(lio_dev,
1512                                     "sg list memory allocation failed for qno %d\n",
1513                                     iq_no);
1514                         rte_free(g);
1515                         break;
1516                 }
1517
1518                 /* The gather component should be aligned on 64-bit boundary */
1519                 if (((unsigned long)g->sg) & 7) {
1520                         g->adjust = 8 - (((unsigned long)g->sg) & 7);
1521                         g->sg =
1522                             (struct lio_sg_entry *)((unsigned long)g->sg +
1523                                                        g->adjust);
1524                 }
1525
1526                 STAILQ_INSERT_TAIL(&lio_dev->glist_head[iq_no], &g->list,
1527                                    entries);
1528         }
1529
1530         if (i != num_descs) {
1531                 lio_delete_sglist(lio_dev->instr_queue[fw_mapped_iq]);
1532                 return -ENOMEM;
1533         }
1534
1535         return 0;
1536 }
1537
1538 void
1539 lio_delete_instruction_queue(struct lio_device *lio_dev, int iq_no)
1540 {
1541         lio_delete_instr_queue(lio_dev, iq_no);
1542         rte_free(lio_dev->instr_queue[iq_no]);
1543         lio_dev->instr_queue[iq_no] = NULL;
1544         lio_dev->num_iqs--;
1545 }
1546
1547 static inline uint32_t
1548 lio_iq_get_available(struct lio_device *lio_dev, uint32_t q_no)
1549 {
1550         return ((lio_dev->instr_queue[q_no]->max_count - 1) -
1551                 (uint32_t)rte_atomic64_read(
1552                                 &lio_dev->instr_queue[q_no]->instr_pending));
1553 }
1554
1555 static inline int
1556 lio_iq_is_full(struct lio_device *lio_dev, uint32_t q_no)
1557 {
1558         return ((uint32_t)rte_atomic64_read(
1559                                 &lio_dev->instr_queue[q_no]->instr_pending) >=
1560                                 (lio_dev->instr_queue[q_no]->max_count - 2));
1561 }
1562
1563 static int
1564 lio_dev_cleanup_iq(struct lio_device *lio_dev, int iq_no)
1565 {
1566         struct lio_instr_queue *iq = lio_dev->instr_queue[iq_no];
1567         uint32_t count = 10000;
1568
1569         while ((lio_iq_get_available(lio_dev, iq_no) < LIO_FLUSH_WM(iq)) &&
1570                         --count)
1571                 lio_flush_iq(lio_dev, iq);
1572
1573         return count ? 0 : 1;
1574 }
1575
1576 static void
1577 lio_ctrl_cmd_callback(uint32_t status __rte_unused, void *sc_ptr)
1578 {
1579         struct lio_soft_command *sc = sc_ptr;
1580         struct lio_dev_ctrl_cmd *ctrl_cmd;
1581         struct lio_ctrl_pkt *ctrl_pkt;
1582
1583         ctrl_pkt = (struct lio_ctrl_pkt *)sc->ctxptr;
1584         ctrl_cmd = ctrl_pkt->ctrl_cmd;
1585         ctrl_cmd->cond = 1;
1586
1587         lio_free_soft_command(sc);
1588 }
1589
1590 static inline struct lio_soft_command *
1591 lio_alloc_ctrl_pkt_sc(struct lio_device *lio_dev,
1592                       struct lio_ctrl_pkt *ctrl_pkt)
1593 {
1594         struct lio_soft_command *sc = NULL;
1595         uint32_t uddsize, datasize;
1596         uint32_t rdatasize;
1597         uint8_t *data;
1598
1599         uddsize = (uint32_t)(ctrl_pkt->ncmd.s.more * 8);
1600
1601         datasize = OCTEON_CMD_SIZE + uddsize;
1602         rdatasize = (ctrl_pkt->wait_time) ? 16 : 0;
1603
1604         sc = lio_alloc_soft_command(lio_dev, datasize,
1605                                     rdatasize, sizeof(struct lio_ctrl_pkt));
1606         if (sc == NULL)
1607                 return NULL;
1608
1609         rte_memcpy(sc->ctxptr, ctrl_pkt, sizeof(struct lio_ctrl_pkt));
1610
1611         data = (uint8_t *)sc->virtdptr;
1612
1613         rte_memcpy(data, &ctrl_pkt->ncmd, OCTEON_CMD_SIZE);
1614
1615         lio_swap_8B_data((uint64_t *)data, OCTEON_CMD_SIZE >> 3);
1616
1617         if (uddsize) {
1618                 /* Endian-Swap for UDD should have been done by caller. */
1619                 rte_memcpy(data + OCTEON_CMD_SIZE, ctrl_pkt->udd, uddsize);
1620         }
1621
1622         sc->iq_no = (uint32_t)ctrl_pkt->iq_no;
1623
1624         lio_prepare_soft_command(lio_dev, sc,
1625                                  LIO_OPCODE, LIO_OPCODE_CMD,
1626                                  0, 0, 0);
1627
1628         sc->callback = lio_ctrl_cmd_callback;
1629         sc->callback_arg = sc;
1630         sc->wait_time = ctrl_pkt->wait_time;
1631
1632         return sc;
1633 }
1634
1635 int
1636 lio_send_ctrl_pkt(struct lio_device *lio_dev, struct lio_ctrl_pkt *ctrl_pkt)
1637 {
1638         struct lio_soft_command *sc = NULL;
1639         int retval;
1640
1641         sc = lio_alloc_ctrl_pkt_sc(lio_dev, ctrl_pkt);
1642         if (sc == NULL) {
1643                 lio_dev_err(lio_dev, "soft command allocation failed\n");
1644                 return -1;
1645         }
1646
1647         retval = lio_send_soft_command(lio_dev, sc);
1648         if (retval == LIO_IQ_SEND_FAILED) {
1649                 lio_free_soft_command(sc);
1650                 lio_dev_err(lio_dev, "Port: %d soft command: %d send failed status: %x\n",
1651                             lio_dev->port_id, ctrl_pkt->ncmd.s.cmd, retval);
1652                 return -1;
1653         }
1654
1655         return retval;
1656 }
1657
1658 /** Send data packet to the device
1659  *  @param lio_dev - lio device pointer
1660  *  @param ndata   - control structure with queueing, and buffer information
1661  *
1662  *  @returns IQ_FAILED if it failed to add to the input queue. IQ_STOP if it the
1663  *  queue should be stopped, and LIO_IQ_SEND_OK if it sent okay.
1664  */
1665 static inline int
1666 lio_send_data_pkt(struct lio_device *lio_dev, struct lio_data_pkt *ndata)
1667 {
1668         return lio_send_command(lio_dev, ndata->q_no, &ndata->cmd,
1669                                 ndata->buf, ndata->datasize, ndata->reqtype);
1670 }
1671
1672 uint16_t
1673 lio_dev_xmit_pkts(void *tx_queue, struct rte_mbuf **pkts, uint16_t nb_pkts)
1674 {
1675         struct lio_instr_queue *txq = tx_queue;
1676         union lio_cmd_setup cmdsetup;
1677         struct lio_device *lio_dev;
1678         struct lio_iq_stats *stats;
1679         struct lio_data_pkt ndata;
1680         int i, processed = 0;
1681         struct rte_mbuf *m;
1682         uint32_t tag = 0;
1683         int status = 0;
1684         int iq_no;
1685
1686         lio_dev = txq->lio_dev;
1687         iq_no = txq->txpciq.s.q_no;
1688         stats = &lio_dev->instr_queue[iq_no]->stats;
1689
1690         if (!lio_dev->intf_open || !lio_dev->linfo.link.s.link_up) {
1691                 PMD_TX_LOG(lio_dev, ERR, "Transmit failed link_status : %d\n",
1692                            lio_dev->linfo.link.s.link_up);
1693                 goto xmit_failed;
1694         }
1695
1696         lio_dev_cleanup_iq(lio_dev, iq_no);
1697
1698         for (i = 0; i < nb_pkts; i++) {
1699                 uint32_t pkt_len = 0;
1700
1701                 m = pkts[i];
1702
1703                 /* Prepare the attributes for the data to be passed to BASE. */
1704                 memset(&ndata, 0, sizeof(struct lio_data_pkt));
1705
1706                 ndata.buf = m;
1707
1708                 ndata.q_no = iq_no;
1709                 if (lio_iq_is_full(lio_dev, ndata.q_no)) {
1710                         stats->tx_iq_busy++;
1711                         if (lio_dev_cleanup_iq(lio_dev, iq_no)) {
1712                                 PMD_TX_LOG(lio_dev, ERR,
1713                                            "Transmit failed iq:%d full\n",
1714                                            ndata.q_no);
1715                                 break;
1716                         }
1717                 }
1718
1719                 cmdsetup.cmd_setup64 = 0;
1720                 cmdsetup.s.iq_no = iq_no;
1721
1722                 /* check checksum offload flags to form cmd */
1723                 if (m->ol_flags & PKT_TX_IP_CKSUM)
1724                         cmdsetup.s.ip_csum = 1;
1725
1726                 if (m->ol_flags & PKT_TX_OUTER_IP_CKSUM)
1727                         cmdsetup.s.tnl_csum = 1;
1728                 else if ((m->ol_flags & PKT_TX_TCP_CKSUM) ||
1729                                 (m->ol_flags & PKT_TX_UDP_CKSUM))
1730                         cmdsetup.s.transport_csum = 1;
1731
1732                 if (m->nb_segs == 1) {
1733                         pkt_len = rte_pktmbuf_data_len(m);
1734                         cmdsetup.s.u.datasize = pkt_len;
1735                         lio_prepare_pci_cmd(lio_dev, &ndata.cmd,
1736                                             &cmdsetup, tag);
1737                         ndata.cmd.cmd3.dptr = rte_mbuf_data_dma_addr(m);
1738                         ndata.reqtype = LIO_REQTYPE_NORESP_NET;
1739                 } else {
1740                         struct lio_buf_free_info *finfo;
1741                         struct lio_gather *g;
1742                         phys_addr_t phyaddr;
1743                         int i, frags;
1744
1745                         finfo = (struct lio_buf_free_info *)rte_malloc(NULL,
1746                                                         sizeof(*finfo), 0);
1747                         if (finfo == NULL) {
1748                                 PMD_TX_LOG(lio_dev, ERR,
1749                                            "free buffer alloc failed\n");
1750                                 goto xmit_failed;
1751                         }
1752
1753                         rte_spinlock_lock(&lio_dev->glist_lock[iq_no]);
1754                         g = (struct lio_gather *)list_delete_first_node(
1755                                                 &lio_dev->glist_head[iq_no]);
1756                         rte_spinlock_unlock(&lio_dev->glist_lock[iq_no]);
1757                         if (g == NULL) {
1758                                 PMD_TX_LOG(lio_dev, ERR,
1759                                            "Transmit scatter gather: glist null!\n");
1760                                 goto xmit_failed;
1761                         }
1762
1763                         cmdsetup.s.gather = 1;
1764                         cmdsetup.s.u.gatherptrs = m->nb_segs;
1765                         lio_prepare_pci_cmd(lio_dev, &ndata.cmd,
1766                                             &cmdsetup, tag);
1767
1768                         memset(g->sg, 0, g->sg_size);
1769                         g->sg[0].ptr[0] = rte_mbuf_data_dma_addr(m);
1770                         lio_add_sg_size(&g->sg[0], m->data_len, 0);
1771                         pkt_len = m->data_len;
1772                         finfo->mbuf = m;
1773
1774                         /* First seg taken care above */
1775                         frags = m->nb_segs - 1;
1776                         i = 1;
1777                         m = m->next;
1778                         while (frags--) {
1779                                 g->sg[(i >> 2)].ptr[(i & 3)] =
1780                                                 rte_mbuf_data_dma_addr(m);
1781                                 lio_add_sg_size(&g->sg[(i >> 2)],
1782                                                 m->data_len, (i & 3));
1783                                 pkt_len += m->data_len;
1784                                 i++;
1785                                 m = m->next;
1786                         }
1787
1788                         phyaddr = rte_mem_virt2phy(g->sg);
1789                         if (phyaddr == RTE_BAD_PHYS_ADDR) {
1790                                 PMD_TX_LOG(lio_dev, ERR, "bad phys addr\n");
1791                                 goto xmit_failed;
1792                         }
1793
1794                         ndata.cmd.cmd3.dptr = phyaddr;
1795                         ndata.reqtype = LIO_REQTYPE_NORESP_NET_SG;
1796
1797                         finfo->g = g;
1798                         finfo->lio_dev = lio_dev;
1799                         finfo->iq_no = (uint64_t)iq_no;
1800                         ndata.buf = finfo;
1801                 }
1802
1803                 ndata.datasize = pkt_len;
1804
1805                 status = lio_send_data_pkt(lio_dev, &ndata);
1806
1807                 if (unlikely(status == LIO_IQ_SEND_FAILED)) {
1808                         PMD_TX_LOG(lio_dev, ERR, "send failed\n");
1809                         break;
1810                 }
1811
1812                 if (unlikely(status == LIO_IQ_SEND_STOP)) {
1813                         PMD_TX_LOG(lio_dev, DEBUG, "iq full\n");
1814                         /* create space as iq is full */
1815                         lio_dev_cleanup_iq(lio_dev, iq_no);
1816                 }
1817
1818                 stats->tx_done++;
1819                 stats->tx_tot_bytes += pkt_len;
1820                 processed++;
1821         }
1822
1823 xmit_failed:
1824         stats->tx_dropped += (nb_pkts - processed);
1825
1826         return processed;
1827 }
1828