18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright(c) 2020 Intel Corporation.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci/*
88c2ecf20Sopenharmony_ci * This file contains HFI1 support for IPOIB SDMA functionality
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/log2.h>
128c2ecf20Sopenharmony_ci#include <linux/circ_buf.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include "sdma.h"
158c2ecf20Sopenharmony_ci#include "verbs.h"
168c2ecf20Sopenharmony_ci#include "trace_ibhdrs.h"
178c2ecf20Sopenharmony_ci#include "ipoib.h"
188c2ecf20Sopenharmony_ci#include "trace_tx.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/* Add a convenience helper */
218c2ecf20Sopenharmony_ci#define CIRC_ADD(val, add, size) (((val) + (add)) & ((size) - 1))
228c2ecf20Sopenharmony_ci#define CIRC_NEXT(val, size) CIRC_ADD(val, 1, size)
238c2ecf20Sopenharmony_ci#define CIRC_PREV(val, size) CIRC_ADD(val, -1, size)
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/**
268c2ecf20Sopenharmony_ci * struct ipoib_txreq - IPOIB transmit descriptor
278c2ecf20Sopenharmony_ci * @txreq: sdma transmit request
288c2ecf20Sopenharmony_ci * @sdma_hdr: 9b ib headers
298c2ecf20Sopenharmony_ci * @sdma_status: status returned by sdma engine
308c2ecf20Sopenharmony_ci * @priv: ipoib netdev private data
318c2ecf20Sopenharmony_ci * @txq: txq on which skb was output
328c2ecf20Sopenharmony_ci * @skb: skb to send
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_cistruct ipoib_txreq {
358c2ecf20Sopenharmony_ci	struct sdma_txreq           txreq;
368c2ecf20Sopenharmony_ci	struct hfi1_sdma_header     sdma_hdr;
378c2ecf20Sopenharmony_ci	int                         sdma_status;
388c2ecf20Sopenharmony_ci	struct hfi1_ipoib_dev_priv *priv;
398c2ecf20Sopenharmony_ci	struct hfi1_ipoib_txq      *txq;
408c2ecf20Sopenharmony_ci	struct sk_buff             *skb;
418c2ecf20Sopenharmony_ci};
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistruct ipoib_txparms {
448c2ecf20Sopenharmony_ci	struct hfi1_devdata        *dd;
458c2ecf20Sopenharmony_ci	struct rdma_ah_attr        *ah_attr;
468c2ecf20Sopenharmony_ci	struct hfi1_ibport         *ibp;
478c2ecf20Sopenharmony_ci	struct hfi1_ipoib_txq      *txq;
488c2ecf20Sopenharmony_ci	union hfi1_ipoib_flow       flow;
498c2ecf20Sopenharmony_ci	u32                         dqpn;
508c2ecf20Sopenharmony_ci	u8                          hdr_dwords;
518c2ecf20Sopenharmony_ci	u8                          entropy;
528c2ecf20Sopenharmony_ci};
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic u64 hfi1_ipoib_txreqs(const u64 sent, const u64 completed)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	return sent - completed;
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic u64 hfi1_ipoib_used(struct hfi1_ipoib_txq *txq)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	return hfi1_ipoib_txreqs(txq->sent_txreqs,
628c2ecf20Sopenharmony_ci				 atomic64_read(&txq->complete_txreqs));
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic void hfi1_ipoib_stop_txq(struct hfi1_ipoib_txq *txq)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	trace_hfi1_txq_stop(txq);
688c2ecf20Sopenharmony_ci	if (atomic_inc_return(&txq->stops) == 1)
698c2ecf20Sopenharmony_ci		netif_stop_subqueue(txq->priv->netdev, txq->q_idx);
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic void hfi1_ipoib_wake_txq(struct hfi1_ipoib_txq *txq)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	trace_hfi1_txq_wake(txq);
758c2ecf20Sopenharmony_ci	if (atomic_dec_and_test(&txq->stops))
768c2ecf20Sopenharmony_ci		netif_wake_subqueue(txq->priv->netdev, txq->q_idx);
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic uint hfi1_ipoib_ring_hwat(struct hfi1_ipoib_txq *txq)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	return min_t(uint, txq->priv->netdev->tx_queue_len,
828c2ecf20Sopenharmony_ci		     txq->tx_ring.max_items - 1);
838c2ecf20Sopenharmony_ci}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic uint hfi1_ipoib_ring_lwat(struct hfi1_ipoib_txq *txq)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	return min_t(uint, txq->priv->netdev->tx_queue_len,
888c2ecf20Sopenharmony_ci		     txq->tx_ring.max_items) >> 1;
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic void hfi1_ipoib_check_queue_depth(struct hfi1_ipoib_txq *txq)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	++txq->sent_txreqs;
948c2ecf20Sopenharmony_ci	if (hfi1_ipoib_used(txq) >= hfi1_ipoib_ring_hwat(txq) &&
958c2ecf20Sopenharmony_ci	    !atomic_xchg(&txq->ring_full, 1)) {
968c2ecf20Sopenharmony_ci		trace_hfi1_txq_full(txq);
978c2ecf20Sopenharmony_ci		hfi1_ipoib_stop_txq(txq);
988c2ecf20Sopenharmony_ci	}
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistatic void hfi1_ipoib_check_queue_stopped(struct hfi1_ipoib_txq *txq)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	struct net_device *dev = txq->priv->netdev;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	/* If shutting down just return as queue state is irrelevant */
1068c2ecf20Sopenharmony_ci	if (unlikely(dev->reg_state != NETREG_REGISTERED))
1078c2ecf20Sopenharmony_ci		return;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	/*
1108c2ecf20Sopenharmony_ci	 * When the queue has been drained to less than half full it will be
1118c2ecf20Sopenharmony_ci	 * restarted.
1128c2ecf20Sopenharmony_ci	 * The size of the txreq ring is fixed at initialization.
1138c2ecf20Sopenharmony_ci	 * The tx queue len can be adjusted upward while the interface is
1148c2ecf20Sopenharmony_ci	 * running.
1158c2ecf20Sopenharmony_ci	 * The tx queue len can be large enough to overflow the txreq_ring.
1168c2ecf20Sopenharmony_ci	 * Use the minimum of the current tx_queue_len or the rings max txreqs
1178c2ecf20Sopenharmony_ci	 * to protect against ring overflow.
1188c2ecf20Sopenharmony_ci	 */
1198c2ecf20Sopenharmony_ci	if (hfi1_ipoib_used(txq) < hfi1_ipoib_ring_lwat(txq) &&
1208c2ecf20Sopenharmony_ci	    atomic_xchg(&txq->ring_full, 0)) {
1218c2ecf20Sopenharmony_ci		trace_hfi1_txq_xmit_unstopped(txq);
1228c2ecf20Sopenharmony_ci		hfi1_ipoib_wake_txq(txq);
1238c2ecf20Sopenharmony_ci	}
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic void hfi1_ipoib_free_tx(struct ipoib_txreq *tx, int budget)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	struct hfi1_ipoib_dev_priv *priv = tx->priv;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	if (likely(!tx->sdma_status)) {
1318c2ecf20Sopenharmony_ci		hfi1_ipoib_update_tx_netstats(priv, 1, tx->skb->len);
1328c2ecf20Sopenharmony_ci	} else {
1338c2ecf20Sopenharmony_ci		++priv->netdev->stats.tx_errors;
1348c2ecf20Sopenharmony_ci		dd_dev_warn(priv->dd,
1358c2ecf20Sopenharmony_ci			    "%s: Status = 0x%x pbc 0x%llx txq = %d sde = %d\n",
1368c2ecf20Sopenharmony_ci			    __func__, tx->sdma_status,
1378c2ecf20Sopenharmony_ci			    le64_to_cpu(tx->sdma_hdr.pbc), tx->txq->q_idx,
1388c2ecf20Sopenharmony_ci			    tx->txq->sde->this_idx);
1398c2ecf20Sopenharmony_ci	}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	napi_consume_skb(tx->skb, budget);
1428c2ecf20Sopenharmony_ci	sdma_txclean(priv->dd, &tx->txreq);
1438c2ecf20Sopenharmony_ci	kmem_cache_free(priv->txreq_cache, tx);
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic int hfi1_ipoib_drain_tx_ring(struct hfi1_ipoib_txq *txq, int budget)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	struct hfi1_ipoib_circ_buf *tx_ring = &txq->tx_ring;
1498c2ecf20Sopenharmony_ci	unsigned long head;
1508c2ecf20Sopenharmony_ci	unsigned long tail;
1518c2ecf20Sopenharmony_ci	unsigned int max_tx;
1528c2ecf20Sopenharmony_ci	int work_done;
1538c2ecf20Sopenharmony_ci	int tx_count;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	spin_lock_bh(&tx_ring->consumer_lock);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	/* Read index before reading contents at that index. */
1588c2ecf20Sopenharmony_ci	head = smp_load_acquire(&tx_ring->head);
1598c2ecf20Sopenharmony_ci	tail = tx_ring->tail;
1608c2ecf20Sopenharmony_ci	max_tx = tx_ring->max_items;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	work_done = min_t(int, CIRC_CNT(head, tail, max_tx), budget);
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	for (tx_count = work_done; tx_count; tx_count--) {
1658c2ecf20Sopenharmony_ci		hfi1_ipoib_free_tx(tx_ring->items[tail], budget);
1668c2ecf20Sopenharmony_ci		tail = CIRC_NEXT(tail, max_tx);
1678c2ecf20Sopenharmony_ci	}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	atomic64_add(work_done, &txq->complete_txreqs);
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	/* Finished freeing tx items so store the tail value. */
1728c2ecf20Sopenharmony_ci	smp_store_release(&tx_ring->tail, tail);
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	spin_unlock_bh(&tx_ring->consumer_lock);
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	hfi1_ipoib_check_queue_stopped(txq);
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	return work_done;
1798c2ecf20Sopenharmony_ci}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_cistatic int hfi1_ipoib_process_tx_ring(struct napi_struct *napi, int budget)
1828c2ecf20Sopenharmony_ci{
1838c2ecf20Sopenharmony_ci	struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(napi->dev);
1848c2ecf20Sopenharmony_ci	struct hfi1_ipoib_txq *txq = &priv->txqs[napi - priv->tx_napis];
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	int work_done = hfi1_ipoib_drain_tx_ring(txq, budget);
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	if (work_done < budget)
1898c2ecf20Sopenharmony_ci		napi_complete_done(napi, work_done);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	return work_done;
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_cistatic void hfi1_ipoib_add_tx(struct ipoib_txreq *tx)
1958c2ecf20Sopenharmony_ci{
1968c2ecf20Sopenharmony_ci	struct hfi1_ipoib_circ_buf *tx_ring = &tx->txq->tx_ring;
1978c2ecf20Sopenharmony_ci	unsigned long head;
1988c2ecf20Sopenharmony_ci	unsigned long tail;
1998c2ecf20Sopenharmony_ci	size_t max_tx;
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	spin_lock(&tx_ring->producer_lock);
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	head = tx_ring->head;
2048c2ecf20Sopenharmony_ci	tail = READ_ONCE(tx_ring->tail);
2058c2ecf20Sopenharmony_ci	max_tx = tx_ring->max_items;
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	if (likely(CIRC_SPACE(head, tail, max_tx))) {
2088c2ecf20Sopenharmony_ci		tx_ring->items[head] = tx;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci		/* Finish storing txreq before incrementing head. */
2118c2ecf20Sopenharmony_ci		smp_store_release(&tx_ring->head, CIRC_ADD(head, 1, max_tx));
2128c2ecf20Sopenharmony_ci		napi_schedule(tx->txq->napi);
2138c2ecf20Sopenharmony_ci	} else {
2148c2ecf20Sopenharmony_ci		struct hfi1_ipoib_txq *txq = tx->txq;
2158c2ecf20Sopenharmony_ci		struct hfi1_ipoib_dev_priv *priv = tx->priv;
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci		/* Ring was full */
2188c2ecf20Sopenharmony_ci		hfi1_ipoib_free_tx(tx, 0);
2198c2ecf20Sopenharmony_ci		atomic64_inc(&txq->complete_txreqs);
2208c2ecf20Sopenharmony_ci		dd_dev_dbg(priv->dd, "txq %d full.\n", txq->q_idx);
2218c2ecf20Sopenharmony_ci	}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	spin_unlock(&tx_ring->producer_lock);
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_cistatic void hfi1_ipoib_sdma_complete(struct sdma_txreq *txreq, int status)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	struct ipoib_txreq *tx = container_of(txreq, struct ipoib_txreq, txreq);
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	tx->sdma_status = status;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	hfi1_ipoib_add_tx(tx);
2338c2ecf20Sopenharmony_ci}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_cistatic int hfi1_ipoib_build_ulp_payload(struct ipoib_txreq *tx,
2368c2ecf20Sopenharmony_ci					struct ipoib_txparms *txp)
2378c2ecf20Sopenharmony_ci{
2388c2ecf20Sopenharmony_ci	struct hfi1_devdata *dd = txp->dd;
2398c2ecf20Sopenharmony_ci	struct sdma_txreq *txreq = &tx->txreq;
2408c2ecf20Sopenharmony_ci	struct sk_buff *skb = tx->skb;
2418c2ecf20Sopenharmony_ci	int ret = 0;
2428c2ecf20Sopenharmony_ci	int i;
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	if (skb_headlen(skb)) {
2458c2ecf20Sopenharmony_ci		ret = sdma_txadd_kvaddr(dd, txreq, skb->data, skb_headlen(skb));
2468c2ecf20Sopenharmony_ci		if (unlikely(ret))
2478c2ecf20Sopenharmony_ci			return ret;
2488c2ecf20Sopenharmony_ci	}
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2518c2ecf20Sopenharmony_ci		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci		ret = sdma_txadd_page(dd,
2548c2ecf20Sopenharmony_ci				      txreq,
2558c2ecf20Sopenharmony_ci				      skb_frag_page(frag),
2568c2ecf20Sopenharmony_ci				      frag->bv_offset,
2578c2ecf20Sopenharmony_ci				      skb_frag_size(frag),
2588c2ecf20Sopenharmony_ci				      NULL, NULL, NULL);
2598c2ecf20Sopenharmony_ci		if (unlikely(ret))
2608c2ecf20Sopenharmony_ci			break;
2618c2ecf20Sopenharmony_ci	}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	return ret;
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic int hfi1_ipoib_build_tx_desc(struct ipoib_txreq *tx,
2678c2ecf20Sopenharmony_ci				    struct ipoib_txparms *txp)
2688c2ecf20Sopenharmony_ci{
2698c2ecf20Sopenharmony_ci	struct hfi1_devdata *dd = txp->dd;
2708c2ecf20Sopenharmony_ci	struct sdma_txreq *txreq = &tx->txreq;
2718c2ecf20Sopenharmony_ci	struct hfi1_sdma_header *sdma_hdr = &tx->sdma_hdr;
2728c2ecf20Sopenharmony_ci	u16 pkt_bytes =
2738c2ecf20Sopenharmony_ci		sizeof(sdma_hdr->pbc) + (txp->hdr_dwords << 2) + tx->skb->len;
2748c2ecf20Sopenharmony_ci	int ret;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	ret = sdma_txinit(txreq, 0, pkt_bytes, hfi1_ipoib_sdma_complete);
2778c2ecf20Sopenharmony_ci	if (unlikely(ret))
2788c2ecf20Sopenharmony_ci		return ret;
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	/* add pbc + headers */
2818c2ecf20Sopenharmony_ci	ret = sdma_txadd_kvaddr(dd,
2828c2ecf20Sopenharmony_ci				txreq,
2838c2ecf20Sopenharmony_ci				sdma_hdr,
2848c2ecf20Sopenharmony_ci				sizeof(sdma_hdr->pbc) + (txp->hdr_dwords << 2));
2858c2ecf20Sopenharmony_ci	if (unlikely(ret))
2868c2ecf20Sopenharmony_ci		return ret;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	/* add the ulp payload */
2898c2ecf20Sopenharmony_ci	return hfi1_ipoib_build_ulp_payload(tx, txp);
2908c2ecf20Sopenharmony_ci}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_cistatic void hfi1_ipoib_build_ib_tx_headers(struct ipoib_txreq *tx,
2938c2ecf20Sopenharmony_ci					   struct ipoib_txparms *txp)
2948c2ecf20Sopenharmony_ci{
2958c2ecf20Sopenharmony_ci	struct hfi1_ipoib_dev_priv *priv = tx->priv;
2968c2ecf20Sopenharmony_ci	struct hfi1_sdma_header *sdma_hdr = &tx->sdma_hdr;
2978c2ecf20Sopenharmony_ci	struct sk_buff *skb = tx->skb;
2988c2ecf20Sopenharmony_ci	struct hfi1_pportdata *ppd = ppd_from_ibp(txp->ibp);
2998c2ecf20Sopenharmony_ci	struct rdma_ah_attr *ah_attr = txp->ah_attr;
3008c2ecf20Sopenharmony_ci	struct ib_other_headers *ohdr;
3018c2ecf20Sopenharmony_ci	struct ib_grh *grh;
3028c2ecf20Sopenharmony_ci	u16 dwords;
3038c2ecf20Sopenharmony_ci	u16 slid;
3048c2ecf20Sopenharmony_ci	u16 dlid;
3058c2ecf20Sopenharmony_ci	u16 lrh0;
3068c2ecf20Sopenharmony_ci	u32 bth0;
3078c2ecf20Sopenharmony_ci	u32 sqpn = (u32)(priv->netdev->dev_addr[1] << 16 |
3088c2ecf20Sopenharmony_ci			 priv->netdev->dev_addr[2] << 8 |
3098c2ecf20Sopenharmony_ci			 priv->netdev->dev_addr[3]);
3108c2ecf20Sopenharmony_ci	u16 payload_dwords;
3118c2ecf20Sopenharmony_ci	u8 pad_cnt;
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	pad_cnt = -skb->len & 3;
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	/* Includes ICRC */
3168c2ecf20Sopenharmony_ci	payload_dwords = ((skb->len + pad_cnt) >> 2) + SIZE_OF_CRC;
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	/* header size in dwords LRH+BTH+DETH = (8+12+8)/4. */
3198c2ecf20Sopenharmony_ci	txp->hdr_dwords = 7;
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
3228c2ecf20Sopenharmony_ci		grh = &sdma_hdr->hdr.ibh.u.l.grh;
3238c2ecf20Sopenharmony_ci		txp->hdr_dwords +=
3248c2ecf20Sopenharmony_ci			hfi1_make_grh(txp->ibp,
3258c2ecf20Sopenharmony_ci				      grh,
3268c2ecf20Sopenharmony_ci				      rdma_ah_read_grh(ah_attr),
3278c2ecf20Sopenharmony_ci				      txp->hdr_dwords - LRH_9B_DWORDS,
3288c2ecf20Sopenharmony_ci				      payload_dwords);
3298c2ecf20Sopenharmony_ci		lrh0 = HFI1_LRH_GRH;
3308c2ecf20Sopenharmony_ci		ohdr = &sdma_hdr->hdr.ibh.u.l.oth;
3318c2ecf20Sopenharmony_ci	} else {
3328c2ecf20Sopenharmony_ci		lrh0 = HFI1_LRH_BTH;
3338c2ecf20Sopenharmony_ci		ohdr = &sdma_hdr->hdr.ibh.u.oth;
3348c2ecf20Sopenharmony_ci	}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	lrh0 |= (rdma_ah_get_sl(ah_attr) & 0xf) << 4;
3378c2ecf20Sopenharmony_ci	lrh0 |= (txp->flow.sc5 & 0xf) << 12;
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	dlid = opa_get_lid(rdma_ah_get_dlid(ah_attr), 9B);
3408c2ecf20Sopenharmony_ci	if (dlid == be16_to_cpu(IB_LID_PERMISSIVE)) {
3418c2ecf20Sopenharmony_ci		slid = be16_to_cpu(IB_LID_PERMISSIVE);
3428c2ecf20Sopenharmony_ci	} else {
3438c2ecf20Sopenharmony_ci		u16 lid = (u16)ppd->lid;
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci		if (lid) {
3468c2ecf20Sopenharmony_ci			lid |= rdma_ah_get_path_bits(ah_attr) &
3478c2ecf20Sopenharmony_ci				((1 << ppd->lmc) - 1);
3488c2ecf20Sopenharmony_ci			slid = lid;
3498c2ecf20Sopenharmony_ci		} else {
3508c2ecf20Sopenharmony_ci			slid = be16_to_cpu(IB_LID_PERMISSIVE);
3518c2ecf20Sopenharmony_ci		}
3528c2ecf20Sopenharmony_ci	}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	/* Includes ICRC */
3558c2ecf20Sopenharmony_ci	dwords = txp->hdr_dwords + payload_dwords;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	/* Build the lrh */
3588c2ecf20Sopenharmony_ci	sdma_hdr->hdr.hdr_type = HFI1_PKT_TYPE_9B;
3598c2ecf20Sopenharmony_ci	hfi1_make_ib_hdr(&sdma_hdr->hdr.ibh, lrh0, dwords, dlid, slid);
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	/* Build the bth */
3628c2ecf20Sopenharmony_ci	bth0 = (IB_OPCODE_UD_SEND_ONLY << 24) | (pad_cnt << 20) | priv->pkey;
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	ohdr->bth[0] = cpu_to_be32(bth0);
3658c2ecf20Sopenharmony_ci	ohdr->bth[1] = cpu_to_be32(txp->dqpn);
3668c2ecf20Sopenharmony_ci	ohdr->bth[2] = cpu_to_be32(mask_psn((u32)txp->txq->sent_txreqs));
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	/* Build the deth */
3698c2ecf20Sopenharmony_ci	ohdr->u.ud.deth[0] = cpu_to_be32(priv->qkey);
3708c2ecf20Sopenharmony_ci	ohdr->u.ud.deth[1] = cpu_to_be32((txp->entropy <<
3718c2ecf20Sopenharmony_ci					  HFI1_IPOIB_ENTROPY_SHIFT) | sqpn);
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	/* Construct the pbc. */
3748c2ecf20Sopenharmony_ci	sdma_hdr->pbc =
3758c2ecf20Sopenharmony_ci		cpu_to_le64(create_pbc(ppd,
3768c2ecf20Sopenharmony_ci				       ib_is_sc5(txp->flow.sc5) <<
3778c2ecf20Sopenharmony_ci							      PBC_DC_INFO_SHIFT,
3788c2ecf20Sopenharmony_ci				       0,
3798c2ecf20Sopenharmony_ci				       sc_to_vlt(priv->dd, txp->flow.sc5),
3808c2ecf20Sopenharmony_ci				       dwords - SIZE_OF_CRC +
3818c2ecf20Sopenharmony_ci						(sizeof(sdma_hdr->pbc) >> 2)));
3828c2ecf20Sopenharmony_ci}
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_cistatic struct ipoib_txreq *hfi1_ipoib_send_dma_common(struct net_device *dev,
3858c2ecf20Sopenharmony_ci						      struct sk_buff *skb,
3868c2ecf20Sopenharmony_ci						      struct ipoib_txparms *txp)
3878c2ecf20Sopenharmony_ci{
3888c2ecf20Sopenharmony_ci	struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(dev);
3898c2ecf20Sopenharmony_ci	struct ipoib_txreq *tx;
3908c2ecf20Sopenharmony_ci	int ret;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	tx = kmem_cache_alloc_node(priv->txreq_cache,
3938c2ecf20Sopenharmony_ci				   GFP_ATOMIC,
3948c2ecf20Sopenharmony_ci				   priv->dd->node);
3958c2ecf20Sopenharmony_ci	if (unlikely(!tx))
3968c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	/* so that we can test if the sdma descriptors are there */
3998c2ecf20Sopenharmony_ci	tx->txreq.num_desc = 0;
4008c2ecf20Sopenharmony_ci	tx->priv = priv;
4018c2ecf20Sopenharmony_ci	tx->txq = txp->txq;
4028c2ecf20Sopenharmony_ci	tx->skb = skb;
4038c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&tx->txreq.list);
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	hfi1_ipoib_build_ib_tx_headers(tx, txp);
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	ret = hfi1_ipoib_build_tx_desc(tx, txp);
4088c2ecf20Sopenharmony_ci	if (likely(!ret)) {
4098c2ecf20Sopenharmony_ci		if (txp->txq->flow.as_int != txp->flow.as_int) {
4108c2ecf20Sopenharmony_ci			txp->txq->flow.tx_queue = txp->flow.tx_queue;
4118c2ecf20Sopenharmony_ci			txp->txq->flow.sc5 = txp->flow.sc5;
4128c2ecf20Sopenharmony_ci			txp->txq->sde =
4138c2ecf20Sopenharmony_ci				sdma_select_engine_sc(priv->dd,
4148c2ecf20Sopenharmony_ci						      txp->flow.tx_queue,
4158c2ecf20Sopenharmony_ci						      txp->flow.sc5);
4168c2ecf20Sopenharmony_ci			trace_hfi1_flow_switch(txp->txq);
4178c2ecf20Sopenharmony_ci		}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci		return tx;
4208c2ecf20Sopenharmony_ci	}
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	sdma_txclean(priv->dd, &tx->txreq);
4238c2ecf20Sopenharmony_ci	kmem_cache_free(priv->txreq_cache, tx);
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	return ERR_PTR(ret);
4268c2ecf20Sopenharmony_ci}
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_cistatic int hfi1_ipoib_submit_tx_list(struct net_device *dev,
4298c2ecf20Sopenharmony_ci				     struct hfi1_ipoib_txq *txq)
4308c2ecf20Sopenharmony_ci{
4318c2ecf20Sopenharmony_ci	int ret;
4328c2ecf20Sopenharmony_ci	u16 count_out;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	ret = sdma_send_txlist(txq->sde,
4358c2ecf20Sopenharmony_ci			       iowait_get_ib_work(&txq->wait),
4368c2ecf20Sopenharmony_ci			       &txq->tx_list,
4378c2ecf20Sopenharmony_ci			       &count_out);
4388c2ecf20Sopenharmony_ci	if (likely(!ret) || ret == -EBUSY || ret == -ECOMM)
4398c2ecf20Sopenharmony_ci		return ret;
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	dd_dev_warn(txq->priv->dd, "cannot send skb tx list, err %d.\n", ret);
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	return ret;
4448c2ecf20Sopenharmony_ci}
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_cistatic int hfi1_ipoib_flush_tx_list(struct net_device *dev,
4478c2ecf20Sopenharmony_ci				    struct hfi1_ipoib_txq *txq)
4488c2ecf20Sopenharmony_ci{
4498c2ecf20Sopenharmony_ci	int ret = 0;
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	if (!list_empty(&txq->tx_list)) {
4528c2ecf20Sopenharmony_ci		/* Flush the current list */
4538c2ecf20Sopenharmony_ci		ret = hfi1_ipoib_submit_tx_list(dev, txq);
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci		if (unlikely(ret))
4568c2ecf20Sopenharmony_ci			if (ret != -EBUSY)
4578c2ecf20Sopenharmony_ci				++dev->stats.tx_carrier_errors;
4588c2ecf20Sopenharmony_ci	}
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	return ret;
4618c2ecf20Sopenharmony_ci}
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_cistatic int hfi1_ipoib_submit_tx(struct hfi1_ipoib_txq *txq,
4648c2ecf20Sopenharmony_ci				struct ipoib_txreq *tx)
4658c2ecf20Sopenharmony_ci{
4668c2ecf20Sopenharmony_ci	int ret;
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	ret = sdma_send_txreq(txq->sde,
4698c2ecf20Sopenharmony_ci			      iowait_get_ib_work(&txq->wait),
4708c2ecf20Sopenharmony_ci			      &tx->txreq,
4718c2ecf20Sopenharmony_ci			      txq->pkts_sent);
4728c2ecf20Sopenharmony_ci	if (likely(!ret)) {
4738c2ecf20Sopenharmony_ci		txq->pkts_sent = true;
4748c2ecf20Sopenharmony_ci		iowait_starve_clear(txq->pkts_sent, &txq->wait);
4758c2ecf20Sopenharmony_ci	}
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	return ret;
4788c2ecf20Sopenharmony_ci}
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_cistatic int hfi1_ipoib_send_dma_single(struct net_device *dev,
4818c2ecf20Sopenharmony_ci				      struct sk_buff *skb,
4828c2ecf20Sopenharmony_ci				      struct ipoib_txparms *txp)
4838c2ecf20Sopenharmony_ci{
4848c2ecf20Sopenharmony_ci	struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(dev);
4858c2ecf20Sopenharmony_ci	struct hfi1_ipoib_txq *txq = txp->txq;
4868c2ecf20Sopenharmony_ci	struct ipoib_txreq *tx;
4878c2ecf20Sopenharmony_ci	int ret;
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	tx = hfi1_ipoib_send_dma_common(dev, skb, txp);
4908c2ecf20Sopenharmony_ci	if (IS_ERR(tx)) {
4918c2ecf20Sopenharmony_ci		int ret = PTR_ERR(tx);
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci		dev_kfree_skb_any(skb);
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci		if (ret == -ENOMEM)
4968c2ecf20Sopenharmony_ci			++dev->stats.tx_errors;
4978c2ecf20Sopenharmony_ci		else
4988c2ecf20Sopenharmony_ci			++dev->stats.tx_carrier_errors;
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci		return NETDEV_TX_OK;
5018c2ecf20Sopenharmony_ci	}
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	ret = hfi1_ipoib_submit_tx(txq, tx);
5048c2ecf20Sopenharmony_ci	if (likely(!ret)) {
5058c2ecf20Sopenharmony_citx_ok:
5068c2ecf20Sopenharmony_ci		trace_sdma_output_ibhdr(tx->priv->dd,
5078c2ecf20Sopenharmony_ci					&tx->sdma_hdr.hdr,
5088c2ecf20Sopenharmony_ci					ib_is_sc5(txp->flow.sc5));
5098c2ecf20Sopenharmony_ci		hfi1_ipoib_check_queue_depth(txq);
5108c2ecf20Sopenharmony_ci		return NETDEV_TX_OK;
5118c2ecf20Sopenharmony_ci	}
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	txq->pkts_sent = false;
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	if (ret == -EBUSY || ret == -ECOMM)
5168c2ecf20Sopenharmony_ci		goto tx_ok;
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	sdma_txclean(priv->dd, &tx->txreq);
5198c2ecf20Sopenharmony_ci	dev_kfree_skb_any(skb);
5208c2ecf20Sopenharmony_ci	kmem_cache_free(priv->txreq_cache, tx);
5218c2ecf20Sopenharmony_ci	++dev->stats.tx_carrier_errors;
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	return NETDEV_TX_OK;
5248c2ecf20Sopenharmony_ci}
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_cistatic int hfi1_ipoib_send_dma_list(struct net_device *dev,
5278c2ecf20Sopenharmony_ci				    struct sk_buff *skb,
5288c2ecf20Sopenharmony_ci				    struct ipoib_txparms *txp)
5298c2ecf20Sopenharmony_ci{
5308c2ecf20Sopenharmony_ci	struct hfi1_ipoib_txq *txq = txp->txq;
5318c2ecf20Sopenharmony_ci	struct ipoib_txreq *tx;
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	/* Has the flow change ? */
5348c2ecf20Sopenharmony_ci	if (txq->flow.as_int != txp->flow.as_int) {
5358c2ecf20Sopenharmony_ci		int ret;
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci		trace_hfi1_flow_flush(txq);
5388c2ecf20Sopenharmony_ci		ret = hfi1_ipoib_flush_tx_list(dev, txq);
5398c2ecf20Sopenharmony_ci		if (unlikely(ret)) {
5408c2ecf20Sopenharmony_ci			if (ret == -EBUSY)
5418c2ecf20Sopenharmony_ci				++dev->stats.tx_dropped;
5428c2ecf20Sopenharmony_ci			dev_kfree_skb_any(skb);
5438c2ecf20Sopenharmony_ci			return NETDEV_TX_OK;
5448c2ecf20Sopenharmony_ci		}
5458c2ecf20Sopenharmony_ci	}
5468c2ecf20Sopenharmony_ci	tx = hfi1_ipoib_send_dma_common(dev, skb, txp);
5478c2ecf20Sopenharmony_ci	if (IS_ERR(tx)) {
5488c2ecf20Sopenharmony_ci		int ret = PTR_ERR(tx);
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci		dev_kfree_skb_any(skb);
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci		if (ret == -ENOMEM)
5538c2ecf20Sopenharmony_ci			++dev->stats.tx_errors;
5548c2ecf20Sopenharmony_ci		else
5558c2ecf20Sopenharmony_ci			++dev->stats.tx_carrier_errors;
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci		return NETDEV_TX_OK;
5588c2ecf20Sopenharmony_ci	}
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	list_add_tail(&tx->txreq.list, &txq->tx_list);
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	hfi1_ipoib_check_queue_depth(txq);
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	trace_sdma_output_ibhdr(tx->priv->dd,
5658c2ecf20Sopenharmony_ci				&tx->sdma_hdr.hdr,
5668c2ecf20Sopenharmony_ci				ib_is_sc5(txp->flow.sc5));
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	if (!netdev_xmit_more())
5698c2ecf20Sopenharmony_ci		(void)hfi1_ipoib_flush_tx_list(dev, txq);
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	return NETDEV_TX_OK;
5728c2ecf20Sopenharmony_ci}
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_cistatic u8 hfi1_ipoib_calc_entropy(struct sk_buff *skb)
5758c2ecf20Sopenharmony_ci{
5768c2ecf20Sopenharmony_ci	if (skb_transport_header_was_set(skb)) {
5778c2ecf20Sopenharmony_ci		u8 *hdr = (u8 *)skb_transport_header(skb);
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci		return (hdr[0] ^ hdr[1] ^ hdr[2] ^ hdr[3]);
5808c2ecf20Sopenharmony_ci	}
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	return (u8)skb_get_queue_mapping(skb);
5838c2ecf20Sopenharmony_ci}
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ciint hfi1_ipoib_send_dma(struct net_device *dev,
5868c2ecf20Sopenharmony_ci			struct sk_buff *skb,
5878c2ecf20Sopenharmony_ci			struct ib_ah *address,
5888c2ecf20Sopenharmony_ci			u32 dqpn)
5898c2ecf20Sopenharmony_ci{
5908c2ecf20Sopenharmony_ci	struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(dev);
5918c2ecf20Sopenharmony_ci	struct ipoib_txparms txp;
5928c2ecf20Sopenharmony_ci	struct rdma_netdev *rn = netdev_priv(dev);
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci	if (unlikely(skb->len > rn->mtu + HFI1_IPOIB_ENCAP_LEN)) {
5958c2ecf20Sopenharmony_ci		dd_dev_warn(priv->dd, "packet len %d (> %d) too long to send, dropping\n",
5968c2ecf20Sopenharmony_ci			    skb->len,
5978c2ecf20Sopenharmony_ci			    rn->mtu + HFI1_IPOIB_ENCAP_LEN);
5988c2ecf20Sopenharmony_ci		++dev->stats.tx_dropped;
5998c2ecf20Sopenharmony_ci		++dev->stats.tx_errors;
6008c2ecf20Sopenharmony_ci		dev_kfree_skb_any(skb);
6018c2ecf20Sopenharmony_ci		return NETDEV_TX_OK;
6028c2ecf20Sopenharmony_ci	}
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	txp.dd = priv->dd;
6058c2ecf20Sopenharmony_ci	txp.ah_attr = &ibah_to_rvtah(address)->attr;
6068c2ecf20Sopenharmony_ci	txp.ibp = to_iport(priv->device, priv->port_num);
6078c2ecf20Sopenharmony_ci	txp.txq = &priv->txqs[skb_get_queue_mapping(skb)];
6088c2ecf20Sopenharmony_ci	txp.dqpn = dqpn;
6098c2ecf20Sopenharmony_ci	txp.flow.sc5 = txp.ibp->sl_to_sc[rdma_ah_get_sl(txp.ah_attr)];
6108c2ecf20Sopenharmony_ci	txp.flow.tx_queue = (u8)skb_get_queue_mapping(skb);
6118c2ecf20Sopenharmony_ci	txp.entropy = hfi1_ipoib_calc_entropy(skb);
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	if (netdev_xmit_more() || !list_empty(&txp.txq->tx_list))
6148c2ecf20Sopenharmony_ci		return hfi1_ipoib_send_dma_list(dev, skb, &txp);
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci	return hfi1_ipoib_send_dma_single(dev, skb,  &txp);
6178c2ecf20Sopenharmony_ci}
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci/*
6208c2ecf20Sopenharmony_ci * hfi1_ipoib_sdma_sleep - ipoib sdma sleep function
6218c2ecf20Sopenharmony_ci *
6228c2ecf20Sopenharmony_ci * This function gets called from sdma_send_txreq() when there are not enough
6238c2ecf20Sopenharmony_ci * sdma descriptors available to send the packet. It adds Tx queue's wait
6248c2ecf20Sopenharmony_ci * structure to sdma engine's dmawait list to be woken up when descriptors
6258c2ecf20Sopenharmony_ci * become available.
6268c2ecf20Sopenharmony_ci */
6278c2ecf20Sopenharmony_cistatic int hfi1_ipoib_sdma_sleep(struct sdma_engine *sde,
6288c2ecf20Sopenharmony_ci				 struct iowait_work *wait,
6298c2ecf20Sopenharmony_ci				 struct sdma_txreq *txreq,
6308c2ecf20Sopenharmony_ci				 uint seq,
6318c2ecf20Sopenharmony_ci				 bool pkts_sent)
6328c2ecf20Sopenharmony_ci{
6338c2ecf20Sopenharmony_ci	struct hfi1_ipoib_txq *txq =
6348c2ecf20Sopenharmony_ci		container_of(wait->iow, struct hfi1_ipoib_txq, wait);
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	write_seqlock(&sde->waitlock);
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	if (likely(txq->priv->netdev->reg_state == NETREG_REGISTERED)) {
6398c2ecf20Sopenharmony_ci		if (sdma_progress(sde, seq, txreq)) {
6408c2ecf20Sopenharmony_ci			write_sequnlock(&sde->waitlock);
6418c2ecf20Sopenharmony_ci			return -EAGAIN;
6428c2ecf20Sopenharmony_ci		}
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci		if (list_empty(&txreq->list))
6458c2ecf20Sopenharmony_ci			/* came from non-list submit */
6468c2ecf20Sopenharmony_ci			list_add_tail(&txreq->list, &txq->tx_list);
6478c2ecf20Sopenharmony_ci		if (list_empty(&txq->wait.list)) {
6488c2ecf20Sopenharmony_ci			if (!atomic_xchg(&txq->no_desc, 1)) {
6498c2ecf20Sopenharmony_ci				trace_hfi1_txq_queued(txq);
6508c2ecf20Sopenharmony_ci				hfi1_ipoib_stop_txq(txq);
6518c2ecf20Sopenharmony_ci			}
6528c2ecf20Sopenharmony_ci			iowait_queue(pkts_sent, wait->iow, &sde->dmawait);
6538c2ecf20Sopenharmony_ci		}
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci		write_sequnlock(&sde->waitlock);
6568c2ecf20Sopenharmony_ci		return -EBUSY;
6578c2ecf20Sopenharmony_ci	}
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci	write_sequnlock(&sde->waitlock);
6608c2ecf20Sopenharmony_ci	return -EINVAL;
6618c2ecf20Sopenharmony_ci}
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ci/*
6648c2ecf20Sopenharmony_ci * hfi1_ipoib_sdma_wakeup - ipoib sdma wakeup function
6658c2ecf20Sopenharmony_ci *
6668c2ecf20Sopenharmony_ci * This function gets called when SDMA descriptors becomes available and Tx
6678c2ecf20Sopenharmony_ci * queue's wait structure was previously added to sdma engine's dmawait list.
6688c2ecf20Sopenharmony_ci */
6698c2ecf20Sopenharmony_cistatic void hfi1_ipoib_sdma_wakeup(struct iowait *wait, int reason)
6708c2ecf20Sopenharmony_ci{
6718c2ecf20Sopenharmony_ci	struct hfi1_ipoib_txq *txq =
6728c2ecf20Sopenharmony_ci		container_of(wait, struct hfi1_ipoib_txq, wait);
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	trace_hfi1_txq_wakeup(txq);
6758c2ecf20Sopenharmony_ci	if (likely(txq->priv->netdev->reg_state == NETREG_REGISTERED))
6768c2ecf20Sopenharmony_ci		iowait_schedule(wait, system_highpri_wq, WORK_CPU_UNBOUND);
6778c2ecf20Sopenharmony_ci}
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_cistatic void hfi1_ipoib_flush_txq(struct work_struct *work)
6808c2ecf20Sopenharmony_ci{
6818c2ecf20Sopenharmony_ci	struct iowait_work *ioww =
6828c2ecf20Sopenharmony_ci		container_of(work, struct iowait_work, iowork);
6838c2ecf20Sopenharmony_ci	struct iowait *wait = iowait_ioww_to_iow(ioww);
6848c2ecf20Sopenharmony_ci	struct hfi1_ipoib_txq *txq =
6858c2ecf20Sopenharmony_ci		container_of(wait, struct hfi1_ipoib_txq, wait);
6868c2ecf20Sopenharmony_ci	struct net_device *dev = txq->priv->netdev;
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	if (likely(dev->reg_state == NETREG_REGISTERED) &&
6898c2ecf20Sopenharmony_ci	    likely(!hfi1_ipoib_flush_tx_list(dev, txq)))
6908c2ecf20Sopenharmony_ci		if (atomic_xchg(&txq->no_desc, 0))
6918c2ecf20Sopenharmony_ci			hfi1_ipoib_wake_txq(txq);
6928c2ecf20Sopenharmony_ci}
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ciint hfi1_ipoib_txreq_init(struct hfi1_ipoib_dev_priv *priv)
6958c2ecf20Sopenharmony_ci{
6968c2ecf20Sopenharmony_ci	struct net_device *dev = priv->netdev;
6978c2ecf20Sopenharmony_ci	char buf[HFI1_IPOIB_TXREQ_NAME_LEN];
6988c2ecf20Sopenharmony_ci	unsigned long tx_ring_size;
6998c2ecf20Sopenharmony_ci	int i;
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci	/*
7028c2ecf20Sopenharmony_ci	 * Ring holds 1 less than tx_ring_size
7038c2ecf20Sopenharmony_ci	 * Round up to next power of 2 in order to hold at least tx_queue_len
7048c2ecf20Sopenharmony_ci	 */
7058c2ecf20Sopenharmony_ci	tx_ring_size = roundup_pow_of_two((unsigned long)dev->tx_queue_len + 1);
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	snprintf(buf, sizeof(buf), "hfi1_%u_ipoib_txreq_cache", priv->dd->unit);
7088c2ecf20Sopenharmony_ci	priv->txreq_cache = kmem_cache_create(buf,
7098c2ecf20Sopenharmony_ci					      sizeof(struct ipoib_txreq),
7108c2ecf20Sopenharmony_ci					      0,
7118c2ecf20Sopenharmony_ci					      0,
7128c2ecf20Sopenharmony_ci					      NULL);
7138c2ecf20Sopenharmony_ci	if (!priv->txreq_cache)
7148c2ecf20Sopenharmony_ci		return -ENOMEM;
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_ci	priv->tx_napis = kcalloc_node(dev->num_tx_queues,
7178c2ecf20Sopenharmony_ci				      sizeof(struct napi_struct),
7188c2ecf20Sopenharmony_ci				      GFP_KERNEL,
7198c2ecf20Sopenharmony_ci				      priv->dd->node);
7208c2ecf20Sopenharmony_ci	if (!priv->tx_napis)
7218c2ecf20Sopenharmony_ci		goto free_txreq_cache;
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	priv->txqs = kcalloc_node(dev->num_tx_queues,
7248c2ecf20Sopenharmony_ci				  sizeof(struct hfi1_ipoib_txq),
7258c2ecf20Sopenharmony_ci				  GFP_KERNEL,
7268c2ecf20Sopenharmony_ci				  priv->dd->node);
7278c2ecf20Sopenharmony_ci	if (!priv->txqs)
7288c2ecf20Sopenharmony_ci		goto free_tx_napis;
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci	for (i = 0; i < dev->num_tx_queues; i++) {
7318c2ecf20Sopenharmony_ci		struct hfi1_ipoib_txq *txq = &priv->txqs[i];
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci		iowait_init(&txq->wait,
7348c2ecf20Sopenharmony_ci			    0,
7358c2ecf20Sopenharmony_ci			    hfi1_ipoib_flush_txq,
7368c2ecf20Sopenharmony_ci			    NULL,
7378c2ecf20Sopenharmony_ci			    hfi1_ipoib_sdma_sleep,
7388c2ecf20Sopenharmony_ci			    hfi1_ipoib_sdma_wakeup,
7398c2ecf20Sopenharmony_ci			    NULL,
7408c2ecf20Sopenharmony_ci			    NULL);
7418c2ecf20Sopenharmony_ci		txq->priv = priv;
7428c2ecf20Sopenharmony_ci		txq->sde = NULL;
7438c2ecf20Sopenharmony_ci		INIT_LIST_HEAD(&txq->tx_list);
7448c2ecf20Sopenharmony_ci		atomic64_set(&txq->complete_txreqs, 0);
7458c2ecf20Sopenharmony_ci		atomic_set(&txq->stops, 0);
7468c2ecf20Sopenharmony_ci		atomic_set(&txq->ring_full, 0);
7478c2ecf20Sopenharmony_ci		atomic_set(&txq->no_desc, 0);
7488c2ecf20Sopenharmony_ci		txq->q_idx = i;
7498c2ecf20Sopenharmony_ci		txq->flow.tx_queue = 0xff;
7508c2ecf20Sopenharmony_ci		txq->flow.sc5 = 0xff;
7518c2ecf20Sopenharmony_ci		txq->pkts_sent = false;
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci		netdev_queue_numa_node_write(netdev_get_tx_queue(dev, i),
7548c2ecf20Sopenharmony_ci					     priv->dd->node);
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci		txq->tx_ring.items =
7578c2ecf20Sopenharmony_ci			kcalloc_node(tx_ring_size,
7588c2ecf20Sopenharmony_ci				     sizeof(struct ipoib_txreq *),
7598c2ecf20Sopenharmony_ci				     GFP_KERNEL, priv->dd->node);
7608c2ecf20Sopenharmony_ci		if (!txq->tx_ring.items)
7618c2ecf20Sopenharmony_ci			goto free_txqs;
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci		spin_lock_init(&txq->tx_ring.producer_lock);
7648c2ecf20Sopenharmony_ci		spin_lock_init(&txq->tx_ring.consumer_lock);
7658c2ecf20Sopenharmony_ci		txq->tx_ring.max_items = tx_ring_size;
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci		txq->napi = &priv->tx_napis[i];
7688c2ecf20Sopenharmony_ci		netif_tx_napi_add(dev, txq->napi,
7698c2ecf20Sopenharmony_ci				  hfi1_ipoib_process_tx_ring,
7708c2ecf20Sopenharmony_ci				  NAPI_POLL_WEIGHT);
7718c2ecf20Sopenharmony_ci	}
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci	return 0;
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_cifree_txqs:
7768c2ecf20Sopenharmony_ci	for (i--; i >= 0; i--) {
7778c2ecf20Sopenharmony_ci		struct hfi1_ipoib_txq *txq = &priv->txqs[i];
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci		netif_napi_del(txq->napi);
7808c2ecf20Sopenharmony_ci		kfree(txq->tx_ring.items);
7818c2ecf20Sopenharmony_ci	}
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	kfree(priv->txqs);
7848c2ecf20Sopenharmony_ci	priv->txqs = NULL;
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_cifree_tx_napis:
7878c2ecf20Sopenharmony_ci	kfree(priv->tx_napis);
7888c2ecf20Sopenharmony_ci	priv->tx_napis = NULL;
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_cifree_txreq_cache:
7918c2ecf20Sopenharmony_ci	kmem_cache_destroy(priv->txreq_cache);
7928c2ecf20Sopenharmony_ci	priv->txreq_cache = NULL;
7938c2ecf20Sopenharmony_ci	return -ENOMEM;
7948c2ecf20Sopenharmony_ci}
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_cistatic void hfi1_ipoib_drain_tx_list(struct hfi1_ipoib_txq *txq)
7978c2ecf20Sopenharmony_ci{
7988c2ecf20Sopenharmony_ci	struct sdma_txreq *txreq;
7998c2ecf20Sopenharmony_ci	struct sdma_txreq *txreq_tmp;
8008c2ecf20Sopenharmony_ci	atomic64_t *complete_txreqs = &txq->complete_txreqs;
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci	list_for_each_entry_safe(txreq, txreq_tmp, &txq->tx_list, list) {
8038c2ecf20Sopenharmony_ci		struct ipoib_txreq *tx =
8048c2ecf20Sopenharmony_ci			container_of(txreq, struct ipoib_txreq, txreq);
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci		list_del(&txreq->list);
8078c2ecf20Sopenharmony_ci		sdma_txclean(txq->priv->dd, &tx->txreq);
8088c2ecf20Sopenharmony_ci		dev_kfree_skb_any(tx->skb);
8098c2ecf20Sopenharmony_ci		kmem_cache_free(txq->priv->txreq_cache, tx);
8108c2ecf20Sopenharmony_ci		atomic64_inc(complete_txreqs);
8118c2ecf20Sopenharmony_ci	}
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci	if (hfi1_ipoib_used(txq))
8148c2ecf20Sopenharmony_ci		dd_dev_warn(txq->priv->dd,
8158c2ecf20Sopenharmony_ci			    "txq %d not empty found %llu requests\n",
8168c2ecf20Sopenharmony_ci			    txq->q_idx,
8178c2ecf20Sopenharmony_ci			    hfi1_ipoib_txreqs(txq->sent_txreqs,
8188c2ecf20Sopenharmony_ci					      atomic64_read(complete_txreqs)));
8198c2ecf20Sopenharmony_ci}
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_civoid hfi1_ipoib_txreq_deinit(struct hfi1_ipoib_dev_priv *priv)
8228c2ecf20Sopenharmony_ci{
8238c2ecf20Sopenharmony_ci	int i;
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci	for (i = 0; i < priv->netdev->num_tx_queues; i++) {
8268c2ecf20Sopenharmony_ci		struct hfi1_ipoib_txq *txq = &priv->txqs[i];
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_ci		iowait_cancel_work(&txq->wait);
8298c2ecf20Sopenharmony_ci		iowait_sdma_drain(&txq->wait);
8308c2ecf20Sopenharmony_ci		hfi1_ipoib_drain_tx_list(txq);
8318c2ecf20Sopenharmony_ci		netif_napi_del(txq->napi);
8328c2ecf20Sopenharmony_ci		(void)hfi1_ipoib_drain_tx_ring(txq, txq->tx_ring.max_items);
8338c2ecf20Sopenharmony_ci		kfree(txq->tx_ring.items);
8348c2ecf20Sopenharmony_ci	}
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci	kfree(priv->txqs);
8378c2ecf20Sopenharmony_ci	priv->txqs = NULL;
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci	kfree(priv->tx_napis);
8408c2ecf20Sopenharmony_ci	priv->tx_napis = NULL;
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci	kmem_cache_destroy(priv->txreq_cache);
8438c2ecf20Sopenharmony_ci	priv->txreq_cache = NULL;
8448c2ecf20Sopenharmony_ci}
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_civoid hfi1_ipoib_napi_tx_enable(struct net_device *dev)
8478c2ecf20Sopenharmony_ci{
8488c2ecf20Sopenharmony_ci	struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(dev);
8498c2ecf20Sopenharmony_ci	int i;
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_ci	for (i = 0; i < dev->num_tx_queues; i++) {
8528c2ecf20Sopenharmony_ci		struct hfi1_ipoib_txq *txq = &priv->txqs[i];
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_ci		napi_enable(txq->napi);
8558c2ecf20Sopenharmony_ci	}
8568c2ecf20Sopenharmony_ci}
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_civoid hfi1_ipoib_napi_tx_disable(struct net_device *dev)
8598c2ecf20Sopenharmony_ci{
8608c2ecf20Sopenharmony_ci	struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(dev);
8618c2ecf20Sopenharmony_ci	int i;
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci	for (i = 0; i < dev->num_tx_queues; i++) {
8648c2ecf20Sopenharmony_ci		struct hfi1_ipoib_txq *txq = &priv->txqs[i];
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_ci		napi_disable(txq->napi);
8678c2ecf20Sopenharmony_ci		(void)hfi1_ipoib_drain_tx_ring(txq, txq->tx_ring.max_items);
8688c2ecf20Sopenharmony_ci	}
8698c2ecf20Sopenharmony_ci}
870