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