IPoIB: Don't drop multicast sends when they can be queued
[linux-2.6-block.git] / drivers / infiniband / ulp / ipoib / ipoib_cm.c
CommitLineData
839fcaba
MT
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 * $Id$
33 */
34
35#include <rdma/ib_cm.h>
36#include <rdma/ib_cache.h>
37#include <net/dst.h>
38#include <net/icmp.h>
39#include <linux/icmpv6.h>
518b1646 40#include <linux/delay.h>
839fcaba 41
68e995a2
PS
42#include "ipoib.h"
43
44int ipoib_max_conn_qp = 128;
45
46module_param_named(max_nonsrq_conn_qp, ipoib_max_conn_qp, int, 0444);
47MODULE_PARM_DESC(max_nonsrq_conn_qp,
48 "Max number of connected-mode QPs per interface "
49 "(applied only if shared receive queue is not available)");
50
839fcaba
MT
51#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
52static int data_debug_level;
53
54module_param_named(cm_data_debug_level, data_debug_level, int, 0644);
55MODULE_PARM_DESC(cm_data_debug_level,
56 "Enable data path debug tracing for connected mode if > 0");
57#endif
58
839fcaba
MT
59#define IPOIB_CM_IETF_ID 0x1000000000000000ULL
60
61#define IPOIB_CM_RX_UPDATE_TIME (256 * HZ)
62#define IPOIB_CM_RX_TIMEOUT (2 * 256 * HZ)
63#define IPOIB_CM_RX_DELAY (3 * 256 * HZ)
64#define IPOIB_CM_RX_UPDATE_MASK (0x3)
65
518b1646
MT
66static struct ib_qp_attr ipoib_cm_err_attr = {
67 .qp_state = IB_QPS_ERR
68};
69
09f60f8f 70#define IPOIB_CM_RX_DRAIN_WRID 0xffffffff
518b1646 71
ec56dc0b
MT
72static struct ib_send_wr ipoib_cm_rx_drain_wr = {
73 .wr_id = IPOIB_CM_RX_DRAIN_WRID,
74 .opcode = IB_WR_SEND,
518b1646
MT
75};
76
839fcaba
MT
77static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
78 struct ib_cm_event *event);
79
1812063b 80static void ipoib_cm_dma_unmap_rx(struct ipoib_dev_priv *priv, int frags,
839fcaba
MT
81 u64 mapping[IPOIB_CM_RX_SG])
82{
83 int i;
84
85 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
86
1812063b 87 for (i = 0; i < frags; ++i)
839fcaba
MT
88 ib_dma_unmap_single(priv->ca, mapping[i + 1], PAGE_SIZE, DMA_FROM_DEVICE);
89}
90
68e995a2 91static int ipoib_cm_post_receive_srq(struct net_device *dev, int id)
839fcaba
MT
92{
93 struct ipoib_dev_priv *priv = netdev_priv(dev);
94 struct ib_recv_wr *bad_wr;
95 int i, ret;
96
1b524963 97 priv->cm.rx_wr.wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV;
839fcaba 98
586a6934 99 for (i = 0; i < priv->cm.num_frags; ++i)
839fcaba
MT
100 priv->cm.rx_sge[i].addr = priv->cm.srq_ring[id].mapping[i];
101
102 ret = ib_post_srq_recv(priv->cm.srq, &priv->cm.rx_wr, &bad_wr);
103 if (unlikely(ret)) {
104 ipoib_warn(priv, "post srq failed for buf %d (%d)\n", id, ret);
586a6934 105 ipoib_cm_dma_unmap_rx(priv, priv->cm.num_frags - 1,
1812063b 106 priv->cm.srq_ring[id].mapping);
839fcaba
MT
107 dev_kfree_skb_any(priv->cm.srq_ring[id].skb);
108 priv->cm.srq_ring[id].skb = NULL;
109 }
110
111 return ret;
112}
113
68e995a2
PS
114static int ipoib_cm_post_receive_nonsrq(struct net_device *dev,
115 struct ipoib_cm_rx *rx, int id)
116{
117 struct ipoib_dev_priv *priv = netdev_priv(dev);
118 struct ib_recv_wr *bad_wr;
119 int i, ret;
120
121 priv->cm.rx_wr.wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV;
122
123 for (i = 0; i < IPOIB_CM_RX_SG; ++i)
124 priv->cm.rx_sge[i].addr = rx->rx_ring[id].mapping[i];
125
126 ret = ib_post_recv(rx->qp, &priv->cm.rx_wr, &bad_wr);
127 if (unlikely(ret)) {
128 ipoib_warn(priv, "post recv failed for buf %d (%d)\n", id, ret);
129 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
130 rx->rx_ring[id].mapping);
131 dev_kfree_skb_any(rx->rx_ring[id].skb);
132 rx->rx_ring[id].skb = NULL;
133 }
134
135 return ret;
136}
137
138static struct sk_buff *ipoib_cm_alloc_rx_skb(struct net_device *dev,
139 struct ipoib_cm_rx_buf *rx_ring,
140 int id, int frags,
1812063b 141 u64 mapping[IPOIB_CM_RX_SG])
839fcaba
MT
142{
143 struct ipoib_dev_priv *priv = netdev_priv(dev);
144 struct sk_buff *skb;
145 int i;
146
147 skb = dev_alloc_skb(IPOIB_CM_HEAD_SIZE + 12);
148 if (unlikely(!skb))
1812063b 149 return NULL;
839fcaba
MT
150
151 /*
152 * IPoIB adds a 4 byte header. So we need 12 more bytes to align the
153 * IP header to a multiple of 16.
154 */
155 skb_reserve(skb, 12);
156
157 mapping[0] = ib_dma_map_single(priv->ca, skb->data, IPOIB_CM_HEAD_SIZE,
158 DMA_FROM_DEVICE);
159 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0]))) {
160 dev_kfree_skb_any(skb);
1812063b 161 return NULL;
839fcaba
MT
162 }
163
1812063b 164 for (i = 0; i < frags; i++) {
839fcaba
MT
165 struct page *page = alloc_page(GFP_ATOMIC);
166
167 if (!page)
168 goto partial_error;
169 skb_fill_page_desc(skb, i, page, 0, PAGE_SIZE);
170
171 mapping[i + 1] = ib_dma_map_page(priv->ca, skb_shinfo(skb)->frags[i].page,
6371ea3d 172 0, PAGE_SIZE, DMA_FROM_DEVICE);
839fcaba
MT
173 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[i + 1])))
174 goto partial_error;
175 }
176
68e995a2 177 rx_ring[id].skb = skb;
1812063b 178 return skb;
839fcaba
MT
179
180partial_error:
181
182 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
183
841adfca
RC
184 for (; i > 0; --i)
185 ib_dma_unmap_single(priv->ca, mapping[i], PAGE_SIZE, DMA_FROM_DEVICE);
839fcaba 186
8a2e65f8 187 dev_kfree_skb_any(skb);
1812063b 188 return NULL;
839fcaba
MT
189}
190
1efb6144
RD
191static void ipoib_cm_free_rx_ring(struct net_device *dev,
192 struct ipoib_cm_rx_buf *rx_ring)
193{
194 struct ipoib_dev_priv *priv = netdev_priv(dev);
195 int i;
196
197 for (i = 0; i < ipoib_recvq_size; ++i)
198 if (rx_ring[i].skb) {
199 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
200 rx_ring[i].mapping);
201 dev_kfree_skb_any(rx_ring[i].skb);
202 }
203
204 kfree(rx_ring);
205}
206
2337f809 207static void ipoib_cm_start_rx_drain(struct ipoib_dev_priv *priv)
518b1646 208{
ec56dc0b
MT
209 struct ib_send_wr *bad_wr;
210 struct ipoib_cm_rx *p;
518b1646 211
ec56dc0b 212 /* We only reserved 1 extra slot in CQ for drain WRs, so
518b1646
MT
213 * make sure we have at most 1 outstanding WR. */
214 if (list_empty(&priv->cm.rx_flush_list) ||
215 !list_empty(&priv->cm.rx_drain_list))
216 return;
217
ec56dc0b
MT
218 /*
219 * QPs on flush list are error state. This way, a "flush
220 * error" WC will be immediately generated for each WR we post.
221 */
222 p = list_entry(priv->cm.rx_flush_list.next, typeof(*p), list);
223 if (ib_post_send(p->qp, &ipoib_cm_rx_drain_wr, &bad_wr))
224 ipoib_warn(priv, "failed to post drain wr\n");
518b1646
MT
225
226 list_splice_init(&priv->cm.rx_flush_list, &priv->cm.rx_drain_list);
227}
228
229static void ipoib_cm_rx_event_handler(struct ib_event *event, void *ctx)
230{
231 struct ipoib_cm_rx *p = ctx;
232 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
233 unsigned long flags;
234
235 if (event->event != IB_EVENT_QP_LAST_WQE_REACHED)
236 return;
237
238 spin_lock_irqsave(&priv->lock, flags);
239 list_move(&p->list, &priv->cm.rx_flush_list);
240 p->state = IPOIB_CM_RX_FLUSH;
241 ipoib_cm_start_rx_drain(priv);
242 spin_unlock_irqrestore(&priv->lock, flags);
243}
244
839fcaba
MT
245static struct ib_qp *ipoib_cm_create_rx_qp(struct net_device *dev,
246 struct ipoib_cm_rx *p)
247{
248 struct ipoib_dev_priv *priv = netdev_priv(dev);
249 struct ib_qp_init_attr attr = {
518b1646 250 .event_handler = ipoib_cm_rx_event_handler,
ec56dc0b 251 .send_cq = priv->cq, /* For drain WR */
839fcaba
MT
252 .recv_cq = priv->cq,
253 .srq = priv->cm.srq,
ec56dc0b 254 .cap.max_send_wr = 1, /* For drain WR */
839fcaba
MT
255 .cap.max_send_sge = 1, /* FIXME: 0 Seems not to work */
256 .sq_sig_type = IB_SIGNAL_ALL_WR,
257 .qp_type = IB_QPT_RC,
258 .qp_context = p,
259 };
68e995a2
PS
260
261 if (!ipoib_cm_has_srq(dev)) {
262 attr.cap.max_recv_wr = ipoib_recvq_size;
263 attr.cap.max_recv_sge = IPOIB_CM_RX_SG;
264 }
265
839fcaba
MT
266 return ib_create_qp(priv->pd, &attr);
267}
268
269static int ipoib_cm_modify_rx_qp(struct net_device *dev,
68e995a2
PS
270 struct ib_cm_id *cm_id, struct ib_qp *qp,
271 unsigned psn)
839fcaba
MT
272{
273 struct ipoib_dev_priv *priv = netdev_priv(dev);
274 struct ib_qp_attr qp_attr;
275 int qp_attr_mask, ret;
276
277 qp_attr.qp_state = IB_QPS_INIT;
278 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
279 if (ret) {
280 ipoib_warn(priv, "failed to init QP attr for INIT: %d\n", ret);
281 return ret;
282 }
283 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
284 if (ret) {
285 ipoib_warn(priv, "failed to modify QP to INIT: %d\n", ret);
286 return ret;
287 }
288 qp_attr.qp_state = IB_QPS_RTR;
289 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
290 if (ret) {
291 ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
292 return ret;
293 }
294 qp_attr.rq_psn = psn;
295 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
296 if (ret) {
297 ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
298 return ret;
299 }
ec56dc0b
MT
300
301 /*
302 * Current Mellanox HCA firmware won't generate completions
303 * with error for drain WRs unless the QP has been moved to
304 * RTS first. This work-around leaves a window where a QP has
305 * moved to error asynchronously, but this will eventually get
306 * fixed in firmware, so let's not error out if modify QP
307 * fails.
308 */
309 qp_attr.qp_state = IB_QPS_RTS;
310 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
311 if (ret) {
312 ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
313 return 0;
314 }
315 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
316 if (ret) {
317 ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
318 return 0;
319 }
320
839fcaba
MT
321 return 0;
322}
323
68e995a2
PS
324static int ipoib_cm_nonsrq_init_rx(struct net_device *dev, struct ib_cm_id *cm_id,
325 struct ipoib_cm_rx *rx)
326{
327 struct ipoib_dev_priv *priv = netdev_priv(dev);
328 int ret;
329 int i;
330
331 rx->rx_ring = kcalloc(ipoib_recvq_size, sizeof *rx->rx_ring, GFP_KERNEL);
332 if (!rx->rx_ring)
333 return -ENOMEM;
334
335 spin_lock_irq(&priv->lock);
336
337 if (priv->cm.nonsrq_conn_qp >= ipoib_max_conn_qp) {
338 spin_unlock_irq(&priv->lock);
339 ib_send_cm_rej(cm_id, IB_CM_REJ_NO_QP, NULL, 0, NULL, 0);
340 ret = -EINVAL;
341 goto err_free;
342 } else
343 ++priv->cm.nonsrq_conn_qp;
344
345 spin_unlock_irq(&priv->lock);
346
347 for (i = 0; i < ipoib_recvq_size; ++i) {
348 if (!ipoib_cm_alloc_rx_skb(dev, rx->rx_ring, i, IPOIB_CM_RX_SG - 1,
349 rx->rx_ring[i].mapping)) {
350 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
351 ret = -ENOMEM;
352 goto err_count;
353 }
354 ret = ipoib_cm_post_receive_nonsrq(dev, rx, i);
355 if (ret) {
356 ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq "
357 "failed for buf %d\n", i);
358 ret = -EIO;
359 goto err_count;
360 }
361 }
362
363 rx->recv_count = ipoib_recvq_size;
364
365 return 0;
366
367err_count:
368 spin_lock_irq(&priv->lock);
369 --priv->cm.nonsrq_conn_qp;
370 spin_unlock_irq(&priv->lock);
371
372err_free:
373 ipoib_cm_free_rx_ring(dev, rx->rx_ring);
374
375 return ret;
376}
377
839fcaba
MT
378static int ipoib_cm_send_rep(struct net_device *dev, struct ib_cm_id *cm_id,
379 struct ib_qp *qp, struct ib_cm_req_event_param *req,
380 unsigned psn)
381{
382 struct ipoib_dev_priv *priv = netdev_priv(dev);
383 struct ipoib_cm_data data = {};
384 struct ib_cm_rep_param rep = {};
385
386 data.qpn = cpu_to_be32(priv->qp->qp_num);
387 data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
388
389 rep.private_data = &data;
390 rep.private_data_len = sizeof data;
391 rep.flow_control = 0;
392 rep.rnr_retry_count = req->rnr_retry_count;
68e995a2 393 rep.srq = ipoib_cm_has_srq(dev);
839fcaba
MT
394 rep.qp_num = qp->qp_num;
395 rep.starting_psn = psn;
396 return ib_send_cm_rep(cm_id, &rep);
397}
398
399static int ipoib_cm_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
400{
401 struct net_device *dev = cm_id->context;
402 struct ipoib_dev_priv *priv = netdev_priv(dev);
403 struct ipoib_cm_rx *p;
839fcaba
MT
404 unsigned psn;
405 int ret;
406
407 ipoib_dbg(priv, "REQ arrived\n");
408 p = kzalloc(sizeof *p, GFP_KERNEL);
409 if (!p)
410 return -ENOMEM;
411 p->dev = dev;
412 p->id = cm_id;
3ec7393a
MT
413 cm_id->context = p;
414 p->state = IPOIB_CM_RX_LIVE;
415 p->jiffies = jiffies;
416 INIT_LIST_HEAD(&p->list);
417
839fcaba
MT
418 p->qp = ipoib_cm_create_rx_qp(dev, p);
419 if (IS_ERR(p->qp)) {
420 ret = PTR_ERR(p->qp);
421 goto err_qp;
422 }
423
424 psn = random32() & 0xffffff;
425 ret = ipoib_cm_modify_rx_qp(dev, cm_id, p->qp, psn);
426 if (ret)
427 goto err_modify;
428
68e995a2
PS
429 if (!ipoib_cm_has_srq(dev)) {
430 ret = ipoib_cm_nonsrq_init_rx(dev, cm_id, p);
431 if (ret)
432 goto err_modify;
433 }
434
3ec7393a
MT
435 spin_lock_irq(&priv->lock);
436 queue_delayed_work(ipoib_workqueue,
437 &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
438 /* Add this entry to passive ids list head, but do not re-add it
439 * if IB_EVENT_QP_LAST_WQE_REACHED has moved it to flush list. */
440 p->jiffies = jiffies;
441 if (p->state == IPOIB_CM_RX_LIVE)
442 list_move(&p->list, &priv->cm.passive_ids);
443 spin_unlock_irq(&priv->lock);
444
839fcaba
MT
445 ret = ipoib_cm_send_rep(dev, cm_id, p->qp, &event->param.req_rcvd, psn);
446 if (ret) {
447 ipoib_warn(priv, "failed to send REP: %d\n", ret);
3ec7393a
MT
448 if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE))
449 ipoib_warn(priv, "unable to move qp to error state\n");
839fcaba 450 }
839fcaba
MT
451 return 0;
452
839fcaba
MT
453err_modify:
454 ib_destroy_qp(p->qp);
455err_qp:
456 kfree(p);
457 return ret;
458}
459
460static int ipoib_cm_rx_handler(struct ib_cm_id *cm_id,
461 struct ib_cm_event *event)
462{
463 struct ipoib_cm_rx *p;
464 struct ipoib_dev_priv *priv;
839fcaba
MT
465
466 switch (event->event) {
467 case IB_CM_REQ_RECEIVED:
468 return ipoib_cm_req_handler(cm_id, event);
469 case IB_CM_DREQ_RECEIVED:
470 p = cm_id->context;
471 ib_send_cm_drep(cm_id, NULL, 0);
472 /* Fall through */
473 case IB_CM_REJ_RECEIVED:
474 p = cm_id->context;
475 priv = netdev_priv(p->dev);
518b1646
MT
476 if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE))
477 ipoib_warn(priv, "unable to move qp to error state\n");
478 /* Fall through */
839fcaba
MT
479 default:
480 return 0;
481 }
482}
483/* Adjust length of skb with fragments to match received data */
484static void skb_put_frags(struct sk_buff *skb, unsigned int hdr_space,
1812063b 485 unsigned int length, struct sk_buff *toskb)
839fcaba
MT
486{
487 int i, num_frags;
488 unsigned int size;
489
490 /* put header into skb */
491 size = min(length, hdr_space);
492 skb->tail += size;
493 skb->len += size;
494 length -= size;
495
496 num_frags = skb_shinfo(skb)->nr_frags;
497 for (i = 0; i < num_frags; i++) {
498 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
499
500 if (length == 0) {
501 /* don't need this page */
1812063b 502 skb_fill_page_desc(toskb, i, frag->page, 0, PAGE_SIZE);
839fcaba
MT
503 --skb_shinfo(skb)->nr_frags;
504 } else {
505 size = min(length, (unsigned) PAGE_SIZE);
506
507 frag->size = size;
508 skb->data_len += size;
509 skb->truesize += size;
510 skb->len += size;
511 length -= size;
512 }
513 }
514}
515
516void ipoib_cm_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
517{
518 struct ipoib_dev_priv *priv = netdev_priv(dev);
68e995a2 519 struct ipoib_cm_rx_buf *rx_ring;
1b524963 520 unsigned int wr_id = wc->wr_id & ~(IPOIB_OP_CM | IPOIB_OP_RECV);
1812063b 521 struct sk_buff *skb, *newskb;
839fcaba
MT
522 struct ipoib_cm_rx *p;
523 unsigned long flags;
524 u64 mapping[IPOIB_CM_RX_SG];
1812063b 525 int frags;
68e995a2 526 int has_srq;
839fcaba 527
a89875fc
RD
528 ipoib_dbg_data(priv, "cm recv completion: id %d, status: %d\n",
529 wr_id, wc->status);
839fcaba
MT
530
531 if (unlikely(wr_id >= ipoib_recvq_size)) {
1b524963 532 if (wr_id == (IPOIB_CM_RX_DRAIN_WRID & ~(IPOIB_OP_CM | IPOIB_OP_RECV))) {
518b1646
MT
533 spin_lock_irqsave(&priv->lock, flags);
534 list_splice_init(&priv->cm.rx_drain_list, &priv->cm.rx_reap_list);
535 ipoib_cm_start_rx_drain(priv);
536 queue_work(ipoib_workqueue, &priv->cm.rx_reap_task);
537 spin_unlock_irqrestore(&priv->lock, flags);
538 } else
539 ipoib_warn(priv, "cm recv completion event with wrid %d (> %d)\n",
540 wr_id, ipoib_recvq_size);
839fcaba
MT
541 return;
542 }
543
68e995a2
PS
544 p = wc->qp->qp_context;
545
546 has_srq = ipoib_cm_has_srq(dev);
547 rx_ring = has_srq ? priv->cm.srq_ring : p->rx_ring;
548
549 skb = rx_ring[wr_id].skb;
839fcaba
MT
550
551 if (unlikely(wc->status != IB_WC_SUCCESS)) {
552 ipoib_dbg(priv, "cm recv error "
553 "(status=%d, wrid=%d vend_err %x)\n",
554 wc->status, wr_id, wc->vendor_err);
de903512 555 ++dev->stats.rx_dropped;
68e995a2
PS
556 if (has_srq)
557 goto repost;
558 else {
559 if (!--p->recv_count) {
560 spin_lock_irqsave(&priv->lock, flags);
561 list_move(&p->list, &priv->cm.rx_reap_list);
562 spin_unlock_irqrestore(&priv->lock, flags);
563 queue_work(ipoib_workqueue, &priv->cm.rx_reap_task);
564 }
565 return;
566 }
839fcaba
MT
567 }
568
fd312561 569 if (unlikely(!(wr_id & IPOIB_CM_RX_UPDATE_MASK))) {
d6ef7d68 570 if (p && time_after_eq(jiffies, p->jiffies + IPOIB_CM_RX_UPDATE_TIME)) {
839fcaba
MT
571 spin_lock_irqsave(&priv->lock, flags);
572 p->jiffies = jiffies;
518b1646
MT
573 /* Move this entry to list head, but do not re-add it
574 * if it has been moved out of list. */
575 if (p->state == IPOIB_CM_RX_LIVE)
839fcaba
MT
576 list_move(&p->list, &priv->cm.passive_ids);
577 spin_unlock_irqrestore(&priv->lock, flags);
839fcaba
MT
578 }
579 }
580
1812063b
MT
581 frags = PAGE_ALIGN(wc->byte_len - min(wc->byte_len,
582 (unsigned)IPOIB_CM_HEAD_SIZE)) / PAGE_SIZE;
583
68e995a2 584 newskb = ipoib_cm_alloc_rx_skb(dev, rx_ring, wr_id, frags, mapping);
1812063b 585 if (unlikely(!newskb)) {
839fcaba
MT
586 /*
587 * If we can't allocate a new RX buffer, dump
588 * this packet and reuse the old buffer.
589 */
590 ipoib_dbg(priv, "failed to allocate receive buffer %d\n", wr_id);
de903512 591 ++dev->stats.rx_dropped;
839fcaba
MT
592 goto repost;
593 }
594
68e995a2
PS
595 ipoib_cm_dma_unmap_rx(priv, frags, rx_ring[wr_id].mapping);
596 memcpy(rx_ring[wr_id].mapping, mapping, (frags + 1) * sizeof *mapping);
839fcaba
MT
597
598 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
599 wc->byte_len, wc->slid);
600
1812063b 601 skb_put_frags(skb, IPOIB_CM_HEAD_SIZE, wc->byte_len, newskb);
839fcaba
MT
602
603 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
459a98ed 604 skb_reset_mac_header(skb);
839fcaba
MT
605 skb_pull(skb, IPOIB_ENCAP_LEN);
606
607 dev->last_rx = jiffies;
de903512
RD
608 ++dev->stats.rx_packets;
609 dev->stats.rx_bytes += skb->len;
839fcaba
MT
610
611 skb->dev = dev;
612 /* XXX get correct PACKET_ type here */
613 skb->pkt_type = PACKET_HOST;
8d1cc86a 614 netif_receive_skb(skb);
839fcaba
MT
615
616repost:
68e995a2
PS
617 if (has_srq) {
618 if (unlikely(ipoib_cm_post_receive_srq(dev, wr_id)))
619 ipoib_warn(priv, "ipoib_cm_post_receive_srq failed "
620 "for buf %d\n", wr_id);
621 } else {
622 if (unlikely(ipoib_cm_post_receive_nonsrq(dev, p, wr_id))) {
623 --p->recv_count;
624 ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq failed "
625 "for buf %d\n", wr_id);
626 }
627 }
839fcaba
MT
628}
629
630static inline int post_send(struct ipoib_dev_priv *priv,
631 struct ipoib_cm_tx *tx,
632 unsigned int wr_id,
633 u64 addr, int len)
634{
635 struct ib_send_wr *bad_wr;
636
7143740d
EC
637 priv->tx_sge[0].addr = addr;
638 priv->tx_sge[0].length = len;
839fcaba 639
2337f809 640 priv->tx_wr.wr_id = wr_id | IPOIB_OP_CM;
839fcaba
MT
641
642 return ib_post_send(tx->qp, &priv->tx_wr, &bad_wr);
643}
644
645void ipoib_cm_send(struct net_device *dev, struct sk_buff *skb, struct ipoib_cm_tx *tx)
646{
647 struct ipoib_dev_priv *priv = netdev_priv(dev);
648 struct ipoib_tx_buf *tx_req;
649 u64 addr;
650
651 if (unlikely(skb->len > tx->mtu)) {
652 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
653 skb->len, tx->mtu);
de903512
RD
654 ++dev->stats.tx_dropped;
655 ++dev->stats.tx_errors;
77d8e1ef 656 ipoib_cm_skb_too_long(dev, skb, tx->mtu - IPOIB_ENCAP_LEN);
839fcaba
MT
657 return;
658 }
659
660 ipoib_dbg_data(priv, "sending packet: head 0x%x length %d connection 0x%x\n",
661 tx->tx_head, skb->len, tx->qp->qp_num);
662
663 /*
664 * We put the skb into the tx_ring _before_ we call post_send()
665 * because it's entirely possible that the completion handler will
666 * run before we execute anything after the post_send(). That
667 * means we have to make sure everything is properly recorded and
668 * our state is consistent before we call post_send().
669 */
670 tx_req = &tx->tx_ring[tx->tx_head & (ipoib_sendq_size - 1)];
671 tx_req->skb = skb;
672 addr = ib_dma_map_single(priv->ca, skb->data, skb->len, DMA_TO_DEVICE);
673 if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
de903512 674 ++dev->stats.tx_errors;
839fcaba
MT
675 dev_kfree_skb_any(skb);
676 return;
677 }
678
7143740d 679 tx_req->mapping[0] = addr;
839fcaba
MT
680
681 if (unlikely(post_send(priv, tx, tx->tx_head & (ipoib_sendq_size - 1),
2337f809 682 addr, skb->len))) {
839fcaba 683 ipoib_warn(priv, "post_send failed\n");
de903512 684 ++dev->stats.tx_errors;
839fcaba
MT
685 ib_dma_unmap_single(priv->ca, addr, skb->len, DMA_TO_DEVICE);
686 dev_kfree_skb_any(skb);
687 } else {
688 dev->trans_start = jiffies;
689 ++tx->tx_head;
690
1b524963 691 if (++priv->tx_outstanding == ipoib_sendq_size) {
839fcaba
MT
692 ipoib_dbg(priv, "TX ring 0x%x full, stopping kernel net queue\n",
693 tx->qp->qp_num);
694 netif_stop_queue(dev);
839fcaba
MT
695 }
696 }
697}
698
1b524963 699void ipoib_cm_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
839fcaba
MT
700{
701 struct ipoib_dev_priv *priv = netdev_priv(dev);
1b524963
MT
702 struct ipoib_cm_tx *tx = wc->qp->qp_context;
703 unsigned int wr_id = wc->wr_id & ~IPOIB_OP_CM;
839fcaba
MT
704 struct ipoib_tx_buf *tx_req;
705 unsigned long flags;
706
a89875fc
RD
707 ipoib_dbg_data(priv, "cm send completion: id %d, status: %d\n",
708 wr_id, wc->status);
839fcaba
MT
709
710 if (unlikely(wr_id >= ipoib_sendq_size)) {
711 ipoib_warn(priv, "cm send completion event with wrid %d (> %d)\n",
712 wr_id, ipoib_sendq_size);
713 return;
714 }
715
716 tx_req = &tx->tx_ring[wr_id];
717
7143740d 718 ib_dma_unmap_single(priv->ca, tx_req->mapping[0], tx_req->skb->len, DMA_TO_DEVICE);
839fcaba
MT
719
720 /* FIXME: is this right? Shouldn't we only increment on success? */
de903512
RD
721 ++dev->stats.tx_packets;
722 dev->stats.tx_bytes += tx_req->skb->len;
839fcaba
MT
723
724 dev_kfree_skb_any(tx_req->skb);
725
726 spin_lock_irqsave(&priv->tx_lock, flags);
727 ++tx->tx_tail;
1b524963
MT
728 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
729 netif_queue_stopped(dev) &&
730 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
839fcaba 731 netif_wake_queue(dev);
839fcaba
MT
732
733 if (wc->status != IB_WC_SUCCESS &&
734 wc->status != IB_WC_WR_FLUSH_ERR) {
735 struct ipoib_neigh *neigh;
736
737 ipoib_dbg(priv, "failed cm send event "
738 "(status=%d, wrid=%d vend_err %x)\n",
739 wc->status, wr_id, wc->vendor_err);
740
741 spin_lock(&priv->lock);
742 neigh = tx->neigh;
743
744 if (neigh) {
745 neigh->cm = NULL;
746 list_del(&neigh->list);
747 if (neigh->ah)
748 ipoib_put_ah(neigh->ah);
749 ipoib_neigh_free(dev, neigh);
750
751 tx->neigh = NULL;
752 }
753
839fcaba
MT
754 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
755 list_move(&tx->list, &priv->cm.reap_list);
756 queue_work(ipoib_workqueue, &priv->cm.reap_task);
757 }
758
759 clear_bit(IPOIB_FLAG_OPER_UP, &tx->flags);
760
761 spin_unlock(&priv->lock);
762 }
763
764 spin_unlock_irqrestore(&priv->tx_lock, flags);
765}
766
839fcaba
MT
767int ipoib_cm_dev_open(struct net_device *dev)
768{
769 struct ipoib_dev_priv *priv = netdev_priv(dev);
770 int ret;
771
772 if (!IPOIB_CM_SUPPORTED(dev->dev_addr))
773 return 0;
774
775 priv->cm.id = ib_create_cm_id(priv->ca, ipoib_cm_rx_handler, dev);
776 if (IS_ERR(priv->cm.id)) {
777 printk(KERN_WARNING "%s: failed to create CM ID\n", priv->ca->name);
347fcfbe 778 ret = PTR_ERR(priv->cm.id);
518b1646 779 goto err_cm;
839fcaba
MT
780 }
781
782 ret = ib_cm_listen(priv->cm.id, cpu_to_be64(IPOIB_CM_IETF_ID | priv->qp->qp_num),
783 0, NULL);
784 if (ret) {
785 printk(KERN_WARNING "%s: failed to listen on ID 0x%llx\n", priv->ca->name,
786 IPOIB_CM_IETF_ID | priv->qp->qp_num);
518b1646 787 goto err_listen;
839fcaba 788 }
518b1646 789
839fcaba 790 return 0;
518b1646
MT
791
792err_listen:
793 ib_destroy_cm_id(priv->cm.id);
794err_cm:
795 priv->cm.id = NULL;
518b1646 796 return ret;
839fcaba
MT
797}
798
efcd9971
RD
799static void ipoib_cm_free_rx_reap_list(struct net_device *dev)
800{
801 struct ipoib_dev_priv *priv = netdev_priv(dev);
802 struct ipoib_cm_rx *rx, *n;
803 LIST_HEAD(list);
804
805 spin_lock_irq(&priv->lock);
806 list_splice_init(&priv->cm.rx_reap_list, &list);
807 spin_unlock_irq(&priv->lock);
808
809 list_for_each_entry_safe(rx, n, &list, list) {
810 ib_destroy_cm_id(rx->id);
811 ib_destroy_qp(rx->qp);
68e995a2
PS
812 if (!ipoib_cm_has_srq(dev)) {
813 ipoib_cm_free_rx_ring(priv->dev, rx->rx_ring);
814 spin_lock_irq(&priv->lock);
815 --priv->cm.nonsrq_conn_qp;
816 spin_unlock_irq(&priv->lock);
817 }
efcd9971
RD
818 kfree(rx);
819 }
820}
821
839fcaba
MT
822void ipoib_cm_dev_stop(struct net_device *dev)
823{
824 struct ipoib_dev_priv *priv = netdev_priv(dev);
efcd9971 825 struct ipoib_cm_rx *p;
518b1646 826 unsigned long begin;
518b1646 827 int ret;
839fcaba 828
347fcfbe 829 if (!IPOIB_CM_SUPPORTED(dev->dev_addr) || !priv->cm.id)
839fcaba
MT
830 return;
831
832 ib_destroy_cm_id(priv->cm.id);
347fcfbe 833 priv->cm.id = NULL;
518b1646 834
37aebbde 835 spin_lock_irq(&priv->lock);
839fcaba
MT
836 while (!list_empty(&priv->cm.passive_ids)) {
837 p = list_entry(priv->cm.passive_ids.next, typeof(*p), list);
518b1646
MT
838 list_move(&p->list, &priv->cm.rx_error_list);
839 p->state = IPOIB_CM_RX_ERROR;
37aebbde 840 spin_unlock_irq(&priv->lock);
518b1646
MT
841 ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
842 if (ret)
843 ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
844 spin_lock_irq(&priv->lock);
845 }
846
847 /* Wait for all RX to be drained */
848 begin = jiffies;
849
850 while (!list_empty(&priv->cm.rx_error_list) ||
851 !list_empty(&priv->cm.rx_flush_list) ||
852 !list_empty(&priv->cm.rx_drain_list)) {
8fd357a6 853 if (time_after(jiffies, begin + 5 * HZ)) {
518b1646
MT
854 ipoib_warn(priv, "RX drain timing out\n");
855
856 /*
857 * assume the HW is wedged and just free up everything.
858 */
ec229e5e
PS
859 list_splice_init(&priv->cm.rx_flush_list,
860 &priv->cm.rx_reap_list);
861 list_splice_init(&priv->cm.rx_error_list,
862 &priv->cm.rx_reap_list);
863 list_splice_init(&priv->cm.rx_drain_list,
864 &priv->cm.rx_reap_list);
518b1646
MT
865 break;
866 }
867 spin_unlock_irq(&priv->lock);
868 msleep(1);
2dfbfc37 869 ipoib_drain_cq(dev);
518b1646
MT
870 spin_lock_irq(&priv->lock);
871 }
872
518b1646
MT
873 spin_unlock_irq(&priv->lock);
874
efcd9971 875 ipoib_cm_free_rx_reap_list(dev);
839fcaba
MT
876
877 cancel_delayed_work(&priv->cm.stale_task);
878}
879
880static int ipoib_cm_rep_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
881{
882 struct ipoib_cm_tx *p = cm_id->context;
883 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
884 struct ipoib_cm_data *data = event->private_data;
885 struct sk_buff_head skqueue;
886 struct ib_qp_attr qp_attr;
887 int qp_attr_mask, ret;
888 struct sk_buff *skb;
839fcaba
MT
889
890 p->mtu = be32_to_cpu(data->mtu);
891
82c3aca6
MT
892 if (p->mtu <= IPOIB_ENCAP_LEN) {
893 ipoib_warn(priv, "Rejecting connection: mtu %d <= %d\n",
894 p->mtu, IPOIB_ENCAP_LEN);
839fcaba
MT
895 return -EINVAL;
896 }
897
898 qp_attr.qp_state = IB_QPS_RTR;
899 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
900 if (ret) {
901 ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
902 return ret;
903 }
904
905 qp_attr.rq_psn = 0 /* FIXME */;
906 ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
907 if (ret) {
908 ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
909 return ret;
910 }
911
912 qp_attr.qp_state = IB_QPS_RTS;
913 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
914 if (ret) {
915 ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
916 return ret;
917 }
918 ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
919 if (ret) {
920 ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
921 return ret;
922 }
923
924 skb_queue_head_init(&skqueue);
925
37aebbde 926 spin_lock_irq(&priv->lock);
839fcaba
MT
927 set_bit(IPOIB_FLAG_OPER_UP, &p->flags);
928 if (p->neigh)
929 while ((skb = __skb_dequeue(&p->neigh->queue)))
930 __skb_queue_tail(&skqueue, skb);
37aebbde 931 spin_unlock_irq(&priv->lock);
839fcaba
MT
932
933 while ((skb = __skb_dequeue(&skqueue))) {
934 skb->dev = p->dev;
935 if (dev_queue_xmit(skb))
936 ipoib_warn(priv, "dev_queue_xmit failed "
937 "to requeue packet\n");
938 }
939
940 ret = ib_send_cm_rtu(cm_id, NULL, 0);
941 if (ret) {
942 ipoib_warn(priv, "failed to send RTU: %d\n", ret);
943 return ret;
944 }
945 return 0;
946}
947
1b524963 948static struct ib_qp *ipoib_cm_create_tx_qp(struct net_device *dev, struct ipoib_cm_tx *tx)
839fcaba
MT
949{
950 struct ipoib_dev_priv *priv = netdev_priv(dev);
ede6bc04 951 struct ib_qp_init_attr attr = {
1b524963 952 .send_cq = priv->cq,
ede6bc04
DB
953 .recv_cq = priv->cq,
954 .srq = priv->cm.srq,
955 .cap.max_send_wr = ipoib_sendq_size,
956 .cap.max_send_sge = 1,
957 .sq_sig_type = IB_SIGNAL_ALL_WR,
958 .qp_type = IB_QPT_RC,
1b524963 959 .qp_context = tx
2337f809 960 };
ede6bc04 961
839fcaba
MT
962 return ib_create_qp(priv->pd, &attr);
963}
964
965static int ipoib_cm_send_req(struct net_device *dev,
966 struct ib_cm_id *id, struct ib_qp *qp,
967 u32 qpn,
968 struct ib_sa_path_rec *pathrec)
969{
970 struct ipoib_dev_priv *priv = netdev_priv(dev);
971 struct ipoib_cm_data data = {};
972 struct ib_cm_req_param req = {};
973
974 data.qpn = cpu_to_be32(priv->qp->qp_num);
975 data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
976
2337f809
RD
977 req.primary_path = pathrec;
978 req.alternate_path = NULL;
979 req.service_id = cpu_to_be64(IPOIB_CM_IETF_ID | qpn);
980 req.qp_num = qp->qp_num;
981 req.qp_type = qp->qp_type;
982 req.private_data = &data;
983 req.private_data_len = sizeof data;
984 req.flow_control = 0;
839fcaba 985
2337f809 986 req.starting_psn = 0; /* FIXME */
839fcaba
MT
987
988 /*
989 * Pick some arbitrary defaults here; we could make these
990 * module parameters if anyone cared about setting them.
991 */
2337f809
RD
992 req.responder_resources = 4;
993 req.remote_cm_response_timeout = 20;
994 req.local_cm_response_timeout = 20;
995 req.retry_count = 0; /* RFC draft warns against retries */
996 req.rnr_retry_count = 0; /* RFC draft warns against retries */
997 req.max_cm_retries = 15;
68e995a2 998 req.srq = ipoib_cm_has_srq(dev);
839fcaba
MT
999 return ib_send_cm_req(id, &req);
1000}
1001
1002static int ipoib_cm_modify_tx_init(struct net_device *dev,
1003 struct ib_cm_id *cm_id, struct ib_qp *qp)
1004{
1005 struct ipoib_dev_priv *priv = netdev_priv(dev);
1006 struct ib_qp_attr qp_attr;
1007 int qp_attr_mask, ret;
1008 ret = ib_find_cached_pkey(priv->ca, priv->port, priv->pkey, &qp_attr.pkey_index);
1009 if (ret) {
1010 ipoib_warn(priv, "pkey 0x%x not in cache: %d\n", priv->pkey, ret);
1011 return ret;
1012 }
1013
1014 qp_attr.qp_state = IB_QPS_INIT;
1015 qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE;
1016 qp_attr.port_num = priv->port;
1017 qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
1018
1019 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
1020 if (ret) {
1021 ipoib_warn(priv, "failed to modify tx QP to INIT: %d\n", ret);
1022 return ret;
1023 }
1024 return 0;
1025}
1026
1027static int ipoib_cm_tx_init(struct ipoib_cm_tx *p, u32 qpn,
1028 struct ib_sa_path_rec *pathrec)
1029{
1030 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
1031 int ret;
1032
1033 p->tx_ring = kzalloc(ipoib_sendq_size * sizeof *p->tx_ring,
1034 GFP_KERNEL);
1035 if (!p->tx_ring) {
1036 ipoib_warn(priv, "failed to allocate tx ring\n");
1037 ret = -ENOMEM;
1038 goto err_tx;
1039 }
1040
1b524963 1041 p->qp = ipoib_cm_create_tx_qp(p->dev, p);
839fcaba
MT
1042 if (IS_ERR(p->qp)) {
1043 ret = PTR_ERR(p->qp);
1044 ipoib_warn(priv, "failed to allocate tx qp: %d\n", ret);
1045 goto err_qp;
1046 }
1047
1048 p->id = ib_create_cm_id(priv->ca, ipoib_cm_tx_handler, p);
1049 if (IS_ERR(p->id)) {
1050 ret = PTR_ERR(p->id);
1051 ipoib_warn(priv, "failed to create tx cm id: %d\n", ret);
1052 goto err_id;
1053 }
1054
1055 ret = ipoib_cm_modify_tx_init(p->dev, p->id, p->qp);
1056 if (ret) {
1057 ipoib_warn(priv, "failed to modify tx qp to rtr: %d\n", ret);
1058 goto err_modify;
1059 }
1060
1061 ret = ipoib_cm_send_req(p->dev, p->id, p->qp, qpn, pathrec);
1062 if (ret) {
1063 ipoib_warn(priv, "failed to send cm req: %d\n", ret);
1064 goto err_send_cm;
1065 }
1066
1067 ipoib_dbg(priv, "Request connection 0x%x for gid " IPOIB_GID_FMT " qpn 0x%x\n",
1068 p->qp->qp_num, IPOIB_GID_ARG(pathrec->dgid), qpn);
1069
1070 return 0;
1071
1072err_send_cm:
1073err_modify:
1074 ib_destroy_cm_id(p->id);
1075err_id:
1076 p->id = NULL;
1077 ib_destroy_qp(p->qp);
839fcaba
MT
1078err_qp:
1079 p->qp = NULL;
839fcaba
MT
1080err_tx:
1081 return ret;
1082}
1083
1084static void ipoib_cm_tx_destroy(struct ipoib_cm_tx *p)
1085{
1086 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
1087 struct ipoib_tx_buf *tx_req;
1b524963
MT
1088 unsigned long flags;
1089 unsigned long begin;
839fcaba
MT
1090
1091 ipoib_dbg(priv, "Destroy active connection 0x%x head 0x%x tail 0x%x\n",
1092 p->qp ? p->qp->qp_num : 0, p->tx_head, p->tx_tail);
1093
1094 if (p->id)
1095 ib_destroy_cm_id(p->id);
1096
839fcaba 1097 if (p->tx_ring) {
1b524963
MT
1098 /* Wait for all sends to complete */
1099 begin = jiffies;
839fcaba 1100 while ((int) p->tx_tail - (int) p->tx_head < 0) {
1b524963
MT
1101 if (time_after(jiffies, begin + 5 * HZ)) {
1102 ipoib_warn(priv, "timing out; %d sends not completed\n",
1103 p->tx_head - p->tx_tail);
1104 goto timeout;
1105 }
1106
1107 msleep(1);
839fcaba 1108 }
1b524963
MT
1109 }
1110
1111timeout:
839fcaba 1112
1b524963
MT
1113 while ((int) p->tx_tail - (int) p->tx_head < 0) {
1114 tx_req = &p->tx_ring[p->tx_tail & (ipoib_sendq_size - 1)];
7143740d 1115 ib_dma_unmap_single(priv->ca, tx_req->mapping[0], tx_req->skb->len,
1b524963
MT
1116 DMA_TO_DEVICE);
1117 dev_kfree_skb_any(tx_req->skb);
1118 ++p->tx_tail;
1119 spin_lock_irqsave(&priv->tx_lock, flags);
1120 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
1121 netif_queue_stopped(p->dev) &&
1122 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
1123 netif_wake_queue(p->dev);
1124 spin_unlock_irqrestore(&priv->tx_lock, flags);
839fcaba
MT
1125 }
1126
1b524963
MT
1127 if (p->qp)
1128 ib_destroy_qp(p->qp);
1129
1130 kfree(p->tx_ring);
839fcaba
MT
1131 kfree(p);
1132}
1133
1134static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
1135 struct ib_cm_event *event)
1136{
1137 struct ipoib_cm_tx *tx = cm_id->context;
1138 struct ipoib_dev_priv *priv = netdev_priv(tx->dev);
1139 struct net_device *dev = priv->dev;
1140 struct ipoib_neigh *neigh;
839fcaba
MT
1141 int ret;
1142
1143 switch (event->event) {
1144 case IB_CM_DREQ_RECEIVED:
1145 ipoib_dbg(priv, "DREQ received.\n");
1146 ib_send_cm_drep(cm_id, NULL, 0);
1147 break;
1148 case IB_CM_REP_RECEIVED:
1149 ipoib_dbg(priv, "REP received.\n");
1150 ret = ipoib_cm_rep_handler(cm_id, event);
1151 if (ret)
1152 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
1153 NULL, 0, NULL, 0);
1154 break;
1155 case IB_CM_REQ_ERROR:
1156 case IB_CM_REJ_RECEIVED:
1157 case IB_CM_TIMEWAIT_EXIT:
1158 ipoib_dbg(priv, "CM error %d.\n", event->event);
37aebbde 1159 spin_lock_irq(&priv->tx_lock);
839fcaba
MT
1160 spin_lock(&priv->lock);
1161 neigh = tx->neigh;
1162
1163 if (neigh) {
1164 neigh->cm = NULL;
1165 list_del(&neigh->list);
1166 if (neigh->ah)
1167 ipoib_put_ah(neigh->ah);
1168 ipoib_neigh_free(dev, neigh);
1169
1170 tx->neigh = NULL;
1171 }
1172
1173 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
1174 list_move(&tx->list, &priv->cm.reap_list);
1175 queue_work(ipoib_workqueue, &priv->cm.reap_task);
1176 }
1177
1178 spin_unlock(&priv->lock);
37aebbde 1179 spin_unlock_irq(&priv->tx_lock);
839fcaba
MT
1180 break;
1181 default:
1182 break;
1183 }
1184
1185 return 0;
1186}
1187
1188struct ipoib_cm_tx *ipoib_cm_create_tx(struct net_device *dev, struct ipoib_path *path,
1189 struct ipoib_neigh *neigh)
1190{
1191 struct ipoib_dev_priv *priv = netdev_priv(dev);
1192 struct ipoib_cm_tx *tx;
1193
1194 tx = kzalloc(sizeof *tx, GFP_ATOMIC);
1195 if (!tx)
1196 return NULL;
1197
1198 neigh->cm = tx;
1199 tx->neigh = neigh;
1200 tx->path = path;
1201 tx->dev = dev;
1202 list_add(&tx->list, &priv->cm.start_list);
1203 set_bit(IPOIB_FLAG_INITIALIZED, &tx->flags);
1204 queue_work(ipoib_workqueue, &priv->cm.start_task);
1205 return tx;
1206}
1207
1208void ipoib_cm_destroy_tx(struct ipoib_cm_tx *tx)
1209{
1210 struct ipoib_dev_priv *priv = netdev_priv(tx->dev);
1211 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
1212 list_move(&tx->list, &priv->cm.reap_list);
1213 queue_work(ipoib_workqueue, &priv->cm.reap_task);
1214 ipoib_dbg(priv, "Reap connection for gid " IPOIB_GID_FMT "\n",
1215 IPOIB_GID_ARG(tx->neigh->dgid));
1216 tx->neigh = NULL;
1217 }
1218}
1219
1220static void ipoib_cm_tx_start(struct work_struct *work)
1221{
1222 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1223 cm.start_task);
1224 struct net_device *dev = priv->dev;
1225 struct ipoib_neigh *neigh;
1226 struct ipoib_cm_tx *p;
1227 unsigned long flags;
1228 int ret;
1229
1230 struct ib_sa_path_rec pathrec;
1231 u32 qpn;
1232
1233 spin_lock_irqsave(&priv->tx_lock, flags);
1234 spin_lock(&priv->lock);
1235 while (!list_empty(&priv->cm.start_list)) {
1236 p = list_entry(priv->cm.start_list.next, typeof(*p), list);
1237 list_del_init(&p->list);
1238 neigh = p->neigh;
1239 qpn = IPOIB_QPN(neigh->neighbour->ha);
1240 memcpy(&pathrec, &p->path->pathrec, sizeof pathrec);
1241 spin_unlock(&priv->lock);
1242 spin_unlock_irqrestore(&priv->tx_lock, flags);
1243 ret = ipoib_cm_tx_init(p, qpn, &pathrec);
1244 spin_lock_irqsave(&priv->tx_lock, flags);
1245 spin_lock(&priv->lock);
1246 if (ret) {
1247 neigh = p->neigh;
1248 if (neigh) {
1249 neigh->cm = NULL;
1250 list_del(&neigh->list);
1251 if (neigh->ah)
1252 ipoib_put_ah(neigh->ah);
1253 ipoib_neigh_free(dev, neigh);
1254 }
1255 list_del(&p->list);
1256 kfree(p);
1257 }
1258 }
1259 spin_unlock(&priv->lock);
1260 spin_unlock_irqrestore(&priv->tx_lock, flags);
1261}
1262
1263static void ipoib_cm_tx_reap(struct work_struct *work)
1264{
1265 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1266 cm.reap_task);
1267 struct ipoib_cm_tx *p;
839fcaba 1268
37aebbde 1269 spin_lock_irq(&priv->tx_lock);
839fcaba
MT
1270 spin_lock(&priv->lock);
1271 while (!list_empty(&priv->cm.reap_list)) {
1272 p = list_entry(priv->cm.reap_list.next, typeof(*p), list);
1273 list_del(&p->list);
1274 spin_unlock(&priv->lock);
37aebbde 1275 spin_unlock_irq(&priv->tx_lock);
839fcaba 1276 ipoib_cm_tx_destroy(p);
37aebbde 1277 spin_lock_irq(&priv->tx_lock);
839fcaba
MT
1278 spin_lock(&priv->lock);
1279 }
1280 spin_unlock(&priv->lock);
37aebbde 1281 spin_unlock_irq(&priv->tx_lock);
839fcaba
MT
1282}
1283
1284static void ipoib_cm_skb_reap(struct work_struct *work)
1285{
1286 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1287 cm.skb_task);
839fcaba 1288 struct sk_buff *skb;
839fcaba
MT
1289
1290 unsigned mtu = priv->mcast_mtu;
1291
37aebbde 1292 spin_lock_irq(&priv->tx_lock);
839fcaba
MT
1293 spin_lock(&priv->lock);
1294 while ((skb = skb_dequeue(&priv->cm.skb_queue))) {
1295 spin_unlock(&priv->lock);
37aebbde 1296 spin_unlock_irq(&priv->tx_lock);
839fcaba
MT
1297 if (skb->protocol == htons(ETH_P_IP))
1298 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
1299#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1300 else if (skb->protocol == htons(ETH_P_IPV6))
20089ca5 1301 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, priv->dev);
839fcaba
MT
1302#endif
1303 dev_kfree_skb_any(skb);
37aebbde 1304 spin_lock_irq(&priv->tx_lock);
839fcaba
MT
1305 spin_lock(&priv->lock);
1306 }
1307 spin_unlock(&priv->lock);
37aebbde 1308 spin_unlock_irq(&priv->tx_lock);
839fcaba
MT
1309}
1310
2337f809 1311void ipoib_cm_skb_too_long(struct net_device *dev, struct sk_buff *skb,
839fcaba
MT
1312 unsigned int mtu)
1313{
1314 struct ipoib_dev_priv *priv = netdev_priv(dev);
1315 int e = skb_queue_empty(&priv->cm.skb_queue);
1316
1317 if (skb->dst)
1318 skb->dst->ops->update_pmtu(skb->dst, mtu);
1319
1320 skb_queue_tail(&priv->cm.skb_queue, skb);
1321 if (e)
1322 queue_work(ipoib_workqueue, &priv->cm.skb_task);
1323}
1324
518b1646
MT
1325static void ipoib_cm_rx_reap(struct work_struct *work)
1326{
efcd9971
RD
1327 ipoib_cm_free_rx_reap_list(container_of(work, struct ipoib_dev_priv,
1328 cm.rx_reap_task)->dev);
518b1646
MT
1329}
1330
839fcaba
MT
1331static void ipoib_cm_stale_task(struct work_struct *work)
1332{
1333 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1334 cm.stale_task.work);
1335 struct ipoib_cm_rx *p;
518b1646 1336 int ret;
839fcaba 1337
37aebbde 1338 spin_lock_irq(&priv->lock);
839fcaba 1339 while (!list_empty(&priv->cm.passive_ids)) {
518b1646 1340 /* List is sorted by LRU, start from tail,
839fcaba
MT
1341 * stop when we see a recently used entry */
1342 p = list_entry(priv->cm.passive_ids.prev, typeof(*p), list);
60a596da 1343 if (time_before_eq(jiffies, p->jiffies + IPOIB_CM_RX_TIMEOUT))
839fcaba 1344 break;
518b1646
MT
1345 list_move(&p->list, &priv->cm.rx_error_list);
1346 p->state = IPOIB_CM_RX_ERROR;
37aebbde 1347 spin_unlock_irq(&priv->lock);
518b1646
MT
1348 ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
1349 if (ret)
1350 ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
37aebbde 1351 spin_lock_irq(&priv->lock);
839fcaba 1352 }
7c5b9ef8
MT
1353
1354 if (!list_empty(&priv->cm.passive_ids))
1355 queue_delayed_work(ipoib_workqueue,
1356 &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
37aebbde 1357 spin_unlock_irq(&priv->lock);
839fcaba
MT
1358}
1359
1360
2337f809 1361static ssize_t show_mode(struct device *d, struct device_attribute *attr,
839fcaba
MT
1362 char *buf)
1363{
1364 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(d));
1365
1366 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
1367 return sprintf(buf, "connected\n");
1368 else
1369 return sprintf(buf, "datagram\n");
1370}
1371
1372static ssize_t set_mode(struct device *d, struct device_attribute *attr,
1373 const char *buf, size_t count)
1374{
1375 struct net_device *dev = to_net_dev(d);
1376 struct ipoib_dev_priv *priv = netdev_priv(dev);
1377
1378 /* flush paths if we switch modes so that connections are restarted */
1379 if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
1380 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
1381 ipoib_warn(priv, "enabling connected mode "
1382 "will cause multicast packet drops\n");
1383 ipoib_flush_paths(dev);
1384 return count;
1385 }
1386
1387 if (!strcmp(buf, "datagram\n")) {
1388 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
1389 dev->mtu = min(priv->mcast_mtu, dev->mtu);
1390 ipoib_flush_paths(dev);
1391 return count;
1392 }
1393
1394 return -EINVAL;
1395}
1396
551fd612 1397static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO, show_mode, set_mode);
839fcaba
MT
1398
1399int ipoib_cm_add_mode_attr(struct net_device *dev)
1400{
1401 return device_create_file(&dev->dev, &dev_attr_mode);
1402}
1403
586a6934 1404static void ipoib_cm_create_srq(struct net_device *dev, int max_sge)
839fcaba
MT
1405{
1406 struct ipoib_dev_priv *priv = netdev_priv(dev);
1407 struct ib_srq_init_attr srq_init_attr = {
1408 .attr = {
1409 .max_wr = ipoib_recvq_size,
586a6934 1410 .max_sge = max_sge
839fcaba
MT
1411 }
1412 };
7b3687df
RD
1413
1414 priv->cm.srq = ib_create_srq(priv->pd, &srq_init_attr);
1415 if (IS_ERR(priv->cm.srq)) {
68e995a2
PS
1416 if (PTR_ERR(priv->cm.srq) != -ENOSYS)
1417 printk(KERN_WARNING "%s: failed to allocate SRQ, error %ld\n",
1418 priv->ca->name, PTR_ERR(priv->cm.srq));
7b3687df 1419 priv->cm.srq = NULL;
68e995a2 1420 return;
7b3687df
RD
1421 }
1422
1423 priv->cm.srq_ring = kzalloc(ipoib_recvq_size * sizeof *priv->cm.srq_ring,
1424 GFP_KERNEL);
1425 if (!priv->cm.srq_ring) {
68e995a2 1426 printk(KERN_WARNING "%s: failed to allocate CM SRQ ring (%d entries)\n",
7b3687df
RD
1427 priv->ca->name, ipoib_recvq_size);
1428 ib_destroy_srq(priv->cm.srq);
1429 priv->cm.srq = NULL;
7b3687df 1430 }
7b3687df
RD
1431}
1432
1433int ipoib_cm_dev_init(struct net_device *dev)
1434{
1435 struct ipoib_dev_priv *priv = netdev_priv(dev);
586a6934
PS
1436 int i, ret;
1437 struct ib_device_attr attr;
839fcaba
MT
1438
1439 INIT_LIST_HEAD(&priv->cm.passive_ids);
1440 INIT_LIST_HEAD(&priv->cm.reap_list);
1441 INIT_LIST_HEAD(&priv->cm.start_list);
518b1646
MT
1442 INIT_LIST_HEAD(&priv->cm.rx_error_list);
1443 INIT_LIST_HEAD(&priv->cm.rx_flush_list);
1444 INIT_LIST_HEAD(&priv->cm.rx_drain_list);
1445 INIT_LIST_HEAD(&priv->cm.rx_reap_list);
839fcaba
MT
1446 INIT_WORK(&priv->cm.start_task, ipoib_cm_tx_start);
1447 INIT_WORK(&priv->cm.reap_task, ipoib_cm_tx_reap);
1448 INIT_WORK(&priv->cm.skb_task, ipoib_cm_skb_reap);
518b1646 1449 INIT_WORK(&priv->cm.rx_reap_task, ipoib_cm_rx_reap);
839fcaba
MT
1450 INIT_DELAYED_WORK(&priv->cm.stale_task, ipoib_cm_stale_task);
1451
1452 skb_queue_head_init(&priv->cm.skb_queue);
1453
586a6934
PS
1454 ret = ib_query_device(priv->ca, &attr);
1455 if (ret) {
1456 printk(KERN_WARNING "ib_query_device() failed with %d\n", ret);
1457 return ret;
1458 }
1459
1460 ipoib_dbg(priv, "max_srq_sge=%d\n", attr.max_srq_sge);
1461
1462 attr.max_srq_sge = min_t(int, IPOIB_CM_RX_SG, attr.max_srq_sge);
1463 ipoib_cm_create_srq(dev, attr.max_srq_sge);
1464 if (ipoib_cm_has_srq(dev)) {
1465 priv->cm.max_cm_mtu = attr.max_srq_sge * PAGE_SIZE - 0x10;
1466 priv->cm.num_frags = attr.max_srq_sge;
1467 ipoib_dbg(priv, "max_cm_mtu = 0x%x, num_frags=%d\n",
1468 priv->cm.max_cm_mtu, priv->cm.num_frags);
1469 } else {
1470 priv->cm.max_cm_mtu = IPOIB_CM_MTU;
1471 priv->cm.num_frags = IPOIB_CM_RX_SG;
1472 }
1473
1474 for (i = 0; i < priv->cm.num_frags; ++i)
839fcaba
MT
1475 priv->cm.rx_sge[i].lkey = priv->mr->lkey;
1476
1477 priv->cm.rx_sge[0].length = IPOIB_CM_HEAD_SIZE;
586a6934 1478 for (i = 1; i < priv->cm.num_frags; ++i)
839fcaba
MT
1479 priv->cm.rx_sge[i].length = PAGE_SIZE;
1480 priv->cm.rx_wr.next = NULL;
1481 priv->cm.rx_wr.sg_list = priv->cm.rx_sge;
586a6934 1482 priv->cm.rx_wr.num_sge = priv->cm.num_frags;
68e995a2
PS
1483
1484 if (ipoib_cm_has_srq(dev)) {
1485 for (i = 0; i < ipoib_recvq_size; ++i) {
1486 if (!ipoib_cm_alloc_rx_skb(dev, priv->cm.srq_ring, i,
586a6934 1487 priv->cm.num_frags - 1,
68e995a2
PS
1488 priv->cm.srq_ring[i].mapping)) {
1489 ipoib_warn(priv, "failed to allocate "
1490 "receive buffer %d\n", i);
1491 ipoib_cm_dev_cleanup(dev);
1492 return -ENOMEM;
1493 }
7b3687df 1494
68e995a2
PS
1495 if (ipoib_cm_post_receive_srq(dev, i)) {
1496 ipoib_warn(priv, "ipoib_cm_post_receive_srq "
1497 "failed for buf %d\n", i);
1498 ipoib_cm_dev_cleanup(dev);
1499 return -EIO;
1500 }
839fcaba
MT
1501 }
1502 }
1503
1504 priv->dev->dev_addr[0] = IPOIB_FLAGS_RC;
1505 return 0;
1506}
1507
1508void ipoib_cm_dev_cleanup(struct net_device *dev)
1509{
1510 struct ipoib_dev_priv *priv = netdev_priv(dev);
1efb6144 1511 int ret;
839fcaba
MT
1512
1513 if (!priv->cm.srq)
1514 return;
1515
1516 ipoib_dbg(priv, "Cleanup ipoib connected mode.\n");
1517
1518 ret = ib_destroy_srq(priv->cm.srq);
1519 if (ret)
1520 ipoib_warn(priv, "ib_destroy_srq failed: %d\n", ret);
1521
1522 priv->cm.srq = NULL;
1523 if (!priv->cm.srq_ring)
1524 return;
1efb6144
RD
1525
1526 ipoib_cm_free_rx_ring(dev, priv->cm.srq_ring);
839fcaba
MT
1527 priv->cm.srq_ring = NULL;
1528}