Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : *
3 : : * Copyright(c) 2019-2021 Xilinx, Inc.
4 : : * Copyright(c) 2019 Solarflare Communications Inc.
5 : : *
6 : : * This software was jointly developed between OKTET Labs (under contract
7 : : * for Solarflare) and Solarflare Communications, Inc.
8 : : */
9 : :
10 : : #include <stdint.h>
11 : :
12 : : #include <rte_flow_driver.h>
13 : : #include <rte_flow.h>
14 : : #include <rte_mbuf.h>
15 : : #include <rte_ethdev.h>
16 : : #include <rte_malloc.h>
17 : : #include <ethdev_driver.h>
18 : :
19 : : #include "efx.h"
20 : :
21 : : #include "sfc_log.h"
22 : : #include "sfc_debug.h"
23 : : #include "sfc_repr.h"
24 : : #include "sfc_ethdev_state.h"
25 : : #include "sfc_repr_proxy_api.h"
26 : : #include "sfc_switch.h"
27 : : #include "sfc_dp_tx.h"
28 : :
29 : : /** Multi-process shared representor private data */
30 : : struct sfc_repr_shared {
31 : : uint16_t pf_port_id;
32 : : uint16_t repr_id;
33 : : uint16_t switch_domain_id;
34 : : uint16_t switch_port_id;
35 : : unsigned int max_pdu;
36 : : };
37 : :
38 : : struct sfc_repr_queue_stats {
39 : : union sfc_pkts_bytes packets_bytes;
40 : : };
41 : :
42 : : struct sfc_repr_rxq {
43 : : /* Datapath members */
44 : : struct rte_ring *ring;
45 : : struct sfc_repr_queue_stats stats;
46 : : };
47 : :
48 : : struct sfc_repr_txq {
49 : : /* Datapath members */
50 : : struct rte_ring *ring;
51 : : efx_mport_id_t egress_mport;
52 : : struct sfc_repr_queue_stats stats;
53 : : };
54 : :
55 : : /** Primary process representor private data */
56 : : struct sfc_repr {
57 : : /**
58 : : * PMD setup and configuration is not thread safe. Since it is not
59 : : * performance sensitive, it is better to guarantee thread-safety
60 : : * and add device level lock. Adapter control operations which
61 : : * change its state should acquire the lock.
62 : : */
63 : : rte_spinlock_t lock;
64 : : enum sfc_ethdev_state state;
65 : : };
66 : :
67 : : #define sfcr_err(sr, ...) \
68 : : do { \
69 : : const struct sfc_repr *_sr = (sr); \
70 : : \
71 : : (void)_sr; \
72 : : SFC_GENERIC_LOG(ERR, __VA_ARGS__); \
73 : : } while (0)
74 : :
75 : : #define sfcr_warn(sr, ...) \
76 : : do { \
77 : : const struct sfc_repr *_sr = (sr); \
78 : : \
79 : : (void)_sr; \
80 : : SFC_GENERIC_LOG(WARNING, __VA_ARGS__); \
81 : : } while (0)
82 : :
83 : : #define sfcr_info(sr, ...) \
84 : : do { \
85 : : const struct sfc_repr *_sr = (sr); \
86 : : \
87 : : (void)_sr; \
88 : : SFC_GENERIC_LOG(INFO, \
89 : : RTE_FMT("%s() " \
90 : : RTE_FMT_HEAD(__VA_ARGS__ ,), \
91 : : __func__, \
92 : : RTE_FMT_TAIL(__VA_ARGS__ ,))); \
93 : : } while (0)
94 : :
95 : : static inline struct sfc_repr_shared *
96 : : sfc_repr_shared_by_eth_dev(struct rte_eth_dev *eth_dev)
97 : : {
98 : 0 : struct sfc_repr_shared *srs = eth_dev->data->dev_private;
99 : :
100 : : return srs;
101 : : }
102 : :
103 : : static inline struct sfc_repr *
104 : : sfc_repr_by_eth_dev(struct rte_eth_dev *eth_dev)
105 : : {
106 : 0 : struct sfc_repr *sr = eth_dev->process_private;
107 : :
108 : : return sr;
109 : : }
110 : :
111 : : /*
112 : : * Add wrapper functions to acquire/release lock to be able to remove or
113 : : * change the lock in one place.
114 : : */
115 : :
116 : : #define sfc_repr_lock_init(sr) rte_spinlock_init(&(sr)->lock)
117 : : #define sfc_repr_lock_is_locked(sr) rte_spinlock_is_locked(&(sr)->lock)
118 : : #define sfc_repr_lock(sr) rte_spinlock_lock(&(sr)->lock)
119 : : #define sfc_repr_unlock(sr) rte_spinlock_unlock(&(sr)->lock)
120 : : #define sfc_repr_lock_fini(sr) RTE_SET_USED(sr)
121 : :
122 : : static void
123 : : sfc_repr_rx_queue_stop(void *queue)
124 : : {
125 : : struct sfc_repr_rxq *rxq = queue;
126 : :
127 : 0 : if (rxq == NULL)
128 : : return;
129 : :
130 : 0 : rte_ring_reset(rxq->ring);
131 : : }
132 : :
133 : : static void
134 : : sfc_repr_tx_queue_stop(void *queue)
135 : : {
136 : : struct sfc_repr_txq *txq = queue;
137 : :
138 : 0 : if (txq == NULL)
139 : : return;
140 : :
141 : 0 : rte_ring_reset(txq->ring);
142 : : }
143 : :
144 : : static uint16_t
145 : 0 : sfc_repr_rx_burst(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
146 : : {
147 : : struct sfc_repr_rxq *rxq = rx_queue;
148 : : void **objs = (void *)&rx_pkts[0];
149 : : unsigned int n_rx;
150 : :
151 : : /* mbufs port is already filled correctly by representors proxy */
152 [ # # ]: 0 : n_rx = rte_ring_sc_dequeue_burst(rxq->ring, objs, nb_pkts, NULL);
153 : :
154 : : if (n_rx > 0) {
155 : : unsigned int n_bytes = 0;
156 : : unsigned int i = 0;
157 : :
158 : : do {
159 : 0 : n_bytes += rx_pkts[i]->pkt_len;
160 [ # # ]: 0 : } while (++i < n_rx);
161 : :
162 : 0 : sfc_pkts_bytes_add(&rxq->stats.packets_bytes, n_rx, n_bytes);
163 : : }
164 : :
165 : 0 : return n_rx;
166 : : }
167 : :
168 : : static uint16_t
169 : 0 : sfc_repr_tx_burst(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
170 : : {
171 : : struct sfc_repr_txq *txq = tx_queue;
172 : : unsigned int n_bytes = 0;
173 : : unsigned int n_tx;
174 : : void **objs;
175 : : uint16_t i;
176 : :
177 : : /*
178 : : * mbuf is likely cache-hot. Set flag and egress m-port here instead of
179 : : * doing that in representors proxy. Also, it should help to avoid
180 : : * cache bounce. Moreover, potentially, it allows using one
181 : : * multi-producer single-consumer ring for all representors.
182 : : *
183 : : * The only potential problem is doing so many times if enqueue
184 : : * fails and sender retries.
185 : : */
186 [ # # ]: 0 : for (i = 0; i < nb_pkts; ++i) {
187 : 0 : struct rte_mbuf *m = tx_pkts[i];
188 : :
189 : 0 : m->ol_flags |= sfc_dp_mport_override;
190 : 0 : *RTE_MBUF_DYNFIELD(m, sfc_dp_mport_offset,
191 : 0 : efx_mport_id_t *) = txq->egress_mport;
192 : 0 : n_bytes += tx_pkts[i]->pkt_len;
193 : : }
194 : :
195 : : objs = (void *)&tx_pkts[0];
196 [ # # ]: 0 : n_tx = rte_ring_sp_enqueue_burst(txq->ring, objs, nb_pkts, NULL);
197 : :
198 : : /*
199 : : * Remove m-port override flag from packets that were not enqueued
200 : : * Setting the flag only for enqueued packets after the burst is
201 : : * not possible since the ownership of enqueued packets is
202 : : * transferred to representor proxy. The same logic applies to
203 : : * counting the enqueued packets' bytes.
204 : : */
205 [ # # ]: 0 : for (i = n_tx; i < nb_pkts; ++i) {
206 : 0 : struct rte_mbuf *m = tx_pkts[i];
207 : :
208 : 0 : m->ol_flags &= ~sfc_dp_mport_override;
209 : 0 : n_bytes -= m->pkt_len;
210 : : }
211 : :
212 : 0 : sfc_pkts_bytes_add(&txq->stats.packets_bytes, n_tx, n_bytes);
213 : :
214 : 0 : return n_tx;
215 : : }
216 : :
217 : : static int
218 : 0 : sfc_repr_start(struct rte_eth_dev *dev)
219 : : {
220 : : struct sfc_repr *sr = sfc_repr_by_eth_dev(dev);
221 : : struct sfc_repr_shared *srs;
222 : : int ret;
223 : :
224 : 0 : sfcr_info(sr, "entry");
225 : :
226 : : SFC_ASSERT(sfc_repr_lock_is_locked(sr));
227 : :
228 [ # # # ]: 0 : switch (sr->state) {
229 : : case SFC_ETHDEV_CONFIGURED:
230 : : break;
231 : 0 : case SFC_ETHDEV_STARTED:
232 : 0 : sfcr_info(sr, "already started");
233 : 0 : return 0;
234 : 0 : default:
235 : : ret = -EINVAL;
236 : 0 : goto fail_bad_state;
237 : : }
238 : :
239 : 0 : sr->state = SFC_ETHDEV_STARTING;
240 : :
241 : : srs = sfc_repr_shared_by_eth_dev(dev);
242 : 0 : ret = sfc_repr_proxy_start_repr(srs->pf_port_id, srs->repr_id);
243 [ # # ]: 0 : if (ret != 0) {
244 : : SFC_ASSERT(ret > 0);
245 : 0 : ret = -ret;
246 : 0 : goto fail_start;
247 : : }
248 : :
249 : 0 : sr->state = SFC_ETHDEV_STARTED;
250 : :
251 : 0 : sfcr_info(sr, "done");
252 : :
253 : 0 : return 0;
254 : :
255 : : fail_start:
256 : 0 : sr->state = SFC_ETHDEV_CONFIGURED;
257 : :
258 : 0 : fail_bad_state:
259 : 0 : sfcr_err(sr, "%s() failed: %s", __func__, rte_strerror(-ret));
260 : 0 : return ret;
261 : : }
262 : :
263 : : static int
264 : 0 : sfc_repr_dev_start(struct rte_eth_dev *dev)
265 : : {
266 : : struct sfc_repr *sr = sfc_repr_by_eth_dev(dev);
267 : : uint16_t i;
268 : : int ret;
269 : :
270 : 0 : sfcr_info(sr, "entry");
271 : :
272 : 0 : sfc_repr_lock(sr);
273 : 0 : ret = sfc_repr_start(dev);
274 : : sfc_repr_unlock(sr);
275 : :
276 [ # # ]: 0 : if (ret != 0)
277 : 0 : goto fail_start;
278 : :
279 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++)
280 : 0 : dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
281 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++)
282 : 0 : dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
283 : :
284 : 0 : sfcr_info(sr, "done");
285 : :
286 : 0 : return 0;
287 : :
288 : : fail_start:
289 : 0 : sfcr_err(sr, "%s() failed: %s", __func__, rte_strerror(-ret));
290 : 0 : return ret;
291 : : }
292 : :
293 : : static int
294 : 0 : sfc_repr_stop(struct rte_eth_dev *dev)
295 : : {
296 : : struct sfc_repr *sr = sfc_repr_by_eth_dev(dev);
297 : : struct sfc_repr_shared *srs;
298 : : unsigned int i;
299 : : int ret;
300 : :
301 : 0 : sfcr_info(sr, "entry");
302 : :
303 : : SFC_ASSERT(sfc_repr_lock_is_locked(sr));
304 : :
305 [ # # # ]: 0 : switch (sr->state) {
306 : : case SFC_ETHDEV_STARTED:
307 : : break;
308 : 0 : case SFC_ETHDEV_CONFIGURED:
309 : 0 : sfcr_info(sr, "already stopped");
310 : 0 : return 0;
311 : 0 : default:
312 : 0 : sfcr_err(sr, "stop in unexpected state %u", sr->state);
313 : : SFC_ASSERT(B_FALSE);
314 : : ret = -EINVAL;
315 : 0 : goto fail_bad_state;
316 : : }
317 : :
318 : : srs = sfc_repr_shared_by_eth_dev(dev);
319 : 0 : ret = sfc_repr_proxy_stop_repr(srs->pf_port_id, srs->repr_id);
320 [ # # ]: 0 : if (ret != 0) {
321 : : SFC_ASSERT(ret > 0);
322 : 0 : ret = -ret;
323 : 0 : goto fail_stop;
324 : : }
325 : :
326 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++)
327 [ # # ]: 0 : sfc_repr_rx_queue_stop(dev->data->rx_queues[i]);
328 : :
329 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++)
330 [ # # ]: 0 : sfc_repr_tx_queue_stop(dev->data->tx_queues[i]);
331 : :
332 : 0 : sr->state = SFC_ETHDEV_CONFIGURED;
333 : 0 : sfcr_info(sr, "done");
334 : :
335 : 0 : return 0;
336 : :
337 : : fail_bad_state:
338 : 0 : fail_stop:
339 : 0 : sfcr_err(sr, "%s() failed: %s", __func__, rte_strerror(-ret));
340 : :
341 : 0 : return ret;
342 : : }
343 : :
344 : : static int
345 : 0 : sfc_repr_dev_stop(struct rte_eth_dev *dev)
346 : : {
347 : : struct sfc_repr *sr = sfc_repr_by_eth_dev(dev);
348 : : uint16_t i;
349 : : int ret;
350 : :
351 : 0 : sfcr_info(sr, "entry");
352 : :
353 : 0 : sfc_repr_lock(sr);
354 : :
355 : 0 : ret = sfc_repr_stop(dev);
356 [ # # ]: 0 : if (ret != 0) {
357 : 0 : sfcr_err(sr, "%s() failed to stop representor", __func__);
358 : 0 : goto fail_stop;
359 : : }
360 : :
361 : : sfc_repr_unlock(sr);
362 : :
363 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++)
364 : 0 : dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
365 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++)
366 : 0 : dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
367 : :
368 : 0 : sfcr_info(sr, "done");
369 : :
370 : 0 : return 0;
371 : :
372 : : fail_stop:
373 : : sfc_repr_unlock(sr);
374 : :
375 : 0 : sfcr_err(sr, "%s() failed %s", __func__, rte_strerror(-ret));
376 : :
377 : 0 : return ret;
378 : : }
379 : :
380 : : static int
381 : 0 : sfc_repr_check_conf(struct sfc_repr *sr, const struct rte_eth_conf *conf)
382 : : {
383 : : int ret = 0;
384 : :
385 : 0 : sfcr_info(sr, "entry");
386 : :
387 [ # # ]: 0 : if (conf->link_speeds != 0) {
388 : 0 : sfcr_err(sr, "specific link speeds not supported");
389 : : ret = -EINVAL;
390 : : }
391 : :
392 [ # # ]: 0 : if (conf->lpbk_mode != 0) {
393 : 0 : sfcr_err(sr, "loopback not supported");
394 : : ret = -EINVAL;
395 : : }
396 : :
397 [ # # ]: 0 : if (conf->dcb_capability_en != 0) {
398 : 0 : sfcr_err(sr, "priority-based flow control not supported");
399 : : ret = -EINVAL;
400 : : }
401 : :
402 [ # # ]: 0 : if (conf->intr_conf.lsc != 0) {
403 : 0 : sfcr_err(sr, "link status change interrupt not supported");
404 : : ret = -EINVAL;
405 : : }
406 : :
407 [ # # ]: 0 : if (conf->intr_conf.rxq != 0) {
408 : 0 : sfcr_err(sr, "receive queue interrupt not supported");
409 : : ret = -EINVAL;
410 : : }
411 : :
412 [ # # ]: 0 : if (conf->intr_conf.rmv != 0) {
413 : 0 : sfcr_err(sr, "remove interrupt not supported");
414 : : ret = -EINVAL;
415 : : }
416 : :
417 : 0 : sfcr_info(sr, "done %d", ret);
418 : :
419 : 0 : return ret;
420 : : }
421 : :
422 : :
423 : : static int
424 : 0 : sfc_repr_configure(struct sfc_repr *sr, const struct rte_eth_conf *conf)
425 : : {
426 : : int ret;
427 : :
428 : 0 : sfcr_info(sr, "entry");
429 : :
430 : : SFC_ASSERT(sfc_repr_lock_is_locked(sr));
431 : :
432 : 0 : ret = sfc_repr_check_conf(sr, conf);
433 [ # # ]: 0 : if (ret != 0)
434 : 0 : goto fail_check_conf;
435 : :
436 : 0 : sr->state = SFC_ETHDEV_CONFIGURED;
437 : :
438 : 0 : sfcr_info(sr, "done");
439 : :
440 : 0 : return 0;
441 : :
442 : : fail_check_conf:
443 : 0 : sfcr_info(sr, "failed %s", rte_strerror(-ret));
444 : 0 : return ret;
445 : : }
446 : :
447 : : static int
448 : 0 : sfc_repr_dev_configure(struct rte_eth_dev *dev)
449 : : {
450 : : struct sfc_repr *sr = sfc_repr_by_eth_dev(dev);
451 : 0 : struct rte_eth_dev_data *dev_data = dev->data;
452 : : int ret;
453 : :
454 : 0 : sfcr_info(sr, "entry n_rxq=%u n_txq=%u",
455 : : dev_data->nb_rx_queues, dev_data->nb_tx_queues);
456 : :
457 : 0 : sfc_repr_lock(sr);
458 [ # # ]: 0 : switch (sr->state) {
459 : 0 : case SFC_ETHDEV_CONFIGURED:
460 : : /* FALLTHROUGH */
461 : : case SFC_ETHDEV_INITIALIZED:
462 : 0 : ret = sfc_repr_configure(sr, &dev_data->dev_conf);
463 : 0 : break;
464 : 0 : default:
465 : 0 : sfcr_err(sr, "unexpected adapter state %u to configure",
466 : : sr->state);
467 : : ret = -EINVAL;
468 : 0 : break;
469 : : }
470 : : sfc_repr_unlock(sr);
471 : :
472 : 0 : sfcr_info(sr, "done %s", rte_strerror(-ret));
473 : :
474 : 0 : return ret;
475 : : }
476 : :
477 : : static int
478 : 0 : sfc_repr_dev_infos_get(struct rte_eth_dev *dev,
479 : : struct rte_eth_dev_info *dev_info)
480 : : {
481 : : struct sfc_repr_shared *srs = sfc_repr_shared_by_eth_dev(dev);
482 : :
483 : 0 : dev_info->device = dev->device;
484 : :
485 : 0 : dev_info->max_rx_pktlen = srs->max_pdu;
486 : 0 : dev_info->max_rx_queues = SFC_REPR_RXQ_MAX;
487 : 0 : dev_info->max_tx_queues = SFC_REPR_TXQ_MAX;
488 : 0 : dev_info->default_rxconf.rx_drop_en = 1;
489 : 0 : dev_info->switch_info.domain_id = srs->switch_domain_id;
490 : 0 : dev_info->switch_info.port_id = srs->switch_port_id;
491 : :
492 : 0 : return 0;
493 : : }
494 : :
495 : : static int
496 : 0 : sfc_repr_dev_link_update(struct rte_eth_dev *dev,
497 : : __rte_unused int wait_to_complete)
498 : : {
499 : : struct sfc_repr *sr = sfc_repr_by_eth_dev(dev);
500 : : struct rte_eth_link link;
501 : :
502 [ # # ]: 0 : if (sr->state != SFC_ETHDEV_STARTED) {
503 : 0 : sfc_port_link_mode_to_info(EFX_LINK_UNKNOWN, 0, &link);
504 : : } else {
505 : : memset(&link, 0, sizeof(link));
506 : 0 : link.link_status = RTE_ETH_LINK_UP;
507 : 0 : link.link_speed = RTE_ETH_SPEED_NUM_UNKNOWN;
508 : : }
509 : :
510 : 0 : return rte_eth_linkstatus_set(dev, &link);
511 : : }
512 : :
513 : : static int
514 : 0 : sfc_repr_ring_create(uint16_t pf_port_id, uint16_t repr_id,
515 : : const char *type_name, uint16_t qid, uint16_t nb_desc,
516 : : unsigned int socket_id, struct rte_ring **ring)
517 : : {
518 : : char ring_name[RTE_RING_NAMESIZE];
519 : : int ret;
520 : :
521 [ # # ]: 0 : ret = snprintf(ring_name, sizeof(ring_name), "sfc_%u_repr_%u_%sq%u",
522 : : pf_port_id, repr_id, type_name, qid);
523 [ # # ]: 0 : if (ret >= (int)sizeof(ring_name))
524 : : return -ENAMETOOLONG;
525 : :
526 : : /*
527 : : * Single producer/consumer rings are used since the API for Tx/Rx
528 : : * packet burst for representors are guaranteed to be called from
529 : : * a single thread, and the user of the other end (representor proxy)
530 : : * is also single-threaded.
531 : : */
532 : 0 : *ring = rte_ring_create(ring_name, nb_desc, socket_id,
533 : : RING_F_SP_ENQ | RING_F_SC_DEQ);
534 [ # # ]: 0 : if (*ring == NULL)
535 : 0 : return -rte_errno;
536 : :
537 : : return 0;
538 : : }
539 : :
540 : : static int
541 : 0 : sfc_repr_rx_qcheck_conf(struct sfc_repr *sr,
542 : : const struct rte_eth_rxconf *rx_conf)
543 : : {
544 : : int ret = 0;
545 : :
546 : 0 : sfcr_info(sr, "entry");
547 : :
548 : 0 : if (rx_conf->rx_thresh.pthresh != 0 ||
549 [ # # ]: 0 : rx_conf->rx_thresh.hthresh != 0 ||
550 : : rx_conf->rx_thresh.wthresh != 0) {
551 : 0 : sfcr_warn(sr,
552 : : "RxQ prefetch/host/writeback thresholds are not supported");
553 : : }
554 : :
555 [ # # ]: 0 : if (rx_conf->rx_free_thresh != 0)
556 : 0 : sfcr_warn(sr, "RxQ free threshold is not supported");
557 : :
558 [ # # ]: 0 : if (rx_conf->rx_drop_en == 0)
559 : 0 : sfcr_warn(sr, "RxQ drop disable is not supported");
560 : :
561 [ # # ]: 0 : if (rx_conf->rx_deferred_start) {
562 : 0 : sfcr_err(sr, "Deferred start is not supported");
563 : : ret = -EINVAL;
564 : : }
565 : :
566 : 0 : sfcr_info(sr, "done: %s", rte_strerror(-ret));
567 : :
568 : 0 : return ret;
569 : : }
570 : :
571 : : static int
572 : 0 : sfc_repr_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
573 : : uint16_t nb_rx_desc, unsigned int socket_id,
574 : : __rte_unused const struct rte_eth_rxconf *rx_conf,
575 : : struct rte_mempool *mb_pool)
576 : : {
577 : : struct sfc_repr_shared *srs = sfc_repr_shared_by_eth_dev(dev);
578 : : struct sfc_repr *sr = sfc_repr_by_eth_dev(dev);
579 : : struct sfc_repr_rxq *rxq;
580 : : int ret;
581 : :
582 : 0 : sfcr_info(sr, "entry");
583 : :
584 : 0 : ret = sfc_repr_rx_qcheck_conf(sr, rx_conf);
585 [ # # ]: 0 : if (ret != 0)
586 : 0 : goto fail_check_conf;
587 : :
588 : : ret = -ENOMEM;
589 : 0 : rxq = rte_zmalloc_socket("sfc-repr-rxq", sizeof(*rxq),
590 : : RTE_CACHE_LINE_SIZE, socket_id);
591 [ # # ]: 0 : if (rxq == NULL) {
592 : 0 : sfcr_err(sr, "%s() failed to alloc RxQ", __func__);
593 : 0 : goto fail_rxq_alloc;
594 : : }
595 : :
596 : 0 : ret = sfc_repr_ring_create(srs->pf_port_id, srs->repr_id,
597 : : "rx", rx_queue_id, nb_rx_desc,
598 : : socket_id, &rxq->ring);
599 [ # # ]: 0 : if (ret != 0) {
600 : 0 : sfcr_err(sr, "%s() failed to create ring", __func__);
601 : 0 : goto fail_ring_create;
602 : : }
603 : :
604 : 0 : ret = sfc_repr_proxy_add_rxq(srs->pf_port_id, srs->repr_id,
605 : : rx_queue_id, rxq->ring, mb_pool);
606 [ # # ]: 0 : if (ret != 0) {
607 : : SFC_ASSERT(ret > 0);
608 : 0 : ret = -ret;
609 : 0 : sfcr_err(sr, "%s() failed to add proxy RxQ", __func__);
610 : 0 : goto fail_proxy_add_rxq;
611 : : }
612 : :
613 : 0 : dev->data->rx_queues[rx_queue_id] = rxq;
614 : :
615 : 0 : sfcr_info(sr, "done");
616 : :
617 : 0 : return 0;
618 : :
619 : : fail_proxy_add_rxq:
620 : 0 : rte_ring_free(rxq->ring);
621 : :
622 : 0 : fail_ring_create:
623 : 0 : rte_free(rxq);
624 : :
625 : 0 : fail_rxq_alloc:
626 : 0 : fail_check_conf:
627 : 0 : sfcr_err(sr, "%s() failed: %s", __func__, rte_strerror(-ret));
628 : 0 : return ret;
629 : : }
630 : :
631 : : static void
632 : 0 : sfc_repr_rx_queue_release(struct rte_eth_dev *dev, uint16_t rx_queue_id)
633 : : {
634 : : struct sfc_repr_shared *srs = sfc_repr_shared_by_eth_dev(dev);
635 : 0 : struct sfc_repr_rxq *rxq = dev->data->rx_queues[rx_queue_id];
636 : :
637 : 0 : sfc_repr_proxy_del_rxq(srs->pf_port_id, srs->repr_id, rx_queue_id);
638 : 0 : rte_ring_free(rxq->ring);
639 : 0 : rte_free(rxq);
640 : 0 : }
641 : :
642 : : static int
643 : 0 : sfc_repr_tx_qcheck_conf(struct sfc_repr *sr,
644 : : const struct rte_eth_txconf *tx_conf)
645 : : {
646 : : int ret = 0;
647 : :
648 : 0 : sfcr_info(sr, "entry");
649 : :
650 [ # # ]: 0 : if (tx_conf->tx_rs_thresh != 0)
651 : 0 : sfcr_warn(sr, "RS bit in transmit descriptor is not supported");
652 : :
653 [ # # ]: 0 : if (tx_conf->tx_free_thresh != 0)
654 : 0 : sfcr_warn(sr, "TxQ free threshold is not supported");
655 : :
656 : 0 : if (tx_conf->tx_thresh.pthresh != 0 ||
657 [ # # ]: 0 : tx_conf->tx_thresh.hthresh != 0 ||
658 : : tx_conf->tx_thresh.wthresh != 0) {
659 : 0 : sfcr_warn(sr,
660 : : "prefetch/host/writeback thresholds are not supported");
661 : : }
662 : :
663 [ # # ]: 0 : if (tx_conf->tx_deferred_start) {
664 : 0 : sfcr_err(sr, "Deferred start is not supported");
665 : : ret = -EINVAL;
666 : : }
667 : :
668 : 0 : sfcr_info(sr, "done: %s", rte_strerror(-ret));
669 : :
670 : 0 : return ret;
671 : : }
672 : :
673 : : static int
674 : 0 : sfc_repr_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
675 : : uint16_t nb_tx_desc, unsigned int socket_id,
676 : : const struct rte_eth_txconf *tx_conf)
677 : : {
678 : : struct sfc_repr_shared *srs = sfc_repr_shared_by_eth_dev(dev);
679 : : struct sfc_repr *sr = sfc_repr_by_eth_dev(dev);
680 : : struct sfc_repr_txq *txq;
681 : : int ret;
682 : :
683 : 0 : sfcr_info(sr, "entry");
684 : :
685 : 0 : ret = sfc_repr_tx_qcheck_conf(sr, tx_conf);
686 [ # # ]: 0 : if (ret != 0)
687 : 0 : goto fail_check_conf;
688 : :
689 : : ret = -ENOMEM;
690 : 0 : txq = rte_zmalloc_socket("sfc-repr-txq", sizeof(*txq),
691 : : RTE_CACHE_LINE_SIZE, socket_id);
692 [ # # ]: 0 : if (txq == NULL)
693 : 0 : goto fail_txq_alloc;
694 : :
695 : 0 : ret = sfc_repr_ring_create(srs->pf_port_id, srs->repr_id,
696 : : "tx", tx_queue_id, nb_tx_desc,
697 : : socket_id, &txq->ring);
698 [ # # ]: 0 : if (ret != 0)
699 : 0 : goto fail_ring_create;
700 : :
701 : 0 : ret = sfc_repr_proxy_add_txq(srs->pf_port_id, srs->repr_id,
702 : : tx_queue_id, txq->ring,
703 : : &txq->egress_mport);
704 [ # # ]: 0 : if (ret != 0)
705 : 0 : goto fail_proxy_add_txq;
706 : :
707 : 0 : dev->data->tx_queues[tx_queue_id] = txq;
708 : :
709 : 0 : sfcr_info(sr, "done");
710 : :
711 : 0 : return 0;
712 : :
713 : : fail_proxy_add_txq:
714 : 0 : rte_ring_free(txq->ring);
715 : :
716 : 0 : fail_ring_create:
717 : 0 : rte_free(txq);
718 : :
719 : 0 : fail_txq_alloc:
720 : 0 : fail_check_conf:
721 : 0 : sfcr_err(sr, "%s() failed: %s", __func__, rte_strerror(-ret));
722 : 0 : return ret;
723 : : }
724 : :
725 : : static void
726 : 0 : sfc_repr_tx_queue_release(struct rte_eth_dev *dev, uint16_t tx_queue_id)
727 : : {
728 : : struct sfc_repr_shared *srs = sfc_repr_shared_by_eth_dev(dev);
729 : 0 : struct sfc_repr_txq *txq = dev->data->tx_queues[tx_queue_id];
730 : :
731 : 0 : sfc_repr_proxy_del_txq(srs->pf_port_id, srs->repr_id, tx_queue_id);
732 : 0 : rte_ring_free(txq->ring);
733 : 0 : rte_free(txq);
734 : 0 : }
735 : :
736 : : static void
737 : : sfc_repr_close(struct sfc_repr *sr)
738 : : {
739 : : SFC_ASSERT(sfc_repr_lock_is_locked(sr));
740 : : SFC_ASSERT(sr->state == SFC_ETHDEV_CONFIGURED);
741 : :
742 : 0 : sr->state = SFC_ETHDEV_INITIALIZED;
743 : 0 : }
744 : :
745 : : static int
746 : 0 : sfc_repr_dev_close(struct rte_eth_dev *dev)
747 : : {
748 : : struct sfc_repr *sr = sfc_repr_by_eth_dev(dev);
749 : : struct sfc_repr_shared *srs = sfc_repr_shared_by_eth_dev(dev);
750 : : unsigned int i;
751 : :
752 : 0 : sfcr_info(sr, "entry");
753 : :
754 : 0 : sfc_repr_lock(sr);
755 [ # # # # ]: 0 : switch (sr->state) {
756 : 0 : case SFC_ETHDEV_STARTED:
757 : 0 : sfc_repr_stop(dev);
758 : : SFC_ASSERT(sr->state == SFC_ETHDEV_CONFIGURED);
759 : : /* FALLTHROUGH */
760 : 0 : case SFC_ETHDEV_CONFIGURED:
761 : : sfc_repr_close(sr);
762 : : SFC_ASSERT(sr->state == SFC_ETHDEV_INITIALIZED);
763 : : /* FALLTHROUGH */
764 : : case SFC_ETHDEV_INITIALIZED:
765 : : break;
766 : 0 : default:
767 : 0 : sfcr_err(sr, "unexpected adapter state %u on close", sr->state);
768 : 0 : break;
769 : : }
770 : :
771 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++) {
772 : 0 : sfc_repr_rx_queue_release(dev, i);
773 : 0 : dev->data->rx_queues[i] = NULL;
774 : : }
775 : :
776 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++) {
777 : 0 : sfc_repr_tx_queue_release(dev, i);
778 : 0 : dev->data->tx_queues[i] = NULL;
779 : : }
780 : :
781 : : /*
782 : : * Cleanup all resources.
783 : : * Rollback primary process sfc_repr_eth_dev_init() below.
784 : : */
785 : :
786 : 0 : (void)sfc_repr_proxy_del_port(srs->pf_port_id, srs->repr_id);
787 : :
788 : 0 : sfc_mae_clear_switch_port(srs->switch_domain_id, srs->switch_port_id);
789 : :
790 : : sfc_repr_unlock(sr);
791 : : sfc_repr_lock_fini(sr);
792 : :
793 : 0 : sfcr_info(sr, "done");
794 : :
795 : 0 : free(sr);
796 : :
797 : 0 : return 0;
798 : : }
799 : :
800 : : static int
801 : 0 : sfc_repr_mac_addr_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
802 : : {
803 : : struct sfc_repr_shared *srs = sfc_repr_shared_by_eth_dev(dev);
804 : : int ret;
805 : :
806 : 0 : ret = sfc_repr_proxy_repr_entity_mac_addr_set(srs->pf_port_id,
807 : 0 : srs->repr_id, mac_addr);
808 : 0 : return -ret;
809 : : }
810 : :
811 : : static int
812 : 0 : sfc_repr_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats,
813 : : struct eth_queue_stats *qstats __rte_unused)
814 : : {
815 : : union sfc_pkts_bytes queue_stats;
816 : : uint16_t i;
817 : :
818 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++) {
819 : 0 : struct sfc_repr_rxq *rxq = dev->data->rx_queues[i];
820 : :
821 : : sfc_pkts_bytes_get(&rxq->stats.packets_bytes,
822 : : &queue_stats);
823 : :
824 : 0 : stats->ipackets += queue_stats.pkts;
825 : 0 : stats->ibytes += queue_stats.bytes;
826 : : }
827 : :
828 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++) {
829 : 0 : struct sfc_repr_txq *txq = dev->data->tx_queues[i];
830 : :
831 : : sfc_pkts_bytes_get(&txq->stats.packets_bytes,
832 : : &queue_stats);
833 : :
834 : 0 : stats->opackets += queue_stats.pkts;
835 : 0 : stats->obytes += queue_stats.bytes;
836 : : }
837 : :
838 : 0 : return 0;
839 : : }
840 : :
841 : : static int
842 : 0 : sfc_repr_flow_pick_transfer_proxy(struct rte_eth_dev *dev,
843 : : uint16_t *transfer_proxy_port,
844 : : struct rte_flow_error *error)
845 : : {
846 : : struct sfc_repr_shared *srs = sfc_repr_shared_by_eth_dev(dev);
847 : :
848 : 0 : return rte_flow_pick_transfer_proxy(srs->pf_port_id,
849 : : transfer_proxy_port, error);
850 : : }
851 : :
852 : : const struct rte_flow_ops sfc_repr_flow_ops = {
853 : : .pick_transfer_proxy = sfc_repr_flow_pick_transfer_proxy,
854 : : };
855 : :
856 : : static int
857 : 0 : sfc_repr_dev_flow_ops_get(struct rte_eth_dev *dev __rte_unused,
858 : : const struct rte_flow_ops **ops)
859 : : {
860 : 0 : *ops = &sfc_repr_flow_ops;
861 : 0 : return 0;
862 : : }
863 : :
864 : : static const struct eth_dev_ops sfc_repr_dev_ops = {
865 : : .dev_configure = sfc_repr_dev_configure,
866 : : .dev_start = sfc_repr_dev_start,
867 : : .dev_stop = sfc_repr_dev_stop,
868 : : .dev_close = sfc_repr_dev_close,
869 : : .dev_infos_get = sfc_repr_dev_infos_get,
870 : : .link_update = sfc_repr_dev_link_update,
871 : : .mac_addr_set = sfc_repr_mac_addr_set,
872 : : .stats_get = sfc_repr_stats_get,
873 : : .rx_queue_setup = sfc_repr_rx_queue_setup,
874 : : .rx_queue_release = sfc_repr_rx_queue_release,
875 : : .tx_queue_setup = sfc_repr_tx_queue_setup,
876 : : .tx_queue_release = sfc_repr_tx_queue_release,
877 : : .flow_ops_get = sfc_repr_dev_flow_ops_get,
878 : : };
879 : :
880 : :
881 : : struct sfc_repr_init_data {
882 : : uint16_t pf_port_id;
883 : : uint16_t switch_domain_id;
884 : : efx_mport_sel_t mport_sel;
885 : : efx_pcie_interface_t intf;
886 : : uint16_t pf;
887 : : uint16_t vf;
888 : : unsigned int max_pdu;
889 : : };
890 : :
891 : : static int
892 : : sfc_repr_assign_mae_switch_port(uint16_t switch_domain_id,
893 : : const struct sfc_mae_switch_port_request *req,
894 : : uint16_t *switch_port_id)
895 : : {
896 : : int rc;
897 : :
898 : 0 : rc = sfc_mae_assign_switch_port(switch_domain_id, req, switch_port_id);
899 : :
900 : : SFC_ASSERT(rc >= 0);
901 : 0 : return -rc;
902 : : }
903 : :
904 : : static int
905 : 0 : sfc_repr_eth_dev_init(struct rte_eth_dev *dev, void *init_params)
906 : : {
907 : : const struct sfc_repr_init_data *repr_data = init_params;
908 : : struct sfc_repr_shared *srs = sfc_repr_shared_by_eth_dev(dev);
909 : : struct sfc_mae_switch_port_request switch_port_request;
910 : : efx_mport_sel_t ethdev_mport_sel;
911 : : efx_mport_id_t proxy_mport_id;
912 : : struct sfc_repr *sr;
913 : : int ret;
914 : :
915 : : /*
916 : : * For each representor, a driver-internal flow has to be installed
917 : : * in order to direct traffic coming from the represented entity to
918 : : * the "representor proxy". Such internal flows need to find ethdev
919 : : * mport by ethdev ID of the representors in question to specify in
920 : : * delivery action. So set the representor ethdev's mport to that
921 : : * of the "representor proxy" in below switch port request.
922 : : */
923 : 0 : sfc_repr_proxy_mport_alias_get(repr_data->pf_port_id, &proxy_mport_id);
924 : :
925 : 0 : ret = efx_mae_mport_by_id(&proxy_mport_id, ðdev_mport_sel);
926 [ # # ]: 0 : if (ret != 0) {
927 : 0 : SFC_GENERIC_LOG(ERR,
928 : : "%s() failed to get repr proxy mport by ID", __func__);
929 : 0 : goto fail_get_selector;
930 : : }
931 : :
932 : : memset(&switch_port_request, 0, sizeof(switch_port_request));
933 : 0 : switch_port_request.type = SFC_MAE_SWITCH_PORT_REPRESENTOR;
934 : 0 : switch_port_request.ethdev_mportp = ðdev_mport_sel;
935 : 0 : switch_port_request.entity_mportp = &repr_data->mport_sel;
936 : 0 : switch_port_request.ethdev_port_id = dev->data->port_id;
937 : 0 : switch_port_request.port_data.repr.intf = repr_data->intf;
938 : 0 : switch_port_request.port_data.repr.pf = repr_data->pf;
939 : 0 : switch_port_request.port_data.repr.vf = repr_data->vf;
940 : :
941 : 0 : ret = sfc_repr_assign_mae_switch_port(repr_data->switch_domain_id,
942 : : &switch_port_request,
943 : : &srs->switch_port_id);
944 [ # # ]: 0 : if (ret != 0) {
945 : 0 : SFC_GENERIC_LOG(ERR,
946 : : "%s() failed to assign MAE switch port (domain id %u)",
947 : : __func__, repr_data->switch_domain_id);
948 : 0 : goto fail_mae_assign_switch_port;
949 : : }
950 : :
951 : 0 : ret = sfc_repr_proxy_add_port(repr_data->pf_port_id,
952 : 0 : srs->switch_port_id, dev->data->port_id,
953 : 0 : &repr_data->mport_sel, repr_data->intf,
954 : 0 : repr_data->pf, repr_data->vf);
955 [ # # ]: 0 : if (ret != 0) {
956 : 0 : SFC_GENERIC_LOG(ERR, "%s() failed to add repr proxy port",
957 : : __func__);
958 : : SFC_ASSERT(ret > 0);
959 : 0 : ret = -ret;
960 : 0 : goto fail_create_port;
961 : : }
962 : :
963 : : /*
964 : : * Allocate process private data from heap, since it should not
965 : : * be located in shared memory allocated using rte_malloc() API.
966 : : */
967 : 0 : sr = calloc(1, sizeof(*sr));
968 [ # # ]: 0 : if (sr == NULL) {
969 : : ret = -ENOMEM;
970 : 0 : goto fail_alloc_sr;
971 : : }
972 : :
973 : : sfc_repr_lock_init(sr);
974 : 0 : sfc_repr_lock(sr);
975 : :
976 : 0 : dev->process_private = sr;
977 : :
978 : 0 : srs->pf_port_id = repr_data->pf_port_id;
979 : 0 : srs->repr_id = srs->switch_port_id;
980 : 0 : srs->switch_domain_id = repr_data->switch_domain_id;
981 : 0 : srs->max_pdu = repr_data->max_pdu;
982 : :
983 : 0 : dev->data->dev_flags |= RTE_ETH_DEV_REPRESENTOR;
984 : 0 : dev->data->representor_id = srs->repr_id;
985 : 0 : dev->data->backer_port_id = srs->pf_port_id;
986 : :
987 : 0 : dev->data->mac_addrs = rte_zmalloc("sfcr", RTE_ETHER_ADDR_LEN, 0);
988 [ # # ]: 0 : if (dev->data->mac_addrs == NULL) {
989 : : ret = -ENOMEM;
990 : 0 : goto fail_mac_addrs;
991 : : }
992 : :
993 : 0 : rte_eth_random_addr(dev->data->mac_addrs[0].addr_bytes);
994 : :
995 : 0 : ret = sfc_repr_proxy_repr_entity_mac_addr_set(repr_data->pf_port_id,
996 : 0 : srs->repr_id,
997 : 0 : &dev->data->mac_addrs[0]);
998 [ # # ]: 0 : if (ret != 0) {
999 : 0 : ret = -ret;
1000 : 0 : goto fail_mac_addr_set;
1001 : : }
1002 : :
1003 : 0 : dev->rx_pkt_burst = sfc_repr_rx_burst;
1004 : 0 : dev->tx_pkt_burst = sfc_repr_tx_burst;
1005 : 0 : dev->dev_ops = &sfc_repr_dev_ops;
1006 : :
1007 : 0 : sr->state = SFC_ETHDEV_INITIALIZED;
1008 : : sfc_repr_unlock(sr);
1009 : :
1010 : 0 : return 0;
1011 : :
1012 : : fail_mac_addr_set:
1013 : 0 : fail_mac_addrs:
1014 : : sfc_repr_unlock(sr);
1015 : 0 : free(sr);
1016 : :
1017 : 0 : fail_alloc_sr:
1018 : 0 : (void)sfc_repr_proxy_del_port(repr_data->pf_port_id,
1019 : 0 : srs->switch_port_id);
1020 : :
1021 : 0 : fail_create_port:
1022 : 0 : fail_mae_assign_switch_port:
1023 : 0 : fail_get_selector:
1024 : 0 : SFC_GENERIC_LOG(ERR, "%s() failed: %s", __func__, rte_strerror(-ret));
1025 : 0 : return ret;
1026 : : }
1027 : :
1028 : : int
1029 : 0 : sfc_repr_create(struct rte_eth_dev *parent,
1030 : : struct sfc_repr_entity_info *entity,
1031 : : uint16_t switch_domain_id,
1032 : : unsigned int max_pdu,
1033 : : const efx_mport_sel_t *mport_sel)
1034 : : {
1035 : : struct sfc_repr_init_data repr_data;
1036 : : char name[RTE_ETH_NAME_MAX_LEN];
1037 : : int controller;
1038 : : int ret;
1039 : : int rc;
1040 : : struct rte_eth_dev *dev;
1041 : :
1042 : 0 : controller = -1;
1043 : 0 : rc = sfc_mae_switch_domain_get_controller(switch_domain_id,
1044 : : entity->intf, &controller);
1045 [ # # ]: 0 : if (rc != 0) {
1046 : 0 : SFC_GENERIC_LOG(ERR, "%s() failed to get DPDK controller for %d",
1047 : : __func__, entity->intf);
1048 : 0 : return -rc;
1049 : : }
1050 : :
1051 [ # # # ]: 0 : switch (entity->type) {
1052 : 0 : case RTE_ETH_REPRESENTOR_VF:
1053 : 0 : ret = snprintf(name, sizeof(name), "net_%s_representor_c%upf%uvf%u",
1054 : 0 : parent->device->name, controller, entity->pf,
1055 : 0 : entity->vf);
1056 : 0 : break;
1057 : 0 : case RTE_ETH_REPRESENTOR_PF:
1058 : 0 : ret = snprintf(name, sizeof(name), "net_%s_representor_c%upf%u",
1059 : 0 : parent->device->name, controller, entity->pf);
1060 : 0 : break;
1061 : : default:
1062 : : return -ENOTSUP;
1063 : : }
1064 : :
1065 [ # # ]: 0 : if (ret >= (int)sizeof(name)) {
1066 : 0 : SFC_GENERIC_LOG(ERR, "%s() failed name too long", __func__);
1067 : 0 : return -ENAMETOOLONG;
1068 : : }
1069 : :
1070 : 0 : dev = rte_eth_dev_allocated(name);
1071 [ # # ]: 0 : if (dev == NULL) {
1072 : : memset(&repr_data, 0, sizeof(repr_data));
1073 : 0 : repr_data.pf_port_id = parent->data->port_id;
1074 : 0 : repr_data.switch_domain_id = switch_domain_id;
1075 : 0 : repr_data.mport_sel = *mport_sel;
1076 : 0 : repr_data.intf = entity->intf;
1077 : 0 : repr_data.pf = entity->pf;
1078 : 0 : repr_data.vf = entity->vf;
1079 : 0 : repr_data.max_pdu = max_pdu;
1080 : :
1081 : 0 : ret = rte_eth_dev_create(parent->device, name,
1082 : : sizeof(struct sfc_repr_shared),
1083 : : NULL, NULL,
1084 : : sfc_repr_eth_dev_init, &repr_data);
1085 [ # # ]: 0 : if (ret != 0) {
1086 : 0 : SFC_GENERIC_LOG(ERR, "%s() failed to create device",
1087 : : __func__);
1088 : 0 : return ret;
1089 : : }
1090 : : }
1091 : :
1092 : : return 0;
1093 : : }
|