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