New upstream version 18.02
[deb_dpdk.git] / drivers / bus / dpaa / base / fman / netcfg_layer.c
1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
2  *
3  * Copyright 2010-2016 Freescale Semiconductor Inc.
4  * Copyright 2017 NXP
5  *
6  */
7 #include <inttypes.h>
8 #include <of.h>
9 #include <net/if.h>
10 #include <sys/ioctl.h>
11 #include <error.h>
12 #include <net/if_arp.h>
13 #include <assert.h>
14 #include <unistd.h>
15
16 #include <rte_malloc.h>
17
18 #include <rte_dpaa_logs.h>
19 #include <netcfg.h>
20
21 /* Structure contains information about all the interfaces given by user
22  * on command line.
23  */
24 struct netcfg_interface *netcfg_interface;
25
26 /* This data structure contaings all configurations information
27  * related to usages of DPA devices.
28  */
29 struct netcfg_info *netcfg;
30 /* fd to open a socket for making ioctl request to disable/enable shared
31  *  interfaces.
32  */
33 static int skfd = -1;
34
35 #ifdef RTE_LIBRTE_DPAA_DEBUG_DRIVER
36 void
37 dump_netcfg(struct netcfg_info *cfg_ptr)
38 {
39         int i;
40
41         printf("..........  DPAA Configuration  ..........\n\n");
42
43         /* Network interfaces */
44         printf("Network interfaces: %d\n", cfg_ptr->num_ethports);
45         for (i = 0; i < cfg_ptr->num_ethports; i++) {
46                 struct fman_if_bpool *bpool;
47                 struct fm_eth_port_cfg *p_cfg = &cfg_ptr->port_cfg[i];
48                 struct fman_if *__if = p_cfg->fman_if;
49
50                 printf("\n+ Fman %d, MAC %d (%s);\n",
51                        __if->fman_idx, __if->mac_idx,
52                        (__if->mac_type == fman_mac_1g) ? "1G" : "10G");
53
54                 printf("\tmac_addr: %02x:%02x:%02x:%02x:%02x:%02x\n",
55                        (&__if->mac_addr)->addr_bytes[0],
56                        (&__if->mac_addr)->addr_bytes[1],
57                        (&__if->mac_addr)->addr_bytes[2],
58                        (&__if->mac_addr)->addr_bytes[3],
59                        (&__if->mac_addr)->addr_bytes[4],
60                        (&__if->mac_addr)->addr_bytes[5]);
61
62                 printf("\ttx_channel_id: 0x%02x\n",
63                        __if->tx_channel_id);
64
65                 printf("\tfqid_rx_def: 0x%x\n", p_cfg->rx_def);
66                 printf("\tfqid_rx_err: 0x%x\n", __if->fqid_rx_err);
67
68                 printf("\tfqid_tx_err: 0x%x\n", __if->fqid_tx_err);
69                 printf("\tfqid_tx_confirm: 0x%x\n", __if->fqid_tx_confirm);
70                 fman_if_for_each_bpool(bpool, __if)
71                         printf("\tbuffer pool: (bpid=%d, count=%"PRId64
72                                " size=%"PRId64", addr=0x%"PRIx64")\n",
73                                bpool->bpid, bpool->count, bpool->size,
74                                bpool->addr);
75         }
76 }
77 #endif /* RTE_LIBRTE_DPAA_DEBUG_DRIVER */
78
79 struct netcfg_info *
80 netcfg_acquire(void)
81 {
82         struct fman_if *__if;
83         int _errno, idx = 0;
84         uint8_t num_ports = 0;
85         uint8_t num_cfg_ports = 0;
86         size_t size;
87
88         /* Extract dpa configuration from fman driver and FMC configuration
89          * for command-line interfaces.
90          */
91
92         /* Open a basic socket to enable/disable shared
93          * interfaces.
94          */
95         skfd = socket(AF_PACKET, SOCK_RAW, 0);
96         if (unlikely(skfd < 0)) {
97                 error(0, errno, "%s(): open(SOCK_RAW)", __func__);
98                 return NULL;
99         }
100
101         /* Initialise the Fman driver */
102         _errno = fman_init();
103         if (_errno) {
104                 DPAA_BUS_LOG(ERR, "FMAN driver init failed (%d)", errno);
105                 close(skfd);
106                 skfd = -1;
107                 return NULL;
108         }
109
110         /* Number of MAC ports */
111         list_for_each_entry(__if, fman_if_list, node)
112                 num_ports++;
113
114         if (!num_ports) {
115                 DPAA_BUS_LOG(ERR, "FMAN ports not available");
116                 return NULL;
117         }
118         /* Allocate space for all enabled mac ports */
119         size = sizeof(*netcfg) +
120                 (num_ports * sizeof(struct fm_eth_port_cfg));
121
122         netcfg = calloc(1, size);
123         if (unlikely(netcfg == NULL)) {
124                 DPAA_BUS_LOG(ERR, "Unable to allocat mem for netcfg");
125                 goto error;
126         }
127
128         netcfg->num_ethports = num_ports;
129
130         list_for_each_entry(__if, fman_if_list, node) {
131                 struct fm_eth_port_cfg *cfg = &netcfg->port_cfg[idx];
132                 /* Hook in the fman driver interface */
133                 cfg->fman_if = __if;
134                 cfg->rx_def = __if->fqid_rx_def;
135                 num_cfg_ports++;
136                 idx++;
137         }
138
139         if (!num_cfg_ports) {
140                 DPAA_BUS_LOG(ERR, "No FMAN ports found");
141                 goto error;
142         } else if (num_ports != num_cfg_ports)
143                 netcfg->num_ethports = num_cfg_ports;
144
145         return netcfg;
146
147 error:
148         if (netcfg) {
149                 free(netcfg);
150                 netcfg = NULL;
151         }
152
153         return NULL;
154 }
155
156 void
157 netcfg_release(struct netcfg_info *cfg_ptr)
158 {
159         free(cfg_ptr);
160         /* Close socket for shared interfaces */
161         if (skfd >= 0) {
162                 close(skfd);
163                 skfd = -1;
164         }
165 }