net/dpaa: support Tx and Rx queue setup
[dpdk.git] / drivers / net / dpaa / dpaa_ethdev.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2016 Freescale Semiconductor, Inc. All rights reserved.
5  *   Copyright 2017 NXP.
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  Freescale Semiconductor, 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 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 /* System headers */
34 #include <stdio.h>
35 #include <inttypes.h>
36 #include <unistd.h>
37 #include <limits.h>
38 #include <sched.h>
39 #include <signal.h>
40 #include <pthread.h>
41 #include <sys/types.h>
42 #include <sys/syscall.h>
43
44 #include <rte_config.h>
45 #include <rte_byteorder.h>
46 #include <rte_common.h>
47 #include <rte_interrupts.h>
48 #include <rte_log.h>
49 #include <rte_debug.h>
50 #include <rte_pci.h>
51 #include <rte_atomic.h>
52 #include <rte_branch_prediction.h>
53 #include <rte_memory.h>
54 #include <rte_memzone.h>
55 #include <rte_tailq.h>
56 #include <rte_eal.h>
57 #include <rte_alarm.h>
58 #include <rte_ether.h>
59 #include <rte_ethdev.h>
60 #include <rte_malloc.h>
61 #include <rte_ring.h>
62
63 #include <rte_dpaa_bus.h>
64 #include <rte_dpaa_logs.h>
65 #include <dpaa_mempool.h>
66
67 #include <dpaa_ethdev.h>
68 #include <dpaa_rxtx.h>
69
70 #include <fsl_usd.h>
71 #include <fsl_qman.h>
72 #include <fsl_bman.h>
73 #include <fsl_fman.h>
74
75 /* Keep track of whether QMAN and BMAN have been globally initialized */
76 static int is_global_init;
77
78 static int
79 dpaa_eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
80 {
81         PMD_INIT_FUNC_TRACE();
82
83         return 0;
84 }
85
86 static int dpaa_eth_dev_start(struct rte_eth_dev *dev)
87 {
88         struct dpaa_if *dpaa_intf = dev->data->dev_private;
89
90         PMD_INIT_FUNC_TRACE();
91
92         /* Change tx callback to the real one */
93         dev->tx_pkt_burst = dpaa_eth_queue_tx;
94         fman_if_enable_rx(dpaa_intf->fif);
95
96         return 0;
97 }
98
99 static void dpaa_eth_dev_stop(struct rte_eth_dev *dev)
100 {
101         struct dpaa_if *dpaa_intf = dev->data->dev_private;
102
103         PMD_INIT_FUNC_TRACE();
104
105         fman_if_disable_rx(dpaa_intf->fif);
106         dev->tx_pkt_burst = dpaa_eth_tx_drop_all;
107 }
108
109 static void dpaa_eth_dev_close(struct rte_eth_dev *dev)
110 {
111         PMD_INIT_FUNC_TRACE();
112
113         dpaa_eth_dev_stop(dev);
114 }
115
116 static
117 int dpaa_eth_rx_queue_setup(struct rte_eth_dev *dev, uint16_t queue_idx,
118                             uint16_t nb_desc __rte_unused,
119                             unsigned int socket_id __rte_unused,
120                             const struct rte_eth_rxconf *rx_conf __rte_unused,
121                             struct rte_mempool *mp)
122 {
123         struct dpaa_if *dpaa_intf = dev->data->dev_private;
124
125         PMD_INIT_FUNC_TRACE();
126
127         DPAA_PMD_INFO("Rx queue setup for queue index: %d", queue_idx);
128
129         if (!dpaa_intf->bp_info || dpaa_intf->bp_info->mp != mp) {
130                 struct fman_if_ic_params icp;
131                 uint32_t fd_offset;
132                 uint32_t bp_size;
133
134                 if (!mp->pool_data) {
135                         DPAA_PMD_ERR("Not an offloaded buffer pool!");
136                         return -1;
137                 }
138                 dpaa_intf->bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
139
140                 memset(&icp, 0, sizeof(icp));
141                 /* set ICEOF for to the default value , which is 0*/
142                 icp.iciof = DEFAULT_ICIOF;
143                 icp.iceof = DEFAULT_RX_ICEOF;
144                 icp.icsz = DEFAULT_ICSZ;
145                 fman_if_set_ic_params(dpaa_intf->fif, &icp);
146
147                 fd_offset = RTE_PKTMBUF_HEADROOM + DPAA_HW_BUF_RESERVE;
148                 fman_if_set_fdoff(dpaa_intf->fif, fd_offset);
149
150                 /* Buffer pool size should be equal to Dataroom Size*/
151                 bp_size = rte_pktmbuf_data_room_size(mp);
152                 fman_if_set_bp(dpaa_intf->fif, mp->size,
153                                dpaa_intf->bp_info->bpid, bp_size);
154                 dpaa_intf->valid = 1;
155                 DPAA_PMD_INFO("if =%s - fd_offset = %d offset = %d",
156                             dpaa_intf->name, fd_offset,
157                         fman_if_get_fdoff(dpaa_intf->fif));
158         }
159         dev->data->rx_queues[queue_idx] = &dpaa_intf->rx_queues[queue_idx];
160
161         return 0;
162 }
163
164 static
165 void dpaa_eth_rx_queue_release(void *rxq __rte_unused)
166 {
167         PMD_INIT_FUNC_TRACE();
168 }
169
170 static
171 int dpaa_eth_tx_queue_setup(struct rte_eth_dev *dev, uint16_t queue_idx,
172                             uint16_t nb_desc __rte_unused,
173                 unsigned int socket_id __rte_unused,
174                 const struct rte_eth_txconf *tx_conf __rte_unused)
175 {
176         struct dpaa_if *dpaa_intf = dev->data->dev_private;
177
178         PMD_INIT_FUNC_TRACE();
179
180         DPAA_PMD_INFO("Tx queue setup for queue index: %d", queue_idx);
181         dev->data->tx_queues[queue_idx] = &dpaa_intf->tx_queues[queue_idx];
182         return 0;
183 }
184
185 static void dpaa_eth_tx_queue_release(void *txq __rte_unused)
186 {
187         PMD_INIT_FUNC_TRACE();
188 }
189
190 static struct eth_dev_ops dpaa_devops = {
191         .dev_configure            = dpaa_eth_dev_configure,
192         .dev_start                = dpaa_eth_dev_start,
193         .dev_stop                 = dpaa_eth_dev_stop,
194         .dev_close                = dpaa_eth_dev_close,
195
196         .rx_queue_setup           = dpaa_eth_rx_queue_setup,
197         .tx_queue_setup           = dpaa_eth_tx_queue_setup,
198         .rx_queue_release         = dpaa_eth_rx_queue_release,
199         .tx_queue_release         = dpaa_eth_tx_queue_release,
200 };
201
202 /* Initialise an Rx FQ */
203 static int dpaa_rx_queue_init(struct qman_fq *fq,
204                               uint32_t fqid)
205 {
206         struct qm_mcc_initfq opts;
207         int ret;
208
209         PMD_INIT_FUNC_TRACE();
210
211         ret = qman_reserve_fqid(fqid);
212         if (ret) {
213                 DPAA_PMD_ERR("reserve rx fqid %d failed with ret: %d",
214                              fqid, ret);
215                 return -EINVAL;
216         }
217
218         DPAA_PMD_DEBUG("creating rx fq %p, fqid %d", fq, fqid);
219         ret = qman_create_fq(fqid, QMAN_FQ_FLAG_NO_ENQUEUE, fq);
220         if (ret) {
221                 DPAA_PMD_ERR("create rx fqid %d failed with ret: %d",
222                         fqid, ret);
223                 return ret;
224         }
225
226         opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
227                        QM_INITFQ_WE_CONTEXTA;
228
229         opts.fqd.dest.wq = DPAA_IF_RX_PRIORITY;
230         opts.fqd.fq_ctrl = QM_FQCTRL_AVOIDBLOCK | QM_FQCTRL_CTXASTASHING |
231                            QM_FQCTRL_PREFERINCACHE;
232         opts.fqd.context_a.stashing.exclusive = 0;
233         opts.fqd.context_a.stashing.annotation_cl = DPAA_IF_RX_ANNOTATION_STASH;
234         opts.fqd.context_a.stashing.data_cl = DPAA_IF_RX_DATA_STASH;
235         opts.fqd.context_a.stashing.context_cl = DPAA_IF_RX_CONTEXT_STASH;
236
237         /*Enable tail drop */
238         opts.we_mask = opts.we_mask | QM_INITFQ_WE_TDTHRESH;
239         opts.fqd.fq_ctrl = opts.fqd.fq_ctrl | QM_FQCTRL_TDE;
240         qm_fqd_taildrop_set(&opts.fqd.td, CONG_THRESHOLD_RX_Q, 1);
241
242         ret = qman_init_fq(fq, 0, &opts);
243         if (ret)
244                 DPAA_PMD_ERR("init rx fqid %d failed with ret: %d", fqid, ret);
245         return ret;
246 }
247
248 /* Initialise a Tx FQ */
249 static int dpaa_tx_queue_init(struct qman_fq *fq,
250                               struct fman_if *fman_intf)
251 {
252         struct qm_mcc_initfq opts;
253         int ret;
254
255         PMD_INIT_FUNC_TRACE();
256
257         ret = qman_create_fq(0, QMAN_FQ_FLAG_DYNAMIC_FQID |
258                              QMAN_FQ_FLAG_TO_DCPORTAL, fq);
259         if (ret) {
260                 DPAA_PMD_ERR("create tx fq failed with ret: %d", ret);
261                 return ret;
262         }
263         opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
264                        QM_INITFQ_WE_CONTEXTB | QM_INITFQ_WE_CONTEXTA;
265         opts.fqd.dest.channel = fman_intf->tx_channel_id;
266         opts.fqd.dest.wq = DPAA_IF_TX_PRIORITY;
267         opts.fqd.fq_ctrl = QM_FQCTRL_PREFERINCACHE;
268         opts.fqd.context_b = 0;
269         /* no tx-confirmation */
270         opts.fqd.context_a.hi = 0x80000000 | fman_dealloc_bufs_mask_hi;
271         opts.fqd.context_a.lo = 0 | fman_dealloc_bufs_mask_lo;
272         DPAA_PMD_DEBUG("init tx fq %p, fqid %d", fq, fq->fqid);
273         ret = qman_init_fq(fq, QMAN_INITFQ_FLAG_SCHED, &opts);
274         if (ret)
275                 DPAA_PMD_ERR("init tx fqid %d failed %d", fq->fqid, ret);
276         return ret;
277 }
278
279 /* Initialise a network interface */
280 static int
281 dpaa_dev_init(struct rte_eth_dev *eth_dev)
282 {
283         int num_cores, num_rx_fqs, fqid;
284         int loop, ret = 0;
285         int dev_id;
286         struct rte_dpaa_device *dpaa_device;
287         struct dpaa_if *dpaa_intf;
288         struct fm_eth_port_cfg *cfg;
289         struct fman_if *fman_intf;
290         struct fman_if_bpool *bp, *tmp_bp;
291
292         PMD_INIT_FUNC_TRACE();
293
294         /* For secondary processes, the primary has done all the work */
295         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
296                 return 0;
297
298         dpaa_device = DEV_TO_DPAA_DEVICE(eth_dev->device);
299         dev_id = dpaa_device->id.dev_id;
300         dpaa_intf = eth_dev->data->dev_private;
301         cfg = &dpaa_netcfg->port_cfg[dev_id];
302         fman_intf = cfg->fman_if;
303
304         dpaa_intf->name = dpaa_device->name;
305
306         /* save fman_if & cfg in the interface struture */
307         dpaa_intf->fif = fman_intf;
308         dpaa_intf->ifid = dev_id;
309         dpaa_intf->cfg = cfg;
310
311         /* Initialize Rx FQ's */
312         if (getenv("DPAA_NUM_RX_QUEUES"))
313                 num_rx_fqs = atoi(getenv("DPAA_NUM_RX_QUEUES"));
314         else
315                 num_rx_fqs = DPAA_DEFAULT_NUM_PCD_QUEUES;
316
317         /* Each device can not have more than DPAA_PCD_FQID_MULTIPLIER RX
318          * queues.
319          */
320         if (num_rx_fqs <= 0 || num_rx_fqs > DPAA_PCD_FQID_MULTIPLIER) {
321                 DPAA_PMD_ERR("Invalid number of RX queues\n");
322                 return -EINVAL;
323         }
324
325         dpaa_intf->rx_queues = rte_zmalloc(NULL,
326                 sizeof(struct qman_fq) * num_rx_fqs, MAX_CACHELINE);
327         for (loop = 0; loop < num_rx_fqs; loop++) {
328                 fqid = DPAA_PCD_FQID_START + dpaa_intf->ifid *
329                         DPAA_PCD_FQID_MULTIPLIER + loop;
330                 ret = dpaa_rx_queue_init(&dpaa_intf->rx_queues[loop], fqid);
331                 if (ret)
332                         return ret;
333                 dpaa_intf->rx_queues[loop].dpaa_intf = dpaa_intf;
334         }
335         dpaa_intf->nb_rx_queues = num_rx_fqs;
336
337         /* Initialise Tx FQs. Have as many Tx FQ's as number of cores */
338         num_cores = rte_lcore_count();
339         dpaa_intf->tx_queues = rte_zmalloc(NULL, sizeof(struct qman_fq) *
340                 num_cores, MAX_CACHELINE);
341         if (!dpaa_intf->tx_queues)
342                 return -ENOMEM;
343
344         for (loop = 0; loop < num_cores; loop++) {
345                 ret = dpaa_tx_queue_init(&dpaa_intf->tx_queues[loop],
346                                          fman_intf);
347                 if (ret)
348                         return ret;
349                 dpaa_intf->tx_queues[loop].dpaa_intf = dpaa_intf;
350         }
351         dpaa_intf->nb_tx_queues = num_cores;
352
353         DPAA_PMD_DEBUG("All frame queues created");
354
355         /* reset bpool list, initialize bpool dynamically */
356         list_for_each_entry_safe(bp, tmp_bp, &cfg->fman_if->bpool_list, node) {
357                 list_del(&bp->node);
358                 rte_free(bp);
359         }
360
361         /* Populate ethdev structure */
362         eth_dev->dev_ops = &dpaa_devops;
363         eth_dev->rx_pkt_burst = dpaa_eth_queue_rx;
364         eth_dev->tx_pkt_burst = dpaa_eth_tx_drop_all;
365
366         /* Allocate memory for storing MAC addresses */
367         eth_dev->data->mac_addrs = rte_zmalloc("mac_addr",
368                 ETHER_ADDR_LEN * DPAA_MAX_MAC_FILTER, 0);
369         if (eth_dev->data->mac_addrs == NULL) {
370                 DPAA_PMD_ERR("Failed to allocate %d bytes needed to "
371                                                 "store MAC addresses",
372                                 ETHER_ADDR_LEN * DPAA_MAX_MAC_FILTER);
373                 rte_free(dpaa_intf->rx_queues);
374                 rte_free(dpaa_intf->tx_queues);
375                 dpaa_intf->rx_queues = NULL;
376                 dpaa_intf->tx_queues = NULL;
377                 dpaa_intf->nb_rx_queues = 0;
378                 dpaa_intf->nb_tx_queues = 0;
379                 return -ENOMEM;
380         }
381
382         /* copy the primary mac address */
383         ether_addr_copy(&fman_intf->mac_addr, &eth_dev->data->mac_addrs[0]);
384
385         RTE_LOG(INFO, PMD, "net: dpaa: %s: %02x:%02x:%02x:%02x:%02x:%02x\n",
386                 dpaa_device->name,
387                 fman_intf->mac_addr.addr_bytes[0],
388                 fman_intf->mac_addr.addr_bytes[1],
389                 fman_intf->mac_addr.addr_bytes[2],
390                 fman_intf->mac_addr.addr_bytes[3],
391                 fman_intf->mac_addr.addr_bytes[4],
392                 fman_intf->mac_addr.addr_bytes[5]);
393
394         /* Disable RX mode */
395         fman_if_discard_rx_errors(fman_intf);
396         fman_if_disable_rx(fman_intf);
397         /* Disable promiscuous mode */
398         fman_if_promiscuous_disable(fman_intf);
399         /* Disable multicast */
400         fman_if_reset_mcast_filter_table(fman_intf);
401         /* Reset interface statistics */
402         fman_if_stats_reset(fman_intf);
403
404         return 0;
405 }
406
407 static int
408 dpaa_dev_uninit(struct rte_eth_dev *dev)
409 {
410         struct dpaa_if *dpaa_intf = dev->data->dev_private;
411
412         PMD_INIT_FUNC_TRACE();
413
414         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
415                 return -EPERM;
416
417         if (!dpaa_intf) {
418                 DPAA_PMD_WARN("Already closed or not started");
419                 return -1;
420         }
421
422         dpaa_eth_dev_close(dev);
423
424         /* release configuration memory */
425         if (dpaa_intf->fc_conf)
426                 rte_free(dpaa_intf->fc_conf);
427
428         rte_free(dpaa_intf->rx_queues);
429         dpaa_intf->rx_queues = NULL;
430
431         rte_free(dpaa_intf->tx_queues);
432         dpaa_intf->tx_queues = NULL;
433
434         /* free memory for storing MAC addresses */
435         rte_free(dev->data->mac_addrs);
436         dev->data->mac_addrs = NULL;
437
438         dev->dev_ops = NULL;
439         dev->rx_pkt_burst = NULL;
440         dev->tx_pkt_burst = NULL;
441
442         return 0;
443 }
444
445 static int
446 rte_dpaa_probe(struct rte_dpaa_driver *dpaa_drv,
447                struct rte_dpaa_device *dpaa_dev)
448 {
449         int diag;
450         int ret;
451         struct rte_eth_dev *eth_dev;
452
453         PMD_INIT_FUNC_TRACE();
454
455         /* In case of secondary process, the device is already configured
456          * and no further action is required, except portal initialization
457          * and verifying secondary attachment to port name.
458          */
459         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
460                 eth_dev = rte_eth_dev_attach_secondary(dpaa_dev->name);
461                 if (!eth_dev)
462                         return -ENOMEM;
463                 return 0;
464         }
465
466         if (!is_global_init) {
467                 /* One time load of Qman/Bman drivers */
468                 ret = qman_global_init();
469                 if (ret) {
470                         DPAA_PMD_ERR("QMAN initialization failed: %d",
471                                      ret);
472                         return ret;
473                 }
474                 ret = bman_global_init();
475                 if (ret) {
476                         DPAA_PMD_ERR("BMAN initialization failed: %d",
477                                      ret);
478                         return ret;
479                 }
480
481                 is_global_init = 1;
482         }
483
484         ret = rte_dpaa_portal_init((void *)1);
485         if (ret) {
486                 DPAA_PMD_ERR("Unable to initialize portal");
487                 return ret;
488         }
489
490         eth_dev = rte_eth_dev_allocate(dpaa_dev->name);
491         if (eth_dev == NULL)
492                 return -ENOMEM;
493
494         eth_dev->data->dev_private = rte_zmalloc(
495                                         "ethdev private structure",
496                                         sizeof(struct dpaa_if),
497                                         RTE_CACHE_LINE_SIZE);
498         if (!eth_dev->data->dev_private) {
499                 DPAA_PMD_ERR("Cannot allocate memzone for port data");
500                 rte_eth_dev_release_port(eth_dev);
501                 return -ENOMEM;
502         }
503
504         eth_dev->device = &dpaa_dev->device;
505         eth_dev->device->driver = &dpaa_drv->driver;
506         dpaa_dev->eth_dev = eth_dev;
507
508         /* Invoke PMD device initialization function */
509         diag = dpaa_dev_init(eth_dev);
510         if (diag == 0)
511                 return 0;
512
513         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
514                 rte_free(eth_dev->data->dev_private);
515
516         rte_eth_dev_release_port(eth_dev);
517         return diag;
518 }
519
520 static int
521 rte_dpaa_remove(struct rte_dpaa_device *dpaa_dev)
522 {
523         struct rte_eth_dev *eth_dev;
524
525         PMD_INIT_FUNC_TRACE();
526
527         eth_dev = dpaa_dev->eth_dev;
528         dpaa_dev_uninit(eth_dev);
529
530         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
531                 rte_free(eth_dev->data->dev_private);
532
533         rte_eth_dev_release_port(eth_dev);
534
535         return 0;
536 }
537
538 static struct rte_dpaa_driver rte_dpaa_pmd = {
539         .drv_type = FSL_DPAA_ETH,
540         .probe = rte_dpaa_probe,
541         .remove = rte_dpaa_remove,
542 };
543
544 RTE_PMD_REGISTER_DPAA(net_dpaa, rte_dpaa_pmd);