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