ipoib_ib.c 21.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
3 4 5
 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
 * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
 * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * $Id: ipoib_ib.c 1386 2004-12-27 16:23:17Z roland $
 */

#include <linux/delay.h>
#include <linux/dma-mapping.h>

41
#include <rdma/ib_cache.h>
L
Linus Torvalds 已提交
42 43 44 45 46 47 48 49 50 51 52

#include "ipoib.h"

#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
static int data_debug_level;

module_param(data_debug_level, int, 0644);
MODULE_PARM_DESC(data_debug_level,
		 "Enable data path debug tracing if > 0");
#endif

53
static DEFINE_MUTEX(pkey_mutex);
L
Linus Torvalds 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

struct ipoib_ah *ipoib_create_ah(struct net_device *dev,
				 struct ib_pd *pd, struct ib_ah_attr *attr)
{
	struct ipoib_ah *ah;

	ah = kmalloc(sizeof *ah, GFP_KERNEL);
	if (!ah)
		return NULL;

	ah->dev       = dev;
	ah->last_send = 0;
	kref_init(&ah->ref);

	ah->ah = ib_create_ah(pd, attr);
	if (IS_ERR(ah->ah)) {
		kfree(ah);
		ah = NULL;
	} else
		ipoib_dbg(netdev_priv(dev), "Created ah %p\n", ah->ah);

	return ah;
}

void ipoib_free_ah(struct kref *kref)
{
	struct ipoib_ah *ah = container_of(kref, struct ipoib_ah, ref);
	struct ipoib_dev_priv *priv = netdev_priv(ah->dev);

	unsigned long flags;

85 86 87
	spin_lock_irqsave(&priv->lock, flags);
	list_add_tail(&ah->list, &priv->dead_ahs);
	spin_unlock_irqrestore(&priv->lock, flags);
L
Linus Torvalds 已提交
88 89
}

90
static int ipoib_ib_post_receive(struct net_device *dev, int id)
L
Linus Torvalds 已提交
91
{
92 93 94
	struct ipoib_dev_priv *priv = netdev_priv(dev);
	struct ib_sge list;
	struct ib_recv_wr param;
L
Linus Torvalds 已提交
95
	struct ib_recv_wr *bad_wr;
96 97 98 99 100 101 102 103 104 105 106 107 108 109
	int ret;

	list.addr     = priv->rx_ring[id].mapping;
	list.length   = IPOIB_BUF_SIZE;
	list.lkey     = priv->mr->lkey;

	param.next    = NULL;
	param.wr_id   = id | IPOIB_OP_RECV;
	param.sg_list = &list;
	param.num_sge = 1;

	ret = ib_post_recv(priv->qp, &param, &bad_wr);
	if (unlikely(ret)) {
		ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret);
110 111
		ib_dma_unmap_single(priv->ca, priv->rx_ring[id].mapping,
				    IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
112 113 114
		dev_kfree_skb_any(priv->rx_ring[id].skb);
		priv->rx_ring[id].skb = NULL;
	}
L
Linus Torvalds 已提交
115

116
	return ret;
L
Linus Torvalds 已提交
117 118
}

119
static int ipoib_alloc_rx_skb(struct net_device *dev, int id)
L
Linus Torvalds 已提交
120 121 122
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);
	struct sk_buff *skb;
123
	u64 addr;
L
Linus Torvalds 已提交
124 125

	skb = dev_alloc_skb(IPOIB_BUF_SIZE + 4);
126
	if (!skb)
L
Linus Torvalds 已提交
127
		return -ENOMEM;
128 129 130 131 132 133 134 135

	/*
	 * IB will leave a 40 byte gap for a GRH and IPoIB adds a 4 byte
	 * header.  So we need 4 more bytes to get to 48 and align the
	 * IP header to a multiple of 16.
	 */
	skb_reserve(skb, 4);

136 137 138
	addr = ib_dma_map_single(priv->ca, skb->data, IPOIB_BUF_SIZE,
				 DMA_FROM_DEVICE);
	if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
L
Linus Torvalds 已提交
139
		dev_kfree_skb_any(skb);
140
		return -EIO;
