xref: /freebsd/sys/ofed/drivers/infiniband/ulp/ipoib/ipoib_ib.c (revision 3e8eb5c7f4909209c042403ddee340b2ee7003a5)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3  *
4  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
5  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
6  * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
7  * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
8  *
9  * This software is available to you under a choice of one of two
10  * licenses.  You may choose to be licensed under the terms of the GNU
11  * General Public License (GPL) Version 2, available from the file
12  * COPYING in the main directory of this source tree, or the
13  * OpenIB.org BSD license below:
14  *
15  *     Redistribution and use in source and binary forms, with or
16  *     without modification, are permitted provided that the following
17  *     conditions are met:
18  *
19  *      - Redistributions of source code must retain the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer.
22  *
23  *      - Redistributions in binary form must reproduce the above
24  *        copyright notice, this list of conditions and the following
25  *        disclaimer in the documentation and/or other materials
26  *        provided with the distribution.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
32  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
33  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35  * SOFTWARE.
36  */
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #include "ipoib.h"
42 
43 #include <rdma/ib_cache.h>
44 
45 #include <security/mac/mac_framework.h>
46 
47 #include <linux/delay.h>
48 #include <linux/dma-mapping.h>
49 
50 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
51 static int data_debug_level;
52 
53 module_param(data_debug_level, int, 0644);
54 MODULE_PARM_DESC(data_debug_level,
55 		 "Enable data path debug tracing if > 0");
56 #endif
57 
58 static DEFINE_MUTEX(pkey_mutex);
59 
60 struct ipoib_ah *ipoib_create_ah(struct ipoib_dev_priv *priv,
61 				 struct ib_pd *pd, struct ib_ah_attr *attr)
62 {
63 	struct ipoib_ah *ah;
64 
65 	ah = kmalloc(sizeof *ah, GFP_KERNEL);
66 	if (!ah)
67 		return NULL;
68 
69 	ah->priv      = priv;
70 	ah->last_send = 0;
71 	kref_init(&ah->ref);
72 
73 	ah->ah = ib_create_ah(pd, attr, RDMA_CREATE_AH_SLEEPABLE);
74 	if (IS_ERR(ah->ah)) {
75 		kfree(ah);
76 		ah = NULL;
77 	} else
78 		ipoib_dbg(priv, "Created ah %p\n", ah->ah);
79 
80 	return ah;
81 }
82 
83 void ipoib_free_ah(struct kref *kref)
84 {
85 	struct ipoib_ah *ah = container_of(kref, struct ipoib_ah, ref);
86 	struct ipoib_dev_priv *priv = ah->priv;
87 
88 	unsigned long flags;
89 
90 	spin_lock_irqsave(&priv->lock, flags);
91 	list_add_tail(&ah->list, &priv->dead_ahs);
92 	spin_unlock_irqrestore(&priv->lock, flags);
93 }
94 
95 void
96 ipoib_dma_unmap_rx(struct ipoib_dev_priv *priv, struct ipoib_rx_buf *rx_req)
97 {
98 	struct mbuf *m;
99 	int i;
100 
101 	for (i = 0, m = rx_req->mb; m != NULL; m = m->m_next, i++)
102 		ib_dma_unmap_single(priv->ca, rx_req->mapping[i], m->m_len,
103 		    DMA_FROM_DEVICE);
104 }
105 
106 void
107 ipoib_dma_mb(struct ipoib_dev_priv *priv, struct mbuf *mb, unsigned int length)
108 {
109 
110 	m_adj(mb, -(mb->m_pkthdr.len - length));
111 }
112 
113 struct mbuf *
114 ipoib_alloc_map_mb(struct ipoib_dev_priv *priv, struct ipoib_rx_buf *rx_req,
115     int align, int size, int max_frags)
116 {
117 	struct mbuf *mb, *m;
118 	int i, j;
119 
120 	rx_req->mb = NULL;
121 	mb = m_getm2(NULL, align + size, M_NOWAIT, MT_DATA, M_PKTHDR);
122 	if (mb == NULL)
123 		return (NULL);
124 	for (i = 0, m = mb; m != NULL; m = m->m_next, i++) {
125 		MPASS(i < max_frags);
126 
127 		m->m_len = M_SIZE(m) - align;
128 		m->m_data += align;
129 		align = 0;
130 		mb->m_pkthdr.len += m->m_len;
131 		rx_req->mapping[i] = ib_dma_map_single(priv->ca,
132 		    mtod(m, void *), m->m_len, DMA_FROM_DEVICE);
133 		if (unlikely(ib_dma_mapping_error(priv->ca,
134 		    rx_req->mapping[i])))
135 			goto error;
136 
137 	}
138 	rx_req->mb = mb;
139 	return (mb);
140 error:
141 	for (j = 0, m = mb; j < i; m = m->m_next, j++)
142 		ib_dma_unmap_single(priv->ca, rx_req->mapping[j], m->m_len,
143 		    DMA_FROM_DEVICE);
144 	m_freem(mb);
145 	return (NULL);
146 
147 }
148 
149 static int ipoib_ib_post_receive(struct ipoib_dev_priv *priv, int id)
150 {
151 	struct ipoib_rx_buf *rx_req;
152 	const struct ib_recv_wr *bad_wr;
153 	struct mbuf *m;
154 	int ret;
155 	int i;
156 
157 	rx_req = &priv->rx_ring[id];
158 	for (m = rx_req->mb, i = 0; m != NULL; m = m->m_next, i++) {
159 		priv->rx_sge[i].addr = rx_req->mapping[i];
160 		priv->rx_sge[i].length = m->m_len;
161 	}
162 	priv->rx_wr.num_sge = i;
163 	priv->rx_wr.wr_id = id | IPOIB_OP_RECV;
164 
165 	ret = ib_post_recv(priv->qp, &priv->rx_wr, &bad_wr);
166 	if (unlikely(ret)) {
167 		ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret);
168 		ipoib_dma_unmap_rx(priv, &priv->rx_ring[id]);
169 		m_freem(priv->rx_ring[id].mb);
170 		priv->rx_ring[id].mb = NULL;
171 	}
172 
173 	return ret;
174 }
175 
176 static struct mbuf *
177 ipoib_alloc_rx_mb(struct ipoib_dev_priv *priv, int id)
178 {
179 	return ipoib_alloc_map_mb(priv, &priv->rx_ring[id],
180 	    0, priv->max_ib_mtu + IB_GRH_BYTES, IPOIB_UD_RX_SG);
181 }
182 
183 static int ipoib_ib_post_receives(struct ipoib_dev_priv *priv)
184 {
185 	int i;
186 
187 	for (i = 0; i < ipoib_recvq_size; ++i) {
188 		if (!ipoib_alloc_rx_mb(priv, i)) {
189 			ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
190 			return -ENOMEM;
191 		}
192 		if (ipoib_ib_post_receive(priv, i)) {
193 			ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
194 			return -EIO;
195 		}
196 	}
197 
198 	return 0;
199 }
200 
201 static void
202 ipoib_ib_handle_rx_wc(struct ipoib_dev_priv *priv, struct ib_wc *wc)
203 {
204 	struct ipoib_rx_buf saverx;
205 	unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
206 	struct ifnet *dev = priv->dev;
207 	struct ipoib_header *eh;
208 	struct mbuf *mb;
209 
210 	ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
211 		       wr_id, wc->status);
212 
213 	if (unlikely(wr_id >= ipoib_recvq_size)) {
214 		ipoib_warn(priv, "recv completion event with wrid %d (> %d)\n",
215 			   wr_id, ipoib_recvq_size);
216 		return;
217 	}
218 
219 	mb  = priv->rx_ring[wr_id].mb;
220 
221 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
222 		if (wc->status != IB_WC_WR_FLUSH_ERR) {
223 			ipoib_warn(priv, "failed recv event "
224 				   "(status=%d, wrid=%d vend_err %x)\n",
225 				   wc->status, wr_id, wc->vendor_err);
226 			goto repost;
227 		}
228 		if (mb) {
229 			ipoib_dma_unmap_rx(priv, &priv->rx_ring[wr_id]);
230 			m_freem(mb);
231 			priv->rx_ring[wr_id].mb = NULL;
232 		}
233 		return;
234 	}
235 
236 	/*
237 	 * Drop packets that this interface sent, ie multicast packets
238 	 * that the HCA has replicated.
239 	 */
240 	if (wc->slid == priv->local_lid && wc->src_qp == priv->qp->qp_num)
241 		goto repost;
242 
243 	memcpy(&saverx, &priv->rx_ring[wr_id], sizeof(saverx));
244 	/*
245 	 * If we can't allocate a new RX buffer, dump
246 	 * this packet and reuse the old buffer.
247 	 */
248 	if (unlikely(!ipoib_alloc_rx_mb(priv, wr_id))) {
249 		memcpy(&priv->rx_ring[wr_id], &saverx, sizeof(saverx));
250 		if_inc_counter(dev, IFCOUNTER_IQDROPS, 1);
251 		goto repost;
252 	}
253 
254 	ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
255 		       wc->byte_len, wc->slid);
256 
257 	ipoib_dma_unmap_rx(priv, &saverx);
258 	ipoib_dma_mb(priv, mb, wc->byte_len);
259 
260 	if_inc_counter(dev, IFCOUNTER_IPACKETS, 1);
261 	if_inc_counter(dev, IFCOUNTER_IBYTES, mb->m_pkthdr.len);
262 	mb->m_pkthdr.rcvif = dev;
263 	m_adj(mb, sizeof(struct ib_grh) - INFINIBAND_ALEN);
264 	eh = mtod(mb, struct ipoib_header *);
265 	bzero(eh->hwaddr, 4);	/* Zero the queue pair, only dgid is in grh */
266 
267 	if (test_bit(IPOIB_FLAG_CSUM, &priv->flags) && likely(wc->wc_flags & IB_WC_IP_CSUM_OK))
268 		mb->m_pkthdr.csum_flags = CSUM_IP_CHECKED | CSUM_IP_VALID;
269 
270 	dev->if_input(dev, mb);
271 
272 repost:
273 	if (unlikely(ipoib_ib_post_receive(priv, wr_id)))
274 		ipoib_warn(priv, "ipoib_ib_post_receive failed "
275 			   "for buf %d\n", wr_id);
276 }
277 
278 int ipoib_dma_map_tx(struct ib_device *ca, struct ipoib_tx_buf *tx_req, int max)
279 {
280 	struct mbuf *mb = tx_req->mb;
281 	u64 *mapping = tx_req->mapping;
282 	struct mbuf *m, *p;
283 	int error;
284 	int i;
285 
286 	for (m = mb, p = NULL, i = 0; m != NULL; p = m, m = m->m_next, i++) {
287 		if (m->m_len != 0)
288 			continue;
289 		if (p == NULL)
290 			panic("ipoib_dma_map_tx: First mbuf empty\n");
291 		p->m_next = m_free(m);
292 		m = p;
293 		i--;
294 	}
295 	i--;
296 	if (i >= max) {
297 		tx_req->mb = mb = m_defrag(mb, M_NOWAIT);
298 		if (mb == NULL)
299 			return -EIO;
300 		for (m = mb, i = 0; m != NULL; m = m->m_next, i++);
301 		if (i >= max)
302 			return -EIO;
303 	}
304 	error = 0;
305 	for (m = mb, i = 0; m != NULL; m = m->m_next, i++) {
306 		mapping[i] = ib_dma_map_single(ca, mtod(m, void *),
307 					       m->m_len, DMA_TO_DEVICE);
308 		if (unlikely(ib_dma_mapping_error(ca, mapping[i]))) {
309 			error = -EIO;
310 			break;
311 		}
312 	}
313 	if (error) {
314 		int end;
315 
316 		end = i;
317 		for (m = mb, i = 0; i < end; m = m->m_next, i++)
318 			ib_dma_unmap_single(ca, mapping[i], m->m_len,
319 					    DMA_TO_DEVICE);
320 	}
321 	return error;
322 }
323 
324 void ipoib_dma_unmap_tx(struct ib_device *ca, struct ipoib_tx_buf *tx_req)
325 {
326 	struct mbuf *mb = tx_req->mb;
327 	u64 *mapping = tx_req->mapping;
328 	struct mbuf *m;
329 	int i;
330 
331 	for (m = mb, i = 0; m != NULL; m = m->m_next, i++)
332 		ib_dma_unmap_single(ca, mapping[i], m->m_len, DMA_TO_DEVICE);
333 }
334 
335 static void ipoib_ib_handle_tx_wc(struct ipoib_dev_priv *priv, struct ib_wc *wc)
336 {
337 	struct ifnet *dev = priv->dev;
338 	unsigned int wr_id = wc->wr_id;
339 	struct ipoib_tx_buf *tx_req;
340 
341 	ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
342 		       wr_id, wc->status);
343 
344 	if (unlikely(wr_id >= ipoib_sendq_size)) {
345 		ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
346 			   wr_id, ipoib_sendq_size);
347 		return;
348 	}
349 
350 	tx_req = &priv->tx_ring[wr_id];
351 
352 	ipoib_dma_unmap_tx(priv->ca, tx_req);
353 
354 	if_inc_counter(dev, IFCOUNTER_OPACKETS, 1);
355 
356 	m_freem(tx_req->mb);
357 
358 	++priv->tx_tail;
359 	if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
360 	    (dev->if_drv_flags & IFF_DRV_OACTIVE) &&
361 	    test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
362 		dev->if_drv_flags &= ~IFF_DRV_OACTIVE;
363 
364 	if (wc->status != IB_WC_SUCCESS &&
365 	    wc->status != IB_WC_WR_FLUSH_ERR)
366 		ipoib_warn(priv, "failed send event "
367 			   "(status=%d, wrid=%d vend_err %x)\n",
368 			   wc->status, wr_id, wc->vendor_err);
369 }
370 
371 int
372 ipoib_poll_tx(struct ipoib_dev_priv *priv, bool do_start)
373 {
374 	int n, i;
375 
376 	n = ib_poll_cq(priv->send_cq, MAX_SEND_CQE, priv->send_wc);
377 	for (i = 0; i < n; ++i) {
378 		struct ib_wc *wc = priv->send_wc + i;
379 		if (wc->wr_id & IPOIB_OP_CM)
380 			ipoib_cm_handle_tx_wc(priv, wc);
381 		else
382 			ipoib_ib_handle_tx_wc(priv, wc);
383 	}
384 
385 	if (do_start && n != 0)
386 		ipoib_start_locked(priv->dev, priv);
387 
388 	return n == MAX_SEND_CQE;
389 }
390 
391 static void
392 ipoib_poll(struct ipoib_dev_priv *priv)
393 {
394 	int n, i;
395 
396 poll_more:
397 	spin_lock(&priv->drain_lock);
398 	for (;;) {
399 		n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc);
400 		for (i = 0; i < n; i++) {
401 			struct ib_wc *wc = priv->ibwc + i;
402 
403 			if ((wc->wr_id & IPOIB_OP_RECV) == 0)
404 				panic("ipoib_poll: Bad wr_id 0x%jX\n",
405 				    (intmax_t)wc->wr_id);
406 			if (wc->wr_id & IPOIB_OP_CM)
407 				ipoib_cm_handle_rx_wc(priv, wc);
408 			else
409 				ipoib_ib_handle_rx_wc(priv, wc);
410 		}
411 
412 		if (n != IPOIB_NUM_WC)
413 			break;
414 	}
415 	spin_unlock(&priv->drain_lock);
416 
417 	if (ib_req_notify_cq(priv->recv_cq,
418 	    IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS) > 0)
419 		goto poll_more;
420 }
421 
422 void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
423 {
424 	struct ipoib_dev_priv *priv = dev_ptr;
425 
426 	ipoib_poll(priv);
427 }
428 
429 static void drain_tx_cq(struct ipoib_dev_priv *priv)
430 {
431 	struct ifnet *dev = priv->dev;
432 
433 	spin_lock(&priv->lock);
434 	while (ipoib_poll_tx(priv, true))
435 		; /* nothing */
436 
437 	if (dev->if_drv_flags & IFF_DRV_OACTIVE)
438 		mod_timer(&priv->poll_timer, jiffies + 1);
439 
440 	spin_unlock(&priv->lock);
441 }
442 
443 void ipoib_send_comp_handler(struct ib_cq *cq, void *dev_ptr)
444 {
445 	struct ipoib_dev_priv *priv = dev_ptr;
446 
447 	mod_timer(&priv->poll_timer, jiffies);
448 }
449 
450 static inline int
451 post_send(struct ipoib_dev_priv *priv, unsigned int wr_id,
452     struct ib_ah *address, u32 qpn, struct ipoib_tx_buf *tx_req, void *head,
453     int hlen)
454 {
455 	const struct ib_send_wr *bad_wr;
456 	struct mbuf *mb = tx_req->mb;
457 	u64 *mapping = tx_req->mapping;
458 	struct mbuf *m;
459 	int i;
460 
461 	for (m = mb, i = 0; m != NULL; m = m->m_next, i++) {
462 		priv->tx_sge[i].addr         = mapping[i];
463 		priv->tx_sge[i].length       = m->m_len;
464 	}
465 	priv->tx_wr.wr.num_sge	= i;
466 	priv->tx_wr.wr.wr_id	= wr_id;
467 	priv->tx_wr.remote_qpn	= qpn;
468 	priv->tx_wr.ah		= address;
469 
470 	if (head) {
471 		priv->tx_wr.mss		= 0; /* XXX mb_shinfo(mb)->gso_size; */
472 		priv->tx_wr.header	= head;
473 		priv->tx_wr.hlen	= hlen;
474 		priv->tx_wr.wr.opcode	= IB_WR_LSO;
475 	} else
476 		priv->tx_wr.wr.opcode	= IB_WR_SEND;
477 
478 	return ib_post_send(priv->qp, &priv->tx_wr.wr, &bad_wr);
479 }
480 
481 void
482 ipoib_send(struct ipoib_dev_priv *priv, struct mbuf *mb,
483     struct ipoib_ah *address, u32 qpn)
484 {
485 	struct ifnet *dev = priv->dev;
486 	struct ipoib_tx_buf *tx_req;
487 	int hlen;
488 	void *phead;
489 
490 	if (unlikely(priv->tx_outstanding > MAX_SEND_CQE))
491 		while (ipoib_poll_tx(priv, false))
492 			; /* nothing */
493 
494 	m_adj(mb, sizeof (struct ipoib_pseudoheader));
495 	if (0 /* XXX segment offload mb_is_gso(mb) */) {
496 		/* XXX hlen = mb_transport_offset(mb) + tcp_hdrlen(mb); */
497 		phead = mtod(mb, void *);
498 		if (mb->m_len < hlen) {
499 			ipoib_warn(priv, "linear data too small\n");
500 			if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
501 			m_freem(mb);
502 			return;
503 		}
504 		m_adj(mb, hlen);
505 	} else {
506 		if (unlikely(mb->m_pkthdr.len - IPOIB_ENCAP_LEN > priv->mcast_mtu)) {
507 			ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
508 				   mb->m_pkthdr.len, priv->mcast_mtu);
509 			if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
510 			ipoib_cm_mb_too_long(priv, mb, priv->mcast_mtu);
511 			return;
512 		}
513 		phead = NULL;
514 		hlen  = 0;
515 	}
516 
517 	ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
518 		       mb->m_pkthdr.len, address, qpn);
519 
520 	/*
521 	 * We put the mb into the tx_ring _before_ we call post_send()
522 	 * because it's entirely possible that the completion handler will
523 	 * run before we execute anything after the post_send().  That
524 	 * means we have to make sure everything is properly recorded and
525 	 * our state is consistent before we call post_send().
526 	 */
527 	tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
528 	tx_req->mb = mb;
529 	if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req, IPOIB_UD_TX_SG))) {
530 		if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
531 		if (tx_req->mb)
532 			m_freem(tx_req->mb);
533 		return;
534 	}
535 
536 	if (mb->m_pkthdr.csum_flags & (CSUM_IP|CSUM_TCP|CSUM_UDP))
537 		priv->tx_wr.wr.send_flags |= IB_SEND_IP_CSUM;
538 	else
539 		priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM;
540 
541 	if (++priv->tx_outstanding == ipoib_sendq_size) {
542 		ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
543 		if (ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP))
544 			ipoib_warn(priv, "request notify on send CQ failed\n");
545 		dev->if_drv_flags |= IFF_DRV_OACTIVE;
546 	}
547 
548 	if (unlikely(post_send(priv,
549 	    priv->tx_head & (ipoib_sendq_size - 1), address->ah, qpn,
550 	    tx_req, phead, hlen))) {
551 		ipoib_warn(priv, "post_send failed\n");
552 		if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
553 		--priv->tx_outstanding;
554 		ipoib_dma_unmap_tx(priv->ca, tx_req);
555 		m_freem(mb);
556 		if (dev->if_drv_flags & IFF_DRV_OACTIVE)
557 			dev->if_drv_flags &= ~IFF_DRV_OACTIVE;
558 	} else {
559 		address->last_send = priv->tx_head;
560 		++priv->tx_head;
561 	}
562 }
563 
564 static void __ipoib_reap_ah(struct ipoib_dev_priv *priv)
565 {
566 	struct ipoib_ah *ah, *tah;
567 	LIST_HEAD(remove_list);
568 	unsigned long flags;
569 
570 	spin_lock_irqsave(&priv->lock, flags);
571 
572 	list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
573 		if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
574 			list_del(&ah->list);
575 			ib_destroy_ah(ah->ah, 0);
576 			kfree(ah);
577 		}
578 
579 	spin_unlock_irqrestore(&priv->lock, flags);
580 }
581 
582 void ipoib_reap_ah(struct work_struct *work)
583 {
584 	struct ipoib_dev_priv *priv =
585 		container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
586 
587 	__ipoib_reap_ah(priv);
588 
589 	if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
590 		queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
591 				   HZ);
592 }
593 
594 static void ipoib_ah_dev_cleanup(struct ipoib_dev_priv *priv)
595 {
596 	unsigned long begin;
597 
598 	begin = jiffies;
599 
600 	while (!list_empty(&priv->dead_ahs)) {
601 		__ipoib_reap_ah(priv);
602 
603 		if (time_after(jiffies, begin + HZ)) {
604 			ipoib_warn(priv, "timing out; will leak address handles\n");
605 			break;
606 		}
607 
608 		msleep(1);
609 	}
610 }
611 
612 static void ipoib_ib_tx_timer_func(unsigned long ctx)
613 {
614 	drain_tx_cq((struct ipoib_dev_priv *)ctx);
615 }
616 
617 int ipoib_ib_dev_open(struct ipoib_dev_priv *priv)
618 {
619 	int ret;
620 
621 	if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) {
622 		ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey);
623 		clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
624 		return -1;
625 	}
626 	set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
627 
628 	ret = ipoib_init_qp(priv);
629 	if (ret) {
630 		ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
631 		return -1;
632 	}
633 
634 	ret = ipoib_ib_post_receives(priv);
635 	if (ret) {
636 		ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
637 		ipoib_ib_dev_stop(priv, 1);
638 		return -1;
639 	}
640 
641 	ret = ipoib_cm_dev_open(priv);
642 	if (ret) {
643 		ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
644 		ipoib_ib_dev_stop(priv, 1);
645 		return -1;
646 	}
647 
648 	clear_bit(IPOIB_STOP_REAPER, &priv->flags);
649 	queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);
650 
651 	set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
652 
653 	return 0;
654 }
655 
656 static void ipoib_pkey_dev_check_presence(struct ipoib_dev_priv *priv)
657 {
658 	u16 pkey_index = 0;
659 
660 	if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
661 		clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
662 	else
663 		set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
664 }
665 
666 int ipoib_ib_dev_up(struct ipoib_dev_priv *priv)
667 {
668 
669 	ipoib_pkey_dev_check_presence(priv);
670 
671 	if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
672 		ipoib_dbg(priv, "PKEY is not assigned.\n");
673 		return 0;
674 	}
675 
676 	set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
677 
678 	return ipoib_mcast_start_thread(priv);
679 }
680 
681 int ipoib_ib_dev_down(struct ipoib_dev_priv *priv, int flush)
682 {
683 
684 	ipoib_dbg(priv, "downing ib_dev\n");
685 
686 	clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
687 	if_link_state_change(priv->dev, LINK_STATE_DOWN);
688 
689 	/* Shutdown the P_Key thread if still active */
690 	if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
691 		mutex_lock(&pkey_mutex);
692 		set_bit(IPOIB_PKEY_STOP, &priv->flags);
693 		cancel_delayed_work(&priv->pkey_poll_task);
694 		mutex_unlock(&pkey_mutex);
695 		if (flush)
696 			flush_workqueue(ipoib_workqueue);
697 	}
698 
699 	ipoib_mcast_stop_thread(priv, flush);
700 	ipoib_mcast_dev_flush(priv);
701 
702 	ipoib_flush_paths(priv);
703 
704 	return 0;
705 }
706 
707 static int recvs_pending(struct ipoib_dev_priv *priv)
708 {
709 	int pending = 0;
710 	int i;
711 
712 	for (i = 0; i < ipoib_recvq_size; ++i)
713 		if (priv->rx_ring[i].mb)
714 			++pending;
715 
716 	return pending;
717 }
718 
719 static void check_qp_movement_and_print(struct ipoib_dev_priv *priv,
720 					struct ib_qp *qp,
721 					enum ib_qp_state new_state)
722 {
723 	struct ib_qp_attr qp_attr;
724 	struct ib_qp_init_attr query_init_attr;
725 	int ret;
726 
727 	ret = ib_query_qp(qp, &qp_attr, IB_QP_STATE, &query_init_attr);
728 	if (ret) {
729 		ipoib_warn(priv, "%s: Failed to query QP (%d)\n", __func__, ret);
730 		return;
731 	}
732 
733 	/* print according to the new-state and the previous state */
734 	if (new_state == IB_QPS_ERR && qp_attr.qp_state == IB_QPS_RESET) {
735 		ipoib_dbg(priv, "Failed to modify QP %d->%d, acceptable\n",
736 			  qp_attr.qp_state, new_state);
737 	} else {
738 		ipoib_warn(priv, "Failed to modify QP %d->%d\n",
739 			   qp_attr.qp_state, new_state);
740 	}
741 }
742 
743 void ipoib_drain_cq(struct ipoib_dev_priv *priv)
744 {
745 	int i, n;
746 
747 	spin_lock(&priv->drain_lock);
748 	do {
749 		n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc);
750 		for (i = 0; i < n; ++i) {
751 			/*
752 			 * Convert any successful completions to flush
753 			 * errors to avoid passing packets up the
754 			 * stack after bringing the device down.
755 			 */
756 			if (priv->ibwc[i].status == IB_WC_SUCCESS)
757 				priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
758 
759 			if ((priv->ibwc[i].wr_id & IPOIB_OP_RECV) == 0)
760 				panic("ipoib_drain_cq:  Bad wrid 0x%jX\n",
761 				    (intmax_t)priv->ibwc[i].wr_id);
762 			if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
763 				ipoib_cm_handle_rx_wc(priv, priv->ibwc + i);
764 			else
765 				ipoib_ib_handle_rx_wc(priv, priv->ibwc + i);
766 		}
767 	} while (n == IPOIB_NUM_WC);
768 	spin_unlock(&priv->drain_lock);
769 
770 	spin_lock(&priv->lock);
771 	while (ipoib_poll_tx(priv, true))
772 		; /* nothing */
773 
774 	spin_unlock(&priv->lock);
775 }
776 
777 int ipoib_ib_dev_stop(struct ipoib_dev_priv *priv, int flush)
778 {
779 	struct ib_qp_attr qp_attr;
780 	unsigned long begin;
781 	struct ipoib_tx_buf *tx_req;
782 	int i;
783 
784 	clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
785 
786 	ipoib_cm_dev_stop(priv);
787 
788 	/*
789 	 * Move our QP to the error state and then reinitialize in
790 	 * when all work requests have completed or have been flushed.
791 	 */
792 	qp_attr.qp_state = IB_QPS_ERR;
793 	if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
794 		check_qp_movement_and_print(priv, priv->qp, IB_QPS_ERR);
795 
796 	/* Wait for all sends and receives to complete */
797 	begin = jiffies;
798 
799 	while (priv->tx_head != priv->tx_tail || recvs_pending(priv)) {
800 		if (time_after(jiffies, begin + 5 * HZ)) {
801 			ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
802 				   priv->tx_head - priv->tx_tail, recvs_pending(priv));
803 
804 			/*
805 			 * assume the HW is wedged and just free up
806 			 * all our pending work requests.
807 			 */
808 			while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
809 				tx_req = &priv->tx_ring[priv->tx_tail &
810 							(ipoib_sendq_size - 1)];
811 				ipoib_dma_unmap_tx(priv->ca, tx_req);
812 				m_freem(tx_req->mb);
813 				++priv->tx_tail;
814 				--priv->tx_outstanding;
815 			}
816 
817 			for (i = 0; i < ipoib_recvq_size; ++i) {
818 				struct ipoib_rx_buf *rx_req;
819 
820 				rx_req = &priv->rx_ring[i];
821 				if (!rx_req->mb)
822 					continue;
823 				ipoib_dma_unmap_rx(priv, &priv->rx_ring[i]);
824 				m_freem(rx_req->mb);
825 				rx_req->mb = NULL;
826 			}
827 
828 			goto timeout;
829 		}
830 
831 		ipoib_drain_cq(priv);
832 
833 		msleep(1);
834 	}
835 
836 	ipoib_dbg(priv, "All sends and receives done.\n");
837 
838 timeout:
839 	del_timer_sync(&priv->poll_timer);
840 	qp_attr.qp_state = IB_QPS_RESET;
841 	if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
842 		ipoib_warn(priv, "Failed to modify QP to RESET state\n");
843 
844 	/* Wait for all AHs to be reaped */
845 	set_bit(IPOIB_STOP_REAPER, &priv->flags);
846 	cancel_delayed_work(&priv->ah_reap_task);
847 	if (flush)
848 		flush_workqueue(ipoib_workqueue);
849 
850 	ipoib_ah_dev_cleanup(priv);
851 
852 	ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP);
853 
854 	return 0;
855 }
856 
857 int ipoib_ib_dev_init(struct ipoib_dev_priv *priv, struct ib_device *ca, int port)
858 {
859 	struct ifnet *dev = priv->dev;
860 
861 	priv->ca = ca;
862 	priv->port = port;
863 	priv->qp = NULL;
864 
865 	if (ipoib_transport_dev_init(priv, ca)) {
866 		printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
867 		return -ENODEV;
868 	}
869 
870 	setup_timer(&priv->poll_timer, ipoib_ib_tx_timer_func,
871 		    (unsigned long) priv);
872 
873 	if (dev->if_flags & IFF_UP) {
874 		if (ipoib_ib_dev_open(priv)) {
875 			ipoib_transport_dev_cleanup(priv);
876 			return -ENODEV;
877 		}
878 	}
879 
880 	return 0;
881 }
882 
883 static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv,
884 				enum ipoib_flush_level level)
885 {
886 	struct ipoib_dev_priv *cpriv;
887 	u16 new_index;
888 
889 	mutex_lock(&priv->vlan_mutex);
890 
891 	/*
892 	 * Flush any child interfaces too -- they might be up even if
893 	 * the parent is down.
894 	 */
895 	list_for_each_entry(cpriv, &priv->child_intfs, list)
896 		__ipoib_ib_dev_flush(cpriv, level);
897 
898 	mutex_unlock(&priv->vlan_mutex);
899 
900 	if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
901 		ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
902 		return;
903 	}
904 
905 	if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
906 		ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
907 		return;
908 	}
909 
910 	if (level == IPOIB_FLUSH_HEAVY) {
911 		if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
912 			clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
913 			ipoib_ib_dev_down(priv, 0);
914 			ipoib_ib_dev_stop(priv, 0);
915 			if (ipoib_pkey_dev_delay_open(priv))
916 				return;
917 		}
918 
919 		/* restart QP only if P_Key index is changed */
920 		if (test_and_set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) &&
921 		    new_index == priv->pkey_index) {
922 			ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
923 			return;
924 		}
925 		priv->pkey_index = new_index;
926 	}
927 
928 	if (level == IPOIB_FLUSH_LIGHT) {
929 		ipoib_mark_paths_invalid(priv);
930 		ipoib_mcast_dev_flush(priv);
931 	}
932 
933 	if (level >= IPOIB_FLUSH_NORMAL)
934 		ipoib_ib_dev_down(priv, 0);
935 
936 	if (level == IPOIB_FLUSH_HEAVY) {
937 		ipoib_ib_dev_stop(priv, 0);
938 		ipoib_ib_dev_open(priv);
939 	}
940 
941 	/*
942 	 * The device could have been brought down between the start and when
943 	 * we get here, don't bring it back up if it's not configured up
944 	 */
945 	if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
946 		if (level >= IPOIB_FLUSH_NORMAL)
947 			ipoib_ib_dev_up(priv);
948 		ipoib_mcast_restart_task(&priv->restart_task);
949 	}
950 }
951 
952 void ipoib_ib_dev_flush_light(struct work_struct *work)
953 {
954 	struct ipoib_dev_priv *priv =
955 		container_of(work, struct ipoib_dev_priv, flush_light);
956 
957 	__ipoib_ib_dev_flush(priv, IPOIB_FLUSH_LIGHT);
958 }
959 
960 void ipoib_ib_dev_flush_normal(struct work_struct *work)
961 {
962 	struct ipoib_dev_priv *priv =
963 		container_of(work, struct ipoib_dev_priv, flush_normal);
964 
965 	__ipoib_ib_dev_flush(priv, IPOIB_FLUSH_NORMAL);
966 }
967 
968 void ipoib_ib_dev_flush_heavy(struct work_struct *work)
969 {
970 	struct ipoib_dev_priv *priv =
971 		container_of(work, struct ipoib_dev_priv, flush_heavy);
972 
973 	__ipoib_ib_dev_flush(priv, IPOIB_FLUSH_HEAVY);
974 }
975 
976 void ipoib_ib_dev_cleanup(struct ipoib_dev_priv *priv)
977 {
978 
979 	ipoib_dbg(priv, "cleaning up ib_dev\n");
980 
981 	ipoib_mcast_stop_thread(priv, 1);
982 	ipoib_mcast_dev_flush(priv);
983 
984 	ipoib_ah_dev_cleanup(priv);
985 	ipoib_transport_dev_cleanup(priv);
986 }
987 
988 /*
989  * Delayed P_Key Assigment Interim Support
990  *
991  * The following is initial implementation of delayed P_Key assigment
992  * mechanism. It is using the same approach implemented for the multicast
993  * group join. The single goal of this implementation is to quickly address
994  * Bug #2507. This implementation will probably be removed when the P_Key
995  * change async notification is available.
996  */
997 
998 void ipoib_pkey_poll(struct work_struct *work)
999 {
1000 	struct ipoib_dev_priv *priv =
1001 		container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
1002 
1003 	ipoib_pkey_dev_check_presence(priv);
1004 
1005 	if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
1006 		ipoib_open(priv);
1007 	else {
1008 		mutex_lock(&pkey_mutex);
1009 		if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
1010 			queue_delayed_work(ipoib_workqueue,
1011 					   &priv->pkey_poll_task,
1012 					   HZ);
1013 		mutex_unlock(&pkey_mutex);
1014 	}
1015 }
1016 
1017 int ipoib_pkey_dev_delay_open(struct ipoib_dev_priv *priv)
1018 {
1019 
1020 	/* Look for the interface pkey value in the IB Port P_Key table and */
1021 	/* set the interface pkey assigment flag                            */
1022 	ipoib_pkey_dev_check_presence(priv);
1023 
1024 	/* P_Key value not assigned yet - start polling */
1025 	if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
1026 		mutex_lock(&pkey_mutex);
1027 		clear_bit(IPOIB_PKEY_STOP, &priv->flags);
1028 		queue_delayed_work(ipoib_workqueue,
1029 				   &priv->pkey_poll_task,
1030 				   HZ);
1031 		mutex_unlock(&pkey_mutex);
1032 		return 1;
1033 	}
1034 
1035 	return 0;
1036 }
1037