Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2019 Intel Corporation.
3 : : */
4 : : #include <stdint.h>
5 : : #include <stdio.h>
6 : : #include <string.h>
7 : : #include <errno.h>
8 : :
9 : : #include <eal_export.h>
10 : : #include <rte_common.h>
11 : : #include <rte_lcore.h>
12 : : #include <rte_cycles.h>
13 : : #include <rte_eal.h>
14 : : #include <rte_log.h>
15 : : #include <rte_pci.h>
16 : : #include <rte_mbuf.h>
17 : : #include <bus_pci_driver.h>
18 : : #include <rte_memzone.h>
19 : : #include <rte_memcpy.h>
20 : : #include <rte_rawdev.h>
21 : : #include <rte_rawdev_pmd.h>
22 : :
23 : : #include "ntb_hw_amd.h"
24 : : #include "ntb_hw_intel.h"
25 : : #include "rte_pmd_ntb.h"
26 : : #include "ntb.h"
27 : :
28 : : static const struct rte_pci_id pci_id_ntb_map[] = {
29 : : { RTE_PCI_DEVICE(NTB_AMD_VENDOR_ID, NTB_AMD_DEV_ID_PRI) },
30 : : { RTE_PCI_DEVICE(NTB_AMD_VENDOR_ID, NTB_AMD_DEV_ID_SEC) },
31 : : { RTE_PCI_DEVICE(NTB_INTEL_VENDOR_ID, NTB_INTEL_DEV_ID_B2B_SKX) },
32 : : { RTE_PCI_DEVICE(NTB_INTEL_VENDOR_ID, NTB_INTEL_DEV_ID_B2B_ICX) },
33 : : { .vendor_id = 0, /* sentinel */ },
34 : : };
35 : :
36 : : /* Align with enum ntb_xstats_idx */
37 : : static struct rte_rawdev_xstats_name ntb_xstats_names[] = {
38 : : {"Tx-packets"},
39 : : {"Tx-bytes"},
40 : : {"Tx-errors"},
41 : : {"Rx-packets"},
42 : : {"Rx-bytes"},
43 : : {"Rx-missed"},
44 : : };
45 : : #define NTB_XSTATS_NUM RTE_DIM(ntb_xstats_names)
46 : :
47 : : static void
48 : 0 : ntb_dev_intr_handler(void *param)
49 : : {
50 : : struct rte_rawdev *dev = (struct rte_rawdev *)param;
51 : 0 : struct ntb_hw *hw = dev->dev_private;
52 : : uint64_t db_bits;
53 : :
54 : 0 : db_bits = (*hw->ntb_ops->db_read)(dev);
55 [ # # ]: 0 : if (!db_bits)
56 : 0 : NTB_LOG(ERR, "No doorbells");
57 : :
58 : : /* Doorbell 0 is for peer device ready. */
59 [ # # ]: 0 : if (db_bits & 1) {
60 : 0 : NTB_LOG(INFO, "DB0: Peer device is up.");
61 : : /* Clear received doorbell. */
62 : 0 : (*hw->ntb_ops->db_clear)(dev, 1);
63 : :
64 : : /**
65 : : * Peer dev is already up. All mw settings are already done.
66 : : * Skip them.
67 : : */
68 [ # # ]: 0 : if (hw->peer_dev_up)
69 : : return;
70 : :
71 : : /* Check that the peer memory-window layout matches local. */
72 [ # # ]: 0 : if ((*hw->ntb_ops->peer_mw_validate)(dev) < 0)
73 : : return;
74 : :
75 : 0 : hw->peer_dev_up = 1;
76 : :
77 : : /**
78 : : * Handshake with peer. Scratchpad and translation programming
79 : : * only takes effect when both devices are up, so run the
80 : : * handshake again on the doorbell and ring the peer so the
81 : : * device that came up second does not miss the first doorbell.
82 : : */
83 [ # # ]: 0 : if ((*hw->ntb_ops->dev_handshake)(dev) < 0) {
84 : 0 : NTB_LOG(ERR, "Handshake work failed.");
85 : 0 : return;
86 : : }
87 : :
88 : : /* To get the link info. */
89 : 0 : (*hw->ntb_ops->get_link_status)(dev);
90 : 0 : NTB_LOG(INFO, "Link is up. Link speed: %u. Link width: %u",
91 : : hw->link_speed, hw->link_width);
92 : 0 : return;
93 : : }
94 : :
95 [ # # ]: 0 : if (db_bits & (1 << 1)) {
96 : 0 : NTB_LOG(INFO, "DB1: Peer device is down.");
97 : : /* Clear received doorbell. */
98 : 0 : (*hw->ntb_ops->db_clear)(dev, 2);
99 : :
100 : : /* Peer device will be down, So clean local side too. */
101 : 0 : (*hw->ntb_ops->link_cleanup)(dev);
102 : :
103 : 0 : hw->peer_dev_up = 0;
104 : : /* Response peer's dev_stop request. */
105 : 0 : (*hw->ntb_ops->peer_db_set)(dev, 2);
106 : 0 : return;
107 : : }
108 : :
109 [ # # ]: 0 : if (db_bits & (1 << 2)) {
110 : 0 : NTB_LOG(INFO, "DB2: Peer device agrees dev to be down.");
111 : : /* Clear received doorbell. */
112 : 0 : (*hw->ntb_ops->db_clear)(dev, (1 << 2));
113 : 0 : hw->peer_dev_up = 0;
114 : 0 : return;
115 : : }
116 : :
117 : : /* Clear other received doorbells. */
118 : 0 : (*hw->ntb_ops->db_clear)(dev, db_bits);
119 : :
120 : : /* Let the vendor process any hardware-specific events. */
121 [ # # ]: 0 : if (hw->ntb_ops->handle_hw_event != NULL)
122 : 0 : (*hw->ntb_ops->handle_hw_event)(dev);
123 : : }
124 : :
125 : : static int
126 : 0 : ntb_queue_conf_get(struct rte_rawdev *dev,
127 : : uint16_t queue_id,
128 : : rte_rawdev_obj_t queue_conf,
129 : : size_t conf_size)
130 : : {
131 : : struct ntb_queue_conf *q_conf = queue_conf;
132 : 0 : struct ntb_hw *hw = dev->dev_private;
133 : :
134 [ # # ]: 0 : if (conf_size != sizeof(*q_conf))
135 : : return -EINVAL;
136 : :
137 : 0 : q_conf->tx_free_thresh = hw->tx_queues[queue_id]->tx_free_thresh;
138 : 0 : q_conf->nb_desc = hw->rx_queues[queue_id]->nb_rx_desc;
139 : 0 : q_conf->rx_mp = hw->rx_queues[queue_id]->mpool;
140 : :
141 : 0 : return 0;
142 : : }
143 : :
144 : : static void
145 : 0 : ntb_rxq_release_mbufs(struct ntb_rx_queue *q)
146 : : {
147 : : int i;
148 : :
149 [ # # # # ]: 0 : if (!q || !q->sw_ring) {
150 : 0 : NTB_LOG(ERR, "Pointer to rxq or sw_ring is NULL");
151 : 0 : return;
152 : : }
153 : :
154 [ # # ]: 0 : for (i = 0; i < q->nb_rx_desc; i++) {
155 [ # # ]: 0 : if (q->sw_ring[i].mbuf) {
156 : : rte_pktmbuf_free_seg(q->sw_ring[i].mbuf);
157 : 0 : q->sw_ring[i].mbuf = NULL;
158 : : }
159 : : }
160 : : }
161 : :
162 : : static void
163 : 0 : ntb_rxq_release(struct ntb_rx_queue *rxq)
164 : : {
165 [ # # ]: 0 : if (!rxq) {
166 : 0 : NTB_LOG(ERR, "Pointer to rxq is NULL");
167 : 0 : return;
168 : : }
169 : :
170 : 0 : ntb_rxq_release_mbufs(rxq);
171 : :
172 : 0 : rte_free(rxq->sw_ring);
173 : 0 : rte_free(rxq);
174 : : }
175 : :
176 : : static int
177 : 0 : ntb_rxq_setup(struct rte_rawdev *dev,
178 : : uint16_t qp_id,
179 : : rte_rawdev_obj_t queue_conf,
180 : : size_t conf_size)
181 : : {
182 : : struct ntb_queue_conf *rxq_conf = queue_conf;
183 : 0 : struct ntb_hw *hw = dev->dev_private;
184 : : struct ntb_rx_queue *rxq;
185 : :
186 [ # # ]: 0 : if (conf_size != sizeof(*rxq_conf))
187 : : return -EINVAL;
188 : :
189 : : /* Allocate the rx queue data structure */
190 : 0 : rxq = rte_zmalloc_socket("ntb rx queue",
191 : : sizeof(struct ntb_rx_queue),
192 : : RTE_CACHE_LINE_SIZE,
193 : : dev->socket_id);
194 [ # # ]: 0 : if (!rxq) {
195 : 0 : NTB_LOG(ERR, "Failed to allocate memory for "
196 : : "rx queue data structure.");
197 : 0 : return -ENOMEM;
198 : : }
199 : :
200 [ # # ]: 0 : if (rxq_conf->rx_mp == NULL) {
201 : 0 : NTB_LOG(ERR, "Invalid null mempool pointer.");
202 : 0 : return -EINVAL;
203 : : }
204 : 0 : rxq->nb_rx_desc = rxq_conf->nb_desc;
205 : 0 : rxq->mpool = rxq_conf->rx_mp;
206 : 0 : rxq->port_id = dev->dev_id;
207 : 0 : rxq->queue_id = qp_id;
208 : 0 : rxq->hw = hw;
209 : :
210 : : /* Allocate the software ring. */
211 : 0 : rxq->sw_ring =
212 : 0 : rte_zmalloc_socket("ntb rx sw ring",
213 : : sizeof(struct ntb_rx_entry) *
214 : 0 : rxq->nb_rx_desc,
215 : : RTE_CACHE_LINE_SIZE,
216 : : dev->socket_id);
217 [ # # ]: 0 : if (!rxq->sw_ring) {
218 : 0 : ntb_rxq_release(rxq);
219 : : rxq = NULL;
220 : 0 : NTB_LOG(ERR, "Failed to allocate memory for SW ring");
221 : 0 : return -ENOMEM;
222 : : }
223 : :
224 : 0 : hw->rx_queues[qp_id] = rxq;
225 : :
226 : 0 : return 0;
227 : : }
228 : :
229 : : static void
230 : 0 : ntb_txq_release_mbufs(struct ntb_tx_queue *q)
231 : : {
232 : : int i;
233 : :
234 [ # # # # ]: 0 : if (!q || !q->sw_ring) {
235 : 0 : NTB_LOG(ERR, "Pointer to txq or sw_ring is NULL");
236 : 0 : return;
237 : : }
238 : :
239 [ # # ]: 0 : for (i = 0; i < q->nb_tx_desc; i++) {
240 [ # # ]: 0 : if (q->sw_ring[i].mbuf) {
241 : : rte_pktmbuf_free_seg(q->sw_ring[i].mbuf);
242 : 0 : q->sw_ring[i].mbuf = NULL;
243 : : }
244 : : }
245 : : }
246 : :
247 : : static void
248 : 0 : ntb_txq_release(struct ntb_tx_queue *txq)
249 : : {
250 [ # # ]: 0 : if (!txq) {
251 : 0 : NTB_LOG(ERR, "Pointer to txq is NULL");
252 : 0 : return;
253 : : }
254 : :
255 : 0 : ntb_txq_release_mbufs(txq);
256 : :
257 : 0 : rte_free(txq->sw_ring);
258 : 0 : rte_free(txq);
259 : : }
260 : :
261 : : static int
262 : 0 : ntb_txq_setup(struct rte_rawdev *dev,
263 : : uint16_t qp_id,
264 : : rte_rawdev_obj_t queue_conf,
265 : : size_t conf_size)
266 : : {
267 : : struct ntb_queue_conf *txq_conf = queue_conf;
268 : 0 : struct ntb_hw *hw = dev->dev_private;
269 : : struct ntb_tx_queue *txq;
270 : : uint16_t i, prev;
271 : :
272 [ # # ]: 0 : if (conf_size != sizeof(*txq_conf))
273 : : return -EINVAL;
274 : :
275 : : /* Allocate the TX queue data structure. */
276 : 0 : txq = rte_zmalloc_socket("ntb tx queue",
277 : : sizeof(struct ntb_tx_queue),
278 : : RTE_CACHE_LINE_SIZE,
279 : : dev->socket_id);
280 [ # # ]: 0 : if (!txq) {
281 : 0 : NTB_LOG(ERR, "Failed to allocate memory for "
282 : : "tx queue structure");
283 : 0 : return -ENOMEM;
284 : : }
285 : :
286 : 0 : txq->nb_tx_desc = txq_conf->nb_desc;
287 : 0 : txq->port_id = dev->dev_id;
288 : 0 : txq->queue_id = qp_id;
289 : 0 : txq->hw = hw;
290 : :
291 : : /* Allocate software ring */
292 : 0 : txq->sw_ring =
293 : 0 : rte_zmalloc_socket("ntb tx sw ring",
294 : : sizeof(struct ntb_tx_entry) *
295 : 0 : txq->nb_tx_desc,
296 : : RTE_CACHE_LINE_SIZE,
297 : : dev->socket_id);
298 [ # # ]: 0 : if (!txq->sw_ring) {
299 : 0 : ntb_txq_release(txq);
300 : : txq = NULL;
301 : 0 : NTB_LOG(ERR, "Failed to allocate memory for SW TX ring");
302 : 0 : return -ENOMEM;
303 : : }
304 : :
305 : 0 : prev = txq->nb_tx_desc - 1;
306 [ # # ]: 0 : for (i = 0; i < txq->nb_tx_desc; i++) {
307 : 0 : txq->sw_ring[i].mbuf = NULL;
308 : 0 : txq->sw_ring[i].last_id = i;
309 : 0 : txq->sw_ring[prev].next_id = i;
310 : : prev = i;
311 : : }
312 : :
313 [ # # ]: 0 : txq->tx_free_thresh = txq_conf->tx_free_thresh ?
314 : : txq_conf->tx_free_thresh :
315 : : NTB_DFLT_TX_FREE_THRESH;
316 [ # # ]: 0 : if (txq->tx_free_thresh >= txq->nb_tx_desc - 3) {
317 : 0 : NTB_LOG(ERR, "tx_free_thresh must be less than nb_desc - 3. "
318 : : "(tx_free_thresh=%u qp_id=%u)", txq->tx_free_thresh,
319 : : qp_id);
320 : 0 : return -EINVAL;
321 : : }
322 : :
323 : 0 : hw->tx_queues[qp_id] = txq;
324 : :
325 : 0 : return 0;
326 : : }
327 : :
328 : :
329 : : static int
330 : 0 : ntb_queue_setup(struct rte_rawdev *dev,
331 : : uint16_t queue_id,
332 : : rte_rawdev_obj_t queue_conf,
333 : : size_t conf_size)
334 : : {
335 : 0 : struct ntb_hw *hw = dev->dev_private;
336 : : int ret;
337 : :
338 [ # # ]: 0 : if (queue_id >= hw->queue_pairs)
339 : : return -EINVAL;
340 : :
341 : 0 : ret = ntb_txq_setup(dev, queue_id, queue_conf, conf_size);
342 [ # # ]: 0 : if (ret < 0)
343 : : return ret;
344 : :
345 : 0 : ret = ntb_rxq_setup(dev, queue_id, queue_conf, conf_size);
346 : :
347 : 0 : return ret;
348 : : }
349 : :
350 : : static int
351 : 0 : ntb_queue_release(struct rte_rawdev *dev, uint16_t queue_id)
352 : : {
353 : 0 : struct ntb_hw *hw = dev->dev_private;
354 : :
355 [ # # ]: 0 : if (queue_id >= hw->queue_pairs)
356 : : return -EINVAL;
357 : :
358 : 0 : ntb_txq_release(hw->tx_queues[queue_id]);
359 : 0 : hw->tx_queues[queue_id] = NULL;
360 : 0 : ntb_rxq_release(hw->rx_queues[queue_id]);
361 : 0 : hw->rx_queues[queue_id] = NULL;
362 : :
363 : 0 : return 0;
364 : : }
365 : :
366 : : static uint16_t
367 : 0 : ntb_queue_count(struct rte_rawdev *dev)
368 : : {
369 : 0 : struct ntb_hw *hw = dev->dev_private;
370 : 0 : return hw->queue_pairs;
371 : : }
372 : :
373 : : static int
374 : 0 : ntb_queue_init(struct rte_rawdev *dev, uint16_t qp_id)
375 : : {
376 : 0 : struct ntb_hw *hw = dev->dev_private;
377 : 0 : struct ntb_rx_queue *rxq = hw->rx_queues[qp_id];
378 : 0 : struct ntb_tx_queue *txq = hw->tx_queues[qp_id];
379 : : volatile struct ntb_header *local_hdr;
380 : : struct ntb_header *remote_hdr;
381 : 0 : uint16_t q_size = hw->queue_size;
382 : : uint32_t hdr_offset;
383 : : void *bar_addr;
384 : : uint16_t i;
385 : :
386 : : /* Put queue info into the start of shared memory. */
387 : 0 : hdr_offset = hw->hdr_size_per_queue * qp_id;
388 : 0 : local_hdr = (volatile struct ntb_header *)
389 : 0 : ((size_t)hw->mz[0]->addr + hdr_offset);
390 : 0 : bar_addr = (*hw->ntb_ops->get_peer_mw_addr)(dev, 0);
391 [ # # ]: 0 : if (bar_addr == NULL)
392 : : return -EINVAL;
393 : 0 : remote_hdr = (struct ntb_header *)
394 : 0 : ((size_t)bar_addr + hdr_offset);
395 : :
396 : : /* rxq init. */
397 : 0 : rxq->rx_desc_ring = (struct ntb_desc *)
398 : 0 : (&remote_hdr->desc_ring);
399 : 0 : rxq->rx_used_ring = (volatile struct ntb_used *)
400 : 0 : (&local_hdr->desc_ring[q_size]);
401 : 0 : rxq->avail_cnt = &remote_hdr->avail_cnt;
402 : 0 : rxq->used_cnt = &local_hdr->used_cnt;
403 : :
404 [ # # ]: 0 : for (i = 0; i < rxq->nb_rx_desc - 1; i++) {
405 : 0 : struct rte_mbuf *mbuf = rte_mbuf_raw_alloc(rxq->mpool);
406 [ # # ]: 0 : if (unlikely(!mbuf)) {
407 : 0 : NTB_LOG(ERR, "Failed to allocate mbuf for RX");
408 : 0 : return -ENOMEM;
409 : : }
410 : 0 : mbuf->port = dev->dev_id;
411 : :
412 : 0 : rxq->sw_ring[i].mbuf = mbuf;
413 : :
414 : 0 : rxq->rx_desc_ring[i].addr = rte_pktmbuf_mtod(mbuf, size_t);
415 : 0 : rxq->rx_desc_ring[i].len = mbuf->buf_len - RTE_PKTMBUF_HEADROOM;
416 : : }
417 : : rte_wmb();
418 : 0 : *rxq->avail_cnt = rxq->nb_rx_desc - 1;
419 : 0 : rxq->last_avail = rxq->nb_rx_desc - 1;
420 : 0 : rxq->last_used = 0;
421 : :
422 : : /* txq init */
423 : 0 : txq->tx_desc_ring = (volatile struct ntb_desc *)
424 : 0 : (&local_hdr->desc_ring);
425 : 0 : txq->tx_used_ring = (struct ntb_used *)
426 : 0 : (&remote_hdr->desc_ring[q_size]);
427 : 0 : txq->avail_cnt = &local_hdr->avail_cnt;
428 : 0 : txq->used_cnt = &remote_hdr->used_cnt;
429 : :
430 : : rte_wmb();
431 : 0 : *txq->used_cnt = 0;
432 : 0 : txq->last_used = 0;
433 : 0 : txq->last_avail = 0;
434 : 0 : txq->nb_tx_free = txq->nb_tx_desc - 1;
435 : :
436 : : /* Set per queue stats. */
437 [ # # ]: 0 : for (i = 0; i < NTB_XSTATS_NUM; i++) {
438 : 0 : hw->ntb_xstats[i + NTB_XSTATS_NUM * (qp_id + 1)] = 0;
439 : 0 : hw->ntb_xstats_off[i + NTB_XSTATS_NUM * (qp_id + 1)] = 0;
440 : : }
441 : :
442 : : return 0;
443 : : }
444 : :
445 : : static inline void
446 : 0 : ntb_enqueue_cleanup(struct ntb_tx_queue *txq)
447 : : {
448 : 0 : struct ntb_tx_entry *sw_ring = txq->sw_ring;
449 : 0 : uint16_t tx_free = txq->last_avail;
450 : : uint16_t nb_to_clean, i;
451 : :
452 : : /* avail_cnt + 1 represents where to rx next in the peer. */
453 : 0 : nb_to_clean = (*txq->avail_cnt - txq->last_avail + 1 +
454 : 0 : txq->nb_tx_desc) & (txq->nb_tx_desc - 1);
455 : 0 : nb_to_clean = RTE_MIN(nb_to_clean, txq->tx_free_thresh);
456 [ # # ]: 0 : for (i = 0; i < nb_to_clean; i++) {
457 [ # # ]: 0 : if (sw_ring[tx_free].mbuf)
458 : : rte_pktmbuf_free_seg(sw_ring[tx_free].mbuf);
459 : 0 : tx_free = (tx_free + 1) & (txq->nb_tx_desc - 1);
460 : : }
461 : :
462 : 0 : txq->nb_tx_free += nb_to_clean;
463 : 0 : txq->last_avail = tx_free;
464 : 0 : }
465 : :
466 : : static int
467 : 0 : ntb_enqueue_bufs(struct rte_rawdev *dev,
468 : : struct rte_rawdev_buf **buffers,
469 : : unsigned int count,
470 : : rte_rawdev_obj_t context)
471 : : {
472 : 0 : struct ntb_hw *hw = dev->dev_private;
473 : 0 : struct ntb_tx_queue *txq = hw->tx_queues[(size_t)context];
474 : 0 : struct ntb_tx_entry *sw_ring = txq->sw_ring;
475 : : struct rte_mbuf *txm;
476 : : struct ntb_used tx_used[NTB_MAX_DESC_SIZE];
477 : : volatile struct ntb_desc *tx_item;
478 : : uint16_t tx_last, nb_segs, off, last_used, avail_cnt;
479 : : uint16_t nb_mbufs = 0;
480 : : uint16_t nb_tx = 0;
481 : : uint64_t bytes = 0;
482 : : void *buf_addr;
483 : : int i;
484 : :
485 [ # # # # ]: 0 : if (unlikely(dev->started == 0 || hw->peer_dev_up == 0)) {
486 : 0 : NTB_LOG(DEBUG, "Link is not up.");
487 : 0 : return nb_tx;
488 : : }
489 : :
490 [ # # ]: 0 : if (txq->nb_tx_free < txq->tx_free_thresh)
491 : 0 : ntb_enqueue_cleanup(txq);
492 : :
493 : 0 : off = NTB_XSTATS_NUM * ((size_t)context + 1);
494 : 0 : last_used = txq->last_used;
495 : 0 : avail_cnt = *txq->avail_cnt;/* Where to alloc next. */
496 [ # # ]: 0 : for (nb_tx = 0; nb_tx < count; nb_tx++) {
497 : 0 : txm = (struct rte_mbuf *)(buffers[nb_tx]->buf_addr);
498 [ # # # # ]: 0 : if (txm == NULL || txq->nb_tx_free < txm->nb_segs)
499 : : break;
500 : :
501 : 0 : tx_last = (txq->last_used + txm->nb_segs - 1) &
502 : 0 : (txq->nb_tx_desc - 1);
503 : : nb_segs = txm->nb_segs;
504 [ # # ]: 0 : for (i = 0; i < nb_segs; i++) {
505 : : /* Not enough ring space for tx. */
506 [ # # ]: 0 : if (txq->last_used == avail_cnt)
507 : 0 : goto end_of_tx;
508 : 0 : sw_ring[txq->last_used].mbuf = txm;
509 : 0 : tx_item = txq->tx_desc_ring + txq->last_used;
510 : :
511 [ # # ]: 0 : if (!tx_item->len) {
512 : 0 : (hw->ntb_xstats[NTB_TX_ERRS_ID + off])++;
513 : 0 : goto end_of_tx;
514 : : }
515 [ # # ]: 0 : if (txm->data_len > tx_item->len) {
516 : 0 : NTB_LOG(ERR, "Data length exceeds buf length."
517 : : " Only %u data would be transmitted.",
518 : : tx_item->len);
519 : 0 : txm->data_len = tx_item->len;
520 : : }
521 : :
522 : : /* translate remote virtual addr to bar virtual addr */
523 : 0 : buf_addr = (*hw->ntb_ops->ioremap)(dev, tx_item->addr);
524 [ # # ]: 0 : if (buf_addr == NULL) {
525 : 0 : (hw->ntb_xstats[NTB_TX_ERRS_ID + off])++;
526 : 0 : NTB_LOG(ERR, "Null remap addr.");
527 : 0 : goto end_of_tx;
528 : : }
529 : 0 : rte_memcpy(buf_addr, rte_pktmbuf_mtod(txm, void *),
530 [ # # ]: 0 : txm->data_len);
531 : :
532 : 0 : tx_used[nb_mbufs].len = txm->data_len;
533 : 0 : tx_used[nb_mbufs++].flags = (txq->last_used ==
534 : : tx_last) ?
535 : 0 : NTB_FLAG_EOP : 0;
536 : :
537 : : /* update stats */
538 : 0 : bytes += txm->data_len;
539 : :
540 : 0 : txm = txm->next;
541 : :
542 : 0 : sw_ring[txq->last_used].next_id = (txq->last_used + 1) &
543 : 0 : (txq->nb_tx_desc - 1);
544 : 0 : sw_ring[txq->last_used].last_id = tx_last;
545 : 0 : txq->last_used = (txq->last_used + 1) &
546 : : (txq->nb_tx_desc - 1);
547 : : }
548 : 0 : txq->nb_tx_free -= nb_segs;
549 : : }
550 : :
551 : 0 : end_of_tx:
552 [ # # ]: 0 : if (nb_tx) {
553 : : uint16_t nb1, nb2;
554 [ # # ]: 0 : if (nb_mbufs > txq->nb_tx_desc - last_used) {
555 : 0 : nb1 = txq->nb_tx_desc - last_used;
556 : 0 : nb2 = nb_mbufs - txq->nb_tx_desc + last_used;
557 : : } else {
558 : : nb1 = nb_mbufs;
559 : : nb2 = 0;
560 : : }
561 [ # # ]: 0 : rte_memcpy(txq->tx_used_ring + last_used, tx_used,
562 : : sizeof(struct ntb_used) * nb1);
563 [ # # ]: 0 : rte_memcpy(txq->tx_used_ring, tx_used + nb1,
564 : : sizeof(struct ntb_used) * nb2);
565 : : rte_wmb();
566 : 0 : *txq->used_cnt = txq->last_used;
567 : :
568 : : /* update queue stats */
569 : 0 : hw->ntb_xstats[NTB_TX_BYTES_ID + off] += bytes;
570 : 0 : hw->ntb_xstats[NTB_TX_PKTS_ID + off] += nb_tx;
571 : : }
572 : :
573 : 0 : return nb_tx;
574 : : }
575 : :
576 : : static int
577 : 0 : ntb_dequeue_bufs(struct rte_rawdev *dev,
578 : : struct rte_rawdev_buf **buffers,
579 : : unsigned int count,
580 : : rte_rawdev_obj_t context)
581 : : {
582 : 0 : struct ntb_hw *hw = dev->dev_private;
583 : 0 : struct ntb_rx_queue *rxq = hw->rx_queues[(size_t)context];
584 : 0 : struct ntb_rx_entry *sw_ring = rxq->sw_ring;
585 : : struct ntb_desc rx_desc[NTB_MAX_DESC_SIZE];
586 : : struct rte_mbuf *first, *rxm_t;
587 : : struct rte_mbuf *prev = NULL;
588 : : volatile struct ntb_used *rx_item;
589 : : uint16_t nb_mbufs = 0;
590 : : uint16_t nb_rx = 0;
591 : : uint64_t bytes = 0;
592 : : uint16_t off, last_avail, used_cnt, used_nb;
593 : : int i;
594 : :
595 [ # # # # ]: 0 : if (unlikely(dev->started == 0 || hw->peer_dev_up == 0)) {
596 : 0 : NTB_LOG(DEBUG, "Link is not up");
597 : 0 : return nb_rx;
598 : : }
599 : :
600 : 0 : used_cnt = *rxq->used_cnt;
601 : :
602 [ # # ]: 0 : if (rxq->last_used == used_cnt)
603 : : return nb_rx;
604 : :
605 : 0 : last_avail = rxq->last_avail;
606 : 0 : used_nb = (used_cnt - rxq->last_used) & (rxq->nb_rx_desc - 1);
607 : 0 : count = RTE_MIN(count, used_nb);
608 [ # # ]: 0 : for (nb_rx = 0; nb_rx < count; nb_rx++) {
609 : : i = 0;
610 : : while (true) {
611 [ # # ]: 0 : if (unlikely(nb_mbufs >= rxq->nb_rx_desc)) {
612 : 0 : NTB_LOG(ERR, "Malformed rx stream (no EOP); "
613 : : "aborting to avoid desc overflow.");
614 : 0 : goto end_of_rx;
615 : : }
616 : 0 : rx_item = rxq->rx_used_ring + rxq->last_used;
617 : 0 : rxm_t = sw_ring[rxq->last_used].mbuf;
618 : 0 : rxm_t->data_len = rx_item->len;
619 : 0 : rxm_t->data_off = RTE_PKTMBUF_HEADROOM;
620 : 0 : rxm_t->port = rxq->port_id;
621 : :
622 [ # # ]: 0 : if (!i) {
623 : 0 : rxm_t->nb_segs = 1;
624 : : first = rxm_t;
625 : 0 : first->pkt_len = 0;
626 : 0 : buffers[nb_rx]->buf_addr = rxm_t;
627 : : } else {
628 : 0 : prev->next = rxm_t;
629 : 0 : first->nb_segs++;
630 : : }
631 : :
632 : : prev = rxm_t;
633 : 0 : first->pkt_len += prev->data_len;
634 : 0 : rxq->last_used = (rxq->last_used + 1) &
635 : 0 : (rxq->nb_rx_desc - 1);
636 : :
637 : : /* alloc new mbuf */
638 : 0 : rxm_t = rte_mbuf_raw_alloc(rxq->mpool);
639 [ # # ]: 0 : if (unlikely(rxm_t == NULL)) {
640 : 0 : NTB_LOG(ERR, "recv alloc mbuf failed.");
641 : 0 : goto end_of_rx;
642 : : }
643 : 0 : rxm_t->port = rxq->port_id;
644 : 0 : sw_ring[rxq->last_avail].mbuf = rxm_t;
645 : 0 : i++;
646 : :
647 : : /* fill new desc */
648 : 0 : rx_desc[nb_mbufs].addr =
649 : 0 : rte_pktmbuf_mtod(rxm_t, size_t);
650 : 0 : rx_desc[nb_mbufs++].len = rxm_t->buf_len -
651 : : RTE_PKTMBUF_HEADROOM;
652 : 0 : rxq->last_avail = (rxq->last_avail + 1) &
653 : 0 : (rxq->nb_rx_desc - 1);
654 : :
655 [ # # ]: 0 : if (rx_item->flags & NTB_FLAG_EOP)
656 : : break;
657 : : }
658 : : /* update stats */
659 : 0 : bytes += first->pkt_len;
660 : : }
661 : :
662 : 0 : end_of_rx:
663 [ # # ]: 0 : if (nb_rx) {
664 : : uint16_t nb1, nb2;
665 [ # # ]: 0 : if (nb_mbufs > rxq->nb_rx_desc - last_avail) {
666 : 0 : nb1 = rxq->nb_rx_desc - last_avail;
667 : 0 : nb2 = nb_mbufs - rxq->nb_rx_desc + last_avail;
668 : : } else {
669 : : nb1 = nb_mbufs;
670 : : nb2 = 0;
671 : : }
672 [ # # ]: 0 : rte_memcpy(rxq->rx_desc_ring + last_avail, rx_desc,
673 : : sizeof(struct ntb_desc) * nb1);
674 [ # # ]: 0 : rte_memcpy(rxq->rx_desc_ring, rx_desc + nb1,
675 : : sizeof(struct ntb_desc) * nb2);
676 : : rte_wmb();
677 : 0 : *rxq->avail_cnt = rxq->last_avail;
678 : :
679 : : /* update queue stats */
680 : 0 : off = NTB_XSTATS_NUM * ((size_t)context + 1);
681 : 0 : hw->ntb_xstats[NTB_RX_BYTES_ID + off] += bytes;
682 : 0 : hw->ntb_xstats[NTB_RX_PKTS_ID + off] += nb_rx;
683 : 0 : hw->ntb_xstats[NTB_RX_MISS_ID + off] += (count - nb_rx);
684 : : }
685 : :
686 : 0 : return nb_rx;
687 : : }
688 : :
689 : : static int
690 : 0 : ntb_dev_info_get(struct rte_rawdev *dev, rte_rawdev_obj_t dev_info,
691 : : size_t dev_info_size)
692 : : {
693 : 0 : struct ntb_hw *hw = dev->dev_private;
694 : : struct ntb_dev_info *info = dev_info;
695 : :
696 [ # # ]: 0 : if (dev_info_size != sizeof(*info)) {
697 : 0 : NTB_LOG(ERR, "Invalid size parameter to %s", __func__);
698 : 0 : return -EINVAL;
699 : : }
700 : :
701 : 0 : info->mw_cnt = hw->mw_cnt;
702 : 0 : info->mw_size = hw->mw_size;
703 : :
704 [ # # # # ]: 0 : if (!hw->queue_size || !hw->queue_pairs) {
705 : 0 : NTB_LOG(ERR, "No queue size and queue num assigned.");
706 : 0 : return -EAGAIN;
707 : : }
708 : :
709 : 0 : hw->hdr_size_per_queue = RTE_ALIGN(sizeof(struct ntb_header) +
710 : : hw->queue_size * sizeof(struct ntb_desc) +
711 : : hw->queue_size * sizeof(struct ntb_used),
712 : : RTE_CACHE_LINE_SIZE);
713 : 0 : info->ntb_hdr_size = hw->hdr_size_per_queue * hw->queue_pairs;
714 : :
715 : 0 : return 0;
716 : : }
717 : :
718 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_pmd_ntb_get_mem_align, 26.11)
719 : : uint64_t
720 : 0 : rte_pmd_ntb_get_mem_align(uint16_t dev_id, uint32_t mw_id, uint64_t mw_len)
721 : : {
722 : : struct rte_rawdev *dev;
723 : : struct ntb_hw *hw;
724 : :
725 [ # # ]: 0 : if (dev_id >= RTE_RAWDEV_MAX_DEVS)
726 : : return 0;
727 [ # # ]: 0 : dev = rte_rawdev_pmd_get_dev(dev_id);
728 [ # # ]: 0 : if (dev->dev_private == NULL)
729 : : return 0;
730 : :
731 : : hw = dev->dev_private;
732 [ # # ]: 0 : if (mw_id >= hw->mw_cnt)
733 : : return RTE_CACHE_LINE_SIZE;
734 : :
735 : 0 : return (*hw->ntb_ops->mem_align)(dev, mw_id, mw_len);
736 : : }
737 : :
738 : : static int
739 : 0 : ntb_dev_configure(const struct rte_rawdev *dev, rte_rawdev_obj_t config,
740 : : size_t config_size)
741 : : {
742 : : struct ntb_dev_config *conf = config;
743 : 0 : struct ntb_hw *hw = dev->dev_private;
744 : : uint32_t xstats_num;
745 : : int ret;
746 : :
747 [ # # ]: 0 : if (conf == NULL || config_size != sizeof(*conf))
748 : : return -EINVAL;
749 : :
750 : 0 : hw->queue_pairs = conf->num_queues;
751 : 0 : hw->queue_size = conf->queue_size;
752 : 0 : hw->used_mw_num = conf->mz_num;
753 : 0 : hw->mz = conf->mz_list;
754 : 0 : hw->rx_queues = rte_zmalloc("ntb_rx_queues",
755 : 0 : sizeof(struct ntb_rx_queue *) * hw->queue_pairs, 0);
756 : 0 : hw->tx_queues = rte_zmalloc("ntb_tx_queues",
757 : 0 : sizeof(struct ntb_tx_queue *) * hw->queue_pairs, 0);
758 : : /* First total stats, then per queue stats. */
759 : 0 : xstats_num = (hw->queue_pairs + 1) * NTB_XSTATS_NUM;
760 : 0 : hw->ntb_xstats = rte_zmalloc("ntb_xstats", xstats_num *
761 : : sizeof(uint64_t), 0);
762 : 0 : hw->ntb_xstats_off = rte_zmalloc("ntb_xstats_off", xstats_num *
763 : : sizeof(uint64_t), 0);
764 : :
765 : : /* Start handshake with the peer. */
766 : 0 : ret = (*hw->ntb_ops->dev_handshake)(dev);
767 [ # # ]: 0 : if (ret < 0) {
768 : 0 : rte_free(hw->rx_queues);
769 : 0 : rte_free(hw->tx_queues);
770 : 0 : hw->rx_queues = NULL;
771 : 0 : hw->tx_queues = NULL;
772 : 0 : return ret;
773 : : }
774 : :
775 : : return 0;
776 : : }
777 : :
778 : : static int
779 : 0 : ntb_dev_start(struct rte_rawdev *dev)
780 : : {
781 : 0 : struct ntb_hw *hw = dev->dev_private;
782 : : uint32_t i;
783 : : int ret;
784 : :
785 [ # # # # ]: 0 : if (!hw->link_status || !hw->peer_dev_up)
786 : : return -EINVAL;
787 : :
788 : : /* Set total stats. */
789 [ # # ]: 0 : for (i = 0; i < NTB_XSTATS_NUM; i++) {
790 : 0 : hw->ntb_xstats[i] = 0;
791 : 0 : hw->ntb_xstats_off[i] = 0;
792 : : }
793 : :
794 [ # # ]: 0 : for (i = 0; i < hw->queue_pairs; i++) {
795 : 0 : ret = ntb_queue_init(dev, i);
796 [ # # ]: 0 : if (ret) {
797 : 0 : NTB_LOG(ERR, "Failed to init queue.");
798 : 0 : goto err_q_init;
799 : : }
800 : : }
801 : :
802 : 0 : hw->peer_mw_base = rte_zmalloc("ntb_peer_mw_base", hw->mw_cnt *
803 : : sizeof(uint64_t), 0);
804 [ # # ]: 0 : if (hw->peer_mw_base == NULL) {
805 : 0 : NTB_LOG(ERR, "Cannot allocate memory for peer mw base.");
806 : : ret = -ENOMEM;
807 : 0 : goto err_q_init;
808 : : }
809 : :
810 : : /* Read and validate the peer configuration. */
811 : 0 : ret = (*hw->ntb_ops->read_peer_config)(dev);
812 [ # # ]: 0 : if (ret < 0)
813 : 0 : goto err_up;
814 : :
815 : 0 : dev->started = 1;
816 : :
817 : 0 : return 0;
818 : :
819 : : err_up:
820 : 0 : rte_free(hw->peer_mw_base);
821 : 0 : err_q_init:
822 [ # # ]: 0 : for (i = 0; i < hw->queue_pairs; i++) {
823 : 0 : ntb_rxq_release_mbufs(hw->rx_queues[i]);
824 : 0 : ntb_txq_release_mbufs(hw->tx_queues[i]);
825 : : }
826 : :
827 : : return ret;
828 : : }
829 : :
830 : : static void
831 : 0 : ntb_dev_stop(struct rte_rawdev *dev)
832 : : {
833 : 0 : struct ntb_hw *hw = dev->dev_private;
834 : : uint32_t time_out;
835 : : int status, i;
836 : :
837 [ # # ]: 0 : if (!hw->peer_dev_up)
838 : 0 : goto clean;
839 : :
840 : 0 : (*hw->ntb_ops->link_cleanup)(dev);
841 : :
842 : : /* Notify the peer that device will be down. */
843 : 0 : status = (*hw->ntb_ops->peer_db_set)(dev, 1);
844 [ # # ]: 0 : if (status) {
845 : 0 : NTB_LOG(ERR, "Failed to tell peer device is down.");
846 : 0 : return;
847 : : }
848 : :
849 : : /*
850 : : * Set time out as 1s in case that the peer is stopped accidently
851 : : * without any notification.
852 : : */
853 : : time_out = 1000000;
854 : :
855 : : /* Wait for cleanup work down before db mask clear. */
856 [ # # # # ]: 0 : while (hw->peer_dev_up && time_out) {
857 : 0 : time_out -= 10;
858 : 0 : rte_delay_us(10);
859 : : }
860 : :
861 : 0 : clean:
862 : : /* Clear doorbells mask. */
863 : 0 : status = (*hw->ntb_ops->db_set_mask)(dev,
864 : 0 : (((uint64_t)1 << hw->db_cnt) - 1));
865 [ # # ]: 0 : if (status)
866 : 0 : NTB_LOG(ERR, "Failed to clear doorbells.");
867 : :
868 [ # # ]: 0 : for (i = 0; i < hw->queue_pairs; i++) {
869 : 0 : ntb_rxq_release_mbufs(hw->rx_queues[i]);
870 : 0 : ntb_txq_release_mbufs(hw->tx_queues[i]);
871 : : }
872 : :
873 : 0 : dev->started = 0;
874 : : }
875 : :
876 : : static int
877 : 0 : ntb_dev_close(struct rte_rawdev *dev)
878 : : {
879 : 0 : struct ntb_hw *hw = dev->dev_private;
880 : : struct rte_intr_handle *intr_handle;
881 : : int i;
882 : :
883 [ # # ]: 0 : if (dev->started)
884 : 0 : ntb_dev_stop(dev);
885 : :
886 : : /* free queues */
887 [ # # ]: 0 : for (i = 0; i < hw->queue_pairs; i++)
888 : 0 : ntb_queue_release(dev, i);
889 : 0 : hw->queue_pairs = 0;
890 : :
891 : 0 : intr_handle = hw->pci_dev->intr_handle;
892 : : /* Disable interrupt only once */
893 [ # # # # ]: 0 : if (!rte_intr_nb_efd_get(intr_handle) &&
894 : 0 : !rte_intr_max_intr_get(intr_handle))
895 : : return 0;
896 : :
897 : : /* Clean datapath event and vec mapping */
898 : 0 : rte_intr_efd_disable(intr_handle);
899 : 0 : rte_intr_vec_list_free(intr_handle);
900 : : /* Disable uio intr before callback unregister */
901 : 0 : rte_intr_disable(intr_handle);
902 : :
903 : : /* Unregister callback func to eal lib */
904 : 0 : rte_intr_callback_unregister(intr_handle,
905 : : ntb_dev_intr_handler, dev);
906 : :
907 : 0 : return 0;
908 : : }
909 : :
910 : : static int
911 : 0 : ntb_dev_reset(struct rte_rawdev *rawdev __rte_unused)
912 : : {
913 : 0 : return 0;
914 : : }
915 : :
916 : : static int
917 : 0 : ntb_attr_set(struct rte_rawdev *dev, const char *attr_name,
918 : : uint64_t attr_value)
919 : : {
920 : : struct ntb_hw *hw;
921 : : int index;
922 : :
923 [ # # ]: 0 : if (dev == NULL || attr_name == NULL) {
924 : 0 : NTB_LOG(ERR, "Invalid arguments for setting attributes");
925 : 0 : return -EINVAL;
926 : : }
927 : :
928 : 0 : hw = dev->dev_private;
929 : :
930 [ # # ]: 0 : if (!strncmp(attr_name, NTB_SPAD_USER, NTB_SPAD_USER_LEN)) {
931 : 0 : index = atoi(&attr_name[NTB_SPAD_USER_LEN]);
932 [ # # ]: 0 : if (index < 0 || index >= NTB_SPAD_USER_MAX_NUM) {
933 : 0 : NTB_LOG(ERR, "Invalid attribute (%s)", attr_name);
934 : 0 : return -EINVAL;
935 : : }
936 : 0 : (*hw->ntb_ops->spad_write)(dev, hw->spad_user_list[index],
937 : : 1, attr_value);
938 : 0 : NTB_LOG(DEBUG, "Set attribute (%s) Value (%" PRIu64 ")",
939 : : attr_name, attr_value);
940 : 0 : return 0;
941 : : }
942 : :
943 [ # # ]: 0 : if (!strncmp(attr_name, NTB_QUEUE_SZ_NAME, NTB_ATTR_NAME_LEN)) {
944 : 0 : hw->queue_size = attr_value;
945 : 0 : NTB_LOG(DEBUG, "Set attribute (%s) Value (%" PRIu64 ")",
946 : : attr_name, attr_value);
947 : 0 : return 0;
948 : : }
949 : :
950 [ # # ]: 0 : if (!strncmp(attr_name, NTB_QUEUE_NUM_NAME, NTB_ATTR_NAME_LEN)) {
951 : 0 : hw->queue_pairs = attr_value;
952 : 0 : NTB_LOG(DEBUG, "Set attribute (%s) Value (%" PRIu64 ")",
953 : : attr_name, attr_value);
954 : 0 : return 0;
955 : : }
956 : :
957 : : /* Attribute not found. */
958 : 0 : NTB_LOG(ERR, "Attribute not found.");
959 : 0 : return -EINVAL;
960 : : }
961 : :
962 : : static int
963 : 0 : ntb_attr_get(struct rte_rawdev *dev, const char *attr_name,
964 : : uint64_t *attr_value)
965 : : {
966 : : struct ntb_hw *hw;
967 : : int index;
968 : :
969 [ # # # # ]: 0 : if (dev == NULL || attr_name == NULL || attr_value == NULL) {
970 : 0 : NTB_LOG(ERR, "Invalid arguments for getting attributes");
971 : 0 : return -EINVAL;
972 : : }
973 : :
974 : 0 : hw = dev->dev_private;
975 : :
976 [ # # ]: 0 : if (!strncmp(attr_name, NTB_TOPO_NAME, NTB_ATTR_NAME_LEN)) {
977 : 0 : *attr_value = hw->topo;
978 : 0 : NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
979 : : attr_name, *attr_value);
980 : 0 : return 0;
981 : : }
982 : :
983 [ # # ]: 0 : if (!strncmp(attr_name, NTB_LINK_STATUS_NAME, NTB_ATTR_NAME_LEN)) {
984 : : /* hw->link_status only indicates hw link status. */
985 [ # # # # ]: 0 : *attr_value = hw->link_status && hw->peer_dev_up;
986 : 0 : NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
987 : : attr_name, *attr_value);
988 : 0 : return 0;
989 : : }
990 : :
991 [ # # ]: 0 : if (!strncmp(attr_name, NTB_SPEED_NAME, NTB_ATTR_NAME_LEN)) {
992 : 0 : *attr_value = hw->link_speed;
993 : 0 : NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
994 : : attr_name, *attr_value);
995 : 0 : return 0;
996 : : }
997 : :
998 [ # # ]: 0 : if (!strncmp(attr_name, NTB_WIDTH_NAME, NTB_ATTR_NAME_LEN)) {
999 : 0 : *attr_value = hw->link_width;
1000 : 0 : NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
1001 : : attr_name, *attr_value);
1002 : 0 : return 0;
1003 : : }
1004 : :
1005 [ # # ]: 0 : if (!strncmp(attr_name, NTB_MW_CNT_NAME, NTB_ATTR_NAME_LEN)) {
1006 : 0 : *attr_value = hw->mw_cnt;
1007 : 0 : NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
1008 : : attr_name, *attr_value);
1009 : 0 : return 0;
1010 : : }
1011 : :
1012 [ # # ]: 0 : if (!strncmp(attr_name, NTB_DB_CNT_NAME, NTB_ATTR_NAME_LEN)) {
1013 : 0 : *attr_value = hw->db_cnt;
1014 : 0 : NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
1015 : : attr_name, *attr_value);
1016 : 0 : return 0;
1017 : : }
1018 : :
1019 [ # # ]: 0 : if (!strncmp(attr_name, NTB_SPAD_CNT_NAME, NTB_ATTR_NAME_LEN)) {
1020 : 0 : *attr_value = hw->spad_cnt;
1021 : 0 : NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
1022 : : attr_name, *attr_value);
1023 : 0 : return 0;
1024 : : }
1025 : :
1026 [ # # ]: 0 : if (!strncmp(attr_name, NTB_SPAD_USER, NTB_SPAD_USER_LEN)) {
1027 : 0 : index = atoi(&attr_name[NTB_SPAD_USER_LEN]);
1028 [ # # ]: 0 : if (index < 0 || index >= NTB_SPAD_USER_MAX_NUM) {
1029 : 0 : NTB_LOG(ERR, "Attribute (%s) out of range", attr_name);
1030 : 0 : return -EINVAL;
1031 : : }
1032 : 0 : *attr_value = (*hw->ntb_ops->spad_read)(dev,
1033 : : hw->spad_user_list[index], 0);
1034 : 0 : NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
1035 : : attr_name, *attr_value);
1036 : 0 : return 0;
1037 : : }
1038 : :
1039 : : /* Attribute not found. */
1040 : 0 : NTB_LOG(ERR, "Attribute not found.");
1041 : 0 : return -EINVAL;
1042 : : }
1043 : :
1044 : : static inline uint64_t
1045 : : ntb_stats_update(uint64_t offset, uint64_t stat)
1046 : : {
1047 : 0 : if (stat >= offset)
1048 : 0 : return (stat - offset);
1049 : : else
1050 : 0 : return (uint64_t)(((uint64_t)-1) - offset + stat + 1);
1051 : : }
1052 : :
1053 : : static int
1054 : 0 : ntb_xstats_get(const struct rte_rawdev *dev,
1055 : : const unsigned int ids[],
1056 : : uint64_t values[],
1057 : : unsigned int n)
1058 : : {
1059 : 0 : struct ntb_hw *hw = dev->dev_private;
1060 : : uint32_t i, j, off, xstats_num;
1061 : :
1062 : : /* Calculate total stats of all queues. */
1063 [ # # ]: 0 : for (i = 0; i < NTB_XSTATS_NUM; i++) {
1064 : 0 : hw->ntb_xstats[i] = 0;
1065 [ # # ]: 0 : for (j = 0; j < hw->queue_pairs; j++) {
1066 : 0 : off = NTB_XSTATS_NUM * (j + 1) + i;
1067 : 0 : hw->ntb_xstats[i] +=
1068 : 0 : ntb_stats_update(hw->ntb_xstats_off[off],
1069 [ # # ]: 0 : hw->ntb_xstats[off]);
1070 : : }
1071 : : }
1072 : :
1073 : 0 : xstats_num = NTB_XSTATS_NUM * (hw->queue_pairs + 1);
1074 [ # # # # ]: 0 : for (i = 0; i < n && ids[i] < xstats_num; i++) {
1075 [ # # ]: 0 : if (ids[i] < NTB_XSTATS_NUM)
1076 : 0 : values[i] = hw->ntb_xstats[ids[i]];
1077 : : else
1078 : 0 : values[i] =
1079 : 0 : ntb_stats_update(hw->ntb_xstats_off[ids[i]],
1080 [ # # ]: 0 : hw->ntb_xstats[ids[i]]);
1081 : : }
1082 : :
1083 : 0 : return i;
1084 : : }
1085 : :
1086 : : static int
1087 : 0 : ntb_xstats_get_names(const struct rte_rawdev *dev,
1088 : : struct rte_rawdev_xstats_name *xstats_names,
1089 : : unsigned int size)
1090 : : {
1091 : 0 : struct ntb_hw *hw = dev->dev_private;
1092 : : uint32_t xstats_num, i, j, off;
1093 : :
1094 : 0 : xstats_num = NTB_XSTATS_NUM * (hw->queue_pairs + 1);
1095 [ # # ]: 0 : if (xstats_names == NULL || size < xstats_num)
1096 : 0 : return xstats_num;
1097 : :
1098 : : /* Total stats names */
1099 : : memcpy(xstats_names, ntb_xstats_names, sizeof(ntb_xstats_names));
1100 : :
1101 : : /* Queue stats names */
1102 [ # # ]: 0 : for (i = 0; i < hw->queue_pairs; i++) {
1103 [ # # ]: 0 : for (j = 0; j < NTB_XSTATS_NUM; j++) {
1104 : 0 : off = j + (i + 1) * NTB_XSTATS_NUM;
1105 : 0 : snprintf(xstats_names[off].name,
1106 : : sizeof(xstats_names[0].name),
1107 : 0 : "%s_q%u", ntb_xstats_names[j].name, i);
1108 : : }
1109 : : }
1110 : :
1111 : 0 : return xstats_num;
1112 : : }
1113 : :
1114 : : static uint64_t
1115 : 0 : ntb_xstats_get_by_name(const struct rte_rawdev *dev,
1116 : : const char *name, unsigned int *id)
1117 : : {
1118 : : struct rte_rawdev_xstats_name *xstats_names;
1119 : 0 : struct ntb_hw *hw = dev->dev_private;
1120 : : uint32_t xstats_num, i, j, off;
1121 : :
1122 [ # # ]: 0 : if (name == NULL)
1123 : : return -EINVAL;
1124 : :
1125 : 0 : xstats_num = NTB_XSTATS_NUM * (hw->queue_pairs + 1);
1126 : 0 : xstats_names = rte_zmalloc("ntb_stats_name",
1127 : : sizeof(struct rte_rawdev_xstats_name) *
1128 : : xstats_num, 0);
1129 : 0 : ntb_xstats_get_names(dev, xstats_names, xstats_num);
1130 : :
1131 : : /* Calculate total stats of all queues. */
1132 [ # # ]: 0 : for (i = 0; i < NTB_XSTATS_NUM; i++) {
1133 [ # # ]: 0 : for (j = 0; j < hw->queue_pairs; j++) {
1134 : 0 : off = NTB_XSTATS_NUM * (j + 1) + i;
1135 : 0 : hw->ntb_xstats[i] +=
1136 : 0 : ntb_stats_update(hw->ntb_xstats_off[off],
1137 [ # # ]: 0 : hw->ntb_xstats[off]);
1138 : : }
1139 : : }
1140 : :
1141 [ # # ]: 0 : for (i = 0; i < xstats_num; i++) {
1142 [ # # ]: 0 : if (!strncmp(name, xstats_names[i].name,
1143 : : RTE_RAW_DEV_XSTATS_NAME_SIZE)) {
1144 : 0 : *id = i;
1145 : 0 : rte_free(xstats_names);
1146 [ # # ]: 0 : if (i < NTB_XSTATS_NUM)
1147 : 0 : return hw->ntb_xstats[i];
1148 : : else
1149 : 0 : return ntb_stats_update(hw->ntb_xstats_off[i],
1150 [ # # ]: 0 : hw->ntb_xstats[i]);
1151 : : }
1152 : : }
1153 : :
1154 : 0 : NTB_LOG(ERR, "Cannot find the xstats name.");
1155 : :
1156 : 0 : return -EINVAL;
1157 : : }
1158 : :
1159 : : static int
1160 : 0 : ntb_xstats_reset(struct rte_rawdev *dev,
1161 : : const uint32_t ids[],
1162 : : uint32_t nb_ids)
1163 : : {
1164 : 0 : struct ntb_hw *hw = dev->dev_private;
1165 : : uint32_t i, j, off, xstats_num;
1166 : :
1167 : 0 : xstats_num = NTB_XSTATS_NUM * (hw->queue_pairs + 1);
1168 [ # # # # ]: 0 : for (i = 0; i < nb_ids && ids[i] < xstats_num; i++) {
1169 [ # # ]: 0 : if (ids[i] < NTB_XSTATS_NUM) {
1170 [ # # ]: 0 : for (j = 0; j < hw->queue_pairs; j++) {
1171 : 0 : off = NTB_XSTATS_NUM * (j + 1) + ids[i];
1172 : 0 : hw->ntb_xstats_off[off] = hw->ntb_xstats[off];
1173 : : }
1174 : : } else {
1175 : 0 : hw->ntb_xstats_off[ids[i]] = hw->ntb_xstats[ids[i]];
1176 : : }
1177 : : }
1178 : :
1179 : 0 : return i;
1180 : : }
1181 : :
1182 : : static const struct rte_rawdev_ops ntb_ops = {
1183 : : .dev_info_get = ntb_dev_info_get,
1184 : : .dev_configure = ntb_dev_configure,
1185 : : .dev_start = ntb_dev_start,
1186 : : .dev_stop = ntb_dev_stop,
1187 : : .dev_close = ntb_dev_close,
1188 : : .dev_reset = ntb_dev_reset,
1189 : :
1190 : : .queue_def_conf = ntb_queue_conf_get,
1191 : : .queue_setup = ntb_queue_setup,
1192 : : .queue_release = ntb_queue_release,
1193 : : .queue_count = ntb_queue_count,
1194 : :
1195 : : .enqueue_bufs = ntb_enqueue_bufs,
1196 : : .dequeue_bufs = ntb_dequeue_bufs,
1197 : :
1198 : : .attr_get = ntb_attr_get,
1199 : : .attr_set = ntb_attr_set,
1200 : :
1201 : : .xstats_get = ntb_xstats_get,
1202 : : .xstats_get_names = ntb_xstats_get_names,
1203 : : .xstats_get_by_name = ntb_xstats_get_by_name,
1204 : : .xstats_reset = ntb_xstats_reset,
1205 : : };
1206 : :
1207 : : /* Every vendor ops entry is dereferenced unconditionally in the common code,
1208 : : * so enforce their presence once at probe time instead of guarding every call site at runtime.
1209 : : */
1210 : : static int
1211 : 0 : ntb_vendor_ops_check(const struct ntb_hw *hw)
1212 : : {
1213 : : #define NTB_OPS_REQ(member) do { \
1214 : : if (hw->ntb_ops->member == NULL) { \
1215 : : NTB_LOG(ERR, "Vendor ops lacks mandatory ." #member); \
1216 : : return -EINVAL; \
1217 : : } \
1218 : : } while (0)
1219 [ # # ]: 0 : NTB_OPS_REQ(ntb_dev_init);
1220 [ # # ]: 0 : NTB_OPS_REQ(get_peer_mw_addr);
1221 [ # # ]: 0 : NTB_OPS_REQ(ioremap);
1222 [ # # ]: 0 : NTB_OPS_REQ(get_link_status);
1223 [ # # ]: 0 : NTB_OPS_REQ(set_link);
1224 [ # # ]: 0 : NTB_OPS_REQ(spad_read);
1225 [ # # ]: 0 : NTB_OPS_REQ(spad_write);
1226 [ # # ]: 0 : NTB_OPS_REQ(db_read);
1227 [ # # ]: 0 : NTB_OPS_REQ(db_clear);
1228 [ # # ]: 0 : NTB_OPS_REQ(db_set_mask);
1229 [ # # ]: 0 : NTB_OPS_REQ(peer_db_set);
1230 [ # # ]: 0 : NTB_OPS_REQ(vector_bind);
1231 [ # # ]: 0 : NTB_OPS_REQ(dev_handshake);
1232 [ # # ]: 0 : NTB_OPS_REQ(peer_mw_validate);
1233 [ # # ]: 0 : NTB_OPS_REQ(read_peer_config);
1234 [ # # ]: 0 : NTB_OPS_REQ(link_cleanup);
1235 [ # # ]: 0 : NTB_OPS_REQ(mem_align);
1236 : : #undef NTB_OPS_REQ
1237 : :
1238 : : return 0;
1239 : : }
1240 : :
1241 : : static int
1242 : 0 : ntb_init_hw(struct rte_rawdev *dev, struct rte_pci_device *pci_dev)
1243 : : {
1244 : 0 : struct ntb_hw *hw = dev->dev_private;
1245 : : struct rte_intr_handle *intr_handle;
1246 : : int ret, i;
1247 : :
1248 : 0 : hw->pci_dev = pci_dev;
1249 : 0 : hw->peer_dev_up = 0;
1250 : 0 : hw->link_status = NTB_LINK_DOWN;
1251 : 0 : hw->link_speed = NTB_SPEED_NONE;
1252 : 0 : hw->link_width = NTB_WIDTH_NONE;
1253 : :
1254 [ # # # ]: 0 : switch (pci_dev->id.device_id) {
1255 : 0 : case NTB_AMD_DEV_ID_PRI:
1256 : : case NTB_AMD_DEV_ID_SEC:
1257 : 0 : hw->ntb_ops = &amd_ntb_ops;
1258 : 0 : break;
1259 : 0 : case NTB_INTEL_DEV_ID_B2B_SKX:
1260 : : case NTB_INTEL_DEV_ID_B2B_ICX:
1261 : 0 : hw->ntb_ops = &intel_ntb_ops;
1262 : 0 : break;
1263 : 0 : default:
1264 : 0 : NTB_LOG(ERR, "Not supported device.");
1265 : 0 : return -EINVAL;
1266 : : }
1267 : :
1268 : 0 : ret = ntb_vendor_ops_check(hw);
1269 [ # # ]: 0 : if (ret)
1270 : : return ret;
1271 : :
1272 : 0 : ret = (*hw->ntb_ops->ntb_dev_init)(dev);
1273 [ # # ]: 0 : if (ret) {
1274 : 0 : NTB_LOG(ERR, "Unable to init ntb dev.");
1275 : 0 : return ret;
1276 : : }
1277 : :
1278 : 0 : ret = (*hw->ntb_ops->set_link)(dev, 1);
1279 [ # # ]: 0 : if (ret)
1280 : : return ret;
1281 : :
1282 : : /* Init doorbell. */
1283 : 0 : hw->db_valid_mask = RTE_LEN2MASK(hw->db_cnt, uint64_t);
1284 : : /* Clear all valid doorbell bits before registering intr handler */
1285 : 0 : (*hw->ntb_ops->db_clear)(dev, hw->db_valid_mask);
1286 : :
1287 : 0 : intr_handle = pci_dev->intr_handle;
1288 : : /* Register callback func to eal lib */
1289 : 0 : rte_intr_callback_register(intr_handle,
1290 : : ntb_dev_intr_handler, dev);
1291 : :
1292 : 0 : ret = rte_intr_efd_enable(intr_handle, hw->db_cnt);
1293 [ # # ]: 0 : if (ret)
1294 : : return ret;
1295 : :
1296 : : /* To clarify, the interrupt for each doorbell is already mapped
1297 : : * by default for intel gen3. They are mapped to msix vec 1-32,
1298 : : * and hardware intr is mapped to 0. Map all to 0 for uio.
1299 : : */
1300 [ # # ]: 0 : if (!rte_intr_cap_multiple(intr_handle)) {
1301 [ # # ]: 0 : for (i = 0; i < hw->db_cnt; i++) {
1302 : 0 : ret = (*hw->ntb_ops->vector_bind)(dev, i, 0);
1303 [ # # ]: 0 : if (ret)
1304 : 0 : return ret;
1305 : : }
1306 : : }
1307 : :
1308 : 0 : hw->db_mask = 0;
1309 : 0 : ret = (*hw->ntb_ops->db_set_mask)(dev, hw->db_mask);
1310 [ # # ]: 0 : if (ret) {
1311 : 0 : NTB_LOG(ERR, "Unable to enable intr for all dbs.");
1312 : 0 : return ret;
1313 : : }
1314 : :
1315 : : /* enable uio intr after callback register */
1316 : 0 : rte_intr_enable(intr_handle);
1317 : :
1318 : 0 : return ret;
1319 : : }
1320 : :
1321 : : static int
1322 : 0 : ntb_create(struct rte_pci_device *pci_dev, int socket_id)
1323 : : {
1324 : : char name[RTE_RAWDEV_NAME_MAX_LEN];
1325 : : struct rte_rawdev *rawdev = NULL;
1326 : : int ret;
1327 : :
1328 [ # # ]: 0 : if (pci_dev == NULL) {
1329 : 0 : NTB_LOG(ERR, "Invalid pci_dev.");
1330 : 0 : return -EINVAL;
1331 : : }
1332 : :
1333 : : memset(name, 0, sizeof(name));
1334 : 0 : snprintf(name, RTE_RAWDEV_NAME_MAX_LEN, "NTB:%x:%02x.%x",
1335 : 0 : pci_dev->addr.bus, pci_dev->addr.devid,
1336 : 0 : pci_dev->addr.function);
1337 : :
1338 : 0 : NTB_LOG(INFO, "Init %s on NUMA node %d", name, socket_id);
1339 : :
1340 : : /* Allocate device structure. */
1341 : 0 : rawdev = rte_rawdev_pmd_allocate(name, sizeof(struct ntb_hw),
1342 : : socket_id);
1343 [ # # ]: 0 : if (rawdev == NULL) {
1344 : 0 : NTB_LOG(ERR, "Unable to allocate rawdev.");
1345 : 0 : return -EINVAL;
1346 : : }
1347 : :
1348 : 0 : rawdev->dev_ops = &ntb_ops;
1349 : 0 : rawdev->device = &pci_dev->device;
1350 : 0 : rawdev->driver_name = pci_dev->device.driver->name;
1351 : :
1352 : 0 : ret = ntb_init_hw(rawdev, pci_dev);
1353 [ # # ]: 0 : if (ret < 0) {
1354 : 0 : NTB_LOG(ERR, "Unable to init ntb hw.");
1355 : 0 : goto fail;
1356 : : }
1357 : :
1358 : : return ret;
1359 : :
1360 : : fail:
1361 : : if (rawdev != NULL)
1362 : 0 : rte_rawdev_pmd_release(rawdev);
1363 : :
1364 : 0 : return ret;
1365 : : }
1366 : :
1367 : : static int
1368 : 0 : ntb_destroy(struct rte_pci_device *pci_dev)
1369 : : {
1370 : : char name[RTE_RAWDEV_NAME_MAX_LEN];
1371 : : struct rte_rawdev *rawdev;
1372 : : int ret;
1373 : :
1374 [ # # ]: 0 : if (pci_dev == NULL) {
1375 : 0 : NTB_LOG(ERR, "Invalid pci_dev.");
1376 : : ret = -EINVAL;
1377 : 0 : return ret;
1378 : : }
1379 : :
1380 : : memset(name, 0, sizeof(name));
1381 : 0 : snprintf(name, RTE_RAWDEV_NAME_MAX_LEN, "NTB:%x:%02x.%x",
1382 : 0 : pci_dev->addr.bus, pci_dev->addr.devid,
1383 : 0 : pci_dev->addr.function);
1384 : :
1385 : 0 : NTB_LOG(INFO, "Closing %s on NUMA node %d", name, rte_socket_id());
1386 : :
1387 : 0 : rawdev = rte_rawdev_pmd_get_named_dev(name);
1388 [ # # ]: 0 : if (rawdev == NULL) {
1389 : 0 : NTB_LOG(ERR, "Invalid device name (%s)", name);
1390 : : ret = -EINVAL;
1391 : 0 : return ret;
1392 : : }
1393 : :
1394 : 0 : ret = rte_rawdev_pmd_release(rawdev);
1395 [ # # ]: 0 : if (ret)
1396 : 0 : NTB_LOG(ERR, "Failed to destroy ntb rawdev.");
1397 : :
1398 : : return ret;
1399 : : }
1400 : :
1401 : : static int
1402 : 0 : ntb_probe(struct rte_pci_driver *pci_drv __rte_unused,
1403 : : struct rte_pci_device *pci_dev)
1404 : : {
1405 : 0 : return ntb_create(pci_dev, rte_socket_id());
1406 : : }
1407 : :
1408 : : static int
1409 : 0 : ntb_remove(struct rte_pci_device *pci_dev)
1410 : : {
1411 : 0 : return ntb_destroy(pci_dev);
1412 : : }
1413 : :
1414 : :
1415 : : static struct rte_pci_driver rte_ntb_pmd = {
1416 : : .id_table = pci_id_ntb_map,
1417 : : .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_WC_ACTIVATE,
1418 : : .probe = ntb_probe,
1419 : : .remove = ntb_remove,
1420 : : };
1421 : :
1422 : 305 : RTE_PMD_REGISTER_PCI(raw_ntb, rte_ntb_pmd);
1423 : : RTE_PMD_REGISTER_PCI_TABLE(raw_ntb, pci_id_ntb_map);
1424 : : RTE_PMD_REGISTER_KMOD_DEP(raw_ntb, "* igb_uio | uio_pci_generic | vfio-pci");
1425 [ - + ]: 305 : RTE_LOG_REGISTER_DEFAULT(ntb_logtype, INFO);
|