L
Linus Torvalds 已提交
141 142
	}

143 144 145 146
	priv->rx_ring[id].skb     = skb;
	priv->rx_ring[id].mapping = addr;

	return 0;
L
Linus Torvalds 已提交
147 148 149 150 151 152 153
}

static int ipoib_ib_post_receives(struct net_device *dev)
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);
	int i;

154
	for (i = 0; i < ipoib_recvq_size; ++i) {
155 156 157 158
		if (ipoib_alloc_rx_skb(dev, i)) {
			ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
			return -ENOMEM;
		}
L
Linus Torvalds 已提交
159 160 161 162 163 164 165 166 167
		if (ipoib_ib_post_receive(dev, i)) {
			ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
			return -EIO;
		}
	}

	return 0;
}

168
static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
L
Linus Torvalds 已提交
169 170
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);
171 172
	unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
	struct sk_buff *skb;
173
	u64 addr;
L
Linus Torvalds 已提交
174

175 176
	ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
		       wr_id, wc->status);
L
Linus Torvalds 已提交
177

178 179 180 181 182 183 184 185 186 187 188 189 190 191
	if (unlikely(wr_id >= ipoib_recvq_size)) {
		ipoib_warn(priv, "recv completion event with wrid %d (> %d)\n",
			   wr_id, ipoib_recvq_size);
		return;
	}

	skb  = priv->rx_ring[wr_id].skb;
	addr = priv->rx_ring[wr_id].mapping;

	if (unlikely(wc->status != IB_WC_SUCCESS)) {
		if (wc->status != IB_WC_WR_FLUSH_ERR)
			ipoib_warn(priv, "failed recv event "
				   "(status=%d, wrid=%d vend_err %x)\n",
				   wc->status, wr_id, wc->vendor_err);
192 193
		ib_dma_unmap_single(priv->ca, addr,
				    IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
194 195 196 197
		dev_kfree_skb_any(skb);
		priv->rx_ring[wr_id].skb = NULL;
		return;
	}
L
Linus Torvalds 已提交
198

199 200 201 202 203 204 205 206 207 208 209 210
	/*
	 * If we can't allocate a new RX buffer, dump
	 * this packet and reuse the old buffer.
	 */
	if (unlikely(ipoib_alloc_rx_skb(dev, wr_id))) {
		++priv->stats.rx_dropped;
		goto repost;
	}

	ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
		       wc->byte_len, wc->slid);

211
	ib_dma_unmap_single(priv->ca, addr, IPOIB_BUF_SIZE, DMA_FROM_DEVICE);
212 213 214 215 216 217 218

	skb_put(skb, wc->byte_len);
	skb_pull(skb, IB_GRH_BYTES);

	if (wc->slid != priv->local_lid ||
	    wc->src_qp != priv->qp->qp_num) {
		skb->protocol = ((struct ipoib_header *) skb->data)->proto;
219
		skb_reset_mac_header(skb);
220 221 222 223 224 225 226 227 228
		skb_pull(skb, IPOIB_ENCAP_LEN);

		dev->last_rx = jiffies;
		++priv->stats.rx_packets;
		priv->stats.rx_bytes += skb->len;

		skb->dev = dev;
		/* XXX get correct PACKET_ type here */
		skb->pkt_type = PACKET_HOST;
R
Roland Dreier 已提交
229
		netif_receive_skb(skb);
L
Linus Torvalds 已提交
230
	} else {
231 232 233
		ipoib_dbg_data(priv, "dropping loopback packet\n");
		dev_kfree_skb_any(skb);
	}
L
Linus Torvalds 已提交
234

235 236 237 238 239
repost:
	if (unlikely(ipoib_ib_post_receive(dev, wr_id)))
		ipoib_warn(priv, "ipoib_ib_post_receive failed "
			   "for buf %d\n", wr_id);
}
L
Linus Torvalds 已提交
240

241 242 243 244 245 246
static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);
	unsigned int wr_id = wc->wr_id;
	struct ipoib_tx_buf *tx_req;
	unsigned long flags;
L
Linus Torvalds 已提交
247

248 249
	ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
		       wr_id, wc->status);
L
Linus Torvalds 已提交
250

