]>
Commit | Line | Data |
---|---|---|
839fcaba MT |
1 | /* |
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. | |
839fcaba MT |
31 | */ |
32 | ||
33 | #include <rdma/ib_cm.h> | |
839fcaba MT |
34 | #include <net/dst.h> |
35 | #include <net/icmp.h> | |
36 | #include <linux/icmpv6.h> | |
518b1646 | 37 | #include <linux/delay.h> |
5a0e3ad6 | 38 | #include <linux/slab.h> |
10313cbb | 39 | #include <linux/vmalloc.h> |
fec14d2f | 40 | #include <linux/moduleparam.h> |
174cd4b1 | 41 | #include <linux/sched/signal.h> |
d83187dd | 42 | #include <linux/sched/mm.h> |
839fcaba | 43 | |
68e995a2 PS |
44 | #include "ipoib.h" |
45 | ||
46 | int ipoib_max_conn_qp = 128; | |
47 | ||
48 | module_param_named(max_nonsrq_conn_qp, ipoib_max_conn_qp, int, 0444); | |
49 | MODULE_PARM_DESC(max_nonsrq_conn_qp, | |
50 | "Max number of connected-mode QPs per interface " | |
51 | "(applied only if shared receive queue is not available)"); | |
52 | ||
839fcaba MT |
53 | #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA |
54 | static int data_debug_level; | |
55 | ||
56 | module_param_named(cm_data_debug_level, data_debug_level, int, 0644); | |
57 | MODULE_PARM_DESC(cm_data_debug_level, | |
58 | "Enable data path debug tracing for connected mode if > 0"); | |
59 | #endif | |
60 | ||
839fcaba MT |
61 | #define IPOIB_CM_IETF_ID 0x1000000000000000ULL |
62 | ||
63 | #define IPOIB_CM_RX_UPDATE_TIME (256 * HZ) | |
64 | #define IPOIB_CM_RX_TIMEOUT (2 * 256 * HZ) | |
65 | #define IPOIB_CM_RX_DELAY (3 * 256 * HZ) | |
66 | #define IPOIB_CM_RX_UPDATE_MASK (0x3) | |
67 | ||
fc791b63 PA |
68 | #define IPOIB_CM_RX_RESERVE (ALIGN(IPOIB_HARD_LEN, 16) - IPOIB_ENCAP_LEN) |
69 | ||
518b1646 MT |
70 | static struct ib_qp_attr ipoib_cm_err_attr = { |
71 | .qp_state = IB_QPS_ERR | |
72 | }; | |
73 | ||
09f60f8f | 74 | #define IPOIB_CM_RX_DRAIN_WRID 0xffffffff |
518b1646 | 75 | |
ec56dc0b | 76 | static struct ib_send_wr ipoib_cm_rx_drain_wr = { |
ec56dc0b | 77 | .opcode = IB_WR_SEND, |
518b1646 MT |
78 | }; |
79 | ||
839fcaba MT |
80 | static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id, |
81 | struct ib_cm_event *event); | |
82 | ||
1812063b | 83 | static void ipoib_cm_dma_unmap_rx(struct ipoib_dev_priv *priv, int frags, |
839fcaba MT |
84 | u64 mapping[IPOIB_CM_RX_SG]) |
85 | { | |
86 | int i; | |
87 | ||
88 | ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE); | |
89 | ||
1812063b | 90 | for (i = 0; i < frags; ++i) |
787adb9d | 91 | ib_dma_unmap_page(priv->ca, mapping[i + 1], PAGE_SIZE, DMA_FROM_DEVICE); |
839fcaba MT |
92 | } |
93 | ||
68e995a2 | 94 | static int ipoib_cm_post_receive_srq(struct net_device *dev, int id) |
839fcaba | 95 | { |
c1048aff | 96 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
839fcaba MT |
97 | struct ib_recv_wr *bad_wr; |
98 | int i, ret; | |
99 | ||
1b524963 | 100 | priv->cm.rx_wr.wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV; |
839fcaba | 101 | |
586a6934 | 102 | for (i = 0; i < priv->cm.num_frags; ++i) |
839fcaba MT |
103 | priv->cm.rx_sge[i].addr = priv->cm.srq_ring[id].mapping[i]; |
104 | ||
105 | ret = ib_post_srq_recv(priv->cm.srq, &priv->cm.rx_wr, &bad_wr); | |
106 | if (unlikely(ret)) { | |
107 | ipoib_warn(priv, "post srq failed for buf %d (%d)\n", id, ret); | |
586a6934 | 108 | ipoib_cm_dma_unmap_rx(priv, priv->cm.num_frags - 1, |
1812063b | 109 | priv->cm.srq_ring[id].mapping); |
839fcaba MT |
110 | dev_kfree_skb_any(priv->cm.srq_ring[id].skb); |
111 | priv->cm.srq_ring[id].skb = NULL; | |
112 | } | |
113 | ||
114 | return ret; | |
115 | } | |
116 | ||
68e995a2 | 117 | static int ipoib_cm_post_receive_nonsrq(struct net_device *dev, |
a7d834c4 RD |
118 | struct ipoib_cm_rx *rx, |
119 | struct ib_recv_wr *wr, | |
120 | struct ib_sge *sge, int id) | |
68e995a2 | 121 | { |
c1048aff | 122 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
68e995a2 PS |
123 | struct ib_recv_wr *bad_wr; |
124 | int i, ret; | |
125 | ||
a7d834c4 | 126 | wr->wr_id = id | IPOIB_OP_CM | IPOIB_OP_RECV; |
68e995a2 PS |
127 | |
128 | for (i = 0; i < IPOIB_CM_RX_SG; ++i) | |
a7d834c4 | 129 | sge[i].addr = rx->rx_ring[id].mapping[i]; |
68e995a2 | 130 | |
a7d834c4 | 131 | ret = ib_post_recv(rx->qp, wr, &bad_wr); |
68e995a2 PS |
132 | if (unlikely(ret)) { |
133 | ipoib_warn(priv, "post recv failed for buf %d (%d)\n", id, ret); | |
134 | ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1, | |
135 | rx->rx_ring[id].mapping); | |
136 | dev_kfree_skb_any(rx->rx_ring[id].skb); | |
137 | rx->rx_ring[id].skb = NULL; | |
138 | } | |
139 | ||
140 | return ret; | |
141 | } | |
142 | ||
143 | static struct sk_buff *ipoib_cm_alloc_rx_skb(struct net_device *dev, | |
144 | struct ipoib_cm_rx_buf *rx_ring, | |
145 | int id, int frags, | |
22252b4e TA |
146 | u64 mapping[IPOIB_CM_RX_SG], |
147 | gfp_t gfp) | |
839fcaba | 148 | { |
c1048aff | 149 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
839fcaba MT |
150 | struct sk_buff *skb; |
151 | int i; | |
152 | ||
fc791b63 | 153 | skb = dev_alloc_skb(ALIGN(IPOIB_CM_HEAD_SIZE + IPOIB_PSEUDO_LEN, 16)); |
839fcaba | 154 | if (unlikely(!skb)) |
1812063b | 155 | return NULL; |
839fcaba MT |
156 | |
157 | /* | |
fc791b63 | 158 | * IPoIB adds a IPOIB_ENCAP_LEN byte header, this will align the |
839fcaba MT |
159 | * IP header to a multiple of 16. |
160 | */ | |
fc791b63 | 161 | skb_reserve(skb, IPOIB_CM_RX_RESERVE); |
839fcaba MT |
162 | |
163 | mapping[0] = ib_dma_map_single(priv->ca, skb->data, IPOIB_CM_HEAD_SIZE, | |
164 | DMA_FROM_DEVICE); | |
165 | if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0]))) { | |
166 | dev_kfree_skb_any(skb); | |
1812063b | 167 | return NULL; |
839fcaba MT |
168 | } |
169 | ||
1812063b | 170 | for (i = 0; i < frags; i++) { |
22252b4e | 171 | struct page *page = alloc_page(gfp); |
839fcaba MT |
172 | |
173 | if (!page) | |
174 | goto partial_error; | |
175 | skb_fill_page_desc(skb, i, page, 0, PAGE_SIZE); | |
176 | ||
5581be3b | 177 | mapping[i + 1] = ib_dma_map_page(priv->ca, page, |
6371ea3d | 178 | 0, PAGE_SIZE, DMA_FROM_DEVICE); |
839fcaba MT |
179 | if (unlikely(ib_dma_mapping_error(priv->ca, mapping[i + 1]))) |
180 | goto partial_error; | |
181 | } | |
182 | ||
68e995a2 | 183 | rx_ring[id].skb = skb; |
1812063b | 184 | return skb; |
839fcaba MT |
185 | |
186 | partial_error: | |
187 | ||
188 | ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_CM_HEAD_SIZE, DMA_FROM_DEVICE); | |
189 | ||
841adfca | 190 | for (; i > 0; --i) |
787adb9d | 191 | ib_dma_unmap_page(priv->ca, mapping[i], PAGE_SIZE, DMA_FROM_DEVICE); |
839fcaba | 192 | |
8a2e65f8 | 193 | dev_kfree_skb_any(skb); |
1812063b | 194 | return NULL; |
839fcaba MT |
195 | } |
196 | ||
1efb6144 RD |
197 | static void ipoib_cm_free_rx_ring(struct net_device *dev, |
198 | struct ipoib_cm_rx_buf *rx_ring) | |
199 | { | |
c1048aff | 200 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
1efb6144 RD |
201 | int i; |
202 | ||
203 | for (i = 0; i < ipoib_recvq_size; ++i) | |
204 | if (rx_ring[i].skb) { | |
205 | ipoib_cm_dma_unmap_rx(priv, IPOIB_CM_RX_SG - 1, | |
206 | rx_ring[i].mapping); | |
207 | dev_kfree_skb_any(rx_ring[i].skb); | |
208 | } | |
209 | ||
b1404069 | 210 | vfree(rx_ring); |
1efb6144 RD |
211 | } |
212 | ||
2337f809 | 213 | static void ipoib_cm_start_rx_drain(struct ipoib_dev_priv *priv) |
518b1646 | 214 | { |
ec56dc0b MT |
215 | struct ib_send_wr *bad_wr; |
216 | struct ipoib_cm_rx *p; | |
518b1646 | 217 | |
ec56dc0b | 218 | /* We only reserved 1 extra slot in CQ for drain WRs, so |
518b1646 MT |
219 | * make sure we have at most 1 outstanding WR. */ |
220 | if (list_empty(&priv->cm.rx_flush_list) || | |
221 | !list_empty(&priv->cm.rx_drain_list)) | |
222 | return; | |
223 | ||
ec56dc0b MT |
224 | /* |
225 | * QPs on flush list are error state. This way, a "flush | |
226 | * error" WC will be immediately generated for each WR we post. | |
227 | */ | |
228 | p = list_entry(priv->cm.rx_flush_list.next, typeof(*p), list); | |
14d3a3b2 | 229 | ipoib_cm_rx_drain_wr.wr_id = IPOIB_CM_RX_DRAIN_WRID; |
ec56dc0b MT |
230 | if (ib_post_send(p->qp, &ipoib_cm_rx_drain_wr, &bad_wr)) |
231 | ipoib_warn(priv, "failed to post drain wr\n"); | |
518b1646 MT |
232 | |
233 | list_splice_init(&priv->cm.rx_flush_list, &priv->cm.rx_drain_list); | |
234 | } | |
235 | ||
236 | static void ipoib_cm_rx_event_handler(struct ib_event *event, void *ctx) | |
237 | { | |
238 | struct ipoib_cm_rx *p = ctx; | |
c1048aff | 239 | struct ipoib_dev_priv *priv = ipoib_priv(p->dev); |
518b1646 MT |
240 | unsigned long flags; |
241 | ||
242 | if (event->event != IB_EVENT_QP_LAST_WQE_REACHED) | |
243 | return; | |
244 | ||
245 | spin_lock_irqsave(&priv->lock, flags); | |
246 | list_move(&p->list, &priv->cm.rx_flush_list); | |
247 | p->state = IPOIB_CM_RX_FLUSH; | |
248 | ipoib_cm_start_rx_drain(priv); | |
249 | spin_unlock_irqrestore(&priv->lock, flags); | |
250 | } | |
251 | ||
839fcaba MT |
252 | static struct ib_qp *ipoib_cm_create_rx_qp(struct net_device *dev, |
253 | struct ipoib_cm_rx *p) | |
254 | { | |
c1048aff | 255 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
839fcaba | 256 | struct ib_qp_init_attr attr = { |
518b1646 | 257 | .event_handler = ipoib_cm_rx_event_handler, |
f56bcd80 EC |
258 | .send_cq = priv->recv_cq, /* For drain WR */ |
259 | .recv_cq = priv->recv_cq, | |
839fcaba | 260 | .srq = priv->cm.srq, |
ec56dc0b | 261 | .cap.max_send_wr = 1, /* For drain WR */ |
839fcaba MT |
262 | .cap.max_send_sge = 1, /* FIXME: 0 Seems not to work */ |
263 | .sq_sig_type = IB_SIGNAL_ALL_WR, | |
264 | .qp_type = IB_QPT_RC, | |
265 | .qp_context = p, | |
266 | }; | |
68e995a2 PS |
267 | |
268 | if (!ipoib_cm_has_srq(dev)) { | |
269 | attr.cap.max_recv_wr = ipoib_recvq_size; | |
270 | attr.cap.max_recv_sge = IPOIB_CM_RX_SG; | |
271 | } | |
272 | ||
839fcaba MT |
273 | return ib_create_qp(priv->pd, &attr); |
274 | } | |
275 | ||
276 | static int ipoib_cm_modify_rx_qp(struct net_device *dev, | |
68e995a2 PS |
277 | struct ib_cm_id *cm_id, struct ib_qp *qp, |
278 | unsigned psn) | |
839fcaba | 279 | { |
c1048aff | 280 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
839fcaba MT |
281 | struct ib_qp_attr qp_attr; |
282 | int qp_attr_mask, ret; | |
283 | ||
284 | qp_attr.qp_state = IB_QPS_INIT; | |
285 | ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask); | |
286 | if (ret) { | |
287 | ipoib_warn(priv, "failed to init QP attr for INIT: %d\n", ret); | |
288 | return ret; | |
289 | } | |
290 | ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask); | |
291 | if (ret) { | |
292 | ipoib_warn(priv, "failed to modify QP to INIT: %d\n", ret); | |
293 | return ret; | |
294 | } | |
295 | qp_attr.qp_state = IB_QPS_RTR; | |
296 | ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask); | |
297 | if (ret) { | |
298 | ipoib_warn(priv, "failed to init QP attr for RTR: %d\n", ret); | |
299 | return ret; | |
300 | } | |
301 | qp_attr.rq_psn = psn; | |
302 | ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask); | |
303 | if (ret) { | |
304 | ipoib_warn(priv, "failed to modify QP to RTR: %d\n", ret); | |
305 | return ret; | |
306 | } | |
ec56dc0b MT |
307 | |
308 | /* | |
309 | * Current Mellanox HCA firmware won't generate completions | |
310 | * with error for drain WRs unless the QP has been moved to | |
311 | * RTS first. This work-around leaves a window where a QP has | |
312 | * moved to error asynchronously, but this will eventually get | |
313 | * fixed in firmware, so let's not error out if modify QP | |
314 | * fails. | |
315 | */ | |
316 | qp_attr.qp_state = IB_QPS_RTS; | |
317 | ret = ib_cm_init_qp_attr(cm_id, &qp_attr, &qp_attr_mask); | |
318 | if (ret) { | |
319 | ipoib_warn(priv, "failed to init QP attr for RTS: %d\n", ret); | |
320 | return 0; | |
321 | } | |
322 | ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask); | |
323 | if (ret) { | |
324 | ipoib_warn(priv, "failed to modify QP to RTS: %d\n", ret); | |
325 | return 0; | |
326 | } | |
327 | ||
839fcaba MT |
328 | return 0; |
329 | } | |
330 | ||
a7d834c4 RD |
331 | static void ipoib_cm_init_rx_wr(struct net_device *dev, |
332 | struct ib_recv_wr *wr, | |
333 | struct ib_sge *sge) | |
334 | { | |
c1048aff | 335 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
a7d834c4 RD |
336 | int i; |
337 | ||
338 | for (i = 0; i < priv->cm.num_frags; ++i) | |
77b1f996 | 339 | sge[i].lkey = priv->pd->local_dma_lkey; |
a7d834c4 RD |
340 | |
341 | sge[0].length = IPOIB_CM_HEAD_SIZE; | |
342 | for (i = 1; i < priv->cm.num_frags; ++i) | |
343 | sge[i].length = PAGE_SIZE; | |
344 | ||
345 | wr->next = NULL; | |
e0819816 | 346 | wr->sg_list = sge; |
a7d834c4 RD |
347 | wr->num_sge = priv->cm.num_frags; |
348 | } | |
349 | ||
68e995a2 PS |
350 | static int ipoib_cm_nonsrq_init_rx(struct net_device *dev, struct ib_cm_id *cm_id, |
351 | struct ipoib_cm_rx *rx) | |
352 | { | |
c1048aff | 353 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
a7d834c4 RD |
354 | struct { |
355 | struct ib_recv_wr wr; | |
356 | struct ib_sge sge[IPOIB_CM_RX_SG]; | |
357 | } *t; | |
68e995a2 PS |
358 | int ret; |
359 | int i; | |
360 | ||
948579cd | 361 | rx->rx_ring = vzalloc(ipoib_recvq_size * sizeof *rx->rx_ring); |
74226649 | 362 | if (!rx->rx_ring) |
68e995a2 | 363 | return -ENOMEM; |
b1404069 | 364 | |
a7d834c4 RD |
365 | t = kmalloc(sizeof *t, GFP_KERNEL); |
366 | if (!t) { | |
367 | ret = -ENOMEM; | |
c5e8f57b | 368 | goto err_free_1; |
a7d834c4 RD |
369 | } |
370 | ||
371 | ipoib_cm_init_rx_wr(dev, &t->wr, t->sge); | |
372 | ||
68e995a2 PS |
373 | spin_lock_irq(&priv->lock); |
374 | ||
375 | if (priv->cm.nonsrq_conn_qp >= ipoib_max_conn_qp) { | |
376 | spin_unlock_irq(&priv->lock); | |
377 | ib_send_cm_rej(cm_id, IB_CM_REJ_NO_QP, NULL, 0, NULL, 0); | |
378 | ret = -EINVAL; | |
379 | goto err_free; | |
380 | } else | |
381 | ++priv->cm.nonsrq_conn_qp; | |
382 | ||
383 | spin_unlock_irq(&priv->lock); | |
384 | ||
385 | for (i = 0; i < ipoib_recvq_size; ++i) { | |
386 | if (!ipoib_cm_alloc_rx_skb(dev, rx->rx_ring, i, IPOIB_CM_RX_SG - 1, | |
22252b4e TA |
387 | rx->rx_ring[i].mapping, |
388 | GFP_KERNEL)) { | |
68e995a2 | 389 | ipoib_warn(priv, "failed to allocate receive buffer %d\n", i); |
8f71c1a2 BVA |
390 | ret = -ENOMEM; |
391 | goto err_count; | |
a7d834c4 RD |
392 | } |
393 | ret = ipoib_cm_post_receive_nonsrq(dev, rx, &t->wr, t->sge, i); | |
68e995a2 PS |
394 | if (ret) { |
395 | ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq " | |
396 | "failed for buf %d\n", i); | |
397 | ret = -EIO; | |
398 | goto err_count; | |
399 | } | |
400 | } | |
401 | ||
402 | rx->recv_count = ipoib_recvq_size; | |
403 | ||
a7d834c4 RD |
404 | kfree(t); |
405 | ||
68e995a2 PS |
406 | return 0; |
407 | ||
408 | err_count: | |
409 | spin_lock_irq(&priv->lock); | |
410 | --priv->cm.nonsrq_conn_qp; | |
411 | spin_unlock_irq(&priv->lock); | |
412 | ||
413 | err_free: | |
a7d834c4 | 414 | kfree(t); |
c5e8f57b ZY |
415 | |
416 | err_free_1: | |
68e995a2 PS |
417 | ipoib_cm_free_rx_ring(dev, rx->rx_ring); |
418 | ||
419 | return ret; | |
420 | } | |
421 | ||
839fcaba MT |
422 | static int ipoib_cm_send_rep(struct net_device *dev, struct ib_cm_id *cm_id, |
423 | struct ib_qp *qp, struct ib_cm_req_event_param *req, | |
424 | unsigned psn) | |
425 | { | |
c1048aff | 426 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
839fcaba MT |
427 | struct ipoib_cm_data data = {}; |
428 | struct ib_cm_rep_param rep = {}; | |
429 | ||
430 | data.qpn = cpu_to_be32(priv->qp->qp_num); | |
431 | data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE); | |
432 | ||
433 | rep.private_data = &data; | |
434 | rep.private_data_len = sizeof data; | |
435 | rep.flow_control = 0; | |
436 | rep.rnr_retry_count = req->rnr_retry_count; | |
68e995a2 | 437 | rep.srq = ipoib_cm_has_srq(dev); |
839fcaba MT |
438 | rep.qp_num = qp->qp_num; |
439 | rep.starting_psn = psn; | |
440 | return ib_send_cm_rep(cm_id, &rep); | |
441 | } | |
442 | ||
443 | static int ipoib_cm_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event) | |
444 | { | |
445 | struct net_device *dev = cm_id->context; | |
c1048aff | 446 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
839fcaba | 447 | struct ipoib_cm_rx *p; |
839fcaba MT |
448 | unsigned psn; |
449 | int ret; | |
450 | ||
451 | ipoib_dbg(priv, "REQ arrived\n"); | |
452 | p = kzalloc(sizeof *p, GFP_KERNEL); | |
453 | if (!p) | |
454 | return -ENOMEM; | |
455 | p->dev = dev; | |
456 | p->id = cm_id; | |
3ec7393a MT |
457 | cm_id->context = p; |
458 | p->state = IPOIB_CM_RX_LIVE; | |
459 | p->jiffies = jiffies; | |
460 | INIT_LIST_HEAD(&p->list); | |
461 | ||
839fcaba MT |
462 | p->qp = ipoib_cm_create_rx_qp(dev, p); |
463 | if (IS_ERR(p->qp)) { | |
464 | ret = PTR_ERR(p->qp); | |
465 | goto err_qp; | |
466 | } | |
467 | ||
50bea5c0 | 468 | psn = prandom_u32() & 0xffffff; |
839fcaba MT |
469 | ret = ipoib_cm_modify_rx_qp(dev, cm_id, p->qp, psn); |
470 | if (ret) | |
471 | goto err_modify; | |
472 | ||
68e995a2 PS |
473 | if (!ipoib_cm_has_srq(dev)) { |
474 | ret = ipoib_cm_nonsrq_init_rx(dev, cm_id, p); | |
475 | if (ret) | |
476 | goto err_modify; | |
477 | } | |
478 | ||
3ec7393a | 479 | spin_lock_irq(&priv->lock); |
0b39578b | 480 | queue_delayed_work(priv->wq, |
3ec7393a MT |
481 | &priv->cm.stale_task, IPOIB_CM_RX_DELAY); |
482 | /* Add this entry to passive ids list head, but do not re-add it | |
483 | * if IB_EVENT_QP_LAST_WQE_REACHED has moved it to flush list. */ | |
484 | p->jiffies = jiffies; | |
485 | if (p->state == IPOIB_CM_RX_LIVE) | |
486 | list_move(&p->list, &priv->cm.passive_ids); | |
487 | spin_unlock_irq(&priv->lock); | |
488 | ||
839fcaba MT |
489 | ret = ipoib_cm_send_rep(dev, cm_id, p->qp, &event->param.req_rcvd, psn); |
490 | if (ret) { | |
491 | ipoib_warn(priv, "failed to send REP: %d\n", ret); | |
3ec7393a MT |
492 | if (ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE)) |
493 | ipoib_warn(priv, "unable to move qp to error state\n"); | |
839fcaba | 494 | } |
839fcaba MT |
495 | return 0; |
496 | ||
839fcaba MT |
497 | err_modify: |
498 | ib_destroy_qp(p->qp); | |
499 | err_qp: | |
500 | kfree(p); | |
501 | return ret; | |
502 | } | |
503 | ||
504 | static int ipoib_cm_rx_handler(struct ib_cm_id *cm_id, | |
505 | struct ib_cm_event *event) | |
506 | { | |
507 | struct ipoib_cm_rx *p; | |
508 | struct ipoib_dev_priv *priv; | |
839fcaba MT |
509 | |
510 | switch (event->event) { | |
511 | case IB_CM_REQ_RECEIVED: | |
512 | return ipoib_cm_req_handler(cm_id, event); | |
513 | case IB_CM_DREQ_RECEIVED: | |
839fcaba MT |
514 | ib_send_cm_drep(cm_id, NULL, 0); |
515 | /* Fall through */ | |
516 | case IB_CM_REJ_RECEIVED: | |
517 | p = cm_id->context; | |
c1048aff | 518 | priv = ipoib_priv(p->dev); |
518b1646 MT |
519 | 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 */ | |
839fcaba MT |
522 | default: |
523 | return 0; | |
524 | } | |
525 | } | |
526 | /* Adjust length of skb with fragments to match received data */ | |
527 | static void skb_put_frags(struct sk_buff *skb, unsigned int hdr_space, | |
1812063b | 528 | unsigned int length, struct sk_buff *toskb) |
839fcaba MT |
529 | { |
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 */ | |
5581be3b IC |
545 | skb_fill_page_desc(toskb, i, skb_frag_page(frag), |
546 | 0, PAGE_SIZE); | |
839fcaba MT |
547 | --skb_shinfo(skb)->nr_frags; |
548 | } else { | |
549 | size = min(length, (unsigned) PAGE_SIZE); | |
550 | ||
9e903e08 | 551 | skb_frag_size_set(frag, size); |
839fcaba MT |
552 | skb->data_len += size; |
553 | skb->truesize += size; | |
554 | skb->len += size; | |
555 | length -= size; | |
556 | } | |
557 | } | |
558 | } | |
559 | ||
560 | void ipoib_cm_handle_rx_wc(struct net_device *dev, struct ib_wc *wc) | |
561 | { | |
c1048aff | 562 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
68e995a2 | 563 | struct ipoib_cm_rx_buf *rx_ring; |
1b524963 | 564 | unsigned int wr_id = wc->wr_id & ~(IPOIB_OP_CM | IPOIB_OP_RECV); |
1812063b | 565 | struct sk_buff *skb, *newskb; |
839fcaba MT |
566 | struct ipoib_cm_rx *p; |
567 | unsigned long flags; | |
568 | u64 mapping[IPOIB_CM_RX_SG]; | |
1812063b | 569 | int frags; |
68e995a2 | 570 | int has_srq; |
f89271da | 571 | struct sk_buff *small_skb; |
839fcaba | 572 | |
a89875fc RD |
573 | ipoib_dbg_data(priv, "cm recv completion: id %d, status: %d\n", |
574 | wr_id, wc->status); | |
839fcaba MT |
575 | |
576 | if (unlikely(wr_id >= ipoib_recvq_size)) { | |
1b524963 | 577 | if (wr_id == (IPOIB_CM_RX_DRAIN_WRID & ~(IPOIB_OP_CM | IPOIB_OP_RECV))) { |
518b1646 MT |
578 | 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); | |
0b39578b | 581 | queue_work(priv->wq, &priv->cm.rx_reap_task); |
518b1646 MT |
582 | 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); | |
839fcaba MT |
586 | return; |
587 | } | |
588 | ||
68e995a2 PS |
589 | 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; | |
839fcaba MT |
595 | |
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); | |
de903512 | 600 | ++dev->stats.rx_dropped; |
68e995a2 PS |
601 | 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); | |
0b39578b | 608 | queue_work(priv->wq, &priv->cm.rx_reap_task); |
68e995a2 PS |
609 | } |
610 | return; | |
611 | } | |
839fcaba MT |
612 | } |
613 | ||
fd312561 | 614 | if (unlikely(!(wr_id & IPOIB_CM_RX_UPDATE_MASK))) { |
d6ef7d68 | 615 | if (p && time_after_eq(jiffies, p->jiffies + IPOIB_CM_RX_UPDATE_TIME)) { |
839fcaba MT |
616 | spin_lock_irqsave(&priv->lock, flags); |
617 | p->jiffies = jiffies; | |
518b1646 MT |
618 | /* 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) | |
839fcaba MT |
621 | list_move(&p->list, &priv->cm.passive_ids); |
622 | spin_unlock_irqrestore(&priv->lock, flags); | |
839fcaba MT |
623 | } |
624 | } | |
625 | ||
f89271da EC |
626 | if (wc->byte_len < IPOIB_CM_COPYBREAK) { |
627 | int dlen = wc->byte_len; | |
628 | ||
fc791b63 | 629 | small_skb = dev_alloc_skb(dlen + IPOIB_CM_RX_RESERVE); |
f89271da | 630 | if (small_skb) { |
fc791b63 | 631 | skb_reserve(small_skb, IPOIB_CM_RX_RESERVE); |
f89271da EC |
632 | 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 | ||
1812063b MT |
643 | frags = PAGE_ALIGN(wc->byte_len - min(wc->byte_len, |
644 | (unsigned)IPOIB_CM_HEAD_SIZE)) / PAGE_SIZE; | |
645 | ||
22252b4e TA |
646 | newskb = ipoib_cm_alloc_rx_skb(dev, rx_ring, wr_id, frags, |
647 | mapping, GFP_ATOMIC); | |
1812063b | 648 | if (unlikely(!newskb)) { |
839fcaba MT |
649 | /* |
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); | |
de903512 | 654 | ++dev->stats.rx_dropped; |
839fcaba MT |
655 | goto repost; |
656 | } | |
657 | ||
68e995a2 PS |
658 | ipoib_cm_dma_unmap_rx(priv, frags, rx_ring[wr_id].mapping); |
659 | memcpy(rx_ring[wr_id].mapping, mapping, (frags + 1) * sizeof *mapping); | |
839fcaba MT |
660 | |
661 | ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n", | |
662 | wc->byte_len, wc->slid); | |
663 | ||
1812063b | 664 | skb_put_frags(skb, IPOIB_CM_HEAD_SIZE, wc->byte_len, newskb); |
839fcaba | 665 | |
f89271da | 666 | copied: |
839fcaba | 667 | skb->protocol = ((struct ipoib_header *) skb->data)->proto; |
fc791b63 | 668 | skb_add_pseudo_hdr(skb); |
839fcaba | 669 | |
de903512 RD |
670 | ++dev->stats.rx_packets; |
671 | dev->stats.rx_bytes += skb->len; | |
839fcaba MT |
672 | |
673 | skb->dev = dev; | |
674 | /* XXX get correct PACKET_ type here */ | |
675 | skb->pkt_type = PACKET_HOST; | |
8d1cc86a | 676 | netif_receive_skb(skb); |
839fcaba MT |
677 | |
678 | repost: | |
68e995a2 PS |
679 | 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 { | |
a7d834c4 RD |
684 | if (unlikely(ipoib_cm_post_receive_nonsrq(dev, p, |
685 | &priv->cm.rx_wr, | |
686 | priv->cm.rx_sge, | |
687 | wr_id))) { | |
68e995a2 PS |
688 | --p->recv_count; |
689 | ipoib_warn(priv, "ipoib_cm_post_receive_nonsrq failed " | |
690 | "for buf %d\n", wr_id); | |
691 | } | |
692 | } | |
839fcaba MT |
693 | } |
694 | ||
695 | static inline int post_send(struct ipoib_dev_priv *priv, | |
696 | struct ipoib_cm_tx *tx, | |
697 | unsigned int wr_id, | |
c4268778 | 698 | struct ipoib_tx_buf *tx_req) |
839fcaba MT |
699 | { |
700 | struct ib_send_wr *bad_wr; | |
701 | ||
c4268778 | 702 | ipoib_build_sge(priv, tx_req); |
839fcaba | 703 | |
e622f2f4 | 704 | priv->tx_wr.wr.wr_id = wr_id | IPOIB_OP_CM; |
839fcaba | 705 | |
e622f2f4 | 706 | return ib_post_send(tx->qp, &priv->tx_wr.wr, &bad_wr); |
839fcaba MT |
707 | } |
708 | ||
709 | void ipoib_cm_send(struct net_device *dev, struct sk_buff *skb, struct ipoib_cm_tx *tx) | |
710 | { | |
c1048aff | 711 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
c4268778 | 712 | struct ipoib_tx_buf *tx_req; |
a48f509b | 713 | int rc; |
78a50a5e | 714 | unsigned usable_sge = tx->max_send_sge - !!skb_headlen(skb); |
839fcaba MT |
715 | |
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); | |
de903512 RD |
719 | ++dev->stats.tx_dropped; |
720 | ++dev->stats.tx_errors; | |
77d8e1ef | 721 | ipoib_cm_skb_too_long(dev, skb, tx->mtu - IPOIB_ENCAP_LEN); |
839fcaba MT |
722 | return; |
723 | } | |
78a50a5e HWR |
724 | 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 | } | |
839fcaba MT |
741 | 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; | |
c4268778 YS |
753 | |
754 | if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) { | |
de903512 | 755 | ++dev->stats.tx_errors; |
839fcaba MT |
756 | dev_kfree_skb_any(skb); |
757 | return; | |
758 | } | |
759 | ||
7e5a90c2 SP |
760 | skb_orphan(skb); |
761 | skb_dst_drop(skb); | |
762 | ||
c4268778 | 763 | rc = post_send(priv, tx, tx->tx_head & (ipoib_sendq_size - 1), tx_req); |
a48f509b OG |
764 | if (unlikely(rc)) { |
765 | ipoib_warn(priv, "post_send failed, error %d\n", rc); | |
de903512 | 766 | ++dev->stats.tx_errors; |
c4268778 | 767 | ipoib_dma_unmap_tx(priv, tx_req); |
839fcaba MT |
768 | dev_kfree_skb_any(skb); |
769 | } else { | |
860e9538 | 770 | netif_trans_update(dev); |
839fcaba MT |
771 | ++tx->tx_head; |
772 | ||
1b524963 | 773 | if (++priv->tx_outstanding == ipoib_sendq_size) { |
839fcaba MT |
774 | ipoib_dbg(priv, "TX ring 0x%x full, stopping kernel net queue\n", |
775 | tx->qp->qp_num); | |
776 | netif_stop_queue(dev); | |
1ee9e2aa MM |
777 | 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); | |
839fcaba MT |
783 | } |
784 | } | |
785 | } | |
786 | ||
1b524963 | 787 | void ipoib_cm_handle_tx_wc(struct net_device *dev, struct ib_wc *wc) |
839fcaba | 788 | { |
c1048aff | 789 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
1b524963 MT |
790 | struct ipoib_cm_tx *tx = wc->qp->qp_context; |
791 | unsigned int wr_id = wc->wr_id & ~IPOIB_OP_CM; | |
c4268778 | 792 | struct ipoib_tx_buf *tx_req; |
839fcaba MT |
793 | unsigned long flags; |
794 | ||
a89875fc RD |
795 | ipoib_dbg_data(priv, "cm send completion: id %d, status: %d\n", |
796 | wr_id, wc->status); | |
839fcaba MT |
797 | |
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 | ||
c4268778 | 806 | ipoib_dma_unmap_tx(priv, tx_req); |
839fcaba MT |
807 | |
808 | /* FIXME: is this right? Shouldn't we only increment on success? */ | |
de903512 RD |
809 | ++dev->stats.tx_packets; |
810 | dev->stats.tx_bytes += tx_req->skb->len; | |
839fcaba MT |
811 | |
812 | dev_kfree_skb_any(tx_req->skb); | |
813 | ||
943c246e RD |
814 | netif_tx_lock(dev); |
815 | ||
839fcaba | 816 | ++tx->tx_tail; |
1b524963 MT |
817 | if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) && |
818 | netif_queue_stopped(dev) && | |
819 | test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) | |
839fcaba | 820 | netif_wake_queue(dev); |
839fcaba MT |
821 | |
822 | if (wc->status != IB_WC_SUCCESS && | |
823 | wc->status != IB_WC_WR_FLUSH_ERR) { | |
824 | struct ipoib_neigh *neigh; | |
825 | ||
13ee429a FD |
826 | 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); | |
839fcaba | 832 | |
943c246e | 833 | spin_lock_irqsave(&priv->lock, flags); |
839fcaba MT |
834 | neigh = tx->neigh; |
835 | ||
836 | if (neigh) { | |
837 | neigh->cm = NULL; | |
b63b70d8 | 838 | ipoib_neigh_free(neigh); |
839fcaba MT |
839 | |
840 | tx->neigh = NULL; | |
841 | } | |
842 | ||
839fcaba MT |
843 | if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) { |
844 | list_move(&tx->list, &priv->cm.reap_list); | |
0b39578b | 845 | queue_work(priv->wq, &priv->cm.reap_task); |
839fcaba MT |
846 | } |
847 | ||
848 | clear_bit(IPOIB_FLAG_OPER_UP, &tx->flags); | |
849 | ||
943c246e | 850 | spin_unlock_irqrestore(&priv->lock, flags); |
839fcaba MT |
851 | } |
852 | ||
943c246e | 853 | netif_tx_unlock(dev); |
839fcaba MT |
854 | } |
855 | ||
839fcaba MT |
856 | int ipoib_cm_dev_open(struct net_device *dev) |
857 | { | |
c1048aff | 858 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
839fcaba MT |
859 | 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); | |
347fcfbe | 867 | ret = PTR_ERR(priv->cm.id); |
518b1646 | 868 | goto err_cm; |
839fcaba MT |
869 | } |
870 | ||
871 | ret = ib_cm_listen(priv->cm.id, cpu_to_be64(IPOIB_CM_IETF_ID | priv->qp->qp_num), | |
73fec7fd | 872 | 0); |
839fcaba MT |
873 | 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); | |
518b1646 | 876 | goto err_listen; |
839fcaba | 877 | } |
518b1646 | 878 | |
839fcaba | 879 | return 0; |
518b1646 MT |
880 | |
881 | err_listen: | |
882 | ib_destroy_cm_id(priv->cm.id); | |
883 | err_cm: | |
884 | priv->cm.id = NULL; | |
518b1646 | 885 | return ret; |
839fcaba MT |
886 | } |
887 | ||
efcd9971 RD |
888 | static void ipoib_cm_free_rx_reap_list(struct net_device *dev) |
889 | { | |
c1048aff | 890 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
efcd9971 RD |
891 | 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); | |
68e995a2 PS |
901 | 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 | } | |
efcd9971 RD |
907 | kfree(rx); |
908 | } | |
909 | } | |
910 | ||
839fcaba MT |
911 | void ipoib_cm_dev_stop(struct net_device *dev) |
912 | { | |
c1048aff | 913 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
efcd9971 | 914 | struct ipoib_cm_rx *p; |
518b1646 | 915 | unsigned long begin; |
518b1646 | 916 | int ret; |
839fcaba | 917 | |
347fcfbe | 918 | if (!IPOIB_CM_SUPPORTED(dev->dev_addr) || !priv->cm.id) |
839fcaba MT |
919 | return; |
920 | ||
921 | ib_destroy_cm_id(priv->cm.id); | |
347fcfbe | 922 | priv->cm.id = NULL; |
518b1646 | 923 | |
37aebbde | 924 | spin_lock_irq(&priv->lock); |
839fcaba MT |
925 | while (!list_empty(&priv->cm.passive_ids)) { |
926 | p = list_entry(priv->cm.passive_ids.next, typeof(*p), list); | |
518b1646 MT |
927 | list_move(&p->list, &priv->cm.rx_error_list); |
928 | p->state = IPOIB_CM_RX_ERROR; | |
37aebbde | 929 | spin_unlock_irq(&priv->lock); |
518b1646 MT |
930 | 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)) { | |
8fd357a6 | 942 | if (time_after(jiffies, begin + 5 * HZ)) { |
518b1646 MT |
943 | ipoib_warn(priv, "RX drain timing out\n"); |
944 | ||
945 | /* | |
946 | * assume the HW is wedged and just free up everything. | |
947 | */ | |
ec229e5e PS |
948 | 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); | |
518b1646 MT |
954 | break; |
955 | } | |
956 | spin_unlock_irq(&priv->lock); | |
98e77d9f | 957 | usleep_range(1000, 2000); |
2dfbfc37 | 958 | ipoib_drain_cq(dev); |
518b1646 MT |
959 | spin_lock_irq(&priv->lock); |
960 | } | |
961 | ||
518b1646 MT |
962 | spin_unlock_irq(&priv->lock); |
963 | ||
efcd9971 | 964 | ipoib_cm_free_rx_reap_list(dev); |
839fcaba MT |
965 | |
966 | cancel_delayed_work(&priv->cm.stale_task); | |
967 | } | |
968 | ||
969 | static 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; | |
c1048aff | 972 | struct ipoib_dev_priv *priv = ipoib_priv(p->dev); |
839fcaba MT |
973 | 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; | |
839fcaba MT |
978 | |
979 | p->mtu = be32_to_cpu(data->mtu); | |
980 | ||
82c3aca6 MT |
981 | if (p->mtu <= IPOIB_ENCAP_LEN) { |
982 | ipoib_warn(priv, "Rejecting connection: mtu %d <= %d\n", | |
983 | p->mtu, IPOIB_ENCAP_LEN); | |
839fcaba MT |
984 | 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 | ||
37aebbde | 1015 | spin_lock_irq(&priv->lock); |
839fcaba MT |
1016 | 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); | |
37aebbde | 1020 | spin_unlock_irq(&priv->lock); |
839fcaba MT |
1021 | |
1022 | while ((skb = __skb_dequeue(&skqueue))) { | |
1023 | skb->dev = p->dev; | |
d32b9a81 FD |
1024 | 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); | |
839fcaba MT |
1028 | } |
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 | ||
1b524963 | 1038 | static struct ib_qp *ipoib_cm_create_tx_qp(struct net_device *dev, struct ipoib_cm_tx *tx) |
839fcaba | 1039 | { |
c1048aff | 1040 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
ede6bc04 | 1041 | struct ib_qp_init_attr attr = { |
f56bcd80 EC |
1042 | .send_cq = priv->recv_cq, |
1043 | .recv_cq = priv->recv_cq, | |
ede6bc04 DB |
1044 | .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, | |
09b93088 | 1049 | .qp_context = tx, |
d83187dd | 1050 | .create_flags = 0 |
2337f809 | 1051 | }; |
09b93088 OG |
1052 | struct ib_qp *tx_qp; |
1053 | ||
c4268778 | 1054 | if (dev->features & NETIF_F_SG) |
78a50a5e HWR |
1055 | attr.cap.max_send_sge = |
1056 | min_t(u32, priv->ca->attrs.max_sge, MAX_SKB_FRAGS + 1); | |
c4268778 | 1057 | |
09b93088 | 1058 | tx_qp = ib_create_qp(priv->pd, &attr); |
78a50a5e | 1059 | tx->max_send_sge = attr.cap.max_send_sge; |
09b93088 | 1060 | return tx_qp; |
839fcaba MT |
1061 | } |
1062 | ||
1063 | static int ipoib_cm_send_req(struct net_device *dev, | |
1064 | struct ib_cm_id *id, struct ib_qp *qp, | |
1065 | u32 qpn, | |
c2f8fc4e | 1066 | struct sa_path_rec *pathrec) |
839fcaba | 1067 | { |
c1048aff | 1068 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
839fcaba MT |
1069 | struct ipoib_cm_data data = {}; |
1070 | struct ib_cm_req_param req = {}; | |
1071 | ||
1072 | data.qpn = cpu_to_be32(priv->qp->qp_num); | |
1073 | data.mtu = cpu_to_be32(IPOIB_CM_BUF_SIZE); | |
1074 | ||
2337f809 RD |
1075 | req.primary_path = pathrec; |
1076 | req.alternate_path = NULL; | |
1077 | req.service_id = cpu_to_be64(IPOIB_CM_IETF_ID | qpn); | |
1078 | req.qp_num = qp->qp_num; | |
1079 | req.qp_type = qp->qp_type; | |
1080 | req.private_data = &data; | |
1081 | req.private_data_len = sizeof data; | |
1082 | req.flow_control = 0; | |
839fcaba | 1083 | |
2337f809 | 1084 | req.starting_psn = 0; /* FIXME */ |
839fcaba MT |
1085 | |
1086 | /* | |
1087 | * Pick some arbitrary defaults here; we could make these | |
1088 | * module parameters if anyone cared about setting them. | |
1089 | */ | |
2337f809 RD |
1090 | req.responder_resources = 4; |
1091 | req.remote_cm_response_timeout = 20; | |
1092 | req.local_cm_response_timeout = 20; | |
1093 | req.retry_count = 0; /* RFC draft warns against retries */ | |
1094 | req.rnr_retry_count = 0; /* RFC draft warns against retries */ | |
1095 | req.max_cm_retries = 15; | |
68e995a2 | 1096 | req.srq = ipoib_cm_has_srq(dev); |
839fcaba MT |
1097 | return ib_send_cm_req(id, &req); |
1098 | } | |
1099 | ||
1100 | static int ipoib_cm_modify_tx_init(struct net_device *dev, | |
1101 | struct ib_cm_id *cm_id, struct ib_qp *qp) | |
1102 | { | |
c1048aff | 1103 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
839fcaba MT |
1104 | struct ib_qp_attr qp_attr; |
1105 | int qp_attr_mask, ret; | |
9fdd5e5b | 1106 | ret = ib_find_pkey(priv->ca, priv->port, priv->pkey, &qp_attr.pkey_index); |
839fcaba | 1107 | if (ret) { |
9fdd5e5b | 1108 | ipoib_warn(priv, "pkey 0x%x not found: %d\n", priv->pkey, ret); |
839fcaba MT |
1109 | return ret; |
1110 | } | |
1111 | ||
1112 | qp_attr.qp_state = IB_QPS_INIT; | |
1113 | qp_attr.qp_access_flags = IB_ACCESS_LOCAL_WRITE; | |
1114 | qp_attr.port_num = priv->port; | |
1115 | qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT; | |
1116 | ||
1117 | ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask); | |
1118 | if (ret) { | |
1119 | ipoib_warn(priv, "failed to modify tx QP to INIT: %d\n", ret); | |
1120 | return ret; | |
1121 | } | |
1122 | return 0; | |
1123 | } | |
1124 | ||
1125 | static int ipoib_cm_tx_init(struct ipoib_cm_tx *p, u32 qpn, | |
c2f8fc4e | 1126 | struct sa_path_rec *pathrec) |
839fcaba | 1127 | { |
c1048aff | 1128 | struct ipoib_dev_priv *priv = ipoib_priv(p->dev); |
d83187dd | 1129 | unsigned int noio_flag; |
839fcaba MT |
1130 | int ret; |
1131 | ||
d83187dd LR |
1132 | noio_flag = memalloc_noio_save(); |
1133 | p->tx_ring = vzalloc(ipoib_sendq_size * sizeof(*p->tx_ring)); | |
839fcaba | 1134 | if (!p->tx_ring) { |
839fcaba MT |
1135 | ret = -ENOMEM; |
1136 | goto err_tx; | |
1137 | } | |
09b93088 | 1138 | memset(p->tx_ring, 0, ipoib_sendq_size * sizeof *p->tx_ring); |
839fcaba | 1139 | |
1b524963 | 1140 | p->qp = ipoib_cm_create_tx_qp(p->dev, p); |
d83187dd | 1141 | memalloc_noio_restore(noio_flag); |
839fcaba MT |
1142 | if (IS_ERR(p->qp)) { |
1143 | ret = PTR_ERR(p->qp); | |
d83187dd | 1144 | ipoib_warn(priv, "failed to create tx qp: %d\n", ret); |
839fcaba MT |
1145 | goto err_qp; |
1146 | } | |
1147 | ||
1148 | p->id = ib_create_cm_id(priv->ca, ipoib_cm_tx_handler, p); | |
1149 | if (IS_ERR(p->id)) { | |
1150 | ret = PTR_ERR(p->id); | |
1151 | ipoib_warn(priv, "failed to create tx cm id: %d\n", ret); | |
1152 | goto err_id; | |
1153 | } | |
1154 | ||
1155 | ret = ipoib_cm_modify_tx_init(p->dev, p->id, p->qp); | |
1156 | if (ret) { | |
1157 | ipoib_warn(priv, "failed to modify tx qp to rtr: %d\n", ret); | |
23536dfa | 1158 | goto err_modify_send; |
839fcaba MT |
1159 | } |
1160 | ||
1161 | ret = ipoib_cm_send_req(p->dev, p->id, p->qp, qpn, pathrec); | |
1162 | if (ret) { | |
1163 | ipoib_warn(priv, "failed to send cm req: %d\n", ret); | |
23536dfa | 1164 | goto err_modify_send; |
839fcaba MT |
1165 | } |
1166 | ||
5b095d98 | 1167 | ipoib_dbg(priv, "Request connection 0x%x for gid %pI6 qpn 0x%x\n", |
fcace2fe | 1168 | p->qp->qp_num, pathrec->dgid.raw, qpn); |
839fcaba MT |
1169 | |
1170 | return 0; | |
1171 | ||
23536dfa | 1172 | err_modify_send: |
839fcaba MT |
1173 | ib_destroy_cm_id(p->id); |
1174 | err_id: | |
1175 | p->id = NULL; | |
1176 | ib_destroy_qp(p->qp); | |
839fcaba MT |
1177 | err_qp: |
1178 | p->qp = NULL; | |
10313cbb | 1179 | vfree(p->tx_ring); |
839fcaba MT |
1180 | err_tx: |
1181 | return ret; | |
1182 | } | |
1183 | ||
1184 | static void ipoib_cm_tx_destroy(struct ipoib_cm_tx *p) | |
1185 | { | |
c1048aff | 1186 | struct ipoib_dev_priv *priv = ipoib_priv(p->dev); |
c4268778 | 1187 | struct ipoib_tx_buf *tx_req; |
1b524963 | 1188 | unsigned long begin; |
839fcaba MT |
1189 | |
1190 | ipoib_dbg(priv, "Destroy active connection 0x%x head 0x%x tail 0x%x\n", | |
1191 | p->qp ? p->qp->qp_num : 0, p->tx_head, p->tx_tail); | |
1192 | ||
1193 | if (p->id) | |
1194 | ib_destroy_cm_id(p->id); | |
1195 | ||
839fcaba | 1196 | if (p->tx_ring) { |
1b524963 MT |
1197 | /* Wait for all sends to complete */ |
1198 | begin = jiffies; | |
839fcaba | 1199 | while ((int) p->tx_tail - (int) p->tx_head < 0) { |
1b524963 MT |
1200 | if (time_after(jiffies, begin + 5 * HZ)) { |
1201 | ipoib_warn(priv, "timing out; %d sends not completed\n", | |
1202 | p->tx_head - p->tx_tail); | |
1203 | goto timeout; | |
1204 | } | |
1205 | ||
98e77d9f | 1206 | usleep_range(1000, 2000); |
839fcaba | 1207 | } |
1b524963 MT |
1208 | } |
1209 | ||
1210 | timeout: | |
839fcaba | 1211 | |
1b524963 MT |
1212 | while ((int) p->tx_tail - (int) p->tx_head < 0) { |
1213 | tx_req = &p->tx_ring[p->tx_tail & (ipoib_sendq_size - 1)]; | |
c4268778 | 1214 | ipoib_dma_unmap_tx(priv, tx_req); |
1b524963 MT |
1215 | dev_kfree_skb_any(tx_req->skb); |
1216 | ++p->tx_tail; | |
943c246e | 1217 | netif_tx_lock_bh(p->dev); |
1b524963 MT |
1218 | if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) && |
1219 | netif_queue_stopped(p->dev) && | |
1220 | test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) | |
1221 | netif_wake_queue(p->dev); | |
943c246e | 1222 | netif_tx_unlock_bh(p->dev); |
839fcaba MT |
1223 | } |
1224 | ||
1b524963 MT |
1225 | if (p->qp) |
1226 | ib_destroy_qp(p->qp); | |
1227 | ||
10313cbb | 1228 | vfree(p->tx_ring); |
839fcaba MT |
1229 | kfree(p); |
1230 | } | |
1231 | ||
1232 | static int ipoib_cm_tx_handler(struct ib_cm_id *cm_id, | |
1233 | struct ib_cm_event *event) | |
1234 | { | |
1235 | struct ipoib_cm_tx *tx = cm_id->context; | |
c1048aff | 1236 | struct ipoib_dev_priv *priv = ipoib_priv(tx->dev); |
839fcaba MT |
1237 | struct net_device *dev = priv->dev; |
1238 | struct ipoib_neigh *neigh; | |
943c246e | 1239 | unsigned long flags; |
839fcaba MT |
1240 | int ret; |
1241 | ||
1242 | switch (event->event) { | |
1243 | case IB_CM_DREQ_RECEIVED: | |
1244 | ipoib_dbg(priv, "DREQ received.\n"); | |
1245 | ib_send_cm_drep(cm_id, NULL, 0); | |
1246 | break; | |
1247 | case IB_CM_REP_RECEIVED: | |
1248 | ipoib_dbg(priv, "REP received.\n"); | |
1249 | ret = ipoib_cm_rep_handler(cm_id, event); | |
1250 | if (ret) | |
1251 | ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, | |
1252 | NULL, 0, NULL, 0); | |
1253 | break; | |
1254 | case IB_CM_REQ_ERROR: | |
1255 | case IB_CM_REJ_RECEIVED: | |
1256 | case IB_CM_TIMEWAIT_EXIT: | |
1257 | ipoib_dbg(priv, "CM error %d.\n", event->event); | |
943c246e RD |
1258 | netif_tx_lock_bh(dev); |
1259 | spin_lock_irqsave(&priv->lock, flags); | |
839fcaba MT |
1260 | neigh = tx->neigh; |
1261 | ||
1262 | if (neigh) { | |
1263 | neigh->cm = NULL; | |
b63b70d8 | 1264 | ipoib_neigh_free(neigh); |
839fcaba MT |
1265 | |
1266 | tx->neigh = NULL; | |
1267 | } | |
1268 | ||
1269 | if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) { | |
1270 | list_move(&tx->list, &priv->cm.reap_list); | |
0b39578b | 1271 | queue_work(priv->wq, &priv->cm.reap_task); |
839fcaba MT |
1272 | } |
1273 | ||
943c246e RD |
1274 | spin_unlock_irqrestore(&priv->lock, flags); |
1275 | netif_tx_unlock_bh(dev); | |
839fcaba MT |
1276 | break; |
1277 | default: | |
1278 | break; | |
1279 | } | |
1280 | ||
1281 | return 0; | |
1282 | } | |
1283 | ||
1284 | struct ipoib_cm_tx *ipoib_cm_create_tx(struct net_device *dev, struct ipoib_path *path, | |
1285 | struct ipoib_neigh *neigh) | |
1286 | { | |
c1048aff | 1287 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
839fcaba MT |
1288 | struct ipoib_cm_tx *tx; |
1289 | ||
1290 | tx = kzalloc(sizeof *tx, GFP_ATOMIC); | |
1291 | if (!tx) | |
1292 | return NULL; | |
1293 | ||
1294 | neigh->cm = tx; | |
1295 | tx->neigh = neigh; | |
1296 | tx->path = path; | |
1297 | tx->dev = dev; | |
1298 | list_add(&tx->list, &priv->cm.start_list); | |
1299 | set_bit(IPOIB_FLAG_INITIALIZED, &tx->flags); | |
0b39578b | 1300 | queue_work(priv->wq, &priv->cm.start_task); |
839fcaba MT |
1301 | return tx; |
1302 | } | |
1303 | ||
1304 | void ipoib_cm_destroy_tx(struct ipoib_cm_tx *tx) | |
1305 | { | |
c1048aff | 1306 | struct ipoib_dev_priv *priv = ipoib_priv(tx->dev); |
fa16ebed | 1307 | unsigned long flags; |
839fcaba | 1308 | if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &tx->flags)) { |
fa16ebed | 1309 | spin_lock_irqsave(&priv->lock, flags); |
839fcaba | 1310 | list_move(&tx->list, &priv->cm.reap_list); |
0b39578b | 1311 | queue_work(priv->wq, &priv->cm.reap_task); |
5b095d98 | 1312 | ipoib_dbg(priv, "Reap connection for gid %pI6\n", |
b63b70d8 | 1313 | tx->neigh->daddr + 4); |
839fcaba | 1314 | tx->neigh = NULL; |
fa16ebed | 1315 | spin_unlock_irqrestore(&priv->lock, flags); |
839fcaba MT |
1316 | } |
1317 | } | |
1318 | ||
546481c2 ES |
1319 | #define QPN_AND_OPTIONS_OFFSET 4 |
1320 | ||
839fcaba MT |
1321 | static void ipoib_cm_tx_start(struct work_struct *work) |
1322 | { | |
1323 | struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv, | |
1324 | cm.start_task); | |
1325 | struct net_device *dev = priv->dev; | |
1326 | struct ipoib_neigh *neigh; | |
1327 | struct ipoib_cm_tx *p; | |
1328 | unsigned long flags; | |
546481c2 | 1329 | struct ipoib_path *path; |
839fcaba MT |
1330 | int ret; |
1331 | ||
c2f8fc4e | 1332 | struct sa_path_rec pathrec; |
839fcaba MT |
1333 | u32 qpn; |
1334 | ||
943c246e RD |
1335 | netif_tx_lock_bh(dev); |
1336 | spin_lock_irqsave(&priv->lock, flags); | |
1337 | ||
839fcaba MT |
1338 | while (!list_empty(&priv->cm.start_list)) { |
1339 | p = list_entry(priv->cm.start_list.next, typeof(*p), list); | |
1340 | list_del_init(&p->list); | |
1341 | neigh = p->neigh; | |
546481c2 | 1342 | |
b63b70d8 | 1343 | qpn = IPOIB_QPN(neigh->daddr); |
546481c2 ES |
1344 | /* |
1345 | * As long as the search is with these 2 locks, | |
1346 | * path existence indicates its validity. | |
1347 | */ | |
1348 | path = __path_find(dev, neigh->daddr + QPN_AND_OPTIONS_OFFSET); | |
1349 | if (!path) { | |
1350 | pr_info("%s ignore not valid path %pI6\n", | |
1351 | __func__, | |
1352 | neigh->daddr + QPN_AND_OPTIONS_OFFSET); | |
1353 | goto free_neigh; | |
1354 | } | |
839fcaba | 1355 | memcpy(&pathrec, &p->path->pathrec, sizeof pathrec); |
943c246e RD |
1356 | |
1357 | spin_unlock_irqrestore(&priv->lock, flags); | |
1358 | netif_tx_unlock_bh(dev); | |
1359 | ||
839fcaba | 1360 | ret = ipoib_cm_tx_init(p, qpn, &pathrec); |
943c246e RD |
1361 | |
1362 | netif_tx_lock_bh(dev); | |
1363 | spin_lock_irqsave(&priv->lock, flags); | |
1364 | ||
839fcaba | 1365 | if (ret) { |
546481c2 | 1366 | free_neigh: |
839fcaba MT |
1367 | neigh = p->neigh; |
1368 | if (neigh) { | |
1369 | neigh->cm = NULL; | |
b63b70d8 | 1370 | ipoib_neigh_free(neigh); |
839fcaba MT |
1371 | } |
1372 | list_del(&p->list); | |
1373 | kfree(p); | |
1374 | } | |
1375 | } | |
943c246e RD |
1376 | |
1377 | spin_unlock_irqrestore(&priv->lock, flags); | |
1378 | netif_tx_unlock_bh(dev); | |
839fcaba MT |
1379 | } |
1380 | ||
1381 | static void ipoib_cm_tx_reap(struct work_struct *work) | |
1382 | { | |
1383 | struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv, | |
1384 | cm.reap_task); | |
943c246e | 1385 | struct net_device *dev = priv->dev; |
839fcaba | 1386 | struct ipoib_cm_tx *p; |
943c246e RD |
1387 | unsigned long flags; |
1388 | ||
1389 | netif_tx_lock_bh(dev); | |
1390 | spin_lock_irqsave(&priv->lock, flags); | |
839fcaba | 1391 | |
839fcaba MT |
1392 | while (!list_empty(&priv->cm.reap_list)) { |
1393 | p = list_entry(priv->cm.reap_list.next, typeof(*p), list); | |
27d41d29 | 1394 | list_del_init(&p->list); |
943c246e RD |
1395 | spin_unlock_irqrestore(&priv->lock, flags); |
1396 | netif_tx_unlock_bh(dev); | |
839fcaba | 1397 | ipoib_cm_tx_destroy(p); |
943c246e RD |
1398 | netif_tx_lock_bh(dev); |
1399 | spin_lock_irqsave(&priv->lock, flags); | |
839fcaba | 1400 | } |
943c246e RD |
1401 | |
1402 | spin_unlock_irqrestore(&priv->lock, flags); | |
1403 | netif_tx_unlock_bh(dev); | |
839fcaba MT |
1404 | } |
1405 | ||
1406 | static void ipoib_cm_skb_reap(struct work_struct *work) | |
1407 | { | |
1408 | struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv, | |
1409 | cm.skb_task); | |
943c246e | 1410 | struct net_device *dev = priv->dev; |
839fcaba | 1411 | struct sk_buff *skb; |
943c246e | 1412 | unsigned long flags; |
839fcaba MT |
1413 | unsigned mtu = priv->mcast_mtu; |
1414 | ||
943c246e RD |
1415 | netif_tx_lock_bh(dev); |
1416 | spin_lock_irqsave(&priv->lock, flags); | |
1417 | ||
839fcaba | 1418 | while ((skb = skb_dequeue(&priv->cm.skb_queue))) { |
943c246e RD |
1419 | spin_unlock_irqrestore(&priv->lock, flags); |
1420 | netif_tx_unlock_bh(dev); | |
1421 | ||
839fcaba MT |
1422 | if (skb->protocol == htons(ETH_P_IP)) |
1423 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu)); | |
d90f9b35 | 1424 | #if IS_ENABLED(CONFIG_IPV6) |
839fcaba | 1425 | else if (skb->protocol == htons(ETH_P_IPV6)) |
3ffe533c | 1426 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
839fcaba MT |
1427 | #endif |
1428 | dev_kfree_skb_any(skb); | |
943c246e RD |
1429 | |
1430 | netif_tx_lock_bh(dev); | |
1431 | spin_lock_irqsave(&priv->lock, flags); | |
839fcaba | 1432 | } |
943c246e RD |
1433 | |
1434 | spin_unlock_irqrestore(&priv->lock, flags); | |
1435 | netif_tx_unlock_bh(dev); | |
839fcaba MT |
1436 | } |
1437 | ||
2337f809 | 1438 | void ipoib_cm_skb_too_long(struct net_device *dev, struct sk_buff *skb, |
839fcaba MT |
1439 | unsigned int mtu) |
1440 | { | |
c1048aff | 1441 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
839fcaba MT |
1442 | int e = skb_queue_empty(&priv->cm.skb_queue); |
1443 | ||
adf30907 | 1444 | if (skb_dst(skb)) |
6700c270 | 1445 | skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu); |
839fcaba MT |
1446 | |
1447 | skb_queue_tail(&priv->cm.skb_queue, skb); | |
1448 | if (e) | |
0b39578b | 1449 | queue_work(priv->wq, &priv->cm.skb_task); |
839fcaba MT |
1450 | } |
1451 | ||
518b1646 MT |
1452 | static void ipoib_cm_rx_reap(struct work_struct *work) |
1453 | { | |
efcd9971 RD |
1454 | ipoib_cm_free_rx_reap_list(container_of(work, struct ipoib_dev_priv, |
1455 | cm.rx_reap_task)->dev); | |
518b1646 MT |
1456 | } |
1457 | ||
839fcaba MT |
1458 | static void ipoib_cm_stale_task(struct work_struct *work) |
1459 | { | |
1460 | struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv, | |
1461 | cm.stale_task.work); | |
1462 | struct ipoib_cm_rx *p; | |
518b1646 | 1463 | int ret; |
839fcaba | 1464 | |
37aebbde | 1465 | spin_lock_irq(&priv->lock); |
839fcaba | 1466 | while (!list_empty(&priv->cm.passive_ids)) { |
518b1646 | 1467 | /* List is sorted by LRU, start from tail, |
839fcaba MT |
1468 | * stop when we see a recently used entry */ |
1469 | p = list_entry(priv->cm.passive_ids.prev, typeof(*p), list); | |
60a596da | 1470 | if (time_before_eq(jiffies, p->jiffies + IPOIB_CM_RX_TIMEOUT)) |
839fcaba | 1471 | break; |
518b1646 MT |
1472 | list_move(&p->list, &priv->cm.rx_error_list); |
1473 | p->state = IPOIB_CM_RX_ERROR; | |
37aebbde | 1474 | spin_unlock_irq(&priv->lock); |
518b1646 MT |
1475 | ret = ib_modify_qp(p->qp, &ipoib_cm_err_attr, IB_QP_STATE); |
1476 | if (ret) | |
1477 | ipoib_warn(priv, "unable to move qp to error state: %d\n", ret); | |
37aebbde | 1478 | spin_lock_irq(&priv->lock); |
839fcaba | 1479 | } |
7c5b9ef8 MT |
1480 | |
1481 | if (!list_empty(&priv->cm.passive_ids)) | |
0b39578b | 1482 | queue_delayed_work(priv->wq, |
7c5b9ef8 | 1483 | &priv->cm.stale_task, IPOIB_CM_RX_DELAY); |
37aebbde | 1484 | spin_unlock_irq(&priv->lock); |
839fcaba MT |
1485 | } |
1486 | ||
2337f809 | 1487 | static ssize_t show_mode(struct device *d, struct device_attribute *attr, |
839fcaba MT |
1488 | char *buf) |
1489 | { | |
c1048aff ES |
1490 | struct net_device *dev = to_net_dev(d); |
1491 | struct ipoib_dev_priv *priv = ipoib_priv(dev); | |
839fcaba MT |
1492 | |
1493 | if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags)) | |
1494 | return sprintf(buf, "connected\n"); | |
1495 | else | |
1496 | return sprintf(buf, "datagram\n"); | |
1497 | } | |
1498 | ||
862096a8 OG |
1499 | static ssize_t set_mode(struct device *d, struct device_attribute *attr, |
1500 | const char *buf, size_t count) | |
1501 | { | |
1502 | struct net_device *dev = to_net_dev(d); | |
1503 | int ret; | |
c1048aff | 1504 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
198b12f7 ES |
1505 | |
1506 | if (test_bit(IPOIB_FLAG_GOING_DOWN, &priv->flags)) | |
1507 | return -EPERM; | |
862096a8 OG |
1508 | |
1509 | if (!rtnl_trylock()) | |
1510 | return restart_syscall(); | |
1511 | ||
1512 | ret = ipoib_set_mode(dev, buf); | |
1513 | ||
0a0007f2 FD |
1514 | /* The assumption is that the function ipoib_set_mode returned |
1515 | * with the rtnl held by it, if not the value -EBUSY returned, | |
1516 | * then no need to rtnl_unlock | |
1517 | */ | |
1518 | if (ret != -EBUSY) | |
1519 | rtnl_unlock(); | |
862096a8 | 1520 | |
0a0007f2 | 1521 | return (!ret || ret == -EBUSY) ? count : ret; |
862096a8 OG |
1522 | } |
1523 | ||
551fd612 | 1524 | static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO, show_mode, set_mode); |
839fcaba MT |
1525 | |
1526 | int ipoib_cm_add_mode_attr(struct net_device *dev) | |
1527 | { | |
1528 | return device_create_file(&dev->dev, &dev_attr_mode); | |
1529 | } | |
1530 | ||
586a6934 | 1531 | static void ipoib_cm_create_srq(struct net_device *dev, int max_sge) |
839fcaba | 1532 | { |
c1048aff | 1533 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
839fcaba | 1534 | struct ib_srq_init_attr srq_init_attr = { |
96104eda | 1535 | .srq_type = IB_SRQT_BASIC, |
839fcaba MT |
1536 | .attr = { |
1537 | .max_wr = ipoib_recvq_size, | |
586a6934 | 1538 | .max_sge = max_sge |
839fcaba MT |
1539 | } |
1540 | }; | |
7b3687df RD |
1541 | |
1542 | priv->cm.srq = ib_create_srq(priv->pd, &srq_init_attr); | |
1543 | if (IS_ERR(priv->cm.srq)) { | |
68e995a2 PS |
1544 | if (PTR_ERR(priv->cm.srq) != -ENOSYS) |
1545 | printk(KERN_WARNING "%s: failed to allocate SRQ, error %ld\n", | |
1546 | priv->ca->name, PTR_ERR(priv->cm.srq)); | |
7b3687df | 1547 | priv->cm.srq = NULL; |
68e995a2 | 1548 | return; |
7b3687df RD |
1549 | } |
1550 | ||
948579cd | 1551 | priv->cm.srq_ring = vzalloc(ipoib_recvq_size * sizeof *priv->cm.srq_ring); |
7b3687df | 1552 | if (!priv->cm.srq_ring) { |
7b3687df RD |
1553 | ib_destroy_srq(priv->cm.srq); |
1554 | priv->cm.srq = NULL; | |
b1404069 | 1555 | return; |
7b3687df | 1556 | } |
b1404069 | 1557 | |
7b3687df RD |
1558 | } |
1559 | ||
1560 | int ipoib_cm_dev_init(struct net_device *dev) | |
1561 | { | |
c1048aff | 1562 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
4a061b28 | 1563 | int max_srq_sge, i; |
839fcaba MT |
1564 | |
1565 | INIT_LIST_HEAD(&priv->cm.passive_ids); | |
1566 | INIT_LIST_HEAD(&priv->cm.reap_list); | |
1567 | INIT_LIST_HEAD(&priv->cm.start_list); | |
518b1646 MT |
1568 | INIT_LIST_HEAD(&priv->cm.rx_error_list); |
1569 | INIT_LIST_HEAD(&priv->cm.rx_flush_list); | |
1570 | INIT_LIST_HEAD(&priv->cm.rx_drain_list); | |
1571 | INIT_LIST_HEAD(&priv->cm.rx_reap_list); | |
839fcaba MT |
1572 | INIT_WORK(&priv->cm.start_task, ipoib_cm_tx_start); |
1573 | INIT_WORK(&priv->cm.reap_task, ipoib_cm_tx_reap); | |
1574 | INIT_WORK(&priv->cm.skb_task, ipoib_cm_skb_reap); | |
518b1646 | 1575 | INIT_WORK(&priv->cm.rx_reap_task, ipoib_cm_rx_reap); |
839fcaba MT |
1576 | INIT_DELAYED_WORK(&priv->cm.stale_task, ipoib_cm_stale_task); |
1577 | ||
1578 | skb_queue_head_init(&priv->cm.skb_queue); | |
1579 | ||
4a061b28 | 1580 | ipoib_dbg(priv, "max_srq_sge=%d\n", priv->ca->attrs.max_srq_sge); |
586a6934 | 1581 | |
4a061b28 OG |
1582 | max_srq_sge = min_t(int, IPOIB_CM_RX_SG, priv->ca->attrs.max_srq_sge); |
1583 | ipoib_cm_create_srq(dev, max_srq_sge); | |
586a6934 | 1584 | if (ipoib_cm_has_srq(dev)) { |
4a061b28 OG |
1585 | priv->cm.max_cm_mtu = max_srq_sge * PAGE_SIZE - 0x10; |
1586 | priv->cm.num_frags = max_srq_sge; | |
586a6934 PS |
1587 | ipoib_dbg(priv, "max_cm_mtu = 0x%x, num_frags=%d\n", |
1588 | priv->cm.max_cm_mtu, priv->cm.num_frags); | |
1589 | } else { | |
1590 | priv->cm.max_cm_mtu = IPOIB_CM_MTU; | |
1591 | priv->cm.num_frags = IPOIB_CM_RX_SG; | |
1592 | } | |
1593 | ||
a7d834c4 | 1594 | ipoib_cm_init_rx_wr(dev, &priv->cm.rx_wr, priv->cm.rx_sge); |
68e995a2 PS |
1595 | |
1596 | if (ipoib_cm_has_srq(dev)) { | |
1597 | for (i = 0; i < ipoib_recvq_size; ++i) { | |
1598 | if (!ipoib_cm_alloc_rx_skb(dev, priv->cm.srq_ring, i, | |
586a6934 | 1599 | priv->cm.num_frags - 1, |
22252b4e TA |
1600 | priv->cm.srq_ring[i].mapping, |
1601 | GFP_KERNEL)) { | |
68e995a2 PS |
1602 | ipoib_warn(priv, "failed to allocate " |
1603 | "receive buffer %d\n", i); | |
1604 | ipoib_cm_dev_cleanup(dev); | |
1605 | return -ENOMEM; | |
1606 | } | |
7b3687df | 1607 | |
68e995a2 PS |
1608 | if (ipoib_cm_post_receive_srq(dev, i)) { |
1609 | ipoib_warn(priv, "ipoib_cm_post_receive_srq " | |
1610 | "failed for buf %d\n", i); | |
1611 | ipoib_cm_dev_cleanup(dev); | |
1612 | return -EIO; | |
1613 | } | |
839fcaba MT |
1614 | } |
1615 | } | |
1616 | ||
1617 | priv->dev->dev_addr[0] = IPOIB_FLAGS_RC; | |
1618 | return 0; | |
1619 | } | |
1620 | ||
1621 | void ipoib_cm_dev_cleanup(struct net_device *dev) | |
1622 | { | |
c1048aff | 1623 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
1efb6144 | 1624 | int ret; |
839fcaba MT |
1625 | |
1626 | if (!priv->cm.srq) | |
1627 | return; | |
1628 | ||
1629 | ipoib_dbg(priv, "Cleanup ipoib connected mode.\n"); | |
1630 | ||
1631 | ret = ib_destroy_srq(priv->cm.srq); | |
1632 | if (ret) | |
1633 | ipoib_warn(priv, "ib_destroy_srq failed: %d\n", ret); | |
1634 | ||
1635 | priv->cm.srq = NULL; | |
1636 | if (!priv->cm.srq_ring) | |
1637 | return; | |
1efb6144 RD |
1638 | |
1639 | ipoib_cm_free_rx_ring(dev, priv->cm.srq_ring); | |
839fcaba MT |
1640 | priv->cm.srq_ring = NULL; |
1641 | } |