8ee00ed97151142bb815660cbfaeb65d01cab30d
[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 const uint32_t *
116 dpaa_supported_ptypes_get(struct rte_eth_dev *dev)
117 {
118         static const uint32_t ptypes[] = {
119                 /*todo -= add more types */
120                 RTE_PTYPE_L2_ETHER,
121                 RTE_PTYPE_L3_IPV4,
122                 RTE_PTYPE_L3_IPV4_EXT,
123                 RTE_PTYPE_L3_IPV6,
124                 RTE_PTYPE_L3_IPV6_EXT,
125                 RTE_PTYPE_L4_TCP,
126                 RTE_PTYPE_L4_UDP,
127                 RTE_PTYPE_L4_SCTP
128         };
129
130         PMD_INIT_FUNC_TRACE();
131
132         if (dev->rx_pkt_burst == dpaa_eth_queue_rx)
133                 return ptypes;
134         return NULL;
135 }
136
137 static int dpaa_eth_dev_start(struct rte_eth_dev *dev)
138 {
139         struct dpaa_if *dpaa_intf = dev->data->dev_private;
140
141         PMD_INIT_FUNC_TRACE();
142
143         /* Change tx callback to the real one */
144         dev->tx_pkt_burst = dpaa_eth_queue_tx;
145         fman_if_enable_rx(dpaa_intf->fif);
146
147         return 0;
148 }
149
150 static void dpaa_eth_dev_stop(struct rte_eth_dev *dev)
151 {
152         struct dpaa_if *dpaa_intf = dev->data->dev_private;
153
154         PMD_INIT_FUNC_TRACE();
155
156         fman_if_disable_rx(dpaa_intf->fif);
157         dev->tx_pkt_burst = dpaa_eth_tx_drop_all;
158 }
159
160 static void dpaa_eth_dev_close(struct rte_eth_dev *dev)
161 {
162         PMD_INIT_FUNC_TRACE();
163
164         dpaa_eth_dev_stop(dev);
165 }
166
167 static void dpaa_eth_dev_info(struct rte_eth_dev *dev,
168                               struct rte_eth_dev_info *dev_info)
169 {
170         struct dpaa_if *dpaa_intf = dev->data->dev_private;
171
172         PMD_INIT_FUNC_TRACE();
173
174         dev_info->max_rx_queues = dpaa_intf->nb_rx_queues;
175         dev_info->max_tx_queues = dpaa_intf->nb_tx_queues;
176         dev_info->min_rx_bufsize = DPAA_MIN_RX_BUF_SIZE;
177         dev_info->max_rx_pktlen = DPAA_MAX_RX_PKT_LEN;
178         dev_info->max_mac_addrs = DPAA_MAX_MAC_FILTER;
179         dev_info->max_hash_mac_addrs = 0;
180         dev_info->max_vfs = 0;
181         dev_info->max_vmdq_pools = ETH_16_POOLS;
182         dev_info->flow_type_rss_offloads = DPAA_RSS_OFFLOAD_ALL;
183         dev_info->speed_capa = (ETH_LINK_SPEED_1G |
184                                 ETH_LINK_SPEED_10G);
185         dev_info->rx_offload_capa =
186                 (DEV_RX_OFFLOAD_IPV4_CKSUM |
187                 DEV_RX_OFFLOAD_UDP_CKSUM   |
188                 DEV_RX_OFFLOAD_TCP_CKSUM);
189 }
190
191 static int dpaa_eth_link_update(struct rte_eth_dev *dev,
192                                 int wait_to_complete __rte_unused)
193 {
194         struct dpaa_if *dpaa_intf = dev->data->dev_private;
195         struct rte_eth_link *link = &dev->data->dev_link;
196
197         PMD_INIT_FUNC_TRACE();
198
199         if (dpaa_intf->fif->mac_type == fman_mac_1g)
200                 link->link_speed = 1000;
201         else if (dpaa_intf->fif->mac_type == fman_mac_10g)
202                 link->link_speed = 10000;
203         else
204                 DPAA_PMD_ERR("invalid link_speed: %s, %d",
205                              dpaa_intf->name, dpaa_intf->fif->mac_type);
206
207         link->link_status = dpaa_intf->valid;
208         link->link_duplex = ETH_LINK_FULL_DUPLEX;
209         link->link_autoneg = ETH_LINK_AUTONEG;
210         return 0;
211 }
212
213 static void dpaa_eth_stats_get(struct rte_eth_dev *dev,
214                                struct rte_eth_stats *stats)
215 {
216         struct dpaa_if *dpaa_intf = dev->data->dev_private;
217
218         PMD_INIT_FUNC_TRACE();
219
220         fman_if_stats_get(dpaa_intf->fif, stats);
221 }
222
223 static void dpaa_eth_stats_reset(struct rte_eth_dev *dev)
224 {
225         struct dpaa_if *dpaa_intf = dev->data->dev_private;
226
227         PMD_INIT_FUNC_TRACE();
228
229         fman_if_stats_reset(dpaa_intf->fif);
230 }
231
232 static void dpaa_eth_promiscuous_enable(struct rte_eth_dev *dev)
233 {
234         struct dpaa_if *dpaa_intf = dev->data->dev_private;
235
236         PMD_INIT_FUNC_TRACE();
237
238         fman_if_promiscuous_enable(dpaa_intf->fif);
239 }
240
241 static void dpaa_eth_promiscuous_disable(struct rte_eth_dev *dev)
242 {
243         struct dpaa_if *dpaa_intf = dev->data->dev_private;
244
245         PMD_INIT_FUNC_TRACE();
246
247         fman_if_promiscuous_disable(dpaa_intf->fif);
248 }
249
250 static void dpaa_eth_multicast_enable(struct rte_eth_dev *dev)
251 {
252         struct dpaa_if *dpaa_intf = dev->data->dev_private;
253
254         PMD_INIT_FUNC_TRACE();
255
256         fman_if_set_mcast_filter_table(dpaa_intf->fif);
257 }
258
259 static void dpaa_eth_multicast_disable(struct rte_eth_dev *dev)
260 {
261         struct dpaa_if *dpaa_intf = dev->data->dev_private;
262
263         PMD_INIT_FUNC_TRACE();
264
265         fman_if_reset_mcast_filter_table(dpaa_intf->fif);
266 }
267
268 static
269 int dpaa_eth_rx_queue_setup(struct rte_eth_dev *dev, uint16_t queue_idx,
270                             uint16_t nb_desc __rte_unused,
271                             unsigned int socket_id __rte_unused,
272                             const struct rte_eth_rxconf *rx_conf __rte_unused,
273                             struct rte_mempool *mp)
274 {
275         struct dpaa_if *dpaa_intf = dev->data->dev_private;
276
277         PMD_INIT_FUNC_TRACE();
278
279         DPAA_PMD_INFO("Rx queue setup for queue index: %d", queue_idx);
280
281         if (!dpaa_intf->bp_info || dpaa_intf->bp_info->mp != mp) {
282                 struct fman_if_ic_params icp;
283                 uint32_t fd_offset;
284                 uint32_t bp_size;
285
286                 if (!mp->pool_data) {
287                         DPAA_PMD_ERR("Not an offloaded buffer pool!");
288                         return -1;
289                 }
290                 dpaa_intf->bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
291
292                 memset(&icp, 0, sizeof(icp));
293                 /* set ICEOF for to the default value , which is 0*/
294                 icp.iciof = DEFAULT_ICIOF;
295                 icp.iceof = DEFAULT_RX_ICEOF;
296                 icp.icsz = DEFAULT_ICSZ;
297                 fman_if_set_ic_params(dpaa_intf->fif, &icp);
298
299                 fd_offset = RTE_PKTMBUF_HEADROOM + DPAA_HW_BUF_RESERVE;
300                 fman_if_set_fdoff(dpaa_intf->fif, fd_offset);
301
302                 /* Buffer pool size should be equal to Dataroom Size*/
303                 bp_size = rte_pktmbuf_data_room_size(mp);
304                 fman_if_set_bp(dpaa_intf->fif, mp->size,
305                                dpaa_intf->bp_info->bpid, bp_size);
306                 dpaa_intf->valid = 1;
307                 DPAA_PMD_INFO("if =%s - fd_offset = %d offset = %d",
308                             dpaa_intf->name, fd_offset,
309                         fman_if_get_fdoff(dpaa_intf->fif));
310         }
311         dev->data->rx_queues[queue_idx] = &dpaa_intf->rx_queues[queue_idx];
312
313         return 0;
314 }
315
316 static
317 void dpaa_eth_rx_queue_release(void *rxq __rte_unused)
318 {
319         PMD_INIT_FUNC_TRACE();
320 }
321
322 static
323 int dpaa_eth_tx_queue_setup(struct rte_eth_dev *dev, uint16_t queue_idx,
324                             uint16_t nb_desc __rte_unused,
325                 unsigned int socket_id __rte_unused,
326                 const struct rte_eth_txconf *tx_conf __rte_unused)
327 {
328         struct dpaa_if *dpaa_intf = dev->data->dev_private;
329
330         PMD_INIT_FUNC_TRACE();
331
332         DPAA_PMD_INFO("Tx queue setup for queue index: %d", queue_idx);
333         dev->data->tx_queues[queue_idx] = &dpaa_intf->tx_queues[queue_idx];
334         return 0;
335 }
336
337 static void dpaa_eth_tx_queue_release(void *txq __rte_unused)
338 {
339         PMD_INIT_FUNC_TRACE();
340 }
341
342 static int dpaa_link_down(struct rte_eth_dev *dev)
343 {
344         PMD_INIT_FUNC_TRACE();
345
346         dpaa_eth_dev_stop(dev);
347         return 0;
348 }
349
350 static int dpaa_link_up(struct rte_eth_dev *dev)
351 {
352         PMD_INIT_FUNC_TRACE();
353
354         dpaa_eth_dev_start(dev);
355         return 0;
356 }
357
358 static int
359 dpaa_flow_ctrl_set(struct rte_eth_dev *dev,
360                    struct rte_eth_fc_conf *fc_conf)
361 {
362         struct dpaa_if *dpaa_intf = dev->data->dev_private;
363         struct rte_eth_fc_conf *net_fc;
364
365         PMD_INIT_FUNC_TRACE();
366
367         if (!(dpaa_intf->fc_conf)) {
368                 dpaa_intf->fc_conf = rte_zmalloc(NULL,
369                         sizeof(struct rte_eth_fc_conf), MAX_CACHELINE);
370                 if (!dpaa_intf->fc_conf) {
371                         DPAA_PMD_ERR("unable to save flow control info");
372                         return -ENOMEM;
373                 }
374         }
375         net_fc = dpaa_intf->fc_conf;
376
377         if (fc_conf->high_water < fc_conf->low_water) {
378                 DPAA_PMD_ERR("Incorrect Flow Control Configuration");
379                 return -EINVAL;
380         }
381
382         if (fc_conf->mode == RTE_FC_NONE) {
383                 return 0;
384         } else if (fc_conf->mode == RTE_FC_TX_PAUSE ||
385                  fc_conf->mode == RTE_FC_FULL) {
386                 fman_if_set_fc_threshold(dpaa_intf->fif, fc_conf->high_water,
387                                          fc_conf->low_water,
388                                 dpaa_intf->bp_info->bpid);
389                 if (fc_conf->pause_time)
390                         fman_if_set_fc_quanta(dpaa_intf->fif,
391                                               fc_conf->pause_time);
392         }
393
394         /* Save the information in dpaa device */
395         net_fc->pause_time = fc_conf->pause_time;
396         net_fc->high_water = fc_conf->high_water;
397         net_fc->low_water = fc_conf->low_water;
398         net_fc->send_xon = fc_conf->send_xon;
399         net_fc->mac_ctrl_frame_fwd = fc_conf->mac_ctrl_frame_fwd;
400         net_fc->mode = fc_conf->mode;
401         net_fc->autoneg = fc_conf->autoneg;
402
403         return 0;
404 }
405
406 static int
407 dpaa_flow_ctrl_get(struct rte_eth_dev *dev,
408                    struct rte_eth_fc_conf *fc_conf)
409 {
410         struct dpaa_if *dpaa_intf = dev->data->dev_private;
411         struct rte_eth_fc_conf *net_fc = dpaa_intf->fc_conf;
412         int ret;
413
414         PMD_INIT_FUNC_TRACE();
415
416         if (net_fc) {
417                 fc_conf->pause_time = net_fc->pause_time;
418                 fc_conf->high_water = net_fc->high_water;
419                 fc_conf->low_water = net_fc->low_water;
420                 fc_conf->send_xon = net_fc->send_xon;
421                 fc_conf->mac_ctrl_frame_fwd = net_fc->mac_ctrl_frame_fwd;
422                 fc_conf->mode = net_fc->mode;
423                 fc_conf->autoneg = net_fc->autoneg;
424                 return 0;
425         }
426         ret = fman_if_get_fc_threshold(dpaa_intf->fif);
427         if (ret) {
428                 fc_conf->mode = RTE_FC_TX_PAUSE;
429                 fc_conf->pause_time = fman_if_get_fc_quanta(dpaa_intf->fif);
430         } else {
431                 fc_conf->mode = RTE_FC_NONE;
432         }
433
434         return 0;
435 }
436
437 static int
438 dpaa_dev_add_mac_addr(struct rte_eth_dev *dev,
439                              struct ether_addr *addr,
440                              uint32_t index,
441                              __rte_unused uint32_t pool)
442 {
443         int ret;
444         struct dpaa_if *dpaa_intf = dev->data->dev_private;
445
446         PMD_INIT_FUNC_TRACE();
447
448         ret = fman_if_add_mac_addr(dpaa_intf->fif, addr->addr_bytes, index);
449
450         if (ret)
451                 RTE_LOG(ERR, PMD, "error: Adding the MAC ADDR failed:"
452                         " err = %d", ret);
453         return 0;
454 }
455
456 static void
457 dpaa_dev_remove_mac_addr(struct rte_eth_dev *dev,
458                           uint32_t index)
459 {
460         struct dpaa_if *dpaa_intf = dev->data->dev_private;
461
462         PMD_INIT_FUNC_TRACE();
463
464         fman_if_clear_mac_addr(dpaa_intf->fif, index);
465 }
466
467 static void
468 dpaa_dev_set_mac_addr(struct rte_eth_dev *dev,
469                        struct ether_addr *addr)
470 {
471         int ret;
472         struct dpaa_if *dpaa_intf = dev->data->dev_private;
473
474         PMD_INIT_FUNC_TRACE();
475
476         ret = fman_if_add_mac_addr(dpaa_intf->fif, addr->addr_bytes, 0);
477         if (ret)
478                 RTE_LOG(ERR, PMD, "error: Setting the MAC ADDR failed %d", ret);
479 }
480
481 static struct eth_dev_ops dpaa_devops = {
482         .dev_configure            = dpaa_eth_dev_configure,
483         .dev_start                = dpaa_eth_dev_start,
484         .dev_stop                 = dpaa_eth_dev_stop,
485         .dev_close                = dpaa_eth_dev_close,
486         .dev_infos_get            = dpaa_eth_dev_info,
487         .dev_supported_ptypes_get = dpaa_supported_ptypes_get,
488
489         .rx_queue_setup           = dpaa_eth_rx_queue_setup,
490         .tx_queue_setup           = dpaa_eth_tx_queue_setup,
491         .rx_queue_release         = dpaa_eth_rx_queue_release,
492         .tx_queue_release         = dpaa_eth_tx_queue_release,
493
494         .flow_ctrl_get            = dpaa_flow_ctrl_get,
495         .flow_ctrl_set            = dpaa_flow_ctrl_set,
496
497         .link_update              = dpaa_eth_link_update,
498         .stats_get                = dpaa_eth_stats_get,
499         .stats_reset              = dpaa_eth_stats_reset,
500         .promiscuous_enable       = dpaa_eth_promiscuous_enable,
501         .promiscuous_disable      = dpaa_eth_promiscuous_disable,
502         .allmulticast_enable      = dpaa_eth_multicast_enable,
503         .allmulticast_disable     = dpaa_eth_multicast_disable,
504         .mtu_set                  = dpaa_mtu_set,
505         .dev_set_link_down        = dpaa_link_down,
506         .dev_set_link_up          = dpaa_link_up,
507         .mac_addr_add             = dpaa_dev_add_mac_addr,
508         .mac_addr_remove          = dpaa_dev_remove_mac_addr,
509         .mac_addr_set             = dpaa_dev_set_mac_addr,
510
511 };
512
513 static int dpaa_fc_set_default(struct dpaa_if *dpaa_intf)
514 {
515         struct rte_eth_fc_conf *fc_conf;
516         int ret;
517
518         PMD_INIT_FUNC_TRACE();
519
520         if (!(dpaa_intf->fc_conf)) {
521                 dpaa_intf->fc_conf = rte_zmalloc(NULL,
522                         sizeof(struct rte_eth_fc_conf), MAX_CACHELINE);
523                 if (!dpaa_intf->fc_conf) {
524                         DPAA_PMD_ERR("unable to save flow control info");
525                         return -ENOMEM;
526                 }
527         }
528         fc_conf = dpaa_intf->fc_conf;
529         ret = fman_if_get_fc_threshold(dpaa_intf->fif);
530         if (ret) {
531                 fc_conf->mode = RTE_FC_TX_PAUSE;
532                 fc_conf->pause_time = fman_if_get_fc_quanta(dpaa_intf->fif);
533         } else {
534                 fc_conf->mode = RTE_FC_NONE;
535         }
536
537         return 0;
538 }
539
540 /* Initialise an Rx FQ */
541 static int dpaa_rx_queue_init(struct qman_fq *fq,
542                               uint32_t fqid)
543 {
544         struct qm_mcc_initfq opts;
545         int ret;
546
547         PMD_INIT_FUNC_TRACE();
548
549         ret = qman_reserve_fqid(fqid);
550         if (ret) {
551                 DPAA_PMD_ERR("reserve rx fqid %d failed with ret: %d",
552                              fqid, ret);
553                 return -EINVAL;
554         }
555
556         DPAA_PMD_DEBUG("creating rx fq %p, fqid %d", fq, fqid);
557         ret = qman_create_fq(fqid, QMAN_FQ_FLAG_NO_ENQUEUE, fq);
558         if (ret) {
559                 DPAA_PMD_ERR("create rx fqid %d failed with ret: %d",
560                         fqid, ret);
561                 return ret;
562         }
563
564         opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
565                        QM_INITFQ_WE_CONTEXTA;
566
567         opts.fqd.dest.wq = DPAA_IF_RX_PRIORITY;
568         opts.fqd.fq_ctrl = QM_FQCTRL_AVOIDBLOCK | QM_FQCTRL_CTXASTASHING |
569                            QM_FQCTRL_PREFERINCACHE;
570         opts.fqd.context_a.stashing.exclusive = 0;
571         opts.fqd.context_a.stashing.annotation_cl = DPAA_IF_RX_ANNOTATION_STASH;
572         opts.fqd.context_a.stashing.data_cl = DPAA_IF_RX_DATA_STASH;
573         opts.fqd.context_a.stashing.context_cl = DPAA_IF_RX_CONTEXT_STASH;
574
575         /*Enable tail drop */
576         opts.we_mask = opts.we_mask | QM_INITFQ_WE_TDTHRESH;
577         opts.fqd.fq_ctrl = opts.fqd.fq_ctrl | QM_FQCTRL_TDE;
578         qm_fqd_taildrop_set(&opts.fqd.td, CONG_THRESHOLD_RX_Q, 1);
579
580         ret = qman_init_fq(fq, 0, &opts);
581         if (ret)
582                 DPAA_PMD_ERR("init rx fqid %d failed with ret: %d", fqid, ret);
583         return ret;
584 }
585
586 /* Initialise a Tx FQ */
587 static int dpaa_tx_queue_init(struct qman_fq *fq,
588                               struct fman_if *fman_intf)
589 {
590         struct qm_mcc_initfq opts;
591         int ret;
592
593         PMD_INIT_FUNC_TRACE();
594
595         ret = qman_create_fq(0, QMAN_FQ_FLAG_DYNAMIC_FQID |
596                              QMAN_FQ_FLAG_TO_DCPORTAL, fq);
597         if (ret) {
598                 DPAA_PMD_ERR("create tx fq failed with ret: %d", ret);
599                 return ret;
600         }
601         opts.we_mask = QM_INITFQ_WE_DESTWQ | QM_INITFQ_WE_FQCTRL |
602                        QM_INITFQ_WE_CONTEXTB | QM_INITFQ_WE_CONTEXTA;
603         opts.fqd.dest.channel = fman_intf->tx_channel_id;
604         opts.fqd.dest.wq = DPAA_IF_TX_PRIORITY;
605         opts.fqd.fq_ctrl = QM_FQCTRL_PREFERINCACHE;
606         opts.fqd.context_b = 0;
607         /* no tx-confirmation */
608         opts.fqd.context_a.hi = 0x80000000 | fman_dealloc_bufs_mask_hi;
609         opts.fqd.context_a.lo = 0 | fman_dealloc_bufs_mask_lo;
610         DPAA_PMD_DEBUG("init tx fq %p, fqid %d", fq, fq->fqid);
611         ret = qman_init_fq(fq, QMAN_INITFQ_FLAG_SCHED, &opts);
612         if (ret)
613                 DPAA_PMD_ERR("init tx fqid %d failed %d", fq->fqid, ret);
614         return ret;
615 }
616
617 /* Initialise a network interface */
618 static int
619 dpaa_dev_init(struct rte_eth_dev *eth_dev)
620 {
621         int num_cores, num_rx_fqs, fqid;
622         int loop, ret = 0;
623         int dev_id;
624         struct rte_dpaa_device *dpaa_device;
625         struct dpaa_if *dpaa_intf;
626         struct fm_eth_port_cfg *cfg;
627         struct fman_if *fman_intf;
628         struct fman_if_bpool *bp, *tmp_bp;
629
630         PMD_INIT_FUNC_TRACE();
631
632         /* For secondary processes, the primary has done all the work */
633         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
634                 return 0;
635
636         dpaa_device = DEV_TO_DPAA_DEVICE(eth_dev->device);
637         dev_id = dpaa_device->id.dev_id;
638         dpaa_intf = eth_dev->data->dev_private;
639         cfg = &dpaa_netcfg->port_cfg[dev_id];
640         fman_intf = cfg->fman_if;
641
642         dpaa_intf->name = dpaa_device->name;
643
644         /* save fman_if & cfg in the interface struture */
645         dpaa_intf->fif = fman_intf;
646         dpaa_intf->ifid = dev_id;
647         dpaa_intf->cfg = cfg;
648
649         /* Initialize Rx FQ's */
650         if (getenv("DPAA_NUM_RX_QUEUES"))
651                 num_rx_fqs = atoi(getenv("DPAA_NUM_RX_QUEUES"));
652         else
653                 num_rx_fqs = DPAA_DEFAULT_NUM_PCD_QUEUES;
654
655         /* Each device can not have more than DPAA_PCD_FQID_MULTIPLIER RX
656          * queues.
657          */
658         if (num_rx_fqs <= 0 || num_rx_fqs > DPAA_PCD_FQID_MULTIPLIER) {
659                 DPAA_PMD_ERR("Invalid number of RX queues\n");
660                 return -EINVAL;
661         }
662
663         dpaa_intf->rx_queues = rte_zmalloc(NULL,
664                 sizeof(struct qman_fq) * num_rx_fqs, MAX_CACHELINE);
665         for (loop = 0; loop < num_rx_fqs; loop++) {
666                 fqid = DPAA_PCD_FQID_START + dpaa_intf->ifid *
667                         DPAA_PCD_FQID_MULTIPLIER + loop;
668                 ret = dpaa_rx_queue_init(&dpaa_intf->rx_queues[loop], fqid);
669                 if (ret)
670                         return ret;
671                 dpaa_intf->rx_queues[loop].dpaa_intf = dpaa_intf;
672         }
673         dpaa_intf->nb_rx_queues = num_rx_fqs;
674
675         /* Initialise Tx FQs. Have as many Tx FQ's as number of cores */
676         num_cores = rte_lcore_count();
677         dpaa_intf->tx_queues = rte_zmalloc(NULL, sizeof(struct qman_fq) *
678                 num_cores, MAX_CACHELINE);
679         if (!dpaa_intf->tx_queues)
680                 return -ENOMEM;
681
682         for (loop = 0; loop < num_cores; loop++) {
683                 ret = dpaa_tx_queue_init(&dpaa_intf->tx_queues[loop],
684                                          fman_intf);
685                 if (ret)
686                         return ret;
687                 dpaa_intf->tx_queues[loop].dpaa_intf = dpaa_intf;
688         }
689         dpaa_intf->nb_tx_queues = num_cores;
690
691         DPAA_PMD_DEBUG("All frame queues created");
692
693         /* Get the initial configuration for flow control */
694         dpaa_fc_set_default(dpaa_intf);
695
696         /* reset bpool list, initialize bpool dynamically */
697         list_for_each_entry_safe(bp, tmp_bp, &cfg->fman_if->bpool_list, node) {
698                 list_del(&bp->node);
699                 rte_free(bp);
700         }
701
702         /* Populate ethdev structure */
703         eth_dev->dev_ops = &dpaa_devops;
704         eth_dev->rx_pkt_burst = dpaa_eth_queue_rx;
705         eth_dev->tx_pkt_burst = dpaa_eth_tx_drop_all;
706
707         /* Allocate memory for storing MAC addresses */
708         eth_dev->data->mac_addrs = rte_zmalloc("mac_addr",
709                 ETHER_ADDR_LEN * DPAA_MAX_MAC_FILTER, 0);
710         if (eth_dev->data->mac_addrs == NULL) {
711                 DPAA_PMD_ERR("Failed to allocate %d bytes needed to "
712                                                 "store MAC addresses",
713                                 ETHER_ADDR_LEN * DPAA_MAX_MAC_FILTER);
714                 rte_free(dpaa_intf->rx_queues);
715                 rte_free(dpaa_intf->tx_queues);
716                 dpaa_intf->rx_queues = NULL;
717                 dpaa_intf->tx_queues = NULL;
718                 dpaa_intf->nb_rx_queues = 0;
719                 dpaa_intf->nb_tx_queues = 0;
720                 return -ENOMEM;
721         }
722
723         /* copy the primary mac address */
724         ether_addr_copy(&fman_intf->mac_addr, &eth_dev->data->mac_addrs[0]);
725
726         RTE_LOG(INFO, PMD, "net: dpaa: %s: %02x:%02x:%02x:%02x:%02x:%02x\n",
727                 dpaa_device->name,
728                 fman_intf->mac_addr.addr_bytes[0],
729                 fman_intf->mac_addr.addr_bytes[1],
730                 fman_intf->mac_addr.addr_bytes[2],
731                 fman_intf->mac_addr.addr_bytes[3],
732                 fman_intf->mac_addr.addr_bytes[4],
733                 fman_intf->mac_addr.addr_bytes[5]);
734
735         /* Disable RX mode */
736         fman_if_discard_rx_errors(fman_intf);
737         fman_if_disable_rx(fman_intf);
738         /* Disable promiscuous mode */
739         fman_if_promiscuous_disable(fman_intf);
740         /* Disable multicast */
741         fman_if_reset_mcast_filter_table(fman_intf);
742         /* Reset interface statistics */
743         fman_if_stats_reset(fman_intf);
744
745         return 0;
746 }
747
748 static int
749 dpaa_dev_uninit(struct rte_eth_dev *dev)
750 {
751         struct dpaa_if *dpaa_intf = dev->data->dev_private;
752
753         PMD_INIT_FUNC_TRACE();
754
755         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
756                 return -EPERM;
757
758         if (!dpaa_intf) {
759                 DPAA_PMD_WARN("Already closed or not started");
760                 return -1;
761         }
762
763         dpaa_eth_dev_close(dev);
764
765         /* release configuration memory */
766         if (dpaa_intf->fc_conf)
767                 rte_free(dpaa_intf->fc_conf);
768
769         rte_free(dpaa_intf->rx_queues);
770         dpaa_intf->rx_queues = NULL;
771
772         rte_free(dpaa_intf->tx_queues);
773         dpaa_intf->tx_queues = NULL;
774
775         /* free memory for storing MAC addresses */
776         rte_free(dev->data->mac_addrs);
777         dev->data->mac_addrs = NULL;
778
779         dev->dev_ops = NULL;
780         dev->rx_pkt_burst = NULL;
781         dev->tx_pkt_burst = NULL;
782
783         return 0;
784 }
785
786 static int
787 rte_dpaa_probe(struct rte_dpaa_driver *dpaa_drv,
788                struct rte_dpaa_device *dpaa_dev)
789 {
790         int diag;
791         int ret;
792         struct rte_eth_dev *eth_dev;
793
794         PMD_INIT_FUNC_TRACE();
795
796         /* In case of secondary process, the device is already configured
797          * and no further action is required, except portal initialization
798          * and verifying secondary attachment to port name.
799          */
800         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
801                 eth_dev = rte_eth_dev_attach_secondary(dpaa_dev->name);
802                 if (!eth_dev)
803                         return -ENOMEM;
804                 return 0;
805         }
806
807         if (!is_global_init) {
808                 /* One time load of Qman/Bman drivers */
809                 ret = qman_global_init();
810                 if (ret) {
811                         DPAA_PMD_ERR("QMAN initialization failed: %d",
812                                      ret);
813                         return ret;
814                 }
815                 ret = bman_global_init();
816                 if (ret) {
817                         DPAA_PMD_ERR("BMAN initialization failed: %d",
818                                      ret);
819                         return ret;
820                 }
821
822                 is_global_init = 1;
823         }
824
825         ret = rte_dpaa_portal_init((void *)1);
826         if (ret) {
827                 DPAA_PMD_ERR("Unable to initialize portal");
828                 return ret;
829         }
830
831         eth_dev = rte_eth_dev_allocate(dpaa_dev->name);
832         if (eth_dev == NULL)
833                 return -ENOMEM;
834
835         eth_dev->data->dev_private = rte_zmalloc(
836                                         "ethdev private structure",
837                                         sizeof(struct dpaa_if),
838                                         RTE_CACHE_LINE_SIZE);
839         if (!eth_dev->data->dev_private) {
840                 DPAA_PMD_ERR("Cannot allocate memzone for port data");
841                 rte_eth_dev_release_port(eth_dev);
842                 return -ENOMEM;
843         }
844
845         eth_dev->device = &dpaa_dev->device;
846         eth_dev->device->driver = &dpaa_drv->driver;
847         dpaa_dev->eth_dev = eth_dev;
848
849         /* Invoke PMD device initialization function */
850         diag = dpaa_dev_init(eth_dev);
851         if (diag == 0)
852                 return 0;
853
854         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
855                 rte_free(eth_dev->data->dev_private);
856
857         rte_eth_dev_release_port(eth_dev);
858         return diag;
859 }
860
861 static int
862 rte_dpaa_remove(struct rte_dpaa_device *dpaa_dev)
863 {
864         struct rte_eth_dev *eth_dev;
865
866         PMD_INIT_FUNC_TRACE();
867
868         eth_dev = dpaa_dev->eth_dev;
869         dpaa_dev_uninit(eth_dev);
870
871         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
872                 rte_free(eth_dev->data->dev_private);
873
874         rte_eth_dev_release_port(eth_dev);
875
876         return 0;
877 }
878
879 static struct rte_dpaa_driver rte_dpaa_pmd = {
880         .drv_type = FSL_DPAA_ETH,
881         .probe = rte_dpaa_probe,
882         .remove = rte_dpaa_remove,
883 };
884
885 RTE_PMD_REGISTER_DPAA(net_dpaa, rte_dpaa_pmd);