251 252 253 254
	if (unlikely(wr_id >= ipoib_sendq_size)) {
		ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
			   wr_id, ipoib_sendq_size);
		return;
L
Linus Torvalds 已提交
255
	}
256 257 258

	tx_req = &priv->tx_ring[wr_id];

259 260
	ib_dma_unmap_single(priv->ca, tx_req->mapping,
			    tx_req->skb->len, DMA_TO_DEVICE);
261 262 263 264 265 266 267 268

	++priv->stats.tx_packets;
	priv->stats.tx_bytes += tx_req->skb->len;

	dev_kfree_skb_any(tx_req->skb);

	spin_lock_irqsave(&priv->tx_lock, flags);
	++priv->tx_tail;
269 270 271
	if (unlikely(test_bit(IPOIB_FLAG_NETIF_STOPPED, &priv->flags)) &&
	    priv->tx_head - priv->tx_tail <= ipoib_sendq_size >> 1) {
		clear_bit(IPOIB_FLAG_NETIF_STOPPED, &priv->flags);
272
		netif_wake_queue(dev);
273
	}
274 275 276 277 278 279 280 281 282
	spin_unlock_irqrestore(&priv->tx_lock, flags);

	if (wc->status != IB_WC_SUCCESS &&
	    wc->status != IB_WC_WR_FLUSH_ERR)
		ipoib_warn(priv, "failed send event "
			   "(status=%d, wrid=%d vend_err %x)\n",
			   wc->status, wr_id, wc->vendor_err);
}

R
Roland Dreier 已提交
283
int ipoib_poll(struct net_device *dev, int *budget)
284
{
R
Roland Dreier 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
	struct ipoib_dev_priv *priv = netdev_priv(dev);
	int max = min(*budget, dev->quota);
	int done;
	int t;
	int empty;
	int n, i;

	done  = 0;
	empty = 0;

	while (max) {
		t = min(IPOIB_NUM_WC, max);
		n = ib_poll_cq(priv->cq, t, priv->ibwc);

		for (i = 0; i < n; ++i) {
			struct ib_wc *wc = priv->ibwc + i;

			if (wc->wr_id & IPOIB_CM_OP_SRQ) {
				++done;
				--max;
				ipoib_cm_handle_rx_wc(dev, wc);
			} else if (wc->wr_id & IPOIB_OP_RECV) {
				++done;
				--max;
				ipoib_ib_handle_rx_wc(dev, wc);
			} else
				ipoib_ib_handle_tx_wc(dev, wc);
		}

		if (n != t) {
			empty = 1;
			break;
		}
	}

	dev->quota -= done;
	*budget    -= done;

	if (empty) {
		netif_rx_complete(dev);
		if (unlikely(ib_req_notify_cq(priv->cq,
					      IB_CQ_NEXT_COMP |
					      IB_CQ_REPORT_MISSED_EVENTS)) &&
		    netif_rx_reschedule(dev, 0))
			return 1;

		return 0;
	}

	return 1;
L
Linus Torvalds 已提交
335 336 337 338
}

void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
{
R
Roland Dreier 已提交
339
	netif_rx_schedule(dev_ptr);
L
Linus Torvalds 已提交
340 341 342 343 344
}

static inline int post_send(struct ipoib_dev_priv *priv,
			    unsigned int wr_id,
			    struct ib_ah *address, u32 qpn,
345
			    u64 addr, int len)
L
Linus Torvalds 已提交
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
{
	struct ib_send_wr *bad_wr;

	priv->tx_sge.addr             = addr;
	priv->tx_sge.length           = len;

	priv->tx_wr.wr_id 	      = wr_id;
	priv->tx_wr.wr.ud.remote_qpn  = qpn;
	priv->tx_wr.wr.ud.ah 	      = address;

	return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);
}

void ipoib_send(struct net_device *dev, struct sk_buff *skb,
		struct ipoib_ah *address, u32 qpn)
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);
363
	struct ipoib_tx_buf *tx_req;
364
	u64 addr;
L
Linus Torvalds 已提交
365

366
	if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {
L
Linus Torvalds 已提交
367
		ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
368
			   skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);
