Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (c) 2022 Corigine, Inc.
3 : : * All rights reserved.
4 : : */
5 : :
6 : : #include "nfp_flower_representor.h"
7 : :
8 : : #include "../nfd3/nfp_nfd3.h"
9 : : #include "../nfpcore/nfp_nsp.h"
10 : : #include "../nfp_logs.h"
11 : : #include "../nfp_mtr.h"
12 : : #include "nfp_flower_service.h"
13 : :
14 : : /* Type of representor */
15 : : enum nfp_repr_type {
16 : : NFP_REPR_TYPE_PHYS_PORT, /*<< External NIC port */
17 : : NFP_REPR_TYPE_PF, /*<< Physical function */
18 : : NFP_REPR_TYPE_VF, /*<< Virtual function */
19 : : NFP_REPR_TYPE_MAX, /*<< Number of representor types */
20 : : };
21 : :
22 : : static int
23 : 0 : nfp_flower_repr_link_update(struct rte_eth_dev *dev,
24 : : __rte_unused int wait_to_complete)
25 : : {
26 : : int ret;
27 : : uint32_t nn_link_status;
28 : : struct nfp_net_hw *pf_hw;
29 : : struct rte_eth_link *link;
30 : : struct nfp_flower_representor *repr;
31 : :
32 : 0 : repr = dev->data->dev_private;
33 : 0 : link = &repr->link;
34 : :
35 : 0 : pf_hw = repr->app_fw_flower->pf_hw;
36 : 0 : nn_link_status = nn_cfg_readw(&pf_hw->super, NFP_NET_CFG_STS);
37 : :
38 : 0 : ret = nfp_net_link_update_common(dev, pf_hw, link, nn_link_status);
39 : :
40 : 0 : return ret;
41 : : }
42 : :
43 : : static int
44 : 0 : nfp_flower_repr_dev_infos_get(__rte_unused struct rte_eth_dev *dev,
45 : : struct rte_eth_dev_info *dev_info)
46 : : {
47 : : /* Hardcoded pktlen and queues for now */
48 : 0 : dev_info->max_rx_queues = 1;
49 : 0 : dev_info->max_tx_queues = 1;
50 : 0 : dev_info->min_rx_bufsize = RTE_ETHER_MIN_MTU;
51 : 0 : dev_info->max_rx_pktlen = 9000;
52 : :
53 : : dev_info->rx_offload_capa = RTE_ETH_RX_OFFLOAD_VLAN_STRIP;
54 : 0 : dev_info->rx_offload_capa |= RTE_ETH_RX_OFFLOAD_IPV4_CKSUM |
55 : : RTE_ETH_RX_OFFLOAD_UDP_CKSUM |
56 : : RTE_ETH_RX_OFFLOAD_TCP_CKSUM;
57 : :
58 : : dev_info->tx_offload_capa = RTE_ETH_TX_OFFLOAD_VLAN_INSERT;
59 : : dev_info->tx_offload_capa |= RTE_ETH_TX_OFFLOAD_IPV4_CKSUM |
60 : : RTE_ETH_TX_OFFLOAD_UDP_CKSUM |
61 : : RTE_ETH_TX_OFFLOAD_TCP_CKSUM;
62 : : dev_info->tx_offload_capa |= RTE_ETH_TX_OFFLOAD_TCP_TSO;
63 : 0 : dev_info->tx_offload_capa |= RTE_ETH_TX_OFFLOAD_MULTI_SEGS;
64 : :
65 : 0 : dev_info->max_mac_addrs = 1;
66 : :
67 : 0 : return 0;
68 : : }
69 : :
70 : : static int
71 : 0 : nfp_flower_repr_dev_start(struct rte_eth_dev *dev)
72 : : {
73 : : struct nfp_flower_representor *repr;
74 : : struct nfp_app_fw_flower *app_fw_flower;
75 : : uint16_t i;
76 : :
77 : 0 : repr = dev->data->dev_private;
78 : 0 : app_fw_flower = repr->app_fw_flower;
79 : :
80 [ # # ]: 0 : if (repr->repr_type == NFP_REPR_TYPE_PHYS_PORT) {
81 : 0 : nfp_eth_set_configured(app_fw_flower->pf_hw->pf_dev->cpp,
82 : : repr->nfp_idx, 1);
83 : : }
84 : :
85 : 0 : nfp_flower_cmsg_port_mod(app_fw_flower, repr->port_id, true);
86 : :
87 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++)
88 : 0 : dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
89 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++)
90 : 0 : dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
91 : :
92 : 0 : return 0;
93 : : }
94 : :
95 : : static int
96 : 0 : nfp_flower_repr_dev_stop(struct rte_eth_dev *dev)
97 : : {
98 : : struct nfp_flower_representor *repr;
99 : : struct nfp_app_fw_flower *app_fw_flower;
100 : : uint16_t i;
101 : :
102 : 0 : repr = dev->data->dev_private;
103 : 0 : app_fw_flower = repr->app_fw_flower;
104 : :
105 : 0 : nfp_flower_cmsg_port_mod(app_fw_flower, repr->port_id, false);
106 : :
107 [ # # ]: 0 : if (repr->repr_type == NFP_REPR_TYPE_PHYS_PORT) {
108 : 0 : nfp_eth_set_configured(app_fw_flower->pf_hw->pf_dev->cpp,
109 : : repr->nfp_idx, 0);
110 : : }
111 : :
112 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++)
113 : 0 : dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
114 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++)
115 : 0 : dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
116 : :
117 : 0 : return 0;
118 : : }
119 : :
120 : : static int
121 : 0 : nfp_flower_repr_rx_queue_setup(struct rte_eth_dev *dev,
122 : : uint16_t rx_queue_id,
123 : : __rte_unused uint16_t nb_rx_desc,
124 : : unsigned int socket_id,
125 : : __rte_unused const struct rte_eth_rxconf *rx_conf,
126 : : __rte_unused struct rte_mempool *mb_pool)
127 : : {
128 : : struct nfp_net_rxq *rxq;
129 : : struct nfp_net_hw *pf_hw;
130 : : struct nfp_flower_representor *repr;
131 : :
132 : 0 : repr = dev->data->dev_private;
133 : 0 : pf_hw = repr->app_fw_flower->pf_hw;
134 : :
135 : : /* Allocating rx queue data structure */
136 : 0 : rxq = rte_zmalloc_socket("ethdev RX queue", sizeof(struct nfp_net_rxq),
137 : : RTE_CACHE_LINE_SIZE, socket_id);
138 [ # # ]: 0 : if (rxq == NULL)
139 : : return -ENOMEM;
140 : :
141 : 0 : rxq->hw = pf_hw;
142 : 0 : rxq->qidx = rx_queue_id;
143 : 0 : rxq->port_id = dev->data->port_id;
144 : 0 : dev->data->rx_queues[rx_queue_id] = rxq;
145 : :
146 : 0 : return 0;
147 : : }
148 : :
149 : : static int
150 : 0 : nfp_flower_repr_tx_queue_setup(struct rte_eth_dev *dev,
151 : : uint16_t tx_queue_id,
152 : : __rte_unused uint16_t nb_tx_desc,
153 : : unsigned int socket_id,
154 : : __rte_unused const struct rte_eth_txconf *tx_conf)
155 : : {
156 : : struct nfp_net_txq *txq;
157 : : struct nfp_net_hw *pf_hw;
158 : : struct nfp_flower_representor *repr;
159 : :
160 : 0 : repr = dev->data->dev_private;
161 : 0 : pf_hw = repr->app_fw_flower->pf_hw;
162 : :
163 : : /* Allocating tx queue data structure */
164 : 0 : txq = rte_zmalloc_socket("ethdev TX queue", sizeof(struct nfp_net_txq),
165 : : RTE_CACHE_LINE_SIZE, socket_id);
166 [ # # ]: 0 : if (txq == NULL)
167 : : return -ENOMEM;
168 : :
169 : 0 : txq->hw = pf_hw;
170 : 0 : txq->qidx = tx_queue_id;
171 : 0 : txq->port_id = dev->data->port_id;
172 : 0 : dev->data->tx_queues[tx_queue_id] = txq;
173 : :
174 : 0 : return 0;
175 : : }
176 : :
177 : : static int
178 : 0 : nfp_flower_repr_stats_get(struct rte_eth_dev *ethdev,
179 : : struct rte_eth_stats *stats)
180 : : {
181 : : struct nfp_flower_representor *repr;
182 : :
183 : 0 : repr = ethdev->data->dev_private;
184 [ # # ]: 0 : rte_memcpy(stats, &repr->repr_stats, sizeof(struct rte_eth_stats));
185 : :
186 : 0 : return 0;
187 : : }
188 : :
189 : : static int
190 : 0 : nfp_flower_repr_stats_reset(struct rte_eth_dev *ethdev)
191 : : {
192 : : struct nfp_flower_representor *repr;
193 : :
194 : 0 : repr = ethdev->data->dev_private;
195 : 0 : memset(&repr->repr_stats, 0, sizeof(struct rte_eth_stats));
196 : :
197 : 0 : return 0;
198 : : }
199 : :
200 : : static int
201 : 0 : nfp_flower_repr_mac_addr_set(struct rte_eth_dev *ethdev,
202 : : struct rte_ether_addr *mac_addr)
203 : : {
204 : : struct nfp_flower_representor *repr;
205 : :
206 : 0 : repr = ethdev->data->dev_private;
207 : : rte_ether_addr_copy(mac_addr, &repr->mac_addr);
208 : 0 : rte_ether_addr_copy(mac_addr, ethdev->data->mac_addrs);
209 : :
210 : 0 : return 0;
211 : : }
212 : :
213 : : static uint16_t
214 : 0 : nfp_flower_repr_rx_burst(void *rx_queue,
215 : : struct rte_mbuf **rx_pkts,
216 : : uint16_t nb_pkts)
217 : : {
218 : : unsigned int available = 0;
219 : : unsigned int total_dequeue;
220 : : struct nfp_net_rxq *rxq;
221 : : struct rte_eth_dev *dev;
222 : : struct nfp_flower_representor *repr;
223 : :
224 : : rxq = rx_queue;
225 [ # # ]: 0 : if (unlikely(rxq == NULL)) {
226 : : PMD_RX_LOG(ERR, "RX Bad queue");
227 : : return 0;
228 : : }
229 : :
230 : 0 : dev = &rte_eth_devices[rxq->port_id];
231 : 0 : repr = dev->data->dev_private;
232 [ # # ]: 0 : if (unlikely(repr->ring == NULL)) {
233 : : PMD_RX_LOG(ERR, "representor %s has no ring configured!",
234 : : repr->name);
235 : : return 0;
236 : : }
237 : :
238 [ # # # # : 0 : total_dequeue = rte_ring_dequeue_burst(repr->ring, (void *)rx_pkts,
# ]
239 : : nb_pkts, &available);
240 [ # # ]: 0 : if (total_dequeue != 0) {
241 : : PMD_RX_LOG(DEBUG, "Representor Rx burst for %s, port_id: %#x, "
242 : : "received: %u, available: %u", repr->name,
243 : : repr->port_id, total_dequeue, available);
244 : :
245 : 0 : repr->repr_stats.ipackets += total_dequeue;
246 : : }
247 : :
248 : 0 : return total_dequeue;
249 : : }
250 : :
251 : : static uint16_t
252 : 0 : nfp_flower_repr_tx_burst(void *tx_queue,
253 : : struct rte_mbuf **tx_pkts,
254 : : uint16_t nb_pkts)
255 : : {
256 : : uint16_t i;
257 : : uint16_t sent;
258 : : void *pf_tx_queue;
259 : : struct nfp_net_txq *txq;
260 : : struct nfp_net_hw *pf_hw;
261 : : struct rte_eth_dev *dev;
262 : : struct rte_eth_dev *repr_dev;
263 : : struct nfp_flower_representor *repr;
264 : :
265 : : txq = tx_queue;
266 [ # # ]: 0 : if (unlikely(txq == NULL)) {
267 : : PMD_TX_LOG(ERR, "TX Bad queue");
268 : : return 0;
269 : : }
270 : :
271 : : /* Grab a handle to the representor struct */
272 : 0 : repr_dev = &rte_eth_devices[txq->port_id];
273 : 0 : repr = repr_dev->data->dev_private;
274 : :
275 [ # # ]: 0 : for (i = 0; i < nb_pkts; i++)
276 : 0 : nfp_flower_pkt_add_metadata(repr->app_fw_flower,
277 : 0 : tx_pkts[i], repr->port_id);
278 : :
279 : : /* This points to the PF vNIC that owns this representor */
280 : 0 : pf_hw = txq->hw;
281 : 0 : dev = pf_hw->eth_dev;
282 : :
283 : : /* Only using Tx queue 0 for now. */
284 : 0 : pf_tx_queue = dev->data->tx_queues[0];
285 : 0 : sent = nfp_flower_pf_xmit_pkts(pf_tx_queue, tx_pkts, nb_pkts);
286 [ # # ]: 0 : if (sent != 0) {
287 : : PMD_TX_LOG(DEBUG, "Representor Tx burst for %s, port_id: %#x transmitted: %hu",
288 : : repr->name, repr->port_id, sent);
289 : 0 : repr->repr_stats.opackets += sent;
290 : : }
291 : :
292 : : return sent;
293 : : }
294 : :
295 : : static int
296 : 0 : nfp_flower_repr_uninit(struct rte_eth_dev *eth_dev)
297 : : {
298 : : uint16_t index;
299 : : struct nfp_flower_representor *repr;
300 : :
301 : 0 : repr = eth_dev->data->dev_private;
302 : 0 : rte_ring_free(repr->ring);
303 : :
304 [ # # ]: 0 : if (repr->repr_type == NFP_REPR_TYPE_PHYS_PORT) {
305 : 0 : index = NFP_FLOWER_CMSG_PORT_PHYS_PORT_NUM(repr->port_id);
306 : 0 : repr->app_fw_flower->phy_reprs[index] = NULL;
307 : : } else {
308 : 0 : index = repr->vf_id;
309 : 0 : repr->app_fw_flower->vf_reprs[index] = NULL;
310 : : }
311 : :
312 : 0 : return 0;
313 : : }
314 : :
315 : : static int
316 : : nfp_flower_pf_repr_uninit(struct rte_eth_dev *eth_dev)
317 : : {
318 : 0 : struct nfp_flower_representor *repr = eth_dev->data->dev_private;
319 : :
320 : 0 : repr->app_fw_flower->pf_repr = NULL;
321 : :
322 : 0 : return 0;
323 : : }
324 : :
325 : : static void
326 : 0 : nfp_flower_repr_free(struct nfp_flower_representor *repr,
327 : : enum nfp_repr_type repr_type)
328 : : {
329 [ # # # # ]: 0 : switch (repr_type) {
330 : 0 : case NFP_REPR_TYPE_PHYS_PORT:
331 : 0 : nfp_flower_repr_uninit(repr->eth_dev);
332 : 0 : break;
333 : 0 : case NFP_REPR_TYPE_PF:
334 : 0 : nfp_flower_pf_repr_uninit(repr->eth_dev);
335 : : break;
336 : 0 : case NFP_REPR_TYPE_VF:
337 : 0 : nfp_flower_repr_uninit(repr->eth_dev);
338 : 0 : break;
339 : 0 : default:
340 : 0 : PMD_DRV_LOG(ERR, "Unsupported repr port type.");
341 : 0 : break;
342 : : }
343 : 0 : }
344 : :
345 : : /* Reset and stop device. The device can not be restarted. */
346 : : static int
347 : 0 : nfp_flower_repr_dev_close(struct rte_eth_dev *dev)
348 : : {
349 : : uint16_t i;
350 : : struct nfp_net_hw *hw;
351 : : struct nfp_pf_dev *pf_dev;
352 : : struct nfp_net_txq *this_tx_q;
353 : : struct nfp_net_rxq *this_rx_q;
354 : : struct nfp_flower_representor *repr;
355 : : struct nfp_app_fw_flower *app_fw_flower;
356 : :
357 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY)
358 : : return 0;
359 : :
360 : 0 : repr = dev->data->dev_private;
361 : 0 : app_fw_flower = repr->app_fw_flower;
362 : 0 : hw = app_fw_flower->pf_hw;
363 : 0 : pf_dev = hw->pf_dev;
364 : :
365 : : /*
366 : : * We assume that the DPDK application is stopping all the
367 : : * threads/queues before calling the device close function.
368 : : */
369 : 0 : nfp_net_disable_queues(dev);
370 : :
371 : : /* Clear queues */
372 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++) {
373 : 0 : this_tx_q = dev->data->tx_queues[i];
374 : 0 : nfp_net_reset_tx_queue(this_tx_q);
375 : : }
376 : :
377 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++) {
378 : 0 : this_rx_q = dev->data->rx_queues[i];
379 : 0 : nfp_net_reset_rx_queue(this_rx_q);
380 : : }
381 : :
382 [ # # ]: 0 : if (pf_dev->app_fw_id != NFP_APP_FW_FLOWER_NIC)
383 : : return -EINVAL;
384 : :
385 : 0 : nfp_flower_repr_free(repr, repr->repr_type);
386 : :
387 [ # # ]: 0 : for (i = 0; i < MAX_FLOWER_VFS; i++) {
388 [ # # ]: 0 : if (app_fw_flower->vf_reprs[i] != NULL)
389 : : return 0;
390 : : }
391 : :
392 [ # # ]: 0 : for (i = 0; i < NFP_MAX_PHYPORTS; i++) {
393 [ # # ]: 0 : if (app_fw_flower->phy_reprs[i] != NULL)
394 : : return 0;
395 : : }
396 : :
397 [ # # ]: 0 : if (app_fw_flower->pf_repr != NULL)
398 : : return 0;
399 : :
400 : : /* Stop flower service first */
401 : 0 : nfp_flower_service_stop(app_fw_flower);
402 : :
403 : : /* Disable cpp service */
404 : 0 : nfp_service_disable(&pf_dev->cpp_service_info);
405 : :
406 : : /* Now it is safe to free all PF resources */
407 : 0 : nfp_uninit_app_fw_flower(pf_dev);
408 : 0 : nfp_pf_uninit(pf_dev);
409 : :
410 : 0 : return 0;
411 : : }
412 : :
413 : : static const struct eth_dev_ops nfp_flower_pf_repr_dev_ops = {
414 : : .dev_infos_get = nfp_flower_repr_dev_infos_get,
415 : :
416 : : .dev_start = nfp_flower_pf_start,
417 : : .dev_configure = nfp_net_configure,
418 : : .dev_stop = nfp_net_stop,
419 : : .dev_close = nfp_flower_repr_dev_close,
420 : :
421 : : .rx_queue_setup = nfp_net_rx_queue_setup,
422 : : .tx_queue_setup = nfp_net_tx_queue_setup,
423 : :
424 : : .link_update = nfp_flower_repr_link_update,
425 : :
426 : : .stats_get = nfp_flower_repr_stats_get,
427 : : .stats_reset = nfp_flower_repr_stats_reset,
428 : :
429 : : .promiscuous_enable = nfp_net_promisc_enable,
430 : : .promiscuous_disable = nfp_net_promisc_disable,
431 : :
432 : : .mac_addr_set = nfp_flower_repr_mac_addr_set,
433 : : .fw_version_get = nfp_net_firmware_version_get,
434 : : };
435 : :
436 : : static const struct eth_dev_ops nfp_flower_repr_dev_ops = {
437 : : .dev_infos_get = nfp_flower_repr_dev_infos_get,
438 : :
439 : : .dev_start = nfp_flower_repr_dev_start,
440 : : .dev_configure = nfp_net_configure,
441 : : .dev_stop = nfp_flower_repr_dev_stop,
442 : : .dev_close = nfp_flower_repr_dev_close,
443 : :
444 : : .rx_queue_setup = nfp_flower_repr_rx_queue_setup,
445 : : .tx_queue_setup = nfp_flower_repr_tx_queue_setup,
446 : :
447 : : .link_update = nfp_flower_repr_link_update,
448 : :
449 : : .stats_get = nfp_flower_repr_stats_get,
450 : : .stats_reset = nfp_flower_repr_stats_reset,
451 : :
452 : : .promiscuous_enable = nfp_net_promisc_enable,
453 : : .promiscuous_disable = nfp_net_promisc_disable,
454 : :
455 : : .mac_addr_set = nfp_flower_repr_mac_addr_set,
456 : : .fw_version_get = nfp_net_firmware_version_get,
457 : :
458 : : .flow_ops_get = nfp_flow_ops_get,
459 : : .mtr_ops_get = nfp_net_mtr_ops_get,
460 : : };
461 : :
462 : : static uint32_t
463 : : nfp_flower_get_phys_port_id(uint8_t port)
464 : : {
465 : 0 : return (NFP_FLOWER_CMSG_PORT_TYPE_PHYS_PORT << 28) | port;
466 : : }
467 : :
468 : : static uint32_t
469 : : nfp_get_pcie_port_id(struct nfp_cpp *cpp,
470 : : int type,
471 : : uint8_t vnic,
472 : : uint8_t queue)
473 : : {
474 : : uint8_t nfp_pcie;
475 : : uint32_t port_id;
476 : :
477 : 0 : nfp_pcie = NFP_CPP_INTERFACE_UNIT_of(nfp_cpp_interface(cpp));
478 : 0 : port_id = ((nfp_pcie & 0x3) << 14) |
479 : 0 : ((type & 0x3) << 12) |
480 : 0 : ((vnic & 0x3f) << 6) |
481 : 0 : (queue & 0x3f) |
482 : : ((NFP_FLOWER_CMSG_PORT_TYPE_PCIE_PORT & 0xf) << 28);
483 : :
484 : : return port_id;
485 : : }
486 : :
487 : : static int
488 : 0 : nfp_flower_pf_repr_init(struct rte_eth_dev *eth_dev,
489 : : void *init_params)
490 : : {
491 : : struct nfp_flower_representor *repr;
492 : : struct nfp_flower_representor *init_repr_data;
493 : :
494 : : /* Cast the input representor data to the correct struct here */
495 : : init_repr_data = init_params;
496 : :
497 : : /* Memory has been allocated in the eth_dev_create() function */
498 : 0 : repr = eth_dev->data->dev_private;
499 : :
500 : : /* Copy data here from the input representor template */
501 : 0 : repr->vf_id = init_repr_data->vf_id;
502 : 0 : repr->switch_domain_id = init_repr_data->switch_domain_id;
503 : 0 : repr->repr_type = init_repr_data->repr_type;
504 : 0 : repr->app_fw_flower = init_repr_data->app_fw_flower;
505 : :
506 : 0 : snprintf(repr->name, sizeof(repr->name), "%s", init_repr_data->name);
507 : :
508 : 0 : eth_dev->dev_ops = &nfp_flower_pf_repr_dev_ops;
509 : 0 : eth_dev->rx_pkt_burst = nfp_net_recv_pkts;
510 : 0 : eth_dev->tx_pkt_burst = nfp_flower_pf_xmit_pkts;
511 : 0 : eth_dev->data->dev_flags |= RTE_ETH_DEV_REPRESENTOR;
512 : :
513 : 0 : eth_dev->data->representor_id = 0;
514 : :
515 : : /* This backer port is that of the eth_device created for the PF vNIC */
516 : 0 : eth_dev->data->backer_port_id = 0;
517 : :
518 : : /* Only single queues for representor devices */
519 : 0 : eth_dev->data->nb_rx_queues = 1;
520 : 0 : eth_dev->data->nb_tx_queues = 1;
521 : :
522 : : /* Allocating memory for mac addr */
523 : 0 : eth_dev->data->mac_addrs = rte_zmalloc("mac_addr", RTE_ETHER_ADDR_LEN, 0);
524 [ # # ]: 0 : if (eth_dev->data->mac_addrs == NULL) {
525 : 0 : PMD_INIT_LOG(ERR, "Failed to allocate memory for repr MAC");
526 : 0 : return -ENOMEM;
527 : : }
528 : :
529 : : rte_ether_addr_copy(&init_repr_data->mac_addr, &repr->mac_addr);
530 : : rte_ether_addr_copy(&init_repr_data->mac_addr, eth_dev->data->mac_addrs);
531 : :
532 : 0 : repr->app_fw_flower->pf_repr = repr;
533 : 0 : repr->app_fw_flower->pf_hw->eth_dev = eth_dev;
534 : 0 : repr->eth_dev = eth_dev;
535 : :
536 : 0 : return 0;
537 : : }
538 : :
539 : : static int
540 : 0 : nfp_flower_repr_init(struct rte_eth_dev *eth_dev,
541 : : void *init_params)
542 : : {
543 : : int ret;
544 : : uint16_t index;
545 : : unsigned int numa_node;
546 : : char ring_name[RTE_ETH_NAME_MAX_LEN];
547 : : struct nfp_app_fw_flower *app_fw_flower;
548 : : struct nfp_flower_representor *repr;
549 : : struct nfp_flower_representor *init_repr_data;
550 : :
551 : : /* Cast the input representor data to the correct struct here */
552 : : init_repr_data = init_params;
553 : 0 : app_fw_flower = init_repr_data->app_fw_flower;
554 : :
555 : : /* Memory has been allocated in the eth_dev_create() function */
556 : 0 : repr = eth_dev->data->dev_private;
557 : :
558 : : /*
559 : : * We need multiproduce rings as we can have multiple PF ports.
560 : : * On the other hand, we need single consumer rings, as just one
561 : : * representor PMD will try to read from the ring.
562 : : */
563 : 0 : snprintf(ring_name, sizeof(ring_name), "%s_%s", init_repr_data->name, "ring");
564 : 0 : numa_node = rte_socket_id();
565 : 0 : repr->ring = rte_ring_create(ring_name, 256, numa_node, RING_F_SC_DEQ);
566 [ # # ]: 0 : if (repr->ring == NULL) {
567 : 0 : PMD_DRV_LOG(ERR, "rte_ring_create failed for %s", ring_name);
568 : 0 : return -ENOMEM;
569 : : }
570 : :
571 : : /* Copy data here from the input representor template */
572 : 0 : repr->vf_id = init_repr_data->vf_id;
573 : 0 : repr->switch_domain_id = init_repr_data->switch_domain_id;
574 : 0 : repr->port_id = init_repr_data->port_id;
575 : 0 : repr->nfp_idx = init_repr_data->nfp_idx;
576 : 0 : repr->repr_type = init_repr_data->repr_type;
577 : 0 : repr->app_fw_flower = init_repr_data->app_fw_flower;
578 : :
579 [ # # ]: 0 : snprintf(repr->name, sizeof(repr->name), "%s", init_repr_data->name);
580 : :
581 : 0 : eth_dev->dev_ops = &nfp_flower_repr_dev_ops;
582 : 0 : eth_dev->rx_pkt_burst = nfp_flower_repr_rx_burst;
583 : 0 : eth_dev->tx_pkt_burst = nfp_flower_repr_tx_burst;
584 : 0 : eth_dev->data->dev_flags |= RTE_ETH_DEV_REPRESENTOR;
585 : :
586 [ # # ]: 0 : if (repr->repr_type == NFP_REPR_TYPE_PHYS_PORT)
587 : 0 : eth_dev->data->representor_id = repr->vf_id;
588 : : else
589 : 0 : eth_dev->data->representor_id = repr->vf_id +
590 : 0 : app_fw_flower->num_phyport_reprs + 1;
591 : :
592 : : /* This backer port is that of the eth_device created for the PF vNIC */
593 : 0 : eth_dev->data->backer_port_id = 0;
594 : :
595 : : /* Only single queues for representor devices */
596 : 0 : eth_dev->data->nb_rx_queues = 1;
597 : 0 : eth_dev->data->nb_tx_queues = 1;
598 : :
599 : : /* Allocating memory for mac addr */
600 : 0 : eth_dev->data->mac_addrs = rte_zmalloc("mac_addr", RTE_ETHER_ADDR_LEN, 0);
601 [ # # ]: 0 : if (eth_dev->data->mac_addrs == NULL) {
602 : 0 : PMD_INIT_LOG(ERR, "Failed to allocate memory for repr MAC");
603 : : ret = -ENOMEM;
604 : 0 : goto ring_cleanup;
605 : : }
606 : :
607 : : rte_ether_addr_copy(&init_repr_data->mac_addr, &repr->mac_addr);
608 : : rte_ether_addr_copy(&init_repr_data->mac_addr, eth_dev->data->mac_addrs);
609 : :
610 : : /* Send reify message to hardware to inform it about the new repr */
611 : 0 : ret = nfp_flower_cmsg_repr_reify(app_fw_flower, repr);
612 [ # # ]: 0 : if (ret != 0) {
613 : 0 : PMD_INIT_LOG(WARNING, "Failed to send repr reify message");
614 : 0 : goto mac_cleanup;
615 : : }
616 : :
617 : : /* Add repr to correct array */
618 [ # # ]: 0 : if (repr->repr_type == NFP_REPR_TYPE_PHYS_PORT) {
619 : 0 : index = NFP_FLOWER_CMSG_PORT_PHYS_PORT_NUM(repr->port_id);
620 : 0 : app_fw_flower->phy_reprs[index] = repr;
621 : : } else {
622 : 0 : index = repr->vf_id;
623 : 0 : app_fw_flower->vf_reprs[index] = repr;
624 : : }
625 : :
626 : 0 : repr->eth_dev = eth_dev;
627 : :
628 : 0 : return 0;
629 : :
630 : : mac_cleanup:
631 : 0 : rte_free(eth_dev->data->mac_addrs);
632 : 0 : ring_cleanup:
633 : 0 : rte_ring_free(repr->ring);
634 : :
635 : 0 : return ret;
636 : : }
637 : :
638 : : static void
639 : 0 : nfp_flower_repr_free_all(struct nfp_app_fw_flower *app_fw_flower)
640 : : {
641 : : uint32_t i;
642 : : struct nfp_flower_representor *repr;
643 : :
644 [ # # ]: 0 : for (i = 0; i < MAX_FLOWER_VFS; i++) {
645 : 0 : repr = app_fw_flower->vf_reprs[i];
646 [ # # ]: 0 : if (repr != NULL) {
647 : : nfp_flower_repr_free(repr, NFP_REPR_TYPE_VF);
648 : 0 : app_fw_flower->vf_reprs[i] = NULL;
649 : : }
650 : : }
651 : :
652 [ # # ]: 0 : for (i = 0; i < NFP_MAX_PHYPORTS; i++) {
653 : 0 : repr = app_fw_flower->phy_reprs[i];
654 [ # # ]: 0 : if (repr != NULL) {
655 : : nfp_flower_repr_free(repr, NFP_REPR_TYPE_PHYS_PORT);
656 : 0 : app_fw_flower->phy_reprs[i] = NULL;
657 : : }
658 : : }
659 : :
660 : 0 : repr = app_fw_flower->pf_repr;
661 [ # # ]: 0 : if (repr != NULL) {
662 : : nfp_flower_repr_free(repr, NFP_REPR_TYPE_PF);
663 : 0 : app_fw_flower->pf_repr = NULL;
664 : : }
665 : 0 : }
666 : :
667 : : static int
668 : 0 : nfp_flower_repr_alloc(struct nfp_app_fw_flower *app_fw_flower)
669 : : {
670 : : int i;
671 : : int ret;
672 : : const char *pci_name;
673 : : struct rte_eth_dev *eth_dev;
674 : : struct rte_pci_device *pci_dev;
675 : : struct nfp_eth_table *nfp_eth_table;
676 : : struct nfp_eth_table_port *eth_port;
677 : 0 : struct nfp_flower_representor flower_repr = {
678 : 0 : .switch_domain_id = app_fw_flower->switch_domain_id,
679 : : .app_fw_flower = app_fw_flower,
680 : : };
681 : :
682 : 0 : nfp_eth_table = app_fw_flower->pf_hw->pf_dev->nfp_eth_table;
683 : 0 : eth_dev = app_fw_flower->ctrl_hw->eth_dev;
684 : :
685 : : /* Send a NFP_FLOWER_CMSG_TYPE_MAC_REPR cmsg to hardware */
686 : 0 : ret = nfp_flower_cmsg_mac_repr(app_fw_flower);
687 [ # # ]: 0 : if (ret != 0) {
688 : 0 : PMD_INIT_LOG(ERR, "Cloud not send mac repr cmsgs");
689 : 0 : return ret;
690 : : }
691 : :
692 : : /* Create a rte_eth_dev for PF vNIC representor */
693 : 0 : flower_repr.repr_type = NFP_REPR_TYPE_PF;
694 : :
695 : : /* PF vNIC reprs get a random MAC address */
696 : 0 : rte_eth_random_addr(flower_repr.mac_addr.addr_bytes);
697 : :
698 : 0 : pci_dev = app_fw_flower->pf_hw->pf_dev->pci_dev;
699 : :
700 : 0 : pci_name = strchr(pci_dev->name, ':') + 1;
701 : :
702 : : snprintf(flower_repr.name, sizeof(flower_repr.name),
703 : : "%s_repr_pf", pci_name);
704 : :
705 : : /* Create a eth_dev for this representor */
706 : 0 : ret = rte_eth_dev_create(eth_dev->device, flower_repr.name,
707 : : sizeof(struct nfp_flower_representor),
708 : : NULL, NULL, nfp_flower_pf_repr_init, &flower_repr);
709 [ # # ]: 0 : if (ret != 0) {
710 : 0 : PMD_INIT_LOG(ERR, "Failed to init the pf repr");
711 : 0 : return -EINVAL;
712 : : }
713 : :
714 : : /* Create a rte_eth_dev for every phyport representor */
715 [ # # ]: 0 : for (i = 0; i < app_fw_flower->num_phyport_reprs; i++) {
716 : : eth_port = &nfp_eth_table->ports[i];
717 : 0 : flower_repr.repr_type = NFP_REPR_TYPE_PHYS_PORT;
718 : 0 : flower_repr.port_id = nfp_flower_get_phys_port_id(eth_port->index);
719 : 0 : flower_repr.nfp_idx = eth_port->eth_index;
720 : 0 : flower_repr.vf_id = i + 1;
721 : :
722 : : /* Copy the real mac of the interface to the representor struct */
723 : : rte_ether_addr_copy(ð_port->mac_addr, &flower_repr.mac_addr);
724 : : snprintf(flower_repr.name, sizeof(flower_repr.name),
725 : : "%s_repr_p%d", pci_name, i);
726 : :
727 : : /*
728 : : * Create a eth_dev for this representor.
729 : : * This will also allocate private memory for the device.
730 : : */
731 : 0 : ret = rte_eth_dev_create(eth_dev->device, flower_repr.name,
732 : : sizeof(struct nfp_flower_representor),
733 : : NULL, NULL, nfp_flower_repr_init, &flower_repr);
734 [ # # ]: 0 : if (ret != 0) {
735 : 0 : PMD_INIT_LOG(ERR, "Cloud not create eth_dev for repr");
736 : 0 : break;
737 : : }
738 : : }
739 : :
740 [ # # ]: 0 : if (i < app_fw_flower->num_phyport_reprs)
741 : 0 : goto repr_free;
742 : :
743 : : /*
744 : : * Now allocate eth_dev's for VF representors.
745 : : * Also send reify messages.
746 : : */
747 [ # # ]: 0 : for (i = 0; i < app_fw_flower->num_vf_reprs; i++) {
748 : 0 : flower_repr.repr_type = NFP_REPR_TYPE_VF;
749 : 0 : flower_repr.port_id = nfp_get_pcie_port_id(app_fw_flower->pf_hw->cpp,
750 : : NFP_FLOWER_CMSG_PORT_VNIC_TYPE_VF, i, 0);
751 : 0 : flower_repr.nfp_idx = 0;
752 : 0 : flower_repr.vf_id = i;
753 : :
754 : : /* VF reprs get a random MAC address */
755 : 0 : rte_eth_random_addr(flower_repr.mac_addr.addr_bytes);
756 : : snprintf(flower_repr.name, sizeof(flower_repr.name),
757 : : "%s_repr_vf%d", pci_name, i);
758 : :
759 : : /* This will also allocate private memory for the device */
760 : 0 : ret = rte_eth_dev_create(eth_dev->device, flower_repr.name,
761 : : sizeof(struct nfp_flower_representor),
762 : : NULL, NULL, nfp_flower_repr_init, &flower_repr);
763 [ # # ]: 0 : if (ret != 0) {
764 : 0 : PMD_INIT_LOG(ERR, "Cloud not create eth_dev for repr");
765 : 0 : break;
766 : : }
767 : : }
768 : :
769 [ # # ]: 0 : if (i < app_fw_flower->num_vf_reprs)
770 : 0 : goto repr_free;
771 : :
772 : : return 0;
773 : :
774 : 0 : repr_free:
775 : 0 : nfp_flower_repr_free_all(app_fw_flower);
776 : :
777 : 0 : return ret;
778 : : }
779 : :
780 : : int
781 : 0 : nfp_flower_repr_create(struct nfp_app_fw_flower *app_fw_flower)
782 : : {
783 : : int ret;
784 : : struct nfp_pf_dev *pf_dev;
785 : : struct rte_pci_device *pci_dev;
786 : : struct nfp_eth_table *nfp_eth_table;
787 : 0 : struct rte_eth_devargs eth_da = {
788 : : .nb_representor_ports = 0
789 : : };
790 : :
791 : 0 : pf_dev = app_fw_flower->pf_hw->pf_dev;
792 : 0 : pci_dev = pf_dev->pci_dev;
793 : :
794 : : /* Allocate a switch domain for the flower app */
795 [ # # # # ]: 0 : if (app_fw_flower->switch_domain_id == RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID &&
796 : 0 : rte_eth_switch_domain_alloc(&app_fw_flower->switch_domain_id) != 0) {
797 : 0 : PMD_INIT_LOG(WARNING, "failed to allocate switch domain for device");
798 : : }
799 : :
800 : : /* Now parse PCI device args passed for representor info */
801 [ # # ]: 0 : if (pci_dev->device.devargs != NULL) {
802 : 0 : ret = rte_eth_devargs_parse(pci_dev->device.devargs->args, ð_da, 1);
803 [ # # ]: 0 : if (ret < 0) {
804 : 0 : PMD_INIT_LOG(ERR, "devarg parse failed");
805 : 0 : return -EINVAL;
806 : : }
807 : : }
808 : :
809 [ # # ]: 0 : if (eth_da.nb_representor_ports == 0) {
810 : 0 : PMD_INIT_LOG(DEBUG, "No representor port need to create.");
811 : 0 : return 0;
812 : : }
813 : :
814 : : /* There always exist phy repr */
815 : 0 : nfp_eth_table = pf_dev->nfp_eth_table;
816 [ # # ]: 0 : if (eth_da.nb_representor_ports < nfp_eth_table->count + 1) {
817 : 0 : PMD_INIT_LOG(ERR, "Should also create repr port for phy port and PF vNIC.");
818 : 0 : return -ERANGE;
819 : : }
820 : :
821 : : /* Only support VF representor creation via the command line */
822 [ # # ]: 0 : if (eth_da.type != RTE_ETH_REPRESENTOR_VF) {
823 : 0 : PMD_INIT_LOG(ERR, "Unsupported representor type: %d", eth_da.type);
824 : 0 : return -ENOTSUP;
825 : : }
826 : :
827 : : /* Fill in flower app with repr counts */
828 : 0 : app_fw_flower->num_phyport_reprs = (uint8_t)nfp_eth_table->count;
829 : 0 : app_fw_flower->num_vf_reprs = eth_da.nb_representor_ports -
830 : 0 : nfp_eth_table->count - 1;
831 : :
832 : 0 : PMD_INIT_LOG(INFO, "%d number of VF reprs", app_fw_flower->num_vf_reprs);
833 : 0 : PMD_INIT_LOG(INFO, "%d number of phyport reprs", app_fw_flower->num_phyport_reprs);
834 : :
835 : 0 : ret = nfp_flower_repr_alloc(app_fw_flower);
836 [ # # ]: 0 : if (ret != 0) {
837 : 0 : PMD_INIT_LOG(ERR, "representors allocation failed");
838 : : ret = -EINVAL;
839 : 0 : goto domain_free;
840 : : }
841 : :
842 : : return 0;
843 : :
844 : : domain_free:
845 [ # # ]: 0 : if (rte_eth_switch_domain_free(app_fw_flower->switch_domain_id) != 0)
846 : 0 : PMD_INIT_LOG(WARNING, "failed to free switch domain for device");
847 : :
848 : : return ret;
849 : : }
|