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