L
Linus Torvalds 已提交
369 370
		++priv->stats.tx_dropped;
		++priv->stats.tx_errors;
371
		ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);
L
Linus Torvalds 已提交
372 373 374 375 376 377 378 379 380 381 382 383 384
		return;
	}

	ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
		       skb->len, address, qpn);

	/*
	 * We put the skb into the tx_ring _before_ we call post_send()
	 * because it's entirely possible that the completion handler will
	 * run before we execute anything after the post_send().  That
	 * means we have to make sure everything is properly recorded and
	 * our state is consistent before we call post_send().
	 */
385
	tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
L
Linus Torvalds 已提交
386
	tx_req->skb = skb;
387 388 389
	addr = ib_dma_map_single(priv->ca, skb->data, skb->len,
				 DMA_TO_DEVICE);
	if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
390 391 392 393
		++priv->stats.tx_errors;
		dev_kfree_skb_any(skb);
		return;
	}
394
	tx_req->mapping = addr;
L
Linus Torvalds 已提交
395

396
	if (unlikely(post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),
L
Linus Torvalds 已提交
397 398 399
			       address->ah, qpn, addr, skb->len))) {
		ipoib_warn(priv, "post_send failed\n");
		++priv->stats.tx_errors;
400
		ib_dma_unmap_single(priv->ca, addr, skb->len, DMA_TO_DEVICE);
L
Linus Torvalds 已提交
401 402 403 404 405 406 407
		dev_kfree_skb_any(skb);
	} else {
		dev->trans_start = jiffies;

		address->last_send = priv->tx_head;
		++priv->tx_head;

408
		if (priv->tx_head - priv->tx_tail == ipoib_sendq_size) {
L
Linus Torvalds 已提交
409 410
			ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
			netif_stop_queue(dev);
411
			set_bit(IPOIB_FLAG_NETIF_STOPPED, &priv->flags);
L
Linus Torvalds 已提交
412 413 414 415 416 417 418 419 420 421
		}
	}
}

static void __ipoib_reap_ah(struct net_device *dev)
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);
	struct ipoib_ah *ah, *tah;
	LIST_HEAD(remove_list);

422 423
	spin_lock_irq(&priv->tx_lock);
	spin_lock(&priv->lock);
L
Linus Torvalds 已提交
424
	list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
425
		if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
L
Linus Torvalds 已提交
426
			list_del(&ah->list);
427 428
			ib_destroy_ah(ah->ah);
			kfree(ah);
L
Linus Torvalds 已提交
429
		}
430 431
	spin_unlock(&priv->lock);
	spin_unlock_irq(&priv->tx_lock);
L
Linus Torvalds 已提交
432 433
}

D
David Howells 已提交
434
void ipoib_reap_ah(struct work_struct *work)
L
Linus Torvalds 已提交
435
{
D
David Howells 已提交
436 437 438
	struct ipoib_dev_priv *priv =
		container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
	struct net_device *dev = priv->dev;
L
Linus Torvalds 已提交
439 440 441 442 443 444 445 446 447 448 449 450

	__ipoib_reap_ah(dev);

	if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
		queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);
}

int ipoib_ib_dev_open(struct net_device *dev)
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);
	int ret;

451 452 453 454 455 456 457
	if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) {
		ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey);
		clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
		return -1;
	}
	set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);

458
	ret = ipoib_init_qp(dev);
L
Linus Torvalds 已提交
459
	if (ret) {
460
		ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
L
Linus Torvalds 已提交
461 462 463 464 465 466
		return -1;
	}

	ret = ipoib_ib_post_receives(dev);
	if (ret) {
		ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
467
		ipoib_ib_dev_stop(dev, 1);
L
Linus Torvalds 已提交
468 469 470
		return -1;
	}

471 472
	ret = ipoib_cm_dev_open(dev);
	if (ret) {
473
		ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
474
		ipoib_ib_dev_stop(dev, 1);
475 476 477
		return -1;
	}

L
Linus Torvalds 已提交
478 479 480
	clear_bit(IPOIB_STOP_REAPER, &priv->flags);
	queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);

L
Leonid Arsh 已提交
481 482
	set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);

