ipoib_cm.c revision 234183
1/*
2 * Copyright (c) 2006 Mellanox Technologies. All rights reserved
3 *
4 * This software is available to you under a choice of one of two
5 * licenses.  You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 *     Redistribution and use in source and binary forms, with or
11 *     without modification, are permitted provided that the following
12 *     conditions are met:
13 *
14 *      - Redistributions of source code must retain the above
15 *        copyright notice, this list of conditions and the following
16 *        disclaimer.
17 *
18 *      - Redistributions in binary form must reproduce the above
19 *        copyright notice, this list of conditions and the following
20 *        disclaimer in the documentation and/or other materials
21 *        provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33#include "ipoib.h"
34
35#ifdef CONFIG_INFINIBAND_IPOIB_CM
36
37#include <netinet/ip.h>
38#include <netinet/ip_icmp.h>
39#include <netinet/icmp6.h>
40
41#include <rdma/ib_cm.h>
42#include <rdma/ib_cache.h>
43#include <linux/delay.h>
44
45int ipoib_max_conn_qp = 128;
46
47module_param_named(max_nonsrq_conn_qp, ipoib_max_conn_qp, int, 0444);
48MODULE_PARM_DESC(max_nonsrq_conn_qp,
49		 "Max number of connected-mode QPs per interface "
50		 "(applied only if shared receive queue is not available)");
51
52#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
53static int data_debug_level;
54
55module_param_named(cm_data_debug_level, data_debug_level, int, 0644);
56MODULE_PARM_DESC(cm_data_debug_level,
57		 "Enable data path debug tracing for connected mode if > 0");
58#endif
59
60#define IPOIB_CM_IETF_ID 0x1000000000000000ULL
61
62#define IPOIB_CM_RX_UPDATE_TIME (256 * HZ)
63#define IPOIB_CM_RX_TIMEOUT     (2 * 256 * HZ)
64#define IPOIB_CM_RX_DELAY       (3 * 256 * HZ)
65#define IPOIB_CM_RX_UPDATE_MASK (0x3)
66
67static struct ib_qp_attr ipoib_cm_err_attr = {
68	.qp_state = IB_QPS_ERR
69};
70
71#define IPOIB_CM_RX_DRAIN_WRID 0xffffffff
72
73static struct ib_send_wr ipoib_cm_rx_drain_wr = {
74	.wr_id = IPOIB_CM_RX_DRAIN_WRID,
75	.opcode = IB_WR_SEND,
76};
77
78static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
79			       struct ib_cm_event *event);
80
81static void ipoib_cm_dma_unmap_rx(struct ipoib_dev_priv *priv, struct ipoib_cm_rx_buf *rx_req)
82{
83
84	ipoib_dma_unmap_rx(priv, (struct ipoib_rx_buf *)rx_req);
85
86}
87
88static int ipoib_cm_post_receive_srq(struct ipoib_dev_priv *priv, int id)
89{
90	struct ib_recv_wr *bad_wr;
91	struct ipoib_rx_buf *rx_req;
92	struct mbuf *m;
93	int ret;
94	int i;
95
96	rx_req = (struct ipoib_rx_buf *)&priv->cm.srq_ring[id];
97	for (m = rx_req->mb, i = 0; m != NULL; m = m->m_next, i++) {
98		priv->cm.rx_sge[i].addr = rx_req->mapping[i];
99		priv->cm.rx_sge[i].length = m->m_len;
100	}
101
102	priv->cm.rx_wr.num_sge = i;
103	priv->cm.rx_wr.wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV;
104
105	ret = ib_post_srq_recv(priv->cm.srq, &priv->cm.rx_wr, &bad_wr);
106	if (unlikely(ret)) {
107		ipoib_warn(priv, "post srq failed for buf %d (%d)\n", id, ret);
108		ipoib_dma_unmap_rx(priv, rx_req);
109		m_freem(priv->cm.srq_ring[id].mb);
110		priv->cm.srq_ring[id].mb = NULL;
111	}
112
113	return ret;
114}
115
116static int ipoib_cm_post_receive_nonsrq(struct ipoib_dev_priv *priv,
117					struct ipoib_cm_rx *rx,
118					struct ib_recv_wr *wr,
119					struct ib_sge *sge, int id)
120{
121	struct ipoib_rx_buf *rx_req;
122	struct ib_recv_wr *bad_wr;
123	struct mbuf *m;
124	int ret;
125	int i;
126
127	rx_req = (struct ipoib_rx_buf *)&rx->rx_ring[id];
128	for (m = rx_req->mb, i = 0; m != NULL; m = m->m_next, i++) {
129		sge[i].addr = rx_req->mapping[i];
130		sge[i].length = m->m_len;
131	}
132
133	wr->num_sge = i;
134	wr->wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV;
135
136	ret = ib_post_recv(rx->qp, wr, &bad_wr);
137	if (unlikely(ret)) {
138		ipoib_warn(priv, "post recv failed for buf %d (%d)\n", id, ret);
139		ipoib_dma_unmap_rx(priv, rx_req);
140		m_freem(rx->rx_ring[id].mb);
141		rx->rx_ring[id].mb = NULL;
142	}
143
144	return ret;
145}
146
147static struct mbuf *
148ipoib_cm_alloc_rx_mb(struct ipoib_dev_priv *priv, struct ipoib_cm_rx_buf *rx_req)
149{
150	return ipoib_alloc_map_mb(priv, (struct ipoib_rx_buf *)rx_req,
151	    priv->cm.max_cm_mtu);
152}
153
154static void ipoib_cm_free_rx_ring(struct ipoib_dev_priv *priv,
155				  struct ipoib_cm_rx_buf *rx_ring)
156{
157	int i;
158
159	for (i = 0; i < ipoib_recvq_size; ++i)
160		if (rx_ring[i].mb) {
161			ipoib_cm_dma_unmap_rx(priv, &rx_ring[i]);
162			m_freem(rx_ring[i].mb);
163		}
164
165	kfree(rx_ring);
166}
167
168static void ipoib_cm_start_rx_drain(struct ipoib_dev_priv *priv)
169{
170	struct ib_send_wr *bad_wr;
171	struct ipoib_cm_rx *p;
172
173	/* We only reserved 1 extra slot in CQ for drain WRs, so
174	 * make sure we have at most 1 outstanding WR. */
175	if (list_empty(&priv->cm.rx_flush_list) ||
176	    !list_empty(&priv->cm.rx_drain_list))
177		return;
178
179	/*
180	 * QPs on flush list are error state.  This way, a "flush
181	 * error" WC will be immediately generated for each WR we post.
182	 */
183	p = list_entry(priv->cm.rx_flush_list.next, typeof(*p), list);
184	if (ib_post_send(p->qp, &ipoib_cm_rx_drain_wr, &bad_wr))
185		ipoib_warn(priv, "failed to post drain wr\n");
186
187	list_splice_init(&priv->cm.rx_flush_list, &priv->cm.rx_drain_list);
188}
189
190static void ipoib_cm_rx_event_handler(struct ib_event *event, void *ctx)
191{
192	struct ipoib_cm_rx *p = ctx;
193	struct ipoib_dev_priv *priv = p->priv;
194	unsigned long flags;
195
196	if (event->event != IB_EVENT_QP_LAST_WQE_REACHED)
197		return;
198
199	spin_lock_irqsave(&priv->lock, flags);
200	list_move(&p->list, &priv->cm.rx_flush_list);
201	p->state = IPOIB_CM_RX_FLUSH;
202	ipoib_cm_start_rx_drain(priv);
203	spin_unlock_irqrestore(&priv->lock, flags);
204}
205
206static struct ib_qp *ipoib_cm_create_rx_qp(struct ipoib_dev_priv *priv,
207					   struct ipoib_cm_rx *p)
208{
209	struct ib_qp_init_attr attr = {
210		.event_handler = ipoib_cm_rx_event_handler,
211		.send_cq = priv->recv_cq, /* For drain WR */
212		.recv_cq = priv->recv_cq,
213		.srq = priv->cm.srq,
214		.cap.max_send_wr = 1, /* For drain WR */
215		.cap.max_send_sge = 1,
216		.sq_sig_type = IB_SIGNAL_ALL_WR,
217		.qp_type = IB_QPT_RC,
218		.qp_context = p,
219	};
220
221	if (!ipoib_cm_has_srq(priv)) {
222		attr.cap.max_recv_wr  = ipoib_recvq_size;
223		attr.cap.max_recv_sge = priv->cm.num_frags;
224	}
225
226	return ib_create_qp(priv->pd, &attr);
227}
228
229static int ipoib_cm_modify_rx_qp(struct ipoib_dev_priv *priv,
230				 struct ib_cm_id *cm_id, struct ib_qp *qp,
231				 unsigned psn)
232{
233	struct ib_qp_attr qp_attr;
234	int qp_attr_mask, ret;
235
236	qp_attr.qp_state = IB_QPS_INIT;
237	ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
238	if (ret) {
239		ipoib_warn(priv, "failed to init QP attr for INIT: %d\n", ret);
240		return ret;
241	}
242	ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
243	if (ret) {
244		ipoib_warn(priv, "failed to modify QP to INIT: %d\n", ret);
245		return ret;
246	}
247	qp_attr.qp_state = IB_QPS_RTR;
248	ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
249	if (ret) {
250		ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
251		return ret;
252	}
253	qp_attr.rq_psn = psn;
254	ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
255	if (ret) {
256		ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
257		return ret;
258	}
259
260	/*
261	 * Current Mellanox HCA firmware won't generate completions
262	 * with error for drain WRs unless the QP has been moved to
263	 * RTS first. This work-around leaves a window where a QP has
264	 * moved to error asynchronously, but this will eventually get
265	 * fixed in firmware, so let's not error out if modify QP
266	 * fails.
267	 */
268	qp_attr.qp_state = IB_QPS_RTS;
269	ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
270	if (ret) {
271		ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
272		return 0;
273	}
274	ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
275	if (ret) {
276		ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
277		return 0;
278	}
279
280	return 0;
281}
282
283static void ipoib_cm_init_rx_wr(struct ipoib_dev_priv *priv,
284				struct ib_recv_wr *wr,
285				struct ib_sge *sge)
286{
287	int i;
288
289	for (i = 0; i < IPOIB_CM_RX_SG; i++)
290		sge[i].lkey = priv->mr->lkey;
291
292	wr->next    = NULL;
293	wr->sg_list = sge;
294	wr->num_sge = 1;
295}
296
297static int ipoib_cm_nonsrq_init_rx(struct ipoib_dev_priv *priv,
298    struct ib_cm_id *cm_id, struct ipoib_cm_rx *rx)
299{
300	struct {
301		struct ib_recv_wr wr;
302		struct ib_sge sge[IPOIB_CM_RX_SG];
303	} *t;
304	int ret;
305	int i;
306
307	rx->rx_ring = kzalloc(ipoib_recvq_size * sizeof *rx->rx_ring, GFP_KERNEL);
308	if (!rx->rx_ring) {
309		printk(KERN_WARNING "%s: failed to allocate CM non-SRQ ring (%d entries)\n",
310		       priv->ca->name, ipoib_recvq_size);
311		return -ENOMEM;
312	}
313
314	memset(rx->rx_ring, 0, ipoib_recvq_size * sizeof *rx->rx_ring);
315
316	t = kmalloc(sizeof *t, GFP_KERNEL);
317	if (!t) {
318		ret = -ENOMEM;
319		goto err_free;
320	}
321
322	ipoib_cm_init_rx_wr(priv, &t->wr, t->sge);
323
324	spin_lock_irq(&priv->lock);
325
326	if (priv->cm.nonsrq_conn_qp >= ipoib_max_conn_qp) {
327		spin_unlock_irq(&priv->lock);
328		ib_send_cm_rej(cm_id, IB_CM_REJ_NO_QP, NULL, 0, NULL, 0);
329		ret = -EINVAL;
330		goto err_free;
331	} else
332		++priv->cm.nonsrq_conn_qp;
333
334	spin_unlock_irq(&priv->lock);
335
336	for (i = 0; i < ipoib_recvq_size; ++i) {
337		if (!ipoib_cm_alloc_rx_mb(priv, &rx->rx_ring[i])) {
338			ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
339				ret = -ENOMEM;
340				goto err_count;
341		}
342		ret = ipoib_cm_post_receive_nonsrq(priv, rx, &t->wr, t->sge, i);
343		if (ret) {
344			ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq "
345				   "failed for buf %d\n", i);
346			ret = -EIO;
347			goto err_count;
348		}
349	}
350
351	rx->recv_count = ipoib_recvq_size;
352
353	kfree(t);
354
355	return 0;
356
357err_count:
358	spin_lock_irq(&priv->lock);
359	--priv->cm.nonsrq_conn_qp;
360	spin_unlock_irq(&priv->lock);
361
362err_free:
363	kfree(t);
364	ipoib_cm_free_rx_ring(priv, rx->rx_ring);
365
366	return ret;
367}
368
369static int ipoib_cm_send_rep(struct ipoib_dev_priv *priv, struct ib_cm_id *cm_id,
370			     struct ib_qp *qp, struct ib_cm_req_event_param *req,
371			     unsigned psn)
372{
373	struct ipoib_cm_data data = {};
374	struct ib_cm_rep_param rep = {};
375
376	data.qpn = cpu_to_be32(priv->qp->qp_num);
377	data.mtu = cpu_to_be32(priv->cm.max_cm_mtu);
378
379	rep.private_data = &data;
380	rep.private_data_len = sizeof data;
381	rep.flow_control = 0;
382	rep.rnr_retry_count = req->rnr_retry_count;
383	rep.srq = ipoib_cm_has_srq(priv);
384	rep.qp_num = qp->qp_num;
385	rep.starting_psn = psn;
386	return ib_send_cm_rep(cm_id, &rep);
387}
388
389static int ipoib_cm_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
390{
391	struct ipoib_dev_priv *priv = cm_id->context;
392	struct ipoib_cm_rx *p;
393	unsigned psn;
394	int ret;
395
396	ipoib_dbg(priv, "REQ arrived\n");
397	p = kzalloc(sizeof *p, GFP_KERNEL);
398	if (!p)
399		return -ENOMEM;
400	p->priv = priv;
401	p->id = cm_id;
402	cm_id->context = p;
403	p->state = IPOIB_CM_RX_LIVE;
404	p->jiffies = jiffies;
405	INIT_LIST_HEAD(&p->list);
406
407	p->qp = ipoib_cm_create_rx_qp(priv, p);
408	if (IS_ERR(p->qp)) {
409		ret = PTR_ERR(p->qp);
410		goto err_qp;
411	}
412
413	psn = random() & 0xffffff;
414	ret = ipoib_cm_modify_rx_qp(priv, cm_id, p->qp, psn);
415	if (ret)
416		goto err_modify;
417
418	if (!ipoib_cm_has_srq(priv)) {
419		ret = ipoib_cm_nonsrq_init_rx(priv, cm_id, p);
420		if (ret)
421			goto err_modify;
422	}
423
424	spin_lock_irq(&priv->lock);
425	queue_delayed_work(ipoib_workqueue,
426			   &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
427	/* Add this entry to passive ids list head, but do not re-add it
428	 * if IB_EVENT_QP_LAST_WQE_REACHED has moved it to flush list. */
429	p->jiffies = jiffies;
430	if (p->state == IPOIB_CM_RX_LIVE)
431		list_move(&p->list, &priv->cm.passive_ids);
432	spin_unlock_irq(&priv->lock);
433
434	ret = ipoib_cm_send_rep(priv, cm_id, p->qp, &event->param.req_rcvd, psn);
435	if (ret) {
436		ipoib_warn(priv, "failed to send REP: %d\n", ret);
437		if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE))
438			ipoib_warn(priv, "unable to move qp to error state\n");
439	}
440	return 0;
441
442err_modify:
443	ib_destroy_qp(p->qp);
444err_qp:
445	kfree(p);
446	return ret;
447}
448
449static int ipoib_cm_rx_handler(struct ib_cm_id *cm_id,
450			       struct ib_cm_event *event)
451{
452	struct ipoib_cm_rx *p;
453	struct ipoib_dev_priv *priv;
454
455	switch (event->event) {
456	case IB_CM_REQ_RECEIVED:
457		return ipoib_cm_req_handler(cm_id, event);
458	case IB_CM_DREQ_RECEIVED:
459		p = cm_id->context;
460		ib_send_cm_drep(cm_id, NULL, 0);
461		/* Fall through */
462	case IB_CM_REJ_RECEIVED:
463		p = cm_id->context;
464		priv = p->priv;
465		if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE))
466			ipoib_warn(priv, "unable to move qp to error state\n");
467		/* Fall through */
468	default:
469		return 0;
470	}
471}
472
473void ipoib_cm_handle_rx_wc(struct ipoib_dev_priv *priv, struct ib_wc *wc)
474{
475	struct ipoib_cm_rx_buf saverx;
476	struct ipoib_cm_rx_buf *rx_ring;
477	unsigned int wr_id = wc->wr_id & ~(IPOIB_OP_CM | IPOIB_OP_RECV);
478	struct ifnet *dev = priv->dev;
479	struct mbuf *mb, *newmb;
480	struct ipoib_cm_rx *p;
481	int has_srq;
482	u_short proto;
483
484	ipoib_dbg_data(priv, "cm recv completion: id %d, status: %d\n",
485		       wr_id, wc->status);
486
487	if (unlikely(wr_id >= ipoib_recvq_size)) {
488		if (wr_id == (IPOIB_CM_RX_DRAIN_WRID & ~(IPOIB_OP_CM | IPOIB_OP_RECV))) {
489			spin_lock(&priv->lock);
490			list_splice_init(&priv->cm.rx_drain_list, &priv->cm.rx_reap_list);
491			ipoib_cm_start_rx_drain(priv);
492			if (priv->cm.id != NULL)
493				queue_work(ipoib_workqueue,
494				    &priv->cm.rx_reap_task);
495			spin_unlock(&priv->lock);
496		} else
497			ipoib_warn(priv, "cm recv completion event with wrid %d (> %d)\n",
498				   wr_id, ipoib_recvq_size);
499		return;
500	}
501
502	p = wc->qp->qp_context;
503
504	has_srq = ipoib_cm_has_srq(priv);
505	rx_ring = has_srq ? priv->cm.srq_ring : p->rx_ring;
506
507	mb = rx_ring[wr_id].mb;
508
509	if (unlikely(wc->status != IB_WC_SUCCESS)) {
510		ipoib_dbg(priv, "cm recv error "
511			   "(status=%d, wrid=%d vend_err %x)\n",
512			   wc->status, wr_id, wc->vendor_err);
513		++dev->if_ierrors;
514		if (has_srq)
515			goto repost;
516		else {
517			if (!--p->recv_count) {
518				spin_lock(&priv->lock);
519				list_move(&p->list, &priv->cm.rx_reap_list);
520				queue_work(ipoib_workqueue, &priv->cm.rx_reap_task);
521				spin_unlock(&priv->lock);
522			}
523			return;
524		}
525	}
526
527	if (unlikely(!(wr_id & IPOIB_CM_RX_UPDATE_MASK))) {
528		if (p && time_after_eq(jiffies, p->jiffies + IPOIB_CM_RX_UPDATE_TIME)) {
529			p->jiffies = jiffies;
530			/* Move this entry to list head, but do not re-add it
531			 * if it has been moved out of list. */
532			if (p->state == IPOIB_CM_RX_LIVE)
533				list_move(&p->list, &priv->cm.passive_ids);
534		}
535	}
536
537	memcpy(&saverx, &rx_ring[wr_id], sizeof(saverx));
538	newmb = ipoib_cm_alloc_rx_mb(priv, &rx_ring[wr_id]);
539	if (unlikely(!newmb)) {
540		/*
541		 * If we can't allocate a new RX buffer, dump
542		 * this packet and reuse the old buffer.
543		 */
544		ipoib_dbg(priv, "failed to allocate receive buffer %d\n", wr_id);
545		++dev->if_ierrors;
546		memcpy(&rx_ring[wr_id], &saverx, sizeof(saverx));
547		goto repost;
548	}
549
550	ipoib_cm_dma_unmap_rx(priv, &saverx);
551
552	ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
553		       wc->byte_len, wc->slid);
554
555	ipoib_dma_mb(priv, mb, wc->byte_len);
556
557	++dev->if_ipackets;
558	dev->if_ibytes += mb->m_pkthdr.len;
559
560	mb->m_pkthdr.rcvif = dev;
561	proto = *mtod(mb, uint16_t *);
562	m_adj(mb, IPOIB_ENCAP_LEN);
563
564	IPOIB_MTAP_PROTO(dev, mb, proto);
565	ipoib_demux(dev, mb, ntohs(proto));
566
567repost:
568	if (has_srq) {
569		if (unlikely(ipoib_cm_post_receive_srq(priv, wr_id)))
570			ipoib_warn(priv, "ipoib_cm_post_receive_srq failed "
571				   "for buf %d\n", wr_id);
572	} else {
573		if (unlikely(ipoib_cm_post_receive_nonsrq(priv, p,
574							  &priv->cm.rx_wr,
575							  priv->cm.rx_sge,
576							  wr_id))) {
577			--p->recv_count;
578			ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq failed "
579				   "for buf %d\n", wr_id);
580		}
581	}
582}
583
584static inline int post_send(struct ipoib_dev_priv *priv,
585			    struct ipoib_cm_tx *tx,
586			    struct ipoib_cm_tx_buf *tx_req,
587			    unsigned int wr_id)
588{
589	struct ib_send_wr *bad_wr;
590	struct mbuf *mb = tx_req->mb;
591	u64 *mapping = tx_req->mapping;
592	struct mbuf *m;
593	int i;
594
595	for (m = mb, i = 0; m != NULL; m = m->m_next, i++) {
596		priv->tx_sge[i].addr = mapping[i];
597		priv->tx_sge[i].length = m->m_len;
598	}
599	priv->tx_wr.num_sge = i;
600	priv->tx_wr.wr_id = wr_id | IPOIB_OP_CM;
601	priv->tx_wr.opcode = IB_WR_SEND;
602
603	return ib_post_send(tx->qp, &priv->tx_wr, &bad_wr);
604}
605
606void ipoib_cm_send(struct ipoib_dev_priv *priv, struct mbuf *mb, struct ipoib_cm_tx *tx)
607{
608	struct ipoib_cm_tx_buf *tx_req;
609	struct ifnet *dev = priv->dev;
610
611	if (unlikely(priv->tx_outstanding > MAX_SEND_CQE))
612		while (ipoib_poll_tx(priv)); /* nothing */
613
614	m_adj(mb, sizeof(struct ipoib_pseudoheader));
615	if (unlikely(mb->m_pkthdr.len > tx->mtu)) {
616		ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
617			   mb->m_pkthdr.len, tx->mtu);
618		++dev->if_oerrors;
619		ipoib_cm_mb_too_long(priv, mb, IPOIB_CM_MTU(tx->mtu));
620		return;
621	}
622
623	ipoib_dbg_data(priv, "sending packet: head 0x%x length %d connection 0x%x\n",
624		       tx->tx_head, mb->m_pkthdr.len, tx->qp->qp_num);
625
626
627	/*
628	 * We put the mb into the tx_ring _before_ we call post_send()
629	 * because it's entirely possible that the completion handler will
630	 * run before we execute anything after the post_send().  That
631	 * means we have to make sure everything is properly recorded and
632	 * our state is consistent before we call post_send().
633	 */
634	tx_req = &tx->tx_ring[tx->tx_head & (ipoib_sendq_size - 1)];
635	tx_req->mb = mb;
636	if (unlikely(ipoib_dma_map_tx(priv->ca, (struct ipoib_tx_buf *)tx_req,
637	    priv->cm.num_frags))) {
638		++dev->if_oerrors;
639		if (tx_req->mb)
640			m_freem(tx_req->mb);
641		return;
642	}
643
644	if (unlikely(post_send(priv, tx, tx_req, tx->tx_head & (ipoib_sendq_size - 1)))) {
645		ipoib_warn(priv, "post_send failed\n");
646		++dev->if_oerrors;
647		ipoib_dma_unmap_tx(priv->ca, (struct ipoib_tx_buf *)tx_req);
648		m_freem(mb);
649	} else {
650		++tx->tx_head;
651
652		if (++priv->tx_outstanding == ipoib_sendq_size) {
653			ipoib_dbg(priv, "TX ring 0x%x full, stopping kernel net queue\n",
654				  tx->qp->qp_num);
655			if (ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP))
656				ipoib_warn(priv, "request notify on send CQ failed\n");
657			dev->if_drv_flags |= IFF_DRV_OACTIVE;
658		}
659	}
660
661}
662
663void ipoib_cm_handle_tx_wc(struct ipoib_dev_priv *priv, struct ib_wc *wc)
664{
665	struct ipoib_cm_tx *tx = wc->qp->qp_context;
666	unsigned int wr_id = wc->wr_id & ~IPOIB_OP_CM;
667	struct ifnet *dev = priv->dev;
668	struct ipoib_cm_tx_buf *tx_req;
669
670	ipoib_dbg_data(priv, "cm send completion: id %d, status: %d\n",
671		       wr_id, wc->status);
672
673	if (unlikely(wr_id >= ipoib_sendq_size)) {
674		ipoib_warn(priv, "cm send completion event with wrid %d (> %d)\n",
675			   wr_id, ipoib_sendq_size);
676		return;
677	}
678
679	tx_req = &tx->tx_ring[wr_id];
680
681	ipoib_dma_unmap_tx(priv->ca, (struct ipoib_tx_buf *)tx_req);
682
683	/* FIXME: is this right? Shouldn't we only increment on success? */
684	++dev->if_opackets;
685
686	m_freem(tx_req->mb);
687
688	++tx->tx_tail;
689	if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
690	    (dev->if_drv_flags & IFF_DRV_OACTIVE) != 0 &&
691	    test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
692		dev->if_drv_flags &= ~IFF_DRV_OACTIVE;
693
694	if (wc->status != IB_WC_SUCCESS &&
695	    wc->status != IB_WC_WR_FLUSH_ERR) {
696		struct ipoib_path *path;
697
698		ipoib_dbg(priv, "failed cm send event "
699			   "(status=%d, wrid=%d vend_err %x)\n",
700			   wc->status, wr_id, wc->vendor_err);
701
702		path = tx->path;
703
704		if (path) {
705			path->cm = NULL;
706			rb_erase(&path->rb_node, &priv->path_tree);
707			list_del(&path->list);
708		}
709
710		if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
711			list_move(&tx->list, &priv->cm.reap_list);
712			queue_work(ipoib_workqueue, &priv->cm.reap_task);
713		}
714
715		clear_bit(IPOIB_FLAG_OPER_UP, &tx->flags);
716	}
717
718}
719
720int ipoib_cm_dev_open(struct ipoib_dev_priv *priv)
721{
722	int ret;
723
724	if (!IPOIB_CM_SUPPORTED(IF_LLADDR(priv->dev)))
725		return 0;
726
727	priv->cm.id = ib_create_cm_id(priv->ca, ipoib_cm_rx_handler, priv);
728	if (IS_ERR(priv->cm.id)) {
729		printk(KERN_WARNING "%s: failed to create CM ID\n", priv->ca->name);
730		ret = PTR_ERR(priv->cm.id);
731		goto err_cm;
732	}
733
734	ret = ib_cm_listen(priv->cm.id, cpu_to_be64(IPOIB_CM_IETF_ID | priv->qp->qp_num),
735			   0, NULL);
736	if (ret) {
737		printk(KERN_WARNING "%s: failed to listen on ID 0x%llx\n", priv->ca->name,
738		       IPOIB_CM_IETF_ID | priv->qp->qp_num);
739		goto err_listen;
740	}
741
742	return 0;
743
744err_listen:
745	ib_destroy_cm_id(priv->cm.id);
746err_cm:
747	priv->cm.id = NULL;
748	return ret;
749}
750
751static void ipoib_cm_free_rx_reap_list(struct ipoib_dev_priv *priv)
752{
753	struct ipoib_cm_rx *rx, *n;
754	LIST_HEAD(list);
755
756	spin_lock_irq(&priv->lock);
757	list_splice_init(&priv->cm.rx_reap_list, &list);
758	spin_unlock_irq(&priv->lock);
759
760	list_for_each_entry_safe(rx, n, &list, list) {
761		ib_destroy_cm_id(rx->id);
762		ib_destroy_qp(rx->qp);
763		if (!ipoib_cm_has_srq(priv)) {
764			ipoib_cm_free_rx_ring(priv, rx->rx_ring);
765			spin_lock_irq(&priv->lock);
766			--priv->cm.nonsrq_conn_qp;
767			spin_unlock_irq(&priv->lock);
768		}
769		kfree(rx);
770	}
771}
772
773void ipoib_cm_dev_stop(struct ipoib_dev_priv *priv)
774{
775	struct ipoib_cm_rx *p;
776	unsigned long begin;
777	int ret;
778
779	if (!IPOIB_CM_SUPPORTED(IF_LLADDR(priv->dev)) || !priv->cm.id)
780		return;
781
782	ib_destroy_cm_id(priv->cm.id);
783	priv->cm.id = NULL;
784
785	cancel_work_sync(&priv->cm.rx_reap_task);
786
787	spin_lock_irq(&priv->lock);
788	while (!list_empty(&priv->cm.passive_ids)) {
789		p = list_entry(priv->cm.passive_ids.next, typeof(*p), list);
790		list_move(&p->list, &priv->cm.rx_error_list);
791		p->state = IPOIB_CM_RX_ERROR;
792		spin_unlock_irq(&priv->lock);
793		ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
794		if (ret)
795			ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
796		spin_lock_irq(&priv->lock);
797	}
798
799	/* Wait for all RX to be drained */
800	begin = jiffies;
801
802	while (!list_empty(&priv->cm.rx_error_list) ||
803	       !list_empty(&priv->cm.rx_flush_list) ||
804	       !list_empty(&priv->cm.rx_drain_list)) {
805		if (time_after(jiffies, begin + 5 * HZ)) {
806			ipoib_warn(priv, "RX drain timing out\n");
807
808			/*
809			 * assume the HW is wedged and just free up everything.
810			 */
811			list_splice_init(&priv->cm.rx_flush_list,
812					 &priv->cm.rx_reap_list);
813			list_splice_init(&priv->cm.rx_error_list,
814					 &priv->cm.rx_reap_list);
815			list_splice_init(&priv->cm.rx_drain_list,
816					 &priv->cm.rx_reap_list);
817			break;
818		}
819		spin_unlock_irq(&priv->lock);
820		msleep(1);
821		ipoib_drain_cq(priv);
822		spin_lock_irq(&priv->lock);
823	}
824
825	spin_unlock_irq(&priv->lock);
826
827	ipoib_cm_free_rx_reap_list(priv);
828
829	cancel_delayed_work(&priv->cm.stale_task);
830}
831
832static int ipoib_cm_rep_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
833{
834	struct ipoib_cm_tx *p = cm_id->context;
835	struct ipoib_dev_priv *priv = p->priv;
836	struct ipoib_cm_data *data = event->private_data;
837	struct ifqueue mbqueue;
838	struct ib_qp_attr qp_attr;
839	int qp_attr_mask, ret;
840	struct mbuf *mb;
841
842	ipoib_dbg(priv, "cm rep handler\n");
843	p->mtu = be32_to_cpu(data->mtu);
844
845	if (p->mtu <= IPOIB_ENCAP_LEN) {
846		ipoib_warn(priv, "Rejecting connection: mtu %d <= %d\n",
847			   p->mtu, IPOIB_ENCAP_LEN);
848		return -EINVAL;
849	}
850
851	qp_attr.qp_state = IB_QPS_RTR;
852	ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
853	if (ret) {
854		ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
855		return ret;
856	}
857
858	qp_attr.rq_psn = 0 /* FIXME */;
859	ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
860	if (ret) {
861		ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
862		return ret;
863	}
864
865	qp_attr.qp_state = IB_QPS_RTS;
866	ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
867	if (ret) {
868		ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
869		return ret;
870	}
871	ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
872	if (ret) {
873		ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
874		return ret;
875	}
876
877	bzero(&mbqueue, sizeof(mbqueue));
878
879	spin_lock_irq(&priv->lock);
880	set_bit(IPOIB_FLAG_OPER_UP, &p->flags);
881	if (p->path)
882		for (;;) {
883			_IF_DEQUEUE(&p->path->queue, mb);
884			if (mb == NULL)
885				break;
886			_IF_ENQUEUE(&mbqueue, mb);
887		}
888	spin_unlock_irq(&priv->lock);
889
890	for (;;) {
891		struct ifnet *dev = p->priv->dev;
892		_IF_DEQUEUE(&mbqueue, mb);
893		if (mb == NULL)
894			break;
895		mb->m_pkthdr.rcvif = dev;
896		if (dev->if_transmit(dev, mb))
897			ipoib_warn(priv, "dev_queue_xmit failed "
898				   "to requeue packet\n");
899	}
900
901	ret = ib_send_cm_rtu(cm_id, NULL, 0);
902	if (ret) {
903		ipoib_warn(priv, "failed to send RTU: %d\n", ret);
904		return ret;
905	}
906	return 0;
907}
908
909static struct ib_qp *ipoib_cm_create_tx_qp(struct ipoib_dev_priv *priv,
910    struct ipoib_cm_tx *tx)
911{
912	struct ib_qp_init_attr attr = {
913		.send_cq		= priv->send_cq,
914		.recv_cq		= priv->recv_cq,
915		.srq			= priv->cm.srq,
916		.cap.max_send_wr	= ipoib_sendq_size,
917		.cap.max_send_sge	= priv->cm.num_frags,
918		.sq_sig_type		= IB_SIGNAL_ALL_WR,
919		.qp_type		= IB_QPT_RC,
920		.qp_context		= tx
921	};
922
923	return ib_create_qp(priv->pd, &attr);
924}
925
926static int ipoib_cm_send_req(struct ipoib_dev_priv *priv,
927			     struct ib_cm_id *id, struct ib_qp *qp,
928			     u32 qpn,
929			     struct ib_sa_path_rec *pathrec)
930{
931	struct ipoib_cm_data data = {};
932	struct ib_cm_req_param req = {};
933
934	ipoib_dbg(priv, "cm send req\n");
935
936	data.qpn = cpu_to_be32(priv->qp->qp_num);
937	data.mtu = cpu_to_be32(priv->cm.max_cm_mtu);
938
939	req.primary_path		= pathrec;
940	req.alternate_path		= NULL;
941	req.service_id			= cpu_to_be64(IPOIB_CM_IETF_ID | qpn);
942	req.qp_num			= qp->qp_num;
943	req.qp_type			= qp->qp_type;
944	req.private_data		= &data;
945	req.private_data_len		= sizeof data;
946	req.flow_control		= 0;
947
948	req.starting_psn		= 0; /* FIXME */
949
950	/*
951	 * Pick some arbitrary defaults here; we could make these
952	 * module parameters if anyone cared about setting them.
953	 */
954	req.responder_resources		= 4;
955	req.remote_cm_response_timeout	= 20;
956	req.local_cm_response_timeout	= 20;
957	req.retry_count			= 0; /* RFC draft warns against retries */
958	req.rnr_retry_count		= 0; /* RFC draft warns against retries */
959	req.max_cm_retries		= 15;
960	req.srq				= ipoib_cm_has_srq(priv);
961	return ib_send_cm_req(id, &req);
962}
963
964static int ipoib_cm_modify_tx_init(struct ipoib_dev_priv *priv,
965				  struct ib_cm_id *cm_id, struct ib_qp *qp)
966{
967	struct ib_qp_attr qp_attr;
968	int qp_attr_mask, ret;
969	ret = ib_find_pkey(priv->ca, priv->port, priv->pkey, &qp_attr.pkey_index);
970	if (ret) {
971		ipoib_warn(priv, "pkey 0x%x not found: %d\n", priv->pkey, ret);
972		return ret;
973	}
974
975	qp_attr.qp_state = IB_QPS_INIT;
976	qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE;
977	qp_attr.port_num = priv->port;
978	qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
979
980	ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
981	if (ret) {
982		ipoib_warn(priv, "failed to modify tx QP to INIT: %d\n", ret);
983		return ret;
984	}
985	return 0;
986}
987
988static int ipoib_cm_tx_init(struct ipoib_cm_tx *p, u32 qpn,
989			    struct ib_sa_path_rec *pathrec)
990{
991	struct ipoib_dev_priv *priv = p->priv;
992	int ret;
993
994	p->tx_ring = kzalloc(ipoib_sendq_size * sizeof *p->tx_ring, GFP_KERNEL);
995	if (!p->tx_ring) {
996		ipoib_warn(priv, "failed to allocate tx ring\n");
997		ret = -ENOMEM;
998		goto err_tx;
999	}
1000	memset(p->tx_ring, 0, ipoib_sendq_size * sizeof *p->tx_ring);
1001
1002	p->qp = ipoib_cm_create_tx_qp(p->priv, p);
1003	if (IS_ERR(p->qp)) {
1004		ret = PTR_ERR(p->qp);
1005		ipoib_warn(priv, "failed to allocate tx qp: %d\n", ret);
1006		goto err_qp;
1007	}
1008
1009	p->id = ib_create_cm_id(priv->ca, ipoib_cm_tx_handler, p);
1010	if (IS_ERR(p->id)) {
1011		ret = PTR_ERR(p->id);
1012		ipoib_warn(priv, "failed to create tx cm id: %d\n", ret);
1013		goto err_id;
1014	}
1015
1016	ret = ipoib_cm_modify_tx_init(p->priv, p->id,  p->qp);
1017	if (ret) {
1018		ipoib_warn(priv, "failed to modify tx qp to rtr: %d\n", ret);
1019		goto err_modify;
1020	}
1021
1022	ret = ipoib_cm_send_req(p->priv, p->id, p->qp, qpn, pathrec);
1023	if (ret) {
1024		ipoib_warn(priv, "failed to send cm req: %d\n", ret);
1025		goto err_send_cm;
1026	}
1027
1028	ipoib_dbg(priv, "Request connection 0x%x for gid %pI6 qpn 0x%x\n",
1029		  p->qp->qp_num, pathrec->dgid.raw, qpn);
1030
1031	return 0;
1032
1033err_send_cm:
1034err_modify:
1035	ib_destroy_cm_id(p->id);
1036err_id:
1037	p->id = NULL;
1038	ib_destroy_qp(p->qp);
1039err_qp:
1040	p->qp = NULL;
1041	kfree(p->tx_ring);
1042err_tx:
1043	return ret;
1044}
1045
1046static void ipoib_cm_tx_destroy(struct ipoib_cm_tx *p)
1047{
1048	struct ipoib_dev_priv *priv = p->priv;
1049	struct ifnet *dev = priv->dev;
1050	struct ipoib_cm_tx_buf *tx_req;
1051	unsigned long begin;
1052
1053	ipoib_dbg(priv, "Destroy active connection 0x%x head 0x%x tail 0x%x\n",
1054		  p->qp ? p->qp->qp_num : 0, p->tx_head, p->tx_tail);
1055
1056	if (p->path)
1057		ipoib_path_free(priv, p->path);
1058
1059	if (p->id)
1060		ib_destroy_cm_id(p->id);
1061
1062	if (p->tx_ring) {
1063		/* Wait for all sends to complete */
1064		begin = jiffies;
1065		while ((int) p->tx_tail - (int) p->tx_head < 0) {
1066			if (time_after(jiffies, begin + 5 * HZ)) {
1067				ipoib_warn(priv, "timing out; %d sends not completed\n",
1068					   p->tx_head - p->tx_tail);
1069				goto timeout;
1070			}
1071
1072			msleep(1);
1073		}
1074	}
1075
1076timeout:
1077
1078	while ((int) p->tx_tail - (int) p->tx_head < 0) {
1079		tx_req = &p->tx_ring[p->tx_tail & (ipoib_sendq_size - 1)];
1080		ipoib_dma_unmap_tx(priv->ca, (struct ipoib_tx_buf *)tx_req);
1081		m_freem(tx_req->mb);
1082		++p->tx_tail;
1083		if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
1084		    (dev->if_drv_flags & IFF_DRV_OACTIVE) != 0 &&
1085		    test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
1086			dev->if_drv_flags &= ~IFF_DRV_OACTIVE;
1087	}
1088
1089	if (p->qp)
1090		ib_destroy_qp(p->qp);
1091
1092	kfree(p->tx_ring);
1093	kfree(p);
1094}
1095
1096static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
1097			       struct ib_cm_event *event)
1098{
1099	struct ipoib_cm_tx *tx = cm_id->context;
1100	struct ipoib_dev_priv *priv = tx->priv;
1101	struct ipoib_path *path;
1102	unsigned long flags;
1103	int ret;
1104
1105	switch (event->event) {
1106	case IB_CM_DREQ_RECEIVED:
1107		ipoib_dbg(priv, "DREQ received.\n");
1108		ib_send_cm_drep(cm_id, NULL, 0);
1109		break;
1110	case IB_CM_REP_RECEIVED:
1111		ipoib_dbg(priv, "REP received.\n");
1112		ret = ipoib_cm_rep_handler(cm_id, event);
1113		if (ret)
1114			ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
1115				       NULL, 0, NULL, 0);
1116		break;
1117	case IB_CM_REQ_ERROR:
1118	case IB_CM_REJ_RECEIVED:
1119	case IB_CM_TIMEWAIT_EXIT:
1120		ipoib_dbg(priv, "CM error %d.\n", event->event);
1121		spin_lock_irqsave(&priv->lock, flags);
1122		path = tx->path;
1123
1124		if (path) {
1125			path->cm = NULL;
1126			tx->path = NULL;
1127			rb_erase(&path->rb_node, &priv->path_tree);
1128			list_del(&path->list);
1129		}
1130
1131		if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
1132			list_move(&tx->list, &priv->cm.reap_list);
1133			queue_work(ipoib_workqueue, &priv->cm.reap_task);
1134		}
1135
1136		spin_unlock_irqrestore(&priv->lock, flags);
1137		if (path)
1138			ipoib_path_free(tx->priv, path);
1139		break;
1140	default:
1141		break;
1142	}
1143
1144	return 0;
1145}
1146
1147struct ipoib_cm_tx *ipoib_cm_create_tx(struct ipoib_dev_priv *priv,
1148    struct ipoib_path *path)
1149{
1150	struct ipoib_cm_tx *tx;
1151
1152	tx = kzalloc(sizeof *tx, GFP_ATOMIC);
1153	if (!tx)
1154		return NULL;
1155
1156	ipoib_dbg(priv, "Creating cm tx\n");
1157	path->cm = tx;
1158	tx->path = path;
1159	tx->priv = priv;
1160	list_add(&tx->list, &priv->cm.start_list);
1161	set_bit(IPOIB_FLAG_INITIALIZED, &tx->flags);
1162	queue_work(ipoib_workqueue, &priv->cm.start_task);
1163	return tx;
1164}
1165
1166void ipoib_cm_destroy_tx(struct ipoib_cm_tx *tx)
1167{
1168	struct ipoib_dev_priv *priv = tx->priv;
1169	if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
1170		spin_lock(&priv->lock);
1171		list_move(&tx->list, &priv->cm.reap_list);
1172		spin_unlock(&priv->lock);
1173		queue_work(ipoib_workqueue, &priv->cm.reap_task);
1174		ipoib_dbg(priv, "Reap connection for gid %pI6\n",
1175			  tx->path->pathrec.dgid.raw);
1176		tx->path = NULL;
1177	}
1178}
1179
1180static void ipoib_cm_tx_start(struct work_struct *work)
1181{
1182	struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1183						   cm.start_task);
1184	struct ipoib_path *path;
1185	struct ipoib_cm_tx *p;
1186	unsigned long flags;
1187	int ret;
1188
1189	struct ib_sa_path_rec pathrec;
1190	u32 qpn;
1191
1192	ipoib_dbg(priv, "cm start task\n");
1193	spin_lock_irqsave(&priv->lock, flags);
1194
1195	while (!list_empty(&priv->cm.start_list)) {
1196		p = list_entry(priv->cm.start_list.next, typeof(*p), list);
1197		list_del_init(&p->list);
1198		path = p->path;
1199		qpn = IPOIB_QPN(path->hwaddr);
1200		memcpy(&pathrec, &p->path->pathrec, sizeof pathrec);
1201
1202		spin_unlock_irqrestore(&priv->lock, flags);
1203
1204		ret = ipoib_cm_tx_init(p, qpn, &pathrec);
1205
1206		spin_lock_irqsave(&priv->lock, flags);
1207
1208		if (ret) {
1209			path = p->path;
1210			if (path) {
1211				path->cm = NULL;
1212				rb_erase(&path->rb_node, &priv->path_tree);
1213				list_del(&path->list);
1214				ipoib_path_free(priv, path);
1215			}
1216			list_del(&p->list);
1217			kfree(p);
1218		}
1219	}
1220
1221	spin_unlock_irqrestore(&priv->lock, flags);
1222}
1223
1224static void ipoib_cm_tx_reap(struct work_struct *work)
1225{
1226	struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1227						   cm.reap_task);
1228	struct ipoib_cm_tx *p;
1229	unsigned long flags;
1230
1231	spin_lock_irqsave(&priv->lock, flags);
1232
1233	while (!list_empty(&priv->cm.reap_list)) {
1234		p = list_entry(priv->cm.reap_list.next, typeof(*p), list);
1235		list_del(&p->list);
1236		spin_unlock_irqrestore(&priv->lock, flags);
1237		ipoib_cm_tx_destroy(p);
1238		spin_lock_irqsave(&priv->lock, flags);
1239	}
1240
1241	spin_unlock_irqrestore(&priv->lock, flags);
1242}
1243
1244static void ipoib_cm_mb_reap(struct work_struct *work)
1245{
1246	struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1247						   cm.mb_task);
1248	struct mbuf *mb;
1249	unsigned long flags;
1250#if defined(INET) || defined(INET6)
1251	unsigned mtu = priv->mcast_mtu;
1252#endif
1253	uint16_t proto;
1254
1255	spin_lock_irqsave(&priv->lock, flags);
1256
1257	for (;;) {
1258		IF_DEQUEUE(&priv->cm.mb_queue, mb);
1259		if (mb == NULL)
1260			break;
1261		spin_unlock_irqrestore(&priv->lock, flags);
1262
1263		proto = htons(*mtod(mb, uint16_t *));
1264		m_adj(mb, IPOIB_ENCAP_LEN);
1265		switch (proto) {
1266#if defined(INET)
1267		case ETHERTYPE_IP:
1268			icmp_error(mb, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 0, mtu);
1269			break;
1270#endif
1271#if defined(INET6)
1272		case ETHERTYPE_IPV6:
1273			icmp6_error(mb, ICMP6_PACKET_TOO_BIG, 0, mtu);
1274			break;
1275#endif
1276		default:
1277			m_freem(mb);
1278		}
1279
1280		spin_lock_irqsave(&priv->lock, flags);
1281	}
1282
1283	spin_unlock_irqrestore(&priv->lock, flags);
1284}
1285
1286void
1287ipoib_cm_mb_too_long(struct ipoib_dev_priv *priv, struct mbuf *mb, unsigned int mtu)
1288{
1289	int e = priv->cm.mb_queue.ifq_len;
1290
1291	IF_ENQUEUE(&priv->cm.mb_queue, mb);
1292	if (e == 0)
1293		queue_work(ipoib_workqueue, &priv->cm.mb_task);
1294}
1295
1296static void ipoib_cm_rx_reap(struct work_struct *work)
1297{
1298	ipoib_cm_free_rx_reap_list(container_of(work, struct ipoib_dev_priv,
1299						cm.rx_reap_task));
1300}
1301
1302static void ipoib_cm_stale_task(struct work_struct *work)
1303{
1304	struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1305						   cm.stale_task.work);
1306	struct ipoib_cm_rx *p;
1307	int ret;
1308
1309	spin_lock_irq(&priv->lock);
1310	while (!list_empty(&priv->cm.passive_ids)) {
1311		/* List is sorted by LRU, start from tail,
1312		 * stop when we see a recently used entry */
1313		p = list_entry(priv->cm.passive_ids.prev, typeof(*p), list);
1314		if (time_before_eq(jiffies, p->jiffies + IPOIB_CM_RX_TIMEOUT))
1315			break;
1316		list_move(&p->list, &priv->cm.rx_error_list);
1317		p->state = IPOIB_CM_RX_ERROR;
1318		spin_unlock_irq(&priv->lock);
1319		ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
1320		if (ret)
1321			ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
1322		spin_lock_irq(&priv->lock);
1323	}
1324
1325	if (!list_empty(&priv->cm.passive_ids))
1326		queue_delayed_work(ipoib_workqueue,
1327				   &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
1328	spin_unlock_irq(&priv->lock);
1329}
1330
1331
1332static void ipoib_cm_create_srq(struct ipoib_dev_priv *priv, int max_sge)
1333{
1334	struct ib_srq_init_attr srq_init_attr = {
1335		.attr = {
1336			.max_wr  = ipoib_recvq_size,
1337			.max_sge = max_sge
1338		}
1339	};
1340
1341	priv->cm.srq = ib_create_srq(priv->pd, &srq_init_attr);
1342	if (IS_ERR(priv->cm.srq)) {
1343		if (PTR_ERR(priv->cm.srq) != -ENOSYS)
1344			printk(KERN_WARNING "%s: failed to allocate SRQ, error %ld\n",
1345			       priv->ca->name, PTR_ERR(priv->cm.srq));
1346		priv->cm.srq = NULL;
1347		return;
1348	}
1349
1350	priv->cm.srq_ring = kzalloc(ipoib_recvq_size * sizeof *priv->cm.srq_ring, GFP_KERNEL);
1351	if (!priv->cm.srq_ring) {
1352		printk(KERN_WARNING "%s: failed to allocate CM SRQ ring (%d entries)\n",
1353		       priv->ca->name, ipoib_recvq_size);
1354		ib_destroy_srq(priv->cm.srq);
1355		priv->cm.srq = NULL;
1356		return;
1357	}
1358
1359	memset(priv->cm.srq_ring, 0, ipoib_recvq_size * sizeof *priv->cm.srq_ring);
1360}
1361
1362int ipoib_cm_dev_init(struct ipoib_dev_priv *priv)
1363{
1364	struct ifnet *dev = priv->dev;
1365	int i, ret;
1366	struct ib_device_attr attr;
1367
1368	INIT_LIST_HEAD(&priv->cm.passive_ids);
1369	INIT_LIST_HEAD(&priv->cm.reap_list);
1370	INIT_LIST_HEAD(&priv->cm.start_list);
1371	INIT_LIST_HEAD(&priv->cm.rx_error_list);
1372	INIT_LIST_HEAD(&priv->cm.rx_flush_list);
1373	INIT_LIST_HEAD(&priv->cm.rx_drain_list);
1374	INIT_LIST_HEAD(&priv->cm.rx_reap_list);
1375	INIT_WORK(&priv->cm.start_task, ipoib_cm_tx_start);
1376	INIT_WORK(&priv->cm.reap_task, ipoib_cm_tx_reap);
1377	INIT_WORK(&priv->cm.mb_task, ipoib_cm_mb_reap);
1378	INIT_WORK(&priv->cm.rx_reap_task, ipoib_cm_rx_reap);
1379	INIT_DELAYED_WORK(&priv->cm.stale_task, ipoib_cm_stale_task);
1380
1381	bzero(&priv->cm.mb_queue, sizeof(priv->cm.mb_queue));
1382	mtx_init(&priv->cm.mb_queue.ifq_mtx,
1383	    dev->if_xname, "if send queue", MTX_DEF);
1384
1385	ret = ib_query_device(priv->ca, &attr);
1386	if (ret) {
1387		printk(KERN_WARNING "ib_query_device() failed with %d\n", ret);
1388		return ret;
1389	}
1390
1391	ipoib_dbg(priv, "max_srq_sge=%d\n", attr.max_srq_sge);
1392
1393	attr.max_srq_sge = min_t(int, IPOIB_CM_RX_SG, attr.max_srq_sge);
1394	ipoib_cm_create_srq(priv, attr.max_srq_sge);
1395	if (ipoib_cm_has_srq(priv)) {
1396		priv->cm.max_cm_mtu = attr.max_srq_sge * MJUMPAGESIZE;
1397		priv->cm.num_frags  = attr.max_srq_sge;
1398		ipoib_dbg(priv, "max_cm_mtu = 0x%x, num_frags=%d\n",
1399			  priv->cm.max_cm_mtu, priv->cm.num_frags);
1400	} else {
1401		priv->cm.max_cm_mtu = IPOIB_CM_MAX_MTU;
1402		priv->cm.num_frags  = IPOIB_CM_RX_SG;
1403	}
1404
1405	ipoib_cm_init_rx_wr(priv, &priv->cm.rx_wr, priv->cm.rx_sge);
1406
1407	if (ipoib_cm_has_srq(priv)) {
1408		for (i = 0; i < ipoib_recvq_size; ++i) {
1409			if (!ipoib_cm_alloc_rx_mb(priv, &priv->cm.srq_ring[i])) {
1410				ipoib_warn(priv, "failed to allocate "
1411					   "receive buffer %d\n", i);
1412				ipoib_cm_dev_cleanup(priv);
1413				return -ENOMEM;
1414			}
1415
1416			if (ipoib_cm_post_receive_srq(priv, i)) {
1417				ipoib_warn(priv, "ipoib_cm_post_receive_srq "
1418					   "failed for buf %d\n", i);
1419				ipoib_cm_dev_cleanup(priv);
1420				return -EIO;
1421			}
1422		}
1423	}
1424
1425	IF_LLADDR(priv->dev)[0] = IPOIB_FLAGS_RC;
1426	return 0;
1427}
1428
1429void ipoib_cm_dev_cleanup(struct ipoib_dev_priv *priv)
1430{
1431	int ret;
1432
1433	if (!priv->cm.srq)
1434		return;
1435
1436	ipoib_dbg(priv, "Cleanup ipoib connected mode.\n");
1437
1438	ret = ib_destroy_srq(priv->cm.srq);
1439	if (ret)
1440		ipoib_warn(priv, "ib_destroy_srq failed: %d\n", ret);
1441
1442	priv->cm.srq = NULL;
1443	if (!priv->cm.srq_ring)
1444		return;
1445
1446	ipoib_cm_free_rx_ring(priv, priv->cm.srq_ring);
1447	priv->cm.srq_ring = NULL;
1448
1449	mtx_destroy(&priv->cm.mb_queue.ifq_mtx);
1450}
1451
1452#endif /* CONFIG_INFINIBAND_IPOIB_CM */
1453