18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
38c2ecf20Sopenharmony_ci * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
48c2ecf20Sopenharmony_ci * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
58c2ecf20Sopenharmony_ci * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two
88c2ecf20Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
98c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
108c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the
118c2ecf20Sopenharmony_ci * OpenIB.org BSD license below:
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
148c2ecf20Sopenharmony_ci *     without modification, are permitted provided that the following
158c2ecf20Sopenharmony_ci *     conditions are met:
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci *      - Redistributions of source code must retain the above
188c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
198c2ecf20Sopenharmony_ci *        disclaimer.
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
228c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
238c2ecf20Sopenharmony_ci *        disclaimer in the documentation and/or other materials
248c2ecf20Sopenharmony_ci *        provided with the distribution.
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
278c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
288c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
298c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
308c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
318c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
328c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
338c2ecf20Sopenharmony_ci * SOFTWARE.
348c2ecf20Sopenharmony_ci */
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#include <linux/delay.h>
378c2ecf20Sopenharmony_ci#include <linux/moduleparam.h>
388c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
398c2ecf20Sopenharmony_ci#include <linux/slab.h>
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#include <linux/ip.h>
428c2ecf20Sopenharmony_ci#include <linux/tcp.h>
438c2ecf20Sopenharmony_ci#include <rdma/ib_cache.h>
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#include "ipoib.h"
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
488c2ecf20Sopenharmony_cistatic int data_debug_level;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cimodule_param(data_debug_level, int, 0644);
518c2ecf20Sopenharmony_ciMODULE_PARM_DESC(data_debug_level,
528c2ecf20Sopenharmony_ci		 "Enable data path debug tracing if > 0");
538c2ecf20Sopenharmony_ci#endif
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistruct ipoib_ah *ipoib_create_ah(struct net_device *dev,
568c2ecf20Sopenharmony_ci				 struct ib_pd *pd, struct rdma_ah_attr *attr)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	struct ipoib_ah *ah;
598c2ecf20Sopenharmony_ci	struct ib_ah *vah;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	ah = kmalloc(sizeof(*ah), GFP_KERNEL);
628c2ecf20Sopenharmony_ci	if (!ah)
638c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	ah->dev       = dev;
668c2ecf20Sopenharmony_ci	ah->last_send = 0;
678c2ecf20Sopenharmony_ci	kref_init(&ah->ref);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	vah = rdma_create_ah(pd, attr, RDMA_CREATE_AH_SLEEPABLE);
708c2ecf20Sopenharmony_ci	if (IS_ERR(vah)) {
718c2ecf20Sopenharmony_ci		kfree(ah);
728c2ecf20Sopenharmony_ci		ah = (struct ipoib_ah *)vah;
738c2ecf20Sopenharmony_ci	} else {
748c2ecf20Sopenharmony_ci		ah->ah = vah;
758c2ecf20Sopenharmony_ci		ipoib_dbg(ipoib_priv(dev), "Created ah %p\n", ah->ah);
768c2ecf20Sopenharmony_ci	}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	return ah;
798c2ecf20Sopenharmony_ci}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_civoid ipoib_free_ah(struct kref *kref)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	struct ipoib_ah *ah = container_of(kref, struct ipoib_ah, ref);
848c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv = ipoib_priv(ah->dev);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	unsigned long flags;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
898c2ecf20Sopenharmony_ci	list_add_tail(&ah->list, &priv->dead_ahs);
908c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic void ipoib_ud_dma_unmap_rx(struct ipoib_dev_priv *priv,
948c2ecf20Sopenharmony_ci				  u64 mapping[IPOIB_UD_RX_SG])
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	ib_dma_unmap_single(priv->ca, mapping[0],
978c2ecf20Sopenharmony_ci			    IPOIB_UD_BUF_SIZE(priv->max_ib_mtu),
988c2ecf20Sopenharmony_ci			    DMA_FROM_DEVICE);
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistatic int ipoib_ib_post_receive(struct net_device *dev, int id)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1048c2ecf20Sopenharmony_ci	int ret;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	priv->rx_wr.wr_id   = id | IPOIB_OP_RECV;
1078c2ecf20Sopenharmony_ci	priv->rx_sge[0].addr = priv->rx_ring[id].mapping[0];
1088c2ecf20Sopenharmony_ci	priv->rx_sge[1].addr = priv->rx_ring[id].mapping[1];
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	ret = ib_post_recv(priv->qp, &priv->rx_wr, NULL);
1128c2ecf20Sopenharmony_ci	if (unlikely(ret)) {
1138c2ecf20Sopenharmony_ci		ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret);
1148c2ecf20Sopenharmony_ci		ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[id].mapping);
1158c2ecf20Sopenharmony_ci		dev_kfree_skb_any(priv->rx_ring[id].skb);
1168c2ecf20Sopenharmony_ci		priv->rx_ring[id].skb = NULL;
1178c2ecf20Sopenharmony_ci	}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	return ret;
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic struct sk_buff *ipoib_alloc_rx_skb(struct net_device *dev, int id)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1258c2ecf20Sopenharmony_ci	struct sk_buff *skb;
1268c2ecf20Sopenharmony_ci	int buf_size;
1278c2ecf20Sopenharmony_ci	u64 *mapping;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	buf_size = IPOIB_UD_BUF_SIZE(priv->max_ib_mtu);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	skb = dev_alloc_skb(buf_size + IPOIB_HARD_LEN);
1328c2ecf20Sopenharmony_ci	if (unlikely(!skb))
1338c2ecf20Sopenharmony_ci		return NULL;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	/*
1368c2ecf20Sopenharmony_ci	 * the IP header will be at IPOIP_HARD_LEN + IB_GRH_BYTES, that is
1378c2ecf20Sopenharmony_ci	 * 64 bytes aligned
1388c2ecf20Sopenharmony_ci	 */
1398c2ecf20Sopenharmony_ci	skb_reserve(skb, sizeof(struct ipoib_pseudo_header));
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	mapping = priv->rx_ring[id].mapping;
1428c2ecf20Sopenharmony_ci	mapping[0] = ib_dma_map_single(priv->ca, skb->data, buf_size,
1438c2ecf20Sopenharmony_ci				       DMA_FROM_DEVICE);
1448c2ecf20Sopenharmony_ci	if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0])))
1458c2ecf20Sopenharmony_ci		goto error;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	priv->rx_ring[id].skb = skb;
1488c2ecf20Sopenharmony_ci	return skb;
1498c2ecf20Sopenharmony_cierror:
1508c2ecf20Sopenharmony_ci	dev_kfree_skb_any(skb);
1518c2ecf20Sopenharmony_ci	return NULL;
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic int ipoib_ib_post_receives(struct net_device *dev)
1558c2ecf20Sopenharmony_ci{
1568c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1578c2ecf20Sopenharmony_ci	int i;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	for (i = 0; i < ipoib_recvq_size; ++i) {
1608c2ecf20Sopenharmony_ci		if (!ipoib_alloc_rx_skb(dev, i)) {
1618c2ecf20Sopenharmony_ci			ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
1628c2ecf20Sopenharmony_ci			return -ENOMEM;
1638c2ecf20Sopenharmony_ci		}
1648c2ecf20Sopenharmony_ci		if (ipoib_ib_post_receive(dev, i)) {
1658c2ecf20Sopenharmony_ci			ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
1668c2ecf20Sopenharmony_ci			return -EIO;
1678c2ecf20Sopenharmony_ci		}
1688c2ecf20Sopenharmony_ci	}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	return 0;
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistatic void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
1748c2ecf20Sopenharmony_ci{
1758c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv = ipoib_priv(dev);
1768c2ecf20Sopenharmony_ci	unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
1778c2ecf20Sopenharmony_ci	struct sk_buff *skb;
1788c2ecf20Sopenharmony_ci	u64 mapping[IPOIB_UD_RX_SG];
1798c2ecf20Sopenharmony_ci	union ib_gid *dgid;
1808c2ecf20Sopenharmony_ci	union ib_gid *sgid;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
1838c2ecf20Sopenharmony_ci		       wr_id, wc->status);
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	if (unlikely(wr_id >= ipoib_recvq_size)) {
1868c2ecf20Sopenharmony_ci		ipoib_warn(priv, "recv completion event with wrid %d (> %d)\n",
1878c2ecf20Sopenharmony_ci			   wr_id, ipoib_recvq_size);
1888c2ecf20Sopenharmony_ci		return;
1898c2ecf20Sopenharmony_ci	}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	skb  = priv->rx_ring[wr_id].skb;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	if (unlikely(wc->status != IB_WC_SUCCESS)) {
1948c2ecf20Sopenharmony_ci		if (wc->status != IB_WC_WR_FLUSH_ERR)
1958c2ecf20Sopenharmony_ci			ipoib_warn(priv,
1968c2ecf20Sopenharmony_ci				   "failed recv event (status=%d, wrid=%d vend_err %#x)\n",
1978c2ecf20Sopenharmony_ci				   wc->status, wr_id, wc->vendor_err);
1988c2ecf20Sopenharmony_ci		ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[wr_id].mapping);
1998c2ecf20Sopenharmony_ci		dev_kfree_skb_any(skb);
2008c2ecf20Sopenharmony_ci		priv->rx_ring[wr_id].skb = NULL;
2018c2ecf20Sopenharmony_ci		return;
2028c2ecf20Sopenharmony_ci	}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	memcpy(mapping, priv->rx_ring[wr_id].mapping,
2058c2ecf20Sopenharmony_ci	       IPOIB_UD_RX_SG * sizeof(*mapping));
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	/*
2088c2ecf20Sopenharmony_ci	 * If we can't allocate a new RX buffer, dump
2098c2ecf20Sopenharmony_ci	 * this packet and reuse the old buffer.
2108c2ecf20Sopenharmony_ci	 */
2118c2ecf20Sopenharmony_ci	if (unlikely(!ipoib_alloc_rx_skb(dev, wr_id))) {
2128c2ecf20Sopenharmony_ci		++dev->stats.rx_dropped;
2138c2ecf20Sopenharmony_ci		goto repost;
2148c2ecf20Sopenharmony_ci	}
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
2178c2ecf20Sopenharmony_ci		       wc->byte_len, wc->slid);
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	ipoib_ud_dma_unmap_rx(priv, mapping);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	skb_put(skb, wc->byte_len);
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	/* First byte of dgid signals multicast when 0xff */
2248c2ecf20Sopenharmony_ci	dgid = &((struct ib_grh *)skb->data)->dgid;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	if (!(wc->wc_flags & IB_WC_GRH) || dgid->raw[0] != 0xff)
2278c2ecf20Sopenharmony_ci		skb->pkt_type = PACKET_HOST;
2288c2ecf20Sopenharmony_ci	else if (memcmp(dgid, dev->broadcast + 4, sizeof(union ib_gid)) == 0)
2298c2ecf20Sopenharmony_ci		skb->pkt_type = PACKET_BROADCAST;
2308c2ecf20Sopenharmony_ci	else
2318c2ecf20Sopenharmony_ci		skb->pkt_type = PACKET_MULTICAST;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	sgid = &((struct ib_grh *)skb->data)->sgid;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	/*
2368c2ecf20Sopenharmony_ci	 * Drop packets that this interface sent, ie multicast packets
2378c2ecf20Sopenharmony_ci	 * that the HCA has replicated.
2388c2ecf20Sopenharmony_ci	 */
2398c2ecf20Sopenharmony_ci	if (wc->slid == priv->local_lid && wc->src_qp == priv->qp->qp_num) {
2408c2ecf20Sopenharmony_ci		int need_repost = 1;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci		if ((wc->wc_flags & IB_WC_GRH) &&
2438c2ecf20Sopenharmony_ci		    sgid->global.interface_id != priv->local_gid.global.interface_id)
2448c2ecf20Sopenharmony_ci			need_repost = 0;
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci		if (need_repost) {
2478c2ecf20Sopenharmony_ci			dev_kfree_skb_any(skb);
2488c2ecf20Sopenharmony_ci			goto repost;
2498c2ecf20Sopenharmony_ci		}
2508c2ecf20Sopenharmony_ci	}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	skb_pull(skb, IB_GRH_BYTES);
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	skb->protocol = ((struct ipoib_header *) skb->data)->proto;
2558c2ecf20Sopenharmony_ci	skb_add_pseudo_hdr(skb);
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	++dev->stats.rx_packets;
2588c2ecf20Sopenharmony_ci	dev->stats.rx_bytes += skb->len;
2598c2ecf20Sopenharmony_ci	if (skb->pkt_type == PACKET_MULTICAST)
2608c2ecf20Sopenharmony_ci		dev->stats.multicast++;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	skb->dev = dev;
2638c2ecf20Sopenharmony_ci	if ((dev->features & NETIF_F_RXCSUM) &&
2648c2ecf20Sopenharmony_ci			likely(wc->wc_flags & IB_WC_IP_CSUM_OK))
2658c2ecf20Sopenharmony_ci		skb->ip_summed = CHECKSUM_UNNECESSARY;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	napi_gro_receive(&priv->recv_napi, skb);
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_cirepost:
2708c2ecf20Sopenharmony_ci	if (unlikely(ipoib_ib_post_receive(dev, wr_id)))
2718c2ecf20Sopenharmony_ci		ipoib_warn(priv, "ipoib_ib_post_receive failed "
2728c2ecf20Sopenharmony_ci			   "for buf %d\n", wr_id);
2738c2ecf20Sopenharmony_ci}
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ciint ipoib_dma_map_tx(struct ib_device *ca, struct ipoib_tx_buf *tx_req)
2768c2ecf20Sopenharmony_ci{
2778c2ecf20Sopenharmony_ci	struct sk_buff *skb = tx_req->skb;
2788c2ecf20Sopenharmony_ci	u64 *mapping = tx_req->mapping;
2798c2ecf20Sopenharmony_ci	int i;
2808c2ecf20Sopenharmony_ci	int off;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	if (skb_headlen(skb)) {
2838c2ecf20Sopenharmony_ci		mapping[0] = ib_dma_map_single(ca, skb->data, skb_headlen(skb),
2848c2ecf20Sopenharmony_ci					       DMA_TO_DEVICE);
2858c2ecf20Sopenharmony_ci		if (unlikely(ib_dma_mapping_error(ca, mapping[0])))
2868c2ecf20Sopenharmony_ci			return -EIO;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci		off = 1;
2898c2ecf20Sopenharmony_ci	} else
2908c2ecf20Sopenharmony_ci		off = 0;
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
2938c2ecf20Sopenharmony_ci		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2948c2ecf20Sopenharmony_ci		mapping[i + off] = ib_dma_map_page(ca,
2958c2ecf20Sopenharmony_ci						 skb_frag_page(frag),
2968c2ecf20Sopenharmony_ci						 skb_frag_off(frag),
2978c2ecf20Sopenharmony_ci						 skb_frag_size(frag),
2988c2ecf20Sopenharmony_ci						 DMA_TO_DEVICE);
2998c2ecf20Sopenharmony_ci		if (unlikely(ib_dma_mapping_error(ca, mapping[i + off])))
3008c2ecf20Sopenharmony_ci			goto partial_error;
3018c2ecf20Sopenharmony_ci	}
3028c2ecf20Sopenharmony_ci	return 0;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_cipartial_error:
3058c2ecf20Sopenharmony_ci	for (; i > 0; --i) {
3068c2ecf20Sopenharmony_ci		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci		ib_dma_unmap_page(ca, mapping[i - !off], skb_frag_size(frag), DMA_TO_DEVICE);
3098c2ecf20Sopenharmony_ci	}
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	if (off)
3128c2ecf20Sopenharmony_ci		ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	return -EIO;
3158c2ecf20Sopenharmony_ci}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_civoid ipoib_dma_unmap_tx(struct ipoib_dev_priv *priv,
3188c2ecf20Sopenharmony_ci			struct ipoib_tx_buf *tx_req)
3198c2ecf20Sopenharmony_ci{
3208c2ecf20Sopenharmony_ci	struct sk_buff *skb = tx_req->skb;
3218c2ecf20Sopenharmony_ci	u64 *mapping = tx_req->mapping;
3228c2ecf20Sopenharmony_ci	int i;
3238c2ecf20Sopenharmony_ci	int off;
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	if (skb_headlen(skb)) {
3268c2ecf20Sopenharmony_ci		ib_dma_unmap_single(priv->ca, mapping[0], skb_headlen(skb),
3278c2ecf20Sopenharmony_ci				    DMA_TO_DEVICE);
3288c2ecf20Sopenharmony_ci		off = 1;
3298c2ecf20Sopenharmony_ci	} else
3308c2ecf20Sopenharmony_ci		off = 0;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
3338c2ecf20Sopenharmony_ci		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci		ib_dma_unmap_page(priv->ca, mapping[i + off],
3368c2ecf20Sopenharmony_ci				  skb_frag_size(frag), DMA_TO_DEVICE);
3378c2ecf20Sopenharmony_ci	}
3388c2ecf20Sopenharmony_ci}
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci/*
3418c2ecf20Sopenharmony_ci * As the result of a completion error the QP Can be transferred to SQE states.
3428c2ecf20Sopenharmony_ci * The function checks if the (send)QP is in SQE state and
3438c2ecf20Sopenharmony_ci * moves it back to RTS state, that in order to have it functional again.
3448c2ecf20Sopenharmony_ci */
3458c2ecf20Sopenharmony_cistatic void ipoib_qp_state_validate_work(struct work_struct *work)
3468c2ecf20Sopenharmony_ci{
3478c2ecf20Sopenharmony_ci	struct ipoib_qp_state_validate *qp_work =
3488c2ecf20Sopenharmony_ci		container_of(work, struct ipoib_qp_state_validate, work);
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv = qp_work->priv;
3518c2ecf20Sopenharmony_ci	struct ib_qp_attr qp_attr;
3528c2ecf20Sopenharmony_ci	struct ib_qp_init_attr query_init_attr;
3538c2ecf20Sopenharmony_ci	int ret;
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	ret = ib_query_qp(priv->qp, &qp_attr, IB_QP_STATE, &query_init_attr);
3568c2ecf20Sopenharmony_ci	if (ret) {
3578c2ecf20Sopenharmony_ci		ipoib_warn(priv, "%s: Failed to query QP ret: %d\n",
3588c2ecf20Sopenharmony_ci			   __func__, ret);
3598c2ecf20Sopenharmony_ci		goto free_res;
3608c2ecf20Sopenharmony_ci	}
3618c2ecf20Sopenharmony_ci	pr_info("%s: QP: 0x%x is in state: %d\n",
3628c2ecf20Sopenharmony_ci		__func__, priv->qp->qp_num, qp_attr.qp_state);
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	/* currently support only in SQE->RTS transition*/
3658c2ecf20Sopenharmony_ci	if (qp_attr.qp_state == IB_QPS_SQE) {
3668c2ecf20Sopenharmony_ci		qp_attr.qp_state = IB_QPS_RTS;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci		ret = ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE);
3698c2ecf20Sopenharmony_ci		if (ret) {
3708c2ecf20Sopenharmony_ci			pr_warn("failed(%d) modify QP:0x%x SQE->RTS\n",
3718c2ecf20Sopenharmony_ci				ret, priv->qp->qp_num);
3728c2ecf20Sopenharmony_ci			goto free_res;
3738c2ecf20Sopenharmony_ci		}
3748c2ecf20Sopenharmony_ci		pr_info("%s: QP: 0x%x moved from IB_QPS_SQE to IB_QPS_RTS\n",
3758c2ecf20Sopenharmony_ci			__func__, priv->qp->qp_num);
3768c2ecf20Sopenharmony_ci	} else {
3778c2ecf20Sopenharmony_ci		pr_warn("QP (%d) will stay in state: %d\n",
3788c2ecf20Sopenharmony_ci			priv->qp->qp_num, qp_attr.qp_state);
3798c2ecf20Sopenharmony_ci	}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_cifree_res:
3828c2ecf20Sopenharmony_ci	kfree(qp_work);
3838c2ecf20Sopenharmony_ci}
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_cistatic void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
3868c2ecf20Sopenharmony_ci{
3878c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv = ipoib_priv(dev);
3888c2ecf20Sopenharmony_ci	unsigned int wr_id = wc->wr_id;
3898c2ecf20Sopenharmony_ci	struct ipoib_tx_buf *tx_req;
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
3928c2ecf20Sopenharmony_ci		       wr_id, wc->status);
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	if (unlikely(wr_id >= ipoib_sendq_size)) {
3958c2ecf20Sopenharmony_ci		ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
3968c2ecf20Sopenharmony_ci			   wr_id, ipoib_sendq_size);
3978c2ecf20Sopenharmony_ci		return;
3988c2ecf20Sopenharmony_ci	}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	tx_req = &priv->tx_ring[wr_id];
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	ipoib_dma_unmap_tx(priv, tx_req);
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	++dev->stats.tx_packets;
4058c2ecf20Sopenharmony_ci	dev->stats.tx_bytes += tx_req->skb->len;
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	dev_kfree_skb_any(tx_req->skb);
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	++priv->tx_tail;
4108c2ecf20Sopenharmony_ci	++priv->global_tx_tail;
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	if (unlikely(netif_queue_stopped(dev) &&
4138c2ecf20Sopenharmony_ci		     ((priv->global_tx_head - priv->global_tx_tail) <=
4148c2ecf20Sopenharmony_ci		      ipoib_sendq_size >> 1) &&
4158c2ecf20Sopenharmony_ci		     test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)))
4168c2ecf20Sopenharmony_ci		netif_wake_queue(dev);
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	if (wc->status != IB_WC_SUCCESS &&
4198c2ecf20Sopenharmony_ci	    wc->status != IB_WC_WR_FLUSH_ERR) {
4208c2ecf20Sopenharmony_ci		struct ipoib_qp_state_validate *qp_work;
4218c2ecf20Sopenharmony_ci		ipoib_warn(priv,
4228c2ecf20Sopenharmony_ci			   "failed send event (status=%d, wrid=%d vend_err %#x)\n",
4238c2ecf20Sopenharmony_ci			   wc->status, wr_id, wc->vendor_err);
4248c2ecf20Sopenharmony_ci		qp_work = kzalloc(sizeof(*qp_work), GFP_ATOMIC);
4258c2ecf20Sopenharmony_ci		if (!qp_work)
4268c2ecf20Sopenharmony_ci			return;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci		INIT_WORK(&qp_work->work, ipoib_qp_state_validate_work);
4298c2ecf20Sopenharmony_ci		qp_work->priv = priv;
4308c2ecf20Sopenharmony_ci		queue_work(priv->wq, &qp_work->work);
4318c2ecf20Sopenharmony_ci	}
4328c2ecf20Sopenharmony_ci}
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_cistatic int poll_tx(struct ipoib_dev_priv *priv)
4358c2ecf20Sopenharmony_ci{
4368c2ecf20Sopenharmony_ci	int n, i;
4378c2ecf20Sopenharmony_ci	struct ib_wc *wc;
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	n = ib_poll_cq(priv->send_cq, MAX_SEND_CQE, priv->send_wc);
4408c2ecf20Sopenharmony_ci	for (i = 0; i < n; ++i) {
4418c2ecf20Sopenharmony_ci		wc = priv->send_wc + i;
4428c2ecf20Sopenharmony_ci		if (wc->wr_id & IPOIB_OP_CM)
4438c2ecf20Sopenharmony_ci			ipoib_cm_handle_tx_wc(priv->dev, priv->send_wc + i);
4448c2ecf20Sopenharmony_ci		else
4458c2ecf20Sopenharmony_ci			ipoib_ib_handle_tx_wc(priv->dev, priv->send_wc + i);
4468c2ecf20Sopenharmony_ci	}
4478c2ecf20Sopenharmony_ci	return n == MAX_SEND_CQE;
4488c2ecf20Sopenharmony_ci}
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ciint ipoib_rx_poll(struct napi_struct *napi, int budget)
4518c2ecf20Sopenharmony_ci{
4528c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv =
4538c2ecf20Sopenharmony_ci		container_of(napi, struct ipoib_dev_priv, recv_napi);
4548c2ecf20Sopenharmony_ci	struct net_device *dev = priv->dev;
4558c2ecf20Sopenharmony_ci	int done;
4568c2ecf20Sopenharmony_ci	int t;
4578c2ecf20Sopenharmony_ci	int n, i;
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	done  = 0;
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_cipoll_more:
4628c2ecf20Sopenharmony_ci	while (done < budget) {
4638c2ecf20Sopenharmony_ci		int max = (budget - done);
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci		t = min(IPOIB_NUM_WC, max);
4668c2ecf20Sopenharmony_ci		n = ib_poll_cq(priv->recv_cq, t, priv->ibwc);
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci		for (i = 0; i < n; i++) {
4698c2ecf20Sopenharmony_ci			struct ib_wc *wc = priv->ibwc + i;
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci			if (wc->wr_id & IPOIB_OP_RECV) {
4728c2ecf20Sopenharmony_ci				++done;
4738c2ecf20Sopenharmony_ci				if (wc->wr_id & IPOIB_OP_CM)
4748c2ecf20Sopenharmony_ci					ipoib_cm_handle_rx_wc(dev, wc);
4758c2ecf20Sopenharmony_ci				else
4768c2ecf20Sopenharmony_ci					ipoib_ib_handle_rx_wc(dev, wc);
4778c2ecf20Sopenharmony_ci			} else {
4788c2ecf20Sopenharmony_ci				pr_warn("%s: Got unexpected wqe id\n", __func__);
4798c2ecf20Sopenharmony_ci			}
4808c2ecf20Sopenharmony_ci		}
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci		if (n != t)
4838c2ecf20Sopenharmony_ci			break;
4848c2ecf20Sopenharmony_ci	}
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	if (done < budget) {
4878c2ecf20Sopenharmony_ci		napi_complete(napi);
4888c2ecf20Sopenharmony_ci		if (unlikely(ib_req_notify_cq(priv->recv_cq,
4898c2ecf20Sopenharmony_ci					      IB_CQ_NEXT_COMP |
4908c2ecf20Sopenharmony_ci					      IB_CQ_REPORT_MISSED_EVENTS)) &&
4918c2ecf20Sopenharmony_ci		    napi_reschedule(napi))
4928c2ecf20Sopenharmony_ci			goto poll_more;
4938c2ecf20Sopenharmony_ci	}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	return done;
4968c2ecf20Sopenharmony_ci}
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ciint ipoib_tx_poll(struct napi_struct *napi, int budget)
4998c2ecf20Sopenharmony_ci{
5008c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv = container_of(napi, struct ipoib_dev_priv,
5018c2ecf20Sopenharmony_ci						   send_napi);
5028c2ecf20Sopenharmony_ci	struct net_device *dev = priv->dev;
5038c2ecf20Sopenharmony_ci	int n, i;
5048c2ecf20Sopenharmony_ci	struct ib_wc *wc;
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_cipoll_more:
5078c2ecf20Sopenharmony_ci	n = ib_poll_cq(priv->send_cq, MAX_SEND_CQE, priv->send_wc);
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	for (i = 0; i < n; i++) {
5108c2ecf20Sopenharmony_ci		wc = priv->send_wc + i;
5118c2ecf20Sopenharmony_ci		if (wc->wr_id & IPOIB_OP_CM)
5128c2ecf20Sopenharmony_ci			ipoib_cm_handle_tx_wc(dev, wc);
5138c2ecf20Sopenharmony_ci		else
5148c2ecf20Sopenharmony_ci			ipoib_ib_handle_tx_wc(dev, wc);
5158c2ecf20Sopenharmony_ci	}
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	if (n < budget) {
5188c2ecf20Sopenharmony_ci		napi_complete(napi);
5198c2ecf20Sopenharmony_ci		if (unlikely(ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP |
5208c2ecf20Sopenharmony_ci					      IB_CQ_REPORT_MISSED_EVENTS)) &&
5218c2ecf20Sopenharmony_ci		    napi_reschedule(napi))
5228c2ecf20Sopenharmony_ci			goto poll_more;
5238c2ecf20Sopenharmony_ci	}
5248c2ecf20Sopenharmony_ci	return n < 0 ? 0 : n;
5258c2ecf20Sopenharmony_ci}
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_civoid ipoib_ib_rx_completion(struct ib_cq *cq, void *ctx_ptr)
5288c2ecf20Sopenharmony_ci{
5298c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv = ctx_ptr;
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci	napi_schedule(&priv->recv_napi);
5328c2ecf20Sopenharmony_ci}
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_civoid ipoib_ib_tx_completion(struct ib_cq *cq, void *ctx_ptr)
5358c2ecf20Sopenharmony_ci{
5368c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv = ctx_ptr;
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	napi_schedule(&priv->send_napi);
5398c2ecf20Sopenharmony_ci}
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_cistatic inline int post_send(struct ipoib_dev_priv *priv,
5428c2ecf20Sopenharmony_ci			    unsigned int wr_id,
5438c2ecf20Sopenharmony_ci			    struct ib_ah *address, u32 dqpn,
5448c2ecf20Sopenharmony_ci			    struct ipoib_tx_buf *tx_req,
5458c2ecf20Sopenharmony_ci			    void *head, int hlen)
5468c2ecf20Sopenharmony_ci{
5478c2ecf20Sopenharmony_ci	struct sk_buff *skb = tx_req->skb;
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	ipoib_build_sge(priv, tx_req);
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	priv->tx_wr.wr.wr_id	= wr_id;
5528c2ecf20Sopenharmony_ci	priv->tx_wr.remote_qpn	= dqpn;
5538c2ecf20Sopenharmony_ci	priv->tx_wr.ah		= address;
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	if (head) {
5568c2ecf20Sopenharmony_ci		priv->tx_wr.mss		= skb_shinfo(skb)->gso_size;
5578c2ecf20Sopenharmony_ci		priv->tx_wr.header	= head;
5588c2ecf20Sopenharmony_ci		priv->tx_wr.hlen	= hlen;
5598c2ecf20Sopenharmony_ci		priv->tx_wr.wr.opcode	= IB_WR_LSO;
5608c2ecf20Sopenharmony_ci	} else
5618c2ecf20Sopenharmony_ci		priv->tx_wr.wr.opcode	= IB_WR_SEND;
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	return ib_post_send(priv->qp, &priv->tx_wr.wr, NULL);
5648c2ecf20Sopenharmony_ci}
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ciint ipoib_send(struct net_device *dev, struct sk_buff *skb,
5678c2ecf20Sopenharmony_ci	       struct ib_ah *address, u32 dqpn)
5688c2ecf20Sopenharmony_ci{
5698c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv = ipoib_priv(dev);
5708c2ecf20Sopenharmony_ci	struct ipoib_tx_buf *tx_req;
5718c2ecf20Sopenharmony_ci	int hlen, rc;
5728c2ecf20Sopenharmony_ci	void *phead;
5738c2ecf20Sopenharmony_ci	unsigned int usable_sge = priv->max_send_sge - !!skb_headlen(skb);
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci	if (skb_is_gso(skb)) {
5768c2ecf20Sopenharmony_ci		hlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
5778c2ecf20Sopenharmony_ci		phead = skb->data;
5788c2ecf20Sopenharmony_ci		if (unlikely(!skb_pull(skb, hlen))) {
5798c2ecf20Sopenharmony_ci			ipoib_warn(priv, "linear data too small\n");
5808c2ecf20Sopenharmony_ci			++dev->stats.tx_dropped;
5818c2ecf20Sopenharmony_ci			++dev->stats.tx_errors;
5828c2ecf20Sopenharmony_ci			dev_kfree_skb_any(skb);
5838c2ecf20Sopenharmony_ci			return -1;
5848c2ecf20Sopenharmony_ci		}
5858c2ecf20Sopenharmony_ci	} else {
5868c2ecf20Sopenharmony_ci		if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
5878c2ecf20Sopenharmony_ci			ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
5888c2ecf20Sopenharmony_ci				   skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
5898c2ecf20Sopenharmony_ci			++dev->stats.tx_dropped;
5908c2ecf20Sopenharmony_ci			++dev->stats.tx_errors;
5918c2ecf20Sopenharmony_ci			ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
5928c2ecf20Sopenharmony_ci			return -1;
5938c2ecf20Sopenharmony_ci		}
5948c2ecf20Sopenharmony_ci		phead = NULL;
5958c2ecf20Sopenharmony_ci		hlen  = 0;
5968c2ecf20Sopenharmony_ci	}
5978c2ecf20Sopenharmony_ci	if (skb_shinfo(skb)->nr_frags > usable_sge) {
5988c2ecf20Sopenharmony_ci		if (skb_linearize(skb) < 0) {
5998c2ecf20Sopenharmony_ci			ipoib_warn(priv, "skb could not be linearized\n");
6008c2ecf20Sopenharmony_ci			++dev->stats.tx_dropped;
6018c2ecf20Sopenharmony_ci			++dev->stats.tx_errors;
6028c2ecf20Sopenharmony_ci			dev_kfree_skb_any(skb);
6038c2ecf20Sopenharmony_ci			return -1;
6048c2ecf20Sopenharmony_ci		}
6058c2ecf20Sopenharmony_ci		/* Does skb_linearize return ok without reducing nr_frags? */
6068c2ecf20Sopenharmony_ci		if (skb_shinfo(skb)->nr_frags > usable_sge) {
6078c2ecf20Sopenharmony_ci			ipoib_warn(priv, "too many frags after skb linearize\n");
6088c2ecf20Sopenharmony_ci			++dev->stats.tx_dropped;
6098c2ecf20Sopenharmony_ci			++dev->stats.tx_errors;
6108c2ecf20Sopenharmony_ci			dev_kfree_skb_any(skb);
6118c2ecf20Sopenharmony_ci			return -1;
6128c2ecf20Sopenharmony_ci		}
6138c2ecf20Sopenharmony_ci	}
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	ipoib_dbg_data(priv,
6168c2ecf20Sopenharmony_ci		       "sending packet, length=%d address=%p dqpn=0x%06x\n",
6178c2ecf20Sopenharmony_ci		       skb->len, address, dqpn);
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci	/*
6208c2ecf20Sopenharmony_ci	 * We put the skb into the tx_ring _before_ we call post_send()
6218c2ecf20Sopenharmony_ci	 * because it's entirely possible that the completion handler will
6228c2ecf20Sopenharmony_ci	 * run before we execute anything after the post_send().  That
6238c2ecf20Sopenharmony_ci	 * means we have to make sure everything is properly recorded and
6248c2ecf20Sopenharmony_ci	 * our state is consistent before we call post_send().
6258c2ecf20Sopenharmony_ci	 */
6268c2ecf20Sopenharmony_ci	tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
6278c2ecf20Sopenharmony_ci	tx_req->skb = skb;
6288c2ecf20Sopenharmony_ci	if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) {
6298c2ecf20Sopenharmony_ci		++dev->stats.tx_errors;
6308c2ecf20Sopenharmony_ci		dev_kfree_skb_any(skb);
6318c2ecf20Sopenharmony_ci		return -1;
6328c2ecf20Sopenharmony_ci	}
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	if (skb->ip_summed == CHECKSUM_PARTIAL)
6358c2ecf20Sopenharmony_ci		priv->tx_wr.wr.send_flags |= IB_SEND_IP_CSUM;
6368c2ecf20Sopenharmony_ci	else
6378c2ecf20Sopenharmony_ci		priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM;
6388c2ecf20Sopenharmony_ci	/* increase the tx_head after send success, but use it for queue state */
6398c2ecf20Sopenharmony_ci	if ((priv->global_tx_head - priv->global_tx_tail) ==
6408c2ecf20Sopenharmony_ci	    ipoib_sendq_size - 1) {
6418c2ecf20Sopenharmony_ci		ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
6428c2ecf20Sopenharmony_ci		netif_stop_queue(dev);
6438c2ecf20Sopenharmony_ci	}
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	skb_orphan(skb);
6468c2ecf20Sopenharmony_ci	skb_dst_drop(skb);
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	if (netif_queue_stopped(dev))
6498c2ecf20Sopenharmony_ci		if (ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP |
6508c2ecf20Sopenharmony_ci				     IB_CQ_REPORT_MISSED_EVENTS) < 0)
6518c2ecf20Sopenharmony_ci			ipoib_warn(priv, "request notify on send CQ failed\n");
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci	rc = post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
6548c2ecf20Sopenharmony_ci		       address, dqpn, tx_req, phead, hlen);
6558c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
6568c2ecf20Sopenharmony_ci		ipoib_warn(priv, "post_send failed, error %d\n", rc);
6578c2ecf20Sopenharmony_ci		++dev->stats.tx_errors;
6588c2ecf20Sopenharmony_ci		ipoib_dma_unmap_tx(priv, tx_req);
6598c2ecf20Sopenharmony_ci		dev_kfree_skb_any(skb);
6608c2ecf20Sopenharmony_ci		if (netif_queue_stopped(dev))
6618c2ecf20Sopenharmony_ci			netif_wake_queue(dev);
6628c2ecf20Sopenharmony_ci		rc = 0;
6638c2ecf20Sopenharmony_ci	} else {
6648c2ecf20Sopenharmony_ci		netif_trans_update(dev);
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci		rc = priv->tx_head;
6678c2ecf20Sopenharmony_ci		++priv->tx_head;
6688c2ecf20Sopenharmony_ci		++priv->global_tx_head;
6698c2ecf20Sopenharmony_ci	}
6708c2ecf20Sopenharmony_ci	return rc;
6718c2ecf20Sopenharmony_ci}
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_cistatic void ipoib_reap_dead_ahs(struct ipoib_dev_priv *priv)
6748c2ecf20Sopenharmony_ci{
6758c2ecf20Sopenharmony_ci	struct ipoib_ah *ah, *tah;
6768c2ecf20Sopenharmony_ci	unsigned long flags;
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci	netif_tx_lock_bh(priv->dev);
6798c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
6828c2ecf20Sopenharmony_ci		if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
6838c2ecf20Sopenharmony_ci			list_del(&ah->list);
6848c2ecf20Sopenharmony_ci			rdma_destroy_ah(ah->ah, 0);
6858c2ecf20Sopenharmony_ci			kfree(ah);
6868c2ecf20Sopenharmony_ci		}
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
6898c2ecf20Sopenharmony_ci	netif_tx_unlock_bh(priv->dev);
6908c2ecf20Sopenharmony_ci}
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_civoid ipoib_reap_ah(struct work_struct *work)
6938c2ecf20Sopenharmony_ci{
6948c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv =
6958c2ecf20Sopenharmony_ci		container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci	ipoib_reap_dead_ahs(priv);
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
7008c2ecf20Sopenharmony_ci		queue_delayed_work(priv->wq, &priv->ah_reap_task,
7018c2ecf20Sopenharmony_ci				   round_jiffies_relative(HZ));
7028c2ecf20Sopenharmony_ci}
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_cistatic void ipoib_start_ah_reaper(struct ipoib_dev_priv *priv)
7058c2ecf20Sopenharmony_ci{
7068c2ecf20Sopenharmony_ci	clear_bit(IPOIB_STOP_REAPER, &priv->flags);
7078c2ecf20Sopenharmony_ci	queue_delayed_work(priv->wq, &priv->ah_reap_task,
7088c2ecf20Sopenharmony_ci			   round_jiffies_relative(HZ));
7098c2ecf20Sopenharmony_ci}
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_cistatic void ipoib_stop_ah_reaper(struct ipoib_dev_priv *priv)
7128c2ecf20Sopenharmony_ci{
7138c2ecf20Sopenharmony_ci	set_bit(IPOIB_STOP_REAPER, &priv->flags);
7148c2ecf20Sopenharmony_ci	cancel_delayed_work(&priv->ah_reap_task);
7158c2ecf20Sopenharmony_ci	/*
7168c2ecf20Sopenharmony_ci	 * After ipoib_stop_ah_reaper() we always go through
7178c2ecf20Sopenharmony_ci	 * ipoib_reap_dead_ahs() which ensures the work is really stopped and
7188c2ecf20Sopenharmony_ci	 * does a final flush out of the dead_ah's list
7198c2ecf20Sopenharmony_ci	 */
7208c2ecf20Sopenharmony_ci}
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_cistatic int recvs_pending(struct net_device *dev)
7238c2ecf20Sopenharmony_ci{
7248c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv = ipoib_priv(dev);
7258c2ecf20Sopenharmony_ci	int pending = 0;
7268c2ecf20Sopenharmony_ci	int i;
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci	for (i = 0; i < ipoib_recvq_size; ++i)
7298c2ecf20Sopenharmony_ci		if (priv->rx_ring[i].skb)
7308c2ecf20Sopenharmony_ci			++pending;
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_ci	return pending;
7338c2ecf20Sopenharmony_ci}
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_cistatic void check_qp_movement_and_print(struct ipoib_dev_priv *priv,
7368c2ecf20Sopenharmony_ci					struct ib_qp *qp,
7378c2ecf20Sopenharmony_ci					enum ib_qp_state new_state)
7388c2ecf20Sopenharmony_ci{
7398c2ecf20Sopenharmony_ci	struct ib_qp_attr qp_attr;
7408c2ecf20Sopenharmony_ci	struct ib_qp_init_attr query_init_attr;
7418c2ecf20Sopenharmony_ci	int ret;
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci	ret = ib_query_qp(qp, &qp_attr, IB_QP_STATE, &query_init_attr);
7448c2ecf20Sopenharmony_ci	if (ret) {
7458c2ecf20Sopenharmony_ci		ipoib_warn(priv, "%s: Failed to query QP\n", __func__);
7468c2ecf20Sopenharmony_ci		return;
7478c2ecf20Sopenharmony_ci	}
7488c2ecf20Sopenharmony_ci	/* print according to the new-state and the previous state.*/
7498c2ecf20Sopenharmony_ci	if (new_state == IB_QPS_ERR && qp_attr.qp_state == IB_QPS_RESET)
7508c2ecf20Sopenharmony_ci		ipoib_dbg(priv, "Failed modify QP, IB_QPS_RESET to IB_QPS_ERR, acceptable\n");
7518c2ecf20Sopenharmony_ci	else
7528c2ecf20Sopenharmony_ci		ipoib_warn(priv, "Failed to modify QP to state: %d from state: %d\n",
7538c2ecf20Sopenharmony_ci			   new_state, qp_attr.qp_state);
7548c2ecf20Sopenharmony_ci}
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_cistatic void ipoib_napi_enable(struct net_device *dev)
7578c2ecf20Sopenharmony_ci{
7588c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv = ipoib_priv(dev);
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci	napi_enable(&priv->recv_napi);
7618c2ecf20Sopenharmony_ci	napi_enable(&priv->send_napi);
7628c2ecf20Sopenharmony_ci}
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_cistatic void ipoib_napi_disable(struct net_device *dev)
7658c2ecf20Sopenharmony_ci{
7668c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv = ipoib_priv(dev);
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci	napi_disable(&priv->recv_napi);
7698c2ecf20Sopenharmony_ci	napi_disable(&priv->send_napi);
7708c2ecf20Sopenharmony_ci}
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ciint ipoib_ib_dev_stop_default(struct net_device *dev)
7738c2ecf20Sopenharmony_ci{
7748c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv = ipoib_priv(dev);
7758c2ecf20Sopenharmony_ci	struct ib_qp_attr qp_attr;
7768c2ecf20Sopenharmony_ci	unsigned long begin;
7778c2ecf20Sopenharmony_ci	struct ipoib_tx_buf *tx_req;
7788c2ecf20Sopenharmony_ci	int i;
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	if (test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
7818c2ecf20Sopenharmony_ci		ipoib_napi_disable(dev);
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	ipoib_cm_dev_stop(dev);
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci	/*
7868c2ecf20Sopenharmony_ci	 * Move our QP to the error state and then reinitialize in
7878c2ecf20Sopenharmony_ci	 * when all work requests have completed or have been flushed.
7888c2ecf20Sopenharmony_ci	 */
7898c2ecf20Sopenharmony_ci	qp_attr.qp_state = IB_QPS_ERR;
7908c2ecf20Sopenharmony_ci	if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
7918c2ecf20Sopenharmony_ci		check_qp_movement_and_print(priv, priv->qp, IB_QPS_ERR);
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	/* Wait for all sends and receives to complete */
7948c2ecf20Sopenharmony_ci	begin = jiffies;
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
7978c2ecf20Sopenharmony_ci		if (time_after(jiffies, begin + 5 * HZ)) {
7988c2ecf20Sopenharmony_ci			ipoib_warn(priv,
7998c2ecf20Sopenharmony_ci				   "timing out; %d sends %d receives not completed\n",
8008c2ecf20Sopenharmony_ci				   priv->tx_head - priv->tx_tail,
8018c2ecf20Sopenharmony_ci				   recvs_pending(dev));
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci			/*
8048c2ecf20Sopenharmony_ci			 * assume the HW is wedged and just free up
8058c2ecf20Sopenharmony_ci			 * all our pending work requests.
8068c2ecf20Sopenharmony_ci			 */
8078c2ecf20Sopenharmony_ci			while ((int)priv->tx_tail - (int)priv->tx_head < 0) {
8088c2ecf20Sopenharmony_ci				tx_req = &priv->tx_ring[priv->tx_tail &
8098c2ecf20Sopenharmony_ci							(ipoib_sendq_size - 1)];
8108c2ecf20Sopenharmony_ci				ipoib_dma_unmap_tx(priv, tx_req);
8118c2ecf20Sopenharmony_ci				dev_kfree_skb_any(tx_req->skb);
8128c2ecf20Sopenharmony_ci				++priv->tx_tail;
8138c2ecf20Sopenharmony_ci				++priv->global_tx_tail;
8148c2ecf20Sopenharmony_ci			}
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci			for (i = 0; i < ipoib_recvq_size; ++i) {
8178c2ecf20Sopenharmony_ci				struct ipoib_rx_buf *rx_req;
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci				rx_req = &priv->rx_ring[i];
8208c2ecf20Sopenharmony_ci				if (!rx_req->skb)
8218c2ecf20Sopenharmony_ci					continue;
8228c2ecf20Sopenharmony_ci				ipoib_ud_dma_unmap_rx(priv,
8238c2ecf20Sopenharmony_ci						      priv->rx_ring[i].mapping);
8248c2ecf20Sopenharmony_ci				dev_kfree_skb_any(rx_req->skb);
8258c2ecf20Sopenharmony_ci				rx_req->skb = NULL;
8268c2ecf20Sopenharmony_ci			}
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_ci			goto timeout;
8298c2ecf20Sopenharmony_ci		}
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci		ipoib_drain_cq(dev);
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci		usleep_range(1000, 2000);
8348c2ecf20Sopenharmony_ci	}
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci	ipoib_dbg(priv, "All sends and receives done.\n");
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_citimeout:
8398c2ecf20Sopenharmony_ci	qp_attr.qp_state = IB_QPS_RESET;
8408c2ecf20Sopenharmony_ci	if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
8418c2ecf20Sopenharmony_ci		ipoib_warn(priv, "Failed to modify QP to RESET state\n");
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_ci	ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP);
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	return 0;
8468c2ecf20Sopenharmony_ci}
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ciint ipoib_ib_dev_open_default(struct net_device *dev)
8498c2ecf20Sopenharmony_ci{
8508c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv = ipoib_priv(dev);
8518c2ecf20Sopenharmony_ci	int ret;
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	ret = ipoib_init_qp(dev);
8548c2ecf20Sopenharmony_ci	if (ret) {
8558c2ecf20Sopenharmony_ci		ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
8568c2ecf20Sopenharmony_ci		return -1;
8578c2ecf20Sopenharmony_ci	}
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_ci	ret = ipoib_ib_post_receives(dev);
8608c2ecf20Sopenharmony_ci	if (ret) {
8618c2ecf20Sopenharmony_ci		ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
8628c2ecf20Sopenharmony_ci		goto out;
8638c2ecf20Sopenharmony_ci	}
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci	ret = ipoib_cm_dev_open(dev);
8668c2ecf20Sopenharmony_ci	if (ret) {
8678c2ecf20Sopenharmony_ci		ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
8688c2ecf20Sopenharmony_ci		goto out;
8698c2ecf20Sopenharmony_ci	}
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci	if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
8728c2ecf20Sopenharmony_ci		ipoib_napi_enable(dev);
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci	return 0;
8758c2ecf20Sopenharmony_ciout:
8768c2ecf20Sopenharmony_ci	return -1;
8778c2ecf20Sopenharmony_ci}
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ciint ipoib_ib_dev_open(struct net_device *dev)
8808c2ecf20Sopenharmony_ci{
8818c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv = ipoib_priv(dev);
8828c2ecf20Sopenharmony_ci
8838c2ecf20Sopenharmony_ci	ipoib_pkey_dev_check_presence(dev);
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
8868c2ecf20Sopenharmony_ci		ipoib_warn(priv, "P_Key 0x%04x is %s\n", priv->pkey,
8878c2ecf20Sopenharmony_ci			   (!(priv->pkey & 0x7fff) ? "Invalid" : "not found"));
8888c2ecf20Sopenharmony_ci		return -1;
8898c2ecf20Sopenharmony_ci	}
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci	ipoib_start_ah_reaper(priv);
8928c2ecf20Sopenharmony_ci	if (priv->rn_ops->ndo_open(dev)) {
8938c2ecf20Sopenharmony_ci		pr_warn("%s: Failed to open dev\n", dev->name);
8948c2ecf20Sopenharmony_ci		goto dev_stop;
8958c2ecf20Sopenharmony_ci	}
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci	set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci	return 0;
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_cidev_stop:
9028c2ecf20Sopenharmony_ci	ipoib_stop_ah_reaper(priv);
9038c2ecf20Sopenharmony_ci	return -1;
9048c2ecf20Sopenharmony_ci}
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_civoid ipoib_ib_dev_stop(struct net_device *dev)
9078c2ecf20Sopenharmony_ci{
9088c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv = ipoib_priv(dev);
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_ci	priv->rn_ops->ndo_stop(dev);
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
9138c2ecf20Sopenharmony_ci	ipoib_stop_ah_reaper(priv);
9148c2ecf20Sopenharmony_ci}
9158c2ecf20Sopenharmony_ci
9168c2ecf20Sopenharmony_civoid ipoib_pkey_dev_check_presence(struct net_device *dev)
9178c2ecf20Sopenharmony_ci{
9188c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv = ipoib_priv(dev);
9198c2ecf20Sopenharmony_ci	struct rdma_netdev *rn = netdev_priv(dev);
9208c2ecf20Sopenharmony_ci
9218c2ecf20Sopenharmony_ci	if (!(priv->pkey & 0x7fff) ||
9228c2ecf20Sopenharmony_ci	    ib_find_pkey(priv->ca, priv->port, priv->pkey,
9238c2ecf20Sopenharmony_ci			 &priv->pkey_index)) {
9248c2ecf20Sopenharmony_ci		clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
9258c2ecf20Sopenharmony_ci	} else {
9268c2ecf20Sopenharmony_ci		if (rn->set_id)
9278c2ecf20Sopenharmony_ci			rn->set_id(dev, priv->pkey_index);
9288c2ecf20Sopenharmony_ci		set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
9298c2ecf20Sopenharmony_ci	}
9308c2ecf20Sopenharmony_ci}
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_civoid ipoib_ib_dev_up(struct net_device *dev)
9338c2ecf20Sopenharmony_ci{
9348c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv = ipoib_priv(dev);
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci	ipoib_pkey_dev_check_presence(dev);
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_ci	if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
9398c2ecf20Sopenharmony_ci		ipoib_dbg(priv, "PKEY is not assigned.\n");
9408c2ecf20Sopenharmony_ci		return;
9418c2ecf20Sopenharmony_ci	}
9428c2ecf20Sopenharmony_ci
9438c2ecf20Sopenharmony_ci	set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci	ipoib_mcast_start_thread(dev);
9468c2ecf20Sopenharmony_ci}
9478c2ecf20Sopenharmony_ci
9488c2ecf20Sopenharmony_civoid ipoib_ib_dev_down(struct net_device *dev)
9498c2ecf20Sopenharmony_ci{
9508c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv = ipoib_priv(dev);
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ci	ipoib_dbg(priv, "downing ib_dev\n");
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_ci	clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
9558c2ecf20Sopenharmony_ci	netif_carrier_off(dev);
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ci	ipoib_mcast_stop_thread(dev);
9588c2ecf20Sopenharmony_ci	ipoib_mcast_dev_flush(dev);
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_ci	ipoib_flush_paths(dev);
9618c2ecf20Sopenharmony_ci}
9628c2ecf20Sopenharmony_ci
9638c2ecf20Sopenharmony_civoid ipoib_drain_cq(struct net_device *dev)
9648c2ecf20Sopenharmony_ci{
9658c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv = ipoib_priv(dev);
9668c2ecf20Sopenharmony_ci	int i, n;
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_ci	/*
9698c2ecf20Sopenharmony_ci	 * We call completion handling routines that expect to be
9708c2ecf20Sopenharmony_ci	 * called from the BH-disabled NAPI poll context, so disable
9718c2ecf20Sopenharmony_ci	 * BHs here too.
9728c2ecf20Sopenharmony_ci	 */
9738c2ecf20Sopenharmony_ci	local_bh_disable();
9748c2ecf20Sopenharmony_ci
9758c2ecf20Sopenharmony_ci	do {
9768c2ecf20Sopenharmony_ci		n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc);
9778c2ecf20Sopenharmony_ci		for (i = 0; i < n; ++i) {
9788c2ecf20Sopenharmony_ci			/*
9798c2ecf20Sopenharmony_ci			 * Convert any successful completions to flush
9808c2ecf20Sopenharmony_ci			 * errors to avoid passing packets up the
9818c2ecf20Sopenharmony_ci			 * stack after bringing the device down.
9828c2ecf20Sopenharmony_ci			 */
9838c2ecf20Sopenharmony_ci			if (priv->ibwc[i].status == IB_WC_SUCCESS)
9848c2ecf20Sopenharmony_ci				priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_ci			if (priv->ibwc[i].wr_id & IPOIB_OP_RECV) {
9878c2ecf20Sopenharmony_ci				if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
9888c2ecf20Sopenharmony_ci					ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
9898c2ecf20Sopenharmony_ci				else
9908c2ecf20Sopenharmony_ci					ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
9918c2ecf20Sopenharmony_ci			} else {
9928c2ecf20Sopenharmony_ci				pr_warn("%s: Got unexpected wqe id\n", __func__);
9938c2ecf20Sopenharmony_ci			}
9948c2ecf20Sopenharmony_ci		}
9958c2ecf20Sopenharmony_ci	} while (n == IPOIB_NUM_WC);
9968c2ecf20Sopenharmony_ci
9978c2ecf20Sopenharmony_ci	while (poll_tx(priv))
9988c2ecf20Sopenharmony_ci		; /* nothing */
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_ci	local_bh_enable();
10018c2ecf20Sopenharmony_ci}
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_ci/*
10048c2ecf20Sopenharmony_ci * Takes whatever value which is in pkey index 0 and updates priv->pkey
10058c2ecf20Sopenharmony_ci * returns 0 if the pkey value was changed.
10068c2ecf20Sopenharmony_ci */
10078c2ecf20Sopenharmony_cistatic inline int update_parent_pkey(struct ipoib_dev_priv *priv)
10088c2ecf20Sopenharmony_ci{
10098c2ecf20Sopenharmony_ci	int result;
10108c2ecf20Sopenharmony_ci	u16 prev_pkey;
10118c2ecf20Sopenharmony_ci
10128c2ecf20Sopenharmony_ci	prev_pkey = priv->pkey;
10138c2ecf20Sopenharmony_ci	result = ib_query_pkey(priv->ca, priv->port, 0, &priv->pkey);
10148c2ecf20Sopenharmony_ci	if (result) {
10158c2ecf20Sopenharmony_ci		ipoib_warn(priv, "ib_query_pkey port %d failed (ret = %d)\n",
10168c2ecf20Sopenharmony_ci			   priv->port, result);
10178c2ecf20Sopenharmony_ci		return result;
10188c2ecf20Sopenharmony_ci	}
10198c2ecf20Sopenharmony_ci
10208c2ecf20Sopenharmony_ci	priv->pkey |= 0x8000;
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci	if (prev_pkey != priv->pkey) {
10238c2ecf20Sopenharmony_ci		ipoib_dbg(priv, "pkey changed from 0x%x to 0x%x\n",
10248c2ecf20Sopenharmony_ci			  prev_pkey, priv->pkey);
10258c2ecf20Sopenharmony_ci		/*
10268c2ecf20Sopenharmony_ci		 * Update the pkey in the broadcast address, while making sure to set
10278c2ecf20Sopenharmony_ci		 * the full membership bit, so that we join the right broadcast group.
10288c2ecf20Sopenharmony_ci		 */
10298c2ecf20Sopenharmony_ci		priv->dev->broadcast[8] = priv->pkey >> 8;
10308c2ecf20Sopenharmony_ci		priv->dev->broadcast[9] = priv->pkey & 0xff;
10318c2ecf20Sopenharmony_ci		return 0;
10328c2ecf20Sopenharmony_ci	}
10338c2ecf20Sopenharmony_ci
10348c2ecf20Sopenharmony_ci	return 1;
10358c2ecf20Sopenharmony_ci}
10368c2ecf20Sopenharmony_ci/*
10378c2ecf20Sopenharmony_ci * returns 0 if pkey value was found in a different slot.
10388c2ecf20Sopenharmony_ci */
10398c2ecf20Sopenharmony_cistatic inline int update_child_pkey(struct ipoib_dev_priv *priv)
10408c2ecf20Sopenharmony_ci{
10418c2ecf20Sopenharmony_ci	u16 old_index = priv->pkey_index;
10428c2ecf20Sopenharmony_ci
10438c2ecf20Sopenharmony_ci	priv->pkey_index = 0;
10448c2ecf20Sopenharmony_ci	ipoib_pkey_dev_check_presence(priv->dev);
10458c2ecf20Sopenharmony_ci
10468c2ecf20Sopenharmony_ci	if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) &&
10478c2ecf20Sopenharmony_ci	    (old_index == priv->pkey_index))
10488c2ecf20Sopenharmony_ci		return 1;
10498c2ecf20Sopenharmony_ci	return 0;
10508c2ecf20Sopenharmony_ci}
10518c2ecf20Sopenharmony_ci
10528c2ecf20Sopenharmony_ci/*
10538c2ecf20Sopenharmony_ci * returns true if the device address of the ipoib interface has changed and the
10548c2ecf20Sopenharmony_ci * new address is a valid one (i.e in the gid table), return false otherwise.
10558c2ecf20Sopenharmony_ci */
10568c2ecf20Sopenharmony_cistatic bool ipoib_dev_addr_changed_valid(struct ipoib_dev_priv *priv)
10578c2ecf20Sopenharmony_ci{
10588c2ecf20Sopenharmony_ci	union ib_gid search_gid;
10598c2ecf20Sopenharmony_ci	union ib_gid gid0;
10608c2ecf20Sopenharmony_ci	union ib_gid *netdev_gid;
10618c2ecf20Sopenharmony_ci	int err;
10628c2ecf20Sopenharmony_ci	u16 index;
10638c2ecf20Sopenharmony_ci	u8 port;
10648c2ecf20Sopenharmony_ci	bool ret = false;
10658c2ecf20Sopenharmony_ci
10668c2ecf20Sopenharmony_ci	netdev_gid = (union ib_gid *)(priv->dev->dev_addr + 4);
10678c2ecf20Sopenharmony_ci	if (rdma_query_gid(priv->ca, priv->port, 0, &gid0))
10688c2ecf20Sopenharmony_ci		return false;
10698c2ecf20Sopenharmony_ci
10708c2ecf20Sopenharmony_ci	netif_addr_lock_bh(priv->dev);
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_ci	/* The subnet prefix may have changed, update it now so we won't have
10738c2ecf20Sopenharmony_ci	 * to do it later
10748c2ecf20Sopenharmony_ci	 */
10758c2ecf20Sopenharmony_ci	priv->local_gid.global.subnet_prefix = gid0.global.subnet_prefix;
10768c2ecf20Sopenharmony_ci	netdev_gid->global.subnet_prefix = gid0.global.subnet_prefix;
10778c2ecf20Sopenharmony_ci	search_gid.global.subnet_prefix = gid0.global.subnet_prefix;
10788c2ecf20Sopenharmony_ci
10798c2ecf20Sopenharmony_ci	search_gid.global.interface_id = priv->local_gid.global.interface_id;
10808c2ecf20Sopenharmony_ci
10818c2ecf20Sopenharmony_ci	netif_addr_unlock_bh(priv->dev);
10828c2ecf20Sopenharmony_ci
10838c2ecf20Sopenharmony_ci	err = ib_find_gid(priv->ca, &search_gid, &port, &index);
10848c2ecf20Sopenharmony_ci
10858c2ecf20Sopenharmony_ci	netif_addr_lock_bh(priv->dev);
10868c2ecf20Sopenharmony_ci
10878c2ecf20Sopenharmony_ci	if (search_gid.global.interface_id !=
10888c2ecf20Sopenharmony_ci	    priv->local_gid.global.interface_id)
10898c2ecf20Sopenharmony_ci		/* There was a change while we were looking up the gid, bail
10908c2ecf20Sopenharmony_ci		 * here and let the next work sort this out
10918c2ecf20Sopenharmony_ci		 */
10928c2ecf20Sopenharmony_ci		goto out;
10938c2ecf20Sopenharmony_ci
10948c2ecf20Sopenharmony_ci	/* The next section of code needs some background:
10958c2ecf20Sopenharmony_ci	 * Per IB spec the port GUID can't change if the HCA is powered on.
10968c2ecf20Sopenharmony_ci	 * port GUID is the basis for GID at index 0 which is the basis for
10978c2ecf20Sopenharmony_ci	 * the default device address of a ipoib interface.
10988c2ecf20Sopenharmony_ci	 *
10998c2ecf20Sopenharmony_ci	 * so it seems the flow should be:
11008c2ecf20Sopenharmony_ci	 * if user_changed_dev_addr && gid in gid tbl
11018c2ecf20Sopenharmony_ci	 *	set bit dev_addr_set
11028c2ecf20Sopenharmony_ci	 *	return true
11038c2ecf20Sopenharmony_ci	 * else
11048c2ecf20Sopenharmony_ci	 *	return false
11058c2ecf20Sopenharmony_ci	 *
11068c2ecf20Sopenharmony_ci	 * The issue is that there are devices that don't follow the spec,
11078c2ecf20Sopenharmony_ci	 * they change the port GUID when the HCA is powered, so in order
11088c2ecf20Sopenharmony_ci	 * not to break userspace applications, We need to check if the
11098c2ecf20Sopenharmony_ci	 * user wanted to control the device address and we assume that
11108c2ecf20Sopenharmony_ci	 * if he sets the device address back to be based on GID index 0,
11118c2ecf20Sopenharmony_ci	 * he no longer wishs to control it.
11128c2ecf20Sopenharmony_ci	 *
11138c2ecf20Sopenharmony_ci	 * If the user doesn't control the the device address,
11148c2ecf20Sopenharmony_ci	 * IPOIB_FLAG_DEV_ADDR_SET is set and ib_find_gid failed it means
11158c2ecf20Sopenharmony_ci	 * the port GUID has changed and GID at index 0 has changed
11168c2ecf20Sopenharmony_ci	 * so we need to change priv->local_gid and priv->dev->dev_addr
11178c2ecf20Sopenharmony_ci	 * to reflect the new GID.
11188c2ecf20Sopenharmony_ci	 */
11198c2ecf20Sopenharmony_ci	if (!test_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags)) {
11208c2ecf20Sopenharmony_ci		if (!err && port == priv->port) {
11218c2ecf20Sopenharmony_ci			set_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags);
11228c2ecf20Sopenharmony_ci			if (index == 0)
11238c2ecf20Sopenharmony_ci				clear_bit(IPOIB_FLAG_DEV_ADDR_CTRL,
11248c2ecf20Sopenharmony_ci					  &priv->flags);
11258c2ecf20Sopenharmony_ci			else
11268c2ecf20Sopenharmony_ci				set_bit(IPOIB_FLAG_DEV_ADDR_CTRL, &priv->flags);
11278c2ecf20Sopenharmony_ci			ret = true;
11288c2ecf20Sopenharmony_ci		} else {
11298c2ecf20Sopenharmony_ci			ret = false;
11308c2ecf20Sopenharmony_ci		}
11318c2ecf20Sopenharmony_ci	} else {
11328c2ecf20Sopenharmony_ci		if (!err && port == priv->port) {
11338c2ecf20Sopenharmony_ci			ret = true;
11348c2ecf20Sopenharmony_ci		} else {
11358c2ecf20Sopenharmony_ci			if (!test_bit(IPOIB_FLAG_DEV_ADDR_CTRL, &priv->flags)) {
11368c2ecf20Sopenharmony_ci				memcpy(&priv->local_gid, &gid0,
11378c2ecf20Sopenharmony_ci				       sizeof(priv->local_gid));
11388c2ecf20Sopenharmony_ci				memcpy(priv->dev->dev_addr + 4, &gid0,
11398c2ecf20Sopenharmony_ci				       sizeof(priv->local_gid));
11408c2ecf20Sopenharmony_ci				ret = true;
11418c2ecf20Sopenharmony_ci			}
11428c2ecf20Sopenharmony_ci		}
11438c2ecf20Sopenharmony_ci	}
11448c2ecf20Sopenharmony_ci
11458c2ecf20Sopenharmony_ciout:
11468c2ecf20Sopenharmony_ci	netif_addr_unlock_bh(priv->dev);
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_ci	return ret;
11498c2ecf20Sopenharmony_ci}
11508c2ecf20Sopenharmony_ci
11518c2ecf20Sopenharmony_cistatic void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv,
11528c2ecf20Sopenharmony_ci				enum ipoib_flush_level level,
11538c2ecf20Sopenharmony_ci				int nesting)
11548c2ecf20Sopenharmony_ci{
11558c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *cpriv;
11568c2ecf20Sopenharmony_ci	struct net_device *dev = priv->dev;
11578c2ecf20Sopenharmony_ci	int result;
11588c2ecf20Sopenharmony_ci
11598c2ecf20Sopenharmony_ci	down_read_nested(&priv->vlan_rwsem, nesting);
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_ci	/*
11628c2ecf20Sopenharmony_ci	 * Flush any child interfaces too -- they might be up even if
11638c2ecf20Sopenharmony_ci	 * the parent is down.
11648c2ecf20Sopenharmony_ci	 */
11658c2ecf20Sopenharmony_ci	list_for_each_entry(cpriv, &priv->child_intfs, list)
11668c2ecf20Sopenharmony_ci		__ipoib_ib_dev_flush(cpriv, level, nesting + 1);
11678c2ecf20Sopenharmony_ci
11688c2ecf20Sopenharmony_ci	up_read(&priv->vlan_rwsem);
11698c2ecf20Sopenharmony_ci
11708c2ecf20Sopenharmony_ci	if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags) &&
11718c2ecf20Sopenharmony_ci	    level != IPOIB_FLUSH_HEAVY) {
11728c2ecf20Sopenharmony_ci		/* Make sure the dev_addr is set even if not flushing */
11738c2ecf20Sopenharmony_ci		if (level == IPOIB_FLUSH_LIGHT)
11748c2ecf20Sopenharmony_ci			ipoib_dev_addr_changed_valid(priv);
11758c2ecf20Sopenharmony_ci		ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
11768c2ecf20Sopenharmony_ci		return;
11778c2ecf20Sopenharmony_ci	}
11788c2ecf20Sopenharmony_ci
11798c2ecf20Sopenharmony_ci	if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
11808c2ecf20Sopenharmony_ci		/* interface is down. update pkey and leave. */
11818c2ecf20Sopenharmony_ci		if (level == IPOIB_FLUSH_HEAVY) {
11828c2ecf20Sopenharmony_ci			if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags))
11838c2ecf20Sopenharmony_ci				update_parent_pkey(priv);
11848c2ecf20Sopenharmony_ci			else
11858c2ecf20Sopenharmony_ci				update_child_pkey(priv);
11868c2ecf20Sopenharmony_ci		} else if (level == IPOIB_FLUSH_LIGHT)
11878c2ecf20Sopenharmony_ci			ipoib_dev_addr_changed_valid(priv);
11888c2ecf20Sopenharmony_ci		ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
11898c2ecf20Sopenharmony_ci		return;
11908c2ecf20Sopenharmony_ci	}
11918c2ecf20Sopenharmony_ci
11928c2ecf20Sopenharmony_ci	if (level == IPOIB_FLUSH_HEAVY) {
11938c2ecf20Sopenharmony_ci		/* child devices chase their origin pkey value, while non-child
11948c2ecf20Sopenharmony_ci		 * (parent) devices should always takes what present in pkey index 0
11958c2ecf20Sopenharmony_ci		 */
11968c2ecf20Sopenharmony_ci		if (test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
11978c2ecf20Sopenharmony_ci			result = update_child_pkey(priv);
11988c2ecf20Sopenharmony_ci			if (result) {
11998c2ecf20Sopenharmony_ci				/* restart QP only if P_Key index is changed */
12008c2ecf20Sopenharmony_ci				ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
12018c2ecf20Sopenharmony_ci				return;
12028c2ecf20Sopenharmony_ci			}
12038c2ecf20Sopenharmony_ci
12048c2ecf20Sopenharmony_ci		} else {
12058c2ecf20Sopenharmony_ci			result = update_parent_pkey(priv);
12068c2ecf20Sopenharmony_ci			/* restart QP only if P_Key value changed */
12078c2ecf20Sopenharmony_ci			if (result) {
12088c2ecf20Sopenharmony_ci				ipoib_dbg(priv, "Not flushing - P_Key value not changed.\n");
12098c2ecf20Sopenharmony_ci				return;
12108c2ecf20Sopenharmony_ci			}
12118c2ecf20Sopenharmony_ci		}
12128c2ecf20Sopenharmony_ci	}
12138c2ecf20Sopenharmony_ci
12148c2ecf20Sopenharmony_ci	if (level == IPOIB_FLUSH_LIGHT) {
12158c2ecf20Sopenharmony_ci		int oper_up;
12168c2ecf20Sopenharmony_ci		ipoib_mark_paths_invalid(dev);
12178c2ecf20Sopenharmony_ci		/* Set IPoIB operation as down to prevent races between:
12188c2ecf20Sopenharmony_ci		 * the flush flow which leaves MCG and on the fly joins
12198c2ecf20Sopenharmony_ci		 * which can happen during that time. mcast restart task
12208c2ecf20Sopenharmony_ci		 * should deal with join requests we missed.
12218c2ecf20Sopenharmony_ci		 */
12228c2ecf20Sopenharmony_ci		oper_up = test_and_clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
12238c2ecf20Sopenharmony_ci		ipoib_mcast_dev_flush(dev);
12248c2ecf20Sopenharmony_ci		if (oper_up)
12258c2ecf20Sopenharmony_ci			set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
12268c2ecf20Sopenharmony_ci		ipoib_reap_dead_ahs(priv);
12278c2ecf20Sopenharmony_ci	}
12288c2ecf20Sopenharmony_ci
12298c2ecf20Sopenharmony_ci	if (level >= IPOIB_FLUSH_NORMAL)
12308c2ecf20Sopenharmony_ci		ipoib_ib_dev_down(dev);
12318c2ecf20Sopenharmony_ci
12328c2ecf20Sopenharmony_ci	if (level == IPOIB_FLUSH_HEAVY) {
12338c2ecf20Sopenharmony_ci		if (test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))
12348c2ecf20Sopenharmony_ci			ipoib_ib_dev_stop(dev);
12358c2ecf20Sopenharmony_ci
12368c2ecf20Sopenharmony_ci		if (ipoib_ib_dev_open(dev))
12378c2ecf20Sopenharmony_ci			return;
12388c2ecf20Sopenharmony_ci
12398c2ecf20Sopenharmony_ci		if (netif_queue_stopped(dev))
12408c2ecf20Sopenharmony_ci			netif_start_queue(dev);
12418c2ecf20Sopenharmony_ci	}
12428c2ecf20Sopenharmony_ci
12438c2ecf20Sopenharmony_ci	/*
12448c2ecf20Sopenharmony_ci	 * The device could have been brought down between the start and when
12458c2ecf20Sopenharmony_ci	 * we get here, don't bring it back up if it's not configured up
12468c2ecf20Sopenharmony_ci	 */
12478c2ecf20Sopenharmony_ci	if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
12488c2ecf20Sopenharmony_ci		if (level >= IPOIB_FLUSH_NORMAL)
12498c2ecf20Sopenharmony_ci			ipoib_ib_dev_up(dev);
12508c2ecf20Sopenharmony_ci		if (ipoib_dev_addr_changed_valid(priv))
12518c2ecf20Sopenharmony_ci			ipoib_mcast_restart_task(&priv->restart_task);
12528c2ecf20Sopenharmony_ci	}
12538c2ecf20Sopenharmony_ci}
12548c2ecf20Sopenharmony_ci
12558c2ecf20Sopenharmony_civoid ipoib_ib_dev_flush_light(struct work_struct *work)
12568c2ecf20Sopenharmony_ci{
12578c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv =
12588c2ecf20Sopenharmony_ci		container_of(work, struct ipoib_dev_priv, flush_light);
12598c2ecf20Sopenharmony_ci
12608c2ecf20Sopenharmony_ci	__ipoib_ib_dev_flush(priv, IPOIB_FLUSH_LIGHT, 0);
12618c2ecf20Sopenharmony_ci}
12628c2ecf20Sopenharmony_ci
12638c2ecf20Sopenharmony_civoid ipoib_ib_dev_flush_normal(struct work_struct *work)
12648c2ecf20Sopenharmony_ci{
12658c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv =
12668c2ecf20Sopenharmony_ci		container_of(work, struct ipoib_dev_priv, flush_normal);
12678c2ecf20Sopenharmony_ci
12688c2ecf20Sopenharmony_ci	__ipoib_ib_dev_flush(priv, IPOIB_FLUSH_NORMAL, 0);
12698c2ecf20Sopenharmony_ci}
12708c2ecf20Sopenharmony_ci
12718c2ecf20Sopenharmony_civoid ipoib_ib_dev_flush_heavy(struct work_struct *work)
12728c2ecf20Sopenharmony_ci{
12738c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv =
12748c2ecf20Sopenharmony_ci		container_of(work, struct ipoib_dev_priv, flush_heavy);
12758c2ecf20Sopenharmony_ci
12768c2ecf20Sopenharmony_ci	rtnl_lock();
12778c2ecf20Sopenharmony_ci	__ipoib_ib_dev_flush(priv, IPOIB_FLUSH_HEAVY, 0);
12788c2ecf20Sopenharmony_ci	rtnl_unlock();
12798c2ecf20Sopenharmony_ci}
12808c2ecf20Sopenharmony_ci
12818c2ecf20Sopenharmony_civoid ipoib_ib_dev_cleanup(struct net_device *dev)
12828c2ecf20Sopenharmony_ci{
12838c2ecf20Sopenharmony_ci	struct ipoib_dev_priv *priv = ipoib_priv(dev);
12848c2ecf20Sopenharmony_ci
12858c2ecf20Sopenharmony_ci	ipoib_dbg(priv, "cleaning up ib_dev\n");
12868c2ecf20Sopenharmony_ci	/*
12878c2ecf20Sopenharmony_ci	 * We must make sure there are no more (path) completions
12888c2ecf20Sopenharmony_ci	 * that may wish to touch priv fields that are no longer valid
12898c2ecf20Sopenharmony_ci	 */
12908c2ecf20Sopenharmony_ci	ipoib_flush_paths(dev);
12918c2ecf20Sopenharmony_ci
12928c2ecf20Sopenharmony_ci	ipoib_mcast_stop_thread(dev);
12938c2ecf20Sopenharmony_ci	ipoib_mcast_dev_flush(dev);
12948c2ecf20Sopenharmony_ci
12958c2ecf20Sopenharmony_ci	/*
12968c2ecf20Sopenharmony_ci	 * All of our ah references aren't free until after
12978c2ecf20Sopenharmony_ci	 * ipoib_mcast_dev_flush(), ipoib_flush_paths, and
12988c2ecf20Sopenharmony_ci	 * the neighbor garbage collection is stopped and reaped.
12998c2ecf20Sopenharmony_ci	 * That should all be done now, so make a final ah flush.
13008c2ecf20Sopenharmony_ci	 */
13018c2ecf20Sopenharmony_ci	ipoib_reap_dead_ahs(priv);
13028c2ecf20Sopenharmony_ci
13038c2ecf20Sopenharmony_ci	clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
13048c2ecf20Sopenharmony_ci
13058c2ecf20Sopenharmony_ci	priv->rn_ops->ndo_uninit(dev);
13068c2ecf20Sopenharmony_ci
13078c2ecf20Sopenharmony_ci	if (priv->pd) {
13088c2ecf20Sopenharmony_ci		ib_dealloc_pd(priv->pd);
13098c2ecf20Sopenharmony_ci		priv->pd = NULL;
13108c2ecf20Sopenharmony_ci	}
13118c2ecf20Sopenharmony_ci}
13128c2ecf20Sopenharmony_ci
1313