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