blob: 951d9abcca8b283e652368c7c9c96f1fa25f3df4 [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>
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020034#include <net/dst.h>
35#include <net/icmp.h>
36#include <linux/icmpv6.h>
Michael S. Tsirkin518b1642007-05-21 15:04:59 +030037#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Roland Dreier10313cb2008-03-12 07:51:03 -070039#include <linux/vmalloc.h>
Paul Gortmakerfec14d22011-08-30 12:32:52 -040040#include <linux/moduleparam.h>
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020041
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -080042#include "ipoib.h"
43
44int ipoib_max_conn_qp = 128;
45
46module_param_named(max_nonsrq_conn_qp, ipoib_max_conn_qp, int, 0444);
47MODULE_PARM_DESC(max_nonsrq_conn_qp,
48 "Max number of connected-mode QPs per interface "
49 "(applied only if shared receive queue is not available)");
50
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020051#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
52static int data_debug_level;
53
54module_param_named(cm_data_debug_level, data_debug_level, int, 0644);
55MODULE_PARM_DESC(cm_data_debug_level,
56 "Enable data path debug tracing for connected mode if > 0");
57#endif
58
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020059#define IPOIB_CM_IETF_ID 0x1000000000000000ULL
60
61#define IPOIB_CM_RX_UPDATE_TIME (256 * HZ)
62#define IPOIB_CM_RX_TIMEOUT (2 * 256 * HZ)
63#define IPOIB_CM_RX_DELAY (3 * 256 * HZ)
64#define IPOIB_CM_RX_UPDATE_MASK (0x3)
65
Michael S. Tsirkin518b1642007-05-21 15:04:59 +030066static struct ib_qp_attr ipoib_cm_err_attr = {
67 .qp_state = IB_QPS_ERR
68};
69
Roland Dreier09f60f82007-10-26 13:44:25 -070070#define IPOIB_CM_RX_DRAIN_WRID 0xffffffff
Michael S. Tsirkin518b1642007-05-21 15:04:59 +030071
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +030072static struct ib_send_wr ipoib_cm_rx_drain_wr = {
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +030073 .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)
Dotan Barak787adb92011-10-18 15:22:14 +020087 ib_dma_unmap_page(priv->ca, mapping[i + 1], PAGE_SIZE, DMA_FROM_DEVICE);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020088}
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,
Roland Dreiera7d834c2008-07-14 23:48:47 -0700114 struct ipoib_cm_rx *rx,
115 struct ib_recv_wr *wr,
116 struct ib_sge *sge, int id)
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800117{
118 struct ipoib_dev_priv *priv = netdev_priv(dev);
119 struct ib_recv_wr *bad_wr;
120 int i, ret;
121
Roland Dreiera7d834c2008-07-14 23:48:47 -0700122 wr->wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800123
124 for (i = 0; i < IPOIB_CM_RX_SG; ++i)
Roland Dreiera7d834c2008-07-14 23:48:47 -0700125 sge[i].addr = rx->rx_ring[id].mapping[i];
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800126
Roland Dreiera7d834c2008-07-14 23:48:47 -0700127 ret = ib_post_recv(rx->qp, wr, &bad_wr);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800128 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,
Tal Alon22252b42013-10-16 17:37:48 +0300142 u64 mapping[IPOIB_CM_RX_SG],
143 gfp_t gfp)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200144{
145 struct ipoib_dev_priv *priv = netdev_priv(dev);
146 struct sk_buff *skb;
147 int i;
148
149 skb = dev_alloc_skb(IPOIB_CM_HEAD_SIZE + 12);
150 if (unlikely(!skb))
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200151 return NULL;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200152
153 /*
154 * IPoIB adds a 4 byte header. So we need 12 more bytes to align the
155 * IP header to a multiple of 16.
156 */
157 skb_reserve(skb, 12);
158
159 mapping[0] = ib_dma_map_single(priv->ca, skb->data, IPOIB_CM_HEAD_SIZE,
160 DMA_FROM_DEVICE);
161 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0]))) {
162 dev_kfree_skb_any(skb);
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200163 return NULL;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200164 }
165
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200166 for (i = 0; i < frags; i++) {
Tal Alon22252b42013-10-16 17:37:48 +0300167 struct page *page = alloc_page(gfp);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200168
169 if (!page)
170 goto partial_error;
171 skb_fill_page_desc(skb, i, page, 0, PAGE_SIZE);
172
Ian Campbell5581be32011-08-24 22:28:10 +0000173 mapping[i + 1] = ib_dma_map_page(priv->ca, page,
Michael S. Tsirkin6371ea32007-04-10 18:32:42 +0300174 0, PAGE_SIZE, DMA_FROM_DEVICE);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200175 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[i + 1])))
176 goto partial_error;
177 }
178
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800179 rx_ring[id].skb = skb;
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200180 return skb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200181
182partial_error:
183
184 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
185
Ralph Campbell841adfc2007-06-29 11:37:56 -0700186 for (; i > 0; --i)
Dotan Barak787adb92011-10-18 15:22:14 +0200187 ib_dma_unmap_page(priv->ca, mapping[i], PAGE_SIZE, DMA_FROM_DEVICE);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200188
Michael S. Tsirkin8a2e65f2007-02-16 00:16:13 +0200189 dev_kfree_skb_any(skb);
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200190 return NULL;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200191}
192
Roland Dreier1efb6142008-01-25 14:15:24 -0800193static void ipoib_cm_free_rx_ring(struct net_device *dev,
194 struct ipoib_cm_rx_buf *rx_ring)
195{
196 struct ipoib_dev_priv *priv = netdev_priv(dev);
197 int i;
198
199 for (i = 0; i < ipoib_recvq_size; ++i)
200 if (rx_ring[i].skb) {
201 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
202 rx_ring[i].mapping);
203 dev_kfree_skb_any(rx_ring[i].skb);
204 }
205
David J. Wilderb1404062008-08-08 15:51:29 -0700206 vfree(rx_ring);
Roland Dreier1efb6142008-01-25 14:15:24 -0800207}
208
Roland Dreier2337f802007-10-23 19:57:54 -0700209static void ipoib_cm_start_rx_drain(struct ipoib_dev_priv *priv)
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300210{
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300211 struct ib_send_wr *bad_wr;
212 struct ipoib_cm_rx *p;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300213
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300214 /* We only reserved 1 extra slot in CQ for drain WRs, so
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300215 * make sure we have at most 1 outstanding WR. */
216 if (list_empty(&priv->cm.rx_flush_list) ||
217 !list_empty(&priv->cm.rx_drain_list))
218 return;
219
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300220 /*
221 * QPs on flush list are error state. This way, a "flush
222 * error" WC will be immediately generated for each WR we post.
223 */
224 p = list_entry(priv->cm.rx_flush_list.next, typeof(*p), list);
Christoph Hellwig14d3a3b2015-12-11 11:53:03 -0800225 ipoib_cm_rx_drain_wr.wr_id = IPOIB_CM_RX_DRAIN_WRID;
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300226 if (ib_post_send(p->qp, &ipoib_cm_rx_drain_wr, &bad_wr))
227 ipoib_warn(priv, "failed to post drain wr\n");
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300228
229 list_splice_init(&priv->cm.rx_flush_list, &priv->cm.rx_drain_list);
230}
231
232static void ipoib_cm_rx_event_handler(struct ib_event *event, void *ctx)
233{
234 struct ipoib_cm_rx *p = ctx;
235 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
236 unsigned long flags;
237
238 if (event->event != IB_EVENT_QP_LAST_WQE_REACHED)
239 return;
240
241 spin_lock_irqsave(&priv->lock, flags);
242 list_move(&p->list, &priv->cm.rx_flush_list);
243 p->state = IPOIB_CM_RX_FLUSH;
244 ipoib_cm_start_rx_drain(priv);
245 spin_unlock_irqrestore(&priv->lock, flags);
246}
247
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200248static struct ib_qp *ipoib_cm_create_rx_qp(struct net_device *dev,
249 struct ipoib_cm_rx *p)
250{
251 struct ipoib_dev_priv *priv = netdev_priv(dev);
252 struct ib_qp_init_attr attr = {
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300253 .event_handler = ipoib_cm_rx_event_handler,
Eli Cohenf56bcd82008-04-29 13:46:53 -0700254 .send_cq = priv->recv_cq, /* For drain WR */
255 .recv_cq = priv->recv_cq,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200256 .srq = priv->cm.srq,
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300257 .cap.max_send_wr = 1, /* For drain WR */
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200258 .cap.max_send_sge = 1, /* FIXME: 0 Seems not to work */
259 .sq_sig_type = IB_SIGNAL_ALL_WR,
260 .qp_type = IB_QPT_RC,
261 .qp_context = p,
262 };
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800263
264 if (!ipoib_cm_has_srq(dev)) {
265 attr.cap.max_recv_wr = ipoib_recvq_size;
266 attr.cap.max_recv_sge = IPOIB_CM_RX_SG;
267 }
268
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200269 return ib_create_qp(priv->pd, &attr);
270}
271
272static int ipoib_cm_modify_rx_qp(struct net_device *dev,
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800273 struct ib_cm_id *cm_id, struct ib_qp *qp,
274 unsigned psn)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200275{
276 struct ipoib_dev_priv *priv = netdev_priv(dev);
277 struct ib_qp_attr qp_attr;
278 int qp_attr_mask, ret;
279
280 qp_attr.qp_state = IB_QPS_INIT;
281 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
282 if (ret) {
283 ipoib_warn(priv, "failed to init QP attr for INIT: %d\n", ret);
284 return ret;
285 }
286 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
287 if (ret) {
288 ipoib_warn(priv, "failed to modify QP to INIT: %d\n", ret);
289 return ret;
290 }
291 qp_attr.qp_state = IB_QPS_RTR;
292 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
293 if (ret) {
294 ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
295 return ret;
296 }
297 qp_attr.rq_psn = psn;
298 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
299 if (ret) {
300 ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
301 return ret;
302 }
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300303
304 /*
305 * Current Mellanox HCA firmware won't generate completions
306 * with error for drain WRs unless the QP has been moved to
307 * RTS first. This work-around leaves a window where a QP has
308 * moved to error asynchronously, but this will eventually get
309 * fixed in firmware, so let's not error out if modify QP
310 * fails.
311 */
312 qp_attr.qp_state = IB_QPS_RTS;
313 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
314 if (ret) {
315 ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
316 return 0;
317 }
318 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
319 if (ret) {
320 ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
321 return 0;
322 }
323
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200324 return 0;
325}
326
Roland Dreiera7d834c2008-07-14 23:48:47 -0700327static void ipoib_cm_init_rx_wr(struct net_device *dev,
328 struct ib_recv_wr *wr,
329 struct ib_sge *sge)
330{
331 struct ipoib_dev_priv *priv = netdev_priv(dev);
332 int i;
333
334 for (i = 0; i < priv->cm.num_frags; ++i)
Jason Gunthorpe77b1f992015-07-30 17:22:17 -0600335 sge[i].lkey = priv->pd->local_dma_lkey;
Roland Dreiera7d834c2008-07-14 23:48:47 -0700336
337 sge[0].length = IPOIB_CM_HEAD_SIZE;
338 for (i = 1; i < priv->cm.num_frags; ++i)
339 sge[i].length = PAGE_SIZE;
340
341 wr->next = NULL;
Roland Dreiere0819812008-07-30 07:21:46 -0700342 wr->sg_list = sge;
Roland Dreiera7d834c2008-07-14 23:48:47 -0700343 wr->num_sge = priv->cm.num_frags;
344}
345
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800346static int ipoib_cm_nonsrq_init_rx(struct net_device *dev, struct ib_cm_id *cm_id,
347 struct ipoib_cm_rx *rx)
348{
349 struct ipoib_dev_priv *priv = netdev_priv(dev);
Roland Dreiera7d834c2008-07-14 23:48:47 -0700350 struct {
351 struct ib_recv_wr wr;
352 struct ib_sge sge[IPOIB_CM_RX_SG];
353 } *t;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800354 int ret;
355 int i;
356
Joe Perches948579c2010-11-05 03:07:36 +0000357 rx->rx_ring = vzalloc(ipoib_recvq_size * sizeof *rx->rx_ring);
David J. Wilderb1404062008-08-08 15:51:29 -0700358 if (!rx->rx_ring) {
359 printk(KERN_WARNING "%s: failed to allocate CM non-SRQ ring (%d entries)\n",
360 priv->ca->name, ipoib_recvq_size);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800361 return -ENOMEM;
David J. Wilderb1404062008-08-08 15:51:29 -0700362 }
363
Roland Dreiera7d834c2008-07-14 23:48:47 -0700364 t = kmalloc(sizeof *t, GFP_KERNEL);
365 if (!t) {
366 ret = -ENOMEM;
367 goto err_free;
368 }
369
370 ipoib_cm_init_rx_wr(dev, &t->wr, t->sge);
371
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800372 spin_lock_irq(&priv->lock);
373
374 if (priv->cm.nonsrq_conn_qp >= ipoib_max_conn_qp) {
375 spin_unlock_irq(&priv->lock);
376 ib_send_cm_rej(cm_id, IB_CM_REJ_NO_QP, NULL, 0, NULL, 0);
377 ret = -EINVAL;
378 goto err_free;
379 } else
380 ++priv->cm.nonsrq_conn_qp;
381
382 spin_unlock_irq(&priv->lock);
383
384 for (i = 0; i < ipoib_recvq_size; ++i) {
385 if (!ipoib_cm_alloc_rx_skb(dev, rx->rx_ring, i, IPOIB_CM_RX_SG - 1,
Tal Alon22252b42013-10-16 17:37:48 +0300386 rx->rx_ring[i].mapping,
387 GFP_KERNEL)) {
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800388 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
Bart Van Assche8f71c1a2015-05-05 13:01:39 +0200389 ret = -ENOMEM;
390 goto err_count;
Roland Dreiera7d834c2008-07-14 23:48:47 -0700391 }
392 ret = ipoib_cm_post_receive_nonsrq(dev, rx, &t->wr, t->sge, i);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800393 if (ret) {
394 ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq "
395 "failed for buf %d\n", i);
396 ret = -EIO;
397 goto err_count;
398 }
399 }
400
401 rx->recv_count = ipoib_recvq_size;
402
Roland Dreiera7d834c2008-07-14 23:48:47 -0700403 kfree(t);
404
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800405 return 0;
406
407err_count:
408 spin_lock_irq(&priv->lock);
409 --priv->cm.nonsrq_conn_qp;
410 spin_unlock_irq(&priv->lock);
411
412err_free:
Roland Dreiera7d834c2008-07-14 23:48:47 -0700413 kfree(t);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800414 ipoib_cm_free_rx_ring(dev, rx->rx_ring);
415
416 return ret;
417}
418
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200419static int ipoib_cm_send_rep(struct net_device *dev, struct ib_cm_id *cm_id,
420 struct ib_qp *qp, struct ib_cm_req_event_param *req,
421 unsigned psn)
422{
423 struct ipoib_dev_priv *priv = netdev_priv(dev);
424 struct ipoib_cm_data data = {};
425 struct ib_cm_rep_param rep = {};
426
427 data.qpn = cpu_to_be32(priv->qp->qp_num);
428 data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
429
430 rep.private_data = &data;
431 rep.private_data_len = sizeof data;
432 rep.flow_control = 0;
433 rep.rnr_retry_count = req->rnr_retry_count;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800434 rep.srq = ipoib_cm_has_srq(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200435 rep.qp_num = qp->qp_num;
436 rep.starting_psn = psn;
437 return ib_send_cm_rep(cm_id, &rep);
438}
439
440static int ipoib_cm_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
441{
442 struct net_device *dev = cm_id->context;
443 struct ipoib_dev_priv *priv = netdev_priv(dev);
444 struct ipoib_cm_rx *p;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200445 unsigned psn;
446 int ret;
447
448 ipoib_dbg(priv, "REQ arrived\n");
449 p = kzalloc(sizeof *p, GFP_KERNEL);
450 if (!p)
451 return -ENOMEM;
452 p->dev = dev;
453 p->id = cm_id;
Michael S. Tsirkin3ec73932007-06-19 13:40:41 +0300454 cm_id->context = p;
455 p->state = IPOIB_CM_RX_LIVE;
456 p->jiffies = jiffies;
457 INIT_LIST_HEAD(&p->list);
458
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200459 p->qp = ipoib_cm_create_rx_qp(dev, p);
460 if (IS_ERR(p->qp)) {
461 ret = PTR_ERR(p->qp);
462 goto err_qp;
463 }
464
Andrew Morton50bea5c2013-05-07 16:18:16 -0700465 psn = prandom_u32() & 0xffffff;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200466 ret = ipoib_cm_modify_rx_qp(dev, cm_id, p->qp, psn);
467 if (ret)
468 goto err_modify;
469
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800470 if (!ipoib_cm_has_srq(dev)) {
471 ret = ipoib_cm_nonsrq_init_rx(dev, cm_id, p);
472 if (ret)
473 goto err_modify;
474 }
475
Michael S. Tsirkin3ec73932007-06-19 13:40:41 +0300476 spin_lock_irq(&priv->lock);
Doug Ledford0b395782015-02-21 19:27:03 -0500477 queue_delayed_work(priv->wq,
Michael S. Tsirkin3ec73932007-06-19 13:40:41 +0300478 &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
479 /* Add this entry to passive ids list head, but do not re-add it
480 * if IB_EVENT_QP_LAST_WQE_REACHED has moved it to flush list. */
481 p->jiffies = jiffies;
482 if (p->state == IPOIB_CM_RX_LIVE)
483 list_move(&p->list, &priv->cm.passive_ids);
484 spin_unlock_irq(&priv->lock);
485
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200486 ret = ipoib_cm_send_rep(dev, cm_id, p->qp, &event->param.req_rcvd, psn);
487 if (ret) {
488 ipoib_warn(priv, "failed to send REP: %d\n", ret);
Michael S. Tsirkin3ec73932007-06-19 13:40:41 +0300489 if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE))
490 ipoib_warn(priv, "unable to move qp to error state\n");
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200491 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200492 return 0;
493
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200494err_modify:
495 ib_destroy_qp(p->qp);
496err_qp:
497 kfree(p);
498 return ret;
499}
500
501static int ipoib_cm_rx_handler(struct ib_cm_id *cm_id,
502 struct ib_cm_event *event)
503{
504 struct ipoib_cm_rx *p;
505 struct ipoib_dev_priv *priv;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200506
507 switch (event->event) {
508 case IB_CM_REQ_RECEIVED:
509 return ipoib_cm_req_handler(cm_id, event);
510 case IB_CM_DREQ_RECEIVED:
511 p = cm_id->context;
512 ib_send_cm_drep(cm_id, NULL, 0);
513 /* Fall through */
514 case IB_CM_REJ_RECEIVED:
515 p = cm_id->context;
516 priv = netdev_priv(p->dev);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300517 if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE))
518 ipoib_warn(priv, "unable to move qp to error state\n");
519 /* Fall through */
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200520 default:
521 return 0;
522 }
523}
524/* Adjust length of skb with fragments to match received data */
525static void skb_put_frags(struct sk_buff *skb, unsigned int hdr_space,
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200526 unsigned int length, struct sk_buff *toskb)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200527{
528 int i, num_frags;
529 unsigned int size;
530
531 /* put header into skb */
532 size = min(length, hdr_space);
533 skb->tail += size;
534 skb->len += size;
535 length -= size;
536
537 num_frags = skb_shinfo(skb)->nr_frags;
538 for (i = 0; i < num_frags; i++) {
539 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
540
541 if (length == 0) {
542 /* don't need this page */
Ian Campbell5581be32011-08-24 22:28:10 +0000543 skb_fill_page_desc(toskb, i, skb_frag_page(frag),
544 0, PAGE_SIZE);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200545 --skb_shinfo(skb)->nr_frags;
546 } else {
547 size = min(length, (unsigned) PAGE_SIZE);
548
Eric Dumazet9e903e02011-10-18 21:00:24 +0000549 skb_frag_size_set(frag, size);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200550 skb->data_len += size;
551 skb->truesize += size;
552 skb->len += size;
553 length -= size;
554 }
555 }
556}
557
558void ipoib_cm_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
559{
560 struct ipoib_dev_priv *priv = netdev_priv(dev);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800561 struct ipoib_cm_rx_buf *rx_ring;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300562 unsigned int wr_id = wc->wr_id & ~(IPOIB_OP_CM | IPOIB_OP_RECV);
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200563 struct sk_buff *skb, *newskb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200564 struct ipoib_cm_rx *p;
565 unsigned long flags;
566 u64 mapping[IPOIB_CM_RX_SG];
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200567 int frags;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800568 int has_srq;
Eli Cohenf89271da2008-07-14 23:48:44 -0700569 struct sk_buff *small_skb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200570
Roland Dreiera89875f2007-04-18 20:20:53 -0700571 ipoib_dbg_data(priv, "cm recv completion: id %d, status: %d\n",
572 wr_id, wc->status);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200573
574 if (unlikely(wr_id >= ipoib_recvq_size)) {
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300575 if (wr_id == (IPOIB_CM_RX_DRAIN_WRID & ~(IPOIB_OP_CM | IPOIB_OP_RECV))) {
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300576 spin_lock_irqsave(&priv->lock, flags);
577 list_splice_init(&priv->cm.rx_drain_list, &priv->cm.rx_reap_list);
578 ipoib_cm_start_rx_drain(priv);
Doug Ledford0b395782015-02-21 19:27:03 -0500579 queue_work(priv->wq, &priv->cm.rx_reap_task);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300580 spin_unlock_irqrestore(&priv->lock, flags);
581 } else
582 ipoib_warn(priv, "cm recv completion event with wrid %d (> %d)\n",
583 wr_id, ipoib_recvq_size);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200584 return;
585 }
586
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800587 p = wc->qp->qp_context;
588
589 has_srq = ipoib_cm_has_srq(dev);
590 rx_ring = has_srq ? priv->cm.srq_ring : p->rx_ring;
591
592 skb = rx_ring[wr_id].skb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200593
594 if (unlikely(wc->status != IB_WC_SUCCESS)) {
595 ipoib_dbg(priv, "cm recv error "
596 "(status=%d, wrid=%d vend_err %x)\n",
597 wc->status, wr_id, wc->vendor_err);
Roland Dreierde903512007-09-28 15:33:51 -0700598 ++dev->stats.rx_dropped;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800599 if (has_srq)
600 goto repost;
601 else {
602 if (!--p->recv_count) {
603 spin_lock_irqsave(&priv->lock, flags);
604 list_move(&p->list, &priv->cm.rx_reap_list);
605 spin_unlock_irqrestore(&priv->lock, flags);
Doug Ledford0b395782015-02-21 19:27:03 -0500606 queue_work(priv->wq, &priv->cm.rx_reap_task);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800607 }
608 return;
609 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200610 }
611
Roland Dreierfd312562007-10-17 21:54:44 -0700612 if (unlikely(!(wr_id & IPOIB_CM_RX_UPDATE_MASK))) {
Michael S. Tsirkind6ef7d62007-05-02 15:31:12 +0300613 if (p && time_after_eq(jiffies, p->jiffies + IPOIB_CM_RX_UPDATE_TIME)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200614 spin_lock_irqsave(&priv->lock, flags);
615 p->jiffies = jiffies;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300616 /* Move this entry to list head, but do not re-add it
617 * if it has been moved out of list. */
618 if (p->state == IPOIB_CM_RX_LIVE)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200619 list_move(&p->list, &priv->cm.passive_ids);
620 spin_unlock_irqrestore(&priv->lock, flags);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200621 }
622 }
623
Eli Cohenf89271da2008-07-14 23:48:44 -0700624 if (wc->byte_len < IPOIB_CM_COPYBREAK) {
625 int dlen = wc->byte_len;
626
627 small_skb = dev_alloc_skb(dlen + 12);
628 if (small_skb) {
629 skb_reserve(small_skb, 12);
630 ib_dma_sync_single_for_cpu(priv->ca, rx_ring[wr_id].mapping[0],
631 dlen, DMA_FROM_DEVICE);
632 skb_copy_from_linear_data(skb, small_skb->data, dlen);
633 ib_dma_sync_single_for_device(priv->ca, rx_ring[wr_id].mapping[0],
634 dlen, DMA_FROM_DEVICE);
635 skb_put(small_skb, dlen);
636 skb = small_skb;
637 goto copied;
638 }
639 }
640
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200641 frags = PAGE_ALIGN(wc->byte_len - min(wc->byte_len,
642 (unsigned)IPOIB_CM_HEAD_SIZE)) / PAGE_SIZE;
643
Tal Alon22252b42013-10-16 17:37:48 +0300644 newskb = ipoib_cm_alloc_rx_skb(dev, rx_ring, wr_id, frags,
645 mapping, GFP_ATOMIC);
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200646 if (unlikely(!newskb)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200647 /*
648 * If we can't allocate a new RX buffer, dump
649 * this packet and reuse the old buffer.
650 */
651 ipoib_dbg(priv, "failed to allocate receive buffer %d\n", wr_id);
Roland Dreierde903512007-09-28 15:33:51 -0700652 ++dev->stats.rx_dropped;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200653 goto repost;
654 }
655
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800656 ipoib_cm_dma_unmap_rx(priv, frags, rx_ring[wr_id].mapping);
657 memcpy(rx_ring[wr_id].mapping, mapping, (frags + 1) * sizeof *mapping);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200658
659 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
660 wc->byte_len, wc->slid);
661
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200662 skb_put_frags(skb, IPOIB_CM_HEAD_SIZE, wc->byte_len, newskb);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200663
Eli Cohenf89271da2008-07-14 23:48:44 -0700664copied:
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200665 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700666 skb_reset_mac_header(skb);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200667 skb_pull(skb, IPOIB_ENCAP_LEN);
668
Roland Dreierde903512007-09-28 15:33:51 -0700669 ++dev->stats.rx_packets;
670 dev->stats.rx_bytes += skb->len;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200671
672 skb->dev = dev;
673 /* XXX get correct PACKET_ type here */
674 skb->pkt_type = PACKET_HOST;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700675 netif_receive_skb(skb);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200676
677repost:
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800678 if (has_srq) {
679 if (unlikely(ipoib_cm_post_receive_srq(dev, wr_id)))
680 ipoib_warn(priv, "ipoib_cm_post_receive_srq failed "
681 "for buf %d\n", wr_id);
682 } else {
Roland Dreiera7d834c2008-07-14 23:48:47 -0700683 if (unlikely(ipoib_cm_post_receive_nonsrq(dev, p,
684 &priv->cm.rx_wr,
685 priv->cm.rx_sge,
686 wr_id))) {
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800687 --p->recv_count;
688 ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq failed "
689 "for buf %d\n", wr_id);
690 }
691 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200692}
693
694static inline int post_send(struct ipoib_dev_priv *priv,
695 struct ipoib_cm_tx *tx,
696 unsigned int wr_id,
Yuval Shaiac4268772015-07-12 01:24:09 -0700697 struct ipoib_tx_buf *tx_req)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200698{
699 struct ib_send_wr *bad_wr;
700
Yuval Shaiac4268772015-07-12 01:24:09 -0700701 ipoib_build_sge(priv, tx_req);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200702
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100703 priv->tx_wr.wr.wr_id = wr_id | IPOIB_OP_CM;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200704
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100705 return ib_post_send(tx->qp, &priv->tx_wr.wr, &bad_wr);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200706}
707
708void ipoib_cm_send(struct net_device *dev, struct sk_buff *skb, struct ipoib_cm_tx *tx)
709{
710 struct ipoib_dev_priv *priv = netdev_priv(dev);
Yuval Shaiac4268772015-07-12 01:24:09 -0700711 struct ipoib_tx_buf *tx_req;
Or Gerlitza48f5092010-03-04 13:17:37 +0000712 int rc;
Hans Westgaard Ry78a50a52016-03-02 13:44:28 +0100713 unsigned usable_sge = tx->max_send_sge - !!skb_headlen(skb);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200714
715 if (unlikely(skb->len > tx->mtu)) {
716 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
717 skb->len, tx->mtu);
Roland Dreierde903512007-09-28 15:33:51 -0700718 ++dev->stats.tx_dropped;
719 ++dev->stats.tx_errors;
Michael S. Tsirkin77d8e1e2007-03-21 15:45:05 +0200720 ipoib_cm_skb_too_long(dev, skb, tx->mtu - IPOIB_ENCAP_LEN);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200721 return;
722 }
Hans Westgaard Ry78a50a52016-03-02 13:44:28 +0100723 if (skb_shinfo(skb)->nr_frags > usable_sge) {
724 if (skb_linearize(skb) < 0) {
725 ipoib_warn(priv, "skb could not be linearized\n");
726 ++dev->stats.tx_dropped;
727 ++dev->stats.tx_errors;
728 dev_kfree_skb_any(skb);
729 return;
730 }
731 /* Does skb_linearize return ok without reducing nr_frags? */
732 if (skb_shinfo(skb)->nr_frags > usable_sge) {
733 ipoib_warn(priv, "too many frags after skb linearize\n");
734 ++dev->stats.tx_dropped;
735 ++dev->stats.tx_errors;
736 dev_kfree_skb_any(skb);
737 return;
738 }
739 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200740 ipoib_dbg_data(priv, "sending packet: head 0x%x length %d connection 0x%x\n",
741 tx->tx_head, skb->len, tx->qp->qp_num);
742
743 /*
744 * We put the skb into the tx_ring _before_ we call post_send()
745 * because it's entirely possible that the completion handler will
746 * run before we execute anything after the post_send(). That
747 * means we have to make sure everything is properly recorded and
748 * our state is consistent before we call post_send().
749 */
750 tx_req = &tx->tx_ring[tx->tx_head & (ipoib_sendq_size - 1)];
751 tx_req->skb = skb;
Yuval Shaiac4268772015-07-12 01:24:09 -0700752
753 if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) {
Roland Dreierde903512007-09-28 15:33:51 -0700754 ++dev->stats.tx_errors;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200755 dev_kfree_skb_any(skb);
756 return;
757 }
758
Shlomo Pongratz7e5a90c2013-02-04 15:29:10 +0000759 skb_orphan(skb);
760 skb_dst_drop(skb);
761
Yuval Shaiac4268772015-07-12 01:24:09 -0700762 rc = post_send(priv, tx, tx->tx_head & (ipoib_sendq_size - 1), tx_req);
Or Gerlitza48f5092010-03-04 13:17:37 +0000763 if (unlikely(rc)) {
764 ipoib_warn(priv, "post_send failed, error %d\n", rc);
Roland Dreierde903512007-09-28 15:33:51 -0700765 ++dev->stats.tx_errors;
Yuval Shaiac4268772015-07-12 01:24:09 -0700766 ipoib_dma_unmap_tx(priv, tx_req);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200767 dev_kfree_skb_any(skb);
768 } else {
Florian Westphal860e9532016-05-03 16:33:13 +0200769 netif_trans_update(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200770 ++tx->tx_head;
771
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300772 if (++priv->tx_outstanding == ipoib_sendq_size) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200773 ipoib_dbg(priv, "TX ring 0x%x full, stopping kernel net queue\n",
774 tx->qp->qp_num);
775 netif_stop_queue(dev);
Mike Marciniszyn1ee9e2a2013-02-26 15:46:27 +0000776 rc = ib_req_notify_cq(priv->send_cq,
777 IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS);
778 if (rc < 0)
779 ipoib_warn(priv, "request notify on send CQ failed\n");
780 else if (rc)
781 ipoib_send_comp_handler(priv->send_cq, dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200782 }
783 }
784}
785
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300786void ipoib_cm_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200787{
788 struct ipoib_dev_priv *priv = netdev_priv(dev);
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300789 struct ipoib_cm_tx *tx = wc->qp->qp_context;
790 unsigned int wr_id = wc->wr_id & ~IPOIB_OP_CM;
Yuval Shaiac4268772015-07-12 01:24:09 -0700791 struct ipoib_tx_buf *tx_req;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200792 unsigned long flags;
793
Roland Dreiera89875f2007-04-18 20:20:53 -0700794 ipoib_dbg_data(priv, "cm send completion: id %d, status: %d\n",
795 wr_id, wc->status);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200796
797 if (unlikely(wr_id >= ipoib_sendq_size)) {
798 ipoib_warn(priv, "cm send completion event with wrid %d (> %d)\n",
799 wr_id, ipoib_sendq_size);
800 return;
801 }
802
803 tx_req = &tx->tx_ring[wr_id];
804
Yuval Shaiac4268772015-07-12 01:24:09 -0700805 ipoib_dma_unmap_tx(priv, tx_req);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200806
807 /* FIXME: is this right? Shouldn't we only increment on success? */
Roland Dreierde903512007-09-28 15:33:51 -0700808 ++dev->stats.tx_packets;
809 dev->stats.tx_bytes += tx_req->skb->len;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200810
811 dev_kfree_skb_any(tx_req->skb);
812
Roland Dreier943c2462008-09-30 10:36:21 -0700813 netif_tx_lock(dev);
814
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200815 ++tx->tx_tail;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300816 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
817 netif_queue_stopped(dev) &&
818 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200819 netif_wake_queue(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200820
821 if (wc->status != IB_WC_SUCCESS &&
822 wc->status != IB_WC_WR_FLUSH_ERR) {
823 struct ipoib_neigh *neigh;
824
825 ipoib_dbg(priv, "failed cm send event "
826 "(status=%d, wrid=%d vend_err %x)\n",
827 wc->status, wr_id, wc->vendor_err);
828
Roland Dreier943c2462008-09-30 10:36:21 -0700829 spin_lock_irqsave(&priv->lock, flags);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200830 neigh = tx->neigh;
831
832 if (neigh) {
833 neigh->cm = NULL;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000834 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200835
836 tx->neigh = NULL;
837 }
838
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200839 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
840 list_move(&tx->list, &priv->cm.reap_list);
Doug Ledford0b395782015-02-21 19:27:03 -0500841 queue_work(priv->wq, &priv->cm.reap_task);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200842 }
843
844 clear_bit(IPOIB_FLAG_OPER_UP, &tx->flags);
845
Roland Dreier943c2462008-09-30 10:36:21 -0700846 spin_unlock_irqrestore(&priv->lock, flags);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200847 }
848
Roland Dreier943c2462008-09-30 10:36:21 -0700849 netif_tx_unlock(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200850}
851
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200852int ipoib_cm_dev_open(struct net_device *dev)
853{
854 struct ipoib_dev_priv *priv = netdev_priv(dev);
855 int ret;
856
857 if (!IPOIB_CM_SUPPORTED(dev->dev_addr))
858 return 0;
859
860 priv->cm.id = ib_create_cm_id(priv->ca, ipoib_cm_rx_handler, dev);
861 if (IS_ERR(priv->cm.id)) {
862 printk(KERN_WARNING "%s: failed to create CM ID\n", priv->ca->name);
Michael S. Tsirkin347fcfb2007-04-30 17:30:28 -0700863 ret = PTR_ERR(priv->cm.id);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300864 goto err_cm;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200865 }
866
867 ret = ib_cm_listen(priv->cm.id, cpu_to_be64(IPOIB_CM_IETF_ID | priv->qp->qp_num),
Haggai Eran73fec7f2015-07-30 17:50:26 +0300868 0);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200869 if (ret) {
870 printk(KERN_WARNING "%s: failed to listen on ID 0x%llx\n", priv->ca->name,
871 IPOIB_CM_IETF_ID | priv->qp->qp_num);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300872 goto err_listen;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200873 }
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300874
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200875 return 0;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300876
877err_listen:
878 ib_destroy_cm_id(priv->cm.id);
879err_cm:
880 priv->cm.id = NULL;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300881 return ret;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200882}
883
Roland Dreierefcd9972008-01-25 14:15:24 -0800884static void ipoib_cm_free_rx_reap_list(struct net_device *dev)
885{
886 struct ipoib_dev_priv *priv = netdev_priv(dev);
887 struct ipoib_cm_rx *rx, *n;
888 LIST_HEAD(list);
889
890 spin_lock_irq(&priv->lock);
891 list_splice_init(&priv->cm.rx_reap_list, &list);
892 spin_unlock_irq(&priv->lock);
893
894 list_for_each_entry_safe(rx, n, &list, list) {
895 ib_destroy_cm_id(rx->id);
896 ib_destroy_qp(rx->qp);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800897 if (!ipoib_cm_has_srq(dev)) {
898 ipoib_cm_free_rx_ring(priv->dev, rx->rx_ring);
899 spin_lock_irq(&priv->lock);
900 --priv->cm.nonsrq_conn_qp;
901 spin_unlock_irq(&priv->lock);
902 }
Roland Dreierefcd9972008-01-25 14:15:24 -0800903 kfree(rx);
904 }
905}
906
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200907void ipoib_cm_dev_stop(struct net_device *dev)
908{
909 struct ipoib_dev_priv *priv = netdev_priv(dev);
Roland Dreierefcd9972008-01-25 14:15:24 -0800910 struct ipoib_cm_rx *p;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300911 unsigned long begin;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300912 int ret;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200913
Michael S. Tsirkin347fcfb2007-04-30 17:30:28 -0700914 if (!IPOIB_CM_SUPPORTED(dev->dev_addr) || !priv->cm.id)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200915 return;
916
917 ib_destroy_cm_id(priv->cm.id);
Michael S. Tsirkin347fcfb2007-04-30 17:30:28 -0700918 priv->cm.id = NULL;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300919
Roland Dreier37aebbde2007-04-24 21:30:37 -0700920 spin_lock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200921 while (!list_empty(&priv->cm.passive_ids)) {
922 p = list_entry(priv->cm.passive_ids.next, typeof(*p), list);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300923 list_move(&p->list, &priv->cm.rx_error_list);
924 p->state = IPOIB_CM_RX_ERROR;
Roland Dreier37aebbde2007-04-24 21:30:37 -0700925 spin_unlock_irq(&priv->lock);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300926 ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
927 if (ret)
928 ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
929 spin_lock_irq(&priv->lock);
930 }
931
932 /* Wait for all RX to be drained */
933 begin = jiffies;
934
935 while (!list_empty(&priv->cm.rx_error_list) ||
936 !list_empty(&priv->cm.rx_flush_list) ||
937 !list_empty(&priv->cm.rx_drain_list)) {
Michael S. Tsirkin8fd357a2007-05-24 14:02:39 -0700938 if (time_after(jiffies, begin + 5 * HZ)) {
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300939 ipoib_warn(priv, "RX drain timing out\n");
940
941 /*
942 * assume the HW is wedged and just free up everything.
943 */
Pradeep Satyanarayanaec229e52008-02-12 15:00:59 -0800944 list_splice_init(&priv->cm.rx_flush_list,
945 &priv->cm.rx_reap_list);
946 list_splice_init(&priv->cm.rx_error_list,
947 &priv->cm.rx_reap_list);
948 list_splice_init(&priv->cm.rx_drain_list,
949 &priv->cm.rx_reap_list);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300950 break;
951 }
952 spin_unlock_irq(&priv->lock);
953 msleep(1);
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300954 ipoib_drain_cq(dev);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300955 spin_lock_irq(&priv->lock);
956 }
957
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300958 spin_unlock_irq(&priv->lock);
959
Roland Dreierefcd9972008-01-25 14:15:24 -0800960 ipoib_cm_free_rx_reap_list(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200961
962 cancel_delayed_work(&priv->cm.stale_task);
963}
964
965static int ipoib_cm_rep_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
966{
967 struct ipoib_cm_tx *p = cm_id->context;
968 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
969 struct ipoib_cm_data *data = event->private_data;
970 struct sk_buff_head skqueue;
971 struct ib_qp_attr qp_attr;
972 int qp_attr_mask, ret;
973 struct sk_buff *skb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200974
975 p->mtu = be32_to_cpu(data->mtu);
976
Michael S. Tsirkin82c3aca2007-06-20 19:22:15 +0300977 if (p->mtu <= IPOIB_ENCAP_LEN) {
978 ipoib_warn(priv, "Rejecting connection: mtu %d <= %d\n",
979 p->mtu, IPOIB_ENCAP_LEN);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200980 return -EINVAL;
981 }
982
983 qp_attr.qp_state = IB_QPS_RTR;
984 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
985 if (ret) {
986 ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
987 return ret;
988 }
989
990 qp_attr.rq_psn = 0 /* FIXME */;
991 ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
992 if (ret) {
993 ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
994 return ret;
995 }
996
997 qp_attr.qp_state = IB_QPS_RTS;
998 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
999 if (ret) {
1000 ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
1001 return ret;
1002 }
1003 ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
1004 if (ret) {
1005 ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
1006 return ret;
1007 }
1008
1009 skb_queue_head_init(&skqueue);
1010
Roland Dreier37aebbde2007-04-24 21:30:37 -07001011 spin_lock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001012 set_bit(IPOIB_FLAG_OPER_UP, &p->flags);
1013 if (p->neigh)
1014 while ((skb = __skb_dequeue(&p->neigh->queue)))
1015 __skb_queue_tail(&skqueue, skb);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001016 spin_unlock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001017
1018 while ((skb = __skb_dequeue(&skqueue))) {
1019 skb->dev = p->dev;
1020 if (dev_queue_xmit(skb))
1021 ipoib_warn(priv, "dev_queue_xmit failed "
1022 "to requeue packet\n");
1023 }
1024
1025 ret = ib_send_cm_rtu(cm_id, NULL, 0);
1026 if (ret) {
1027 ipoib_warn(priv, "failed to send RTU: %d\n", ret);
1028 return ret;
1029 }
1030 return 0;
1031}
1032
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001033static 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 +02001034{
1035 struct ipoib_dev_priv *priv = netdev_priv(dev);
Dotan Barakede6bc02007-10-07 09:30:48 +02001036 struct ib_qp_init_attr attr = {
Eli Cohenf56bcd82008-04-29 13:46:53 -07001037 .send_cq = priv->recv_cq,
1038 .recv_cq = priv->recv_cq,
Dotan Barakede6bc02007-10-07 09:30:48 +02001039 .srq = priv->cm.srq,
1040 .cap.max_send_wr = ipoib_sendq_size,
1041 .cap.max_send_sge = 1,
1042 .sq_sig_type = IB_SIGNAL_ALL_WR,
1043 .qp_type = IB_QPT_RC,
Or Gerlitz09b93082014-05-11 15:15:11 +03001044 .qp_context = tx,
1045 .create_flags = IB_QP_CREATE_USE_GFP_NOIO
Roland Dreier2337f802007-10-23 19:57:54 -07001046 };
Dotan Barakede6bc02007-10-07 09:30:48 +02001047
Or Gerlitz09b93082014-05-11 15:15:11 +03001048 struct ib_qp *tx_qp;
1049
Yuval Shaiac4268772015-07-12 01:24:09 -07001050 if (dev->features & NETIF_F_SG)
Hans Westgaard Ry78a50a52016-03-02 13:44:28 +01001051 attr.cap.max_send_sge =
1052 min_t(u32, priv->ca->attrs.max_sge, MAX_SKB_FRAGS + 1);
Yuval Shaiac4268772015-07-12 01:24:09 -07001053
Or Gerlitz09b93082014-05-11 15:15:11 +03001054 tx_qp = ib_create_qp(priv->pd, &attr);
1055 if (PTR_ERR(tx_qp) == -EINVAL) {
1056 ipoib_warn(priv, "can't use GFP_NOIO for QPs on device %s, using GFP_KERNEL\n",
1057 priv->ca->name);
1058 attr.create_flags &= ~IB_QP_CREATE_USE_GFP_NOIO;
1059 tx_qp = ib_create_qp(priv->pd, &attr);
1060 }
Hans Westgaard Ry78a50a52016-03-02 13:44:28 +01001061 tx->max_send_sge = attr.cap.max_send_sge;
Or Gerlitz09b93082014-05-11 15:15:11 +03001062 return tx_qp;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001063}
1064
1065static int ipoib_cm_send_req(struct net_device *dev,
1066 struct ib_cm_id *id, struct ib_qp *qp,
1067 u32 qpn,
1068 struct ib_sa_path_rec *pathrec)
1069{
1070 struct ipoib_dev_priv *priv = netdev_priv(dev);
1071 struct ipoib_cm_data data = {};
1072 struct ib_cm_req_param req = {};
1073
1074 data.qpn = cpu_to_be32(priv->qp->qp_num);
1075 data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
1076
Roland Dreier2337f802007-10-23 19:57:54 -07001077 req.primary_path = pathrec;
1078 req.alternate_path = NULL;
1079 req.service_id = cpu_to_be64(IPOIB_CM_IETF_ID | qpn);
1080 req.qp_num = qp->qp_num;
1081 req.qp_type = qp->qp_type;
1082 req.private_data = &data;
1083 req.private_data_len = sizeof data;
1084 req.flow_control = 0;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001085
Roland Dreier2337f802007-10-23 19:57:54 -07001086 req.starting_psn = 0; /* FIXME */
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001087
1088 /*
1089 * Pick some arbitrary defaults here; we could make these
1090 * module parameters if anyone cared about setting them.
1091 */
Roland Dreier2337f802007-10-23 19:57:54 -07001092 req.responder_resources = 4;
1093 req.remote_cm_response_timeout = 20;
1094 req.local_cm_response_timeout = 20;
1095 req.retry_count = 0; /* RFC draft warns against retries */
1096 req.rnr_retry_count = 0; /* RFC draft warns against retries */
1097 req.max_cm_retries = 15;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001098 req.srq = ipoib_cm_has_srq(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001099 return ib_send_cm_req(id, &req);
1100}
1101
1102static int ipoib_cm_modify_tx_init(struct net_device *dev,
1103 struct ib_cm_id *cm_id, struct ib_qp *qp)
1104{
1105 struct ipoib_dev_priv *priv = netdev_priv(dev);
1106 struct ib_qp_attr qp_attr;
1107 int qp_attr_mask, ret;
Roland Dreier9fdd5e52008-04-16 21:09:35 -07001108 ret = ib_find_pkey(priv->ca, priv->port, priv->pkey, &qp_attr.pkey_index);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001109 if (ret) {
Roland Dreier9fdd5e52008-04-16 21:09:35 -07001110 ipoib_warn(priv, "pkey 0x%x not found: %d\n", priv->pkey, ret);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001111 return ret;
1112 }
1113
1114 qp_attr.qp_state = IB_QPS_INIT;
1115 qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE;
1116 qp_attr.port_num = priv->port;
1117 qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
1118
1119 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
1120 if (ret) {
1121 ipoib_warn(priv, "failed to modify tx QP to INIT: %d\n", ret);
1122 return ret;
1123 }
1124 return 0;
1125}
1126
1127static int ipoib_cm_tx_init(struct ipoib_cm_tx *p, u32 qpn,
1128 struct ib_sa_path_rec *pathrec)
1129{
1130 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
1131 int ret;
1132
Or Gerlitz09b93082014-05-11 15:15:11 +03001133 p->tx_ring = __vmalloc(ipoib_sendq_size * sizeof *p->tx_ring,
1134 GFP_NOIO, PAGE_KERNEL);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001135 if (!p->tx_ring) {
1136 ipoib_warn(priv, "failed to allocate tx ring\n");
1137 ret = -ENOMEM;
1138 goto err_tx;
1139 }
Or Gerlitz09b93082014-05-11 15:15:11 +03001140 memset(p->tx_ring, 0, ipoib_sendq_size * sizeof *p->tx_ring);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001141
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001142 p->qp = ipoib_cm_create_tx_qp(p->dev, p);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001143 if (IS_ERR(p->qp)) {
1144 ret = PTR_ERR(p->qp);
1145 ipoib_warn(priv, "failed to allocate tx qp: %d\n", ret);
1146 goto err_qp;
1147 }
1148
1149 p->id = ib_create_cm_id(priv->ca, ipoib_cm_tx_handler, p);
1150 if (IS_ERR(p->id)) {
1151 ret = PTR_ERR(p->id);
1152 ipoib_warn(priv, "failed to create tx cm id: %d\n", ret);
1153 goto err_id;
1154 }
1155
1156 ret = ipoib_cm_modify_tx_init(p->dev, p->id, p->qp);
1157 if (ret) {
1158 ipoib_warn(priv, "failed to modify tx qp to rtr: %d\n", ret);
1159 goto err_modify;
1160 }
1161
1162 ret = ipoib_cm_send_req(p->dev, p->id, p->qp, qpn, pathrec);
1163 if (ret) {
1164 ipoib_warn(priv, "failed to send cm req: %d\n", ret);
1165 goto err_send_cm;
1166 }
1167
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001168 ipoib_dbg(priv, "Request connection 0x%x for gid %pI6 qpn 0x%x\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -07001169 p->qp->qp_num, pathrec->dgid.raw, qpn);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001170
1171 return 0;
1172
1173err_send_cm:
1174err_modify:
1175 ib_destroy_cm_id(p->id);
1176err_id:
1177 p->id = NULL;
1178 ib_destroy_qp(p->qp);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001179err_qp:
1180 p->qp = NULL;
Roland Dreier10313cb2008-03-12 07:51:03 -07001181 vfree(p->tx_ring);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001182err_tx:
1183 return ret;
1184}
1185
1186static void ipoib_cm_tx_destroy(struct ipoib_cm_tx *p)
1187{
1188 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
Yuval Shaiac4268772015-07-12 01:24:09 -07001189 struct ipoib_tx_buf *tx_req;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001190 unsigned long begin;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001191
1192 ipoib_dbg(priv, "Destroy active connection 0x%x head 0x%x tail 0x%x\n",
1193 p->qp ? p->qp->qp_num : 0, p->tx_head, p->tx_tail);
1194
1195 if (p->id)
1196 ib_destroy_cm_id(p->id);
1197
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001198 if (p->tx_ring) {
1199 /* Wait for all sends to complete */
1200 begin = jiffies;
1201 while ((int) p->tx_tail - (int) p->tx_head < 0) {
1202 if (time_after(jiffies, begin + 5 * HZ)) {
1203 ipoib_warn(priv, "timing out; %d sends not completed\n",
1204 p->tx_head - p->tx_tail);
1205 goto timeout;
1206 }
1207
1208 msleep(1);
1209 }
1210 }
1211
1212timeout:
1213
1214 while ((int) p->tx_tail - (int) p->tx_head < 0) {
1215 tx_req = &p->tx_ring[p->tx_tail & (ipoib_sendq_size - 1)];
Yuval Shaiac4268772015-07-12 01:24:09 -07001216 ipoib_dma_unmap_tx(priv, tx_req);
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001217 dev_kfree_skb_any(tx_req->skb);
1218 ++p->tx_tail;
Roland Dreier943c2462008-09-30 10:36:21 -07001219 netif_tx_lock_bh(p->dev);
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001220 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
1221 netif_queue_stopped(p->dev) &&
1222 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
1223 netif_wake_queue(p->dev);
Roland Dreier943c2462008-09-30 10:36:21 -07001224 netif_tx_unlock_bh(p->dev);
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001225 }
1226
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001227 if (p->qp)
1228 ib_destroy_qp(p->qp);
1229
Roland Dreier10313cb2008-03-12 07:51:03 -07001230 vfree(p->tx_ring);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001231 kfree(p);
1232}
1233
1234static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
1235 struct ib_cm_event *event)
1236{
1237 struct ipoib_cm_tx *tx = cm_id->context;
1238 struct ipoib_dev_priv *priv = netdev_priv(tx->dev);
1239 struct net_device *dev = priv->dev;
1240 struct ipoib_neigh *neigh;
Roland Dreier943c2462008-09-30 10:36:21 -07001241 unsigned long flags;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001242 int ret;
1243
1244 switch (event->event) {
1245 case IB_CM_DREQ_RECEIVED:
1246 ipoib_dbg(priv, "DREQ received.\n");
1247 ib_send_cm_drep(cm_id, NULL, 0);
1248 break;
1249 case IB_CM_REP_RECEIVED:
1250 ipoib_dbg(priv, "REP received.\n");
1251 ret = ipoib_cm_rep_handler(cm_id, event);
1252 if (ret)
1253 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
1254 NULL, 0, NULL, 0);
1255 break;
1256 case IB_CM_REQ_ERROR:
1257 case IB_CM_REJ_RECEIVED:
1258 case IB_CM_TIMEWAIT_EXIT:
1259 ipoib_dbg(priv, "CM error %d.\n", event->event);
Roland Dreier943c2462008-09-30 10:36:21 -07001260 netif_tx_lock_bh(dev);
1261 spin_lock_irqsave(&priv->lock, flags);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001262 neigh = tx->neigh;
1263
1264 if (neigh) {
1265 neigh->cm = NULL;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001266 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001267
1268 tx->neigh = NULL;
1269 }
1270
1271 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
1272 list_move(&tx->list, &priv->cm.reap_list);
Doug Ledford0b395782015-02-21 19:27:03 -05001273 queue_work(priv->wq, &priv->cm.reap_task);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001274 }
1275
Roland Dreier943c2462008-09-30 10:36:21 -07001276 spin_unlock_irqrestore(&priv->lock, flags);
1277 netif_tx_unlock_bh(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001278 break;
1279 default:
1280 break;
1281 }
1282
1283 return 0;
1284}
1285
1286struct ipoib_cm_tx *ipoib_cm_create_tx(struct net_device *dev, struct ipoib_path *path,
1287 struct ipoib_neigh *neigh)
1288{
1289 struct ipoib_dev_priv *priv = netdev_priv(dev);
1290 struct ipoib_cm_tx *tx;
1291
1292 tx = kzalloc(sizeof *tx, GFP_ATOMIC);
1293 if (!tx)
1294 return NULL;
1295
1296 neigh->cm = tx;
1297 tx->neigh = neigh;
1298 tx->path = path;
1299 tx->dev = dev;
1300 list_add(&tx->list, &priv->cm.start_list);
1301 set_bit(IPOIB_FLAG_INITIALIZED, &tx->flags);
Doug Ledford0b395782015-02-21 19:27:03 -05001302 queue_work(priv->wq, &priv->cm.start_task);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001303 return tx;
1304}
1305
1306void ipoib_cm_destroy_tx(struct ipoib_cm_tx *tx)
1307{
1308 struct ipoib_dev_priv *priv = netdev_priv(tx->dev);
Shlomo Pongratzfa16ebe2012-08-13 14:39:49 +00001309 unsigned long flags;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001310 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
Shlomo Pongratzfa16ebe2012-08-13 14:39:49 +00001311 spin_lock_irqsave(&priv->lock, flags);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001312 list_move(&tx->list, &priv->cm.reap_list);
Doug Ledford0b395782015-02-21 19:27:03 -05001313 queue_work(priv->wq, &priv->cm.reap_task);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001314 ipoib_dbg(priv, "Reap connection for gid %pI6\n",
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001315 tx->neigh->daddr + 4);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001316 tx->neigh = NULL;
Shlomo Pongratzfa16ebe2012-08-13 14:39:49 +00001317 spin_unlock_irqrestore(&priv->lock, flags);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001318 }
1319}
1320
1321static void ipoib_cm_tx_start(struct work_struct *work)
1322{
1323 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1324 cm.start_task);
1325 struct net_device *dev = priv->dev;
1326 struct ipoib_neigh *neigh;
1327 struct ipoib_cm_tx *p;
1328 unsigned long flags;
1329 int ret;
1330
1331 struct ib_sa_path_rec pathrec;
1332 u32 qpn;
1333
Roland Dreier943c2462008-09-30 10:36:21 -07001334 netif_tx_lock_bh(dev);
1335 spin_lock_irqsave(&priv->lock, flags);
1336
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001337 while (!list_empty(&priv->cm.start_list)) {
1338 p = list_entry(priv->cm.start_list.next, typeof(*p), list);
1339 list_del_init(&p->list);
1340 neigh = p->neigh;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001341 qpn = IPOIB_QPN(neigh->daddr);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001342 memcpy(&pathrec, &p->path->pathrec, sizeof pathrec);
Roland Dreier943c2462008-09-30 10:36:21 -07001343
1344 spin_unlock_irqrestore(&priv->lock, flags);
1345 netif_tx_unlock_bh(dev);
1346
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001347 ret = ipoib_cm_tx_init(p, qpn, &pathrec);
Roland Dreier943c2462008-09-30 10:36:21 -07001348
1349 netif_tx_lock_bh(dev);
1350 spin_lock_irqsave(&priv->lock, flags);
1351
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001352 if (ret) {
1353 neigh = p->neigh;
1354 if (neigh) {
1355 neigh->cm = NULL;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001356 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001357 }
1358 list_del(&p->list);
1359 kfree(p);
1360 }
1361 }
Roland Dreier943c2462008-09-30 10:36:21 -07001362
1363 spin_unlock_irqrestore(&priv->lock, flags);
1364 netif_tx_unlock_bh(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001365}
1366
1367static void ipoib_cm_tx_reap(struct work_struct *work)
1368{
1369 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1370 cm.reap_task);
Roland Dreier943c2462008-09-30 10:36:21 -07001371 struct net_device *dev = priv->dev;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001372 struct ipoib_cm_tx *p;
Roland Dreier943c2462008-09-30 10:36:21 -07001373 unsigned long flags;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001374
Roland Dreier943c2462008-09-30 10:36:21 -07001375 netif_tx_lock_bh(dev);
1376 spin_lock_irqsave(&priv->lock, flags);
1377
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001378 while (!list_empty(&priv->cm.reap_list)) {
1379 p = list_entry(priv->cm.reap_list.next, typeof(*p), list);
1380 list_del(&p->list);
Roland Dreier943c2462008-09-30 10:36:21 -07001381 spin_unlock_irqrestore(&priv->lock, flags);
1382 netif_tx_unlock_bh(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001383 ipoib_cm_tx_destroy(p);
Roland Dreier943c2462008-09-30 10:36:21 -07001384 netif_tx_lock_bh(dev);
1385 spin_lock_irqsave(&priv->lock, flags);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001386 }
Roland Dreier943c2462008-09-30 10:36:21 -07001387
1388 spin_unlock_irqrestore(&priv->lock, flags);
1389 netif_tx_unlock_bh(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001390}
1391
1392static void ipoib_cm_skb_reap(struct work_struct *work)
1393{
1394 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1395 cm.skb_task);
Roland Dreier943c2462008-09-30 10:36:21 -07001396 struct net_device *dev = priv->dev;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001397 struct sk_buff *skb;
Roland Dreier943c2462008-09-30 10:36:21 -07001398 unsigned long flags;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001399 unsigned mtu = priv->mcast_mtu;
1400
Roland Dreier943c2462008-09-30 10:36:21 -07001401 netif_tx_lock_bh(dev);
1402 spin_lock_irqsave(&priv->lock, flags);
1403
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001404 while ((skb = skb_dequeue(&priv->cm.skb_queue))) {
Roland Dreier943c2462008-09-30 10:36:21 -07001405 spin_unlock_irqrestore(&priv->lock, flags);
1406 netif_tx_unlock_bh(dev);
1407
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001408 if (skb->protocol == htons(ETH_P_IP))
1409 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
Roland Dreierd90f9b32012-07-05 22:39:34 -07001410#if IS_ENABLED(CONFIG_IPV6)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001411 else if (skb->protocol == htons(ETH_P_IPV6))
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001412 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001413#endif
1414 dev_kfree_skb_any(skb);
Roland Dreier943c2462008-09-30 10:36:21 -07001415
1416 netif_tx_lock_bh(dev);
1417 spin_lock_irqsave(&priv->lock, flags);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001418 }
Roland Dreier943c2462008-09-30 10:36:21 -07001419
1420 spin_unlock_irqrestore(&priv->lock, flags);
1421 netif_tx_unlock_bh(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001422}
1423
Roland Dreier2337f802007-10-23 19:57:54 -07001424void ipoib_cm_skb_too_long(struct net_device *dev, struct sk_buff *skb,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001425 unsigned int mtu)
1426{
1427 struct ipoib_dev_priv *priv = netdev_priv(dev);
1428 int e = skb_queue_empty(&priv->cm.skb_queue);
1429
Eric Dumazetadf30902009-06-02 05:19:30 +00001430 if (skb_dst(skb))
David S. Miller6700c272012-07-17 03:29:28 -07001431 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001432
1433 skb_queue_tail(&priv->cm.skb_queue, skb);
1434 if (e)
Doug Ledford0b395782015-02-21 19:27:03 -05001435 queue_work(priv->wq, &priv->cm.skb_task);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001436}
1437
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001438static void ipoib_cm_rx_reap(struct work_struct *work)
1439{
Roland Dreierefcd9972008-01-25 14:15:24 -08001440 ipoib_cm_free_rx_reap_list(container_of(work, struct ipoib_dev_priv,
1441 cm.rx_reap_task)->dev);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001442}
1443
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001444static void ipoib_cm_stale_task(struct work_struct *work)
1445{
1446 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1447 cm.stale_task.work);
1448 struct ipoib_cm_rx *p;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001449 int ret;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001450
Roland Dreier37aebbde2007-04-24 21:30:37 -07001451 spin_lock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001452 while (!list_empty(&priv->cm.passive_ids)) {
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001453 /* List is sorted by LRU, start from tail,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001454 * stop when we see a recently used entry */
1455 p = list_entry(priv->cm.passive_ids.prev, typeof(*p), list);
Michael S. Tsirkin60a596d2007-03-22 14:32:09 -07001456 if (time_before_eq(jiffies, p->jiffies + IPOIB_CM_RX_TIMEOUT))
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001457 break;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001458 list_move(&p->list, &priv->cm.rx_error_list);
1459 p->state = IPOIB_CM_RX_ERROR;
Roland Dreier37aebbde2007-04-24 21:30:37 -07001460 spin_unlock_irq(&priv->lock);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001461 ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
1462 if (ret)
1463 ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001464 spin_lock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001465 }
Michael S. Tsirkin7c5b9ef2007-05-14 07:26:51 +03001466
1467 if (!list_empty(&priv->cm.passive_ids))
Doug Ledford0b395782015-02-21 19:27:03 -05001468 queue_delayed_work(priv->wq,
Michael S. Tsirkin7c5b9ef2007-05-14 07:26:51 +03001469 &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001470 spin_unlock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001471}
1472
Roland Dreier2337f802007-10-23 19:57:54 -07001473static ssize_t show_mode(struct device *d, struct device_attribute *attr,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001474 char *buf)
1475{
1476 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(d));
1477
1478 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
1479 return sprintf(buf, "connected\n");
1480 else
1481 return sprintf(buf, "datagram\n");
1482}
1483
Or Gerlitz862096a2012-09-27 12:06:02 +00001484static ssize_t set_mode(struct device *d, struct device_attribute *attr,
1485 const char *buf, size_t count)
1486{
1487 struct net_device *dev = to_net_dev(d);
1488 int ret;
Erez Shitrit198b12f2016-06-04 15:15:20 +03001489 struct ipoib_dev_priv *priv = netdev_priv(dev);
1490
1491 if (test_bit(IPOIB_FLAG_GOING_DOWN, &priv->flags))
1492 return -EPERM;
Or Gerlitz862096a2012-09-27 12:06:02 +00001493
1494 if (!rtnl_trylock())
1495 return restart_syscall();
1496
1497 ret = ipoib_set_mode(dev, buf);
1498
1499 rtnl_unlock();
1500
1501 if (!ret)
1502 return count;
1503
1504 return ret;
1505}
1506
Roland Dreier551fd612007-02-16 13:57:33 -08001507static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO, show_mode, set_mode);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001508
1509int ipoib_cm_add_mode_attr(struct net_device *dev)
1510{
1511 return device_create_file(&dev->dev, &dev_attr_mode);
1512}
1513
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001514static void ipoib_cm_create_srq(struct net_device *dev, int max_sge)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001515{
1516 struct ipoib_dev_priv *priv = netdev_priv(dev);
1517 struct ib_srq_init_attr srq_init_attr = {
Sean Hefty96104ed2011-05-23 16:31:36 -07001518 .srq_type = IB_SRQT_BASIC,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001519 .attr = {
1520 .max_wr = ipoib_recvq_size,
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001521 .max_sge = max_sge
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001522 }
1523 };
Roland Dreier7b3687d2008-01-25 14:15:24 -08001524
1525 priv->cm.srq = ib_create_srq(priv->pd, &srq_init_attr);
1526 if (IS_ERR(priv->cm.srq)) {
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001527 if (PTR_ERR(priv->cm.srq) != -ENOSYS)
1528 printk(KERN_WARNING "%s: failed to allocate SRQ, error %ld\n",
1529 priv->ca->name, PTR_ERR(priv->cm.srq));
Roland Dreier7b3687d2008-01-25 14:15:24 -08001530 priv->cm.srq = NULL;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001531 return;
Roland Dreier7b3687d2008-01-25 14:15:24 -08001532 }
1533
Joe Perches948579c2010-11-05 03:07:36 +00001534 priv->cm.srq_ring = vzalloc(ipoib_recvq_size * sizeof *priv->cm.srq_ring);
Roland Dreier7b3687d2008-01-25 14:15:24 -08001535 if (!priv->cm.srq_ring) {
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001536 printk(KERN_WARNING "%s: failed to allocate CM SRQ ring (%d entries)\n",
Roland Dreier7b3687d2008-01-25 14:15:24 -08001537 priv->ca->name, ipoib_recvq_size);
1538 ib_destroy_srq(priv->cm.srq);
1539 priv->cm.srq = NULL;
David J. Wilderb1404062008-08-08 15:51:29 -07001540 return;
Roland Dreier7b3687d2008-01-25 14:15:24 -08001541 }
David J. Wilderb1404062008-08-08 15:51:29 -07001542
Roland Dreier7b3687d2008-01-25 14:15:24 -08001543}
1544
1545int ipoib_cm_dev_init(struct net_device *dev)
1546{
1547 struct ipoib_dev_priv *priv = netdev_priv(dev);
Or Gerlitz4a061b22015-12-18 10:59:46 +02001548 int max_srq_sge, i;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001549
1550 INIT_LIST_HEAD(&priv->cm.passive_ids);
1551 INIT_LIST_HEAD(&priv->cm.reap_list);
1552 INIT_LIST_HEAD(&priv->cm.start_list);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001553 INIT_LIST_HEAD(&priv->cm.rx_error_list);
1554 INIT_LIST_HEAD(&priv->cm.rx_flush_list);
1555 INIT_LIST_HEAD(&priv->cm.rx_drain_list);
1556 INIT_LIST_HEAD(&priv->cm.rx_reap_list);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001557 INIT_WORK(&priv->cm.start_task, ipoib_cm_tx_start);
1558 INIT_WORK(&priv->cm.reap_task, ipoib_cm_tx_reap);
1559 INIT_WORK(&priv->cm.skb_task, ipoib_cm_skb_reap);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001560 INIT_WORK(&priv->cm.rx_reap_task, ipoib_cm_rx_reap);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001561 INIT_DELAYED_WORK(&priv->cm.stale_task, ipoib_cm_stale_task);
1562
1563 skb_queue_head_init(&priv->cm.skb_queue);
1564
Or Gerlitz4a061b22015-12-18 10:59:46 +02001565 ipoib_dbg(priv, "max_srq_sge=%d\n", priv->ca->attrs.max_srq_sge);
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001566
Or Gerlitz4a061b22015-12-18 10:59:46 +02001567 max_srq_sge = min_t(int, IPOIB_CM_RX_SG, priv->ca->attrs.max_srq_sge);
1568 ipoib_cm_create_srq(dev, max_srq_sge);
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001569 if (ipoib_cm_has_srq(dev)) {
Or Gerlitz4a061b22015-12-18 10:59:46 +02001570 priv->cm.max_cm_mtu = max_srq_sge * PAGE_SIZE - 0x10;
1571 priv->cm.num_frags = max_srq_sge;
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001572 ipoib_dbg(priv, "max_cm_mtu = 0x%x, num_frags=%d\n",
1573 priv->cm.max_cm_mtu, priv->cm.num_frags);
1574 } else {
1575 priv->cm.max_cm_mtu = IPOIB_CM_MTU;
1576 priv->cm.num_frags = IPOIB_CM_RX_SG;
1577 }
1578
Roland Dreiera7d834c2008-07-14 23:48:47 -07001579 ipoib_cm_init_rx_wr(dev, &priv->cm.rx_wr, priv->cm.rx_sge);
Roland Dreier7b3687d2008-01-25 14:15:24 -08001580
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001581 if (ipoib_cm_has_srq(dev)) {
1582 for (i = 0; i < ipoib_recvq_size; ++i) {
1583 if (!ipoib_cm_alloc_rx_skb(dev, priv->cm.srq_ring, i,
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001584 priv->cm.num_frags - 1,
Tal Alon22252b42013-10-16 17:37:48 +03001585 priv->cm.srq_ring[i].mapping,
1586 GFP_KERNEL)) {
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001587 ipoib_warn(priv, "failed to allocate "
1588 "receive buffer %d\n", i);
1589 ipoib_cm_dev_cleanup(dev);
1590 return -ENOMEM;
1591 }
1592
1593 if (ipoib_cm_post_receive_srq(dev, i)) {
1594 ipoib_warn(priv, "ipoib_cm_post_receive_srq "
1595 "failed for buf %d\n", i);
1596 ipoib_cm_dev_cleanup(dev);
1597 return -EIO;
1598 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001599 }
1600 }
1601
1602 priv->dev->dev_addr[0] = IPOIB_FLAGS_RC;
1603 return 0;
1604}
1605
1606void ipoib_cm_dev_cleanup(struct net_device *dev)
1607{
1608 struct ipoib_dev_priv *priv = netdev_priv(dev);
Roland Dreier1efb6142008-01-25 14:15:24 -08001609 int ret;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001610
1611 if (!priv->cm.srq)
1612 return;
1613
1614 ipoib_dbg(priv, "Cleanup ipoib connected mode.\n");
1615
1616 ret = ib_destroy_srq(priv->cm.srq);
1617 if (ret)
1618 ipoib_warn(priv, "ib_destroy_srq failed: %d\n", ret);
1619
1620 priv->cm.srq = NULL;
1621 if (!priv->cm.srq_ring)
1622 return;
Roland Dreier1efb6142008-01-25 14:15:24 -08001623
1624 ipoib_cm_free_rx_ring(dev, priv->cm.srq_ring);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001625 priv->cm.srq_ring = NULL;
1626}