L
Linus Torvalds 已提交
483 484 485
	return 0;
}

L
Leonid Arsh 已提交
486 487 488 489 490 491 492 493 494 495 496
static void ipoib_pkey_dev_check_presence(struct net_device *dev)
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);
	u16 pkey_index = 0;

	if (ib_find_cached_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
		clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
	else
		set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
}

L
Linus Torvalds 已提交
497 498 499 500
int ipoib_ib_dev_up(struct net_device *dev)
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);

L
Leonid Arsh 已提交
501 502 503 504 505 506 507
	ipoib_pkey_dev_check_presence(dev);

	if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
		ipoib_dbg(priv, "PKEY is not assigned.\n");
		return 0;
	}

L
Linus Torvalds 已提交
508 509 510 511 512
	set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);

	return ipoib_mcast_start_thread(dev);
}

513
int ipoib_ib_dev_down(struct net_device *dev, int flush)
L
Linus Torvalds 已提交
514 515 516 517 518 519 520 521 522 523
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);

	ipoib_dbg(priv, "downing ib_dev\n");

	clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
	netif_carrier_off(dev);

	/* Shutdown the P_Key thread if still active */
	if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
524
		mutex_lock(&pkey_mutex);
L
Linus Torvalds 已提交
525
		set_bit(IPOIB_PKEY_STOP, &priv->flags);
526
		cancel_delayed_work(&priv->pkey_poll_task);
527
		mutex_unlock(&pkey_mutex);
528 529
		if (flush)
			flush_workqueue(ipoib_workqueue);
L
Linus Torvalds 已提交
530 531
	}

532
	ipoib_mcast_stop_thread(dev, flush);
L
Linus Torvalds 已提交
533 534 535 536 537 538 539 540 541 542 543 544 545
	ipoib_mcast_dev_flush(dev);

	ipoib_flush_paths(dev);

	return 0;
}

static int recvs_pending(struct net_device *dev)
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);
	int pending = 0;
	int i;

546
	for (i = 0; i < ipoib_recvq_size; ++i)
L
Linus Torvalds 已提交
547 548 549 550 551 552
		if (priv->rx_ring[i].skb)
			++pending;

	return pending;
}

553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
void ipoib_drain_cq(struct net_device *dev)
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);
	int i, n;
	do {
		n = ib_poll_cq(priv->cq, IPOIB_NUM_WC, priv->ibwc);
		for (i = 0; i < n; ++i) {
			if (priv->ibwc[i].wr_id & IPOIB_CM_OP_SRQ)
				ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);
			else if (priv->ibwc[i].wr_id & IPOIB_OP_RECV)
				ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);
			else
				ipoib_ib_handle_tx_wc(dev, priv->ibwc + i);
		}
	} while (n == IPOIB_NUM_WC);
}

570
int ipoib_ib_dev_stop(struct net_device *dev, int flush)
L
Linus Torvalds 已提交
571 572 573 574
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);
	struct ib_qp_attr qp_attr;
	unsigned long begin;
575
	struct ipoib_tx_buf *tx_req;
576
	int i;
L
Linus Torvalds 已提交
577

L
Leonid Arsh 已提交
578
	clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
R
Roland Dreier 已提交
579
	netif_poll_disable(dev);
L
Leonid Arsh 已提交
580

581 582
	ipoib_cm_dev_stop(dev);

583 584 585 586
	/*
	 * Move our QP to the error state and then reinitialize in
	 * when all work requests have completed or have been flushed.
	 */
L
Linus Torvalds 已提交
587
	qp_attr.qp_state = IB_QPS_ERR;
588
	if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
L
Linus Torvalds 已提交
589 590 591 592 593 594 595 596 597 598 599 600 601 602
		ipoib_warn(priv, "Failed to modify QP to ERROR state\n");

	/* Wait for all sends and receives to complete */
	begin = jiffies;

	while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {
		if (time_after(jiffies, begin + 5 * HZ)) {
			ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
				   priv->tx_head - priv->tx_tail, recvs_pending(dev));

			/*
			 * assume the HW is wedged and just free up
			 * all our pending work requests.
			 */
603
			while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
L
Linus Torvalds 已提交
604
				tx_req = &priv->tx_ring[priv->tx_tail &
605
							(ipoib_sendq_size - 1)];
606 607 608 609
				ib_dma_unmap_single(priv->ca,
						    tx_req->mapping,
						    tx_req->skb->len,
						    DMA_TO_DEVICE);
L
Linus Torvalds 已提交
610 611 612 613
				dev_kfree_skb_any(tx_req->skb);
				++priv->tx_tail;
			}

