blob: 7cbcfdac6529cc5a93f43212a3d05744985f814c [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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010041#include <linux/sched/signal.h>
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020042
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -080043#include "ipoib.h"
44
45int ipoib_max_conn_qp = 128;
46
47module_param_named(max_nonsrq_conn_qp, ipoib_max_conn_qp, int, 0444);
48MODULE_PARM_DESC(max_nonsrq_conn_qp,
49 "Max number of connected-mode QPs per interface "
50 "(applied only if shared receive queue is not available)");
51
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020052#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
53static int data_debug_level;
54
55module_param_named(cm_data_debug_level, data_debug_level, int, 0644);
56MODULE_PARM_DESC(cm_data_debug_level,
57 "Enable data path debug tracing for connected mode if > 0");
58#endif
59
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020060#define IPOIB_CM_IETF_ID 0x1000000000000000ULL
61
62#define IPOIB_CM_RX_UPDATE_TIME (256 * HZ)
63#define IPOIB_CM_RX_TIMEOUT (2 * 256 * HZ)
64#define IPOIB_CM_RX_DELAY (3 * 256 * HZ)
65#define IPOIB_CM_RX_UPDATE_MASK (0x3)
66
Paolo Abenifc791b62016-10-13 18:26:56 +020067#define IPOIB_CM_RX_RESERVE (ALIGN(IPOIB_HARD_LEN, 16) - IPOIB_ENCAP_LEN)
68
Michael S. Tsirkin518b1642007-05-21 15:04:59 +030069static struct ib_qp_attr ipoib_cm_err_attr = {
70 .qp_state = IB_QPS_ERR
71};
72
Roland Dreier09f60f82007-10-26 13:44:25 -070073#define IPOIB_CM_RX_DRAIN_WRID 0xffffffff
Michael S. Tsirkin518b1642007-05-21 15:04:59 +030074
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +030075static struct ib_send_wr ipoib_cm_rx_drain_wr = {
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +030076 .opcode = IB_WR_SEND,
Michael S. Tsirkin518b1642007-05-21 15:04:59 +030077};
78
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020079static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id,
80 struct ib_cm_event *event);
81
Michael S. Tsirkin18120632007-02-20 20:17:55 +020082static void ipoib_cm_dma_unmap_rx(struct ipoib_dev_priv *priv, int frags,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020083 u64 mapping[IPOIB_CM_RX_SG])
84{
85 int i;
86
87 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
88
Michael S. Tsirkin18120632007-02-20 20:17:55 +020089 for (i = 0; i < frags; ++i)
Dotan Barak787adb92011-10-18 15:22:14 +020090 ib_dma_unmap_page(priv->ca, mapping[i + 1], PAGE_SIZE, DMA_FROM_DEVICE);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020091}
92
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -080093static int ipoib_cm_post_receive_srq(struct net_device *dev, int id)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020094{
Erez Shitritc1048af2017-04-10 11:22:29 +030095 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +020096 struct ib_recv_wr *bad_wr;
97 int i, ret;
98
Michael S. Tsirkin1b524962007-08-16 15:36:16 +030099 priv->cm.rx_wr.wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200100
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800101 for (i = 0; i < priv->cm.num_frags; ++i)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200102 priv->cm.rx_sge[i].addr = priv->cm.srq_ring[id].mapping[i];
103
104 ret = ib_post_srq_recv(priv->cm.srq, &priv->cm.rx_wr, &bad_wr);
105 if (unlikely(ret)) {
106 ipoib_warn(priv, "post srq failed for buf %d (%d)\n", id, ret);
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800107 ipoib_cm_dma_unmap_rx(priv, priv->cm.num_frags - 1,
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200108 priv->cm.srq_ring[id].mapping);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200109 dev_kfree_skb_any(priv->cm.srq_ring[id].skb);
110 priv->cm.srq_ring[id].skb = NULL;
111 }
112
113 return ret;
114}
115
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800116static int ipoib_cm_post_receive_nonsrq(struct net_device *dev,
Roland Dreiera7d834c2008-07-14 23:48:47 -0700117 struct ipoib_cm_rx *rx,
118 struct ib_recv_wr *wr,
119 struct ib_sge *sge, int id)
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800120{
Erez Shitritc1048af2017-04-10 11:22:29 +0300121 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800122 struct ib_recv_wr *bad_wr;
123 int i, ret;
124
Roland Dreiera7d834c2008-07-14 23:48:47 -0700125 wr->wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800126
127 for (i = 0; i < IPOIB_CM_RX_SG; ++i)
Roland Dreiera7d834c2008-07-14 23:48:47 -0700128 sge[i].addr = rx->rx_ring[id].mapping[i];
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800129
Roland Dreiera7d834c2008-07-14 23:48:47 -0700130 ret = ib_post_recv(rx->qp, wr, &bad_wr);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800131 if (unlikely(ret)) {
132 ipoib_warn(priv, "post recv failed for buf %d (%d)\n", id, ret);
133 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
134 rx->rx_ring[id].mapping);
135 dev_kfree_skb_any(rx->rx_ring[id].skb);
136 rx->rx_ring[id].skb = NULL;
137 }
138
139 return ret;
140}
141
142static struct sk_buff *ipoib_cm_alloc_rx_skb(struct net_device *dev,
143 struct ipoib_cm_rx_buf *rx_ring,
144 int id, int frags,
Tal Alon22252b42013-10-16 17:37:48 +0300145 u64 mapping[IPOIB_CM_RX_SG],
146 gfp_t gfp)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200147{
Erez Shitritc1048af2017-04-10 11:22:29 +0300148 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200149 struct sk_buff *skb;
150 int i;
151
Paolo Abenifc791b62016-10-13 18:26:56 +0200152 skb = dev_alloc_skb(ALIGN(IPOIB_CM_HEAD_SIZE + IPOIB_PSEUDO_LEN, 16));
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200153 if (unlikely(!skb))
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200154 return NULL;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200155
156 /*
Paolo Abenifc791b62016-10-13 18:26:56 +0200157 * IPoIB adds a IPOIB_ENCAP_LEN byte header, this will align the
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200158 * IP header to a multiple of 16.
159 */
Paolo Abenifc791b62016-10-13 18:26:56 +0200160 skb_reserve(skb, IPOIB_CM_RX_RESERVE);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200161
162 mapping[0] = ib_dma_map_single(priv->ca, skb->data, IPOIB_CM_HEAD_SIZE,
163 DMA_FROM_DEVICE);
164 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0]))) {
165 dev_kfree_skb_any(skb);
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200166 return NULL;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200167 }
168
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200169 for (i = 0; i < frags; i++) {
Tal Alon22252b42013-10-16 17:37:48 +0300170 struct page *page = alloc_page(gfp);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200171
172 if (!page)
173 goto partial_error;
174 skb_fill_page_desc(skb, i, page, 0, PAGE_SIZE);
175
Ian Campbell5581be32011-08-24 22:28:10 +0000176 mapping[i + 1] = ib_dma_map_page(priv->ca, page,
Michael S. Tsirkin6371ea32007-04-10 18:32:42 +0300177 0, PAGE_SIZE, DMA_FROM_DEVICE);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200178 if (unlikely(ib_dma_mapping_error(priv->ca, mapping[i + 1])))
179 goto partial_error;
180 }
181
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800182 rx_ring[id].skb = skb;
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200183 return skb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200184
185partial_error:
186
187 ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE);
188
Ralph Campbell841adfc2007-06-29 11:37:56 -0700189 for (; i > 0; --i)
Dotan Barak787adb92011-10-18 15:22:14 +0200190 ib_dma_unmap_page(priv->ca, mapping[i], PAGE_SIZE, DMA_FROM_DEVICE);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200191
Michael S. Tsirkin8a2e65f2007-02-16 00:16:13 +0200192 dev_kfree_skb_any(skb);
Michael S. Tsirkin18120632007-02-20 20:17:55 +0200193 return NULL;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200194}
195
Roland Dreier1efb6142008-01-25 14:15:24 -0800196static void ipoib_cm_free_rx_ring(struct net_device *dev,
197 struct ipoib_cm_rx_buf *rx_ring)
198{
Erez Shitritc1048af2017-04-10 11:22:29 +0300199 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Roland Dreier1efb6142008-01-25 14:15:24 -0800200 int i;
201
202 for (i = 0; i < ipoib_recvq_size; ++i)
203 if (rx_ring[i].skb) {
204 ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1,
205 rx_ring[i].mapping);
206 dev_kfree_skb_any(rx_ring[i].skb);
207 }
208
David J. Wilderb1404062008-08-08 15:51:29 -0700209 vfree(rx_ring);
Roland Dreier1efb6142008-01-25 14:15:24 -0800210}
211
Roland Dreier2337f802007-10-23 19:57:54 -0700212static void ipoib_cm_start_rx_drain(struct ipoib_dev_priv *priv)
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300213{
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300214 struct ib_send_wr *bad_wr;
215 struct ipoib_cm_rx *p;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300216
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300217 /* We only reserved 1 extra slot in CQ for drain WRs, so
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300218 * make sure we have at most 1 outstanding WR. */
219 if (list_empty(&priv->cm.rx_flush_list) ||
220 !list_empty(&priv->cm.rx_drain_list))
221 return;
222
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300223 /*
224 * QPs on flush list are error state. This way, a "flush
225 * error" WC will be immediately generated for each WR we post.
226 */
227 p = list_entry(priv->cm.rx_flush_list.next, typeof(*p), list);
Christoph Hellwig14d3a3b2015-12-11 11:53:03 -0800228 ipoib_cm_rx_drain_wr.wr_id = IPOIB_CM_RX_DRAIN_WRID;
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300229 if (ib_post_send(p->qp, &ipoib_cm_rx_drain_wr, &bad_wr))
230 ipoib_warn(priv, "failed to post drain wr\n");
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300231
232 list_splice_init(&priv->cm.rx_flush_list, &priv->cm.rx_drain_list);
233}
234
235static void ipoib_cm_rx_event_handler(struct ib_event *event, void *ctx)
236{
237 struct ipoib_cm_rx *p = ctx;
Erez Shitritc1048af2017-04-10 11:22:29 +0300238 struct ipoib_dev_priv *priv = ipoib_priv(p->dev);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300239 unsigned long flags;
240
241 if (event->event != IB_EVENT_QP_LAST_WQE_REACHED)
242 return;
243
244 spin_lock_irqsave(&priv->lock, flags);
245 list_move(&p->list, &priv->cm.rx_flush_list);
246 p->state = IPOIB_CM_RX_FLUSH;
247 ipoib_cm_start_rx_drain(priv);
248 spin_unlock_irqrestore(&priv->lock, flags);
249}
250
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200251static struct ib_qp *ipoib_cm_create_rx_qp(struct net_device *dev,
252 struct ipoib_cm_rx *p)
253{
Erez Shitritc1048af2017-04-10 11:22:29 +0300254 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200255 struct ib_qp_init_attr attr = {
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300256 .event_handler = ipoib_cm_rx_event_handler,
Eli Cohenf56bcd82008-04-29 13:46:53 -0700257 .send_cq = priv->recv_cq, /* For drain WR */
258 .recv_cq = priv->recv_cq,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200259 .srq = priv->cm.srq,
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300260 .cap.max_send_wr = 1, /* For drain WR */
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200261 .cap.max_send_sge = 1, /* FIXME: 0 Seems not to work */
262 .sq_sig_type = IB_SIGNAL_ALL_WR,
263 .qp_type = IB_QPT_RC,
264 .qp_context = p,
265 };
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800266
267 if (!ipoib_cm_has_srq(dev)) {
268 attr.cap.max_recv_wr = ipoib_recvq_size;
269 attr.cap.max_recv_sge = IPOIB_CM_RX_SG;
270 }
271
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200272 return ib_create_qp(priv->pd, &attr);
273}
274
275static int ipoib_cm_modify_rx_qp(struct net_device *dev,
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800276 struct ib_cm_id *cm_id, struct ib_qp *qp,
277 unsigned psn)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200278{
Erez Shitritc1048af2017-04-10 11:22:29 +0300279 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200280 struct ib_qp_attr qp_attr;
281 int qp_attr_mask, ret;
282
283 qp_attr.qp_state = IB_QPS_INIT;
284 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
285 if (ret) {
286 ipoib_warn(priv, "failed to init QP attr for INIT: %d\n", ret);
287 return ret;
288 }
289 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
290 if (ret) {
291 ipoib_warn(priv, "failed to modify QP to INIT: %d\n", ret);
292 return ret;
293 }
294 qp_attr.qp_state = IB_QPS_RTR;
295 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
296 if (ret) {
297 ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
298 return ret;
299 }
300 qp_attr.rq_psn = psn;
301 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
302 if (ret) {
303 ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
304 return ret;
305 }
Michael S. Tsirkinec56dc02007-05-28 14:37:27 +0300306
307 /*
308 * Current Mellanox HCA firmware won't generate completions
309 * with error for drain WRs unless the QP has been moved to
310 * RTS first. This work-around leaves a window where a QP has
311 * moved to error asynchronously, but this will eventually get
312 * fixed in firmware, so let's not error out if modify QP
313 * fails.
314 */
315 qp_attr.qp_state = IB_QPS_RTS;
316 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
317 if (ret) {
318 ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
319 return 0;
320 }
321 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
322 if (ret) {
323 ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
324 return 0;
325 }
326
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200327 return 0;
328}
329
Roland Dreiera7d834c2008-07-14 23:48:47 -0700330static void ipoib_cm_init_rx_wr(struct net_device *dev,
331 struct ib_recv_wr *wr,
332 struct ib_sge *sge)
333{
Erez Shitritc1048af2017-04-10 11:22:29 +0300334 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Roland Dreiera7d834c2008-07-14 23:48:47 -0700335 int i;
336
337 for (i = 0; i < priv->cm.num_frags; ++i)
Jason Gunthorpe77b1f992015-07-30 17:22:17 -0600338 sge[i].lkey = priv->pd->local_dma_lkey;
Roland Dreiera7d834c2008-07-14 23:48:47 -0700339
340 sge[0].length = IPOIB_CM_HEAD_SIZE;
341 for (i = 1; i < priv->cm.num_frags; ++i)
342 sge[i].length = PAGE_SIZE;
343
344 wr->next = NULL;
Roland Dreiere0819812008-07-30 07:21:46 -0700345 wr->sg_list = sge;
Roland Dreiera7d834c2008-07-14 23:48:47 -0700346 wr->num_sge = priv->cm.num_frags;
347}
348
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800349static int ipoib_cm_nonsrq_init_rx(struct net_device *dev, struct ib_cm_id *cm_id,
350 struct ipoib_cm_rx *rx)
351{
Erez Shitritc1048af2017-04-10 11:22:29 +0300352 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Roland Dreiera7d834c2008-07-14 23:48:47 -0700353 struct {
354 struct ib_recv_wr wr;
355 struct ib_sge sge[IPOIB_CM_RX_SG];
356 } *t;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800357 int ret;
358 int i;
359
Joe Perches948579c2010-11-05 03:07:36 +0000360 rx->rx_ring = vzalloc(ipoib_recvq_size * sizeof *rx->rx_ring);
Leon Romanovsky74226642016-11-03 16:44:25 +0200361 if (!rx->rx_ring)
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800362 return -ENOMEM;
David J. Wilderb1404062008-08-08 15:51:29 -0700363
Roland Dreiera7d834c2008-07-14 23:48:47 -0700364 t = kmalloc(sizeof *t, GFP_KERNEL);
365 if (!t) {
366 ret = -ENOMEM;
Zhu Yanjunc5e8f57b02017-02-10 01:48:42 -0500367 goto err_free_1;
Roland Dreiera7d834c2008-07-14 23:48:47 -0700368 }
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);
Zhu Yanjunc5e8f57b02017-02-10 01:48:42 -0500414
415err_free_1:
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{
Erez Shitritc1048af2017-04-10 11:22:29 +0300425 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200426 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;
Erez Shitritc1048af2017-04-10 11:22:29 +0300445 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200446 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;
Erez Shitritc1048af2017-04-10 11:22:29 +0300518 priv = ipoib_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{
Erez Shitritc1048af2017-04-10 11:22:29 +0300562 struct ipoib_dev_priv *priv = ipoib_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{
Erez Shitritc1048af2017-04-10 11:22:29 +0300711 struct ipoib_dev_priv *priv = ipoib_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. Tsirkin77d8e1ef2007-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{
Erez Shitritc1048af2017-04-10 11:22:29 +0300789 struct ipoib_dev_priv *priv = ipoib_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
Feras Daoud13ee4292016-12-28 14:47:26 +0200826 if (wc->status != IB_WC_RNR_RETRY_EXC_ERR)
827 ipoib_warn(priv, "failed cm send event (status=%d, wrid=%d vend_err %x)\n",
828 wc->status, wr_id, wc->vendor_err);
829 else
830 ipoib_dbg(priv, "failed cm send event (status=%d, wrid=%d vend_err %x)\n",
831 wc->status, wr_id, wc->vendor_err);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200832
Roland Dreier943c2462008-09-30 10:36:21 -0700833 spin_lock_irqsave(&priv->lock, flags);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200834 neigh = tx->neigh;
835
836 if (neigh) {
837 neigh->cm = NULL;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000838 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200839
840 tx->neigh = NULL;
841 }
842
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200843 if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) {
844 list_move(&tx->list, &priv->cm.reap_list);
Doug Ledford0b395782015-02-21 19:27:03 -0500845 queue_work(priv->wq, &priv->cm.reap_task);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200846 }
847
848 clear_bit(IPOIB_FLAG_OPER_UP, &tx->flags);
849
Roland Dreier943c2462008-09-30 10:36:21 -0700850 spin_unlock_irqrestore(&priv->lock, flags);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200851 }
852
Roland Dreier943c2462008-09-30 10:36:21 -0700853 netif_tx_unlock(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200854}
855
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200856int ipoib_cm_dev_open(struct net_device *dev)
857{
Erez Shitritc1048af2017-04-10 11:22:29 +0300858 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200859 int ret;
860
861 if (!IPOIB_CM_SUPPORTED(dev->dev_addr))
862 return 0;
863
864 priv->cm.id = ib_create_cm_id(priv->ca, ipoib_cm_rx_handler, dev);
865 if (IS_ERR(priv->cm.id)) {
866 printk(KERN_WARNING "%s: failed to create CM ID\n", priv->ca->name);
Michael S. Tsirkin347fcfb2007-04-30 17:30:28 -0700867 ret = PTR_ERR(priv->cm.id);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300868 goto err_cm;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200869 }
870
871 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 +0300872 0);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200873 if (ret) {
874 printk(KERN_WARNING "%s: failed to listen on ID 0x%llx\n", priv->ca->name,
875 IPOIB_CM_IETF_ID | priv->qp->qp_num);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300876 goto err_listen;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200877 }
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300878
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200879 return 0;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300880
881err_listen:
882 ib_destroy_cm_id(priv->cm.id);
883err_cm:
884 priv->cm.id = NULL;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300885 return ret;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200886}
887
Roland Dreierefcd9972008-01-25 14:15:24 -0800888static void ipoib_cm_free_rx_reap_list(struct net_device *dev)
889{
Erez Shitritc1048af2017-04-10 11:22:29 +0300890 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Roland Dreierefcd9972008-01-25 14:15:24 -0800891 struct ipoib_cm_rx *rx, *n;
892 LIST_HEAD(list);
893
894 spin_lock_irq(&priv->lock);
895 list_splice_init(&priv->cm.rx_reap_list, &list);
896 spin_unlock_irq(&priv->lock);
897
898 list_for_each_entry_safe(rx, n, &list, list) {
899 ib_destroy_cm_id(rx->id);
900 ib_destroy_qp(rx->qp);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -0800901 if (!ipoib_cm_has_srq(dev)) {
902 ipoib_cm_free_rx_ring(priv->dev, rx->rx_ring);
903 spin_lock_irq(&priv->lock);
904 --priv->cm.nonsrq_conn_qp;
905 spin_unlock_irq(&priv->lock);
906 }
Roland Dreierefcd9972008-01-25 14:15:24 -0800907 kfree(rx);
908 }
909}
910
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200911void ipoib_cm_dev_stop(struct net_device *dev)
912{
Erez Shitritc1048af2017-04-10 11:22:29 +0300913 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Roland Dreierefcd9972008-01-25 14:15:24 -0800914 struct ipoib_cm_rx *p;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300915 unsigned long begin;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300916 int ret;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200917
Michael S. Tsirkin347fcfb2007-04-30 17:30:28 -0700918 if (!IPOIB_CM_SUPPORTED(dev->dev_addr) || !priv->cm.id)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200919 return;
920
921 ib_destroy_cm_id(priv->cm.id);
Michael S. Tsirkin347fcfb2007-04-30 17:30:28 -0700922 priv->cm.id = NULL;
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300923
Roland Dreier37aebbde2007-04-24 21:30:37 -0700924 spin_lock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200925 while (!list_empty(&priv->cm.passive_ids)) {
926 p = list_entry(priv->cm.passive_ids.next, typeof(*p), list);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300927 list_move(&p->list, &priv->cm.rx_error_list);
928 p->state = IPOIB_CM_RX_ERROR;
Roland Dreier37aebbde2007-04-24 21:30:37 -0700929 spin_unlock_irq(&priv->lock);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300930 ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE);
931 if (ret)
932 ipoib_warn(priv, "unable to move qp to error state: %d\n", ret);
933 spin_lock_irq(&priv->lock);
934 }
935
936 /* Wait for all RX to be drained */
937 begin = jiffies;
938
939 while (!list_empty(&priv->cm.rx_error_list) ||
940 !list_empty(&priv->cm.rx_flush_list) ||
941 !list_empty(&priv->cm.rx_drain_list)) {
Michael S. Tsirkin8fd357a2007-05-24 14:02:39 -0700942 if (time_after(jiffies, begin + 5 * HZ)) {
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300943 ipoib_warn(priv, "RX drain timing out\n");
944
945 /*
946 * assume the HW is wedged and just free up everything.
947 */
Pradeep Satyanarayanaec229e52008-02-12 15:00:59 -0800948 list_splice_init(&priv->cm.rx_flush_list,
949 &priv->cm.rx_reap_list);
950 list_splice_init(&priv->cm.rx_error_list,
951 &priv->cm.rx_reap_list);
952 list_splice_init(&priv->cm.rx_drain_list,
953 &priv->cm.rx_reap_list);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300954 break;
955 }
956 spin_unlock_irq(&priv->lock);
957 msleep(1);
Michael S. Tsirkin2dfbfc32007-05-24 18:32:46 +0300958 ipoib_drain_cq(dev);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300959 spin_lock_irq(&priv->lock);
960 }
961
Michael S. Tsirkin518b1642007-05-21 15:04:59 +0300962 spin_unlock_irq(&priv->lock);
963
Roland Dreierefcd9972008-01-25 14:15:24 -0800964 ipoib_cm_free_rx_reap_list(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200965
966 cancel_delayed_work(&priv->cm.stale_task);
967}
968
969static int ipoib_cm_rep_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
970{
971 struct ipoib_cm_tx *p = cm_id->context;
Erez Shitritc1048af2017-04-10 11:22:29 +0300972 struct ipoib_dev_priv *priv = ipoib_priv(p->dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200973 struct ipoib_cm_data *data = event->private_data;
974 struct sk_buff_head skqueue;
975 struct ib_qp_attr qp_attr;
976 int qp_attr_mask, ret;
977 struct sk_buff *skb;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200978
979 p->mtu = be32_to_cpu(data->mtu);
980
Michael S. Tsirkin82c3aca2007-06-20 19:22:15 +0300981 if (p->mtu <= IPOIB_ENCAP_LEN) {
982 ipoib_warn(priv, "Rejecting connection: mtu %d <= %d\n",
983 p->mtu, IPOIB_ENCAP_LEN);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200984 return -EINVAL;
985 }
986
987 qp_attr.qp_state = IB_QPS_RTR;
988 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
989 if (ret) {
990 ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret);
991 return ret;
992 }
993
994 qp_attr.rq_psn = 0 /* FIXME */;
995 ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
996 if (ret) {
997 ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret);
998 return ret;
999 }
1000
1001 qp_attr.qp_state = IB_QPS_RTS;
1002 ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask);
1003 if (ret) {
1004 ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret);
1005 return ret;
1006 }
1007 ret = ib_modify_qp(p->qp, &qp_attr, qp_attr_mask);
1008 if (ret) {
1009 ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret);
1010 return ret;
1011 }
1012
1013 skb_queue_head_init(&skqueue);
1014
Roland Dreier37aebbde2007-04-24 21:30:37 -07001015 spin_lock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001016 set_bit(IPOIB_FLAG_OPER_UP, &p->flags);
1017 if (p->neigh)
1018 while ((skb = __skb_dequeue(&p->neigh->queue)))
1019 __skb_queue_tail(&skqueue, skb);
Roland Dreier37aebbde2007-04-24 21:30:37 -07001020 spin_unlock_irq(&priv->lock);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001021
1022 while ((skb = __skb_dequeue(&skqueue))) {
1023 skb->dev = p->dev;
Feras Daoudd32b9a82016-12-28 14:47:25 +02001024 ret = dev_queue_xmit(skb);
1025 if (ret)
1026 ipoib_warn(priv, "%s:dev_queue_xmit failed to re-queue packet, ret:%d\n",
1027 __func__, ret);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001028 }
1029
1030 ret = ib_send_cm_rtu(cm_id, NULL, 0);
1031 if (ret) {
1032 ipoib_warn(priv, "failed to send RTU: %d\n", ret);
1033 return ret;
1034 }
1035 return 0;
1036}
1037
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001038static 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 +02001039{
Erez Shitritc1048af2017-04-10 11:22:29 +03001040 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Dotan Barakede6bc02007-10-07 09:30:48 +02001041 struct ib_qp_init_attr attr = {
Eli Cohenf56bcd82008-04-29 13:46:53 -07001042 .send_cq = priv->recv_cq,
1043 .recv_cq = priv->recv_cq,
Dotan Barakede6bc02007-10-07 09:30:48 +02001044 .srq = priv->cm.srq,
1045 .cap.max_send_wr = ipoib_sendq_size,
1046 .cap.max_send_sge = 1,
1047 .sq_sig_type = IB_SIGNAL_ALL_WR,
1048 .qp_type = IB_QPT_RC,
Or Gerlitz09b93082014-05-11 15:15:11 +03001049 .qp_context = tx,
1050 .create_flags = IB_QP_CREATE_USE_GFP_NOIO
Roland Dreier2337f802007-10-23 19:57:54 -07001051 };
Dotan Barakede6bc02007-10-07 09:30:48 +02001052
Or Gerlitz09b93082014-05-11 15:15:11 +03001053 struct ib_qp *tx_qp;
1054
Yuval Shaiac4268772015-07-12 01:24:09 -07001055 if (dev->features & NETIF_F_SG)
Hans Westgaard Ry78a50a52016-03-02 13:44:28 +01001056 attr.cap.max_send_sge =
1057 min_t(u32, priv->ca->attrs.max_sge, MAX_SKB_FRAGS + 1);
Yuval Shaiac4268772015-07-12 01:24:09 -07001058
Or Gerlitz09b93082014-05-11 15:15:11 +03001059 tx_qp = ib_create_qp(priv->pd, &attr);
1060 if (PTR_ERR(tx_qp) == -EINVAL) {
Or Gerlitz09b93082014-05-11 15:15:11 +03001061 attr.create_flags &= ~IB_QP_CREATE_USE_GFP_NOIO;
1062 tx_qp = ib_create_qp(priv->pd, &attr);
1063 }
Hans Westgaard Ry78a50a52016-03-02 13:44:28 +01001064 tx->max_send_sge = attr.cap.max_send_sge;
Or Gerlitz09b93082014-05-11 15:15:11 +03001065 return tx_qp;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001066}
1067
1068static int ipoib_cm_send_req(struct net_device *dev,
1069 struct ib_cm_id *id, struct ib_qp *qp,
1070 u32 qpn,
Dasaratharaman Chandramoulic2f8fc42017-04-27 19:05:58 -04001071 struct sa_path_rec *pathrec)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001072{
Erez Shitritc1048af2017-04-10 11:22:29 +03001073 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001074 struct ipoib_cm_data data = {};
1075 struct ib_cm_req_param req = {};
1076
1077 data.qpn = cpu_to_be32(priv->qp->qp_num);
1078 data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE);
1079
Roland Dreier2337f802007-10-23 19:57:54 -07001080 req.primary_path = pathrec;
1081 req.alternate_path = NULL;
1082 req.service_id = cpu_to_be64(IPOIB_CM_IETF_ID | qpn);
1083 req.qp_num = qp->qp_num;
1084 req.qp_type = qp->qp_type;
1085 req.private_data = &data;
1086 req.private_data_len = sizeof data;
1087 req.flow_control = 0;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001088
Roland Dreier2337f802007-10-23 19:57:54 -07001089 req.starting_psn = 0; /* FIXME */
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001090
1091 /*
1092 * Pick some arbitrary defaults here; we could make these
1093 * module parameters if anyone cared about setting them.
1094 */
Roland Dreier2337f802007-10-23 19:57:54 -07001095 req.responder_resources = 4;
1096 req.remote_cm_response_timeout = 20;
1097 req.local_cm_response_timeout = 20;
1098 req.retry_count = 0; /* RFC draft warns against retries */
1099 req.rnr_retry_count = 0; /* RFC draft warns against retries */
1100 req.max_cm_retries = 15;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001101 req.srq = ipoib_cm_has_srq(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001102 return ib_send_cm_req(id, &req);
1103}
1104
1105static int ipoib_cm_modify_tx_init(struct net_device *dev,
1106 struct ib_cm_id *cm_id, struct ib_qp *qp)
1107{
Erez Shitritc1048af2017-04-10 11:22:29 +03001108 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001109 struct ib_qp_attr qp_attr;
1110 int qp_attr_mask, ret;
Roland Dreier9fdd5e52008-04-16 21:09:35 -07001111 ret = ib_find_pkey(priv->ca, priv->port, priv->pkey, &qp_attr.pkey_index);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001112 if (ret) {
Roland Dreier9fdd5e52008-04-16 21:09:35 -07001113 ipoib_warn(priv, "pkey 0x%x not found: %d\n", priv->pkey, ret);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001114 return ret;
1115 }
1116
1117 qp_attr.qp_state = IB_QPS_INIT;
1118 qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE;
1119 qp_attr.port_num = priv->port;
1120 qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
1121
1122 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
1123 if (ret) {
1124 ipoib_warn(priv, "failed to modify tx QP to INIT: %d\n", ret);
1125 return ret;
1126 }
1127 return 0;
1128}
1129
1130static int ipoib_cm_tx_init(struct ipoib_cm_tx *p, u32 qpn,
Dasaratharaman Chandramoulic2f8fc42017-04-27 19:05:58 -04001131 struct sa_path_rec *pathrec)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001132{
Erez Shitritc1048af2017-04-10 11:22:29 +03001133 struct ipoib_dev_priv *priv = ipoib_priv(p->dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001134 int ret;
1135
Or Gerlitz09b93082014-05-11 15:15:11 +03001136 p->tx_ring = __vmalloc(ipoib_sendq_size * sizeof *p->tx_ring,
1137 GFP_NOIO, PAGE_KERNEL);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001138 if (!p->tx_ring) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001139 ret = -ENOMEM;
1140 goto err_tx;
1141 }
Or Gerlitz09b93082014-05-11 15:15:11 +03001142 memset(p->tx_ring, 0, ipoib_sendq_size * sizeof *p->tx_ring);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001143
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001144 p->qp = ipoib_cm_create_tx_qp(p->dev, p);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001145 if (IS_ERR(p->qp)) {
1146 ret = PTR_ERR(p->qp);
1147 ipoib_warn(priv, "failed to allocate tx qp: %d\n", ret);
1148 goto err_qp;
1149 }
1150
1151 p->id = ib_create_cm_id(priv->ca, ipoib_cm_tx_handler, p);
1152 if (IS_ERR(p->id)) {
1153 ret = PTR_ERR(p->id);
1154 ipoib_warn(priv, "failed to create tx cm id: %d\n", ret);
1155 goto err_id;
1156 }
1157
1158 ret = ipoib_cm_modify_tx_init(p->dev, p->id, p->qp);
1159 if (ret) {
1160 ipoib_warn(priv, "failed to modify tx qp to rtr: %d\n", ret);
Zhu Yanjun23536df2017-02-14 03:43:12 -05001161 goto err_modify_send;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001162 }
1163
1164 ret = ipoib_cm_send_req(p->dev, p->id, p->qp, qpn, pathrec);
1165 if (ret) {
1166 ipoib_warn(priv, "failed to send cm req: %d\n", ret);
Zhu Yanjun23536df2017-02-14 03:43:12 -05001167 goto err_modify_send;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001168 }
1169
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001170 ipoib_dbg(priv, "Request connection 0x%x for gid %pI6 qpn 0x%x\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -07001171 p->qp->qp_num, pathrec->dgid.raw, qpn);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001172
1173 return 0;
1174
Zhu Yanjun23536df2017-02-14 03:43:12 -05001175err_modify_send:
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001176 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{
Erez Shitritc1048af2017-04-10 11:22:29 +03001189 struct ipoib_dev_priv *priv = ipoib_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;
Erez Shitritc1048af2017-04-10 11:22:29 +03001239 struct ipoib_dev_priv *priv = ipoib_priv(tx->dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001240 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{
Erez Shitritc1048af2017-04-10 11:22:29 +03001290 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001291 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{
Erez Shitritc1048af2017-04-10 11:22:29 +03001309 struct ipoib_dev_priv *priv = ipoib_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
Dasaratharaman Chandramoulic2f8fc42017-04-27 19:05:58 -04001335 struct sa_path_rec pathrec;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001336 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 Daoud27d41d22016-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{
Erez Shitritc1048af2017-04-10 11:22:29 +03001444 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001445 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{
Erez Shitritc1048af2017-04-10 11:22:29 +03001493 struct net_device *dev = to_net_dev(d);
1494 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001495
1496 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
1497 return sprintf(buf, "connected\n");
1498 else
1499 return sprintf(buf, "datagram\n");
1500}
1501
Or Gerlitz862096a2012-09-27 12:06:02 +00001502static ssize_t set_mode(struct device *d, struct device_attribute *attr,
1503 const char *buf, size_t count)
1504{
1505 struct net_device *dev = to_net_dev(d);
1506 int ret;
Erez Shitritc1048af2017-04-10 11:22:29 +03001507 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Erez Shitrit198b12f2016-06-04 15:15:20 +03001508
1509 if (test_bit(IPOIB_FLAG_GOING_DOWN, &priv->flags))
1510 return -EPERM;
Or Gerlitz862096a2012-09-27 12:06:02 +00001511
1512 if (!rtnl_trylock())
1513 return restart_syscall();
1514
1515 ret = ipoib_set_mode(dev, buf);
1516
Feras Daoud0a0007f2016-12-28 14:47:23 +02001517 /* The assumption is that the function ipoib_set_mode returned
1518 * with the rtnl held by it, if not the value -EBUSY returned,
1519 * then no need to rtnl_unlock
1520 */
1521 if (ret != -EBUSY)
1522 rtnl_unlock();
Or Gerlitz862096a2012-09-27 12:06:02 +00001523
Feras Daoud0a0007f2016-12-28 14:47:23 +02001524 return (!ret || ret == -EBUSY) ? count : ret;
Or Gerlitz862096a2012-09-27 12:06:02 +00001525}
1526
Roland Dreier551fd612007-02-16 13:57:33 -08001527static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO, show_mode, set_mode);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001528
1529int ipoib_cm_add_mode_attr(struct net_device *dev)
1530{
1531 return device_create_file(&dev->dev, &dev_attr_mode);
1532}
1533
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001534static void ipoib_cm_create_srq(struct net_device *dev, int max_sge)
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001535{
Erez Shitritc1048af2017-04-10 11:22:29 +03001536 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001537 struct ib_srq_init_attr srq_init_attr = {
Sean Hefty96104ed2011-05-23 16:31:36 -07001538 .srq_type = IB_SRQT_BASIC,
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001539 .attr = {
1540 .max_wr = ipoib_recvq_size,
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001541 .max_sge = max_sge
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001542 }
1543 };
Roland Dreier7b3687d2008-01-25 14:15:24 -08001544
1545 priv->cm.srq = ib_create_srq(priv->pd, &srq_init_attr);
1546 if (IS_ERR(priv->cm.srq)) {
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001547 if (PTR_ERR(priv->cm.srq) != -ENOSYS)
1548 printk(KERN_WARNING "%s: failed to allocate SRQ, error %ld\n",
1549 priv->ca->name, PTR_ERR(priv->cm.srq));
Roland Dreier7b3687d2008-01-25 14:15:24 -08001550 priv->cm.srq = NULL;
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001551 return;
Roland Dreier7b3687d2008-01-25 14:15:24 -08001552 }
1553
Joe Perches948579c2010-11-05 03:07:36 +00001554 priv->cm.srq_ring = vzalloc(ipoib_recvq_size * sizeof *priv->cm.srq_ring);
Roland Dreier7b3687d2008-01-25 14:15:24 -08001555 if (!priv->cm.srq_ring) {
Roland Dreier7b3687d2008-01-25 14:15:24 -08001556 ib_destroy_srq(priv->cm.srq);
1557 priv->cm.srq = NULL;
David J. Wilderb1404062008-08-08 15:51:29 -07001558 return;
Roland Dreier7b3687d2008-01-25 14:15:24 -08001559 }
David J. Wilderb1404062008-08-08 15:51:29 -07001560
Roland Dreier7b3687d2008-01-25 14:15:24 -08001561}
1562
1563int ipoib_cm_dev_init(struct net_device *dev)
1564{
Erez Shitritc1048af2017-04-10 11:22:29 +03001565 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Or Gerlitz4a061b22015-12-18 10:59:46 +02001566 int max_srq_sge, i;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001567
1568 INIT_LIST_HEAD(&priv->cm.passive_ids);
1569 INIT_LIST_HEAD(&priv->cm.reap_list);
1570 INIT_LIST_HEAD(&priv->cm.start_list);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001571 INIT_LIST_HEAD(&priv->cm.rx_error_list);
1572 INIT_LIST_HEAD(&priv->cm.rx_flush_list);
1573 INIT_LIST_HEAD(&priv->cm.rx_drain_list);
1574 INIT_LIST_HEAD(&priv->cm.rx_reap_list);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001575 INIT_WORK(&priv->cm.start_task, ipoib_cm_tx_start);
1576 INIT_WORK(&priv->cm.reap_task, ipoib_cm_tx_reap);
1577 INIT_WORK(&priv->cm.skb_task, ipoib_cm_skb_reap);
Michael S. Tsirkin518b1642007-05-21 15:04:59 +03001578 INIT_WORK(&priv->cm.rx_reap_task, ipoib_cm_rx_reap);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001579 INIT_DELAYED_WORK(&priv->cm.stale_task, ipoib_cm_stale_task);
1580
1581 skb_queue_head_init(&priv->cm.skb_queue);
1582
Or Gerlitz4a061b22015-12-18 10:59:46 +02001583 ipoib_dbg(priv, "max_srq_sge=%d\n", priv->ca->attrs.max_srq_sge);
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001584
Or Gerlitz4a061b22015-12-18 10:59:46 +02001585 max_srq_sge = min_t(int, IPOIB_CM_RX_SG, priv->ca->attrs.max_srq_sge);
1586 ipoib_cm_create_srq(dev, max_srq_sge);
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001587 if (ipoib_cm_has_srq(dev)) {
Or Gerlitz4a061b22015-12-18 10:59:46 +02001588 priv->cm.max_cm_mtu = max_srq_sge * PAGE_SIZE - 0x10;
1589 priv->cm.num_frags = max_srq_sge;
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001590 ipoib_dbg(priv, "max_cm_mtu = 0x%x, num_frags=%d\n",
1591 priv->cm.max_cm_mtu, priv->cm.num_frags);
1592 } else {
1593 priv->cm.max_cm_mtu = IPOIB_CM_MTU;
1594 priv->cm.num_frags = IPOIB_CM_RX_SG;
1595 }
1596
Roland Dreiera7d834c2008-07-14 23:48:47 -07001597 ipoib_cm_init_rx_wr(dev, &priv->cm.rx_wr, priv->cm.rx_sge);
Roland Dreier7b3687d2008-01-25 14:15:24 -08001598
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001599 if (ipoib_cm_has_srq(dev)) {
1600 for (i = 0; i < ipoib_recvq_size; ++i) {
1601 if (!ipoib_cm_alloc_rx_skb(dev, priv->cm.srq_ring, i,
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -08001602 priv->cm.num_frags - 1,
Tal Alon22252b42013-10-16 17:37:48 +03001603 priv->cm.srq_ring[i].mapping,
1604 GFP_KERNEL)) {
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001605 ipoib_warn(priv, "failed to allocate "
1606 "receive buffer %d\n", i);
1607 ipoib_cm_dev_cleanup(dev);
1608 return -ENOMEM;
1609 }
1610
1611 if (ipoib_cm_post_receive_srq(dev, i)) {
1612 ipoib_warn(priv, "ipoib_cm_post_receive_srq "
1613 "failed for buf %d\n", i);
1614 ipoib_cm_dev_cleanup(dev);
1615 return -EIO;
1616 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001617 }
1618 }
1619
1620 priv->dev->dev_addr[0] = IPOIB_FLAGS_RC;
1621 return 0;
1622}
1623
1624void ipoib_cm_dev_cleanup(struct net_device *dev)
1625{
Erez Shitritc1048af2017-04-10 11:22:29 +03001626 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Roland Dreier1efb6142008-01-25 14:15:24 -08001627 int ret;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001628
1629 if (!priv->cm.srq)
1630 return;
1631
1632 ipoib_dbg(priv, "Cleanup ipoib connected mode.\n");
1633
1634 ret = ib_destroy_srq(priv->cm.srq);
1635 if (ret)
1636 ipoib_warn(priv, "ib_destroy_srq failed: %d\n", ret);
1637
1638 priv->cm.srq = NULL;
1639 if (!priv->cm.srq_ring)
1640 return;
Roland Dreier1efb6142008-01-25 14:15:24 -08001641
1642 ipoib_cm_free_rx_ring(dev, priv->cm.srq_ring);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001643 priv->cm.srq_ring = NULL;
1644}