ipoib_cm.c revision 331769
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->pd->local_dma_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	CURVNET_SET_QUIET(dev->if_vnet);
485
486	ipoib_dbg_data(priv, "cm recv completion: id %d, status: %d\n",
487		       wr_id, wc->status);
488
489	if (unlikely(wr_id >= ipoib_recvq_size)) {
490		if (wr_id == (IPOIB_CM_RX_DRAIN_WRID & ~(IPOIB_OP_CM | IPOIB_OP_RECV))) {
491			spin_lock(&priv->lock);
492			list_splice_init(&priv->cm.rx_drain_list, &priv->cm.rx_reap_list);
493			ipoib_cm_start_rx_drain(priv);
494			if (priv->cm.id != NULL)
495				queue_work(ipoib_workqueue,
496				    &priv->cm.rx_reap_task);
497			spin_unlock(&priv->lock);
498		} else
499			ipoib_warn(priv, "cm recv completion event with wrid %d (> %d)\n",
500				   wr_id, ipoib_recvq_size);
501		goto done;
502	}
503
504	p = wc->qp->qp_context;
505
506	has_srq = ipoib_cm_has_srq(priv);
507	rx_ring = has_srq ? priv->cm.srq_ring : p->rx_ring;
508
509	mb = rx_ring[wr_id].mb;
510
511	if (unlikely(wc->status != IB_WC_SUCCESS)) {
512		ipoib_dbg(priv, "cm recv error "
513			   "(status=%d, wrid=%d vend_err %x)\n",
514			   wc->status, wr_id, wc->vendor_err);
515		if_inc_counter(dev, IFCOUNTER_IERRORS, 1);
516		if (has_srq)
517			goto repost;
518		else {
519			if (!--p->recv_count) {
520				spin_lock(&priv->lock);
521				list_move(&p->list, &priv->cm.rx_reap_list);
522				queue_work(ipoib_workqueue, &priv->cm.rx_reap_task);
523				spin_unlock(&priv->lock);
524			}
525			goto done;
526		}
527	}
528
529	if (unlikely(!(wr_id & IPOIB_CM_RX_UPDATE_MASK))) {
530		if (p && time_after_eq(jiffies, p->jiffies + IPOIB_CM_RX_UPDATE_TIME)) {
531			p->jiffies = jiffies;
532			/* Move this entry to list head, but do not re-add it
533			 * if it has been moved out of list. */
534			if (p->state == IPOIB_CM_RX_LIVE)
535				list_move(&p->list, &priv->cm.passive_ids);
536		}
537	}
538
539	memcpy(&saverx, &rx_ring[wr_id], sizeof(saverx));
540	newmb = ipoib_cm_alloc_rx_mb(priv, &rx_ring[wr_id]);
541	if (unlikely(!newmb)) {
542		/*
543		 * If we can't allocate a new RX buffer, dump
544		 * this packet and reuse the old buffer.
545		 */
546		ipoib_dbg(priv, "failed to allocate receive buffer %d\n", wr_id);
547		if_inc_counter(dev, IFCOUNTER_IERRORS, 1);
548		memcpy(&rx_ring[wr_id], &saverx, sizeof(saverx));
549		goto repost;
550	}
551
552	ipoib_cm_dma_unmap_rx(priv, &saverx);
553
554	ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
555		       wc->byte_len, wc->slid);
556
557	ipoib_dma_mb(priv, mb, wc->byte_len);
558
559	if_inc_counter(dev, IFCOUNTER_IPACKETS, 1);
560	if_inc_counter(dev, IFCOUNTER_IBYTES, mb->m_pkthdr.len);
561
562	mb->m_pkthdr.rcvif = dev;
563	proto = *mtod(mb, uint16_t *);
564	m_adj(mb, IPOIB_ENCAP_LEN);
565
566	IPOIB_MTAP_PROTO(dev, mb, proto);
567	ipoib_demux(dev, mb, ntohs(proto));
568
569repost:
570	if (has_srq) {
571		if (unlikely(ipoib_cm_post_receive_srq(priv, wr_id)))
572			ipoib_warn(priv, "ipoib_cm_post_receive_srq failed "
573				   "for buf %d\n", wr_id);
574	} else {
575		if (unlikely(ipoib_cm_post_receive_nonsrq(priv, p,
576							  &priv->cm.rx_wr,
577							  priv->cm.rx_sge,
578							  wr_id))) {
579			--p->recv_count;
580			ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq failed "
581				   "for buf %d\n", wr_id);
582		}
583	}
584done:
585	CURVNET_RESTORE();
586	return;
587}
588
589static inline int post_send(struct ipoib_dev_priv *priv,
590			    struct ipoib_cm_tx *tx,
591			    struct ipoib_cm_tx_buf *tx_req,
592			    unsigned int wr_id)
593{
594	struct ib_send_wr *bad_wr;
595	struct mbuf *mb = tx_req->mb;
596	u64 *mapping = tx_req->mapping;
597	struct mbuf *m;
598	int i;
599
600	for (m = mb, i = 0; m != NULL; m = m->m_next, i++) {
601		priv->tx_sge[i].addr = mapping[i];
602		priv->tx_sge[i].length = m->m_len;
603	}
604	priv->tx_wr.wr.num_sge = i;
605	priv->tx_wr.wr.wr_id = wr_id | IPOIB_OP_CM;
606	priv->tx_wr.wr.opcode = IB_WR_SEND;
607
608	return ib_post_send(tx->qp, &priv->tx_wr.wr, &bad_wr);
609}
610
611void ipoib_cm_send(struct ipoib_dev_priv *priv, struct mbuf *mb, struct ipoib_cm_tx *tx)
612{
613	struct ipoib_cm_tx_buf *tx_req;
614	struct ifnet *dev = priv->dev;
615
616	if (unlikely(priv->tx_outstanding > MAX_SEND_CQE))
617		while (ipoib_poll_tx(priv)); /* nothing */
618
619	m_adj(mb, sizeof(struct ipoib_pseudoheader));
620	if (unlikely(mb->m_pkthdr.len > tx->mtu)) {
621		ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
622			   mb->m_pkthdr.len, tx->mtu);
623		if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
624		ipoib_cm_mb_too_long(priv, mb, IPOIB_CM_MTU(tx->mtu));
625		return;
626	}
627
628	ipoib_dbg_data(priv, "sending packet: head 0x%x length %d connection 0x%x\n",
629		       tx->tx_head, mb->m_pkthdr.len, tx->qp->qp_num);
630
631
632	/*
633	 * We put the mb into the tx_ring _before_ we call post_send()
634	 * because it's entirely possible that the completion handler will
635	 * run before we execute anything after the post_send().  That
636	 * means we have to make sure everything is properly recorded and
637	 * our state is consistent before we call post_send().
638	 */
639	tx_req = &tx->tx_ring[tx->tx_head & (ipoib_sendq_size - 1)];
640	tx_req->mb = mb;
641	if (unlikely(ipoib_dma_map_tx(priv->ca, (struct ipoib_tx_buf *)tx_req,
642	    priv->cm.num_frags))) {
643		if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
644		if (tx_req->mb)
645			m_freem(tx_req->mb);
646		return;
647	}
648
649	if (unlikely(post_send(priv, tx, tx_req, tx->tx_head & (ipoib_sendq_size - 1)))) {
650		ipoib_warn(priv, "post_send failed\n");
651		if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
652		ipoib_dma_unmap_tx(priv->ca, (struct ipoib_tx_buf *)tx_req);
653		m_freem(mb);
654	} else {
655		++tx->tx_head;
656
657		if (++priv->tx_outstanding == ipoib_sendq_size) {
658			ipoib_dbg(priv, "TX ring 0x%x full, stopping kernel net queue\n",
659				  tx->qp->qp_num);
660			if (ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP))
661				ipoib_warn(priv, "request notify on send CQ failed\n");
662			dev->if_drv_flags |= IFF_DRV_OACTIVE;
663		}
664	}
665
666}
667
668void ipoib_cm_handle_tx_wc(struct ipoib_dev_priv *priv, struct ib_wc *wc)
669{
670	struct ipoib_cm_tx *tx = wc->qp->qp_context;
671	unsigned int wr_id = wc->wr_id & ~IPOIB_OP_CM;
672	struct ifnet *dev = priv->dev;
673	struct ipoib_cm_tx_buf *tx_req;
674
675	ipoib_dbg_data(priv, "cm send completion: id %d, status: %d\n",
676		       wr_id, wc->status);
677
678	if (unlikely(wr_id >= ipoib_sendq_size)) {
679		ipoib_warn(priv, "cm send completion event with wrid %d (> %d)\n",
680			   wr_id, ipoib_sendq_size);
681		return;
682	}
683
684	tx_req = &tx->tx_ring[wr_id];
685
686	ipoib_dma_unmap_tx(priv->ca, (struct ipoib_tx_buf *)tx_req);
687
688	/* FIXME: is this right? Shouldn't we only increment on success? */
689	if_inc_counter(dev, IFCOUNTER_OPACKETS, 1);
690
691	m_freem(tx_req->mb);
692
693	++tx->tx_tail;
694	if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
695	    (dev->if_drv_flags & IFF_DRV_OACTIVE) != 0 &&
696	    test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
697		dev->if_drv_flags &= ~IFF_DRV_OACTIVE;
698
699	if (wc->status != IB_WC_SUCCESS &&
700	    wc->status != IB_WC_WR_FLUSH_ERR) {
701		struct ipoib_path *path;
702
703		ipoib_dbg(priv, "failed cm send event "
704			   "(status=%d, wrid=%d vend_err %x)\n",
705			   wc->status, wr_id, wc->vendor_err);
706
707		path = tx->path;
708
709		if (path) {
710			path->cm = NULL;
711			rb_erase(&path->rb_node, &priv->path_tree);
712			list_del(&path->list);
713		}
714
715		if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
716			list_move(&tx->list, &priv->cm.reap_list);
717			queue_work(ipoib_workqueue, &priv->cm.reap_task);
718		}
719
720		clear_bit(IPOIB_FLAG_OPER_UP, &tx->flags);
721	}
722
723}
724
725int ipoib_cm_dev_open(struct ipoib_dev_priv *priv)
726{
727	int ret;
728
729	if (!IPOIB_CM_SUPPORTED(IF_LLADDR(priv->dev)))
730		return 0;
731
732	priv->cm.id = ib_create_cm_id(priv->ca, ipoib_cm_rx_handler, priv);
733	if (IS_ERR(priv->cm.id)) {
734		printk(KERN_WARNING "%s: failed to create CM ID\n", priv->ca->name);
735		ret = PTR_ERR(priv->cm.id);
736		goto err_cm;
737	}
738
739	ret = ib_cm_listen(priv->cm.id, cpu_to_be64(IPOIB_CM_IETF_ID | priv->qp->qp_num), 0);
740	if (ret) {
741		printk(KERN_WARNING "%s: failed to listen on ID 0x%llx\n", priv->ca->name,
742		       IPOIB_CM_IETF_ID | priv->qp->qp_num);
743		goto err_listen;
744	}
745
746	return 0;
747
748err_listen:
749	ib_destroy_cm_id(priv->cm.id);
750err_cm:
751	priv->cm.id = NULL;
752	return ret;
753}
754
755static void ipoib_cm_free_rx_reap_list(struct ipoib_dev_priv *priv)
756{
757	struct ipoib_cm_rx *rx, *n;
758	LIST_HEAD(list);
759
760	spin_lock_irq(&priv->lock);
761	list_splice_init(&priv->cm.rx_reap_list, &list);
762	spin_unlock_irq(&priv->lock);
763
764	list_for_each_entry_safe(rx, n, &list, list) {
765		ib_destroy_cm_id(rx->id);
766		ib_destroy_qp(rx->qp);
767		if (!ipoib_cm_has_srq(priv)) {
768			ipoib_cm_free_rx_ring(priv, rx->rx_ring);
769			spin_lock_irq(&priv->lock);
770			--priv->cm.nonsrq_conn_qp;
771			spin_unlock_irq(&priv->lock);
772		}
773		kfree(rx);
774	}
775}
776
777void ipoib_cm_dev_stop(struct ipoib_dev_priv *priv)
778{
779	struct ipoib_cm_rx *p;
780	unsigned long begin;
781	int ret;
782
783	if (!IPOIB_CM_SUPPORTED(IF_LLADDR(priv->dev)) || !priv->cm.id)
784		return;
785
786	ib_destroy_cm_id(priv->cm.id);
787	priv->cm.id = NULL;
788
789	cancel_work_sync(&priv->cm.rx_reap_task);
790
791	spin_lock_irq(&priv->lock);
792	while (!list_empty(&priv->cm.passive_ids)) {
793		p = list_entry(priv->cm.passive_ids.next, typeof(*p), list);
794		list_move(&p->list, &priv->cm.rx_error_list);
795		p->state = IPOIB_CM_RX_ERROR;
796		spin_unlock_irq(&priv->lock);
797		ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
798		if (ret)
799			ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
800		spin_lock_irq(&priv->lock);
801	}
802
803	/* Wait for all RX to be drained */
804	begin = jiffies;
805
806	while (!list_empty(&priv->cm.rx_error_list) ||
807	       !list_empty(&priv->cm.rx_flush_list) ||
808	       !list_empty(&priv->cm.rx_drain_list)) {
809		if (time_after(jiffies, begin + 5 * HZ)) {
810			ipoib_warn(priv, "RX drain timing out\n");
811
812			/*
813			 * assume the HW is wedged and just free up everything.
814			 */
815			list_splice_init(&priv->cm.rx_flush_list,
816					 &priv->cm.rx_reap_list);
817			list_splice_init(&priv->cm.rx_error_list,
818					 &priv->cm.rx_reap_list);
819			list_splice_init(&priv->cm.rx_drain_list,
820					 &priv->cm.rx_reap_list);
821			break;
822		}
823		spin_unlock_irq(&priv->lock);
824		msleep(1);
825		ipoib_drain_cq(priv);
826		spin_lock_irq(&priv->lock);
827	}
828
829	spin_unlock_irq(&priv->lock);
830
831	ipoib_cm_free_rx_reap_list(priv);
832
833	cancel_delayed_work_sync(&priv->cm.stale_task);
834}
835
836static int ipoib_cm_rep_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
837{
838	struct ipoib_cm_tx *p = cm_id->context;
839	struct ipoib_dev_priv *priv = p->priv;
840	struct ipoib_cm_data *data = event->private_data;
841	struct ifqueue mbqueue;
842	struct ib_qp_attr qp_attr;
843	int qp_attr_mask, ret;
844	struct mbuf *mb;
845
846	ipoib_dbg(priv, "cm rep handler\n");
847	p->mtu = be32_to_cpu(data->mtu);
848
849	if (p->mtu <= IPOIB_ENCAP_LEN) {
850		ipoib_warn(priv, "Rejecting connection: mtu %d <= %d\n",
851			   p->mtu, IPOIB_ENCAP_LEN);
852		return -EINVAL;
853	}
854
855	qp_attr.qp_state = IB_QPS_RTR;
856	ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
857	if (ret) {
858		ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
859		return ret;
860	}
861
862	qp_attr.rq_psn = 0 /* FIXME */;
863	ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
864	if (ret) {
865		ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
866		return ret;
867	}
868
869	qp_attr.qp_state = IB_QPS_RTS;
870	ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
871	if (ret) {
872		ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
873		return ret;
874	}
875	ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
876	if (ret) {
877		ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
878		return ret;
879	}
880
881	bzero(&mbqueue, sizeof(mbqueue));
882
883	spin_lock_irq(&priv->lock);
884	set_bit(IPOIB_FLAG_OPER_UP, &p->flags);
885	if (p->path)
886		for (;;) {
887			_IF_DEQUEUE(&p->path->queue, mb);
888			if (mb == NULL)
889				break;
890			_IF_ENQUEUE(&mbqueue, mb);
891		}
892	spin_unlock_irq(&priv->lock);
893
894	for (;;) {
895		struct ifnet *dev = p->priv->dev;
896		_IF_DEQUEUE(&mbqueue, mb);
897		if (mb == NULL)
898			break;
899		mb->m_pkthdr.rcvif = dev;
900		if (dev->if_transmit(dev, mb))
901			ipoib_warn(priv, "dev_queue_xmit failed "
902				   "to requeue packet\n");
903	}
904
905	ret = ib_send_cm_rtu(cm_id, NULL, 0);
906	if (ret) {
907		ipoib_warn(priv, "failed to send RTU: %d\n", ret);
908		return ret;
909	}
910	return 0;
911}
912
913static struct ib_qp *ipoib_cm_create_tx_qp(struct ipoib_dev_priv *priv,
914    struct ipoib_cm_tx *tx)
915{
916	struct ib_qp_init_attr attr = {
917		.send_cq		= priv->send_cq,
918		.recv_cq		= priv->recv_cq,
919		.srq			= priv->cm.srq,
920		.cap.max_send_wr	= ipoib_sendq_size,
921		.cap.max_send_sge	= priv->cm.num_frags,
922		.sq_sig_type		= IB_SIGNAL_ALL_WR,
923		.qp_type		= IB_QPT_RC,
924		.qp_context		= tx
925	};
926
927	return ib_create_qp(priv->pd, &attr);
928}
929
930static int ipoib_cm_send_req(struct ipoib_dev_priv *priv,
931			     struct ib_cm_id *id, struct ib_qp *qp,
932			     u32 qpn,
933			     struct ib_sa_path_rec *pathrec)
934{
935	struct ipoib_cm_data data = {};
936	struct ib_cm_req_param req = {};
937
938	ipoib_dbg(priv, "cm send req\n");
939
940	data.qpn = cpu_to_be32(priv->qp->qp_num);
941	data.mtu = cpu_to_be32(priv->cm.max_cm_mtu);
942
943	req.primary_path		= pathrec;
944	req.alternate_path		= NULL;
945	req.service_id			= cpu_to_be64(IPOIB_CM_IETF_ID | qpn);
946	req.qp_num			= qp->qp_num;
947	req.qp_type			= qp->qp_type;
948	req.private_data		= &data;
949	req.private_data_len		= sizeof data;
950	req.flow_control		= 0;
951
952	req.starting_psn		= 0; /* FIXME */
953
954	/*
955	 * Pick some arbitrary defaults here; we could make these
956	 * module parameters if anyone cared about setting them.
957	 */
958	req.responder_resources		= 4;
959	req.remote_cm_response_timeout	= 20;
960	req.local_cm_response_timeout	= 20;
961	req.retry_count			= 0; /* RFC draft warns against retries */
962	req.rnr_retry_count		= 0; /* RFC draft warns against retries */
963	req.max_cm_retries		= 15;
964	req.srq				= ipoib_cm_has_srq(priv);
965	return ib_send_cm_req(id, &req);
966}
967
968static int ipoib_cm_modify_tx_init(struct ipoib_dev_priv *priv,
969				  struct ib_cm_id *cm_id, struct ib_qp *qp)
970{
971	struct ib_qp_attr qp_attr;
972	int qp_attr_mask, ret;
973	ret = ib_find_pkey(priv->ca, priv->port, priv->pkey, &qp_attr.pkey_index);
974	if (ret) {
975		ipoib_warn(priv, "pkey 0x%x not found: %d\n", priv->pkey, ret);
976		return ret;
977	}
978
979	qp_attr.qp_state = IB_QPS_INIT;
980	qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE;
981	qp_attr.port_num = priv->port;
982	qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
983
984	ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
985	if (ret) {
986		ipoib_warn(priv, "failed to modify tx QP to INIT: %d\n", ret);
987		return ret;
988	}
989	return 0;
990}
991
992static int ipoib_cm_tx_init(struct ipoib_cm_tx *p, u32 qpn,
993			    struct ib_sa_path_rec *pathrec)
994{
995	struct ipoib_dev_priv *priv = p->priv;
996	int ret;
997
998	p->tx_ring = kzalloc(ipoib_sendq_size * sizeof *p->tx_ring, GFP_KERNEL);
999	if (!p->tx_ring) {
1000		ipoib_warn(priv, "failed to allocate tx ring\n");
1001		ret = -ENOMEM;
1002		goto err_tx;
1003	}
1004	memset(p->tx_ring, 0, ipoib_sendq_size * sizeof *p->tx_ring);
1005
1006	p->qp = ipoib_cm_create_tx_qp(p->priv, p);
1007	if (IS_ERR(p->qp)) {
1008		ret = PTR_ERR(p->qp);
1009		ipoib_warn(priv, "failed to allocate tx qp: %d\n", ret);
1010		goto err_qp;
1011	}
1012
1013	p->id = ib_create_cm_id(priv->ca, ipoib_cm_tx_handler, p);
1014	if (IS_ERR(p->id)) {
1015		ret = PTR_ERR(p->id);
1016		ipoib_warn(priv, "failed to create tx cm id: %d\n", ret);
1017		goto err_id;
1018	}
1019
1020	ret = ipoib_cm_modify_tx_init(p->priv, p->id,  p->qp);
1021	if (ret) {
1022		ipoib_warn(priv, "failed to modify tx qp to rtr: %d\n", ret);
1023		goto err_modify;
1024	}
1025
1026	ret = ipoib_cm_send_req(p->priv, p->id, p->qp, qpn, pathrec);
1027	if (ret) {
1028		ipoib_warn(priv, "failed to send cm req: %d\n", ret);
1029		goto err_send_cm;
1030	}
1031
1032	ipoib_dbg(priv, "Request connection 0x%x for gid %pI6 qpn 0x%x\n",
1033		  p->qp->qp_num, pathrec->dgid.raw, qpn);
1034
1035	return 0;
1036
1037err_send_cm:
1038err_modify:
1039	ib_destroy_cm_id(p->id);
1040err_id:
1041	p->id = NULL;
1042	ib_destroy_qp(p->qp);
1043err_qp:
1044	p->qp = NULL;
1045	kfree(p->tx_ring);
1046err_tx:
1047	return ret;
1048}
1049
1050static void ipoib_cm_tx_destroy(struct ipoib_cm_tx *p)
1051{
1052	struct ipoib_dev_priv *priv = p->priv;
1053	struct ifnet *dev = priv->dev;
1054	struct ipoib_cm_tx_buf *tx_req;
1055	unsigned long begin;
1056
1057	ipoib_dbg(priv, "Destroy active connection 0x%x head 0x%x tail 0x%x\n",
1058		  p->qp ? p->qp->qp_num : 0, p->tx_head, p->tx_tail);
1059
1060	if (p->path)
1061		ipoib_path_free(priv, p->path);
1062
1063	if (p->id)
1064		ib_destroy_cm_id(p->id);
1065
1066	if (p->tx_ring) {
1067		/* Wait for all sends to complete */
1068		begin = jiffies;
1069		while ((int) p->tx_tail - (int) p->tx_head < 0) {
1070			if (time_after(jiffies, begin + 5 * HZ)) {
1071				ipoib_warn(priv, "timing out; %d sends not completed\n",
1072					   p->tx_head - p->tx_tail);
1073				goto timeout;
1074			}
1075
1076			msleep(1);
1077		}
1078	}
1079
1080timeout:
1081
1082	while ((int) p->tx_tail - (int) p->tx_head < 0) {
1083		tx_req = &p->tx_ring[p->tx_tail & (ipoib_sendq_size - 1)];
1084		ipoib_dma_unmap_tx(priv->ca, (struct ipoib_tx_buf *)tx_req);
1085		m_freem(tx_req->mb);
1086		++p->tx_tail;
1087		if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
1088		    (dev->if_drv_flags & IFF_DRV_OACTIVE) != 0 &&
1089		    test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
1090			dev->if_drv_flags &= ~IFF_DRV_OACTIVE;
1091	}
1092
1093	if (p->qp)
1094		ib_destroy_qp(p->qp);
1095
1096	kfree(p->tx_ring);
1097	kfree(p);
1098}
1099
1100static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
1101			       struct ib_cm_event *event)
1102{
1103	struct ipoib_cm_tx *tx = cm_id->context;
1104	struct ipoib_dev_priv *priv = tx->priv;
1105	struct ipoib_path *path;
1106	unsigned long flags;
1107	int ret;
1108
1109	switch (event->event) {
1110	case IB_CM_DREQ_RECEIVED:
1111		ipoib_dbg(priv, "DREQ received.\n");
1112		ib_send_cm_drep(cm_id, NULL, 0);
1113		break;
1114	case IB_CM_REP_RECEIVED:
1115		ipoib_dbg(priv, "REP received.\n");
1116		ret = ipoib_cm_rep_handler(cm_id, event);
1117		if (ret)
1118			ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
1119				       NULL, 0, NULL, 0);
1120		break;
1121	case IB_CM_REQ_ERROR:
1122	case IB_CM_REJ_RECEIVED:
1123	case IB_CM_TIMEWAIT_EXIT:
1124		ipoib_dbg(priv, "CM error %d.\n", event->event);
1125		spin_lock_irqsave(&priv->lock, flags);
1126		path = tx->path;
1127
1128		if (path) {
1129			path->cm = NULL;
1130			tx->path = NULL;
1131			rb_erase(&path->rb_node, &priv->path_tree);
1132			list_del(&path->list);
1133		}
1134
1135		if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
1136			list_move(&tx->list, &priv->cm.reap_list);
1137			queue_work(ipoib_workqueue, &priv->cm.reap_task);
1138		}
1139
1140		spin_unlock_irqrestore(&priv->lock, flags);
1141		if (path)
1142			ipoib_path_free(tx->priv, path);
1143		break;
1144	default:
1145		break;
1146	}
1147
1148	return 0;
1149}
1150
1151struct ipoib_cm_tx *ipoib_cm_create_tx(struct ipoib_dev_priv *priv,
1152    struct ipoib_path *path)
1153{
1154	struct ipoib_cm_tx *tx;
1155
1156	tx = kzalloc(sizeof *tx, GFP_ATOMIC);
1157	if (!tx)
1158		return NULL;
1159
1160	ipoib_dbg(priv, "Creating cm tx\n");
1161	path->cm = tx;
1162	tx->path = path;
1163	tx->priv = priv;
1164	list_add(&tx->list, &priv->cm.start_list);
1165	set_bit(IPOIB_FLAG_INITIALIZED, &tx->flags);
1166	queue_work(ipoib_workqueue, &priv->cm.start_task);
1167	return tx;
1168}
1169
1170void ipoib_cm_destroy_tx(struct ipoib_cm_tx *tx)
1171{
1172	struct ipoib_dev_priv *priv = tx->priv;
1173	if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
1174		spin_lock(&priv->lock);
1175		list_move(&tx->list, &priv->cm.reap_list);
1176		spin_unlock(&priv->lock);
1177		queue_work(ipoib_workqueue, &priv->cm.reap_task);
1178		ipoib_dbg(priv, "Reap connection for gid %pI6\n",
1179			  tx->path->pathrec.dgid.raw);
1180		tx->path = NULL;
1181	}
1182}
1183
1184static void ipoib_cm_tx_start(struct work_struct *work)
1185{
1186	struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1187						   cm.start_task);
1188	struct ipoib_path *path;
1189	struct ipoib_cm_tx *p;
1190	unsigned long flags;
1191	int ret;
1192
1193	struct ib_sa_path_rec pathrec;
1194	u32 qpn;
1195
1196	ipoib_dbg(priv, "cm start task\n");
1197	spin_lock_irqsave(&priv->lock, flags);
1198
1199	while (!list_empty(&priv->cm.start_list)) {
1200		p = list_entry(priv->cm.start_list.next, typeof(*p), list);
1201		list_del_init(&p->list);
1202		path = p->path;
1203		qpn = IPOIB_QPN(path->hwaddr);
1204		memcpy(&pathrec, &p->path->pathrec, sizeof pathrec);
1205
1206		spin_unlock_irqrestore(&priv->lock, flags);
1207
1208		ret = ipoib_cm_tx_init(p, qpn, &pathrec);
1209
1210		spin_lock_irqsave(&priv->lock, flags);
1211
1212		if (ret) {
1213			path = p->path;
1214			if (path) {
1215				path->cm = NULL;
1216				rb_erase(&path->rb_node, &priv->path_tree);
1217				list_del(&path->list);
1218				ipoib_path_free(priv, path);
1219			}
1220			list_del(&p->list);
1221			kfree(p);
1222		}
1223	}
1224
1225	spin_unlock_irqrestore(&priv->lock, flags);
1226}
1227
1228static void ipoib_cm_tx_reap(struct work_struct *work)
1229{
1230	struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1231						   cm.reap_task);
1232	struct ipoib_cm_tx *p;
1233	unsigned long flags;
1234
1235	spin_lock_irqsave(&priv->lock, flags);
1236
1237	while (!list_empty(&priv->cm.reap_list)) {
1238		p = list_entry(priv->cm.reap_list.next, typeof(*p), list);
1239		list_del(&p->list);
1240		spin_unlock_irqrestore(&priv->lock, flags);
1241		ipoib_cm_tx_destroy(p);
1242		spin_lock_irqsave(&priv->lock, flags);
1243	}
1244
1245	spin_unlock_irqrestore(&priv->lock, flags);
1246}
1247
1248static void ipoib_cm_mb_reap(struct work_struct *work)
1249{
1250	struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1251						   cm.mb_task);
1252	struct mbuf *mb;
1253	unsigned long flags;
1254#if defined(INET) || defined(INET6)
1255	unsigned mtu = priv->mcast_mtu;
1256#endif
1257	uint16_t proto;
1258
1259	spin_lock_irqsave(&priv->lock, flags);
1260
1261	for (;;) {
1262		IF_DEQUEUE(&priv->cm.mb_queue, mb);
1263		if (mb == NULL)
1264			break;
1265		spin_unlock_irqrestore(&priv->lock, flags);
1266
1267		proto = htons(*mtod(mb, uint16_t *));
1268		m_adj(mb, IPOIB_ENCAP_LEN);
1269		switch (proto) {
1270#if defined(INET)
1271		case ETHERTYPE_IP:
1272			icmp_error(mb, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG, 0, mtu);
1273			break;
1274#endif
1275#if defined(INET6)
1276		case ETHERTYPE_IPV6:
1277			icmp6_error(mb, ICMP6_PACKET_TOO_BIG, 0, mtu);
1278			break;
1279#endif
1280		default:
1281			m_freem(mb);
1282		}
1283
1284		spin_lock_irqsave(&priv->lock, flags);
1285	}
1286
1287	spin_unlock_irqrestore(&priv->lock, flags);
1288}
1289
1290void
1291ipoib_cm_mb_too_long(struct ipoib_dev_priv *priv, struct mbuf *mb, unsigned int mtu)
1292{
1293	int e = priv->cm.mb_queue.ifq_len;
1294
1295	IF_ENQUEUE(&priv->cm.mb_queue, mb);
1296	if (e == 0)
1297		queue_work(ipoib_workqueue, &priv->cm.mb_task);
1298}
1299
1300static void ipoib_cm_rx_reap(struct work_struct *work)
1301{
1302	ipoib_cm_free_rx_reap_list(container_of(work, struct ipoib_dev_priv,
1303						cm.rx_reap_task));
1304}
1305
1306static void ipoib_cm_stale_task(struct work_struct *work)
1307{
1308	struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1309						   cm.stale_task.work);
1310	struct ipoib_cm_rx *p;
1311	int ret;
1312
1313	spin_lock_irq(&priv->lock);
1314	while (!list_empty(&priv->cm.passive_ids)) {
1315		/* List is sorted by LRU, start from tail,
1316		 * stop when we see a recently used entry */
1317		p = list_entry(priv->cm.passive_ids.prev, typeof(*p), list);
1318		if (time_before_eq(jiffies, p->jiffies + IPOIB_CM_RX_TIMEOUT))
1319			break;
1320		list_move(&p->list, &priv->cm.rx_error_list);
1321		p->state = IPOIB_CM_RX_ERROR;
1322		spin_unlock_irq(&priv->lock);
1323		ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
1324		if (ret)
1325			ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
1326		spin_lock_irq(&priv->lock);
1327	}
1328
1329	if (!list_empty(&priv->cm.passive_ids))
1330		queue_delayed_work(ipoib_workqueue,
1331				   &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
1332	spin_unlock_irq(&priv->lock);
1333}
1334
1335
1336static void ipoib_cm_create_srq(struct ipoib_dev_priv *priv, int max_sge)
1337{
1338	struct ib_srq_init_attr srq_init_attr = {
1339		.attr = {
1340			.max_wr  = ipoib_recvq_size,
1341			.max_sge = max_sge
1342		}
1343	};
1344
1345	priv->cm.srq = ib_create_srq(priv->pd, &srq_init_attr);
1346	if (IS_ERR(priv->cm.srq)) {
1347		if (PTR_ERR(priv->cm.srq) != -ENOSYS)
1348			printk(KERN_WARNING "%s: failed to allocate SRQ, error %ld\n",
1349			       priv->ca->name, PTR_ERR(priv->cm.srq));
1350		priv->cm.srq = NULL;
1351		return;
1352	}
1353
1354	priv->cm.srq_ring = kzalloc(ipoib_recvq_size * sizeof *priv->cm.srq_ring, GFP_KERNEL);
1355	if (!priv->cm.srq_ring) {
1356		printk(KERN_WARNING "%s: failed to allocate CM SRQ ring (%d entries)\n",
1357		       priv->ca->name, ipoib_recvq_size);
1358		ib_destroy_srq(priv->cm.srq);
1359		priv->cm.srq = NULL;
1360		return;
1361	}
1362
1363	memset(priv->cm.srq_ring, 0, ipoib_recvq_size * sizeof *priv->cm.srq_ring);
1364}
1365
1366int ipoib_cm_dev_init(struct ipoib_dev_priv *priv)
1367{
1368	struct ifnet *dev = priv->dev;
1369	int i;
1370	int max_srq_sge;
1371
1372	INIT_LIST_HEAD(&priv->cm.passive_ids);
1373	INIT_LIST_HEAD(&priv->cm.reap_list);
1374	INIT_LIST_HEAD(&priv->cm.start_list);
1375	INIT_LIST_HEAD(&priv->cm.rx_error_list);
1376	INIT_LIST_HEAD(&priv->cm.rx_flush_list);
1377	INIT_LIST_HEAD(&priv->cm.rx_drain_list);
1378	INIT_LIST_HEAD(&priv->cm.rx_reap_list);
1379	INIT_WORK(&priv->cm.start_task, ipoib_cm_tx_start);
1380	INIT_WORK(&priv->cm.reap_task, ipoib_cm_tx_reap);
1381	INIT_WORK(&priv->cm.mb_task, ipoib_cm_mb_reap);
1382	INIT_WORK(&priv->cm.rx_reap_task, ipoib_cm_rx_reap);
1383	INIT_DELAYED_WORK(&priv->cm.stale_task, ipoib_cm_stale_task);
1384
1385	bzero(&priv->cm.mb_queue, sizeof(priv->cm.mb_queue));
1386	mtx_init(&priv->cm.mb_queue.ifq_mtx,
1387	    dev->if_xname, "if send queue", MTX_DEF);
1388
1389	max_srq_sge = priv->ca->attrs.max_srq_sge;
1390
1391	ipoib_dbg(priv, "max_srq_sge=%d\n", max_srq_sge);
1392
1393	max_srq_sge = min_t(int, IPOIB_CM_RX_SG, max_srq_sge);
1394	ipoib_cm_create_srq(priv, max_srq_sge);
1395	if (ipoib_cm_has_srq(priv)) {
1396		priv->cm.max_cm_mtu = max_srq_sge * MJUMPAGESIZE;
1397		priv->cm.num_frags  = 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