Make sure omap cpufreq driver initializes after cpufreq framework and governors
[linux-ginger.git] / drivers / infiniband / ulp / ipoib / ipoib_cm.c
blob30bdf427ee6d099b907f13ffe0d05dfa9d53c39a
1 /*
2 * Copyright (c) 2006 Mellanox Technologies. All rights reserved
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
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.
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.
33 #include <rdma/ib_cm.h>
34 #include <net/dst.h>
35 #include <net/icmp.h>
36 #include <linux/icmpv6.h>
37 #include <linux/delay.h>
38 #include <linux/vmalloc.h>
40 #include "ipoib.h"
42 int ipoib_max_conn_qp = 128;
44 module_param_named(max_nonsrq_conn_qp, ipoib_max_conn_qp, int, 0444);
45 MODULE_PARM_DESC(max_nonsrq_conn_qp,
46 "Max number of connected-mode QPs per interface "
47 "(applied only if shared receive queue is not available)");
49 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
50 static int data_debug_level;
52 module_param_named(cm_data_debug_level, data_debug_level, int, 0644);
53 MODULE_PARM_DESC(cm_data_debug_level,
54 "Enable data path debug tracing for connected mode if > 0");
55 #endif
57 #define IPOIB_CM_IETF_ID 0x1000000000000000ULL
59 #define IPOIB_CM_RX_UPDATE_TIME (256 * HZ)
60 #define IPOIB_CM_RX_TIMEOUT (2 * 256 * HZ)
61 #define IPOIB_CM_RX_DELAY (3 * 256 * HZ)
62 #define IPOIB_CM_RX_UPDATE_MASK (0x3)
64 static struct ib_qp_attr ipoib_cm_err_attr = {
65 .qp_state = IB_QPS_ERR
68 #define IPOIB_CM_RX_DRAIN_WRID 0xffffffff
70 static struct ib_send_wr ipoib_cm_rx_drain_wr = {
71 .wr_id = IPOIB_CM_RX_DRAIN_WRID,
72 .opcode = IB_WR_SEND,
75 static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
76 struct ib_cm_event *event);
78 static void ipoib_cm_dma_unmap_rx(struct ipoib_dev_priv *priv, int frags,
79 u64 mapping[IPOIB_CM_RX_SG])
81 int i;
83 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
85 for (i = 0; i < frags; ++i)
86 ib_dma_unmap_single(priv->ca, mapping[i + 1], PAGE_SIZE, DMA_FROM_DEVICE);
89 static int ipoib_cm_post_receive_srq(struct net_device *dev, int id)
91 struct ipoib_dev_priv *priv = netdev_priv(dev);
92 struct ib_recv_wr *bad_wr;
93 int i, ret;
95 priv->cm.rx_wr.wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV;
97 for (i = 0; i < priv->cm.num_frags; ++i)
98 priv->cm.rx_sge[i].addr = priv->cm.srq_ring[id].mapping[i];
100 ret = ib_post_srq_recv(priv->cm.srq, &priv->cm.rx_wr, &bad_wr);
101 if (unlikely(ret)) {
102 ipoib_warn(priv, "post srq failed for buf %d (%d)\n", id, ret);
103 ipoib_cm_dma_unmap_rx(priv, priv->cm.num_frags - 1,
104 priv->cm.srq_ring[id].mapping);
105 dev_kfree_skb_any(priv->cm.srq_ring[id].skb);
106 priv->cm.srq_ring[id].skb = NULL;
109 return ret;
112 static int ipoib_cm_post_receive_nonsrq(struct net_device *dev,
113 struct ipoib_cm_rx *rx,
114 struct ib_recv_wr *wr,
115 struct ib_sge *sge, int id)
117 struct ipoib_dev_priv *priv = netdev_priv(dev);
118 struct ib_recv_wr *bad_wr;
119 int i, ret;
121 wr->wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV;
123 for (i = 0; i < IPOIB_CM_RX_SG; ++i)
124 sge[i].addr = rx->rx_ring[id].mapping[i];
126 ret = ib_post_recv(rx->qp, wr, &bad_wr);
127 if (unlikely(ret)) {
128 ipoib_warn(priv, "post recv failed for buf %d (%d)\n", id, ret);
129 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
130 rx->rx_ring[id].mapping);
131 dev_kfree_skb_any(rx->rx_ring[id].skb);
132 rx->rx_ring[id].skb = NULL;
135 return ret;
138 static struct sk_buff *ipoib_cm_alloc_rx_skb(struct net_device *dev,
139 struct ipoib_cm_rx_buf *rx_ring,
140 int id, int frags,
141 u64 mapping[IPOIB_CM_RX_SG])
143 struct ipoib_dev_priv *priv = netdev_priv(dev);
144 struct sk_buff *skb;
145 int i;
147 skb = dev_alloc_skb(IPOIB_CM_HEAD_SIZE + 12);
148 if (unlikely(!skb))
149 return NULL;
152 * IPoIB adds a 4 byte header. So we need 12 more bytes to align the
153 * IP header to a multiple of 16.
155 skb_reserve(skb, 12);
157 mapping[0] = ib_dma_map_single(priv->ca, skb->data, IPOIB_CM_HEAD_SIZE,
158 DMA_FROM_DEVICE);
159 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0]))) {
160 dev_kfree_skb_any(skb);
161 return NULL;
164 for (i = 0; i < frags; i++) {
165 struct page *page = alloc_page(GFP_ATOMIC);
167 if (!page)
168 goto partial_error;
169 skb_fill_page_desc(skb, i, page, 0, PAGE_SIZE);
171 mapping[i + 1] = ib_dma_map_page(priv->ca, skb_shinfo(skb)->frags[i].page,
172 0, PAGE_SIZE, DMA_FROM_DEVICE);
173 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[i + 1])))
174 goto partial_error;
177 rx_ring[id].skb = skb;
178 return skb;
180 partial_error:
182 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
184 for (; i > 0; --i)
185 ib_dma_unmap_single(priv->ca, mapping[i], PAGE_SIZE, DMA_FROM_DEVICE);
187 dev_kfree_skb_any(skb);
188 return NULL;
191 static void ipoib_cm_free_rx_ring(struct net_device *dev,
192 struct ipoib_cm_rx_buf *rx_ring)
194 struct ipoib_dev_priv *priv = netdev_priv(dev);
195 int i;
197 for (i = 0; i < ipoib_recvq_size; ++i)
198 if (rx_ring[i].skb) {
199 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
200 rx_ring[i].mapping);
201 dev_kfree_skb_any(rx_ring[i].skb);
204 vfree(rx_ring);
207 static void ipoib_cm_start_rx_drain(struct ipoib_dev_priv *priv)
209 struct ib_send_wr *bad_wr;
210 struct ipoib_cm_rx *p;
212 /* We only reserved 1 extra slot in CQ for drain WRs, so
213 * make sure we have at most 1 outstanding WR. */
214 if (list_empty(&priv->cm.rx_flush_list) ||
215 !list_empty(&priv->cm.rx_drain_list))
216 return;
219 * QPs on flush list are error state. This way, a "flush
220 * error" WC will be immediately generated for each WR we post.
222 p = list_entry(priv->cm.rx_flush_list.next, typeof(*p), list);
223 if (ib_post_send(p->qp, &ipoib_cm_rx_drain_wr, &bad_wr))
224 ipoib_warn(priv, "failed to post drain wr\n");
226 list_splice_init(&priv->cm.rx_flush_list, &priv->cm.rx_drain_list);
229 static void ipoib_cm_rx_event_handler(struct ib_event *event, void *ctx)
231 struct ipoib_cm_rx *p = ctx;
232 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
233 unsigned long flags;
235 if (event->event != IB_EVENT_QP_LAST_WQE_REACHED)
236 return;
238 spin_lock_irqsave(&priv->lock, flags);
239 list_move(&p->list, &priv->cm.rx_flush_list);
240 p->state = IPOIB_CM_RX_FLUSH;
241 ipoib_cm_start_rx_drain(priv);
242 spin_unlock_irqrestore(&priv->lock, flags);
245 static struct ib_qp *ipoib_cm_create_rx_qp(struct net_device *dev,
246 struct ipoib_cm_rx *p)
248 struct ipoib_dev_priv *priv = netdev_priv(dev);
249 struct ib_qp_init_attr attr = {
250 .event_handler = ipoib_cm_rx_event_handler,
251 .send_cq = priv->recv_cq, /* For drain WR */
252 .recv_cq = priv->recv_cq,
253 .srq = priv->cm.srq,
254 .cap.max_send_wr = 1, /* For drain WR */
255 .cap.max_send_sge = 1, /* FIXME: 0 Seems not to work */
256 .sq_sig_type = IB_SIGNAL_ALL_WR,
257 .qp_type = IB_QPT_RC,
258 .qp_context = p,
261 if (!ipoib_cm_has_srq(dev)) {
262 attr.cap.max_recv_wr = ipoib_recvq_size;
263 attr.cap.max_recv_sge = IPOIB_CM_RX_SG;
266 return ib_create_qp(priv->pd, &attr);
269 static int ipoib_cm_modify_rx_qp(struct net_device *dev,
270 struct ib_cm_id *cm_id, struct ib_qp *qp,
271 unsigned psn)
273 struct ipoib_dev_priv *priv = netdev_priv(dev);
274 struct ib_qp_attr qp_attr;
275 int qp_attr_mask, ret;
277 qp_attr.qp_state = IB_QPS_INIT;
278 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
279 if (ret) {
280 ipoib_warn(priv, "failed to init QP attr for INIT: %d\n", ret);
281 return ret;
283 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
284 if (ret) {
285 ipoib_warn(priv, "failed to modify QP to INIT: %d\n", ret);
286 return ret;
288 qp_attr.qp_state = IB_QPS_RTR;
289 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
290 if (ret) {
291 ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
292 return ret;
294 qp_attr.rq_psn = psn;
295 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
296 if (ret) {
297 ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
298 return ret;
302 * Current Mellanox HCA firmware won't generate completions
303 * with error for drain WRs unless the QP has been moved to
304 * RTS first. This work-around leaves a window where a QP has
305 * moved to error asynchronously, but this will eventually get
306 * fixed in firmware, so let's not error out if modify QP
307 * fails.
309 qp_attr.qp_state = IB_QPS_RTS;
310 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
311 if (ret) {
312 ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
313 return 0;
315 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
316 if (ret) {
317 ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
318 return 0;
321 return 0;
324 static void ipoib_cm_init_rx_wr(struct net_device *dev,
325 struct ib_recv_wr *wr,
326 struct ib_sge *sge)
328 struct ipoib_dev_priv *priv = netdev_priv(dev);
329 int i;
331 for (i = 0; i < priv->cm.num_frags; ++i)
332 sge[i].lkey = priv->mr->lkey;
334 sge[0].length = IPOIB_CM_HEAD_SIZE;
335 for (i = 1; i < priv->cm.num_frags; ++i)
336 sge[i].length = PAGE_SIZE;
338 wr->next = NULL;
339 wr->sg_list = sge;
340 wr->num_sge = priv->cm.num_frags;
343 static int ipoib_cm_nonsrq_init_rx(struct net_device *dev, struct ib_cm_id *cm_id,
344 struct ipoib_cm_rx *rx)
346 struct ipoib_dev_priv *priv = netdev_priv(dev);
347 struct {
348 struct ib_recv_wr wr;
349 struct ib_sge sge[IPOIB_CM_RX_SG];
350 } *t;
351 int ret;
352 int i;
354 rx->rx_ring = vmalloc(ipoib_recvq_size * sizeof *rx->rx_ring);
355 if (!rx->rx_ring) {
356 printk(KERN_WARNING "%s: failed to allocate CM non-SRQ ring (%d entries)\n",
357 priv->ca->name, ipoib_recvq_size);
358 return -ENOMEM;
361 memset(rx->rx_ring, 0, ipoib_recvq_size * sizeof *rx->rx_ring);
363 t = kmalloc(sizeof *t, GFP_KERNEL);
364 if (!t) {
365 ret = -ENOMEM;
366 goto err_free;
369 ipoib_cm_init_rx_wr(dev, &t->wr, t->sge);
371 spin_lock_irq(&priv->lock);
373 if (priv->cm.nonsrq_conn_qp >= ipoib_max_conn_qp) {
374 spin_unlock_irq(&priv->lock);
375 ib_send_cm_rej(cm_id, IB_CM_REJ_NO_QP, NULL, 0, NULL, 0);
376 ret = -EINVAL;
377 goto err_free;
378 } else
379 ++priv->cm.nonsrq_conn_qp;
381 spin_unlock_irq(&priv->lock);
383 for (i = 0; i < ipoib_recvq_size; ++i) {
384 if (!ipoib_cm_alloc_rx_skb(dev, rx->rx_ring, i, IPOIB_CM_RX_SG - 1,
385 rx->rx_ring[i].mapping)) {
386 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
387 ret = -ENOMEM;
388 goto err_count;
390 ret = ipoib_cm_post_receive_nonsrq(dev, rx, &t->wr, t->sge, i);
391 if (ret) {
392 ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq "
393 "failed for buf %d\n", i);
394 ret = -EIO;
395 goto err_count;
399 rx->recv_count = ipoib_recvq_size;
401 kfree(t);
403 return 0;
405 err_count:
406 spin_lock_irq(&priv->lock);
407 --priv->cm.nonsrq_conn_qp;
408 spin_unlock_irq(&priv->lock);
410 err_free:
411 kfree(t);
412 ipoib_cm_free_rx_ring(dev, rx->rx_ring);
414 return ret;
417 static int ipoib_cm_send_rep(struct net_device *dev, struct ib_cm_id *cm_id,
418 struct ib_qp *qp, struct ib_cm_req_event_param *req,
419 unsigned psn)
421 struct ipoib_dev_priv *priv = netdev_priv(dev);
422 struct ipoib_cm_data data = {};
423 struct ib_cm_rep_param rep = {};
425 data.qpn = cpu_to_be32(priv->qp->qp_num);
426 data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
428 rep.private_data = &data;
429 rep.private_data_len = sizeof data;
430 rep.flow_control = 0;
431 rep.rnr_retry_count = req->rnr_retry_count;
432 rep.srq = ipoib_cm_has_srq(dev);
433 rep.qp_num = qp->qp_num;
434 rep.starting_psn = psn;
435 return ib_send_cm_rep(cm_id, &rep);
438 static int ipoib_cm_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
440 struct net_device *dev = cm_id->context;
441 struct ipoib_dev_priv *priv = netdev_priv(dev);
442 struct ipoib_cm_rx *p;
443 unsigned psn;
444 int ret;
446 ipoib_dbg(priv, "REQ arrived\n");
447 p = kzalloc(sizeof *p, GFP_KERNEL);
448 if (!p)
449 return -ENOMEM;
450 p->dev = dev;
451 p->id = cm_id;
452 cm_id->context = p;
453 p->state = IPOIB_CM_RX_LIVE;
454 p->jiffies = jiffies;
455 INIT_LIST_HEAD(&p->list);
457 p->qp = ipoib_cm_create_rx_qp(dev, p);
458 if (IS_ERR(p->qp)) {
459 ret = PTR_ERR(p->qp);
460 goto err_qp;
463 psn = random32() & 0xffffff;
464 ret = ipoib_cm_modify_rx_qp(dev, cm_id, p->qp, psn);
465 if (ret)
466 goto err_modify;
468 if (!ipoib_cm_has_srq(dev)) {
469 ret = ipoib_cm_nonsrq_init_rx(dev, cm_id, p);
470 if (ret)
471 goto err_modify;
474 spin_lock_irq(&priv->lock);
475 queue_delayed_work(ipoib_workqueue,
476 &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
477 /* Add this entry to passive ids list head, but do not re-add it
478 * if IB_EVENT_QP_LAST_WQE_REACHED has moved it to flush list. */
479 p->jiffies = jiffies;
480 if (p->state == IPOIB_CM_RX_LIVE)
481 list_move(&p->list, &priv->cm.passive_ids);
482 spin_unlock_irq(&priv->lock);
484 ret = ipoib_cm_send_rep(dev, cm_id, p->qp, &event->param.req_rcvd, psn);
485 if (ret) {
486 ipoib_warn(priv, "failed to send REP: %d\n", ret);
487 if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE))
488 ipoib_warn(priv, "unable to move qp to error state\n");
490 return 0;
492 err_modify:
493 ib_destroy_qp(p->qp);
494 err_qp:
495 kfree(p);
496 return ret;
499 static int ipoib_cm_rx_handler(struct ib_cm_id *cm_id,
500 struct ib_cm_event *event)
502 struct ipoib_cm_rx *p;
503 struct ipoib_dev_priv *priv;
505 switch (event->event) {
506 case IB_CM_REQ_RECEIVED:
507 return ipoib_cm_req_handler(cm_id, event);
508 case IB_CM_DREQ_RECEIVED:
509 p = cm_id->context;
510 ib_send_cm_drep(cm_id, NULL, 0);
511 /* Fall through */
512 case IB_CM_REJ_RECEIVED:
513 p = cm_id->context;
514 priv = netdev_priv(p->dev);
515 if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE))
516 ipoib_warn(priv, "unable to move qp to error state\n");
517 /* Fall through */
518 default:
519 return 0;
522 /* Adjust length of skb with fragments to match received data */
523 static void skb_put_frags(struct sk_buff *skb, unsigned int hdr_space,
524 unsigned int length, struct sk_buff *toskb)
526 int i, num_frags;
527 unsigned int size;
529 /* put header into skb */
530 size = min(length, hdr_space);
531 skb->tail += size;
532 skb->len += size;
533 length -= size;
535 num_frags = skb_shinfo(skb)->nr_frags;
536 for (i = 0; i < num_frags; i++) {
537 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
539 if (length == 0) {
540 /* don't need this page */
541 skb_fill_page_desc(toskb, i, frag->page, 0, PAGE_SIZE);
542 --skb_shinfo(skb)->nr_frags;
543 } else {
544 size = min(length, (unsigned) PAGE_SIZE);
546 frag->size = size;
547 skb->data_len += size;
548 skb->truesize += size;
549 skb->len += size;
550 length -= size;
555 void ipoib_cm_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
557 struct ipoib_dev_priv *priv = netdev_priv(dev);
558 struct ipoib_cm_rx_buf *rx_ring;
559 unsigned int wr_id = wc->wr_id & ~(IPOIB_OP_CM | IPOIB_OP_RECV);
560 struct sk_buff *skb, *newskb;
561 struct ipoib_cm_rx *p;
562 unsigned long flags;
563 u64 mapping[IPOIB_CM_RX_SG];
564 int frags;
565 int has_srq;
566 struct sk_buff *small_skb;
568 ipoib_dbg_data(priv, "cm recv completion: id %d, status: %d\n",
569 wr_id, wc->status);
571 if (unlikely(wr_id >= ipoib_recvq_size)) {
572 if (wr_id == (IPOIB_CM_RX_DRAIN_WRID & ~(IPOIB_OP_CM | IPOIB_OP_RECV))) {
573 spin_lock_irqsave(&priv->lock, flags);
574 list_splice_init(&priv->cm.rx_drain_list, &priv->cm.rx_reap_list);
575 ipoib_cm_start_rx_drain(priv);
576 queue_work(ipoib_workqueue, &priv->cm.rx_reap_task);
577 spin_unlock_irqrestore(&priv->lock, flags);
578 } else
579 ipoib_warn(priv, "cm recv completion event with wrid %d (> %d)\n",
580 wr_id, ipoib_recvq_size);
581 return;
584 p = wc->qp->qp_context;
586 has_srq = ipoib_cm_has_srq(dev);
587 rx_ring = has_srq ? priv->cm.srq_ring : p->rx_ring;
589 skb = rx_ring[wr_id].skb;
591 if (unlikely(wc->status != IB_WC_SUCCESS)) {
592 ipoib_dbg(priv, "cm recv error "
593 "(status=%d, wrid=%d vend_err %x)\n",
594 wc->status, wr_id, wc->vendor_err);
595 ++dev->stats.rx_dropped;
596 if (has_srq)
597 goto repost;
598 else {
599 if (!--p->recv_count) {
600 spin_lock_irqsave(&priv->lock, flags);
601 list_move(&p->list, &priv->cm.rx_reap_list);
602 spin_unlock_irqrestore(&priv->lock, flags);
603 queue_work(ipoib_workqueue, &priv->cm.rx_reap_task);
605 return;
609 if (unlikely(!(wr_id & IPOIB_CM_RX_UPDATE_MASK))) {
610 if (p && time_after_eq(jiffies, p->jiffies + IPOIB_CM_RX_UPDATE_TIME)) {
611 spin_lock_irqsave(&priv->lock, flags);
612 p->jiffies = jiffies;
613 /* Move this entry to list head, but do not re-add it
614 * if it has been moved out of list. */
615 if (p->state == IPOIB_CM_RX_LIVE)
616 list_move(&p->list, &priv->cm.passive_ids);
617 spin_unlock_irqrestore(&priv->lock, flags);
621 if (wc->byte_len < IPOIB_CM_COPYBREAK) {
622 int dlen = wc->byte_len;
624 small_skb = dev_alloc_skb(dlen + 12);
625 if (small_skb) {
626 skb_reserve(small_skb, 12);
627 ib_dma_sync_single_for_cpu(priv->ca, rx_ring[wr_id].mapping[0],
628 dlen, DMA_FROM_DEVICE);
629 skb_copy_from_linear_data(skb, small_skb->data, dlen);
630 ib_dma_sync_single_for_device(priv->ca, rx_ring[wr_id].mapping[0],
631 dlen, DMA_FROM_DEVICE);
632 skb_put(small_skb, dlen);
633 skb = small_skb;
634 goto copied;
638 frags = PAGE_ALIGN(wc->byte_len - min(wc->byte_len,
639 (unsigned)IPOIB_CM_HEAD_SIZE)) / PAGE_SIZE;
641 newskb = ipoib_cm_alloc_rx_skb(dev, rx_ring, wr_id, frags, mapping);
642 if (unlikely(!newskb)) {
644 * If we can't allocate a new RX buffer, dump
645 * this packet and reuse the old buffer.
647 ipoib_dbg(priv, "failed to allocate receive buffer %d\n", wr_id);
648 ++dev->stats.rx_dropped;
649 goto repost;
652 ipoib_cm_dma_unmap_rx(priv, frags, rx_ring[wr_id].mapping);
653 memcpy(rx_ring[wr_id].mapping, mapping, (frags + 1) * sizeof *mapping);
655 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
656 wc->byte_len, wc->slid);
658 skb_put_frags(skb, IPOIB_CM_HEAD_SIZE, wc->byte_len, newskb);
660 copied:
661 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
662 skb_reset_mac_header(skb);
663 skb_pull(skb, IPOIB_ENCAP_LEN);
665 ++dev->stats.rx_packets;
666 dev->stats.rx_bytes += skb->len;
668 skb->dev = dev;
669 /* XXX get correct PACKET_ type here */
670 skb->pkt_type = PACKET_HOST;
671 netif_receive_skb(skb);
673 repost:
674 if (has_srq) {
675 if (unlikely(ipoib_cm_post_receive_srq(dev, wr_id)))
676 ipoib_warn(priv, "ipoib_cm_post_receive_srq failed "
677 "for buf %d\n", wr_id);
678 } else {
679 if (unlikely(ipoib_cm_post_receive_nonsrq(dev, p,
680 &priv->cm.rx_wr,
681 priv->cm.rx_sge,
682 wr_id))) {
683 --p->recv_count;
684 ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq failed "
685 "for buf %d\n", wr_id);
690 static inline int post_send(struct ipoib_dev_priv *priv,
691 struct ipoib_cm_tx *tx,
692 unsigned int wr_id,
693 u64 addr, int len)
695 struct ib_send_wr *bad_wr;
697 priv->tx_sge[0].addr = addr;
698 priv->tx_sge[0].length = len;
700 priv->tx_wr.num_sge = 1;
701 priv->tx_wr.wr_id = wr_id | IPOIB_OP_CM;
703 return ib_post_send(tx->qp, &priv->tx_wr, &bad_wr);
706 void ipoib_cm_send(struct net_device *dev, struct sk_buff *skb, struct ipoib_cm_tx *tx)
708 struct ipoib_dev_priv *priv = netdev_priv(dev);
709 struct ipoib_cm_tx_buf *tx_req;
710 u64 addr;
712 if (unlikely(skb->len > tx->mtu)) {
713 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
714 skb->len, tx->mtu);
715 ++dev->stats.tx_dropped;
716 ++dev->stats.tx_errors;
717 ipoib_cm_skb_too_long(dev, skb, tx->mtu - IPOIB_ENCAP_LEN);
718 return;
721 ipoib_dbg_data(priv, "sending packet: head 0x%x length %d connection 0x%x\n",
722 tx->tx_head, skb->len, tx->qp->qp_num);
725 * We put the skb into the tx_ring _before_ we call post_send()
726 * because it's entirely possible that the completion handler will
727 * run before we execute anything after the post_send(). That
728 * means we have to make sure everything is properly recorded and
729 * our state is consistent before we call post_send().
731 tx_req = &tx->tx_ring[tx->tx_head & (ipoib_sendq_size - 1)];
732 tx_req->skb = skb;
733 addr = ib_dma_map_single(priv->ca, skb->data, skb->len, DMA_TO_DEVICE);
734 if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
735 ++dev->stats.tx_errors;
736 dev_kfree_skb_any(skb);
737 return;
740 tx_req->mapping = addr;
742 if (unlikely(post_send(priv, tx, tx->tx_head & (ipoib_sendq_size - 1),
743 addr, skb->len))) {
744 ipoib_warn(priv, "post_send failed\n");
745 ++dev->stats.tx_errors;
746 ib_dma_unmap_single(priv->ca, addr, skb->len, DMA_TO_DEVICE);
747 dev_kfree_skb_any(skb);
748 } else {
749 dev->trans_start = jiffies;
750 ++tx->tx_head;
752 if (++priv->tx_outstanding == ipoib_sendq_size) {
753 ipoib_dbg(priv, "TX ring 0x%x full, stopping kernel net queue\n",
754 tx->qp->qp_num);
755 netif_stop_queue(dev);
760 void ipoib_cm_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
762 struct ipoib_dev_priv *priv = netdev_priv(dev);
763 struct ipoib_cm_tx *tx = wc->qp->qp_context;
764 unsigned int wr_id = wc->wr_id & ~IPOIB_OP_CM;
765 struct ipoib_cm_tx_buf *tx_req;
766 unsigned long flags;
768 ipoib_dbg_data(priv, "cm send completion: id %d, status: %d\n",
769 wr_id, wc->status);
771 if (unlikely(wr_id >= ipoib_sendq_size)) {
772 ipoib_warn(priv, "cm send completion event with wrid %d (> %d)\n",
773 wr_id, ipoib_sendq_size);
774 return;
777 tx_req = &tx->tx_ring[wr_id];
779 ib_dma_unmap_single(priv->ca, tx_req->mapping, tx_req->skb->len, DMA_TO_DEVICE);
781 /* FIXME: is this right? Shouldn't we only increment on success? */
782 ++dev->stats.tx_packets;
783 dev->stats.tx_bytes += tx_req->skb->len;
785 dev_kfree_skb_any(tx_req->skb);
787 netif_tx_lock(dev);
789 ++tx->tx_tail;
790 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
791 netif_queue_stopped(dev) &&
792 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
793 netif_wake_queue(dev);
795 if (wc->status != IB_WC_SUCCESS &&
796 wc->status != IB_WC_WR_FLUSH_ERR) {
797 struct ipoib_neigh *neigh;
799 ipoib_dbg(priv, "failed cm send event "
800 "(status=%d, wrid=%d vend_err %x)\n",
801 wc->status, wr_id, wc->vendor_err);
803 spin_lock_irqsave(&priv->lock, flags);
804 neigh = tx->neigh;
806 if (neigh) {
807 neigh->cm = NULL;
808 list_del(&neigh->list);
809 if (neigh->ah)
810 ipoib_put_ah(neigh->ah);
811 ipoib_neigh_free(dev, neigh);
813 tx->neigh = NULL;
816 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
817 list_move(&tx->list, &priv->cm.reap_list);
818 queue_work(ipoib_workqueue, &priv->cm.reap_task);
821 clear_bit(IPOIB_FLAG_OPER_UP, &tx->flags);
823 spin_unlock_irqrestore(&priv->lock, flags);
826 netif_tx_unlock(dev);
829 int ipoib_cm_dev_open(struct net_device *dev)
831 struct ipoib_dev_priv *priv = netdev_priv(dev);
832 int ret;
834 if (!IPOIB_CM_SUPPORTED(dev->dev_addr))
835 return 0;
837 priv->cm.id = ib_create_cm_id(priv->ca, ipoib_cm_rx_handler, dev);
838 if (IS_ERR(priv->cm.id)) {
839 printk(KERN_WARNING "%s: failed to create CM ID\n", priv->ca->name);
840 ret = PTR_ERR(priv->cm.id);
841 goto err_cm;
844 ret = ib_cm_listen(priv->cm.id, cpu_to_be64(IPOIB_CM_IETF_ID | priv->qp->qp_num),
845 0, NULL);
846 if (ret) {
847 printk(KERN_WARNING "%s: failed to listen on ID 0x%llx\n", priv->ca->name,
848 IPOIB_CM_IETF_ID | priv->qp->qp_num);
849 goto err_listen;
852 return 0;
854 err_listen:
855 ib_destroy_cm_id(priv->cm.id);
856 err_cm:
857 priv->cm.id = NULL;
858 return ret;
861 static void ipoib_cm_free_rx_reap_list(struct net_device *dev)
863 struct ipoib_dev_priv *priv = netdev_priv(dev);
864 struct ipoib_cm_rx *rx, *n;
865 LIST_HEAD(list);
867 spin_lock_irq(&priv->lock);
868 list_splice_init(&priv->cm.rx_reap_list, &list);
869 spin_unlock_irq(&priv->lock);
871 list_for_each_entry_safe(rx, n, &list, list) {
872 ib_destroy_cm_id(rx->id);
873 ib_destroy_qp(rx->qp);
874 if (!ipoib_cm_has_srq(dev)) {
875 ipoib_cm_free_rx_ring(priv->dev, rx->rx_ring);
876 spin_lock_irq(&priv->lock);
877 --priv->cm.nonsrq_conn_qp;
878 spin_unlock_irq(&priv->lock);
880 kfree(rx);
884 void ipoib_cm_dev_stop(struct net_device *dev)
886 struct ipoib_dev_priv *priv = netdev_priv(dev);
887 struct ipoib_cm_rx *p;
888 unsigned long begin;
889 int ret;
891 if (!IPOIB_CM_SUPPORTED(dev->dev_addr) || !priv->cm.id)
892 return;
894 ib_destroy_cm_id(priv->cm.id);
895 priv->cm.id = NULL;
897 spin_lock_irq(&priv->lock);
898 while (!list_empty(&priv->cm.passive_ids)) {
899 p = list_entry(priv->cm.passive_ids.next, typeof(*p), list);
900 list_move(&p->list, &priv->cm.rx_error_list);
901 p->state = IPOIB_CM_RX_ERROR;
902 spin_unlock_irq(&priv->lock);
903 ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
904 if (ret)
905 ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
906 spin_lock_irq(&priv->lock);
909 /* Wait for all RX to be drained */
910 begin = jiffies;
912 while (!list_empty(&priv->cm.rx_error_list) ||
913 !list_empty(&priv->cm.rx_flush_list) ||
914 !list_empty(&priv->cm.rx_drain_list)) {
915 if (time_after(jiffies, begin + 5 * HZ)) {
916 ipoib_warn(priv, "RX drain timing out\n");
919 * assume the HW is wedged and just free up everything.
921 list_splice_init(&priv->cm.rx_flush_list,
922 &priv->cm.rx_reap_list);
923 list_splice_init(&priv->cm.rx_error_list,
924 &priv->cm.rx_reap_list);
925 list_splice_init(&priv->cm.rx_drain_list,
926 &priv->cm.rx_reap_list);
927 break;
929 spin_unlock_irq(&priv->lock);
930 msleep(1);
931 ipoib_drain_cq(dev);
932 spin_lock_irq(&priv->lock);
935 spin_unlock_irq(&priv->lock);
937 ipoib_cm_free_rx_reap_list(dev);
939 cancel_delayed_work(&priv->cm.stale_task);
942 static int ipoib_cm_rep_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
944 struct ipoib_cm_tx *p = cm_id->context;
945 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
946 struct ipoib_cm_data *data = event->private_data;
947 struct sk_buff_head skqueue;
948 struct ib_qp_attr qp_attr;
949 int qp_attr_mask, ret;
950 struct sk_buff *skb;
952 p->mtu = be32_to_cpu(data->mtu);
954 if (p->mtu <= IPOIB_ENCAP_LEN) {
955 ipoib_warn(priv, "Rejecting connection: mtu %d <= %d\n",
956 p->mtu, IPOIB_ENCAP_LEN);
957 return -EINVAL;
960 qp_attr.qp_state = IB_QPS_RTR;
961 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
962 if (ret) {
963 ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
964 return ret;
967 qp_attr.rq_psn = 0 /* FIXME */;
968 ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
969 if (ret) {
970 ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
971 return ret;
974 qp_attr.qp_state = IB_QPS_RTS;
975 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
976 if (ret) {
977 ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
978 return ret;
980 ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
981 if (ret) {
982 ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
983 return ret;
986 skb_queue_head_init(&skqueue);
988 spin_lock_irq(&priv->lock);
989 set_bit(IPOIB_FLAG_OPER_UP, &p->flags);
990 if (p->neigh)
991 while ((skb = __skb_dequeue(&p->neigh->queue)))
992 __skb_queue_tail(&skqueue, skb);
993 spin_unlock_irq(&priv->lock);
995 while ((skb = __skb_dequeue(&skqueue))) {
996 skb->dev = p->dev;
997 if (dev_queue_xmit(skb))
998 ipoib_warn(priv, "dev_queue_xmit failed "
999 "to requeue packet\n");
1002 ret = ib_send_cm_rtu(cm_id, NULL, 0);
1003 if (ret) {
1004 ipoib_warn(priv, "failed to send RTU: %d\n", ret);
1005 return ret;
1007 return 0;
1010 static struct ib_qp *ipoib_cm_create_tx_qp(struct net_device *dev, struct ipoib_cm_tx *tx)
1012 struct ipoib_dev_priv *priv = netdev_priv(dev);
1013 struct ib_qp_init_attr attr = {
1014 .send_cq = priv->recv_cq,
1015 .recv_cq = priv->recv_cq,
1016 .srq = priv->cm.srq,
1017 .cap.max_send_wr = ipoib_sendq_size,
1018 .cap.max_send_sge = 1,
1019 .sq_sig_type = IB_SIGNAL_ALL_WR,
1020 .qp_type = IB_QPT_RC,
1021 .qp_context = tx
1024 return ib_create_qp(priv->pd, &attr);
1027 static int ipoib_cm_send_req(struct net_device *dev,
1028 struct ib_cm_id *id, struct ib_qp *qp,
1029 u32 qpn,
1030 struct ib_sa_path_rec *pathrec)
1032 struct ipoib_dev_priv *priv = netdev_priv(dev);
1033 struct ipoib_cm_data data = {};
1034 struct ib_cm_req_param req = {};
1036 data.qpn = cpu_to_be32(priv->qp->qp_num);
1037 data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
1039 req.primary_path = pathrec;
1040 req.alternate_path = NULL;
1041 req.service_id = cpu_to_be64(IPOIB_CM_IETF_ID | qpn);
1042 req.qp_num = qp->qp_num;
1043 req.qp_type = qp->qp_type;
1044 req.private_data = &data;
1045 req.private_data_len = sizeof data;
1046 req.flow_control = 0;
1048 req.starting_psn = 0; /* FIXME */
1051 * Pick some arbitrary defaults here; we could make these
1052 * module parameters if anyone cared about setting them.
1054 req.responder_resources = 4;
1055 req.remote_cm_response_timeout = 20;
1056 req.local_cm_response_timeout = 20;
1057 req.retry_count = 0; /* RFC draft warns against retries */
1058 req.rnr_retry_count = 0; /* RFC draft warns against retries */
1059 req.max_cm_retries = 15;
1060 req.srq = ipoib_cm_has_srq(dev);
1061 return ib_send_cm_req(id, &req);
1064 static int ipoib_cm_modify_tx_init(struct net_device *dev,
1065 struct ib_cm_id *cm_id, struct ib_qp *qp)
1067 struct ipoib_dev_priv *priv = netdev_priv(dev);
1068 struct ib_qp_attr qp_attr;
1069 int qp_attr_mask, ret;
1070 ret = ib_find_pkey(priv->ca, priv->port, priv->pkey, &qp_attr.pkey_index);
1071 if (ret) {
1072 ipoib_warn(priv, "pkey 0x%x not found: %d\n", priv->pkey, ret);
1073 return ret;
1076 qp_attr.qp_state = IB_QPS_INIT;
1077 qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE;
1078 qp_attr.port_num = priv->port;
1079 qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
1081 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
1082 if (ret) {
1083 ipoib_warn(priv, "failed to modify tx QP to INIT: %d\n", ret);
1084 return ret;
1086 return 0;
1089 static int ipoib_cm_tx_init(struct ipoib_cm_tx *p, u32 qpn,
1090 struct ib_sa_path_rec *pathrec)
1092 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
1093 int ret;
1095 p->tx_ring = vmalloc(ipoib_sendq_size * sizeof *p->tx_ring);
1096 if (!p->tx_ring) {
1097 ipoib_warn(priv, "failed to allocate tx ring\n");
1098 ret = -ENOMEM;
1099 goto err_tx;
1101 memset(p->tx_ring, 0, ipoib_sendq_size * sizeof *p->tx_ring);
1103 p->qp = ipoib_cm_create_tx_qp(p->dev, p);
1104 if (IS_ERR(p->qp)) {
1105 ret = PTR_ERR(p->qp);
1106 ipoib_warn(priv, "failed to allocate tx qp: %d\n", ret);
1107 goto err_qp;
1110 p->id = ib_create_cm_id(priv->ca, ipoib_cm_tx_handler, p);
1111 if (IS_ERR(p->id)) {
1112 ret = PTR_ERR(p->id);
1113 ipoib_warn(priv, "failed to create tx cm id: %d\n", ret);
1114 goto err_id;
1117 ret = ipoib_cm_modify_tx_init(p->dev, p->id, p->qp);
1118 if (ret) {
1119 ipoib_warn(priv, "failed to modify tx qp to rtr: %d\n", ret);
1120 goto err_modify;
1123 ret = ipoib_cm_send_req(p->dev, p->id, p->qp, qpn, pathrec);
1124 if (ret) {
1125 ipoib_warn(priv, "failed to send cm req: %d\n", ret);
1126 goto err_send_cm;
1129 ipoib_dbg(priv, "Request connection 0x%x for gid %pI6 qpn 0x%x\n",
1130 p->qp->qp_num, pathrec->dgid.raw, qpn);
1132 return 0;
1134 err_send_cm:
1135 err_modify:
1136 ib_destroy_cm_id(p->id);
1137 err_id:
1138 p->id = NULL;
1139 ib_destroy_qp(p->qp);
1140 err_qp:
1141 p->qp = NULL;
1142 vfree(p->tx_ring);
1143 err_tx:
1144 return ret;
1147 static void ipoib_cm_tx_destroy(struct ipoib_cm_tx *p)
1149 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
1150 struct ipoib_cm_tx_buf *tx_req;
1151 unsigned long begin;
1153 ipoib_dbg(priv, "Destroy active connection 0x%x head 0x%x tail 0x%x\n",
1154 p->qp ? p->qp->qp_num : 0, p->tx_head, p->tx_tail);
1156 if (p->id)
1157 ib_destroy_cm_id(p->id);
1159 if (p->tx_ring) {
1160 /* Wait for all sends to complete */
1161 begin = jiffies;
1162 while ((int) p->tx_tail - (int) p->tx_head < 0) {
1163 if (time_after(jiffies, begin + 5 * HZ)) {
1164 ipoib_warn(priv, "timing out; %d sends not completed\n",
1165 p->tx_head - p->tx_tail);
1166 goto timeout;
1169 msleep(1);
1173 timeout:
1175 while ((int) p->tx_tail - (int) p->tx_head < 0) {
1176 tx_req = &p->tx_ring[p->tx_tail & (ipoib_sendq_size - 1)];
1177 ib_dma_unmap_single(priv->ca, tx_req->mapping, tx_req->skb->len,
1178 DMA_TO_DEVICE);
1179 dev_kfree_skb_any(tx_req->skb);
1180 ++p->tx_tail;
1181 netif_tx_lock_bh(p->dev);
1182 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
1183 netif_queue_stopped(p->dev) &&
1184 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
1185 netif_wake_queue(p->dev);
1186 netif_tx_unlock_bh(p->dev);
1189 if (p->qp)
1190 ib_destroy_qp(p->qp);
1192 vfree(p->tx_ring);
1193 kfree(p);
1196 static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
1197 struct ib_cm_event *event)
1199 struct ipoib_cm_tx *tx = cm_id->context;
1200 struct ipoib_dev_priv *priv = netdev_priv(tx->dev);
1201 struct net_device *dev = priv->dev;
1202 struct ipoib_neigh *neigh;
1203 unsigned long flags;
1204 int ret;
1206 switch (event->event) {
1207 case IB_CM_DREQ_RECEIVED:
1208 ipoib_dbg(priv, "DREQ received.\n");
1209 ib_send_cm_drep(cm_id, NULL, 0);
1210 break;
1211 case IB_CM_REP_RECEIVED:
1212 ipoib_dbg(priv, "REP received.\n");
1213 ret = ipoib_cm_rep_handler(cm_id, event);
1214 if (ret)
1215 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
1216 NULL, 0, NULL, 0);
1217 break;
1218 case IB_CM_REQ_ERROR:
1219 case IB_CM_REJ_RECEIVED:
1220 case IB_CM_TIMEWAIT_EXIT:
1221 ipoib_dbg(priv, "CM error %d.\n", event->event);
1222 netif_tx_lock_bh(dev);
1223 spin_lock_irqsave(&priv->lock, flags);
1224 neigh = tx->neigh;
1226 if (neigh) {
1227 neigh->cm = NULL;
1228 list_del(&neigh->list);
1229 if (neigh->ah)
1230 ipoib_put_ah(neigh->ah);
1231 ipoib_neigh_free(dev, neigh);
1233 tx->neigh = NULL;
1236 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
1237 list_move(&tx->list, &priv->cm.reap_list);
1238 queue_work(ipoib_workqueue, &priv->cm.reap_task);
1241 spin_unlock_irqrestore(&priv->lock, flags);
1242 netif_tx_unlock_bh(dev);
1243 break;
1244 default:
1245 break;
1248 return 0;
1251 struct ipoib_cm_tx *ipoib_cm_create_tx(struct net_device *dev, struct ipoib_path *path,
1252 struct ipoib_neigh *neigh)
1254 struct ipoib_dev_priv *priv = netdev_priv(dev);
1255 struct ipoib_cm_tx *tx;
1257 tx = kzalloc(sizeof *tx, GFP_ATOMIC);
1258 if (!tx)
1259 return NULL;
1261 neigh->cm = tx;
1262 tx->neigh = neigh;
1263 tx->path = path;
1264 tx->dev = dev;
1265 list_add(&tx->list, &priv->cm.start_list);
1266 set_bit(IPOIB_FLAG_INITIALIZED, &tx->flags);
1267 queue_work(ipoib_workqueue, &priv->cm.start_task);
1268 return tx;
1271 void ipoib_cm_destroy_tx(struct ipoib_cm_tx *tx)
1273 struct ipoib_dev_priv *priv = netdev_priv(tx->dev);
1274 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
1275 list_move(&tx->list, &priv->cm.reap_list);
1276 queue_work(ipoib_workqueue, &priv->cm.reap_task);
1277 ipoib_dbg(priv, "Reap connection for gid %pI6\n",
1278 tx->neigh->dgid.raw);
1279 tx->neigh = NULL;
1283 static void ipoib_cm_tx_start(struct work_struct *work)
1285 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1286 cm.start_task);
1287 struct net_device *dev = priv->dev;
1288 struct ipoib_neigh *neigh;
1289 struct ipoib_cm_tx *p;
1290 unsigned long flags;
1291 int ret;
1293 struct ib_sa_path_rec pathrec;
1294 u32 qpn;
1296 netif_tx_lock_bh(dev);
1297 spin_lock_irqsave(&priv->lock, flags);
1299 while (!list_empty(&priv->cm.start_list)) {
1300 p = list_entry(priv->cm.start_list.next, typeof(*p), list);
1301 list_del_init(&p->list);
1302 neigh = p->neigh;
1303 qpn = IPOIB_QPN(neigh->neighbour->ha);
1304 memcpy(&pathrec, &p->path->pathrec, sizeof pathrec);
1306 spin_unlock_irqrestore(&priv->lock, flags);
1307 netif_tx_unlock_bh(dev);
1309 ret = ipoib_cm_tx_init(p, qpn, &pathrec);
1311 netif_tx_lock_bh(dev);
1312 spin_lock_irqsave(&priv->lock, flags);
1314 if (ret) {
1315 neigh = p->neigh;
1316 if (neigh) {
1317 neigh->cm = NULL;
1318 list_del(&neigh->list);
1319 if (neigh->ah)
1320 ipoib_put_ah(neigh->ah);
1321 ipoib_neigh_free(dev, neigh);
1323 list_del(&p->list);
1324 kfree(p);
1328 spin_unlock_irqrestore(&priv->lock, flags);
1329 netif_tx_unlock_bh(dev);
1332 static void ipoib_cm_tx_reap(struct work_struct *work)
1334 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1335 cm.reap_task);
1336 struct net_device *dev = priv->dev;
1337 struct ipoib_cm_tx *p;
1338 unsigned long flags;
1340 netif_tx_lock_bh(dev);
1341 spin_lock_irqsave(&priv->lock, flags);
1343 while (!list_empty(&priv->cm.reap_list)) {
1344 p = list_entry(priv->cm.reap_list.next, typeof(*p), list);
1345 list_del(&p->list);
1346 spin_unlock_irqrestore(&priv->lock, flags);
1347 netif_tx_unlock_bh(dev);
1348 ipoib_cm_tx_destroy(p);
1349 netif_tx_lock_bh(dev);
1350 spin_lock_irqsave(&priv->lock, flags);
1353 spin_unlock_irqrestore(&priv->lock, flags);
1354 netif_tx_unlock_bh(dev);
1357 static void ipoib_cm_skb_reap(struct work_struct *work)
1359 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1360 cm.skb_task);
1361 struct net_device *dev = priv->dev;
1362 struct sk_buff *skb;
1363 unsigned long flags;
1364 unsigned mtu = priv->mcast_mtu;
1366 netif_tx_lock_bh(dev);
1367 spin_lock_irqsave(&priv->lock, flags);
1369 while ((skb = skb_dequeue(&priv->cm.skb_queue))) {
1370 spin_unlock_irqrestore(&priv->lock, flags);
1371 netif_tx_unlock_bh(dev);
1373 if (skb->protocol == htons(ETH_P_IP))
1374 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
1375 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1376 else if (skb->protocol == htons(ETH_P_IPV6))
1377 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, priv->dev);
1378 #endif
1379 dev_kfree_skb_any(skb);
1381 netif_tx_lock_bh(dev);
1382 spin_lock_irqsave(&priv->lock, flags);
1385 spin_unlock_irqrestore(&priv->lock, flags);
1386 netif_tx_unlock_bh(dev);
1389 void ipoib_cm_skb_too_long(struct net_device *dev, struct sk_buff *skb,
1390 unsigned int mtu)
1392 struct ipoib_dev_priv *priv = netdev_priv(dev);
1393 int e = skb_queue_empty(&priv->cm.skb_queue);
1395 if (skb_dst(skb))
1396 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
1398 skb_queue_tail(&priv->cm.skb_queue, skb);
1399 if (e)
1400 queue_work(ipoib_workqueue, &priv->cm.skb_task);
1403 static void ipoib_cm_rx_reap(struct work_struct *work)
1405 ipoib_cm_free_rx_reap_list(container_of(work, struct ipoib_dev_priv,
1406 cm.rx_reap_task)->dev);
1409 static void ipoib_cm_stale_task(struct work_struct *work)
1411 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1412 cm.stale_task.work);
1413 struct ipoib_cm_rx *p;
1414 int ret;
1416 spin_lock_irq(&priv->lock);
1417 while (!list_empty(&priv->cm.passive_ids)) {
1418 /* List is sorted by LRU, start from tail,
1419 * stop when we see a recently used entry */
1420 p = list_entry(priv->cm.passive_ids.prev, typeof(*p), list);
1421 if (time_before_eq(jiffies, p->jiffies + IPOIB_CM_RX_TIMEOUT))
1422 break;
1423 list_move(&p->list, &priv->cm.rx_error_list);
1424 p->state = IPOIB_CM_RX_ERROR;
1425 spin_unlock_irq(&priv->lock);
1426 ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
1427 if (ret)
1428 ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
1429 spin_lock_irq(&priv->lock);
1432 if (!list_empty(&priv->cm.passive_ids))
1433 queue_delayed_work(ipoib_workqueue,
1434 &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
1435 spin_unlock_irq(&priv->lock);
1439 static ssize_t show_mode(struct device *d, struct device_attribute *attr,
1440 char *buf)
1442 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(d));
1444 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
1445 return sprintf(buf, "connected\n");
1446 else
1447 return sprintf(buf, "datagram\n");
1450 static ssize_t set_mode(struct device *d, struct device_attribute *attr,
1451 const char *buf, size_t count)
1453 struct net_device *dev = to_net_dev(d);
1454 struct ipoib_dev_priv *priv = netdev_priv(dev);
1456 if (!rtnl_trylock())
1457 return restart_syscall();
1459 /* flush paths if we switch modes so that connections are restarted */
1460 if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
1461 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
1462 ipoib_warn(priv, "enabling connected mode "
1463 "will cause multicast packet drops\n");
1465 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_TSO);
1466 rtnl_unlock();
1467 priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
1469 ipoib_flush_paths(dev);
1470 return count;
1473 if (!strcmp(buf, "datagram\n")) {
1474 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
1476 if (test_bit(IPOIB_FLAG_CSUM, &priv->flags)) {
1477 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
1478 if (priv->hca_caps & IB_DEVICE_UD_TSO)
1479 dev->features |= NETIF_F_TSO;
1481 dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
1482 rtnl_unlock();
1483 ipoib_flush_paths(dev);
1485 return count;
1487 rtnl_unlock();
1489 return -EINVAL;
1492 static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO, show_mode, set_mode);
1494 int ipoib_cm_add_mode_attr(struct net_device *dev)
1496 return device_create_file(&dev->dev, &dev_attr_mode);
1499 static void ipoib_cm_create_srq(struct net_device *dev, int max_sge)
1501 struct ipoib_dev_priv *priv = netdev_priv(dev);
1502 struct ib_srq_init_attr srq_init_attr = {
1503 .attr = {
1504 .max_wr = ipoib_recvq_size,
1505 .max_sge = max_sge
1509 priv->cm.srq = ib_create_srq(priv->pd, &srq_init_attr);
1510 if (IS_ERR(priv->cm.srq)) {
1511 if (PTR_ERR(priv->cm.srq) != -ENOSYS)
1512 printk(KERN_WARNING "%s: failed to allocate SRQ, error %ld\n",
1513 priv->ca->name, PTR_ERR(priv->cm.srq));
1514 priv->cm.srq = NULL;
1515 return;
1518 priv->cm.srq_ring = vmalloc(ipoib_recvq_size * sizeof *priv->cm.srq_ring);
1519 if (!priv->cm.srq_ring) {
1520 printk(KERN_WARNING "%s: failed to allocate CM SRQ ring (%d entries)\n",
1521 priv->ca->name, ipoib_recvq_size);
1522 ib_destroy_srq(priv->cm.srq);
1523 priv->cm.srq = NULL;
1524 return;
1527 memset(priv->cm.srq_ring, 0, ipoib_recvq_size * sizeof *priv->cm.srq_ring);
1530 int ipoib_cm_dev_init(struct net_device *dev)
1532 struct ipoib_dev_priv *priv = netdev_priv(dev);
1533 int i, ret;
1534 struct ib_device_attr attr;
1536 INIT_LIST_HEAD(&priv->cm.passive_ids);
1537 INIT_LIST_HEAD(&priv->cm.reap_list);
1538 INIT_LIST_HEAD(&priv->cm.start_list);
1539 INIT_LIST_HEAD(&priv->cm.rx_error_list);
1540 INIT_LIST_HEAD(&priv->cm.rx_flush_list);
1541 INIT_LIST_HEAD(&priv->cm.rx_drain_list);
1542 INIT_LIST_HEAD(&priv->cm.rx_reap_list);
1543 INIT_WORK(&priv->cm.start_task, ipoib_cm_tx_start);
1544 INIT_WORK(&priv->cm.reap_task, ipoib_cm_tx_reap);
1545 INIT_WORK(&priv->cm.skb_task, ipoib_cm_skb_reap);
1546 INIT_WORK(&priv->cm.rx_reap_task, ipoib_cm_rx_reap);
1547 INIT_DELAYED_WORK(&priv->cm.stale_task, ipoib_cm_stale_task);
1549 skb_queue_head_init(&priv->cm.skb_queue);
1551 ret = ib_query_device(priv->ca, &attr);
1552 if (ret) {
1553 printk(KERN_WARNING "ib_query_device() failed with %d\n", ret);
1554 return ret;
1557 ipoib_dbg(priv, "max_srq_sge=%d\n", attr.max_srq_sge);
1559 attr.max_srq_sge = min_t(int, IPOIB_CM_RX_SG, attr.max_srq_sge);
1560 ipoib_cm_create_srq(dev, attr.max_srq_sge);
1561 if (ipoib_cm_has_srq(dev)) {
1562 priv->cm.max_cm_mtu = attr.max_srq_sge * PAGE_SIZE - 0x10;
1563 priv->cm.num_frags = attr.max_srq_sge;
1564 ipoib_dbg(priv, "max_cm_mtu = 0x%x, num_frags=%d\n",
1565 priv->cm.max_cm_mtu, priv->cm.num_frags);
1566 } else {
1567 priv->cm.max_cm_mtu = IPOIB_CM_MTU;
1568 priv->cm.num_frags = IPOIB_CM_RX_SG;
1571 ipoib_cm_init_rx_wr(dev, &priv->cm.rx_wr, priv->cm.rx_sge);
1573 if (ipoib_cm_has_srq(dev)) {
1574 for (i = 0; i < ipoib_recvq_size; ++i) {
1575 if (!ipoib_cm_alloc_rx_skb(dev, priv->cm.srq_ring, i,
1576 priv->cm.num_frags - 1,
1577 priv->cm.srq_ring[i].mapping)) {
1578 ipoib_warn(priv, "failed to allocate "
1579 "receive buffer %d\n", i);
1580 ipoib_cm_dev_cleanup(dev);
1581 return -ENOMEM;
1584 if (ipoib_cm_post_receive_srq(dev, i)) {
1585 ipoib_warn(priv, "ipoib_cm_post_receive_srq "
1586 "failed for buf %d\n", i);
1587 ipoib_cm_dev_cleanup(dev);
1588 return -EIO;
1593 priv->dev->dev_addr[0] = IPOIB_FLAGS_RC;
1594 return 0;
1597 void ipoib_cm_dev_cleanup(struct net_device *dev)
1599 struct ipoib_dev_priv *priv = netdev_priv(dev);
1600 int ret;
1602 if (!priv->cm.srq)
1603 return;
1605 ipoib_dbg(priv, "Cleanup ipoib connected mode.\n");
1607 ret = ib_destroy_srq(priv->cm.srq);
1608 if (ret)
1609 ipoib_warn(priv, "ib_destroy_srq failed: %d\n", ret);
1611 priv->cm.srq = NULL;
1612 if (!priv->cm.srq_ring)
1613 return;
1615 ipoib_cm_free_rx_ring(dev, priv->cm.srq_ring);
1616 priv->cm.srq_ring = NULL;