2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2020 Advanced Micro Devices, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * Contact Information :
28 * Rajesh Kumar <rajesh1.kumar@amd.com>
29 * Shreyank Amartya <Shreyank.Amartya@amd.com>
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 #include "xgbe-common.h"
42 static int axgbe_isc_txd_encap(void *, if_pkt_info_t);
43 static void axgbe_isc_txd_flush(void *, uint16_t, qidx_t);
44 static int axgbe_isc_txd_credits_update(void *, uint16_t, bool);
45 static void axgbe_isc_rxd_refill(void *, if_rxd_update_t);
46 static void axgbe_isc_rxd_flush(void *, uint16_t, uint8_t, qidx_t);
47 static int axgbe_isc_rxd_available(void *, uint16_t, qidx_t, qidx_t);
48 static int axgbe_isc_rxd_pkt_get(void *, if_rxd_info_t);
50 struct if_txrx axgbe_txrx = {
51 .ift_txd_encap = axgbe_isc_txd_encap,
52 .ift_txd_flush = axgbe_isc_txd_flush,
53 .ift_txd_credits_update = axgbe_isc_txd_credits_update,
54 .ift_rxd_available = axgbe_isc_rxd_available,
55 .ift_rxd_pkt_get = axgbe_isc_rxd_pkt_get,
56 .ift_rxd_refill = axgbe_isc_rxd_refill,
57 .ift_rxd_flush = axgbe_isc_rxd_flush,
58 .ift_legacy_intr = NULL
62 xgbe_print_pkt_info(struct xgbe_prv_data *pdata, if_pkt_info_t pi)
65 axgbe_printf(1, "------Packet Info Start------\n");
66 axgbe_printf(1, "pi len: %d qsidx: %d nsegs: %d ndescs: %d flags: %x pidx: %d\n",
67 pi->ipi_len, pi->ipi_qsidx, pi->ipi_nsegs, pi->ipi_ndescs, pi->ipi_flags, pi->ipi_pidx);
68 axgbe_printf(1, "pi new_pidx: %d csum_flags: %x mflags: %x vtag: %d\n",
69 pi->ipi_new_pidx, pi->ipi_csum_flags, pi->ipi_mflags, pi->ipi_vtag);
70 axgbe_printf(1, "pi etype: %d ehdrlen: %d ip_hlen: %d ipproto: %d\n",
71 pi->ipi_etype, pi->ipi_ehdrlen, pi->ipi_ip_hlen, pi->ipi_ipproto);
72 axgbe_printf(1, "pi tcp_hlen: %d tcp_hflags: %x tcp_seq: %d tso_segsz %d\n",
73 pi->ipi_tcp_hlen, pi->ipi_tcp_hflags, pi->ipi_tcp_seq, pi->ipi_tso_segsz);
77 axgbe_ctx_desc_setup(struct xgbe_prv_data *pdata, struct xgbe_ring *ring,
80 struct xgbe_ring_desc *rdesc;
81 struct xgbe_ring_data *rdata;
84 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
87 axgbe_printf(1, "ipi_tso_segsz %d cur_mss %d idx %d\n",
88 pi->ipi_tso_segsz, ring->tx.cur_mss, ring->cur);
90 axgbe_printf(1, "ipi_vtag 0x%x cur_vlan_ctag 0x%x\n",
91 pi->ipi_vtag, ring->tx.cur_vlan_ctag);
93 if ((pi->ipi_csum_flags & CSUM_TSO) &&
94 (pi->ipi_tso_segsz != ring->tx.cur_mss)) {
96 * Set TSO maximum segment size
97 * Mark as context descriptor
98 * Indicate this descriptor contains MSS
100 XGMAC_SET_BITS_LE(rdesc->desc2, TX_CONTEXT_DESC2,
101 MSS, pi->ipi_tso_segsz);
102 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3, CTXT, 1);
103 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3, TCMSSV, 1);
104 ring->tx.cur_mss = pi->ipi_tso_segsz;
108 if (pi->ipi_vtag && (pi->ipi_vtag != ring->tx.cur_vlan_ctag)) {
110 * Mark it as context descriptor
112 * Indicate this descriptor contains the VLAN tag
114 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3, CTXT, 1);
115 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
117 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3, VLTV, 1);
118 ring->tx.cur_vlan_ctag = pi->ipi_vtag;
126 axgbe_calculate_tx_parms(struct xgbe_prv_data *pdata, if_pkt_info_t pi,
127 struct xgbe_packet_data *packet)
129 uint32_t tcp_payload_len = 0, bytes = 0;
130 uint16_t max_len, hlen, payload_len, pkts = 0;
132 packet->tx_packets = packet->tx_bytes = 0;
134 hlen = pi->ipi_ehdrlen + pi->ipi_ip_hlen + pi->ipi_tcp_hlen;
135 if (pi->ipi_csum_flags & CSUM_TSO) {
137 tcp_payload_len = pi->ipi_len - hlen;
138 axgbe_printf(1, "%s: ipi_len %x elen %d iplen %d tcplen %d\n",
139 __func__, pi->ipi_len, pi->ipi_ehdrlen, pi->ipi_ip_hlen,
142 max_len = if_getmtu(pdata->netdev) + ETH_HLEN;
144 max_len += VLAN_HLEN;
146 while (tcp_payload_len) {
148 payload_len = max_len - hlen;
149 payload_len = min(payload_len, tcp_payload_len);
150 tcp_payload_len -= payload_len;
152 bytes += (hlen + payload_len);
153 axgbe_printf(1, "%s: max_len %d payload_len %d "
154 "tcp_len %d\n", __func__, max_len, payload_len,
162 packet->tx_packets = pkts;
163 packet->tx_bytes = bytes;
165 axgbe_printf(1, "%s: packets %d bytes %d hlen %d\n", __func__,
166 packet->tx_packets, packet->tx_bytes, hlen);
172 axgbe_isc_txd_encap(void *arg, if_pkt_info_t pi)
174 struct axgbe_if_softc *sc = (struct axgbe_if_softc*)arg;
175 struct xgbe_prv_data *pdata = &sc->pdata;
176 struct xgbe_channel *channel;
177 struct xgbe_ring *ring;
178 struct xgbe_ring_desc *rdesc;
179 struct xgbe_ring_data *rdata;
180 struct xgbe_packet_data *packet;
181 unsigned int cur, start, tx_set_ic;
182 uint16_t offset, hlen, datalen, tcp_payload_len = 0;
185 xgbe_print_pkt_info(pdata, pi);
187 channel = pdata->channel[pi->ipi_qsidx];
188 ring = channel->tx_ring;
189 packet = &ring->packet_data;
190 cur = start = ring->cur;
192 axgbe_printf(1, "--> %s: txq %d cur %d dirty %d\n",
193 __func__, pi->ipi_qsidx, ring->cur, ring->dirty);
195 MPASS(pi->ipi_len != 0);
196 if (__predict_false(pi->ipi_len == 0)) {
197 axgbe_error("empty packet received from stack\n");
201 MPASS(ring->cur == pi->ipi_pidx);
202 if (__predict_false(ring->cur != pi->ipi_pidx)) {
203 axgbe_error("--> %s: cur(%d) ne pidx(%d)\n", __func__,
204 ring->cur, pi->ipi_pidx);
207 /* Determine if an interrupt should be generated for this Tx:
209 * - Tx frame count exceeds the frame count setting
210 * - Addition of Tx frame count to the frame count since the
211 * last interrupt was set exceeds the frame count setting
213 * - No frame count setting specified (ethtool -C ethX tx-frames 0)
214 * - Addition of Tx frame count to the frame count since the
215 * last interrupt was set does not exceed the frame count setting
217 memset(packet, 0, sizeof(*packet));
218 hlen = axgbe_calculate_tx_parms(pdata, pi, packet);
219 axgbe_printf(1, "%s: ipi_len %d tx_pkts %d tx_bytes %d hlen %d\n",
220 __func__, pi->ipi_len, packet->tx_packets, packet->tx_bytes, hlen);
222 ring->coalesce_count += packet->tx_packets;
223 if (!pdata->tx_frames)
225 else if (packet->tx_packets > pdata->tx_frames)
227 else if ((ring->coalesce_count % pdata->tx_frames) < (packet->tx_packets))
232 /* Add Context descriptor if needed (for TSO, VLAN cases) */
233 if (axgbe_ctx_desc_setup(pdata, ring, pi))
236 rdata = XGBE_GET_DESC_DATA(ring, cur);
237 rdesc = rdata->rdesc;
239 axgbe_printf(1, "%s: cur %d lo 0x%lx hi 0x%lx ds_len 0x%x "
240 "ipi_len 0x%x\n", __func__, cur,
241 lower_32_bits(pi->ipi_segs[cur_seg].ds_addr),
242 upper_32_bits(pi->ipi_segs[cur_seg].ds_addr),
243 (int)pi->ipi_segs[cur_seg].ds_len, pi->ipi_len);
245 /* Update buffer address (for TSO this is the header) */
246 rdesc->desc0 = cpu_to_le32(lower_32_bits(pi->ipi_segs[cur_seg].ds_addr));
247 rdesc->desc1 = cpu_to_le32(upper_32_bits(pi->ipi_segs[cur_seg].ds_addr));
249 /* Update the buffer length */
251 hlen = pi->ipi_segs[cur_seg].ds_len;
252 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, HL_B1L, hlen);
254 /* VLAN tag insertion check */
256 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, VTIR,
257 TX_NORMAL_DESC2_VLAN_INSERT);
260 /* Mark it as First Descriptor */
261 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, FD, 1);
263 /* Mark it as a NORMAL descriptor */
264 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT, 0);
267 * Set the OWN bit if this is not the first descriptor. For first
268 * descriptor, OWN bit will be set at last so that hardware will
269 * process the descriptors only after the OWN bit for the first
273 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
275 if (pi->ipi_csum_flags & CSUM_TSO) {
277 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TSE, 1);
279 tcp_payload_len = pi->ipi_len - hlen;
281 /* Set TCP payload length*/
282 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TCPPL,
285 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TCPHDRLEN,
288 axgbe_printf(1, "tcp_payload %d tcp_hlen %d\n", tcp_payload_len,
291 /* Enable CRC and Pad Insertion */
292 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CPC, 0);
295 if (pi->ipi_csum_flags)
296 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CIC, 0x3);
298 /* Set total length to be transmitted */
299 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, FL, pi->ipi_len);
304 for (cur_seg = 0 ; cur_seg < pi->ipi_nsegs ; cur_seg++) {
308 datalen = pi->ipi_segs[cur_seg].ds_len - hlen;
311 datalen = pi->ipi_segs[cur_seg].ds_len;
315 rdata = XGBE_GET_DESC_DATA(ring, cur);
316 rdesc = rdata->rdesc;
319 /* Update buffer address */
321 cpu_to_le32(lower_32_bits(pi->ipi_segs[cur_seg].ds_addr + offset));
323 cpu_to_le32(upper_32_bits(pi->ipi_segs[cur_seg].ds_addr + offset));
325 /* Update the buffer length */
326 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, HL_B1L, datalen);
329 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
331 /* Mark it as NORMAL descriptor */
332 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT, 0);
335 if (pi->ipi_csum_flags)
336 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CIC, 0x3);
338 axgbe_printf(1, "%s: cur %d lo 0x%lx hi 0x%lx ds_len 0x%x "
339 "ipi_len 0x%x\n", __func__, cur,
340 lower_32_bits(pi->ipi_segs[cur_seg].ds_addr),
341 upper_32_bits(pi->ipi_segs[cur_seg].ds_addr),
342 (int)pi->ipi_segs[cur_seg].ds_len, pi->ipi_len);
348 /* Set LAST bit for the last descriptor */
349 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, LD, 1);
351 /* Set IC bit based on Tx coalescing settings */
353 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, IC, 1);
357 /* Set OWN bit for the first descriptor */
358 rdata = XGBE_GET_DESC_DATA(ring, start);
359 rdesc = rdata->rdesc;
360 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
362 ring->cur = pi->ipi_new_pidx = (cur & (ring->rdesc_count - 1));
364 axgbe_printf(1, "<-- %s: end cur %d dirty %d\n", __func__, ring->cur,
371 axgbe_isc_txd_flush(void *arg, uint16_t txqid, qidx_t pidx)
373 struct axgbe_if_softc *sc = (struct axgbe_if_softc*)arg;
374 struct xgbe_prv_data *pdata = &sc->pdata;
375 struct xgbe_channel *channel = pdata->channel[txqid];
376 struct xgbe_ring *ring = channel->tx_ring;
377 struct xgbe_ring_data *rdata = XGBE_GET_DESC_DATA(ring, pidx);
379 axgbe_printf(1, "--> %s: flush txq %d pidx %d cur %d dirty %d\n",
380 __func__, txqid, pidx, ring->cur, ring->dirty);
383 XGMAC_DMA_IOWRITE(channel, DMA_CH_TDTR_LO,
384 lower_32_bits(rdata->rdata_paddr));
388 axgbe_isc_txd_credits_update(void *arg, uint16_t txqid, bool clear)
390 struct axgbe_if_softc *sc = (struct axgbe_if_softc*)arg;
391 struct xgbe_hw_if *hw_if = &sc->pdata.hw_if;
392 struct xgbe_prv_data *pdata = &sc->pdata;
393 struct xgbe_channel *channel = pdata->channel[txqid];
394 struct xgbe_ring *ring = channel->tx_ring;
395 struct xgbe_ring_data *rdata;
398 axgbe_printf(1, "%s: txq %d clear %d cur %d dirty %d\n",
399 __func__, txqid, clear, ring->cur, ring->dirty);
401 if (__predict_false(ring->cur == ring->dirty)) {
402 axgbe_printf(1, "<-- %s: cur(%d) equals dirty(%d)\n",
403 __func__, ring->cur, ring->dirty);
407 /* Check whether the first dirty descriptor is Tx complete */
408 rdata = XGBE_GET_DESC_DATA(ring, ring->dirty);
409 if (!hw_if->tx_complete(rdata->rdesc)) {
410 axgbe_printf(1, "<-- %s: (dirty %d)\n", __func__, ring->dirty);
415 * If clear is false just let the caller know that there
416 * are descriptors to reclaim
419 axgbe_printf(1, "<-- %s: (!clear)\n", __func__);
424 hw_if->tx_desc_reset(rdata);
426 ring->dirty = (ring->dirty + 1) & (ring->rdesc_count - 1);
429 * tx_complete will return true for unused descriptors also.
430 * so, check tx_complete only until used descriptors.
432 if (ring->cur == ring->dirty)
435 rdata = XGBE_GET_DESC_DATA(ring, ring->dirty);
436 } while (hw_if->tx_complete(rdata->rdesc));
438 axgbe_printf(1, "<-- %s: processed %d cur %d dirty %d\n", __func__,
439 processed, ring->cur, ring->dirty);
445 axgbe_isc_rxd_refill(void *arg, if_rxd_update_t iru)
447 struct axgbe_if_softc *sc = (struct axgbe_if_softc*)arg;
448 struct xgbe_prv_data *pdata = &sc->pdata;
449 struct xgbe_channel *channel = pdata->channel[iru->iru_qsidx];
450 struct xgbe_ring *ring = channel->rx_ring;
451 struct xgbe_ring_data *rdata;
452 struct xgbe_ring_desc *rdesc;
453 unsigned int rx_usecs = pdata->rx_usecs;
454 unsigned int rx_frames = pdata->rx_frames;
456 uint8_t count = iru->iru_count;
458 bool config_intr = false;
460 axgbe_printf(1, "--> %s: rxq %d fl %d pidx %d count %d ring cur %d "
461 "dirty %d\n", __func__, iru->iru_qsidx, iru->iru_flidx,
462 iru->iru_pidx, count, ring->cur, ring->dirty);
464 for (i = iru->iru_pidx, j = 0 ; j < count ; i++, j++) {
466 if (i == sc->scctx->isc_nrxd[0])
469 rdata = XGBE_GET_DESC_DATA(ring, i);
470 rdesc = rdata->rdesc;
472 if (__predict_false(XGMAC_GET_BITS_LE(rdesc->desc3,
473 RX_NORMAL_DESC3, OWN))) {
474 axgbe_error("%s: refill clash, cur %d dirty %d index %d"
475 "pidx %d\n", __func__, ring->cur, ring->dirty, j, i);
478 if (pdata->sph_enable) {
479 if (iru->iru_flidx == 0) {
481 /* Fill header/buffer1 address */
483 cpu_to_le32(lower_32_bits(iru->iru_paddrs[j]));
485 cpu_to_le32(upper_32_bits(iru->iru_paddrs[j]));
488 /* Fill data/buffer2 address */
490 cpu_to_le32(lower_32_bits(iru->iru_paddrs[j]));
492 cpu_to_le32(upper_32_bits(iru->iru_paddrs[j]));
497 /* Fill header/buffer1 address */
498 rdesc->desc0 = rdesc->desc2 =
499 cpu_to_le32(lower_32_bits(iru->iru_paddrs[j]));
500 rdesc->desc1 = rdesc->desc3 =
501 cpu_to_le32(upper_32_bits(iru->iru_paddrs[j]));
508 if (!rx_usecs && !rx_frames) {
509 /* No coalescing, interrupt for every descriptor */
512 /* Set interrupt based on Rx frame coalescing setting */
513 if (rx_frames && !((ring->dirty + 1) % rx_frames))
519 XGMAC_SET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, INTE, inte);
521 XGMAC_SET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, OWN, 1);
525 ring->dirty = ((ring->dirty + 1) & (ring->rdesc_count - 1));
531 axgbe_printf(1, "<-- %s: rxq: %d cur: %d dirty: %d\n", __func__,
532 channel->queue_index, ring->cur, ring->dirty);
536 axgbe_isc_rxd_flush(void *arg, uint16_t qsidx, uint8_t flidx, qidx_t pidx)
538 struct axgbe_if_softc *sc = (struct axgbe_if_softc*)arg;
539 struct xgbe_prv_data *pdata = &sc->pdata;
540 struct xgbe_channel *channel = pdata->channel[qsidx];
541 struct xgbe_ring *ring = channel->rx_ring;
542 struct xgbe_ring_data *rdata;
544 axgbe_printf(1, "--> %s: rxq %d fl %d pidx %d cur %d dirty %d\n",
545 __func__, qsidx, flidx, pidx, ring->cur, ring->dirty);
547 rdata = XGBE_GET_DESC_DATA(ring, pidx);
550 * update RX descriptor tail pointer in hardware to indicate
551 * that new buffers are present in the allocated memory region
553 if (!pdata->sph_enable || flidx == 1) {
554 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDTR_LO,
555 lower_32_bits(rdata->rdata_paddr));
560 axgbe_isc_rxd_available(void *arg, uint16_t qsidx, qidx_t idx, qidx_t budget)
562 struct axgbe_if_softc *sc = (struct axgbe_if_softc*)arg;
563 struct xgbe_prv_data *pdata = &sc->pdata;
564 struct xgbe_channel *channel = pdata->channel[qsidx];
565 struct xgbe_ring *ring = channel->rx_ring;
566 struct xgbe_ring_data *rdata;
567 struct xgbe_ring_desc *rdesc;
570 uint8_t incomplete = 1, context_next = 0, running = 0;
572 axgbe_printf(1, "--> %s: rxq %d idx %d budget %d cur %d dirty %d\n",
573 __func__, qsidx, idx, budget, ring->cur, ring->dirty);
575 if (__predict_false(test_bit(XGBE_DOWN, &pdata->dev_state))) {
576 axgbe_printf(0, "%s: Polling when XGBE_DOWN\n", __func__);
581 for (count = 0; count <= budget; ) {
583 rdata = XGBE_GET_DESC_DATA(ring, cur);
584 rdesc = rdata->rdesc;
586 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, OWN))
591 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, LD))
594 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, CDA))
597 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, CTXT))
600 cur = (cur + 1) & (ring->rdesc_count - 1);
602 if (incomplete || context_next)
605 /* Increment pkt count & reset variables for next full packet */
612 axgbe_printf(1, "--> %s: rxq %d cur %d incomp %d con_next %d running %d "
613 "count %d\n", __func__, qsidx, cur, incomplete, context_next,
620 xgbe_rx_buf1_len(struct xgbe_prv_data *pdata, struct xgbe_ring_data *rdata,
621 struct xgbe_packet_data *packet)
623 unsigned int ret = 0;
625 if (pdata->sph_enable) {
626 /* Always zero if not the first descriptor */
627 if (!XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, FIRST)) {
628 axgbe_printf(1, "%s: Not First\n", __func__);
633 /* First descriptor with split header, return header length */
634 if (rdata->rx.hdr_len) {
635 axgbe_printf(1, "%s: hdr_len %d\n", __func__, rdata->rx.hdr_len);
636 return (rdata->rx.hdr_len);
639 /* First descriptor but not the last descriptor and no split header,
640 * so the full buffer was used, 256 represents the hardcoded value of
641 * a max header split defined in the hardware
643 if (!XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, LAST)) {
644 axgbe_printf(1, "%s: Not last %d\n", __func__,
646 if (pdata->sph_enable) {
649 return (pdata->rx_buf_size);
653 /* First descriptor and last descriptor and no split header, so
654 * calculate how much of the buffer was used, we can return the
655 * segment length or the remaining bytes of the packet
657 axgbe_printf(1, "%s: pkt_len %d buf_size %d\n", __func__, rdata->rx.len,
660 if (pdata->sph_enable) {
661 ret = min_t(unsigned int, 256, rdata->rx.len);
670 xgbe_rx_buf2_len(struct xgbe_prv_data *pdata, struct xgbe_ring_data *rdata,
671 struct xgbe_packet_data *packet, unsigned int len)
674 /* Always the full buffer if not the last descriptor */
675 if (!XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, LAST)) {
676 axgbe_printf(1, "%s: Not last %d\n", __func__, pdata->rx_buf_size);
677 return (pdata->rx_buf_size);
680 /* Last descriptor so calculate how much of the buffer was used
681 * for the last bit of data
683 return ((rdata->rx.len != 0)? (rdata->rx.len - len) : 0);
687 axgbe_add_frag(struct xgbe_prv_data *pdata, if_rxd_info_t ri, int idx, int len,
690 axgbe_printf(2, "idx %d len %d pos %d flid %d\n", idx, len, pos, flid);
691 ri->iri_frags[pos].irf_flid = flid;
692 ri->iri_frags[pos].irf_idx = idx;
693 ri->iri_frags[pos].irf_len = len;
697 axgbe_isc_rxd_pkt_get(void *arg, if_rxd_info_t ri)
699 struct axgbe_if_softc *sc = (struct axgbe_if_softc*)arg;
700 struct xgbe_prv_data *pdata = &sc->pdata;
701 struct xgbe_hw_if *hw_if = &pdata->hw_if;
702 struct xgbe_channel *channel = pdata->channel[ri->iri_qsidx];
703 struct xgbe_ring *ring = channel->rx_ring;
704 struct xgbe_packet_data *packet = &ring->packet_data;
705 struct xgbe_ring_data *rdata;
706 unsigned int last, context_next, context;
707 unsigned int buf1_len, buf2_len, max_len, len = 0, prev_cur;
710 axgbe_printf(2, "%s: rxq %d cidx %d cur %d dirty %d\n", __func__,
711 ri->iri_qsidx, ri->iri_cidx, ring->cur, ring->dirty);
713 memset(packet, 0, sizeof(struct xgbe_packet_data));
718 if (hw_if->dev_read(channel)) {
719 axgbe_printf(2, "<-- %s: OWN bit seen on %d\n",
720 __func__, ring->cur);
724 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
725 prev_cur = ring->cur;
726 ring->cur = (ring->cur + 1) & (ring->rdesc_count - 1);
728 last = XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
731 context_next = XGMAC_GET_BITS(packet->attributes,
732 RX_PACKET_ATTRIBUTES, CONTEXT_NEXT);
734 context = XGMAC_GET_BITS(packet->attributes,
735 RX_PACKET_ATTRIBUTES, CONTEXT);
738 /* Get the data length in the descriptor buffers */
739 buf1_len = xgbe_rx_buf1_len(pdata, rdata, packet);
741 if (pdata->sph_enable) {
742 buf2_len = xgbe_rx_buf2_len(pdata, rdata, packet, len);
746 buf1_len = buf2_len = 0;
749 axgbe_printf(1, "%s: last %d context %d con_next %d buf1 %d "
750 "buf2 %d len %d frags %d error %d\n", __func__, last, context,
751 context_next, buf1_len, buf2_len, len, i, packet->errors);
753 axgbe_add_frag(pdata, ri, prev_cur, buf1_len, i, 0);
755 if (pdata->sph_enable) {
756 axgbe_add_frag(pdata, ri, prev_cur, buf2_len, i, 1);
760 if (!last || context_next)
766 if (XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, CSUM_DONE)) {
767 ri->iri_csum_flags |= CSUM_IP_CHECKED;
768 ri->iri_csum_flags |= CSUM_IP_VALID;
769 axgbe_printf(2, "%s: csum flags 0x%x\n", __func__, ri->iri_csum_flags);
772 max_len = if_getmtu(pdata->netdev) + ETH_HLEN;
773 if (XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, VLAN_CTAG)) {
774 ri->iri_flags |= M_VLANTAG;
775 ri->iri_vtag = packet->vlan_ctag;
776 max_len += VLAN_HLEN;
777 axgbe_printf(2, "%s: iri_flags 0x%x vtag 0x%x\n", __func__,
778 ri->iri_flags, ri->iri_vtag);
782 if (XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, RSS_HASH)) {
783 ri->iri_flowid = packet->rss_hash;
784 ri->iri_rsstype = packet->rss_hash_type;
785 axgbe_printf(2, "%s: hash 0x%x/0x%x rsstype 0x%x/0x%x\n",
786 __func__, packet->rss_hash, ri->iri_flowid,
787 packet->rss_hash_type, ri->iri_rsstype);
790 if (__predict_false(len == 0))
791 axgbe_printf(1, "%s: Discarding Zero len packet\n", __func__);
793 if (__predict_false(len > max_len))
794 axgbe_error("%s: Big packet %d/%d\n", __func__, len, max_len);
796 if (__predict_false(packet->errors))
797 axgbe_printf(1, "<-- %s: rxq: %d len: %d frags: %d cidx %d cur: %d "
798 "dirty: %d error 0x%x\n", __func__, ri->iri_qsidx, len, i,
799 ri->iri_cidx, ring->cur, ring->dirty, packet->errors);
801 axgbe_printf(1, "%s: Packet len %d frags %d\n", __func__, len, i);