Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2016-2018 Microsoft Corporation
3 : : * Copyright(c) 2013-2016 Brocade Communications Systems, Inc.
4 : : * All rights reserved.
5 : : */
6 : :
7 : : #include <stdint.h>
8 : : #include <string.h>
9 : : #include <stdio.h>
10 : : #include <errno.h>
11 : : #include <unistd.h>
12 : : #include <strings.h>
13 : : #include <malloc.h>
14 : :
15 : : #include <rte_ethdev.h>
16 : : #include <rte_memcpy.h>
17 : : #include <rte_string_fns.h>
18 : : #include <rte_memzone.h>
19 : : #include <rte_malloc.h>
20 : : #include <rte_stdatomic.h>
21 : : #include <rte_bitmap.h>
22 : : #include <rte_branch_prediction.h>
23 : : #include <rte_ether.h>
24 : : #include <rte_common.h>
25 : : #include <rte_errno.h>
26 : : #include <rte_memory.h>
27 : : #include <rte_eal.h>
28 : : #include <dev_driver.h>
29 : : #include <rte_net.h>
30 : : #include <bus_vmbus_driver.h>
31 : : #include <rte_spinlock.h>
32 : :
33 : : #include "hn_logs.h"
34 : : #include "hn_var.h"
35 : : #include "hn_rndis.h"
36 : : #include "hn_nvs.h"
37 : : #include "ndis.h"
38 : :
39 : : #define HN_NVS_SEND_MSG_SIZE \
40 : : (sizeof(struct vmbus_chanpkt_hdr) + sizeof(struct hn_nvs_rndis))
41 : :
42 : : #define HN_TXD_CACHE_SIZE 32 /* per cpu tx_descriptor pool cache */
43 : : #define HN_RXQ_EVENT_DEFAULT 2048
44 : :
45 : : struct hn_rxinfo {
46 : : uint32_t vlan_info;
47 : : uint32_t csum_info;
48 : : uint32_t hash_info;
49 : : uint32_t hash_value;
50 : : };
51 : :
52 : : #define HN_RXINFO_VLAN 0x0001
53 : : #define HN_RXINFO_CSUM 0x0002
54 : : #define HN_RXINFO_HASHINF 0x0004
55 : : #define HN_RXINFO_HASHVAL 0x0008
56 : : #define HN_RXINFO_ALL \
57 : : (HN_RXINFO_VLAN | \
58 : : HN_RXINFO_CSUM | \
59 : : HN_RXINFO_HASHINF | \
60 : : HN_RXINFO_HASHVAL)
61 : :
62 : : #define HN_NDIS_VLAN_INFO_INVALID 0xffffffff
63 : : #define HN_NDIS_RXCSUM_INFO_INVALID 0
64 : : #define HN_NDIS_HASH_INFO_INVALID 0
65 : :
66 : : /*
67 : : * Per-transmit book keeping.
68 : : * A slot in transmit ring (chim_index) is reserved for each transmit.
69 : : *
70 : : * There are two types of transmit:
71 : : * - buffered transmit where chimney buffer is used and RNDIS header
72 : : * is in the buffer. mbuf == NULL for this case.
73 : : *
74 : : * - direct transmit where RNDIS header is in the in rndis_pkt
75 : : * mbuf is freed after transmit.
76 : : *
77 : : * Descriptors come from per-port pool which is used
78 : : * to limit number of outstanding requests per device.
79 : : */
80 : : struct hn_txdesc {
81 : : struct rte_mbuf *m;
82 : :
83 : : uint16_t queue_id;
84 : : uint32_t chim_index;
85 : : uint32_t chim_size;
86 : : uint32_t data_size;
87 : : uint32_t packets;
88 : :
89 : : struct rndis_packet_msg *rndis_pkt;
90 : : };
91 : :
92 : : #define HN_RNDIS_PKT_LEN \
93 : : (sizeof(struct rndis_packet_msg) + \
94 : : RNDIS_PKTINFO_SIZE(NDIS_HASH_VALUE_SIZE) + \
95 : : RNDIS_PKTINFO_SIZE(NDIS_VLAN_INFO_SIZE) + \
96 : : RNDIS_PKTINFO_SIZE(NDIS_LSO2_INFO_SIZE) + \
97 : : RNDIS_PKTINFO_SIZE(NDIS_TXCSUM_INFO_SIZE))
98 : :
99 : : #define HN_RNDIS_PKT_ALIGNED RTE_ALIGN(HN_RNDIS_PKT_LEN, RTE_CACHE_LINE_SIZE)
100 : :
101 : : /* Minimum space required for a packet */
102 : : #define HN_PKTSIZE_MIN(align) \
103 : : RTE_ALIGN(RTE_ETHER_MIN_LEN + HN_RNDIS_PKT_LEN, align)
104 : :
105 : : #define DEFAULT_TX_FREE_THRESH 32
106 : :
107 : : static void
108 : 0 : hn_update_packet_stats(struct hn_stats *stats, const struct rte_mbuf *m)
109 : : {
110 : 0 : uint32_t s = m->pkt_len;
111 : : const struct rte_ether_addr *ea;
112 : :
113 [ # # ]: 0 : if (s >= 1024)
114 [ # # ]: 0 : stats->size_bins[6 + (s > 1518)]++;
115 [ # # ]: 0 : else if (s <= 64)
116 : 0 : stats->size_bins[s >> 6]++;
117 : : else
118 : 0 : stats->size_bins[32UL - rte_clz32(s) - 5]++;
119 : :
120 [ # # ]: 0 : ea = rte_pktmbuf_mtod(m, const struct rte_ether_addr *);
121 : : RTE_BUILD_BUG_ON(offsetof(struct hn_stats, broadcast) !=
122 : : offsetof(struct hn_stats, multicast) + sizeof(uint64_t));
123 [ # # ]: 0 : if (unlikely(rte_is_multicast_ether_addr(ea)))
124 : 0 : (&stats->multicast)[rte_is_broadcast_ether_addr(ea)]++;
125 : 0 : }
126 : :
127 : : static inline unsigned int hn_rndis_pktlen(const struct rndis_packet_msg *pkt)
128 : : {
129 : 0 : return pkt->pktinfooffset + pkt->pktinfolen;
130 : : }
131 : :
132 : : static inline uint32_t
133 : : hn_rndis_pktmsg_offset(uint32_t ofs)
134 : : {
135 : 0 : return ofs - offsetof(struct rndis_packet_msg, dataoffset);
136 : : }
137 : :
138 : 0 : static void hn_txd_init(struct rte_mempool *mp __rte_unused,
139 : : void *opaque, void *obj, unsigned int idx)
140 : : {
141 : : struct hn_tx_queue *txq = opaque;
142 : : struct hn_txdesc *txd = obj;
143 : :
144 : : memset(txd, 0, sizeof(*txd));
145 : :
146 : 0 : txd->queue_id = txq->queue_id;
147 : 0 : txd->chim_index = NVS_CHIM_IDX_INVALID;
148 : 0 : txd->rndis_pkt = (struct rndis_packet_msg *)((char *)txq->tx_rndis
149 : 0 : + idx * HN_RNDIS_PKT_ALIGNED);
150 : 0 : }
151 : :
152 : : int
153 : 0 : hn_chim_init(struct rte_eth_dev *dev)
154 : : {
155 : 0 : struct hn_data *hv = dev->data->dev_private;
156 : : uint32_t i, chim_bmp_size;
157 : :
158 : : rte_spinlock_init(&hv->chim_lock);
159 : 0 : chim_bmp_size = rte_bitmap_get_memory_footprint(hv->chim_cnt);
160 : 0 : hv->chim_bmem = rte_zmalloc("hn_chim_bitmap", chim_bmp_size,
161 : : RTE_CACHE_LINE_SIZE);
162 [ # # ]: 0 : if (hv->chim_bmem == NULL) {
163 : 0 : PMD_INIT_LOG(ERR, "failed to allocate bitmap size %u",
164 : : chim_bmp_size);
165 : 0 : return -1;
166 : : }
167 : :
168 : 0 : hv->chim_bmap = rte_bitmap_init(hv->chim_cnt,
169 : : hv->chim_bmem, chim_bmp_size);
170 [ # # ]: 0 : if (hv->chim_bmap == NULL) {
171 : 0 : PMD_INIT_LOG(ERR, "failed to init chim bitmap");
172 : 0 : return -1;
173 : : }
174 : :
175 [ # # ]: 0 : for (i = 0; i < hv->chim_cnt; i++)
176 : 0 : rte_bitmap_set(hv->chim_bmap, i);
177 : :
178 : : return 0;
179 : : }
180 : :
181 : : void
182 : 0 : hn_chim_uninit(struct rte_eth_dev *dev)
183 : : {
184 : 0 : struct hn_data *hv = dev->data->dev_private;
185 : :
186 : : rte_bitmap_free(hv->chim_bmap);
187 : 0 : rte_free(hv->chim_bmem);
188 : 0 : hv->chim_bmem = NULL;
189 : 0 : }
190 : :
191 : 0 : static uint32_t hn_chim_alloc(struct hn_data *hv)
192 : : {
193 : 0 : uint32_t index = NVS_CHIM_IDX_INVALID;
194 : 0 : uint64_t slab = 0;
195 : :
196 : 0 : rte_spinlock_lock(&hv->chim_lock);
197 [ # # ]: 0 : if (rte_bitmap_scan(hv->chim_bmap, &index, &slab)) {
198 : 0 : index += rte_bsf64(slab);
199 : 0 : rte_bitmap_clear(hv->chim_bmap, index);
200 : : }
201 : : rte_spinlock_unlock(&hv->chim_lock);
202 : :
203 : 0 : return index;
204 : : }
205 : :
206 : 0 : static void hn_chim_free(struct hn_data *hv, uint32_t chim_idx)
207 : : {
208 [ # # ]: 0 : if (chim_idx >= hv->chim_cnt) {
209 : 0 : PMD_DRV_LOG(ERR, "Invalid chimney index %u", chim_idx);
210 : : } else {
211 : 0 : rte_spinlock_lock(&hv->chim_lock);
212 : 0 : rte_bitmap_set(hv->chim_bmap, chim_idx);
213 : : rte_spinlock_unlock(&hv->chim_lock);
214 : : }
215 : 0 : }
216 : :
217 : : static void hn_reset_txagg(struct hn_tx_queue *txq)
218 : : {
219 : 0 : txq->agg_szleft = txq->agg_szmax;
220 : 0 : txq->agg_pktleft = txq->agg_pktmax;
221 : 0 : txq->agg_txd = NULL;
222 : 0 : txq->agg_prevpkt = NULL;
223 : 0 : }
224 : :
225 : : static void
226 : 0 : hn_rx_queue_free_common(struct hn_rx_queue *rxq)
227 : : {
228 [ # # ]: 0 : if (!rxq)
229 : : return;
230 : :
231 : 0 : rte_free(rxq->rxbuf_info);
232 : 0 : rte_free(rxq->event_buf);
233 : 0 : rte_free(rxq);
234 : : }
235 : :
236 : : int
237 : 0 : hn_dev_tx_queue_setup(struct rte_eth_dev *dev,
238 : : uint16_t queue_idx, uint16_t nb_desc,
239 : : unsigned int socket_id,
240 : : const struct rte_eth_txconf *tx_conf)
241 : :
242 : : {
243 : 0 : struct hn_data *hv = dev->data->dev_private;
244 : : struct hn_tx_queue *txq;
245 : : struct hn_rx_queue *rxq = NULL;
246 : : char name[RTE_MEMPOOL_NAMESIZE];
247 : : uint32_t tx_free_thresh;
248 : : int err = -ENOMEM;
249 : :
250 : 0 : PMD_INIT_FUNC_TRACE();
251 : :
252 : 0 : tx_free_thresh = tx_conf->tx_free_thresh;
253 [ # # ]: 0 : if (tx_free_thresh == 0)
254 : 0 : tx_free_thresh = RTE_MIN(nb_desc / 4,
255 : : DEFAULT_TX_FREE_THRESH);
256 : :
257 [ # # ]: 0 : if (tx_free_thresh + 3 >= nb_desc) {
258 : 0 : PMD_INIT_LOG(ERR,
259 : : "tx_free_thresh must be less than the number of TX entries minus 3(%u)."
260 : : " (tx_free_thresh=%u port=%u queue=%u)",
261 : : nb_desc - 3,
262 : : tx_free_thresh, dev->data->port_id, queue_idx);
263 : 0 : return -EINVAL;
264 : : }
265 : :
266 : 0 : txq = rte_zmalloc_socket("HN_TXQ", sizeof(*txq), RTE_CACHE_LINE_SIZE,
267 : : socket_id);
268 [ # # ]: 0 : if (!txq)
269 : : return -ENOMEM;
270 : :
271 : 0 : txq->hv = hv;
272 : 0 : txq->chan = hv->channels[queue_idx];
273 : 0 : txq->port_id = dev->data->port_id;
274 : 0 : txq->queue_id = queue_idx;
275 : 0 : txq->free_thresh = tx_free_thresh;
276 : :
277 : 0 : snprintf(name, sizeof(name),
278 : : "hn_txd_%u_%u", dev->data->port_id, queue_idx);
279 : :
280 : 0 : PMD_INIT_LOG(DEBUG, "TX descriptor pool %s n=%u size=%zu",
281 : : name, nb_desc, sizeof(struct hn_txdesc));
282 : :
283 : 0 : txq->tx_rndis_mz = rte_memzone_reserve_aligned(name,
284 : 0 : nb_desc * HN_RNDIS_PKT_ALIGNED, rte_socket_id(),
285 : : RTE_MEMZONE_IOVA_CONTIG, HN_RNDIS_PKT_ALIGNED);
286 [ # # ]: 0 : if (!txq->tx_rndis_mz) {
287 : 0 : err = -rte_errno;
288 : 0 : goto error;
289 : : }
290 : 0 : txq->tx_rndis = txq->tx_rndis_mz->addr;
291 : 0 : txq->tx_rndis_iova = txq->tx_rndis_mz->iova;
292 : :
293 : 0 : txq->txdesc_pool = rte_mempool_create(name, nb_desc,
294 : : sizeof(struct hn_txdesc),
295 : : 0, 0, NULL, NULL,
296 : : hn_txd_init, txq,
297 : 0 : dev->device->numa_node, 0);
298 [ # # ]: 0 : if (txq->txdesc_pool == NULL) {
299 : 0 : PMD_DRV_LOG(ERR,
300 : : "mempool %s create failed: %d", name, rte_errno);
301 : 0 : goto error;
302 : : }
303 : :
304 : : /*
305 : : * If there are more Tx queues than Rx queues, allocate rx_queues
306 : : * with event buffer so that Tx completion messages can still be
307 : : * received
308 : : */
309 [ # # ]: 0 : if (queue_idx >= dev->data->nb_rx_queues) {
310 : 0 : rxq = hn_rx_queue_alloc(hv, queue_idx, socket_id);
311 : :
312 [ # # ]: 0 : if (!rxq) {
313 : : err = -ENOMEM;
314 : 0 : goto error;
315 : : }
316 : :
317 : : /*
318 : : * Don't allocate mbuf pool or rx ring. RSS is always configured
319 : : * to ensure packets aren't received by this Rx queue.
320 : : */
321 : 0 : rxq->mb_pool = NULL;
322 : 0 : rxq->rx_ring = NULL;
323 : : }
324 : :
325 : 0 : txq->agg_szmax = RTE_MIN(hv->chim_szmax, hv->rndis_agg_size);
326 : 0 : txq->agg_pktmax = hv->rndis_agg_pkts;
327 : 0 : txq->agg_align = hv->rndis_agg_align;
328 : :
329 : : hn_reset_txagg(txq);
330 : :
331 : 0 : err = hn_vf_tx_queue_setup(dev, queue_idx, nb_desc,
332 : : socket_id, tx_conf);
333 [ # # ]: 0 : if (err == 0) {
334 : 0 : dev->data->tx_queues[queue_idx] = txq;
335 [ # # ]: 0 : if (rxq != NULL)
336 : 0 : dev->data->rx_queues[queue_idx] = rxq;
337 : 0 : return 0;
338 : : }
339 : :
340 : 0 : error:
341 : 0 : rte_mempool_free(txq->txdesc_pool);
342 : 0 : rte_memzone_free(txq->tx_rndis_mz);
343 : 0 : hn_rx_queue_free_common(rxq);
344 : 0 : rte_free(txq);
345 : 0 : return err;
346 : : }
347 : :
348 : : void
349 : 0 : hn_dev_tx_queue_info(struct rte_eth_dev *dev, uint16_t queue_id,
350 : : struct rte_eth_txq_info *qinfo)
351 : : {
352 : 0 : struct hn_tx_queue *txq = dev->data->tx_queues[queue_id];
353 : :
354 : 0 : qinfo->nb_desc = txq->txdesc_pool->size;
355 : 0 : qinfo->conf.offloads = dev->data->dev_conf.txmode.offloads;
356 : 0 : }
357 : :
358 : 0 : static struct hn_txdesc *hn_txd_get(struct hn_tx_queue *txq)
359 : : {
360 : : struct hn_txdesc *txd;
361 : :
362 [ # # # # ]: 0 : if (rte_mempool_get(txq->txdesc_pool, (void **)&txd)) {
363 : 0 : ++txq->stats.ring_full;
364 : : PMD_TX_LOG(DEBUG, "tx pool exhausted!");
365 : 0 : return NULL;
366 : : }
367 : :
368 : 0 : txd->m = NULL;
369 : 0 : txd->packets = 0;
370 : 0 : txd->data_size = 0;
371 : 0 : txd->chim_size = 0;
372 : :
373 : 0 : return txd;
374 : : }
375 : :
376 : 0 : static void hn_txd_put(struct hn_tx_queue *txq, struct hn_txdesc *txd)
377 : : {
378 [ # # ]: 0 : rte_mempool_put(txq->txdesc_pool, txd);
379 : 0 : }
380 : :
381 : : void
382 : 0 : hn_dev_tx_queue_release(struct rte_eth_dev *dev, uint16_t qid)
383 : : {
384 : 0 : struct hn_tx_queue *txq = dev->data->tx_queues[qid];
385 : :
386 : 0 : PMD_INIT_FUNC_TRACE();
387 : :
388 [ # # ]: 0 : if (!txq)
389 : : return;
390 : : /*
391 : : * Free any Rx queues allocated for a Tx queue without a corresponding
392 : : * Rx queue
393 : : */
394 [ # # ]: 0 : if (qid >= dev->data->nb_rx_queues)
395 : 0 : hn_rx_queue_free_common(dev->data->rx_queues[qid]);
396 : :
397 : 0 : rte_mempool_free(txq->txdesc_pool);
398 : :
399 : 0 : rte_memzone_free(txq->tx_rndis_mz);
400 : 0 : rte_free(txq);
401 : : }
402 : :
403 : : /*
404 : : * Check the status of a Tx descriptor in the queue.
405 : : *
406 : : * returns:
407 : : * - -EINVAL - offset outside of tx_descriptor pool.
408 : : * - RTE_ETH_TX_DESC_FULL - descriptor is not acknowledged by host.
409 : : * - RTE_ETH_TX_DESC_DONE - descriptor is available.
410 : : */
411 : 0 : int hn_dev_tx_descriptor_status(void *arg, uint16_t offset)
412 : : {
413 : : const struct hn_tx_queue *txq = arg;
414 : :
415 : 0 : hn_process_events(txq->hv, txq->queue_id, 0);
416 : :
417 [ # # ]: 0 : if (offset >= rte_mempool_avail_count(txq->txdesc_pool))
418 : : return -EINVAL;
419 : :
420 [ # # ]: 0 : if (offset < rte_mempool_in_use_count(txq->txdesc_pool))
421 : : return RTE_ETH_TX_DESC_FULL;
422 : : else
423 : 0 : return RTE_ETH_TX_DESC_DONE;
424 : : }
425 : :
426 : : static void
427 : 0 : hn_nvs_send_completed(struct rte_eth_dev *dev, uint16_t queue_id,
428 : : unsigned long xactid, const struct hn_nvs_rndis_ack *ack)
429 : : {
430 : 0 : struct hn_data *hv = dev->data->dev_private;
431 : 0 : struct hn_txdesc *txd = (struct hn_txdesc *)xactid;
432 : : struct hn_tx_queue *txq;
433 : :
434 : : /* Control packets are sent with xacid == 0 */
435 [ # # ]: 0 : if (!txd)
436 : : return;
437 : :
438 : 0 : txq = dev->data->tx_queues[queue_id];
439 [ # # ]: 0 : if (likely(ack->status == NVS_STATUS_OK)) {
440 : : PMD_TX_LOG(DEBUG, "port %u:%u complete tx %u packets %u bytes %u",
441 : : txq->port_id, txq->queue_id, txd->chim_index,
442 : : txd->packets, txd->data_size);
443 : 0 : txq->stats.bytes += txd->data_size;
444 : 0 : txq->stats.packets += txd->packets;
445 : : } else {
446 : 0 : PMD_DRV_LOG(NOTICE, "port %u:%u complete tx %u failed status %u",
447 : : txq->port_id, txq->queue_id, txd->chim_index, ack->status);
448 : 0 : ++txq->stats.errors;
449 : : }
450 : :
451 [ # # ]: 0 : if (txd->chim_index != NVS_CHIM_IDX_INVALID) {
452 : 0 : hn_chim_free(hv, txd->chim_index);
453 : 0 : txd->chim_index = NVS_CHIM_IDX_INVALID;
454 : : }
455 : :
456 : 0 : rte_pktmbuf_free(txd->m);
457 : 0 : hn_txd_put(txq, txd);
458 : : }
459 : :
460 : : /* Handle transmit completion events */
461 : : static void
462 : 0 : hn_nvs_handle_comp(struct rte_eth_dev *dev, uint16_t queue_id,
463 : : const struct vmbus_chanpkt_hdr *pkt,
464 : : const void *data)
465 : : {
466 : : const struct hn_nvs_hdr *hdr = data;
467 : :
468 [ # # ]: 0 : switch (hdr->type) {
469 : 0 : case NVS_TYPE_RNDIS_ACK:
470 : 0 : hn_nvs_send_completed(dev, queue_id, pkt->xactid, data);
471 : 0 : break;
472 : :
473 : 0 : default:
474 : 0 : PMD_DRV_LOG(NOTICE, "unexpected send completion type %u",
475 : : hdr->type);
476 : : }
477 : 0 : }
478 : :
479 : : /* Parse per-packet info (meta data) */
480 : : static int
481 : 0 : hn_rndis_rxinfo(const void *info_data, unsigned int info_dlen,
482 : : struct hn_rxinfo *info)
483 : : {
484 : : const struct rndis_pktinfo *pi = info_data;
485 : : uint32_t mask = 0;
486 : :
487 [ # # ]: 0 : while (info_dlen != 0) {
488 : : const void *data;
489 : : uint32_t dlen;
490 : :
491 [ # # ]: 0 : if (unlikely(info_dlen < sizeof(*pi)))
492 : : return -EINVAL;
493 : :
494 [ # # ]: 0 : if (unlikely(info_dlen < pi->size))
495 : : return -EINVAL;
496 : 0 : info_dlen -= pi->size;
497 : :
498 [ # # ]: 0 : if (unlikely(pi->size & RNDIS_PKTINFO_SIZE_ALIGNMASK))
499 : : return -EINVAL;
500 [ # # ]: 0 : if (unlikely(pi->size < pi->offset))
501 : : return -EINVAL;
502 : :
503 : 0 : dlen = pi->size - pi->offset;
504 : : data = pi->data;
505 : :
506 [ # # # # : 0 : switch (pi->type) {
# ]
507 : 0 : case NDIS_PKTINFO_TYPE_VLAN:
508 [ # # ]: 0 : if (unlikely(dlen < NDIS_VLAN_INFO_SIZE))
509 : : return -EINVAL;
510 : 0 : info->vlan_info = *((const uint32_t *)data);
511 : 0 : mask |= HN_RXINFO_VLAN;
512 : 0 : break;
513 : :
514 : 0 : case NDIS_PKTINFO_TYPE_CSUM:
515 [ # # ]: 0 : if (unlikely(dlen < NDIS_RXCSUM_INFO_SIZE))
516 : : return -EINVAL;
517 : 0 : info->csum_info = *((const uint32_t *)data);
518 : 0 : mask |= HN_RXINFO_CSUM;
519 : 0 : break;
520 : :
521 : 0 : case NDIS_PKTINFO_TYPE_HASHVAL:
522 [ # # ]: 0 : if (unlikely(dlen < NDIS_HASH_VALUE_SIZE))
523 : : return -EINVAL;
524 : 0 : info->hash_value = *((const uint32_t *)data);
525 : 0 : mask |= HN_RXINFO_HASHVAL;
526 : 0 : break;
527 : :
528 : 0 : case NDIS_PKTINFO_TYPE_HASHINF:
529 [ # # ]: 0 : if (unlikely(dlen < NDIS_HASH_INFO_SIZE))
530 : : return -EINVAL;
531 : 0 : info->hash_info = *((const uint32_t *)data);
532 : 0 : mask |= HN_RXINFO_HASHINF;
533 : 0 : break;
534 : :
535 : 0 : default:
536 : 0 : goto next;
537 : : }
538 : :
539 [ # # ]: 0 : if (mask == HN_RXINFO_ALL)
540 : : break; /* All found; done */
541 : 0 : next:
542 : 0 : pi = (const struct rndis_pktinfo *)
543 : 0 : ((const uint8_t *)pi + pi->size);
544 : : }
545 : :
546 : : /*
547 : : * Final fixup.
548 : : * - If there is no hash value, invalidate the hash info.
549 : : */
550 [ # # ]: 0 : if (!(mask & HN_RXINFO_HASHVAL))
551 : 0 : info->hash_info = HN_NDIS_HASH_INFO_INVALID;
552 : : return 0;
553 : : }
554 : :
555 : 0 : static void hn_rx_buf_free_cb(void *buf __rte_unused, void *opaque)
556 : : {
557 : : struct hn_rx_bufinfo *rxb = opaque;
558 : 0 : struct hn_rx_queue *rxq = rxb->rxq;
559 : 0 : struct hn_data *hv = rxq->hv;
560 : :
561 : 0 : rte_atomic_fetch_sub_explicit(&rxq->rxbuf_outstanding, 1,
562 : : rte_memory_order_release);
563 : 0 : hn_nvs_ack_rxbuf(hv, rxb->chan, rxb->xactid);
564 : 0 : }
565 : :
566 : 0 : static struct hn_rx_bufinfo *hn_rx_buf_init(struct hn_rx_queue *rxq,
567 : : const struct vmbus_chanpkt_rxbuf *pkt)
568 : : {
569 : : struct hn_rx_bufinfo *rxb;
570 : :
571 : 0 : rxb = rxq->rxbuf_info + pkt->hdr.xactid;
572 : 0 : rxb->chan = rxq->chan;
573 : 0 : rxb->xactid = pkt->hdr.xactid;
574 : 0 : rxb->rxq = rxq;
575 : :
576 : 0 : rxb->shinfo.free_cb = hn_rx_buf_free_cb;
577 : 0 : rxb->shinfo.fcb_opaque = rxb;
578 : : rte_mbuf_ext_refcnt_set(&rxb->shinfo, 1);
579 : 0 : return rxb;
580 : : }
581 : :
582 : 0 : static void hn_rxpkt(struct hn_rx_queue *rxq, struct hn_rx_bufinfo *rxb,
583 : : uint8_t *data, unsigned int headroom, unsigned int dlen,
584 : : const struct hn_rxinfo *info)
585 : : {
586 : 0 : struct hn_data *hv = rxq->hv;
587 : 0 : struct rte_mbuf *m = NULL;
588 : : bool use_extbuf = false;
589 : :
590 [ # # ]: 0 : if (likely(rxq->mb_pool != NULL))
591 : 0 : m = rte_pktmbuf_alloc(rxq->mb_pool);
592 : :
593 [ # # ]: 0 : if (unlikely(!m)) {
594 : : struct rte_eth_dev *dev =
595 : 0 : &rte_eth_devices[rxq->port_id];
596 : :
597 : 0 : dev->data->rx_mbuf_alloc_failed++;
598 : 0 : return;
599 : : }
600 : :
601 : : /*
602 : : * For large packets, avoid copy if possible but need to keep
603 : : * some space available in receive area for later packets.
604 : : */
605 [ # # # # ]: 0 : if (hv->rx_extmbuf_enable && dlen > hv->rx_copybreak &&
606 : 0 : rte_atomic_load_explicit(&rxq->rxbuf_outstanding,
607 [ # # ]: 0 : rte_memory_order_relaxed) < hv->rxbuf_section_cnt / 2) {
608 : : struct rte_mbuf_ext_shared_info *shinfo;
609 : : const void *rxbuf;
610 : : rte_iova_t iova;
611 : :
612 : : /*
613 : : * Build an external mbuf that points to receive area.
614 : : * Use refcount to handle multiple packets in same
615 : : * receive buffer section.
616 : : */
617 : 0 : rxbuf = hv->rxbuf_res.addr;
618 : 0 : iova = rte_mem_virt2iova(rxbuf) + RTE_PTR_DIFF(data, rxbuf);
619 : 0 : shinfo = &rxb->shinfo;
620 : :
621 : : /* shinfo is already set to 1 by the caller */
622 [ # # ]: 0 : if (rte_mbuf_ext_refcnt_update(shinfo, 1) == 2)
623 : 0 : rte_atomic_fetch_add_explicit(&rxq->rxbuf_outstanding, 1,
624 : : rte_memory_order_acquire);
625 : :
626 : 0 : rte_pktmbuf_attach_extbuf(m, data, iova,
627 : 0 : dlen + headroom, shinfo);
628 : 0 : m->data_off = headroom;
629 : : use_extbuf = true;
630 : : } else {
631 : : /* Mbuf's in pool must be large enough to hold small packets */
632 [ # # ]: 0 : if (unlikely(rte_pktmbuf_tailroom(m) < dlen)) {
633 : : rte_pktmbuf_free_seg(m);
634 : 0 : ++rxq->stats.errors;
635 : 0 : return;
636 : : }
637 : 0 : rte_memcpy(rte_pktmbuf_mtod(m, void *),
638 [ # # ]: 0 : data + headroom, dlen);
639 : : }
640 : :
641 : 0 : m->port = rxq->port_id;
642 : 0 : m->pkt_len = dlen;
643 : 0 : m->data_len = dlen;
644 : 0 : m->packet_type = rte_net_get_ptype(m, NULL,
645 : : RTE_PTYPE_L2_MASK |
646 : : RTE_PTYPE_L3_MASK |
647 : : RTE_PTYPE_L4_MASK);
648 : :
649 [ # # ]: 0 : if (info->vlan_info != HN_NDIS_VLAN_INFO_INVALID) {
650 : 0 : m->vlan_tci = RTE_VLAN_TCI_MAKE(NDIS_VLAN_INFO_ID(info->vlan_info),
651 : : NDIS_VLAN_INFO_PRI(info->vlan_info),
652 : : NDIS_VLAN_INFO_CFI(info->vlan_info));
653 : 0 : m->ol_flags |= RTE_MBUF_F_RX_VLAN_STRIPPED | RTE_MBUF_F_RX_VLAN;
654 : :
655 : : /* NDIS always strips tag, put it back if necessary */
656 [ # # # # ]: 0 : if (!hv->vlan_strip && rte_vlan_insert(&m)) {
657 : 0 : PMD_DRV_LOG(DEBUG, "vlan insert failed");
658 : 0 : ++rxq->stats.errors;
659 [ # # ]: 0 : if (use_extbuf)
660 : 0 : rte_pktmbuf_detach_extbuf(m);
661 : 0 : rte_pktmbuf_free(m);
662 : 0 : return;
663 : : }
664 : : }
665 : :
666 [ # # ]: 0 : if (info->csum_info != HN_NDIS_RXCSUM_INFO_INVALID) {
667 [ # # ]: 0 : if (info->csum_info & NDIS_RXCSUM_INFO_IPCS_OK)
668 : 0 : m->ol_flags |= RTE_MBUF_F_RX_IP_CKSUM_GOOD;
669 : :
670 [ # # ]: 0 : if (info->csum_info & (NDIS_RXCSUM_INFO_UDPCS_OK
671 : : | NDIS_RXCSUM_INFO_TCPCS_OK))
672 : 0 : m->ol_flags |= RTE_MBUF_F_RX_L4_CKSUM_GOOD;
673 [ # # ]: 0 : else if (info->csum_info & (NDIS_RXCSUM_INFO_TCPCS_FAILED
674 : : | NDIS_RXCSUM_INFO_UDPCS_FAILED))
675 : 0 : m->ol_flags |= RTE_MBUF_F_RX_L4_CKSUM_BAD;
676 : : }
677 : :
678 [ # # ]: 0 : if (info->hash_info != HN_NDIS_HASH_INFO_INVALID) {
679 : 0 : m->ol_flags |= RTE_MBUF_F_RX_RSS_HASH;
680 : 0 : m->hash.rss = info->hash_value;
681 : : }
682 : :
683 : : PMD_RX_LOG(DEBUG,
684 : : "port %u:%u RX id %"PRIu64" size %u type %#x ol_flags %#"PRIx64,
685 : : rxq->port_id, rxq->queue_id, rxb->xactid,
686 : : m->pkt_len, m->packet_type, m->ol_flags);
687 : :
688 : 0 : ++rxq->stats.packets;
689 : 0 : rxq->stats.bytes += m->pkt_len;
690 : 0 : hn_update_packet_stats(&rxq->stats, m);
691 : :
692 [ # # # # ]: 0 : if (unlikely(rte_ring_sp_enqueue(rxq->rx_ring, m) != 0)) {
693 : 0 : ++rxq->stats.ring_full;
694 : : PMD_RX_LOG(DEBUG, "rx ring full");
695 [ # # ]: 0 : if (use_extbuf)
696 : 0 : rte_pktmbuf_detach_extbuf(m);
697 : 0 : rte_pktmbuf_free(m);
698 : : }
699 : : }
700 : :
701 : 0 : static void hn_rndis_rx_data(struct hn_rx_queue *rxq,
702 : : struct hn_rx_bufinfo *rxb,
703 : : void *data, uint32_t dlen)
704 : : {
705 : : unsigned int data_off, data_len;
706 : : unsigned int pktinfo_off, pktinfo_len;
707 : : const struct rndis_packet_msg *pkt = data;
708 : 0 : struct hn_rxinfo info = {
709 : : .vlan_info = HN_NDIS_VLAN_INFO_INVALID,
710 : : .csum_info = HN_NDIS_RXCSUM_INFO_INVALID,
711 : : .hash_info = HN_NDIS_HASH_INFO_INVALID,
712 : : };
713 : : int err;
714 : :
715 : : hn_rndis_dump(pkt);
716 : :
717 [ # # ]: 0 : if (unlikely(dlen < sizeof(*pkt)))
718 : 0 : goto error;
719 : :
720 [ # # ]: 0 : if (unlikely(dlen < pkt->len))
721 : 0 : goto error; /* truncated RNDIS from host */
722 : :
723 [ # # ]: 0 : if (unlikely(pkt->len < pkt->datalen
724 : : + pkt->oobdatalen + pkt->pktinfolen))
725 : 0 : goto error;
726 : :
727 [ # # ]: 0 : if (unlikely(pkt->datalen == 0))
728 : 0 : goto error;
729 : :
730 : : /* Check offsets. */
731 [ # # ]: 0 : if (unlikely(pkt->dataoffset < RNDIS_PACKET_MSG_OFFSET_MIN))
732 : 0 : goto error;
733 : :
734 [ # # ]: 0 : if (likely(pkt->pktinfooffset > 0) &&
735 [ # # # # ]: 0 : unlikely(pkt->pktinfooffset < RNDIS_PACKET_MSG_OFFSET_MIN ||
736 : : (pkt->pktinfooffset & RNDIS_PACKET_MSG_OFFSET_ALIGNMASK)))
737 : 0 : goto error;
738 : :
739 : 0 : data_off = RNDIS_PACKET_MSG_OFFSET_ABS(pkt->dataoffset);
740 : : data_len = pkt->datalen;
741 : 0 : pktinfo_off = RNDIS_PACKET_MSG_OFFSET_ABS(pkt->pktinfooffset);
742 : : pktinfo_len = pkt->pktinfolen;
743 : :
744 [ # # ]: 0 : if (likely(pktinfo_len > 0)) {
745 : 0 : err = hn_rndis_rxinfo((const uint8_t *)pkt + pktinfo_off,
746 : : pktinfo_len, &info);
747 [ # # ]: 0 : if (err)
748 : 0 : goto error;
749 : : }
750 : :
751 : : /* overflow check */
752 [ # # # # ]: 0 : if (data_len > data_len + data_off || data_len + data_off > pkt->len)
753 : 0 : goto error;
754 : :
755 [ # # ]: 0 : if (unlikely(data_len < RTE_ETHER_HDR_LEN))
756 : 0 : goto error;
757 : :
758 : 0 : hn_rxpkt(rxq, rxb, data, data_off, data_len, &info);
759 : 0 : return;
760 : 0 : error:
761 : 0 : ++rxq->stats.errors;
762 : : }
763 : :
764 : : static void
765 : 0 : hn_rndis_receive(struct rte_eth_dev *dev, struct hn_rx_queue *rxq,
766 : : struct hn_rx_bufinfo *rxb, void *buf, uint32_t len)
767 : : {
768 : : const struct rndis_msghdr *hdr = buf;
769 : :
770 [ # # # # ]: 0 : switch (hdr->type) {
771 : 0 : case RNDIS_PACKET_MSG:
772 [ # # ]: 0 : if (dev->data->dev_started)
773 : 0 : hn_rndis_rx_data(rxq, rxb, buf, len);
774 : : break;
775 : :
776 : 0 : case RNDIS_INDICATE_STATUS_MSG:
777 : 0 : hn_rndis_link_status(dev, buf);
778 : 0 : break;
779 : :
780 : 0 : case RNDIS_INITIALIZE_CMPLT:
781 : : case RNDIS_QUERY_CMPLT:
782 : : case RNDIS_SET_CMPLT:
783 : 0 : hn_rndis_receive_response(rxq->hv, buf, len);
784 : 0 : break;
785 : :
786 : 0 : default:
787 : 0 : PMD_DRV_LOG(NOTICE,
788 : : "unexpected RNDIS message (type %#x len %u)",
789 : : hdr->type, len);
790 : 0 : break;
791 : : }
792 : 0 : }
793 : :
794 : : static void
795 : 0 : hn_nvs_handle_rxbuf(struct rte_eth_dev *dev,
796 : : struct hn_data *hv,
797 : : struct hn_rx_queue *rxq,
798 : : const struct vmbus_chanpkt_hdr *hdr,
799 : : const void *buf)
800 : : {
801 : : const struct vmbus_chanpkt_rxbuf *pkt;
802 : : const struct hn_nvs_hdr *nvs_hdr = buf;
803 : 0 : uint32_t rxbuf_sz = hv->rxbuf_res.len;
804 [ # # ]: 0 : char *rxbuf = hv->rxbuf_res.addr;
805 : : unsigned int i, hlen, count;
806 : : struct hn_rx_bufinfo *rxb;
807 : :
808 : : /* At minimum we need type header */
809 [ # # ]: 0 : if (unlikely(vmbus_chanpkt_datalen(hdr) < sizeof(*nvs_hdr))) {
810 : : PMD_RX_LOG(ERR, "invalid receive nvs RNDIS");
811 : : return;
812 : : }
813 : :
814 : : /* Make sure that this is a RNDIS message. */
815 [ # # ]: 0 : if (unlikely(nvs_hdr->type != NVS_TYPE_RNDIS)) {
816 : : PMD_RX_LOG(ERR, "nvs type %u, not RNDIS",
817 : : nvs_hdr->type);
818 : : return;
819 : : }
820 : :
821 : : hlen = vmbus_chanpkt_getlen(hdr->hlen);
822 [ # # ]: 0 : if (unlikely(hlen < sizeof(*pkt))) {
823 : : PMD_RX_LOG(ERR, "invalid rxbuf chanpkt");
824 : : return;
825 : : }
826 : :
827 : : pkt = container_of(hdr, const struct vmbus_chanpkt_rxbuf, hdr);
828 [ # # ]: 0 : if (unlikely(pkt->rxbuf_id != NVS_RXBUF_SIG)) {
829 : : PMD_RX_LOG(ERR, "invalid rxbuf_id 0x%08x",
830 : : pkt->rxbuf_id);
831 : : return;
832 : : }
833 : :
834 : 0 : count = pkt->rxbuf_cnt;
835 [ # # ]: 0 : if (unlikely(hlen < offsetof(struct vmbus_chanpkt_rxbuf,
836 : : rxbuf[count]))) {
837 : : PMD_RX_LOG(ERR, "invalid rxbuf_cnt %u", count);
838 : : return;
839 : : }
840 : :
841 [ # # ]: 0 : if (pkt->hdr.xactid > hv->rxbuf_section_cnt) {
842 : : PMD_RX_LOG(ERR, "invalid rxbuf section id %" PRIx64,
843 : : pkt->hdr.xactid);
844 : : return;
845 : : }
846 : :
847 : : /* Setup receive buffer info to allow for callback */
848 : 0 : rxb = hn_rx_buf_init(rxq, pkt);
849 : :
850 : : /* Each range represents 1 RNDIS pkt that contains 1 Ethernet frame */
851 [ # # ]: 0 : for (i = 0; i < count; ++i) {
852 : : unsigned int ofs, len;
853 : :
854 : 0 : ofs = pkt->rxbuf[i].ofs;
855 : 0 : len = pkt->rxbuf[i].len;
856 : :
857 [ # # ]: 0 : if (unlikely(ofs + len > rxbuf_sz)) {
858 : : PMD_RX_LOG(ERR,
859 : : "%uth RNDIS msg overflow ofs %u, len %u",
860 : : i, ofs, len);
861 : 0 : continue;
862 : : }
863 : :
864 [ # # ]: 0 : if (unlikely(len == 0)) {
865 : : PMD_RX_LOG(ERR, "%uth RNDIS msg len %u", i, len);
866 : 0 : continue;
867 : : }
868 : :
869 : 0 : hn_rndis_receive(dev, rxq, rxb,
870 : 0 : rxbuf + ofs, len);
871 : : }
872 : :
873 : : /* Send ACK now if external mbuf not used */
874 [ # # ]: 0 : if (rte_mbuf_ext_refcnt_update(&rxb->shinfo, -1) == 0)
875 : 0 : hn_nvs_ack_rxbuf(hv, rxb->chan, rxb->xactid);
876 : : }
877 : :
878 : : /*
879 : : * Called when NVS inband events are received.
880 : : * Send up a two part message with port_id and the NVS message
881 : : * to the pipe to the netvsc-vf-event control thread.
882 : : */
883 : 0 : static void hn_nvs_handle_notify(struct rte_eth_dev *dev,
884 : : const struct vmbus_chanpkt_hdr *pkt,
885 : : const void *data)
886 : : {
887 : : const struct hn_nvs_hdr *hdr = data;
888 : :
889 [ # # # ]: 0 : switch (hdr->type) {
890 : 0 : case NVS_TYPE_TXTBL_NOTE:
891 : : /* Transmit indirection table has locking problems
892 : : * in DPDK and therefore not implemented
893 : : */
894 : 0 : PMD_DRV_LOG(DEBUG, "host notify of transmit indirection table");
895 : 0 : break;
896 : :
897 : 0 : case NVS_TYPE_VFASSOC_NOTE:
898 : 0 : hn_nvs_handle_vfassoc(dev, pkt, data);
899 : 0 : break;
900 : :
901 : 0 : default:
902 : 0 : PMD_DRV_LOG(INFO,
903 : : "got notify, nvs type %u", hdr->type);
904 : : }
905 : 0 : }
906 : :
907 : 0 : struct hn_rx_queue *hn_rx_queue_alloc(struct hn_data *hv,
908 : : uint16_t queue_id,
909 : : unsigned int socket_id)
910 : : {
911 : : struct hn_rx_queue *rxq;
912 : :
913 : 0 : rxq = rte_zmalloc_socket("HN_RXQ", sizeof(*rxq),
914 : : RTE_CACHE_LINE_SIZE, socket_id);
915 [ # # ]: 0 : if (!rxq)
916 : : return NULL;
917 : :
918 : 0 : rxq->hv = hv;
919 : 0 : rxq->chan = hv->channels[queue_id];
920 : : rte_spinlock_init(&rxq->ring_lock);
921 : 0 : rxq->port_id = hv->port_id;
922 : 0 : rxq->queue_id = queue_id;
923 : 0 : rxq->event_sz = HN_RXQ_EVENT_DEFAULT;
924 : 0 : rxq->event_buf = rte_malloc_socket("HN_EVENTS", HN_RXQ_EVENT_DEFAULT,
925 : : RTE_CACHE_LINE_SIZE, socket_id);
926 [ # # ]: 0 : if (!rxq->event_buf) {
927 : 0 : rte_free(rxq);
928 : 0 : return NULL;
929 : : }
930 : :
931 : : /* setup rxbuf_info for non-primary queue */
932 [ # # ]: 0 : if (queue_id) {
933 : 0 : rxq->rxbuf_info = rte_calloc("HN_RXBUF_INFO",
934 : 0 : hv->rxbuf_section_cnt,
935 : : sizeof(*rxq->rxbuf_info),
936 : : RTE_CACHE_LINE_SIZE);
937 : :
938 [ # # ]: 0 : if (!rxq->rxbuf_info) {
939 : 0 : PMD_DRV_LOG(ERR,
940 : : "Could not allocate rxbuf info for queue %d",
941 : : queue_id);
942 : 0 : rte_free(rxq->event_buf);
943 : 0 : rte_free(rxq);
944 : 0 : return NULL;
945 : : }
946 : : }
947 : :
948 : : return rxq;
949 : : }
950 : :
951 : : void
952 : 0 : hn_dev_rx_queue_info(struct rte_eth_dev *dev, uint16_t queue_id,
953 : : struct rte_eth_rxq_info *qinfo)
954 : : {
955 : 0 : struct hn_rx_queue *rxq = dev->data->rx_queues[queue_id];
956 : :
957 : 0 : qinfo->mp = rxq->mb_pool;
958 : 0 : qinfo->nb_desc = rxq->rx_ring->size;
959 : 0 : qinfo->conf.offloads = dev->data->dev_conf.rxmode.offloads;
960 : 0 : }
961 : :
962 : : int
963 : 0 : hn_dev_rx_queue_setup(struct rte_eth_dev *dev,
964 : : uint16_t queue_idx, uint16_t nb_desc,
965 : : unsigned int socket_id,
966 : : const struct rte_eth_rxconf *rx_conf,
967 : : struct rte_mempool *mp)
968 : : {
969 : 0 : struct hn_data *hv = dev->data->dev_private;
970 : : char ring_name[RTE_RING_NAMESIZE];
971 : : struct hn_rx_queue *rxq;
972 : : unsigned int count;
973 : : int error = -ENOMEM;
974 : :
975 : 0 : PMD_INIT_FUNC_TRACE();
976 : :
977 [ # # ]: 0 : if (queue_idx == 0) {
978 : 0 : rxq = hv->primary;
979 : : } else {
980 : : /*
981 : : * If the number of Tx queues was previously greater than the
982 : : * number of Rx queues, we may already have allocated an rxq.
983 : : */
984 [ # # ]: 0 : if (!dev->data->rx_queues[queue_idx])
985 : 0 : rxq = hn_rx_queue_alloc(hv, queue_idx, socket_id);
986 : : else
987 : : rxq = dev->data->rx_queues[queue_idx];
988 : :
989 [ # # ]: 0 : if (!rxq)
990 : : return -ENOMEM;
991 : : }
992 : :
993 : 0 : rxq->mb_pool = mp;
994 : 0 : count = rte_mempool_avail_count(mp) / dev->data->nb_rx_queues;
995 [ # # # # ]: 0 : if (nb_desc == 0 || nb_desc > count)
996 : 0 : nb_desc = count;
997 : :
998 : : /*
999 : : * Staging ring from receive event logic to rx_pkts.
1000 : : * rx_pkts assumes caller is handling multi-thread issue.
1001 : : * event logic has locking.
1002 : : */
1003 : 0 : snprintf(ring_name, sizeof(ring_name),
1004 : 0 : "hn_rx_%u_%u", dev->data->port_id, queue_idx);
1005 : 0 : rxq->rx_ring = rte_ring_create(ring_name,
1006 : : rte_align32pow2(nb_desc),
1007 : : socket_id, 0);
1008 [ # # ]: 0 : if (!rxq->rx_ring)
1009 : 0 : goto fail;
1010 : :
1011 : 0 : error = hn_vf_rx_queue_setup(dev, queue_idx, nb_desc,
1012 : : socket_id, rx_conf, mp);
1013 [ # # ]: 0 : if (error)
1014 : 0 : goto fail;
1015 : :
1016 : 0 : dev->data->rx_queues[queue_idx] = rxq;
1017 : 0 : return 0;
1018 : :
1019 : 0 : fail:
1020 : 0 : rte_ring_free(rxq->rx_ring);
1021 : : /* Only free rxq if it was created in this function. */
1022 [ # # ]: 0 : if (!dev->data->rx_queues[queue_idx])
1023 : 0 : hn_rx_queue_free_common(rxq);
1024 : :
1025 : : return error;
1026 : : }
1027 : :
1028 : : static void
1029 : 0 : hn_rx_queue_free(struct hn_rx_queue *rxq, bool keep_primary)
1030 : : {
1031 : :
1032 [ # # ]: 0 : if (!rxq)
1033 : : return;
1034 : :
1035 : 0 : rte_ring_free(rxq->rx_ring);
1036 : 0 : rxq->rx_ring = NULL;
1037 : 0 : rxq->mb_pool = NULL;
1038 : :
1039 : 0 : hn_vf_rx_queue_release(rxq->hv, rxq->queue_id);
1040 : :
1041 : : /* Keep primary queue to allow for control operations */
1042 [ # # # # ]: 0 : if (keep_primary && rxq == rxq->hv->primary)
1043 : : return;
1044 : :
1045 : 0 : hn_rx_queue_free_common(rxq);
1046 : : }
1047 : :
1048 : : void
1049 : 0 : hn_dev_rx_queue_release(struct rte_eth_dev *dev, uint16_t qid)
1050 : : {
1051 : 0 : struct hn_rx_queue *rxq = dev->data->rx_queues[qid];
1052 : :
1053 : 0 : PMD_INIT_FUNC_TRACE();
1054 : :
1055 : 0 : hn_rx_queue_free(rxq, true);
1056 : 0 : }
1057 : :
1058 : : /*
1059 : : * Get the number of used descriptor in a rx queue
1060 : : * For this device that means how many packets are pending in the ring.
1061 : : */
1062 : : int
1063 : 0 : hn_dev_rx_queue_count(void *rx_queue)
1064 : : {
1065 : : struct hn_rx_queue *rxq = rx_queue;
1066 : :
1067 : 0 : return rte_ring_count(rxq->rx_ring);
1068 : : }
1069 : :
1070 : : /*
1071 : : * Check the status of a Rx descriptor in the queue
1072 : : *
1073 : : * returns:
1074 : : * - -EINVAL - offset outside of ring
1075 : : * - RTE_ETH_RX_DESC_AVAIL - no data available yet
1076 : : * - RTE_ETH_RX_DESC_DONE - data is waiting in staging ring
1077 : : */
1078 : 0 : int hn_dev_rx_queue_status(void *arg, uint16_t offset)
1079 : : {
1080 : : const struct hn_rx_queue *rxq = arg;
1081 : :
1082 : 0 : hn_process_events(rxq->hv, rxq->queue_id, 0);
1083 [ # # ]: 0 : if (offset >= rxq->rx_ring->capacity)
1084 : : return -EINVAL;
1085 : :
1086 [ # # ]: 0 : if (offset < rte_ring_count(rxq->rx_ring))
1087 : : return RTE_ETH_RX_DESC_DONE;
1088 : : else
1089 : 0 : return RTE_ETH_RX_DESC_AVAIL;
1090 : : }
1091 : :
1092 : : int
1093 : 0 : hn_dev_tx_done_cleanup(void *arg, uint32_t free_cnt)
1094 : : {
1095 : : struct hn_tx_queue *txq = arg;
1096 : :
1097 : 0 : return hn_process_events(txq->hv, txq->queue_id, free_cnt);
1098 : : }
1099 : :
1100 : : /*
1101 : : * Process pending events on the channel.
1102 : : * Called from both Rx queue poll and Tx cleanup
1103 : : */
1104 : 0 : uint32_t hn_process_events(struct hn_data *hv, uint16_t queue_id,
1105 : : uint32_t tx_limit)
1106 : : {
1107 : 0 : struct rte_eth_dev *dev = &rte_eth_devices[hv->port_id];
1108 : : struct hn_rx_queue *rxq;
1109 : : uint32_t bytes_read = 0;
1110 : : uint32_t tx_done = 0;
1111 : : int ret = 0;
1112 : :
1113 [ # # ]: 0 : rxq = queue_id == 0 ? hv->primary : dev->data->rx_queues[queue_id];
1114 : :
1115 : : /*
1116 : : * Since channel is shared between Rx and TX queue need to have a lock
1117 : : * since DPDK does not force same CPU to be used for Rx/Tx.
1118 : : */
1119 [ # # ]: 0 : if (unlikely(!rte_spinlock_trylock(&rxq->ring_lock)))
1120 : : return 0;
1121 : :
1122 : 0 : for (;;) {
1123 : : const struct vmbus_chanpkt_hdr *pkt;
1124 : 0 : uint32_t len = rxq->event_sz;
1125 : : const void *data;
1126 : :
1127 : 0 : retry:
1128 : 0 : ret = rte_vmbus_chan_recv_raw(rxq->chan, rxq->event_buf, &len);
1129 [ # # ]: 0 : if (ret == -EAGAIN)
1130 : : break; /* ring is empty */
1131 : :
1132 [ # # ]: 0 : if (unlikely(ret == -ENOBUFS)) {
1133 : : /* event buffer not large enough to read ring */
1134 : :
1135 : 0 : PMD_DRV_LOG(DEBUG,
1136 : : "event buffer expansion (need %u)", len);
1137 : 0 : rxq->event_sz = len + len / 4;
1138 : 0 : rxq->event_buf = rte_realloc(rxq->event_buf, rxq->event_sz,
1139 : : RTE_CACHE_LINE_SIZE);
1140 [ # # ]: 0 : if (rxq->event_buf)
1141 : 0 : goto retry;
1142 : : /* out of memory, no more events now */
1143 : 0 : rxq->event_sz = 0;
1144 : 0 : break;
1145 : : }
1146 : :
1147 [ # # ]: 0 : if (unlikely(ret <= 0)) {
1148 : : /* This indicates a failure to communicate (or worse) */
1149 : 0 : rte_exit(EXIT_FAILURE,
1150 : : "vmbus ring buffer error: %d", ret);
1151 : : }
1152 : :
1153 : 0 : bytes_read += ret;
1154 : 0 : pkt = (const struct vmbus_chanpkt_hdr *)rxq->event_buf;
1155 [ # # # # ]: 0 : data = (char *)rxq->event_buf + vmbus_chanpkt_getlen(pkt->hlen);
1156 : :
1157 [ # # # # ]: 0 : switch (pkt->type) {
1158 : 0 : case VMBUS_CHANPKT_TYPE_COMP:
1159 : 0 : ++tx_done;
1160 : 0 : hn_nvs_handle_comp(dev, queue_id, pkt, data);
1161 : 0 : break;
1162 : :
1163 : 0 : case VMBUS_CHANPKT_TYPE_RXBUF:
1164 : 0 : hn_nvs_handle_rxbuf(dev, hv, rxq, pkt, data);
1165 : 0 : break;
1166 : :
1167 : 0 : case VMBUS_CHANPKT_TYPE_INBAND:
1168 : 0 : hn_nvs_handle_notify(dev, pkt, data);
1169 : 0 : break;
1170 : :
1171 : 0 : default:
1172 : 0 : PMD_DRV_LOG(ERR, "unknown chan pkt %u", pkt->type);
1173 : 0 : break;
1174 : : }
1175 : :
1176 [ # # ]: 0 : if (tx_limit && tx_done >= tx_limit)
1177 : : break;
1178 : : }
1179 : :
1180 [ # # ]: 0 : if (bytes_read > 0)
1181 : 0 : rte_vmbus_chan_signal_read(hn_nvs_get_vmbus_device(hv), rxq->chan, bytes_read);
1182 : :
1183 : : rte_spinlock_unlock(&rxq->ring_lock);
1184 : :
1185 : 0 : return tx_done;
1186 : : }
1187 : :
1188 : 0 : static void hn_append_to_chim(struct hn_tx_queue *txq,
1189 : : struct rndis_packet_msg *pkt,
1190 : : const struct rte_mbuf *m)
1191 : : {
1192 : 0 : struct hn_txdesc *txd = txq->agg_txd;
1193 : : uint8_t *buf = (uint8_t *)pkt;
1194 : : unsigned int data_offs;
1195 : :
1196 : : hn_rndis_dump(pkt);
1197 : :
1198 : 0 : data_offs = RNDIS_PACKET_MSG_OFFSET_ABS(pkt->dataoffset);
1199 : 0 : txd->chim_size += pkt->len;
1200 : 0 : txd->data_size += m->pkt_len;
1201 : 0 : ++txd->packets;
1202 : 0 : hn_update_packet_stats(&txq->stats, m);
1203 : :
1204 [ # # ]: 0 : for (; m; m = m->next) {
1205 : 0 : uint16_t len = rte_pktmbuf_data_len(m);
1206 : :
1207 : 0 : rte_memcpy(buf + data_offs,
1208 [ # # ]: 0 : rte_pktmbuf_mtod(m, const char *), len);
1209 : 0 : data_offs += len;
1210 : : }
1211 : 0 : }
1212 : :
1213 : : /*
1214 : : * Send pending aggregated data in chimney buffer (if any).
1215 : : * Returns error if send was unsuccessful because channel ring buffer
1216 : : * was full.
1217 : : */
1218 : 0 : static int hn_flush_txagg(struct hn_tx_queue *txq, bool *need_sig)
1219 : :
1220 : : {
1221 : 0 : struct hn_txdesc *txd = txq->agg_txd;
1222 : : struct hn_nvs_rndis rndis;
1223 : : int ret;
1224 : :
1225 [ # # ]: 0 : if (!txd)
1226 : : return 0;
1227 : :
1228 : 0 : rndis = (struct hn_nvs_rndis) {
1229 : : .type = NVS_TYPE_RNDIS,
1230 : : .rndis_mtype = NVS_RNDIS_MTYPE_DATA,
1231 : 0 : .chim_idx = txd->chim_index,
1232 : 0 : .chim_sz = txd->chim_size,
1233 : : };
1234 : :
1235 : : PMD_TX_LOG(DEBUG, "port %u:%u tx %u size %u",
1236 : : txq->port_id, txq->queue_id, txd->chim_index, txd->chim_size);
1237 : :
1238 : 0 : ret = hn_nvs_send(txq->hv, txq->chan, VMBUS_CHANPKT_FLAG_RC,
1239 : : &rndis, sizeof(rndis), (uintptr_t)txd, need_sig);
1240 : :
1241 [ # # ]: 0 : if (likely(ret == 0))
1242 : : hn_reset_txagg(txq);
1243 [ # # ]: 0 : else if (ret == -EAGAIN) {
1244 : : PMD_TX_LOG(DEBUG, "port %u:%u channel full",
1245 : : txq->port_id, txq->queue_id);
1246 : 0 : ++txq->stats.channel_full;
1247 : : } else {
1248 : 0 : ++txq->stats.errors;
1249 : :
1250 : 0 : PMD_DRV_LOG(NOTICE, "port %u:%u send failed: %d",
1251 : : txq->port_id, txq->queue_id, ret);
1252 : : }
1253 : : return ret;
1254 : : }
1255 : :
1256 : : /*
1257 : : * Try and find a place in a send chimney buffer to put
1258 : : * the small packet. If space is available, this routine
1259 : : * returns a pointer of where to place the data.
1260 : : * If no space, caller should try direct transmit.
1261 : : */
1262 : : static void *
1263 : 0 : hn_try_txagg(struct hn_data *hv, struct hn_tx_queue *txq,
1264 : : struct hn_txdesc *txd, uint32_t pktsize)
1265 : : {
1266 : 0 : struct hn_txdesc *agg_txd = txq->agg_txd;
1267 : : struct rndis_packet_msg *pkt;
1268 : : void *chim;
1269 : :
1270 [ # # ]: 0 : if (agg_txd) {
1271 : : unsigned int padding, olen;
1272 : :
1273 : : /*
1274 : : * Update the previous RNDIS packet's total length,
1275 : : * it can be increased due to the mandatory alignment
1276 : : * padding for this RNDIS packet. And update the
1277 : : * aggregating txdesc's chimney sending buffer size
1278 : : * accordingly.
1279 : : *
1280 : : * Zero-out the padding, as required by the RNDIS spec.
1281 : : */
1282 : 0 : pkt = txq->agg_prevpkt;
1283 : 0 : olen = pkt->len;
1284 : 0 : padding = RTE_ALIGN(olen, txq->agg_align) - olen;
1285 [ # # ]: 0 : if (padding > 0) {
1286 : 0 : agg_txd->chim_size += padding;
1287 : 0 : pkt->len += padding;
1288 : 0 : memset((uint8_t *)pkt + olen, 0, padding);
1289 : : }
1290 : :
1291 : 0 : chim = (uint8_t *)pkt + pkt->len;
1292 : 0 : txq->agg_prevpkt = chim;
1293 : 0 : txq->agg_pktleft--;
1294 : 0 : txq->agg_szleft -= pktsize;
1295 [ # # ]: 0 : if (txq->agg_szleft < HN_PKTSIZE_MIN(txq->agg_align)) {
1296 : : /*
1297 : : * Probably can't aggregate more packets,
1298 : : * flush this aggregating txdesc proactively.
1299 : : */
1300 : 0 : txq->agg_pktleft = 0;
1301 : : }
1302 : :
1303 : 0 : hn_txd_put(txq, txd);
1304 : 0 : return chim;
1305 : : }
1306 : :
1307 : 0 : txd->chim_index = hn_chim_alloc(hv);
1308 [ # # ]: 0 : if (txd->chim_index == NVS_CHIM_IDX_INVALID)
1309 : : return NULL;
1310 : :
1311 : 0 : chim = (uint8_t *)hv->chim_res.addr
1312 : 0 : + txd->chim_index * hv->chim_szmax;
1313 : :
1314 : 0 : txq->agg_txd = txd;
1315 : 0 : txq->agg_pktleft = txq->agg_pktmax - 1;
1316 : 0 : txq->agg_szleft = txq->agg_szmax - pktsize;
1317 : 0 : txq->agg_prevpkt = chim;
1318 : :
1319 : 0 : return chim;
1320 : : }
1321 : :
1322 : : static inline void *
1323 : : hn_rndis_pktinfo_append(struct rndis_packet_msg *pkt,
1324 : : uint32_t pi_dlen, uint32_t pi_type)
1325 : : {
1326 : : const uint32_t pi_size = RNDIS_PKTINFO_SIZE(pi_dlen);
1327 : : struct rndis_pktinfo *pi;
1328 : :
1329 : : /*
1330 : : * Per-packet-info does not move; it only grows.
1331 : : *
1332 : : * NOTE:
1333 : : * pktinfooffset in this phase counts from the beginning
1334 : : * of rndis_packet_msg.
1335 : : */
1336 : 0 : pi = (struct rndis_pktinfo *)((uint8_t *)pkt + hn_rndis_pktlen(pkt));
1337 : :
1338 : 0 : pkt->pktinfolen += pi_size;
1339 : :
1340 : 0 : pi->size = pi_size;
1341 : 0 : pi->type = pi_type;
1342 : 0 : pi->offset = RNDIS_PKTINFO_OFFSET;
1343 : :
1344 : : return pi->data;
1345 : : }
1346 : :
1347 : : /* Put RNDIS header and packet info on packet */
1348 : 0 : static void hn_encap(struct rndis_packet_msg *pkt,
1349 : : uint16_t queue_id,
1350 : : const struct rte_mbuf *m)
1351 : : {
1352 : 0 : unsigned int hlen = m->l2_len + m->l3_len;
1353 : : uint32_t *pi_data;
1354 : : uint32_t pkt_hlen;
1355 : :
1356 : 0 : pkt->type = RNDIS_PACKET_MSG;
1357 : 0 : pkt->len = m->pkt_len;
1358 : 0 : pkt->dataoffset = 0;
1359 : 0 : pkt->datalen = m->pkt_len;
1360 : 0 : pkt->oobdataoffset = 0;
1361 : 0 : pkt->oobdatalen = 0;
1362 : 0 : pkt->oobdataelements = 0;
1363 : 0 : pkt->pktinfooffset = sizeof(*pkt);
1364 : : pkt->pktinfolen = 0;
1365 : 0 : pkt->vchandle = 0;
1366 : 0 : pkt->reserved = 0;
1367 : :
1368 : : /*
1369 : : * Set the hash value for this packet, to the queue_id to cause
1370 : : * TX done event for this packet on the right channel.
1371 : : */
1372 : : pi_data = hn_rndis_pktinfo_append(pkt, NDIS_HASH_VALUE_SIZE,
1373 : : NDIS_PKTINFO_TYPE_HASHVAL);
1374 : 0 : *pi_data = queue_id;
1375 : :
1376 [ # # ]: 0 : if (m->ol_flags & RTE_MBUF_F_TX_VLAN) {
1377 : : pi_data = hn_rndis_pktinfo_append(pkt, NDIS_VLAN_INFO_SIZE,
1378 : : NDIS_PKTINFO_TYPE_VLAN);
1379 : 0 : *pi_data = NDIS_VLAN_INFO_MAKE(RTE_VLAN_TCI_ID(m->vlan_tci),
1380 : : RTE_VLAN_TCI_PRI(m->vlan_tci),
1381 : : RTE_VLAN_TCI_DEI(m->vlan_tci));
1382 : : }
1383 : :
1384 [ # # ]: 0 : if (m->ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
1385 : : pi_data = hn_rndis_pktinfo_append(pkt, NDIS_LSO2_INFO_SIZE,
1386 : : NDIS_PKTINFO_TYPE_LSO);
1387 : :
1388 [ # # ]: 0 : if (m->ol_flags & RTE_MBUF_F_TX_IPV6) {
1389 : 0 : *pi_data = NDIS_LSO2_INFO_MAKEIPV6(hlen,
1390 : : m->tso_segsz);
1391 : : } else {
1392 : 0 : *pi_data = NDIS_LSO2_INFO_MAKEIPV4(hlen,
1393 : : m->tso_segsz);
1394 : : }
1395 : 0 : } else if ((m->ol_flags & RTE_MBUF_F_TX_L4_MASK) ==
1396 [ # # ]: 0 : RTE_MBUF_F_TX_TCP_CKSUM ||
1397 : : (m->ol_flags & RTE_MBUF_F_TX_L4_MASK) ==
1398 : 0 : RTE_MBUF_F_TX_UDP_CKSUM ||
1399 [ # # ]: 0 : (m->ol_flags & RTE_MBUF_F_TX_IP_CKSUM)) {
1400 : : pi_data = hn_rndis_pktinfo_append(pkt, NDIS_TXCSUM_INFO_SIZE,
1401 : : NDIS_PKTINFO_TYPE_CSUM);
1402 : 0 : *pi_data = 0;
1403 : :
1404 [ # # ]: 0 : if (m->ol_flags & RTE_MBUF_F_TX_IPV6)
1405 : 0 : *pi_data |= NDIS_TXCSUM_INFO_IPV6;
1406 [ # # ]: 0 : if (m->ol_flags & RTE_MBUF_F_TX_IPV4) {
1407 : 0 : *pi_data |= NDIS_TXCSUM_INFO_IPV4;
1408 : :
1409 [ # # ]: 0 : if (m->ol_flags & RTE_MBUF_F_TX_IP_CKSUM)
1410 : 0 : *pi_data |= NDIS_TXCSUM_INFO_IPCS;
1411 : : }
1412 : :
1413 [ # # ]: 0 : if ((m->ol_flags & RTE_MBUF_F_TX_L4_MASK) ==
1414 : : RTE_MBUF_F_TX_TCP_CKSUM)
1415 : 0 : *pi_data |= NDIS_TXCSUM_INFO_MKTCPCS(hlen);
1416 [ # # ]: 0 : else if ((m->ol_flags & RTE_MBUF_F_TX_L4_MASK) ==
1417 : : RTE_MBUF_F_TX_UDP_CKSUM)
1418 : 0 : *pi_data |= NDIS_TXCSUM_INFO_MKUDPCS(hlen);
1419 : : }
1420 : :
1421 : 0 : pkt_hlen = pkt->pktinfooffset + pkt->pktinfolen;
1422 : : /* Fixup RNDIS packet message total length */
1423 : 0 : pkt->len += pkt_hlen;
1424 : :
1425 : : /* Convert RNDIS packet message offsets */
1426 : 0 : pkt->dataoffset = hn_rndis_pktmsg_offset(pkt_hlen);
1427 : 0 : pkt->pktinfooffset = hn_rndis_pktmsg_offset(pkt->pktinfooffset);
1428 : 0 : }
1429 : :
1430 : : /* How many scatter gather list elements ar needed */
1431 : : static unsigned int hn_get_slots(const struct rte_mbuf *m)
1432 : : {
1433 : : unsigned int slots = 1; /* for RNDIS header */
1434 : :
1435 [ # # ]: 0 : while (m) {
1436 : 0 : unsigned int size = rte_pktmbuf_data_len(m);
1437 : 0 : unsigned int offs = rte_mbuf_data_iova(m) & HYPERV_PAGE_MASK;
1438 : :
1439 : 0 : slots += (offs + size + HYPERV_PAGE_SIZE - 1) /
1440 : : HYPERV_PAGE_SIZE;
1441 : 0 : m = m->next;
1442 : : }
1443 : :
1444 : : return slots;
1445 : : }
1446 : :
1447 : : /* Build scatter gather list from chained mbuf */
1448 : 0 : static unsigned int hn_fill_sg(struct vmbus_gpa *sg,
1449 : : const struct rte_mbuf *m)
1450 : : {
1451 : : unsigned int segs = 0;
1452 : :
1453 [ # # ]: 0 : while (m) {
1454 : : rte_iova_t addr = rte_mbuf_data_iova(m);
1455 : 0 : unsigned int page = addr / HYPERV_PAGE_SIZE;
1456 : 0 : unsigned int offset = addr & HYPERV_PAGE_MASK;
1457 : 0 : unsigned int len = rte_pktmbuf_data_len(m);
1458 : :
1459 [ # # ]: 0 : while (len > 0) {
1460 : 0 : unsigned int bytes = RTE_MIN(len,
1461 : : HYPERV_PAGE_SIZE - offset);
1462 : :
1463 : 0 : sg[segs].page = page;
1464 : 0 : sg[segs].ofs = offset;
1465 : 0 : sg[segs].len = bytes;
1466 : 0 : segs++;
1467 : :
1468 : 0 : ++page;
1469 : : offset = 0;
1470 : 0 : len -= bytes;
1471 : : }
1472 : 0 : m = m->next;
1473 : : }
1474 : :
1475 : 0 : return segs;
1476 : : }
1477 : :
1478 : : /* Transmit directly from mbuf */
1479 : 0 : static int hn_xmit_sg(struct hn_tx_queue *txq,
1480 : : const struct hn_txdesc *txd, const struct rte_mbuf *m,
1481 : : bool *need_sig)
1482 : 0 : {
1483 : 0 : struct vmbus_gpa sg[hn_get_slots(m)];
1484 : 0 : struct hn_nvs_rndis nvs_rndis = {
1485 : : .type = NVS_TYPE_RNDIS,
1486 : : .rndis_mtype = NVS_RNDIS_MTYPE_DATA,
1487 : 0 : .chim_sz = txd->chim_size,
1488 : : };
1489 : : rte_iova_t addr;
1490 : : unsigned int segs;
1491 : :
1492 : : /* attach aggregation data if present */
1493 [ # # ]: 0 : if (txd->chim_size > 0)
1494 : 0 : nvs_rndis.chim_idx = txd->chim_index;
1495 : : else
1496 : 0 : nvs_rndis.chim_idx = NVS_CHIM_IDX_INVALID;
1497 : :
1498 : : hn_rndis_dump(txd->rndis_pkt);
1499 : :
1500 : : /* pass IOVA of rndis header in first segment */
1501 : 0 : addr = txq->tx_rndis_iova +
1502 : 0 : ((char *)txd->rndis_pkt - (char *)txq->tx_rndis);
1503 : :
1504 : 0 : sg[0].page = addr / HYPERV_PAGE_SIZE;
1505 : 0 : sg[0].ofs = addr & HYPERV_PAGE_MASK;
1506 : 0 : sg[0].len = RNDIS_PACKET_MSG_OFFSET_ABS(hn_rndis_pktlen(txd->rndis_pkt));
1507 : : segs = 1;
1508 : :
1509 : 0 : hn_update_packet_stats(&txq->stats, m);
1510 : :
1511 : 0 : segs += hn_fill_sg(sg + 1, m);
1512 : :
1513 : : PMD_TX_LOG(DEBUG, "port %u:%u tx %u segs %u size %u",
1514 : : txq->port_id, txq->queue_id, txd->chim_index,
1515 : : segs, nvs_rndis.chim_sz);
1516 : :
1517 : 0 : return hn_nvs_send_sglist(txq->hv, txq->chan, sg, segs, &nvs_rndis,
1518 : : sizeof(nvs_rndis), (uintptr_t)txd, need_sig);
1519 : : }
1520 : :
1521 : : uint16_t
1522 : 0 : hn_xmit_pkts(void *ptxq, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
1523 : : {
1524 : : struct hn_tx_queue *txq = ptxq;
1525 : 0 : uint16_t queue_id = txq->queue_id;
1526 : 0 : struct hn_data *hv = txq->hv;
1527 : : struct rte_eth_dev *vf_dev;
1528 : 0 : bool need_sig = false;
1529 : : uint16_t nb_tx, tx_thresh;
1530 : : int ret;
1531 : :
1532 [ # # ]: 0 : if (unlikely(hv->closed))
1533 : : return 0;
1534 : :
1535 : : /*
1536 : : * Always check for events on the primary channel
1537 : : * because that is where hotplug notifications occur.
1538 : : */
1539 : 0 : tx_thresh = RTE_MAX(txq->free_thresh, nb_pkts);
1540 [ # # # # ]: 0 : if (txq->queue_id == 0 ||
1541 : 0 : rte_mempool_avail_count(txq->txdesc_pool) < tx_thresh)
1542 : 0 : hn_process_events(hv, txq->queue_id, 0);
1543 : :
1544 : : /* Transmit over VF if present and up */
1545 : 0 : rte_rwlock_read_lock(&hv->vf_lock);
1546 : : vf_dev = hn_get_vf_dev(hv);
1547 [ # # # # ]: 0 : if (hv->vf_ctx.vf_vsc_switched && vf_dev &&
1548 [ # # ]: 0 : vf_dev->data->dev_started) {
1549 : 0 : void *sub_q = vf_dev->data->tx_queues[queue_id];
1550 : :
1551 : 0 : nb_tx = (*vf_dev->tx_pkt_burst)
1552 : : (sub_q, tx_pkts, nb_pkts);
1553 : : rte_rwlock_read_unlock(&hv->vf_lock);
1554 : 0 : return nb_tx;
1555 : : }
1556 : : rte_rwlock_read_unlock(&hv->vf_lock);
1557 : :
1558 [ # # ]: 0 : for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
1559 : 0 : struct rte_mbuf *m = tx_pkts[nb_tx];
1560 : : struct rndis_packet_msg *pkt;
1561 : : struct hn_txdesc *txd;
1562 : : uint32_t pkt_size;
1563 : :
1564 : 0 : txd = hn_txd_get(txq);
1565 [ # # ]: 0 : if (txd == NULL)
1566 : : break;
1567 : :
1568 [ # # ]: 0 : if (!(m->ol_flags & RTE_MBUF_F_TX_VLAN)) {
1569 : 0 : struct rte_ether_hdr *eh =
1570 : 0 : rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
1571 : : struct rte_vlan_hdr *vh;
1572 : :
1573 : : /* Force TX vlan offloading for 801.2Q packet */
1574 [ # # ]: 0 : if (eh->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN)) {
1575 : : vh = (struct rte_vlan_hdr *)(eh + 1);
1576 : 0 : m->ol_flags |= RTE_MBUF_F_TX_VLAN;
1577 [ # # # # ]: 0 : m->vlan_tci = rte_be_to_cpu_16(vh->vlan_tci);
1578 : :
1579 : : /* Copy ether header over */
1580 : : memmove(rte_pktmbuf_adj(m, sizeof(struct rte_vlan_hdr)),
1581 : : eh, 2 * RTE_ETHER_ADDR_LEN);
1582 : : }
1583 : : }
1584 : 0 : pkt_size = m->pkt_len + HN_RNDIS_PKT_LEN;
1585 : :
1586 : : /* For small packets aggregate them in chimney buffer */
1587 [ # # ]: 0 : if (m->pkt_len <= hv->tx_copybreak &&
1588 [ # # ]: 0 : pkt_size <= txq->agg_szmax) {
1589 : : /* If this packet will not fit, then flush */
1590 [ # # ]: 0 : if (txq->agg_pktleft == 0 ||
1591 [ # # ]: 0 : RTE_ALIGN(pkt_size, txq->agg_align) > txq->agg_szleft) {
1592 [ # # ]: 0 : if (hn_flush_txagg(txq, &need_sig))
1593 : 0 : goto fail;
1594 : : }
1595 : :
1596 : :
1597 : 0 : pkt = hn_try_txagg(hv, txq, txd, pkt_size);
1598 [ # # ]: 0 : if (unlikely(!pkt))
1599 : : break;
1600 : :
1601 : 0 : hn_encap(pkt, queue_id, m);
1602 : 0 : hn_append_to_chim(txq, pkt, m);
1603 : :
1604 : 0 : rte_pktmbuf_free(m);
1605 : :
1606 : : /* if buffer is full, flush */
1607 [ # # # # ]: 0 : if (txq->agg_pktleft == 0 &&
1608 : 0 : hn_flush_txagg(txq, &need_sig))
1609 : 0 : goto fail;
1610 : : } else {
1611 : : /* Send any outstanding packets in buffer */
1612 [ # # # # ]: 0 : if (txq->agg_txd && hn_flush_txagg(txq, &need_sig))
1613 : 0 : goto fail;
1614 : :
1615 : 0 : pkt = txd->rndis_pkt;
1616 : 0 : txd->m = m;
1617 : 0 : txd->data_size = m->pkt_len;
1618 : 0 : ++txd->packets;
1619 : :
1620 : 0 : hn_encap(pkt, queue_id, m);
1621 : :
1622 : 0 : ret = hn_xmit_sg(txq, txd, m, &need_sig);
1623 [ # # ]: 0 : if (unlikely(ret != 0)) {
1624 [ # # ]: 0 : if (ret == -EAGAIN) {
1625 : : PMD_TX_LOG(DEBUG, "sg channel full");
1626 : 0 : ++txq->stats.channel_full;
1627 : : } else {
1628 : 0 : PMD_DRV_LOG(NOTICE, "sg send failed: %d", ret);
1629 : 0 : ++txq->stats.errors;
1630 : : }
1631 : 0 : hn_txd_put(txq, txd);
1632 : 0 : goto fail;
1633 : : }
1634 : : }
1635 : : }
1636 : :
1637 : : /* If partial buffer left, then try and send it.
1638 : : * if that fails, then reuse it on next send.
1639 : : */
1640 : 0 : hn_flush_txagg(txq, &need_sig);
1641 : :
1642 : 0 : fail:
1643 [ # # ]: 0 : if (need_sig)
1644 : 0 : rte_vmbus_chan_signal_tx(hn_nvs_get_vmbus_device(hv), txq->chan);
1645 : :
1646 : : return nb_tx;
1647 : : }
1648 : :
1649 : : static uint16_t
1650 : 0 : hn_recv_vf(uint16_t vf_port, const struct hn_rx_queue *rxq,
1651 : : struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1652 : : {
1653 : : uint16_t i, n;
1654 : :
1655 [ # # ]: 0 : if (unlikely(nb_pkts == 0))
1656 : : return 0;
1657 : :
1658 : 0 : n = rte_eth_rx_burst(vf_port, rxq->queue_id, rx_pkts, nb_pkts);
1659 : :
1660 : : /* relabel the received mbufs */
1661 [ # # ]: 0 : for (i = 0; i < n; i++)
1662 : 0 : rx_pkts[i]->port = rxq->port_id;
1663 : :
1664 : : return n;
1665 : : }
1666 : :
1667 : : uint16_t
1668 : 0 : hn_recv_pkts(void *prxq, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1669 : : {
1670 : : struct hn_rx_queue *rxq = prxq;
1671 : 0 : struct hn_data *hv = rxq->hv;
1672 : : struct rte_eth_dev *vf_dev;
1673 : : uint16_t nb_rcv;
1674 : :
1675 [ # # ]: 0 : if (unlikely(hv->closed))
1676 : : return 0;
1677 : :
1678 : : /* Check for new completions (and hotplug) */
1679 [ # # ]: 0 : if (likely(rte_ring_count(rxq->rx_ring) < nb_pkts))
1680 : 0 : hn_process_events(hv, rxq->queue_id, 0);
1681 : :
1682 : : /* Always check the vmbus path for multicast and new flows */
1683 [ # # ]: 0 : nb_rcv = rte_ring_sc_dequeue_burst(rxq->rx_ring,
1684 : : (void **)rx_pkts, nb_pkts, NULL);
1685 : :
1686 : : /* If VF is available, check that as well */
1687 : 0 : rte_rwlock_read_lock(&hv->vf_lock);
1688 : : vf_dev = hn_get_vf_dev(hv);
1689 [ # # # # ]: 0 : if (hv->vf_ctx.vf_vsc_switched && vf_dev &&
1690 [ # # ]: 0 : vf_dev->data->dev_started)
1691 : 0 : nb_rcv += hn_recv_vf(vf_dev->data->port_id, rxq,
1692 : 0 : rx_pkts + nb_rcv,
1693 : 0 : nb_pkts - nb_rcv);
1694 : : rte_rwlock_read_unlock(&hv->vf_lock);
1695 : :
1696 : 0 : return nb_rcv;
1697 : : }
1698 : :
1699 : : void
1700 : 0 : hn_dev_free_queues(struct rte_eth_dev *dev)
1701 : : {
1702 : : unsigned int i;
1703 : :
1704 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++) {
1705 : 0 : struct hn_rx_queue *rxq = dev->data->rx_queues[i];
1706 : :
1707 : 0 : hn_rx_queue_free(rxq, false);
1708 : 0 : dev->data->rx_queues[i] = NULL;
1709 : : }
1710 : 0 : dev->data->nb_rx_queues = 0;
1711 : :
1712 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++) {
1713 : 0 : hn_dev_tx_queue_release(dev, i);
1714 : 0 : dev->data->tx_queues[i] = NULL;
1715 : : }
1716 : 0 : dev->data->nb_tx_queues = 0;
1717 : 0 : }
|