drivers: use SPDX tag in NXP dpaa files
[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 static inline int
80 get_num_netcfg_interfaces(char *str)
81 {
82         char *pch;
83         uint8_t count = 0;
84
85         if (str == NULL)
86                 return -EINVAL;
87         pch = strtok(str, ",");
88         while (pch != NULL) {
89                 count++;
90                 pch = strtok(NULL, ",");
91         }
92         return count;
93 }
94
95 struct netcfg_info *
96 netcfg_acquire(void)
97 {
98         struct fman_if *__if;
99         int _errno, idx = 0;
100         uint8_t num_ports = 0;
101         uint8_t num_cfg_ports = 0;
102         size_t size;
103
104         /* Extract dpa configuration from fman driver and FMC configuration
105          * for command-line interfaces.
106          */
107
108         /* Open a basic socket to enable/disable shared
109          * interfaces.
110          */
111         skfd = socket(AF_PACKET, SOCK_RAW, 0);
112         if (unlikely(skfd < 0)) {
113                 error(0, errno, "%s(): open(SOCK_RAW)", __func__);
114                 return NULL;
115         }
116
117         /* Initialise the Fman driver */
118         _errno = fman_init();
119         if (_errno) {
120                 DPAA_BUS_LOG(ERR, "FMAN driver init failed (%d)", errno);
121                 close(skfd);
122                 skfd = -1;
123                 return NULL;
124         }
125
126         /* Number of MAC ports */
127         list_for_each_entry(__if, fman_if_list, node)
128                 num_ports++;
129
130         if (!num_ports) {
131                 DPAA_BUS_LOG(ERR, "FMAN ports not available");
132                 return NULL;
133         }
134         /* Allocate space for all enabled mac ports */
135         size = sizeof(*netcfg) +
136                 (num_ports * sizeof(struct fm_eth_port_cfg));
137
138         netcfg = calloc(1, size);
139         if (unlikely(netcfg == NULL)) {
140                 DPAA_BUS_LOG(ERR, "Unable to allocat mem for netcfg");
141                 goto error;
142         }
143
144         netcfg->num_ethports = num_ports;
145
146         list_for_each_entry(__if, fman_if_list, node) {
147                 struct fm_eth_port_cfg *cfg = &netcfg->port_cfg[idx];
148                 /* Hook in the fman driver interface */
149                 cfg->fman_if = __if;
150                 cfg->rx_def = __if->fqid_rx_def;
151                 num_cfg_ports++;
152                 idx++;
153         }
154
155         if (!num_cfg_ports) {
156                 DPAA_BUS_LOG(ERR, "No FMAN ports found");
157                 goto error;
158         } else if (num_ports != num_cfg_ports)
159                 netcfg->num_ethports = num_cfg_ports;
160
161         return netcfg;
162
163 error:
164         if (netcfg) {
165                 free(netcfg);
166                 netcfg = NULL;
167         }
168
169         return NULL;
170 }
171
172 void
173 netcfg_release(struct netcfg_info *cfg_ptr)
174 {
175         free(cfg_ptr);
176         /* Close socket for shared interfaces */
177         if (skfd >= 0) {
178                 close(skfd);
179                 skfd = -1;
180         }
181 }