614 615 616 617 618 619 620 621 622 623 624 625 626
			for (i = 0; i < ipoib_recvq_size; ++i) {
				struct ipoib_rx_buf *rx_req;

				rx_req = &priv->rx_ring[i];
				if (!rx_req->skb)
					continue;
				ib_dma_unmap_single(priv->ca,
						    rx_req->mapping,
						    IPOIB_BUF_SIZE,
						    DMA_FROM_DEVICE);
				dev_kfree_skb_any(rx_req->skb);
				rx_req->skb = NULL;
			}
L
Linus Torvalds 已提交
627 628 629 630

			goto timeout;
		}

631
		ipoib_drain_cq(dev);
R
Roland Dreier 已提交
632

L
Linus Torvalds 已提交
633 634 635 636 637 638 639
		msleep(1);
	}

	ipoib_dbg(priv, "All sends and receives done.\n");

timeout:
	qp_attr.qp_state = IB_QPS_RESET;
640
	if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
L
Linus Torvalds 已提交
641 642 643 644 645
		ipoib_warn(priv, "Failed to modify QP to RESET state\n");

	/* Wait for all AHs to be reaped */
	set_bit(IPOIB_STOP_REAPER, &priv->flags);
	cancel_delayed_work(&priv->ah_reap_task);
646 647
	if (flush)
		flush_workqueue(ipoib_workqueue);
L
Linus Torvalds 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660 661

	begin = jiffies;

	while (!list_empty(&priv->dead_ahs)) {
		__ipoib_reap_ah(dev);

		if (time_after(jiffies, begin + HZ)) {
			ipoib_warn(priv, "timing out; will leak address handles\n");
			break;
		}

		msleep(1);
	}

R
Roland Dreier 已提交
662 663 664
	netif_poll_enable(dev);
	ib_req_notify_cq(priv->cq, IB_CQ_NEXT_COMP);

L
Linus Torvalds 已提交
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
	return 0;
}

int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);

	priv->ca = ca;
	priv->port = port;
	priv->qp = NULL;

	if (ipoib_transport_dev_init(dev, ca)) {
		printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
		return -ENODEV;
	}

	if (dev->flags & IFF_UP) {
		if (ipoib_ib_dev_open(dev)) {
			ipoib_transport_dev_cleanup(dev);
			return -ENODEV;
		}
	}

	return 0;
}

691
static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv, int pkey_event)
L
Linus Torvalds 已提交
692
{
693
	struct ipoib_dev_priv *cpriv;
D
David Howells 已提交
694
	struct net_device *dev = priv->dev;
695 696 697
	u16 new_index;

	mutex_lock(&priv->vlan_mutex);
L
Linus Torvalds 已提交
698

699 700 701 702 703 704 705 706 707 708
	/*
	 * Flush any child interfaces too -- they might be up even if
	 * the parent is down.
	 */
	list_for_each_entry(cpriv, &priv->child_intfs, list)
		__ipoib_ib_dev_flush(cpriv, pkey_event);

	mutex_unlock(&priv->vlan_mutex);

	if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
L
Leonid Arsh 已提交
709 710 711 712 713 714
		ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
		return;
	}

	if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
		ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
L
Linus Torvalds 已提交
715
		return;
L
Leonid Arsh 已提交
716
	}
L
Linus Torvalds 已提交
717

718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
	if (pkey_event) {
		if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
			clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
			ipoib_ib_dev_down(dev, 0);
			ipoib_pkey_dev_delay_open(dev);
			return;
		}
		set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);

		/* restart QP only if P_Key index is changed */
		if (new_index == priv->pkey_index) {
			ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
			return;
		}
		priv->pkey_index = new_index;
	}

