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