blob: 97e67d36378fa1532cb2224624ae20ea53acefe2 [file] [log] [blame]
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001/*
2 * Copyright (c) 2006 Mellanox Technologies. All rights reserved
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 * $Id$
33 */
34
35#include <rdma/ib_cm.h>
36#include <rdma/ib_cache.h>
37#include <net/dst.h>
38#include <net/icmp.h>
39#include <linux/icmpv6.h>
Michael S. Tsirkin518b1642007-05-21 15:04:59 +030040#include <linux/delay.h>
Roland Dreier10313cb2008-03-12 07:51:03 -070041#include <linux/vmalloc.h>
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020042
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -080043#include "ipoib.h"
44
45int ipoib_max_conn_qp = 128;
46
47module_param_named(max_nonsrq_conn_qp, ipoib_max_conn_qp, int, 0444);
48MODULE_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
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020052#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
53static int data_debug_level;
54
55module_param_named(cm_data_debug_level, data_debug_level, int, 0644);
56MODULE_PARM_DESC(cm_data_debug_level,
57 "Enable data path debug tracing for connected mode if > 0");
58#endif
59
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020060#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
Michael S. Tsirkin518b1642007-05-21 15:04:59 +030067static struct ib_qp_attr ipoib_cm_err_attr = {
68 .qp_state = IB_QPS_ERR
69};
70
Roland Dreier09f60f82007-10-26 13:44:25 -070071#define IPOIB_CM_RX_DRAIN_WRID 0xffffffff
Michael S. Tsirkin518b1642007-05-21 15:04:59 +030072
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +030073static struct ib_send_wr ipoib_cm_rx_drain_wr = {
74 .wr_id = IPOIB_CM_RX_DRAIN_WRID,
75 .opcode = IB_WR_SEND,
Michael S. Tsirkin518b1642007-05-21 15:04:59 +030076};
77
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020078static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
79 struct ib_cm_event *event);
80
Michael S. Tsirkin18120632007-02-20 20:17:55 +020081static void ipoib_cm_dma_unmap_rx(struct ipoib_dev_priv *priv, int frags,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020082 u64 mapping[IPOIB_CM_RX_SG])
83{
84 int i;
85
86 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
87
Michael S. Tsirkin18120632007-02-20 20:17:55 +020088 for (i = 0; i < frags; ++i)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020089 ib_dma_unmap_single(priv->ca, mapping[i + 1], PAGE_SIZE, DMA_FROM_DEVICE);
90}
91
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -080092static int ipoib_cm_post_receive_srq(struct net_device *dev, int id)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020093{
94 struct ipoib_dev_priv *priv = netdev_priv(dev);
95 struct ib_recv_wr *bad_wr;
96 int i, ret;
97
Michael S. Tsirkin1b524962007-08-16 15:36:16 +030098 priv->cm.rx_wr.wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020099
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800100 for (i = 0; i < priv->cm.num_frags; ++i)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200101 priv->cm.rx_sge[i].addr = priv->cm.srq_ring[id].mapping[i];
102
103 ret = ib_post_srq_recv(priv->cm.srq, &priv->cm.rx_wr, &bad_wr);
104 if (unlikely(ret)) {
105 ipoib_warn(priv, "post srq failed for buf %d (%d)\n", id, ret);
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800106 ipoib_cm_dma_unmap_rx(priv, priv->cm.num_frags - 1,
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200107 priv->cm.srq_ring[id].mapping);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200108 dev_kfree_skb_any(priv->cm.srq_ring[id].skb);
109 priv->cm.srq_ring[id].skb = NULL;
110 }
111
112 return ret;
113}
114
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800115static int ipoib_cm_post_receive_nonsrq(struct net_device *dev,
116 struct ipoib_cm_rx *rx, int id)
117{
118 struct ipoib_dev_priv *priv = netdev_priv(dev);
119 struct ib_recv_wr *bad_wr;
120 int i, ret;
121
122 priv->cm.rx_wr.wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV;
123
124 for (i = 0; i < IPOIB_CM_RX_SG; ++i)
125 priv->cm.rx_sge[i].addr = rx->rx_ring[id].mapping[i];
126
127 ret = ib_post_recv(rx->qp, &priv->cm.rx_wr, &bad_wr);
128 if (unlikely(ret)) {
129 ipoib_warn(priv, "post recv failed for buf %d (%d)\n", id, ret);
130 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
131 rx->rx_ring[id].mapping);
132 dev_kfree_skb_any(rx->rx_ring[id].skb);
133 rx->rx_ring[id].skb = NULL;
134 }
135
136 return ret;
137}
138
139static struct sk_buff *ipoib_cm_alloc_rx_skb(struct net_device *dev,
140 struct ipoib_cm_rx_buf *rx_ring,
141 int id, int frags,
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200142 u64 mapping[IPOIB_CM_RX_SG])
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200143{
144 struct ipoib_dev_priv *priv = netdev_priv(dev);
145 struct sk_buff *skb;
146 int i;
147
148 skb = dev_alloc_skb(IPOIB_CM_HEAD_SIZE + 12);
149 if (unlikely(!skb))
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200150 return NULL;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200151
152 /*
153 * IPoIB adds a 4 byte header. So we need 12 more bytes to align the
154 * IP header to a multiple of 16.
155 */
156 skb_reserve(skb, 12);
157
158 mapping[0] = ib_dma_map_single(priv->ca, skb->data, IPOIB_CM_HEAD_SIZE,
159 DMA_FROM_DEVICE);
160 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0]))) {
161 dev_kfree_skb_any(skb);
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200162 return NULL;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200163 }
164
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200165 for (i = 0; i < frags; i++) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200166 struct page *page = alloc_page(GFP_ATOMIC);
167
168 if (!page)
169 goto partial_error;
170 skb_fill_page_desc(skb, i, page, 0, PAGE_SIZE);
171
172 mapping[i + 1] = ib_dma_map_page(priv->ca, skb_shinfo(skb)->frags[i].page,
Michael S. Tsirkin6371ea32007-04-10 18:32:42 +0300173 0, PAGE_SIZE, DMA_FROM_DEVICE);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200174 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[i + 1])))
175 goto partial_error;
176 }
177
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800178 rx_ring[id].skb = skb;
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200179 return skb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200180
181partial_error:
182
183 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
184
Ralph Campbell841adfc2007-06-29 11:37:56 -0700185 for (; i > 0; --i)
186 ib_dma_unmap_single(priv->ca, mapping[i], PAGE_SIZE, DMA_FROM_DEVICE);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200187
Michael S. Tsirkin8a2e65f2007-02-16 00:16:13 +0200188 dev_kfree_skb_any(skb);
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200189 return NULL;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200190}
191
Roland Dreier1efb6142008-01-25 14:15:24 -0800192static void ipoib_cm_free_rx_ring(struct net_device *dev,
193 struct ipoib_cm_rx_buf *rx_ring)
194{
195 struct ipoib_dev_priv *priv = netdev_priv(dev);
196 int i;
197
198 for (i = 0; i < ipoib_recvq_size; ++i)
199 if (rx_ring[i].skb) {
200 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
201 rx_ring[i].mapping);
202 dev_kfree_skb_any(rx_ring[i].skb);
203 }
204
205 kfree(rx_ring);
206}
207
Roland Dreier2337f802007-10-23 19:57:54 -0700208static void ipoib_cm_start_rx_drain(struct ipoib_dev_priv *priv)
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300209{
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300210 struct ib_send_wr *bad_wr;
211 struct ipoib_cm_rx *p;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300212
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300213 /* We only reserved 1 extra slot in CQ for drain WRs, so
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300214 * make sure we have at most 1 outstanding WR. */
215 if (list_empty(&priv->cm.rx_flush_list) ||
216 !list_empty(&priv->cm.rx_drain_list))
217 return;
218
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300219 /*
220 * QPs on flush list are error state. This way, a "flush
221 * error" WC will be immediately generated for each WR we post.
222 */
223 p = list_entry(priv->cm.rx_flush_list.next, typeof(*p), list);
224 if (ib_post_send(p->qp, &ipoib_cm_rx_drain_wr, &bad_wr))
225 ipoib_warn(priv, "failed to post drain wr\n");
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300226
227 list_splice_init(&priv->cm.rx_flush_list, &priv->cm.rx_drain_list);
228}
229
230static void ipoib_cm_rx_event_handler(struct ib_event *event, void *ctx)
231{
232 struct ipoib_cm_rx *p = ctx;
233 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
234 unsigned long flags;
235
236 if (event->event != IB_EVENT_QP_LAST_WQE_REACHED)
237 return;
238
239 spin_lock_irqsave(&priv->lock, flags);
240 list_move(&p->list, &priv->cm.rx_flush_list);
241 p->state = IPOIB_CM_RX_FLUSH;
242 ipoib_cm_start_rx_drain(priv);
243 spin_unlock_irqrestore(&priv->lock, flags);
244}
245
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200246static struct ib_qp *ipoib_cm_create_rx_qp(struct net_device *dev,
247 struct ipoib_cm_rx *p)
248{
249 struct ipoib_dev_priv *priv = netdev_priv(dev);
250 struct ib_qp_init_attr attr = {
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300251 .event_handler = ipoib_cm_rx_event_handler,
Eli Cohenf56bcd82008-04-29 13:46:53 -0700252 .send_cq = priv->recv_cq, /* For drain WR */
253 .recv_cq = priv->recv_cq,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200254 .srq = priv->cm.srq,
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300255 .cap.max_send_wr = 1, /* For drain WR */
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200256 .cap.max_send_sge = 1, /* FIXME: 0 Seems not to work */
257 .sq_sig_type = IB_SIGNAL_ALL_WR,
258 .qp_type = IB_QPT_RC,
259 .qp_context = p,
260 };
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800261
262 if (!ipoib_cm_has_srq(dev)) {
263 attr.cap.max_recv_wr = ipoib_recvq_size;
264 attr.cap.max_recv_sge = IPOIB_CM_RX_SG;
265 }
266
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200267 return ib_create_qp(priv->pd, &attr);
268}
269
270static int ipoib_cm_modify_rx_qp(struct net_device *dev,
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800271 struct ib_cm_id *cm_id, struct ib_qp *qp,
272 unsigned psn)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200273{
274 struct ipoib_dev_priv *priv = netdev_priv(dev);
275 struct ib_qp_attr qp_attr;
276 int qp_attr_mask, ret;
277
278 qp_attr.qp_state = IB_QPS_INIT;
279 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
280 if (ret) {
281 ipoib_warn(priv, "failed to init QP attr for INIT: %d\n", ret);
282 return ret;
283 }
284 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
285 if (ret) {
286 ipoib_warn(priv, "failed to modify QP to INIT: %d\n", ret);
287 return ret;
288 }
289 qp_attr.qp_state = IB_QPS_RTR;
290 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
291 if (ret) {
292 ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
293 return ret;
294 }
295 qp_attr.rq_psn = psn;
296 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
297 if (ret) {
298 ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
299 return ret;
300 }
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300301
302 /*
303 * Current Mellanox HCA firmware won't generate completions
304 * with error for drain WRs unless the QP has been moved to
305 * RTS first. This work-around leaves a window where a QP has
306 * moved to error asynchronously, but this will eventually get
307 * fixed in firmware, so let's not error out if modify QP
308 * fails.
309 */
310 qp_attr.qp_state = IB_QPS_RTS;
311 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
312 if (ret) {
313 ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
314 return 0;
315 }
316 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
317 if (ret) {
318 ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
319 return 0;
320 }
321
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200322 return 0;
323}
324
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800325static int ipoib_cm_nonsrq_init_rx(struct net_device *dev, struct ib_cm_id *cm_id,
326 struct ipoib_cm_rx *rx)
327{
328 struct ipoib_dev_priv *priv = netdev_priv(dev);
329 int ret;
330 int i;
331
332 rx->rx_ring = kcalloc(ipoib_recvq_size, sizeof *rx->rx_ring, GFP_KERNEL);
333 if (!rx->rx_ring)
334 return -ENOMEM;
335
336 spin_lock_irq(&priv->lock);
337
338 if (priv->cm.nonsrq_conn_qp >= ipoib_max_conn_qp) {
339 spin_unlock_irq(&priv->lock);
340 ib_send_cm_rej(cm_id, IB_CM_REJ_NO_QP, NULL, 0, NULL, 0);
341 ret = -EINVAL;
342 goto err_free;
343 } else
344 ++priv->cm.nonsrq_conn_qp;
345
346 spin_unlock_irq(&priv->lock);
347
348 for (i = 0; i < ipoib_recvq_size; ++i) {
349 if (!ipoib_cm_alloc_rx_skb(dev, rx->rx_ring, i, IPOIB_CM_RX_SG - 1,
350 rx->rx_ring[i].mapping)) {
351 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
352 ret = -ENOMEM;
353 goto err_count;
354 }
355 ret = ipoib_cm_post_receive_nonsrq(dev, rx, i);
356 if (ret) {
357 ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq "
358 "failed for buf %d\n", i);
359 ret = -EIO;
360 goto err_count;
361 }
362 }
363
364 rx->recv_count = ipoib_recvq_size;
365
366 return 0;
367
368err_count:
369 spin_lock_irq(&priv->lock);
370 --priv->cm.nonsrq_conn_qp;
371 spin_unlock_irq(&priv->lock);
372
373err_free:
374 ipoib_cm_free_rx_ring(dev, rx->rx_ring);
375
376 return ret;
377}
378
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200379static int ipoib_cm_send_rep(struct net_device *dev, struct ib_cm_id *cm_id,
380 struct ib_qp *qp, struct ib_cm_req_event_param *req,
381 unsigned psn)
382{
383 struct ipoib_dev_priv *priv = netdev_priv(dev);
384 struct ipoib_cm_data data = {};
385 struct ib_cm_rep_param rep = {};
386
387 data.qpn = cpu_to_be32(priv->qp->qp_num);
388 data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
389
390 rep.private_data = &data;
391 rep.private_data_len = sizeof data;
392 rep.flow_control = 0;
393 rep.rnr_retry_count = req->rnr_retry_count;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800394 rep.srq = ipoib_cm_has_srq(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200395 rep.qp_num = qp->qp_num;
396 rep.starting_psn = psn;
397 return ib_send_cm_rep(cm_id, &rep);
398}
399
400static int ipoib_cm_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
401{
402 struct net_device *dev = cm_id->context;
403 struct ipoib_dev_priv *priv = netdev_priv(dev);
404 struct ipoib_cm_rx *p;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200405 unsigned psn;
406 int ret;
407
408 ipoib_dbg(priv, "REQ arrived\n");
409 p = kzalloc(sizeof *p, GFP_KERNEL);
410 if (!p)
411 return -ENOMEM;
412 p->dev = dev;
413 p->id = cm_id;
Michael S. Tsirkin3ec73932007-06-19 13:40:41 +0300414 cm_id->context = p;
415 p->state = IPOIB_CM_RX_LIVE;
416 p->jiffies = jiffies;
417 INIT_LIST_HEAD(&p->list);
418
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200419 p->qp = ipoib_cm_create_rx_qp(dev, p);
420 if (IS_ERR(p->qp)) {
421 ret = PTR_ERR(p->qp);
422 goto err_qp;
423 }
424
425 psn = random32() & 0xffffff;
426 ret = ipoib_cm_modify_rx_qp(dev, cm_id, p->qp, psn);
427 if (ret)
428 goto err_modify;
429
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800430 if (!ipoib_cm_has_srq(dev)) {
431 ret = ipoib_cm_nonsrq_init_rx(dev, cm_id, p);
432 if (ret)
433 goto err_modify;
434 }
435
Michael S. Tsirkin3ec73932007-06-19 13:40:41 +0300436 spin_lock_irq(&priv->lock);
437 queue_delayed_work(ipoib_workqueue,
438 &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
439 /* Add this entry to passive ids list head, but do not re-add it
440 * if IB_EVENT_QP_LAST_WQE_REACHED has moved it to flush list. */
441 p->jiffies = jiffies;
442 if (p->state == IPOIB_CM_RX_LIVE)
443 list_move(&p->list, &priv->cm.passive_ids);
444 spin_unlock_irq(&priv->lock);
445
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200446 ret = ipoib_cm_send_rep(dev, cm_id, p->qp, &event->param.req_rcvd, psn);
447 if (ret) {
448 ipoib_warn(priv, "failed to send REP: %d\n", ret);
Michael S. Tsirkin3ec73932007-06-19 13:40:41 +0300449 if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE))
450 ipoib_warn(priv, "unable to move qp to error state\n");
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200451 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200452 return 0;
453
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200454err_modify:
455 ib_destroy_qp(p->qp);
456err_qp:
457 kfree(p);
458 return ret;
459}
460
461static int ipoib_cm_rx_handler(struct ib_cm_id *cm_id,
462 struct ib_cm_event *event)
463{
464 struct ipoib_cm_rx *p;
465 struct ipoib_dev_priv *priv;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200466
467 switch (event->event) {
468 case IB_CM_REQ_RECEIVED:
469 return ipoib_cm_req_handler(cm_id, event);
470 case IB_CM_DREQ_RECEIVED:
471 p = cm_id->context;
472 ib_send_cm_drep(cm_id, NULL, 0);
473 /* Fall through */
474 case IB_CM_REJ_RECEIVED:
475 p = cm_id->context;
476 priv = netdev_priv(p->dev);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300477 if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE))
478 ipoib_warn(priv, "unable to move qp to error state\n");
479 /* Fall through */
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200480 default:
481 return 0;
482 }
483}
484/* Adjust length of skb with fragments to match received data */
485static void skb_put_frags(struct sk_buff *skb, unsigned int hdr_space,
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200486 unsigned int length, struct sk_buff *toskb)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200487{
488 int i, num_frags;
489 unsigned int size;
490
491 /* put header into skb */
492 size = min(length, hdr_space);
493 skb->tail += size;
494 skb->len += size;
495 length -= size;
496
497 num_frags = skb_shinfo(skb)->nr_frags;
498 for (i = 0; i < num_frags; i++) {
499 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
500
501 if (length == 0) {
502 /* don't need this page */
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200503 skb_fill_page_desc(toskb, i, frag->page, 0, PAGE_SIZE);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200504 --skb_shinfo(skb)->nr_frags;
505 } else {
506 size = min(length, (unsigned) PAGE_SIZE);
507
508 frag->size = size;
509 skb->data_len += size;
510 skb->truesize += size;
511 skb->len += size;
512 length -= size;
513 }
514 }
515}
516
517void ipoib_cm_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
518{
519 struct ipoib_dev_priv *priv = netdev_priv(dev);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800520 struct ipoib_cm_rx_buf *rx_ring;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300521 unsigned int wr_id = wc->wr_id & ~(IPOIB_OP_CM | IPOIB_OP_RECV);
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200522 struct sk_buff *skb, *newskb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200523 struct ipoib_cm_rx *p;
524 unsigned long flags;
525 u64 mapping[IPOIB_CM_RX_SG];
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200526 int frags;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800527 int has_srq;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200528
Roland Dreiera89875f2007-04-18 20:20:53 -0700529 ipoib_dbg_data(priv, "cm recv completion: id %d, status: %d\n",
530 wr_id, wc->status);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200531
532 if (unlikely(wr_id >= ipoib_recvq_size)) {
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300533 if (wr_id == (IPOIB_CM_RX_DRAIN_WRID & ~(IPOIB_OP_CM | IPOIB_OP_RECV))) {
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300534 spin_lock_irqsave(&priv->lock, flags);
535 list_splice_init(&priv->cm.rx_drain_list, &priv->cm.rx_reap_list);
536 ipoib_cm_start_rx_drain(priv);
537 queue_work(ipoib_workqueue, &priv->cm.rx_reap_task);
538 spin_unlock_irqrestore(&priv->lock, flags);
539 } else
540 ipoib_warn(priv, "cm recv completion event with wrid %d (> %d)\n",
541 wr_id, ipoib_recvq_size);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200542 return;
543 }
544
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800545 p = wc->qp->qp_context;
546
547 has_srq = ipoib_cm_has_srq(dev);
548 rx_ring = has_srq ? priv->cm.srq_ring : p->rx_ring;
549
550 skb = rx_ring[wr_id].skb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200551
552 if (unlikely(wc->status != IB_WC_SUCCESS)) {
553 ipoib_dbg(priv, "cm recv error "
554 "(status=%d, wrid=%d vend_err %x)\n",
555 wc->status, wr_id, wc->vendor_err);
Roland Dreierde903512007-09-28 15:33:51 -0700556 ++dev->stats.rx_dropped;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800557 if (has_srq)
558 goto repost;
559 else {
560 if (!--p->recv_count) {
561 spin_lock_irqsave(&priv->lock, flags);
562 list_move(&p->list, &priv->cm.rx_reap_list);
563 spin_unlock_irqrestore(&priv->lock, flags);
564 queue_work(ipoib_workqueue, &priv->cm.rx_reap_task);
565 }
566 return;
567 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200568 }
569
Roland Dreierfd312562007-10-17 21:54:44 -0700570 if (unlikely(!(wr_id & IPOIB_CM_RX_UPDATE_MASK))) {
Michael S. Tsirkind6ef7d62007-05-02 15:31:12 +0300571 if (p && time_after_eq(jiffies, p->jiffies + IPOIB_CM_RX_UPDATE_TIME)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200572 spin_lock_irqsave(&priv->lock, flags);
573 p->jiffies = jiffies;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300574 /* Move this entry to list head, but do not re-add it
575 * if it has been moved out of list. */
576 if (p->state == IPOIB_CM_RX_LIVE)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200577 list_move(&p->list, &priv->cm.passive_ids);
578 spin_unlock_irqrestore(&priv->lock, flags);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200579 }
580 }
581
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200582 frags = PAGE_ALIGN(wc->byte_len - min(wc->byte_len,
583 (unsigned)IPOIB_CM_HEAD_SIZE)) / PAGE_SIZE;
584
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800585 newskb = ipoib_cm_alloc_rx_skb(dev, rx_ring, wr_id, frags, mapping);
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200586 if (unlikely(!newskb)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200587 /*
588 * If we can't allocate a new RX buffer, dump
589 * this packet and reuse the old buffer.
590 */
591 ipoib_dbg(priv, "failed to allocate receive buffer %d\n", wr_id);
Roland Dreierde903512007-09-28 15:33:51 -0700592 ++dev->stats.rx_dropped;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200593 goto repost;
594 }
595
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800596 ipoib_cm_dma_unmap_rx(priv, frags, rx_ring[wr_id].mapping);
597 memcpy(rx_ring[wr_id].mapping, mapping, (frags + 1) * sizeof *mapping);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200598
599 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
600 wc->byte_len, wc->slid);
601
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200602 skb_put_frags(skb, IPOIB_CM_HEAD_SIZE, wc->byte_len, newskb);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200603
604 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700605 skb_reset_mac_header(skb);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200606 skb_pull(skb, IPOIB_ENCAP_LEN);
607
608 dev->last_rx = jiffies;
Roland Dreierde903512007-09-28 15:33:51 -0700609 ++dev->stats.rx_packets;
610 dev->stats.rx_bytes += skb->len;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200611
612 skb->dev = dev;
613 /* XXX get correct PACKET_ type here */
614 skb->pkt_type = PACKET_HOST;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700615 netif_receive_skb(skb);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200616
617repost:
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800618 if (has_srq) {
619 if (unlikely(ipoib_cm_post_receive_srq(dev, wr_id)))
620 ipoib_warn(priv, "ipoib_cm_post_receive_srq failed "
621 "for buf %d\n", wr_id);
622 } else {
623 if (unlikely(ipoib_cm_post_receive_nonsrq(dev, p, wr_id))) {
624 --p->recv_count;
625 ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq failed "
626 "for buf %d\n", wr_id);
627 }
628 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200629}
630
631static inline int post_send(struct ipoib_dev_priv *priv,
632 struct ipoib_cm_tx *tx,
633 unsigned int wr_id,
634 u64 addr, int len)
635{
636 struct ib_send_wr *bad_wr;
637
Eli Cohen71437402008-01-30 18:30:53 +0200638 priv->tx_sge[0].addr = addr;
639 priv->tx_sge[0].length = len;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200640
Roland Dreier42004062008-03-11 18:35:20 -0700641 priv->tx_wr.num_sge = 1;
Roland Dreier2337f802007-10-23 19:57:54 -0700642 priv->tx_wr.wr_id = wr_id | IPOIB_OP_CM;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200643
644 return ib_post_send(tx->qp, &priv->tx_wr, &bad_wr);
645}
646
647void ipoib_cm_send(struct net_device *dev, struct sk_buff *skb, struct ipoib_cm_tx *tx)
648{
649 struct ipoib_dev_priv *priv = netdev_priv(dev);
650 struct ipoib_tx_buf *tx_req;
651 u64 addr;
652
653 if (unlikely(skb->len > tx->mtu)) {
654 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
655 skb->len, tx->mtu);
Roland Dreierde903512007-09-28 15:33:51 -0700656 ++dev->stats.tx_dropped;
657 ++dev->stats.tx_errors;
Michael S. Tsirkin77d8e1e2007-03-21 15:45:05 +0200658 ipoib_cm_skb_too_long(dev, skb, tx->mtu - IPOIB_ENCAP_LEN);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200659 return;
660 }
661
662 ipoib_dbg_data(priv, "sending packet: head 0x%x length %d connection 0x%x\n",
663 tx->tx_head, skb->len, tx->qp->qp_num);
664
665 /*
666 * We put the skb into the tx_ring _before_ we call post_send()
667 * because it's entirely possible that the completion handler will
668 * run before we execute anything after the post_send(). That
669 * means we have to make sure everything is properly recorded and
670 * our state is consistent before we call post_send().
671 */
672 tx_req = &tx->tx_ring[tx->tx_head & (ipoib_sendq_size - 1)];
673 tx_req->skb = skb;
674 addr = ib_dma_map_single(priv->ca, skb->data, skb->len, DMA_TO_DEVICE);
675 if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
Roland Dreierde903512007-09-28 15:33:51 -0700676 ++dev->stats.tx_errors;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200677 dev_kfree_skb_any(skb);
678 return;
679 }
680
Eli Cohen71437402008-01-30 18:30:53 +0200681 tx_req->mapping[0] = addr;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200682
683 if (unlikely(post_send(priv, tx, tx->tx_head & (ipoib_sendq_size - 1),
Roland Dreier2337f802007-10-23 19:57:54 -0700684 addr, skb->len))) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200685 ipoib_warn(priv, "post_send failed\n");
Roland Dreierde903512007-09-28 15:33:51 -0700686 ++dev->stats.tx_errors;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200687 ib_dma_unmap_single(priv->ca, addr, skb->len, DMA_TO_DEVICE);
688 dev_kfree_skb_any(skb);
689 } else {
690 dev->trans_start = jiffies;
691 ++tx->tx_head;
692
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300693 if (++priv->tx_outstanding == ipoib_sendq_size) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200694 ipoib_dbg(priv, "TX ring 0x%x full, stopping kernel net queue\n",
695 tx->qp->qp_num);
696 netif_stop_queue(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200697 }
698 }
699}
700
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300701void ipoib_cm_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200702{
703 struct ipoib_dev_priv *priv = netdev_priv(dev);
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300704 struct ipoib_cm_tx *tx = wc->qp->qp_context;
705 unsigned int wr_id = wc->wr_id & ~IPOIB_OP_CM;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200706 struct ipoib_tx_buf *tx_req;
707 unsigned long flags;
708
Roland Dreiera89875f2007-04-18 20:20:53 -0700709 ipoib_dbg_data(priv, "cm send completion: id %d, status: %d\n",
710 wr_id, wc->status);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200711
712 if (unlikely(wr_id >= ipoib_sendq_size)) {
713 ipoib_warn(priv, "cm send completion event with wrid %d (> %d)\n",
714 wr_id, ipoib_sendq_size);
715 return;
716 }
717
718 tx_req = &tx->tx_ring[wr_id];
719
Eli Cohen71437402008-01-30 18:30:53 +0200720 ib_dma_unmap_single(priv->ca, tx_req->mapping[0], tx_req->skb->len, DMA_TO_DEVICE);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200721
722 /* FIXME: is this right? Shouldn't we only increment on success? */
Roland Dreierde903512007-09-28 15:33:51 -0700723 ++dev->stats.tx_packets;
724 dev->stats.tx_bytes += tx_req->skb->len;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200725
726 dev_kfree_skb_any(tx_req->skb);
727
728 spin_lock_irqsave(&priv->tx_lock, flags);
729 ++tx->tx_tail;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300730 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
731 netif_queue_stopped(dev) &&
732 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200733 netif_wake_queue(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200734
735 if (wc->status != IB_WC_SUCCESS &&
736 wc->status != IB_WC_WR_FLUSH_ERR) {
737 struct ipoib_neigh *neigh;
738
739 ipoib_dbg(priv, "failed cm send event "
740 "(status=%d, wrid=%d vend_err %x)\n",
741 wc->status, wr_id, wc->vendor_err);
742
743 spin_lock(&priv->lock);
744 neigh = tx->neigh;
745
746 if (neigh) {
747 neigh->cm = NULL;
748 list_del(&neigh->list);
749 if (neigh->ah)
750 ipoib_put_ah(neigh->ah);
751 ipoib_neigh_free(dev, neigh);
752
753 tx->neigh = NULL;
754 }
755
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200756 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
757 list_move(&tx->list, &priv->cm.reap_list);
758 queue_work(ipoib_workqueue, &priv->cm.reap_task);
759 }
760
761 clear_bit(IPOIB_FLAG_OPER_UP, &tx->flags);
762
763 spin_unlock(&priv->lock);
764 }
765
766 spin_unlock_irqrestore(&priv->tx_lock, flags);
767}
768
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200769int ipoib_cm_dev_open(struct net_device *dev)
770{
771 struct ipoib_dev_priv *priv = netdev_priv(dev);
772 int ret;
773
774 if (!IPOIB_CM_SUPPORTED(dev->dev_addr))
775 return 0;
776
777 priv->cm.id = ib_create_cm_id(priv->ca, ipoib_cm_rx_handler, dev);
778 if (IS_ERR(priv->cm.id)) {
779 printk(KERN_WARNING "%s: failed to create CM ID\n", priv->ca->name);
Michael S. Tsirkin347fcfb2007-04-30 17:30:28 -0700780 ret = PTR_ERR(priv->cm.id);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300781 goto err_cm;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200782 }
783
784 ret = ib_cm_listen(priv->cm.id, cpu_to_be64(IPOIB_CM_IETF_ID | priv->qp->qp_num),
785 0, NULL);
786 if (ret) {
787 printk(KERN_WARNING "%s: failed to listen on ID 0x%llx\n", priv->ca->name,
788 IPOIB_CM_IETF_ID | priv->qp->qp_num);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300789 goto err_listen;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200790 }
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300791
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200792 return 0;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300793
794err_listen:
795 ib_destroy_cm_id(priv->cm.id);
796err_cm:
797 priv->cm.id = NULL;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300798 return ret;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200799}
800
Roland Dreierefcd9972008-01-25 14:15:24 -0800801static void ipoib_cm_free_rx_reap_list(struct net_device *dev)
802{
803 struct ipoib_dev_priv *priv = netdev_priv(dev);
804 struct ipoib_cm_rx *rx, *n;
805 LIST_HEAD(list);
806
807 spin_lock_irq(&priv->lock);
808 list_splice_init(&priv->cm.rx_reap_list, &list);
809 spin_unlock_irq(&priv->lock);
810
811 list_for_each_entry_safe(rx, n, &list, list) {
812 ib_destroy_cm_id(rx->id);
813 ib_destroy_qp(rx->qp);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800814 if (!ipoib_cm_has_srq(dev)) {
815 ipoib_cm_free_rx_ring(priv->dev, rx->rx_ring);
816 spin_lock_irq(&priv->lock);
817 --priv->cm.nonsrq_conn_qp;
818 spin_unlock_irq(&priv->lock);
819 }
Roland Dreierefcd9972008-01-25 14:15:24 -0800820 kfree(rx);
821 }
822}
823
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200824void ipoib_cm_dev_stop(struct net_device *dev)
825{
826 struct ipoib_dev_priv *priv = netdev_priv(dev);
Roland Dreierefcd9972008-01-25 14:15:24 -0800827 struct ipoib_cm_rx *p;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300828 unsigned long begin;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300829 int ret;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200830
Michael S. Tsirkin347fcfb2007-04-30 17:30:28 -0700831 if (!IPOIB_CM_SUPPORTED(dev->dev_addr) || !priv->cm.id)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200832 return;
833
834 ib_destroy_cm_id(priv->cm.id);
Michael S. Tsirkin347fcfb2007-04-30 17:30:28 -0700835 priv->cm.id = NULL;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300836
Roland Dreier37aebbde2007-04-24 21:30:37 -0700837 spin_lock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200838 while (!list_empty(&priv->cm.passive_ids)) {
839 p = list_entry(priv->cm.passive_ids.next, typeof(*p), list);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300840 list_move(&p->list, &priv->cm.rx_error_list);
841 p->state = IPOIB_CM_RX_ERROR;
Roland Dreier37aebbde2007-04-24 21:30:37 -0700842 spin_unlock_irq(&priv->lock);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300843 ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
844 if (ret)
845 ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
846 spin_lock_irq(&priv->lock);
847 }
848
849 /* Wait for all RX to be drained */
850 begin = jiffies;
851
852 while (!list_empty(&priv->cm.rx_error_list) ||
853 !list_empty(&priv->cm.rx_flush_list) ||
854 !list_empty(&priv->cm.rx_drain_list)) {
Michael S. Tsirkin8fd357a2007-05-24 14:02:39 -0700855 if (time_after(jiffies, begin + 5 * HZ)) {
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300856 ipoib_warn(priv, "RX drain timing out\n");
857
858 /*
859 * assume the HW is wedged and just free up everything.
860 */
Pradeep Satyanarayanaec229e52008-02-12 15:00:59 -0800861 list_splice_init(&priv->cm.rx_flush_list,
862 &priv->cm.rx_reap_list);
863 list_splice_init(&priv->cm.rx_error_list,
864 &priv->cm.rx_reap_list);
865 list_splice_init(&priv->cm.rx_drain_list,
866 &priv->cm.rx_reap_list);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300867 break;
868 }
869 spin_unlock_irq(&priv->lock);
870 msleep(1);
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300871 ipoib_drain_cq(dev);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300872 spin_lock_irq(&priv->lock);
873 }
874
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300875 spin_unlock_irq(&priv->lock);
876
Roland Dreierefcd9972008-01-25 14:15:24 -0800877 ipoib_cm_free_rx_reap_list(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200878
879 cancel_delayed_work(&priv->cm.stale_task);
880}
881
882static int ipoib_cm_rep_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
883{
884 struct ipoib_cm_tx *p = cm_id->context;
885 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
886 struct ipoib_cm_data *data = event->private_data;
887 struct sk_buff_head skqueue;
888 struct ib_qp_attr qp_attr;
889 int qp_attr_mask, ret;
890 struct sk_buff *skb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200891
892 p->mtu = be32_to_cpu(data->mtu);
893
Michael S. Tsirkin82c3aca2007-06-20 19:22:15 +0300894 if (p->mtu <= IPOIB_ENCAP_LEN) {
895 ipoib_warn(priv, "Rejecting connection: mtu %d <= %d\n",
896 p->mtu, IPOIB_ENCAP_LEN);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200897 return -EINVAL;
898 }
899
900 qp_attr.qp_state = IB_QPS_RTR;
901 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
902 if (ret) {
903 ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
904 return ret;
905 }
906
907 qp_attr.rq_psn = 0 /* FIXME */;
908 ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
909 if (ret) {
910 ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
911 return ret;
912 }
913
914 qp_attr.qp_state = IB_QPS_RTS;
915 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
916 if (ret) {
917 ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
918 return ret;
919 }
920 ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
921 if (ret) {
922 ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
923 return ret;
924 }
925
926 skb_queue_head_init(&skqueue);
927
Roland Dreier37aebbde2007-04-24 21:30:37 -0700928 spin_lock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200929 set_bit(IPOIB_FLAG_OPER_UP, &p->flags);
930 if (p->neigh)
931 while ((skb = __skb_dequeue(&p->neigh->queue)))
932 __skb_queue_tail(&skqueue, skb);
Roland Dreier37aebbde2007-04-24 21:30:37 -0700933 spin_unlock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200934
935 while ((skb = __skb_dequeue(&skqueue))) {
936 skb->dev = p->dev;
937 if (dev_queue_xmit(skb))
938 ipoib_warn(priv, "dev_queue_xmit failed "
939 "to requeue packet\n");
940 }
941
942 ret = ib_send_cm_rtu(cm_id, NULL, 0);
943 if (ret) {
944 ipoib_warn(priv, "failed to send RTU: %d\n", ret);
945 return ret;
946 }
947 return 0;
948}
949
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300950static struct ib_qp *ipoib_cm_create_tx_qp(struct net_device *dev, struct ipoib_cm_tx *tx)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200951{
952 struct ipoib_dev_priv *priv = netdev_priv(dev);
Dotan Barakede6bc02007-10-07 09:30:48 +0200953 struct ib_qp_init_attr attr = {
Eli Cohenf56bcd82008-04-29 13:46:53 -0700954 .send_cq = priv->recv_cq,
955 .recv_cq = priv->recv_cq,
Dotan Barakede6bc02007-10-07 09:30:48 +0200956 .srq = priv->cm.srq,
957 .cap.max_send_wr = ipoib_sendq_size,
958 .cap.max_send_sge = 1,
959 .sq_sig_type = IB_SIGNAL_ALL_WR,
960 .qp_type = IB_QPT_RC,
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300961 .qp_context = tx
Roland Dreier2337f802007-10-23 19:57:54 -0700962 };
Dotan Barakede6bc02007-10-07 09:30:48 +0200963
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200964 return ib_create_qp(priv->pd, &attr);
965}
966
967static int ipoib_cm_send_req(struct net_device *dev,
968 struct ib_cm_id *id, struct ib_qp *qp,
969 u32 qpn,
970 struct ib_sa_path_rec *pathrec)
971{
972 struct ipoib_dev_priv *priv = netdev_priv(dev);
973 struct ipoib_cm_data data = {};
974 struct ib_cm_req_param req = {};
975
976 data.qpn = cpu_to_be32(priv->qp->qp_num);
977 data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
978
Roland Dreier2337f802007-10-23 19:57:54 -0700979 req.primary_path = pathrec;
980 req.alternate_path = NULL;
981 req.service_id = cpu_to_be64(IPOIB_CM_IETF_ID | qpn);
982 req.qp_num = qp->qp_num;
983 req.qp_type = qp->qp_type;
984 req.private_data = &data;
985 req.private_data_len = sizeof data;
986 req.flow_control = 0;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200987
Roland Dreier2337f802007-10-23 19:57:54 -0700988 req.starting_psn = 0; /* FIXME */
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200989
990 /*
991 * Pick some arbitrary defaults here; we could make these
992 * module parameters if anyone cared about setting them.
993 */
Roland Dreier2337f802007-10-23 19:57:54 -0700994 req.responder_resources = 4;
995 req.remote_cm_response_timeout = 20;
996 req.local_cm_response_timeout = 20;
997 req.retry_count = 0; /* RFC draft warns against retries */
998 req.rnr_retry_count = 0; /* RFC draft warns against retries */
999 req.max_cm_retries = 15;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001000 req.srq = ipoib_cm_has_srq(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001001 return ib_send_cm_req(id, &req);
1002}
1003
1004static int ipoib_cm_modify_tx_init(struct net_device *dev,
1005 struct ib_cm_id *cm_id, struct ib_qp *qp)
1006{
1007 struct ipoib_dev_priv *priv = netdev_priv(dev);
1008 struct ib_qp_attr qp_attr;
1009 int qp_attr_mask, ret;
Roland Dreier9fdd5e52008-04-16 21:09:35 -07001010 ret = ib_find_pkey(priv->ca, priv->port, priv->pkey, &qp_attr.pkey_index);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001011 if (ret) {
Roland Dreier9fdd5e52008-04-16 21:09:35 -07001012 ipoib_warn(priv, "pkey 0x%x not found: %d\n", priv->pkey, ret);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001013 return ret;
1014 }
1015
1016 qp_attr.qp_state = IB_QPS_INIT;
1017 qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE;
1018 qp_attr.port_num = priv->port;
1019 qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
1020
1021 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
1022 if (ret) {
1023 ipoib_warn(priv, "failed to modify tx QP to INIT: %d\n", ret);
1024 return ret;
1025 }
1026 return 0;
1027}
1028
1029static int ipoib_cm_tx_init(struct ipoib_cm_tx *p, u32 qpn,
1030 struct ib_sa_path_rec *pathrec)
1031{
1032 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
1033 int ret;
1034
Roland Dreier10313cb2008-03-12 07:51:03 -07001035 p->tx_ring = vmalloc(ipoib_sendq_size * sizeof *p->tx_ring);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001036 if (!p->tx_ring) {
1037 ipoib_warn(priv, "failed to allocate tx ring\n");
1038 ret = -ENOMEM;
1039 goto err_tx;
1040 }
Roland Dreier10313cb2008-03-12 07:51:03 -07001041 memset(p->tx_ring, 0, ipoib_sendq_size * sizeof *p->tx_ring);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001042
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001043 p->qp = ipoib_cm_create_tx_qp(p->dev, p);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001044 if (IS_ERR(p->qp)) {
1045 ret = PTR_ERR(p->qp);
1046 ipoib_warn(priv, "failed to allocate tx qp: %d\n", ret);
1047 goto err_qp;
1048 }
1049
1050 p->id = ib_create_cm_id(priv->ca, ipoib_cm_tx_handler, p);
1051 if (IS_ERR(p->id)) {
1052 ret = PTR_ERR(p->id);
1053 ipoib_warn(priv, "failed to create tx cm id: %d\n", ret);
1054 goto err_id;
1055 }
1056
1057 ret = ipoib_cm_modify_tx_init(p->dev, p->id, p->qp);
1058 if (ret) {
1059 ipoib_warn(priv, "failed to modify tx qp to rtr: %d\n", ret);
1060 goto err_modify;
1061 }
1062
1063 ret = ipoib_cm_send_req(p->dev, p->id, p->qp, qpn, pathrec);
1064 if (ret) {
1065 ipoib_warn(priv, "failed to send cm req: %d\n", ret);
1066 goto err_send_cm;
1067 }
1068
1069 ipoib_dbg(priv, "Request connection 0x%x for gid " IPOIB_GID_FMT " qpn 0x%x\n",
1070 p->qp->qp_num, IPOIB_GID_ARG(pathrec->dgid), qpn);
1071
1072 return 0;
1073
1074err_send_cm:
1075err_modify:
1076 ib_destroy_cm_id(p->id);
1077err_id:
1078 p->id = NULL;
1079 ib_destroy_qp(p->qp);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001080err_qp:
1081 p->qp = NULL;
Roland Dreier10313cb2008-03-12 07:51:03 -07001082 vfree(p->tx_ring);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001083err_tx:
1084 return ret;
1085}
1086
1087static void ipoib_cm_tx_destroy(struct ipoib_cm_tx *p)
1088{
1089 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
1090 struct ipoib_tx_buf *tx_req;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001091 unsigned long flags;
1092 unsigned long begin;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001093
1094 ipoib_dbg(priv, "Destroy active connection 0x%x head 0x%x tail 0x%x\n",
1095 p->qp ? p->qp->qp_num : 0, p->tx_head, p->tx_tail);
1096
1097 if (p->id)
1098 ib_destroy_cm_id(p->id);
1099
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001100 if (p->tx_ring) {
1101 /* Wait for all sends to complete */
1102 begin = jiffies;
1103 while ((int) p->tx_tail - (int) p->tx_head < 0) {
1104 if (time_after(jiffies, begin + 5 * HZ)) {
1105 ipoib_warn(priv, "timing out; %d sends not completed\n",
1106 p->tx_head - p->tx_tail);
1107 goto timeout;
1108 }
1109
1110 msleep(1);
1111 }
1112 }
1113
1114timeout:
1115
1116 while ((int) p->tx_tail - (int) p->tx_head < 0) {
1117 tx_req = &p->tx_ring[p->tx_tail & (ipoib_sendq_size - 1)];
Eli Cohen71437402008-01-30 18:30:53 +02001118 ib_dma_unmap_single(priv->ca, tx_req->mapping[0], tx_req->skb->len,
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001119 DMA_TO_DEVICE);
1120 dev_kfree_skb_any(tx_req->skb);
1121 ++p->tx_tail;
1122 spin_lock_irqsave(&priv->tx_lock, flags);
1123 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
1124 netif_queue_stopped(p->dev) &&
1125 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
1126 netif_wake_queue(p->dev);
1127 spin_unlock_irqrestore(&priv->tx_lock, flags);
1128 }
1129
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001130 if (p->qp)
1131 ib_destroy_qp(p->qp);
1132
Roland Dreier10313cb2008-03-12 07:51:03 -07001133 vfree(p->tx_ring);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001134 kfree(p);
1135}
1136
1137static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
1138 struct ib_cm_event *event)
1139{
1140 struct ipoib_cm_tx *tx = cm_id->context;
1141 struct ipoib_dev_priv *priv = netdev_priv(tx->dev);
1142 struct net_device *dev = priv->dev;
1143 struct ipoib_neigh *neigh;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001144 int ret;
1145
1146 switch (event->event) {
1147 case IB_CM_DREQ_RECEIVED:
1148 ipoib_dbg(priv, "DREQ received.\n");
1149 ib_send_cm_drep(cm_id, NULL, 0);
1150 break;
1151 case IB_CM_REP_RECEIVED:
1152 ipoib_dbg(priv, "REP received.\n");
1153 ret = ipoib_cm_rep_handler(cm_id, event);
1154 if (ret)
1155 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
1156 NULL, 0, NULL, 0);
1157 break;
1158 case IB_CM_REQ_ERROR:
1159 case IB_CM_REJ_RECEIVED:
1160 case IB_CM_TIMEWAIT_EXIT:
1161 ipoib_dbg(priv, "CM error %d.\n", event->event);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001162 spin_lock_irq(&priv->tx_lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001163 spin_lock(&priv->lock);
1164 neigh = tx->neigh;
1165
1166 if (neigh) {
1167 neigh->cm = NULL;
1168 list_del(&neigh->list);
1169 if (neigh->ah)
1170 ipoib_put_ah(neigh->ah);
1171 ipoib_neigh_free(dev, neigh);
1172
1173 tx->neigh = NULL;
1174 }
1175
1176 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
1177 list_move(&tx->list, &priv->cm.reap_list);
1178 queue_work(ipoib_workqueue, &priv->cm.reap_task);
1179 }
1180
1181 spin_unlock(&priv->lock);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001182 spin_unlock_irq(&priv->tx_lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001183 break;
1184 default:
1185 break;
1186 }
1187
1188 return 0;
1189}
1190
1191struct ipoib_cm_tx *ipoib_cm_create_tx(struct net_device *dev, struct ipoib_path *path,
1192 struct ipoib_neigh *neigh)
1193{
1194 struct ipoib_dev_priv *priv = netdev_priv(dev);
1195 struct ipoib_cm_tx *tx;
1196
1197 tx = kzalloc(sizeof *tx, GFP_ATOMIC);
1198 if (!tx)
1199 return NULL;
1200
1201 neigh->cm = tx;
1202 tx->neigh = neigh;
1203 tx->path = path;
1204 tx->dev = dev;
1205 list_add(&tx->list, &priv->cm.start_list);
1206 set_bit(IPOIB_FLAG_INITIALIZED, &tx->flags);
1207 queue_work(ipoib_workqueue, &priv->cm.start_task);
1208 return tx;
1209}
1210
1211void ipoib_cm_destroy_tx(struct ipoib_cm_tx *tx)
1212{
1213 struct ipoib_dev_priv *priv = netdev_priv(tx->dev);
1214 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
1215 list_move(&tx->list, &priv->cm.reap_list);
1216 queue_work(ipoib_workqueue, &priv->cm.reap_task);
1217 ipoib_dbg(priv, "Reap connection for gid " IPOIB_GID_FMT "\n",
1218 IPOIB_GID_ARG(tx->neigh->dgid));
1219 tx->neigh = NULL;
1220 }
1221}
1222
1223static void ipoib_cm_tx_start(struct work_struct *work)
1224{
1225 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1226 cm.start_task);
1227 struct net_device *dev = priv->dev;
1228 struct ipoib_neigh *neigh;
1229 struct ipoib_cm_tx *p;
1230 unsigned long flags;
1231 int ret;
1232
1233 struct ib_sa_path_rec pathrec;
1234 u32 qpn;
1235
1236 spin_lock_irqsave(&priv->tx_lock, flags);
1237 spin_lock(&priv->lock);
1238 while (!list_empty(&priv->cm.start_list)) {
1239 p = list_entry(priv->cm.start_list.next, typeof(*p), list);
1240 list_del_init(&p->list);
1241 neigh = p->neigh;
1242 qpn = IPOIB_QPN(neigh->neighbour->ha);
1243 memcpy(&pathrec, &p->path->pathrec, sizeof pathrec);
1244 spin_unlock(&priv->lock);
1245 spin_unlock_irqrestore(&priv->tx_lock, flags);
1246 ret = ipoib_cm_tx_init(p, qpn, &pathrec);
1247 spin_lock_irqsave(&priv->tx_lock, flags);
1248 spin_lock(&priv->lock);
1249 if (ret) {
1250 neigh = p->neigh;
1251 if (neigh) {
1252 neigh->cm = NULL;
1253 list_del(&neigh->list);
1254 if (neigh->ah)
1255 ipoib_put_ah(neigh->ah);
1256 ipoib_neigh_free(dev, neigh);
1257 }
1258 list_del(&p->list);
1259 kfree(p);
1260 }
1261 }
1262 spin_unlock(&priv->lock);
1263 spin_unlock_irqrestore(&priv->tx_lock, flags);
1264}
1265
1266static void ipoib_cm_tx_reap(struct work_struct *work)
1267{
1268 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1269 cm.reap_task);
1270 struct ipoib_cm_tx *p;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001271
Roland Dreier37aebbde2007-04-24 21:30:37 -07001272 spin_lock_irq(&priv->tx_lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001273 spin_lock(&priv->lock);
1274 while (!list_empty(&priv->cm.reap_list)) {
1275 p = list_entry(priv->cm.reap_list.next, typeof(*p), list);
1276 list_del(&p->list);
1277 spin_unlock(&priv->lock);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001278 spin_unlock_irq(&priv->tx_lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001279 ipoib_cm_tx_destroy(p);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001280 spin_lock_irq(&priv->tx_lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001281 spin_lock(&priv->lock);
1282 }
1283 spin_unlock(&priv->lock);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001284 spin_unlock_irq(&priv->tx_lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001285}
1286
1287static void ipoib_cm_skb_reap(struct work_struct *work)
1288{
1289 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1290 cm.skb_task);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001291 struct sk_buff *skb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001292
1293 unsigned mtu = priv->mcast_mtu;
1294
Roland Dreier37aebbde2007-04-24 21:30:37 -07001295 spin_lock_irq(&priv->tx_lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001296 spin_lock(&priv->lock);
1297 while ((skb = skb_dequeue(&priv->cm.skb_queue))) {
1298 spin_unlock(&priv->lock);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001299 spin_unlock_irq(&priv->tx_lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001300 if (skb->protocol == htons(ETH_P_IP))
1301 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
1302#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1303 else if (skb->protocol == htons(ETH_P_IPV6))
Roland Dreier20089ca2007-07-10 11:18:34 -07001304 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, priv->dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001305#endif
1306 dev_kfree_skb_any(skb);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001307 spin_lock_irq(&priv->tx_lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001308 spin_lock(&priv->lock);
1309 }
1310 spin_unlock(&priv->lock);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001311 spin_unlock_irq(&priv->tx_lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001312}
1313
Roland Dreier2337f802007-10-23 19:57:54 -07001314void ipoib_cm_skb_too_long(struct net_device *dev, struct sk_buff *skb,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001315 unsigned int mtu)
1316{
1317 struct ipoib_dev_priv *priv = netdev_priv(dev);
1318 int e = skb_queue_empty(&priv->cm.skb_queue);
1319
1320 if (skb->dst)
1321 skb->dst->ops->update_pmtu(skb->dst, mtu);
1322
1323 skb_queue_tail(&priv->cm.skb_queue, skb);
1324 if (e)
1325 queue_work(ipoib_workqueue, &priv->cm.skb_task);
1326}
1327
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001328static void ipoib_cm_rx_reap(struct work_struct *work)
1329{
Roland Dreierefcd9972008-01-25 14:15:24 -08001330 ipoib_cm_free_rx_reap_list(container_of(work, struct ipoib_dev_priv,
1331 cm.rx_reap_task)->dev);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001332}
1333
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001334static void ipoib_cm_stale_task(struct work_struct *work)
1335{
1336 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1337 cm.stale_task.work);
1338 struct ipoib_cm_rx *p;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001339 int ret;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001340
Roland Dreier37aebbde2007-04-24 21:30:37 -07001341 spin_lock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001342 while (!list_empty(&priv->cm.passive_ids)) {
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001343 /* List is sorted by LRU, start from tail,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001344 * stop when we see a recently used entry */
1345 p = list_entry(priv->cm.passive_ids.prev, typeof(*p), list);
Michael S. Tsirkin60a596d2007-03-22 14:32:09 -07001346 if (time_before_eq(jiffies, p->jiffies + IPOIB_CM_RX_TIMEOUT))
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001347 break;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001348 list_move(&p->list, &priv->cm.rx_error_list);
1349 p->state = IPOIB_CM_RX_ERROR;
Roland Dreier37aebbde2007-04-24 21:30:37 -07001350 spin_unlock_irq(&priv->lock);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001351 ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
1352 if (ret)
1353 ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001354 spin_lock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001355 }
Michael S. Tsirkin7c5b9ef2007-05-14 07:26:51 +03001356
1357 if (!list_empty(&priv->cm.passive_ids))
1358 queue_delayed_work(ipoib_workqueue,
1359 &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001360 spin_unlock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001361}
1362
1363
Roland Dreier2337f802007-10-23 19:57:54 -07001364static ssize_t show_mode(struct device *d, struct device_attribute *attr,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001365 char *buf)
1366{
1367 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(d));
1368
1369 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
1370 return sprintf(buf, "connected\n");
1371 else
1372 return sprintf(buf, "datagram\n");
1373}
1374
1375static ssize_t set_mode(struct device *d, struct device_attribute *attr,
1376 const char *buf, size_t count)
1377{
1378 struct net_device *dev = to_net_dev(d);
1379 struct ipoib_dev_priv *priv = netdev_priv(dev);
1380
1381 /* flush paths if we switch modes so that connections are restarted */
1382 if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
1383 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
1384 ipoib_warn(priv, "enabling connected mode "
1385 "will cause multicast packet drops\n");
Eli Cohen60461362008-04-16 21:01:10 -07001386
Eli Cohen40ca1982008-04-16 21:09:27 -07001387 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_TSO);
Eli Cohen60461362008-04-16 21:01:10 -07001388 priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
1389
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001390 ipoib_flush_paths(dev);
1391 return count;
1392 }
1393
1394 if (!strcmp(buf, "datagram\n")) {
1395 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
1396 dev->mtu = min(priv->mcast_mtu, dev->mtu);
1397 ipoib_flush_paths(dev);
Eli Cohen60461362008-04-16 21:01:10 -07001398
Eli Cohen40ca1982008-04-16 21:09:27 -07001399 if (test_bit(IPOIB_FLAG_CSUM, &priv->flags)) {
Eli Cohen60461362008-04-16 21:01:10 -07001400 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
Eli Cohen40ca1982008-04-16 21:09:27 -07001401 if (priv->hca_caps & IB_DEVICE_UD_TSO)
1402 dev->features |= NETIF_F_TSO;
1403 }
Eli Cohen60461362008-04-16 21:01:10 -07001404
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001405 return count;
1406 }
1407
1408 return -EINVAL;
1409}
1410
Roland Dreier551fd612007-02-16 13:57:33 -08001411static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO, show_mode, set_mode);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001412
1413int ipoib_cm_add_mode_attr(struct net_device *dev)
1414{
1415 return device_create_file(&dev->dev, &dev_attr_mode);
1416}
1417
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001418static void ipoib_cm_create_srq(struct net_device *dev, int max_sge)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001419{
1420 struct ipoib_dev_priv *priv = netdev_priv(dev);
1421 struct ib_srq_init_attr srq_init_attr = {
1422 .attr = {
1423 .max_wr = ipoib_recvq_size,
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001424 .max_sge = max_sge
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001425 }
1426 };
Roland Dreier7b3687d2008-01-25 14:15:24 -08001427
1428 priv->cm.srq = ib_create_srq(priv->pd, &srq_init_attr);
1429 if (IS_ERR(priv->cm.srq)) {
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001430 if (PTR_ERR(priv->cm.srq) != -ENOSYS)
1431 printk(KERN_WARNING "%s: failed to allocate SRQ, error %ld\n",
1432 priv->ca->name, PTR_ERR(priv->cm.srq));
Roland Dreier7b3687d2008-01-25 14:15:24 -08001433 priv->cm.srq = NULL;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001434 return;
Roland Dreier7b3687d2008-01-25 14:15:24 -08001435 }
1436
1437 priv->cm.srq_ring = kzalloc(ipoib_recvq_size * sizeof *priv->cm.srq_ring,
1438 GFP_KERNEL);
1439 if (!priv->cm.srq_ring) {
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001440 printk(KERN_WARNING "%s: failed to allocate CM SRQ ring (%d entries)\n",
Roland Dreier7b3687d2008-01-25 14:15:24 -08001441 priv->ca->name, ipoib_recvq_size);
1442 ib_destroy_srq(priv->cm.srq);
1443 priv->cm.srq = NULL;
Roland Dreier7b3687d2008-01-25 14:15:24 -08001444 }
Roland Dreier7b3687d2008-01-25 14:15:24 -08001445}
1446
1447int ipoib_cm_dev_init(struct net_device *dev)
1448{
1449 struct ipoib_dev_priv *priv = netdev_priv(dev);
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001450 int i, ret;
1451 struct ib_device_attr attr;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001452
1453 INIT_LIST_HEAD(&priv->cm.passive_ids);
1454 INIT_LIST_HEAD(&priv->cm.reap_list);
1455 INIT_LIST_HEAD(&priv->cm.start_list);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001456 INIT_LIST_HEAD(&priv->cm.rx_error_list);
1457 INIT_LIST_HEAD(&priv->cm.rx_flush_list);
1458 INIT_LIST_HEAD(&priv->cm.rx_drain_list);
1459 INIT_LIST_HEAD(&priv->cm.rx_reap_list);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001460 INIT_WORK(&priv->cm.start_task, ipoib_cm_tx_start);
1461 INIT_WORK(&priv->cm.reap_task, ipoib_cm_tx_reap);
1462 INIT_WORK(&priv->cm.skb_task, ipoib_cm_skb_reap);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001463 INIT_WORK(&priv->cm.rx_reap_task, ipoib_cm_rx_reap);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001464 INIT_DELAYED_WORK(&priv->cm.stale_task, ipoib_cm_stale_task);
1465
1466 skb_queue_head_init(&priv->cm.skb_queue);
1467
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001468 ret = ib_query_device(priv->ca, &attr);
1469 if (ret) {
1470 printk(KERN_WARNING "ib_query_device() failed with %d\n", ret);
1471 return ret;
1472 }
1473
1474 ipoib_dbg(priv, "max_srq_sge=%d\n", attr.max_srq_sge);
1475
1476 attr.max_srq_sge = min_t(int, IPOIB_CM_RX_SG, attr.max_srq_sge);
1477 ipoib_cm_create_srq(dev, attr.max_srq_sge);
1478 if (ipoib_cm_has_srq(dev)) {
1479 priv->cm.max_cm_mtu = attr.max_srq_sge * PAGE_SIZE - 0x10;
1480 priv->cm.num_frags = attr.max_srq_sge;
1481 ipoib_dbg(priv, "max_cm_mtu = 0x%x, num_frags=%d\n",
1482 priv->cm.max_cm_mtu, priv->cm.num_frags);
1483 } else {
1484 priv->cm.max_cm_mtu = IPOIB_CM_MTU;
1485 priv->cm.num_frags = IPOIB_CM_RX_SG;
1486 }
1487
1488 for (i = 0; i < priv->cm.num_frags; ++i)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001489 priv->cm.rx_sge[i].lkey = priv->mr->lkey;
1490
1491 priv->cm.rx_sge[0].length = IPOIB_CM_HEAD_SIZE;
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001492 for (i = 1; i < priv->cm.num_frags; ++i)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001493 priv->cm.rx_sge[i].length = PAGE_SIZE;
1494 priv->cm.rx_wr.next = NULL;
1495 priv->cm.rx_wr.sg_list = priv->cm.rx_sge;
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001496 priv->cm.rx_wr.num_sge = priv->cm.num_frags;
Roland Dreier7b3687d2008-01-25 14:15:24 -08001497
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001498 if (ipoib_cm_has_srq(dev)) {
1499 for (i = 0; i < ipoib_recvq_size; ++i) {
1500 if (!ipoib_cm_alloc_rx_skb(dev, priv->cm.srq_ring, i,
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001501 priv->cm.num_frags - 1,
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001502 priv->cm.srq_ring[i].mapping)) {
1503 ipoib_warn(priv, "failed to allocate "
1504 "receive buffer %d\n", i);
1505 ipoib_cm_dev_cleanup(dev);
1506 return -ENOMEM;
1507 }
1508
1509 if (ipoib_cm_post_receive_srq(dev, i)) {
1510 ipoib_warn(priv, "ipoib_cm_post_receive_srq "
1511 "failed for buf %d\n", i);
1512 ipoib_cm_dev_cleanup(dev);
1513 return -EIO;
1514 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001515 }
1516 }
1517
1518 priv->dev->dev_addr[0] = IPOIB_FLAGS_RC;
1519 return 0;
1520}
1521
1522void ipoib_cm_dev_cleanup(struct net_device *dev)
1523{
1524 struct ipoib_dev_priv *priv = netdev_priv(dev);
Roland Dreier1efb6142008-01-25 14:15:24 -08001525 int ret;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001526
1527 if (!priv->cm.srq)
1528 return;
1529
1530 ipoib_dbg(priv, "Cleanup ipoib connected mode.\n");
1531
1532 ret = ib_destroy_srq(priv->cm.srq);
1533 if (ret)
1534 ipoib_warn(priv, "ib_destroy_srq failed: %d\n", ret);
1535
1536 priv->cm.srq = NULL;
1537 if (!priv->cm.srq_ring)
1538 return;
Roland Dreier1efb6142008-01-25 14:15:24 -08001539
1540 ipoib_cm_free_rx_ring(dev, priv->cm.srq_ring);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001541 priv->cm.srq_ring = NULL;
1542}