L
Linus Torvalds 已提交
735 736
	ipoib_dbg(priv, "flushing\n");

737
	ipoib_ib_dev_down(dev, 0);
L
Linus Torvalds 已提交
738

739 740 741 742 743
	if (pkey_event) {
		ipoib_ib_dev_stop(dev, 0);
		ipoib_ib_dev_open(dev);
	}

L
Linus Torvalds 已提交
744 745 746 747
	/*
	 * The device could have been brought down between the start and when
	 * we get here, don't bring it back up if it's not configured up
	 */
748
	if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
L
Linus Torvalds 已提交
749
		ipoib_ib_dev_up(dev);
D
David Howells 已提交
750
		ipoib_mcast_restart_task(&priv->restart_task);
751
	}
752
}
L
Linus Torvalds 已提交
753

754 755 756 757
void ipoib_ib_dev_flush(struct work_struct *work)
{
	struct ipoib_dev_priv *priv =
		container_of(work, struct ipoib_dev_priv, flush_task);
758

759 760 761
	ipoib_dbg(priv, "Flushing %s\n", priv->dev->name);
	__ipoib_ib_dev_flush(priv, 0);
}
762

763 764 765 766 767 768 769
void ipoib_pkey_event(struct work_struct *work)
{
	struct ipoib_dev_priv *priv =
		container_of(work, struct ipoib_dev_priv, pkey_event_task);

	ipoib_dbg(priv, "Flushing %s and restarting its QP\n", priv->dev->name);
	__ipoib_ib_dev_flush(priv, 1);
L
Linus Torvalds 已提交
770 771 772 773 774 775 776 777
}

void ipoib_ib_dev_cleanup(struct net_device *dev)
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);

	ipoib_dbg(priv, "cleaning up ib_dev\n");

778
	ipoib_mcast_stop_thread(dev, 1);
779
	ipoib_mcast_dev_flush(dev);
L
Linus Torvalds 已提交
780 781 782 783 784 785 786 787 788 789 790 791 792 793

	ipoib_transport_dev_cleanup(dev);
}

/*
 * Delayed P_Key Assigment Interim Support
 *
 * The following is initial implementation of delayed P_Key assigment
 * mechanism. It is using the same approach implemented for the multicast
 * group join. The single goal of this implementation is to quickly address
 * Bug #2507. This implementation will probably be removed when the P_Key
 * change async notification is available.
 */

D
David Howells 已提交
794
void ipoib_pkey_poll(struct work_struct *work)
L
Linus Torvalds 已提交
795
{
D
David Howells 已提交
796
	struct ipoib_dev_priv *priv =
797
		container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
D
David Howells 已提交
798
	struct net_device *dev = priv->dev;
L
Linus Torvalds 已提交
799 800 801 802 803 804

	ipoib_pkey_dev_check_presence(dev);

	if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
		ipoib_open(dev);
	else {
805
		mutex_lock(&pkey_mutex);
L
Linus Torvalds 已提交
806 807
		if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
			queue_delayed_work(ipoib_workqueue,
808
					   &priv->pkey_poll_task,
L
Linus Torvalds 已提交
809
					   HZ);
810
		mutex_unlock(&pkey_mutex);
L
Linus Torvalds 已提交
811 812 813 814 815 816 817 818 819 820 821 822 823
	}
}

int ipoib_pkey_dev_delay_open(struct net_device *dev)
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);

	/* Look for the interface pkey value in the IB Port P_Key table and */
	/* set the interface pkey assigment flag                            */
	ipoib_pkey_dev_check_presence(dev);

	/* P_Key value not assigned yet - start polling */
	if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
824
		mutex_lock(&pkey_mutex);
L
Linus Torvalds 已提交
825 826
		clear_bit(IPOIB_PKEY_STOP, &priv->flags);
		queue_delayed_work(ipoib_workqueue,
827
				   &priv->pkey_poll_task,
L
Linus Torvalds 已提交
828
				   HZ);
829
		mutex_unlock(&pkey_mutex);
L
Linus Torvalds 已提交
830 831 832 833 834
		return 1;
	}

	return 0;
}