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