blob: ad9b486ca7ea91e1fa8400250ccecfd4970e0de2 [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
Paolo Abenifc791b62016-10-13 18:26:56 +020066#define IPOIB_CM_RX_RESERVE (ALIGN(IPOIB_HARD_LEN, 16) - IPOIB_ENCAP_LEN)
67
Michael S. Tsirkin518b1642007-05-21 15:04:59 +030068static struct ib_qp_attr ipoib_cm_err_attr = {
69 .qp_state = IB_QPS_ERR
70};
71
Roland Dreier09f60f82007-10-26 13:44:25 -070072#define IPOIB_CM_RX_DRAIN_WRID 0xffffffff
Michael S. Tsirkin518b1642007-05-21 15:04:59 +030073
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +030074static struct ib_send_wr ipoib_cm_rx_drain_wr = {
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +030075 .opcode = IB_WR_SEND,
Michael S. Tsirkin518b1642007-05-21 15:04:59 +030076};
77
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020078static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
79 struct ib_cm_event *event);
80
Michael S. Tsirkin18120632007-02-20 20:17:55 +020081static void ipoib_cm_dma_unmap_rx(struct ipoib_dev_priv *priv, int frags,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020082 u64 mapping[IPOIB_CM_RX_SG])
83{
84 int i;
85
86 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
87
Michael S. Tsirkin18120632007-02-20 20:17:55 +020088 for (i = 0; i < frags; ++i)
Dotan Barak787adb92011-10-18 15:22:14 +020089 ib_dma_unmap_page(priv->ca, mapping[i + 1], PAGE_SIZE, DMA_FROM_DEVICE);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020090}
91
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -080092static int ipoib_cm_post_receive_srq(struct net_device *dev, int id)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020093{
94 struct ipoib_dev_priv *priv = netdev_priv(dev);
95 struct ib_recv_wr *bad_wr;
96 int i, ret;
97
Michael S. Tsirkin1b524962007-08-16 15:36:16 +030098 priv->cm.rx_wr.wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020099
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800100 for (i = 0; i < priv->cm.num_frags; ++i)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200101 priv->cm.rx_sge[i].addr = priv->cm.srq_ring[id].mapping[i];
102
103 ret = ib_post_srq_recv(priv->cm.srq, &priv->cm.rx_wr, &bad_wr);
104 if (unlikely(ret)) {
105 ipoib_warn(priv, "post srq failed for buf %d (%d)\n", id, ret);
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800106 ipoib_cm_dma_unmap_rx(priv, priv->cm.num_frags - 1,
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200107 priv->cm.srq_ring[id].mapping);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200108 dev_kfree_skb_any(priv->cm.srq_ring[id].skb);
109 priv->cm.srq_ring[id].skb = NULL;
110 }
111
112 return ret;
113}
114
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800115static int ipoib_cm_post_receive_nonsrq(struct net_device *dev,
Roland Dreiera7d834c2008-07-14 23:48:47 -0700116 struct ipoib_cm_rx *rx,
117 struct ib_recv_wr *wr,
118 struct ib_sge *sge, int id)
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800119{
120 struct ipoib_dev_priv *priv = netdev_priv(dev);
121 struct ib_recv_wr *bad_wr;
122 int i, ret;
123
Roland Dreiera7d834c2008-07-14 23:48:47 -0700124 wr->wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800125
126 for (i = 0; i < IPOIB_CM_RX_SG; ++i)
Roland Dreiera7d834c2008-07-14 23:48:47 -0700127 sge[i].addr = rx->rx_ring[id].mapping[i];
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800128
Roland Dreiera7d834c2008-07-14 23:48:47 -0700129 ret = ib_post_recv(rx->qp, wr, &bad_wr);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800130 if (unlikely(ret)) {
131 ipoib_warn(priv, "post recv failed for buf %d (%d)\n", id, ret);
132 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
133 rx->rx_ring[id].mapping);
134 dev_kfree_skb_any(rx->rx_ring[id].skb);
135 rx->rx_ring[id].skb = NULL;
136 }
137
138 return ret;
139}
140
141static struct sk_buff *ipoib_cm_alloc_rx_skb(struct net_device *dev,
142 struct ipoib_cm_rx_buf *rx_ring,
143 int id, int frags,
Tal Alon22252b42013-10-16 17:37:48 +0300144 u64 mapping[IPOIB_CM_RX_SG],
145 gfp_t gfp)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200146{
147 struct ipoib_dev_priv *priv = netdev_priv(dev);
148 struct sk_buff *skb;
149 int i;
150
Paolo Abenifc791b62016-10-13 18:26:56 +0200151 skb = dev_alloc_skb(ALIGN(IPOIB_CM_HEAD_SIZE + IPOIB_PSEUDO_LEN, 16));
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200152 if (unlikely(!skb))
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200153 return NULL;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200154
155 /*
Paolo Abenifc791b62016-10-13 18:26:56 +0200156 * IPoIB adds a IPOIB_ENCAP_LEN byte header, this will align the
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200157 * IP header to a multiple of 16.
158 */
Paolo Abenifc791b62016-10-13 18:26:56 +0200159 skb_reserve(skb, IPOIB_CM_RX_RESERVE);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200160
161 mapping[0] = ib_dma_map_single(priv->ca, skb->data, IPOIB_CM_HEAD_SIZE,
162 DMA_FROM_DEVICE);
163 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0]))) {
164 dev_kfree_skb_any(skb);
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200165 return NULL;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200166 }
167
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200168 for (i = 0; i < frags; i++) {
Tal Alon22252b42013-10-16 17:37:48 +0300169 struct page *page = alloc_page(gfp);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200170
171 if (!page)
172 goto partial_error;
173 skb_fill_page_desc(skb, i, page, 0, PAGE_SIZE);
174
Ian Campbell5581be32011-08-24 22:28:10 +0000175 mapping[i + 1] = ib_dma_map_page(priv->ca, page,
Michael S. Tsirkin6371ea32007-04-10 18:32:42 +0300176 0, PAGE_SIZE, DMA_FROM_DEVICE);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200177 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[i + 1])))
178 goto partial_error;
179 }
180
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800181 rx_ring[id].skb = skb;
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200182 return skb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200183
184partial_error:
185
186 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
187
Ralph Campbell841adfc2007-06-29 11:37:56 -0700188 for (; i > 0; --i)
Dotan Barak787adb92011-10-18 15:22:14 +0200189 ib_dma_unmap_page(priv->ca, mapping[i], PAGE_SIZE, DMA_FROM_DEVICE);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200190
Michael S. Tsirkin8a2e65f2007-02-16 00:16:13 +0200191 dev_kfree_skb_any(skb);
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200192 return NULL;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200193}
194
Roland Dreier1efb6142008-01-25 14:15:24 -0800195static void ipoib_cm_free_rx_ring(struct net_device *dev,
196 struct ipoib_cm_rx_buf *rx_ring)
197{
198 struct ipoib_dev_priv *priv = netdev_priv(dev);
199 int i;
200
201 for (i = 0; i < ipoib_recvq_size; ++i)
202 if (rx_ring[i].skb) {
203 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
204 rx_ring[i].mapping);
205 dev_kfree_skb_any(rx_ring[i].skb);
206 }
207
David J. Wilderb1404062008-08-08 15:51:29 -0700208 vfree(rx_ring);
Roland Dreier1efb6142008-01-25 14:15:24 -0800209}
210
Roland Dreier2337f802007-10-23 19:57:54 -0700211static void ipoib_cm_start_rx_drain(struct ipoib_dev_priv *priv)
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300212{
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300213 struct ib_send_wr *bad_wr;
214 struct ipoib_cm_rx *p;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300215
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300216 /* We only reserved 1 extra slot in CQ for drain WRs, so
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300217 * make sure we have at most 1 outstanding WR. */
218 if (list_empty(&priv->cm.rx_flush_list) ||
219 !list_empty(&priv->cm.rx_drain_list))
220 return;
221
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300222 /*
223 * QPs on flush list are error state. This way, a "flush
224 * error" WC will be immediately generated for each WR we post.
225 */
226 p = list_entry(priv->cm.rx_flush_list.next, typeof(*p), list);
Christoph Hellwig14d3a3b2015-12-11 11:53:03 -0800227 ipoib_cm_rx_drain_wr.wr_id = IPOIB_CM_RX_DRAIN_WRID;
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300228 if (ib_post_send(p->qp, &ipoib_cm_rx_drain_wr, &bad_wr))
229 ipoib_warn(priv, "failed to post drain wr\n");
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300230
231 list_splice_init(&priv->cm.rx_flush_list, &priv->cm.rx_drain_list);
232}
233
234static void ipoib_cm_rx_event_handler(struct ib_event *event, void *ctx)
235{
236 struct ipoib_cm_rx *p = ctx;
237 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
238 unsigned long flags;
239
240 if (event->event != IB_EVENT_QP_LAST_WQE_REACHED)
241 return;
242
243 spin_lock_irqsave(&priv->lock, flags);
244 list_move(&p->list, &priv->cm.rx_flush_list);
245 p->state = IPOIB_CM_RX_FLUSH;
246 ipoib_cm_start_rx_drain(priv);
247 spin_unlock_irqrestore(&priv->lock, flags);
248}
249
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200250static struct ib_qp *ipoib_cm_create_rx_qp(struct net_device *dev,
251 struct ipoib_cm_rx *p)
252{
253 struct ipoib_dev_priv *priv = netdev_priv(dev);
254 struct ib_qp_init_attr attr = {
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300255 .event_handler = ipoib_cm_rx_event_handler,
Eli Cohenf56bcd82008-04-29 13:46:53 -0700256 .send_cq = priv->recv_cq, /* For drain WR */
257 .recv_cq = priv->recv_cq,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200258 .srq = priv->cm.srq,
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300259 .cap.max_send_wr = 1, /* For drain WR */
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200260 .cap.max_send_sge = 1, /* FIXME: 0 Seems not to work */
261 .sq_sig_type = IB_SIGNAL_ALL_WR,
262 .qp_type = IB_QPT_RC,
263 .qp_context = p,
264 };
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800265
266 if (!ipoib_cm_has_srq(dev)) {
267 attr.cap.max_recv_wr = ipoib_recvq_size;
268 attr.cap.max_recv_sge = IPOIB_CM_RX_SG;
269 }
270
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200271 return ib_create_qp(priv->pd, &attr);
272}
273
274static int ipoib_cm_modify_rx_qp(struct net_device *dev,
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800275 struct ib_cm_id *cm_id, struct ib_qp *qp,
276 unsigned psn)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200277{
278 struct ipoib_dev_priv *priv = netdev_priv(dev);
279 struct ib_qp_attr qp_attr;
280 int qp_attr_mask, ret;
281
282 qp_attr.qp_state = IB_QPS_INIT;
283 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
284 if (ret) {
285 ipoib_warn(priv, "failed to init QP attr for INIT: %d\n", ret);
286 return ret;
287 }
288 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
289 if (ret) {
290 ipoib_warn(priv, "failed to modify QP to INIT: %d\n", ret);
291 return ret;
292 }
293 qp_attr.qp_state = IB_QPS_RTR;
294 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
295 if (ret) {
296 ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
297 return ret;
298 }
299 qp_attr.rq_psn = psn;
300 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
301 if (ret) {
302 ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
303 return ret;
304 }
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300305
306 /*
307 * Current Mellanox HCA firmware won't generate completions
308 * with error for drain WRs unless the QP has been moved to
309 * RTS first. This work-around leaves a window where a QP has
310 * moved to error asynchronously, but this will eventually get
311 * fixed in firmware, so let's not error out if modify QP
312 * fails.
313 */
314 qp_attr.qp_state = IB_QPS_RTS;
315 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
316 if (ret) {
317 ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
318 return 0;
319 }
320 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
321 if (ret) {
322 ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
323 return 0;
324 }
325
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200326 return 0;
327}
328
Roland Dreiera7d834c2008-07-14 23:48:47 -0700329static void ipoib_cm_init_rx_wr(struct net_device *dev,
330 struct ib_recv_wr *wr,
331 struct ib_sge *sge)
332{
333 struct ipoib_dev_priv *priv = netdev_priv(dev);
334 int i;
335
336 for (i = 0; i < priv->cm.num_frags; ++i)
Jason Gunthorpe77b1f992015-07-30 17:22:17 -0600337 sge[i].lkey = priv->pd->local_dma_lkey;
Roland Dreiera7d834c2008-07-14 23:48:47 -0700338
339 sge[0].length = IPOIB_CM_HEAD_SIZE;
340 for (i = 1; i < priv->cm.num_frags; ++i)
341 sge[i].length = PAGE_SIZE;
342
343 wr->next = NULL;
Roland Dreiere0819812008-07-30 07:21:46 -0700344 wr->sg_list = sge;
Roland Dreiera7d834c2008-07-14 23:48:47 -0700345 wr->num_sge = priv->cm.num_frags;
346}
347
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800348static int ipoib_cm_nonsrq_init_rx(struct net_device *dev, struct ib_cm_id *cm_id,
349 struct ipoib_cm_rx *rx)
350{
351 struct ipoib_dev_priv *priv = netdev_priv(dev);
Roland Dreiera7d834c2008-07-14 23:48:47 -0700352 struct {
353 struct ib_recv_wr wr;
354 struct ib_sge sge[IPOIB_CM_RX_SG];
355 } *t;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800356 int ret;
357 int i;
358
Joe Perches948579c2010-11-05 03:07:36 +0000359 rx->rx_ring = vzalloc(ipoib_recvq_size * sizeof *rx->rx_ring);
David J. Wilderb1404062008-08-08 15:51:29 -0700360 if (!rx->rx_ring) {
361 printk(KERN_WARNING "%s: failed to allocate CM non-SRQ ring (%d entries)\n",
362 priv->ca->name, ipoib_recvq_size);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800363 return -ENOMEM;
David J. Wilderb1404062008-08-08 15:51:29 -0700364 }
365
Roland Dreiera7d834c2008-07-14 23:48:47 -0700366 t = kmalloc(sizeof *t, GFP_KERNEL);
367 if (!t) {
368 ret = -ENOMEM;
369 goto err_free;
370 }
371
372 ipoib_cm_init_rx_wr(dev, &t->wr, t->sge);
373
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800374 spin_lock_irq(&priv->lock);
375
376 if (priv->cm.nonsrq_conn_qp >= ipoib_max_conn_qp) {
377 spin_unlock_irq(&priv->lock);
378 ib_send_cm_rej(cm_id, IB_CM_REJ_NO_QP, NULL, 0, NULL, 0);
379 ret = -EINVAL;
380 goto err_free;
381 } else
382 ++priv->cm.nonsrq_conn_qp;
383
384 spin_unlock_irq(&priv->lock);
385
386 for (i = 0; i < ipoib_recvq_size; ++i) {
387 if (!ipoib_cm_alloc_rx_skb(dev, rx->rx_ring, i, IPOIB_CM_RX_SG - 1,
Tal Alon22252b42013-10-16 17:37:48 +0300388 rx->rx_ring[i].mapping,
389 GFP_KERNEL)) {
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800390 ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
Bart Van Assche8f71c1a2015-05-05 13:01:39 +0200391 ret = -ENOMEM;
392 goto err_count;
Roland Dreiera7d834c2008-07-14 23:48:47 -0700393 }
394 ret = ipoib_cm_post_receive_nonsrq(dev, rx, &t->wr, t->sge, i);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800395 if (ret) {
396 ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq "
397 "failed for buf %d\n", i);
398 ret = -EIO;
399 goto err_count;
400 }
401 }
402
403 rx->recv_count = ipoib_recvq_size;
404
Roland Dreiera7d834c2008-07-14 23:48:47 -0700405 kfree(t);
406
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800407 return 0;
408
409err_count:
410 spin_lock_irq(&priv->lock);
411 --priv->cm.nonsrq_conn_qp;
412 spin_unlock_irq(&priv->lock);
413
414err_free:
Roland Dreiera7d834c2008-07-14 23:48:47 -0700415 kfree(t);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800416 ipoib_cm_free_rx_ring(dev, rx->rx_ring);
417
418 return ret;
419}
420
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200421static int ipoib_cm_send_rep(struct net_device *dev, struct ib_cm_id *cm_id,
422 struct ib_qp *qp, struct ib_cm_req_event_param *req,
423 unsigned psn)
424{
425 struct ipoib_dev_priv *priv = netdev_priv(dev);
426 struct ipoib_cm_data data = {};
427 struct ib_cm_rep_param rep = {};
428
429 data.qpn = cpu_to_be32(priv->qp->qp_num);
430 data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
431
432 rep.private_data = &data;
433 rep.private_data_len = sizeof data;
434 rep.flow_control = 0;
435 rep.rnr_retry_count = req->rnr_retry_count;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800436 rep.srq = ipoib_cm_has_srq(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200437 rep.qp_num = qp->qp_num;
438 rep.starting_psn = psn;
439 return ib_send_cm_rep(cm_id, &rep);
440}
441
442static int ipoib_cm_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
443{
444 struct net_device *dev = cm_id->context;
445 struct ipoib_dev_priv *priv = netdev_priv(dev);
446 struct ipoib_cm_rx *p;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200447 unsigned psn;
448 int ret;
449
450 ipoib_dbg(priv, "REQ arrived\n");
451 p = kzalloc(sizeof *p, GFP_KERNEL);
452 if (!p)
453 return -ENOMEM;
454 p->dev = dev;
455 p->id = cm_id;
Michael S. Tsirkin3ec73932007-06-19 13:40:41 +0300456 cm_id->context = p;
457 p->state = IPOIB_CM_RX_LIVE;
458 p->jiffies = jiffies;
459 INIT_LIST_HEAD(&p->list);
460
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200461 p->qp = ipoib_cm_create_rx_qp(dev, p);
462 if (IS_ERR(p->qp)) {
463 ret = PTR_ERR(p->qp);
464 goto err_qp;
465 }
466
Andrew Morton50bea5c2013-05-07 16:18:16 -0700467 psn = prandom_u32() & 0xffffff;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200468 ret = ipoib_cm_modify_rx_qp(dev, cm_id, p->qp, psn);
469 if (ret)
470 goto err_modify;
471
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800472 if (!ipoib_cm_has_srq(dev)) {
473 ret = ipoib_cm_nonsrq_init_rx(dev, cm_id, p);
474 if (ret)
475 goto err_modify;
476 }
477
Michael S. Tsirkin3ec73932007-06-19 13:40:41 +0300478 spin_lock_irq(&priv->lock);
Doug Ledford0b395782015-02-21 19:27:03 -0500479 queue_delayed_work(priv->wq,
Michael S. Tsirkin3ec73932007-06-19 13:40:41 +0300480 &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
481 /* Add this entry to passive ids list head, but do not re-add it
482 * if IB_EVENT_QP_LAST_WQE_REACHED has moved it to flush list. */
483 p->jiffies = jiffies;
484 if (p->state == IPOIB_CM_RX_LIVE)
485 list_move(&p->list, &priv->cm.passive_ids);
486 spin_unlock_irq(&priv->lock);
487
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200488 ret = ipoib_cm_send_rep(dev, cm_id, p->qp, &event->param.req_rcvd, psn);
489 if (ret) {
490 ipoib_warn(priv, "failed to send REP: %d\n", ret);
Michael S. Tsirkin3ec73932007-06-19 13:40:41 +0300491 if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE))
492 ipoib_warn(priv, "unable to move qp to error state\n");
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200493 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200494 return 0;
495
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200496err_modify:
497 ib_destroy_qp(p->qp);
498err_qp:
499 kfree(p);
500 return ret;
501}
502
503static int ipoib_cm_rx_handler(struct ib_cm_id *cm_id,
504 struct ib_cm_event *event)
505{
506 struct ipoib_cm_rx *p;
507 struct ipoib_dev_priv *priv;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200508
509 switch (event->event) {
510 case IB_CM_REQ_RECEIVED:
511 return ipoib_cm_req_handler(cm_id, event);
512 case IB_CM_DREQ_RECEIVED:
513 p = cm_id->context;
514 ib_send_cm_drep(cm_id, NULL, 0);
515 /* Fall through */
516 case IB_CM_REJ_RECEIVED:
517 p = cm_id->context;
518 priv = netdev_priv(p->dev);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300519 if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE))
520 ipoib_warn(priv, "unable to move qp to error state\n");
521 /* Fall through */
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200522 default:
523 return 0;
524 }
525}
526/* Adjust length of skb with fragments to match received data */
527static void skb_put_frags(struct sk_buff *skb, unsigned int hdr_space,
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200528 unsigned int length, struct sk_buff *toskb)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200529{
530 int i, num_frags;
531 unsigned int size;
532
533 /* put header into skb */
534 size = min(length, hdr_space);
535 skb->tail += size;
536 skb->len += size;
537 length -= size;
538
539 num_frags = skb_shinfo(skb)->nr_frags;
540 for (i = 0; i < num_frags; i++) {
541 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
542
543 if (length == 0) {
544 /* don't need this page */
Ian Campbell5581be32011-08-24 22:28:10 +0000545 skb_fill_page_desc(toskb, i, skb_frag_page(frag),
546 0, PAGE_SIZE);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200547 --skb_shinfo(skb)->nr_frags;
548 } else {
549 size = min(length, (unsigned) PAGE_SIZE);
550
Eric Dumazet9e903e02011-10-18 21:00:24 +0000551 skb_frag_size_set(frag, size);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200552 skb->data_len += size;
553 skb->truesize += size;
554 skb->len += size;
555 length -= size;
556 }
557 }
558}
559
560void ipoib_cm_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)
561{
562 struct ipoib_dev_priv *priv = netdev_priv(dev);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800563 struct ipoib_cm_rx_buf *rx_ring;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300564 unsigned int wr_id = wc->wr_id & ~(IPOIB_OP_CM | IPOIB_OP_RECV);
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200565 struct sk_buff *skb, *newskb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200566 struct ipoib_cm_rx *p;
567 unsigned long flags;
568 u64 mapping[IPOIB_CM_RX_SG];
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200569 int frags;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800570 int has_srq;
Eli Cohenf89271da2008-07-14 23:48:44 -0700571 struct sk_buff *small_skb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200572
Roland Dreiera89875f2007-04-18 20:20:53 -0700573 ipoib_dbg_data(priv, "cm recv completion: id %d, status: %d\n",
574 wr_id, wc->status);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200575
576 if (unlikely(wr_id >= ipoib_recvq_size)) {
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300577 if (wr_id == (IPOIB_CM_RX_DRAIN_WRID & ~(IPOIB_OP_CM | IPOIB_OP_RECV))) {
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300578 spin_lock_irqsave(&priv->lock, flags);
579 list_splice_init(&priv->cm.rx_drain_list, &priv->cm.rx_reap_list);
580 ipoib_cm_start_rx_drain(priv);
Doug Ledford0b395782015-02-21 19:27:03 -0500581 queue_work(priv->wq, &priv->cm.rx_reap_task);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300582 spin_unlock_irqrestore(&priv->lock, flags);
583 } else
584 ipoib_warn(priv, "cm recv completion event with wrid %d (> %d)\n",
585 wr_id, ipoib_recvq_size);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200586 return;
587 }
588
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800589 p = wc->qp->qp_context;
590
591 has_srq = ipoib_cm_has_srq(dev);
592 rx_ring = has_srq ? priv->cm.srq_ring : p->rx_ring;
593
594 skb = rx_ring[wr_id].skb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200595
596 if (unlikely(wc->status != IB_WC_SUCCESS)) {
597 ipoib_dbg(priv, "cm recv error "
598 "(status=%d, wrid=%d vend_err %x)\n",
599 wc->status, wr_id, wc->vendor_err);
Roland Dreierde903512007-09-28 15:33:51 -0700600 ++dev->stats.rx_dropped;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800601 if (has_srq)
602 goto repost;
603 else {
604 if (!--p->recv_count) {
605 spin_lock_irqsave(&priv->lock, flags);
606 list_move(&p->list, &priv->cm.rx_reap_list);
607 spin_unlock_irqrestore(&priv->lock, flags);
Doug Ledford0b395782015-02-21 19:27:03 -0500608 queue_work(priv->wq, &priv->cm.rx_reap_task);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800609 }
610 return;
611 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200612 }
613
Roland Dreierfd312562007-10-17 21:54:44 -0700614 if (unlikely(!(wr_id & IPOIB_CM_RX_UPDATE_MASK))) {
Michael S. Tsirkind6ef7d62007-05-02 15:31:12 +0300615 if (p && time_after_eq(jiffies, p->jiffies + IPOIB_CM_RX_UPDATE_TIME)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200616 spin_lock_irqsave(&priv->lock, flags);
617 p->jiffies = jiffies;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300618 /* Move this entry to list head, but do not re-add it
619 * if it has been moved out of list. */
620 if (p->state == IPOIB_CM_RX_LIVE)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200621 list_move(&p->list, &priv->cm.passive_ids);
622 spin_unlock_irqrestore(&priv->lock, flags);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200623 }
624 }
625
Eli Cohenf89271da2008-07-14 23:48:44 -0700626 if (wc->byte_len < IPOIB_CM_COPYBREAK) {
627 int dlen = wc->byte_len;
628
Paolo Abenifc791b62016-10-13 18:26:56 +0200629 small_skb = dev_alloc_skb(dlen + IPOIB_CM_RX_RESERVE);
Eli Cohenf89271da2008-07-14 23:48:44 -0700630 if (small_skb) {
Paolo Abenifc791b62016-10-13 18:26:56 +0200631 skb_reserve(small_skb, IPOIB_CM_RX_RESERVE);
Eli Cohenf89271da2008-07-14 23:48:44 -0700632 ib_dma_sync_single_for_cpu(priv->ca, rx_ring[wr_id].mapping[0],
633 dlen, DMA_FROM_DEVICE);
634 skb_copy_from_linear_data(skb, small_skb->data, dlen);
635 ib_dma_sync_single_for_device(priv->ca, rx_ring[wr_id].mapping[0],
636 dlen, DMA_FROM_DEVICE);
637 skb_put(small_skb, dlen);
638 skb = small_skb;
639 goto copied;
640 }
641 }
642
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200643 frags = PAGE_ALIGN(wc->byte_len - min(wc->byte_len,
644 (unsigned)IPOIB_CM_HEAD_SIZE)) / PAGE_SIZE;
645
Tal Alon22252b42013-10-16 17:37:48 +0300646 newskb = ipoib_cm_alloc_rx_skb(dev, rx_ring, wr_id, frags,
647 mapping, GFP_ATOMIC);
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200648 if (unlikely(!newskb)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200649 /*
650 * If we can't allocate a new RX buffer, dump
651 * this packet and reuse the old buffer.
652 */
653 ipoib_dbg(priv, "failed to allocate receive buffer %d\n", wr_id);
Roland Dreierde903512007-09-28 15:33:51 -0700654 ++dev->stats.rx_dropped;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200655 goto repost;
656 }
657
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800658 ipoib_cm_dma_unmap_rx(priv, frags, rx_ring[wr_id].mapping);
659 memcpy(rx_ring[wr_id].mapping, mapping, (frags + 1) * sizeof *mapping);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200660
661 ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
662 wc->byte_len, wc->slid);
663
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200664 skb_put_frags(skb, IPOIB_CM_HEAD_SIZE, wc->byte_len, newskb);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200665
Eli Cohenf89271da2008-07-14 23:48:44 -0700666copied:
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200667 skb->protocol = ((struct ipoib_header *) skb->data)->proto;
Paolo Abenifc791b62016-10-13 18:26:56 +0200668 skb_add_pseudo_hdr(skb);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200669
Roland Dreierde903512007-09-28 15:33:51 -0700670 ++dev->stats.rx_packets;
671 dev->stats.rx_bytes += skb->len;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200672
673 skb->dev = dev;
674 /* XXX get correct PACKET_ type here */
675 skb->pkt_type = PACKET_HOST;
Roland Dreier8d1cc862007-05-06 21:05:32 -0700676 netif_receive_skb(skb);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200677
678repost:
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800679 if (has_srq) {
680 if (unlikely(ipoib_cm_post_receive_srq(dev, wr_id)))
681 ipoib_warn(priv, "ipoib_cm_post_receive_srq failed "
682 "for buf %d\n", wr_id);
683 } else {
Roland Dreiera7d834c2008-07-14 23:48:47 -0700684 if (unlikely(ipoib_cm_post_receive_nonsrq(dev, p,
685 &priv->cm.rx_wr,
686 priv->cm.rx_sge,
687 wr_id))) {
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800688 --p->recv_count;
689 ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq failed "
690 "for buf %d\n", wr_id);
691 }
692 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200693}
694
695static inline int post_send(struct ipoib_dev_priv *priv,
696 struct ipoib_cm_tx *tx,
697 unsigned int wr_id,
Yuval Shaiac4268772015-07-12 01:24:09 -0700698 struct ipoib_tx_buf *tx_req)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200699{
700 struct ib_send_wr *bad_wr;
701
Yuval Shaiac4268772015-07-12 01:24:09 -0700702 ipoib_build_sge(priv, tx_req);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200703
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100704 priv->tx_wr.wr.wr_id = wr_id | IPOIB_OP_CM;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200705
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100706 return ib_post_send(tx->qp, &priv->tx_wr.wr, &bad_wr);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200707}
708
709void ipoib_cm_send(struct net_device *dev, struct sk_buff *skb, struct ipoib_cm_tx *tx)
710{
711 struct ipoib_dev_priv *priv = netdev_priv(dev);
Yuval Shaiac4268772015-07-12 01:24:09 -0700712 struct ipoib_tx_buf *tx_req;
Or Gerlitza48f5092010-03-04 13:17:37 +0000713 int rc;
Hans Westgaard Ry78a50a52016-03-02 13:44:28 +0100714 unsigned usable_sge = tx->max_send_sge - !!skb_headlen(skb);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200715
716 if (unlikely(skb->len > tx->mtu)) {
717 ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
718 skb->len, tx->mtu);
Roland Dreierde903512007-09-28 15:33:51 -0700719 ++dev->stats.tx_dropped;
720 ++dev->stats.tx_errors;
Michael S. Tsirkin77d8e1e2007-03-21 15:45:05 +0200721 ipoib_cm_skb_too_long(dev, skb, tx->mtu - IPOIB_ENCAP_LEN);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200722 return;
723 }
Hans Westgaard Ry78a50a52016-03-02 13:44:28 +0100724 if (skb_shinfo(skb)->nr_frags > usable_sge) {
725 if (skb_linearize(skb) < 0) {
726 ipoib_warn(priv, "skb could not be linearized\n");
727 ++dev->stats.tx_dropped;
728 ++dev->stats.tx_errors;
729 dev_kfree_skb_any(skb);
730 return;
731 }
732 /* Does skb_linearize return ok without reducing nr_frags? */
733 if (skb_shinfo(skb)->nr_frags > usable_sge) {
734 ipoib_warn(priv, "too many frags after skb linearize\n");
735 ++dev->stats.tx_dropped;
736 ++dev->stats.tx_errors;
737 dev_kfree_skb_any(skb);
738 return;
739 }
740 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200741 ipoib_dbg_data(priv, "sending packet: head 0x%x length %d connection 0x%x\n",
742 tx->tx_head, skb->len, tx->qp->qp_num);
743
744 /*
745 * We put the skb into the tx_ring _before_ we call post_send()
746 * because it's entirely possible that the completion handler will
747 * run before we execute anything after the post_send(). That
748 * means we have to make sure everything is properly recorded and
749 * our state is consistent before we call post_send().
750 */
751 tx_req = &tx->tx_ring[tx->tx_head & (ipoib_sendq_size - 1)];
752 tx_req->skb = skb;
Yuval Shaiac4268772015-07-12 01:24:09 -0700753
754 if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) {
Roland Dreierde903512007-09-28 15:33:51 -0700755 ++dev->stats.tx_errors;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200756 dev_kfree_skb_any(skb);
757 return;
758 }
759
Shlomo Pongratz7e5a90c2013-02-04 15:29:10 +0000760 skb_orphan(skb);
761 skb_dst_drop(skb);
762
Yuval Shaiac4268772015-07-12 01:24:09 -0700763 rc = post_send(priv, tx, tx->tx_head & (ipoib_sendq_size - 1), tx_req);
Or Gerlitza48f5092010-03-04 13:17:37 +0000764 if (unlikely(rc)) {
765 ipoib_warn(priv, "post_send failed, error %d\n", rc);
Roland Dreierde903512007-09-28 15:33:51 -0700766 ++dev->stats.tx_errors;
Yuval Shaiac4268772015-07-12 01:24:09 -0700767 ipoib_dma_unmap_tx(priv, tx_req);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200768 dev_kfree_skb_any(skb);
769 } else {
Florian Westphal860e9532016-05-03 16:33:13 +0200770 netif_trans_update(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200771 ++tx->tx_head;
772
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300773 if (++priv->tx_outstanding == ipoib_sendq_size) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200774 ipoib_dbg(priv, "TX ring 0x%x full, stopping kernel net queue\n",
775 tx->qp->qp_num);
776 netif_stop_queue(dev);
Mike Marciniszyn1ee9e2a2013-02-26 15:46:27 +0000777 rc = ib_req_notify_cq(priv->send_cq,
778 IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS);
779 if (rc < 0)
780 ipoib_warn(priv, "request notify on send CQ failed\n");
781 else if (rc)
782 ipoib_send_comp_handler(priv->send_cq, dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200783 }
784 }
785}
786
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300787void ipoib_cm_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200788{
789 struct ipoib_dev_priv *priv = netdev_priv(dev);
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300790 struct ipoib_cm_tx *tx = wc->qp->qp_context;
791 unsigned int wr_id = wc->wr_id & ~IPOIB_OP_CM;
Yuval Shaiac4268772015-07-12 01:24:09 -0700792 struct ipoib_tx_buf *tx_req;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200793 unsigned long flags;
794
Roland Dreiera89875f2007-04-18 20:20:53 -0700795 ipoib_dbg_data(priv, "cm send completion: id %d, status: %d\n",
796 wr_id, wc->status);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200797
798 if (unlikely(wr_id >= ipoib_sendq_size)) {
799 ipoib_warn(priv, "cm send completion event with wrid %d (> %d)\n",
800 wr_id, ipoib_sendq_size);
801 return;
802 }
803
804 tx_req = &tx->tx_ring[wr_id];
805
Yuval Shaiac4268772015-07-12 01:24:09 -0700806 ipoib_dma_unmap_tx(priv, tx_req);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200807
808 /* FIXME: is this right? Shouldn't we only increment on success? */
Roland Dreierde903512007-09-28 15:33:51 -0700809 ++dev->stats.tx_packets;
810 dev->stats.tx_bytes += tx_req->skb->len;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200811
812 dev_kfree_skb_any(tx_req->skb);
813
Roland Dreier943c2462008-09-30 10:36:21 -0700814 netif_tx_lock(dev);
815
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200816 ++tx->tx_tail;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300817 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
818 netif_queue_stopped(dev) &&
819 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200820 netif_wake_queue(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200821
822 if (wc->status != IB_WC_SUCCESS &&
823 wc->status != IB_WC_WR_FLUSH_ERR) {
824 struct ipoib_neigh *neigh;
825
826 ipoib_dbg(priv, "failed cm send event "
827 "(status=%d, wrid=%d vend_err %x)\n",
828 wc->status, wr_id, wc->vendor_err);
829
Roland Dreier943c2462008-09-30 10:36:21 -0700830 spin_lock_irqsave(&priv->lock, flags);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200831 neigh = tx->neigh;
832
833 if (neigh) {
834 neigh->cm = NULL;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000835 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200836
837 tx->neigh = NULL;
838 }
839
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200840 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
841 list_move(&tx->list, &priv->cm.reap_list);
Doug Ledford0b395782015-02-21 19:27:03 -0500842 queue_work(priv->wq, &priv->cm.reap_task);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200843 }
844
845 clear_bit(IPOIB_FLAG_OPER_UP, &tx->flags);
846
Roland Dreier943c2462008-09-30 10:36:21 -0700847 spin_unlock_irqrestore(&priv->lock, flags);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200848 }
849
Roland Dreier943c2462008-09-30 10:36:21 -0700850 netif_tx_unlock(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200851}
852
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200853int ipoib_cm_dev_open(struct net_device *dev)
854{
855 struct ipoib_dev_priv *priv = netdev_priv(dev);
856 int ret;
857
858 if (!IPOIB_CM_SUPPORTED(dev->dev_addr))
859 return 0;
860
861 priv->cm.id = ib_create_cm_id(priv->ca, ipoib_cm_rx_handler, dev);
862 if (IS_ERR(priv->cm.id)) {
863 printk(KERN_WARNING "%s: failed to create CM ID\n", priv->ca->name);
Michael S. Tsirkin347fcfb2007-04-30 17:30:28 -0700864 ret = PTR_ERR(priv->cm.id);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300865 goto err_cm;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200866 }
867
868 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 +0300869 0);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200870 if (ret) {
871 printk(KERN_WARNING "%s: failed to listen on ID 0x%llx\n", priv->ca->name,
872 IPOIB_CM_IETF_ID | priv->qp->qp_num);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300873 goto err_listen;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200874 }
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300875
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200876 return 0;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300877
878err_listen:
879 ib_destroy_cm_id(priv->cm.id);
880err_cm:
881 priv->cm.id = NULL;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300882 return ret;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200883}
884
Roland Dreierefcd9972008-01-25 14:15:24 -0800885static void ipoib_cm_free_rx_reap_list(struct net_device *dev)
886{
887 struct ipoib_dev_priv *priv = netdev_priv(dev);
888 struct ipoib_cm_rx *rx, *n;
889 LIST_HEAD(list);
890
891 spin_lock_irq(&priv->lock);
892 list_splice_init(&priv->cm.rx_reap_list, &list);
893 spin_unlock_irq(&priv->lock);
894
895 list_for_each_entry_safe(rx, n, &list, list) {
896 ib_destroy_cm_id(rx->id);
897 ib_destroy_qp(rx->qp);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800898 if (!ipoib_cm_has_srq(dev)) {
899 ipoib_cm_free_rx_ring(priv->dev, rx->rx_ring);
900 spin_lock_irq(&priv->lock);
901 --priv->cm.nonsrq_conn_qp;
902 spin_unlock_irq(&priv->lock);
903 }
Roland Dreierefcd9972008-01-25 14:15:24 -0800904 kfree(rx);
905 }
906}
907
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200908void ipoib_cm_dev_stop(struct net_device *dev)
909{
910 struct ipoib_dev_priv *priv = netdev_priv(dev);
Roland Dreierefcd9972008-01-25 14:15:24 -0800911 struct ipoib_cm_rx *p;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300912 unsigned long begin;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300913 int ret;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200914
Michael S. Tsirkin347fcfb2007-04-30 17:30:28 -0700915 if (!IPOIB_CM_SUPPORTED(dev->dev_addr) || !priv->cm.id)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200916 return;
917
918 ib_destroy_cm_id(priv->cm.id);
Michael S. Tsirkin347fcfb2007-04-30 17:30:28 -0700919 priv->cm.id = NULL;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300920
Roland Dreier37aebbde2007-04-24 21:30:37 -0700921 spin_lock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200922 while (!list_empty(&priv->cm.passive_ids)) {
923 p = list_entry(priv->cm.passive_ids.next, typeof(*p), list);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300924 list_move(&p->list, &priv->cm.rx_error_list);
925 p->state = IPOIB_CM_RX_ERROR;
Roland Dreier37aebbde2007-04-24 21:30:37 -0700926 spin_unlock_irq(&priv->lock);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300927 ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
928 if (ret)
929 ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
930 spin_lock_irq(&priv->lock);
931 }
932
933 /* Wait for all RX to be drained */
934 begin = jiffies;
935
936 while (!list_empty(&priv->cm.rx_error_list) ||
937 !list_empty(&priv->cm.rx_flush_list) ||
938 !list_empty(&priv->cm.rx_drain_list)) {
Michael S. Tsirkin8fd357a2007-05-24 14:02:39 -0700939 if (time_after(jiffies, begin + 5 * HZ)) {
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300940 ipoib_warn(priv, "RX drain timing out\n");
941
942 /*
943 * assume the HW is wedged and just free up everything.
944 */
Pradeep Satyanarayanaec229e52008-02-12 15:00:59 -0800945 list_splice_init(&priv->cm.rx_flush_list,
946 &priv->cm.rx_reap_list);
947 list_splice_init(&priv->cm.rx_error_list,
948 &priv->cm.rx_reap_list);
949 list_splice_init(&priv->cm.rx_drain_list,
950 &priv->cm.rx_reap_list);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300951 break;
952 }
953 spin_unlock_irq(&priv->lock);
954 msleep(1);
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300955 ipoib_drain_cq(dev);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300956 spin_lock_irq(&priv->lock);
957 }
958
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300959 spin_unlock_irq(&priv->lock);
960
Roland Dreierefcd9972008-01-25 14:15:24 -0800961 ipoib_cm_free_rx_reap_list(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200962
963 cancel_delayed_work(&priv->cm.stale_task);
964}
965
966static int ipoib_cm_rep_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
967{
968 struct ipoib_cm_tx *p = cm_id->context;
969 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
970 struct ipoib_cm_data *data = event->private_data;
971 struct sk_buff_head skqueue;
972 struct ib_qp_attr qp_attr;
973 int qp_attr_mask, ret;
974 struct sk_buff *skb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200975
976 p->mtu = be32_to_cpu(data->mtu);
977
Michael S. Tsirkin82c3aca2007-06-20 19:22:15 +0300978 if (p->mtu <= IPOIB_ENCAP_LEN) {
979 ipoib_warn(priv, "Rejecting connection: mtu %d <= %d\n",
980 p->mtu, IPOIB_ENCAP_LEN);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200981 return -EINVAL;
982 }
983
984 qp_attr.qp_state = IB_QPS_RTR;
985 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
986 if (ret) {
987 ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
988 return ret;
989 }
990
991 qp_attr.rq_psn = 0 /* FIXME */;
992 ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
993 if (ret) {
994 ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
995 return ret;
996 }
997
998 qp_attr.qp_state = IB_QPS_RTS;
999 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
1000 if (ret) {
1001 ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
1002 return ret;
1003 }
1004 ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
1005 if (ret) {
1006 ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
1007 return ret;
1008 }
1009
1010 skb_queue_head_init(&skqueue);
1011
Aaron Knister542d6fa2018-08-24 08:42:46 -04001012 netif_tx_lock_bh(p->dev);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001013 spin_lock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001014 set_bit(IPOIB_FLAG_OPER_UP, &p->flags);
1015 if (p->neigh)
1016 while ((skb = __skb_dequeue(&p->neigh->queue)))
1017 __skb_queue_tail(&skqueue, skb);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001018 spin_unlock_irq(&priv->lock);
Aaron Knister542d6fa2018-08-24 08:42:46 -04001019 netif_tx_unlock_bh(p->dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001020
1021 while ((skb = __skb_dequeue(&skqueue))) {
1022 skb->dev = p->dev;
1023 if (dev_queue_xmit(skb))
1024 ipoib_warn(priv, "dev_queue_xmit failed "
1025 "to requeue packet\n");
1026 }
1027
1028 ret = ib_send_cm_rtu(cm_id, NULL, 0);
1029 if (ret) {
1030 ipoib_warn(priv, "failed to send RTU: %d\n", ret);
1031 return ret;
1032 }
1033 return 0;
1034}
1035
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001036static 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 +02001037{
1038 struct ipoib_dev_priv *priv = netdev_priv(dev);
Dotan Barakede6bc02007-10-07 09:30:48 +02001039 struct ib_qp_init_attr attr = {
Eli Cohenf56bcd82008-04-29 13:46:53 -07001040 .send_cq = priv->recv_cq,
1041 .recv_cq = priv->recv_cq,
Dotan Barakede6bc02007-10-07 09:30:48 +02001042 .srq = priv->cm.srq,
1043 .cap.max_send_wr = ipoib_sendq_size,
1044 .cap.max_send_sge = 1,
1045 .sq_sig_type = IB_SIGNAL_ALL_WR,
1046 .qp_type = IB_QPT_RC,
Or Gerlitz09b93082014-05-11 15:15:11 +03001047 .qp_context = tx,
1048 .create_flags = IB_QP_CREATE_USE_GFP_NOIO
Roland Dreier2337f802007-10-23 19:57:54 -07001049 };
Dotan Barakede6bc02007-10-07 09:30:48 +02001050
Or Gerlitz09b93082014-05-11 15:15:11 +03001051 struct ib_qp *tx_qp;
1052
Yuval Shaiac4268772015-07-12 01:24:09 -07001053 if (dev->features & NETIF_F_SG)
Hans Westgaard Ry78a50a52016-03-02 13:44:28 +01001054 attr.cap.max_send_sge =
1055 min_t(u32, priv->ca->attrs.max_sge, MAX_SKB_FRAGS + 1);
Yuval Shaiac4268772015-07-12 01:24:09 -07001056
Or Gerlitz09b93082014-05-11 15:15:11 +03001057 tx_qp = ib_create_qp(priv->pd, &attr);
1058 if (PTR_ERR(tx_qp) == -EINVAL) {
Or Gerlitz09b93082014-05-11 15:15:11 +03001059 attr.create_flags &= ~IB_QP_CREATE_USE_GFP_NOIO;
1060 tx_qp = ib_create_qp(priv->pd, &attr);
1061 }
Hans Westgaard Ry78a50a52016-03-02 13:44:28 +01001062 tx->max_send_sge = attr.cap.max_send_sge;
Or Gerlitz09b93082014-05-11 15:15:11 +03001063 return tx_qp;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001064}
1065
1066static int ipoib_cm_send_req(struct net_device *dev,
1067 struct ib_cm_id *id, struct ib_qp *qp,
1068 u32 qpn,
1069 struct ib_sa_path_rec *pathrec)
1070{
1071 struct ipoib_dev_priv *priv = netdev_priv(dev);
1072 struct ipoib_cm_data data = {};
1073 struct ib_cm_req_param req = {};
1074
1075 data.qpn = cpu_to_be32(priv->qp->qp_num);
1076 data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
1077
Roland Dreier2337f802007-10-23 19:57:54 -07001078 req.primary_path = pathrec;
1079 req.alternate_path = NULL;
1080 req.service_id = cpu_to_be64(IPOIB_CM_IETF_ID | qpn);
1081 req.qp_num = qp->qp_num;
1082 req.qp_type = qp->qp_type;
1083 req.private_data = &data;
1084 req.private_data_len = sizeof data;
1085 req.flow_control = 0;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001086
Roland Dreier2337f802007-10-23 19:57:54 -07001087 req.starting_psn = 0; /* FIXME */
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001088
1089 /*
1090 * Pick some arbitrary defaults here; we could make these
1091 * module parameters if anyone cared about setting them.
1092 */
Roland Dreier2337f802007-10-23 19:57:54 -07001093 req.responder_resources = 4;
1094 req.remote_cm_response_timeout = 20;
1095 req.local_cm_response_timeout = 20;
1096 req.retry_count = 0; /* RFC draft warns against retries */
1097 req.rnr_retry_count = 0; /* RFC draft warns against retries */
1098 req.max_cm_retries = 15;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001099 req.srq = ipoib_cm_has_srq(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001100 return ib_send_cm_req(id, &req);
1101}
1102
1103static int ipoib_cm_modify_tx_init(struct net_device *dev,
1104 struct ib_cm_id *cm_id, struct ib_qp *qp)
1105{
1106 struct ipoib_dev_priv *priv = netdev_priv(dev);
1107 struct ib_qp_attr qp_attr;
1108 int qp_attr_mask, ret;
Roland Dreier9fdd5e52008-04-16 21:09:35 -07001109 ret = ib_find_pkey(priv->ca, priv->port, priv->pkey, &qp_attr.pkey_index);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001110 if (ret) {
Roland Dreier9fdd5e52008-04-16 21:09:35 -07001111 ipoib_warn(priv, "pkey 0x%x not found: %d\n", priv->pkey, ret);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001112 return ret;
1113 }
1114
1115 qp_attr.qp_state = IB_QPS_INIT;
1116 qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE;
1117 qp_attr.port_num = priv->port;
1118 qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
1119
1120 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
1121 if (ret) {
1122 ipoib_warn(priv, "failed to modify tx QP to INIT: %d\n", ret);
1123 return ret;
1124 }
1125 return 0;
1126}
1127
1128static int ipoib_cm_tx_init(struct ipoib_cm_tx *p, u32 qpn,
1129 struct ib_sa_path_rec *pathrec)
1130{
1131 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
1132 int ret;
1133
Or Gerlitz09b93082014-05-11 15:15:11 +03001134 p->tx_ring = __vmalloc(ipoib_sendq_size * sizeof *p->tx_ring,
1135 GFP_NOIO, PAGE_KERNEL);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001136 if (!p->tx_ring) {
1137 ipoib_warn(priv, "failed to allocate tx ring\n");
1138 ret = -ENOMEM;
1139 goto err_tx;
1140 }
Or Gerlitz09b93082014-05-11 15:15:11 +03001141 memset(p->tx_ring, 0, ipoib_sendq_size * sizeof *p->tx_ring);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001142
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001143 p->qp = ipoib_cm_create_tx_qp(p->dev, p);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001144 if (IS_ERR(p->qp)) {
1145 ret = PTR_ERR(p->qp);
1146 ipoib_warn(priv, "failed to allocate tx qp: %d\n", ret);
1147 goto err_qp;
1148 }
1149
1150 p->id = ib_create_cm_id(priv->ca, ipoib_cm_tx_handler, p);
1151 if (IS_ERR(p->id)) {
1152 ret = PTR_ERR(p->id);
1153 ipoib_warn(priv, "failed to create tx cm id: %d\n", ret);
1154 goto err_id;
1155 }
1156
1157 ret = ipoib_cm_modify_tx_init(p->dev, p->id, p->qp);
1158 if (ret) {
1159 ipoib_warn(priv, "failed to modify tx qp to rtr: %d\n", ret);
1160 goto err_modify;
1161 }
1162
1163 ret = ipoib_cm_send_req(p->dev, p->id, p->qp, qpn, pathrec);
1164 if (ret) {
1165 ipoib_warn(priv, "failed to send cm req: %d\n", ret);
1166 goto err_send_cm;
1167 }
1168
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001169 ipoib_dbg(priv, "Request connection 0x%x for gid %pI6 qpn 0x%x\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -07001170 p->qp->qp_num, pathrec->dgid.raw, qpn);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001171
1172 return 0;
1173
1174err_send_cm:
1175err_modify:
1176 ib_destroy_cm_id(p->id);
1177err_id:
1178 p->id = NULL;
1179 ib_destroy_qp(p->qp);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001180err_qp:
1181 p->qp = NULL;
Roland Dreier10313cb2008-03-12 07:51:03 -07001182 vfree(p->tx_ring);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001183err_tx:
1184 return ret;
1185}
1186
1187static void ipoib_cm_tx_destroy(struct ipoib_cm_tx *p)
1188{
1189 struct ipoib_dev_priv *priv = netdev_priv(p->dev);
Yuval Shaiac4268772015-07-12 01:24:09 -07001190 struct ipoib_tx_buf *tx_req;
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001191 unsigned long begin;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001192
1193 ipoib_dbg(priv, "Destroy active connection 0x%x head 0x%x tail 0x%x\n",
1194 p->qp ? p->qp->qp_num : 0, p->tx_head, p->tx_tail);
1195
1196 if (p->id)
1197 ib_destroy_cm_id(p->id);
1198
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001199 if (p->tx_ring) {
1200 /* Wait for all sends to complete */
1201 begin = jiffies;
1202 while ((int) p->tx_tail - (int) p->tx_head < 0) {
1203 if (time_after(jiffies, begin + 5 * HZ)) {
1204 ipoib_warn(priv, "timing out; %d sends not completed\n",
1205 p->tx_head - p->tx_tail);
1206 goto timeout;
1207 }
1208
1209 msleep(1);
1210 }
1211 }
1212
1213timeout:
1214
1215 while ((int) p->tx_tail - (int) p->tx_head < 0) {
1216 tx_req = &p->tx_ring[p->tx_tail & (ipoib_sendq_size - 1)];
Yuval Shaiac4268772015-07-12 01:24:09 -07001217 ipoib_dma_unmap_tx(priv, tx_req);
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001218 dev_kfree_skb_any(tx_req->skb);
1219 ++p->tx_tail;
Roland Dreier943c2462008-09-30 10:36:21 -07001220 netif_tx_lock_bh(p->dev);
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001221 if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
1222 netif_queue_stopped(p->dev) &&
1223 test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
1224 netif_wake_queue(p->dev);
Roland Dreier943c2462008-09-30 10:36:21 -07001225 netif_tx_unlock_bh(p->dev);
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001226 }
1227
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001228 if (p->qp)
1229 ib_destroy_qp(p->qp);
1230
Roland Dreier10313cb2008-03-12 07:51:03 -07001231 vfree(p->tx_ring);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001232 kfree(p);
1233}
1234
1235static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
1236 struct ib_cm_event *event)
1237{
1238 struct ipoib_cm_tx *tx = cm_id->context;
1239 struct ipoib_dev_priv *priv = netdev_priv(tx->dev);
1240 struct net_device *dev = priv->dev;
1241 struct ipoib_neigh *neigh;
Roland Dreier943c2462008-09-30 10:36:21 -07001242 unsigned long flags;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001243 int ret;
1244
1245 switch (event->event) {
1246 case IB_CM_DREQ_RECEIVED:
1247 ipoib_dbg(priv, "DREQ received.\n");
1248 ib_send_cm_drep(cm_id, NULL, 0);
1249 break;
1250 case IB_CM_REP_RECEIVED:
1251 ipoib_dbg(priv, "REP received.\n");
1252 ret = ipoib_cm_rep_handler(cm_id, event);
1253 if (ret)
1254 ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED,
1255 NULL, 0, NULL, 0);
1256 break;
1257 case IB_CM_REQ_ERROR:
1258 case IB_CM_REJ_RECEIVED:
1259 case IB_CM_TIMEWAIT_EXIT:
1260 ipoib_dbg(priv, "CM error %d.\n", event->event);
Roland Dreier943c2462008-09-30 10:36:21 -07001261 netif_tx_lock_bh(dev);
1262 spin_lock_irqsave(&priv->lock, flags);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001263 neigh = tx->neigh;
1264
1265 if (neigh) {
1266 neigh->cm = NULL;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001267 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001268
1269 tx->neigh = NULL;
1270 }
1271
1272 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
1273 list_move(&tx->list, &priv->cm.reap_list);
Doug Ledford0b395782015-02-21 19:27:03 -05001274 queue_work(priv->wq, &priv->cm.reap_task);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001275 }
1276
Roland Dreier943c2462008-09-30 10:36:21 -07001277 spin_unlock_irqrestore(&priv->lock, flags);
1278 netif_tx_unlock_bh(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001279 break;
1280 default:
1281 break;
1282 }
1283
1284 return 0;
1285}
1286
1287struct ipoib_cm_tx *ipoib_cm_create_tx(struct net_device *dev, struct ipoib_path *path,
1288 struct ipoib_neigh *neigh)
1289{
1290 struct ipoib_dev_priv *priv = netdev_priv(dev);
1291 struct ipoib_cm_tx *tx;
1292
1293 tx = kzalloc(sizeof *tx, GFP_ATOMIC);
1294 if (!tx)
1295 return NULL;
1296
1297 neigh->cm = tx;
1298 tx->neigh = neigh;
1299 tx->path = path;
1300 tx->dev = dev;
1301 list_add(&tx->list, &priv->cm.start_list);
1302 set_bit(IPOIB_FLAG_INITIALIZED, &tx->flags);
Doug Ledford0b395782015-02-21 19:27:03 -05001303 queue_work(priv->wq, &priv->cm.start_task);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001304 return tx;
1305}
1306
1307void ipoib_cm_destroy_tx(struct ipoib_cm_tx *tx)
1308{
1309 struct ipoib_dev_priv *priv = netdev_priv(tx->dev);
Shlomo Pongratzfa16ebe2012-08-13 14:39:49 +00001310 unsigned long flags;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001311 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
Shlomo Pongratzfa16ebe2012-08-13 14:39:49 +00001312 spin_lock_irqsave(&priv->lock, flags);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001313 list_move(&tx->list, &priv->cm.reap_list);
Doug Ledford0b395782015-02-21 19:27:03 -05001314 queue_work(priv->wq, &priv->cm.reap_task);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001315 ipoib_dbg(priv, "Reap connection for gid %pI6\n",
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001316 tx->neigh->daddr + 4);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001317 tx->neigh = NULL;
Shlomo Pongratzfa16ebe2012-08-13 14:39:49 +00001318 spin_unlock_irqrestore(&priv->lock, flags);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001319 }
1320}
1321
Erez Shitrit546481c2016-08-28 10:58:31 +03001322#define QPN_AND_OPTIONS_OFFSET 4
1323
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001324static void ipoib_cm_tx_start(struct work_struct *work)
1325{
1326 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1327 cm.start_task);
1328 struct net_device *dev = priv->dev;
1329 struct ipoib_neigh *neigh;
1330 struct ipoib_cm_tx *p;
1331 unsigned long flags;
Erez Shitrit546481c2016-08-28 10:58:31 +03001332 struct ipoib_path *path;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001333 int ret;
1334
1335 struct ib_sa_path_rec pathrec;
1336 u32 qpn;
1337
Roland Dreier943c2462008-09-30 10:36:21 -07001338 netif_tx_lock_bh(dev);
1339 spin_lock_irqsave(&priv->lock, flags);
1340
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001341 while (!list_empty(&priv->cm.start_list)) {
1342 p = list_entry(priv->cm.start_list.next, typeof(*p), list);
1343 list_del_init(&p->list);
1344 neigh = p->neigh;
Erez Shitrit546481c2016-08-28 10:58:31 +03001345
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001346 qpn = IPOIB_QPN(neigh->daddr);
Erez Shitrit546481c2016-08-28 10:58:31 +03001347 /*
1348 * As long as the search is with these 2 locks,
1349 * path existence indicates its validity.
1350 */
1351 path = __path_find(dev, neigh->daddr + QPN_AND_OPTIONS_OFFSET);
1352 if (!path) {
1353 pr_info("%s ignore not valid path %pI6\n",
1354 __func__,
1355 neigh->daddr + QPN_AND_OPTIONS_OFFSET);
1356 goto free_neigh;
1357 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001358 memcpy(&pathrec, &p->path->pathrec, sizeof pathrec);
Roland Dreier943c2462008-09-30 10:36:21 -07001359
1360 spin_unlock_irqrestore(&priv->lock, flags);
1361 netif_tx_unlock_bh(dev);
1362
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001363 ret = ipoib_cm_tx_init(p, qpn, &pathrec);
Roland Dreier943c2462008-09-30 10:36:21 -07001364
1365 netif_tx_lock_bh(dev);
1366 spin_lock_irqsave(&priv->lock, flags);
1367
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001368 if (ret) {
Erez Shitrit546481c2016-08-28 10:58:31 +03001369free_neigh:
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001370 neigh = p->neigh;
1371 if (neigh) {
1372 neigh->cm = NULL;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001373 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001374 }
1375 list_del(&p->list);
1376 kfree(p);
1377 }
1378 }
Roland Dreier943c2462008-09-30 10:36:21 -07001379
1380 spin_unlock_irqrestore(&priv->lock, flags);
1381 netif_tx_unlock_bh(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001382}
1383
1384static void ipoib_cm_tx_reap(struct work_struct *work)
1385{
1386 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1387 cm.reap_task);
Roland Dreier943c2462008-09-30 10:36:21 -07001388 struct net_device *dev = priv->dev;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001389 struct ipoib_cm_tx *p;
Roland Dreier943c2462008-09-30 10:36:21 -07001390 unsigned long flags;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001391
Roland Dreier943c2462008-09-30 10:36:21 -07001392 netif_tx_lock_bh(dev);
1393 spin_lock_irqsave(&priv->lock, flags);
1394
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001395 while (!list_empty(&priv->cm.reap_list)) {
1396 p = list_entry(priv->cm.reap_list.next, typeof(*p), list);
Feras Daoudd369bba2016-12-28 14:47:28 +02001397 list_del_init(&p->list);
Roland Dreier943c2462008-09-30 10:36:21 -07001398 spin_unlock_irqrestore(&priv->lock, flags);
1399 netif_tx_unlock_bh(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001400 ipoib_cm_tx_destroy(p);
Roland Dreier943c2462008-09-30 10:36:21 -07001401 netif_tx_lock_bh(dev);
1402 spin_lock_irqsave(&priv->lock, flags);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001403 }
Roland Dreier943c2462008-09-30 10:36:21 -07001404
1405 spin_unlock_irqrestore(&priv->lock, flags);
1406 netif_tx_unlock_bh(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001407}
1408
1409static void ipoib_cm_skb_reap(struct work_struct *work)
1410{
1411 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1412 cm.skb_task);
Roland Dreier943c2462008-09-30 10:36:21 -07001413 struct net_device *dev = priv->dev;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001414 struct sk_buff *skb;
Roland Dreier943c2462008-09-30 10:36:21 -07001415 unsigned long flags;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001416 unsigned mtu = priv->mcast_mtu;
1417
Roland Dreier943c2462008-09-30 10:36:21 -07001418 netif_tx_lock_bh(dev);
1419 spin_lock_irqsave(&priv->lock, flags);
1420
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001421 while ((skb = skb_dequeue(&priv->cm.skb_queue))) {
Roland Dreier943c2462008-09-30 10:36:21 -07001422 spin_unlock_irqrestore(&priv->lock, flags);
1423 netif_tx_unlock_bh(dev);
1424
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001425 if (skb->protocol == htons(ETH_P_IP))
1426 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
Roland Dreierd90f9b32012-07-05 22:39:34 -07001427#if IS_ENABLED(CONFIG_IPV6)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001428 else if (skb->protocol == htons(ETH_P_IPV6))
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001429 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001430#endif
1431 dev_kfree_skb_any(skb);
Roland Dreier943c2462008-09-30 10:36:21 -07001432
1433 netif_tx_lock_bh(dev);
1434 spin_lock_irqsave(&priv->lock, flags);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001435 }
Roland Dreier943c2462008-09-30 10:36:21 -07001436
1437 spin_unlock_irqrestore(&priv->lock, flags);
1438 netif_tx_unlock_bh(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001439}
1440
Roland Dreier2337f802007-10-23 19:57:54 -07001441void ipoib_cm_skb_too_long(struct net_device *dev, struct sk_buff *skb,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001442 unsigned int mtu)
1443{
1444 struct ipoib_dev_priv *priv = netdev_priv(dev);
1445 int e = skb_queue_empty(&priv->cm.skb_queue);
1446
Eric Dumazetadf30902009-06-02 05:19:30 +00001447 if (skb_dst(skb))
David S. Miller6700c272012-07-17 03:29:28 -07001448 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001449
1450 skb_queue_tail(&priv->cm.skb_queue, skb);
1451 if (e)
Doug Ledford0b395782015-02-21 19:27:03 -05001452 queue_work(priv->wq, &priv->cm.skb_task);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001453}
1454
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001455static void ipoib_cm_rx_reap(struct work_struct *work)
1456{
Roland Dreierefcd9972008-01-25 14:15:24 -08001457 ipoib_cm_free_rx_reap_list(container_of(work, struct ipoib_dev_priv,
1458 cm.rx_reap_task)->dev);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001459}
1460
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001461static void ipoib_cm_stale_task(struct work_struct *work)
1462{
1463 struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
1464 cm.stale_task.work);
1465 struct ipoib_cm_rx *p;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001466 int ret;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001467
Roland Dreier37aebbde2007-04-24 21:30:37 -07001468 spin_lock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001469 while (!list_empty(&priv->cm.passive_ids)) {
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001470 /* List is sorted by LRU, start from tail,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001471 * stop when we see a recently used entry */
1472 p = list_entry(priv->cm.passive_ids.prev, typeof(*p), list);
Michael S. Tsirkin60a596d2007-03-22 14:32:09 -07001473 if (time_before_eq(jiffies, p->jiffies + IPOIB_CM_RX_TIMEOUT))
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001474 break;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001475 list_move(&p->list, &priv->cm.rx_error_list);
1476 p->state = IPOIB_CM_RX_ERROR;
Roland Dreier37aebbde2007-04-24 21:30:37 -07001477 spin_unlock_irq(&priv->lock);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001478 ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
1479 if (ret)
1480 ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001481 spin_lock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001482 }
Michael S. Tsirkin7c5b9ef2007-05-14 07:26:51 +03001483
1484 if (!list_empty(&priv->cm.passive_ids))
Doug Ledford0b395782015-02-21 19:27:03 -05001485 queue_delayed_work(priv->wq,
Michael S. Tsirkin7c5b9ef2007-05-14 07:26:51 +03001486 &priv->cm.stale_task, IPOIB_CM_RX_DELAY);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001487 spin_unlock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001488}
1489
Roland Dreier2337f802007-10-23 19:57:54 -07001490static ssize_t show_mode(struct device *d, struct device_attribute *attr,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001491 char *buf)
1492{
1493 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(d));
1494
1495 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
1496 return sprintf(buf, "connected\n");
1497 else
1498 return sprintf(buf, "datagram\n");
1499}
1500
Or Gerlitz862096a2012-09-27 12:06:02 +00001501static ssize_t set_mode(struct device *d, struct device_attribute *attr,
1502 const char *buf, size_t count)
1503{
1504 struct net_device *dev = to_net_dev(d);
1505 int ret;
Erez Shitrit198b12f2016-06-04 15:15:20 +03001506 struct ipoib_dev_priv *priv = netdev_priv(dev);
1507
1508 if (test_bit(IPOIB_FLAG_GOING_DOWN, &priv->flags))
1509 return -EPERM;
Or Gerlitz862096a2012-09-27 12:06:02 +00001510
1511 if (!rtnl_trylock())
1512 return restart_syscall();
1513
1514 ret = ipoib_set_mode(dev, buf);
1515
Feras Daoud16260762016-12-28 14:47:23 +02001516 /* The assumption is that the function ipoib_set_mode returned
1517 * with the rtnl held by it, if not the value -EBUSY returned,
1518 * then no need to rtnl_unlock
1519 */
1520 if (ret != -EBUSY)
1521 rtnl_unlock();
Or Gerlitz862096a2012-09-27 12:06:02 +00001522
Feras Daoud16260762016-12-28 14:47:23 +02001523 return (!ret || ret == -EBUSY) ? count : ret;
Or Gerlitz862096a2012-09-27 12:06:02 +00001524}
1525
Roland Dreier551fd612007-02-16 13:57:33 -08001526static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO, show_mode, set_mode);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001527
1528int ipoib_cm_add_mode_attr(struct net_device *dev)
1529{
1530 return device_create_file(&dev->dev, &dev_attr_mode);
1531}
1532
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001533static void ipoib_cm_create_srq(struct net_device *dev, int max_sge)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001534{
1535 struct ipoib_dev_priv *priv = netdev_priv(dev);
1536 struct ib_srq_init_attr srq_init_attr = {
Sean Hefty96104ed2011-05-23 16:31:36 -07001537 .srq_type = IB_SRQT_BASIC,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001538 .attr = {
1539 .max_wr = ipoib_recvq_size,
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001540 .max_sge = max_sge
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001541 }
1542 };
Roland Dreier7b3687d2008-01-25 14:15:24 -08001543
1544 priv->cm.srq = ib_create_srq(priv->pd, &srq_init_attr);
1545 if (IS_ERR(priv->cm.srq)) {
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001546 if (PTR_ERR(priv->cm.srq) != -ENOSYS)
1547 printk(KERN_WARNING "%s: failed to allocate SRQ, error %ld\n",
1548 priv->ca->name, PTR_ERR(priv->cm.srq));
Roland Dreier7b3687d2008-01-25 14:15:24 -08001549 priv->cm.srq = NULL;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001550 return;
Roland Dreier7b3687d2008-01-25 14:15:24 -08001551 }
1552
Joe Perches948579c2010-11-05 03:07:36 +00001553 priv->cm.srq_ring = vzalloc(ipoib_recvq_size * sizeof *priv->cm.srq_ring);
Roland Dreier7b3687d2008-01-25 14:15:24 -08001554 if (!priv->cm.srq_ring) {
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001555 printk(KERN_WARNING "%s: failed to allocate CM SRQ ring (%d entries)\n",
Roland Dreier7b3687d2008-01-25 14:15:24 -08001556 priv->ca->name, ipoib_recvq_size);
1557 ib_destroy_srq(priv->cm.srq);
1558 priv->cm.srq = NULL;
David J. Wilderb1404062008-08-08 15:51:29 -07001559 return;
Roland Dreier7b3687d2008-01-25 14:15:24 -08001560 }
David J. Wilderb1404062008-08-08 15:51:29 -07001561
Roland Dreier7b3687d2008-01-25 14:15:24 -08001562}
1563
1564int ipoib_cm_dev_init(struct net_device *dev)
1565{
1566 struct ipoib_dev_priv *priv = netdev_priv(dev);
Or Gerlitz4a061b22015-12-18 10:59:46 +02001567 int max_srq_sge, i;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001568
1569 INIT_LIST_HEAD(&priv->cm.passive_ids);
1570 INIT_LIST_HEAD(&priv->cm.reap_list);
1571 INIT_LIST_HEAD(&priv->cm.start_list);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001572 INIT_LIST_HEAD(&priv->cm.rx_error_list);
1573 INIT_LIST_HEAD(&priv->cm.rx_flush_list);
1574 INIT_LIST_HEAD(&priv->cm.rx_drain_list);
1575 INIT_LIST_HEAD(&priv->cm.rx_reap_list);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001576 INIT_WORK(&priv->cm.start_task, ipoib_cm_tx_start);
1577 INIT_WORK(&priv->cm.reap_task, ipoib_cm_tx_reap);
1578 INIT_WORK(&priv->cm.skb_task, ipoib_cm_skb_reap);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001579 INIT_WORK(&priv->cm.rx_reap_task, ipoib_cm_rx_reap);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001580 INIT_DELAYED_WORK(&priv->cm.stale_task, ipoib_cm_stale_task);
1581
1582 skb_queue_head_init(&priv->cm.skb_queue);
1583
Or Gerlitz4a061b22015-12-18 10:59:46 +02001584 ipoib_dbg(priv, "max_srq_sge=%d\n", priv->ca->attrs.max_srq_sge);
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001585
Or Gerlitz4a061b22015-12-18 10:59:46 +02001586 max_srq_sge = min_t(int, IPOIB_CM_RX_SG, priv->ca->attrs.max_srq_sge);
1587 ipoib_cm_create_srq(dev, max_srq_sge);
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001588 if (ipoib_cm_has_srq(dev)) {
Or Gerlitz4a061b22015-12-18 10:59:46 +02001589 priv->cm.max_cm_mtu = max_srq_sge * PAGE_SIZE - 0x10;
1590 priv->cm.num_frags = max_srq_sge;
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001591 ipoib_dbg(priv, "max_cm_mtu = 0x%x, num_frags=%d\n",
1592 priv->cm.max_cm_mtu, priv->cm.num_frags);
1593 } else {
1594 priv->cm.max_cm_mtu = IPOIB_CM_MTU;
1595 priv->cm.num_frags = IPOIB_CM_RX_SG;
1596 }
1597
Roland Dreiera7d834c2008-07-14 23:48:47 -07001598 ipoib_cm_init_rx_wr(dev, &priv->cm.rx_wr, priv->cm.rx_sge);
Roland Dreier7b3687d2008-01-25 14:15:24 -08001599
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001600 if (ipoib_cm_has_srq(dev)) {
1601 for (i = 0; i < ipoib_recvq_size; ++i) {
1602 if (!ipoib_cm_alloc_rx_skb(dev, priv->cm.srq_ring, i,
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001603 priv->cm.num_frags - 1,
Tal Alon22252b42013-10-16 17:37:48 +03001604 priv->cm.srq_ring[i].mapping,
1605 GFP_KERNEL)) {
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001606 ipoib_warn(priv, "failed to allocate "
1607 "receive buffer %d\n", i);
1608 ipoib_cm_dev_cleanup(dev);
1609 return -ENOMEM;
1610 }
1611
1612 if (ipoib_cm_post_receive_srq(dev, i)) {
1613 ipoib_warn(priv, "ipoib_cm_post_receive_srq "
1614 "failed for buf %d\n", i);
1615 ipoib_cm_dev_cleanup(dev);
1616 return -EIO;
1617 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001618 }
1619 }
1620
1621 priv->dev->dev_addr[0] = IPOIB_FLAGS_RC;
1622 return 0;
1623}
1624
1625void ipoib_cm_dev_cleanup(struct net_device *dev)
1626{
1627 struct ipoib_dev_priv *priv = netdev_priv(dev);
Roland Dreier1efb6142008-01-25 14:15:24 -08001628 int ret;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001629
1630 if (!priv->cm.srq)
1631 return;
1632
1633 ipoib_dbg(priv, "Cleanup ipoib connected mode.\n");
1634
1635 ret = ib_destroy_srq(priv->cm.srq);
1636 if (ret)
1637 ipoib_warn(priv, "ib_destroy_srq failed: %d\n", ret);
1638
1639 priv->cm.srq = NULL;
1640 if (!priv->cm.srq_ring)
1641 return;
Roland Dreier1efb6142008-01-25 14:15:24 -08001642
1643 ipoib_cm_free_rx_ring(dev, priv->cm.srq_ring);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001644 priv->cm.srq_ring = NULL;
1645}