net/liquidio: add API to send packet to device
[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  *  lio_init_instr_queue()
67  *  @param lio_dev      - pointer to the lio device structure.
68  *  @param txpciq       - queue to be initialized.
69  *
70  *  Called at driver init time for each input queue. iq_conf has the
71  *  configuration parameters for the queue.
72  *
73  *  @return  Success: 0 Failure: -1
74  */
75 static int
76 lio_init_instr_queue(struct lio_device *lio_dev,
77                      union octeon_txpciq txpciq,
78                      uint32_t num_descs, unsigned int socket_id)
79 {
80         uint32_t iq_no = (uint32_t)txpciq.s.q_no;
81         struct lio_instr_queue *iq;
82         uint32_t instr_type;
83         uint32_t q_size;
84
85         instr_type = LIO_IQ_INSTR_TYPE(lio_dev);
86
87         q_size = instr_type * num_descs;
88         iq = lio_dev->instr_queue[iq_no];
89         iq->iq_mz = rte_eth_dma_zone_reserve(lio_dev->eth_dev,
90                                              "instr_queue", iq_no, q_size,
91                                              RTE_CACHE_LINE_SIZE,
92                                              socket_id);
93         if (iq->iq_mz == NULL) {
94                 lio_dev_err(lio_dev, "Cannot allocate memory for instr queue %d\n",
95                             iq_no);
96                 return -1;
97         }
98
99         iq->base_addr_dma = iq->iq_mz->phys_addr;
100         iq->base_addr = (uint8_t *)iq->iq_mz->addr;
101
102         iq->max_count = num_descs;
103
104         /* Initialize a list to holds requests that have been posted to Octeon
105          * but has yet to be fetched by octeon
106          */
107         iq->request_list = rte_zmalloc_socket("request_list",
108                                               sizeof(*iq->request_list) *
109                                                         num_descs,
110                                               RTE_CACHE_LINE_SIZE,
111                                               socket_id);
112         if (iq->request_list == NULL) {
113                 lio_dev_err(lio_dev, "Alloc failed for IQ[%d] nr free list\n",
114                             iq_no);
115                 lio_dma_zone_free(lio_dev, iq->iq_mz);
116                 return -1;
117         }
118
119         lio_dev_dbg(lio_dev, "IQ[%d]: base: %p basedma: %lx count: %d\n",
120                     iq_no, iq->base_addr, (unsigned long)iq->base_addr_dma,
121                     iq->max_count);
122
123         iq->lio_dev = lio_dev;
124         iq->txpciq.txpciq64 = txpciq.txpciq64;
125         iq->fill_cnt = 0;
126         iq->host_write_index = 0;
127         iq->lio_read_index = 0;
128         iq->flush_index = 0;
129
130         rte_atomic64_set(&iq->instr_pending, 0);
131
132         /* Initialize the spinlock for this instruction queue */
133         rte_spinlock_init(&iq->lock);
134         rte_spinlock_init(&iq->post_lock);
135
136         rte_atomic64_clear(&iq->iq_flush_running);
137
138         lio_dev->io_qmask.iq |= (1ULL << iq_no);
139
140         /* Set the 32B/64B mode for each input queue */
141         lio_dev->io_qmask.iq64B |= ((instr_type == 64) << iq_no);
142         iq->iqcmd_64B = (instr_type == 64);
143
144         lio_dev->fn_list.setup_iq_regs(lio_dev, iq_no);
145
146         return 0;
147 }
148
149 int
150 lio_setup_instr_queue0(struct lio_device *lio_dev)
151 {
152         union octeon_txpciq txpciq;
153         uint32_t num_descs = 0;
154         uint32_t iq_no = 0;
155
156         num_descs = LIO_NUM_DEF_TX_DESCS_CFG(lio_dev);
157
158         lio_dev->num_iqs = 0;
159
160         lio_dev->instr_queue[0] = rte_zmalloc(NULL,
161                                         sizeof(struct lio_instr_queue), 0);
162         if (lio_dev->instr_queue[0] == NULL)
163                 return -ENOMEM;
164
165         lio_dev->instr_queue[0]->q_index = 0;
166         lio_dev->instr_queue[0]->app_ctx = (void *)(size_t)0;
167         txpciq.txpciq64 = 0;
168         txpciq.s.q_no = iq_no;
169         txpciq.s.pkind = lio_dev->pfvf_hsword.pkind;
170         txpciq.s.use_qpg = 0;
171         txpciq.s.qpg = 0;
172         if (lio_init_instr_queue(lio_dev, txpciq, num_descs, SOCKET_ID_ANY)) {
173                 rte_free(lio_dev->instr_queue[0]);
174                 lio_dev->instr_queue[0] = NULL;
175                 return -1;
176         }
177
178         lio_dev->num_iqs++;
179
180         return 0;
181 }
182
183 /**
184  *  lio_delete_instr_queue()
185  *  @param lio_dev      - pointer to the lio device structure.
186  *  @param iq_no        - queue to be deleted.
187  *
188  *  Called at driver unload time for each input queue. Deletes all
189  *  allocated resources for the input queue.
190  */
191 static void
192 lio_delete_instr_queue(struct lio_device *lio_dev, uint32_t iq_no)
193 {
194         struct lio_instr_queue *iq = lio_dev->instr_queue[iq_no];
195
196         rte_free(iq->request_list);
197         iq->request_list = NULL;
198         lio_dma_zone_free(lio_dev, iq->iq_mz);
199 }
200
201 void
202 lio_free_instr_queue0(struct lio_device *lio_dev)
203 {
204         lio_delete_instr_queue(lio_dev, 0);
205         rte_free(lio_dev->instr_queue[0]);
206         lio_dev->instr_queue[0] = NULL;
207         lio_dev->num_iqs--;
208 }
209
210 static inline void
211 lio_ring_doorbell(struct lio_device *lio_dev,
212                   struct lio_instr_queue *iq)
213 {
214         if (rte_atomic64_read(&lio_dev->status) == LIO_DEV_RUNNING) {
215                 rte_write32(iq->fill_cnt, iq->doorbell_reg);
216                 /* make sure doorbell write goes through */
217                 rte_wmb();
218                 iq->fill_cnt = 0;
219         }
220 }
221
222 static inline void
223 copy_cmd_into_iq(struct lio_instr_queue *iq, uint8_t *cmd)
224 {
225         uint8_t *iqptr, cmdsize;
226
227         cmdsize = ((iq->iqcmd_64B) ? 64 : 32);
228         iqptr = iq->base_addr + (cmdsize * iq->host_write_index);
229
230         rte_memcpy(iqptr, cmd, cmdsize);
231 }
232
233 static inline struct lio_iq_post_status
234 post_command2(struct lio_instr_queue *iq, uint8_t *cmd)
235 {
236         struct lio_iq_post_status st;
237
238         st.status = LIO_IQ_SEND_OK;
239
240         /* This ensures that the read index does not wrap around to the same
241          * position if queue gets full before Octeon could fetch any instr.
242          */
243         if (rte_atomic64_read(&iq->instr_pending) >=
244                         (int32_t)(iq->max_count - 1)) {
245                 st.status = LIO_IQ_SEND_FAILED;
246                 st.index = -1;
247                 return st;
248         }
249
250         if (rte_atomic64_read(&iq->instr_pending) >=
251                         (int32_t)(iq->max_count - 2))
252                 st.status = LIO_IQ_SEND_STOP;
253
254         copy_cmd_into_iq(iq, cmd);
255
256         /* "index" is returned, host_write_index is modified. */
257         st.index = iq->host_write_index;
258         iq->host_write_index = lio_incr_index(iq->host_write_index, 1,
259                                               iq->max_count);
260         iq->fill_cnt++;
261
262         /* Flush the command into memory. We need to be sure the data is in
263          * memory before indicating that the instruction is pending.
264          */
265         rte_wmb();
266
267         rte_atomic64_inc(&iq->instr_pending);
268
269         return st;
270 }
271
272 static inline void
273 lio_add_to_request_list(struct lio_instr_queue *iq,
274                         int idx, void *buf, int reqtype)
275 {
276         iq->request_list[idx].buf = buf;
277         iq->request_list[idx].reqtype = reqtype;
278 }
279
280 static int
281 lio_send_command(struct lio_device *lio_dev, uint32_t iq_no, void *cmd,
282                  void *buf, uint32_t datasize __rte_unused, uint32_t reqtype)
283 {
284         struct lio_instr_queue *iq = lio_dev->instr_queue[iq_no];
285         struct lio_iq_post_status st;
286
287         rte_spinlock_lock(&iq->post_lock);
288
289         st = post_command2(iq, cmd);
290
291         if (st.status != LIO_IQ_SEND_FAILED) {
292                 lio_add_to_request_list(iq, st.index, buf, reqtype);
293                 lio_ring_doorbell(lio_dev, iq);
294         }
295
296         rte_spinlock_unlock(&iq->post_lock);
297
298         return st.status;
299 }
300
301 void
302 lio_prepare_soft_command(struct lio_device *lio_dev,
303                          struct lio_soft_command *sc, uint8_t opcode,
304                          uint8_t subcode, uint32_t irh_ossp, uint64_t ossp0,
305                          uint64_t ossp1)
306 {
307         struct octeon_instr_pki_ih3 *pki_ih3;
308         struct octeon_instr_ih3 *ih3;
309         struct octeon_instr_irh *irh;
310         struct octeon_instr_rdp *rdp;
311
312         RTE_ASSERT(opcode <= 15);
313         RTE_ASSERT(subcode <= 127);
314
315         ih3       = (struct octeon_instr_ih3 *)&sc->cmd.cmd3.ih3;
316
317         ih3->pkind = lio_dev->instr_queue[sc->iq_no]->txpciq.s.pkind;
318
319         pki_ih3 = (struct octeon_instr_pki_ih3 *)&sc->cmd.cmd3.pki_ih3;
320
321         pki_ih3->w      = 1;
322         pki_ih3->raw    = 1;
323         pki_ih3->utag   = 1;
324         pki_ih3->uqpg   = lio_dev->instr_queue[sc->iq_no]->txpciq.s.use_qpg;
325         pki_ih3->utt    = 1;
326
327         pki_ih3->tag    = LIO_CONTROL;
328         pki_ih3->tagtype = OCTEON_ATOMIC_TAG;
329         pki_ih3->qpg    = lio_dev->instr_queue[sc->iq_no]->txpciq.s.qpg;
330         pki_ih3->pm     = 0x7;
331         pki_ih3->sl     = 8;
332
333         if (sc->datasize)
334                 ih3->dlengsz = sc->datasize;
335
336         irh             = (struct octeon_instr_irh *)&sc->cmd.cmd3.irh;
337         irh->opcode     = opcode;
338         irh->subcode    = subcode;
339
340         /* opcode/subcode specific parameters (ossp) */
341         irh->ossp = irh_ossp;
342         sc->cmd.cmd3.ossp[0] = ossp0;
343         sc->cmd.cmd3.ossp[1] = ossp1;
344
345         if (sc->rdatasize) {
346                 rdp = (struct octeon_instr_rdp *)&sc->cmd.cmd3.rdp;
347                 rdp->pcie_port = lio_dev->pcie_port;
348                 rdp->rlen      = sc->rdatasize;
349                 irh->rflag = 1;
350                 /* PKI IH3 */
351                 ih3->fsz    = OCTEON_SOFT_CMD_RESP_IH3;
352         } else {
353                 irh->rflag = 0;
354                 /* PKI IH3 */
355                 ih3->fsz    = OCTEON_PCI_CMD_O3;
356         }
357 }
358
359 int
360 lio_send_soft_command(struct lio_device *lio_dev,
361                       struct lio_soft_command *sc)
362 {
363         struct octeon_instr_ih3 *ih3;
364         struct octeon_instr_irh *irh;
365         uint32_t len = 0;
366
367         ih3 = (struct octeon_instr_ih3 *)&sc->cmd.cmd3.ih3;
368         if (ih3->dlengsz) {
369                 RTE_ASSERT(sc->dmadptr);
370                 sc->cmd.cmd3.dptr = sc->dmadptr;
371         }
372
373         irh = (struct octeon_instr_irh *)&sc->cmd.cmd3.irh;
374         if (irh->rflag) {
375                 RTE_ASSERT(sc->dmarptr);
376                 RTE_ASSERT(sc->status_word != NULL);
377                 *sc->status_word = LIO_COMPLETION_WORD_INIT;
378                 sc->cmd.cmd3.rptr = sc->dmarptr;
379         }
380
381         len = (uint32_t)ih3->dlengsz;
382
383         if (sc->wait_time)
384                 sc->timeout = lio_uptime + sc->wait_time;
385
386         return lio_send_command(lio_dev, sc->iq_no, &sc->cmd, sc, len,
387                                 LIO_REQTYPE_SOFT_COMMAND);
388 }
389
390 int
391 lio_setup_sc_buffer_pool(struct lio_device *lio_dev)
392 {
393         char sc_pool_name[RTE_MEMPOOL_NAMESIZE];
394         uint16_t buf_size;
395
396         buf_size = LIO_SOFT_COMMAND_BUFFER_SIZE + RTE_PKTMBUF_HEADROOM;
397         snprintf(sc_pool_name, sizeof(sc_pool_name),
398                  "lio_sc_pool_%u", lio_dev->port_id);
399         lio_dev->sc_buf_pool = rte_pktmbuf_pool_create(sc_pool_name,
400                                                 LIO_MAX_SOFT_COMMAND_BUFFERS,
401                                                 0, 0, buf_size, SOCKET_ID_ANY);
402         return 0;
403 }
404
405 void
406 lio_free_sc_buffer_pool(struct lio_device *lio_dev)
407 {
408         rte_mempool_free(lio_dev->sc_buf_pool);
409 }
410
411 struct lio_soft_command *
412 lio_alloc_soft_command(struct lio_device *lio_dev, uint32_t datasize,
413                        uint32_t rdatasize, uint32_t ctxsize)
414 {
415         uint32_t offset = sizeof(struct lio_soft_command);
416         struct lio_soft_command *sc;
417         struct rte_mbuf *m;
418         uint64_t dma_addr;
419
420         RTE_ASSERT((offset + datasize + rdatasize + ctxsize) <=
421                    LIO_SOFT_COMMAND_BUFFER_SIZE);
422
423         m = rte_pktmbuf_alloc(lio_dev->sc_buf_pool);
424         if (m == NULL) {
425                 lio_dev_err(lio_dev, "Cannot allocate mbuf for sc\n");
426                 return NULL;
427         }
428
429         /* set rte_mbuf data size and there is only 1 segment */
430         m->pkt_len = LIO_SOFT_COMMAND_BUFFER_SIZE;
431         m->data_len = LIO_SOFT_COMMAND_BUFFER_SIZE;
432
433         /* use rte_mbuf buffer for soft command */
434         sc = rte_pktmbuf_mtod(m, struct lio_soft_command *);
435         memset(sc, 0, LIO_SOFT_COMMAND_BUFFER_SIZE);
436         sc->size = LIO_SOFT_COMMAND_BUFFER_SIZE;
437         sc->dma_addr = rte_mbuf_data_dma_addr(m);
438         sc->mbuf = m;
439
440         dma_addr = sc->dma_addr;
441
442         if (ctxsize) {
443                 sc->ctxptr = (uint8_t *)sc + offset;
444                 sc->ctxsize = ctxsize;
445         }
446
447         /* Start data at 128 byte boundary */
448         offset = (offset + ctxsize + 127) & 0xffffff80;
449
450         if (datasize) {
451                 sc->virtdptr = (uint8_t *)sc + offset;
452                 sc->dmadptr = dma_addr + offset;
453                 sc->datasize = datasize;
454         }
455
456         /* Start rdata at 128 byte boundary */
457         offset = (offset + datasize + 127) & 0xffffff80;
458
459         if (rdatasize) {
460                 RTE_ASSERT(rdatasize >= 16);
461                 sc->virtrptr = (uint8_t *)sc + offset;
462                 sc->dmarptr = dma_addr + offset;
463                 sc->rdatasize = rdatasize;
464                 sc->status_word = (uint64_t *)((uint8_t *)(sc->virtrptr) +
465                                                rdatasize - 8);
466         }
467
468         return sc;
469 }
470
471 void
472 lio_free_soft_command(struct lio_soft_command *sc)
473 {
474         rte_pktmbuf_free(sc->mbuf);
475 }
476
477 void
478 lio_setup_response_list(struct lio_device *lio_dev)
479 {
480         STAILQ_INIT(&lio_dev->response_list.head);
481         rte_spinlock_init(&lio_dev->response_list.lock);
482         rte_atomic64_set(&lio_dev->response_list.pending_req_count, 0);
483 }
484
485 int
486 lio_process_ordered_list(struct lio_device *lio_dev)
487 {
488         int resp_to_process = LIO_MAX_ORD_REQS_TO_PROCESS;
489         struct lio_response_list *ordered_sc_list;
490         struct lio_soft_command *sc;
491         int request_complete = 0;
492         uint64_t status64;
493         uint32_t status;
494
495         ordered_sc_list = &lio_dev->response_list;
496
497         do {
498                 rte_spinlock_lock(&ordered_sc_list->lock);
499
500                 if (STAILQ_EMPTY(&ordered_sc_list->head)) {
501                         /* ordered_sc_list is empty; there is
502                          * nothing to process
503                          */
504                         rte_spinlock_unlock(&ordered_sc_list->lock);
505                         return -1;
506                 }
507
508                 sc = LIO_STQUEUE_FIRST_ENTRY(&ordered_sc_list->head,
509                                              struct lio_soft_command, node);
510
511                 status = LIO_REQUEST_PENDING;
512
513                 /* check if octeon has finished DMA'ing a response
514                  * to where rptr is pointing to
515                  */
516                 status64 = *sc->status_word;
517
518                 if (status64 != LIO_COMPLETION_WORD_INIT) {
519                         /* This logic ensures that all 64b have been written.
520                          * 1. check byte 0 for non-FF
521                          * 2. if non-FF, then swap result from BE to host order
522                          * 3. check byte 7 (swapped to 0) for non-FF
523                          * 4. if non-FF, use the low 32-bit status code
524                          * 5. if either byte 0 or byte 7 is FF, don't use status
525                          */
526                         if ((status64 & 0xff) != 0xff) {
527                                 lio_swap_8B_data(&status64, 1);
528                                 if (((status64 & 0xff) != 0xff)) {
529                                         /* retrieve 16-bit firmware status */
530                                         status = (uint32_t)(status64 &
531                                                             0xffffULL);
532                                         if (status) {
533                                                 status =
534                                                 LIO_FIRMWARE_STATUS_CODE(
535                                                                         status);
536                                         } else {
537                                                 /* i.e. no error */
538                                                 status = LIO_REQUEST_DONE;
539                                         }
540                                 }
541                         }
542                 } else if ((sc->timeout && lio_check_timeout(lio_uptime,
543                                                              sc->timeout))) {
544                         lio_dev_err(lio_dev,
545                                     "cmd failed, timeout (%ld, %ld)\n",
546                                     (long)lio_uptime, (long)sc->timeout);
547                         status = LIO_REQUEST_TIMEOUT;
548                 }
549
550                 if (status != LIO_REQUEST_PENDING) {
551                         /* we have received a response or we have timed out.
552                          * remove node from linked list
553                          */
554                         STAILQ_REMOVE(&ordered_sc_list->head,
555                                       &sc->node, lio_stailq_node, entries);
556                         rte_atomic64_dec(
557                             &lio_dev->response_list.pending_req_count);
558                         rte_spinlock_unlock(&ordered_sc_list->lock);
559
560                         if (sc->callback)
561                                 sc->callback(status, sc->callback_arg);
562
563                         request_complete++;
564                 } else {
565                         /* no response yet */
566                         request_complete = 0;
567                         rte_spinlock_unlock(&ordered_sc_list->lock);
568                 }
569
570                 /* If we hit the Max Ordered requests to process every loop,
571                  * we quit and let this function be invoked the next time
572                  * the poll thread runs to process the remaining requests.
573                  * This function can take up the entire CPU if there is
574                  * no upper limit to the requests processed.
575                  */
576                 if (request_complete >= resp_to_process)
577                         break;
578         } while (request_complete);
579
580         return 0;
581 }