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