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