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