blob: bb1004114dec09c6f3b6df7c9dab66d1f09f5bae [file] [log] [blame]
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001/*
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.
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020031 */
32
33#include <rdma/ib_cm.h>
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020034#include <net/dst.h>
35#include <net/icmp.h>
36#include <linux/icmpv6.h>
Michael S. Tsirkin518b1642007-05-21 15:04:59 +030037#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Roland Dreier10313cb2008-03-12 07:51:03 -070039#include <linux/vmalloc.h>
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020040
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -080041#include "ipoib.h"
42
43int ipoib_max_conn_qp = 128;
44
45module_param_named(max_nonsrq_conn_qp, ipoib_max_conn_qp, int, 0444);
46MODULE_PARM_DESC(max_nonsrq_conn_qp,
47 "Max number of connected-mode QPs per interface "
48 "(applied only if shared receive queue is not available)");
49
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020050#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
51static int data_debug_level;
52
53module_param_named(cm_data_debug_level, data_debug_level, int, 0644);
54MODULE_PARM_DESC(cm_data_debug_level,
55 "Enable data path debug tracing for connected mode if > 0");
56#endif
57
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020058#define IPOIB_CM_IETF_ID 0x1000000000000000ULL
59
60#define IPOIB_CM_RX_UPDATE_TIME (256 * HZ)
61#define IPOIB_CM_RX_TIMEOUT (2 * 256 * HZ)
62#define IPOIB_CM_RX_DELAY (3 * 256 * HZ)
63#define IPOIB_CM_RX_UPDATE_MASK (0x3)
64
Michael S. Tsirkin518b1642007-05-21 15:04:59 +030065static struct ib_qp_attr ipoib_cm_err_attr = {
66 .qp_state = IB_QPS_ERR
67};
68
Roland Dreier09f60f82007-10-26 13:44:25 -070069#define IPOIB_CM_RX_DRAIN_WRID 0xffffffff
Michael S. Tsirkin518b1642007-05-21 15:04:59 +030070
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +030071static struct ib_send_wr ipoib_cm_rx_drain_wr = {
72 .wr_id = IPOIB_CM_RX_DRAIN_WRID,
73 .opcode = IB_WR_SEND,
Michael S. Tsirkin518b1642007-05-21 15:04:59 +030074};
75
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020076static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
77 struct ib_cm_event *event);
78
Michael S. Tsirkin18120632007-02-20 20:17:55 +020079static void ipoib_cm_dma_unmap_rx(struct ipoib_dev_priv *priv, int frags,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020080 u64 mapping[IPOIB_CM_RX_SG])
81{
82 int i;
83
84 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
85
Michael S. Tsirkin18120632007-02-20 20:17:55 +020086 for (i = 0; i < frags; ++i)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020087 ib_dma_unmap_single(priv->ca, mapping[i + 1], PAGE_SIZE, DMA_FROM_DEVICE);
88}
89
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -080090static int ipoib_cm_post_receive_srq(struct net_device *dev, int id)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020091{
92 struct ipoib_dev_priv *priv = netdev_priv(dev);
93 struct ib_recv_wr *bad_wr;
94 int i, ret;
95
Michael S. Tsirkin1b524962007-08-16 15:36:16 +030096 priv->cm.rx_wr.wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020097
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -080098 for (i = 0; i < priv->cm.num_frags; ++i)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020099 priv->cm.rx_sge[i].addr = priv->cm.srq_ring[id].mapping[i];
100
101 ret = ib_post_srq_recv(priv->cm.srq, &priv->cm.rx_wr, &bad_wr);
102 if (unlikely(ret)) {
103 ipoib_warn(priv, "post srq failed for buf %d (%d)\n", id, ret);
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800104 ipoib_cm_dma_unmap_rx(priv, priv->cm.num_frags - 1,
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200105 priv->cm.srq_ring[id].mapping);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200106 dev_kfree_skb_any(priv->cm.srq_ring[id].skb);
107 priv->cm.srq_ring[id].skb = NULL;
108 }
109
110 return ret;
111}
112
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800113static int ipoib_cm_post_receive_nonsrq(struct net_device *dev,
Roland Dreiera7d834c2008-07-14 23:48:47 -0700114 struct ipoib_cm_rx *rx,
115 struct ib_recv_wr *wr,
116 struct ib_sge *sge, int id)
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800117{
118 struct ipoib_dev_priv *priv = netdev_priv(dev);
119 struct ib_recv_wr *bad_wr;
120 int i, ret;
121
Roland Dreiera7d834c2008-07-14 23:48:47 -0700122 wr->wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800123
124 for (i = 0; i < IPOIB_CM_RX_SG; ++i)
Roland Dreiera7d834c2008-07-14 23:48:47 -0700125 sge[i].addr = rx->rx_ring[id].mapping[i];
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800126
Roland Dreiera7d834c2008-07-14 23:48:47 -0700127 ret = ib_post_recv(rx->qp, wr, &bad_wr);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800128 if (unlikely(ret)) {
129 ipoib_warn(priv, "post recv failed for buf %d (%d)\n", id, ret);
130 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
131 rx->rx_ring[id].mapping);
132 dev_kfree_skb_any(rx->rx_ring[id].skb);
133 rx->rx_ring[id].skb = NULL;
134 }
135
136 return ret;
137}
138
139static struct sk_buff *ipoib_cm_alloc_rx_skb(struct net_device *dev,
140 struct ipoib_cm_rx_buf *rx_ring,
141 int id, int frags,
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200142 u64 mapping[IPOIB_CM_RX_SG])
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200143{
144 struct ipoib_dev_priv *priv = netdev_priv(dev);
145 struct sk_buff *skb;
146 int i;
147
148 skb = dev_alloc_skb(IPOIB_CM_HEAD_SIZE + 12);
149 if (unlikely(!skb))
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200150 return NULL;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200151
152 /*
153 * IPoIB adds a 4 byte header. So we need 12 more bytes to align the
154 * IP header to a multiple of 16.
155 */
156 skb_reserve(skb, 12);
157
158 mapping[0] = ib_dma_map_single(priv->ca, skb->data, IPOIB_CM_HEAD_SIZE,
159 DMA_FROM_DEVICE);
160 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0]))) {
161 dev_kfree_skb_any(skb);
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200162 return NULL;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200163 }
164
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200165 for (i = 0; i < frags; i++) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200166 struct page *page = alloc_page(GFP_ATOMIC);
167
168 if (!page)
169 goto partial_error;
170 skb_fill_page_desc(skb, i, page, 0, PAGE_SIZE);
171
172 mapping[i + 1] = ib_dma_map_page(priv->ca, skb_shinfo(skb)->frags[i].page,
Michael S. Tsirkin6371ea32007-04-10 18:32:42 +0300173 0, PAGE_SIZE, DMA_FROM_DEVICE);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200174 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[i + 1])))
175 goto partial_error;
176 }
177
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800178 rx_ring[id].skb = skb;
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200179 return skb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200180
181partial_error:
182
183 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
184
Ralph Campbell841adfc2007-06-29 11:37:56 -0700185 for (; i > 0; --i)
186 ib_dma_unmap_single(priv->ca, mapping[i], PAGE_SIZE, DMA_FROM_DEVICE);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200187
Michael S. Tsirkin8a2e65f2007-02-16 00:16:13 +0200188 dev_kfree_skb_any(skb);
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200189 return NULL;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200190}
191
Roland Dreier1efb6142008-01-25 14:15:24 -0800192static void ipoib_cm_free_rx_ring(struct net_device *dev,
193 struct ipoib_cm_rx_buf *rx_ring)
194{
195 struct ipoib_dev_priv *priv = netdev_priv(dev);
196 int i;
197
198 for (i = 0; i < ipoib_recvq_size; ++i)
199 if (rx_ring[i].skb) {
200 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
201 rx_ring[i].mapping);
202 dev_kfree_skb_any(rx_ring[i].skb);
203 }
204
David J. Wilderb1404062008-08-08 15:51:29 -0700205 vfree(rx_ring);
Roland Dreier1efb6142008-01-25 14:15:24 -0800206}
207
Roland Dreier2337f802007-10-23 19:57:54 -0700208static void ipoib_cm_start_rx_drain(struct ipoib_dev_priv *priv)
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300209{
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300210 struct ib_send_wr *bad_wr;
211 struct ipoib_cm_rx *p;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300212
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300213 /* We only reserved 1 extra slot in CQ for drain WRs, so
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300214 * make sure we have at most 1 outstanding WR. */
215 if (list_empty(&priv->cm.rx_flush_list) ||
216 !list_empty(&priv->cm.rx_drain_list))
217 return;
218
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300219 /*
220 * QPs on flush list are error state. This way, a "flush
221 * error" WC will be immediately generated for each WR we post.
222 */
223 p = list_entry(priv->cm.rx_flush_list.next, typeof(*p), list);
224 if (ib_post_send(p->qp, &ipoib_cm_rx_drain_wr, &bad_wr))
225 ipoib_warn(priv, "failed to post drain wr\n");
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300226
227 list_splice_init(&priv->cm.rx_flush_list, &priv->cm.rx_drain_list);
228}
229
230static void ipoib_cm_rx_event_handler(struct ib_event *event, void *ctx)
231{
232 struct ipoib_cm_rx *p = ctx;
233 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
234 unsigned long flags;
235
236 if (event->event != IB_EVENT_QP_LAST_WQE_REACHED)
237 return;
238
239 spin_lock_irqsave(&priv->lock, flags);
240 list_move(&p->list, &priv->cm.rx_flush_list);
241 p->state = IPOIB_CM_RX_FLUSH;
242 ipoib_cm_start_rx_drain(priv);
243 spin_unlock_irqrestore(&priv->lock, flags);
244}
245
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200246static struct ib_qp *ipoib_cm_create_rx_qp(struct net_device *dev,
247 struct ipoib_cm_rx *p)
248{
249 struct ipoib_dev_priv *priv = netdev_priv(dev);
250 struct ib_qp_init_attr attr = {
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300251 .event_handler = ipoib_cm_rx_event_handler,
Eli Cohenf56bcd82008-04-29 13:46:53 -0700252 .send_cq = priv->recv_cq, /* For drain WR */
253 .recv_cq = priv->recv_cq,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200254 .srq = priv->cm.srq,
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300255 .cap.max_send_wr = 1, /* For drain WR */
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200256 .cap.max_send_sge = 1, /* FIXME: 0 Seems not to work */
257 .sq_sig_type = IB_SIGNAL_ALL_WR,
258 .qp_type = IB_QPT_RC,
259 .qp_context = p,
260 };
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800261
262 if (!ipoib_cm_has_srq(dev)) {
263 attr.cap.max_recv_wr = ipoib_recvq_size;
264 attr.cap.max_recv_sge = IPOIB_CM_RX_SG;
265 }
266
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200267 return ib_create_qp(priv->pd, &attr);
268}
269
270static int ipoib_cm_modify_rx_qp(struct net_device *dev,
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800271 struct ib_cm_id *cm_id, struct ib_qp *qp,
272 unsigned psn)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200273{
274 struct ipoib_dev_priv *priv = netdev_priv(dev);
275 struct ib_qp_attr qp_attr;
276 int qp_attr_mask, ret;
277
278 qp_attr.qp_state = IB_QPS_INIT;
279 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
280 if (ret) {
281 ipoib_warn(priv, "failed to init QP attr for INIT: %d\n", ret);
282 return ret;
283 }
284 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
285 if (ret) {
286 ipoib_warn(priv, "failed to modify QP to INIT: %d\n", ret);
287 return ret;
288 }
289 qp_attr.qp_state = IB_QPS_RTR;
290 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
291 if (ret) {
292 ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
293 return ret;
294 }
295 qp_attr.rq_psn = psn;
296 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
297 if (ret) {
298 ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
299 return ret;
300 }
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300301
302 /*
303 * Current Mellanox HCA firmware won't generate completions
304 * with error for drain WRs unless the QP has been moved to
305 * RTS first. This work-around leaves a window where a QP has
306 * moved to error asynchronously, but this will eventually get
307 * fixed in firmware, so let's not error out if modify QP
308 * fails.
309 */
310 qp_attr.qp_state = IB_QPS_RTS;
311 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
312 if (ret) {
313 ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
314 return 0;
315 }
316 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
317 if (ret) {
318 ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
319 return 0;
320 }
321
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200322 return 0;
323}
324
Roland Dreiera7d834c2008-07-14 23:48:47 -0700325static void ipoib_cm_init_rx_wr(struct net_device *dev,
326 struct ib_recv_wr *wr,
327 struct ib_sge *sge)
328{
329 struct ipoib_dev_priv *priv = netdev_priv(dev);
330 int i;
331
332 for (i = 0; i < priv->cm.num_frags; ++i)
333 sge[i].lkey = priv->mr->lkey;
334
335 sge[0].length = IPOIB_CM_HEAD_SIZE;
336 for (i = 1; i < priv->cm.num_frags; ++i)
337 sge[i].length = PAGE_SIZE;
338
339 wr->next = NULL;
Roland Dreiere0819812008-07-30 07:21:46 -0700340 wr->sg_list = sge;
Roland Dreiera7d834c2008-07-14 23:48:47 -0700341 wr->num_sge = priv->cm.num_frags;
342}
343
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800344static int ipoib_cm_nonsrq_init_rx(struct net_device *dev, struct ib_cm_id *cm_id,
345 struct ipoib_cm_rx *rx)
346{
347 struct ipoib_dev_priv *priv = netdev_priv(dev);
Roland Dreiera7d834c2008-07-14 23:48:47 -0700348 struct {
349 struct ib_recv_wr wr;
350 struct ib_sge sge[IPOIB_CM_RX_SG];
351 } *t;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800352 int ret;
353 int i;
354
David J. Wilderb1404062008-08-08 15:51:29 -0700355 rx->rx_ring = vmalloc(ipoib_recvq_size * sizeof *rx->rx_ring);
356 if (!rx->rx_ring) {
357 printk(KERN_WARNING "%s: failed to allocate CM non-SRQ ring (%d entries)\n",
358 priv->ca->name, ipoib_recvq_size);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800359 return -ENOMEM;
David J. Wilderb1404062008-08-08 15:51:29 -0700360 }
361
362 memset(rx->rx_ring, 0, ipoib_recvq_size * sizeof *rx->rx_ring);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800363
Roland Dreiera7d834c2008-07-14 23:48:47 -0700364 t = kmalloc(sizeof *t, GFP_KERNEL);
365 if (!t) {
366 ret = -ENOMEM;
367 goto err_free;
368 }
369
370 ipoib_cm_init_rx_wr(dev, &t->wr, t->sge);
371
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800372 spin_lock_irq(&priv->lock);
373
374 if (priv->cm.nonsrq_conn_qp >= ipoib_max_conn_qp) {
375 spin_unlock_irq(&priv->lock);
376 ib_send_cm_rej(cm_id, IB_CM_REJ_NO_QP, NULL, 0, NULL, 0);
377 ret = -EINVAL;
378 goto err_free;
379 } else
380 ++priv->cm.nonsrq_conn_qp;
381
382 spin_unlock_irq(&priv->lock);
383
384 for (i = 0; i < ipoib_recvq_size; ++i) {
385 if (!ipoib_cm_alloc_rx_skb(dev, rx->rx_ring, i, IPOIB_CM_RX_SG - 1,
386 rx->rx_ring[i].mapping)) {
387 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
388 ret = -ENOMEM;
389 goto err_count;
Roland Dreiera7d834c2008-07-14 23:48:47 -0700390 }
391 ret = ipoib_cm_post_receive_nonsrq(dev, rx, &t->wr, t->sge, i);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800392 if (ret) {
393 ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq "
394 "failed for buf %d\n", i);
395 ret = -EIO;
396 goto err_count;
397 }
398 }
399
400 rx->recv_count = ipoib_recvq_size;
401
Roland Dreiera7d834c2008-07-14 23:48:47 -0700402 kfree(t);
403
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800404 return 0;
405
406err_count:
407 spin_lock_irq(&priv->lock);
408 --priv->cm.nonsrq_conn_qp;
409 spin_unlock_irq(&priv->lock);
410
411err_free:
Roland Dreiera7d834c2008-07-14 23:48:47 -0700412 kfree(t);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800413 ipoib_cm_free_rx_ring(dev, rx->rx_ring);
414
415 return ret;
416}
417
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200418static int ipoib_cm_send_rep(struct net_device *dev, struct ib_cm_id *cm_id,
419 struct ib_qp *qp, struct ib_cm_req_event_param *req,
420 unsigned psn)
421{
422 struct ipoib_dev_priv *priv = netdev_priv(dev);
423 struct ipoib_cm_data data = {};
424 struct ib_cm_rep_param rep = {};
425
426 data.qpn = cpu_to_be32(priv->qp->qp_num);
427 data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
428
429 rep.private_data = &data;
430 rep.private_data_len = sizeof data;
431 rep.flow_control = 0;
432 rep.rnr_retry_count = req->rnr_retry_count;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800433 rep.srq = ipoib_cm_has_srq(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200434 rep.qp_num = qp->qp_num;
435 rep.starting_psn = psn;
436 return ib_send_cm_rep(cm_id, &rep);
437}
438
439static int ipoib_cm_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
440{
441 struct net_device *dev = cm_id->context;
442 struct ipoib_dev_priv *priv = netdev_priv(dev);
443 struct ipoib_cm_rx *p;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200444 unsigned psn;
445 int ret;
446
447 ipoib_dbg(priv, "REQ arrived\n");
448 p = kzalloc(sizeof *p, GFP_KERNEL);
449 if (!p)
450 return -ENOMEM;
451 p->dev = dev;
452 p->id = cm_id;
Michael S. Tsirkin3ec73932007-06-19 13:40:41 +0300453 cm_id->context = p;
454 p->state = IPOIB_CM_RX_LIVE;
455 p->jiffies = jiffies;
456 INIT_LIST_HEAD(&p->list);
457
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200458 p->qp = ipoib_cm_create_rx_qp(dev, p);
459 if (IS_ERR(p->qp)) {
460 ret = PTR_ERR(p->qp);
461 goto err_qp;
462 }
463
464 psn = random32() & 0xffffff;
465 ret = ipoib_cm_modify_rx_qp(dev, cm_id, p->qp, psn);
466 if (ret)
467 goto err_modify;
468
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800469 if (!ipoib_cm_has_srq(dev)) {
470 ret = ipoib_cm_nonsrq_init_rx(dev, cm_id, p);
471 if (ret)
472 goto err_modify;
473 }
474
Michael S. Tsirkin3ec73932007-06-19 13:40:41 +0300475 spin_lock_irq(&priv->lock);
476 queue_delayed_work(ipoib_workqueue,
477 &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
478 /* Add this entry to passive ids list head, but do not re-add it
479 * if IB_EVENT_QP_LAST_WQE_REACHED has moved it to flush list. */
480 p->jiffies = jiffies;
481 if (p->state == IPOIB_CM_RX_LIVE)
482 list_move(&p->list, &priv->cm.passive_ids);
483 spin_unlock_irq(&priv->lock);
484
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200485 ret = ipoib_cm_send_rep(dev, cm_id, p->qp, &event->param.req_rcvd, psn);
486 if (ret) {
487 ipoib_warn(priv, "failed to send REP: %d\n", ret);
Michael S. Tsirkin3ec73932007-06-19 13:40:41 +0300488 if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE))
489 ipoib_warn(priv, "unable to move qp to error state\n");
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200490 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200491 return 0;
492
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200493err_modify:
494 ib_destroy_qp(p->qp);
495err_qp:
496 kfree(p);
497 return ret;
498}
499
500static int ipoib_cm_rx_handler(struct ib_cm_id *cm_id,
501 struct ib_cm_event *event)
502{
503 struct ipoib_cm_rx *p;
504 struct ipoib_dev_priv *priv;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200505
506 switch (event->event) {
507 case IB_CM_REQ_RECEIVED:
508 return ipoib_cm_req_handler(cm_id, event);
509 case IB_CM_DREQ_RECEIVED:
510 p = cm_id->context;
511 ib_send_cm_drep(cm_id, NULL, 0);
512 /* Fall through */
513 case IB_CM_REJ_RECEIVED:
514 p = cm_id->context;
515 priv = netdev_priv(p->dev);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300516 if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE))
517 ipoib_warn(priv, "unable to move qp to error state\n");
518 /* Fall through */
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200519 default:
520 return 0;
521 }
522}
523/* Adjust length of skb with fragments to match received data */
524static void skb_put_frags(struct sk_buff *skb, unsigned int hdr_space,
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200525 unsigned int length, struct sk_buff *toskb)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200526{
527 int i, num_frags;
528 unsigned int size;
529
530 /* put header into skb */
531 size = min(length, hdr_space);
532 skb->tail += size;
533 skb->len += size;
534 length -= size;
535
536 num_frags = skb_shinfo(skb)->nr_frags;
537 for (i = 0; i < num_frags; i++) {
538 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
539
540 if (length == 0) {
541 /* don't need this page */
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200542 skb_fill_page_desc(toskb, i, frag->page, 0, PAGE_SIZE);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200543 --skb_shinfo(skb)->nr_frags;
544 } else {
545 size = min(length, (unsigned) PAGE_SIZE);
546
547 frag->size = size;
548 skb->data_len += size;
549 skb->truesize += size;
550 skb->len += size;
551 length -= size;
552 }
553 }
554}
555
556void ipoib_cm_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
557{
558 struct ipoib_dev_priv *priv = netdev_priv(dev);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800559 struct ipoib_cm_rx_buf *rx_ring;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300560 unsigned int wr_id = wc->wr_id & ~(IPOIB_OP_CM | IPOIB_OP_RECV);
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200561 struct sk_buff *skb, *newskb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200562 struct ipoib_cm_rx *p;
563 unsigned long flags;
564 u64 mapping[IPOIB_CM_RX_SG];
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200565 int frags;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800566 int has_srq;
Eli Cohenf89271da2008-07-14 23:48:44 -0700567 struct sk_buff *small_skb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200568
Roland Dreiera89875f2007-04-18 20:20:53 -0700569 ipoib_dbg_data(priv, "cm recv completion: id %d, status: %d\n",
570 wr_id, wc->status);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200571
572 if (unlikely(wr_id >= ipoib_recvq_size)) {
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300573 if (wr_id == (IPOIB_CM_RX_DRAIN_WRID & ~(IPOIB_OP_CM | IPOIB_OP_RECV))) {
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300574 spin_lock_irqsave(&priv->lock, flags);
575 list_splice_init(&priv->cm.rx_drain_list, &priv->cm.rx_reap_list);
576 ipoib_cm_start_rx_drain(priv);
577 queue_work(ipoib_workqueue, &priv->cm.rx_reap_task);
578 spin_unlock_irqrestore(&priv->lock, flags);
579 } else
580 ipoib_warn(priv, "cm recv completion event with wrid %d (> %d)\n",
581 wr_id, ipoib_recvq_size);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200582 return;
583 }
584
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800585 p = wc->qp->qp_context;
586
587 has_srq = ipoib_cm_has_srq(dev);
588 rx_ring = has_srq ? priv->cm.srq_ring : p->rx_ring;
589
590 skb = rx_ring[wr_id].skb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200591
592 if (unlikely(wc->status != IB_WC_SUCCESS)) {
593 ipoib_dbg(priv, "cm recv error "
594 "(status=%d, wrid=%d vend_err %x)\n",
595 wc->status, wr_id, wc->vendor_err);
Roland Dreierde903512007-09-28 15:33:51 -0700596 ++dev->stats.rx_dropped;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800597 if (has_srq)
598 goto repost;
599 else {
600 if (!--p->recv_count) {
601 spin_lock_irqsave(&priv->lock, flags);
602 list_move(&p->list, &priv->cm.rx_reap_list);
603 spin_unlock_irqrestore(&priv->lock, flags);
604 queue_work(ipoib_workqueue, &priv->cm.rx_reap_task);
605 }
606 return;
607 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200608 }
609
Roland Dreierfd312562007-10-17 21:54:44 -0700610 if (unlikely(!(wr_id & IPOIB_CM_RX_UPDATE_MASK))) {
Michael S. Tsirkind6ef7d62007-05-02 15:31:12 +0300611 if (p && time_after_eq(jiffies, p->jiffies + IPOIB_CM_RX_UPDATE_TIME)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200612 spin_lock_irqsave(&priv->lock, flags);
613 p->jiffies = jiffies;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300614 /* Move this entry to list head, but do not re-add it
615 * if it has been moved out of list. */
616 if (p->state == IPOIB_CM_RX_LIVE)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200617 list_move(&p->list, &priv->cm.passive_ids);
618 spin_unlock_irqrestore(&priv->lock, flags);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200619 }
620 }
621
Eli Cohenf89271da2008-07-14 23:48:44 -0700622 if (wc->byte_len < IPOIB_CM_COPYBREAK) {
623 int dlen = wc->byte_len;
624
625 small_skb = dev_alloc_skb(dlen + 12);
626 if (small_skb) {
627 skb_reserve(small_skb, 12);
628 ib_dma_sync_single_for_cpu(priv->ca, rx_ring[wr_id].mapping[0],
629 dlen, DMA_FROM_DEVICE);
630 skb_copy_from_linear_data(skb, small_skb->data, dlen);
631 ib_dma_sync_single_for_device(priv->ca, rx_ring[wr_id].mapping[0],
632 dlen, DMA_FROM_DEVICE);
633 skb_put(small_skb, dlen);
634 skb = small_skb;
635 goto copied;
636 }
637 }
638
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200639 frags = PAGE_ALIGN(wc->byte_len - min(wc->byte_len,
640 (unsigned)IPOIB_CM_HEAD_SIZE)) / PAGE_SIZE;
641
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800642 newskb = ipoib_cm_alloc_rx_skb(dev, rx_ring, wr_id, frags, mapping);
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200643 if (unlikely(!newskb)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200644 /*
645 * If we can't allocate a new RX buffer, dump
646 * this packet and reuse the old buffer.
647 */
648 ipoib_dbg(priv, "failed to allocate receive buffer %d\n", wr_id);
Roland Dreierde903512007-09-28 15:33:51 -0700649 ++dev->stats.rx_dropped;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200650 goto repost;
651 }
652
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800653 ipoib_cm_dma_unmap_rx(priv, frags, rx_ring[wr_id].mapping);
654 memcpy(rx_ring[wr_id].mapping, mapping, (frags + 1) * sizeof *mapping);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200655
656 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
657 wc->byte_len, wc->slid);
658
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200659 skb_put_frags(skb, IPOIB_CM_HEAD_SIZE, wc->byte_len, newskb);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200660
Eli Cohenf89271da2008-07-14 23:48:44 -0700661copied:
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200662 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700663 skb_reset_mac_header(skb);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200664 skb_pull(skb, IPOIB_ENCAP_LEN);
665
Roland Dreierde903512007-09-28 15:33:51 -0700666 ++dev->stats.rx_packets;
667 dev->stats.rx_bytes += skb->len;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200668
669 skb->dev = dev;
670 /* XXX get correct PACKET_ type here */
671 skb->pkt_type = PACKET_HOST;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700672 netif_receive_skb(skb);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200673
674repost:
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800675 if (has_srq) {
676 if (unlikely(ipoib_cm_post_receive_srq(dev, wr_id)))
677 ipoib_warn(priv, "ipoib_cm_post_receive_srq failed "
678 "for buf %d\n", wr_id);
679 } else {
Roland Dreiera7d834c2008-07-14 23:48:47 -0700680 if (unlikely(ipoib_cm_post_receive_nonsrq(dev, p,
681 &priv->cm.rx_wr,
682 priv->cm.rx_sge,
683 wr_id))) {
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800684 --p->recv_count;
685 ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq failed "
686 "for buf %d\n", wr_id);
687 }
688 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200689}
690
691static inline int post_send(struct ipoib_dev_priv *priv,
692 struct ipoib_cm_tx *tx,
693 unsigned int wr_id,
694 u64 addr, int len)
695{
696 struct ib_send_wr *bad_wr;
697
Eli Cohen71437402008-01-30 18:30:53 +0200698 priv->tx_sge[0].addr = addr;
699 priv->tx_sge[0].length = len;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200700
Roland Dreier42004062008-03-11 18:35:20 -0700701 priv->tx_wr.num_sge = 1;
Roland Dreier2337f802007-10-23 19:57:54 -0700702 priv->tx_wr.wr_id = wr_id | IPOIB_OP_CM;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200703
704 return ib_post_send(tx->qp, &priv->tx_wr, &bad_wr);
705}
706
707void ipoib_cm_send(struct net_device *dev, struct sk_buff *skb, struct ipoib_cm_tx *tx)
708{
709 struct ipoib_dev_priv *priv = netdev_priv(dev);
Eli Cohene1123732008-07-14 23:48:52 -0700710 struct ipoib_cm_tx_buf *tx_req;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200711 u64 addr;
Or Gerlitza48f5092010-03-04 13:17:37 +0000712 int rc;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200713
714 if (unlikely(skb->len > tx->mtu)) {
715 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
716 skb->len, tx->mtu);
Roland Dreierde903512007-09-28 15:33:51 -0700717 ++dev->stats.tx_dropped;
718 ++dev->stats.tx_errors;
Michael S. Tsirkin77d8e1e2007-03-21 15:45:05 +0200719 ipoib_cm_skb_too_long(dev, skb, tx->mtu - IPOIB_ENCAP_LEN);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200720 return;
721 }
722
723 ipoib_dbg_data(priv, "sending packet: head 0x%x length %d connection 0x%x\n",
724 tx->tx_head, skb->len, tx->qp->qp_num);
725
726 /*
727 * We put the skb into the tx_ring _before_ we call post_send()
728 * because it's entirely possible that the completion handler will
729 * run before we execute anything after the post_send(). That
730 * means we have to make sure everything is properly recorded and
731 * our state is consistent before we call post_send().
732 */
733 tx_req = &tx->tx_ring[tx->tx_head & (ipoib_sendq_size - 1)];
734 tx_req->skb = skb;
735 addr = ib_dma_map_single(priv->ca, skb->data, skb->len, DMA_TO_DEVICE);
736 if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
Roland Dreierde903512007-09-28 15:33:51 -0700737 ++dev->stats.tx_errors;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200738 dev_kfree_skb_any(skb);
739 return;
740 }
741
Eli Cohene1123732008-07-14 23:48:52 -0700742 tx_req->mapping = addr;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200743
Or Gerlitza48f5092010-03-04 13:17:37 +0000744 rc = post_send(priv, tx, tx->tx_head & (ipoib_sendq_size - 1),
745 addr, skb->len);
746 if (unlikely(rc)) {
747 ipoib_warn(priv, "post_send failed, error %d\n", rc);
Roland Dreierde903512007-09-28 15:33:51 -0700748 ++dev->stats.tx_errors;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200749 ib_dma_unmap_single(priv->ca, addr, skb->len, DMA_TO_DEVICE);
750 dev_kfree_skb_any(skb);
751 } else {
752 dev->trans_start = jiffies;
753 ++tx->tx_head;
754
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300755 if (++priv->tx_outstanding == ipoib_sendq_size) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200756 ipoib_dbg(priv, "TX ring 0x%x full, stopping kernel net queue\n",
757 tx->qp->qp_num);
Eli Cohenf0dc1172010-03-03 12:27:52 +0000758 if (ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP))
759 ipoib_warn(priv, "request notify on send CQ failed\n");
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200760 netif_stop_queue(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200761 }
762 }
763}
764
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300765void ipoib_cm_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200766{
767 struct ipoib_dev_priv *priv = netdev_priv(dev);
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300768 struct ipoib_cm_tx *tx = wc->qp->qp_context;
769 unsigned int wr_id = wc->wr_id & ~IPOIB_OP_CM;
Eli Cohene1123732008-07-14 23:48:52 -0700770 struct ipoib_cm_tx_buf *tx_req;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200771 unsigned long flags;
772
Roland Dreiera89875f2007-04-18 20:20:53 -0700773 ipoib_dbg_data(priv, "cm send completion: id %d, status: %d\n",
774 wr_id, wc->status);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200775
776 if (unlikely(wr_id >= ipoib_sendq_size)) {
777 ipoib_warn(priv, "cm send completion event with wrid %d (> %d)\n",
778 wr_id, ipoib_sendq_size);
779 return;
780 }
781
782 tx_req = &tx->tx_ring[wr_id];
783
Eli Cohene1123732008-07-14 23:48:52 -0700784 ib_dma_unmap_single(priv->ca, tx_req->mapping, tx_req->skb->len, DMA_TO_DEVICE);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200785
786 /* FIXME: is this right? Shouldn't we only increment on success? */
Roland Dreierde903512007-09-28 15:33:51 -0700787 ++dev->stats.tx_packets;
788 dev->stats.tx_bytes += tx_req->skb->len;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200789
790 dev_kfree_skb_any(tx_req->skb);
791
Roland Dreier943c2462008-09-30 10:36:21 -0700792 netif_tx_lock(dev);
793
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200794 ++tx->tx_tail;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300795 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
796 netif_queue_stopped(dev) &&
797 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200798 netif_wake_queue(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200799
800 if (wc->status != IB_WC_SUCCESS &&
801 wc->status != IB_WC_WR_FLUSH_ERR) {
802 struct ipoib_neigh *neigh;
803
804 ipoib_dbg(priv, "failed cm send event "
805 "(status=%d, wrid=%d vend_err %x)\n",
806 wc->status, wr_id, wc->vendor_err);
807
Roland Dreier943c2462008-09-30 10:36:21 -0700808 spin_lock_irqsave(&priv->lock, flags);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200809 neigh = tx->neigh;
810
811 if (neigh) {
812 neigh->cm = NULL;
813 list_del(&neigh->list);
814 if (neigh->ah)
815 ipoib_put_ah(neigh->ah);
816 ipoib_neigh_free(dev, neigh);
817
818 tx->neigh = NULL;
819 }
820
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200821 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
822 list_move(&tx->list, &priv->cm.reap_list);
823 queue_work(ipoib_workqueue, &priv->cm.reap_task);
824 }
825
826 clear_bit(IPOIB_FLAG_OPER_UP, &tx->flags);
827
Roland Dreier943c2462008-09-30 10:36:21 -0700828 spin_unlock_irqrestore(&priv->lock, flags);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200829 }
830
Roland Dreier943c2462008-09-30 10:36:21 -0700831 netif_tx_unlock(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200832}
833
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200834int ipoib_cm_dev_open(struct net_device *dev)
835{
836 struct ipoib_dev_priv *priv = netdev_priv(dev);
837 int ret;
838
839 if (!IPOIB_CM_SUPPORTED(dev->dev_addr))
840 return 0;
841
842 priv->cm.id = ib_create_cm_id(priv->ca, ipoib_cm_rx_handler, dev);
843 if (IS_ERR(priv->cm.id)) {
844 printk(KERN_WARNING "%s: failed to create CM ID\n", priv->ca->name);
Michael S. Tsirkin347fcfb2007-04-30 17:30:28 -0700845 ret = PTR_ERR(priv->cm.id);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300846 goto err_cm;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200847 }
848
849 ret = ib_cm_listen(priv->cm.id, cpu_to_be64(IPOIB_CM_IETF_ID | priv->qp->qp_num),
850 0, NULL);
851 if (ret) {
852 printk(KERN_WARNING "%s: failed to listen on ID 0x%llx\n", priv->ca->name,
853 IPOIB_CM_IETF_ID | priv->qp->qp_num);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300854 goto err_listen;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200855 }
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300856
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200857 return 0;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300858
859err_listen:
860 ib_destroy_cm_id(priv->cm.id);
861err_cm:
862 priv->cm.id = NULL;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300863 return ret;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200864}
865
Roland Dreierefcd9972008-01-25 14:15:24 -0800866static void ipoib_cm_free_rx_reap_list(struct net_device *dev)
867{
868 struct ipoib_dev_priv *priv = netdev_priv(dev);
869 struct ipoib_cm_rx *rx, *n;
870 LIST_HEAD(list);
871
872 spin_lock_irq(&priv->lock);
873 list_splice_init(&priv->cm.rx_reap_list, &list);
874 spin_unlock_irq(&priv->lock);
875
876 list_for_each_entry_safe(rx, n, &list, list) {
877 ib_destroy_cm_id(rx->id);
878 ib_destroy_qp(rx->qp);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800879 if (!ipoib_cm_has_srq(dev)) {
880 ipoib_cm_free_rx_ring(priv->dev, rx->rx_ring);
881 spin_lock_irq(&priv->lock);
882 --priv->cm.nonsrq_conn_qp;
883 spin_unlock_irq(&priv->lock);
884 }
Roland Dreierefcd9972008-01-25 14:15:24 -0800885 kfree(rx);
886 }
887}
888
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200889void ipoib_cm_dev_stop(struct net_device *dev)
890{
891 struct ipoib_dev_priv *priv = netdev_priv(dev);
Roland Dreierefcd9972008-01-25 14:15:24 -0800892 struct ipoib_cm_rx *p;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300893 unsigned long begin;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300894 int ret;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200895
Michael S. Tsirkin347fcfb2007-04-30 17:30:28 -0700896 if (!IPOIB_CM_SUPPORTED(dev->dev_addr) || !priv->cm.id)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200897 return;
898
899 ib_destroy_cm_id(priv->cm.id);
Michael S. Tsirkin347fcfb2007-04-30 17:30:28 -0700900 priv->cm.id = NULL;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300901
Roland Dreier37aebbde2007-04-24 21:30:37 -0700902 spin_lock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200903 while (!list_empty(&priv->cm.passive_ids)) {
904 p = list_entry(priv->cm.passive_ids.next, typeof(*p), list);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300905 list_move(&p->list, &priv->cm.rx_error_list);
906 p->state = IPOIB_CM_RX_ERROR;
Roland Dreier37aebbde2007-04-24 21:30:37 -0700907 spin_unlock_irq(&priv->lock);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300908 ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
909 if (ret)
910 ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
911 spin_lock_irq(&priv->lock);
912 }
913
914 /* Wait for all RX to be drained */
915 begin = jiffies;
916
917 while (!list_empty(&priv->cm.rx_error_list) ||
918 !list_empty(&priv->cm.rx_flush_list) ||
919 !list_empty(&priv->cm.rx_drain_list)) {
Michael S. Tsirkin8fd357a2007-05-24 14:02:39 -0700920 if (time_after(jiffies, begin + 5 * HZ)) {
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300921 ipoib_warn(priv, "RX drain timing out\n");
922
923 /*
924 * assume the HW is wedged and just free up everything.
925 */
Pradeep Satyanarayanaec229e52008-02-12 15:00:59 -0800926 list_splice_init(&priv->cm.rx_flush_list,
927 &priv->cm.rx_reap_list);
928 list_splice_init(&priv->cm.rx_error_list,
929 &priv->cm.rx_reap_list);
930 list_splice_init(&priv->cm.rx_drain_list,
931 &priv->cm.rx_reap_list);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300932 break;
933 }
934 spin_unlock_irq(&priv->lock);
935 msleep(1);
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300936 ipoib_drain_cq(dev);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300937 spin_lock_irq(&priv->lock);
938 }
939
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300940 spin_unlock_irq(&priv->lock);
941
Roland Dreierefcd9972008-01-25 14:15:24 -0800942 ipoib_cm_free_rx_reap_list(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200943
944 cancel_delayed_work(&priv->cm.stale_task);
945}
946
947static int ipoib_cm_rep_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
948{
949 struct ipoib_cm_tx *p = cm_id->context;
950 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
951 struct ipoib_cm_data *data = event->private_data;
952 struct sk_buff_head skqueue;
953 struct ib_qp_attr qp_attr;
954 int qp_attr_mask, ret;
955 struct sk_buff *skb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200956
957 p->mtu = be32_to_cpu(data->mtu);
958
Michael S. Tsirkin82c3aca2007-06-20 19:22:15 +0300959 if (p->mtu <= IPOIB_ENCAP_LEN) {
960 ipoib_warn(priv, "Rejecting connection: mtu %d <= %d\n",
961 p->mtu, IPOIB_ENCAP_LEN);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200962 return -EINVAL;
963 }
964
965 qp_attr.qp_state = IB_QPS_RTR;
966 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
967 if (ret) {
968 ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
969 return ret;
970 }
971
972 qp_attr.rq_psn = 0 /* FIXME */;
973 ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
974 if (ret) {
975 ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
976 return ret;
977 }
978
979 qp_attr.qp_state = IB_QPS_RTS;
980 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
981 if (ret) {
982 ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
983 return ret;
984 }
985 ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
986 if (ret) {
987 ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
988 return ret;
989 }
990
991 skb_queue_head_init(&skqueue);
992
Roland Dreier37aebbde2007-04-24 21:30:37 -0700993 spin_lock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200994 set_bit(IPOIB_FLAG_OPER_UP, &p->flags);
995 if (p->neigh)
996 while ((skb = __skb_dequeue(&p->neigh->queue)))
997 __skb_queue_tail(&skqueue, skb);
Roland Dreier37aebbde2007-04-24 21:30:37 -0700998 spin_unlock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200999
1000 while ((skb = __skb_dequeue(&skqueue))) {
1001 skb->dev = p->dev;
1002 if (dev_queue_xmit(skb))
1003 ipoib_warn(priv, "dev_queue_xmit failed "
1004 "to requeue packet\n");
1005 }
1006
1007 ret = ib_send_cm_rtu(cm_id, NULL, 0);
1008 if (ret) {
1009 ipoib_warn(priv, "failed to send RTU: %d\n", ret);
1010 return ret;
1011 }
1012 return 0;
1013}
1014
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001015static struct ib_qp *ipoib_cm_create_tx_qp(struct net_device *dev, struct ipoib_cm_tx *tx)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001016{
1017 struct ipoib_dev_priv *priv = netdev_priv(dev);
Dotan Barakede6bc02007-10-07 09:30:48 +02001018 struct ib_qp_init_attr attr = {
Eli Cohenf56bcd82008-04-29 13:46:53 -07001019 .send_cq = priv->recv_cq,
1020 .recv_cq = priv->recv_cq,
Dotan Barakede6bc02007-10-07 09:30:48 +02001021 .srq = priv->cm.srq,
1022 .cap.max_send_wr = ipoib_sendq_size,
1023 .cap.max_send_sge = 1,
1024 .sq_sig_type = IB_SIGNAL_ALL_WR,
1025 .qp_type = IB_QPT_RC,
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001026 .qp_context = tx
Roland Dreier2337f802007-10-23 19:57:54 -07001027 };
Dotan Barakede6bc02007-10-07 09:30:48 +02001028
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001029 return ib_create_qp(priv->pd, &attr);
1030}
1031
1032static int ipoib_cm_send_req(struct net_device *dev,
1033 struct ib_cm_id *id, struct ib_qp *qp,
1034 u32 qpn,
1035 struct ib_sa_path_rec *pathrec)
1036{
1037 struct ipoib_dev_priv *priv = netdev_priv(dev);
1038 struct ipoib_cm_data data = {};
1039 struct ib_cm_req_param req = {};
1040
1041 data.qpn = cpu_to_be32(priv->qp->qp_num);
1042 data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
1043
Roland Dreier2337f802007-10-23 19:57:54 -07001044 req.primary_path = pathrec;
1045 req.alternate_path = NULL;
1046 req.service_id = cpu_to_be64(IPOIB_CM_IETF_ID | qpn);
1047 req.qp_num = qp->qp_num;
1048 req.qp_type = qp->qp_type;
1049 req.private_data = &data;
1050 req.private_data_len = sizeof data;
1051 req.flow_control = 0;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001052
Roland Dreier2337f802007-10-23 19:57:54 -07001053 req.starting_psn = 0; /* FIXME */
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001054
1055 /*
1056 * Pick some arbitrary defaults here; we could make these
1057 * module parameters if anyone cared about setting them.
1058 */
Roland Dreier2337f802007-10-23 19:57:54 -07001059 req.responder_resources = 4;
1060 req.remote_cm_response_timeout = 20;
1061 req.local_cm_response_timeout = 20;
1062 req.retry_count = 0; /* RFC draft warns against retries */
1063 req.rnr_retry_count = 0; /* RFC draft warns against retries */
1064 req.max_cm_retries = 15;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001065 req.srq = ipoib_cm_has_srq(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001066 return ib_send_cm_req(id, &req);
1067}
1068
1069static int ipoib_cm_modify_tx_init(struct net_device *dev,
1070 struct ib_cm_id *cm_id, struct ib_qp *qp)
1071{
1072 struct ipoib_dev_priv *priv = netdev_priv(dev);
1073 struct ib_qp_attr qp_attr;
1074 int qp_attr_mask, ret;
Roland Dreier9fdd5e52008-04-16 21:09:35 -07001075 ret = ib_find_pkey(priv->ca, priv->port, priv->pkey, &qp_attr.pkey_index);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001076 if (ret) {
Roland Dreier9fdd5e52008-04-16 21:09:35 -07001077 ipoib_warn(priv, "pkey 0x%x not found: %d\n", priv->pkey, ret);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001078 return ret;
1079 }
1080
1081 qp_attr.qp_state = IB_QPS_INIT;
1082 qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE;
1083 qp_attr.port_num = priv->port;
1084 qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
1085
1086 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
1087 if (ret) {
1088 ipoib_warn(priv, "failed to modify tx QP to INIT: %d\n", ret);
1089 return ret;
1090 }
1091 return 0;
1092}
1093
1094static int ipoib_cm_tx_init(struct ipoib_cm_tx *p, u32 qpn,
1095 struct ib_sa_path_rec *pathrec)
1096{
1097 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
1098 int ret;
1099
Roland Dreier10313cb2008-03-12 07:51:03 -07001100 p->tx_ring = vmalloc(ipoib_sendq_size * sizeof *p->tx_ring);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001101 if (!p->tx_ring) {
1102 ipoib_warn(priv, "failed to allocate tx ring\n");
1103 ret = -ENOMEM;
1104 goto err_tx;
1105 }
Roland Dreier10313cb2008-03-12 07:51:03 -07001106 memset(p->tx_ring, 0, ipoib_sendq_size * sizeof *p->tx_ring);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001107
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001108 p->qp = ipoib_cm_create_tx_qp(p->dev, p);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001109 if (IS_ERR(p->qp)) {
1110 ret = PTR_ERR(p->qp);
1111 ipoib_warn(priv, "failed to allocate tx qp: %d\n", ret);
1112 goto err_qp;
1113 }
1114
1115 p->id = ib_create_cm_id(priv->ca, ipoib_cm_tx_handler, p);
1116 if (IS_ERR(p->id)) {
1117 ret = PTR_ERR(p->id);
1118 ipoib_warn(priv, "failed to create tx cm id: %d\n", ret);
1119 goto err_id;
1120 }
1121
1122 ret = ipoib_cm_modify_tx_init(p->dev, p->id, p->qp);
1123 if (ret) {
1124 ipoib_warn(priv, "failed to modify tx qp to rtr: %d\n", ret);
1125 goto err_modify;
1126 }
1127
1128 ret = ipoib_cm_send_req(p->dev, p->id, p->qp, qpn, pathrec);
1129 if (ret) {
1130 ipoib_warn(priv, "failed to send cm req: %d\n", ret);
1131 goto err_send_cm;
1132 }
1133
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001134 ipoib_dbg(priv, "Request connection 0x%x for gid %pI6 qpn 0x%x\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -07001135 p->qp->qp_num, pathrec->dgid.raw, qpn);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001136
1137 return 0;
1138
1139err_send_cm:
1140err_modify:
1141 ib_destroy_cm_id(p->id);
1142err_id:
1143 p->id = NULL;
1144 ib_destroy_qp(p->qp);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001145err_qp:
1146 p->qp = NULL;
Roland Dreier10313cb2008-03-12 07:51:03 -07001147 vfree(p->tx_ring);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001148err_tx:
1149 return ret;
1150}
1151
1152static void ipoib_cm_tx_destroy(struct ipoib_cm_tx *p)
1153{
1154 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
Eli Cohene1123732008-07-14 23:48:52 -07001155 struct ipoib_cm_tx_buf *tx_req;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001156 unsigned long begin;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001157
1158 ipoib_dbg(priv, "Destroy active connection 0x%x head 0x%x tail 0x%x\n",
1159 p->qp ? p->qp->qp_num : 0, p->tx_head, p->tx_tail);
1160
1161 if (p->id)
1162 ib_destroy_cm_id(p->id);
1163
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001164 if (p->tx_ring) {
1165 /* Wait for all sends to complete */
1166 begin = jiffies;
1167 while ((int) p->tx_tail - (int) p->tx_head < 0) {
1168 if (time_after(jiffies, begin + 5 * HZ)) {
1169 ipoib_warn(priv, "timing out; %d sends not completed\n",
1170 p->tx_head - p->tx_tail);
1171 goto timeout;
1172 }
1173
1174 msleep(1);
1175 }
1176 }
1177
1178timeout:
1179
1180 while ((int) p->tx_tail - (int) p->tx_head < 0) {
1181 tx_req = &p->tx_ring[p->tx_tail & (ipoib_sendq_size - 1)];
Eli Cohene1123732008-07-14 23:48:52 -07001182 ib_dma_unmap_single(priv->ca, tx_req->mapping, tx_req->skb->len,
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001183 DMA_TO_DEVICE);
1184 dev_kfree_skb_any(tx_req->skb);
1185 ++p->tx_tail;
Roland Dreier943c2462008-09-30 10:36:21 -07001186 netif_tx_lock_bh(p->dev);
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001187 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
1188 netif_queue_stopped(p->dev) &&
1189 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
1190 netif_wake_queue(p->dev);
Roland Dreier943c2462008-09-30 10:36:21 -07001191 netif_tx_unlock_bh(p->dev);
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001192 }
1193
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001194 if (p->qp)
1195 ib_destroy_qp(p->qp);
1196
Roland Dreier10313cb2008-03-12 07:51:03 -07001197 vfree(p->tx_ring);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001198 kfree(p);
1199}
1200
1201static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
1202 struct ib_cm_event *event)
1203{
1204 struct ipoib_cm_tx *tx = cm_id->context;
1205 struct ipoib_dev_priv *priv = netdev_priv(tx->dev);
1206 struct net_device *dev = priv->dev;
1207 struct ipoib_neigh *neigh;
Roland Dreier943c2462008-09-30 10:36:21 -07001208 unsigned long flags;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001209 int ret;
1210
1211 switch (event->event) {
1212 case IB_CM_DREQ_RECEIVED:
1213 ipoib_dbg(priv, "DREQ received.\n");
1214 ib_send_cm_drep(cm_id, NULL, 0);
1215 break;
1216 case IB_CM_REP_RECEIVED:
1217 ipoib_dbg(priv, "REP received.\n");
1218 ret = ipoib_cm_rep_handler(cm_id, event);
1219 if (ret)
1220 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
1221 NULL, 0, NULL, 0);
1222 break;
1223 case IB_CM_REQ_ERROR:
1224 case IB_CM_REJ_RECEIVED:
1225 case IB_CM_TIMEWAIT_EXIT:
1226 ipoib_dbg(priv, "CM error %d.\n", event->event);
Roland Dreier943c2462008-09-30 10:36:21 -07001227 netif_tx_lock_bh(dev);
1228 spin_lock_irqsave(&priv->lock, flags);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001229 neigh = tx->neigh;
1230
1231 if (neigh) {
1232 neigh->cm = NULL;
1233 list_del(&neigh->list);
1234 if (neigh->ah)
1235 ipoib_put_ah(neigh->ah);
1236 ipoib_neigh_free(dev, neigh);
1237
1238 tx->neigh = NULL;
1239 }
1240
1241 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
1242 list_move(&tx->list, &priv->cm.reap_list);
1243 queue_work(ipoib_workqueue, &priv->cm.reap_task);
1244 }
1245
Roland Dreier943c2462008-09-30 10:36:21 -07001246 spin_unlock_irqrestore(&priv->lock, flags);
1247 netif_tx_unlock_bh(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001248 break;
1249 default:
1250 break;
1251 }
1252
1253 return 0;
1254}
1255
1256struct ipoib_cm_tx *ipoib_cm_create_tx(struct net_device *dev, struct ipoib_path *path,
1257 struct ipoib_neigh *neigh)
1258{
1259 struct ipoib_dev_priv *priv = netdev_priv(dev);
1260 struct ipoib_cm_tx *tx;
1261
1262 tx = kzalloc(sizeof *tx, GFP_ATOMIC);
1263 if (!tx)
1264 return NULL;
1265
1266 neigh->cm = tx;
1267 tx->neigh = neigh;
1268 tx->path = path;
1269 tx->dev = dev;
1270 list_add(&tx->list, &priv->cm.start_list);
1271 set_bit(IPOIB_FLAG_INITIALIZED, &tx->flags);
1272 queue_work(ipoib_workqueue, &priv->cm.start_task);
1273 return tx;
1274}
1275
1276void ipoib_cm_destroy_tx(struct ipoib_cm_tx *tx)
1277{
1278 struct ipoib_dev_priv *priv = netdev_priv(tx->dev);
1279 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
1280 list_move(&tx->list, &priv->cm.reap_list);
1281 queue_work(ipoib_workqueue, &priv->cm.reap_task);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001282 ipoib_dbg(priv, "Reap connection for gid %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -07001283 tx->neigh->dgid.raw);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001284 tx->neigh = NULL;
1285 }
1286}
1287
1288static void ipoib_cm_tx_start(struct work_struct *work)
1289{
1290 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1291 cm.start_task);
1292 struct net_device *dev = priv->dev;
1293 struct ipoib_neigh *neigh;
1294 struct ipoib_cm_tx *p;
1295 unsigned long flags;
1296 int ret;
1297
1298 struct ib_sa_path_rec pathrec;
1299 u32 qpn;
1300
Roland Dreier943c2462008-09-30 10:36:21 -07001301 netif_tx_lock_bh(dev);
1302 spin_lock_irqsave(&priv->lock, flags);
1303
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001304 while (!list_empty(&priv->cm.start_list)) {
1305 p = list_entry(priv->cm.start_list.next, typeof(*p), list);
1306 list_del_init(&p->list);
1307 neigh = p->neigh;
1308 qpn = IPOIB_QPN(neigh->neighbour->ha);
1309 memcpy(&pathrec, &p->path->pathrec, sizeof pathrec);
Roland Dreier943c2462008-09-30 10:36:21 -07001310
1311 spin_unlock_irqrestore(&priv->lock, flags);
1312 netif_tx_unlock_bh(dev);
1313
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001314 ret = ipoib_cm_tx_init(p, qpn, &pathrec);
Roland Dreier943c2462008-09-30 10:36:21 -07001315
1316 netif_tx_lock_bh(dev);
1317 spin_lock_irqsave(&priv->lock, flags);
1318
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001319 if (ret) {
1320 neigh = p->neigh;
1321 if (neigh) {
1322 neigh->cm = NULL;
1323 list_del(&neigh->list);
1324 if (neigh->ah)
1325 ipoib_put_ah(neigh->ah);
1326 ipoib_neigh_free(dev, neigh);
1327 }
1328 list_del(&p->list);
1329 kfree(p);
1330 }
1331 }
Roland Dreier943c2462008-09-30 10:36:21 -07001332
1333 spin_unlock_irqrestore(&priv->lock, flags);
1334 netif_tx_unlock_bh(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001335}
1336
1337static void ipoib_cm_tx_reap(struct work_struct *work)
1338{
1339 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1340 cm.reap_task);
Roland Dreier943c2462008-09-30 10:36:21 -07001341 struct net_device *dev = priv->dev;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001342 struct ipoib_cm_tx *p;
Roland Dreier943c2462008-09-30 10:36:21 -07001343 unsigned long flags;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001344
Roland Dreier943c2462008-09-30 10:36:21 -07001345 netif_tx_lock_bh(dev);
1346 spin_lock_irqsave(&priv->lock, flags);
1347
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001348 while (!list_empty(&priv->cm.reap_list)) {
1349 p = list_entry(priv->cm.reap_list.next, typeof(*p), list);
1350 list_del(&p->list);
Roland Dreier943c2462008-09-30 10:36:21 -07001351 spin_unlock_irqrestore(&priv->lock, flags);
1352 netif_tx_unlock_bh(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001353 ipoib_cm_tx_destroy(p);
Roland Dreier943c2462008-09-30 10:36:21 -07001354 netif_tx_lock_bh(dev);
1355 spin_lock_irqsave(&priv->lock, flags);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001356 }
Roland Dreier943c2462008-09-30 10:36:21 -07001357
1358 spin_unlock_irqrestore(&priv->lock, flags);
1359 netif_tx_unlock_bh(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001360}
1361
1362static void ipoib_cm_skb_reap(struct work_struct *work)
1363{
1364 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1365 cm.skb_task);
Roland Dreier943c2462008-09-30 10:36:21 -07001366 struct net_device *dev = priv->dev;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001367 struct sk_buff *skb;
Roland Dreier943c2462008-09-30 10:36:21 -07001368 unsigned long flags;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001369 unsigned mtu = priv->mcast_mtu;
1370
Roland Dreier943c2462008-09-30 10:36:21 -07001371 netif_tx_lock_bh(dev);
1372 spin_lock_irqsave(&priv->lock, flags);
1373
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001374 while ((skb = skb_dequeue(&priv->cm.skb_queue))) {
Roland Dreier943c2462008-09-30 10:36:21 -07001375 spin_unlock_irqrestore(&priv->lock, flags);
1376 netif_tx_unlock_bh(dev);
1377
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001378 if (skb->protocol == htons(ETH_P_IP))
1379 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
1380#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1381 else if (skb->protocol == htons(ETH_P_IPV6))
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001382 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001383#endif
1384 dev_kfree_skb_any(skb);
Roland Dreier943c2462008-09-30 10:36:21 -07001385
1386 netif_tx_lock_bh(dev);
1387 spin_lock_irqsave(&priv->lock, flags);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001388 }
Roland Dreier943c2462008-09-30 10:36:21 -07001389
1390 spin_unlock_irqrestore(&priv->lock, flags);
1391 netif_tx_unlock_bh(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001392}
1393
Roland Dreier2337f802007-10-23 19:57:54 -07001394void ipoib_cm_skb_too_long(struct net_device *dev, struct sk_buff *skb,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001395 unsigned int mtu)
1396{
1397 struct ipoib_dev_priv *priv = netdev_priv(dev);
1398 int e = skb_queue_empty(&priv->cm.skb_queue);
1399
Eric Dumazetadf30902009-06-02 05:19:30 +00001400 if (skb_dst(skb))
1401 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001402
1403 skb_queue_tail(&priv->cm.skb_queue, skb);
1404 if (e)
1405 queue_work(ipoib_workqueue, &priv->cm.skb_task);
1406}
1407
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001408static void ipoib_cm_rx_reap(struct work_struct *work)
1409{
Roland Dreierefcd9972008-01-25 14:15:24 -08001410 ipoib_cm_free_rx_reap_list(container_of(work, struct ipoib_dev_priv,
1411 cm.rx_reap_task)->dev);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001412}
1413
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001414static void ipoib_cm_stale_task(struct work_struct *work)
1415{
1416 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1417 cm.stale_task.work);
1418 struct ipoib_cm_rx *p;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001419 int ret;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001420
Roland Dreier37aebbde2007-04-24 21:30:37 -07001421 spin_lock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001422 while (!list_empty(&priv->cm.passive_ids)) {
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001423 /* List is sorted by LRU, start from tail,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001424 * stop when we see a recently used entry */
1425 p = list_entry(priv->cm.passive_ids.prev, typeof(*p), list);
Michael S. Tsirkin60a596d2007-03-22 14:32:09 -07001426 if (time_before_eq(jiffies, p->jiffies + IPOIB_CM_RX_TIMEOUT))
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001427 break;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001428 list_move(&p->list, &priv->cm.rx_error_list);
1429 p->state = IPOIB_CM_RX_ERROR;
Roland Dreier37aebbde2007-04-24 21:30:37 -07001430 spin_unlock_irq(&priv->lock);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001431 ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
1432 if (ret)
1433 ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001434 spin_lock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001435 }
Michael S. Tsirkin7c5b9ef2007-05-14 07:26:51 +03001436
1437 if (!list_empty(&priv->cm.passive_ids))
1438 queue_delayed_work(ipoib_workqueue,
1439 &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001440 spin_unlock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001441}
1442
1443
Roland Dreier2337f802007-10-23 19:57:54 -07001444static ssize_t show_mode(struct device *d, struct device_attribute *attr,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001445 char *buf)
1446{
1447 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(d));
1448
1449 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
1450 return sprintf(buf, "connected\n");
1451 else
1452 return sprintf(buf, "datagram\n");
1453}
1454
1455static ssize_t set_mode(struct device *d, struct device_attribute *attr,
1456 const char *buf, size_t count)
1457{
1458 struct net_device *dev = to_net_dev(d);
1459 struct ipoib_dev_priv *priv = netdev_priv(dev);
1460
Eric W. Biederman26574402009-05-13 17:01:51 +00001461 if (!rtnl_trylock())
1462 return restart_syscall();
1463
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001464 /* flush paths if we switch modes so that connections are restarted */
1465 if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
1466 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
1467 ipoib_warn(priv, "enabling connected mode "
1468 "will cause multicast packet drops\n");
Eli Cohen60461362008-04-16 21:01:10 -07001469
Eli Cohen40ca1982008-04-16 21:09:27 -07001470 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_TSO);
Eli Cohenc8c2afe2008-07-14 23:48:51 -07001471 rtnl_unlock();
Eli Cohen60461362008-04-16 21:01:10 -07001472 priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
1473
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001474 ipoib_flush_paths(dev);
1475 return count;
1476 }
1477
1478 if (!strcmp(buf, "datagram\n")) {
1479 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
Eli Cohen60461362008-04-16 21:01:10 -07001480
Eli Cohen40ca1982008-04-16 21:09:27 -07001481 if (test_bit(IPOIB_FLAG_CSUM, &priv->flags)) {
Eli Cohen60461362008-04-16 21:01:10 -07001482 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
Eli Cohen40ca1982008-04-16 21:09:27 -07001483 if (priv->hca_caps & IB_DEVICE_UD_TSO)
1484 dev->features |= NETIF_F_TSO;
1485 }
Eli Cohenbd360672008-07-14 23:48:51 -07001486 dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
Eli Cohenc8c2afe2008-07-14 23:48:51 -07001487 rtnl_unlock();
1488 ipoib_flush_paths(dev);
Eli Cohen60461362008-04-16 21:01:10 -07001489
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001490 return count;
1491 }
Eric W. Biederman26574402009-05-13 17:01:51 +00001492 rtnl_unlock();
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001493
1494 return -EINVAL;
1495}
1496
Roland Dreier551fd612007-02-16 13:57:33 -08001497static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO, show_mode, set_mode);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001498
1499int ipoib_cm_add_mode_attr(struct net_device *dev)
1500{
1501 return device_create_file(&dev->dev, &dev_attr_mode);
1502}
1503
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001504static void ipoib_cm_create_srq(struct net_device *dev, int max_sge)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001505{
1506 struct ipoib_dev_priv *priv = netdev_priv(dev);
1507 struct ib_srq_init_attr srq_init_attr = {
1508 .attr = {
1509 .max_wr = ipoib_recvq_size,
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001510 .max_sge = max_sge
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001511 }
1512 };
Roland Dreier7b3687d2008-01-25 14:15:24 -08001513
1514 priv->cm.srq = ib_create_srq(priv->pd, &srq_init_attr);
1515 if (IS_ERR(priv->cm.srq)) {
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001516 if (PTR_ERR(priv->cm.srq) != -ENOSYS)
1517 printk(KERN_WARNING "%s: failed to allocate SRQ, error %ld\n",
1518 priv->ca->name, PTR_ERR(priv->cm.srq));
Roland Dreier7b3687d2008-01-25 14:15:24 -08001519 priv->cm.srq = NULL;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001520 return;
Roland Dreier7b3687d2008-01-25 14:15:24 -08001521 }
1522
David J. Wilderb1404062008-08-08 15:51:29 -07001523 priv->cm.srq_ring = vmalloc(ipoib_recvq_size * sizeof *priv->cm.srq_ring);
Roland Dreier7b3687d2008-01-25 14:15:24 -08001524 if (!priv->cm.srq_ring) {
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001525 printk(KERN_WARNING "%s: failed to allocate CM SRQ ring (%d entries)\n",
Roland Dreier7b3687d2008-01-25 14:15:24 -08001526 priv->ca->name, ipoib_recvq_size);
1527 ib_destroy_srq(priv->cm.srq);
1528 priv->cm.srq = NULL;
David J. Wilderb1404062008-08-08 15:51:29 -07001529 return;
Roland Dreier7b3687d2008-01-25 14:15:24 -08001530 }
David J. Wilderb1404062008-08-08 15:51:29 -07001531
1532 memset(priv->cm.srq_ring, 0, ipoib_recvq_size * sizeof *priv->cm.srq_ring);
Roland Dreier7b3687d2008-01-25 14:15:24 -08001533}
1534
1535int ipoib_cm_dev_init(struct net_device *dev)
1536{
1537 struct ipoib_dev_priv *priv = netdev_priv(dev);
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001538 int i, ret;
1539 struct ib_device_attr attr;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001540
1541 INIT_LIST_HEAD(&priv->cm.passive_ids);
1542 INIT_LIST_HEAD(&priv->cm.reap_list);
1543 INIT_LIST_HEAD(&priv->cm.start_list);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001544 INIT_LIST_HEAD(&priv->cm.rx_error_list);
1545 INIT_LIST_HEAD(&priv->cm.rx_flush_list);
1546 INIT_LIST_HEAD(&priv->cm.rx_drain_list);
1547 INIT_LIST_HEAD(&priv->cm.rx_reap_list);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001548 INIT_WORK(&priv->cm.start_task, ipoib_cm_tx_start);
1549 INIT_WORK(&priv->cm.reap_task, ipoib_cm_tx_reap);
1550 INIT_WORK(&priv->cm.skb_task, ipoib_cm_skb_reap);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001551 INIT_WORK(&priv->cm.rx_reap_task, ipoib_cm_rx_reap);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001552 INIT_DELAYED_WORK(&priv->cm.stale_task, ipoib_cm_stale_task);
1553
1554 skb_queue_head_init(&priv->cm.skb_queue);
1555
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001556 ret = ib_query_device(priv->ca, &attr);
1557 if (ret) {
1558 printk(KERN_WARNING "ib_query_device() failed with %d\n", ret);
1559 return ret;
1560 }
1561
1562 ipoib_dbg(priv, "max_srq_sge=%d\n", attr.max_srq_sge);
1563
1564 attr.max_srq_sge = min_t(int, IPOIB_CM_RX_SG, attr.max_srq_sge);
1565 ipoib_cm_create_srq(dev, attr.max_srq_sge);
1566 if (ipoib_cm_has_srq(dev)) {
1567 priv->cm.max_cm_mtu = attr.max_srq_sge * PAGE_SIZE - 0x10;
1568 priv->cm.num_frags = attr.max_srq_sge;
1569 ipoib_dbg(priv, "max_cm_mtu = 0x%x, num_frags=%d\n",
1570 priv->cm.max_cm_mtu, priv->cm.num_frags);
1571 } else {
1572 priv->cm.max_cm_mtu = IPOIB_CM_MTU;
1573 priv->cm.num_frags = IPOIB_CM_RX_SG;
1574 }
1575
Roland Dreiera7d834c2008-07-14 23:48:47 -07001576 ipoib_cm_init_rx_wr(dev, &priv->cm.rx_wr, priv->cm.rx_sge);
Roland Dreier7b3687d2008-01-25 14:15:24 -08001577
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001578 if (ipoib_cm_has_srq(dev)) {
1579 for (i = 0; i < ipoib_recvq_size; ++i) {
1580 if (!ipoib_cm_alloc_rx_skb(dev, priv->cm.srq_ring, i,
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001581 priv->cm.num_frags - 1,
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001582 priv->cm.srq_ring[i].mapping)) {
1583 ipoib_warn(priv, "failed to allocate "
1584 "receive buffer %d\n", i);
1585 ipoib_cm_dev_cleanup(dev);
1586 return -ENOMEM;
1587 }
1588
1589 if (ipoib_cm_post_receive_srq(dev, i)) {
1590 ipoib_warn(priv, "ipoib_cm_post_receive_srq "
1591 "failed for buf %d\n", i);
1592 ipoib_cm_dev_cleanup(dev);
1593 return -EIO;
1594 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001595 }
1596 }
1597
1598 priv->dev->dev_addr[0] = IPOIB_FLAGS_RC;
1599 return 0;
1600}
1601
1602void ipoib_cm_dev_cleanup(struct net_device *dev)
1603{
1604 struct ipoib_dev_priv *priv = netdev_priv(dev);
Roland Dreier1efb6142008-01-25 14:15:24 -08001605 int ret;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001606
1607 if (!priv->cm.srq)
1608 return;
1609
1610 ipoib_dbg(priv, "Cleanup ipoib connected mode.\n");
1611
1612 ret = ib_destroy_srq(priv->cm.srq);
1613 if (ret)
1614 ipoib_warn(priv, "ib_destroy_srq failed: %d\n", ret);
1615
1616 priv->cm.srq = NULL;
1617 if (!priv->cm.srq_ring)
1618 return;
Roland Dreier1efb6142008-01-25 14:15:24 -08001619
1620 ipoib_cm_free_rx_ring(dev, priv->cm.srq_ring);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001621 priv->cm.srq_ring = NULL;
1622}