blob: 6223fc39af7035bacf7e22e0f8222d44c1616128 [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.
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020031 */
32
33#include <rdma/ib_cm.h>
34#include <rdma/ib_cache.h>
35#include <net/dst.h>
36#include <net/icmp.h>
37#include <linux/icmpv6.h>
Michael S. Tsirkin518b1642007-05-21 15:04:59 +030038#include <linux/delay.h>
Roland Dreier10313cb2008-03-12 07:51:03 -070039#include <linux/vmalloc.h>
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020040
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -080041#include "ipoib.h"
42
43int ipoib_max_conn_qp = 128;
44
45module_param_named(max_nonsrq_conn_qp, ipoib_max_conn_qp, int, 0444);
46MODULE_PARM_DESC(max_nonsrq_conn_qp,
47 "Max number of connected-mode QPs per interface "
48 "(applied only if shared receive queue is not available)");
49
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020050#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
51static int data_debug_level;
52
53module_param_named(cm_data_debug_level, data_debug_level, int, 0644);
54MODULE_PARM_DESC(cm_data_debug_level,
55 "Enable data path debug tracing for connected mode if > 0");
56#endif
57
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020058#define IPOIB_CM_IETF_ID 0x1000000000000000ULL
59
60#define IPOIB_CM_RX_UPDATE_TIME (256 * HZ)
61#define IPOIB_CM_RX_TIMEOUT (2 * 256 * HZ)
62#define IPOIB_CM_RX_DELAY (3 * 256 * HZ)
63#define IPOIB_CM_RX_UPDATE_MASK (0x3)
64
Michael S. Tsirkin518b1642007-05-21 15:04:59 +030065static struct ib_qp_attr ipoib_cm_err_attr = {
66 .qp_state = IB_QPS_ERR
67};
68
Roland Dreier09f60f82007-10-26 13:44:25 -070069#define IPOIB_CM_RX_DRAIN_WRID 0xffffffff
Michael S. Tsirkin518b1642007-05-21 15:04:59 +030070
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +030071static struct ib_send_wr ipoib_cm_rx_drain_wr = {
72 .wr_id = IPOIB_CM_RX_DRAIN_WRID,
73 .opcode = IB_WR_SEND,
Michael S. Tsirkin518b1642007-05-21 15:04:59 +030074};
75
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020076static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
77 struct ib_cm_event *event);
78
Michael S. Tsirkin18120632007-02-20 20:17:55 +020079static void ipoib_cm_dma_unmap_rx(struct ipoib_dev_priv *priv, int frags,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020080 u64 mapping[IPOIB_CM_RX_SG])
81{
82 int i;
83
84 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
85
Michael S. Tsirkin18120632007-02-20 20:17:55 +020086 for (i = 0; i < frags; ++i)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020087 ib_dma_unmap_single(priv->ca, mapping[i + 1], PAGE_SIZE, DMA_FROM_DEVICE);
88}
89
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -080090static int ipoib_cm_post_receive_srq(struct net_device *dev, int id)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020091{
92 struct ipoib_dev_priv *priv = netdev_priv(dev);
93 struct ib_recv_wr *bad_wr;
94 int i, ret;
95
Michael S. Tsirkin1b524962007-08-16 15:36:16 +030096 priv->cm.rx_wr.wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020097
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -080098 for (i = 0; i < priv->cm.num_frags; ++i)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020099 priv->cm.rx_sge[i].addr = priv->cm.srq_ring[id].mapping[i];
100
101 ret = ib_post_srq_recv(priv->cm.srq, &priv->cm.rx_wr, &bad_wr);
102 if (unlikely(ret)) {
103 ipoib_warn(priv, "post srq failed for buf %d (%d)\n", id, ret);
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800104 ipoib_cm_dma_unmap_rx(priv, priv->cm.num_frags - 1,
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200105 priv->cm.srq_ring[id].mapping);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200106 dev_kfree_skb_any(priv->cm.srq_ring[id].skb);
107 priv->cm.srq_ring[id].skb = NULL;
108 }
109
110 return ret;
111}
112
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800113static int ipoib_cm_post_receive_nonsrq(struct net_device *dev,
114 struct ipoib_cm_rx *rx, int id)
115{
116 struct ipoib_dev_priv *priv = netdev_priv(dev);
117 struct ib_recv_wr *bad_wr;
118 int i, ret;
119
120 priv->cm.rx_wr.wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV;
121
122 for (i = 0; i < IPOIB_CM_RX_SG; ++i)
123 priv->cm.rx_sge[i].addr = rx->rx_ring[id].mapping[i];
124
125 ret = ib_post_recv(rx->qp, &priv->cm.rx_wr, &bad_wr);
126 if (unlikely(ret)) {
127 ipoib_warn(priv, "post recv failed for buf %d (%d)\n", id, ret);
128 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
129 rx->rx_ring[id].mapping);
130 dev_kfree_skb_any(rx->rx_ring[id].skb);
131 rx->rx_ring[id].skb = NULL;
132 }
133
134 return ret;
135}
136
137static struct sk_buff *ipoib_cm_alloc_rx_skb(struct net_device *dev,
138 struct ipoib_cm_rx_buf *rx_ring,
139 int id, int frags,
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200140 u64 mapping[IPOIB_CM_RX_SG])
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200141{
142 struct ipoib_dev_priv *priv = netdev_priv(dev);
143 struct sk_buff *skb;
144 int i;
145
146 skb = dev_alloc_skb(IPOIB_CM_HEAD_SIZE + 12);
147 if (unlikely(!skb))
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200148 return NULL;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200149
150 /*
151 * IPoIB adds a 4 byte header. So we need 12 more bytes to align the
152 * IP header to a multiple of 16.
153 */
154 skb_reserve(skb, 12);
155
156 mapping[0] = ib_dma_map_single(priv->ca, skb->data, IPOIB_CM_HEAD_SIZE,
157 DMA_FROM_DEVICE);
158 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0]))) {
159 dev_kfree_skb_any(skb);
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200160 return NULL;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200161 }
162
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200163 for (i = 0; i < frags; i++) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200164 struct page *page = alloc_page(GFP_ATOMIC);
165
166 if (!page)
167 goto partial_error;
168 skb_fill_page_desc(skb, i, page, 0, PAGE_SIZE);
169
170 mapping[i + 1] = ib_dma_map_page(priv->ca, skb_shinfo(skb)->frags[i].page,
Michael S. Tsirkin6371ea32007-04-10 18:32:42 +0300171 0, PAGE_SIZE, DMA_FROM_DEVICE);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200172 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[i + 1])))
173 goto partial_error;
174 }
175
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800176 rx_ring[id].skb = skb;
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200177 return skb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200178
179partial_error:
180
181 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
182
Ralph Campbell841adfc2007-06-29 11:37:56 -0700183 for (; i > 0; --i)
184 ib_dma_unmap_single(priv->ca, mapping[i], PAGE_SIZE, DMA_FROM_DEVICE);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200185
Michael S. Tsirkin8a2e65f2007-02-16 00:16:13 +0200186 dev_kfree_skb_any(skb);
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200187 return NULL;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200188}
189
Roland Dreier1efb6142008-01-25 14:15:24 -0800190static void ipoib_cm_free_rx_ring(struct net_device *dev,
191 struct ipoib_cm_rx_buf *rx_ring)
192{
193 struct ipoib_dev_priv *priv = netdev_priv(dev);
194 int i;
195
196 for (i = 0; i < ipoib_recvq_size; ++i)
197 if (rx_ring[i].skb) {
198 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
199 rx_ring[i].mapping);
200 dev_kfree_skb_any(rx_ring[i].skb);
201 }
202
203 kfree(rx_ring);
204}
205
Roland Dreier2337f802007-10-23 19:57:54 -0700206static void ipoib_cm_start_rx_drain(struct ipoib_dev_priv *priv)
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300207{
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300208 struct ib_send_wr *bad_wr;
209 struct ipoib_cm_rx *p;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300210
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300211 /* We only reserved 1 extra slot in CQ for drain WRs, so
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300212 * make sure we have at most 1 outstanding WR. */
213 if (list_empty(&priv->cm.rx_flush_list) ||
214 !list_empty(&priv->cm.rx_drain_list))
215 return;
216
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300217 /*
218 * QPs on flush list are error state. This way, a "flush
219 * error" WC will be immediately generated for each WR we post.
220 */
221 p = list_entry(priv->cm.rx_flush_list.next, typeof(*p), list);
222 if (ib_post_send(p->qp, &ipoib_cm_rx_drain_wr, &bad_wr))
223 ipoib_warn(priv, "failed to post drain wr\n");
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300224
225 list_splice_init(&priv->cm.rx_flush_list, &priv->cm.rx_drain_list);
226}
227
228static void ipoib_cm_rx_event_handler(struct ib_event *event, void *ctx)
229{
230 struct ipoib_cm_rx *p = ctx;
231 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
232 unsigned long flags;
233
234 if (event->event != IB_EVENT_QP_LAST_WQE_REACHED)
235 return;
236
237 spin_lock_irqsave(&priv->lock, flags);
238 list_move(&p->list, &priv->cm.rx_flush_list);
239 p->state = IPOIB_CM_RX_FLUSH;
240 ipoib_cm_start_rx_drain(priv);
241 spin_unlock_irqrestore(&priv->lock, flags);
242}
243
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200244static struct ib_qp *ipoib_cm_create_rx_qp(struct net_device *dev,
245 struct ipoib_cm_rx *p)
246{
247 struct ipoib_dev_priv *priv = netdev_priv(dev);
248 struct ib_qp_init_attr attr = {
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300249 .event_handler = ipoib_cm_rx_event_handler,
Eli Cohenf56bcd82008-04-29 13:46:53 -0700250 .send_cq = priv->recv_cq, /* For drain WR */
251 .recv_cq = priv->recv_cq,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200252 .srq = priv->cm.srq,
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300253 .cap.max_send_wr = 1, /* For drain WR */
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200254 .cap.max_send_sge = 1, /* FIXME: 0 Seems not to work */
255 .sq_sig_type = IB_SIGNAL_ALL_WR,
256 .qp_type = IB_QPT_RC,
257 .qp_context = p,
258 };
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800259
260 if (!ipoib_cm_has_srq(dev)) {
261 attr.cap.max_recv_wr = ipoib_recvq_size;
262 attr.cap.max_recv_sge = IPOIB_CM_RX_SG;
263 }
264
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200265 return ib_create_qp(priv->pd, &attr);
266}
267
268static int ipoib_cm_modify_rx_qp(struct net_device *dev,
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800269 struct ib_cm_id *cm_id, struct ib_qp *qp,
270 unsigned psn)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200271{
272 struct ipoib_dev_priv *priv = netdev_priv(dev);
273 struct ib_qp_attr qp_attr;
274 int qp_attr_mask, ret;
275
276 qp_attr.qp_state = IB_QPS_INIT;
277 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
278 if (ret) {
279 ipoib_warn(priv, "failed to init QP attr for INIT: %d\n", ret);
280 return ret;
281 }
282 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
283 if (ret) {
284 ipoib_warn(priv, "failed to modify QP to INIT: %d\n", ret);
285 return ret;
286 }
287 qp_attr.qp_state = IB_QPS_RTR;
288 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
289 if (ret) {
290 ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
291 return ret;
292 }
293 qp_attr.rq_psn = psn;
294 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
295 if (ret) {
296 ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
297 return ret;
298 }
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300299
300 /*
301 * Current Mellanox HCA firmware won't generate completions
302 * with error for drain WRs unless the QP has been moved to
303 * RTS first. This work-around leaves a window where a QP has
304 * moved to error asynchronously, but this will eventually get
305 * fixed in firmware, so let's not error out if modify QP
306 * fails.
307 */
308 qp_attr.qp_state = IB_QPS_RTS;
309 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
310 if (ret) {
311 ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
312 return 0;
313 }
314 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
315 if (ret) {
316 ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
317 return 0;
318 }
319
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200320 return 0;
321}
322
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800323static int ipoib_cm_nonsrq_init_rx(struct net_device *dev, struct ib_cm_id *cm_id,
324 struct ipoib_cm_rx *rx)
325{
326 struct ipoib_dev_priv *priv = netdev_priv(dev);
327 int ret;
328 int i;
329
330 rx->rx_ring = kcalloc(ipoib_recvq_size, sizeof *rx->rx_ring, GFP_KERNEL);
331 if (!rx->rx_ring)
332 return -ENOMEM;
333
334 spin_lock_irq(&priv->lock);
335
336 if (priv->cm.nonsrq_conn_qp >= ipoib_max_conn_qp) {
337 spin_unlock_irq(&priv->lock);
338 ib_send_cm_rej(cm_id, IB_CM_REJ_NO_QP, NULL, 0, NULL, 0);
339 ret = -EINVAL;
340 goto err_free;
341 } else
342 ++priv->cm.nonsrq_conn_qp;
343
344 spin_unlock_irq(&priv->lock);
345
346 for (i = 0; i < ipoib_recvq_size; ++i) {
347 if (!ipoib_cm_alloc_rx_skb(dev, rx->rx_ring, i, IPOIB_CM_RX_SG - 1,
348 rx->rx_ring[i].mapping)) {
349 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
350 ret = -ENOMEM;
351 goto err_count;
352 }
353 ret = ipoib_cm_post_receive_nonsrq(dev, rx, i);
354 if (ret) {
355 ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq "
356 "failed for buf %d\n", i);
357 ret = -EIO;
358 goto err_count;
359 }
360 }
361
362 rx->recv_count = ipoib_recvq_size;
363
364 return 0;
365
366err_count:
367 spin_lock_irq(&priv->lock);
368 --priv->cm.nonsrq_conn_qp;
369 spin_unlock_irq(&priv->lock);
370
371err_free:
372 ipoib_cm_free_rx_ring(dev, rx->rx_ring);
373
374 return ret;
375}
376
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200377static int ipoib_cm_send_rep(struct net_device *dev, struct ib_cm_id *cm_id,
378 struct ib_qp *qp, struct ib_cm_req_event_param *req,
379 unsigned psn)
380{
381 struct ipoib_dev_priv *priv = netdev_priv(dev);
382 struct ipoib_cm_data data = {};
383 struct ib_cm_rep_param rep = {};
384
385 data.qpn = cpu_to_be32(priv->qp->qp_num);
386 data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
387
388 rep.private_data = &data;
389 rep.private_data_len = sizeof data;
390 rep.flow_control = 0;
391 rep.rnr_retry_count = req->rnr_retry_count;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800392 rep.srq = ipoib_cm_has_srq(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200393 rep.qp_num = qp->qp_num;
394 rep.starting_psn = psn;
395 return ib_send_cm_rep(cm_id, &rep);
396}
397
398static int ipoib_cm_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
399{
400 struct net_device *dev = cm_id->context;
401 struct ipoib_dev_priv *priv = netdev_priv(dev);
402 struct ipoib_cm_rx *p;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200403 unsigned psn;
404 int ret;
405
406 ipoib_dbg(priv, "REQ arrived\n");
407 p = kzalloc(sizeof *p, GFP_KERNEL);
408 if (!p)
409 return -ENOMEM;
410 p->dev = dev;
411 p->id = cm_id;
Michael S. Tsirkin3ec73932007-06-19 13:40:41 +0300412 cm_id->context = p;
413 p->state = IPOIB_CM_RX_LIVE;
414 p->jiffies = jiffies;
415 INIT_LIST_HEAD(&p->list);
416
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200417 p->qp = ipoib_cm_create_rx_qp(dev, p);
418 if (IS_ERR(p->qp)) {
419 ret = PTR_ERR(p->qp);
420 goto err_qp;
421 }
422
423 psn = random32() & 0xffffff;
424 ret = ipoib_cm_modify_rx_qp(dev, cm_id, p->qp, psn);
425 if (ret)
426 goto err_modify;
427
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800428 if (!ipoib_cm_has_srq(dev)) {
429 ret = ipoib_cm_nonsrq_init_rx(dev, cm_id, p);
430 if (ret)
431 goto err_modify;
432 }
433
Michael S. Tsirkin3ec73932007-06-19 13:40:41 +0300434 spin_lock_irq(&priv->lock);
435 queue_delayed_work(ipoib_workqueue,
436 &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
437 /* Add this entry to passive ids list head, but do not re-add it
438 * if IB_EVENT_QP_LAST_WQE_REACHED has moved it to flush list. */
439 p->jiffies = jiffies;
440 if (p->state == IPOIB_CM_RX_LIVE)
441 list_move(&p->list, &priv->cm.passive_ids);
442 spin_unlock_irq(&priv->lock);
443
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200444 ret = ipoib_cm_send_rep(dev, cm_id, p->qp, &event->param.req_rcvd, psn);
445 if (ret) {
446 ipoib_warn(priv, "failed to send REP: %d\n", ret);
Michael S. Tsirkin3ec73932007-06-19 13:40:41 +0300447 if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE))
448 ipoib_warn(priv, "unable to move qp to error state\n");
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200449 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200450 return 0;
451
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200452err_modify:
453 ib_destroy_qp(p->qp);
454err_qp:
455 kfree(p);
456 return ret;
457}
458
459static int ipoib_cm_rx_handler(struct ib_cm_id *cm_id,
460 struct ib_cm_event *event)
461{
462 struct ipoib_cm_rx *p;
463 struct ipoib_dev_priv *priv;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200464
465 switch (event->event) {
466 case IB_CM_REQ_RECEIVED:
467 return ipoib_cm_req_handler(cm_id, event);
468 case IB_CM_DREQ_RECEIVED:
469 p = cm_id->context;
470 ib_send_cm_drep(cm_id, NULL, 0);
471 /* Fall through */
472 case IB_CM_REJ_RECEIVED:
473 p = cm_id->context;
474 priv = netdev_priv(p->dev);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300475 if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE))
476 ipoib_warn(priv, "unable to move qp to error state\n");
477 /* Fall through */
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200478 default:
479 return 0;
480 }
481}
482/* Adjust length of skb with fragments to match received data */
483static void skb_put_frags(struct sk_buff *skb, unsigned int hdr_space,
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200484 unsigned int length, struct sk_buff *toskb)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200485{
486 int i, num_frags;
487 unsigned int size;
488
489 /* put header into skb */
490 size = min(length, hdr_space);
491 skb->tail += size;
492 skb->len += size;
493 length -= size;
494
495 num_frags = skb_shinfo(skb)->nr_frags;
496 for (i = 0; i < num_frags; i++) {
497 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
498
499 if (length == 0) {
500 /* don't need this page */
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200501 skb_fill_page_desc(toskb, i, frag->page, 0, PAGE_SIZE);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200502 --skb_shinfo(skb)->nr_frags;
503 } else {
504 size = min(length, (unsigned) PAGE_SIZE);
505
506 frag->size = size;
507 skb->data_len += size;
508 skb->truesize += size;
509 skb->len += size;
510 length -= size;
511 }
512 }
513}
514
515void ipoib_cm_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
516{
517 struct ipoib_dev_priv *priv = netdev_priv(dev);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800518 struct ipoib_cm_rx_buf *rx_ring;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300519 unsigned int wr_id = wc->wr_id & ~(IPOIB_OP_CM | IPOIB_OP_RECV);
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200520 struct sk_buff *skb, *newskb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200521 struct ipoib_cm_rx *p;
522 unsigned long flags;
523 u64 mapping[IPOIB_CM_RX_SG];
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200524 int frags;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800525 int has_srq;
Eli Cohenf89271da2008-07-14 23:48:44 -0700526 struct sk_buff *small_skb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200527
Roland Dreiera89875f2007-04-18 20:20:53 -0700528 ipoib_dbg_data(priv, "cm recv completion: id %d, status: %d\n",
529 wr_id, wc->status);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200530
531 if (unlikely(wr_id >= ipoib_recvq_size)) {
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300532 if (wr_id == (IPOIB_CM_RX_DRAIN_WRID & ~(IPOIB_OP_CM | IPOIB_OP_RECV))) {
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300533 spin_lock_irqsave(&priv->lock, flags);
534 list_splice_init(&priv->cm.rx_drain_list, &priv->cm.rx_reap_list);
535 ipoib_cm_start_rx_drain(priv);
536 queue_work(ipoib_workqueue, &priv->cm.rx_reap_task);
537 spin_unlock_irqrestore(&priv->lock, flags);
538 } else
539 ipoib_warn(priv, "cm recv completion event with wrid %d (> %d)\n",
540 wr_id, ipoib_recvq_size);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200541 return;
542 }
543
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800544 p = wc->qp->qp_context;
545
546 has_srq = ipoib_cm_has_srq(dev);
547 rx_ring = has_srq ? priv->cm.srq_ring : p->rx_ring;
548
549 skb = rx_ring[wr_id].skb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200550
551 if (unlikely(wc->status != IB_WC_SUCCESS)) {
552 ipoib_dbg(priv, "cm recv error "
553 "(status=%d, wrid=%d vend_err %x)\n",
554 wc->status, wr_id, wc->vendor_err);
Roland Dreierde903512007-09-28 15:33:51 -0700555 ++dev->stats.rx_dropped;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800556 if (has_srq)
557 goto repost;
558 else {
559 if (!--p->recv_count) {
560 spin_lock_irqsave(&priv->lock, flags);
561 list_move(&p->list, &priv->cm.rx_reap_list);
562 spin_unlock_irqrestore(&priv->lock, flags);
563 queue_work(ipoib_workqueue, &priv->cm.rx_reap_task);
564 }
565 return;
566 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200567 }
568
Roland Dreierfd312562007-10-17 21:54:44 -0700569 if (unlikely(!(wr_id & IPOIB_CM_RX_UPDATE_MASK))) {
Michael S. Tsirkind6ef7d62007-05-02 15:31:12 +0300570 if (p && time_after_eq(jiffies, p->jiffies + IPOIB_CM_RX_UPDATE_TIME)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200571 spin_lock_irqsave(&priv->lock, flags);
572 p->jiffies = jiffies;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300573 /* Move this entry to list head, but do not re-add it
574 * if it has been moved out of list. */
575 if (p->state == IPOIB_CM_RX_LIVE)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200576 list_move(&p->list, &priv->cm.passive_ids);
577 spin_unlock_irqrestore(&priv->lock, flags);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200578 }
579 }
580
Eli Cohenf89271da2008-07-14 23:48:44 -0700581 if (wc->byte_len < IPOIB_CM_COPYBREAK) {
582 int dlen = wc->byte_len;
583
584 small_skb = dev_alloc_skb(dlen + 12);
585 if (small_skb) {
586 skb_reserve(small_skb, 12);
587 ib_dma_sync_single_for_cpu(priv->ca, rx_ring[wr_id].mapping[0],
588 dlen, DMA_FROM_DEVICE);
589 skb_copy_from_linear_data(skb, small_skb->data, dlen);
590 ib_dma_sync_single_for_device(priv->ca, rx_ring[wr_id].mapping[0],
591 dlen, DMA_FROM_DEVICE);
592 skb_put(small_skb, dlen);
593 skb = small_skb;
594 goto copied;
595 }
596 }
597
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200598 frags = PAGE_ALIGN(wc->byte_len - min(wc->byte_len,
599 (unsigned)IPOIB_CM_HEAD_SIZE)) / PAGE_SIZE;
600
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800601 newskb = ipoib_cm_alloc_rx_skb(dev, rx_ring, wr_id, frags, mapping);
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200602 if (unlikely(!newskb)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200603 /*
604 * If we can't allocate a new RX buffer, dump
605 * this packet and reuse the old buffer.
606 */
607 ipoib_dbg(priv, "failed to allocate receive buffer %d\n", wr_id);
Roland Dreierde903512007-09-28 15:33:51 -0700608 ++dev->stats.rx_dropped;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200609 goto repost;
610 }
611
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800612 ipoib_cm_dma_unmap_rx(priv, frags, rx_ring[wr_id].mapping);
613 memcpy(rx_ring[wr_id].mapping, mapping, (frags + 1) * sizeof *mapping);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200614
615 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
616 wc->byte_len, wc->slid);
617
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200618 skb_put_frags(skb, IPOIB_CM_HEAD_SIZE, wc->byte_len, newskb);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200619
Eli Cohenf89271da2008-07-14 23:48:44 -0700620copied:
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200621 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700622 skb_reset_mac_header(skb);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200623 skb_pull(skb, IPOIB_ENCAP_LEN);
624
625 dev->last_rx = jiffies;
Roland Dreierde903512007-09-28 15:33:51 -0700626 ++dev->stats.rx_packets;
627 dev->stats.rx_bytes += skb->len;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200628
629 skb->dev = dev;
630 /* XXX get correct PACKET_ type here */
631 skb->pkt_type = PACKET_HOST;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700632 netif_receive_skb(skb);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200633
634repost:
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800635 if (has_srq) {
636 if (unlikely(ipoib_cm_post_receive_srq(dev, wr_id)))
637 ipoib_warn(priv, "ipoib_cm_post_receive_srq failed "
638 "for buf %d\n", wr_id);
639 } else {
640 if (unlikely(ipoib_cm_post_receive_nonsrq(dev, p, wr_id))) {
641 --p->recv_count;
642 ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq failed "
643 "for buf %d\n", wr_id);
644 }
645 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200646}
647
648static inline int post_send(struct ipoib_dev_priv *priv,
649 struct ipoib_cm_tx *tx,
650 unsigned int wr_id,
651 u64 addr, int len)
652{
653 struct ib_send_wr *bad_wr;
654
Eli Cohen71437402008-01-30 18:30:53 +0200655 priv->tx_sge[0].addr = addr;
656 priv->tx_sge[0].length = len;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200657
Roland Dreier42004062008-03-11 18:35:20 -0700658 priv->tx_wr.num_sge = 1;
Roland Dreier2337f802007-10-23 19:57:54 -0700659 priv->tx_wr.wr_id = wr_id | IPOIB_OP_CM;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200660
661 return ib_post_send(tx->qp, &priv->tx_wr, &bad_wr);
662}
663
664void ipoib_cm_send(struct net_device *dev, struct sk_buff *skb, struct ipoib_cm_tx *tx)
665{
666 struct ipoib_dev_priv *priv = netdev_priv(dev);
667 struct ipoib_tx_buf *tx_req;
668 u64 addr;
669
670 if (unlikely(skb->len > tx->mtu)) {
671 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
672 skb->len, tx->mtu);
Roland Dreierde903512007-09-28 15:33:51 -0700673 ++dev->stats.tx_dropped;
674 ++dev->stats.tx_errors;
Michael S. Tsirkin77d8e1e2007-03-21 15:45:05 +0200675 ipoib_cm_skb_too_long(dev, skb, tx->mtu - IPOIB_ENCAP_LEN);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200676 return;
677 }
678
679 ipoib_dbg_data(priv, "sending packet: head 0x%x length %d connection 0x%x\n",
680 tx->tx_head, skb->len, tx->qp->qp_num);
681
682 /*
683 * We put the skb into the tx_ring _before_ we call post_send()
684 * because it's entirely possible that the completion handler will
685 * run before we execute anything after the post_send(). That
686 * means we have to make sure everything is properly recorded and
687 * our state is consistent before we call post_send().
688 */
689 tx_req = &tx->tx_ring[tx->tx_head & (ipoib_sendq_size - 1)];
690 tx_req->skb = skb;
691 addr = ib_dma_map_single(priv->ca, skb->data, skb->len, DMA_TO_DEVICE);
692 if (unlikely(ib_dma_mapping_error(priv->ca, addr))) {
Roland Dreierde903512007-09-28 15:33:51 -0700693 ++dev->stats.tx_errors;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200694 dev_kfree_skb_any(skb);
695 return;
696 }
697
Eli Cohen71437402008-01-30 18:30:53 +0200698 tx_req->mapping[0] = addr;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200699
700 if (unlikely(post_send(priv, tx, tx->tx_head & (ipoib_sendq_size - 1),
Roland Dreier2337f802007-10-23 19:57:54 -0700701 addr, skb->len))) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200702 ipoib_warn(priv, "post_send failed\n");
Roland Dreierde903512007-09-28 15:33:51 -0700703 ++dev->stats.tx_errors;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200704 ib_dma_unmap_single(priv->ca, addr, skb->len, DMA_TO_DEVICE);
705 dev_kfree_skb_any(skb);
706 } else {
707 dev->trans_start = jiffies;
708 ++tx->tx_head;
709
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300710 if (++priv->tx_outstanding == ipoib_sendq_size) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200711 ipoib_dbg(priv, "TX ring 0x%x full, stopping kernel net queue\n",
712 tx->qp->qp_num);
713 netif_stop_queue(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200714 }
715 }
716}
717
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300718void ipoib_cm_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200719{
720 struct ipoib_dev_priv *priv = netdev_priv(dev);
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300721 struct ipoib_cm_tx *tx = wc->qp->qp_context;
722 unsigned int wr_id = wc->wr_id & ~IPOIB_OP_CM;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200723 struct ipoib_tx_buf *tx_req;
724 unsigned long flags;
725
Roland Dreiera89875f2007-04-18 20:20:53 -0700726 ipoib_dbg_data(priv, "cm send completion: id %d, status: %d\n",
727 wr_id, wc->status);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200728
729 if (unlikely(wr_id >= ipoib_sendq_size)) {
730 ipoib_warn(priv, "cm send completion event with wrid %d (> %d)\n",
731 wr_id, ipoib_sendq_size);
732 return;
733 }
734
735 tx_req = &tx->tx_ring[wr_id];
736
Eli Cohen71437402008-01-30 18:30:53 +0200737 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 +0200738
739 /* FIXME: is this right? Shouldn't we only increment on success? */
Roland Dreierde903512007-09-28 15:33:51 -0700740 ++dev->stats.tx_packets;
741 dev->stats.tx_bytes += tx_req->skb->len;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200742
743 dev_kfree_skb_any(tx_req->skb);
744
745 spin_lock_irqsave(&priv->tx_lock, flags);
746 ++tx->tx_tail;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300747 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
748 netif_queue_stopped(dev) &&
749 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200750 netif_wake_queue(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200751
752 if (wc->status != IB_WC_SUCCESS &&
753 wc->status != IB_WC_WR_FLUSH_ERR) {
754 struct ipoib_neigh *neigh;
755
756 ipoib_dbg(priv, "failed cm send event "
757 "(status=%d, wrid=%d vend_err %x)\n",
758 wc->status, wr_id, wc->vendor_err);
759
760 spin_lock(&priv->lock);
761 neigh = tx->neigh;
762
763 if (neigh) {
764 neigh->cm = NULL;
765 list_del(&neigh->list);
766 if (neigh->ah)
767 ipoib_put_ah(neigh->ah);
768 ipoib_neigh_free(dev, neigh);
769
770 tx->neigh = NULL;
771 }
772
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200773 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
774 list_move(&tx->list, &priv->cm.reap_list);
775 queue_work(ipoib_workqueue, &priv->cm.reap_task);
776 }
777
778 clear_bit(IPOIB_FLAG_OPER_UP, &tx->flags);
779
780 spin_unlock(&priv->lock);
781 }
782
783 spin_unlock_irqrestore(&priv->tx_lock, flags);
784}
785
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200786int ipoib_cm_dev_open(struct net_device *dev)
787{
788 struct ipoib_dev_priv *priv = netdev_priv(dev);
789 int ret;
790
791 if (!IPOIB_CM_SUPPORTED(dev->dev_addr))
792 return 0;
793
794 priv->cm.id = ib_create_cm_id(priv->ca, ipoib_cm_rx_handler, dev);
795 if (IS_ERR(priv->cm.id)) {
796 printk(KERN_WARNING "%s: failed to create CM ID\n", priv->ca->name);
Michael S. Tsirkin347fcfb2007-04-30 17:30:28 -0700797 ret = PTR_ERR(priv->cm.id);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300798 goto err_cm;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200799 }
800
801 ret = ib_cm_listen(priv->cm.id, cpu_to_be64(IPOIB_CM_IETF_ID | priv->qp->qp_num),
802 0, NULL);
803 if (ret) {
804 printk(KERN_WARNING "%s: failed to listen on ID 0x%llx\n", priv->ca->name,
805 IPOIB_CM_IETF_ID | priv->qp->qp_num);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300806 goto err_listen;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200807 }
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300808
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200809 return 0;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300810
811err_listen:
812 ib_destroy_cm_id(priv->cm.id);
813err_cm:
814 priv->cm.id = NULL;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300815 return ret;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200816}
817
Roland Dreierefcd9972008-01-25 14:15:24 -0800818static void ipoib_cm_free_rx_reap_list(struct net_device *dev)
819{
820 struct ipoib_dev_priv *priv = netdev_priv(dev);
821 struct ipoib_cm_rx *rx, *n;
822 LIST_HEAD(list);
823
824 spin_lock_irq(&priv->lock);
825 list_splice_init(&priv->cm.rx_reap_list, &list);
826 spin_unlock_irq(&priv->lock);
827
828 list_for_each_entry_safe(rx, n, &list, list) {
829 ib_destroy_cm_id(rx->id);
830 ib_destroy_qp(rx->qp);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800831 if (!ipoib_cm_has_srq(dev)) {
832 ipoib_cm_free_rx_ring(priv->dev, rx->rx_ring);
833 spin_lock_irq(&priv->lock);
834 --priv->cm.nonsrq_conn_qp;
835 spin_unlock_irq(&priv->lock);
836 }
Roland Dreierefcd9972008-01-25 14:15:24 -0800837 kfree(rx);
838 }
839}
840
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200841void ipoib_cm_dev_stop(struct net_device *dev)
842{
843 struct ipoib_dev_priv *priv = netdev_priv(dev);
Roland Dreierefcd9972008-01-25 14:15:24 -0800844 struct ipoib_cm_rx *p;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300845 unsigned long begin;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300846 int ret;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200847
Michael S. Tsirkin347fcfb2007-04-30 17:30:28 -0700848 if (!IPOIB_CM_SUPPORTED(dev->dev_addr) || !priv->cm.id)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200849 return;
850
851 ib_destroy_cm_id(priv->cm.id);
Michael S. Tsirkin347fcfb2007-04-30 17:30:28 -0700852 priv->cm.id = NULL;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300853
Roland Dreier37aebbde2007-04-24 21:30:37 -0700854 spin_lock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200855 while (!list_empty(&priv->cm.passive_ids)) {
856 p = list_entry(priv->cm.passive_ids.next, typeof(*p), list);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300857 list_move(&p->list, &priv->cm.rx_error_list);
858 p->state = IPOIB_CM_RX_ERROR;
Roland Dreier37aebbde2007-04-24 21:30:37 -0700859 spin_unlock_irq(&priv->lock);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300860 ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
861 if (ret)
862 ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
863 spin_lock_irq(&priv->lock);
864 }
865
866 /* Wait for all RX to be drained */
867 begin = jiffies;
868
869 while (!list_empty(&priv->cm.rx_error_list) ||
870 !list_empty(&priv->cm.rx_flush_list) ||
871 !list_empty(&priv->cm.rx_drain_list)) {
Michael S. Tsirkin8fd357a2007-05-24 14:02:39 -0700872 if (time_after(jiffies, begin + 5 * HZ)) {
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300873 ipoib_warn(priv, "RX drain timing out\n");
874
875 /*
876 * assume the HW is wedged and just free up everything.
877 */
Pradeep Satyanarayanaec229e52008-02-12 15:00:59 -0800878 list_splice_init(&priv->cm.rx_flush_list,
879 &priv->cm.rx_reap_list);
880 list_splice_init(&priv->cm.rx_error_list,
881 &priv->cm.rx_reap_list);
882 list_splice_init(&priv->cm.rx_drain_list,
883 &priv->cm.rx_reap_list);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300884 break;
885 }
886 spin_unlock_irq(&priv->lock);
887 msleep(1);
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300888 ipoib_drain_cq(dev);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300889 spin_lock_irq(&priv->lock);
890 }
891
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300892 spin_unlock_irq(&priv->lock);
893
Roland Dreierefcd9972008-01-25 14:15:24 -0800894 ipoib_cm_free_rx_reap_list(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200895
896 cancel_delayed_work(&priv->cm.stale_task);
897}
898
899static int ipoib_cm_rep_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
900{
901 struct ipoib_cm_tx *p = cm_id->context;
902 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
903 struct ipoib_cm_data *data = event->private_data;
904 struct sk_buff_head skqueue;
905 struct ib_qp_attr qp_attr;
906 int qp_attr_mask, ret;
907 struct sk_buff *skb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200908
909 p->mtu = be32_to_cpu(data->mtu);
910
Michael S. Tsirkin82c3aca2007-06-20 19:22:15 +0300911 if (p->mtu <= IPOIB_ENCAP_LEN) {
912 ipoib_warn(priv, "Rejecting connection: mtu %d <= %d\n",
913 p->mtu, IPOIB_ENCAP_LEN);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200914 return -EINVAL;
915 }
916
917 qp_attr.qp_state = IB_QPS_RTR;
918 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
919 if (ret) {
920 ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
921 return ret;
922 }
923
924 qp_attr.rq_psn = 0 /* FIXME */;
925 ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
926 if (ret) {
927 ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
928 return ret;
929 }
930
931 qp_attr.qp_state = IB_QPS_RTS;
932 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
933 if (ret) {
934 ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
935 return ret;
936 }
937 ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
938 if (ret) {
939 ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
940 return ret;
941 }
942
943 skb_queue_head_init(&skqueue);
944
Roland Dreier37aebbde2007-04-24 21:30:37 -0700945 spin_lock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200946 set_bit(IPOIB_FLAG_OPER_UP, &p->flags);
947 if (p->neigh)
948 while ((skb = __skb_dequeue(&p->neigh->queue)))
949 __skb_queue_tail(&skqueue, skb);
Roland Dreier37aebbde2007-04-24 21:30:37 -0700950 spin_unlock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200951
952 while ((skb = __skb_dequeue(&skqueue))) {
953 skb->dev = p->dev;
954 if (dev_queue_xmit(skb))
955 ipoib_warn(priv, "dev_queue_xmit failed "
956 "to requeue packet\n");
957 }
958
959 ret = ib_send_cm_rtu(cm_id, NULL, 0);
960 if (ret) {
961 ipoib_warn(priv, "failed to send RTU: %d\n", ret);
962 return ret;
963 }
964 return 0;
965}
966
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300967static 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 +0200968{
969 struct ipoib_dev_priv *priv = netdev_priv(dev);
Dotan Barakede6bc02007-10-07 09:30:48 +0200970 struct ib_qp_init_attr attr = {
Eli Cohenf56bcd82008-04-29 13:46:53 -0700971 .send_cq = priv->recv_cq,
972 .recv_cq = priv->recv_cq,
Dotan Barakede6bc02007-10-07 09:30:48 +0200973 .srq = priv->cm.srq,
974 .cap.max_send_wr = ipoib_sendq_size,
975 .cap.max_send_sge = 1,
976 .sq_sig_type = IB_SIGNAL_ALL_WR,
977 .qp_type = IB_QPT_RC,
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300978 .qp_context = tx
Roland Dreier2337f802007-10-23 19:57:54 -0700979 };
Dotan Barakede6bc02007-10-07 09:30:48 +0200980
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200981 return ib_create_qp(priv->pd, &attr);
982}
983
984static int ipoib_cm_send_req(struct net_device *dev,
985 struct ib_cm_id *id, struct ib_qp *qp,
986 u32 qpn,
987 struct ib_sa_path_rec *pathrec)
988{
989 struct ipoib_dev_priv *priv = netdev_priv(dev);
990 struct ipoib_cm_data data = {};
991 struct ib_cm_req_param req = {};
992
993 data.qpn = cpu_to_be32(priv->qp->qp_num);
994 data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
995
Roland Dreier2337f802007-10-23 19:57:54 -0700996 req.primary_path = pathrec;
997 req.alternate_path = NULL;
998 req.service_id = cpu_to_be64(IPOIB_CM_IETF_ID | qpn);
999 req.qp_num = qp->qp_num;
1000 req.qp_type = qp->qp_type;
1001 req.private_data = &data;
1002 req.private_data_len = sizeof data;
1003 req.flow_control = 0;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001004
Roland Dreier2337f802007-10-23 19:57:54 -07001005 req.starting_psn = 0; /* FIXME */
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001006
1007 /*
1008 * Pick some arbitrary defaults here; we could make these
1009 * module parameters if anyone cared about setting them.
1010 */
Roland Dreier2337f802007-10-23 19:57:54 -07001011 req.responder_resources = 4;
1012 req.remote_cm_response_timeout = 20;
1013 req.local_cm_response_timeout = 20;
1014 req.retry_count = 0; /* RFC draft warns against retries */
1015 req.rnr_retry_count = 0; /* RFC draft warns against retries */
1016 req.max_cm_retries = 15;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001017 req.srq = ipoib_cm_has_srq(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001018 return ib_send_cm_req(id, &req);
1019}
1020
1021static int ipoib_cm_modify_tx_init(struct net_device *dev,
1022 struct ib_cm_id *cm_id, struct ib_qp *qp)
1023{
1024 struct ipoib_dev_priv *priv = netdev_priv(dev);
1025 struct ib_qp_attr qp_attr;
1026 int qp_attr_mask, ret;
Roland Dreier9fdd5e52008-04-16 21:09:35 -07001027 ret = ib_find_pkey(priv->ca, priv->port, priv->pkey, &qp_attr.pkey_index);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001028 if (ret) {
Roland Dreier9fdd5e52008-04-16 21:09:35 -07001029 ipoib_warn(priv, "pkey 0x%x not found: %d\n", priv->pkey, ret);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001030 return ret;
1031 }
1032
1033 qp_attr.qp_state = IB_QPS_INIT;
1034 qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE;
1035 qp_attr.port_num = priv->port;
1036 qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
1037
1038 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
1039 if (ret) {
1040 ipoib_warn(priv, "failed to modify tx QP to INIT: %d\n", ret);
1041 return ret;
1042 }
1043 return 0;
1044}
1045
1046static int ipoib_cm_tx_init(struct ipoib_cm_tx *p, u32 qpn,
1047 struct ib_sa_path_rec *pathrec)
1048{
1049 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
1050 int ret;
1051
Roland Dreier10313cb2008-03-12 07:51:03 -07001052 p->tx_ring = vmalloc(ipoib_sendq_size * sizeof *p->tx_ring);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001053 if (!p->tx_ring) {
1054 ipoib_warn(priv, "failed to allocate tx ring\n");
1055 ret = -ENOMEM;
1056 goto err_tx;
1057 }
Roland Dreier10313cb2008-03-12 07:51:03 -07001058 memset(p->tx_ring, 0, ipoib_sendq_size * sizeof *p->tx_ring);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001059
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001060 p->qp = ipoib_cm_create_tx_qp(p->dev, p);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001061 if (IS_ERR(p->qp)) {
1062 ret = PTR_ERR(p->qp);
1063 ipoib_warn(priv, "failed to allocate tx qp: %d\n", ret);
1064 goto err_qp;
1065 }
1066
1067 p->id = ib_create_cm_id(priv->ca, ipoib_cm_tx_handler, p);
1068 if (IS_ERR(p->id)) {
1069 ret = PTR_ERR(p->id);
1070 ipoib_warn(priv, "failed to create tx cm id: %d\n", ret);
1071 goto err_id;
1072 }
1073
1074 ret = ipoib_cm_modify_tx_init(p->dev, p->id, p->qp);
1075 if (ret) {
1076 ipoib_warn(priv, "failed to modify tx qp to rtr: %d\n", ret);
1077 goto err_modify;
1078 }
1079
1080 ret = ipoib_cm_send_req(p->dev, p->id, p->qp, qpn, pathrec);
1081 if (ret) {
1082 ipoib_warn(priv, "failed to send cm req: %d\n", ret);
1083 goto err_send_cm;
1084 }
1085
1086 ipoib_dbg(priv, "Request connection 0x%x for gid " IPOIB_GID_FMT " qpn 0x%x\n",
1087 p->qp->qp_num, IPOIB_GID_ARG(pathrec->dgid), qpn);
1088
1089 return 0;
1090
1091err_send_cm:
1092err_modify:
1093 ib_destroy_cm_id(p->id);
1094err_id:
1095 p->id = NULL;
1096 ib_destroy_qp(p->qp);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001097err_qp:
1098 p->qp = NULL;
Roland Dreier10313cb2008-03-12 07:51:03 -07001099 vfree(p->tx_ring);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001100err_tx:
1101 return ret;
1102}
1103
1104static void ipoib_cm_tx_destroy(struct ipoib_cm_tx *p)
1105{
1106 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
1107 struct ipoib_tx_buf *tx_req;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001108 unsigned long flags;
1109 unsigned long begin;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001110
1111 ipoib_dbg(priv, "Destroy active connection 0x%x head 0x%x tail 0x%x\n",
1112 p->qp ? p->qp->qp_num : 0, p->tx_head, p->tx_tail);
1113
1114 if (p->id)
1115 ib_destroy_cm_id(p->id);
1116
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001117 if (p->tx_ring) {
1118 /* Wait for all sends to complete */
1119 begin = jiffies;
1120 while ((int) p->tx_tail - (int) p->tx_head < 0) {
1121 if (time_after(jiffies, begin + 5 * HZ)) {
1122 ipoib_warn(priv, "timing out; %d sends not completed\n",
1123 p->tx_head - p->tx_tail);
1124 goto timeout;
1125 }
1126
1127 msleep(1);
1128 }
1129 }
1130
1131timeout:
1132
1133 while ((int) p->tx_tail - (int) p->tx_head < 0) {
1134 tx_req = &p->tx_ring[p->tx_tail & (ipoib_sendq_size - 1)];
Eli Cohen71437402008-01-30 18:30:53 +02001135 ib_dma_unmap_single(priv->ca, tx_req->mapping[0], tx_req->skb->len,
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001136 DMA_TO_DEVICE);
1137 dev_kfree_skb_any(tx_req->skb);
1138 ++p->tx_tail;
1139 spin_lock_irqsave(&priv->tx_lock, flags);
1140 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
1141 netif_queue_stopped(p->dev) &&
1142 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
1143 netif_wake_queue(p->dev);
1144 spin_unlock_irqrestore(&priv->tx_lock, flags);
1145 }
1146
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001147 if (p->qp)
1148 ib_destroy_qp(p->qp);
1149
Roland Dreier10313cb2008-03-12 07:51:03 -07001150 vfree(p->tx_ring);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001151 kfree(p);
1152}
1153
1154static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
1155 struct ib_cm_event *event)
1156{
1157 struct ipoib_cm_tx *tx = cm_id->context;
1158 struct ipoib_dev_priv *priv = netdev_priv(tx->dev);
1159 struct net_device *dev = priv->dev;
1160 struct ipoib_neigh *neigh;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001161 int ret;
1162
1163 switch (event->event) {
1164 case IB_CM_DREQ_RECEIVED:
1165 ipoib_dbg(priv, "DREQ received.\n");
1166 ib_send_cm_drep(cm_id, NULL, 0);
1167 break;
1168 case IB_CM_REP_RECEIVED:
1169 ipoib_dbg(priv, "REP received.\n");
1170 ret = ipoib_cm_rep_handler(cm_id, event);
1171 if (ret)
1172 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
1173 NULL, 0, NULL, 0);
1174 break;
1175 case IB_CM_REQ_ERROR:
1176 case IB_CM_REJ_RECEIVED:
1177 case IB_CM_TIMEWAIT_EXIT:
1178 ipoib_dbg(priv, "CM error %d.\n", event->event);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001179 spin_lock_irq(&priv->tx_lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001180 spin_lock(&priv->lock);
1181 neigh = tx->neigh;
1182
1183 if (neigh) {
1184 neigh->cm = NULL;
1185 list_del(&neigh->list);
1186 if (neigh->ah)
1187 ipoib_put_ah(neigh->ah);
1188 ipoib_neigh_free(dev, neigh);
1189
1190 tx->neigh = NULL;
1191 }
1192
1193 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
1194 list_move(&tx->list, &priv->cm.reap_list);
1195 queue_work(ipoib_workqueue, &priv->cm.reap_task);
1196 }
1197
1198 spin_unlock(&priv->lock);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001199 spin_unlock_irq(&priv->tx_lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001200 break;
1201 default:
1202 break;
1203 }
1204
1205 return 0;
1206}
1207
1208struct ipoib_cm_tx *ipoib_cm_create_tx(struct net_device *dev, struct ipoib_path *path,
1209 struct ipoib_neigh *neigh)
1210{
1211 struct ipoib_dev_priv *priv = netdev_priv(dev);
1212 struct ipoib_cm_tx *tx;
1213
1214 tx = kzalloc(sizeof *tx, GFP_ATOMIC);
1215 if (!tx)
1216 return NULL;
1217
1218 neigh->cm = tx;
1219 tx->neigh = neigh;
1220 tx->path = path;
1221 tx->dev = dev;
1222 list_add(&tx->list, &priv->cm.start_list);
1223 set_bit(IPOIB_FLAG_INITIALIZED, &tx->flags);
1224 queue_work(ipoib_workqueue, &priv->cm.start_task);
1225 return tx;
1226}
1227
1228void ipoib_cm_destroy_tx(struct ipoib_cm_tx *tx)
1229{
1230 struct ipoib_dev_priv *priv = netdev_priv(tx->dev);
1231 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
1232 list_move(&tx->list, &priv->cm.reap_list);
1233 queue_work(ipoib_workqueue, &priv->cm.reap_task);
1234 ipoib_dbg(priv, "Reap connection for gid " IPOIB_GID_FMT "\n",
1235 IPOIB_GID_ARG(tx->neigh->dgid));
1236 tx->neigh = NULL;
1237 }
1238}
1239
1240static void ipoib_cm_tx_start(struct work_struct *work)
1241{
1242 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1243 cm.start_task);
1244 struct net_device *dev = priv->dev;
1245 struct ipoib_neigh *neigh;
1246 struct ipoib_cm_tx *p;
1247 unsigned long flags;
1248 int ret;
1249
1250 struct ib_sa_path_rec pathrec;
1251 u32 qpn;
1252
1253 spin_lock_irqsave(&priv->tx_lock, flags);
1254 spin_lock(&priv->lock);
1255 while (!list_empty(&priv->cm.start_list)) {
1256 p = list_entry(priv->cm.start_list.next, typeof(*p), list);
1257 list_del_init(&p->list);
1258 neigh = p->neigh;
1259 qpn = IPOIB_QPN(neigh->neighbour->ha);
1260 memcpy(&pathrec, &p->path->pathrec, sizeof pathrec);
1261 spin_unlock(&priv->lock);
1262 spin_unlock_irqrestore(&priv->tx_lock, flags);
1263 ret = ipoib_cm_tx_init(p, qpn, &pathrec);
1264 spin_lock_irqsave(&priv->tx_lock, flags);
1265 spin_lock(&priv->lock);
1266 if (ret) {
1267 neigh = p->neigh;
1268 if (neigh) {
1269 neigh->cm = NULL;
1270 list_del(&neigh->list);
1271 if (neigh->ah)
1272 ipoib_put_ah(neigh->ah);
1273 ipoib_neigh_free(dev, neigh);
1274 }
1275 list_del(&p->list);
1276 kfree(p);
1277 }
1278 }
1279 spin_unlock(&priv->lock);
1280 spin_unlock_irqrestore(&priv->tx_lock, flags);
1281}
1282
1283static void ipoib_cm_tx_reap(struct work_struct *work)
1284{
1285 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1286 cm.reap_task);
1287 struct ipoib_cm_tx *p;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001288
Roland Dreier37aebbde2007-04-24 21:30:37 -07001289 spin_lock_irq(&priv->tx_lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001290 spin_lock(&priv->lock);
1291 while (!list_empty(&priv->cm.reap_list)) {
1292 p = list_entry(priv->cm.reap_list.next, typeof(*p), list);
1293 list_del(&p->list);
1294 spin_unlock(&priv->lock);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001295 spin_unlock_irq(&priv->tx_lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001296 ipoib_cm_tx_destroy(p);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001297 spin_lock_irq(&priv->tx_lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001298 spin_lock(&priv->lock);
1299 }
1300 spin_unlock(&priv->lock);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001301 spin_unlock_irq(&priv->tx_lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001302}
1303
1304static void ipoib_cm_skb_reap(struct work_struct *work)
1305{
1306 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1307 cm.skb_task);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001308 struct sk_buff *skb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001309
1310 unsigned mtu = priv->mcast_mtu;
1311
Roland Dreier37aebbde2007-04-24 21:30:37 -07001312 spin_lock_irq(&priv->tx_lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001313 spin_lock(&priv->lock);
1314 while ((skb = skb_dequeue(&priv->cm.skb_queue))) {
1315 spin_unlock(&priv->lock);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001316 spin_unlock_irq(&priv->tx_lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001317 if (skb->protocol == htons(ETH_P_IP))
1318 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
1319#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1320 else if (skb->protocol == htons(ETH_P_IPV6))
Roland Dreier20089ca2007-07-10 11:18:34 -07001321 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, priv->dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001322#endif
1323 dev_kfree_skb_any(skb);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001324 spin_lock_irq(&priv->tx_lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001325 spin_lock(&priv->lock);
1326 }
1327 spin_unlock(&priv->lock);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001328 spin_unlock_irq(&priv->tx_lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001329}
1330
Roland Dreier2337f802007-10-23 19:57:54 -07001331void ipoib_cm_skb_too_long(struct net_device *dev, struct sk_buff *skb,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001332 unsigned int mtu)
1333{
1334 struct ipoib_dev_priv *priv = netdev_priv(dev);
1335 int e = skb_queue_empty(&priv->cm.skb_queue);
1336
1337 if (skb->dst)
1338 skb->dst->ops->update_pmtu(skb->dst, mtu);
1339
1340 skb_queue_tail(&priv->cm.skb_queue, skb);
1341 if (e)
1342 queue_work(ipoib_workqueue, &priv->cm.skb_task);
1343}
1344
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001345static void ipoib_cm_rx_reap(struct work_struct *work)
1346{
Roland Dreierefcd9972008-01-25 14:15:24 -08001347 ipoib_cm_free_rx_reap_list(container_of(work, struct ipoib_dev_priv,
1348 cm.rx_reap_task)->dev);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001349}
1350
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001351static void ipoib_cm_stale_task(struct work_struct *work)
1352{
1353 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1354 cm.stale_task.work);
1355 struct ipoib_cm_rx *p;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001356 int ret;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001357
Roland Dreier37aebbde2007-04-24 21:30:37 -07001358 spin_lock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001359 while (!list_empty(&priv->cm.passive_ids)) {
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001360 /* List is sorted by LRU, start from tail,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001361 * stop when we see a recently used entry */
1362 p = list_entry(priv->cm.passive_ids.prev, typeof(*p), list);
Michael S. Tsirkin60a596d2007-03-22 14:32:09 -07001363 if (time_before_eq(jiffies, p->jiffies + IPOIB_CM_RX_TIMEOUT))
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001364 break;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001365 list_move(&p->list, &priv->cm.rx_error_list);
1366 p->state = IPOIB_CM_RX_ERROR;
Roland Dreier37aebbde2007-04-24 21:30:37 -07001367 spin_unlock_irq(&priv->lock);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001368 ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
1369 if (ret)
1370 ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001371 spin_lock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001372 }
Michael S. Tsirkin7c5b9ef2007-05-14 07:26:51 +03001373
1374 if (!list_empty(&priv->cm.passive_ids))
1375 queue_delayed_work(ipoib_workqueue,
1376 &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001377 spin_unlock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001378}
1379
1380
Roland Dreier2337f802007-10-23 19:57:54 -07001381static ssize_t show_mode(struct device *d, struct device_attribute *attr,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001382 char *buf)
1383{
1384 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(d));
1385
1386 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
1387 return sprintf(buf, "connected\n");
1388 else
1389 return sprintf(buf, "datagram\n");
1390}
1391
1392static ssize_t set_mode(struct device *d, struct device_attribute *attr,
1393 const char *buf, size_t count)
1394{
1395 struct net_device *dev = to_net_dev(d);
1396 struct ipoib_dev_priv *priv = netdev_priv(dev);
1397
1398 /* flush paths if we switch modes so that connections are restarted */
1399 if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
1400 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
1401 ipoib_warn(priv, "enabling connected mode "
1402 "will cause multicast packet drops\n");
Eli Cohen60461362008-04-16 21:01:10 -07001403
Eli Cohen40ca1982008-04-16 21:09:27 -07001404 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_TSO);
Eli Cohen60461362008-04-16 21:01:10 -07001405 priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
1406
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001407 ipoib_flush_paths(dev);
1408 return count;
1409 }
1410
1411 if (!strcmp(buf, "datagram\n")) {
1412 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
1413 dev->mtu = min(priv->mcast_mtu, dev->mtu);
1414 ipoib_flush_paths(dev);
Eli Cohen60461362008-04-16 21:01:10 -07001415
Eli Cohen40ca1982008-04-16 21:09:27 -07001416 if (test_bit(IPOIB_FLAG_CSUM, &priv->flags)) {
Eli Cohen60461362008-04-16 21:01:10 -07001417 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
Eli Cohen40ca1982008-04-16 21:09:27 -07001418 if (priv->hca_caps & IB_DEVICE_UD_TSO)
1419 dev->features |= NETIF_F_TSO;
1420 }
Eli Cohen60461362008-04-16 21:01:10 -07001421
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001422 return count;
1423 }
1424
1425 return -EINVAL;
1426}
1427
Roland Dreier551fd612007-02-16 13:57:33 -08001428static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO, show_mode, set_mode);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001429
1430int ipoib_cm_add_mode_attr(struct net_device *dev)
1431{
1432 return device_create_file(&dev->dev, &dev_attr_mode);
1433}
1434
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001435static void ipoib_cm_create_srq(struct net_device *dev, int max_sge)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001436{
1437 struct ipoib_dev_priv *priv = netdev_priv(dev);
1438 struct ib_srq_init_attr srq_init_attr = {
1439 .attr = {
1440 .max_wr = ipoib_recvq_size,
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001441 .max_sge = max_sge
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001442 }
1443 };
Roland Dreier7b3687d2008-01-25 14:15:24 -08001444
1445 priv->cm.srq = ib_create_srq(priv->pd, &srq_init_attr);
1446 if (IS_ERR(priv->cm.srq)) {
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001447 if (PTR_ERR(priv->cm.srq) != -ENOSYS)
1448 printk(KERN_WARNING "%s: failed to allocate SRQ, error %ld\n",
1449 priv->ca->name, PTR_ERR(priv->cm.srq));
Roland Dreier7b3687d2008-01-25 14:15:24 -08001450 priv->cm.srq = NULL;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001451 return;
Roland Dreier7b3687d2008-01-25 14:15:24 -08001452 }
1453
1454 priv->cm.srq_ring = kzalloc(ipoib_recvq_size * sizeof *priv->cm.srq_ring,
1455 GFP_KERNEL);
1456 if (!priv->cm.srq_ring) {
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001457 printk(KERN_WARNING "%s: failed to allocate CM SRQ ring (%d entries)\n",
Roland Dreier7b3687d2008-01-25 14:15:24 -08001458 priv->ca->name, ipoib_recvq_size);
1459 ib_destroy_srq(priv->cm.srq);
1460 priv->cm.srq = NULL;
Roland Dreier7b3687d2008-01-25 14:15:24 -08001461 }
Roland Dreier7b3687d2008-01-25 14:15:24 -08001462}
1463
1464int ipoib_cm_dev_init(struct net_device *dev)
1465{
1466 struct ipoib_dev_priv *priv = netdev_priv(dev);
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001467 int i, ret;
1468 struct ib_device_attr attr;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001469
1470 INIT_LIST_HEAD(&priv->cm.passive_ids);
1471 INIT_LIST_HEAD(&priv->cm.reap_list);
1472 INIT_LIST_HEAD(&priv->cm.start_list);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001473 INIT_LIST_HEAD(&priv->cm.rx_error_list);
1474 INIT_LIST_HEAD(&priv->cm.rx_flush_list);
1475 INIT_LIST_HEAD(&priv->cm.rx_drain_list);
1476 INIT_LIST_HEAD(&priv->cm.rx_reap_list);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001477 INIT_WORK(&priv->cm.start_task, ipoib_cm_tx_start);
1478 INIT_WORK(&priv->cm.reap_task, ipoib_cm_tx_reap);
1479 INIT_WORK(&priv->cm.skb_task, ipoib_cm_skb_reap);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001480 INIT_WORK(&priv->cm.rx_reap_task, ipoib_cm_rx_reap);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001481 INIT_DELAYED_WORK(&priv->cm.stale_task, ipoib_cm_stale_task);
1482
1483 skb_queue_head_init(&priv->cm.skb_queue);
1484
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001485 ret = ib_query_device(priv->ca, &attr);
1486 if (ret) {
1487 printk(KERN_WARNING "ib_query_device() failed with %d\n", ret);
1488 return ret;
1489 }
1490
1491 ipoib_dbg(priv, "max_srq_sge=%d\n", attr.max_srq_sge);
1492
1493 attr.max_srq_sge = min_t(int, IPOIB_CM_RX_SG, attr.max_srq_sge);
1494 ipoib_cm_create_srq(dev, attr.max_srq_sge);
1495 if (ipoib_cm_has_srq(dev)) {
1496 priv->cm.max_cm_mtu = attr.max_srq_sge * PAGE_SIZE - 0x10;
1497 priv->cm.num_frags = attr.max_srq_sge;
1498 ipoib_dbg(priv, "max_cm_mtu = 0x%x, num_frags=%d\n",
1499 priv->cm.max_cm_mtu, priv->cm.num_frags);
1500 } else {
1501 priv->cm.max_cm_mtu = IPOIB_CM_MTU;
1502 priv->cm.num_frags = IPOIB_CM_RX_SG;
1503 }
1504
1505 for (i = 0; i < priv->cm.num_frags; ++i)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001506 priv->cm.rx_sge[i].lkey = priv->mr->lkey;
1507
1508 priv->cm.rx_sge[0].length = IPOIB_CM_HEAD_SIZE;
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001509 for (i = 1; i < priv->cm.num_frags; ++i)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001510 priv->cm.rx_sge[i].length = PAGE_SIZE;
1511 priv->cm.rx_wr.next = NULL;
1512 priv->cm.rx_wr.sg_list = priv->cm.rx_sge;
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001513 priv->cm.rx_wr.num_sge = priv->cm.num_frags;
Roland Dreier7b3687d2008-01-25 14:15:24 -08001514
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001515 if (ipoib_cm_has_srq(dev)) {
1516 for (i = 0; i < ipoib_recvq_size; ++i) {
1517 if (!ipoib_cm_alloc_rx_skb(dev, priv->cm.srq_ring, i,
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001518 priv->cm.num_frags - 1,
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001519 priv->cm.srq_ring[i].mapping)) {
1520 ipoib_warn(priv, "failed to allocate "
1521 "receive buffer %d\n", i);
1522 ipoib_cm_dev_cleanup(dev);
1523 return -ENOMEM;
1524 }
1525
1526 if (ipoib_cm_post_receive_srq(dev, i)) {
1527 ipoib_warn(priv, "ipoib_cm_post_receive_srq "
1528 "failed for buf %d\n", i);
1529 ipoib_cm_dev_cleanup(dev);
1530 return -EIO;
1531 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001532 }
1533 }
1534
1535 priv->dev->dev_addr[0] = IPOIB_FLAGS_RC;
1536 return 0;
1537}
1538
1539void ipoib_cm_dev_cleanup(struct net_device *dev)
1540{
1541 struct ipoib_dev_priv *priv = netdev_priv(dev);
Roland Dreier1efb6142008-01-25 14:15:24 -08001542 int ret;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001543
1544 if (!priv->cm.srq)
1545 return;
1546
1547 ipoib_dbg(priv, "Cleanup ipoib connected mode.\n");
1548
1549 ret = ib_destroy_srq(priv->cm.srq);
1550 if (ret)
1551 ipoib_warn(priv, "ib_destroy_srq failed: %d\n", ret);
1552
1553 priv->cm.srq = NULL;
1554 if (!priv->cm.srq_ring)
1555 return;
Roland Dreier1efb6142008-01-25 14:15:24 -08001556
1557 ipoib_cm_free_rx_ring(dev, priv->cm.srq_ring);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001558 priv->cm.srq_ring = NULL;
1559}