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