1133c2c65SJasvinder Singh /* SPDX-License-Identifier: BSD-3-Clause
2133c2c65SJasvinder Singh * Copyright(c) 2010-2018 Intel Corporation
3133c2c65SJasvinder Singh */
4133c2c65SJasvinder Singh
5133c2c65SJasvinder Singh #include <stdlib.h>
6133c2c65SJasvinder Singh #include <string.h>
7133c2c65SJasvinder Singh
8133c2c65SJasvinder Singh #include <rte_ethdev.h>
97959831bSJasvinder Singh #include <rte_string_fns.h>
10133c2c65SJasvinder Singh
11133c2c65SJasvinder Singh #include "link.h"
12133c2c65SJasvinder Singh #include "mempool.h"
13133c2c65SJasvinder Singh
14133c2c65SJasvinder Singh static struct link_list link_list;
15133c2c65SJasvinder Singh
16133c2c65SJasvinder Singh int
link_init(void)17133c2c65SJasvinder Singh link_init(void)
18133c2c65SJasvinder Singh {
19133c2c65SJasvinder Singh TAILQ_INIT(&link_list);
20133c2c65SJasvinder Singh
21133c2c65SJasvinder Singh return 0;
22133c2c65SJasvinder Singh }
23133c2c65SJasvinder Singh
24133c2c65SJasvinder Singh struct link *
link_find(const char * name)25133c2c65SJasvinder Singh link_find(const char *name)
26133c2c65SJasvinder Singh {
27133c2c65SJasvinder Singh struct link *link;
28133c2c65SJasvinder Singh
29133c2c65SJasvinder Singh if (name == NULL)
30133c2c65SJasvinder Singh return NULL;
31133c2c65SJasvinder Singh
32133c2c65SJasvinder Singh TAILQ_FOREACH(link, &link_list, node)
33133c2c65SJasvinder Singh if (strcmp(link->name, name) == 0)
34133c2c65SJasvinder Singh return link;
35133c2c65SJasvinder Singh
36133c2c65SJasvinder Singh return NULL;
37133c2c65SJasvinder Singh }
38133c2c65SJasvinder Singh
39ecfc2b1cSKevin Laatz struct link *
link_next(struct link * link)40ecfc2b1cSKevin Laatz link_next(struct link *link)
41ecfc2b1cSKevin Laatz {
42ecfc2b1cSKevin Laatz return (link == NULL) ? TAILQ_FIRST(&link_list) : TAILQ_NEXT(link, node);
43ecfc2b1cSKevin Laatz }
44ecfc2b1cSKevin Laatz
45133c2c65SJasvinder Singh static struct rte_eth_conf port_conf_default = {
46133c2c65SJasvinder Singh .link_speeds = 0,
47133c2c65SJasvinder Singh .rxmode = {
48*295968d1SFerruh Yigit .mq_mode = RTE_ETH_MQ_RX_NONE,
491bb4a528SFerruh Yigit .mtu = 9000 - (RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN), /* Jumbo frame MTU */
50133c2c65SJasvinder Singh },
51133c2c65SJasvinder Singh .rx_adv_conf = {
52133c2c65SJasvinder Singh .rss_conf = {
53133c2c65SJasvinder Singh .rss_key = NULL,
54133c2c65SJasvinder Singh .rss_key_len = 40,
55133c2c65SJasvinder Singh .rss_hf = 0,
56133c2c65SJasvinder Singh },
57133c2c65SJasvinder Singh },
58133c2c65SJasvinder Singh .txmode = {
59*295968d1SFerruh Yigit .mq_mode = RTE_ETH_MQ_TX_NONE,
60133c2c65SJasvinder Singh },
61133c2c65SJasvinder Singh .lpbk_mode = 0,
62133c2c65SJasvinder Singh };
63133c2c65SJasvinder Singh
64*295968d1SFerruh Yigit #define RETA_CONF_SIZE (RTE_ETH_RSS_RETA_SIZE_512 / RTE_ETH_RETA_GROUP_SIZE)
65133c2c65SJasvinder Singh
66133c2c65SJasvinder Singh static int
rss_setup(uint16_t port_id,uint16_t reta_size,struct link_params_rss * rss)67133c2c65SJasvinder Singh rss_setup(uint16_t port_id,
68133c2c65SJasvinder Singh uint16_t reta_size,
69133c2c65SJasvinder Singh struct link_params_rss *rss)
70133c2c65SJasvinder Singh {
71133c2c65SJasvinder Singh struct rte_eth_rss_reta_entry64 reta_conf[RETA_CONF_SIZE];
72133c2c65SJasvinder Singh uint32_t i;
73133c2c65SJasvinder Singh int status;
74133c2c65SJasvinder Singh
75133c2c65SJasvinder Singh /* RETA setting */
76133c2c65SJasvinder Singh memset(reta_conf, 0, sizeof(reta_conf));
77133c2c65SJasvinder Singh
78133c2c65SJasvinder Singh for (i = 0; i < reta_size; i++)
79*295968d1SFerruh Yigit reta_conf[i / RTE_ETH_RETA_GROUP_SIZE].mask = UINT64_MAX;
80133c2c65SJasvinder Singh
81133c2c65SJasvinder Singh for (i = 0; i < reta_size; i++) {
82*295968d1SFerruh Yigit uint32_t reta_id = i / RTE_ETH_RETA_GROUP_SIZE;
83*295968d1SFerruh Yigit uint32_t reta_pos = i % RTE_ETH_RETA_GROUP_SIZE;
84133c2c65SJasvinder Singh uint32_t rss_qs_pos = i % rss->n_queues;
85133c2c65SJasvinder Singh
86133c2c65SJasvinder Singh reta_conf[reta_id].reta[reta_pos] =
87133c2c65SJasvinder Singh (uint16_t) rss->queue_id[rss_qs_pos];
88133c2c65SJasvinder Singh }
89133c2c65SJasvinder Singh
90133c2c65SJasvinder Singh /* RETA update */
91133c2c65SJasvinder Singh status = rte_eth_dev_rss_reta_update(port_id,
92133c2c65SJasvinder Singh reta_conf,
93133c2c65SJasvinder Singh reta_size);
94133c2c65SJasvinder Singh
95133c2c65SJasvinder Singh return status;
96133c2c65SJasvinder Singh }
97133c2c65SJasvinder Singh
98133c2c65SJasvinder Singh struct link *
link_create(const char * name,struct link_params * params)99133c2c65SJasvinder Singh link_create(const char *name, struct link_params *params)
100133c2c65SJasvinder Singh {
101133c2c65SJasvinder Singh struct rte_eth_dev_info port_info;
102133c2c65SJasvinder Singh struct rte_eth_conf port_conf;
103133c2c65SJasvinder Singh struct link *link;
104133c2c65SJasvinder Singh struct link_params_rss *rss;
105133c2c65SJasvinder Singh struct mempool *mempool;
106133c2c65SJasvinder Singh uint32_t cpu_id, i;
107133c2c65SJasvinder Singh int status;
108133c2c65SJasvinder Singh uint16_t port_id;
109133c2c65SJasvinder Singh
110133c2c65SJasvinder Singh /* Check input params */
111133c2c65SJasvinder Singh if ((name == NULL) ||
112133c2c65SJasvinder Singh link_find(name) ||
113133c2c65SJasvinder Singh (params == NULL) ||
114133c2c65SJasvinder Singh (params->rx.n_queues == 0) ||
115133c2c65SJasvinder Singh (params->rx.queue_size == 0) ||
116133c2c65SJasvinder Singh (params->tx.n_queues == 0) ||
117133c2c65SJasvinder Singh (params->tx.queue_size == 0))
118133c2c65SJasvinder Singh return NULL;
119133c2c65SJasvinder Singh
120133c2c65SJasvinder Singh port_id = params->port_id;
121133c2c65SJasvinder Singh if (params->dev_name) {
122133c2c65SJasvinder Singh status = rte_eth_dev_get_port_by_name(params->dev_name,
123133c2c65SJasvinder Singh &port_id);
124133c2c65SJasvinder Singh
125133c2c65SJasvinder Singh if (status)
126133c2c65SJasvinder Singh return NULL;
127133c2c65SJasvinder Singh } else
128133c2c65SJasvinder Singh if (!rte_eth_dev_is_valid_port(port_id))
129133c2c65SJasvinder Singh return NULL;
130133c2c65SJasvinder Singh
131089e5ed7SIvan Ilchenko if (rte_eth_dev_info_get(port_id, &port_info) != 0)
132089e5ed7SIvan Ilchenko return NULL;
133133c2c65SJasvinder Singh
134133c2c65SJasvinder Singh mempool = mempool_find(params->rx.mempool_name);
135133c2c65SJasvinder Singh if (mempool == NULL)
136133c2c65SJasvinder Singh return NULL;
137133c2c65SJasvinder Singh
138133c2c65SJasvinder Singh rss = params->rx.rss;
139133c2c65SJasvinder Singh if (rss) {
140133c2c65SJasvinder Singh if ((port_info.reta_size == 0) ||
141*295968d1SFerruh Yigit (port_info.reta_size > RTE_ETH_RSS_RETA_SIZE_512))
142133c2c65SJasvinder Singh return NULL;
143133c2c65SJasvinder Singh
144133c2c65SJasvinder Singh if ((rss->n_queues == 0) ||
145133c2c65SJasvinder Singh (rss->n_queues >= LINK_RXQ_RSS_MAX))
146133c2c65SJasvinder Singh return NULL;
147133c2c65SJasvinder Singh
148133c2c65SJasvinder Singh for (i = 0; i < rss->n_queues; i++)
149133c2c65SJasvinder Singh if (rss->queue_id[i] >= port_info.max_rx_queues)
150133c2c65SJasvinder Singh return NULL;
151133c2c65SJasvinder Singh }
152133c2c65SJasvinder Singh
153133c2c65SJasvinder Singh /**
154133c2c65SJasvinder Singh * Resource create
155133c2c65SJasvinder Singh */
156133c2c65SJasvinder Singh /* Port */
157133c2c65SJasvinder Singh memcpy(&port_conf, &port_conf_default, sizeof(port_conf));
158133c2c65SJasvinder Singh if (rss) {
159*295968d1SFerruh Yigit port_conf.rxmode.mq_mode = RTE_ETH_MQ_RX_RSS;
1602e1141b6SCristian Dumitrescu port_conf.rx_adv_conf.rss_conf.rss_hf =
161*295968d1SFerruh Yigit (RTE_ETH_RSS_IP | RTE_ETH_RSS_TCP | RTE_ETH_RSS_UDP) &
1622e1141b6SCristian Dumitrescu port_info.flow_type_rss_offloads;
163133c2c65SJasvinder Singh }
164133c2c65SJasvinder Singh
165133c2c65SJasvinder Singh cpu_id = (uint32_t) rte_eth_dev_socket_id(port_id);
166133c2c65SJasvinder Singh if (cpu_id == (uint32_t) SOCKET_ID_ANY)
167133c2c65SJasvinder Singh cpu_id = 0;
168133c2c65SJasvinder Singh
169133c2c65SJasvinder Singh status = rte_eth_dev_configure(
170133c2c65SJasvinder Singh port_id,
171133c2c65SJasvinder Singh params->rx.n_queues,
172133c2c65SJasvinder Singh params->tx.n_queues,
173133c2c65SJasvinder Singh &port_conf);
174133c2c65SJasvinder Singh
175133c2c65SJasvinder Singh if (status < 0)
176133c2c65SJasvinder Singh return NULL;
177133c2c65SJasvinder Singh
178f430bbceSIvan Ilchenko if (params->promiscuous) {
179f430bbceSIvan Ilchenko status = rte_eth_promiscuous_enable(port_id);
180f430bbceSIvan Ilchenko if (status != 0)
181f430bbceSIvan Ilchenko return NULL;
182f430bbceSIvan Ilchenko }
183133c2c65SJasvinder Singh
184133c2c65SJasvinder Singh /* Port RX */
185133c2c65SJasvinder Singh for (i = 0; i < params->rx.n_queues; i++) {
186133c2c65SJasvinder Singh status = rte_eth_rx_queue_setup(
187133c2c65SJasvinder Singh port_id,
188133c2c65SJasvinder Singh i,
189133c2c65SJasvinder Singh params->rx.queue_size,
190133c2c65SJasvinder Singh cpu_id,
191133c2c65SJasvinder Singh NULL,
192133c2c65SJasvinder Singh mempool->m);
193133c2c65SJasvinder Singh
194133c2c65SJasvinder Singh if (status < 0)
195133c2c65SJasvinder Singh return NULL;
196133c2c65SJasvinder Singh }
197133c2c65SJasvinder Singh
198133c2c65SJasvinder Singh /* Port TX */
199133c2c65SJasvinder Singh for (i = 0; i < params->tx.n_queues; i++) {
200133c2c65SJasvinder Singh status = rte_eth_tx_queue_setup(
201133c2c65SJasvinder Singh port_id,
202133c2c65SJasvinder Singh i,
203133c2c65SJasvinder Singh params->tx.queue_size,
204133c2c65SJasvinder Singh cpu_id,
205133c2c65SJasvinder Singh NULL);
206133c2c65SJasvinder Singh
207133c2c65SJasvinder Singh if (status < 0)
208133c2c65SJasvinder Singh return NULL;
209133c2c65SJasvinder Singh }
210133c2c65SJasvinder Singh
211133c2c65SJasvinder Singh /* Port start */
212133c2c65SJasvinder Singh status = rte_eth_dev_start(port_id);
213133c2c65SJasvinder Singh if (status < 0)
214133c2c65SJasvinder Singh return NULL;
215133c2c65SJasvinder Singh
216133c2c65SJasvinder Singh if (rss) {
217133c2c65SJasvinder Singh status = rss_setup(port_id, port_info.reta_size, rss);
218133c2c65SJasvinder Singh
219133c2c65SJasvinder Singh if (status) {
220133c2c65SJasvinder Singh rte_eth_dev_stop(port_id);
221133c2c65SJasvinder Singh return NULL;
222133c2c65SJasvinder Singh }
223133c2c65SJasvinder Singh }
224133c2c65SJasvinder Singh
225133c2c65SJasvinder Singh /* Port link up */
226133c2c65SJasvinder Singh status = rte_eth_dev_set_link_up(port_id);
227133c2c65SJasvinder Singh if ((status < 0) && (status != -ENOTSUP)) {
228133c2c65SJasvinder Singh rte_eth_dev_stop(port_id);
229133c2c65SJasvinder Singh return NULL;
230133c2c65SJasvinder Singh }
231133c2c65SJasvinder Singh
232133c2c65SJasvinder Singh /* Node allocation */
233133c2c65SJasvinder Singh link = calloc(1, sizeof(struct link));
234133c2c65SJasvinder Singh if (link == NULL) {
235133c2c65SJasvinder Singh rte_eth_dev_stop(port_id);
236133c2c65SJasvinder Singh return NULL;
237133c2c65SJasvinder Singh }
238133c2c65SJasvinder Singh
239133c2c65SJasvinder Singh /* Node fill in */
2407959831bSJasvinder Singh strlcpy(link->name, name, sizeof(link->name));
241133c2c65SJasvinder Singh link->port_id = port_id;
242133c2c65SJasvinder Singh link->n_rxq = params->rx.n_queues;
243133c2c65SJasvinder Singh link->n_txq = params->tx.n_queues;
244133c2c65SJasvinder Singh
245133c2c65SJasvinder Singh /* Node add to list */
246133c2c65SJasvinder Singh TAILQ_INSERT_TAIL(&link_list, link, node);
247133c2c65SJasvinder Singh
248133c2c65SJasvinder Singh return link;
249133c2c65SJasvinder Singh }
250133c2c65SJasvinder Singh
251133c2c65SJasvinder Singh int
link_is_up(const char * name)252133c2c65SJasvinder Singh link_is_up(const char *name)
253133c2c65SJasvinder Singh {
254133c2c65SJasvinder Singh struct rte_eth_link link_params;
255133c2c65SJasvinder Singh struct link *link;
256133c2c65SJasvinder Singh
257133c2c65SJasvinder Singh /* Check input params */
258133c2c65SJasvinder Singh if (name == NULL)
259133c2c65SJasvinder Singh return 0;
260133c2c65SJasvinder Singh
261133c2c65SJasvinder Singh link = link_find(name);
262133c2c65SJasvinder Singh if (link == NULL)
263133c2c65SJasvinder Singh return 0;
264133c2c65SJasvinder Singh
265133c2c65SJasvinder Singh /* Resource */
26622e5c73bSIgor Romanov if (rte_eth_link_get(link->port_id, &link_params) < 0)
26722e5c73bSIgor Romanov return 0;
268133c2c65SJasvinder Singh
269*295968d1SFerruh Yigit return (link_params.link_status == RTE_ETH_LINK_DOWN) ? 0 : 1;
270133c2c65SJasvinder Singh }
271