New upstream version 17.11-rc3
[deb_dpdk.git] / drivers / bus / dpaa / base / fman / netcfg_layer.c
1 /*-
2  * This file is provided under a dual BSD/GPLv2 license. When using or
3  * redistributing this file, you may do so under either license.
4  *
5  *   BSD LICENSE
6  *
7  * Copyright 2010-2016 Freescale Semiconductor Inc.
8  * Copyright 2017 NXP.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  * * Neither the name of the above-listed copyright holders nor the
18  * names of any contributors may be used to endorse or promote products
19  * derived from this software without specific prior written permission.
20  *
21  *   GPL LICENSE SUMMARY
22  *
23  * ALTERNATIVELY, this software may be distributed under the terms of the
24  * GNU General Public License ("GPL") as published by the Free Software
25  * Foundation, either version 2 of that License or (at your option) any
26  * later version.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
32  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40 #include <inttypes.h>
41 #include <of.h>
42 #include <net/if.h>
43 #include <sys/ioctl.h>
44 #include <error.h>
45 #include <net/if_arp.h>
46 #include <assert.h>
47 #include <unistd.h>
48
49 #include <rte_malloc.h>
50
51 #include <rte_dpaa_logs.h>
52 #include <netcfg.h>
53
54 /* Structure contains information about all the interfaces given by user
55  * on command line.
56  */
57 struct netcfg_interface *netcfg_interface;
58
59 /* This data structure contaings all configurations information
60  * related to usages of DPA devices.
61  */
62 struct netcfg_info *netcfg;
63 /* fd to open a socket for making ioctl request to disable/enable shared
64  *  interfaces.
65  */
66 static int skfd = -1;
67
68 #ifdef RTE_LIBRTE_DPAA_DEBUG_DRIVER
69 void
70 dump_netcfg(struct netcfg_info *cfg_ptr)
71 {
72         int i;
73
74         printf("..........  DPAA Configuration  ..........\n\n");
75
76         /* Network interfaces */
77         printf("Network interfaces: %d\n", cfg_ptr->num_ethports);
78         for (i = 0; i < cfg_ptr->num_ethports; i++) {
79                 struct fman_if_bpool *bpool;
80                 struct fm_eth_port_cfg *p_cfg = &cfg_ptr->port_cfg[i];
81                 struct fman_if *__if = p_cfg->fman_if;
82
83                 printf("\n+ Fman %d, MAC %d (%s);\n",
84                        __if->fman_idx, __if->mac_idx,
85                        (__if->mac_type == fman_mac_1g) ? "1G" : "10G");
86
87                 printf("\tmac_addr: %02x:%02x:%02x:%02x:%02x:%02x\n",
88                        (&__if->mac_addr)->addr_bytes[0],
89                        (&__if->mac_addr)->addr_bytes[1],
90                        (&__if->mac_addr)->addr_bytes[2],
91                        (&__if->mac_addr)->addr_bytes[3],
92                        (&__if->mac_addr)->addr_bytes[4],
93                        (&__if->mac_addr)->addr_bytes[5]);
94
95                 printf("\ttx_channel_id: 0x%02x\n",
96                        __if->tx_channel_id);
97
98                 printf("\tfqid_rx_def: 0x%x\n", p_cfg->rx_def);
99                 printf("\tfqid_rx_err: 0x%x\n", __if->fqid_rx_err);
100
101                 printf("\tfqid_tx_err: 0x%x\n", __if->fqid_tx_err);
102                 printf("\tfqid_tx_confirm: 0x%x\n", __if->fqid_tx_confirm);
103                 fman_if_for_each_bpool(bpool, __if)
104                         printf("\tbuffer pool: (bpid=%d, count=%"PRId64
105                                " size=%"PRId64", addr=0x%"PRIx64")\n",
106                                bpool->bpid, bpool->count, bpool->size,
107                                bpool->addr);
108         }
109 }
110 #endif /* RTE_LIBRTE_DPAA_DEBUG_DRIVER */
111
112 static inline int
113 get_num_netcfg_interfaces(char *str)
114 {
115         char *pch;
116         uint8_t count = 0;
117
118         if (str == NULL)
119                 return -EINVAL;
120         pch = strtok(str, ",");
121         while (pch != NULL) {
122                 count++;
123                 pch = strtok(NULL, ",");
124         }
125         return count;
126 }
127
128 struct netcfg_info *
129 netcfg_acquire(void)
130 {
131         struct fman_if *__if;
132         int _errno, idx = 0;
133         uint8_t num_ports = 0;
134         uint8_t num_cfg_ports = 0;
135         size_t size;
136
137         /* Extract dpa configuration from fman driver and FMC configuration
138          * for command-line interfaces.
139          */
140
141         /* Open a basic socket to enable/disable shared
142          * interfaces.
143          */
144         skfd = socket(AF_PACKET, SOCK_RAW, 0);
145         if (unlikely(skfd < 0)) {
146                 error(0, errno, "%s(): open(SOCK_RAW)", __func__);
147                 return NULL;
148         }
149
150         /* Initialise the Fman driver */
151         _errno = fman_init();
152         if (_errno) {
153                 DPAA_BUS_LOG(ERR, "FMAN driver init failed (%d)", errno);
154                 close(skfd);
155                 skfd = -1;
156                 return NULL;
157         }
158
159         /* Number of MAC ports */
160         list_for_each_entry(__if, fman_if_list, node)
161                 num_ports++;
162
163         if (!num_ports) {
164                 DPAA_BUS_LOG(ERR, "FMAN ports not available");
165                 return NULL;
166         }
167         /* Allocate space for all enabled mac ports */
168         size = sizeof(*netcfg) +
169                 (num_ports * sizeof(struct fm_eth_port_cfg));
170
171         netcfg = calloc(1, size);
172         if (unlikely(netcfg == NULL)) {
173                 DPAA_BUS_LOG(ERR, "Unable to allocat mem for netcfg");
174                 goto error;
175         }
176
177         netcfg->num_ethports = num_ports;
178
179         list_for_each_entry(__if, fman_if_list, node) {
180                 struct fm_eth_port_cfg *cfg = &netcfg->port_cfg[idx];
181                 /* Hook in the fman driver interface */
182                 cfg->fman_if = __if;
183                 cfg->rx_def = __if->fqid_rx_def;
184                 num_cfg_ports++;
185                 idx++;
186         }
187
188         if (!num_cfg_ports) {
189                 DPAA_BUS_LOG(ERR, "No FMAN ports found");
190                 goto error;
191         } else if (num_ports != num_cfg_ports)
192                 netcfg->num_ethports = num_cfg_ports;
193
194         return netcfg;
195
196 error:
197         if (netcfg) {
198                 free(netcfg);
199                 netcfg = NULL;
200         }
201
202         return NULL;
203 }
204
205 void
206 netcfg_release(struct netcfg_info *cfg_ptr)
207 {
208         free(cfg_ptr);
209         /* Close socket for shared interfaces */
210         if (skfd >= 0) {
211                 close(skfd);
212                 skfd = -1;
213         }
214 }