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